egaki 0.11.0 → 0.13.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/dist/cli/cli.d.ts.map +1 -1
  3. package/dist/cli/cli.js +171 -2
  4. package/dist/cli/clone-generate.d.ts +1 -0
  5. package/dist/cli/clone-generate.d.ts.map +1 -1
  6. package/dist/cli/clone-generate.js +1 -1
  7. package/dist/cli/generate.d.ts +4 -2
  8. package/dist/cli/generate.d.ts.map +1 -1
  9. package/dist/cli/generate.js +14 -6
  10. package/dist/cli/model-catalog.d.ts.map +1 -1
  11. package/dist/cli/model-catalog.js +158 -5
  12. package/dist/cli/upscale-generate.d.ts +97 -0
  13. package/dist/cli/upscale-generate.d.ts.map +1 -0
  14. package/dist/cli/upscale-generate.js +300 -0
  15. package/dist/cli/upscale-generate.test.d.ts +2 -0
  16. package/dist/cli/upscale-generate.test.d.ts.map +1 -0
  17. package/dist/cli/upscale-generate.test.js +92 -0
  18. package/dist/cli/voice-change-generate.d.ts +39 -0
  19. package/dist/cli/voice-change-generate.d.ts.map +1 -0
  20. package/dist/cli/voice-change-generate.js +123 -0
  21. package/dist/vite/angled-screen-shader.d.ts +4 -39
  22. package/dist/vite/angled-screen-shader.d.ts.map +1 -1
  23. package/dist/vite/angled-screen-shader.js +36 -440
  24. package/dist/vite/angled-screen.d.ts.map +1 -1
  25. package/dist/vite/angled-screen.js +9 -7
  26. package/dist/vite/shader-renderer.d.ts +0 -4
  27. package/dist/vite/shader-renderer.d.ts.map +1 -1
  28. package/dist/vite/shader-renderer.js +2 -66
  29. package/package.json +11 -6
  30. package/src/cli/cli.ts +231 -2
  31. package/src/cli/clone-generate.ts +1 -1
  32. package/src/cli/generate.ts +24 -11
  33. package/src/cli/model-catalog.ts +167 -5
  34. package/src/cli/upscale-generate.test.ts +96 -0
  35. package/src/cli/upscale-generate.ts +395 -0
  36. package/src/cli/voice-change-generate.ts +187 -0
  37. package/src/vite/angled-screen-shader.tsx +45 -498
  38. package/src/vite/angled-screen.tsx +9 -7
  39. package/src/vite/shader-renderer.tsx +7 -78
@@ -5,27 +5,9 @@
5
5
  *
6
6
  * Shader-based successor to BasicAngledScreen (angled-screen.tsx). Children
7
7
  * DOM is captured per frame with the HTML-in-canvas API (texElementImage2D)
8
- * and rendered as a 3D-tilted plane in a WebGL2 fragment shader:
9
- *
10
- * screen pixel → ray from CSS-style perspective camera → ray/plane
11
- * intersection → plane-local UV → sample DOM texture
12
- *
13
- * The intersection distance gives true per-pixel depth, which drives:
14
- * - bokeh blur (sparse 6-blade hex iris, radius grows with distance from
15
- * focus — fewer taps so the aperture shape reads as hard disks instead
16
- * of smoothing into a gaussian)
17
- * - chromatic aberration (radial R/B split, stronger in the bokeh)
18
- * - fog toward backgroundColor (like the original Three.js plugin's
19
- * scene fog, unframer-private/plugin-angled-screen BokehPass setup)
20
- * - film grain (from the plugin's filmGrainShader, u_time-seeded)
21
- *
22
- * Prop names match BasicAngledScreen where semantics carry over:
23
- * perspective/rotateX/rotateY/rotateZ/translateZ are interpreted exactly
24
- * like the CSS transform version. Depth of field uses the exact Three.js
25
- * BokehShader algorithm and the plugin's defaults (aperture 0.5,
26
- * maxblur 0.12, focus at the plane center): blur grows linearly with the
27
- * signed distance from the focus plane and saturates at maxBlur, sampled
28
- * with the 41-tap Martins Upitis ring kernel in screen space.
8
+ * and drawn by the framework-free renderer in @egaki/angled-screen, which
9
+ * holds the shader, the depth of field model and the coordinate notes.
10
+ * This file only adds Remotion timing, tweakpane controls and the export path.
29
11
  *
30
12
  * Requires Chrome 149+ with chrome://flags/#canvas-draw-element enabled.
31
13
  * Falls back to the CSS BasicAngledScreen when unsupported.
@@ -57,12 +39,6 @@
57
39
  * 3. The DOM composer cannot read the transferred placeholder canvas, so
58
40
  * every painted GL frame is blitted into a plain 2D mirror canvas that
59
41
  * the composer picks up like any regular canvas.
