@uxfront/scene 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +128 -0
  2. package/dist/experience.d.ts +40 -0
  3. package/dist/experience.js +306 -0
  4. package/dist/formations/corridor/index.d.ts +8 -0
  5. package/dist/formations/corridor/index.js +52 -0
  6. package/dist/formations/corridor/shader.js +37 -0
  7. package/dist/formations/index.d.ts +11 -0
  8. package/dist/formations/index.js +11 -0
  9. package/dist/formations/ink/index.d.ts +10 -0
  10. package/dist/formations/ink/index.js +53 -0
  11. package/dist/formations/ink/shader.js +177 -0
  12. package/dist/formations/monolith/index.d.ts +5 -0
  13. package/dist/formations/monolith/index.js +49 -0
  14. package/dist/formations/monolith/mark.js +89 -0
  15. package/dist/formations/monolith/shader.js +120 -0
  16. package/dist/formations/plates/art.d.ts +46 -0
  17. package/dist/formations/plates/art.js +238 -0
  18. package/dist/formations/plates/geometry.d.ts +5 -0
  19. package/dist/formations/plates/geometry.js +8 -0
  20. package/dist/formations/plates/index.d.ts +13 -0
  21. package/dist/formations/plates/index.js +67 -0
  22. package/dist/formations/plates/shader.js +97 -0
  23. package/dist/formations/prism/geometry.d.ts +4 -0
  24. package/dist/formations/prism/geometry.js +15 -0
  25. package/dist/formations/prism/index.d.ts +9 -0
  26. package/dist/formations/prism/index.js +64 -0
  27. package/dist/formations/prism/shader.js +140 -0
  28. package/dist/formations/tokens/geometry.d.ts +4 -0
  29. package/dist/formations/tokens/geometry.js +22 -0
  30. package/dist/formations/tokens/index.d.ts +18 -0
  31. package/dist/formations/tokens/index.js +79 -0
  32. package/dist/formations/tokens/shader.js +125 -0
  33. package/dist/gl/compose.d.ts +14 -0
  34. package/dist/gl/compose.js +52 -0
  35. package/dist/gl/constants.d.ts +6 -0
  36. package/dist/gl/constants.js +7 -0
  37. package/dist/gl/engine.js +559 -0
  38. package/dist/gl/formation.d.ts +7 -0
  39. package/dist/gl/formation.js +39 -0
  40. package/dist/gl/glsl.d.ts +9 -0
  41. package/dist/gl/glsl.js +419 -0
  42. package/dist/gl/math.d.ts +16 -0
  43. package/dist/gl/math.js +102 -0
  44. package/dist/gl/stage.d.ts +28 -0
  45. package/dist/gl/stage.js +55 -0
  46. package/dist/gl/types.d.ts +103 -0
  47. package/dist/index.d.ts +10 -0
  48. package/dist/index.js +9 -0
  49. package/dist/layout.d.ts +9 -0
  50. package/dist/layout.js +10 -0
  51. package/package.json +51 -0
