-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
672 lines (549 loc) · 19.6 KB
/
Copy pathscript.js
File metadata and controls
672 lines (549 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
const cells = new GPU2D(document.getElementById("cells"), {
antialias: false,
preserveDrawingBuffer: true
})
let particleCount = JSON.parse(localStorage.getItem("particleCount")) || 200000
const ui = {
trail: {
followMouse: false,
color: 0,
colorOffset: 0,
},
particles: {
randomness: 1,
sensorDistance: 0.02,
moveSpeed: 0.001
},
diffuse: {
decayRate: 0.1
},
angles: {
turnAngle: 45,
sensorAngle: 45
},
other: {
gifLength: 3,
particleCount,
canvasSize: cells.canvas.width
},
/** @type { Record<string, UISlider> } */
sliders: null
}
function uiSetup() {
function glProxy(obj, prog) {
const proxy = new Proxy(obj, {
set(target, key, value) {
const { gl } = cells
target[key] = value
gl.useProgram(prog.program)
if (typeof target[key] === "boolean") {
gl.uniform1i(prog.uniforms[key], target[key])
} else {
gl.uniform1f(prog.uniforms[key], target[key])
}
updateUrl()
return true
}
})
Object.keys(obj).forEach(key => {
proxy[key] = obj[key]
})
return proxy
}
ui.trail = glProxy(ui.trail, trail)
ui.particles = glProxy(ui.particles, particles)
ui.diffuse = glProxy(ui.diffuse, diffuse)
ui.angles = new Proxy(ui.angles, {
set(target, key, value) {
const { gl } = cells
target[key] = value
if (key === "turnAngle") {
gl.useProgram(particles.program)
gl.uniform1f(particles.uniforms.turnSpeed, value / 360)
} else if (key === "sensorAngle") {
gl.useProgram(particles.program)
const sin = Math.sin
const cos = Math.cos
const radians = value / 180 * Math.PI
gl.uniformMatrix2fv(particles.uniforms.rotation, false, [
cos(radians), -sin(radians),
sin(radians), cos(radians)
])
gl.uniformMatrix2fv(particles.uniforms.invRotation, false, [
cos(-radians), -sin(-radians),
sin(-radians), cos(-radians)
])
}
updateUrl()
return true
}
})
Object.keys(ui.angles).forEach(key => ui.angles[key] = ui.angles[key])
cells.canvas.addEventListener("mousemove", event => {
if (!ui.trail.followMouse) { return }
const { gl, canvas } = cells
const rect = canvas.getBoundingClientRect()
const x = (event.clientX - rect.left) / rect.width
const y = 1 - (event.clientY - rect.top) / rect.height
gl.useProgram(trail.program)
gl.uniform2f(trail.uniforms.mousePos, x, y)
})
document.getElementById("followMouse").addEventListener("change", event => {
ui.trail.followMouse = event.target.checked
})
ui.sliders = {
randomness: new UISlider("randomness", ui.particles),
sensorDistance: new UISlider("sensorDistance", ui.particles),
moveSpeed: new UISlider("moveSpeed", ui.particles),
decayRate: new UISlider("decayRate", ui.diffuse),
sensorAngle: new UISlider("sensorAngle", ui.angles),
turnAngle: new UISlider("turnAngle", ui.angles),
color: new UISlider("color", ui.trail),
colorOffset: new UISlider("colorOffset", ui.trail),
gifLength: new UISlider("gifLength", ui.other),
particleCount: new UISlider("particleCount", ui.other),
canvasSize: new UISlider("canvasSize", ui.other)
}
document.getElementById("restart").addEventListener("click", () => {
cells.canvas.width = ui.other.canvasSize
cells.canvas.height = ui.other.canvasSize
particleCount = ui.other.particleCount
resetTrailAndParticles()
updateUrl()
localStorage.setItem("particleCount", particleCount)
})
if (ui.other.canvasSize !== cells.canvas.width) {
cells.canvas.width = ui.other.canvasSize
cells.canvas.height = ui.other.canvasSize
resetTrailAndParticles()
}
const downloadEl = document.getElementById("download")
document.getElementById("screenshot").addEventListener("click", event => {
cells.canvas.toBlob(blob => {
const url = URL.createObjectURL(blob)
downloadEl.href = url
downloadEl.download = "physarum.jpg"
downloadEl.click()
}, "image/jpeg")
})
}
class UISlider {
constructor(name, obj, transform) {
const el = document.getElementById(name)
el.addEventListener("input", event => {
let val = parseFloat(el.value)
if (transform) { val = transform(val)}
obj[name] = val
this.update(true)
})
this.name = name
this.obj = obj
this.el = el
this.update()
}
update(fromInput = false) {
const { name, obj, el } = this
const displayEl = el.parentElement.querySelector(".range-display")
if (displayEl) {
const val = obj[name]
let valStr = val.toLocaleString()
if (val % 1 !== 0) {
const leadingZeroes = Math.ceil(-Math.min(Math.log10(Math.abs(val % 1)), 0))
valStr = val.toLocaleString(undefined, { minimumFractionDigits: leadingZeroes, maximumFractionDigits: leadingZeroes + 2 })
}
displayEl.innerHTML = `(${valStr})`
}
if (!fromInput) {
el.value = obj[name]
}
}
}
function randomizeParameters() {
ui.particles.sensorDistance = Math.floor(Math.random() * 100) / 1000 + 0.001
ui.angles.sensorAngle = Math.floor(Math.random() * 85) + 5
ui.angles.turnAngle = Math.floor(Math.random() * 85) + 5
ui.trail.color = Math.floor(Math.random() * 3)
ui.trail.colorOffset = Math.random()
if (ui.sliders) {
ui.sliders.sensorDistance.update()
ui.sliders.sensorAngle.update()
ui.sliders.turnAngle.update()
ui.sliders.color.update()
ui.sliders.colorOffset.update()
}
}
randomizeParameters()
document.getElementById("randomize").addEventListener("click", randomizeParameters)
function exportParameters() {
const parameters = {}
Object.keys(ui).forEach(key => {
if (key == "sliders") { return }
const params = ui[key]
Object.keys(params).forEach(key => {
parameters[key] = params[key]
})
})
delete parameters.particleCount
delete parameters.gifLength
return parameters
}
document.getElementById("copy-parameters").addEventListener("click", event => {
const el = document.getElementById("copy-url")
const url = location.protocol + "//" + location.host + location.pathname
const params = exportParameters()
const paramValues = []
Object.keys(params).forEach(key => {
if (key == "canvasSize") { return }
paramValues.push(`${key}=${params[key]}`)
})
el.value = url + "#" + paramValues.join("&")
el.select()
document.execCommand("copy")
})
function updateUrl() {
const params = exportParameters()
const paramValues = []
Object.keys(params).forEach(key => {
paramValues.push(`${key}=${params[key]}`)
})
location.hash = paramValues.join("&")
}
let paramObjects
function importParameters(parameters) {
if (!paramObjects) {
paramObjects = {}
Object.keys(ui).forEach(key => {
if (key == "sliders") { return }
const params = ui[key]
Object.keys(params).forEach(key => {
paramObjects[key] = params
})
})
}
Object.keys(parameters).forEach(key => {
const params = paramObjects[key]
if (params) {
params[key] = parameters[key]
}
})
if (!ui.sliders) return
Object.keys(ui.sliders).forEach(key => {
ui.sliders[key].update()
})
}
if (location.hash) {
const hash = location.hash
let params = {}
try {
const json = decodeURIComponent(hash.slice(1))
params = JSON.parse(json)
} catch (err) {
hash.slice(1).split("&").forEach(param => {
const [key, value] = param.split("=")
try {
params[key] = JSON.parse(value)
} catch (err) {
params[key] = value
}
})
}
if (params.sensorDistance > 1) {
params.sensorDistance /= 2048
}
importParameters(params)
}
let initialSetup = true
async function setup() {
// await Promise.all([
// createParticleUpdateProgram(),
// createTrailUpdateProgram(),
// createTrailDiffuseProgram()
// ])
await createParticleUpdateProgram()
await createTrailUpdateProgram()
await createTrailDiffuseProgram()
uiSetup()
if (initialSetup) {
initialSetup = false
drawLoop()
}
}
window.addEventListener("load", setup)
const particles = {
program: null,
framebuffer: null,
texture: null,
texSize: {
width: 0,
height: 0
},
uniforms: {}
}
async function createParticleUpdateProgram() {
const { gl, canvas } = cells
const pixels = particleCount * 2
const width = Math.min(pixels, canvas.width)
const height = Math.ceil(pixels / canvas.width)
particles.texSize = { width, height }
const updateCode = await fetch("shaders/particleupdate.frag.cpp").then(res => res.text())
const program = cells.create2dProgram(updateCode, false)
const framebuffer = gl.createFramebuffer()
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)
const newParticleTex = gl.createTexture()
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_2D, newParticleTex)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null)
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, newParticleTex, 0)
const initial = new Uint8Array(4 * width * height)
for (let i = 0; i < particleCount; i++) {
const offset = i * 8
const x = Math.floor(Math.random() * 64) + 96
const y = Math.floor(Math.random() * 64) + 96
const dMiddleX = 128 - x
const dMiddleY = 128 - y
let direction = Math.random() // Math.atan2(dMiddleY, dMiddleX) / Math.PI
// if (direction < 0.0) direction += 1
initial[offset] = x
initial[offset + 1] = Math.floor(Math.random() * 256)
initial[offset + 2] = y
initial[offset + 3] = Math.floor(Math.random() * 256)
initial[offset + 4] = direction * 256 // Math.floor(Math.random() * 256)
initial[offset + 5] = direction / 256 % 256 // Math.floor(Math.random() * 256)
}
const particleTex = gl.createTexture()
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, particleTex)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, initial)
cells.useProgram(program)
cells.setUniformValue("particles", (gl, p) => gl.uniform1i(p, 1))
cells.setUniformValue("particleCount", (gl, p) => gl.uniform1f(p, particleCount))
cells.setUniformValue("trails", (gl, p) => gl.uniform1i(p, 2))
particles.uniforms.randomness = gl.getUniformLocation(program, "randomness")
particles.uniforms.sensorDistance = gl.getUniformLocation(program, "sensorDistance")
particles.uniforms.moveSpeed = gl.getUniformLocation(program, "moveSpeed")
particles.uniforms.turnSpeed = gl.getUniformLocation(program, "turnSpeed")
particles.uniforms.rotation = gl.getUniformLocation(program, "rotation")
particles.uniforms.invRotation = gl.getUniformLocation(program, "invRotation")
particles.program = program
particles.framebuffer = framebuffer
particles.texture = particleTex
}
function updateParticles() {
const { gl } = cells
const { width, height } = particles.texSize
cells.useProgram(particles.program)
const randomSeeds = [700_000 + Math.random() * 1000, 700_000 + Math.random() * 1000, Math.random()]
cells.setUniformValue("randomSeed", (gl, p) => gl.uniform3fv(p, randomSeeds))
gl.bindFramebuffer(gl.FRAMEBUFFER, particles.framebuffer)
gl.viewport(0, 0, width, height)
cells.draw(false)
gl.activeTexture(gl.TEXTURE1)
gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0)
}
const trail = {
program: null,
framebuffer: null,
texture: null,
vertexBuffer: null,
uniforms: {
mousePos: null,
followMouse: null
}
}
async function createTrailUpdateProgram() {
const { gl, canvas } = cells
const program = gl.createProgram()
const vs = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vs, await fetch("shaders/trail.vert.cpp").then(res => res.text()))
gl.attachShader(program, vs)
gl.compileShader(vs)
const fs = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fs, await fetch("shaders/trail.frag.cpp").then(res => res.text()))
gl.attachShader(program, fs)
gl.compileShader(fs)
gl.linkProgram(program)
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error('Failed to link trail program: ' + gl.getProgramInfoLog(program) + '\n\nVertex Shader: ' + gl.getShaderInfoLog(vs) + '\n\nFragment Shader: ' + gl.getShaderInfoLog(fs))
}
cells.useProgram(program)
cells.setUniformValue("particles", (gl, p) => gl.uniform1i(p, 1))
cells.setUniformValue("particleCount", (gl, p) => gl.uniform1f(p, particleCount))
cells.setUniformValue("uViewSize", (gl, p) => gl.uniform2f(p, canvas.width, canvas.height))
trail.uniforms.mousePos = gl.getUniformLocation(program, "mousePos")
trail.uniforms.followMouse = gl.getUniformLocation(program, "followMouse")
trail.uniforms.color = gl.getUniformLocation(program, "color")
trail.uniforms.colorOffset = gl.getUniformLocation(program, "colorOffset")
const framebuffer = gl.createFramebuffer()
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)
const trailTex = gl.createTexture()
gl.activeTexture(gl.TEXTURE2)
gl.bindTexture(gl.TEXTURE_2D, trailTex)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, particleCount, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null)
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, trailTex, 0)
const vertexArray = new Float32Array(particleCount)
for (let i = 0; i < particleCount; i++) {
vertexArray[i] = i
}
const vertexBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer)
gl.bufferData(gl.ARRAY_BUFFER, vertexArray, gl.STATIC_DRAW)
const aVertexIndex = gl.getAttribLocation(program, "aVertexIndex")
gl.enableVertexAttribArray(aVertexIndex)
gl.vertexAttribPointer(aVertexIndex, 1, gl.FLOAT, false, 0, 0)
trail.program = program
trail.framebuffer = framebuffer
trail.texture = trailTex
trail.vertexBuffer = vertexBuffer
}
function drawTrail() {
const { gl, canvas } = cells
gl.useProgram(trail.program)
gl.bindBuffer(gl.ARRAY_BUFFER, trail.vertexBuffer)
const aVertexIndex = gl.getAttribLocation(trail.program, "aVertexIndex")
gl.enableVertexAttribArray(aVertexIndex)
gl.vertexAttribPointer(aVertexIndex, 1, gl.FLOAT, false, 0, 0)
gl.bindFramebuffer(gl.FRAMEBUFFER, trail.framebuffer)
gl.viewport(0, 0, canvas.width, canvas.height)
gl.drawArrays(gl.POINTS, 0, particleCount)
}
const diffuse = {
program: null,
framebuffer: null,
uniforms: {}
}
async function createTrailDiffuseProgram() {
const { gl } = cells
const diffuseCode = await fetch("shaders/diffuse.frag.cpp").then(res => res.text())
const program = cells.create2dProgram(diffuseCode, false)
cells.useProgram(program)
cells.setUniformValue("trails", (gl, p) => gl.uniform1i(p, 2))
diffuse.uniforms.decayRate = gl.getUniformLocation(program, "decayRate")
const framebuffer = gl.createFramebuffer()
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer)
const diffuseTex = gl.createTexture()
gl.activeTexture(gl.TEXTURE3)
gl.bindTexture(gl.TEXTURE_2D, diffuseTex)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, particleCount, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, null)
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, diffuseTex, 0)
diffuse.program = program
diffuse.framebuffer = framebuffer
}
function diffuseTrails() {
const { gl, canvas } = cells
const { width, height } = canvas
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
cells.useProgram(diffuse.program)
cells.draw()
gl.activeTexture(gl.TEXTURE2)
gl.copyTexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 0, 0, width, height, 0)
}
let gif, gifPromise
let remainingGifFrames = -1
function recordGif(frames) {
const { width, height } = cells.canvas
gif = new GIF({
width: width,
height: height,
quality: 10,
workerScript: "gifjs/gif.worker.js",
background: "#000"
})
remainingGifFrames = frames
gifButton.disabled = true
gifButton.innerText = "Recording..."
}
const gifButton = document.getElementById("record-gif")
gifButton.addEventListener("click", () => recordGif(ui.other.gifLength * 20))
function drawLoop() {
updateParticles()
drawTrail()
diffuseTrails()
if (remainingGifFrames > 0) {
remainingGifFrames -= 1
const promise = new Promise(resolve => {
cells.canvas.toBlob(blob => {
const img = new Image(512, 512)
img.src = URL.createObjectURL(blob)
img.onload = () => {
gif.addFrame(img, { delay: 50 })
resolve()
}
}, "image/jpeg")
})
if (gifPromise) {
gifPromise = gifPromise.then(() => promise)
} else {
gifPromise = promise
}
} else if (remainingGifFrames === 0) {
gif.on("finished", blob => {
console.log(blob)
const url = URL.createObjectURL(blob)
const downloadEl = document.getElementById("download")
downloadEl.href = url
downloadEl.download = "physarum.gif"
downloadEl.click()
gifButton.disabled = false
gifButton.innerText = "Record GIF"
})
gifPromise.then(() => {
gifButton.innerText = "Processing..."
gif.render()
})
remainingGifFrames = -1
}
requestAnimationFrame(drawLoop)
}
function resetTrailAndParticles() {
const { gl, canvas } = cells
cells.useProgram(trail.program)
cells.setUniformValue("uViewSize", (gl, p) => gl.uniform2f(p, canvas.width, canvas.height))
cells.useProgram(particles.program)
cells.setUniformValue("particleCount", (gl, p) => gl.uniform1f(p, particleCount))
gl.bindFramebuffer(gl.FRAMEBUFFER, trail.framebuffer)
gl.viewport(0, 0, canvas.width, canvas.height)
gl.clearColor(0, 0, 0, 0)
gl.clear(gl.COLOR_BUFFER_BIT)
const vertexArray = new Float32Array(particleCount)
for (let i = 0; i < particleCount; i++) {
vertexArray[i] = i
}
gl.bindBuffer(gl.ARRAY_BUFFER, trail.vertexBuffer)
gl.bufferData(gl.ARRAY_BUFFER, vertexArray, gl.STATIC_DRAW)
const pixels = particleCount * 2
const width = Math.min(pixels, canvas.width)
const height = Math.ceil(pixels / canvas.width)
particles.texSize = { width, height }
const initial = new Uint8Array(4 * width * height)
for (let i = 0; i < particleCount; i++) {
const offset = i * 8
const x = Math.floor(Math.random() * 64) + 96
const y = Math.floor(Math.random() * 64) + 96
let direction = Math.random()
initial[offset] = x
initial[offset + 1] = Math.floor(Math.random() * 256)
initial[offset + 2] = y
initial[offset + 3] = Math.floor(Math.random() * 256)
initial[offset + 4] = direction * 256
initial[offset + 5] = direction / 256 % 256
}
gl.activeTexture(gl.TEXTURE1)
gl.bindTexture(gl.TEXTURE_2D, particles.texture)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, initial)
}