60
- *
61
- * Coordinate mapping notes (hard-won, don't re-derive):
62
- * - GL space is y-up, z toward viewer, camera at (0, 0, perspective).
63
- * - CSS rotateX(+)/rotateY(+) match standard GL Rx/Ry; CSS rotateZ is
64
- * visually clockwise (y-down), so we negate it for GL.
65
- * - UNPACK_FLIP_Y_WEBGL=true puts DOM top at v=1, matching y-up plane UVs.
66
42
  */
67
43
 
68
44
  import {
@@ -76,318 +52,34 @@ import {
76
52
  } from 'remotion'
77
53
  import { useCallback, useLayoutEffect, useRef, type CSSProperties, type ReactNode } from 'react'
78
54
  import { useTweakpane } from './tweakpane-hook.tsx'
79
- import { compileGlShader, linkGlProgram, parseHexColor } from './shader-renderer.tsx'
55
+ import {
56
+ ANGLED_SCREEN_DEFAULTS,
57
+ createAngledScreenRenderer,
58
+ type AngledScreenParams,
59
+ type AngledScreenRenderer,
60
+ } from '@egaki/angled-screen/src'
80
61
  import { BasicAngledScreen } from './angled-screen.tsx'
81
62
 
82
- export interface AngledScreenProps {
63
+ /** Shader params (perspective, rotateX/Y/Z, bokeh, fog...) are documented on
64
+ * `AngledScreenParams` in @egaki/angled-screen. */
65
+ export interface AngledScreenProps extends Partial<AngledScreenParams> {
83
66
  children: ReactNode
84
-
85
- // --- 3D transform (same semantics as BasicAngledScreen) ---
86
- /** CSS perspective distance in px. Smaller = more dramatic. Default 1200. */
87
- perspective?: number
88
- /** X-axis rotation in degrees (tilts top/bottom). Default 8. */
89
- rotateX?: number
90
- /** Y-axis rotation in degrees (angles left/right). Default -12. */
91
- rotateY?: number
92
- /** Z-axis rotation in degrees. Default 0. */
93
- rotateZ?: number
94
- /** Shift the plane horizontally in px. Positive = right. Default 0. */
95
- translateX?: number
96
- /** Push the plane forward/back in px. Positive = closer/larger. Default 0. */
97
- translateZ?: number
98
-
99
- // --- Depth of field (Three.js BokehShader model, same as the Framer plugin) ---
100
- /** Enable depth-based bokeh blur. Default true. */
101
- bokeh?: boolean
102
- /** Aperture — bigger values for shallower depth of field. Blur grows
103
- * linearly with distance from the focus plane, scaled by this.
104
- * Same semantics as Three's BokehPass. Default 0.5 (plugin value). */
105
- aperture?: number
106
- /** Max blur in screen-UV units (fraction of frame size), the saturation
107
- * cap of the blur. Same as Three's `maxblur`. Default 0.12 (plugin value). */
108
- maxBlur?: number
109
- /** Focus distance as a fraction of `perspective`. Default 0 = auto: focus
110
- * tracks the nearest visible point of the plane, so the near side stays
111
- * sharp and blur ramps progressively toward the far edge only. */
112
- focus?: number
113
-
114
- // --- Atmosphere (from the original Three.js plugin) ---
115
- /** Fog toward backgroundColor at the far depth (0-1). Default 0.35. */
116
- fog?: number
117
- /** Film grain intensity. Default 0.02. */
118
- grainIntensity?: number
119
- /** Chromatic aberration strength. RGB channels separate along the radial
120
- * axis from frame center, scaled up by local blur so fringing reads
121
- * strongest in the bokeh (like real lens CA on the reference photos).
122
- * 0 = off. Default 0.45. */
123
- chromaticAberration?: number
124
-
125
- /** Background color behind the tilted screen. Default '#000000'. */
126
- backgroundColor?: string
127
-
128
- // --- Content sizing ---
129
67
  /** Width of the inner content wrapper. Default '80%'. */
130
68
  width?: string | number
131
69
  /** Height of the inner content wrapper. Default 'auto'. */
132
70
  height?: string | number
133
-
134
- /** Render normalized depth as grayscale for debugging the DOF ramp. */
135
- debug?: boolean
136
-
137
71
  /** Optional inline style on the outer container. */
138
72
  style?: CSSProperties
139
73
  }
140
74
 
141
- const VERTEX_SHADER = `#version 300 es
142
- precision highp float;
143
- in vec2 a_position;
144
- in vec2 a_texCoord;
145
- out vec2 v_uv;
146
- void main() {
147
- v_uv = a_texCoord;
148
- gl_Position = vec4(a_position, 0.0, 1.0);
149
- }
150
- `
151
-
152
- const FRAGMENT_SHADER = `#version 300 es
153
- precision highp float;
154
-
155
- uniform sampler2D u_tex;
156
- uniform mat3 u_rot; // plane local -> world rotation
157
- uniform vec2 u_planeSize; // logical composition px
158
- uniform float u_perspective; // camera z in px
159
- uniform float u_translateX; // plane center x in px
160
- uniform float u_translateZ; // plane center z in px
161
- uniform float u_depthMin; // nearest corner distance to camera
162
- uniform float u_depthMax; // farthest corner distance to camera
163
- uniform float u_bokehEnabled;
164
- uniform float u_aperture; // Three BokehShader aperture (plugin: 0.5)
165
- uniform float u_maxblur; // Three BokehShader maxblur, screen-UV units (plugin: 0.12)
166
- uniform float u_focusDist; // focus distance in px along the ray
167
- uniform float u_aspect; // width / height, for circular bokeh
168
- uniform float u_fog;
169
- uniform float u_grain;
170
- uniform float u_ca; // chromatic aberration strength
171
- uniform vec4 u_background;
172
- uniform float u_debug;
173
- uniform float u_time;
174
-
175
- in vec2 v_uv;
176
- out vec4 fragColor;
177
-
178
- float rand(vec2 co) {
179
- return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
180
- }
181
-
182
- // Sparse 6-blade hexagonal iris. Far fewer taps than the 41-sample Martins
183
- // Upitis kernel so the aperture SHAPE reads as discrete hex disks instead of
184
- // smoothing into a gaussian. Outer ring is weighted heavier so bright points
185
- // form hard-edged bokeh blobs (the look of real lens DOF on the refs).
186
- // Unit circle coords at radius 1.0 / 0.55.
187
- const vec2 HEX_OUTER[6] = vec2[](
188
- vec2( 1.0000, 0.0000),
189
- vec2( 0.5000, 0.8660),
190
- vec2(-0.5000, 0.8660),
191
- vec2(-1.0000, 0.0000),
192
- vec2(-0.5000, -0.8660),
193
- vec2( 0.5000, -0.8660)
194
- );
195
- const vec2 HEX_MID[6] = vec2[](
196
- vec2( 0.5500, 0.0000),
197
- vec2( 0.2750, 0.4763),
198
- vec2(-0.2750, 0.4763),
199
- vec2(-0.5500, 0.0000),
200
- vec2(-0.2750, -0.4763),
201
- vec2( 0.2750, -0.4763)
202
- );
203
-
204
- // Intersect the camera ray through screen uv suv with the tilted plane.
205
- // Returns ray distance t, or -1.0 on miss. Outputs plane uv + edge coverage.
206
- float intersectPlane(vec2 suv, out vec2 uv, out float coverage) {
207
- vec2 screen = (suv - 0.5) * u_planeSize;
208
- vec3 C = vec3(0.0, 0.0, u_perspective);
209
- vec3 dir = normalize(vec3(screen, 0.0) - C);
210
- vec3 n = u_rot * vec3(0.0, 0.0, 1.0);
211
- vec3 q0 = vec3(u_translateX, 0.0, u_translateZ);
212
- float denom = dot(n, dir);
213
- if (abs(denom) < 1e-6) return -1.0;
214
- float t = dot(n, q0 - C) / denom;
215
- if (t <= 0.0) return -1.0;
216
- vec3 local = transpose(u_rot) * (C + t * dir - q0);
217
- uv = local.xy / u_planeSize + 0.5;
218
- vec2 edgePx = (0.5 - abs(uv - 0.5)) * u_planeSize;
219
- coverage = clamp(min(edgePx.x, edgePx.y) + 0.5, 0.0, 1.0);
220
- return t;
221
- }
222
-
223
- // Full scene color at a screen position: plane composited over background.
224
- // Screen-space sampling like Three's BokehPass (which blurs the rendered
225
- // frame), so blur also softens the plane edges into the background.
226
- vec4 sampleScene(vec2 suv) {
227
- vec2 uv;
228
- float coverage;
229
- float t = intersectPlane(suv, uv, coverage);
230
- if (t < 0.0 || coverage <= 0.0) return u_background;
231
- vec4 c = texture(u_tex, uv);
232
- c = vec4(mix(u_background.rgb, c.rgb, c.a), 1.0);
233
- return mix(u_background, c, coverage);
234
- }
235
-
236
- // Per-channel sample with chromatic aberration: R and B pull apart along
237
- // the radial axis from frame center (classic lens CA purple/cyan fringing).
238
- vec4 sampleSceneCA(vec2 suv, vec2 caOffset) {
239
- float r = sampleScene(suv + caOffset).r;
240
- float g = sampleScene(suv).g;
241
- float b = sampleScene(suv - caOffset).b;
242
- // Coverage/alpha from the green (unshifted) sample keeps silhouette stable.
243
- vec4 mid = sampleScene(suv);
244
- return vec4(r, g, b, mid.a);
245
- }
246
-
247
- void main() {
248
- vec4 bg = u_background;
249
-
250
- vec2 uv;
251
- float coverage;
252
- float t = intersectPlane(v_uv, uv, coverage);
253
- bool hit = t > 0.0 && coverage > 0.0;
254
-
255
- // Normalized depth across the visible plane (0 = near, 1 = far) — used
256
- // for fog and the debug view.
257
- float depth01 = hit
258
- ? clamp((t - u_depthMin) / max(u_depthMax - u_depthMin, 1e-3), 0.0, 1.0)
259
- : 1.0;
260
-
261
- if (u_debug > 0.5) {
262
- fragColor = hit ? mix(bg, vec4(vec3(depth01), 1.0), coverage) : bg;
263
- return;
264
- }
265
-
266
- // Three.js BokehShader blur factor, made one-sided: only content BEYOND
267
- // the focus plane blurs (the far side of the tilted screen). The near side
268
- // stays tack sharp. Background pixels (no hit) count as far away, so the
269
- // plane silhouette melts softly where it is out of focus.
270
- float factor = hit ? max(t - u_focusDist, 0.0) / u_perspective : 1.0;
271
- float blurAmt = clamp(factor * u_aperture, 0.0, u_maxblur) * u_bokehEnabled;
272
- vec2 dofblur = vec2(blurAmt);
273
-
274
- vec2 aspectcorrect = vec2(1.0, u_aspect);
275
-
276
- // Radial CA direction from frame center. Strength grows with local blur so
277
- // sharp regions stay clean and only the bokeh fringes with purple/cyan.
278
- vec2 fromCenter = v_uv - 0.5;
279
- float radial = length(fromCenter);
280
- vec2 caDir = radial > 1e-5 ? fromCenter / radial : vec2(1.0, 0.0);
281
- // Base fringe + blur-scaled fringe. u_ca ~0.45 reads like a real lens on
282
- // the reference photos without turning the whole frame into a prism.
283
- float caAmt = u_ca * (0.0015 + blurAmt * 0.55);
284
- vec2 caOffset = caDir * caAmt * aspectcorrect;
285
-
286
- // Sparse hex gather. Outer ring weight 2.0 so the 6-blade iris is visible
287
- // as hard bokeh disks; mid ring 1.0 fills the disk; center 1.0 anchors.
288
- // Total weight = 6*2 + 6*1 + 1 = 19.
289
- const float W_OUTER = 2.0;
290
- const float W_MID = 1.0;
291
- const float W_CENTER = 1.0;
292
- vec4 col = sampleSceneCA(v_uv, caOffset) * W_CENTER;
293
- for (int i = 0; i < 6; i++) {
294
- col += sampleSceneCA(v_uv + HEX_OUTER[i] * aspectcorrect * dofblur, caOffset) * W_OUTER;
295
- }
296
- for (int i = 0; i < 6; i++) {
297
- col += sampleSceneCA(v_uv + HEX_MID[i] * aspectcorrect * dofblur, caOffset) * W_MID;
298
- }
299
- col /= (6.0 * W_OUTER + 6.0 * W_MID + W_CENTER);
300
-
301
- // Mild highlight bias: bright samples punch through more (classic optical
302
- // bokeh where speculars form bright disks instead of averaging down).
303
- // Applied as a soft lift so it doesn't blow whites on already-bright UI.
304
- float luma = dot(col.rgb, vec3(0.299, 0.587, 0.114));
305
- col.rgb += col.rgb * blurAmt * 1.8 * smoothstep(0.55, 0.95, luma);
306
-
307
- // Fog toward background with depth (plugin's THREE.Fog equivalent)
308
- col.rgb = mix(col.rgb, bg.rgb, u_fog * depth01 * (hit ? 1.0 : 0.0));
309
-
310
- // Film grain (plugin's filmGrainShader)
311
- col.rgb += (rand(v_uv + fract(u_time)) - 0.5) * u_grain;
312
- col.a = 1.0;
313
-
314
- fragColor = col;
315
- }
316
- `
317
-
318
- interface GlState {
319
- gl: WebGL2RenderingContext
320
- program: WebGLProgram
321
- texture: WebGLTexture
322
- locs: Record<string, WebGLUniformLocation | null>
323
- /** The OffscreenCanvas the WebGL context renders into. */
324
- canvas: OffscreenCanvas
325
- }
326
-
327
75
  /** Chromium's html-in-canvas layout canvas exposes a non-standard
328
76
  * requestPaint() (behind chrome://flags/#canvas-draw-element). */
329
77
  type LayoutSubtreeCanvas = HTMLCanvasElement & { requestPaint?: () => void }
330
78
 
331
- const UNIFORM_NAMES = [
332
- 'u_tex',
333
- 'u_rot',
334
- 'u_planeSize',
335
- 'u_perspective',
336
- 'u_translateX',
337
- 'u_translateZ',
338
- 'u_depthMin',
339
- 'u_depthMax',
340
- 'u_bokehEnabled',
341
- 'u_aperture',
342
- 'u_maxblur',
343
- 'u_focusDist',
344
- 'u_aspect',
345
- 'u_fog',
346
- 'u_grain',
347
- 'u_ca',
348
- 'u_background',
349
- 'u_debug',
350
- 'u_time',
351
- ] as const
352
-
353
- /**
354
- * Build the CSS-order rotation matrix M = Rx * Ry * Rz(-z) in GL coords
355
- * (y up, z toward viewer). Returns row-major 3x3 as number[][].
356
- */
357
- function rotationMatrix(rxDeg: number, ryDeg: number, rzDeg: number): number[][] {
358
- const rx = (rxDeg * Math.PI) / 180
359
- const ry = (ryDeg * Math.PI) / 180
360
- const rz = (-rzDeg * Math.PI) / 180
361
- const cx = Math.cos(rx)
362
- const sx = Math.sin(rx)
363
- const cy = Math.cos(ry)
364
- const sy = Math.sin(ry)
365
- const cz = Math.cos(rz)
366
- const sz = Math.sin(rz)
367
- const Rx = [
368
- [1, 0, 0],
369
- [0, cx, -sx],
370
- [0, sx, cx],
371
- ]
372
- const Ry = [
373
- [cy, 0, sy],
374
- [0, 1, 0],
375
- [-sy, 0, cy],
376
- ]
377
- const Rz = [
378
- [cz, -sz, 0],
379
- [sz, cz, 0],
380
- [0, 0, 1],
381
- ]
382
- const mul = (a: number[][], b: number[][]) =>
383
- a.map((row, i) => row.map((_, j) => a[i]![0]! * b[0]![j]! + a[i]![1]! * b[1]![j]! + a[i]![2]! * b[2]![j]!))
384
- return mul(mul(Rx, Ry), Rz)
385
- }
386
-
387
79
  export function AngledScreen(props: AngledScreenProps) {
388
80
  const frame = useCurrentFrame()
389
81
  const { width: compW, height: compH, fps } = useVideoConfig()
390
- const glRef = useRef<GlState | null>(null)
82
+ const glRef = useRef<{ renderer: AngledScreenRenderer; canvas: OffscreenCanvas } | null>(null)
391
83
  // Mirror canvas: the web-renderer's DOM composer (used for exports and SDK
392
84
  // screenshots whenever nested HTML-in-canvas capture is unavailable)
393
85
  // cannot read the HtmlInCanvas offscreen and paints the raw children
@@ -413,35 +105,25 @@ export function AngledScreen(props: AngledScreenProps) {
413
105
  const layoutCanvasRef = useRef<LayoutSubtreeCanvas | null>(null)
414
106
  const pendingPaintHandleRef = useRef<number | null>(null)
415
107
 
108
+ const d = ANGLED_SCREEN_DEFAULTS
416
109
  const tp = useTweakpane('AngledScreen', {
417
- perspective: { value: props.perspective ?? 1200, min: 100, max: 3000, step: 10 },
418
- rotateX: { value: props.rotateX ?? 8, min: -90, max: 90, step: 0.5 },
419
- rotateY: { value: props.rotateY ?? -12, min: -90, max: 90, step: 0.5 },
420
- rotateZ: { value: props.rotateZ ?? 0, min: -180, max: 180, step: 0.5 },
421
- translateX: { value: props.translateX ?? 0, min: -1000, max: 1000, step: 1 },
422
- translateZ: { value: props.translateZ ?? 0, min: -500, max: 500, step: 1 },
423
- bokeh: props.bokeh ?? true,
424
- aperture: { value: props.aperture ?? 0.5, min: 0, max: 2, step: 0.01 },
425
- maxBlur: { value: props.maxBlur ?? 0.12, min: 0, max: 0.3, step: 0.005 },
426
- // 0 = auto: focus tracks the nearest visible point of the plane, so only
427
- // the far side blurs, progressively. Any other value = fraction of
428
- // perspective, like Three's focus uniform.
429
- focus: {
430
- value: props.focus ?? 0,
431
- min: 0,
432
- max: 2,
433
- step: 0.01,
434
- },
435
- fog: { value: props.fog ?? 0.35, min: 0, max: 1, step: 0.01 },
436
- grainIntensity: { value: props.grainIntensity ?? 0.02, min: 0, max: 0.3, step: 0.005 },
437
- chromaticAberration: {
438
- value: props.chromaticAberration ?? 0.45,
439
- min: 0,
440
- max: 2,
441
- step: 0.01,
442
- },
443
- backgroundColor: props.backgroundColor ?? '#000000',
444
- debug: props.debug ?? false,
110
+ perspective: { value: props.perspective ?? d.perspective, min: 100, max: 3000, step: 10 },
111
+ rotateX: { value: props.rotateX ?? d.rotateX, min: -90, max: 90, step: 0.5 },
112
+ rotateY: { value: props.rotateY ?? d.rotateY, min: -90, max: 90, step: 0.5 },
113
+ rotateZ: { value: props.rotateZ ?? d.rotateZ, min: -180, max: 180, step: 0.5 },
114
+ translateX: { value: props.translateX ?? d.translateX, min: -1000, max: 1000, step: 1 },
115
+ translateZ: { value: props.translateZ ?? d.translateZ, min: -500, max: 500, step: 1 },
116
+ bokeh: props.bokeh ?? d.bokeh,
117
+ aperture: { value: props.aperture ?? d.aperture, min: 0, max: 2, step: 0.01 },
118
+ maxBlur: { value: props.maxBlur ?? d.maxBlur, min: 0, max: 0.3, step: 0.005 },
119
+ // 0 = auto: only the far side blurs, progressively. Any other value =
120
+ // fraction of perspective, like Three's focus uniform.
121
+ focus: { value: props.focus ?? d.focus, min: 0, max: 2, step: 0.01 },
122
+ fog: { value: props.fog ?? d.fog, min: 0, max: 1, step: 0.01 },
123
+ grainIntensity: { value: props.grainIntensity ?? d.grainIntensity, min: 0, max: 0.3, step: 0.005 },
124
+ chromaticAberration: { value: props.chromaticAberration ?? d.chromaticAberration, min: 0, max: 2, step: 0.01 },
125
+ backgroundColor: props.backgroundColor ?? d.backgroundColor,
126
+ debug: props.debug ?? d.debug,
445
127
  })
446
128
 
447
129
  const { perspective, rotateX, rotateY, rotateZ, translateX, translateZ } = tp
@@ -450,66 +132,10 @@ export function AngledScreen(props: AngledScreenProps) {
450
132
  const time = frame / fps
451
133
 
452
134
  const onInit: HtmlInCanvasOnInit = useCallback(({ canvas }) => {
453
- const gl = canvas.getContext('webgl2', {
454
- alpha: true,
455
- premultipliedAlpha: true,
456
- antialias: false,
457
- // Required so the mirror canvas (export path) can drawImage() the GL
458
- // buffer after the draw call instead of getting a cleared buffer.
459
- preserveDrawingBuffer: true,
460
- })
461
- if (!gl) {
462
- throw new Error('[AngledScreen] WebGL2 unavailable')
463
- }
464
-
465
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true)
466
-
467
- const vs = compileGlShader(gl, gl.VERTEX_SHADER, VERTEX_SHADER)
468
- const fs = compileGlShader(gl, gl.FRAGMENT_SHADER, FRAGMENT_SHADER)
469
- const program = linkGlProgram(gl, vs, fs)
470
- gl.deleteShader(vs)
471
- gl.deleteShader(fs)
472
-
473
- const texture = gl.createTexture()!
474
- gl.bindTexture(gl.TEXTURE_2D, texture)
475
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
476
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
477
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
478
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
479
-
480
- // Fullscreen quad: interleaved position + uv, 6 vertices
481
- const buffer = gl.createBuffer()!
482
- gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
483
- // prettier-ignore
484
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
485
- -1, -1, 0, 0, 1, -1, 1, 0, -1, 1, 0, 1,
486
- -1, 1, 0, 1, 1, -1, 1, 0, 1, 1, 1, 1,
487
- ]), gl.STATIC_DRAW)
488
-
489
- const vao = gl.createVertexArray()!
490
- gl.bindVertexArray(vao)
491
- const posLoc = gl.getAttribLocation(program, 'a_position')
492
- const uvLoc = gl.getAttribLocation(program, 'a_texCoord')
493
- gl.enableVertexAttribArray(posLoc)
494
- gl.vertexAttribPointer(posLoc, 2, gl.FLOAT, false, 16, 0)
495
- gl.enableVertexAttribArray(uvLoc)
496
- gl.vertexAttribPointer(uvLoc, 2, gl.FLOAT, false, 16, 8)
497
-
498
- const locs: Record<string, WebGLUniformLocation | null> = {}
499
- for (const name of UNIFORM_NAMES) {
500
- locs[name] = gl.getUniformLocation(program, name)
501
- }
502
-
503
- gl.useProgram(program)
504
- gl.bindVertexArray(vao)
505
-
506
- glRef.current = { gl, program, texture, locs, canvas }
507
-
135
+ const renderer = createAngledScreenRenderer({ canvas })
136
+ glRef.current = { renderer, canvas }
508
137
  return () => {
509
- gl.deleteProgram(program)
510
- gl.deleteTexture(texture)
511
- gl.deleteVertexArray(vao)
512
- gl.deleteBuffer(buffer)
138
+ renderer.dispose()
513
139
  glRef.current = null
514
140
  }
515
141
  }, [])
@@ -518,97 +144,18 @@ export function AngledScreen(props: AngledScreenProps) {
518
144
  ({ elementImage }) => {
519
145
  const state = glRef.current
520
146
  if (!state) return
521
- const { gl, locs } = state
522
-
523
- gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight)
524
-
525
- gl.activeTexture(gl.TEXTURE0)
526
- gl.bindTexture(gl.TEXTURE_2D, state.texture)
527
- // The HTML-in-canvas API changed shape between Chromium builds:
528
- // Chrome 149-151: texElementImage2D(target, level, internalformat, format, type, element)
529
- // Chrome 152+: texElementImage2D(target, internalformat, element)
530
- // where internalformat must be sized (RGBA8, SRGB8_ALPHA8, RGBA16F, RGBA32F)
531
- // Detect via Function.length (3 = new signature).
532
- if (gl.texElementImage2D.length <= 3) {
533
- ;(gl.texElementImage2D as any)(gl.TEXTURE_2D, gl.RGBA8, elementImage)
534
- } else {
535
- gl.texElementImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, elementImage)
536
- }
537
-
538
- // CPU-side scene setup: rotation matrix + VISIBLE depth statistics.
539
- // Depth is sampled on a 7x7 grid of rays across the screen and
540
- // summarized with percentiles. Percentiles are area-weighted, which
541
- // matters: rays toward the plane's vanishing direction produce huge t
542
- // values, and using the raw corner min/max would compress the blur
543
- // ramp so much that most of the image reads sharp. Quantiles keep the
544
- // ramp perceptually spread across the screen.
545
- const M = rotationMatrix(rotateX, rotateY, rotateZ)
546
- const hw = compW / 2
547
- const hh = compH / 2
548
- // Plane normal (third column of M)
549
- const nx = M[0]![2]!
550
- const ny = M[1]![2]!
551
- const nz = M[2]![2]!
552
- const depths: number[] = []
553
- for (let gy = 0; gy < 7; gy++) {
554
- for (let gx = 0; gx < 7; gx++) {
555
- // Ray from camera C=(0,0,perspective) through screen point (sx,sy,0)
556
- const sx = ((gx / 6) * 2 - 1) * hw
557
- const sy = ((gy / 6) * 2 - 1) * hh
558
- const len = Math.hypot(sx, sy, perspective)
559
- const denom = (nx * sx + ny * sy - nz * perspective) / len
560
- if (Math.abs(denom) < 1e-6) continue
561
- // t = dot(n, q0 - C) / dot(n, dir), q0 = (translateX,0,translateZ)
562
- const t = (nx * translateX + nz * (translateZ - perspective)) / denom
563
- if (t > 0 && Number.isFinite(t)) depths.push(t)
564
- }
565
- }
566
- depths.sort((a, b) => a - b)
567
- const quantile = (q: number) =>
568
- depths[Math.min(depths.length - 1, Math.round(q * (depths.length - 1)))] ?? perspective
569
- const depthMin = depths.length > 0 ? depths[0]! : perspective
570
- const depthMax = depths.length > 0 ? quantile(0.95) : perspective + 1
571
-
572
- if (locs.u_tex) gl.uniform1i(locs.u_tex, 0)
573
- if (locs.u_rot) {
574
- // GLSL mat3 is column-major; M is row-major → transpose on flatten
575
- // prettier-ignore
576
- gl.uniformMatrix3fv(locs.u_rot, false, [
577
- M[0]![0]!, M[1]![0]!, M[2]![0]!,
578
- M[0]![1]!, M[1]![1]!, M[2]![1]!,
579
- M[0]![2]!, M[1]![2]!, M[2]![2]!,
580
- ])
581
- }
582
- if (locs.u_planeSize) gl.uniform2f(locs.u_planeSize, compW, compH)
583
- if (locs.u_perspective) gl.uniform1f(locs.u_perspective, perspective)
584
- if (locs.u_translateX) gl.uniform1f(locs.u_translateX, translateX)
585
- if (locs.u_translateZ) gl.uniform1f(locs.u_translateZ, translateZ)
586
- if (locs.u_depthMin) gl.uniform1f(locs.u_depthMin, depthMin)
587
- if (locs.u_depthMax) gl.uniform1f(locs.u_depthMax, depthMax)
588
- // focus=0 (auto): the focus plane sits at the 40th depth percentile,
589
- // so roughly the near half of the image reads sharp and blur ramps
590
- // progressively from there toward the far edge. The effective aperture
591
- // is auto-scaled so blur reaches maxBlur at the 95th percentile
592
- // regardless of scene scale (aperture prop acts as a multiplier
593
- // relative to its 0.5 default: higher = saturates earlier).
594
- const focusDist = focus > 0 ? focus * perspective : quantile(0.4)
595
- const apertureEff =
596
- focus > 0
597
- ? aperture
598
- : (aperture / 0.5) * ((maxBlur * perspective) / Math.max(depthMax - focusDist, 1))
599
- if (locs.u_bokehEnabled) gl.uniform1f(locs.u_bokehEnabled, bokeh ? 1 : 0)
600
- if (locs.u_aperture) gl.uniform1f(locs.u_aperture, apertureEff)
601
- if (locs.u_maxblur) gl.uniform1f(locs.u_maxblur, maxBlur)
602
- if (locs.u_focusDist) gl.uniform1f(locs.u_focusDist, focusDist)
603
- if (locs.u_aspect) gl.uniform1f(locs.u_aspect, compW / compH)
604
- if (locs.u_fog) gl.uniform1f(locs.u_fog, fog)
605
- if (locs.u_grain) gl.uniform1f(locs.u_grain, grainIntensity)
606
- if (locs.u_ca) gl.uniform1f(locs.u_ca, chromaticAberration)
607
- if (locs.u_background) gl.uniform4fv(locs.u_background, parseHexColor(backgroundColor))
608
- if (locs.u_debug) gl.uniform1f(locs.u_debug, debug ? 1 : 0)
609
- if (locs.u_time) gl.uniform1f(locs.u_time, time)
610
147
 
611
- gl.drawArrays(gl.TRIANGLES, 0, 6)
148
+ state.renderer.uploadElementImage(elementImage)
149
+ state.renderer.draw({
150
+ width: compW,
151
+ height: compH,
152
+ time,
153
+ params: {
154
+ perspective, rotateX, rotateY, rotateZ, translateX, translateZ,
155
+ bokeh, aperture, maxBlur, focus, fog, grainIntensity, chromaticAberration,
156
+ backgroundColor, debug,
157
+ },
158
+ })
612
159
 
613
160
  // Export path: mirror the GL frame into the overlay 2D canvas so the
614
161
  // DOM composer (used when nested HTML-in-canvas capture is
@@ -25,6 +25,7 @@
25
25
  */
26
26
 
27
27
  import { type CSSProperties, type ReactNode } from 'react'
28
+ import { ANGLED_SCREEN_DEFAULTS } from '@egaki/angled-screen/src'
28
29
  import { useTweakpane } from './tweakpane-hook.tsx'
29
30
 
30
31
  export interface BasicAngledScreenProps {
@@ -105,19 +106,20 @@ export function BasicAngledScreen(props: BasicAngledScreenProps) {
105
106
  children,
106
107
  transformOrigin = '50% 50%',
107
108
  bokehDirection,
108
- backgroundColor = '#000000',
109
+ backgroundColor = ANGLED_SCREEN_DEFAULTS.backgroundColor,
109
110
  width = '80%',
110
111
  height = 'auto',
111
112
  style,
112
113
  } = props
113
114
 
115
+ const d = ANGLED_SCREEN_DEFAULTS
114
116
  const tp = useTweakpane('BasicAngledScreen', {
115
- perspective: { value: props.perspective ?? 1200, min: 100, max: 3000, step: 10 },
116
- rotateX: { value: props.rotateX ?? 8, min: -90, max: 90, step: 0.5 },
117
- rotateY: { value: props.rotateY ?? -12, min: -90, max: 90, step: 0.5 },
118
- rotateZ: { value: props.rotateZ ?? 0, min: -180, max: 180, step: 0.5 },
119
- translateZ: { value: props.translateZ ?? 0, min: -500, max: 500, step: 1 },
120
- bokeh: props.bokeh ?? true,
117
+ perspective: { value: props.perspective ?? d.perspective, min: 100, max: 3000, step: 10 },
118
+ rotateX: { value: props.rotateX ?? d.rotateX, min: -90, max: 90, step: 0.5 },
119
+ rotateY: { value: props.rotateY ?? d.rotateY, min: -90, max: 90, step: 0.5 },
120
+ rotateZ: { value: props.rotateZ ?? d.rotateZ, min: -180, max: 180, step: 0.5 },
121
+ translateZ: { value: props.translateZ ?? d.translateZ, min: -500, max: 500, step: 1 },
122
+ bokeh: props.bokeh ?? d.bokeh,
121
123
  bokehBlur: { value: props.bokehBlur ?? 12, min: 0, max: 50, step: 0.5 },
122
124
  bokehOffset: { value: props.bokehOffset ?? 0.3, min: 0, max: 1, step: 0.01 },
123
125
  debug: props.debug ?? false,