@@ -0,0 +1,419 @@
1
+ //#region src/gl/glsl.ts
2
+ /**
3
+ * The particle pipeline shared by every formation, and the post-processing
4
+ * passes. Formations only supply `Particle $main(uint id, float sel, Frame f)`
5
+ * (see `compose.ts`); everything here runs for all of them.
6
+ */
7
+ /** Formats a number as a GLSL float literal, for constants interpolated into shaders. */
8
+ const glslFloat = (n) => Number.isInteger(n) ? `${n}.0` : `${n}`;
9
+ /** Helpers every formation can use: hashing, noise, curl flow and path sampling. */
10
+ const COMMON = `
11
+ #define PI 3.141592653589793
12
+ #define TAU 6.283185307179586
13
+
14
+ uint hashU(uint x) {
15
+ x ^= x >> 16u;
16
+ x *= 0x7feb352du;
17
+ x ^= x >> 15u;
18
+ x *= 0x846ca68bu;
19
+ x ^= x >> 16u;
20
+ return x;
21
+ }
22
+
23
+ float rnd(inout uint s) {
24
+ s = hashU(s);
25
+ return float(s >> 8u) * (1.0 / 16777216.0);
26
+ }
27
+
28
+ uint seedOf(uint id, uint salt) {
29
+ return hashU(id * 0x9E3779B9u ^ salt);
30
+ }
31
+
32
+ vec2 gauss(float u1, float u2) {
33
+ return sqrt(-2.0 * log(max(u1, 1e-6))) * vec2(cos(TAU * u2), sin(TAU * u2));
34
+ }
35
+
36
+ vec3 hash33(vec3 p) {
37
+ uvec3 q = uvec3(ivec3(p)) * uvec3(1597334673u, 3812015801u, 2798796415u);
38
+ q = (q.x ^ q.y ^ q.z) * uvec3(1597334673u, 3812015801u, 2798796415u);
39
+ return -1.0 + 2.0 * vec3(q) * (1.0 / 4294967295.0);
40
+ }
41
+
42
+ // Gradient noise with analytic derivatives (Inigo Quilez): vec4(value, d/dx, d/dy, d/dz).
43
+ vec4 noised(vec3 x) {
44
+ vec3 i = floor(x);
45
+ vec3 f = fract(x);
46
+ vec3 u = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
47
+ vec3 du = 30.0 * f * f * (f * (f - 2.0) + 1.0);
48
+
49
+ vec3 ga = hash33(i);
50
+ vec3 gb = hash33(i + vec3(1.0, 0.0, 0.0));
51
+ vec3 gc = hash33(i + vec3(0.0, 1.0, 0.0));
52
+ vec3 gd = hash33(i + vec3(1.0, 1.0, 0.0));
53
+ vec3 ge = hash33(i + vec3(0.0, 0.0, 1.0));
54
+ vec3 gf = hash33(i + vec3(1.0, 0.0, 1.0));
55
+ vec3 gg = hash33(i + vec3(0.0, 1.0, 1.0));
56
+ vec3 gh = hash33(i + vec3(1.0, 1.0, 1.0));
57
+
58
+ float va = dot(ga, f);
59
+ float vb = dot(gb, f - vec3(1.0, 0.0, 0.0));
60
+ float vc = dot(gc, f - vec3(0.0, 1.0, 0.0));
61
+ float vd = dot(gd, f - vec3(1.0, 1.0, 0.0));
62
+ float ve = dot(ge, f - vec3(0.0, 0.0, 1.0));
63
+ float vf = dot(gf, f - vec3(1.0, 0.0, 1.0));
64
+ float vg = dot(gg, f - vec3(0.0, 1.0, 1.0));
65
+ float vh = dot(gh, f - vec3(1.0, 1.0, 1.0));
66
+
67
+ float k0 = va - vb - vc + vd;
68
+ float k1 = va - vc - ve + vg;
69
+ float k2 = va - vb - ve + vf;
70
+ float k3 = -va + vb + vc - vd + ve - vf - vg + vh;
71
+
72
+ return vec4(
73
+ va + u.x * (vb - va) + u.y * (vc - va) + u.z * (ve - va)
74
+ + u.x * u.y * k0 + u.y * u.z * k1 + u.z * u.x * k2 + u.x * u.y * u.z * k3,
75
+ ga + u.x * (gb - ga) + u.y * (gc - ga) + u.z * (ge - ga)
76
+ + u.x * u.y * (ga - gb - gc + gd) + u.y * u.z * (ga - gc - ge + gg)
77
+ + u.z * u.x * (ga - gb - ge + gf) + u.x * u.y * u.z * (-ga + gb + gc - gd + ge - gf - gg + gh)
78
+ + du * (vec3(vb - va, vc - va, ve - va) + u.yzx * vec3(k0, k1, k2)
79
+ + u.zxy * vec3(k2, k0, k1) + u.yzx * u.zxy * k3)
80
+ );
81
+ }
82
+
83
+ // Divergence-free flow: the curl of a noise potential field.
84
+ vec3 curl(vec3 p) {
85
+ vec4 a = noised(p);
86
+ vec4 b = noised(p + vec3(31.341, -43.123, 12.853));
87
+ vec4 c = noised(p + vec3(-17.953, 23.417, -35.291));
88
+ return vec3(c.z - b.w, a.w - c.y, b.y - a.z);
89
+ }
90
+
91
+ // Divergence-free like curl(), for two lookups instead of three: the cross
92
+ // product of two gradient fields.
93
+ vec3 bitangent(vec3 p) {
94
+ return cross(noised(p).yzw, noised(p + vec3(31.341, -43.123, 12.853)).yzw);
95
+ }
96
+
97
+ vec2 bezier3(vec2 p0, vec2 p1, vec2 p2, vec2 p3, float t) {
98
+ float u = 1.0 - t;
99
+ return u * u * u * p0 + 3.0 * u * u * t * p1 + 3.0 * u * t * t * p2 + t * t * t * p3;
100
+ }
101
+
102
+ // A point at arc length t (0 → 1) around a rounded rectangle g = (centre, half size).
103
+ vec2 rrectPoint(vec4 g, float r, float t) {
104
+ vec2 c = g.xy;
105
+ vec2 hs = g.zw;
106
+ r = min(r, min(hs.x, hs.y));
107
+ vec2 inner = hs - r;
108
+ float lx = 2.0 * inner.x;
109
+ float ly = 2.0 * inner.y;
110
+ float la = 0.5 * PI * r;
111
+ float d = t * (2.0 * (lx + ly) + 4.0 * la);
112
+ float rr = max(r, 1e-5);
113
+ if (d < lx) return c + vec2(-inner.x + d, hs.y);
114
+ d -= lx;
115
+ if (d < la) { float a = d / rr; return c + inner + r * vec2(sin(a), cos(a)); }
116
+ d -= la;
117
+ if (d < ly) return c + vec2(hs.x, inner.y - d);
118
+ d -= ly;
119
+ if (d < la) { float a = d / rr; return c + vec2(inner.x, -inner.y) + r * vec2(cos(a), -sin(a)); }
120
+ d -= la;
121
+ if (d < lx) return c + vec2(inner.x - d, -hs.y);
122
+ d -= lx;
123
+ if (d < la) { float a = d / rr; return c - inner + r * vec2(-sin(a), -cos(a)); }
124
+ d -= la;
125
+ if (d < ly) return c + vec2(-hs.x, -inner.y + d);
126
+ d -= ly;
127
+ float a = d / rr;
128
+ return c + vec2(-inner.x, inner.y) + r * vec2(-cos(a), sin(a));
129
+ }
130
+ `;
131
+ /** Declarations every formation's `$main` works with. */
132
+ const PARTICLE_TYPES = `
133
+ struct Particle {
134
+ vec3 pos;
135
+ vec3 col;
136
+ float size;
137
+ // How strongly the mirror floor reflects it (see FormationLook.reflect).
138
+ float refl;
139
+ };
140
+
141
+ Particle blank() {
142
+ Particle P;
143
+ P.pos = vec3(0.0);
144
+ P.col = vec3(0.0);
145
+ P.size = 1.0;
146
+ P.refl = 0.0;
147
+ return P;
148
+ }
149
+
150
+ // Where the formation stands in the scroll (see FormationProgress).
151
+ struct Frame {
152
+ float local;
153
+ float approach;
154
+ float time;
155
+ };
156
+ `;
157
+ const PARTICLE_UNIFORMS = `
158
+ uniform mat4 uProjection;
159
+ uniform mat4 uView;
160
+ uniform float uTime;
161
+ uniform float uStage;
162
+ uniform float uLocal;
163
+ uniform float uIntro;
164
+ uniform float uCount;
165
+ uniform float uPointScale;
166
+ uniform float uIntensity;
167
+ uniform vec3 uRayOrigin;
168
+ uniform vec3 uRayDir;
169
+ uniform float uPointer;
170
+ uniform vec4 uShock;
171
+ uniform float uMirror;
172
+ uniform float uReflect;
173
+ `;
174
+ /** The vertex shader's `main()`. Needs `formation(int k, uint id, float sel)` and `LAST_STAGE`. */
175
+ const PARTICLE_MAIN = `
176
+ void main() {
177
+ uint id = uint(gl_VertexID);
178
+ float sel = (float(id) + 0.5) / uCount;
179
+ uint s = seedOf(id, 0x9E3779B9u);
180
+ float r0 = rnd(s);
181
+ float r1 = rnd(s);
182
+ float r2 = rnd(s);
183
+ float time = uTime;
184
+
185
+ float st = clamp(uStage, 0.0, float(LAST_STAGE));
186
+ int ia = int(floor(st));
187
+ float t = st - float(ia);
188
+
189
+ Particle P = formation(ia, id, sel);
190
+
191
+ // Morph: each particle leaves on its own schedule and rides a curl-noise
192
+ // current, so formations dissolve and re-condense instead of sliding.
193
+ if (t > 0.0001) {
194
+ Particle B = formation(min(ia + 1, LAST_STAGE), id, sel);
195
+ float delay = r0 * 0.4;
196
+ float lt = clamp((t - delay) / 0.6, 0.0, 1.0);
197
+ lt = lt * lt * (3.0 - 2.0 * lt);
198
+ float travel = sin(lt * PI);
199
+ vec3 mid = mix(P.pos, B.pos, lt);
200
+ mid += curl(mid * 0.32 + vec3(r1 * 0.2, 0.0, time * 0.04)) * travel * 0.8;
201
+ mid.y += travel * (r2 - 0.5) * 0.7;
202
+ P.pos = mid;
203
+ P.col = mix(P.col, B.col, lt) + travel * 0.05 * vec3(0.8, 0.85, 1.0);
204
+ P.size = mix(P.size, B.size, lt);
205
+ P.refl = mix(P.refl, B.refl, lt);
206
+ }
207
+
208
+ // Intro: the foundation line is laid, then the first formation rises from it.
209
+ if (uIntro < 0.999) {
210
+ float hgt = clamp((P.pos.y - FLOOR_Y) / 3.0, 0.0, 1.0);
211
+ float d0 = hgt * 0.5 + r0 * 0.2;
212
+ float e = smoothstep(d0, d0 + 0.3, uIntro);
213
+ vec3 from = vec3(P.pos.x * 1.5 + (r1 - 0.5) * 7.0, FLOOR_Y + 0.002, P.pos.z * 1.2 + (r2 - 0.5) * 0.08);
214
+ vec3 lineCol = vec3(0.8, 0.86, 1.0) * 0.05 * smoothstep(0.0, 0.12, uIntro);
215
+ P.pos = mix(from, P.pos, e);
216
+ P.col = mix(lineCol, P.col, e) * (1.0 + sin(e * PI) * 1.5);
217
+ }
218
+
219
+ // The cursor is a lens: particles part around it and light up.
220
+ if (uPointer > 0.001) {
221
+ vec3 rel = P.pos - uRayOrigin;
222
+ float along = max(dot(rel, uRayDir), 0.0);
223
+ vec3 away = rel - uRayDir * along;
224
+ float dist = length(away);
225
+ float radius = 0.045 * along;
226
+ float fall = uPointer * (1.0 - smoothstep(0.0, radius, dist));
227
+ fall *= fall;
228
+ vec3 n = away / max(dist, 1e-4);
229
+ P.pos += (n * 0.45 + cross(uRayDir, n) * 0.35) * fall * radius;
230
+ P.col *= 1.0 + fall * 1.2;
231
+ }
232
+
233
+ // Click shockwave.
234
+ if (uShock.w >= 0.0 && uShock.w < 3.0) {
235
+ vec3 dp = P.pos - uShock.xyz;
236
+ float dist = length(dp);
237
+ float ring = exp(-pow((dist - uShock.w * 3.2) * 2.6, 2.0)) * exp(-uShock.w * 1.2);
238
+ P.pos += dp / max(dist, 1e-4) * ring * 0.35;
239
+ P.col *= 1.0 + ring * 2.5;
240
+ }
241
+
242
+ // Mirror pass for the floor reflection.
243
+ if (uMirror > 0.5) {
244
+ float hgt = P.pos.y - FLOOR_Y;
245
+ P.pos.y = FLOOR_Y - hgt;
246
+ P.col *= hgt > 0.0 ? P.refl * uReflect * 0.24 * exp(-hgt * 1.8) : 0.0;
247
+ }
248
+
249
+ vec4 view = uView * vec4(P.pos, 1.0);
250
+ float depth = -view.z;
251
+ gl_Position = uProjection * view;
252
+
253
+ // Perspective-correct sprite size. Sub-pixel sprites are clamped and dimmed
254
+ // to conserve energy (no shimmer); oversized ones spread into faint bokeh.
255
+ float px = P.size * 0.0105 * uPointScale / max(depth, 0.05);
256
+ float energy = 1.0;
257
+ if (px < 1.3) {
258
+ energy = (px * px) / 1.69;
259
+ px = 1.3;
260
+ } else if (px > 7.0) {
261
+ energy = (7.0 * 7.0) / (px * px);
262
+ }
263
+ gl_PointSize = min(px, 48.0);
264
+ float fog = exp(-max(depth - 10.0, 0.0) * 0.06);
265
+ vColor = P.col * energy * fog * uIntensity;
266
+
267
+ if (depth < 0.25 || dot(vColor, vColor) < 1e-9) {
268
+ gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
269
+ gl_PointSize = 0.0;
270
+ }
271
+ }
272
+ `;
273
+ const PARTICLE_FRAG = `#version 300 es
274
+ precision highp float;
275
+
276
+ in vec3 vColor;
277
+ out vec4 outColor;
278
+
279
+ void main() {
280
+ vec2 d = gl_PointCoord * 2.0 - 1.0;
281
+ float r2 = dot(d, d);
282
+ if (r2 > 1.0) discard;
283
+ outColor = vec4(vColor * (exp(-r2 * 4.0) - 0.0183), 1.0);
284
+ }
285
+ `;
286
+ const QUAD_VERT = `#version 300 es
287
+ out vec2 vUv;
288
+
289
+ void main() {
290
+ vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2));
291
+ vUv = p;
292
+ gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0);
293
+ }
294
+ `;
295
+ const TRAIL_FRAG = `#version 300 es
296
+ precision highp float;
297
+
298
+ uniform sampler2D uScene;
299
+ uniform sampler2D uPrevious;
300
+ uniform float uDecay;
301
+
302
+ in vec2 vUv;
303
+ out vec4 outColor;
304
+
305
+ void main() {
306
+ vec3 current = texture(uScene, vUv).rgb;
307
+ vec3 previous = texture(uPrevious, vUv).rgb * uDecay;
308
+ outColor = vec4(max(current, previous), 1.0);
309
+ }
310
+ `;
311
+ const BLOOM_PREFILTER_FRAG = `#version 300 es
312
+ precision highp float;
313
+
314
+ uniform sampler2D uSource;
315
+ uniform vec2 uTexel;
316
+ uniform vec4 uThreshold;
317
+
318
+ in vec2 vUv;
319
+ out vec4 outColor;
320
+
321
+ void main() {
322
+ vec4 o = uTexel.xyxy * vec4(-1.0, -1.0, 1.0, 1.0);
323
+ vec3 c = texture(uSource, vUv + o.xy).rgb + texture(uSource, vUv + o.zy).rgb
324
+ + texture(uSource, vUv + o.xw).rgb + texture(uSource, vUv + o.zw).rgb;
325
+ c *= 0.25;
326
+ // Soft-knee threshold.
327
+ float br = max(c.r, max(c.g, c.b));
328
+ float rq = clamp(br - uThreshold.y, 0.0, uThreshold.z);
329
+ rq = uThreshold.w * rq * rq;
330
+ c *= max(rq, br - uThreshold.x) / max(br, 1e-4);
331
+ outColor = vec4(c, 1.0);
332
+ }
333
+ `;
334
+ const BLOOM_DOWN_FRAG = `#version 300 es
335
+ precision highp float;
336
+
337
+ uniform sampler2D uSource;
338
+ uniform vec2 uTexel;
339
+
340
+ in vec2 vUv;
341
+ out vec4 outColor;
342
+
343
+ void main() {
344
+ vec3 sum = texture(uSource, vUv).rgb * 4.0;
345
+ sum += texture(uSource, vUv - uTexel).rgb;
346
+ sum += texture(uSource, vUv + uTexel).rgb;
347
+ sum += texture(uSource, vUv + vec2(uTexel.x, -uTexel.y)).rgb;
348
+ sum += texture(uSource, vUv - vec2(uTexel.x, -uTexel.y)).rgb;
349
+ outColor = vec4(sum * 0.125, 1.0);
350
+ }
351
+ `;
352
+ const BLOOM_UP_FRAG = `#version 300 es
353
+ precision highp float;
354
+
355
+ uniform sampler2D uSource;
356
+ uniform vec2 uTexel;
357
+
358
+ in vec2 vUv;
359
+ out vec4 outColor;
360
+
361
+ void main() {
362
+ vec2 h = uTexel;
363
+ vec3 sum = texture(uSource, vUv + vec2(-h.x * 2.0, 0.0)).rgb;
364
+ sum += texture(uSource, vUv + vec2(-h.x, h.y)).rgb * 2.0;
365
+ sum += texture(uSource, vUv + vec2(0.0, h.y * 2.0)).rgb;
366
+ sum += texture(uSource, vUv + vec2(h.x, h.y)).rgb * 2.0;
367
+ sum += texture(uSource, vUv + vec2(h.x * 2.0, 0.0)).rgb;
368
+ sum += texture(uSource, vUv + vec2(h.x, -h.y)).rgb * 2.0;
369
+ sum += texture(uSource, vUv + vec2(0.0, -h.y * 2.0)).rgb;
370
+ sum += texture(uSource, vUv + vec2(-h.x, -h.y)).rgb * 2.0;
371
+ outColor = vec4(sum / 12.0, 1.0);
372
+ }
373
+ `;
374
+ const COMPOSITE_FRAG = `#version 300 es
375
+ precision highp float;
376
+
377
+ uniform sampler2D uScene;
378
+ uniform sampler2D uBloom;
379
+ uniform float uBloomStrength;
380
+ uniform float uExposure;
381
+ uniform float uTime;
382
+ uniform float uAberration;
383
+ uniform float uFade;
384
+ uniform vec3 uBackground;
385
+
386
+ in vec2 vUv;
387
+ out vec4 outColor;
388
+
389
+ vec3 aces(vec3 x) {
390
+ return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0);
391
+ }
392
+
393
+ float grain(vec2 p) {
394
+ uvec2 q = uvec2(p) * uvec2(1597334673u, 3812015801u);
395
+ uint n = (q.x ^ q.y) * 1597334673u;
396
+ return float(n) * (1.0 / 4294967295.0);
397
+ }
398
+
399
+ void main() {
400
+ vec2 dc = vUv - 0.5;
401
+ float r2 = dot(dc, dc);
402
+ vec2 off = dc * r2 * uAberration;
403
+ vec3 col = vec3(
404
+ texture(uScene, vUv - off).r,
405
+ texture(uScene, vUv).g,
406
+ texture(uScene, vUv + off).b
407
+ );
408
+ col += texture(uBloom, vUv).rgb * uBloomStrength;
409
+ col = aces(col * uExposure);
410
+ col *= 1.0 - smoothstep(0.25, 0.95, sqrt(r2) * 1.25) * 0.55;
411
+ col = col * uFade + uBackground;
412
+ col = pow(col, vec3(1.0 / 2.2));
413
+ // Animated grain doubles as dither against banding in the dark gradients.
414
+ col += (grain(gl_FragCoord.xy + fract(uTime * 7.3) * 1024.0) - 0.5) * 0.028;
415
+ outColor = vec4(col, 1.0);
416
+ }
417
+ `;
418
+ //#endregion
419
+ export { BLOOM_DOWN_FRAG, BLOOM_PREFILTER_FRAG, BLOOM_UP_FRAG, COMMON, COMPOSITE_FRAG, PARTICLE_FRAG, PARTICLE_MAIN, PARTICLE_TYPES, PARTICLE_UNIFORMS, QUAD_VERT, TRAIL_FRAG, glslFloat };
@@ -0,0 +1,16 @@
1
+ //#region src/gl/math.d.ts
2
+ export type Vec2 = [number, number];
3
+ export type Vec3 = [number, number, number];
4
+ export type Mat4 = Float32Array;
5
+ export declare const clamp: (value: number, min: number, max: number) => number;
6
+ export declare const lerp: (a: number, b: number, t: number) => number;
7
+ export declare const smoothstep: (edge0: number, edge1: number, x: number) => number;
8
+ /** Frame-rate independent exponential smoothing towards `target`. */
9
+ export declare const damp: (current: number, target: number, lambda: number, dt: number) => number;
10
+ export declare const lerp3: (out: Vec3, a: Vec3, b: Vec3, t: number) => Vec3;
11
+ /**
12
+ * Projects a world-space point to CSS pixels. Returns false when the point is
13
+ * behind the camera.
14
+ */
15
+ export declare function projectToScreen(out: Vec3, viewProjection: Mat4, point: Vec3, width: number, height: number): boolean;
16
+ //#endregion
@@ -0,0 +1,102 @@
1
+ //#region src/gl/math.ts
2
+ const clamp = (value, min, max) => Math.min(max, Math.max(min, value));
3
+ const lerp = (a, b, t) => a + (b - a) * t;
4
+ const smoothstep = (edge0, edge1, x) => {
5
+ const t = clamp((x - edge0) / (edge1 - edge0), 0, 1);
6
+ return t * t * (3 - 2 * t);
7
+ };
8
+ /** Frame-rate independent exponential smoothing towards `target`. */
9
+ const damp = (current, target, lambda, dt) => lerp(current, target, 1 - Math.exp(-lambda * dt));
10
+ const lerp3 = (out, a, b, t) => {
11
+ out[0] = lerp(a[0], b[0], t);
12
+ out[1] = lerp(a[1], b[1], t);
13
+ out[2] = lerp(a[2], b[2], t);
14
+ return out;
15
+ };
16
+ const mat4 = () => {
17
+ const m = /* @__PURE__ */ new Float32Array(16);
18
+ m[0] = m[5] = m[10] = m[15] = 1;
19
+ return m;
20
+ };
21
+ /**
22
+ * Perspective projection with a lens shift: `shiftX`/`shiftY` move the image
23
+ * centre in NDC without changing the perspective, which lets a formation sit
24
+ * beside the copy instead of behind it.
25
+ */
26
+ function perspective(out, fovY, aspect, near, far, shiftX = 0, shiftY = 0) {
27
+ const f = 1 / Math.tan(fovY / 2);
28
+ const nf = 1 / (near - far);
29
+ out.fill(0);
30
+ out[0] = f / aspect;
31
+ out[5] = f;
32
+ out[8] = -shiftX;
33
+ out[9] = -shiftY;
34
+ out[10] = (far + near) * nf;
35
+ out[11] = -1;
36
+ out[14] = 2 * far * near * nf;
37
+ return out;
38
+ }
39
+ function lookAt(out, eye, target, up) {
40
+ let zx = eye[0] - target[0];
41
+ let zy = eye[1] - target[1];
42
+ let zz = eye[2] - target[2];
43
+ let len = Math.hypot(zx, zy, zz) || 1;
44
+ zx /= len;
45
+ zy /= len;
46
+ zz /= len;
47
+ let xx = up[1] * zz - up[2] * zy;
48
+ let xy = up[2] * zx - up[0] * zz;
49
+ let xz = up[0] * zy - up[1] * zx;
50
+ len = Math.hypot(xx, xy, xz) || 1;
51
+ xx /= len;
52
+ xy /= len;
53
+ xz /= len;
54
+ const yx = zy * xz - zz * xy;
55
+ const yy = zz * xx - zx * xz;
56
+ const yz = zx * xy - zy * xx;
57
+ out[0] = xx;
58
+ out[1] = yx;
59
+ out[2] = zx;
60
+ out[3] = 0;
61
+ out[4] = xy;
62
+ out[5] = yy;
63
+ out[6] = zy;
64
+ out[7] = 0;
65
+ out[8] = xz;
66
+ out[9] = yz;
67
+ out[10] = zz;
68
+ out[11] = 0;
69
+ out[12] = -(xx * eye[0] + xy * eye[1] + xz * eye[2]);
70
+ out[13] = -(yx * eye[0] + yy * eye[1] + yz * eye[2]);
71
+ out[14] = -(zx * eye[0] + zy * eye[1] + zz * eye[2]);
72
+ out[15] = 1;
73
+ return out;
74
+ }
75
+ function multiply(out, a, b) {
76
+ for (let col = 0; col < 4; col++) {
77
+ const b0 = b[col * 4];
78
+ const b1 = b[col * 4 + 1];
79
+ const b2 = b[col * 4 + 2];
80
+ const b3 = b[col * 4 + 3];
81
+ for (let row = 0; row < 4; row++) out[col * 4 + row] = a[row] * b0 + a[4 + row] * b1 + a[8 + row] * b2 + a[12 + row] * b3;
82
+ }
83
+ return out;
84
+ }
85
+ /**
86
+ * Projects a world-space point to CSS pixels. Returns false when the point is
87
+ * behind the camera.
88
+ */
89
+ function projectToScreen(out, viewProjection, point, width, height) {
90
+ const [x, y, z] = point;
91
+ const m = viewProjection;
92
+ const w = m[3] * x + m[7] * y + m[11] * z + m[15];
93
+ if (w <= 1e-4) return false;
94
+ const cx = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
95
+ const cy = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
96
+ out[0] = (cx * .5 + .5) * width;
97
+ out[1] = (.5 - cy * .5) * height;
98
+ out[2] = w;
99
+ return true;
100
+ }
101
+ //#endregion
102
+ export { clamp, damp, lerp, lerp3, lookAt, mat4, multiply, perspective, projectToScreen, smoothstep };
@@ -0,0 +1,28 @@
1
+ import { FormationProgress } from "./types.js";
2
+ //#region src/gl/stage.d.ts
3
+ /**
4
+ * Stage math shared by the controller, the engine and the anchors. A stage is
5
+ * a formation index plus the progress of the morph to the next formation.
6
+ */
7
+ /** Local progress formation `k` sees at the given stage. */
8
+ export declare const localOf: (k: number, stage: number, local: number) => number;
9
+ /** How present formation `k` is: 1 while held, fading out across transitions. */
10
+ export declare const presenceOf: (k: number, stage: number) => number;
11
+ /** 0 → 1 while formation `k` morphs in from the previous one, 1 from then on. */
12
+ export declare const approachOf: (k: number, stage: number) => number;
13
+ export declare function progressOf(k: number, stage: number, local: number, time: number): FormationProgress;
14
+ export interface SectionSpan {
15
+ /** Scroll offset at which the section's top reaches the top of the viewport. */
16
+ start: number;
17
+ /** Scroll offset at which its bottom reaches the bottom of the viewport. */
18
+ end: number;
19
+ }
20
+ /**
21
+ * Scroll → stage. A section holds its formation while pinned (local 0 → 1)
22
+ * and morphs into the next one while the next section scrolls in.
23
+ */
24
+ export declare function stageAt(sections: SectionSpan[], y: number): {
25
+ stage: number;
26
+ local: number;
27
+ };
28
+ //#endregion
@@ -0,0 +1,55 @@
1
+ import { clamp } from "./math.js";
2
+ //#region src/gl/stage.ts
3
+ /**
4
+ * Stage math shared by the controller, the engine and the anchors. A stage is
5
+ * a formation index plus the progress of the morph to the next formation.
6
+ */
7
+ /** Local progress formation `k` sees at the given stage. */
8
+ const localOf = (k, stage, local) => stage > k + 1e-4 ? 1 : stage < k - 1e-4 ? 0 : local;
9
+ /** How present formation `k` is: 1 while held, fading out across transitions. */
10
+ const presenceOf = (k, stage) => clamp(1 - Math.abs(stage - k), 0, 1);
11
+ /** 0 → 1 while formation `k` morphs in from the previous one, 1 from then on. */
12
+ const approachOf = (k, stage) => clamp(stage - k + 1, 0, 1);
13
+ function progressOf(k, stage, local, time) {
14
+ return {
15
+ stage,
16
+ local: localOf(k, stage, local),
17
+ approach: approachOf(k, stage),
18
+ presence: presenceOf(k, stage),
19
+ time
20
+ };
21
+ }
22
+ /**
23
+ * Scroll → stage. A section holds its formation while pinned (local 0 → 1)
24
+ * and morphs into the next one while the next section scrolls in.
25
+ */
26
+ function stageAt(sections, y) {
27
+ for (let k = 0; k < sections.length; k++) {
28
+ const section = sections[k];
29
+ if (y < section.start) {
30
+ if (k === 0) return {
31
+ stage: 0,
32
+ local: 0
33
+ };
34
+ const previous = sections[k - 1];
35
+ const t = (y - previous.end) / Math.max(1, section.start - previous.end);
36
+ return {
37
+ stage: k - 1 + clamp(t, 0, 1),
38
+ local: 0
39
+ };
40
+ }
41
+ if (y <= section.end) {
42
+ const span = section.end - section.start;
43
+ return {
44
+ stage: k,
45
+ local: span > 0 ? (y - section.start) / span : 0
46
+ };
47
+ }
48
+ }
49
+ return {
50
+ stage: Math.max(0, sections.length - 1),
51
+ local: 1
52
+ };
53
+ }
54
+ //#endregion
55
+ export { approachOf, localOf, presenceOf, progressOf, stageAt };