@kassoulet/cathode 0.0.0-stage → 0.1.0
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.
- package/CHANGELOG.md +13 -0
- package/LICENSE +25 -0
- package/README.md +134 -2
- package/assets/crt/CRT Masks/Aperture Grille.png +0 -0
- package/assets/crt/CRT Masks/SX-64.png +0 -0
- package/assets/crt/CRT Masks/Shadow Mask EDP.png +0 -0
- package/assets/crt/CRT Masks/Shadow Mask.png +0 -0
- package/assets/crt/CRT Masks/Slot Mask.png +0 -0
- package/assets/crt/LICENSE +121 -0
- package/assets/crt/Presets/C64 Ultimate HDMI.yml +28 -0
- package/assets/crt/Presets/Cheap and broken TV.yml +27 -0
- package/assets/crt/Presets/Default.yml +27 -0
- package/assets/crt/Presets/SX-64.yml +27 -0
- package/assets/crt/Presets/VICE.yml +28 -0
- package/assets/crt/README.md +3 -0
- package/assets/crt/Shaders/2d-texture.glsl +4 -0
- package/assets/crt/Shaders/crt-adjacent-excitation.glsl +29 -0
- package/assets/crt/Shaders/crt-bezel-reflections.glsl +101 -0
- package/assets/crt/Shaders/crt-calc-bloom.glsl +24 -0
- package/assets/crt/Shaders/crt-curved.glsl +225 -0
- package/assets/crt/Shaders/crt-halation.glsl +35 -0
- package/assets/crt/Shaders/crt-simulation.glsl +175 -0
- package/assets/crt/Shaders/decoder-ntsc.glsl +105 -0
- package/assets/crt/Shaders/decoder-pal.glsl +83 -0
- package/assets/crt/Shaders/decoder-yiq.glsl +25 -0
- package/assets/crt/Shaders/decoder-yuv.glsl +25 -0
- package/assets/crt/Shaders/encoder-c64.glsl +26 -0
- package/assets/crt/Shaders/encoder-raw.glsl +19 -0
- package/assets/crt/Shaders/includes/colorGrade.glsl +25 -0
- package/assets/crt/Shaders/includes/colorSpaces.glsl +63 -0
- package/assets/crt/Shaders/includes/common.glsl +157 -0
- package/assets/crt/Shaders/includes/getMipMapColor.glsl +31 -0
- package/assets/crt/Shaders/includes/getSignal.glsl +95 -0
- package/assets/crt/Shaders/includes/grain.glsl +19 -0
- package/assets/crt/Shaders/includes/jailbars.glsl +11 -0
- package/assets/crt/Shaders/includes/mathDefines.glsl +6 -0
- package/assets/crt/Shaders/includes/overscan.glsl +18 -0
- package/assets/crt/Shaders/includes/verticalRoll.glsl +46 -0
- package/assets/crt/Shaders/lights.glsl +16 -0
- package/assets/crt/Shaders/overlay-dust-layer.glsl +27 -0
- package/assets/crt/Shaders/overlay-dust-particle.glsl +100 -0
- package/assets/crt/Shaders/overlay.glsl +73 -0
- package/assets/crt/Shaders/raw-overscan.glsl +9 -0
- package/assets/crt/reflections.png +0 -0
- package/package.json +64 -4
- package/src/cathode/backends/fast.js +95 -0
- package/src/cathode/backends/gl.js +35 -0
- package/src/cathode/cathode.js +123 -0
- package/src/cathode/presets.js +62 -0
- package/src/cathode/settings.js +56 -0
- package/src/crt.js +583 -0
- package/src/gl.js +313 -0
- package/src/index.d.ts +84 -0
- package/src/index.js +121 -0
- package/src/presets.js +293 -0
- package/src/shaders.js +130 -0
package/src/gl.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
// Minimal WebGL2 helpers used by the CRT pipeline.
|
|
2
|
+
|
|
3
|
+
const TEXTURE_2D = 0x0de1;
|
|
4
|
+
|
|
5
|
+
export function getContext(canvas, attributes = {}) {
|
|
6
|
+
const gl = canvas.getContext("webgl2", {
|
|
7
|
+
alpha: false,
|
|
8
|
+
depth: false,
|
|
9
|
+
stencil: false,
|
|
10
|
+
antialias: false,
|
|
11
|
+
premultipliedAlpha: false,
|
|
12
|
+
preserveDrawingBuffer: false,
|
|
13
|
+
powerPreference: "high-performance",
|
|
14
|
+
...attributes,
|
|
15
|
+
});
|
|
16
|
+
if (!gl) {
|
|
17
|
+
throw new Error("cathode: WebGL2 is not available in this browser");
|
|
18
|
+
}
|
|
19
|
+
if (!gl.getExtension("EXT_color_buffer_float")) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
"cathode: EXT_color_buffer_float is required (RGBA16F render targets)",
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
return gl;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function compile(gl, type, source, label) {
|
|
28
|
+
const shader = gl.createShader(type);
|
|
29
|
+
gl.shaderSource(shader, source);
|
|
30
|
+
gl.compileShader(shader);
|
|
31
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
32
|
+
const log = gl.getShaderInfoLog(shader);
|
|
33
|
+
gl.deleteShader(shader);
|
|
34
|
+
throw new Error(`cathode: compiling ${label} failed:\n${log}`);
|
|
35
|
+
}
|
|
36
|
+
return shader;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class Program {
|
|
40
|
+
constructor(gl, vertexSource, fragmentSource, label = "program") {
|
|
41
|
+
this.gl = gl;
|
|
42
|
+
this.label = label;
|
|
43
|
+
this.locations = new Map();
|
|
44
|
+
|
|
45
|
+
const vs = compile(gl, gl.VERTEX_SHADER, vertexSource, `${label} (vertex)`);
|
|
46
|
+
const fs = compile(gl, gl.FRAGMENT_SHADER, fragmentSource, `${label} (fragment)`);
|
|
47
|
+
const program = gl.createProgram();
|
|
48
|
+
gl.attachShader(program, vs);
|
|
49
|
+
gl.attachShader(program, fs);
|
|
50
|
+
gl.linkProgram(program);
|
|
51
|
+
gl.deleteShader(vs);
|
|
52
|
+
gl.deleteShader(fs);
|
|
53
|
+
|
|
54
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
55
|
+
const log = gl.getProgramInfoLog(program);
|
|
56
|
+
gl.deleteProgram(program);
|
|
57
|
+
throw new Error(`cathode: linking ${label} failed:\n${log}`);
|
|
58
|
+
}
|
|
59
|
+
this.program = program;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
use() {
|
|
63
|
+
this.gl.useProgram(this.program);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
loc(name) {
|
|
68
|
+
if (!this.locations.has(name)) {
|
|
69
|
+
this.locations.set(name, this.gl.getUniformLocation(this.program, name));
|
|
70
|
+
}
|
|
71
|
+
return this.locations.get(name);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
f32(name, value) {
|
|
75
|
+
const loc = this.loc(name);
|
|
76
|
+
if (loc) this.gl.uniform1f(loc, value);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
i32(name, value) {
|
|
81
|
+
const loc = this.loc(name);
|
|
82
|
+
if (loc) this.gl.uniform1i(loc, value);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
bool(name, value) {
|
|
87
|
+
return this.i32(name, value ? 1 : 0);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
vec2(name, x, y) {
|
|
91
|
+
const loc = this.loc(name);
|
|
92
|
+
if (loc) this.gl.uniform2f(loc, x, y);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
vec3(name, x, y, z) {
|
|
97
|
+
const loc = this.loc(name);
|
|
98
|
+
if (loc) this.gl.uniform3f(loc, x, y, z);
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
vec3v(name, value) {
|
|
103
|
+
return this.vec3(name, value[0], value[1], value[2]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
mat2(name, value) {
|
|
107
|
+
const loc = this.loc(name);
|
|
108
|
+
if (loc) this.gl.uniformMatrix2fv(loc, false, value);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
sampler(name, unit) {
|
|
113
|
+
return this.i32(name, unit);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
resolution(width, height) {
|
|
117
|
+
return this.vec2("iResolution", width, height);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
dispose() {
|
|
121
|
+
this.gl.deleteProgram(this.program);
|
|
122
|
+
this.locations.clear();
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function createTexture(
|
|
127
|
+
gl,
|
|
128
|
+
{
|
|
129
|
+
width,
|
|
130
|
+
height,
|
|
131
|
+
internalFormat = gl.RGBA8,
|
|
132
|
+
format = gl.RGBA,
|
|
133
|
+
type = gl.UNSIGNED_BYTE,
|
|
134
|
+
filter = gl.LINEAR,
|
|
135
|
+
wrap = gl.CLAMP_TO_EDGE,
|
|
136
|
+
mipmaps = false,
|
|
137
|
+
} = {},
|
|
138
|
+
) { const texture = gl.createTexture();
|
|
139
|
+
gl.bindTexture(TEXTURE_2D, texture);
|
|
140
|
+
|
|
141
|
+
const levels = mipmaps
|
|
142
|
+
? 1 + Math.floor(Math.log2(Math.max(width, height)))
|
|
143
|
+
: 1;
|
|
144
|
+
gl.texStorage2D(TEXTURE_2D, levels, internalFormat, width, height);
|
|
145
|
+
|
|
146
|
+
gl.texParameteri(TEXTURE_2D, gl.TEXTURE_WRAP_S, wrap);
|
|
147
|
+
gl.texParameteri(TEXTURE_2D, gl.TEXTURE_WRAP_T, wrap);
|
|
148
|
+
gl.texParameteri(
|
|
149
|
+
TEXTURE_2D,
|
|
150
|
+
gl.TEXTURE_MIN_FILTER,
|
|
151
|
+
mipmaps ? gl.LINEAR_MIPMAP_LINEAR : filter,
|
|
152
|
+
);
|
|
153
|
+
gl.texParameteri(TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
|
|
154
|
+
|
|
155
|
+
gl.bindTexture(TEXTURE_2D, null);
|
|
156
|
+
return { texture, width, height, levels, mipmaps };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function uploadImage(gl, target, image) {
|
|
160
|
+
gl.bindTexture(TEXTURE_2D, target.texture);
|
|
161
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
|
|
162
|
+
gl.texSubImage2D(
|
|
163
|
+
TEXTURE_2D,
|
|
164
|
+
0,
|
|
165
|
+
0,
|
|
166
|
+
0,
|
|
167
|
+
target.width,
|
|
168
|
+
target.height,
|
|
169
|
+
gl.RGBA,
|
|
170
|
+
gl.UNSIGNED_BYTE,
|
|
171
|
+
image,
|
|
172
|
+
);
|
|
173
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
174
|
+
if (target.mipmaps) gl.generateMipmap(TEXTURE_2D);
|
|
175
|
+
gl.bindTexture(TEXTURE_2D, null);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function uploadCanvas(gl, target, canvas) {
|
|
179
|
+
gl.bindTexture(TEXTURE_2D, target.texture);
|
|
180
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
|
|
181
|
+
gl.texSubImage2D(
|
|
182
|
+
TEXTURE_2D,
|
|
183
|
+
0,
|
|
184
|
+
0,
|
|
185
|
+
0,
|
|
186
|
+
target.width,
|
|
187
|
+
target.height,
|
|
188
|
+
gl.RGBA,
|
|
189
|
+
gl.UNSIGNED_BYTE,
|
|
190
|
+
canvas,
|
|
191
|
+
);
|
|
192
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
|
|
193
|
+
gl.generateMipmap(TEXTURE_2D);
|
|
194
|
+
gl.bindTexture(TEXTURE_2D, null);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export class Target {
|
|
198
|
+
constructor(
|
|
199
|
+
gl,
|
|
200
|
+
width,
|
|
201
|
+
height,
|
|
202
|
+
internalFormat,
|
|
203
|
+
format,
|
|
204
|
+
type,
|
|
205
|
+
{ mipmaps = false, filter = gl.LINEAR } = {},
|
|
206
|
+
) {
|
|
207
|
+
this.gl = gl;
|
|
208
|
+
this.width = width;
|
|
209
|
+
this.height = height;
|
|
210
|
+
this.mipmaps = mipmaps;
|
|
211
|
+
this.texture = createTexture(gl, {
|
|
212
|
+
width,
|
|
213
|
+
height,
|
|
214
|
+
internalFormat,
|
|
215
|
+
format,
|
|
216
|
+
type,
|
|
217
|
+
filter,
|
|
218
|
+
wrap: gl.CLAMP_TO_EDGE, // WebGL2 has no CLAMP_TO_BORDER
|
|
219
|
+
mipmaps,
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
this.framebuffer = gl.createFramebuffer();
|
|
223
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
|
|
224
|
+
gl.framebufferTexture2D(
|
|
225
|
+
gl.FRAMEBUFFER,
|
|
226
|
+
gl.COLOR_ATTACHMENT0,
|
|
227
|
+
TEXTURE_2D,
|
|
228
|
+
this.texture.texture,
|
|
229
|
+
0,
|
|
230
|
+
);
|
|
231
|
+
const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
|
|
232
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
233
|
+
if (status !== gl.FRAMEBUFFER_COMPLETE) {
|
|
234
|
+
throw new Error(
|
|
235
|
+
`cathode: incomplete framebuffer (${status.toString(16)})`,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
bind() {
|
|
241
|
+
const gl = this.gl;
|
|
242
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
|
|
243
|
+
gl.viewport(0, 0, this.width, this.height);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
generateMipmaps() {
|
|
247
|
+
if (!this.mipmaps) return;
|
|
248
|
+
const gl = this.gl;
|
|
249
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
250
|
+
gl.bindTexture(TEXTURE_2D, this.texture.texture);
|
|
251
|
+
gl.generateMipmap(TEXTURE_2D);
|
|
252
|
+
gl.bindTexture(TEXTURE_2D, null);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
dispose() {
|
|
256
|
+
const gl = this.gl;
|
|
257
|
+
gl.deleteFramebuffer(this.framebuffer);
|
|
258
|
+
gl.deleteTexture(this.texture.texture);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export class Quad {
|
|
263
|
+
constructor(gl) {
|
|
264
|
+
this.gl = gl;
|
|
265
|
+
this.vao = gl.createVertexArray();
|
|
266
|
+
this.buffer = gl.createBuffer();
|
|
267
|
+
gl.bindVertexArray(this.vao);
|
|
268
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
|
|
269
|
+
gl.bufferData(
|
|
270
|
+
gl.ARRAY_BUFFER,
|
|
271
|
+
new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]),
|
|
272
|
+
gl.STATIC_DRAW,
|
|
273
|
+
);
|
|
274
|
+
gl.enableVertexAttribArray(0);
|
|
275
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
276
|
+
gl.bindVertexArray(null);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
draw() {
|
|
280
|
+
const gl = this.gl;
|
|
281
|
+
gl.bindVertexArray(this.vao);
|
|
282
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
dispose() {
|
|
286
|
+
const gl = this.gl;
|
|
287
|
+
gl.deleteBuffer(this.buffer);
|
|
288
|
+
gl.deleteVertexArray(this.vao);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function bindTexture(gl, program, name, unit, texture) {
|
|
293
|
+
gl.activeTexture(gl.TEXTURE0 + unit);
|
|
294
|
+
gl.bindTexture(TEXTURE_2D, texture);
|
|
295
|
+
program.sampler(name, unit);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function readPixels(gl, width, height) {
|
|
299
|
+
const pixels = new Uint8Array(width * height * 4);
|
|
300
|
+
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
301
|
+
return pixels;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Flip a bottom-up RGBA buffer (as returned by readPixels) for canvas use. */
|
|
305
|
+
export function flipRows(pixels, width, height) {
|
|
306
|
+
const row = width * 4;
|
|
307
|
+
const out = new Uint8Array(pixels.length);
|
|
308
|
+
for (let y = 0; y < height; y += 1) {
|
|
309
|
+
const src = (height - 1 - y) * row;
|
|
310
|
+
out.set(pixels.subarray(src, src + row), y * row);
|
|
311
|
+
}
|
|
312
|
+
return out;
|
|
313
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type Analog = "off" | "pal" | "ntsc";
|
|
2
|
+
export type Complexity = "simple" | "detailed" | "exhaustive";
|
|
3
|
+
export type Tier = "clean" | "enhance" | "degrade";
|
|
4
|
+
export type MaskType = "aperture-grille" | "shadow-mask" | "dot-triad";
|
|
5
|
+
export type Source = HTMLVideoElement | HTMLCanvasElement | HTMLImageElement | ImageBitmap | OffscreenCanvas;
|
|
6
|
+
|
|
7
|
+
/** Grouped Cathode settings (all keys optional when passed to `configure`). */
|
|
8
|
+
export interface CathodeSettings {
|
|
9
|
+
geometry: { curvature: number; cornerRadius: number; vignette: number; aspectRatio?: number };
|
|
10
|
+
beam: { scanlineIntensity: number; scanlineCount: number; focus: number; beamSharpness?: number };
|
|
11
|
+
phosphor: { type: MaskType; mask: number; persistence: number; bleed: number };
|
|
12
|
+
signal: { chromaticAberration: number; compositeArtifacts: number; noise: number; humBar: number; analog: Analog };
|
|
13
|
+
glass: { bloom: number; reflection: number };
|
|
14
|
+
}
|
|
15
|
+
export type CathodeSettingsPatch = { [G in keyof CathodeSettings]?: Partial<CathodeSettings[G]> };
|
|
16
|
+
|
|
17
|
+
export type PresetInput = Readonly<CathodeSettings> | ({ complexity: Complexity; tier: Tier } & CathodeSettingsPatch);
|
|
18
|
+
|
|
19
|
+
export interface CathodeOptions {
|
|
20
|
+
canvas: HTMLCanvasElement;
|
|
21
|
+
preset?: PresetInput;
|
|
22
|
+
/** `sw` and `wgpu` throw: not implemented. `gl` falls back to `fast` without WebGL2. */
|
|
23
|
+
backend?: "gl" | "fast" | "sw" | "wgpu";
|
|
24
|
+
/** Extra options forwarded to crt-canvas, e.g. `{ dataUrl }`. */
|
|
25
|
+
gl?: CreateCRTOptions;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class Cathode {
|
|
29
|
+
constructor(options: CathodeOptions);
|
|
30
|
+
readonly ready: Promise<this>;
|
|
31
|
+
settings: CathodeSettings;
|
|
32
|
+
attachSource(source: Source): this;
|
|
33
|
+
configure(patch: CathodeSettingsPatch): this;
|
|
34
|
+
setPreset(preset: PresetInput): this;
|
|
35
|
+
render(timeSeconds?: number): void;
|
|
36
|
+
run(): this;
|
|
37
|
+
stop(): this;
|
|
38
|
+
play(): this;
|
|
39
|
+
pause(): this;
|
|
40
|
+
toJSON(): CathodeSettings;
|
|
41
|
+
dispose(): void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type PresetGroup = { Clean: Readonly<CathodeSettings>; Enhance: Readonly<CathodeSettings>; Degrade: Readonly<CathodeSettings> };
|
|
45
|
+
export const Presets: { Simple: PresetGroup; Detailed: PresetGroup; Exhaustive: PresetGroup };
|
|
46
|
+
|
|
47
|
+
export interface CreateCRTOptions {
|
|
48
|
+
preset?: string | Record<string, number>;
|
|
49
|
+
/** URL of the CRTEmulation-Data directory (shaders, masks, presets). Required when bundling. */
|
|
50
|
+
dataUrl?: string;
|
|
51
|
+
analog?: Analog;
|
|
52
|
+
lines?: number;
|
|
53
|
+
aspect?: number;
|
|
54
|
+
autoResize?: boolean;
|
|
55
|
+
reflection?: boolean;
|
|
56
|
+
overrides?: Record<string, number>;
|
|
57
|
+
gl?: WebGLContextAttributes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export class CRT {
|
|
61
|
+
render(source: Source, timeSeconds?: number): void;
|
|
62
|
+
setPreset(name: string): void;
|
|
63
|
+
setMask(name: string): void;
|
|
64
|
+
setAnalog(mode: Analog): void;
|
|
65
|
+
setReflection(on: boolean): void;
|
|
66
|
+
setOptions(options: Record<string, number | string>): void;
|
|
67
|
+
snapshot(): string;
|
|
68
|
+
dispose(): void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function createCRT(canvas: HTMLCanvasElement, options?: CreateCRTOptions): Promise<CRT>;
|
|
72
|
+
export function attach(
|
|
73
|
+
canvas: HTMLCanvasElement,
|
|
74
|
+
source: Source | (() => Source),
|
|
75
|
+
options?: CreateCRTOptions,
|
|
76
|
+
): Promise<{ crt: CRT; stop(): void }>;
|
|
77
|
+
|
|
78
|
+
export const PRESET_NAMES: string[];
|
|
79
|
+
export const MASKS: string[];
|
|
80
|
+
export const DEFAULT_SETTINGS: Record<string, number>;
|
|
81
|
+
export function maskFileName(name: string): string;
|
|
82
|
+
export function normalizeAnalog(value?: string): Analog;
|
|
83
|
+
export function parsePresetYaml(text: string): Record<string, number | string>;
|
|
84
|
+
export function plan(...args: unknown[]): unknown;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Cathode — CRT emulation for a <canvas>, using the shaders from
|
|
2
|
+
// Toddler-Boy/CRTEmulation-Data.
|
|
3
|
+
|
|
4
|
+
import { getContext } from "./gl.js";
|
|
5
|
+
import { CRT } from "./crt.js";
|
|
6
|
+
import {
|
|
7
|
+
fragmentShader,
|
|
8
|
+
loadShader,
|
|
9
|
+
SHADER_FILES,
|
|
10
|
+
} from "./shaders.js";
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_SETTINGS,
|
|
13
|
+
MASKS,
|
|
14
|
+
PRESET_NAMES,
|
|
15
|
+
detectDataUrl,
|
|
16
|
+
resolveSettings,
|
|
17
|
+
} from "./presets.js";
|
|
18
|
+
|
|
19
|
+
export { CRT, plan } from "./crt.js";
|
|
20
|
+
export { Cathode } from "./cathode/cathode.js";
|
|
21
|
+
export { Presets } from "./cathode/presets.js";
|
|
22
|
+
export {
|
|
23
|
+
DEFAULT_SETTINGS,
|
|
24
|
+
MASKS,
|
|
25
|
+
PRESET_NAMES,
|
|
26
|
+
maskFileName,
|
|
27
|
+
normalizeAnalog,
|
|
28
|
+
parsePresetYaml,
|
|
29
|
+
} from "./presets.js";
|
|
30
|
+
|
|
31
|
+
function loadImage(url) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const image = new Image();
|
|
34
|
+
image.crossOrigin = "anonymous";
|
|
35
|
+
image.onload = () => resolve(image);
|
|
36
|
+
image.onerror = () =>
|
|
37
|
+
reject(new Error(`cathode: cannot load image ${url}`));
|
|
38
|
+
image.src = url;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create a CRT renderer bound to `canvas`.
|
|
44
|
+
*
|
|
45
|
+
* @param {HTMLCanvasElement} canvas
|
|
46
|
+
* @param {object} [options]
|
|
47
|
+
* @param {string} [options.preset="Default"] Preset name or settings object.
|
|
48
|
+
* @param {string} [options.dataUrl] URL of the CRTEmulation-Data directory.
|
|
49
|
+
* @param {"off"|"pal"|"ntsc"} [options.analog="off"]
|
|
50
|
+
* @param {number} [options.lines=270] Simulated scanline count.
|
|
51
|
+
* @param {number} [options.aspect] Source aspect; inferred per frame.
|
|
52
|
+
* @param {boolean} [options.autoResize=true] Track the canvas CSS size.
|
|
53
|
+
* @param {boolean} [options.reflection=true] Draw the glass reflection.
|
|
54
|
+
* @param {object} [options.overrides] Setting overrides (0..1 units).
|
|
55
|
+
* @returns {Promise<CRT>}
|
|
56
|
+
*/
|
|
57
|
+
export async function createCRT(canvas, options = {}) {
|
|
58
|
+
if (!(canvas instanceof HTMLCanvasElement)) {
|
|
59
|
+
throw new Error("cathode: the first argument must be a <canvas>");
|
|
60
|
+
}
|
|
61
|
+
const gl = getContext(canvas, options.gl);
|
|
62
|
+
const dataUrl = await detectDataUrl(options.dataUrl);
|
|
63
|
+
const settings = await resolveSettings(dataUrl, options);
|
|
64
|
+
|
|
65
|
+
const shaderBase = new URL("Shaders/", dataUrl);
|
|
66
|
+
const cache = new Map();
|
|
67
|
+
const bodies = await Promise.all(
|
|
68
|
+
Object.values(SHADER_FILES).map((file) => loadShader(shaderBase, file, cache)),
|
|
69
|
+
);
|
|
70
|
+
const shaders = {};
|
|
71
|
+
Object.keys(SHADER_FILES).forEach((key, index) => {
|
|
72
|
+
shaders[key] = fragmentShader(bodies[index]);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const maskImages = new Map();
|
|
76
|
+
const [reflectionImage] = await Promise.all([
|
|
77
|
+
loadImage(new URL("reflections.png", dataUrl).href),
|
|
78
|
+
...MASKS.map(async (mask) => {
|
|
79
|
+
const image = await loadImage(
|
|
80
|
+
new URL(`CRT Masks/${encodeURIComponent(mask)}.png`, dataUrl).href,
|
|
81
|
+
);
|
|
82
|
+
maskImages.set(mask, image);
|
|
83
|
+
}),
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
return new CRT(gl, canvas, {
|
|
87
|
+
dataUrl,
|
|
88
|
+
options,
|
|
89
|
+
settings,
|
|
90
|
+
shaders,
|
|
91
|
+
maskImages,
|
|
92
|
+
reflectionImage,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Convenience wrapper: create a renderer and drive it from requestAnimationFrame.
|
|
98
|
+
* `source` may be a video/image/canvas or a function returning one.
|
|
99
|
+
*/
|
|
100
|
+
export async function attach(canvas, source, options = {}) {
|
|
101
|
+
const crt = await createCRT(canvas, options);
|
|
102
|
+
let running = true;
|
|
103
|
+
const loop = (now) => {
|
|
104
|
+
if (!running) return;
|
|
105
|
+
try {
|
|
106
|
+
crt.render(typeof source === "function" ? source() : source, now / 1000);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
console.error(error);
|
|
109
|
+
running = false;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
requestAnimationFrame(loop);
|
|
113
|
+
};
|
|
114
|
+
requestAnimationFrame(loop);
|
|
115
|
+
return {
|
|
116
|
+
crt,
|
|
117
|
+
stop() {
|
|
118
|
+
running = false;
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|