@openartifactsai/motion-stage 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +70 -4
- package/src/build.d.ts +18 -0
- package/src/build.js +73 -0
- package/src/host.d.ts +37 -0
- package/src/host.js +76 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -0
- package/src/layers.d.ts +30 -0
- package/src/layers.js +56 -0
- package/src/offline/audio.d.ts +19 -0
- package/src/offline/audio.js +20 -0
- package/src/offline/data.d.ts +26 -0
- package/src/offline/data.js +71 -0
- package/src/offline/main.d.ts +9 -0
- package/src/offline/main.js +52 -0
- package/src/offline/player.d.ts +23 -0
- package/src/offline/player.js +157 -0
- package/src/protocol/clip-guards.d.ts +5 -0
- package/src/protocol/clip-guards.js +22 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +257 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +100 -0
- package/src/protocol/layers.d.ts +135 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +340 -0
- package/src/protocol/types.js +7 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/raster.d.ts +4 -0
- package/src/raster.js +2 -0
- package/src/runtime/assets.d.ts +19 -0
- package/src/runtime/assets.js +63 -0
- package/src/runtime/caption-box.d.ts +3 -0
- package/src/runtime/caption-box.js +16 -0
- package/src/runtime/captions.d.ts +31 -0
- package/src/runtime/captions.js +115 -0
- package/src/runtime/clip-captions.d.ts +9 -0
- package/src/runtime/clip-captions.js +27 -0
- package/src/runtime/clips.d.ts +83 -0
- package/src/runtime/clips.js +259 -0
- package/src/runtime/console.d.ts +13 -0
- package/src/runtime/console.js +99 -0
- package/src/runtime/cut.d.ts +7 -0
- package/src/runtime/cut.js +63 -0
- package/src/runtime/determinism.d.ts +12 -0
- package/src/runtime/determinism.js +47 -0
- package/src/runtime/film.d.ts +43 -0
- package/src/runtime/film.js +31 -0
- package/src/runtime/focus.d.ts +8 -0
- package/src/runtime/focus.js +71 -0
- package/src/runtime/fonts.d.ts +22 -0
- package/src/runtime/fonts.js +109 -0
- package/src/runtime/frame.d.ts +9 -0
- package/src/runtime/frame.js +140 -0
- package/src/runtime/fx/backdrop.d.ts +60 -0
- package/src/runtime/fx/backdrop.js +264 -0
- package/src/runtime/fx/color.d.ts +19 -0
- package/src/runtime/fx/color.js +84 -0
- package/src/runtime/fx/css-color.d.ts +6 -0
- package/src/runtime/fx/css-color.js +44 -0
- package/src/runtime/fx/curve.d.ts +12 -0
- package/src/runtime/fx/curve.js +34 -0
- package/src/runtime/fx/gl.d.ts +29 -0
- package/src/runtime/fx/gl.js +207 -0
- package/src/runtime/fx/glsl.d.ts +35 -0
- package/src/runtime/fx/glsl.js +113 -0
- package/src/runtime/fx/index.d.ts +23 -0
- package/src/runtime/fx/index.js +151 -0
- package/src/runtime/fx/key.d.ts +11 -0
- package/src/runtime/fx/key.js +21 -0
- package/src/runtime/fx/numbers.d.ts +14 -0
- package/src/runtime/fx/numbers.js +57 -0
- package/src/runtime/fx/pace.d.ts +17 -0
- package/src/runtime/fx/pace.js +26 -0
- package/src/runtime/fx/paint.d.ts +28 -0
- package/src/runtime/fx/paint.js +150 -0
- package/src/runtime/guard.d.ts +4 -0
- package/src/runtime/guard.js +141 -0
- package/src/runtime/hand-titles/index.d.ts +9 -0
- package/src/runtime/hand-titles/index.js +73 -0
- package/src/runtime/hand-titles/layer.d.ts +20 -0
- package/src/runtime/hand-titles/layer.js +118 -0
- package/src/runtime/hand-titles/place.d.ts +27 -0
- package/src/runtime/hand-titles/place.js +45 -0
- package/src/runtime/held-store.d.ts +35 -0
- package/src/runtime/held-store.js +63 -0
- package/src/runtime/held.d.ts +19 -0
- package/src/runtime/held.js +225 -0
- package/src/runtime/kit/camera.d.ts +59 -0
- package/src/runtime/kit/camera.js +81 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -0
- package/src/runtime/kit/count.d.ts +37 -0
- package/src/runtime/kit/count.js +100 -0
- package/src/runtime/kit/cursor.d.ts +46 -0
- package/src/runtime/kit/cursor.js +108 -0
- package/src/runtime/kit/dom.d.ts +14 -0
- package/src/runtime/kit/dom.js +46 -0
- package/src/runtime/kit/geometry.d.ts +24 -0
- package/src/runtime/kit/geometry.js +56 -0
- package/src/runtime/kit/index.d.ts +113 -0
- package/src/runtime/kit/index.js +157 -0
- package/src/runtime/kit/mark-strokes.d.ts +65 -0
- package/src/runtime/kit/mark-strokes.js +226 -0
- package/src/runtime/kit/marks.d.ts +21 -0
- package/src/runtime/kit/marks.js +165 -0
- package/src/runtime/kit/math.d.ts +23 -0
- package/src/runtime/kit/math.js +35 -0
- package/src/runtime/kit/paper.d.ts +7 -0
- package/src/runtime/kit/paper.js +27 -0
- package/src/runtime/kit/random.d.ts +8 -0
- package/src/runtime/kit/random.js +32 -0
- package/src/runtime/kit/text-fx.d.ts +18 -0
- package/src/runtime/kit/text-fx.js +55 -0
- package/src/runtime/kit/typing.d.ts +21 -0
- package/src/runtime/kit/typing.js +42 -0
- package/src/runtime/laid/layer.d.ts +44 -0
- package/src/runtime/laid/layer.js +370 -0
- package/src/runtime/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +123 -0
- package/src/runtime/layers/item.js +150 -0
- package/src/runtime/layers/parts.d.ts +53 -0
- package/src/runtime/layers/parts.js +158 -0
- package/src/runtime/layers/rows.d.ts +4 -0
- package/src/runtime/layers/rows.js +233 -0
- package/src/runtime/layers/scan.d.ts +24 -0
- package/src/runtime/layers/scan.js +204 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +337 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +267 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +90 -0
- package/src/runtime/modules.d.ts +23 -0
- package/src/runtime/modules.js +53 -0
- package/src/runtime/port.d.ts +8 -0
- package/src/runtime/port.js +46 -0
- package/src/runtime/questions.d.ts +24 -0
- package/src/runtime/questions.js +162 -0
- package/src/runtime/recut.d.ts +81 -0
- package/src/runtime/recut.js +325 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +358 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +171 -0
- package/src/runtime/retouch/entry.d.ts +77 -0
- package/src/runtime/retouch/entry.js +1 -0
- package/src/runtime/retouch/judge.d.ts +25 -0
- package/src/runtime/retouch/judge.js +209 -0
- package/src/runtime/retouch/look.d.ts +39 -0
- package/src/runtime/retouch/look.js +135 -0
- package/src/runtime/retouch/own-styles.d.ts +19 -0
- package/src/runtime/retouch/own-styles.js +141 -0
- package/src/runtime/retouch/parts.d.ts +22 -0
- package/src/runtime/retouch/parts.js +166 -0
- package/src/runtime/retouch/retouch.d.ts +37 -0
- package/src/runtime/retouch/retouch.js +238 -0
- package/src/runtime/retouch/seat.d.ts +40 -0
- package/src/runtime/retouch/seat.js +228 -0
- package/src/runtime/retouch/state.d.ts +37 -0
- package/src/runtime/retouch/state.js +153 -0
- package/src/runtime/retouch/type-style.d.ts +19 -0
- package/src/runtime/retouch/type-style.js +111 -0
- package/src/runtime/scan.d.ts +2 -0
- package/src/runtime/scan.js +49 -0
- package/src/runtime/scenes/card.d.ts +12 -0
- package/src/runtime/scenes/card.js +56 -0
- package/src/runtime/scenes/install.d.ts +69 -0
- package/src/runtime/scenes/install.js +528 -0
- package/src/runtime/scenes/read.d.ts +27 -0
- package/src/runtime/scenes/read.js +73 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +46 -0
- package/src/runtime/score.d.ts +32 -0
- package/src/runtime/score.js +262 -0
- package/src/runtime/sketch/pen.d.ts +3 -0
- package/src/runtime/sketch/pen.js +371 -0
- package/src/runtime/sketch/strokes.d.ts +41 -0
- package/src/runtime/sketch/strokes.js +187 -0
- package/src/runtime/snapshot/draw.d.ts +14 -0
- package/src/runtime/snapshot/draw.js +125 -0
- package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
- package/src/runtime/snapshot/hold-first-draw.js +128 -0
- package/src/runtime/snapshot/lift.d.ts +41 -0
- package/src/runtime/snapshot/lift.js +161 -0
- package/src/runtime/snapshot/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +51 -0
- package/src/runtime/snapshot/serialize.js +286 -0
- package/src/runtime/snapshot/styles.d.ts +8 -0
- package/src/runtime/snapshot/styles.js +109 -0
- package/src/runtime/snapshot/xml.d.ts +6 -0
- package/src/runtime/snapshot/xml.js +110 -0
- package/src/runtime/stage-size.d.ts +7 -0
- package/src/runtime/stage-size.js +3 -0
- package/src/runtime/stage.d.ts +27 -0
- package/src/runtime/stage.js +154 -0
- package/src/runtime/stretch.d.ts +13 -0
- package/src/runtime/stretch.js +16 -0
- package/src/runtime/texts.d.ts +24 -0
- package/src/runtime/texts.js +211 -0
- package/src/runtime/video-sync.d.ts +51 -0
- package/src/runtime/video-sync.js +218 -0
- package/src/stills.d.ts +90 -0
- package/src/stills.js +329 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/levels.d.ts +8 -0
- package/src/tools/levels.js +28 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +223 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +35 -0
- package/src/tools/types.d.ts +84 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +6 -0
- package/src/tools.js +4 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { UniformValue } from './glsl.js';
|
|
2
|
+
/** What the computer's WebGL can do for shaders. */
|
|
3
|
+
export interface GlReport {
|
|
4
|
+
/** It draws, and what it draws can be read back. */
|
|
5
|
+
can: boolean;
|
|
6
|
+
/** It draws in software: too slow to run shaders while the film plays. */
|
|
7
|
+
soft: boolean;
|
|
8
|
+
}
|
|
9
|
+
export type Surface = OffscreenCanvas | HTMLCanvasElement;
|
|
10
|
+
/** Whether a red, then a green, cleared on a WebGL2 surface come out the way the shaders' pictures do. */
|
|
11
|
+
export declare function probeGl(): GlReport;
|
|
12
|
+
/** Every shader of the film draws on one WebGL2 surface, its picture then copied onto the backdrop. */
|
|
13
|
+
export declare class GlPainter {
|
|
14
|
+
private surface;
|
|
15
|
+
private gl;
|
|
16
|
+
private programs;
|
|
17
|
+
private context;
|
|
18
|
+
/** Compiles a film's shader body, or throws with what the compiler said. */
|
|
19
|
+
compile(body: string): void;
|
|
20
|
+
private program;
|
|
21
|
+
/**
|
|
22
|
+
* The shader drawn at `pw` by `ph` real pixels for a film `W` by `H`, with `values` for its uniforms (colors read
|
|
23
|
+
* as [r, g, b] already); a uniform they leave out is 0. Gives the surface it drew on.
|
|
24
|
+
*/
|
|
25
|
+
draw(body: string, pw: number, ph: number, W: number, H: number, values: ReadonlyMap<string, UniformValue>): Surface;
|
|
26
|
+
private send;
|
|
27
|
+
/** Lets the browser have its WebGL context back: it keeps only a few. */
|
|
28
|
+
release(): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { FILM_SIZE_UNIFORM, VERTEX_SOURCE, fragmentSource, shapeOf, uniformNumbers } from "./glsl.js";
|
|
2
|
+
const SOFT_RENDERERS = /swiftshader|llvmpipe|softpipe|software|basic render/i;
|
|
3
|
+
function newSurface() {
|
|
4
|
+
if (typeof OffscreenCanvas === 'function')
|
|
5
|
+
return new OffscreenCanvas(1, 1);
|
|
6
|
+
const canvas = document.createElement('canvas');
|
|
7
|
+
canvas.width = 1;
|
|
8
|
+
canvas.height = 1;
|
|
9
|
+
return canvas;
|
|
10
|
+
}
|
|
11
|
+
const OPTIONS = { alpha: true, premultipliedAlpha: true, antialias: false, depth: false, stencil: false, preserveDrawingBuffer: true };
|
|
12
|
+
function contextOf(surface) {
|
|
13
|
+
try {
|
|
14
|
+
const made = surface.getContext('webgl2', OPTIONS);
|
|
15
|
+
return made instanceof WebGL2RenderingContext ? made : null;
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function rendererOf(gl) {
|
|
22
|
+
const info = gl.getExtension('WEBGL_debug_renderer_info');
|
|
23
|
+
return String(gl.getParameter(info ? info.UNMASKED_RENDERER_WEBGL : gl.RENDERER));
|
|
24
|
+
}
|
|
25
|
+
/** Whether a red, then a green, cleared on a WebGL2 surface come out the way the shaders' pictures do. */
|
|
26
|
+
export function probeGl() {
|
|
27
|
+
try {
|
|
28
|
+
const surface = newSurface();
|
|
29
|
+
const gl = contextOf(surface);
|
|
30
|
+
if (!gl)
|
|
31
|
+
return { can: false, soft: false };
|
|
32
|
+
const soft = SOFT_RENDERERS.test(rendererOf(gl));
|
|
33
|
+
surface.width = 2;
|
|
34
|
+
surface.height = 2;
|
|
35
|
+
const reader = document.createElement('canvas');
|
|
36
|
+
reader.width = 1;
|
|
37
|
+
reader.height = 1;
|
|
38
|
+
const read = reader.getContext('2d', { willReadFrequently: true });
|
|
39
|
+
if (!read)
|
|
40
|
+
return { can: false, soft };
|
|
41
|
+
const shows = (red, green) => {
|
|
42
|
+
gl.clearColor(red, green, 0, 1);
|
|
43
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
44
|
+
read.clearRect(0, 0, 1, 1);
|
|
45
|
+
read.drawImage(surface, 0, 0, 1, 1);
|
|
46
|
+
const d = read.getImageData(0, 0, 1, 1).data;
|
|
47
|
+
return red ? (d[0] ?? 0) > 200 && (d[1] ?? 0) < 60 : (d[1] ?? 0) > 200 && (d[0] ?? 0) < 60;
|
|
48
|
+
};
|
|
49
|
+
const can = shows(1, 0) && shows(0, 1);
|
|
50
|
+
gl.getExtension('WEBGL_lose_context')?.loseContext();
|
|
51
|
+
return { can, soft };
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return { can: false, soft: false };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** The fault in a shader, its lines as the film wrote them. */
|
|
58
|
+
const faultOf = (gl, shader) => (gl.getShaderInfoLog(shader) ?? '').trim().split('\n').slice(0, 8).join('\n');
|
|
59
|
+
/** Every shader of the film draws on one WebGL2 surface, its picture then copied onto the backdrop. */
|
|
60
|
+
export class GlPainter {
|
|
61
|
+
surface = null;
|
|
62
|
+
gl = null;
|
|
63
|
+
programs = new Map();
|
|
64
|
+
context() {
|
|
65
|
+
if (this.gl && !this.gl.isContextLost())
|
|
66
|
+
return this.gl;
|
|
67
|
+
this.programs.clear();
|
|
68
|
+
this.surface = newSurface();
|
|
69
|
+
this.gl = contextOf(this.surface);
|
|
70
|
+
if (!this.gl)
|
|
71
|
+
throw new Error('WebGL2 is not available');
|
|
72
|
+
return this.gl;
|
|
73
|
+
}
|
|
74
|
+
/** Compiles a film's shader body, or throws with what the compiler said. */
|
|
75
|
+
compile(body) {
|
|
76
|
+
this.program(body);
|
|
77
|
+
}
|
|
78
|
+
program(body) {
|
|
79
|
+
const gl = this.context();
|
|
80
|
+
const kept = this.programs.get(body);
|
|
81
|
+
if (kept)
|
|
82
|
+
return kept;
|
|
83
|
+
const stage = (type, source) => {
|
|
84
|
+
const shader = gl.createShader(type);
|
|
85
|
+
if (!shader)
|
|
86
|
+
throw new Error('the shader could not be made');
|
|
87
|
+
gl.shaderSource(shader, source);
|
|
88
|
+
gl.compileShader(shader);
|
|
89
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS) && !gl.isContextLost()) {
|
|
90
|
+
const fault = faultOf(gl, shader);
|
|
91
|
+
gl.deleteShader(shader);
|
|
92
|
+
throw new Error('the shader does not compile:\n' + fault);
|
|
93
|
+
}
|
|
94
|
+
return shader;
|
|
95
|
+
};
|
|
96
|
+
const vertex = stage(gl.VERTEX_SHADER, VERTEX_SOURCE);
|
|
97
|
+
const fragment = stage(gl.FRAGMENT_SHADER, fragmentSource(body));
|
|
98
|
+
const program = gl.createProgram();
|
|
99
|
+
if (!program)
|
|
100
|
+
throw new Error('the shader could not be made');
|
|
101
|
+
gl.attachShader(program, vertex);
|
|
102
|
+
gl.attachShader(program, fragment);
|
|
103
|
+
gl.linkProgram(program);
|
|
104
|
+
gl.deleteShader(vertex);
|
|
105
|
+
gl.deleteShader(fragment);
|
|
106
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS) && !gl.isContextLost()) {
|
|
107
|
+
const fault = (gl.getProgramInfoLog(program) ?? '').trim();
|
|
108
|
+
gl.deleteProgram(program);
|
|
109
|
+
throw new Error('the shader does not link:\n' + fault);
|
|
110
|
+
}
|
|
111
|
+
const uniforms = new Map();
|
|
112
|
+
const count = Number(gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS)) || 0;
|
|
113
|
+
for (let i = 0; i < count; i++) {
|
|
114
|
+
const info = gl.getActiveUniform(program, i);
|
|
115
|
+
if (!info)
|
|
116
|
+
continue;
|
|
117
|
+
const name = info.name.replace(/\[0\]$/, '');
|
|
118
|
+
const at = gl.getUniformLocation(program, info.name);
|
|
119
|
+
if (at)
|
|
120
|
+
uniforms.set(name, { at, type: info.type, shape: shapeOf(info.type), size: info.size });
|
|
121
|
+
}
|
|
122
|
+
const made = { program, uniforms };
|
|
123
|
+
this.programs.set(body, made);
|
|
124
|
+
return made;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The shader drawn at `pw` by `ph` real pixels for a film `W` by `H`, with `values` for its uniforms (colors read
|
|
128
|
+
* as [r, g, b] already); a uniform they leave out is 0. Gives the surface it drew on.
|
|
129
|
+
*/
|
|
130
|
+
draw(body, pw, ph, W, H, values) {
|
|
131
|
+
const gl = this.context();
|
|
132
|
+
const surface = this.surface;
|
|
133
|
+
if (!surface)
|
|
134
|
+
throw new Error('WebGL2 is not available');
|
|
135
|
+
const made = this.program(body);
|
|
136
|
+
if (surface.width !== pw)
|
|
137
|
+
surface.width = pw;
|
|
138
|
+
if (surface.height !== ph)
|
|
139
|
+
surface.height = ph;
|
|
140
|
+
gl.viewport(0, 0, pw, ph);
|
|
141
|
+
gl.useProgram(made.program);
|
|
142
|
+
for (const [name, uniform] of made.uniforms) {
|
|
143
|
+
const value = name === 'R' ? [pw, ph] : name === FILM_SIZE_UNIFORM ? [W, H] : values.get(name);
|
|
144
|
+
if (uniform.shape.send === 'none')
|
|
145
|
+
continue;
|
|
146
|
+
const numbers = value === undefined ? new Array(uniform.shape.count * uniform.size).fill(0) : uniformNumbers(name, value, uniform.shape, uniform.size);
|
|
147
|
+
this.send(gl, uniform, numbers);
|
|
148
|
+
}
|
|
149
|
+
gl.disable(gl.BLEND);
|
|
150
|
+
gl.clearColor(0, 0, 0, 0);
|
|
151
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
152
|
+
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
153
|
+
return surface;
|
|
154
|
+
}
|
|
155
|
+
send(gl, u, n) {
|
|
156
|
+
const at = u.at;
|
|
157
|
+
if (u.shape.send === 'float') {
|
|
158
|
+
const list = new Float32Array(n);
|
|
159
|
+
if (u.shape.count === 1)
|
|
160
|
+
gl.uniform1fv(at, list);
|
|
161
|
+
else if (u.shape.count === 2)
|
|
162
|
+
gl.uniform2fv(at, list);
|
|
163
|
+
else if (u.shape.count === 3)
|
|
164
|
+
gl.uniform3fv(at, list);
|
|
165
|
+
else
|
|
166
|
+
gl.uniform4fv(at, list);
|
|
167
|
+
}
|
|
168
|
+
else if (u.shape.send === 'int') {
|
|
169
|
+
const list = new Int32Array(n.map(Math.round));
|
|
170
|
+
if (u.shape.count === 1)
|
|
171
|
+
gl.uniform1iv(at, list);
|
|
172
|
+
else if (u.shape.count === 2)
|
|
173
|
+
gl.uniform2iv(at, list);
|
|
174
|
+
else if (u.shape.count === 3)
|
|
175
|
+
gl.uniform3iv(at, list);
|
|
176
|
+
else
|
|
177
|
+
gl.uniform4iv(at, list);
|
|
178
|
+
}
|
|
179
|
+
else if (u.shape.send === 'uint') {
|
|
180
|
+
const list = new Uint32Array(n.map((v) => Math.max(0, Math.round(v))));
|
|
181
|
+
if (u.shape.count === 1)
|
|
182
|
+
gl.uniform1uiv(at, list);
|
|
183
|
+
else if (u.shape.count === 2)
|
|
184
|
+
gl.uniform2uiv(at, list);
|
|
185
|
+
else if (u.shape.count === 3)
|
|
186
|
+
gl.uniform3uiv(at, list);
|
|
187
|
+
else
|
|
188
|
+
gl.uniform4uiv(at, list);
|
|
189
|
+
}
|
|
190
|
+
else if (u.shape.send === 'matrix') {
|
|
191
|
+
const list = new Float32Array(n);
|
|
192
|
+
if (u.shape.count === 4)
|
|
193
|
+
gl.uniformMatrix2fv(at, false, list);
|
|
194
|
+
else if (u.shape.count === 9)
|
|
195
|
+
gl.uniformMatrix3fv(at, false, list);
|
|
196
|
+
else
|
|
197
|
+
gl.uniformMatrix4fv(at, false, list);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** Lets the browser have its WebGL context back: it keeps only a few. */
|
|
201
|
+
release() {
|
|
202
|
+
this.gl?.getExtension('WEBGL_lose_context')?.loseContext();
|
|
203
|
+
this.gl = null;
|
|
204
|
+
this.surface = null;
|
|
205
|
+
this.programs.clear();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** The largest noise seed a film may give: nine digits. */
|
|
2
|
+
export declare const MOST_NOISE_SEED = 999999999;
|
|
3
|
+
/** A film's noise seed, mixed into the 32 bits the noise is keyed by; throws for anything but a whole number of nine digits at most. */
|
|
4
|
+
export declare function noiseKey(seed: unknown): number;
|
|
5
|
+
/**
|
|
6
|
+
* GLSL that gives `tnoise(vec2 | vec3)` (smooth value noise) and `thash(vec2 | vec3)` (one number a whole cell),
|
|
7
|
+
* both in 0..1 and keyed by the seed. They hash whole cells with 32-bit integers, so every GPU gets the same cells.
|
|
8
|
+
* The text ends by numbering the next line 1, so a fault in the film's own GLSL is told at its own line.
|
|
9
|
+
*/
|
|
10
|
+
export declare function tnoiseSource(seed: number): string;
|
|
11
|
+
export declare function thashAt(seed: number, x: number, y: number, z?: number): number;
|
|
12
|
+
export declare function tnoiseAt(seed: number, x: number, y: number, z?: number): number;
|
|
13
|
+
/** Each shader step is one triangle that covers the whole picture. */
|
|
14
|
+
export declare const VERTEX_SOURCE = "#version 300 es\nvoid main(){vec2 p=vec2(float((gl_VertexID<<1)&2),float(gl_VertexID&2));gl_Position=vec4(p*2.-1.,0.,1.);}";
|
|
15
|
+
/** The uniform that carries the film's own size, for `px`. */
|
|
16
|
+
export declare const FILM_SIZE_UNIFORM = "fxFilmSize_";
|
|
17
|
+
/**
|
|
18
|
+
* A film's fragment shader body made whole: it reads `uv` (0..1 from the bottom left), `px` (film pixels from the top
|
|
19
|
+
* left) and `R` (the pixels it is drawn at), and writes `o`. Its own `main` runs inside ours, which sets those first.
|
|
20
|
+
*/
|
|
21
|
+
export declare function fragmentSource(body: string): string;
|
|
22
|
+
/** What a uniform takes: how many numbers an item, and how they are sent. */
|
|
23
|
+
export interface UniformShape {
|
|
24
|
+
count: number;
|
|
25
|
+
send: 'float' | 'int' | 'uint' | 'matrix' | 'none';
|
|
26
|
+
}
|
|
27
|
+
/** The shape of a uniform of GL type `type`; samplers and the rest take nothing from the film. */
|
|
28
|
+
export declare const shapeOf: (type: number) => UniformShape;
|
|
29
|
+
/** A value given for a uniform, with colors already read as [r, g, b]. */
|
|
30
|
+
export type UniformValue = number | boolean | readonly number[];
|
|
31
|
+
/**
|
|
32
|
+
* The numbers a uniform of `shape` (and `size` items) is sent, from what the film gave: a number, a boolean, or a list
|
|
33
|
+
* of numbers. A three-number color given to a vec4 gets alpha 1. Throws, saying which uniform, for anything else.
|
|
34
|
+
*/
|
|
35
|
+
export declare function uniformNumbers(name: string, value: UniformValue, shape: UniformShape, size?: number): number[];
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { mix32 } from "./numbers.js";
|
|
2
|
+
/** The largest noise seed a film may give: nine digits. */
|
|
3
|
+
export const MOST_NOISE_SEED = 999_999_999;
|
|
4
|
+
/** A film's noise seed, mixed into the 32 bits the noise is keyed by; throws for anything but a whole number of nine digits at most. */
|
|
5
|
+
export function noiseKey(seed) {
|
|
6
|
+
if (typeof seed !== 'number' || !Number.isInteger(seed) || Math.abs(seed) > MOST_NOISE_SEED)
|
|
7
|
+
throw new Error('FX.tnoise takes a seed: a whole number of up to nine digits');
|
|
8
|
+
return mix32(seed ^ 0x2545f491);
|
|
9
|
+
}
|
|
10
|
+
const CELL_X = 0x8da6b343;
|
|
11
|
+
const CELL_Y = 0xd8163841;
|
|
12
|
+
const CELL_Z = 0xcb1ab31f;
|
|
13
|
+
const hex32 = (n) => '0x' + (n >>> 0).toString(16) + 'u';
|
|
14
|
+
/**
|
|
15
|
+
* GLSL that gives `tnoise(vec2 | vec3)` (smooth value noise) and `thash(vec2 | vec3)` (one number a whole cell),
|
|
16
|
+
* both in 0..1 and keyed by the seed. They hash whole cells with 32-bit integers, so every GPU gets the same cells.
|
|
17
|
+
* The text ends by numbering the next line 1, so a fault in the film's own GLSL is told at its own line.
|
|
18
|
+
*/
|
|
19
|
+
export function tnoiseSource(seed) {
|
|
20
|
+
const key = noiseKey(seed);
|
|
21
|
+
return ('uint tnMix_(uint h){h^=h>>16u;h*=0x21f0aaadu;h^=h>>15u;h*=0x735a2d97u;h^=h>>15u;return h;}' +
|
|
22
|
+
`uint tnCell_(ivec3 c){return tnMix_(uint(c.x)*${hex32(CELL_X)}^tnMix_(uint(c.y)*${hex32(CELL_Y)}^tnMix_(uint(c.z)*${hex32(CELL_Z)}^${hex32(key)})));}` +
|
|
23
|
+
'float tnUnit_(uint h){return float(h>>8u)/16777216.;}' +
|
|
24
|
+
'float thash(vec3 p){return tnUnit_(tnCell_(ivec3(floor(p))));}' +
|
|
25
|
+
'float thash(vec2 p){return thash(vec3(p,0.));}' +
|
|
26
|
+
'float tnoise(vec3 p){vec3 i=floor(p),f=p-i;f=f*f*(3.-2.*f);ivec3 c=ivec3(i);' +
|
|
27
|
+
'float a=mix(tnUnit_(tnCell_(c)),tnUnit_(tnCell_(c+ivec3(1,0,0))),f.x);' +
|
|
28
|
+
'float b=mix(tnUnit_(tnCell_(c+ivec3(0,1,0))),tnUnit_(tnCell_(c+ivec3(1,1,0))),f.x);' +
|
|
29
|
+
'float d=mix(tnUnit_(tnCell_(c+ivec3(0,0,1))),tnUnit_(tnCell_(c+ivec3(1,0,1))),f.x);' +
|
|
30
|
+
'float e=mix(tnUnit_(tnCell_(c+ivec3(0,1,1))),tnUnit_(tnCell_(c+ivec3(1,1,1))),f.x);' +
|
|
31
|
+
'return mix(mix(a,b,f.y),mix(d,e,f.y),f.z);}' +
|
|
32
|
+
'float tnoise(vec2 p){vec2 i=floor(p),f=p-i;f=f*f*(3.-2.*f);ivec3 c=ivec3(ivec2(i),0);' +
|
|
33
|
+
'return mix(mix(tnUnit_(tnCell_(c)),tnUnit_(tnCell_(c+ivec3(1,0,0))),f.x),mix(tnUnit_(tnCell_(c+ivec3(0,1,0))),tnUnit_(tnCell_(c+ivec3(1,1,0))),f.x),f.y);}' +
|
|
34
|
+
'\n#line 1\n');
|
|
35
|
+
}
|
|
36
|
+
/** The same cell hash in JavaScript, as the GPU computes it: for tests, and for a film's own checks. */
|
|
37
|
+
function cellOf(key, x, y, z) {
|
|
38
|
+
const inner = mix32((Math.imul(z | 0, CELL_Z) ^ key) >>> 0);
|
|
39
|
+
const middle = mix32((Math.imul(y | 0, CELL_Y) ^ inner) >>> 0);
|
|
40
|
+
return mix32((Math.imul(x | 0, CELL_X) ^ middle) >>> 0);
|
|
41
|
+
}
|
|
42
|
+
const toUnit = (h) => (h >>> 8) / 16777216;
|
|
43
|
+
export function thashAt(seed, x, y, z = 0) {
|
|
44
|
+
return toUnit(cellOf(noiseKey(seed), Math.floor(x), Math.floor(y), Math.floor(z)));
|
|
45
|
+
}
|
|
46
|
+
export function tnoiseAt(seed, x, y, z = 0) {
|
|
47
|
+
const key = noiseKey(seed);
|
|
48
|
+
const i = Math.floor(x);
|
|
49
|
+
const j = Math.floor(y);
|
|
50
|
+
const k = Math.floor(z);
|
|
51
|
+
const ease = (t) => t * t * (3 - 2 * t);
|
|
52
|
+
const fx = ease(x - i);
|
|
53
|
+
const fy = ease(y - j);
|
|
54
|
+
const fz = ease(z - k);
|
|
55
|
+
const at = (a, b, c) => toUnit(cellOf(key, i + a, j + b, k + c));
|
|
56
|
+
const lerp = (a, b, t) => a + (b - a) * t;
|
|
57
|
+
const plane = (c) => lerp(lerp(at(0, 0, c), at(1, 0, c), fx), lerp(at(0, 1, c), at(1, 1, c), fx), fy);
|
|
58
|
+
return lerp(plane(0), plane(1), fz);
|
|
59
|
+
}
|
|
60
|
+
/** Each shader step is one triangle that covers the whole picture. */
|
|
61
|
+
export const VERTEX_SOURCE = '#version 300 es\nvoid main(){vec2 p=vec2(float((gl_VertexID<<1)&2),float(gl_VertexID&2));gl_Position=vec4(p*2.-1.,0.,1.);}';
|
|
62
|
+
/** The uniform that carries the film's own size, for `px`. */
|
|
63
|
+
export const FILM_SIZE_UNIFORM = 'fxFilmSize_';
|
|
64
|
+
/**
|
|
65
|
+
* A film's fragment shader body made whole: it reads `uv` (0..1 from the bottom left), `px` (film pixels from the top
|
|
66
|
+
* left) and `R` (the pixels it is drawn at), and writes `o`. Its own `main` runs inside ours, which sets those first.
|
|
67
|
+
*/
|
|
68
|
+
export function fragmentSource(body) {
|
|
69
|
+
return ('#version 300 es\nprecision highp float;\nprecision highp int;\n' +
|
|
70
|
+
`uniform vec2 R;\nuniform vec2 ${FILM_SIZE_UNIFORM};\nout vec4 o;\nvec2 uv;\nvec2 px;\n` +
|
|
71
|
+
'#define main fxStep_\n#line 1\n' +
|
|
72
|
+
body +
|
|
73
|
+
'\n#undef main\n' +
|
|
74
|
+
`void main(){uv=gl_FragCoord.xy/R;px=vec2(uv.x,1.-uv.y)*${FILM_SIZE_UNIFORM};fxStep_();}\n`);
|
|
75
|
+
}
|
|
76
|
+
const SHAPES = {
|
|
77
|
+
0x1406: { count: 1, send: 'float' },
|
|
78
|
+
0x8b50: { count: 2, send: 'float' },
|
|
79
|
+
0x8b51: { count: 3, send: 'float' },
|
|
80
|
+
0x8b52: { count: 4, send: 'float' },
|
|
81
|
+
0x1404: { count: 1, send: 'int' },
|
|
82
|
+
0x8b53: { count: 2, send: 'int' },
|
|
83
|
+
0x8b54: { count: 3, send: 'int' },
|
|
84
|
+
0x8b55: { count: 4, send: 'int' },
|
|
85
|
+
0x8b56: { count: 1, send: 'int' },
|
|
86
|
+
0x8b57: { count: 2, send: 'int' },
|
|
87
|
+
0x8b58: { count: 3, send: 'int' },
|
|
88
|
+
0x8b59: { count: 4, send: 'int' },
|
|
89
|
+
0x1405: { count: 1, send: 'uint' },
|
|
90
|
+
0x8dc6: { count: 2, send: 'uint' },
|
|
91
|
+
0x8dc7: { count: 3, send: 'uint' },
|
|
92
|
+
0x8dc8: { count: 4, send: 'uint' },
|
|
93
|
+
0x8b5a: { count: 4, send: 'matrix' },
|
|
94
|
+
0x8b5b: { count: 9, send: 'matrix' },
|
|
95
|
+
0x8b5c: { count: 16, send: 'matrix' },
|
|
96
|
+
};
|
|
97
|
+
/** The shape of a uniform of GL type `type`; samplers and the rest take nothing from the film. */
|
|
98
|
+
export const shapeOf = (type) => SHAPES[type] ?? { count: 0, send: 'none' };
|
|
99
|
+
/**
|
|
100
|
+
* The numbers a uniform of `shape` (and `size` items) is sent, from what the film gave: a number, a boolean, or a list
|
|
101
|
+
* of numbers. A three-number color given to a vec4 gets alpha 1. Throws, saying which uniform, for anything else.
|
|
102
|
+
*/
|
|
103
|
+
export function uniformNumbers(name, value, shape, size = 1) {
|
|
104
|
+
const list = typeof value === 'number' ? [value] : typeof value === 'boolean' ? [value ? 1 : 0] : Array.from(value, Number);
|
|
105
|
+
if (!list.every(Number.isFinite))
|
|
106
|
+
throw new Error('uniform "' + name + '" is not a number');
|
|
107
|
+
if (shape.count === 4 && shape.send === 'float' && list.length === 3)
|
|
108
|
+
list.push(1);
|
|
109
|
+
const want = shape.count * size;
|
|
110
|
+
if (list.length === 0 || list.length % shape.count !== 0 || list.length > want)
|
|
111
|
+
throw new Error('uniform "' + name + '" takes ' + shape.count + (shape.count === 1 ? ' number' : ' numbers') + ', not ' + list.length);
|
|
112
|
+
return list.length === want ? list : list.concat(new Array(want - list.length).fill(0));
|
|
113
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** What the stage gives the painted backdrops. */
|
|
2
|
+
export interface FxStage {
|
|
3
|
+
filmTime(): number;
|
|
4
|
+
reseed(seed: number): void;
|
|
5
|
+
/** The stage's own ground, the flat color of a backdrop that names none. */
|
|
6
|
+
ground(): string;
|
|
7
|
+
/** The scale the stage is shown at. */
|
|
8
|
+
fit(): number;
|
|
9
|
+
}
|
|
10
|
+
/** The film's code calls on FX: it is given the painted backdrops and their helpers. */
|
|
11
|
+
export declare const namesFx: (texts: readonly string[]) => boolean;
|
|
12
|
+
/** The film's code makes an FX of its own: then it is left that one. */
|
|
13
|
+
export declare const ownsFx: (texts: readonly string[]) => boolean;
|
|
14
|
+
/** Puts FX on the window for the film's code. Its WebGL is tried once, here. */
|
|
15
|
+
export declare function installFx(stage: FxStage): void;
|
|
16
|
+
/** A new frame is being drawn: steps recorded from now on are this frame's, and every thing is hidden until drawn again. */
|
|
17
|
+
export declare function fxFrameBegins(): void;
|
|
18
|
+
/** Paints the backdrops at the size they are shown at, smaller while playing if painting is slow. */
|
|
19
|
+
export declare function fxPaintLive(playing: boolean, now: () => number): void;
|
|
20
|
+
/** Paints the backdrops for a pulled frame, at its scale (never above the film's own size), shaders fresh. */
|
|
21
|
+
export declare function fxPaintAt(scale: number): void;
|
|
22
|
+
/** The shown size changed: the moment on show is painted again at it. */
|
|
23
|
+
export declare function fxRepaint(now: () => number): void;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { mixRgb, rgbaText, shadeRgb, toHex } from "./color.js";
|
|
2
|
+
import { readColor } from "./css-color.js";
|
|
3
|
+
import { curve } from "./curve.js";
|
|
4
|
+
import { Backdrop } from "./backdrop.js";
|
|
5
|
+
import { GlPainter, probeGl } from "./gl.js";
|
|
6
|
+
import { tnoiseSource } from "./glsl.js";
|
|
7
|
+
import { hash, kick, noise, rng, spring } from "./numbers.js";
|
|
8
|
+
import { newPace, liveScale, notePaint } from "./pace.js";
|
|
9
|
+
import { bloomOver, coverBox, glowAt, grainOver, linear, makeLayer, radial, vignetteOver } from "./paint.js";
|
|
10
|
+
let installed = null;
|
|
11
|
+
/** The film's code calls on FX: it is given the painted backdrops and their helpers. */
|
|
12
|
+
export const namesFx = (texts) => texts.some((text) => /\bFX\b/.test(text));
|
|
13
|
+
/** The film's code makes an FX of its own: then it is left that one. */
|
|
14
|
+
export const ownsFx = (texts) => texts.some((text) => /\b(?:const|let|var|function|class)\s+FX\b/.test(text));
|
|
15
|
+
const asNumber = (v, otherwise) => (typeof v === 'number' && Number.isFinite(v) ? v : otherwise);
|
|
16
|
+
const rgbOf = (css) => {
|
|
17
|
+
const c = readColor(css);
|
|
18
|
+
return [c[0], c[1], c[2]];
|
|
19
|
+
};
|
|
20
|
+
const blendOf = (v, otherwise) => (typeof v === 'string' && v ? v : otherwise);
|
|
21
|
+
function frameOf(f) {
|
|
22
|
+
const get = (name) => (f && typeof f === 'object' ? Reflect.get(f, name) : undefined);
|
|
23
|
+
const W = asNumber(get('W'), 1920);
|
|
24
|
+
const H = asNumber(get('H'), 1080);
|
|
25
|
+
const px = asNumber(get('px'), 1);
|
|
26
|
+
return { W, H, px, pw: asNumber(get('pw'), Math.round(W * px)), ph: asNumber(get('ph'), Math.round(H * px)), T: asNumber(get('T'), 0) };
|
|
27
|
+
}
|
|
28
|
+
function makeFx(state, stage, flat) {
|
|
29
|
+
return {
|
|
30
|
+
TAU: Math.PI * 2,
|
|
31
|
+
flat,
|
|
32
|
+
mk: (w, h) => makeLayer(w, h),
|
|
33
|
+
hex: (css) => toHex(readColor(css)),
|
|
34
|
+
rgba: (css, a = 1) => rgbaText(rgbOf(css), asNumber(a, 1)),
|
|
35
|
+
shade: (css, k) => toHex(shadeRgb(rgbOf(css), asNumber(k, 0))),
|
|
36
|
+
mixHex: (a, b, k) => toHex(mixRgb(rgbOf(a), rgbOf(b), asNumber(k, 0))),
|
|
37
|
+
lin: (c, x0, y0, x1, y1, ...stops) => linear(c, x0, y0, x1, y1, stops),
|
|
38
|
+
rad: (c, x, y, r0, r1, ...stops) => radial(c, x, y, r0, r1, stops),
|
|
39
|
+
glow: (c, x, y, r, color, a = 1, blend) => glowAt(c, x, y, r, rgbOf(color), asNumber(a, 1), blendOf(blend, 'screen')),
|
|
40
|
+
noise: (x, y = 0) => noise(asNumber(x, 0), asNumber(y, 0)),
|
|
41
|
+
spring: (t, hz, damp) => spring(asNumber(t, 0), asNumber(hz, 3), asNumber(damp, 6)),
|
|
42
|
+
kick: (t, hz, damp) => kick(asNumber(t, 0), asNumber(hz, 4), asNumber(damp, 8)),
|
|
43
|
+
curve: (c, points, close) => curve(c, Array.isArray(points) ? points : [], close === true),
|
|
44
|
+
cover: (c, picture, w, h) => coverBox(c, picture, w, h),
|
|
45
|
+
rng: (seed) => rng(seed),
|
|
46
|
+
hash: (n) => hash(asNumber(n, 0)),
|
|
47
|
+
grain: (c, f, a = 0.08, blend) => grainOver(c, frameOf(f), asNumber(a, 0.08), blendOf(blend, 'overlay')),
|
|
48
|
+
vignette: (c, f, a = 0.35, color = '#000000') => vignetteOver(c, frameOf(f), asNumber(a, 0.35), rgbOf(color)),
|
|
49
|
+
bloom: (c, f, a = 0.35) => bloomOver(c, frameOf(f), asNumber(a, 0.35)),
|
|
50
|
+
tnoise: (seed) => tnoiseSource(typeof seed === 'number' ? seed : NaN),
|
|
51
|
+
backdrop(kit, options = {}) {
|
|
52
|
+
const read = (from, name) => (from && typeof from === 'object' ? Reflect.get(from, name) : undefined);
|
|
53
|
+
const given = options && typeof options === 'object' ? options : {};
|
|
54
|
+
const box = read(kit, 'stage');
|
|
55
|
+
const into = read(given, 'into') ?? box;
|
|
56
|
+
if (!(into instanceof HTMLElement))
|
|
57
|
+
throw new TypeError('FX.backdrop(kit, { into }) takes the element to paint under, such as the scene’s .ground');
|
|
58
|
+
const made = new Backdrop(state.host, asNumber(read(kit, 'W'), 1920), asNumber(read(kit, 'H'), 1080), into, { flat: read(given, 'flat'), label: read(given, 'label'), class: read(given, 'class') }, stage.ground());
|
|
59
|
+
state.backdrops.push(made);
|
|
60
|
+
return {
|
|
61
|
+
el: made.canvas,
|
|
62
|
+
paint: (draw, how) => made.paintStep(draw, how),
|
|
63
|
+
thing: (name, how, draw) => made.thingStep(name, how, draw),
|
|
64
|
+
shader: (body) => made.shaderStep(body),
|
|
65
|
+
};
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Puts FX on the window for the film's code. Its WebGL is tried once, here. */
|
|
70
|
+
export function installFx(stage) {
|
|
71
|
+
if (installed)
|
|
72
|
+
return;
|
|
73
|
+
const told = new Set();
|
|
74
|
+
const ids = new WeakMap();
|
|
75
|
+
let nextId = 0;
|
|
76
|
+
const report = probeGl();
|
|
77
|
+
const gl = report.can ? new GlPainter() : null;
|
|
78
|
+
const state = {
|
|
79
|
+
backdrops: [],
|
|
80
|
+
frame: 0,
|
|
81
|
+
pace: newPace(report.soft),
|
|
82
|
+
playing: false,
|
|
83
|
+
host: {
|
|
84
|
+
frame: () => state.frame,
|
|
85
|
+
filmTime: stage.filmTime,
|
|
86
|
+
reseed: stage.reseed,
|
|
87
|
+
gl,
|
|
88
|
+
keys: {
|
|
89
|
+
idOf(thing) {
|
|
90
|
+
let id = ids.get(thing);
|
|
91
|
+
if (id === undefined)
|
|
92
|
+
ids.set(thing, (id = ++nextId));
|
|
93
|
+
return id;
|
|
94
|
+
},
|
|
95
|
+
stable: (thing) => thing instanceof HTMLImageElement || thing instanceof ImageBitmap,
|
|
96
|
+
},
|
|
97
|
+
tell(message) {
|
|
98
|
+
if (told.has(message))
|
|
99
|
+
return;
|
|
100
|
+
told.add(message);
|
|
101
|
+
console.error(message);
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
installed = state;
|
|
106
|
+
Reflect.set(window, 'FX', Object.freeze(makeFx(state, stage, !gl)));
|
|
107
|
+
window.addEventListener('pagehide', () => gl?.release());
|
|
108
|
+
fxStage = stage;
|
|
109
|
+
}
|
|
110
|
+
let fxStage = null;
|
|
111
|
+
/** A new frame is being drawn: steps recorded from now on are this frame's, and every thing is hidden until drawn again. */
|
|
112
|
+
export function fxFrameBegins() {
|
|
113
|
+
if (!installed)
|
|
114
|
+
return;
|
|
115
|
+
installed.frame++;
|
|
116
|
+
for (const backdrop of installed.backdrops)
|
|
117
|
+
backdrop.hideThings();
|
|
118
|
+
}
|
|
119
|
+
function paintAll(state, share, hold) {
|
|
120
|
+
for (const backdrop of state.backdrops) {
|
|
121
|
+
if (!backdrop.canvas.isConnected)
|
|
122
|
+
continue;
|
|
123
|
+
backdrop.paint({ pw: Math.max(1, Math.round(backdrop.W * share)), ph: Math.max(1, Math.round(backdrop.H * share)), hold });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/** Paints the backdrops at the size they are shown at, smaller while playing if painting is slow. */
|
|
127
|
+
export function fxPaintLive(playing, now) {
|
|
128
|
+
const state = installed;
|
|
129
|
+
if (!state || !fxStage || state.backdrops.length === 0)
|
|
130
|
+
return;
|
|
131
|
+
state.playing = playing;
|
|
132
|
+
const fit = fxStage.fit();
|
|
133
|
+
const ratio = typeof devicePixelRatio === 'number' ? devicePixelRatio : 1;
|
|
134
|
+
const pace = playing ? state.pace : newPace(false);
|
|
135
|
+
const began = now();
|
|
136
|
+
paintAll(state, liveScale(fit, ratio, pace), playing && state.pace.hold);
|
|
137
|
+
if (playing)
|
|
138
|
+
notePaint(state.pace, now() - began);
|
|
139
|
+
}
|
|
140
|
+
/** Paints the backdrops for a pulled frame, at its scale (never above the film's own size), shaders fresh. */
|
|
141
|
+
export function fxPaintAt(scale) {
|
|
142
|
+
const state = installed;
|
|
143
|
+
if (!state)
|
|
144
|
+
return;
|
|
145
|
+
paintAll(state, Math.min(1, scale > 0 ? scale : 1), false);
|
|
146
|
+
}
|
|
147
|
+
/** The shown size changed: the moment on show is painted again at it. */
|
|
148
|
+
export function fxRepaint(now) {
|
|
149
|
+
if (installed)
|
|
150
|
+
fxPaintLive(installed.playing, now);
|
|
151
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a step is drawn from, as text: a step whose text is the same as when the picture was last drawn needs no
|
|
3
|
+
* draw. Functions are left out. A thing that is not plain data (a canvas, a path) is told by identity, and counts as
|
|
4
|
+
* changed every frame unless `stable` says it cannot change (a decoded picture, or one marked still).
|
|
5
|
+
*/
|
|
6
|
+
export interface KeyContext {
|
|
7
|
+
frame: number;
|
|
8
|
+
idOf(thing: object): number;
|
|
9
|
+
stable(thing: object): boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function stepKey(value: unknown, context: KeyContext): string;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function stepKey(value, context) {
|
|
2
|
+
try {
|
|
3
|
+
return (JSON.stringify(value, (_name, item) => {
|
|
4
|
+
if (typeof item === 'function')
|
|
5
|
+
return undefined;
|
|
6
|
+
if (typeof item === 'number' && !Number.isFinite(item))
|
|
7
|
+
return String(item);
|
|
8
|
+
if (item === null || typeof item !== 'object' || Array.isArray(item))
|
|
9
|
+
return item;
|
|
10
|
+
if (ArrayBuffer.isView(item))
|
|
11
|
+
return item instanceof DataView ? '@view' : Array.from(item);
|
|
12
|
+
const proto = Object.getPrototypeOf(item);
|
|
13
|
+
if (proto === Object.prototype || proto === null)
|
|
14
|
+
return item;
|
|
15
|
+
return '@' + context.idOf(item) + (context.stable(item) ? '' : ':' + context.frame);
|
|
16
|
+
}) ?? '');
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return '@loop:' + context.frame;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** A 32-bit integer mixer with good avalanche: every input bit moves about half the output bits. */
|
|
2
|
+
export declare function mix32(x: number): number;
|
|
3
|
+
/** FNV-1a over the UTF-16 units of a string. */
|
|
4
|
+
export declare function stringSeed(text: string): number;
|
|
5
|
+
/** A seeded stream of numbers in [0, 1): the same seed gives the same stream on every device. */
|
|
6
|
+
export declare function rng(seed: unknown): () => number;
|
|
7
|
+
/** A number in [0, 1) that depends only on `n`; numbers 1/4096 apart already differ. */
|
|
8
|
+
export declare function hash(n: number): number;
|
|
9
|
+
/** Smooth value noise in [−1, 1]: whole numbers are its lattice, and it eases between them. */
|
|
10
|
+
export declare function noise(x: number, y?: number): number;
|
|
11
|
+
/** From 0 at t = 0 to 1, overshooting and settling: `hz` swings a second, `damp` how fast they die (per second). */
|
|
12
|
+
export declare const spring: (t: number, hz?: number, damp?: number) => number;
|
|
13
|
+
/** A knock that rings out round 0. */
|
|
14
|
+
export declare const kick: (t: number, hz?: number, damp?: number) => number;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** A 32-bit integer mixer with good avalanche: every input bit moves about half the output bits. */
|
|
2
|
+
export function mix32(x) {
|
|
3
|
+
let h = x | 0;
|
|
4
|
+
h ^= h >>> 16;
|
|
5
|
+
h = Math.imul(h, 0x21f0aaad);
|
|
6
|
+
h ^= h >>> 15;
|
|
7
|
+
h = Math.imul(h, 0x735a2d97);
|
|
8
|
+
h ^= h >>> 15;
|
|
9
|
+
return h >>> 0;
|
|
10
|
+
}
|
|
11
|
+
/** FNV-1a over the UTF-16 units of a string. */
|
|
12
|
+
export function stringSeed(text) {
|
|
13
|
+
let h = 0x811c9dc5;
|
|
14
|
+
for (let i = 0; i < text.length; i++)
|
|
15
|
+
h = Math.imul(h ^ text.charCodeAt(i), 0x01000193);
|
|
16
|
+
return h >>> 0;
|
|
17
|
+
}
|
|
18
|
+
const seedOf = (seed) => {
|
|
19
|
+
if (typeof seed === 'string')
|
|
20
|
+
return stringSeed(seed);
|
|
21
|
+
const n = Number(seed);
|
|
22
|
+
if (!Number.isFinite(n))
|
|
23
|
+
return 0;
|
|
24
|
+
// 1.5 and 1 give different streams: the fraction counts too.
|
|
25
|
+
return (Math.floor(n) ^ Math.round((n - Math.floor(n)) * 0x10000)) >>> 0;
|
|
26
|
+
};
|
|
27
|
+
/** A seeded stream of numbers in [0, 1): the same seed gives the same stream on every device. */
|
|
28
|
+
export function rng(seed) {
|
|
29
|
+
let state = mix32(seedOf(seed) ^ 0x6a09e667);
|
|
30
|
+
return () => {
|
|
31
|
+
state = (state + 0x9e3779b9) >>> 0;
|
|
32
|
+
return mix32(state) / 4294967296;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** A number in [0, 1) that depends only on `n`; numbers 1/4096 apart already differ. */
|
|
36
|
+
export function hash(n) {
|
|
37
|
+
const whole = Number.isFinite(n) ? Math.floor(n * 4096) : 0;
|
|
38
|
+
return mix32(whole ^ 0x5bd1e995) / 4294967296;
|
|
39
|
+
}
|
|
40
|
+
const lattice = (i, j) => mix32(Math.imul(i | 0, 0x27d4eb2d) ^ mix32(j ^ 0x165667b1)) / 4294967296;
|
|
41
|
+
const smooth = (t) => t * t * (3 - 2 * t);
|
|
42
|
+
/** Smooth value noise in [−1, 1]: whole numbers are its lattice, and it eases between them. */
|
|
43
|
+
export function noise(x, y = 0) {
|
|
44
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
45
|
+
return 0;
|
|
46
|
+
const i = Math.floor(x);
|
|
47
|
+
const j = Math.floor(y);
|
|
48
|
+
const u = smooth(x - i);
|
|
49
|
+
const v = smooth(y - j);
|
|
50
|
+
const top = lattice(i, j) + (lattice(i + 1, j) - lattice(i, j)) * u;
|
|
51
|
+
const bottom = lattice(i, j + 1) + (lattice(i + 1, j + 1) - lattice(i, j + 1)) * u;
|
|
52
|
+
return (top + (bottom - top) * v) * 2 - 1;
|
|
53
|
+
}
|
|
54
|
+
/** From 0 at t = 0 to 1, overshooting and settling: `hz` swings a second, `damp` how fast they die (per second). */
|
|
55
|
+
export const spring = (t, hz = 3, damp = 6) => (t > 0 ? 1 - Math.exp(-damp * t) * Math.cos(2 * Math.PI * hz * t) : 0);
|
|
56
|
+
/** A knock that rings out round 0. */
|
|
57
|
+
export const kick = (t, hz = 4, damp = 8) => (t > 0 ? Math.exp(-damp * t) * Math.sin(2 * Math.PI * hz * t) : 0);
|