@kassoulet/cathode 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/LICENSE +25 -0
  3. package/README.md +134 -2
  4. package/assets/crt/CRT Masks/Aperture Grille.png +0 -0
  5. package/assets/crt/CRT Masks/SX-64.png +0 -0
  6. package/assets/crt/CRT Masks/Shadow Mask EDP.png +0 -0
  7. package/assets/crt/CRT Masks/Shadow Mask.png +0 -0
  8. package/assets/crt/CRT Masks/Slot Mask.png +0 -0
  9. package/assets/crt/LICENSE +121 -0
  10. package/assets/crt/Presets/C64 Ultimate HDMI.yml +28 -0
  11. package/assets/crt/Presets/Cheap and broken TV.yml +27 -0
  12. package/assets/crt/Presets/Default.yml +28 -0
  13. package/assets/crt/Presets/SX-64.yml +27 -0
  14. package/assets/crt/Presets/VICE.yml +28 -0
  15. package/assets/crt/README.md +3 -0
  16. package/assets/crt/Shaders/2d-texture.glsl +4 -0
  17. package/assets/crt/Shaders/crt-adjacent-excitation.glsl +29 -0
  18. package/assets/crt/Shaders/crt-bezel-reflections.glsl +101 -0
  19. package/assets/crt/Shaders/crt-calc-bloom.glsl +24 -0
  20. package/assets/crt/Shaders/crt-curved.glsl +225 -0
  21. package/assets/crt/Shaders/crt-halation.glsl +35 -0
  22. package/assets/crt/Shaders/crt-simulation.glsl +175 -0
  23. package/assets/crt/Shaders/decoder-ntsc.glsl +105 -0
  24. package/assets/crt/Shaders/decoder-pal.glsl +83 -0
  25. package/assets/crt/Shaders/decoder-yiq.glsl +25 -0
  26. package/assets/crt/Shaders/decoder-yuv.glsl +25 -0
  27. package/assets/crt/Shaders/encoder-c64.glsl +26 -0
  28. package/assets/crt/Shaders/encoder-raw.glsl +19 -0
  29. package/assets/crt/Shaders/includes/colorGrade.glsl +25 -0
  30. package/assets/crt/Shaders/includes/colorSpaces.glsl +63 -0
  31. package/assets/crt/Shaders/includes/common.glsl +157 -0
  32. package/assets/crt/Shaders/includes/getMipMapColor.glsl +31 -0
  33. package/assets/crt/Shaders/includes/getSignal.glsl +95 -0
  34. package/assets/crt/Shaders/includes/grain.glsl +19 -0
  35. package/assets/crt/Shaders/includes/jailbars.glsl +11 -0
  36. package/assets/crt/Shaders/includes/mathDefines.glsl +6 -0
  37. package/assets/crt/Shaders/includes/overscan.glsl +18 -0
  38. package/assets/crt/Shaders/includes/verticalRoll.glsl +46 -0
  39. package/assets/crt/Shaders/lights.glsl +16 -0
  40. package/assets/crt/Shaders/overlay-dust-layer.glsl +27 -0
  41. package/assets/crt/Shaders/overlay-dust-particle.glsl +100 -0
  42. package/assets/crt/Shaders/overlay.glsl +73 -0
  43. package/assets/crt/Shaders/raw-overscan.glsl +9 -0
  44. package/assets/crt/reflections.png +0 -0
  45. package/package.json +64 -4
  46. package/src/cathode/backends/fast.js +95 -0
  47. package/src/cathode/backends/gl.js +35 -0
  48. package/src/cathode/cathode.js +123 -0
  49. package/src/cathode/presets.js +62 -0
  50. package/src/cathode/settings.js +56 -0
  51. package/src/crt.js +583 -0
  52. package/src/gl.js +313 -0
  53. package/src/index.d.ts +84 -0
  54. package/src/index.js +121 -0
  55. package/src/presets.js +293 -0
  56. package/src/shaders.js +130 -0
