framepress 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +227 -2
- package/assets/backdrops/river-dusk.jpg +0 -0
- package/bin/camera.mjs +190 -0
- package/bin/ffmpeg.mjs +23 -0
- package/bin/mcp.mjs +233 -0
- package/bin/record.mjs +146 -0
- package/bin/sound.mjs +372 -0
- package/bin/studio-video.mjs +282 -0
- package/bin/studio.mjs +494 -0
- package/bin/take.mjs +251 -0
- package/examples/art-variants.js +77 -0
- package/examples/ember.html +1 -0
- package/examples/indexed.html +1 -0
- package/examples/phosphor.html +1 -0
- package/examples/synthetic-desktop/check-alignment.mjs +28 -0
- package/examples/synthetic-desktop/check-sound.mjs +104 -0
- package/examples/synthetic-desktop/desktop.html +343 -0
- package/examples/synthetic-desktop/make.mjs +102 -0
- package/native/Package.swift +9 -0
- package/native/Sources/FramepressCapture/Capture.swift +325 -0
- package/native/Sources/FramepressCapture/Command.swift +219 -0
- package/native/Sources/FramepressCapture/Input.swift +202 -0
- package/native/bin/framepress-capture +0 -0
- package/package.json +55 -4
package/bin/studio.mjs
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
// Studio look: composites each captured page frame as a window on a backdrop, with a
|
|
2
|
+
// spring camera, motion blur from subframes and a drawn cursor. Runs on the GPU in a
|
|
3
|
+
// second headless page; the recorded page itself is never transformed.
|
|
4
|
+
import { readFile } from 'node:fs/promises';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
|
|
7
|
+
// Default backdrop when a take names none: Raj picked the cutting mat (Nov 30).
|
|
8
|
+
export const DEFAULT_BACKDROP = 'cutting-mat';
|
|
9
|
+
|
|
10
|
+
// Named backdrops. A take picks one with `backdrop <name>`; `backdrop #rrggbb` is a flat ink.
|
|
11
|
+
// mode: 0 brushed steel, 1 flat ink with dot screen, 2 grain, 3 riso, 4 cutting mat,
|
|
12
|
+
// 5 river photo, 6 giant 88, 7 pegboard, 8 deep ink, 9 three stop gradient, 10 halftone,
|
|
13
|
+
// 11 walnut, 12 sunburst. Colours are sRGB hex. shadow scales the drop shadow.
|
|
14
|
+
export const BACKDROPS = {
|
|
15
|
+
steel: { mode: 0 },
|
|
16
|
+
cobalt: { mode: 1, bg: '#2f4fd6', shadow: .8 },
|
|
17
|
+
// 1. One deep ink, film grain, soft vignette.
|
|
18
|
+
'grain-terracotta': { mode: 2, bg: '#b5583b', grain: .07, shadow: .85 },
|
|
19
|
+
// 2. Two risograph inks on cream paper, stochastic screen, slight misregistration.
|
|
20
|
+
'riso-blue-orange': { mode: 3, bg: '#f1ebdd', a: '#2f6fc0', b: '#ff7a3d', grain: .03, shadow: .7 },
|
|
21
|
+
// 3. A green self-healing cutting mat with a printed grid and knife scores.
|
|
22
|
+
'cutting-mat': { mode: 4, bg: '#3c7a5e', a: '#f3e7b4', grain: .035, shadow: .9 },
|
|
23
|
+
// 4. Raj's River View lab, autumn dusk, blurred and darkened (assets/backdrops).
|
|
24
|
+
'river-dusk': { mode: 5, image: 'river-dusk.jpg', bg: '#000000', dim: .92, grain: .05, shadow: .9 },
|
|
25
|
+
// 5. A seven-segment 88 the size of the frame, peeking out around the window.
|
|
26
|
+
'giant-88': { mode: 6, bg: '#c9d2b0', a: '#8f9c72', grain: .04, shadow: .7 },
|
|
27
|
+
// 6. Tempered hardboard pegboard, the wall above a workbench.
|
|
28
|
+
pegboard: { mode: 7, bg: '#b88a5c', grain: .03, shadow: .85 },
|
|
29
|
+
// Made for desktop recordings: each one sits apart from a pink to purple macOS wallpaper.
|
|
30
|
+
// 7. One deep green ink, lit from above left, with film grain.
|
|
31
|
+
'deep-green': { mode: 8, bg: '#0e5a42', grain: .055, shadow: 1 },
|
|
32
|
+
// 8. Acid yellow through grass to forest, a grainy three stop gradient.
|
|
33
|
+
'acid-forest': { mode: 9, bg: '#e6ef4a', a: '#4f9a35', b: '#0d3523', grain: .07, shadow: .9 },
|
|
34
|
+
// 9. A coarse halftone screen in black on yellow stock, dots swelling toward the edges.
|
|
35
|
+
halftone: { mode: 10, bg: '#f1d93a', a: '#161512', grain: .03, shadow: .75 },
|
|
36
|
+
// 10. Oiled walnut veneer under a soft key light.
|
|
37
|
+
walnut: { mode: 11, bg: '#3a2113', a: '#7a4a2a', b: '#1a0d07', grain: .03, shadow: 1 },
|
|
38
|
+
// 11. A two tone sunburst radiating from behind the window.
|
|
39
|
+
sunburst: { mode: 12, bg: '#e2452c', a: '#a5261a', grain: .04, shadow: .9 },
|
|
40
|
+
};
|
|
41
|
+
// Built-in cursors on a 360 box (the arrow box is 32 points): a plain arrow for recordings
|
|
42
|
+
// without the chrome arrow, a pointing hand and an I-beam. hot is the hotspot in the 0..1 box.
|
|
43
|
+
const svgUrl = body => `data:image/svg+xml;base64,${Buffer.from(`<svg xmlns="http://www.w3.org/2000/svg" width="360" height="360" viewBox="0 0 360 360">${body}</svg>`).toString('base64')}`;
|
|
44
|
+
const HAND = '<rect x="9" y="1" width="4" height="13" rx="2"/><rect x="13" y="7" width="3.6" height="8" rx="1.8"/><rect x="16.4" y="8" width="3.6" height="8" rx="1.8"/><rect x="19.8" y="9.5" width="3.4" height="8" rx="1.7"/><path d="M9 12 L23.2 12 L23.2 17 Q23.2 23 18 23.5 L13 23.5 Q10 23.5 8 21 L3.5 15.5 Q2.6 13.8 4.3 13.3 Q5.5 13 7 14.5 L9 16.5 Z"/>';
|
|
45
|
+
const IBEAM = 'M-26 -100 Q-6 -100 0 -86 Q6 -100 26 -100 M0 -86 L0 86 M-26 100 Q-6 100 0 86 Q6 100 26 100 M-15 0 L15 0';
|
|
46
|
+
export const CURSOR_SHAPES = {
|
|
47
|
+
arrow: { url: svgUrl('<path transform="translate(60,40) scale(9)" d="M1 1 L1 23 L6.5 17.5 L10 25.5 L13.5 24 L10 16.5 L17.5 16.5 Z" fill="#000" stroke="#fff" stroke-width="1.6" stroke-linejoin="round"/>'), hot: [69 / 360, 49 / 360], flip: false },
|
|
48
|
+
pointer: { url: svgUrl(`<g transform="translate(60,30) scale(10)" stroke-linejoin="round"><g fill="#fff" stroke="#fff" stroke-width="2.6">${HAND}</g><g fill="#000" stroke="#000" stroke-width="1.4">${HAND}</g><g fill="#fff">${HAND}</g><path d="M13 10.5V14M16.4 11V14.5M19.8 12V15.5" stroke="#000" stroke-width=".7" stroke-linecap="round"/></g>`), hot: [170 / 360, 40 / 360], flip: false },
|
|
49
|
+
probe: { url: svgUrl('<circle cx="180" cy="180" r="40" fill="#ff0000"/>'), hot: [.5, .5], flip: false },
|
|
50
|
+
ibeam: { url: svgUrl(`<g transform="translate(180,180)" fill="none" stroke-linecap="round"><path d="${IBEAM}" stroke="#fff" stroke-width="20"/><path d="${IBEAM}" stroke="#000" stroke-width="9"/></g>`), hot: [.5, .5], flip: false },
|
|
51
|
+
};
|
|
52
|
+
const hexRgb = hex => [1, 3, 5].map(i => parseInt(hex.slice(i, i + 2), 16) / 255);
|
|
53
|
+
|
|
54
|
+
// Window corners. radius is a fraction of frame height. nest: the window corner wraps the
|
|
55
|
+
// fitted element's own corner (element radius + fit gap), so the two curves are concentric.
|
|
56
|
+
// hug: no visible frame, the window shrinks to the fitted element and takes its radius, so
|
|
57
|
+
// the element floats on the backdrop with the shadow under its own shape. squircle: a
|
|
58
|
+
// superellipse corner (continuous curvature, like macOS windows) instead of a circular arc.
|
|
59
|
+
// nest and hug need a `fit`; without one they fall back to measured.
|
|
60
|
+
export const CORNERS = {
|
|
61
|
+
measured: { radius: 0.007 }, // Screen Studio exports: about 5 px at 720p.
|
|
62
|
+
medium: { radius: 0.02 },
|
|
63
|
+
large: { radius: 0.035 },
|
|
64
|
+
concentric: { nest: true },
|
|
65
|
+
frameless: { hug: true },
|
|
66
|
+
squircle: { radius: 0.045, squircle: true },
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Window frame proportions as fractions of the output's short side. Screen Studio exports
|
|
70
|
+
// measure 10% top and bottom padding; 14% leaves more backdrop around a tall fitted device.
|
|
71
|
+
// One soft shadow with sigma 0.035 to 0.048 and drop 0.017 to 0.036 of frame height.
|
|
72
|
+
// Shadow layers are [dx, dy, sigma, alpha]; the first is a tight contact shadow.
|
|
73
|
+
// corner names the CORNERS entry; a take can override it with `corner <name>`.
|
|
74
|
+
// Backdrop zoom and screen-space pan follow the window at the measured fraction.
|
|
75
|
+
export const PARALLAX = { zoom: 0.5, pan: 1, over: 1.1 };
|
|
76
|
+
|
|
77
|
+
export const LOOK = { pad: 0.14, corner: 'measured', shadow: [[0, 0.003, 0.004, 0.22], [0, 0.024, 0.04, 0.55]] };
|
|
78
|
+
|
|
79
|
+
function cornerFor(take, s0, H) {
|
|
80
|
+
const k = CORNERS[take.corner ?? LOOK.corner] ?? CORNERS.measured, f = take.fit;
|
|
81
|
+
const none = { radius: [0, 0], inset: [0, 0, 0, 0], squircle: 0 };
|
|
82
|
+
if (take.backdrop === 'none') return none;
|
|
83
|
+
// A captured macOS window: its own corner radius in source points, plus half a point so
|
|
84
|
+
// the mask covers the anti-aliased edge the capture left over black.
|
|
85
|
+
if (take.windowRadius) { const r = (take.windowRadius + 0.5) * s0; return { ...none, radius: [r, r], squircle: 0 }; }
|
|
86
|
+
if ((k.nest || k.hug) && f?.radii && f.gap) {
|
|
87
|
+
const [tl, tr, br, bl] = f.radii, top = (tl + tr) / 2, bottom = (br + bl) / 2, g = f.gap;
|
|
88
|
+
if (k.hug) return { ...none, radius: [top * s0, bottom * s0], inset: [g.l, g.t, g.r, g.b].map(v => v * s0) };
|
|
89
|
+
return { ...none, radius: [(top + g.t) * s0, (bottom + g.b) * s0] };
|
|
90
|
+
}
|
|
91
|
+
const r = (k.radius ?? CORNERS.measured.radius) * H;
|
|
92
|
+
return { ...none, radius: [r, r], squircle: k.squircle ? 1 : 0 };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function layoutFor(take) {
|
|
96
|
+
const [W, H] = take.output, C = take.clip ?? { x: 0, y: 0, width: take.width, height: take.height }, aspect = C.width / C.height;
|
|
97
|
+
const pad = take.backdrop === 'none' ? 0 : (take.pad ?? LOOK.pad) * Math.min(W, H);
|
|
98
|
+
let w = W - 2 * pad, h = w / aspect;
|
|
99
|
+
if (h > H - 2 * pad) { h = H - 2 * pad; w = h * aspect; }
|
|
100
|
+
const win = { x: (W - w) / 2, y: (H - h) / 2, w, h };
|
|
101
|
+
return { W, H, win, clip: C, s0: w / C.width, corner: cornerFor(take, w / C.width, H) };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const vert = `#version 300 es
|
|
105
|
+
in vec2 a; out vec2 v; void main(){ v = a; gl_Position = vec4(a * 2. - 1., 0, 1); }`;
|
|
106
|
+
|
|
107
|
+
const sceneFrag = `#version 300 es
|
|
108
|
+
precision highp float;
|
|
109
|
+
uniform vec2 uRes; uniform vec3 uCam; uniform vec4 uWin, uInset; uniform vec2 uRad; uniform float uSquircle, uPlain, uMode, uGrain, uDim, uOver; uniform vec3 uBgCam; uniform vec3 uBg, uA, uB;
|
|
110
|
+
uniform vec4 uSh0, uSh1; uniform sampler2D uPage, uPrev, uImg; uniform vec2 uImgSize; uniform float uMix;
|
|
111
|
+
out vec4 o;
|
|
112
|
+
float h21(vec2 p){ p = fract(p * vec2(233.34, 851.73)); p += dot(p, p + 23.45); return fract(p.x * p.y); }
|
|
113
|
+
float vn(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3. - 2. * f);
|
|
114
|
+
return mix(mix(h21(i), h21(i + vec2(1, 0)), f.x), mix(h21(i + vec2(0, 1)), h21(i + vec2(1, 1)), f.x), f.y); }
|
|
115
|
+
// Rounded box with its own top and bottom radius; superellipse corners when uSquircle is set.
|
|
116
|
+
float sdBox(vec2 p, vec2 b, vec2 rr){ float r = p.y < 0. ? rr.x : rr.y; vec2 q = abs(p) - b + r, m = max(q, 0.);
|
|
117
|
+
float l = uSquircle > .5 ? pow(pow(m.x, 5.) + pow(m.y, 5.), .2) : length(m);
|
|
118
|
+
return l + min(max(q.x, q.y), 0.) - r; }
|
|
119
|
+
float erf_(float x){ float a = .147, x2 = x * x; return sign(x) * sqrt(1. - exp(-x2 * (1.2732 + a * x2) / (1. + a * x2))); }
|
|
120
|
+
// Brushed stainless: long anisotropic streaks under a soft studio key light.
|
|
121
|
+
vec3 backdrop(vec2 q){
|
|
122
|
+
vec2 u = q / uRes.y;
|
|
123
|
+
float ang = -.035; vec2 d = vec2(cos(ang), sin(ang)), n = vec2(-d.y, d.x);
|
|
124
|
+
float al = dot(q, d), ac = dot(q, n);
|
|
125
|
+
float s = (vn(vec2(al * .0035, ac * .75)) - .5) * .55 + (vn(vec2(al * .012, ac * 2.1 + 9.)) - .5) * .35
|
|
126
|
+
+ (vn(vec2(al * .0012, ac * .16 + 3.)) - .5) * .5 + (vn(vec2(al * .03, ac * 4.7 + 1.)) - .5) * .18;
|
|
127
|
+
// Two softboxes: a wide warm key at left, a narrow cool strip at right.
|
|
128
|
+
float band = exp(-pow((u.x - .2 + .22 * u.y) / .17, 2.));
|
|
129
|
+
float strip = exp(-pow((u.x - .93 + .1 * u.y) / .045, 2.));
|
|
130
|
+
float fall = 1. - .55 * smoothstep(.15, 1.1, length((u - vec2(.3, .2)) * vec2(1., .75)));
|
|
131
|
+
float lum = (.075 + .42 * band + .22 * strip) * fall;
|
|
132
|
+
lum *= 1. + s * (.16 + .5 * band + .4 * strip);
|
|
133
|
+
vec3 tint = mix(vec3(.80, .86, .95), vec3(1., .95, .86), band);
|
|
134
|
+
return tint * lum;
|
|
135
|
+
}
|
|
136
|
+
// Flat print colour: one bold ink, a fine dot screen a shade darker, paper grain. No glow.
|
|
137
|
+
vec3 flatBackdrop(vec2 q){
|
|
138
|
+
float g = uRes.y * .018; vec2 cell = mod(q, g) - g * .5;
|
|
139
|
+
float dot = 1. - smoothstep(g * .07, g * .07 + 1.2, length(cell));
|
|
140
|
+
vec3 c = uBg * (1. - .10 * dot);
|
|
141
|
+
c *= 1. + (h21(floor(q * .5)) - .5) * .035;
|
|
142
|
+
return c;
|
|
143
|
+
}
|
|
144
|
+
float fbm(vec2 p){ return .5 * vn(p) + .25 * vn(p * 2.03 + 7.1) + .125 * vn(p * 4.01 + 3.7) + .125 * vn(p * 8.1 + 1.3); }
|
|
145
|
+
// Soft lens falloff toward the corners, in output space.
|
|
146
|
+
float vignette(vec2 q, float k){ vec2 u = (q - uRes * .5) / uRes.y; return 1. - k * smoothstep(.3, 1., length(u * vec2(1., .82))); }
|
|
147
|
+
// 1. One deep ink with a faint top-left lift.
|
|
148
|
+
vec3 grainBackdrop(vec2 q){
|
|
149
|
+
vec2 u = q / uRes;
|
|
150
|
+
return uBg * vignette(q, .32) * (1. + .07 * (1. - u.y) - .03 * u.x) * (1. + (fbm(q / uRes.y * 5.) - .5) * .05);
|
|
151
|
+
}
|
|
152
|
+
// 2. Risograph: two inks multiply on cream paper. Each ink is laid by a stochastic screen
|
|
153
|
+
// (coverage against a clumpy threshold), the second plate is a few pixels off register.
|
|
154
|
+
float plate(vec2 q, float cov){
|
|
155
|
+
float t = .4 * h21(floor(q / 1.3)) + .6 * vn(q / 1.8);
|
|
156
|
+
return smoothstep(t - .04, t + .04, cov * 1.08 - .04);
|
|
157
|
+
}
|
|
158
|
+
vec3 risoBackdrop(vec2 q){
|
|
159
|
+
vec2 u = q / uRes.y;
|
|
160
|
+
float w = (fbm(u * 1.4 + 2.) - .5) * .3;
|
|
161
|
+
float k = u.y + .35 * u.x / (uRes.x / uRes.y) + w;
|
|
162
|
+
float ca = 1. - smoothstep(.5, .85, k), cb = smoothstep(.42, .78, k);
|
|
163
|
+
float a = plate(q, ca), b = plate(q + vec2(3.5, -2.5), cb);
|
|
164
|
+
vec3 col = uBg * mix(vec3(1), uA, a) * mix(vec3(1), uB, b);
|
|
165
|
+
return col * (1. + (vn(q / 5.) - .5) * .05);
|
|
166
|
+
}
|
|
167
|
+
// 3. Cutting mat: 1 cm grid, bold every 5 cm, 45 degree guides, numbered-ruler ticks along the
|
|
168
|
+
// top and left edges, knife scores and a slightly mottled rubber surface.
|
|
169
|
+
float gridLine(float x, float g, float wpx){ float d = abs(mod(x + g * .5, g) - g * .5); return 1. - smoothstep(wpx * .5, wpx * .5 + 1. / uBgCam.z, d); }
|
|
170
|
+
vec3 matBackdrop(vec2 q){
|
|
171
|
+
float g = uRes.y / 34., px = 1. / uBgCam.z;
|
|
172
|
+
vec2 o = q - vec2(g * .5);
|
|
173
|
+
float fine = max(gridLine(o.x, g, 1.1 * px), gridLine(o.y, g, 1.1 * px));
|
|
174
|
+
float bold = max(gridLine(o.x, g * 5., 2.2 * px), gridLine(o.y, g * 5., 2.2 * px));
|
|
175
|
+
float diag = max(gridLine(o.x - o.y, g * 10. * 1.4142, 1. * px), gridLine(o.x + o.y, g * 10. * 1.4142, 1. * px)) * .5;
|
|
176
|
+
// Ruler ticks hang off the outer lines, longer at every fifth.
|
|
177
|
+
float tick = 0.;
|
|
178
|
+
{ float d = abs(mod(o.x + g * .25, g * .5) - g * .25), len = mod(floor(o.x / (g * .5) + .5), 10.) == 0. ? g * .9 : mod(floor(o.x / (g * .5) + .5), 2.) == 0. ? g * .55 : g * .3;
|
|
179
|
+
tick = max(tick, (1. - smoothstep(.6 * px, .6 * px + px, d)) * step(q.y, g * .55 + len) * step(g * .55, q.y)); }
|
|
180
|
+
{ float d = abs(mod(o.y + g * .25, g * .5) - g * .25), len = mod(floor(o.y / (g * .5) + .5), 10.) == 0. ? g * .9 : mod(floor(o.y / (g * .5) + .5), 2.) == 0. ? g * .55 : g * .3;
|
|
181
|
+
tick = max(tick, (1. - smoothstep(.6 * px, .6 * px + px, d)) * step(q.x, g * .55 + len) * step(g * .55, q.x)); }
|
|
182
|
+
float ink = max(max(fine * .33, bold * .62), max(diag * .4, tick * .7));
|
|
183
|
+
vec3 base = uBg * (1. + (fbm(q / uRes.y * 9.) - .5) * .09) * vignette(q, .25);
|
|
184
|
+
// Knife scores: a few long faint lines, lighter where the rubber was opened.
|
|
185
|
+
float sc = 0.;
|
|
186
|
+
for (int i = 0; i < 7; i++) { float fi = float(i);
|
|
187
|
+
float an = h21(vec2(fi, 3.)) * 3.1416; vec2 dir = vec2(cos(an), sin(an)), c0 = vec2(h21(vec2(fi, 1.)), h21(vec2(fi, 2.))) * uRes;
|
|
188
|
+
float along = dot(q - c0, dir), dist = abs(dot(q - c0, vec2(-dir.y, dir.x)));
|
|
189
|
+
sc = max(sc, (1. - smoothstep(0., 1.2 * px, dist)) * step(abs(along), uRes.y * (.12 + .25 * h21(vec2(fi, 4.)))) * (.5 + .5 * vn(vec2(along * .05, fi)))); }
|
|
190
|
+
base = mix(base, base * 1.25 + .03, sc * .55);
|
|
191
|
+
return mix(base, uA, ink * .85);
|
|
192
|
+
}
|
|
193
|
+
// 4. A photo cover-fitted to the frame, darkened toward the edges.
|
|
194
|
+
vec3 imageBackdrop(vec2 q){
|
|
195
|
+
vec2 u = (q / uRes - .5) / uOver + .5, s = (uRes / uRes.y) / (uImgSize / uImgSize.y);
|
|
196
|
+
vec3 c = texture(uImg, (u - .5) * (s.x > s.y ? vec2(1., s.y / s.x) : vec2(s.x / s.y, 1.)) + .5).rgb;
|
|
197
|
+
return c * uDim * vignette(q, .35);
|
|
198
|
+
}
|
|
199
|
+
// 5. Seven-segment 8: hexagonal-ended bars in a slightly italic cell.
|
|
200
|
+
float seg(vec2 p, vec2 a, vec2 b, float t){
|
|
201
|
+
vec2 d = b - a; float L = length(d); vec2 dir = d / L, n = vec2(-dir.y, dir.x);
|
|
202
|
+
vec2 l = vec2(dot(p - a, dir), dot(p - a, n));
|
|
203
|
+
float x = l.x < L * .5 ? l.x : L - l.x;
|
|
204
|
+
return max(abs(l.y) - t * .5 + max(0., t * .5 - x) , -x);
|
|
205
|
+
}
|
|
206
|
+
float eight(vec2 p, vec2 o, vec2 sz, float t){
|
|
207
|
+
p -= o; p.x -= (sz.y - p.y) * .06;
|
|
208
|
+
float gap = t * .18, x0 = t * .5, x1 = sz.x - t * .5, y0 = t * .5, y1 = sz.y * .5, y2 = sz.y - t * .5;
|
|
209
|
+
float d = 1e9;
|
|
210
|
+
d = min(d, seg(p, vec2(x0 + gap, y0), vec2(x1 - gap, y0), t));
|
|
211
|
+
d = min(d, seg(p, vec2(x0 + gap, y1), vec2(x1 - gap, y1), t));
|
|
212
|
+
d = min(d, seg(p, vec2(x0 + gap, y2), vec2(x1 - gap, y2), t));
|
|
213
|
+
d = min(d, seg(p, vec2(x0, y0 + gap), vec2(x0, y1 - gap), t));
|
|
214
|
+
d = min(d, seg(p, vec2(x1, y0 + gap), vec2(x1, y1 - gap), t));
|
|
215
|
+
d = min(d, seg(p, vec2(x0, y1 + gap), vec2(x0, y2 - gap), t));
|
|
216
|
+
d = min(d, seg(p, vec2(x1, y1 + gap), vec2(x1, y2 - gap), t));
|
|
217
|
+
return d;
|
|
218
|
+
}
|
|
219
|
+
vec3 eightBackdrop(vec2 q){
|
|
220
|
+
float t = uRes.y * .062, px = 1. / uBgCam.z;
|
|
221
|
+
vec2 sz = vec2(uRes.x * .46, uRes.y * .975), o = vec2(uRes.x * .035, uRes.y * .012);
|
|
222
|
+
float d = min(eight(q, o, sz, t), eight(q, o + vec2(uRes.x * .5, 0.), sz, t));
|
|
223
|
+
float ds = min(eight(q - vec2(5., 7.), o, sz, t), eight(q - vec2(5., 7.), o + vec2(uRes.x * .5, 0.), sz, t));
|
|
224
|
+
// Reflector behind the glass: a faint polariser sheen.
|
|
225
|
+
vec3 base = uBg * (1. + .05 * (1. - q.y / uRes.y)) * (1. + (fbm(q / uRes.y * 6.) - .5) * .04);
|
|
226
|
+
base *= 1. - .12 * (1. - smoothstep(-px, px, ds));
|
|
227
|
+
float ink = 1. - smoothstep(-px, px, d);
|
|
228
|
+
return mix(base, uA, ink);
|
|
229
|
+
}
|
|
230
|
+
// 6. Pegboard: tempered hardboard, 1 inch hole grid, holes with a lit lower lip.
|
|
231
|
+
vec3 pegBackdrop(vec2 q){
|
|
232
|
+
float s = uRes.y / 24., r = s * .15, px = 1. / uBgCam.z;
|
|
233
|
+
vec2 cell = mod(q - s * .5, s) - s * .5;
|
|
234
|
+
float fib = fbm(vec2(q.x / uRes.y * 40., q.y / uRes.y * 3.)) * .6 + fbm(q / uRes.y * 14.) * .4;
|
|
235
|
+
vec3 board = uBg * (.92 + .16 * fib) * vignette(q, .3);
|
|
236
|
+
float d = length(cell) - r;
|
|
237
|
+
float hole = 1. - smoothstep(-px, px, d);
|
|
238
|
+
// Inside the hole: dark wall, the top inner edge in shadow, a little light on the bottom.
|
|
239
|
+
vec3 inside = mix(vec3(.07, .05, .035), vec3(.2, .14, .09), smoothstep(-r, r, cell.y) * .6);
|
|
240
|
+
float rim = smoothstep(px * 2.5, 0., d) * (1. - hole);
|
|
241
|
+
vec3 col = mix(board, inside, hole);
|
|
242
|
+
col *= 1. - .25 * rim * smoothstep(0., r, -cell.y);
|
|
243
|
+
col += .12 * rim * smoothstep(0., r, cell.y) * uBg;
|
|
244
|
+
// Drop shadow from each hole's top edge onto the board below it.
|
|
245
|
+
return col * (1. - .12 * (1. - smoothstep(0., r * .7, length(cell - vec2(0., -r * .35)) - r)) * (1. - hole));
|
|
246
|
+
}
|
|
247
|
+
// 7. Deep ink with a broad key light from above left and a faint painted mottle.
|
|
248
|
+
vec3 inkBackdrop(vec2 q){
|
|
249
|
+
vec2 u = (q - uRes * vec2(.28, -.15)) / uRes.y;
|
|
250
|
+
float key = exp(-dot(u, u) * .55);
|
|
251
|
+
float mott = (fbm(q / uRes.y * 2.2 + 4.) - .5) * .06;
|
|
252
|
+
return uBg * (.62 + .55 * key + mott) * vignette(q, .18);
|
|
253
|
+
}
|
|
254
|
+
// 8. Three stops along a diagonal bent by low noise; mixed in linear light so the mids stay rich.
|
|
255
|
+
vec3 lin(vec3 c){ return pow(c, vec3(2.2)); }
|
|
256
|
+
vec3 gradBackdrop(vec2 q){
|
|
257
|
+
vec2 u = q / uRes.y;
|
|
258
|
+
float k = (u.x / (uRes.x / uRes.y)) * .62 + u.y * .55 + (fbm(u * 1.1 + 5.) - .5) * .5 - .08;
|
|
259
|
+
k = clamp(k, 0., 1.);
|
|
260
|
+
vec3 c = k < .5 ? mix(lin(uBg), lin(uA), smoothstep(0., .5, k)) : mix(lin(uA), lin(uB), smoothstep(.5, 1., k));
|
|
261
|
+
return pow(c, vec3(1. / 2.2)) * (1. + (fbm(q / uRes.y * 7.) - .5) * .03);
|
|
262
|
+
}
|
|
263
|
+
// 9. Halftone: a 45 degree screen of separate dots that grow from nothing near the window to
|
|
264
|
+
// about half coverage at the frame corners, with uneven ink at each dot edge and paper tooth.
|
|
265
|
+
vec3 halftoneBackdrop(vec2 q){
|
|
266
|
+
float g = uRes.y / 30., px = 1. / uBgCam.z;
|
|
267
|
+
vec2 c = q - uRes * .5, r = vec2(c.x + c.y, c.y - c.x) * .70710678;
|
|
268
|
+
vec2 cell = mod(r, g) - g * .5, id = floor(r / g);
|
|
269
|
+
vec2 cc = (id + .5) * g; cc = vec2(cc.x - cc.y, cc.x + cc.y) * .70710678;
|
|
270
|
+
vec2 n = cc / uRes * vec2(2., 2.1);
|
|
271
|
+
float t = smoothstep(.75, 1.6, length(n));
|
|
272
|
+
float rad = g * .4 * sqrt(t);
|
|
273
|
+
float d = length(cell) - rad + (vn(q * .3) - .5) * 1.4;
|
|
274
|
+
float ink = (1. - smoothstep(-px, px, d)) * step(.03, t);
|
|
275
|
+
vec3 paper = uBg * (1. + (vn(q * .6) - .5) * .05 + (fbm(q / uRes.y * 4.) - .5) * .05);
|
|
276
|
+
return mix(paper, uA * (1. + (vn(q * .4) - .5) * .25), ink * .94);
|
|
277
|
+
}
|
|
278
|
+
// 10. Walnut: long, nearly straight growth lines with slow cathedral bends, fine pores
|
|
279
|
+
// along the grain, a soft key light from above left and a broad oiled sheen.
|
|
280
|
+
vec3 walnutBackdrop(vec2 q){
|
|
281
|
+
vec2 u = q / uRes.y;
|
|
282
|
+
float bend = (fbm(vec2(u.x * .35, u.y * 1.2) + 3.) - .5) * .9 + .25 * sin(u.x * 1.7 + u.y * 2.);
|
|
283
|
+
float y = u.y + bend * .35;
|
|
284
|
+
float fine = vn(vec2(u.x * 2., y * 260.));
|
|
285
|
+
float med = vn(vec2(u.x * .8, y * 55. + 4.));
|
|
286
|
+
float broad = fbm(vec2(u.x * .6, y * 9.));
|
|
287
|
+
vec3 c = mix(uB, uBg, .25 + .9 * broad);
|
|
288
|
+
c = mix(c, uA, smoothstep(.55, .95, med) * .35);
|
|
289
|
+
c *= .86 + .22 * fine;
|
|
290
|
+
vec2 pc = vec2(q.x / 7., (y * uRes.y) / 1.6); vec2 pi = floor(pc), pf = fract(pc) - .5;
|
|
291
|
+
c *= 1. - .3 * step(.9, h21(pi)) * (1. - smoothstep(.2, .45, abs(pf.y)));
|
|
292
|
+
vec2 k = (q - uRes * vec2(.3, -.15)) / uRes.y;
|
|
293
|
+
float key = exp(-dot(k, k) * .5);
|
|
294
|
+
float sheen = exp(-pow((u.x - .35 - .4 * u.y) / .5, 2.)) * .07;
|
|
295
|
+
return c * (.6 + .65 * key) * vignette(q, .25) + sheen * vec3(1., .85, .7);
|
|
296
|
+
}
|
|
297
|
+
// 11. Sunburst: rays from the frame centre, antialiased in screen space, a soft centre glow-free falloff.
|
|
298
|
+
vec3 burstBackdrop(vec2 q){
|
|
299
|
+
vec2 c = q - uRes * .5;
|
|
300
|
+
float a = atan(c.y, c.x) / 6.2831853 * 36.;
|
|
301
|
+
float f = abs(fract(a) - .5) * 2.;
|
|
302
|
+
float e = fwidth(a) * 2.;
|
|
303
|
+
float ray = smoothstep(.5 - e, .5 + e, f);
|
|
304
|
+
vec3 col = mix(uBg, uA, ray);
|
|
305
|
+
return col * vignette(q, .3) * (1. + (fbm(q / uRes.y * 5.) - .5) * .05);
|
|
306
|
+
}
|
|
307
|
+
vec3 backdropFor(vec2 q){
|
|
308
|
+
if (uMode < .5) return pow(backdrop(q), vec3(1. / 2.2));
|
|
309
|
+
if (uMode < 1.5) return flatBackdrop(q);
|
|
310
|
+
if (uMode < 2.5) return grainBackdrop(q);
|
|
311
|
+
if (uMode < 3.5) return risoBackdrop(q);
|
|
312
|
+
if (uMode < 4.5) return matBackdrop(q);
|
|
313
|
+
if (uMode < 5.5) return imageBackdrop(q);
|
|
314
|
+
if (uMode < 6.5) return eightBackdrop(q);
|
|
315
|
+
if (uMode < 7.5) return pegBackdrop(q);
|
|
316
|
+
if (uMode < 8.5) return inkBackdrop(q);
|
|
317
|
+
if (uMode < 9.5) return gradBackdrop(q);
|
|
318
|
+
if (uMode < 10.5) return halftoneBackdrop(q);
|
|
319
|
+
if (uMode < 11.5) return walnutBackdrop(q);
|
|
320
|
+
return burstBackdrop(q);
|
|
321
|
+
}
|
|
322
|
+
void main(){
|
|
323
|
+
vec2 p = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y);
|
|
324
|
+
vec2 q = (p - uRes * .5) / uCam.z + uCam.xy;
|
|
325
|
+
float px = 1. / uCam.z;
|
|
326
|
+
// The visible shape is the window inset by uInset (left, top, right, bottom); the page still maps to uWin.
|
|
327
|
+
vec2 c = uWin.xy + uWin.zw * .5 + (uInset.xy - uInset.zw) * .5, hb = uWin.zw * .5 - (uInset.xy + uInset.zw) * .5;
|
|
328
|
+
vec3 col = uPlain > .5 ? vec3(1) : backdropFor((p - uRes * .5) / uBgCam.z + uBgCam.xy);
|
|
329
|
+
// Film grain sits on the glass, not the scene: screen space, so it never scales with zoom.
|
|
330
|
+
col *= 1. + (h21(gl_FragCoord.xy * .913 + 17.) + h21(gl_FragCoord.yx * 1.37 + 3.) - 1.) * uGrain;
|
|
331
|
+
if (uPlain < .5) {
|
|
332
|
+
float sh = 0.;
|
|
333
|
+
for (int i = 0; i < 2; i++) { vec4 S = i == 0 ? uSh0 : uSh1;
|
|
334
|
+
float dd = sdBox(q - c - S.xy, hb, uRad);
|
|
335
|
+
sh = 1. - (1. - sh) * (1. - S.w * .5 * (1. - erf_(dd / (S.z * 1.4142)))); }
|
|
336
|
+
col *= 1. - sh;
|
|
337
|
+
}
|
|
338
|
+
float dw = sdBox(q - c, hb, uRad);
|
|
339
|
+
vec2 uv = (q - uWin.xy) / uWin.zw;
|
|
340
|
+
// Recordings blend the previous source frame by subframe time; web takes use uMix 1.
|
|
341
|
+
vec3 pg = uMix > .999 ? texture(uPage, uv).rgb : mix(texture(uPrev, uv).rgb, texture(uPage, uv).rgb, uMix);
|
|
342
|
+
float cov = clamp(.5 - dw / px, 0., 1.);
|
|
343
|
+
col = mix(col, pg, cov);
|
|
344
|
+
// A hairline where glass meets steel, like a real window edge.
|
|
345
|
+
col = mix(col, vec3(0), (uPlain > .5 ? 0. : .16) * clamp(1. - abs(dw / px + .5), 0., 1.));
|
|
346
|
+
o = vec4(col, 1);
|
|
347
|
+
}`;
|
|
348
|
+
|
|
349
|
+
const cursorVert = `#version 300 es
|
|
350
|
+
in vec2 a; uniform vec2 uRes, uTip, uOff, uHot; uniform float uSize, uAng, uPress, uFlip; out vec2 v;
|
|
351
|
+
void main(){
|
|
352
|
+
// uHot: the hotspot in quad space; uFlip mirrors the image (the chrome arrow is drawn mirrored).
|
|
353
|
+
vec2 l = (a - uHot) * uSize * uPress;
|
|
354
|
+
float c = cos(uAng), s = sin(uAng);
|
|
355
|
+
vec2 p = uTip + vec2(c * l.x - s * l.y, s * l.x + c * l.y) + uOff;
|
|
356
|
+
v = vec2(uFlip > .5 ? 1. - a.x : a.x, a.y);
|
|
357
|
+
gl_Position = vec4(p.x / uRes.x * 2. - 1., 1. - p.y / uRes.y * 2., 0, 1);
|
|
358
|
+
}`;
|
|
359
|
+
const cursorFrag = `#version 300 es
|
|
360
|
+
precision highp float; in vec2 v; uniform sampler2D uTex; uniform float uShadow, uBias; out vec4 o;
|
|
361
|
+
void main(){ vec4 t = texture(uTex, v, uBias); o = uShadow > 0. ? vec4(.03, .07, .12, 1) * t.a * uShadow : t; }`;
|
|
362
|
+
|
|
363
|
+
const accumFrag = `#version 300 es
|
|
364
|
+
precision highp float; uniform sampler2D uSub; uniform float uW; uniform vec2 uRes; out vec4 o;
|
|
365
|
+
void main(){ o = vec4(pow(texelFetch(uSub, ivec2(gl_FragCoord.xy), 0).rgb, vec3(2.2)) * uW, uW); }`;
|
|
366
|
+
const finalFrag = `#version 300 es
|
|
367
|
+
precision highp float; uniform sampler2D uAcc; out vec4 o;
|
|
368
|
+
float h(vec2 p){ return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }
|
|
369
|
+
void main(){ vec3 c = pow(texelFetch(uAcc, ivec2(gl_FragCoord.xy), 0).rgb, vec3(1. / 2.2));
|
|
370
|
+
c += (h(gl_FragCoord.xy) + h(gl_FragCoord.yx + 7.) - 1.) / 255.;
|
|
371
|
+
o = vec4(c, 1); }`;
|
|
372
|
+
|
|
373
|
+
function compositorPage(W, H, cursors, plain, bd) {
|
|
374
|
+
return `<!doctype html><meta charset="utf-8"><style>
|
|
375
|
+
html,body{margin:0;background:#000;overflow:hidden}canvas{display:block}
|
|
376
|
+
#cap{position:absolute;left:50%;bottom:${Math.round(H * 0.045)}px;transform:translate(-50%,0);opacity:0;white-space:nowrap;
|
|
377
|
+
display:flex;align-items:center;gap:${Math.round(W * 0.012)}px;padding:${Math.round(W * 0.0125)}px ${Math.round(W * 0.02)}px;border-radius:999px;
|
|
378
|
+
background:rgba(16,18,20,.72);backdrop-filter:blur(14px) saturate(1.2);-webkit-backdrop-filter:blur(14px);
|
|
379
|
+
box-shadow:0 1px 0 rgba(255,255,255,.10) inset,0 0 0 1px rgba(255,255,255,.06),0 10px 30px rgba(0,0,0,.25);
|
|
380
|
+
font:500 ${Math.round(W * 0.0225)}px/1.2 "SF Mono",ui-monospace,Menlo,monospace;letter-spacing:-.01em;color:#f2f3ee;-webkit-font-smoothing:antialiased}
|
|
381
|
+
#cap b{font:600 ${Math.round(W * 0.0175)}px/1 -apple-system,"Helvetica Neue",sans-serif;letter-spacing:.06em;text-transform:uppercase;color:#f5c542}
|
|
382
|
+
#cap i{font-style:normal;color:#9da29a}
|
|
383
|
+
#dbg{position:absolute;left:0;top:0}
|
|
384
|
+
</style><canvas id="c" width="${W}" height="${H}"></canvas><div id="cap"></div><svg id="dbg" width="${W}" height="${H}"></svg>
|
|
385
|
+
<script>
|
|
386
|
+
const W=${W},H=${H},PLAIN=${plain},BD=${JSON.stringify(bd)};
|
|
387
|
+
const gl=c.getContext('webgl2',{antialias:false,preserveDrawingBuffer:true,premultipliedAlpha:false});
|
|
388
|
+
gl.getExtension('EXT_color_buffer_float');
|
|
389
|
+
const sh=(t,s)=>{const x=gl.createShader(t);gl.shaderSource(x,s);gl.compileShader(x);if(!gl.getShaderParameter(x,gl.COMPILE_STATUS))throw Error(gl.getShaderInfoLog(x));return x};
|
|
390
|
+
const prog=(v,f)=>{const p=gl.createProgram();gl.attachShader(p,sh(gl.VERTEX_SHADER,v));gl.attachShader(p,sh(gl.FRAGMENT_SHADER,f));gl.bindAttribLocation(p,0,'a');gl.linkProgram(p);
|
|
391
|
+
const u={};const n=gl.getProgramParameter(p,gl.ACTIVE_UNIFORMS);for(let i=0;i<n;i++){const nm=gl.getActiveUniform(p,i).name;u[nm.replace('[0]','')]=gl.getUniformLocation(p,nm)}return{p,u}};
|
|
392
|
+
const P={scene:prog(${JSON.stringify(vert)},${JSON.stringify(sceneFrag)}),cursor:prog(${JSON.stringify(cursorVert)},${JSON.stringify(cursorFrag)}),
|
|
393
|
+
accum:prog(${JSON.stringify(vert)},${JSON.stringify(accumFrag)}),fin:prog(${JSON.stringify(vert)},${JSON.stringify(finalFrag)})};
|
|
394
|
+
const buf=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,buf);gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([0,0,1,0,0,1,1,1]),gl.STATIC_DRAW);
|
|
395
|
+
gl.enableVertexAttribArray(0);gl.vertexAttribPointer(0,2,gl.FLOAT,false,0,0);
|
|
396
|
+
const tex=(mip)=>{const t=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,t);
|
|
397
|
+
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,mip?gl.LINEAR_MIPMAP_LINEAR:gl.LINEAR);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.LINEAR);
|
|
398
|
+
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);return t};
|
|
399
|
+
const target=fmt=>{const t=tex(false);gl.texImage2D(gl.TEXTURE_2D,0,fmt,W,H,0,gl.RGBA,fmt===gl.RGBA16F?gl.HALF_FLOAT:gl.UNSIGNED_BYTE,null);
|
|
400
|
+
const f=gl.createFramebuffer();gl.bindFramebuffer(gl.FRAMEBUFFER,f);gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,t,0);return{t,f}};
|
|
401
|
+
const sub=target(gl.RGBA8),acc=target(gl.RGBA16F);
|
|
402
|
+
let pageTex=tex(true),prevTex=tex(true);
|
|
403
|
+
// Cursor shapes: {name: {url, hot: [x, y] in the image's 0..1 box, flip}}. The first is the default.
|
|
404
|
+
const CUR=${JSON.stringify(cursors)},curTex={};
|
|
405
|
+
const curReady=Promise.all(Object.entries(CUR).map(([k,c])=>{const t=curTex[k]=tex(true),im=new Image();im.src=c.url;return im.decode().then(()=>{gl.bindTexture(gl.TEXTURE_2D,t);
|
|
406
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,true);gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,im);gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,false);gl.generateMipmap(gl.TEXTURE_2D)})}));
|
|
407
|
+
const curDefault=Object.keys(CUR)[0];
|
|
408
|
+
const imgTex=tex(true);let imgReady=null,imgSize=[1,1];
|
|
409
|
+
if(BD.image){const im=new Image();im.src=BD.image;imgReady=im.decode().then(()=>{imgSize=[im.naturalWidth,im.naturalHeight];gl.bindTexture(gl.TEXTURE_2D,imgTex);
|
|
410
|
+
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,im);gl.generateMipmap(gl.TEXTURE_2D)})}
|
|
411
|
+
window.ready=Promise.all([curReady,imgReady]);
|
|
412
|
+
// keep: the current page becomes the previous one, for subframe blending of recordings.
|
|
413
|
+
window.setPage=async(b64,keep)=>{if(keep)[pageTex,prevTex]=[prevTex,pageTex];const bin=Uint8Array.from(atob(b64),ch=>ch.charCodeAt(0));const bmp=await createImageBitmap(new Blob([bin],{type:'image/jpeg'}));
|
|
414
|
+
gl.bindTexture(gl.TEXTURE_2D,pageTex);gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA,gl.UNSIGNED_BYTE,bmp);gl.generateMipmap(gl.TEXTURE_2D);bmp.close()};
|
|
415
|
+
window.draw=f=>{
|
|
416
|
+
gl.viewport(0,0,W,H);
|
|
417
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER,acc.f);gl.clearColor(0,0,0,0);gl.clear(gl.COLOR_BUFFER_BIT);
|
|
418
|
+
const n=f.subs.length;
|
|
419
|
+
for(const s of f.subs){
|
|
420
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER,sub.f);gl.disable(gl.BLEND);
|
|
421
|
+
let u=P.scene.u;gl.useProgram(P.scene.p);gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,pageTex);gl.uniform1i(u.uPage,0);
|
|
422
|
+
gl.uniform2f(u.uRes,W,H);gl.uniform3f(u.uCam,s.cam.x,s.cam.y,s.cam.z);gl.uniform3f(u.uBgCam,s.bg.x,s.bg.y,s.bg.z);gl.uniform1f(u.uOver,f.over);gl.uniform4f(u.uWin,f.win.x,f.win.y,f.win.w,f.win.h);
|
|
423
|
+
gl.uniform2fv(u.uRad,f.corner.radius);gl.uniform4fv(u.uInset,f.corner.inset);gl.uniform1f(u.uSquircle,f.corner.squircle);gl.uniform1f(u.uPlain,PLAIN);gl.uniform1f(u.uMode,BD.mode);gl.uniform3fv(u.uBg,BD.bg);gl.uniform3fv(u.uA,BD.a);gl.uniform3fv(u.uB,BD.b);gl.uniform1f(u.uGrain,BD.grain);gl.uniform1f(u.uDim,BD.dim);
|
|
424
|
+
gl.activeTexture(gl.TEXTURE1);gl.bindTexture(gl.TEXTURE_2D,imgTex);gl.uniform1i(u.uImg,1);gl.uniform2fv(u.uImgSize,imgSize);gl.activeTexture(gl.TEXTURE2);gl.bindTexture(gl.TEXTURE_2D,s.mix===undefined?pageTex:prevTex);gl.uniform1i(u.uPrev,2);gl.uniform1f(u.uMix,s.mix??1);
|
|
425
|
+
gl.activeTexture(gl.TEXTURE0);gl.uniform4fv(u.uSh0,f.shadow[0]);gl.uniform4fv(u.uSh1,f.shadow[1]);
|
|
426
|
+
gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
|
|
427
|
+
if(s.cursor&&curDefault){u=P.cursor.u;gl.useProgram(P.cursor.p);gl.enable(gl.BLEND);gl.blendFunc(gl.ONE,gl.ONE_MINUS_SRC_ALPHA);
|
|
428
|
+
const k=CUR[s.cursor.shape]?s.cursor.shape:curDefault;gl.bindTexture(gl.TEXTURE_2D,curTex[k]);gl.uniform1i(u.uTex,0);gl.uniform2f(u.uRes,W,H);gl.uniform2fv(u.uHot,CUR[k].hot);gl.uniform1f(u.uFlip,CUR[k].flip?1:0);
|
|
429
|
+
const C=s.cursor;gl.uniform2f(u.uTip,C.x,C.y);gl.uniform1f(u.uSize,C.size);gl.uniform1f(u.uAng,C.angle*Math.PI/180);gl.uniform1f(u.uPress,C.press);
|
|
430
|
+
for(const [oy,bias,a] of [[C.size*.09,2.2,.19],[C.size*.28,3.6,.13],[0,0,0]]){gl.uniform2f(u.uOff,0,oy);gl.uniform1f(u.uBias,bias);gl.uniform1f(u.uShadow,a);gl.drawArrays(gl.TRIANGLE_STRIP,0,4)}}
|
|
431
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER,acc.f);gl.enable(gl.BLEND);gl.blendFunc(gl.ONE,gl.ONE);
|
|
432
|
+
u=P.accum.u;gl.useProgram(P.accum.p);gl.bindTexture(gl.TEXTURE_2D,sub.t);gl.uniform1i(u.uSub,0);gl.uniform1f(u.uW,1/n);gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
|
|
433
|
+
}
|
|
434
|
+
gl.disable(gl.BLEND);gl.bindFramebuffer(gl.FRAMEBUFFER,null);
|
|
435
|
+
const u=P.fin.u;gl.useProgram(P.fin.p);gl.bindTexture(gl.TEXTURE_2D,acc.t);gl.uniform1i(u.uAcc,0);gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
|
|
436
|
+
const cap=document.getElementById('cap');
|
|
437
|
+
if(f.caption){if(cap.dataset.html!==f.caption.html){cap.innerHTML=f.caption.html;cap.dataset.html=f.caption.html}
|
|
438
|
+
cap.style.opacity=f.caption.alpha;cap.style.transform='translate(-50%,'+((1-f.caption.alpha)*10).toFixed(2)+'px)'}else cap.style.opacity=0;
|
|
439
|
+
// Debug marks: a crosshair and a 2 px ring on each point, for checking cursor alignment.
|
|
440
|
+
dbg.innerHTML=(f.marks||[]).map(m=>'<g stroke="#ff2a6d" fill="none" stroke-width="1"><path d="M'+(m.x-14)+' '+m.y+'h10M'+(m.x+4)+' '+m.y+'h10M'+m.x+' '+(m.y-14)+'v10M'+m.x+' '+(m.y+4)+'v10"/><circle cx="'+m.x+'" cy="'+m.y+'" r="2"/></g>').join('');
|
|
441
|
+
gl.finish();
|
|
442
|
+
};
|
|
443
|
+
</script>`;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// Zoom about the window's anchor, then apply the independent pan in screen pixels.
|
|
447
|
+
export function backdropCam(cam, W, H, P, image = false) {
|
|
448
|
+
if (!P) return cam;
|
|
449
|
+
const zb = cam.z ** P.zoom, d = cam.z - 1;
|
|
450
|
+
const anchorFactor = Math.abs(d) < 1e-6 ? P.zoom : cam.z * (1 - 1 / zb) / d;
|
|
451
|
+
const panFactor = P.zoom * P.pan * cam.z / zb;
|
|
452
|
+
const center = (position, pan, half) => {
|
|
453
|
+
const offset = (position - half - pan) * anchorFactor + pan * panFactor;
|
|
454
|
+
// Image backdrops have finite overscan. Procedural backdrops are unbounded.
|
|
455
|
+
const limit = image ? Math.max(0, half * (P.over - 1 / zb)) : Infinity;
|
|
456
|
+
return half + Math.max(-limit, Math.min(limit, offset));
|
|
457
|
+
};
|
|
458
|
+
return { x: center(cam.x, cam.panX ?? 0, W / 2), y: center(cam.y, cam.panY ?? 0, H / 2), z: zb };
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
export async function createStudio(browser, take) {
|
|
462
|
+
const L = layoutFor(take);
|
|
463
|
+
// The take's cursor image is the arrow; recordings also get a hand and an I-beam.
|
|
464
|
+
const cursors = {};
|
|
465
|
+
if (take.cursor) cursors.arrow = { url: `data:image/png;base64,${(await readFile(take.cursor)).toString('base64')}`, hot: [60 / 360, 120 / 360], flip: true };
|
|
466
|
+
else if (take.cursorShapes) cursors.arrow = CURSOR_SHAPES.arrow;
|
|
467
|
+
if (take.cursorShapes) Object.assign(cursors, { pointer: CURSOR_SHAPES.pointer, ibeam: CURSOR_SHAPES.ibeam });
|
|
468
|
+
// Alignment probe: every shape becomes a dot centred on its hotspot.
|
|
469
|
+
if (take.probe) { for (const k in cursors) delete cursors[k]; cursors.arrow = CURSOR_SHAPES.probe; }
|
|
470
|
+
const page = await browser.newPage({ viewport: { width: L.W, height: L.H }, deviceScaleFactor: 1 });
|
|
471
|
+
const errors = []; page.on('pageerror', e => errors.push(e.message));
|
|
472
|
+
const preset = /^#/.test(take.backdrop) ? { mode: 1, bg: take.backdrop, shadow: .8 } : BACKDROPS[take.backdrop] ?? BACKDROPS[DEFAULT_BACKDROP];
|
|
473
|
+
const image = preset.image && `data:image/jpeg;base64,${(await readFile(fileURLToPath(new URL(`../assets/backdrops/${preset.image}`, import.meta.url)))).toString('base64')}`;
|
|
474
|
+
const bd = { mode: preset.mode, image, grain: preset.grain ?? 0, dim: preset.dim ?? 1, ...Object.fromEntries(['bg', 'a', 'b'].map(k => [k, hexRgb(preset[k] ?? '#000000')])) };
|
|
475
|
+
await page.setContent(compositorPage(L.W, L.H, cursors, take.backdrop === 'none' ? 1 : 0, bd));
|
|
476
|
+
await page.evaluate(() => window.ready);
|
|
477
|
+
if (errors.length) throw Error(`Compositor: ${errors.join('\n')}`);
|
|
478
|
+
const P = take.parallax === undefined ? PARALLAX : take.parallax, over = P ? P.over : 1;
|
|
479
|
+
const shadow = (take.shadow ?? LOOK.shadow).map(([x, y, b, a]) => [x * L.H, y * L.H, b * L.H, a * (preset.shadow ?? 1)]);
|
|
480
|
+
return {
|
|
481
|
+
layout: L,
|
|
482
|
+
// Page CSS point to composite point.
|
|
483
|
+
toComposite: (x, y) => ({ x: L.win.x + (x - L.clip.x) * L.s0, y: L.win.y + (y - L.clip.y) * L.s0 }),
|
|
484
|
+
// keepPrev: the last page stays bound as the previous frame (recordings).
|
|
485
|
+
async render(pageJpeg, frame, keepPrev = false) {
|
|
486
|
+
if (pageJpeg) await page.evaluate(([b64, keep]) => window.setPage(b64, keep), [pageJpeg, keepPrev]);
|
|
487
|
+
await page.evaluate(f => window.draw(f), { ...frame, subs: frame.subs.map(s => ({ ...s, bg: backdropCam(s.cam, L.W, L.H, P, !!image) })), win: L.win, corner: L.corner, shadow, over });
|
|
488
|
+
if (errors.length) throw Error(`Compositor: ${errors.join('\n')}`);
|
|
489
|
+
// Playwright's capture covers the whole output; a raw CDP clip came back cropped.
|
|
490
|
+
return (await page.screenshot({ type: 'jpeg', quality: 97 })).toString('base64');
|
|
491
|
+
},
|
|
492
|
+
close: () => page.close(),
|
|
493
|
+
};
|
|
494
|
+
}
|