@blackbelt-technology/pi-dashboard-deck3d 0.0.0-stage → 0.9.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 (260) hide show
  1. package/.pi/skills/deck3d/SKILL.md +227 -0
  2. package/.pi/skills/deck3d/examples/deck.json +388 -0
  3. package/.pi/skills/deck3d/examples/fixture.md +18 -0
  4. package/.pi/skills/deck3d/examples/snapshot-after.png +0 -0
  5. package/.pi/skills/deck3d/examples/snapshot-before.png +0 -0
  6. package/.pi/skills/deck3d/reference/effects.agent.md +78 -0
  7. package/.pi/skills/deck3d/reference/effects.md +1217 -0
  8. package/.pi/skills/deck3d/reference/ir-fields.agent.md +30 -0
  9. package/.pi/skills/deck3d/reference/ir-fields.md +246 -0
  10. package/README.md +177 -2
  11. package/assets/LICENSE-Poppins.txt +99 -0
  12. package/assets/Poppins-Bold.ttf +0 -0
  13. package/assets/props/LICENSES.txt +4 -0
  14. package/assets/props/brain.glb +0 -0
  15. package/assets/props/cloud.glb +0 -0
  16. package/assets/props/cube.glb +0 -0
  17. package/assets/props/database.glb +0 -0
  18. package/assets/props/laptop.glb +0 -0
  19. package/assets/props/manifest.json +119 -0
  20. package/assets/props/robot.glb +0 -0
  21. package/assets/props/server.glb +0 -0
  22. package/assets/props/shield.glb +0 -0
  23. package/bin/deck3d +49 -0
  24. package/dist/cli.js +10228 -0
  25. package/dist/harvest/harness.js +185718 -0
  26. package/dist/runtime.js +64597 -0
  27. package/fixtures/business-2031/build-deck.mjs +60 -0
  28. package/fixtures/business-2031/build-overrides.mjs +154 -0
  29. package/fixtures/business-2031/deck.json +4220 -0
  30. package/fixtures/business-2031/deck.md +470 -0
  31. package/fixtures/business-2031/fx/closing-mark.js +43 -0
  32. package/fixtures/business-2031/fx/closing-mark.meta.json +26 -0
  33. package/fixtures/business-2031/overrides.json +903 -0
  34. package/fixtures/harvest.md +23 -0
  35. package/fixtures/strategy-lab.json +891 -0
  36. package/fixtures/strategy-lab.md +85 -0
  37. package/package.json +80 -4
  38. package/scripts/build-cli.mjs +29 -0
  39. package/scripts/build-harvest.ts +8 -0
  40. package/scripts/build-runtime.mjs +22 -0
  41. package/scripts/gen-effects.ts +14 -0
  42. package/scripts/gen-ir-fields.ts +34 -0
  43. package/src/check/index.ts +315 -0
  44. package/src/check/rules.ts +393 -0
  45. package/src/cli-io.ts +23 -0
  46. package/src/cli.ts +823 -0
  47. package/src/fx/accent-cycle.meta.json +26 -0
  48. package/src/fx/accent-cycle.ts +8 -0
  49. package/src/fx/agent-depth.meta.json +42 -0
  50. package/src/fx/agent-depth.ts +62 -0
  51. package/src/fx/ascii.meta.json +35 -0
  52. package/src/fx/ascii.ts +4 -0
  53. package/src/fx/aurora.meta.json +26 -0
  54. package/src/fx/aurora.ts +18 -0
  55. package/src/fx/billboards.meta.json +34 -0
  56. package/src/fx/billboards.ts +67 -0
  57. package/src/fx/bloom.meta.json +26 -0
  58. package/src/fx/bloom.ts +4 -0
  59. package/src/fx/camera-drift.meta.json +26 -0
  60. package/src/fx/camera-drift.ts +8 -0
  61. package/src/fx/capital-flows.meta.json +30 -0
  62. package/src/fx/capital-flows.ts +64 -0
  63. package/src/fx/catalogue.ts +89 -0
  64. package/src/fx/chromatic-aberration.meta.json +32 -0
  65. package/src/fx/chromatic-aberration.ts +4 -0
  66. package/src/fx/city-grid.meta.json +37 -0
  67. package/src/fx/city-grid.ts +51 -0
  68. package/src/fx/clipped-solids.meta.json +49 -0
  69. package/src/fx/clipped-solids.ts +171 -0
  70. package/src/fx/cohort-grid.meta.json +48 -0
  71. package/src/fx/cohort-grid.ts +63 -0
  72. package/src/fx/compose.ts +114 -0
  73. package/src/fx/constellation.meta.json +51 -0
  74. package/src/fx/constellation.ts +103 -0
  75. package/src/fx/curve-flow.meta.json +40 -0
  76. package/src/fx/curve-flow.ts +63 -0
  77. package/src/fx/dashed-flow.meta.json +27 -0
  78. package/src/fx/dashed-flow.ts +8 -0
  79. package/src/fx/data-columns.meta.json +30 -0
  80. package/src/fx/data-columns.ts +18 -0
  81. package/src/fx/defaults.ts +92 -0
  82. package/src/fx/depth-of-field.meta.json +40 -0
  83. package/src/fx/depth-of-field.ts +4 -0
  84. package/src/fx/dolly.meta.json +26 -0
  85. package/src/fx/dolly.ts +4 -0
  86. package/src/fx/dot-screen.meta.json +35 -0
  87. package/src/fx/dot-screen.ts +4 -0
  88. package/src/fx/dynamic-instances.meta.json +40 -0
  89. package/src/fx/dynamic-instances.ts +57 -0
  90. package/src/fx/emissive.meta.json +26 -0
  91. package/src/fx/emissive.ts +8 -0
  92. package/src/fx/extruded-shapes.meta.json +62 -0
  93. package/src/fx/extruded-shapes.ts +133 -0
  94. package/src/fx/fab-wafer.meta.json +42 -0
  95. package/src/fx/fab-wafer.ts +72 -0
  96. package/src/fx/fade.meta.json +26 -0
  97. package/src/fx/fade.ts +4 -0
  98. package/src/fx/film.meta.json +26 -0
  99. package/src/fx/film.ts +4 -0
  100. package/src/fx/float.meta.json +26 -0
  101. package/src/fx/float.ts +8 -0
  102. package/src/fx/flythrough.meta.json +26 -0
  103. package/src/fx/flythrough.ts +4 -0
  104. package/src/fx/fog.meta.json +26 -0
  105. package/src/fx/fog.ts +8 -0
  106. package/src/fx/geo-fragments.meta.json +48 -0
  107. package/src/fx/geo-fragments.ts +59 -0
  108. package/src/fx/glass.meta.json +26 -0
  109. package/src/fx/glass.ts +8 -0
  110. package/src/fx/globe-arcs.meta.json +36 -0
  111. package/src/fx/globe-arcs.ts +67 -0
  112. package/src/fx/glow-tube.meta.json +26 -0
  113. package/src/fx/glow-tube.ts +8 -0
  114. package/src/fx/glyph-rain.meta.json +30 -0
  115. package/src/fx/glyph-rain.ts +18 -0
  116. package/src/fx/god-rays.meta.json +40 -0
  117. package/src/fx/god-rays.ts +4 -0
  118. package/src/fx/grid-horizon.meta.json +30 -0
  119. package/src/fx/grid-horizon.ts +18 -0
  120. package/src/fx/hex-grid.meta.json +29 -0
  121. package/src/fx/hex-grid.ts +18 -0
  122. package/src/fx/holo-fresnel.meta.json +26 -0
  123. package/src/fx/holo-fresnel.ts +8 -0
  124. package/src/fx/horizon-gates.meta.json +36 -0
  125. package/src/fx/horizon-gates.ts +60 -0
  126. package/src/fx/index.ts +258 -0
  127. package/src/fx/iridescent.meta.json +26 -0
  128. package/src/fx/iridescent.ts +8 -0
  129. package/src/fx/iris.meta.json +26 -0
  130. package/src/fx/iris.ts +4 -0
  131. package/src/fx/lightformers.meta.json +26 -0
  132. package/src/fx/lightformers.ts +8 -0
  133. package/src/fx/local.ts +200 -0
  134. package/src/fx/market-tape.meta.json +53 -0
  135. package/src/fx/market-tape.ts +97 -0
  136. package/src/fx/matcap.meta.json +26 -0
  137. package/src/fx/matcap.ts +8 -0
  138. package/src/fx/meta.schema.json +49 -0
  139. package/src/fx/metal.meta.json +26 -0
  140. package/src/fx/metal.ts +8 -0
  141. package/src/fx/mirror-floor.meta.json +26 -0
  142. package/src/fx/mirror-floor.ts +8 -0
  143. package/src/fx/neural-mesh.meta.json +54 -0
  144. package/src/fx/neural-mesh.ts +110 -0
  145. package/src/fx/orbit-agents.meta.json +36 -0
  146. package/src/fx/orbit-agents.ts +63 -0
  147. package/src/fx/orbit.meta.json +27 -0
  148. package/src/fx/orbit.ts +8 -0
  149. package/src/fx/outline.meta.json +43 -0
  150. package/src/fx/outline.ts +4 -0
  151. package/src/fx/paper-stack.meta.json +36 -0
  152. package/src/fx/paper-stack.ts +61 -0
  153. package/src/fx/particle-stream.meta.json +27 -0
  154. package/src/fx/particle-stream.ts +8 -0
  155. package/src/fx/particles.meta.json +26 -0
  156. package/src/fx/particles.ts +8 -0
  157. package/src/fx/pixelate.meta.json +28 -0
  158. package/src/fx/pixelate.ts +4 -0
  159. package/src/fx/points-on-geometry.meta.json +61 -0
  160. package/src/fx/points-on-geometry.ts +71 -0
  161. package/src/fx/proof-gate.meta.json +30 -0
  162. package/src/fx/proof-gate.ts +67 -0
  163. package/src/fx/rings.meta.json +27 -0
  164. package/src/fx/rings.ts +8 -0
  165. package/src/fx/room-ibl.meta.json +26 -0
  166. package/src/fx/room-ibl.ts +8 -0
  167. package/src/fx/sao.meta.json +32 -0
  168. package/src/fx/sao.ts +4 -0
  169. package/src/fx/scaffold.ts +172 -0
  170. package/src/fx/scatter.meta.json +49 -0
  171. package/src/fx/scatter.ts +74 -0
  172. package/src/fx/selective-bloom.meta.json +36 -0
  173. package/src/fx/selective-bloom.ts +4 -0
  174. package/src/fx/server-racks.meta.json +36 -0
  175. package/src/fx/server-racks.ts +54 -0
  176. package/src/fx/shader-particles.meta.json +46 -0
  177. package/src/fx/shader-particles.ts +75 -0
  178. package/src/fx/signal-pulse.meta.json +27 -0
  179. package/src/fx/signal-pulse.ts +8 -0
  180. package/src/fx/smaa.meta.json +19 -0
  181. package/src/fx/smaa.ts +4 -0
  182. package/src/fx/sobel.meta.json +24 -0
  183. package/src/fx/sobel.ts +4 -0
  184. package/src/fx/soft-shadows.meta.json +26 -0
  185. package/src/fx/soft-shadows.ts +8 -0
  186. package/src/fx/sprites.meta.json +40 -0
  187. package/src/fx/sprites.ts +57 -0
  188. package/src/fx/stagger-reveal.meta.json +26 -0
  189. package/src/fx/stagger-reveal.ts +8 -0
  190. package/src/fx/starfield.meta.json +32 -0
  191. package/src/fx/starfield.ts +18 -0
  192. package/src/fx/swarm.meta.json +27 -0
  193. package/src/fx/swarm.ts +8 -0
  194. package/src/fx/tessellate.meta.json +39 -0
  195. package/src/fx/tessellate.ts +79 -0
  196. package/src/fx/tokens.meta.json +27 -0
  197. package/src/fx/tokens.ts +8 -0
  198. package/src/fx/trail.meta.json +27 -0
  199. package/src/fx/trail.ts +8 -0
  200. package/src/fx/trust-ledger.meta.json +30 -0
  201. package/src/fx/trust-ledger.ts +47 -0
  202. package/src/fx/types.ts +83 -0
  203. package/src/fx/util.ts +108 -0
  204. package/src/fx/vault-glyphs.meta.json +37 -0
  205. package/src/fx/vault-glyphs.ts +55 -0
  206. package/src/fx/video-screen.meta.json +57 -0
  207. package/src/fx/video-screen.ts +99 -0
  208. package/src/fx/vignette.meta.json +32 -0
  209. package/src/fx/vignette.ts +4 -0
  210. package/src/fx/volume-cloud.meta.json +58 -0
  211. package/src/fx/volume-cloud.ts +87 -0
  212. package/src/fx/volume-perlin.meta.json +46 -0
  213. package/src/fx/volume-perlin.ts +82 -0
  214. package/src/fx/volumetric-spot.meta.json +26 -0
  215. package/src/fx/volumetric-spot.ts +8 -0
  216. package/src/fx/wireframe-overlay.meta.json +27 -0
  217. package/src/fx/wireframe-overlay.ts +8 -0
  218. package/src/ir/defaults.ts +62 -0
  219. package/src/ir/field-reference.ts +96 -0
  220. package/src/ir/hash.ts +34 -0
  221. package/src/ir/ids.ts +65 -0
  222. package/src/ir/merge.ts +155 -0
  223. package/src/ir/schema.json +1211 -0
  224. package/src/ir/types.ts +333 -0
  225. package/src/ir/validate.ts +196 -0
  226. package/src/parse/derive.ts +205 -0
  227. package/src/parse/harvest/bundle.ts +56 -0
  228. package/src/parse/harvest/harness.html +22 -0
  229. package/src/parse/harvest/harness.ts +191 -0
  230. package/src/parse/harvest/index.ts +169 -0
  231. package/src/parse/harvest/shapes.ts +75 -0
  232. package/src/parse/markdown.ts +215 -0
  233. package/src/props/embed.ts +92 -0
  234. package/src/props/fetch.ts +97 -0
  235. package/src/props/generate.ts +152 -0
  236. package/src/props/key.ts +13 -0
  237. package/src/props/search.ts +180 -0
  238. package/src/render/embed-video.ts +80 -0
  239. package/src/render/font.ts +86 -0
  240. package/src/render/index.ts +149 -0
  241. package/src/render/template.html +87 -0
  242. package/src/runtime/backgrounds.ts +146 -0
  243. package/src/runtime/builders.ts +808 -0
  244. package/src/runtime/camera.ts +73 -0
  245. package/src/runtime/hud.ts +887 -0
  246. package/src/runtime/index.ts +1168 -0
  247. package/src/runtime/local-fx.ts +151 -0
  248. package/src/runtime/materials.ts +136 -0
  249. package/src/runtime/measure.ts +73 -0
  250. package/src/runtime/node-geometry.ts +26 -0
  251. package/src/runtime/palette.ts +111 -0
  252. package/src/runtime/post.ts +649 -0
  253. package/src/runtime/props.ts +292 -0
  254. package/src/runtime/quality.ts +20 -0
  255. package/src/runtime/rng.ts +8 -0
  256. package/src/runtime/scene.ts +644 -0
  257. package/src/runtime/text.ts +174 -0
  258. package/src/runtime/types.ts +118 -0
  259. package/src/serve/index.ts +376 -0
  260. package/src/util/paths.ts +35 -0
