@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
@@ -0,0 +1,73 @@
1
+ #include "includes/colorGrade.glsl"
2
+ #include "includes/grain.glsl"
3
+
4
+ uniform float ovlDust = 1.0;
5
+ uniform float ovlChromaticAberration = 0.5;
6
+ uniform float ovlChromaticSize = 0.3;
7
+ uniform vec2 ovlChromaticCenter = vec2 ( 0.5, 0.5 );
8
+ uniform float ovlGrain = 0.2;
9
+
10
+ //-----------------------------------------------------------------------------
11
+
12
+ uniform vec2 ovlShadowOffset = vec2 ( 0.02, 0.03 );
13
+ uniform float ovlShadow = 0.4;
14
+ uniform float ovlShadowBlur = 4.0;
15
+
16
+ void main ()
17
+ {
18
+ vec4 pix = texture ( iChannel0, fragCoord );
19
+ vec2 texSize = 1.0 / textureSize ( iChannel0, 0 ).xy;
20
+
21
+ // Chromatic aberration
22
+ vec2 aberrated = ovlChromaticAberration * texSize * vec2 ( 1.0, 0.5 );
23
+ pix.rb = texture ( iChannel0, fragCoord - aberrated ).rb;
24
+ pix.g = texture ( iChannel0, fragCoord + aberrated ).g;
25
+
26
+ //
27
+ // Add soft inner-shadow
28
+ //
29
+ if ( ovlShadow > 0.0 )
30
+ {
31
+ float sourceAlpha = pix.a;
32
+
33
+ vec2 basePos = fragCoord + ovlShadowOffset;
34
+
35
+ float shadowIntensity = 0.0;
36
+ float occlusionIntensity = 0.0;
37
+
38
+ // Offset shadow
39
+ vec2 off = texSize * 1.5;
40
+
41
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( -off.x, 0.0 ), ovlShadowBlur ).a;
42
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( off.x, 0.0 ), ovlShadowBlur ).a;
43
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( 0.0, -off.y ), ovlShadowBlur ).a;
44
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( 0.0, off.y ), ovlShadowBlur ).a;
45
+
46
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( -off.x, 0.0 ), ovlShadowBlur + 1.0 ).a;
47
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( off.x, 0.0 ), ovlShadowBlur + 1.0 ).a;
48
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( 0.0, -off.y ), ovlShadowBlur + 1.0 ).a;
49
+ shadowIntensity += textureLod ( iChannel0, basePos + vec2 ( 0.0, off.y ), ovlShadowBlur + 1.0 ).a;
50
+
51
+ shadowIntensity *= 0.125;
52
+
53
+ float innerShadowAlpha = shadowIntensity * ( 1.0 - sourceAlpha );
54
+ vec4 shadowResult = vec4 ( vec3 ( 0.0 ), innerShadowAlpha * ovlShadow );
55
+
56
+ // Blend shadow
57
+ pix += shadowResult;
58
+ }
59
+
60
+ // Demultiply alpha
61
+ pix.rgb /= pix.a;
62
+
63
+ // Color grade
64
+ pix.rgb = colorGrade ( pix.rgb, iChannel1, iChannel2 );
65
+
66
+ // Grain
67
+ if ( ovlGrain > 0.0 )
68
+ pix.rgb *= grnGrain ( uvec2 ( fragCoord * iResolution.xy ), ovlGrain * 0.5 );
69
+
70
+ // Back to pre-multiplied
71
+ fragColor = vec4 ( pix.rgb * pix.a, pix.a );
72
+ }
73
+ //-----------------------------------------------------------------------------
@@ -0,0 +1,9 @@
1
+ #include "includes/overscan.glsl"
2
+
3
+ //-----------------------------------------------------------------------------
4
+
5
+ void main ()
6
+ {
7
+ fragColor = texture ( iChannel0, overscan ( fragCoord ) );
8
+ }
9
+ //-----------------------------------------------------------------------------
Binary file
package/package.json CHANGED
@@ -1,6 +1,66 @@
1
1
  {
2
2
  "name": "@kassoulet/cathode",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "WebGL2 CRT emulation for a canvas or video: presets, grouped settings, gl and fast backends",
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "module": "./src/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/index.d.ts",
11
+ "default": "./src/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "assets",
18
+ "README.md",
19
+ "LICENSE",
20
+ "CHANGELOG.md"
21
+ ],
22
+ "sideEffects": false,
23
+ "scripts": {
24
+ "demo": "node scripts/serve.mjs",
25
+ "sync-assets": "node scripts/sync-assets.mjs",
26
+ "prepack": "node scripts/sync-assets.mjs",
27
+ "test": "node --test",
28
+ "pack:check": "npm pack --dry-run"
29
+ },
30
+ "keywords": [
31
+ "crt",
32
+ "webgl",
33
+ "webgl2",
34
+ "canvas",
35
+ "shader",
36
+ "retro",
37
+ "scanlines",
38
+ "phosphor",
39
+ "video",
40
+ "crt-filter",
41
+ "retro-gaming",
42
+ "typescript"
43
+ ],
44
+ "license": "MIT",
45
+ "devDependencies": {
46
+ "crt-shader": "^1.0.1"
47
+ },
48
+ "types": "./src/index.d.ts",
49
+ "engines": {
50
+ "node": ">=18"
51
+ },
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/kassoulet/cathode.git",
55
+ "directory": "js"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/kassoulet/cathode/issues"
59
+ },
60
+ "homepage": "https://github.com/kassoulet/cathode#readme",
61
+ "author": "Gautier Portet",
62
+ "publishConfig": {
63
+ "access": "public",
64
+ "provenance": true
65
+ }
66
+ }
@@ -0,0 +1,95 @@
1
+ // `fast` backend: Canvas2D only. Scanlines, vignette, chromatic aberration, rounded bezel,
2
+ // hum bar. No curvature/mask/persistence/analog — use `gl` for those.
3
+ const sizeOf = (s) => [s.videoWidth ?? s.naturalWidth ?? s.displayWidth ?? s.width,
4
+ s.videoHeight ?? s.naturalHeight ?? s.displayHeight ?? s.height];
5
+
6
+ export function createFast(canvas) {
7
+ const ctx = canvas.getContext("2d");
8
+ if (!ctx) throw new Error("cathode: 2D context unavailable");
9
+ const tmp = document.createElement("canvas");
10
+ const tctx = tmp.getContext("2d");
11
+ let s;
12
+
13
+ function fit() {
14
+ const rect = canvas.getBoundingClientRect();
15
+ const dpr = Math.min(window.devicePixelRatio || 1, 2);
16
+ const w = Math.max(2, Math.round((rect.width || canvas.width) * dpr));
17
+ const h = Math.max(2, Math.round((rect.height || canvas.height) * dpr));
18
+ if (canvas.width !== w || canvas.height !== h) Object.assign(canvas, { width: w, height: h });
19
+ }
20
+
21
+ return {
22
+ name: "fast",
23
+ apply(settings) { s = settings; },
24
+ render(source, time) {
25
+ fit();
26
+ const W = canvas.width, H = canvas.height;
27
+ const [sw, sh] = sizeOf(source);
28
+ const aspect = s.geometry.aspectRatio ?? sw / sh;
29
+ const w = Math.min(W, H * aspect), h = w / aspect;
30
+ const x = (W - w) / 2, y = (H - h) / 2;
31
+
32
+ ctx.save();
33
+ ctx.globalCompositeOperation = "source-over";
34
+ ctx.fillStyle = "#000";
35
+ ctx.fillRect(0, 0, W, H);
36
+ ctx.beginPath();
37
+ ctx.roundRect(x, y, w, h, s.geometry.cornerRadius * Math.min(w, h));
38
+ ctx.clip();
39
+ ctx.imageSmoothingEnabled = w < sw * 2; // crisp pixels when upscaling a lot
40
+
41
+ const shift = s.signal.chromaticAberration * w;
42
+ if (shift > 0.05) { // three channel-isolated copies, offset, added together
43
+ tmp.width = W; tmp.height = H;
44
+ ctx.globalCompositeOperation = "lighter";
45
+ [["#f00", -shift], ["#0f0", 0], ["#00f", shift]].forEach(([color, dx]) => {
46
+ tctx.globalCompositeOperation = "source-over";
47
+ tctx.clearRect(0, 0, W, H);
48
+ tctx.drawImage(source, x + dx, y, w, h);
49
+ tctx.globalCompositeOperation = "multiply";
50
+ tctx.fillStyle = color;
51
+ tctx.fillRect(0, 0, W, H);
52
+ ctx.drawImage(tmp, 0, 0);
53
+ });
54
+ ctx.globalCompositeOperation = "source-over";
55
+ } else {
56
+ ctx.drawImage(source, x, y, w, h);
57
+ }
58
+
59
+ const lines = s.beam.scanlineCount || Math.min(480, Math.max(120, sh));
60
+ const period = h / lines;
61
+ if (s.beam.scanlineIntensity > 0 && period >= 2) {
62
+ const row = document.createElement("canvas");
63
+ row.width = 1; row.height = Math.max(2, Math.round(period));
64
+ const rctx = row.getContext("2d");
65
+ rctx.fillStyle = `rgba(0,0,0,${s.beam.scanlineIntensity})`;
66
+ rctx.fillRect(0, row.height / 2, 1, row.height / 2);
67
+ ctx.fillStyle = ctx.createPattern(row, "repeat");
68
+ ctx.translate(x, y);
69
+ ctx.fillRect(0, 0, w, h);
70
+ ctx.translate(-x, -y);
71
+ }
72
+
73
+ const v = s.geometry.vignette * 0.6;
74
+ if (v > 0) {
75
+ const g = ctx.createRadialGradient(W / 2, H / 2, Math.min(w, h) * 0.35, W / 2, H / 2, Math.hypot(w, h) / 2);
76
+ g.addColorStop(0, "rgba(0,0,0,0)");
77
+ g.addColorStop(1, `rgba(0,0,0,${Math.min(1, v)})`);
78
+ ctx.fillStyle = g;
79
+ ctx.fillRect(x, y, w, h);
80
+ }
81
+
82
+ if (s.signal.humBar > 0) {
83
+ const by = y + ((time * 0.1) % 1) * h;
84
+ const g = ctx.createLinearGradient(0, by - h * 0.1, 0, by + h * 0.1);
85
+ g.addColorStop(0, "rgba(255,255,255,0)");
86
+ g.addColorStop(0.5, `rgba(255,255,255,${s.signal.humBar * 0.25})`);
87
+ g.addColorStop(1, "rgba(255,255,255,0)");
88
+ ctx.fillStyle = g;
89
+ ctx.fillRect(x, by - h * 0.1, w, h * 0.2);
90
+ }
91
+ ctx.restore();
92
+ },
93
+ dispose() {},
94
+ };
95
+ }
@@ -0,0 +1,35 @@
1
+ // `gl` backend: the crt-canvas WebGL2 multi-pass pipeline.
2
+ import { createCRT } from "../../index.js";
3
+ import { toCrtCanvas } from "../settings.js";
4
+
5
+ /** True when WebGL2 + float render targets (needed by crt-canvas) are available. */
6
+ export function glSupported() {
7
+ const gl = document.createElement("canvas").getContext("webgl2");
8
+ return Boolean(gl?.getExtension("EXT_color_buffer_float"));
9
+ }
10
+
11
+ export async function createGl(canvas, options = {}) {
12
+ const crt = await createCRT(canvas, { preset: "Default", ...options });
13
+ let settings;
14
+ let autoLines = true;
15
+ return {
16
+ name: "gl",
17
+ apply(s) {
18
+ settings = s;
19
+ const { patch, analog } = toCrtCanvas(s);
20
+ autoLines = !s.beam.scanlineCount;
21
+ if (s.geometry.aspectRatio) crt.options.aspect = s.geometry.aspectRatio;
22
+ else delete crt.options.aspect;
23
+ crt.setAnalog(analog).setOptions(patch);
24
+ },
25
+ render(source, time) {
26
+ if (autoLines) { // follow the source height, within a sensible CRT range
27
+ const h = source.videoHeight ?? source.naturalHeight ?? source.height;
28
+ const lines = Math.min(480, Math.max(120, h));
29
+ if (lines !== crt.settings.lines) crt.setOptions({ lines });
30
+ }
31
+ crt.render(source, time);
32
+ },
33
+ dispose: () => crt.dispose(),
34
+ };
35
+ }
@@ -0,0 +1,123 @@
1
+ // Cathode: unified CRT filter API over interchangeable backends (specs.md).
2
+ import { DEFAULTS, merge } from "./settings.js";
3
+ import { Presets, presetFor } from "./presets.js";
4
+ import { createGl, glSupported } from "./backends/gl.js";
5
+ import { createFast } from "./backends/fast.js";
6
+
7
+ /** A `Presets.X.Y` object, or the spec's `{complexity, tier, ...overrides}` shape. */
8
+ function resolve(preset) {
9
+ if (preset?.complexity && preset?.tier) {
10
+ const { complexity, tier, ...overrides } = preset;
11
+ return merge(presetFor({ complexity, tier }), overrides);
12
+ }
13
+ return merge(DEFAULTS, preset);
14
+ }
15
+
16
+ export class Cathode {
17
+ /**
18
+ * @param {object} o
19
+ * @param {HTMLCanvasElement} o.canvas
20
+ * @param {object} [o.preset=Detailed.Enhance with curvature 0] or `{complexity, tier}` (lowercase, as in the spec)
21
+ * @param {"gl"|"fast"} [o.backend="gl"] gl falls back to fast without WebGL2; wgpu/sw throw.
22
+ * @param {object} [o.gl] extra options forwarded to crt-canvas (e.g. dataUrl).
23
+ */
24
+ constructor({ canvas, preset = { complexity: "detailed", tier: "enhance", geometry: { curvature: 0 } }, backend = "gl", gl: glOptions } = {}) {
25
+ if (backend === "sw" || backend === "wgpu") throw new Error(`cathode: the '${backend}' backend is not implemented in cathode-js`);
26
+ if (!(canvas instanceof HTMLCanvasElement)) throw new Error("cathode: `canvas` must be a <canvas>");
27
+ this.canvas = canvas;
28
+ this.settings = resolve(preset);
29
+ this.source = null;
30
+ this._playing = false;
31
+ this.backend = null;
32
+ if (backend === "gl" && !glSupported()) {
33
+ console.warn("cathode: WebGL2 + float targets unavailable, falling back to 'fast'");
34
+ backend = "fast";
35
+ }
36
+ /** Resolves once the backend is ready. */
37
+ this.ready = (backend === "gl" ? createGl(canvas, glOptions) : Promise.resolve(createFast(canvas)))
38
+ .then((b) => {
39
+ this.backend = b;
40
+ b.apply(this.settings);
41
+ if (this.source && !this._playing) this.render();
42
+ return this;
43
+ });
44
+ }
45
+
46
+ /** Bind a video, canvas, image, ImageBitmap or OffscreenCanvas. */
47
+ attachSource(source) {
48
+ this.source = source;
49
+ if (!this._playing) this.render();
50
+ return this;
51
+ }
52
+
53
+ configure(patch) {
54
+ this.settings = merge(this.settings, patch);
55
+ this.backend?.apply(this.settings);
56
+ if (!this._playing) this.render();
57
+ return this;
58
+ }
59
+
60
+ setPreset(preset) {
61
+ this.settings = resolve(preset);
62
+ this.backend?.apply(this.settings);
63
+ if (!this._playing) this.render();
64
+ return this;
65
+ }
66
+
67
+ /** Draw one frame now (no-op until the backend and a source are ready). */
68
+ render(time = performance.now() / 1000) {
69
+ if (!this.backend || !this.source) return;
70
+ const s = this.source;
71
+ if (s.readyState !== undefined && s.readyState < 2) return; // video without data yet
72
+ this.backend.render(s, time);
73
+ }
74
+
75
+ /** Start the render loop only (requestVideoFrameCallback for a <video>, else rAF). Never touches playback. */
76
+ run() {
77
+ if (this._playing) return this;
78
+ this._playing = true;
79
+ const src = this.source;
80
+ const rvfc = src?.requestVideoFrameCallback ? src : null;
81
+ const loop = () => {
82
+ if (!this._playing) return;
83
+ schedule();
84
+ this.render(performance.now() / 1000);
85
+ };
86
+ const schedule = () => {
87
+ if (rvfc) {
88
+ const id = rvfc.requestVideoFrameCallback(loop);
89
+ this._cancel = () => rvfc.cancelVideoFrameCallback(id);
90
+ } else {
91
+ const id = requestAnimationFrame(loop);
92
+ this._cancel = () => cancelAnimationFrame(id);
93
+ }
94
+ };
95
+ schedule();
96
+ return this;
97
+ }
98
+
99
+ stop() {
100
+ this._playing = false;
101
+ this._cancel?.();
102
+ return this;
103
+ }
104
+
105
+ /** `run()` plus playing an attached <video>. */
106
+ play() {
107
+ this.source?.play?.();
108
+ return this.run();
109
+ }
110
+
111
+ pause() {
112
+ this.source?.pause?.();
113
+ return this.stop();
114
+ }
115
+
116
+ toJSON() { return this.settings; }
117
+
118
+ dispose() {
119
+ this.stop();
120
+ this.backend?.dispose();
121
+ this.backend = null;
122
+ }
123
+ }
@@ -0,0 +1,62 @@
1
+ // Presets = Complexity × Quality, composed from two small tables (specs.md "Core Preset Matrix").
2
+ import { DEFAULTS, merge } from "./settings.js";
3
+
4
+ // What each quality tier looks like (the monitor being emulated).
5
+ const QUALITY = {
6
+ clean: { // studio monitor: sharp, perfect geometry
7
+ geometry: { curvature: 0, cornerRadius: 0, vignette: 0.1 },
8
+ beam: { scanlineIntensity: 0.3, focus: 1.6 },
9
+ phosphor: { persistence: 0, bleed: 0, mask: 0.15 },
10
+ signal: { chromaticAberration: 0, noise: 0 },
11
+ glass: { bloom: 0.15, reflection: 0 },
12
+ },
13
+ enhance: { // arcade / 90s RGB monitor: warm glow, light mask, mild curve
14
+ geometry: { curvature: 0.12, cornerRadius: 0.05, vignette: 0.35 },
15
+ beam: { scanlineIntensity: 0.4, focus: 1.2 },
16
+ phosphor: { persistence: 0.25, bleed: 0.08, mask: 0.3 },
17
+ signal: { chromaticAberration: 0.002, noise: 0.02 },
18
+ glass: { bloom: 0.45, reflection: 0.1 },
19
+ },
20
+ degrade: { // consumer RF/composite TV: artifacts, bleed, warp, hum
21
+ geometry: { curvature: 0.25, cornerRadius: 0.08, vignette: 0.6 },
22
+ beam: { scanlineIntensity: 0.55, focus: 0.8 },
23
+ phosphor: { persistence: 0.6, bleed: 0.15, mask: 0.35 },
24
+ signal: { chromaticAberration: 0.005, compositeArtifacts: 0.6, noise: 0.25, humBar: 0.2, analog: "ntsc" },
25
+ glass: { bloom: 0.6, reflection: 0.3 },
26
+ },
27
+ };
28
+
29
+ // Which parts of the quality tier the complexity level actually switches on.
30
+ const COMPLEXITY = {
31
+ simple: (q) => ({ // 1 pass: scanlines, mild bloom, vignette
32
+ geometry: { ...q.geometry, curvature: 0 },
33
+ beam: q.beam,
34
+ phosphor: { type: "aperture-grille", mask: 0, persistence: 0, bleed: 0 },
35
+ signal: { chromaticAberration: 0, compositeArtifacts: 0, noise: 0, humBar: 0, analog: "off" },
36
+ glass: { bloom: q.glass.bloom / 2, reflection: 0 },
37
+ }),
38
+ detailed: (q) => ({ // + triad mask, beam blur, barrel warp, aberration; no signal/temporal
39
+ geometry: q.geometry,
40
+ beam: q.beam,
41
+ phosphor: { ...q.phosphor, persistence: 0 },
42
+ signal: { chromaticAberration: q.signal.chromaticAberration, compositeArtifacts: 0, noise: 0, humBar: 0, analog: "off" },
43
+ glass: { ...q.glass, reflection: 0 },
44
+ }),
45
+ exhaustive: (q) => q, // everything
46
+ };
47
+
48
+ export const Presets = {};
49
+ for (const [complexity, build] of Object.entries(COMPLEXITY)) {
50
+ const group = (Presets[complexity[0].toUpperCase() + complexity.slice(1)] = {});
51
+ for (const [quality, tier] of Object.entries(QUALITY)) {
52
+ group[quality[0].toUpperCase() + quality.slice(1)] = Object.freeze(merge(DEFAULTS, build(tier)));
53
+ }
54
+ }
55
+
56
+ /** Look a preset up by the spec's lowercase `{complexity, tier}` pair. */
57
+ export function presetFor({ complexity, tier }) {
58
+ const cap = (v) => String(v)[0].toUpperCase() + String(v).slice(1);
59
+ const preset = Presets[cap(complexity)]?.[cap(tier)];
60
+ if (!preset) throw new Error(`cathode: unknown preset ${complexity}/${tier}`);
61
+ return preset;
62
+ }
@@ -0,0 +1,56 @@
1
+ // Grouped Cathode settings (see specs.md) and their mapping to crt-canvas' flat settings.
2
+
3
+ export const DEFAULTS = {
4
+ // aspectRatio: undefined = follow the source (the spec's 4:3 default would distort 16:9 video)
5
+ geometry: { curvature: 0, cornerRadius: 0.05, vignette: 0.3, aspectRatio: undefined },
6
+ beam: { scanlineIntensity: 0.35, scanlineCount: 0, focus: 1.2 }, // scanlineCount 0 = auto
7
+ phosphor: { type: "aperture-grille", mask: 0.25, persistence: 0.15, bleed: 0.08 },
8
+ signal: { chromaticAberration: 0.003, compositeArtifacts: 0, noise: 0.02, humBar: 0, analog: "off" },
9
+ // Extension to the spec: glass/bloom knobs the multi-pass pipeline exposes.
10
+ glass: { bloom: 0.3, reflection: 0 },
11
+ };
12
+
13
+ /** Deep-merge `patch` into a copy of `base` (two levels: group → key). */
14
+ export function merge(base, patch = {}) {
15
+ if (patch.beam?.beamSharpness !== undefined) { // pre-spec name
16
+ patch = { ...patch, beam: { focus: patch.beam.beamSharpness, ...patch.beam } };
17
+ delete patch.beam.beamSharpness;
18
+ }
19
+ const out = {};
20
+ for (const group of Object.keys({ ...base, ...patch })) {
21
+ out[group] = { ...base[group], ...patch[group] };
22
+ }
23
+ return out;
24
+ }
25
+
26
+ const MASK_FILES = {
27
+ "aperture-grille": "Aperture Grille",
28
+ "shadow-mask": "Shadow Mask",
29
+ "dot-triad": "Shadow Mask EDP",
30
+ };
31
+ const clamp01 = (v) => Math.min(1, Math.max(0, v));
32
+
33
+ /** Cathode settings → crt-canvas `setOptions` patch. `lines` is left to the backend when auto. */
34
+ export function toCrtCanvas(s) {
35
+ const analog = s.signal.analog !== "off" ? s.signal.analog : s.signal.compositeArtifacts > 0 ? "ntsc" : "off";
36
+ const patch = {
37
+ curve: s.geometry.curvature,
38
+ vignette: clamp01(s.geometry.vignette),
39
+ scanlines: s.beam.scanlineIntensity,
40
+ bleed: clamp01(0.5 / Math.max(s.beam.focus, 0.1) - 0.1),
41
+ mask: s.phosphor.mask,
42
+ maskBitmap: MASK_FILES[s.phosphor.type] ?? MASK_FILES["aperture-grille"],
43
+ phosphorDecay: s.phosphor.persistence,
44
+ adjacent: s.phosphor.bleed * 4,
45
+ convergence: clamp01(s.signal.chromaticAberration * 60),
46
+ crosstalk: s.signal.compositeArtifacts,
47
+ rainbowing: s.signal.compositeArtifacts,
48
+ noise: s.signal.noise,
49
+ halation: s.glass.bloom,
50
+ reflections: s.glass.reflection,
51
+ reflection: s.glass.reflection > 0,
52
+ hum: s.signal.humBar,
53
+ };
54
+ if (s.beam.scanlineCount) patch.lines = s.beam.scanlineCount;
55
+ return { patch, analog };
56
+ }