package/src/crt.js ADDED
@@ -0,0 +1,583 @@
1
+ // The CRT renderer: ports the Rust player's pass order to WebGL2 and the
2
+ // shaders from Toddler-Boy/CRTEmulation-Data.
3
+
4
+ import {
5
+ Program,
6
+ Quad,
7
+ Target,
8
+ bindTexture,
9
+ createTexture,
10
+ flipRows,
11
+ readPixels,
12
+ uploadImage,
13
+ } from "./gl.js";
14
+ import {
15
+ DEFAULT_SETTINGS,
16
+ applySetting,
17
+ loadPreset,
18
+ } from "./presets.js";
19
+ import { ENCODER_SHADER, VERTEX, VERTEX_ROTATED } from "./shaders.js";
20
+
21
+ const TEXTURE_2D = 0x0de1;
22
+
23
+ // The shipped masks hold 8 RGB triads per 64 texels, so 8 texels per source
24
+ // pixel makes one triad as wide as one input pixel.
25
+ const MASK_TEXELS_PER_PIXEL = 8;
26
+
27
+ function even(value) {
28
+ const v = Math.round(value);
29
+ return v + (v & 1);
30
+ }
31
+
32
+ /** Virtual resolutions: `lines` scanlines for the source, 3 px per line. */
33
+ export function plan(aspect, lines) {
34
+ const a = Math.max(0.5, Math.min(4, aspect));
35
+ const nh = Math.max(60, Math.min(1080, Math.round(lines)));
36
+ const nw = even(nh * a);
37
+
38
+ let ch = Math.min(nh * 3, 2160);
39
+ let cw = even(ch * a);
40
+ if (cw > 3840) {
41
+ cw = 3840;
42
+ ch = even(cw / a);
43
+ }
44
+ return { native: [nw, nh], crt: [cw, ch] };
45
+ }
46
+
47
+ function tubeRect(winW, winH, aspect, margin = 0.96) {
48
+ const w = Math.max(1, winW);
49
+ const h = Math.max(1, winH);
50
+ let tw;
51
+ let th;
52
+ if (w / h > aspect) {
53
+ th = h;
54
+ tw = h * aspect;
55
+ } else {
56
+ tw = w;
57
+ th = w / aspect;
58
+ }
59
+ return [Math.max(2, Math.trunc(tw * margin)), Math.max(2, Math.trunc(th * margin))];
60
+ }
61
+
62
+ function sourceSize(source) {
63
+ if (!source) throw new Error("cathode: no source was provided");
64
+ const width =
65
+ source.videoWidth ?? source.naturalWidth ?? source.displayWidth ?? source.width;
66
+ const height =
67
+ source.videoHeight ?? source.naturalHeight ?? source.displayHeight ?? source.height;
68
+ if (!width || !height) {
69
+ throw new Error(
70
+ "cathode: the source has no dimensions yet (is the video metadata loaded?)",
71
+ );
72
+ }
73
+ return [width, height];
74
+ }
75
+
76
+ function refreshRate(analog) {
77
+ return analog === "pal" ? 50.125 : 59.826;
78
+ }
79
+
80
+ function flicker(settings) {
81
+ const threshold = 0.7;
82
+ const value = Math.max(settings.phosphorDecay - threshold, 0) / (1 - threshold);
83
+ return Math.pow(value, 2 / 3) * 0.05;
84
+ }
85
+
86
+ function decayFactors(settings, dt) {
87
+ const crtDecay = settings.phosphorDecay;
88
+ const decay = 1 + (10 - 1) * crtDecay;
89
+ const factors = [18, 17, 20];
90
+ const t = Math.max(0, Math.min(1, (crtDecay - 0.6) * 10));
91
+ const off = 1 - t * t * (3 - 2 * t);
92
+ return factors.map((factor) => Math.pow(2, -(factor * decay) * dt) * off);
93
+ }
94
+
95
+ export class CRT {
96
+ constructor(gl, canvas, config) {
97
+ this.gl = gl;
98
+ this.canvas = canvas;
99
+ this.dataUrl = config.dataUrl;
100
+ this.options = config.options ?? {};
101
+ this.settings = config.settings;
102
+ this.shaders = config.shaders;
103
+ this.maskImages = config.maskImages;
104
+ this.reflectionImage = config.reflectionImage;
105
+
106
+ this.quad = new Quad(gl);
107
+ this.programs = {
108
+ simulation: new Program(gl, VERTEX, this.shaders.simulation, "crt-simulation"),
109
+ curved: new Program(gl, VERTEX_ROTATED, this.shaders.curved, "crt-curved"),
110
+ adjacent: new Program(gl, VERTEX, this.shaders.adjacent, "crt-adjacent-excitation"),
111
+ halation: new Program(gl, VERTEX, this.shaders.halation, "crt-halation"),
112
+ bloom: new Program(gl, VERTEX, this.shaders.bloom, "crt-calc-bloom"),
113
+ decoderPal: new Program(gl, VERTEX, this.shaders.decoderPal, "decoder-pal"),
114
+ decoderNtsc: new Program(gl, VERTEX, this.shaders.decoderNtsc, "decoder-ntsc"),
115
+ encoder: new Program(gl, VERTEX, ENCODER_SHADER, "encoder-rgb"),
116
+ };
117
+
118
+ this.reflection = createTexture(gl, {
119
+ width: this.reflectionImage.naturalWidth,
120
+ height: this.reflectionImage.naturalHeight,
121
+ internalFormat: gl.RGBA8,
122
+ format: gl.RGBA,
123
+ type: gl.UNSIGNED_BYTE,
124
+ filter: gl.LINEAR,
125
+ wrap: gl.CLAMP_TO_EDGE,
126
+ mipmaps: true,
127
+ });
128
+ uploadImage(gl, this.reflection, this.reflectionImage);
129
+
130
+ this.mask = null;
131
+ this._buildMask(this.settings.maskBitmap);
132
+
133
+ this._targets = null;
134
+ this._lastTime = null;
135
+ this._crtIndex = 0;
136
+ this._bloomIndex = 0;
137
+ this._reflectionOn = this.options.reflection !== false;
138
+ }
139
+
140
+ get width() {
141
+ return this._targets?.size[0] ?? 0;
142
+ }
143
+
144
+ get height() {
145
+ return this._targets?.size[1] ?? 0;
146
+ }
147
+
148
+ get nativeWidth() {
149
+ return this._targets?.native[0] ?? 0;
150
+ }
151
+
152
+ get nativeHeight() {
153
+ return this._targets?.native[1] ?? 0;
154
+ }
155
+
156
+ _buildMask(name) {
157
+ const gl = this.gl;
158
+ const image = this.maskImages.get(name);
159
+ if (!image) {
160
+ throw new Error(`cathode: CRT mask '${name}' was not loaded`);
161
+ }
162
+ if (this.mask) gl.deleteTexture(this.mask.texture);
163
+
164
+ // Native-size bitmap, tiled by the shader so one triad spans one source
165
+ // pixel (see crtMaskRepeats).
166
+ this.mask = createTexture(gl, {
167
+ width: image.naturalWidth,
168
+ height: image.naturalHeight,
169
+ internalFormat: gl.RGBA8,
170
+ format: gl.RGBA,
171
+ type: gl.UNSIGNED_BYTE,
172
+ filter: gl.LINEAR,
173
+ wrap: gl.REPEAT,
174
+ mipmaps: true,
175
+ });
176
+ uploadImage(gl, this.mask, image);
177
+ }
178
+
179
+ _disposeTargets() {
180
+ if (!this._targets) return;
181
+ const gl = this.gl;
182
+ gl.deleteTexture(this._targets.source.texture);
183
+ for (const target of this._targets.crt) target.dispose();
184
+ for (const target of this._targets.bloom) target.dispose();
185
+ this._targets.adjacent.dispose();
186
+ this._targets.halation.dispose();
187
+ this._targets.yuv.dispose();
188
+ this._targets.decoded.dispose();
189
+ this._targets = null;
190
+ }
191
+
192
+ _ensureTargets(aspect) {
193
+ const layout = plan(aspect, this.settings.lines);
194
+ const [nw, nh] = layout.native;
195
+ const [cw, ch] = layout.crt;
196
+ if (
197
+ this._targets &&
198
+ this._targets.native[0] === nw &&
199
+ this._targets.native[1] === nh &&
200
+ this._targets.size[0] === cw &&
201
+ this._targets.size[1] === ch
202
+ ) {
203
+ return;
204
+ }
205
+ this._disposeTargets();
206
+ const gl = this.gl;
207
+
208
+ const sourceCanvas = document.createElement("canvas");
209
+ sourceCanvas.width = nw;
210
+ sourceCanvas.height = nh;
211
+
212
+ const target = (w, h, internalFormat, format, type, options) =>
213
+ new Target(gl, w, h, internalFormat, format, type, options);
214
+
215
+ this._targets = {
216
+ native: [nw, nh],
217
+ size: [cw, ch],
218
+ sourceCanvas,
219
+ sourceCtx: sourceCanvas.getContext("2d"),
220
+ source: createTexture(gl, {
221
+ width: nw,
222
+ height: nh,
223
+ internalFormat: gl.RGBA8,
224
+ format: gl.RGBA,
225
+ type: gl.UNSIGNED_BYTE,
226
+ filter: gl.LINEAR,
227
+ wrap: gl.CLAMP_TO_EDGE,
228
+ mipmaps: true,
229
+ }),
230
+ crt: [
231
+ target(cw, ch, gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT, { mipmaps: true }),
232
+ target(cw, ch, gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT, { mipmaps: true }),
233
+ ],
234
+ adjacent: target(cw, ch, gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT, { mipmaps: true }),
235
+ halation: target(cw, ch, gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT, { mipmaps: true }),
236
+ bloom: [
237
+ target(1, 1, gl.R32F, gl.RED, gl.FLOAT, { filter: gl.NEAREST }),
238
+ target(1, 1, gl.R32F, gl.RED, gl.FLOAT, { filter: gl.NEAREST }),
239
+ ],
240
+ yuv: target(nw, nh, gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT, { mipmaps: true }),
241
+ decoded: target(nw, nh, gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT, { mipmaps: true }),
242
+ };
243
+ // Old frame history belongs to the previous size.
244
+ this._crtIndex = 0;
245
+ this._bloomIndex = 0;
246
+ }
247
+
248
+ _autoResize() {
249
+ if (this.options.autoResize === false) return;
250
+ const rect = this.canvas.getBoundingClientRect();
251
+ if (rect.width < 1 || rect.height < 1) return;
252
+ const dpr = Math.min(window.devicePixelRatio || 1, this.options.maxPixelRatio ?? 2);
253
+ const width = Math.max(2, Math.round(rect.width * dpr));
254
+ const height = Math.max(2, Math.round(rect.height * dpr));
255
+ if (this.canvas.width !== width || this.canvas.height !== height) {
256
+ this.canvas.width = width;
257
+ this.canvas.height = height;
258
+ this._disposed = false;
259
+ }
260
+ }
261
+
262
+ resize() {
263
+ this._autoResize();
264
+ }
265
+
266
+ setReflection(on) {
267
+ this._reflectionOn = Boolean(on);
268
+ return this;
269
+ }
270
+
271
+ toggleReflection() {
272
+ this._reflectionOn = !this._reflectionOn;
273
+ return this._reflectionOn;
274
+ }
275
+
276
+ setAnalog(mode) {
277
+ this.settings.analog =
278
+ String(mode).toLowerCase() === "pal"
279
+ ? "pal"
280
+ : String(mode).toLowerCase() === "ntsc" ||
281
+ String(mode).toLowerCase() === "yiq"
282
+ ? "ntsc"
283
+ : "off";
284
+ return this;
285
+ }
286
+
287
+ setMask(name) {
288
+ this.settings.maskBitmap = name;
289
+ this._buildMask(name);
290
+ return this;
291
+ }
292
+
293
+ /** Load a preset by name, keeping the scanline count and analogue mode. */
294
+ async setPreset(name) {
295
+ const preset = await loadPreset(this.dataUrl, String(name));
296
+ const previous = this.settings;
297
+ this.settings = {
298
+ ...DEFAULT_SETTINGS,
299
+ ...preset,
300
+ lines: previous.lines,
301
+ analog: previous.analog,
302
+ };
303
+ this._buildMask(this.settings.maskBitmap);
304
+ return this;
305
+ }
306
+
307
+ setOptions(patch) {
308
+ const previousLines = this.settings.lines;
309
+ const previousMask = this.settings.maskBitmap;
310
+ for (const [key, value] of Object.entries(patch)) {
311
+ if (key === "reflection") {
312
+ this.setReflection(value);
313
+ } else if (key === "mask" && typeof value === "string") {
314
+ this.settings.maskBitmap = value;
315
+ } else {
316
+ applySetting(this.settings, key, value);
317
+ }
318
+ }
319
+ if (this.settings.maskBitmap !== previousMask) {
320
+ this._buildMask(this.settings.maskBitmap);
321
+ }
322
+ if (this.settings.lines !== previousLines) this._disposeTargets();
323
+ return this;
324
+ }
325
+
326
+ _drawSource(source) {
327
+ const gl = this.gl;
328
+ const t = this._targets;
329
+ const ctx = t.sourceCtx;
330
+ ctx.imageSmoothingEnabled = true;
331
+ ctx.imageSmoothingQuality = "high";
332
+ ctx.drawImage(source, 0, 0, t.native[0], t.native[1]);
333
+
334
+ gl.bindTexture(TEXTURE_2D, t.source.texture);
335
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
336
+ gl.texSubImage2D(
337
+ TEXTURE_2D,
338
+ 0,
339
+ 0,
340
+ 0,
341
+ t.native[0],
342
+ t.native[1],
343
+ gl.RGBA,
344
+ gl.UNSIGNED_BYTE,
345
+ t.sourceCanvas,
346
+ );
347
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
348
+ gl.generateMipmap(TEXTURE_2D);
349
+ gl.bindTexture(TEXTURE_2D, null);
350
+ }
351
+
352
+ _setDecoderUniforms(program, settings, analog, t) {
353
+ const ntsc = analog === "ntsc";
354
+ const rate = refreshRate(analog);
355
+ program.f32("crtRefreshRate", rate);
356
+ program.f32("decSharpening", settings.sharpening);
357
+ program.f32("decLumablur", settings.lumaBlur);
358
+ program.f32("decChromablur", settings.chromaBlur);
359
+ program.f32("decCrosstalk", settings.crosstalk);
360
+ program.f32("decPhase", settings.phase);
361
+ program.f32("decPALDelayLine", settings.hannover);
362
+ program.f32("decCrossColor", settings.rainbowing);
363
+ program.f32("decDrift", settings.drift);
364
+ program.f32("decNoise", settings.noise);
365
+ program.f32("decJailbars", settings.jailbars);
366
+ program.f32("iTime", t);
367
+
368
+ const brightness = settings.brightness - (ntsc ? 0.7 : 0.5);
369
+ const contrast = 0.4 + (1.2 - 0.4) * settings.contrast;
370
+ const saturation = (ntsc ? 0.8 : 0.9) * settings.saturation;
371
+ program.vec3(
372
+ "encBrightnessContrastSaturation",
373
+ brightness * 0.5,
374
+ contrast,
375
+ saturation,
376
+ );
377
+
378
+ const tintLinear = settings.tint;
379
+ const tint =
380
+ Math.pow(Math.abs(tintLinear), 2.5) * Math.sign(tintLinear || 1);
381
+ program.vec3(
382
+ "encTintGains",
383
+ 1 + tint,
384
+ 1 - (tint * (0.299 - 0.114)) / 0.587,
385
+ 1 - tint,
386
+ );
387
+ }
388
+
389
+ render(source, time = performance.now() / 1000) {
390
+ const gl = this.gl;
391
+ const settings = this.settings;
392
+ this._autoResize();
393
+
394
+ const [sw, sh] = sourceSize(source);
395
+ const aspect = this.options.aspect ?? sw / sh;
396
+ this._ensureTargets(aspect);
397
+ this._drawSource(source);
398
+
399
+ const dt =
400
+ this._lastTime === null
401
+ ? 1 / 60
402
+ : Math.min(0.1, Math.max(0.001, time - this._lastTime));
403
+ this._lastTime = time;
404
+ const t = time;
405
+
406
+ const targets = this._targets;
407
+ const analog = settings.analog;
408
+ let sourceTex = targets.source.texture;
409
+
410
+ // ---- analogue signal path -------------------------------------
411
+ if (analog !== "off") {
412
+ const { encoder } = this.programs;
413
+ encoder.use();
414
+ encoder.resolution(targets.native[0], targets.native[1]);
415
+ encoder.f32("iTime", t);
416
+ encoder.i32("uYiq", analog === "ntsc" ? 1 : 0);
417
+ bindTexture(gl, encoder, "iChannel0", 0, targets.source.texture);
418
+ targets.yuv.bind();
419
+ this.quad.draw();
420
+
421
+ const decoder =
422
+ analog === "ntsc" ? this.programs.decoderNtsc : this.programs.decoderPal;
423
+ decoder.use();
424
+ decoder.resolution(targets.native[0], targets.native[1]);
425
+ this._setDecoderUniforms(decoder, settings, analog, t);
426
+ bindTexture(gl, decoder, "iChannel0", 0, targets.yuv.texture.texture);
427
+ targets.decoded.bind();
428
+ this.quad.draw();
429
+ targets.decoded.generateMipmaps();
430
+ sourceTex = targets.decoded.texture.texture;
431
+ }
432
+
433
+ // ---- beam bloom level (1x1 ping-pong) --------------------------
434
+ const bloomRead = targets.bloom[this._bloomIndex];
435
+ const bloomWrite = targets.bloom[this._bloomIndex ^ 1];
436
+ bloomWrite.bind();
437
+ this.programs.bloom.use();
438
+ this.programs.bloom.resolution(1, 1);
439
+ this.programs.bloom.f32("deltaTime", dt);
440
+ bindTexture(gl, this.programs.bloom, "iChannel0", 0, bloomRead.texture.texture);
441
+ bindTexture(gl, this.programs.bloom, "iChannel1", 1, sourceTex);
442
+ this.quad.draw();
443
+
444
+ // ---- CRT simulation -------------------------------------------
445
+ const read = this._crtIndex;
446
+ const write = read ^ 1;
447
+ const crtRead = targets.crt[read];
448
+ const crtWrite = targets.crt[write];
449
+ crtWrite.bind();
450
+ const sim = this.programs.simulation;
451
+ sim.use();
452
+ sim.resolution(crtWrite.width, crtWrite.height);
453
+ sim.f32("iTime", t);
454
+ sim.f32("crtRefreshRate", refreshRate(analog));
455
+ sim.f32("crtLinePixels", 5.0);
456
+ sim.f32("crtBleed", settings.bleed);
457
+ sim.vec2("crtRedOffset", settings.bleedRed[0], settings.bleedRed[1]);
458
+ sim.vec2("crtGreenOffset", settings.bleedGreen[0], settings.bleedGreen[1]);
459
+ sim.vec2("crtBlueOffset", settings.bleedBlue[0], settings.bleedBlue[1]);
460
+ sim.f32("crtHoffset", settings.hWave);
461
+ sim.f32("crtConvergence", settings.convergence);
462
+ sim.f32("crtScanlines", settings.scanlines);
463
+ sim.f32("crtMask", settings.mask);
464
+ sim.vec2(
465
+ "crtMaskRepeats",
466
+ (targets.native[0] * MASK_TEXELS_PER_PIXEL) / this.mask.width,
467
+ (targets.native[1] * MASK_TEXELS_PER_PIXEL) / this.mask.height,
468
+ );
469
+ sim.f32("crtBloomExpansion", settings.expansion);
470
+ sim.f32("crtAmbient", settings.ambient);
471
+ sim.f32("crtHum", settings.hum);
472
+ sim.f32("crtGlow", 1.0);
473
+ sim.f32("u_deltaTime", dt);
474
+ sim.f32("u_phosphorFlicker", flicker(settings));
475
+ const [dr, dg, db] = decayFactors(settings, dt);
476
+ sim.vec3("u_decayFactor", dr, dg, db);
477
+ const over = 1 + (0.86 - 1) * settings.overscan;
478
+ const yOver = over * (analog === "ntsc" ? 0.8825 : 1);
479
+ sim.vec2("crtOverscan", over, yOver);
480
+ bindTexture(gl, sim, "iChannel0", 0, sourceTex);
481
+ bindTexture(gl, sim, "iChannel1", 1, this.mask.texture);
482
+ bindTexture(gl, sim, "iChannel2", 2, crtRead.texture.texture);
483
+ bindTexture(gl, sim, "iChannel3", 3, bloomWrite.texture.texture);
484
+ this.quad.draw();
485
+ crtWrite.generateMipmaps();
486
+
487
+ // ---- phosphor spill -------------------------------------------
488
+ targets.adjacent.bind();
489
+ const adjacent = this.programs.adjacent;
490
+ adjacent.use();
491
+ adjacent.resolution(crtWrite.width, crtWrite.height);
492
+ adjacent.f32("iTime", t);
493
+ adjacent.f32("crtAdjacent", settings.adjacent);
494
+ bindTexture(gl, adjacent, "iChannel0", 0, crtWrite.texture.texture);
495
+ this.quad.draw();
496
+ targets.adjacent.generateMipmaps();
497
+
498
+ // ---- halation -------------------------------------------------
499
+ targets.halation.bind();
500
+ const halation = this.programs.halation;
501
+ halation.use();
502
+ halation.resolution(crtWrite.width, crtWrite.height);
503
+ halation.f32("iTime", t);
504
+ halation.f32("crtHalation", settings.halation);
505
+ bindTexture(gl, halation, "iChannel0", 0, targets.adjacent.texture.texture);
506
+ this.quad.draw();
507
+ targets.halation.generateMipmaps();
508
+
509
+ // ---- tube geometry, vignette, glass reflection ----------------
510
+ const winW = gl.drawingBufferWidth;
511
+ const winH = gl.drawingBufferHeight;
512
+ const [tw, th] = tubeRect(winW, winH, this._targets.size[0] / this._targets.size[1], 0.96);
513
+ const rotation = ((settings.rotation * 0.005) * Math.PI) / 180;
514
+ const sin = Math.sin(rotation);
515
+ const cos = Math.cos(rotation);
516
+
517
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
518
+ gl.viewport(0, 0, winW, winH);
519
+ gl.disable(gl.BLEND);
520
+ gl.clearColor(0, 0, 0, 1);
521
+ gl.clear(gl.COLOR_BUFFER_BIT);
522
+
523
+ const curved = this.programs.curved;
524
+ curved.use();
525
+ curved.resolution(tw, th);
526
+ curved.f32("iTime", t);
527
+ curved.f32("crtCurve", settings.curve);
528
+ curved.f32("crtVignette", settings.vignette);
529
+ curved.f32("crtAmbient", settings.ambient);
530
+ curved.f32("crtDistortion", 0.3);
531
+ curved.f32("crtRotation", rotation);
532
+ curved.f32("crtAspect", tw / th);
533
+ curved.bool("crtSource", false);
534
+ curved.f32("crtRflCorrection", 1.0);
535
+ curved.f32("camZoom", 1.0);
536
+ curved.i32("crtWebcamFormat", 0);
537
+ curved.vec3("camBrightnessContrastSaturation", 1, 1, 1);
538
+ curved.vec3("yuvCol0", 0, 0, 0);
539
+ curved.vec3("yuvCol1", 0, 0, 0);
540
+ curved.vec3("yuvCol2", 0, 0, 0);
541
+ curved.vec3("yuvBias", 0, 0, 0);
542
+ curved.f32(
543
+ "crtReflection",
544
+ this._reflectionOn ? settings.reflections : 0.0,
545
+ );
546
+ curved.vec3("backCol", 0, 0, 0);
547
+ bindTexture(gl, curved, "iChannel0", 0, targets.halation.texture.texture);
548
+ bindTexture(gl, curved, "iChannel1", 1, this.reflection.texture);
549
+ bindTexture(gl, curved, "iChannel2", 2, targets.halation.texture.texture);
550
+ bindTexture(gl, curved, "iChannel3", 3, targets.halation.texture.texture);
551
+
552
+ curved.vec2("uScale", tw / Math.max(1, winW), th / Math.max(1, winH));
553
+ curved.f32("uWindowAspect", Math.max(1, winW) / Math.max(1, winH));
554
+ curved.mat2("uRot", [cos, sin, -sin, cos]);
555
+ this.quad.draw();
556
+
557
+ this._crtIndex = write;
558
+ this._bloomIndex ^= 1;
559
+ return this;
560
+ }
561
+
562
+ /** Read the current drawing buffer top-down (for saving or inspecting). */
563
+ readPixels() {
564
+ const gl = this.gl;
565
+ const width = gl.drawingBufferWidth;
566
+ const height = gl.drawingBufferHeight;
567
+ const data = flipRows(readPixels(gl, width, height), width, height);
568
+ return { width, height, data };
569
+ }
570
+
571
+ /** PNG data URL of the last rendered frame. */
572
+ snapshot(type = "image/png") {
573
+ return this.canvas.toDataURL(type);
574
+ }
575
+
576
+ dispose() {
577
+ this._disposeTargets();
578
+ for (const program of Object.values(this.programs)) program.dispose();
579
+ this.quad.dispose();
580
+ if (this.mask) this.gl.deleteTexture(this.mask.texture);
581
+ this.gl.deleteTexture(this.reflection.texture);
582
+ }
583
+ }