@@ -0,0 +1,649 @@
1
+ /**
2
+ * Post-processing stack (task 17.x / spec deck3d-effects "Post-processing
3
+ * cards drive real composer passes").
4
+ *
5
+ * One slot per `post` corpus card. Passes are created lazily the first time a
6
+ * slide lists the card, then enabled/disabled per slide; their uniforms are
7
+ * refreshed from the card params on every `setActive`. Order is CANONICAL
8
+ * (`POST_ORDER`), never the order of `effects[]`, so two lists with the same
9
+ * members render byte-identically.
10
+ *
11
+ * Determinism rules every slot obeys:
12
+ * - no pass reads wall-clock time; `film` gets the deck clock through
13
+ * `render(t)` and `god-rays` projects the rim light with the frame camera;
14
+ * - nothing here allocates per frame beyond what three's own passes do.
15
+ */
16
+ import * as THREE from "three";
17
+ import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
18
+ import { BokehPass } from "three/examples/jsm/postprocessing/BokehPass.js";
19
+ import { FilmPass } from "three/examples/jsm/postprocessing/FilmPass.js";
20
+ import { OutlinePass } from "three/examples/jsm/postprocessing/OutlinePass.js";
21
+ import { OutputPass } from "three/examples/jsm/postprocessing/OutputPass.js";
22
+ import { Pass, FullScreenQuad } from "three/examples/jsm/postprocessing/Pass.js";
23
+ import { SAOPass } from "three/examples/jsm/postprocessing/SAOPass.js";
24
+ import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
25
+ import { SMAAPass } from "three/examples/jsm/postprocessing/SMAAPass.js";
26
+ import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPass.js";
27
+ import { LuminosityShader } from "three/examples/jsm/shaders/LuminosityShader.js";
28
+ import { RGBShiftShader } from "three/examples/jsm/shaders/RGBShiftShader.js";
29
+ import { SobelOperatorShader } from "three/examples/jsm/shaders/SobelOperatorShader.js";
30
+ import { VignetteShader } from "three/examples/jsm/shaders/VignetteShader.js";
31
+ import type { FxParams } from "../fx/types.js";
32
+ import type { PaletteColors } from "./palette.js";
33
+
34
+ /** Canonical pass order, first to last. `ascii` is a DOM mode, not a pass. */
35
+ export const POST_ORDER = [
36
+ "sao",
37
+ "outline",
38
+ "selective-bloom",
39
+ "bloom",
40
+ "depth-of-field",
41
+ "god-rays",
42
+ "film",
43
+ "chromatic-aberration",
44
+ "dot-screen",
45
+ "sobel",
46
+ "pixelate",
47
+ "vignette",
48
+ "smaa",
49
+ ] as const;
50
+ export type PostId = (typeof POST_ORDER)[number] | "ascii";
51
+ export const POST_IDS: readonly PostId[] = [...POST_ORDER, "ascii"];
52
+
53
+ export type PartName = "diagram" | "title" | "props" | "all";
54
+
55
+ /** What the stack needs from the rig to apply a slide's post list. */
56
+ export interface PostLook {
57
+ mode: "dark" | "light";
58
+ palette: PaletteColors;
59
+ /** Root objects of the named part on the current slide (for selective passes). */
60
+ parts: (name: PartName) => THREE.Object3D[];
61
+ /** World position of the light `god-rays` streams from. */
62
+ sun: () => THREE.Vector3;
63
+ /** Camera → current slide distance: the `depth-of-field` auto focus. */
64
+ focusDistance: () => number;
65
+ }
66
+
67
+ export interface PostRef {
68
+ id: string;
69
+ params: FxParams;
70
+ }
71
+
72
+ interface Slot {
73
+ id: PostId;
74
+ /** Composer pass names, in order (one slot may own two passes). */
75
+ names: string[];
76
+ passes: Pass[];
77
+ enabled: boolean;
78
+ params: FxParams;
79
+ apply: (params: FxParams, look: PostLook) => void;
80
+ /** Per-frame hook for passes that depend on the camera or the deck clock. */
81
+ tick?: (t: number, camera: THREE.Camera) => void;
82
+ dispose: () => void;
83
+ }
84
+
85
+ export interface PostStack {
86
+ composer: EffectComposer;
87
+ /** Enable exactly `refs` (plus baseline bloom when the profile asks for it); everything else off. */
88
+ setActive: (refs: PostRef[], look: PostLook) => void;
89
+ /** Pass names as `effects().active` reports them: RenderPass, enabled passes, OutputPass. */
90
+ passNames: () => string[];
91
+ probe: () => Array<{ id: string; enabled: boolean; params: FxParams }>;
92
+ /** True when at least one pass beyond the render pass is on (else the rig may draw directly). */
93
+ any: () => boolean;
94
+ ascii: () => boolean;
95
+ /** Feed the deck clock + camera to time-/view-dependent passes. Call before `composer.render()`. */
96
+ tick: (t: number, camera: THREE.Camera) => void;
97
+ /** Draw the ASCII overlay from the framebuffer just rendered. */
98
+ drawAscii: (canvas: HTMLCanvasElement) => void;
99
+ resize: (w: number, h: number) => void;
100
+ }
101
+
102
+ const num = (p: FxParams, k: string, d: number): number => (typeof p[k] === "number" ? (p[k] as number) : d);
103
+ const str = (p: FxParams, k: string, d: string): string => (typeof p[k] === "string" ? (p[k] as string) : d);
104
+
105
+ // ---------------------------------------------------------------------------
106
+ // Custom passes
107
+ // ---------------------------------------------------------------------------
108
+
109
+ /** UV-quantising pixelate. NOT `RenderPixelatedPass`: that one replaces the render pass and would drop the backdrop isolation. */
110
+ const PixelateShader = {
111
+ uniforms: { tDiffuse: { value: null }, resolution: { value: new THREE.Vector2(1, 1) }, size: { value: 8 } },
112
+ vertexShader: "varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }",
113
+ fragmentShader: [
114
+ "uniform sampler2D tDiffuse; uniform vec2 resolution; uniform float size; varying vec2 vUv;",
115
+ "void main(){",
116
+ " vec2 cell = size / resolution;",
117
+ " vec2 uv = (floor(vUv / cell) + 0.5) * cell;",
118
+ " gl_FragColor = texture2D(tDiffuse, uv);",
119
+ "}",
120
+ ].join("\n"),
121
+ };
122
+
123
+ /** Radial light shafts streaming from a screen-space sun, luminance-masked. One pass; deterministic. */
124
+ const GodRaysShader = {
125
+ uniforms: {
126
+ tDiffuse: { value: null },
127
+ sun: { value: new THREE.Vector2(0.5, 0.5) },
128
+ intensity: { value: 0.6 },
129
+ decay: { value: 0.96 },
130
+ density: { value: 0.6 },
131
+ threshold: { value: 0.55 },
132
+ },
133
+ vertexShader: "varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }",
134
+ fragmentShader: [
135
+ "uniform sampler2D tDiffuse; uniform vec2 sun; uniform float intensity, decay, density, threshold; varying vec2 vUv;",
136
+ "const int SAMPLES = 64;",
137
+ "void main(){",
138
+ " vec4 base = texture2D(tDiffuse, vUv);",
139
+ " vec2 d = (vUv - sun) * density / float(SAMPLES);",
140
+ " vec2 uv = vUv; float w = 1.0; vec3 acc = vec3(0.0);",
141
+ " for (int i = 0; i < SAMPLES; i++) {",
142
+ " uv -= d;",
143
+ " vec3 s = texture2D(tDiffuse, uv).rgb;",
144
+ " float l = dot(s, vec3(0.2126, 0.7152, 0.0722));",
145
+ " acc += s * max(0.0, l - threshold) * w;",
146
+ " w *= decay;",
147
+ " }",
148
+ " gl_FragColor = vec4(base.rgb + acc * intensity / float(SAMPLES) * 8.0, base.a);",
149
+ "}",
150
+ ].join("\n"),
151
+ };
152
+
153
+ /** Three's `DotScreenShader` with its output clamped: unclamped, dark pixels go negative and tone-map to white. */
154
+ const DotScreenClampedShader = {
155
+ uniforms: { tDiffuse: { value: null }, tSize: { value: new THREE.Vector2(256, 256) }, center: { value: new THREE.Vector2(0.5, 0.5) }, angle: { value: 1.57 }, scale: { value: 1.0 } },
156
+ vertexShader: "varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }",
157
+ fragmentShader: [
158
+ "uniform vec2 center; uniform float angle; uniform float scale; uniform vec2 tSize; uniform sampler2D tDiffuse; varying vec2 vUv;",
159
+ "float pattern() {",
160
+ " float s = sin(angle), c = cos(angle);",
161
+ " vec2 tex = vUv * tSize - center;",
162
+ " vec2 point = vec2(c * tex.x - s * tex.y, s * tex.x + c * tex.y) * scale;",
163
+ " return (sin(point.x) * sin(point.y)) * 4.0;",
164
+ "}",
165
+ "void main() {",
166
+ " vec4 color = texture2D(tDiffuse, vUv);",
167
+ " float average = (color.r + color.g + color.b) / 3.0;",
168
+ " gl_FragColor = vec4(clamp(color.rgb * vec3(average * 10.0 - 5.0 + pattern()), 0.0, 1.0), color.a);",
169
+ "}",
170
+ ].join("\n"),
171
+ };
172
+
173
+ const AddShader = {
174
+ uniforms: { tDiffuse: { value: null }, tAdd: { value: null }, strength: { value: 1 } },
175
+ vertexShader: "varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }",
176
+ fragmentShader:
177
+ "uniform sampler2D tDiffuse; uniform sampler2D tAdd; uniform float strength; varying vec2 vUv; void main(){ gl_FragColor = texture2D(tDiffuse, vUv) + strength * texture2D(tAdd, vUv); }",
178
+ };
179
+
180
+ /** `FilmPass` accumulates real elapsed time in `render`; pin it to the deck clock instead. */
181
+ class DeterministicFilmPass extends FilmPass {
182
+ time = 0;
183
+ render(renderer: THREE.WebGLRenderer, write: THREE.WebGLRenderTarget, read: THREE.WebGLRenderTarget): void {
184
+ (this.uniforms as { time: { value: number } }).time.value = this.time;
185
+ super.render(renderer, write, read, 0, false);
186
+ }
187
+ }
188
+
189
+ /**
190
+ * Selective bloom (the three `unreal_bloom_selective` technique): render the
191
+ * scene with every non-selected material blacked out, bloom that, and add the
192
+ * result onto the frame. `render` is the layered backdrop/content draw the rig
193
+ * uses, so the mask respects the same depth isolation as the main pass.
194
+ */
195
+ class SelectiveBloomPass extends Pass {
196
+ private readonly target: THREE.WebGLRenderTarget;
197
+ private readonly bloom: UnrealBloomPass;
198
+ private readonly mix: ShaderPass;
199
+ private readonly dark = new THREE.MeshBasicMaterial({ color: 0x000000 });
200
+ private readonly black = new THREE.Color(0x000000);
201
+ selected = new Set<THREE.Object3D>();
202
+
203
+ constructor(
204
+ private readonly scene: THREE.Scene,
205
+ private readonly camera: THREE.Camera,
206
+ private readonly draw: (renderer: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.Camera) => void,
207
+ ) {
208
+ super();
209
+ this.target = new THREE.WebGLRenderTarget(1, 1, { type: THREE.HalfFloatType });
210
+ this.bloom = new UnrealBloomPass(new THREE.Vector2(1, 1), 0.9, 0.5, 0.15);
211
+ this.mix = new ShaderPass(AddShader);
212
+ this.needsSwap = true;
213
+ }
214
+
215
+ setSize(w: number, h: number): void {
216
+ this.target.setSize(w, h);
217
+ this.bloom.setSize(w, h);
218
+ }
219
+
220
+ set strength(v: number) {
221
+ this.mix.uniforms.strength.value = v;
222
+ }
223
+
224
+ render(renderer: THREE.WebGLRenderer, write: THREE.WebGLRenderTarget, read: THREE.WebGLRenderTarget): void {
225
+ const swapped: Array<[THREE.Mesh, THREE.Material | THREE.Material[]]> = [];
226
+ this.scene.traverse((o) => {
227
+ const m = o as THREE.Mesh;
228
+ if (!m.isMesh && !(o as THREE.Points).isPoints && !(o as THREE.Line).isLine) return;
229
+ if (this.selected.has(o)) return;
230
+ swapped.push([m, m.material]);
231
+ m.material = this.dark;
232
+ });
233
+ const bg = this.scene.background;
234
+ const fog = this.scene.fog;
235
+ this.scene.background = this.black;
236
+ this.scene.fog = null;
237
+ renderer.setRenderTarget(this.target);
238
+ renderer.clear();
239
+ this.draw(renderer, this.scene, this.camera);
240
+ this.scene.background = bg;
241
+ this.scene.fog = fog;
242
+ for (const [m, mat] of swapped) m.material = mat;
243
+
244
+ // Bloom composites additively INTO its read buffer, so this leaves
245
+ // `target` = masked scene + its bloom.
246
+ this.bloom.render(renderer, write, this.target, 0, false);
247
+
248
+ this.mix.uniforms.tAdd.value = this.target.texture;
249
+ this.mix.renderToScreen = this.renderToScreen;
250
+ this.mix.render(renderer, write, read, 0, false);
251
+ }
252
+
253
+ dispose(): void {
254
+ this.target.dispose();
255
+ this.bloom.dispose();
256
+ this.mix.dispose();
257
+ this.dark.dispose();
258
+ }
259
+ }
260
+
261
+ /** Expand part roots to the set of drawable descendants (backdrop excluded). */
262
+ function selectionOf(roots: THREE.Object3D[]): Set<THREE.Object3D> {
263
+ const out = new Set<THREE.Object3D>();
264
+ for (const r of roots) {
265
+ r.traverse((o) => {
266
+ if (o.userData.backdrop) return;
267
+ out.add(o);
268
+ });
269
+ }
270
+ return out;
271
+ }
272
+
273
+ // ---------------------------------------------------------------------------
274
+ // Stack
275
+ // ---------------------------------------------------------------------------
276
+
277
+ export function createPostStack(
278
+ renderer: THREE.WebGLRenderer,
279
+ scene: THREE.Scene,
280
+ camera: THREE.PerspectiveCamera,
281
+ renderPass: Pass,
282
+ draw: (renderer: THREE.WebGLRenderer, scene: THREE.Scene, camera: THREE.Camera) => void,
283
+ baselineBloom: boolean,
284
+ ): PostStack {
285
+ // Own target WITH a stencil buffer: three's default composer target has none,
286
+ // which would silently break `clipped-solids` caps whenever a pass is on.
287
+ const target = new THREE.WebGLRenderTarget(1, 1, { type: THREE.HalfFloatType, stencilBuffer: true });
288
+ const composer = new EffectComposer(renderer, target);
289
+ composer.addPass(renderPass);
290
+ const output = new OutputPass();
291
+ composer.addPass(output);
292
+
293
+ const size = new THREE.Vector2(1, 1);
294
+ const slots = new Map<PostId, Slot>();
295
+ let asciiOn = false;
296
+ let asciiParams: FxParams = {};
297
+ let asciiEl: HTMLPreElement | null = null;
298
+ let lastLook: PostLook | null = null;
299
+
300
+ /** Insert a slot's passes before OutputPass, keeping `POST_ORDER`. */
301
+ function mount(slot: Slot): void {
302
+ const rank = (id: string) => POST_ORDER.indexOf(id as (typeof POST_ORDER)[number]);
303
+ let at = 1;
304
+ for (let i = 1; i < composer.passes.length - 1; i++) {
305
+ const owner = [...slots.values()].find((s) => s.passes.includes(composer.passes[i]));
306
+ if (owner && rank(owner.id) < rank(slot.id)) at = i + 1;
307
+ }
308
+ for (const p of slot.passes) {
309
+ p.setSize(size.x, size.y);
310
+ composer.insertPass(p, at++);
311
+ }
312
+ }
313
+
314
+ function shader<T extends Record<string, THREE.IUniform>>(def: { uniforms: T; vertexShader: string; fragmentShader: string }): ShaderPass {
315
+ const p = new ShaderPass({ uniforms: THREE.UniformsUtils.clone(def.uniforms), vertexShader: def.vertexShader, fragmentShader: def.fragmentShader });
316
+ return p;
317
+ }
318
+
319
+ const make: Record<Exclude<PostId, "ascii">, () => Slot> = {
320
+ bloom: () => {
321
+ const pass = new UnrealBloomPass(new THREE.Vector2(1, 1), 0.5, 0.5, 0.9);
322
+ return {
323
+ id: "bloom",
324
+ names: ["UnrealBloomPass"],
325
+ passes: [pass],
326
+ enabled: false,
327
+ params: {},
328
+ apply: (p, look) => {
329
+ pass.threshold = look.mode === "dark" ? 0.95 : 1.1;
330
+ pass.strength = (look.mode === "dark" ? 0.35 : 0.12) * num(p, "intensity", 1);
331
+ },
332
+ dispose: () => pass.dispose(),
333
+ };
334
+ },
335
+ "selective-bloom": () => {
336
+ const pass = new SelectiveBloomPass(scene, camera, draw);
337
+ return {
338
+ id: "selective-bloom",
339
+ names: ["SelectiveBloomPass"],
340
+ passes: [pass],
341
+ enabled: false,
342
+ params: {},
343
+ apply: (p, look) => {
344
+ // Additive over the frame: the selected parts' own colour rides along with
345
+ // their bloom, so keep the mix modest or the nodes wash out to white.
346
+ pass.strength = 0.3 * num(p, "intensity", 1);
347
+ pass.selected = selectionOf(look.parts(str(p, "parts", "diagram") as PartName));
348
+ },
349
+ dispose: () => pass.dispose(),
350
+ };
351
+ },
352
+ film: () => {
353
+ const pass = new DeterministicFilmPass(0.5, false);
354
+ return {
355
+ id: "film",
356
+ names: ["FilmPass"],
357
+ passes: [pass],
358
+ enabled: false,
359
+ params: {},
360
+ apply: (p) => {
361
+ (pass.uniforms as { intensity: { value: number } }).intensity.value = num(p, "intensity", 0.5);
362
+ },
363
+ tick: (t) => {
364
+ pass.time = t;
365
+ },
366
+ dispose: () => pass.dispose(),
367
+ };
368
+ },
369
+ vignette: () => {
370
+ const pass = shader(VignetteShader);
371
+ return {
372
+ id: "vignette",
373
+ names: ["VignettePass"],
374
+ passes: [pass],
375
+ enabled: false,
376
+ params: {},
377
+ apply: (p) => {
378
+ pass.uniforms.offset.value = num(p, "offset", 1.0);
379
+ pass.uniforms.darkness.value = num(p, "darkness", 1.0);
380
+ },
381
+ dispose: () => pass.dispose(),
382
+ };
383
+ },
384
+ smaa: () => {
385
+ const pass = new SMAAPass(size.x, size.y);
386
+ return { id: "smaa", names: ["SMAAPass"], passes: [pass], enabled: false, params: {}, apply: () => {}, dispose: () => pass.dispose() };
387
+ },
388
+ sao: () => {
389
+ const pass = new SAOPass(scene, camera, size.clone());
390
+ return {
391
+ id: "sao",
392
+ names: ["SAOPass"],
393
+ passes: [pass],
394
+ enabled: false,
395
+ params: {},
396
+ apply: (p) => {
397
+ pass.params.saoIntensity = 0.06 * num(p, "intensity", 1);
398
+ pass.params.saoScale = 10;
399
+ pass.params.saoKernelRadius = 40 * num(p, "radius", 1);
400
+ pass.params.saoBlur = true;
401
+ },
402
+ dispose: () => pass.dispose(),
403
+ };
404
+ },
405
+ "depth-of-field": () => {
406
+ const pass = new BokehPass(scene, camera, { focus: 12, aperture: 0.00015, maxblur: 0.01 });
407
+ const u = pass.uniforms as unknown as { focus: { value: number }; aperture: { value: number }; maxblur: { value: number } };
408
+ return {
409
+ id: "depth-of-field",
410
+ names: ["BokehPass"],
411
+ passes: [pass],
412
+ enabled: false,
413
+ params: {},
414
+ apply: (p, look) => {
415
+ // 0 = focus on the slide (camera → anchor distance); anything else is a world distance.
416
+ const focus = num(p, "focus", 0);
417
+ u.focus.value = focus > 0 ? focus : look.focusDistance();
418
+ u.aperture.value = 0.0002 * num(p, "aperture", 1);
419
+ u.maxblur.value = 0.012 * num(p, "blur", 1);
420
+ },
421
+ dispose: () => pass.dispose(),
422
+ };
423
+ },
424
+ "god-rays": () => {
425
+ const pass = shader(GodRaysShader);
426
+ const v = new THREE.Vector3();
427
+ return {
428
+ id: "god-rays",
429
+ names: ["GodRaysPass"],
430
+ passes: [pass],
431
+ enabled: false,
432
+ params: {},
433
+ apply: (p) => {
434
+ pass.uniforms.intensity.value = 0.6 * num(p, "intensity", 1);
435
+ pass.uniforms.density.value = num(p, "density", 0.6);
436
+ pass.uniforms.threshold.value = num(p, "threshold", 0.55);
437
+ },
438
+ tick: (_t, cam) => {
439
+ if (!lastLook) return;
440
+ v.copy(lastLook.sun()).project(cam);
441
+ pass.uniforms.sun.value.set(v.x * 0.5 + 0.5, v.y * 0.5 + 0.5);
442
+ },
443
+ dispose: () => pass.dispose(),
444
+ };
445
+ },
446
+ "chromatic-aberration": () => {
447
+ const pass = shader(RGBShiftShader);
448
+ return {
449
+ id: "chromatic-aberration",
450
+ names: ["RGBShiftPass"],
451
+ passes: [pass],
452
+ enabled: false,
453
+ params: {},
454
+ apply: (p) => {
455
+ pass.uniforms.amount.value = num(p, "offset", 0.003);
456
+ pass.uniforms.angle.value = num(p, "angle", 0);
457
+ },
458
+ dispose: () => pass.dispose(),
459
+ };
460
+ },
461
+ "dot-screen": () => {
462
+ const pass = shader(DotScreenClampedShader);
463
+ return {
464
+ id: "dot-screen",
465
+ names: ["DotScreenPass"],
466
+ passes: [pass],
467
+ enabled: false,
468
+ params: {},
469
+ apply: (p) => {
470
+ pass.uniforms.scale.value = num(p, "scale", 1.2);
471
+ pass.uniforms.angle.value = num(p, "angle", 1.57);
472
+ pass.uniforms.tSize.value.copy(size);
473
+ },
474
+ dispose: () => pass.dispose(),
475
+ };
476
+ },
477
+ sobel: () => {
478
+ const lum = shader(LuminosityShader);
479
+ const sobel = shader(SobelOperatorShader);
480
+ return {
481
+ id: "sobel",
482
+ names: ["LuminosityPass", "SobelPass"],
483
+ passes: [lum, sobel],
484
+ enabled: false,
485
+ params: {},
486
+ apply: () => {
487
+ sobel.uniforms.resolution.value.copy(size);
488
+ },
489
+ dispose: () => {
490
+ lum.dispose();
491
+ sobel.dispose();
492
+ },
493
+ };
494
+ },
495
+ pixelate: () => {
496
+ const pass = shader(PixelateShader);
497
+ return {
498
+ id: "pixelate",
499
+ names: ["PixelatePass"],
500
+ passes: [pass],
501
+ enabled: false,
502
+ params: {},
503
+ apply: (p) => {
504
+ pass.uniforms.size.value = Math.max(1, num(p, "size", 6));
505
+ pass.uniforms.resolution.value.copy(size);
506
+ },
507
+ dispose: () => pass.dispose(),
508
+ };
509
+ },
510
+ outline: () => {
511
+ const pass = new OutlinePass(size.clone(), scene, camera, []);
512
+ pass.edgeStrength = 4;
513
+ pass.edgeGlow = 0.4;
514
+ pass.edgeThickness = 1.5;
515
+ return {
516
+ id: "outline",
517
+ names: ["OutlinePass"],
518
+ passes: [pass],
519
+ enabled: false,
520
+ params: {},
521
+ apply: (p, look) => {
522
+ pass.edgeStrength = 4 * num(p, "strength", 1);
523
+ pass.edgeThickness = num(p, "thickness", 1.5);
524
+ pass.visibleEdgeColor.set(look.palette.accent);
525
+ pass.hiddenEdgeColor.set(look.palette.accent).multiplyScalar(0.3);
526
+ pass.selectedObjects = look.parts(str(p, "parts", "diagram") as PartName);
527
+ },
528
+ dispose: () => pass.dispose(),
529
+ };
530
+ },
531
+ };
532
+
533
+ function slot(id: Exclude<PostId, "ascii">): Slot {
534
+ let s = slots.get(id);
535
+ if (!s) {
536
+ s = make[id]();
537
+ slots.set(id, s);
538
+ mount(s);
539
+ }
540
+ return s;
541
+ }
542
+
543
+ function setEnabled(s: Slot, on: boolean): void {
544
+ s.enabled = on;
545
+ for (const p of s.passes) p.enabled = on;
546
+ }
547
+
548
+ function setActive(refs: PostRef[], look: PostLook): void {
549
+ lastLook = look;
550
+ const wanted = new Map<string, FxParams>();
551
+ for (const r of refs) wanted.set(r.id, r.params);
552
+ // Baseline bloom is quality-driven and pre-dates cards; the `bloom` card
553
+ // only adds params on top of it. Everything else is opt-in per slide.
554
+ if (baselineBloom && !wanted.has("bloom")) wanted.set("bloom", {});
555
+
556
+ for (const id of POST_ORDER) {
557
+ const params = wanted.get(id);
558
+ const existing = slots.get(id);
559
+ if (!params) {
560
+ if (existing) setEnabled(existing, false);
561
+ continue;
562
+ }
563
+ const s = slot(id);
564
+ s.params = params;
565
+ s.apply(params, look);
566
+ setEnabled(s, true);
567
+ }
568
+ asciiOn = wanted.has("ascii");
569
+ asciiParams = wanted.get("ascii") ?? {};
570
+ if (asciiEl) asciiEl.style.display = asciiOn ? "block" : "none";
571
+ renderer.domElement.style.visibility = asciiOn ? "hidden" : "";
572
+ if (asciiOn && lastLook) {
573
+ const el = ensureAscii();
574
+ el.style.color = lastLook.palette.text;
575
+ el.style.background = lastLook.palette.bg;
576
+ el.style.display = "block";
577
+ }
578
+ }
579
+
580
+ function ensureAscii(): HTMLPreElement {
581
+ if (asciiEl) return asciiEl;
582
+ asciiEl = document.createElement("pre");
583
+ asciiEl.className = "deck3d-ascii";
584
+ asciiEl.setAttribute("aria-hidden", "true");
585
+ document.body.appendChild(asciiEl);
586
+ return asciiEl;
587
+ }
588
+
589
+ const CHARS = " .:-=+*#%@";
590
+ const asciiCanvas = document.createElement("canvas");
591
+ function drawAscii(canvas: HTMLCanvasElement): void {
592
+ if (!asciiOn) return;
593
+ const el = ensureAscii();
594
+ const cell = Math.max(4, num(asciiParams, "size", 9));
595
+ const invert = asciiParams.invert === true;
596
+ const cols = Math.max(1, Math.floor(canvas.clientWidth / (cell * 0.6)));
597
+ const rows = Math.max(1, Math.floor(canvas.clientHeight / cell));
598
+ asciiCanvas.width = cols;
599
+ asciiCanvas.height = rows;
600
+ const x = asciiCanvas.getContext("2d", { willReadFrequently: true }) as CanvasRenderingContext2D;
601
+ x.drawImage(canvas, 0, 0, cols, rows);
602
+ const d = x.getImageData(0, 0, cols, rows).data;
603
+ const lines: string[] = [];
604
+ for (let r = 0; r < rows; r++) {
605
+ let line = "";
606
+ for (let c = 0; c < cols; c++) {
607
+ const i = (r * cols + c) * 4;
608
+ let l = ((0.2126 * d[i] + 0.7152 * d[i + 1] + 0.0722 * d[i + 2]) / 255) ** 1.5;
609
+ if (invert) l = 1 - l;
610
+ line += CHARS[Math.min(CHARS.length - 1, Math.floor(l * CHARS.length))];
611
+ }
612
+ lines.push(line);
613
+ }
614
+ el.style.fontSize = `${cell}px`;
615
+ el.style.lineHeight = `${cell}px`;
616
+ el.textContent = lines.join("\n");
617
+ }
618
+
619
+ return {
620
+ composer,
621
+ setActive,
622
+ passNames: () => {
623
+ const names = ["RenderPass"];
624
+ for (const id of POST_ORDER) {
625
+ const s = slots.get(id);
626
+ if (s?.enabled) names.push(...s.names);
627
+ }
628
+ names.push("OutputPass");
629
+ return names;
630
+ },
631
+ probe: () =>
632
+ POST_IDS.map((id) =>
633
+ id === "ascii"
634
+ ? { id, enabled: asciiOn, params: asciiParams }
635
+ : { id, enabled: slots.get(id)?.enabled ?? false, params: slots.get(id)?.params ?? {} },
636
+ ),
637
+ any: () => [...slots.values()].some((s) => s.enabled),
638
+ ascii: () => asciiOn,
639
+ tick: (t, cam) => {
640
+ for (const s of slots.values()) if (s.enabled) s.tick?.(t, cam);
641
+ },
642
+ drawAscii,
643
+ resize: (w, h) => {
644
+ size.set(w, h);
645
+ composer.setSize(w, h);
646
+ for (const s of slots.values()) if (s.enabled && lastLook) s.apply(s.params, lastLook);
647
+ },
648
+ };
649
+ }