@lovo/matter 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +5 -1
- package/dist/index.cjs +129 -125
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +58 -58
- package/dist/index.d.ts +58 -58
- package/dist/index.js +130 -126
- package/dist/index.js.map +1 -1
- package/package.json +26 -26
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// src/runtime/
|
|
2
|
-
import { WebGPURenderer } from "three/webgpu";
|
|
1
|
+
// src/runtime/create-renderer/create-renderer.ts
|
|
3
2
|
import { Color } from "three";
|
|
3
|
+
import { WebGPURenderer } from "three/webgpu";
|
|
4
4
|
async function createRenderer(canvas, opts = {}) {
|
|
5
5
|
const {
|
|
6
6
|
antialias = true,
|
|
@@ -26,7 +26,8 @@ async function createRenderer(canvas, opts = {}) {
|
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
28
|
resize();
|
|
29
|
-
const
|
|
29
|
+
const isWebGL = "isWebGLBackend" in three.backend && three.backend.isWebGLBackend === true;
|
|
30
|
+
const backend = forceWebGL || isWebGL ? "webgl2" : "webgpu";
|
|
30
31
|
return {
|
|
31
32
|
three,
|
|
32
33
|
backend,
|
|
@@ -35,105 +36,7 @@ async function createRenderer(canvas, opts = {}) {
|
|
|
35
36
|
};
|
|
36
37
|
}
|
|
37
38
|
|
|
38
|
-
// src/
|
|
39
|
-
var MatterScheduler = class {
|
|
40
|
-
clients = /* @__PURE__ */ new Set();
|
|
41
|
-
rafId = null;
|
|
42
|
-
running = false;
|
|
43
|
-
paused = false;
|
|
44
|
-
idle = false;
|
|
45
|
-
flushPending = false;
|
|
46
|
-
startedAt = 0;
|
|
47
|
-
lastTickAt = 0;
|
|
48
|
-
/** Activate the scheduler. The rAF loop starts on the first client added. */
|
|
49
|
-
start() {
|
|
50
|
-
this.running = true;
|
|
51
|
-
this.paused = false;
|
|
52
|
-
this.maybeQueue();
|
|
53
|
-
}
|
|
54
|
-
/** Halt the rAF loop entirely. Use dispose() for permanent teardown. */
|
|
55
|
-
stop() {
|
|
56
|
-
this.running = false;
|
|
57
|
-
this.cancel();
|
|
58
|
-
}
|
|
59
|
-
/** Temporarily skip ticks without losing client registrations. */
|
|
60
|
-
pause() {
|
|
61
|
-
this.paused = true;
|
|
62
|
-
}
|
|
63
|
-
/** Resume after pause(). */
|
|
64
|
-
resume() {
|
|
65
|
-
this.paused = false;
|
|
66
|
-
if (this.running) this.maybeQueue();
|
|
67
|
-
}
|
|
68
|
-
/** Register a client to be called every frame. */
|
|
69
|
-
add(client) {
|
|
70
|
-
this.clients.add(client);
|
|
71
|
-
if (this.running) this.maybeQueue();
|
|
72
|
-
}
|
|
73
|
-
/** Unregister a client. */
|
|
74
|
-
remove(client) {
|
|
75
|
-
this.clients.delete(client);
|
|
76
|
-
}
|
|
77
|
-
/** Permanent teardown: stop the loop and drop all clients. */
|
|
78
|
-
dispose() {
|
|
79
|
-
this.stop();
|
|
80
|
-
this.clients.clear();
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Mark the scheduler idle. The next tick still fires (a final flush so
|
|
84
|
-
* uniform changes that triggered the idle state are rendered), then the
|
|
85
|
-
* rAF loop halts. Use `requestRender()` or `setIdle(false)` to wake.
|
|
86
|
-
*/
|
|
87
|
-
setIdle(idle) {
|
|
88
|
-
if (this.idle === idle) return;
|
|
89
|
-
this.idle = idle;
|
|
90
|
-
if (idle) {
|
|
91
|
-
this.flushPending = true;
|
|
92
|
-
this.maybeQueue();
|
|
93
|
-
} else {
|
|
94
|
-
this.flushPending = false;
|
|
95
|
-
this.maybeQueue();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/** Force a single tick while idle. Useful for prop-change invalidation. */
|
|
99
|
-
requestRender() {
|
|
100
|
-
if (!this.idle) return;
|
|
101
|
-
this.flushPending = true;
|
|
102
|
-
this.maybeQueue();
|
|
103
|
-
}
|
|
104
|
-
maybeQueue() {
|
|
105
|
-
if (this.rafId !== null) return;
|
|
106
|
-
if (!this.running) return;
|
|
107
|
-
if (this.clients.size === 0) return;
|
|
108
|
-
if (this.idle && !this.flushPending) return;
|
|
109
|
-
this.rafId = requestAnimationFrame(this.frame);
|
|
110
|
-
}
|
|
111
|
-
cancel() {
|
|
112
|
-
if (this.rafId !== null) {
|
|
113
|
-
cancelAnimationFrame(this.rafId);
|
|
114
|
-
this.rafId = null;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
frame = (now) => {
|
|
118
|
-
this.rafId = null;
|
|
119
|
-
if (!this.running || this.paused) return;
|
|
120
|
-
if (this.startedAt === 0) {
|
|
121
|
-
this.startedAt = now;
|
|
122
|
-
this.lastTickAt = now;
|
|
123
|
-
}
|
|
124
|
-
const delta = (now - this.lastTickAt) / 1e3;
|
|
125
|
-
const elapsed = (now - this.startedAt) / 1e3;
|
|
126
|
-
this.lastTickAt = now;
|
|
127
|
-
const tick = { delta, elapsed, now };
|
|
128
|
-
for (const client of this.clients) {
|
|
129
|
-
client(tick);
|
|
130
|
-
}
|
|
131
|
-
this.flushPending = false;
|
|
132
|
-
this.maybeQueue();
|
|
133
|
-
};
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
// src/inputs/CursorInput.ts
|
|
39
|
+
// src/inputs/cursor-input/cursor-input.ts
|
|
137
40
|
var CursorInput = class {
|
|
138
41
|
value;
|
|
139
42
|
target;
|
|
@@ -152,6 +55,7 @@ var CursorInput = class {
|
|
|
152
55
|
this.eventTarget = target ?? (typeof window !== "undefined" ? window : new EventTarget());
|
|
153
56
|
this.element = element;
|
|
154
57
|
this.handleMouseMove = (e) => {
|
|
58
|
+
if (!(e instanceof MouseEvent)) return;
|
|
155
59
|
const me = e;
|
|
156
60
|
if (this.element) {
|
|
157
61
|
const r = this.element.getBoundingClientRect();
|
|
@@ -167,7 +71,7 @@ var CursorInput = class {
|
|
|
167
71
|
};
|
|
168
72
|
this.eventTarget.addEventListener("mousemove", this.handleMouseMove);
|
|
169
73
|
}
|
|
170
|
-
/** Current smoothed position. Implements
|
|
74
|
+
/** Current smoothed position. Implements AnimatableSignal protocol. */
|
|
171
75
|
get() {
|
|
172
76
|
return this.value;
|
|
173
77
|
}
|
|
@@ -206,16 +110,18 @@ var CursorInput = class {
|
|
|
206
110
|
var clamp01 = (n) => Math.max(0, Math.min(1, n));
|
|
207
111
|
var lerp = (a, b, t) => a + (b - a) * t;
|
|
208
112
|
|
|
209
|
-
// src/primitives/
|
|
113
|
+
// src/primitives/color-ramp/color-ramp.ts
|
|
210
114
|
import { mix, vec3 } from "three/tsl";
|
|
211
115
|
import { clamp, div, sub } from "three/tsl";
|
|
212
116
|
function colorRamp(t, stops) {
|
|
213
|
-
|
|
214
|
-
if (
|
|
215
|
-
|
|
216
|
-
|
|
117
|
+
const first = stops[0];
|
|
118
|
+
if (first === void 0) return vec3(0, 0, 0);
|
|
119
|
+
if (stops.length === 1) return mix(first.color, first.color, 0);
|
|
120
|
+
let result = mix(first.color, first.color, 0);
|
|
121
|
+
for (let i = 1; i < stops.length; i += 1) {
|
|
217
122
|
const prev = stops[i - 1];
|
|
218
123
|
const next = stops[i];
|
|
124
|
+
if (prev === void 0 || next === void 0) continue;
|
|
219
125
|
const span = next.position - prev.position;
|
|
220
126
|
if (span <= 0) continue;
|
|
221
127
|
const localT = clamp(div(sub(t, prev.position), span), 0, 1);
|
|
@@ -224,13 +130,13 @@ function colorRamp(t, stops) {
|
|
|
224
130
|
return result;
|
|
225
131
|
}
|
|
226
132
|
|
|
227
|
-
// src/primitives/noise.ts
|
|
133
|
+
// src/primitives/noise/noise.ts
|
|
228
134
|
import { mx_noise_float } from "three/tsl";
|
|
229
135
|
function noise(p) {
|
|
230
136
|
return mx_noise_float(p);
|
|
231
137
|
}
|
|
232
138
|
|
|
233
|
-
// src/primitives/fbm.ts
|
|
139
|
+
// src/primitives/fbm/fbm.ts
|
|
234
140
|
import { add, mul } from "three/tsl";
|
|
235
141
|
function fbm(p, opts = {}) {
|
|
236
142
|
const octaves = opts.octaves ?? 4;
|
|
@@ -240,7 +146,7 @@ function fbm(p, opts = {}) {
|
|
|
240
146
|
let amp = 1;
|
|
241
147
|
let freq = 1;
|
|
242
148
|
let total = amp;
|
|
243
|
-
for (let i = 1; i < octaves; i
|
|
149
|
+
for (let i = 1; i < octaves; i += 1) {
|
|
244
150
|
freq *= lacunarity;
|
|
245
151
|
amp *= gain;
|
|
246
152
|
total += amp;
|
|
@@ -251,13 +157,13 @@ function fbm(p, opts = {}) {
|
|
|
251
157
|
return sum.div(total);
|
|
252
158
|
}
|
|
253
159
|
|
|
254
|
-
// src/primitives/voronoi.ts
|
|
160
|
+
// src/primitives/voronoi/voronoi.ts
|
|
255
161
|
import { mx_worley_noise_float } from "three/tsl";
|
|
256
162
|
function voronoi(p) {
|
|
257
163
|
return mx_worley_noise_float(p);
|
|
258
164
|
}
|
|
259
165
|
|
|
260
|
-
// src/primitives/quantize.ts
|
|
166
|
+
// src/primitives/quantize/quantize.ts
|
|
261
167
|
function quantize(t, steps) {
|
|
262
168
|
if (steps <= 1) {
|
|
263
169
|
return t.mul(0);
|
|
@@ -266,25 +172,25 @@ function quantize(t, steps) {
|
|
|
266
172
|
return t.mul(denom).add(0.5).floor().div(denom);
|
|
267
173
|
}
|
|
268
174
|
|
|
269
|
-
// src/primitives/
|
|
175
|
+
// src/primitives/sdf-circle/sdf-circle.ts
|
|
270
176
|
import { length } from "three/tsl";
|
|
271
177
|
function sdfCircle(p, radius) {
|
|
272
178
|
return length(p).sub(radius);
|
|
273
179
|
}
|
|
274
180
|
|
|
275
|
-
// src/primitives/displace.ts
|
|
181
|
+
// src/primitives/displace/displace.ts
|
|
276
182
|
import { add as add2 } from "three/tsl";
|
|
277
183
|
function displace(p, by) {
|
|
278
184
|
return add2(p, by);
|
|
279
185
|
}
|
|
280
186
|
|
|
281
|
-
// src/primitives/
|
|
282
|
-
import {
|
|
187
|
+
// src/primitives/cursor-ripple/cursor-ripple.ts
|
|
188
|
+
import { length as length2, sin, smoothstep, sub as sub2 } from "three/tsl";
|
|
283
189
|
|
|
284
|
-
// src/primitives/time.ts
|
|
190
|
+
// src/primitives/time/time.ts
|
|
285
191
|
import { time as _builtinTime } from "three/tsl";
|
|
286
192
|
|
|
287
|
-
// src/runtime/
|
|
193
|
+
// src/runtime/reduced-motion/reduced-motion.ts
|
|
288
194
|
import { uniform } from "three/tsl";
|
|
289
195
|
var state = {
|
|
290
196
|
policy: "auto",
|
|
@@ -371,10 +277,10 @@ function getReducedMotionTimeScale() {
|
|
|
371
277
|
return globalScaleUniform;
|
|
372
278
|
}
|
|
373
279
|
|
|
374
|
-
// src/primitives/time.ts
|
|
280
|
+
// src/primitives/time/time.ts
|
|
375
281
|
var time = _builtinTime.mul(getReducedMotionTimeScale());
|
|
376
282
|
|
|
377
|
-
// src/primitives/
|
|
283
|
+
// src/primitives/cursor-ripple/cursor-ripple.ts
|
|
378
284
|
function cursorRipple(p, center, opts = {}) {
|
|
379
285
|
const reach = opts.reach ?? 0.4;
|
|
380
286
|
const frequency = opts.frequency ?? 30;
|
|
@@ -386,8 +292,8 @@ function cursorRipple(p, center, opts = {}) {
|
|
|
386
292
|
return wave.mul(amplitude).mul(decay);
|
|
387
293
|
}
|
|
388
294
|
|
|
389
|
-
// src/primitives/
|
|
390
|
-
import {
|
|
295
|
+
// src/primitives/film-grain/film-grain.ts
|
|
296
|
+
import { fract, length as length3, sin as sin2, vec2 } from "three/tsl";
|
|
391
297
|
function filmGrain(uvNode, intensity, timeOffset = 0) {
|
|
392
298
|
const HASH_C1 = vec2(2127.1, 81.17);
|
|
393
299
|
const HASH_C2 = vec2(1269.5, 283.37);
|
|
@@ -396,7 +302,7 @@ function filmGrain(uvNode, intensity, timeOffset = 0) {
|
|
|
396
302
|
return length3(hash).sub(0.765).mul(intensity);
|
|
397
303
|
}
|
|
398
304
|
|
|
399
|
-
// src/runtime/visibility.ts
|
|
305
|
+
// src/runtime/visibility/visibility.ts
|
|
400
306
|
function createVisibilityWatcher() {
|
|
401
307
|
if (typeof document === "undefined") {
|
|
402
308
|
return {
|
|
@@ -426,7 +332,7 @@ function createVisibilityWatcher() {
|
|
|
426
332
|
};
|
|
427
333
|
}
|
|
428
334
|
|
|
429
|
-
// src/runtime/intersection.ts
|
|
335
|
+
// src/runtime/intersection/intersection.ts
|
|
430
336
|
function createIntersectionWatcher(canvas) {
|
|
431
337
|
if (typeof IntersectionObserver === "undefined") {
|
|
432
338
|
return {
|
|
@@ -461,9 +367,107 @@ function createIntersectionWatcher(canvas) {
|
|
|
461
367
|
}
|
|
462
368
|
};
|
|
463
369
|
}
|
|
370
|
+
|
|
371
|
+
// src/runtime/frame-scheduler/frame-scheduler.ts
|
|
372
|
+
var FrameScheduler = class {
|
|
373
|
+
clients = /* @__PURE__ */ new Set();
|
|
374
|
+
rafId = null;
|
|
375
|
+
running = false;
|
|
376
|
+
paused = false;
|
|
377
|
+
idle = false;
|
|
378
|
+
flushPending = false;
|
|
379
|
+
startedAt = 0;
|
|
380
|
+
lastTickAt = 0;
|
|
381
|
+
/** Activate the scheduler. The rAF loop starts on the first client added. */
|
|
382
|
+
start() {
|
|
383
|
+
this.running = true;
|
|
384
|
+
this.paused = false;
|
|
385
|
+
this.maybeQueue();
|
|
386
|
+
}
|
|
387
|
+
/** Halt the rAF loop entirely. Use dispose() for permanent teardown. */
|
|
388
|
+
stop() {
|
|
389
|
+
this.running = false;
|
|
390
|
+
this.cancel();
|
|
391
|
+
}
|
|
392
|
+
/** Temporarily skip ticks without losing client registrations. */
|
|
393
|
+
pause() {
|
|
394
|
+
this.paused = true;
|
|
395
|
+
}
|
|
396
|
+
/** Resume after pause(). */
|
|
397
|
+
resume() {
|
|
398
|
+
this.paused = false;
|
|
399
|
+
if (this.running) this.maybeQueue();
|
|
400
|
+
}
|
|
401
|
+
/** Register a client to be called every frame. */
|
|
402
|
+
add(client) {
|
|
403
|
+
this.clients.add(client);
|
|
404
|
+
if (this.running) this.maybeQueue();
|
|
405
|
+
}
|
|
406
|
+
/** Unregister a client. */
|
|
407
|
+
remove(client) {
|
|
408
|
+
this.clients.delete(client);
|
|
409
|
+
}
|
|
410
|
+
/** Permanent teardown: stop the loop and drop all clients. */
|
|
411
|
+
dispose() {
|
|
412
|
+
this.stop();
|
|
413
|
+
this.clients.clear();
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Mark the scheduler idle. The next tick still fires (a final flush so
|
|
417
|
+
* uniform changes that triggered the idle state are rendered), then the
|
|
418
|
+
* rAF loop halts. Use `requestRender()` or `setIdle(false)` to wake.
|
|
419
|
+
*/
|
|
420
|
+
setIdle(idle) {
|
|
421
|
+
if (this.idle === idle) return;
|
|
422
|
+
this.idle = idle;
|
|
423
|
+
if (idle) {
|
|
424
|
+
this.flushPending = true;
|
|
425
|
+
this.maybeQueue();
|
|
426
|
+
} else {
|
|
427
|
+
this.flushPending = false;
|
|
428
|
+
this.maybeQueue();
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
/** Force a single tick while idle. Useful for prop-change invalidation. */
|
|
432
|
+
requestRender() {
|
|
433
|
+
if (!this.idle) return;
|
|
434
|
+
this.flushPending = true;
|
|
435
|
+
this.maybeQueue();
|
|
436
|
+
}
|
|
437
|
+
maybeQueue() {
|
|
438
|
+
if (this.rafId !== null) return;
|
|
439
|
+
if (!this.running) return;
|
|
440
|
+
if (this.clients.size === 0) return;
|
|
441
|
+
if (this.idle && !this.flushPending) return;
|
|
442
|
+
this.rafId = requestAnimationFrame(this.frame);
|
|
443
|
+
}
|
|
444
|
+
cancel() {
|
|
445
|
+
if (this.rafId !== null) {
|
|
446
|
+
cancelAnimationFrame(this.rafId);
|
|
447
|
+
this.rafId = null;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
frame = (now) => {
|
|
451
|
+
this.rafId = null;
|
|
452
|
+
if (!this.running || this.paused) return;
|
|
453
|
+
if (this.startedAt === 0) {
|
|
454
|
+
this.startedAt = now;
|
|
455
|
+
this.lastTickAt = now;
|
|
456
|
+
}
|
|
457
|
+
const delta = (now - this.lastTickAt) / 1e3;
|
|
458
|
+
const elapsed = (now - this.startedAt) / 1e3;
|
|
459
|
+
this.lastTickAt = now;
|
|
460
|
+
const tick = { delta, elapsed, now };
|
|
461
|
+
for (const client of this.clients) {
|
|
462
|
+
client(tick);
|
|
463
|
+
}
|
|
464
|
+
this.flushPending = false;
|
|
465
|
+
this.maybeQueue();
|
|
466
|
+
};
|
|
467
|
+
};
|
|
464
468
|
export {
|
|
465
469
|
CursorInput,
|
|
466
|
-
|
|
470
|
+
FrameScheduler,
|
|
467
471
|
colorRamp,
|
|
468
472
|
createIntersectionWatcher,
|
|
469
473
|
createReducedMotionWatcher,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/runtime/createRenderer.ts","../src/runtime/MatterScheduler.ts","../src/inputs/CursorInput.ts","../src/primitives/colorRamp.ts","../src/primitives/noise.ts","../src/primitives/fbm.ts","../src/primitives/voronoi.ts","../src/primitives/quantize.ts","../src/primitives/sdfCircle.ts","../src/primitives/displace.ts","../src/primitives/cursorRipple.ts","../src/primitives/time.ts","../src/runtime/reducedMotion.ts","../src/primitives/filmGrain.ts","../src/runtime/visibility.ts","../src/runtime/intersection.ts"],"sourcesContent":["import { WebGPURenderer } from 'three/webgpu'\nimport { Color } from 'three'\n\nexport type MatterBackend = 'webgpu' | 'webgl2'\n\nexport interface CreateRendererOptions {\n /** Anti-alias the framebuffer. Default: true. */\n antialias?: boolean\n /** Force WebGL2 even if WebGPU is available (useful for testing fallback). Default: false. */\n forceWebGL?: boolean\n /** Clear color (hex, CSS string, or THREE.Color). Default: transparent. */\n clearColor?: number | string | Color\n /** Clear alpha (0–1). Default: 0 (transparent). */\n clearAlpha?: number\n /** Cap on devicePixelRatio. Default: 2. Pass Infinity to disable. */\n maxDPR?: number\n}\n\nexport interface MatterRenderer {\n /** The underlying Three.js WebGPURenderer (which may be running on a WebGL2 backend). */\n three: WebGPURenderer\n /** Which backend the renderer initialized with. */\n backend: MatterBackend\n /** Tear down the renderer and release GPU resources. */\n dispose: () => void\n /** Resize the renderer to the canvas's current client dimensions. */\n resize: () => void\n}\n\n/**\n * Create a Matter renderer wrapping THREE.WebGPURenderer.\n *\n * Tries WebGPU first; falls back to WebGL2 automatically if WebGPU is\n * unavailable on the host. The returned object exposes the underlying\n * three renderer plus a small wrapper for resize and disposal.\n */\nexport async function createRenderer(\n canvas: HTMLCanvasElement,\n opts: CreateRendererOptions = {},\n): Promise<MatterRenderer> {\n const {\n antialias = true,\n forceWebGL = false,\n clearColor = 0x000000,\n clearAlpha = 0,\n maxDPR = 2,\n } = opts\n\n const three = new WebGPURenderer({\n canvas,\n antialias,\n forceWebGL,\n })\n\n await three.init()\n\n three.setPixelRatio(Math.min(window.devicePixelRatio, maxDPR))\n const resolvedClearColor =\n clearColor instanceof Color ? clearColor : new Color(clearColor as number | string)\n three.setClearColor(resolvedClearColor, clearAlpha)\n\n const resize = () => {\n const w = canvas.clientWidth\n const h = canvas.clientHeight\n if (canvas.width !== w * three.getPixelRatio() || canvas.height !== h * three.getPixelRatio()) {\n three.setSize(w, h, false)\n }\n }\n resize()\n\n // Detect backend after init. The exact API may differ between three versions;\n // probe the renderer's backend symbol if present, fall back to a property check.\n const backend: MatterBackend =\n forceWebGL ||\n (three as unknown as { backend?: { isWebGLBackend?: boolean } }).backend?.isWebGLBackend\n ? 'webgl2'\n : 'webgpu'\n\n return {\n three,\n backend,\n dispose: () => three.dispose(),\n resize,\n }\n}\n","export interface SchedulerTick {\n /** Seconds since the previous tick. 0 on the first call. */\n delta: number\n /** Total seconds since the scheduler started its current run. */\n elapsed: number\n /** The raw `performance.now()` timestamp the rAF callback received. */\n now: number\n}\n\nexport type SchedulerClient = (tick: SchedulerTick) => void\n\n/**\n * Batches `requestAnimationFrame` calls across all clients registered with\n * a single scheduler. One scheduler is created per <MatterScene>; clients\n * are typically a Three.js renderer's render call.\n */\nexport class MatterScheduler {\n private readonly clients = new Set<SchedulerClient>()\n private rafId: number | null = null\n private running = false\n private paused = false\n private idle = false\n private flushPending = false\n private startedAt = 0\n private lastTickAt = 0\n\n /** Activate the scheduler. The rAF loop starts on the first client added. */\n start(): void {\n this.running = true\n this.paused = false\n this.maybeQueue()\n }\n\n /** Halt the rAF loop entirely. Use dispose() for permanent teardown. */\n stop(): void {\n this.running = false\n this.cancel()\n }\n\n /** Temporarily skip ticks without losing client registrations. */\n pause(): void {\n this.paused = true\n }\n\n /** Resume after pause(). */\n resume(): void {\n this.paused = false\n if (this.running) this.maybeQueue()\n }\n\n /** Register a client to be called every frame. */\n add(client: SchedulerClient): void {\n this.clients.add(client)\n if (this.running) this.maybeQueue()\n }\n\n /** Unregister a client. */\n remove(client: SchedulerClient): void {\n this.clients.delete(client)\n }\n\n /** Permanent teardown: stop the loop and drop all clients. */\n dispose(): void {\n this.stop()\n this.clients.clear()\n }\n\n /**\n * Mark the scheduler idle. The next tick still fires (a final flush so\n * uniform changes that triggered the idle state are rendered), then the\n * rAF loop halts. Use `requestRender()` or `setIdle(false)` to wake.\n */\n setIdle(idle: boolean): void {\n if (this.idle === idle) return\n this.idle = idle\n if (idle) {\n this.flushPending = true\n this.maybeQueue()\n } else {\n this.flushPending = false\n this.maybeQueue()\n }\n }\n\n /** Force a single tick while idle. Useful for prop-change invalidation. */\n requestRender(): void {\n if (!this.idle) return\n this.flushPending = true\n this.maybeQueue()\n }\n\n private maybeQueue(): void {\n if (this.rafId !== null) return\n if (!this.running) return\n if (this.clients.size === 0) return\n if (this.idle && !this.flushPending) return\n this.rafId = requestAnimationFrame(this.frame)\n }\n\n private cancel(): void {\n if (this.rafId !== null) {\n cancelAnimationFrame(this.rafId)\n this.rafId = null\n }\n }\n\n private readonly frame = (now: number): void => {\n this.rafId = null\n if (!this.running || this.paused) return\n\n if (this.startedAt === 0) {\n this.startedAt = now\n this.lastTickAt = now\n }\n const delta = (now - this.lastTickAt) / 1000\n const elapsed = (now - this.startedAt) / 1000\n this.lastTickAt = now\n\n const tick: SchedulerTick = { delta, elapsed, now }\n for (const client of this.clients) {\n client(tick)\n }\n\n this.flushPending = false\n this.maybeQueue()\n }\n}\n","export type Vec2 = readonly [number, number]\n\nexport interface CursorInputOptions {\n /**\n * Smoothing factor: 0 = no smoothing (snap to target instantly).\n * 1 = max smoothing (essentially never reaches target).\n * Sensible default: 0.1.\n *\n * Implementation: per-frame, value moves toward target by `(1 - smoothing) * delta * 60`,\n * roughly meaning \"at smoothing=0.1, ~90% of the gap is closed in 1 second at 60fps.\"\n */\n smoothing?: number\n /** Starting position. Default: [0.5, 0.5] (center). */\n initial?: Vec2\n /** Listen on this target. Default: window. */\n target?: EventTarget\n /**\n * Element to normalize cursor coordinates against. Default: window viewport.\n *\n * When set, cursor x/y are in [0,1] across the element's bounding rect, with\n * extrapolation outside (negative when left/above, >1 when right/below). This\n * matches what shader UV space expects: a cursor at the canvas's top-left\n * corner reads as (0, 0); at bottom-right as (1, 1); regardless of where the\n * canvas sits in the viewport. Without this, components inside a partial-\n * viewport scene (e.g. a 70vh hero section) see a cursor offset that scales\n * with the canvas's vertical position on the page.\n */\n element?: {\n getBoundingClientRect(): { left: number; top: number; width: number; height: number }\n }\n}\n\ntype ChangeListener = (value: Vec2) => void\n\n/**\n * Smoothed pointer tracker emitting a normalized (0..1) Vec2 position.\n * Implements the MatterSignal protocol (`get()` + `on('change', cb)`)\n * so it composes with Motion's `useTransform` and similar tools.\n */\nexport class CursorInput {\n private value: [number, number]\n private target: [number, number]\n private targetDirty = false\n private readonly smoothing: number\n private readonly listeners = new Set<ChangeListener>()\n private readonly eventTarget: EventTarget\n private readonly element: CursorInputOptions['element']\n private readonly handleMouseMove: (e: Event) => void\n private disposed = false\n\n constructor(opts: CursorInputOptions = {}) {\n const { smoothing = 0.1, initial = [0.5, 0.5], target, element } = opts\n this.smoothing = clamp01(smoothing)\n this.value = [initial[0], initial[1]]\n this.target = [initial[0], initial[1]]\n this.eventTarget = target ?? (typeof window !== 'undefined' ? window : new EventTarget())\n this.element = element\n\n this.handleMouseMove = (e: Event) => {\n const me = e as MouseEvent\n if (this.element) {\n // Normalize to 0..1 across the element's bounding rect. Reading the\n // rect on every move is fine — `getBoundingClientRect` is cheap and\n // mousemove is already throttled to ~60Hz by the browser. The benefit\n // is tracking the element's position even if it moved/scrolled since\n // the last frame.\n const r = this.element.getBoundingClientRect()\n const w = r.width || 1\n const h = r.height || 1\n this.target = [(me.clientX - r.left) / w, (me.clientY - r.top) / h]\n } else {\n // Fallback: viewport-normalized. Used when no element is supplied —\n // mostly the standalone-API case for users not consuming through\n // <MatterScene>'s context.\n const w = (typeof window !== 'undefined' && window.innerWidth) || 1\n const h = (typeof window !== 'undefined' && window.innerHeight) || 1\n this.target = [me.clientX / w, me.clientY / h]\n }\n this.targetDirty = true\n }\n\n this.eventTarget.addEventListener('mousemove', this.handleMouseMove)\n }\n\n /** Current smoothed position. Implements MatterSignal protocol. */\n get(): Vec2 {\n return this.value\n }\n\n /** Subscribe to change events. Returns an unsubscribe function. */\n on(_event: 'change', cb: ChangeListener): () => void {\n this.listeners.add(cb)\n return () => this.listeners.delete(cb)\n }\n\n /**\n * Advance the smoothing one tick. Called by the host scheduler; not\n * typically called directly except in tests.\n */\n tick(delta: number): void {\n if (this.disposed) return\n const factor = this.smoothing === 0 ? 1 : 1 - Math.pow(this.smoothing, delta * 60)\n const prev0 = this.value[0]\n const prev1 = this.value[1]\n const next0 = lerp(prev0, this.target[0], factor)\n const next1 = lerp(prev1, this.target[1], factor)\n const moved = next0 !== prev0 || next1 !== prev1\n if (moved || this.targetDirty) {\n this.value = [next0, next1]\n this.targetDirty = false\n const snapshot: Vec2 = [next0, next1]\n for (const listener of this.listeners) listener(snapshot)\n }\n }\n\n /** Tear down listeners. */\n dispose(): void {\n if (this.disposed) return\n this.disposed = true\n this.eventTarget.removeEventListener('mousemove', this.handleMouseMove)\n this.listeners.clear()\n }\n}\n\nconst clamp01 = (n: number) => Math.max(0, Math.min(1, n))\nconst lerp = (a: number, b: number, t: number) => a + (b - a) * t\n","import { mix, vec3 } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport { clamp, div, sub } from 'three/tsl'\n\n/**\n * Canonical TSL-node *input* shape used throughout `@lovo/matter`.\n *\n * Stays as the broad `Node | ShaderNodeObject<Node>` union so callers can\n * pass uniform-typed nodes (e.g. `ShaderNodeObject<UniformNode<Vector2>>`)\n * without casting at the call site — those are subtypes of `Node` but NOT\n * subtypes of `ShaderNodeObject<Node>` due to invariant generic parameters.\n *\n * Wrappers should return the narrower `ShaderNodeObject<Node>` so the\n * **output** is always chainable without casts.\n */\nexport type TSLNode = Node | ShaderNodeObject<Node>\n\nexport interface ColorRampStop {\n /** Color expressed as a TSL node (typically `vec3(r,g,b)`). */\n color: TSLNode\n /** Position 0..1 along the ramp. */\n position: number\n}\n\n/**\n * Multi-stop color interpolation. Given a t in [0..1] and N color stops at\n * fixed positions, returns the smoothly-interpolated color.\n *\n * Falls back to the first/last stop's color outside the bracketing positions.\n */\nexport function colorRamp(t: TSLNode, stops: ColorRampStop[]): ShaderNodeObject<Node> {\n if (stops.length === 0) return vec3(0, 0, 0)\n if (stops.length === 1) return stops[0]!.color as ShaderNodeObject<Node>\n\n // Build a chain of nested mixes, one per adjacent pair of stops.\n // For three stops at positions 0, 0.5, 1:\n // inner = mix(stop0, stop1, smoothstep(0, 0.5, t))\n // outer = mix(inner, stop2, smoothstep(0.5, 1, t))\n let result: ShaderNodeObject<Node> = stops[0]!.color as ShaderNodeObject<Node>\n for (let i = 1; i < stops.length; i++) {\n const prev = stops[i - 1]!\n const next = stops[i]!\n const span = next.position - prev.position\n if (span <= 0) continue\n // Localize t into the [prev..next] range. `t` is TSLNode (the union),\n // so we use functional-form ops to avoid needing a chain-method receiver.\n const localT = clamp(div(sub(t, prev.position), span), 0, 1)\n result = mix(result, next.color, localT)\n }\n return result\n}\n","// packages/matter/src/primitives/noise.ts\nimport { mx_noise_float } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\nimport type { TSLNode } from './colorRamp.js'\n\n/**\n * 2D simplex noise sampled at a point. Returns a scalar TSL node in\n * approximately [-1, 1] (MaterialX's mx_noise_float is roughly that range).\n *\n * @param p — Vec2 TSL node (typically `uv()` or a scaled/offset uv).\n *\n * Built on top of three's `mx_noise_float`; we wrap it so consumers have a\n * stable import path through `@lovo/matter` and we can swap the\n * implementation if a different noise primitive proves better in practice.\n *\n * Returns `ShaderNodeObject<Node>` (chainable) rather than the broader\n * `TSLNode` union, so callers can `.add(...)`/`.mul(...)` without casting.\n */\nexport function noise(p: TSLNode): ShaderNodeObject<Node> {\n return mx_noise_float(p)\n}\n","// packages/matter/src/primitives/fbm.ts\nimport { add, mul } from 'three/tsl'\nimport { noise } from './noise.js'\nimport type { TSLNode } from './colorRamp.js'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nexport interface FBMOptions {\n /** Number of octaves to sum. JS-side number — fixed at TSL build time, not a uniform. Default: 4. */\n octaves?: number\n /** Per-octave frequency multiplier. JS-side number. Default: 2. */\n lacunarity?: number\n /** Per-octave amplitude multiplier. JS-side number. Default: 0.5. */\n gain?: number\n}\n\n/**\n * Fractal Brownian Motion — sum of N octaves of 2D simplex noise.\n *\n * Each octave samples noise at a higher frequency (× `lacunarity`) and lower\n * amplitude (× `gain`) than the previous one, AND at a translated coordinate\n * so the octaves sample uncorrelated regions of noise space. Without the\n * per-octave translation, octaves at related frequencies tend to pile up\n * peaks and troughs at the same input coordinates, producing visibly muddy\n * \"spotty\" output. With it, the octaves look like independent noise patterns\n * layered together — Inigo Quilez's classic FBM technique.\n *\n * `octaves`, `lacunarity`, and `gain` are JavaScript numbers (NOT TSL\n * uniforms) because the loop must be unrolled at TSL-build time — TSL has\n * no dynamic-length loop primitive that maps cleanly to all backends.\n * Animatable parameters that *do* survive on the GPU are the input UV\n * (which the caller can scale/translate per frame) and `time`.\n *\n * Returns `ShaderNodeObject<Node>` (chainable) for cast-free call sites.\n *\n * @param p — Vec2 or Vec3 TSL node (UV-space position).\n * @returns scalar TSL node, normalized to roughly [-1..1] regardless of\n * octave count thanks to the amplitude-sum division at the end.\n */\nexport function fbm(p: TSLNode, opts: FBMOptions = {}): ShaderNodeObject<Node> {\n const octaves = opts.octaves ?? 4\n const lacunarity = opts.lacunarity ?? 2\n const gain = opts.gain ?? 0.5\n\n let sum: ShaderNodeObject<Node> = noise(p)\n let amp = 1\n let freq = 1\n let total = amp\n for (let i = 1; i < octaves; i++) {\n freq *= lacunarity\n amp *= gain\n total += amp\n // Per-octave decorrelation: translate the sample point by a growing\n // offset so this octave reads from a totally different region of noise\n // space than the previous one. Magnitude 100 is well past simplex\n // noise's ~1-unit feature size, so adjacent octaves are fully\n // decorrelated. The scalar broadcasts across all components of `p`\n // (works for vec2 and vec3 inputs alike).\n //\n // Build the chain functionally from `p`: gotcha #12 doesn't apply\n // because `p` is uv-rooted, but the TSLNode union still requires\n // functional form on this hop.\n const pAtFreq = add(mul(p, freq), i * 100)\n const layer = noise(pAtFreq).mul(amp)\n sum = sum.add(layer)\n }\n // Normalize to approximate [-1..1] regardless of octave count / gain.\n return sum.div(total)\n}\n","// packages/matter/src/primitives/voronoi.ts\nimport { mx_worley_noise_float } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\nimport type { TSLNode } from './colorRamp.js'\n\n/**\n * 2D voronoi (Worley) noise — distance to the nearest jittered cell point,\n * normalized roughly to [0, 1]. Higher values = farther from any cell point\n * (cell interiors); lower values = near a cell boundary.\n *\n * Built on three's `mx_worley_noise_float`. Combine with `colorRamp` for\n * a multi-color cellular pattern; threshold via `step`/`smoothstep` for\n * hard cell shapes.\n *\n * Returns `ShaderNodeObject<Node>` (chainable) for cast-free call sites.\n *\n * @param p — Vec2 TSL node, typically `uv() * scale`.\n */\nexport function voronoi(p: TSLNode): ShaderNodeObject<Node> {\n return mx_worley_noise_float(p)\n}\n","// packages/matter/src/primitives/quantize.ts\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\n/**\n * Quantize a scalar TSL node to `steps` discrete levels.\n *\n * quantize(t, 4) → values in {0, 0.25, 0.5, 0.75, 1.0}\n *\n * `steps` is a JS-side number (loop-equivalent at TSL build time, baked in).\n * If you need an animatable step count, rebuild the TSL fragment.\n */\nexport function quantize(t: ShaderNodeObject<Node>, steps: number): ShaderNodeObject<Node> {\n if (steps <= 1) {\n // Edge case: single step → constant 0. Return as-is wrapped in mul(0).\n return t.mul(0)\n }\n const denom = steps - 1\n // floor(t * (steps-1) + 0.5) / (steps-1)\n // Using floor(x + 0.5) instead of round() for TSL portability.\n return t.mul(denom).add(0.5).floor().div(denom)\n}\n","import { length } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\nimport type { TSLNode } from './colorRamp.js'\n\n/**\n * Signed distance field for a circle centered at the origin.\n *\n * sdfCircle(p, r) = length(p) - r\n *\n * Negative inside the circle, zero on the boundary, positive outside.\n * Combine with `smoothstep(-edge, +edge, sdf)` to render a soft-edged disk.\n *\n * @param p — Vec2 TSL node (typically a UV-space offset from the center).\n * @param radius — JS-side scalar OR a scalar TSL node.\n */\nexport function sdfCircle(p: TSLNode, radius: TSLNode | number): ShaderNodeObject<Node> {\n return length(p).sub(radius)\n}\n","import { add } from 'three/tsl'\nimport type { TSLNode } from './colorRamp.js'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\n/**\n * Naive vector addition: returns `p + by`.\n *\n * displace(p, by) = p + by\n *\n * Thin wrapper that names the spatial intent of shifting a sample point.\n *\n * **SDF caveat:** when using this to translate an SDF render, pass the\n * NEGATED translation — `sdfCircle(displace(p, v.mul(-1)), r)` renders the\n * disk at position `+v` because SDF translation evaluates as\n * `length(p - center) - r`. Adding `+v` to the sample point shifts the\n * rendered shape in the OPPOSITE direction.\n *\n * @param p — Vec2 TSL node (the position being displaced).\n * @param by — Vec2 TSL node (the displacement vector).\n */\nexport function displace(p: TSLNode, by: TSLNode): ShaderNodeObject<Node> {\n return add(p, by)\n}\n","import { sin, length, smoothstep, sub } from 'three/tsl'\nimport { time } from './time.js'\nimport type { TSLNode } from './colorRamp.js'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nexport interface CursorRippleOptions {\n /** Decay radius (UV space). Beyond this, the ripple is ~0. Default: 0.4. */\n reach?: number\n /** Wavelength controls the ripple spacing. Default: 30. Larger = wider rings. */\n frequency?: number\n /** Time multiplier on the wave phase. Default: 6. Larger = faster oscillation. */\n speed?: number\n /** Output amplitude. Default: 0.5. Final result is in roughly [-amplitude, +amplitude]. */\n amplitude?: number\n}\n\n/**\n * A radial ripple emanating from `center`. Returns a scalar TSL node in\n * roughly [-amplitude, +amplitude] that decays to ~0 outside `reach`.\n *\n * ripple = sin(d*frequency - time*speed) * amplitude * smoothstep(reach, 0, d)\n *\n * Compose into a wave field by adding it to the underlying base wave.\n *\n * Note: `frequency` / `speed` / `reach` / `amplitude` are JS-side numbers\n * (baked into the TSL fragment at material-build time). The animatable\n * cursor position is the only live uniform consumed.\n *\n * @param p — Vec2 TSL node (typically `uv()`).\n * @param center — Vec2 TSL node (cursor uniform, in UV space).\n */\nexport function cursorRipple(\n p: TSLNode,\n center: TSLNode,\n opts: CursorRippleOptions = {},\n): ShaderNodeObject<Node> {\n const reach = opts.reach ?? 0.4\n const frequency = opts.frequency ?? 30\n const speed = opts.speed ?? 6\n const amplitude = opts.amplitude ?? 0.5\n\n // d = length(p - center). Use functional `sub(p, center)` because both\n // are typed as the broad TSLNode union (no chain receiver). Per gotcha #12,\n // building from a raw `uniform()` receiver silently produces wrong GPU\n // values, so the functional form is also safer for `center` being a uniform.\n const d = length(sub(p, center))\n // `time` is the engine-gated TSL node (from primitives/time.ts);\n // chains rooted in `time` automatically respect `prefers-reduced-motion` and\n // the runtime override set via `setReducedMotionPolicy`.\n const wave = sin(d.mul(frequency).sub(time.mul(speed)))\n const decay = smoothstep(reach, 0, d)\n return wave.mul(amplitude).mul(decay)\n}\n","// Engine-gated `time` — equals the TSL built-in `time` multiplied by the\n// reduced-motion scale uniform. Components consuming `time` from `@lovo/matter`\n// automatically respect `prefers-reduced-motion` and the policy override set\n// via `setReducedMotionPolicy`.\n//\n// If you want raw uncapped time (e.g. for a debug overlay), import `time`\n// from `three/tsl` directly.\n\nimport { time as _builtinTime } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\nimport { getReducedMotionTimeScale } from '../runtime/reducedMotion.js'\n\nexport const time: ShaderNodeObject<Node> = _builtinTime.mul(getReducedMotionTimeScale())\n","import { uniform } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nexport type ReducedMotionPolicy = 'auto' | 'off' | 'slow' | 'paused'\n\n/**\n * Public surface exposed to package consumers. `recompute` is intentionally\n * absent — it is engine-internal and should not be callable from outside.\n */\nexport interface ReducedMotionWatcher {\n /** Current time scale: 0, 0.3, or 1. */\n scale(): number\n /** Subscribe to scale changes. Returns unsubscribe. */\n subscribe(cb: (scale: number) => void): () => void\n /** Tear down media-query listener. */\n dispose(): void\n}\n\n/**\n * Engine-internal extension of the public watcher. Only `setReducedMotionPolicy`\n * calls `recompute`; it is never part of the consumer-visible type.\n */\ninterface InternalWatcher extends ReducedMotionWatcher {\n recompute(): void\n}\n\ninterface PolicyState {\n policy: ReducedMotionPolicy\n watchers: Set<InternalWatcher>\n}\n\nconst state: PolicyState = {\n policy: 'auto',\n watchers: new Set(),\n}\n\n/**\n * Override Matter's default behavior of honoring `prefers-reduced-motion`.\n * - 'auto' — follow the OS media query (default)\n * - 'off' — full speed regardless of OS setting\n * - 'slow' — 30% speed regardless of OS setting\n * - 'paused' — 0 (animation effectively frozen) regardless of OS setting\n */\nexport function setReducedMotionPolicy(policy: ReducedMotionPolicy): void {\n if (state.policy === policy) return\n state.policy = policy\n for (const w of state.watchers) w.recompute()\n}\n\nexport function getReducedMotionPolicy(): ReducedMotionPolicy {\n return state.policy\n}\n\nconst computeScale = (mqlMatches: boolean): number => {\n switch (state.policy) {\n case 'off':\n return 1\n case 'slow':\n return 0.3\n case 'paused':\n return 0\n case 'auto':\n return mqlMatches ? 0.3 : 1\n }\n}\n\n/**\n * Create a watcher that tracks `prefers-reduced-motion: reduce` and the\n * global Matter policy override. Strict-mode-safe — callers create+dispose\n * one per mount cycle.\n */\nexport function createReducedMotionWatcher(): ReducedMotionWatcher {\n // SSR safety: bail to the no-op watcher if matchMedia is missing.\n // SSR watcher: scale() respects policy override but does not emit\n // subscription events (the engine has no way to notify SSR-created\n // watchers because they are not added to state.watchers — but in\n // practice CLAUDE.md gotcha #10 requires `ssr: false` for any component\n // that touches the matter engine).\n if (typeof matchMedia !== 'function') {\n return {\n scale: () => computeScale(false),\n subscribe: (cb) => {\n // No-op: SSR watchers are not in state.watchers and will never\n // receive policy-change notifications.\n void cb\n return () => {}\n },\n /** SSR watcher does not emit policy-change notifications. */\n dispose: () => {},\n }\n }\n\n const mql = matchMedia('(prefers-reduced-motion: reduce)')\n const subs = new Set<(s: number) => void>()\n let last = computeScale(mql.matches)\n\n const onChange = () => {\n const next = computeScale(mql.matches)\n if (next !== last) {\n last = next\n for (const cb of subs) cb(next)\n }\n }\n\n mql.addEventListener('change', onChange)\n\n const watcher: InternalWatcher = {\n scale: () => last,\n subscribe(cb) {\n subs.add(cb)\n return () => subs.delete(cb)\n },\n recompute() {\n const next = computeScale(mql.matches)\n if (next !== last) {\n last = next\n for (const cb of subs) cb(next)\n }\n },\n dispose() {\n mql.removeEventListener('change', onChange)\n subs.clear()\n state.watchers.delete(watcher)\n },\n }\n state.watchers.add(watcher)\n return watcher\n}\n\nlet globalScaleUniform: ShaderNodeObject<Node> | null = null\nlet globalWatcher: ReducedMotionWatcher | null = null\n\n/**\n * Returns the engine-shared TSL uniform that `time` is multiplied by. Lazily\n * initialized on first read; reused across all materials. Mutating `.value`\n * imperatively when policy changes is safe — TSL re-reads the uniform every\n * frame.\n */\nexport function getReducedMotionTimeScale(): ShaderNodeObject<Node> {\n if (globalScaleUniform === null) {\n globalWatcher = createReducedMotionWatcher()\n globalScaleUniform = uniform(globalWatcher.scale()) as unknown as ShaderNodeObject<Node>\n globalWatcher.subscribe((s) => {\n if (globalScaleUniform === null) return\n ;(globalScaleUniform as unknown as { value: number }).value = s\n })\n }\n return globalScaleUniform\n}\n\n// Keep a typed reference for tests that may want to re-init between tests.\nexport const __resetReducedMotionForTests = () => {\n globalWatcher?.dispose()\n globalWatcher = null\n globalScaleUniform = null\n}\n","import { vec2, sin, fract, length } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\n/**\n * Hash-based film grain — chaotic, uncorrelated per-pixel noise sampled\n * from `uvNode`. The output is *centered* around zero so it acts as a\n * brightness-preserving texture overlay (half the pixels brighten by up\n * to `intensity`, half darken, mean unchanged). ADD the result to a color.\n *\n * filmGrain(uv, k) → static grain\n * filmGrain(uv, k, time) → twinkling grain. Pass a quantized time node\n * (e.g. `time.mul(speed).mul(60).floor()`) so\n * the grain re-randomizes at a controllable\n * \"shutter rate\" instead of every frame.\n *\n * Recipe:\n *\n * base = vec2(uv·c1, uv·c2) + timeOffset\n * hash = fract(sin(base) * 43758.5453)\n * out = (length(hash) - 0.765) * intensity\n *\n * `c1 = (2127.1, 81.17)` and `c2 = (1269.5, 283.37)` are arbitrary\n * near-prime constants that produce visually-uncorrelated noise. `0.765`\n * is the empirical mean of `length(vec2(u, v))` for uniform u, v ∈ [0, 1),\n * computed once so we don't have to subtract it at runtime per pixel.\n *\n * For a film-stock look (darkens as grain rises — silver-emulsion\n * physics) subtract the result from the color instead of adding.\n *\n * @param uvNode vec2 TSL node, typically `uv()`.\n * @param intensity number or TSL node in [0, 1]; scales the grain.\n * @param timeOffset optional number or TSL node added to each sample\n * before hashing. `0` (default) → static grain.\n */\nexport function filmGrain(\n uvNode: ShaderNodeObject<Node>,\n intensity: ShaderNodeObject<Node> | number,\n timeOffset: ShaderNodeObject<Node> | number = 0,\n): ShaderNodeObject<Node> {\n const HASH_C1 = vec2(2127.1, 81.17)\n const HASH_C2 = vec2(1269.5, 283.37)\n const base = vec2(uvNode.dot(HASH_C1).add(timeOffset), uvNode.dot(HASH_C2).add(timeOffset))\n\n const hash = fract(sin(base).mul(43758.5453))\n\n return length(hash).sub(0.765).mul(intensity)\n}\n","export interface VisibilityWatcher {\n isVisible(): boolean\n /** Subscribe to changes. Receives the new visibility state. Returns unsubscribe. */\n subscribe(cb: (visible: boolean) => void): () => void\n dispose(): void\n}\n\n/**\n * Watch `document.visibilityState`. Strict-mode-safe — callers create+dispose\n * one per mount cycle.\n *\n * SSR: if `document` is unavailable, returns a no-op watcher whose\n * `isVisible()` always returns `true` and whose `subscribe` does nothing.\n */\nexport function createVisibilityWatcher(): VisibilityWatcher {\n if (typeof document === 'undefined') {\n return {\n isVisible: () => true,\n subscribe: () => () => {},\n dispose: () => {},\n }\n }\n\n const subs = new Set<(v: boolean) => void>()\n const onChange = () => {\n const v = document.visibilityState === 'visible'\n for (const cb of subs) cb(v)\n }\n document.addEventListener('visibilitychange', onChange)\n\n return {\n isVisible: () => document.visibilityState === 'visible',\n subscribe(cb) {\n subs.add(cb)\n return () => subs.delete(cb)\n },\n dispose() {\n document.removeEventListener('visibilitychange', onChange)\n subs.clear()\n },\n }\n}\n","export interface IntersectionWatcher {\n isInView(): boolean\n /** Subscribe to changes. Receives the new in-view state. Returns unsubscribe. */\n subscribe(cb: (inView: boolean) => void): () => void\n dispose(): void\n}\n\n/**\n * Watch a canvas's viewport intersection. Pauses tied to this watcher should\n * be resumed when the canvas is *any* fraction visible. Strict-mode-safe.\n *\n * SSR: if `IntersectionObserver` is unavailable, returns a no-op watcher whose\n * `isInView()` always returns `true` and whose `subscribe` does nothing.\n */\nexport function createIntersectionWatcher(canvas: HTMLCanvasElement): IntersectionWatcher {\n if (typeof IntersectionObserver === 'undefined') {\n return {\n isInView: () => true,\n subscribe: () => () => {},\n dispose: () => {},\n }\n }\n\n const subs = new Set<(v: boolean) => void>()\n let inView = true\n const obs = new IntersectionObserver(\n (entries) => {\n const next = entries.some((e) => e.isIntersecting)\n if (next === inView) return\n inView = next\n for (const cb of subs) cb(inView)\n },\n { threshold: 0 },\n )\n obs.observe(canvas)\n\n return {\n isInView: () => inView,\n subscribe(cb) {\n subs.add(cb)\n return () => subs.delete(cb)\n },\n dispose() {\n obs.disconnect()\n subs.clear()\n },\n }\n}\n"],"mappings":";AAAA,SAAS,sBAAsB;AAC/B,SAAS,aAAa;AAmCtB,eAAsB,eACpB,QACA,OAA8B,CAAC,GACN;AACzB,QAAM;AAAA,IACJ,YAAY;AAAA,IACZ,aAAa;AAAA,IACb,aAAa;AAAA,IACb,aAAa;AAAA,IACb,SAAS;AAAA,EACX,IAAI;AAEJ,QAAM,QAAQ,IAAI,eAAe;AAAA,IAC/B;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,MAAM,KAAK;AAEjB,QAAM,cAAc,KAAK,IAAI,OAAO,kBAAkB,MAAM,CAAC;AAC7D,QAAM,qBACJ,sBAAsB,QAAQ,aAAa,IAAI,MAAM,UAA6B;AACpF,QAAM,cAAc,oBAAoB,UAAU;AAElD,QAAM,SAAS,MAAM;AACnB,UAAM,IAAI,OAAO;AACjB,UAAM,IAAI,OAAO;AACjB,QAAI,OAAO,UAAU,IAAI,MAAM,cAAc,KAAK,OAAO,WAAW,IAAI,MAAM,cAAc,GAAG;AAC7F,YAAM,QAAQ,GAAG,GAAG,KAAK;AAAA,IAC3B;AAAA,EACF;AACA,SAAO;AAIP,QAAM,UACJ,cACC,MAAgE,SAAS,iBACtE,WACA;AAEN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EA,QAAM,OAAO,oBAAI,IAA0B;AAC3C,QAAM,WAAW,MAAM;AACrB,UAAM,IAAI,SAAS,oBAAoB;AACvC,eAAW,MAAM,KAAM,IAAG,CAAC;AAAA,EAC7B;AACA,WAAS,iBAAiB,oBAAoB,QAAQ;AAEtD,SAAO;AAAA,IACL,WAAW,MAAM,SAAS,oBAAoB;AAAA,IAC9C,UAAU,IAAI;AACZ,WAAK,IAAI,EAAE;AACX,aAAO,MAAM,KAAK,OAAO,EAAE;AAAA,IAC7B;AAAA,IACA,UAAU;AACR,eAAS,oBAAoB,oBAAoB,QAAQ;AACzD,WAAK,MAAM;AAAA,IACb;AAAA,EACF;AACF;;;AC3BO,SAAS,0BAA0B,QAAgD;AACxF,MAAI,OAAO,yBAAyB,aAAa;AAC/C,WAAO;AAAA,MACL,UAAU,MAAM;AAAA,MAChB,WAAW,MAAM,MAAM;AAAA,MAAC;AAAA,MACxB,SAAS,MAAM;AAAA,MAAC;AAAA,IAClB;AAAA,EACF;AAEA,QAAM,OAAO,oBAAI,IAA0B;AAC3C,MAAI,SAAS;AACb,QAAM,MAAM,IAAI;AAAA,IACd,CAAC,YAAY;AACX,YAAM,OAAO,QAAQ,KAAK,CAAC,MAAM,EAAE,cAAc;AACjD,UAAI,SAAS,OAAQ;AACrB,eAAS;AACT,iBAAW,MAAM,KAAM,IAAG,MAAM;AAAA,IAClC;AAAA,IACA,EAAE,WAAW,EAAE;AAAA,EACjB;AACA,MAAI,QAAQ,MAAM;AAElB,SAAO;AAAA,IACL,UAAU,MAAM;AAAA,IAChB,UAAU,IAAI;AACZ,WAAK,IAAI,EAAE;AACX,aAAO,MAAM,KAAK,OAAO,EAAE;AAAA,IAC7B;AAAA,IACA,UAAU;AACR,UAAI,WAAW;AACf,WAAK,MAAM;AAAA,IACb;AAAA,EACF;AACF;","names":["add","length","sub","length","sub","sin","length"]}
|
|
1
|
+
{"version":3,"sources":["../src/runtime/create-renderer/create-renderer.ts","../src/inputs/cursor-input/cursor-input.ts","../src/primitives/color-ramp/color-ramp.ts","../src/primitives/noise/noise.ts","../src/primitives/fbm/fbm.ts","../src/primitives/voronoi/voronoi.ts","../src/primitives/quantize/quantize.ts","../src/primitives/sdf-circle/sdf-circle.ts","../src/primitives/displace/displace.ts","../src/primitives/cursor-ripple/cursor-ripple.ts","../src/primitives/time/time.ts","../src/runtime/reduced-motion/reduced-motion.ts","../src/primitives/film-grain/film-grain.ts","../src/runtime/visibility/visibility.ts","../src/runtime/intersection/intersection.ts","../src/runtime/frame-scheduler/frame-scheduler.ts"],"sourcesContent":["import { Color } from 'three'\nimport { WebGPURenderer } from 'three/webgpu'\n\nexport type GpuBackend = 'webgpu' | 'webgl2'\n\nexport interface CreateRendererOptions {\n /** Anti-alias the framebuffer. Default: true. */\n antialias?: boolean\n /** Force WebGL2 even if WebGPU is available (useful for testing fallback). Default: false. */\n forceWebGL?: boolean\n /** Clear color (hex, CSS string, or THREE.Color). Default: transparent. */\n clearColor?: number | string | Color\n /** Clear alpha (0–1). Default: 0 (transparent). */\n clearAlpha?: number\n /** Cap on devicePixelRatio. Default: 2. Pass Infinity to disable. */\n maxDPR?: number\n}\n\nexport interface GpuRenderer {\n /** The underlying Three.js WebGPURenderer (which may be running on a WebGL2 backend). */\n three: WebGPURenderer\n /** Which backend the renderer initialized with. */\n backend: GpuBackend\n /** Tear down the renderer and release GPU resources. */\n dispose: () => void\n /** Resize the renderer to the canvas's current client dimensions. */\n resize: () => void\n}\n\n/**\n * Create a Matter renderer wrapping THREE.WebGPURenderer.\n *\n * Tries WebGPU first; falls back to WebGL2 automatically if WebGPU is\n * unavailable on the host. The returned object exposes the underlying\n * three renderer plus a small wrapper for resize and disposal.\n */\nexport async function createRenderer(\n canvas: HTMLCanvasElement,\n opts: CreateRendererOptions = {},\n): Promise<GpuRenderer> {\n const {\n antialias = true,\n forceWebGL = false,\n clearColor = 0x000000,\n clearAlpha = 0,\n maxDPR = 2,\n } = opts\n\n const three = new WebGPURenderer({\n canvas,\n antialias,\n forceWebGL,\n })\n\n await three.init()\n\n three.setPixelRatio(Math.min(window.devicePixelRatio, maxDPR))\n const resolvedClearColor = clearColor instanceof Color ? clearColor : new Color(clearColor)\n\n three.setClearColor(resolvedClearColor, clearAlpha)\n\n const resize = () => {\n const w = canvas.clientWidth\n const h = canvas.clientHeight\n\n if (canvas.width !== w * three.getPixelRatio() || canvas.height !== h * three.getPixelRatio()) {\n three.setSize(w, h, false)\n }\n }\n\n resize()\n\n // Detect backend after init. `isWebGLBackend` is an internal duck-type flag\n // not declared in three's public Backend type — probe via `in` rather than\n // a property access that would trip strict typing.\n const isWebGL = 'isWebGLBackend' in three.backend && three.backend.isWebGLBackend === true\n const backend: GpuBackend = forceWebGL || isWebGL ? 'webgl2' : 'webgpu'\n\n return {\n three,\n backend,\n dispose: () => three.dispose(),\n resize,\n }\n}\n","export type Vec2 = readonly [number, number]\n\nexport interface CursorInputOptions {\n /**\n * Smoothing factor: 0 = no smoothing (snap to target instantly).\n * 1 = max smoothing (essentially never reaches target).\n * Sensible default: 0.1.\n *\n * Implementation: per-frame, value moves toward target by `(1 - smoothing) * delta * 60`,\n * roughly meaning \"at smoothing=0.1, ~90% of the gap is closed in 1 second at 60fps.\"\n */\n smoothing?: number\n /** Starting position. Default: [0.5, 0.5] (center). */\n initial?: Vec2\n /** Listen on this target. Default: window. */\n target?: EventTarget\n /**\n * Element to normalize cursor coordinates against. Default: window viewport.\n *\n * When set, cursor x/y are in [0,1] across the element's bounding rect, with\n * extrapolation outside (negative when left/above, >1 when right/below). This\n * matches what shader UV space expects: a cursor at the canvas's top-left\n * corner reads as (0, 0); at bottom-right as (1, 1); regardless of where the\n * canvas sits in the viewport. Without this, components inside a partial-\n * viewport scene (e.g. a 70vh hero section) see a cursor offset that scales\n * with the canvas's vertical position on the page.\n */\n element?: {\n getBoundingClientRect(): { left: number; top: number; width: number; height: number }\n }\n}\n\ntype ChangeListener = (value: Vec2) => void\n\n/**\n * Smoothed pointer tracker emitting a normalized (0..1) Vec2 position.\n * Implements the AnimatableSignal protocol (`get()` + `on('change', cb)`)\n * so it composes with Motion's `useTransform` and similar tools.\n */\nexport class CursorInput {\n private value: [number, number]\n private target: [number, number]\n private targetDirty = false\n private readonly smoothing: number\n private readonly listeners = new Set<ChangeListener>()\n private readonly eventTarget: EventTarget\n private readonly element: CursorInputOptions['element']\n private readonly handleMouseMove: (e: Event) => void\n private disposed = false\n\n constructor(opts: CursorInputOptions = {}) {\n const { smoothing = 0.1, initial = [0.5, 0.5], target, element } = opts\n\n this.smoothing = clamp01(smoothing)\n this.value = [initial[0], initial[1]]\n this.target = [initial[0], initial[1]]\n this.eventTarget = target ?? (typeof window !== 'undefined' ? window : new EventTarget())\n this.element = element\n\n this.handleMouseMove = (e: Event) => {\n if (!(e instanceof MouseEvent)) return\n const me = e\n\n if (this.element) {\n // Normalize to 0..1 across the element's bounding rect. Reading the\n // rect on every move is fine — `getBoundingClientRect` is cheap and\n // mousemove is already throttled to ~60Hz by the browser. The benefit\n // is tracking the element's position even if it moved/scrolled since\n // the last frame.\n const r = this.element.getBoundingClientRect()\n const w = r.width || 1\n const h = r.height || 1\n\n this.target = [(me.clientX - r.left) / w, (me.clientY - r.top) / h]\n } else {\n // Fallback: viewport-normalized. Used when no element is supplied —\n // mostly the standalone-API case for users not consuming through\n // <ShaderScene>'s context.\n const w = (typeof window !== 'undefined' && window.innerWidth) || 1\n const h = (typeof window !== 'undefined' && window.innerHeight) || 1\n\n this.target = [me.clientX / w, me.clientY / h]\n }\n this.targetDirty = true\n }\n\n this.eventTarget.addEventListener('mousemove', this.handleMouseMove)\n }\n\n /** Current smoothed position. Implements AnimatableSignal protocol. */\n get(): Vec2 {\n return this.value\n }\n\n /** Subscribe to change events. Returns an unsubscribe function. */\n on(_event: 'change', cb: ChangeListener): () => void {\n this.listeners.add(cb)\n\n return () => this.listeners.delete(cb)\n }\n\n /**\n * Advance the smoothing one tick. Called by the host scheduler; not\n * typically called directly except in tests.\n */\n tick(delta: number): void {\n if (this.disposed) return\n const factor = this.smoothing === 0 ? 1 : 1 - Math.pow(this.smoothing, delta * 60)\n const prev0 = this.value[0]\n const prev1 = this.value[1]\n const next0 = lerp(prev0, this.target[0], factor)\n const next1 = lerp(prev1, this.target[1], factor)\n const moved = next0 !== prev0 || next1 !== prev1\n\n if (moved || this.targetDirty) {\n this.value = [next0, next1]\n this.targetDirty = false\n const snapshot: Vec2 = [next0, next1]\n\n for (const listener of this.listeners) listener(snapshot)\n }\n }\n\n /** Tear down listeners. */\n dispose(): void {\n if (this.disposed) return\n this.disposed = true\n this.eventTarget.removeEventListener('mousemove', this.handleMouseMove)\n this.listeners.clear()\n }\n}\n\nconst clamp01 = (n: number) => Math.max(0, Math.min(1, n))\nconst lerp = (a: number, b: number, t: number) => a + (b - a) * t\n","import type { ShaderNodeObject } from 'three/tsl'\nimport { mix, vec3 } from 'three/tsl'\nimport { clamp, div, sub } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\n/**\n * Canonical TSL-node *input* shape used throughout `@lovo/matter`.\n *\n * Stays as the broad `Node | ShaderNodeObject<Node>` union so callers can\n * pass uniform-typed nodes (e.g. `ShaderNodeObject<UniformNode<Vector2>>`)\n * without casting at the call site — those are subtypes of `Node` but NOT\n * subtypes of `ShaderNodeObject<Node>` due to invariant generic parameters.\n *\n * Wrappers should return the narrower `ShaderNodeObject<Node>` so the\n * **output** is always chainable without casts.\n */\nexport type TSLNode = Node | ShaderNodeObject<Node>\n\nexport interface ColorRampStop {\n /** Color expressed as a TSL node (typically `vec3(r,g,b)`). */\n color: TSLNode\n /** Position 0..1 along the ramp. */\n position: number\n}\n\n/**\n * Multi-stop color interpolation. Given a t in [0..1] and N color stops at\n * fixed positions, returns the smoothly-interpolated color.\n *\n * Falls back to the first/last stop's color outside the bracketing positions.\n */\nexport function colorRamp(t: TSLNode, stops: ColorRampStop[]): ShaderNodeObject<Node> {\n // TSLNode is wider than ShaderNodeObject<Node> in TSL's published types\n // (see CLAUDE.md gotcha #5). Wrapping with mix(node, node, 0) yields a\n // chainable ShaderNodeObject<Node> without a cast — the GPU shader compiler\n // folds the no-op interpolation away.\n const first = stops[0]\n\n if (first === undefined) return vec3(0, 0, 0)\n if (stops.length === 1) return mix(first.color, first.color, 0)\n\n // Build a chain of nested mixes, one per adjacent pair of stops.\n // For three stops at positions 0, 0.5, 1:\n // inner = mix(stop0, stop1, smoothstep(0, 0.5, t))\n // outer = mix(inner, stop2, smoothstep(0.5, 1, t))\n let result = mix(first.color, first.color, 0)\n\n for (let i = 1; i < stops.length; i += 1) {\n const prev = stops[i - 1]\n const next = stops[i]\n\n if (prev === undefined || next === undefined) continue\n const span = next.position - prev.position\n\n if (span <= 0) continue\n // Localize t into the [prev..next] range. `t` is TSLNode (the union),\n // so we use functional-form ops to avoid needing a chain-method receiver.\n const localT = clamp(div(sub(t, prev.position), span), 0, 1)\n\n result = mix(result, next.color, localT)\n }\n\n return result\n}\n","// packages/matter/src/primitives/noise/noise.ts\nimport { mx_noise_float } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport type { TSLNode } from '../color-ramp/color-ramp.js'\n\n/**\n * 2D simplex noise sampled at a point. Returns a scalar TSL node in\n * approximately [-1, 1] (MaterialX's mx_noise_float is roughly that range).\n *\n * @param p — Vec2 TSL node (typically `uv()` or a scaled/offset uv).\n *\n * Built on top of three's `mx_noise_float`; we wrap it so consumers have a\n * stable import path through `@lovo/matter` and we can swap the\n * implementation if a different noise primitive proves better in practice.\n *\n * Returns `ShaderNodeObject<Node>` (chainable) rather than the broader\n * `TSLNode` union, so callers can `.add(...)`/`.mul(...)` without casting.\n */\nexport function noise(p: TSLNode): ShaderNodeObject<Node> {\n return mx_noise_float(p)\n}\n","// packages/matter/src/primitives/fbm/fbm.ts\nimport { add, mul } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport type { TSLNode } from '../color-ramp/color-ramp.js'\nimport { noise } from '../noise/noise.js'\n\nexport interface FBMOptions {\n /** Number of octaves to sum. JS-side number — fixed at TSL build time, not a uniform. Default: 4. */\n octaves?: number\n /** Per-octave frequency multiplier. JS-side number. Default: 2. */\n lacunarity?: number\n /** Per-octave amplitude multiplier. JS-side number. Default: 0.5. */\n gain?: number\n}\n\n/**\n * Fractal Brownian Motion — sum of N octaves of 2D simplex noise.\n *\n * Each octave samples noise at a higher frequency (× `lacunarity`) and lower\n * amplitude (× `gain`) than the previous one, AND at a translated coordinate\n * so the octaves sample uncorrelated regions of noise space. Without the\n * per-octave translation, octaves at related frequencies tend to pile up\n * peaks and troughs at the same input coordinates, producing visibly muddy\n * \"spotty\" output. With it, the octaves look like independent noise patterns\n * layered together — Inigo Quilez's classic FBM technique.\n *\n * `octaves`, `lacunarity`, and `gain` are JavaScript numbers (NOT TSL\n * uniforms) because the loop must be unrolled at TSL-build time — TSL has\n * no dynamic-length loop primitive that maps cleanly to all backends.\n * Animatable parameters that *do* survive on the GPU are the input UV\n * (which the caller can scale/translate per frame) and `time`.\n *\n * Returns `ShaderNodeObject<Node>` (chainable) for cast-free call sites.\n *\n * @param p — Vec2 or Vec3 TSL node (UV-space position).\n * @returns scalar TSL node, normalized to roughly [-1..1] regardless of\n * octave count thanks to the amplitude-sum division at the end.\n */\nexport function fbm(p: TSLNode, opts: FBMOptions = {}): ShaderNodeObject<Node> {\n const octaves = opts.octaves ?? 4\n const lacunarity = opts.lacunarity ?? 2\n const gain = opts.gain ?? 0.5\n\n let sum: ShaderNodeObject<Node> = noise(p)\n let amp = 1\n let freq = 1\n let total = amp\n\n for (let i = 1; i < octaves; i += 1) {\n freq *= lacunarity\n amp *= gain\n total += amp\n // Per-octave decorrelation: translate the sample point by a growing\n // offset so this octave reads from a totally different region of noise\n // space than the previous one. Magnitude 100 is well past simplex\n // noise's ~1-unit feature size, so adjacent octaves are fully\n // decorrelated. The scalar broadcasts across all components of `p`\n // (works for vec2 and vec3 inputs alike).\n //\n // Build the chain functionally from `p`: gotcha #12 doesn't apply\n // because `p` is uv-rooted, but the TSLNode union still requires\n // functional form on this hop.\n const pAtFreq = add(mul(p, freq), i * 100)\n const layer = noise(pAtFreq).mul(amp)\n\n sum = sum.add(layer)\n }\n\n // Normalize to approximate [-1..1] regardless of octave count / gain.\n return sum.div(total)\n}\n","// packages/matter/src/primitives/voronoi/voronoi.ts\nimport { mx_worley_noise_float } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport type { TSLNode } from '../color-ramp/color-ramp.js'\n\n/**\n * 2D voronoi (Worley) noise — distance to the nearest jittered cell point,\n * normalized roughly to [0, 1]. Higher values = farther from any cell point\n * (cell interiors); lower values = near a cell boundary.\n *\n * Built on three's `mx_worley_noise_float`. Combine with `colorRamp` for\n * a multi-color cellular pattern; threshold via `step`/`smoothstep` for\n * hard cell shapes.\n *\n * Returns `ShaderNodeObject<Node>` (chainable) for cast-free call sites.\n *\n * @param p — Vec2 TSL node, typically `uv() * scale`.\n */\nexport function voronoi(p: TSLNode): ShaderNodeObject<Node> {\n return mx_worley_noise_float(p)\n}\n","// packages/matter/src/primitives/quantize/quantize.ts\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\n/**\n * Quantize a scalar TSL node to `steps` discrete levels.\n *\n * quantize(t, 4) → values in {0, 0.25, 0.5, 0.75, 1.0}\n *\n * `steps` is a JS-side number (loop-equivalent at TSL build time, baked in).\n * If you need an animatable step count, rebuild the TSL fragment.\n */\nexport function quantize(t: ShaderNodeObject<Node>, steps: number): ShaderNodeObject<Node> {\n if (steps <= 1) {\n // Edge case: single step → constant 0. Return as-is wrapped in mul(0).\n return t.mul(0)\n }\n const denom = steps - 1\n\n // floor(t * (steps-1) + 0.5) / (steps-1)\n // Using floor(x + 0.5) instead of round() for TSL portability.\n return t.mul(denom).add(0.5).floor().div(denom)\n}\n","import { length } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport type { TSLNode } from '../color-ramp/color-ramp.js'\n\n/**\n * Signed distance field for a circle centered at the origin.\n *\n * sdfCircle(p, r) = length(p) - r\n *\n * Negative inside the circle, zero on the boundary, positive outside.\n * Combine with `smoothstep(-edge, +edge, sdf)` to render a soft-edged disk.\n *\n * @param p — Vec2 TSL node (typically a UV-space offset from the center).\n * @param radius — JS-side scalar OR a scalar TSL node.\n */\nexport function sdfCircle(p: TSLNode, radius: TSLNode | number): ShaderNodeObject<Node> {\n return length(p).sub(radius)\n}\n","import { add } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport type { TSLNode } from '../color-ramp/color-ramp.js'\n\n/**\n * Naive vector addition: returns `p + by`.\n *\n * displace(p, by) = p + by\n *\n * Thin wrapper that names the spatial intent of shifting a sample point.\n *\n * **SDF caveat:** when using this to translate an SDF render, pass the\n * NEGATED translation — `sdfCircle(displace(p, v.mul(-1)), r)` renders the\n * disk at position `+v` because SDF translation evaluates as\n * `length(p - center) - r`. Adding `+v` to the sample point shifts the\n * rendered shape in the OPPOSITE direction.\n *\n * @param p — Vec2 TSL node (the position being displaced).\n * @param by — Vec2 TSL node (the displacement vector).\n */\nexport function displace(p: TSLNode, by: TSLNode): ShaderNodeObject<Node> {\n return add(p, by)\n}\n","import { length, sin, smoothstep, sub } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport type { TSLNode } from '../color-ramp/color-ramp.js'\nimport { time } from '../time/time.js'\n\nexport interface CursorRippleOptions {\n /** Decay radius (UV space). Beyond this, the ripple is ~0. Default: 0.4. */\n reach?: number\n /** Wavelength controls the ripple spacing. Default: 30. Larger = wider rings. */\n frequency?: number\n /** Time multiplier on the wave phase. Default: 6. Larger = faster oscillation. */\n speed?: number\n /** Output amplitude. Default: 0.5. Final result is in roughly [-amplitude, +amplitude]. */\n amplitude?: number\n}\n\n/**\n * A radial ripple emanating from `center`. Returns a scalar TSL node in\n * roughly [-amplitude, +amplitude] that decays to ~0 outside `reach`.\n *\n * ripple = sin(d*frequency - time*speed) * amplitude * smoothstep(reach, 0, d)\n *\n * Compose into a wave field by adding it to the underlying base wave.\n *\n * Note: `frequency` / `speed` / `reach` / `amplitude` are JS-side numbers\n * (baked into the TSL fragment at material-build time). The animatable\n * cursor position is the only live uniform consumed.\n *\n * @param p — Vec2 TSL node (typically `uv()`).\n * @param center — Vec2 TSL node (cursor uniform, in UV space).\n */\nexport function cursorRipple(\n p: TSLNode,\n center: TSLNode,\n opts: CursorRippleOptions = {},\n): ShaderNodeObject<Node> {\n const reach = opts.reach ?? 0.4\n const frequency = opts.frequency ?? 30\n const speed = opts.speed ?? 6\n const amplitude = opts.amplitude ?? 0.5\n\n // d = length(p - center). Use functional `sub(p, center)` because both\n // are typed as the broad TSLNode union (no chain receiver). Per gotcha #12,\n // building from a raw `uniform()` receiver silently produces wrong GPU\n // values, so the functional form is also safer for `center` being a uniform.\n const d = length(sub(p, center))\n // `time` is the engine-gated TSL node (from primitives/time/time.ts);\n // chains rooted in `time` automatically respect `prefers-reduced-motion` and\n // the runtime override set via `setReducedMotionPolicy`.\n const wave = sin(d.mul(frequency).sub(time.mul(speed)))\n const decay = smoothstep(reach, 0, d)\n\n return wave.mul(amplitude).mul(decay)\n}\n","// Engine-gated `time` — equals the TSL built-in `time` multiplied by the\n// reduced-motion scale uniform. Components consuming `time` from `@lovo/matter`\n// automatically respect `prefers-reduced-motion` and the policy override set\n// via `setReducedMotionPolicy`.\n//\n// If you want raw uncapped time (e.g. for a debug overlay), import `time`\n// from `three/tsl` directly.\n\nimport { time as _builtinTime } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nimport { getReducedMotionTimeScale } from '../../runtime/reduced-motion/reduced-motion.js'\n\nexport const time: ShaderNodeObject<Node> = _builtinTime.mul(getReducedMotionTimeScale())\n","import { uniform } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\nexport type ReducedMotionPolicy = 'auto' | 'off' | 'slow' | 'paused'\n\n/**\n * Public surface exposed to package consumers. `recompute` is intentionally\n * absent — it is engine-internal and should not be callable from outside.\n */\nexport interface ReducedMotionWatcher {\n /** Current time scale: 0, 0.3, or 1. */\n scale(): number\n /** Subscribe to scale changes. Returns unsubscribe. */\n subscribe(cb: (scale: number) => void): () => void\n /** Tear down media-query listener. */\n dispose(): void\n}\n\n/**\n * Engine-internal extension of the public watcher. Only `setReducedMotionPolicy`\n * calls `recompute`; it is never part of the consumer-visible type.\n */\ninterface InternalWatcher extends ReducedMotionWatcher {\n recompute(): void\n}\n\ninterface PolicyState {\n policy: ReducedMotionPolicy\n watchers: Set<InternalWatcher>\n}\n\nconst state: PolicyState = {\n policy: 'auto',\n watchers: new Set(),\n}\n\n/**\n * Override Matter's default behavior of honoring `prefers-reduced-motion`.\n * - 'auto' — follow the OS media query (default)\n * - 'off' — full speed regardless of OS setting\n * - 'slow' — 30% speed regardless of OS setting\n * - 'paused' — 0 (animation effectively frozen) regardless of OS setting\n */\nexport function setReducedMotionPolicy(policy: ReducedMotionPolicy): void {\n if (state.policy === policy) return\n state.policy = policy\n for (const w of state.watchers) w.recompute()\n}\n\nexport function getReducedMotionPolicy(): ReducedMotionPolicy {\n return state.policy\n}\n\nconst computeScale = (mqlMatches: boolean): number => {\n switch (state.policy) {\n case 'off':\n return 1\n case 'slow':\n return 0.3\n case 'paused':\n return 0\n case 'auto':\n return mqlMatches ? 0.3 : 1\n }\n}\n\n/**\n * Create a watcher that tracks `prefers-reduced-motion: reduce` and the\n * global Matter policy override. Strict-mode-safe — callers create+dispose\n * one per mount cycle.\n */\nexport function createReducedMotionWatcher(): ReducedMotionWatcher {\n // SSR safety: bail to the no-op watcher if matchMedia is missing.\n // SSR watcher: scale() respects policy override but does not emit\n // subscription events (the engine has no way to notify SSR-created\n // watchers because they are not added to state.watchers — but in\n // practice CLAUDE.md gotcha #10 requires `ssr: false` for any component\n // that touches the matter engine).\n if (typeof matchMedia !== 'function') {\n return {\n scale: () => computeScale(false),\n subscribe: (cb) => {\n // No-op: SSR watchers are not in state.watchers and will never\n // receive policy-change notifications.\n void cb\n\n return () => {\n // SSR no-op unsubscribe\n }\n },\n /** SSR watcher does not emit policy-change notifications. */\n dispose: () => {\n // SSR no-op dispose\n },\n }\n }\n\n const mql = matchMedia('(prefers-reduced-motion: reduce)')\n const subs = new Set<(s: number) => void>()\n let last = computeScale(mql.matches)\n\n const onChange = () => {\n const next = computeScale(mql.matches)\n\n if (next !== last) {\n last = next\n for (const cb of subs) cb(next)\n }\n }\n\n mql.addEventListener('change', onChange)\n\n const watcher: InternalWatcher = {\n scale: () => last,\n subscribe(cb) {\n subs.add(cb)\n\n return () => subs.delete(cb)\n },\n recompute() {\n const next = computeScale(mql.matches)\n\n if (next !== last) {\n last = next\n for (const cb of subs) cb(next)\n }\n },\n dispose() {\n mql.removeEventListener('change', onChange)\n subs.clear()\n state.watchers.delete(watcher)\n },\n }\n\n state.watchers.add(watcher)\n\n return watcher\n}\n\nlet globalScaleUniform: ReturnType<typeof uniform<number>> | null = null\nlet globalWatcher: ReducedMotionWatcher | null = null\n\n/**\n * Returns the engine-shared TSL uniform that `time` is multiplied by. Lazily\n * initialized on first read; reused across all materials. Mutating `.value`\n * imperatively when policy changes is safe — TSL re-reads the uniform every\n * frame.\n */\nexport function getReducedMotionTimeScale(): ShaderNodeObject<Node> {\n if (globalScaleUniform === null) {\n globalWatcher = createReducedMotionWatcher()\n globalScaleUniform = uniform(globalWatcher.scale())\n globalWatcher.subscribe((s) => {\n if (globalScaleUniform === null) return\n globalScaleUniform.value = s\n })\n }\n\n // ShaderNodeObject<UniformNode<number>> isn't structurally assignable to\n // ShaderNodeObject<Node> (invariant generic methods); chains work the same\n // at runtime, so widen at the return boundary.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion\n return globalScaleUniform as unknown as ShaderNodeObject<Node>\n}\n\n// Keep a typed reference for tests that may want to re-init between tests.\nexport const __resetReducedMotionForTests = () => {\n globalWatcher?.dispose()\n globalWatcher = null\n globalScaleUniform = null\n}\n","import { fract, length, sin, vec2 } from 'three/tsl'\nimport type { ShaderNodeObject } from 'three/tsl'\nimport type { Node } from 'three/webgpu'\n\n/**\n * Hash-based film grain — chaotic, uncorrelated per-pixel noise sampled\n * from `uvNode`. The output is *centered* around zero so it acts as a\n * brightness-preserving texture overlay (half the pixels brighten by up\n * to `intensity`, half darken, mean unchanged). ADD the result to a color.\n *\n * filmGrain(uv, k) → static grain\n * filmGrain(uv, k, time) → twinkling grain. Pass a quantized time node\n * (e.g. `time.mul(speed).mul(60).floor()`) so\n * the grain re-randomizes at a controllable\n * \"shutter rate\" instead of every frame.\n *\n * Recipe:\n *\n * base = vec2(uv·c1, uv·c2) + timeOffset\n * hash = fract(sin(base) * 43758.5453)\n * out = (length(hash) - 0.765) * intensity\n *\n * `c1 = (2127.1, 81.17)` and `c2 = (1269.5, 283.37)` are arbitrary\n * near-prime constants that produce visually-uncorrelated noise. `0.765`\n * is the empirical mean of `length(vec2(u, v))` for uniform u, v ∈ [0, 1),\n * computed once so we don't have to subtract it at runtime per pixel.\n *\n * For a film-stock look (darkens as grain rises — silver-emulsion\n * physics) subtract the result from the color instead of adding.\n *\n * @param uvNode vec2 TSL node, typically `uv()`.\n * @param intensity number or TSL node in [0, 1]; scales the grain.\n * @param timeOffset optional number or TSL node added to each sample\n * before hashing. `0` (default) → static grain.\n */\nexport function filmGrain(\n uvNode: ShaderNodeObject<Node>,\n intensity: ShaderNodeObject<Node> | number,\n timeOffset: ShaderNodeObject<Node> | number = 0,\n): ShaderNodeObject<Node> {\n const HASH_C1 = vec2(2127.1, 81.17)\n const HASH_C2 = vec2(1269.5, 283.37)\n const base = vec2(uvNode.dot(HASH_C1).add(timeOffset), uvNode.dot(HASH_C2).add(timeOffset))\n\n const hash = fract(sin(base).mul(43758.5453))\n\n return length(hash).sub(0.765).mul(intensity)\n}\n","export interface VisibilityWatcher {\n isVisible(): boolean\n /** Subscribe to changes. Receives the new visibility state. Returns unsubscribe. */\n subscribe(cb: (visible: boolean) => void): () => void\n dispose(): void\n}\n\n/**\n * Watch `document.visibilityState`. Strict-mode-safe — callers create+dispose\n * one per mount cycle.\n *\n * SSR: if `document` is unavailable, returns a no-op watcher whose\n * `isVisible()` always returns `true` and whose `subscribe` does nothing.\n */\nexport function createVisibilityWatcher(): VisibilityWatcher {\n if (typeof document === 'undefined') {\n return {\n isVisible: () => true,\n subscribe: () => () => {\n // SSR no-op unsubscribe\n },\n dispose: () => {\n // SSR no-op dispose\n },\n }\n }\n\n const subs = new Set<(v: boolean) => void>()\n const onChange = () => {\n const v = document.visibilityState === 'visible'\n\n for (const cb of subs) cb(v)\n }\n\n document.addEventListener('visibilitychange', onChange)\n\n return {\n isVisible: () => document.visibilityState === 'visible',\n subscribe(cb) {\n subs.add(cb)\n\n return () => subs.delete(cb)\n },\n dispose() {\n document.removeEventListener('visibilitychange', onChange)\n subs.clear()\n },\n }\n}\n","export interface IntersectionWatcher {\n isInView(): boolean\n /** Subscribe to changes. Receives the new in-view state. Returns unsubscribe. */\n subscribe(cb: (inView: boolean) => void): () => void\n dispose(): void\n}\n\n/**\n * Watch a canvas's viewport intersection. Pauses tied to this watcher should\n * be resumed when the canvas is *any* fraction visible. Strict-mode-safe.\n *\n * SSR: if `IntersectionObserver` is unavailable, returns a no-op watcher whose\n * `isInView()` always returns `true` and whose `subscribe` does nothing.\n */\nexport function createIntersectionWatcher(canvas: HTMLCanvasElement): IntersectionWatcher {\n if (typeof IntersectionObserver === 'undefined') {\n return {\n isInView: () => true,\n subscribe: () => () => {\n // SSR no-op unsubscribe\n },\n dispose: () => {\n // SSR no-op dispose\n },\n }\n }\n\n const subs = new Set<(v: boolean) => void>()\n let inView = true\n const obs = new IntersectionObserver(\n (entries) => {\n const next = entries.some((e) => e.isIntersecting)\n\n if (next === inView) return\n inView = next\n for (const cb of subs) cb(inView)\n },\n { threshold: 0 },\n )\n\n obs.observe(canvas)\n\n return {\n isInView: () => inView,\n subscribe(cb) {\n subs.add(cb)\n\n return () => subs.delete(cb)\n },\n dispose() {\n obs.disconnect()\n subs.clear()\n },\n }\n}\n","export interface SchedulerTick {\n /** Seconds since the previous tick. 0 on the first call. */\n delta: number\n /** Total seconds since the scheduler started its current run. */\n elapsed: number\n /** The raw `performance.now()` timestamp the rAF callback received. */\n now: number\n}\n\nexport type SchedulerClient = (tick: SchedulerTick) => void\n\n/**\n * Batches `requestAnimationFrame` calls across all clients registered with\n * a single scheduler. One scheduler is created per <ShaderScene>; clients\n * are typically a Three.js renderer's render call.\n */\nexport class FrameScheduler {\n private readonly clients = new Set<SchedulerClient>()\n private rafId: number | null = null\n private running = false\n private paused = false\n private idle = false\n private flushPending = false\n private startedAt = 0\n private lastTickAt = 0\n\n /** Activate the scheduler. The rAF loop starts on the first client added. */\n start(): void {\n this.running = true\n this.paused = false\n this.maybeQueue()\n }\n\n /** Halt the rAF loop entirely. Use dispose() for permanent teardown. */\n stop(): void {\n this.running = false\n this.cancel()\n }\n\n /** Temporarily skip ticks without losing client registrations. */\n pause(): void {\n this.paused = true\n }\n\n /** Resume after pause(). */\n resume(): void {\n this.paused = false\n if (this.running) this.maybeQueue()\n }\n\n /** Register a client to be called every frame. */\n add(client: SchedulerClient): void {\n this.clients.add(client)\n if (this.running) this.maybeQueue()\n }\n\n /** Unregister a client. */\n remove(client: SchedulerClient): void {\n this.clients.delete(client)\n }\n\n /** Permanent teardown: stop the loop and drop all clients. */\n dispose(): void {\n this.stop()\n this.clients.clear()\n }\n\n /**\n * Mark the scheduler idle. The next tick still fires (a final flush so\n * uniform changes that triggered the idle state are rendered), then the\n * rAF loop halts. Use `requestRender()` or `setIdle(false)` to wake.\n */\n setIdle(idle: boolean): void {\n if (this.idle === idle) return\n this.idle = idle\n if (idle) {\n this.flushPending = true\n this.maybeQueue()\n } else {\n this.flushPending = false\n this.maybeQueue()\n }\n }\n\n /** Force a single tick while idle. Useful for prop-change invalidation. */\n requestRender(): void {\n if (!this.idle) return\n this.flushPending = true\n this.maybeQueue()\n }\n\n private maybeQueue(): void {\n if (this.rafId !== null) return\n if (!this.running) return\n if (this.clients.size === 0) return\n if (this.idle && !this.flushPending) return\n this.rafId = requestAnimationFrame(this.frame)\n }\n\n private cancel(): void {\n if (this.rafId !== null) {\n cancelAnimationFrame(this.rafId)\n this.rafId = null\n }\n }\n\n private readonly frame = (now: number): void => {\n this.rafId = null\n if (!this.running || this.paused) return\n\n if (this.startedAt === 0) {\n this.startedAt = now\n this.lastTickAt = now\n }\n const delta = (now - this.lastTickAt) / 1000\n const elapsed = (now - this.startedAt) / 1000\n\n this.lastTickAt = now\n\n const tick: SchedulerTick = { delta, elapsed, now }\n\n for (const client of this.clients) {\n client(tick)\n }\n\n this.flushPending = false\n this.maybeQueue()\n }\n}\n"],"mappings":";AAAA,SAAS,aAAa;AACtB,SAAS,sBAAsB;AAmC/B,eAAsB,eACpB,QACA,OAA8B,CAAC,GACT;AACtB,QAAM;AAAA,IACJ,YAAY;AAAA,IACZ,aAAa;AAAA,IACb,aAAa;AAAA,IACb,aAAa;AAAA,IACb,SAAS;AAAA,EACX,IAAI;AAEJ,QAAM,QAAQ,IAAI,eAAe;AAAA,IAC/B;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAED,QAAM,MAAM,KAAK;AAEjB,QAAM,cAAc,KAAK,IAAI,OAAO,kBAAkB,MAAM,CAAC;AAC7D,QAAM,qBAAqB,sBAAsB,QAAQ,aAAa,IAAI,MAAM,UAAU;AAE1F,QAAM,cAAc,oBAAoB,UAAU;AAElD,QAAM,SAAS,MAAM;AACnB,UAAM,IAAI,OAAO;AACjB,UAAM,IAAI,OAAO;AAEjB,QAAI,OAAO,UAAU,IAAI,MAAM,cAAc,KAAK,OAAO,WAAW,IAAI,MAAM,cAAc,GAAG;AAC7F,YAAM,QAAQ,GAAG,GAAG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