@react-three/tsl 10.0.0-canary.04087d8

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.
@@ -0,0 +1,847 @@
1
+ import { RootState, useStore } from '@react-three/fiber/extension';
2
+ import * as three_webgpu from 'three/webgpu';
3
+ import { Node, StorageTexture, Storage3DTexture, StorageArrayTexture, Data3DTexture, RenderPipeline, Vector2, Vector3, Vector4, Color, Matrix2, Matrix3, Matrix4 } from 'three/webgpu';
4
+
5
+ //* Buffer Types (useBuffers) ========================================
6
+
7
+ /**
8
+ * Buffer-like types for GPU compute and storage operations.
9
+ * Includes raw CPU arrays, Three.js buffer attributes, and TSL buffer nodes.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * const { positions, velocities } = useBuffers(() => ({
14
+ * positions: instancedArray(count, 'vec3'), // StorageBufferNode
15
+ * velocities: new Float32Array(count * 3), // TypedArray
16
+ * }), 'particles')
17
+ * ```
18
+ */
19
+ type BufferLike =
20
+ | Float32Array
21
+ | Uint32Array
22
+ | Int32Array
23
+ | Float64Array
24
+ | Uint8Array
25
+ | Int8Array
26
+ | Uint16Array
27
+ | Int16Array
28
+ | three_webgpu.BufferAttribute // Base class for all buffer attributes
29
+ | three_webgpu.InterleavedBufferAttribute
30
+ | Node // TSL buffer nodes (instancedArray, storage)
31
+
32
+ /** Flat record of buffer-like values (no nested scopes) */
33
+ type BufferRecord = Record<string, BufferLike>
34
+
35
+ /**
36
+ * Buffer store that can contain both root-level buffers and scoped buffer objects.
37
+ * Structure: { positions: Float32Array, particles: { vel: StorageBufferNode } }
38
+ */
39
+ type BufferStore = Record<string, BufferLike | BufferRecord>
40
+
41
+ //* Node Types (useNodes) ========================================
42
+
43
+ /**
44
+ * Every node representation `useNodes` accepts and `state.nodes` holds: three's real `Node`,
45
+ * the callable proxy `Fn()` returns, and the legacy structural shape (`uuid`/`nodeType`) older
46
+ * code passes through. Creators are constrained to this same type, so the shape a creator returns
47
+ * and the shape the store holds are one type by construction.
48
+ */
49
+ type NodeLike = TSLNodeType | LegacyTSLNodeLike
50
+
51
+ /** Flat record of TSL nodes (no nested scopes) */
52
+ type NodeRecord<T extends NodeLike = NodeLike> = Record<string, T>
53
+
54
+ /**
55
+ * Node store that can contain both root-level nodes and scoped node objects.
56
+ * Structure: { wobble: OperatorNode, fx: { blur: ShaderCallable } }
57
+ */
58
+ type NodeStore = Record<string, NodeLike | NodeRecord>
59
+
60
+ //* Storage Types (useGPUStorage) ========================================
61
+
62
+ /**
63
+ * GPU storage types for texture-based storage operations.
64
+ * Includes Three.js storage textures and TSL storage texture nodes.
65
+ *
66
+ * @example
67
+ * ```tsx
68
+ * const { heightMap } = useGPUStorage(() => ({
69
+ * heightMap: new StorageTexture(512, 512),
70
+ * }), 'terrain')
71
+ * ```
72
+ */
73
+ type StorageLike =
74
+ | StorageTexture // 2D GPU storage texture
75
+ | Storage3DTexture // 3D GPU storage texture (volumes, fluid grids)
76
+ | StorageArrayTexture // 2D-array GPU storage texture
77
+ | Data3DTexture // 3D texture (can be used as storage)
78
+ | Node // TSL storage texture nodes (storageTexture)
79
+
80
+ /** Flat record of storage-like values (no nested scopes) */
81
+ type StorageRecord = Record<string, StorageLike>
82
+
83
+ /**
84
+ * Storage store that can contain both root-level storage and scoped storage objects.
85
+ * Structure: { heightMap: StorageTexture, terrain: { normal: StorageTextureNode } }
86
+ */
87
+ type StorageStore = Record<string, StorageLike | StorageRecord>
88
+
89
+ /**
90
+ * The fields @react-three/tsl adds to RootState. Its root extension sets them up on every canvas;
91
+ * a secondary canvas holds its primary's map objects, and portals inherit them from their parent. So
92
+ * `state.uniforms` reads the same in useFrame, useThree, creators and handlers, on any canvas.
93
+ */
94
+ interface TSLRootState {
95
+ /**
96
+ * TSL uniform nodes - root-level uniforms + scoped sub-objects. Use useUniforms(). Typed by name
97
+ * once you register them (see `Register`), so `useFrame(({ uniforms }) => uniforms.uTime.value)`
98
+ * needs no cast.
99
+ */
100
+ uniforms: AppUniforms
101
+ /** TSL nodes - root-level nodes + scoped sub-objects. Use useNodes() */
102
+ nodes: NodeStore
103
+ /** Buffers - root-level buffers + scoped sub-objects. Use useBuffers() */
104
+ buffers: BufferStore
105
+ /** GPU storage (textures, etc.) - root-level storage + scoped sub-objects. Use useGPUStorage() */
106
+ gpuStorage: StorageStore
107
+ /** Internal: bumped by hot reloads and rebuilds to re-run creators */
108
+ _hmrVersion: number
109
+ /** The canvas's RenderPipeline, once useRenderPipeline created one. Per canvas, not shared. */
110
+ renderPipeline?: RenderPipeline | null
111
+ /** Pass nodes registered with useRenderPipeline */
112
+ passes?: PassRecord
113
+ }
114
+
115
+ // Every fiber entry shares one core, and so one RootState: augmenting it through the root entry
116
+ // reaches /legacy, /webgpu and /extension as well.
117
+ declare module '@react-three/fiber' {
118
+ interface RootState extends TSLRootState {}
119
+ }
120
+
121
+ // /webgpu exports WebGPURootState as RootState. Augmenting the base state it extends reaches both,
122
+ // and also the stores (useStore(), get(), primaryStore) typed with the base.
123
+ declare module '@react-three/fiber/webgpu' {
124
+ interface BaseRootState extends TSLRootState {}
125
+ }
126
+
127
+ declare module '@react-three/fiber/extension' {
128
+ interface RootState extends TSLRootState {}
129
+ }
130
+
131
+ //* Global TSL Types ==============================
132
+
133
+ declare global {
134
+ /**
135
+ * Broad callable-node fallback for dynamic store readers.
136
+ * Creator hooks preserve the exact function signatures inferred from Three's `Fn`.
137
+ */
138
+ type CallableTSLNode = ((...params: never[]) => unknown) & {
139
+ readonly shaderNode: unknown
140
+ readonly id: number
141
+ }
142
+
143
+ /** Legacy structural node accepted when at least one historical marker exists. */
144
+ type LegacyTSLNodeLike = {
145
+ label?: ((label: string) => unknown) | string
146
+ setName?: (name: string) => unknown
147
+ } & ({ uuid: string | undefined; nodeType?: string | null } | { uuid?: string; nodeType: string | null | undefined })
148
+
149
+ /** Node type accepted by broad dynamic node-store readers. */
150
+ type TSLNodeType = three_webgpu.Node | CallableTSLNode
151
+
152
+ /**
153
+ * What TSL's constant constructors return: `int(1)`, `vec2(0, 1)`, `color('red')` and friends
154
+ * wrap a ConstNode in a VarNode. three's `uniform()` unwraps exactly this shape to
155
+ * `UniformNode<TNodeType, TValue>`, and so do the mappings below.
156
+ */
157
+ type TSLConstNode<TNodeType, TValue> = three_webgpu.VarNode<
158
+ TNodeType,
159
+ three_webgpu.ConstNode<TNodeType, TValue>
160
+ >
161
+
162
+ /**
163
+ * Derive Three's shader node type from an existing node or a supported raw input.
164
+ * Existing InputNode generics take precedence over structural value normalization.
165
+ * Both generics must be inferred because Three's UniformNode intersects an unknown-typed
166
+ * InputNode base with its concrete InputNode specialization.
167
+ */
168
+ type UniformNodeType<T> = T extends three_webgpu.UniformNode<infer TNodeType, infer _TValue>
169
+ ? TNodeType
170
+ : T extends three_webgpu.InputNode<infer TNodeType, infer _TValue>
171
+ ? TNodeType
172
+ : T extends TSLConstNode<infer TNodeType, infer _TValue>
173
+ ? TNodeType
174
+ : T extends number
175
+ ? 'float'
176
+ : T extends boolean
177
+ ? 'bool'
178
+ : T extends string | three_webgpu.Color | { r: number; g: number; b: number }
179
+ ? 'color'
180
+ : T extends three_webgpu.Vector4 | { x: number; y: number; z: number; w: number }
181
+ ? 'vec4'
182
+ : T extends three_webgpu.Vector3 | { x: number; y: number; z: number }
183
+ ? 'vec3'
184
+ : T extends three_webgpu.Vector2 | { x: number; y: number }
185
+ ? 'vec2'
186
+ : T extends three_webgpu.Matrix4
187
+ ? 'mat4'
188
+ : T extends three_webgpu.Matrix3
189
+ ? 'mat3'
190
+ : T extends three_webgpu.Matrix2
191
+ ? 'mat2'
192
+ : unknown
193
+
194
+ /** Derive the normalized JavaScript value stored by a uniform node. */
195
+ type UniformNodeValue<T> = T extends three_webgpu.UniformNode<infer _TNodeType, infer TValue>
196
+ ? TValue
197
+ : T extends three_webgpu.InputNode<infer _TNodeType, infer TValue>
198
+ ? TValue
199
+ : T extends TSLConstNode<infer _TNodeType, infer TValue>
200
+ ? TValue
201
+ : T extends string | { r: number; g: number; b: number }
202
+ ? three_webgpu.Color
203
+ : T extends { x: number; y: number; z: number; w: number }
204
+ ? three_webgpu.Vector4
205
+ : T extends { x: number; y: number; z: number }
206
+ ? three_webgpu.Vector3
207
+ : T extends { x: number; y: number }
208
+ ? three_webgpu.Vector2
209
+ : T extends number
210
+ ? number
211
+ : T extends boolean
212
+ ? boolean
213
+ : T
214
+
215
+ /** Three's exact UniformNode with shader and value generics derived from an input. */
216
+ type UniformNodeFor<T> = three_webgpu.UniformNode<UniformNodeType<T>, UniformNodeValue<T>>
217
+
218
+ /** Preserve every input key while mapping values to exact Three uniform nodes. */
219
+ type UniformNodesFor<T extends UniformInputRecord> = {
220
+ [K in keyof T]: UniformNodeFor<T[K]>
221
+ }
222
+
223
+ /** Backward-compatible single-parameter uniform alias. */
224
+ type UniformNode<T = unknown> = UniformNodeFor<T>
225
+
226
+ /** Flat record of uniform nodes (no nested scopes) */
227
+ type UniformRecord<T extends UniformNode = UniformNode> = Record<string, T>
228
+
229
+ /**
230
+ * Uniform store that can contain both root-level uniforms and scoped uniform objects
231
+ * Used by state.uniforms which has structure like:
232
+ * { uTime: UniformNode, player: { uHealth: UniformNode }, enemy: { uHealth: UniformNode } }
233
+ */
234
+ type UniformStore = Record<string, UniformNode | UniformRecord>
235
+
236
+ /**
237
+ * Helper to safely access a uniform node from the store.
238
+ * Use this when accessing state.uniforms to get proper typing.
239
+ * @example
240
+ * const uTime = uniforms.uTime as UniformNode<number>
241
+ * const uColor = uniforms.uColor as UniformNode<import('three/webgpu').Color>
242
+ */
243
+ type GetUniform<T = unknown> = UniformNode<T>
244
+
245
+ /**
246
+ * Acceptable input values for useUniforms - raw values that get converted to UniformNodes
247
+ * Supports:
248
+ * - Primitives: number, string (color), boolean
249
+ * - Three.js types: Color, Vector2/3/4, Matrix2/3/4
250
+ * - Plain objects: { x, y, z, w } converted to vectors
251
+ * - TSL nodes: color(), vec3(), float() for type casting
252
+ * - UniformNode: existing uniforms (reused as-is)
253
+ */
254
+ type UniformValue =
255
+ | number
256
+ | string
257
+ | boolean
258
+ | three_webgpu.Color
259
+ | three_webgpu.Vector2
260
+ | three_webgpu.Vector3
261
+ | three_webgpu.Vector4
262
+ | three_webgpu.Matrix2
263
+ | three_webgpu.Matrix3
264
+ | three_webgpu.Matrix4
265
+ | { x: number; y: number }
266
+ | { x: number; y: number; z: number }
267
+ | { x: number; y: number; z: number; w: number }
268
+ | { r: number; g: number; b: number; a?: number } // Plain objects converted to Color
269
+ | three_webgpu.Node // TSL nodes like color(), vec3(), float() for type casting
270
+ | UniformNode
271
+
272
+ /** Input record for useUniforms - accepts raw values or UniformNodes */
273
+ type UniformInputRecord = Record<string, UniformValue>
274
+ }
275
+
276
+ declare global {
277
+ /**
278
+ * three's own render pipeline type. Named here so the rest of R3F refers to three's shape
279
+ * rather than `any`. three r183 renamed `PostProcessing` to `RenderPipeline`; our peer floor
280
+ * is r185, so the new name is the only one.
281
+ */
282
+ type ThreeRenderPipeline = three_webgpu.RenderPipeline
283
+
284
+ /**
285
+ * The pass `useRenderPipeline` creates for you: three's own `PassNode`, as returned by
286
+ * `pass(scene, camera)` from `three/tsl`. Referenced from three so members like
287
+ * `getTextureNode`, `setMRT` and `dispose` track the installed version.
288
+ */
289
+ type ScenePassNode = three_webgpu.PassNode
290
+
291
+ /**
292
+ * Pass record - stores TSL pass nodes for render pipeline.
293
+ *
294
+ * `scenePass` is the only key the library owns. It is optional here because this is also the
295
+ * shape of `state.passes`, which is `{}` before the pipeline exists and again after `reset()`
296
+ * or `clearPasses()`. Inside the callbacks it is always present; see
297
+ * {@link RenderPipelineCallbackState}.
298
+ *
299
+ * Every other key is user-registered, via a callback's return value. Those are TSL nodes of
300
+ * any kind, not only passes: texture reads of an MRT attachment, effect nodes, extra
301
+ * `pass()` instances. `Node` is the common base, so that is the bound. Narrow at the call
302
+ * site when you need a member, e.g. `passes.velocity as TextureNode`.
303
+ *
304
+ * The index signature admits `undefined` because a key may simply not be registered, and
305
+ * because the optional `scenePass` must be assignable to it: under `skipLibCheck: false` an
306
+ * optional property whose type excludes `undefined` from the index signature is an error
307
+ * (TS2411) for every consumer compiling these declarations.
308
+ */
309
+ interface PassRecord {
310
+ scenePass?: ScenePassNode
311
+ [key: string]: three_webgpu.Node | undefined
312
+ }
313
+
314
+ /**
315
+ * State passed to pipeline callbacks after the active pipeline has been created.
316
+ *
317
+ * `passes.scenePass` is required here: the hook installs the default scene pass before either
318
+ * callback runs, so callbacks can use it without a guard or a cast.
319
+ *
320
+ * `renderer` is narrowed to `WebGPURenderer` and `isLegacy` to `false`: the hook throws under
321
+ * the legacy renderer before either callback can run, so a callback never sees WebGL.
322
+ */
323
+ type RenderPipelineCallbackState = RootState & {
324
+ renderPipeline: ThreeRenderPipeline
325
+ passes: PassRecord & { scenePass: ScenePassNode }
326
+ renderer: three_webgpu.WebGPURenderer
327
+ isLegacy: false
328
+ }
329
+
330
+ /**
331
+ * What a callback may return to register entries into `state.passes`.
332
+ *
333
+ * `scenePass` is reserved. The hook owns that entry and its lifecycle: it caches the pass it
334
+ * created, and `rebuild()` / `reset()` dispose that cached pass. A callback overwriting the
335
+ * store entry would leave the store pointing at a node the hook never disposes, and the hook
336
+ * disposing a pass nothing references. So returning it is a type error.
337
+ */
338
+ type RegisteredPasses = Record<string, three_webgpu.Node> & { scenePass?: never }
339
+
340
+ /** Setup callback - runs first to configure MRT, create additional passes */
341
+ type RenderPipelineSetupCallback = (state: RenderPipelineCallbackState) => RegisteredPasses | void
342
+
343
+ /** Main callback - runs second to configure outputNode, create effect passes */
344
+ type RenderPipelineMainCallback = (state: RenderPipelineCallbackState) => RegisteredPasses | void
345
+
346
+ /** The imperative half of useRenderPipeline's return value, present in every state */
347
+ interface UseRenderPipelineActions {
348
+ /** Current passes from state */
349
+ passes: PassRecord
350
+ /** Clear all passes from state */
351
+ clearPasses: () => void
352
+ /** Reset RenderPipeline entirely (clears PP + passes) */
353
+ reset: () => void
354
+ /** Re-run setup/main callbacks with current closure values */
355
+ rebuild: () => void
356
+ }
357
+
358
+ /**
359
+ * Return type for useRenderPipeline hook, discriminated on `isReady`.
360
+ *
361
+ * `if (isReady)` narrows `renderPipeline` to non-null, which is what the docs already tell
362
+ * callers to check. `passes.scenePass` deliberately stays optional in the ready branch:
363
+ * `clearPasses()` empties the record while leaving the pipeline in place.
364
+ */
365
+ type UseRenderPipelineReturn = UseRenderPipelineActions &
366
+ (
367
+ | {
368
+ /** True when RenderPipeline is configured and ready */
369
+ isReady: true
370
+ /** RenderPipeline instance */
371
+ renderPipeline: ThreeRenderPipeline
372
+ }
373
+ | {
374
+ /** False until the pipeline has been created */
375
+ isReady: false
376
+ /** Not initialized yet, or torn down by reset() */
377
+ renderPipeline: null
378
+ }
379
+ )
380
+ }
381
+
382
+ /** Distinguishes a resource leaf from a nested resource scope. */
383
+ type ResourceLeafGuard<T> = (value: unknown) => value is T;
384
+
385
+ /**
386
+ * ScopedStore - Type-safe wrapper for nested stores (uniforms, nodes)
387
+ *
388
+ * Provides TypeScript-friendly access to uniform/node stores where the runtime
389
+ * structure is `Record<string, T | Record<string, T>>` (leaf nodes or nested scopes).
390
+ *
391
+ * The wrapper uses a Proxy to:
392
+ * 1. Return `T` for property access (type assumption: assumes leaf node)
393
+ * 2. Provide `.scope(key)` method for explicit nested access
394
+ * 3. Support iteration methods: has(), keys(), Object.keys(), for...in
395
+ *
396
+ * @example
397
+ * ```tsx
398
+ * // With uniforms registered (see `Register` and the Typed Uniforms guide), reads are typed by name
399
+ * useLocalNodes(({ uniforms }) => ({
400
+ * wobble: sin(uniforms.uTime.mul(2)),
401
+ * playerHealth: uniforms.scope('player').uHealth, // or uniforms.player.uHealth
402
+ * }), [])
403
+ *
404
+ * // Without registration, give a scope its schema
405
+ * useLocalNodes(({ uniforms }) => {
406
+ * const player = uniforms.scope<{ uHealth: UniformNode<'float', number> }>('player')
407
+ * return { damage: player.uHealth.mul(2) }
408
+ * }, [])
409
+ * ```
410
+ */
411
+
412
+ /**
413
+ * Type-safe wrapper interface for accessing nested store data.
414
+ * Property access returns `T` (assumes leaf node).
415
+ * Use `.scope(key)` for nested object access.
416
+ *
417
+ */
418
+ type ScopedStoreMethods<TLeaf> = {
419
+ /** Access a nested scope by key. Returns empty wrapper if scope doesn't exist. */
420
+ scope<TScope extends Record<string, TLeaf> = Record<string, TLeaf>>(key: string): ScopedStoreType<TLeaf, TScope>;
421
+ /** Check if a key exists in the store */
422
+ has(key: string): boolean;
423
+ /** Get all keys in the store */
424
+ keys(): string[];
425
+ };
426
+ type ScopedStoreType<TLeaf, TEntries extends Record<string, TLeaf> = Record<string, TLeaf>> = Readonly<TEntries> & ScopedStoreMethods<TLeaf>;
427
+ interface ScopedStoreData<TLeaf> {
428
+ [key: string]: TLeaf | ScopedStoreData<TLeaf>;
429
+ }
430
+ /**
431
+ * Create a type-safe ScopedStore wrapper around store data.
432
+ * @param data - The raw store data (uniforms or nodes from RootState)
433
+ * @returns A ScopedStoreType wrapper with type-safe access
434
+ */
435
+ declare function createScopedStore<TLeaf>(data: ScopedStoreData<TLeaf>, isLeaf: ResourceLeafGuard<TLeaf>): ScopedStoreType<TLeaf>;
436
+ /**
437
+ * State type passed to creator functions with ScopedStore wrappers.
438
+ * Provides type-safe access to uniforms, nodes, buffers, and gpuStorage without manual casting.
439
+ */
440
+ type CreatorState = Omit<RootState, 'uniforms' | 'nodes' | 'buffers' | 'gpuStorage'> & {
441
+ /** Type-safe uniform access - property access returns UniformNode, typed by name when registered */
442
+ uniforms: CreatorUniforms;
443
+ /** Type-safe node access for real, callable, and legacy structural nodes */
444
+ nodes: ScopedStoreType<NodeLike>;
445
+ /** Type-safe buffer access - property access returns BufferLike (TypedArrays, BufferAttributes, TSL nodes) */
446
+ buffers: ScopedStoreType<BufferLike>;
447
+ /** Type-safe GPU storage access - property access returns StorageLike (StorageTexture, TSL nodes) */
448
+ gpuStorage: ScopedStoreType<StorageLike>;
449
+ };
450
+
451
+ /**
452
+ * @fileoverview Typed uniforms by registration (the TanStack Router `Register` pattern).
453
+ *
454
+ * Nothing registered: every type below falls back to the loose shapes the hooks have always had.
455
+ * Registered once, anywhere in the app:
456
+ *
457
+ * ```ts
458
+ * export const globalUniforms = { uTime: 0, uColor: new Color('hotpink') }
459
+ * export const playerUniforms = { uHealth: 1 }
460
+ *
461
+ * declare module '@react-three/tsl' {
462
+ * interface Register {
463
+ * uniforms: typeof globalUniforms
464
+ * scopes: { player: typeof playerUniforms }
465
+ * // strict: false // unregistered root keys become loose instead of errors
466
+ * }
467
+ * }
468
+ *
469
+ * configureTSL({ uniforms: globalUniforms, scopes: { player: playerUniforms } })
470
+ * ```
471
+ *
472
+ * then `useUniforms().uTime`, `useUniforms('player').uHealth`, `useUniform('uTime')`,
473
+ * `useFrame(({ uniforms }) => uniforms.uTime)`, `useThree((s) => s.uniforms.uTime)` and a creator's
474
+ * `({ uniforms }) => uniforms.uTime` are typed by name, with no generics.
475
+ */
476
+
477
+ /**
478
+ * Augment this to type uniforms by name. Recognised keys:
479
+ * - `uniforms`: the root-level uniform inputs (e.g. `typeof globalUniforms`)
480
+ * - `scopes`: named scopes and their inputs (e.g. `{ player: typeof playerUniforms }`)
481
+ * - `strict`: `false` makes unregistered root keys loose instead of errors (default: strict)
482
+ */
483
+ interface Register {
484
+ }
485
+ /** The registered root-level uniform inputs, or `{}` when nothing is registered. */
486
+ type RegisteredUniforms = Register extends {
487
+ uniforms: infer U extends UniformInputRecord;
488
+ } ? U : {};
489
+ /** The registered scopes and their inputs, or `{}` when nothing is registered. */
490
+ type RegisteredScopes = Register extends {
491
+ scopes: infer S extends Record<string, UniformInputRecord>;
492
+ } ? S : {};
493
+ type IsRegistered = Register extends {
494
+ uniforms: any;
495
+ } | {
496
+ scopes: any;
497
+ } ? true : false;
498
+ type IsStrict = Register extends {
499
+ strict: false;
500
+ } ? false : true;
501
+ type TypedUniforms = UniformNodesFor<RegisteredUniforms> & {
502
+ [K in keyof RegisteredScopes]: UniformNodesFor<RegisteredScopes[K]>;
503
+ };
504
+ /**
505
+ * What `state.uniforms` and `useUniforms()` are: the registered shape (strict by default, so a typo
506
+ * is an error), or today's loose `UniformStore` when nothing is registered.
507
+ */
508
+ type AppUniforms = IsRegistered extends true ? IsStrict extends true ? TypedUniforms : TypedUniforms & UniformStore : UniformStore;
509
+ /** Creator input at root level: new keys are free, registered keys must keep their registered type. */
510
+ type RootUniformInput = Partial<RegisteredUniforms> & UniformInputRecord;
511
+ /** Creator input for scope `S`: checked against the registered scope, free for any other scope. */
512
+ type ScopeUniformInput<S extends string> = S extends keyof RegisteredScopes ? Partial<RegisteredScopes[S]> : unknown;
513
+ /** A registered scope's uniform nodes. */
514
+ type RegisteredScopeUniforms<S extends keyof RegisteredScopes> = UniformNodesFor<RegisteredScopes[S]>;
515
+ /** A registered root uniform's node. */
516
+ type RegisteredUniform<K extends keyof RegisteredUniforms> = UniformNodesFor<RegisteredUniforms>[K];
517
+ /**
518
+ * `uniforms` in a creator (`useNodes(({ uniforms }) => ...)`). Unregistered: any name is a
519
+ * `UniformNode`, as before. Registered: typed by name like `state.uniforms`, and `.scope(name)` is
520
+ * typed for registered scopes.
521
+ */
522
+ type CreatorUniforms = IsRegistered extends true ? {
523
+ scope<S extends keyof RegisteredScopes & string>(key: S): ScopedStoreType<UniformNode, RegisteredScopeUniforms<S>>;
524
+ } & Readonly<AppUniforms> & ScopedStoreMethods<UniformNode> : ScopedStoreType<UniformNode>;
525
+
526
+ /** Uniforms that exist on every primary canvas from the start. See configureTSL. */
527
+ interface TSLConfig {
528
+ /** Root-level uniform inputs, e.g. the object you registered as `Register['uniforms']` */
529
+ uniforms?: UniformInputRecord;
530
+ /** Scoped uniform inputs, e.g. the object you registered as `Register['scopes']` */
531
+ scopes?: Record<string, UniformInputRecord>;
532
+ }
533
+ /**
534
+ * Create uniforms on every primary canvas up front -- the runtime half of `Register`. Pass the same
535
+ * objects you registered, so the registered types are true before the first frame:
536
+ *
537
+ * ```ts
538
+ * configureTSL({ uniforms: globalUniforms, scopes: { player: playerUniforms } })
539
+ * ```
540
+ *
541
+ * They land on the primary canvas's RootState, so `state.uniforms` has them on the primary, its
542
+ * secondaries and every portal. Applies to canvases already mounted and to every canvas mounted
543
+ * later. Calling it again adds to the configuration; uniforms that already exist are never
544
+ * replaced. Optional: without it, a uniform exists once some component creates it.
545
+ */
546
+ declare function configureTSL(next: TSLConfig): void;
547
+
548
+ /** Creator function that returns uniform inputs (can be raw values or UniformNodes) */
549
+ type UniformCreator<T extends UniformInputRecord = UniformInputRecord> = (state: CreatorState) => T;
550
+ /** Function signature for removeUniforms util */
551
+ type RemoveUniformsFn = (names: string | string[], scope?: string) => void;
552
+ /** Function signature for clearUniforms util */
553
+ type ClearUniformsFn = (scope?: string) => void;
554
+ /** Function signature for rebuildUniforms util */
555
+ type RebuildUniformsFn = (scope?: string) => void;
556
+ /** Return type with utils included */
557
+ type UniformsWithUtils<T extends UniformRecord | UniformStore = UniformRecord> = T & {
558
+ removeUniforms: RemoveUniformsFn;
559
+ clearUniforms: ClearUniformsFn;
560
+ rebuildUniforms: RebuildUniformsFn;
561
+ };
562
+ declare function useUniforms<T extends RootUniformInput>(creator: UniformCreator<T>): UniformsWithUtils<UniformNodesFor<T>>;
563
+ declare function useUniforms<T extends UniformInputRecord, S extends string>(creator: (state: CreatorState) => T & ScopeUniformInput<S>, scope: S): UniformsWithUtils<UniformNodesFor<T>>;
564
+ declare function useUniforms<T extends RootUniformInput>(uniforms: T): UniformsWithUtils<UniformNodesFor<T>>;
565
+ declare function useUniforms<T extends UniformInputRecord, S extends string>(uniforms: T & ScopeUniformInput<S>, scope: S): UniformsWithUtils<UniformNodesFor<T>>;
566
+ declare function useUniforms(): UniformsWithUtils<AppUniforms>;
567
+ declare function useUniforms<S extends keyof RegisteredScopes & string>(scope: S): UniformsWithUtils<RegisteredScopeUniforms<S>>;
568
+ declare function useUniforms<T extends UniformInputRecord>(scope?: string): UniformsWithUtils<UniformNodesFor<T>>;
569
+ /**
570
+ * Global rebuildUniforms function for HMR integration.
571
+ * Invalidates cached uniforms and increments _hmrVersion to trigger re-creation.
572
+ * Call this when HMR is detected to refresh all uniform creators.
573
+ *
574
+ * Resolves to the primary store so shared TSL resources rebuild on the
575
+ * authoritative store.
576
+ *
577
+ * @param store - The R3F store (from useStore or context)
578
+ * @param scope - Optional scope to rebuild ('root' for root only, string for specific scope, undefined for all)
579
+ */
580
+ declare function rebuildAllUniforms(store: ReturnType<typeof useStore>, scope?: string): void;
581
+
582
+ /**
583
+ * Supported uniform value types:
584
+ * - Raw values: number, boolean, Vector2, Vector3, Vector4, Color, Matrix2, Matrix3, Matrix4
585
+ * - String colors: '#ff0000', 'red', 'rgb(255,0,0)' (auto-converted to Color)
586
+ * - TSL nodes: color(), vec3(), float(), etc. (for type casting)
587
+ * - UniformNode: existing uniforms (reused as-is)
588
+ */
589
+ type UniformValue = number | boolean | string | Vector2 | Vector3 | Vector4 | Color | Matrix2 | Matrix3 | Matrix4 | Node | UniformNode;
590
+ declare function useUniform<K extends keyof RegisteredUniforms & string>(name: K): RegisteredUniform<K>;
591
+ declare function useUniform<T extends UniformValue = UniformValue>(name: string): UniformNodeFor<T>;
592
+ declare function useUniform<T extends UniformValue>(name: string, value: T): UniformNodeFor<T>;
593
+
594
+ /**
595
+ * Every node representation a creator may return. The definition lives with the store types as
596
+ * `NodeLike`, so the shape creators return and the shape `state.nodes` holds are one type by
597
+ * construction: three's real `Node`, the callable proxy `Fn()` returns, or the legacy structural
598
+ * `{ uuid, nodeType }` shape.
599
+ */
600
+ type TSLNodeLike = NodeLike;
601
+
602
+ /** Backward-compatible alias covering every accepted node representation. */
603
+ type TSLNode = TSLNodeLike;
604
+ /**
605
+ * Creator function that returns a record of nodes.
606
+ * Exact creator return inference is preserved within the compatible node constraint.
607
+ */
608
+ type NodeCreator<T extends Record<string, TSLNodeLike>> = (state: CreatorState) => T;
609
+ /** Function signature for removeNodes util */
610
+ type RemoveNodesFn = (names: string | string[], scope?: string) => void;
611
+ /** Function signature for clearNodes util */
612
+ type ClearNodesFn = (scope?: string) => void;
613
+ /** Function signature for rebuildNodes util */
614
+ type RebuildNodesFn = (scope?: string) => void;
615
+ /** Return type with utils included */
616
+ type NodesWithUtils<T extends Record<string, unknown> = NodeRecord> = T & {
617
+ removeNodes: RemoveNodesFn;
618
+ clearNodes: ClearNodesFn;
619
+ rebuildNodes: RebuildNodesFn;
620
+ };
621
+ declare function useNodes(): NodesWithUtils<NodeStore>;
622
+ declare function useNodes(scope: string): NodesWithUtils<NodeRecord>;
623
+ declare function useNodes<T extends NodeRecord>(scope?: string): NodesWithUtils<T>;
624
+ declare function useNodes<T extends NodeRecord>(creator: NodeCreator<T>): NodesWithUtils<T>;
625
+ declare function useNodes<T extends NodeRecord>(creator: NodeCreator<T>, scope: string): NodesWithUtils<T>;
626
+ declare function useNodes(creatorOrScope?: NodeCreator<NodeRecord> | string, scope?: string): NodesWithUtils<Record<string, unknown>>;
627
+ /**
628
+ * Global rebuildNodes function for HMR integration.
629
+ * Invalidates cached nodes and increments _hmrVersion to trigger re-creation.
630
+ * Call this when HMR is detected to refresh all node creators.
631
+ *
632
+ * Resolves to the primary store so shared TSL resources rebuild on the
633
+ * authoritative store.
634
+ *
635
+ * @param store - The R3F store (from useStore or context)
636
+ * @param scope - Optional scope to rebuild ('root' for root only, string for specific scope, undefined for all)
637
+ */
638
+ declare function rebuildAllNodes(store: ReturnType<typeof useStore>, scope?: string): void;
639
+
640
+ /** Creator receives CreatorState with ScopedStore wrappers for type-safe access. Returns any record. */
641
+ type LocalNodeCreator<T extends Record<string, unknown>> = (state: CreatorState) => T;
642
+ /**
643
+ * The install step an install-form creator returns: it runs after commit (as a layout effect) and
644
+ * may return a cleanup, which runs before the next install and on unmount.
645
+ */
646
+ type LocalNodeInstall = () => void | (() => void);
647
+ /** A creator that builds during render and returns an install step instead of a record. */
648
+ type LocalNodeInstaller = (state: CreatorState) => LocalNodeInstall;
649
+ /**
650
+ * Creates component-local values from the rendering context and the shared TSL resources.
651
+ *
652
+ * Unlike `useNodes`, this does NOT register to the global store — nothing is published during
653
+ * render or commit. The creator runs in the render phase and is pure computation.
654
+ *
655
+ * **When the creator re-runs** is controlled by the optional `deps` array, mirroring `useMemo`:
656
+ *
657
+ * | Call | Ordinary component renders |
658
+ * | -------------------------------- | ----------------------------------------------------------- |
659
+ * | `useLocalNodes(creator)` | Re-evaluate every render, even with a `useCallback` creator |
660
+ * | `useLocalNodes(creator, [])` | Reuse the result |
661
+ * | `useLocalNodes(creator, [a, b])` | Reuse until a declared dependency changes by `Object.is` |
662
+ *
663
+ * Independently of `deps`, three things re-run the creator: a change to a shared resource the
664
+ * creator READ (replaced, removed, or appearing where it read nothing), a change of the owning
665
+ * (primary) store, and an HMR / `rebuild*` invalidation. Registrations the creator did not read
666
+ * change nothing, and writing `.value` on a uniform it read is not a change. `[]` therefore means
667
+ * "no JavaScript construction inputs", not "never rebuild". Whenever it re-runs, the creator from
668
+ * the CURRENT render is used; creator identity itself is never a rebuild trigger once an array is
669
+ * supplied.
670
+ *
671
+ * Only reads the creator makes before it returns are tracked. Inside `Fn(() => …)` the body runs
672
+ * later, while three builds the shader, so read the resource in the creator and close over it.
673
+ *
674
+ * `[]` is the normal case. A value that changes (a color prop, a slider) belongs in a uniform: the
675
+ * graph references the `UniformNode`, so updating its `.value` needs no rebuild and must not be a
676
+ * dependency. Declare only inputs that decide the graph's structure and cannot be uniforms: which
677
+ * node to use, a loop count, whether a branch exists.
678
+ *
679
+ * **Install form.** To put a node onto a Three object (`scene.fogNode`, `scene.backgroundNode`),
680
+ * return a function instead of a record. The creator still builds during render; the returned
681
+ * function runs after commit and may return a cleanup, which runs before the next install and on
682
+ * unmount. Mutating a Three object inside the creator itself is unsafe: React can discard a
683
+ * render, and StrictMode renders twice. The hook returns nothing in this form.
684
+ *
685
+ * ```tsx
686
+ * useLocalNodes(({ scene, uniforms }) => {
687
+ * const fogNode = fog(uniforms.fogColor, rangeFogFactor(uniforms.near, uniforms.far))
688
+ * return () => {
689
+ * scene.fogNode = fogNode
690
+ * return () => { scene.fogNode = null }
691
+ * }
692
+ * }, [])
693
+ * ```
694
+ *
695
+ * @example
696
+ * ```tsx
697
+ * // Resource-driven composition: no surrounding JS inputs. `uniforms.uTime` is typed when the
698
+ * // app registers its uniforms (see `Register`); otherwise give a scope its schema or cast.
699
+ * const { wobble, uTime } = useLocalNodes(({ uniforms, nodes }) => ({
700
+ * wobble: sin(uniforms.uTime.mul(2)),
701
+ * uTime: uniforms.uTime, // can return uniforms too
702
+ * }), [])
703
+ *
704
+ * // `pattern` picks which node the graph is built from: a structural input.
705
+ * const { result } = useLocalNodes(({ nodes }) => ({
706
+ * result: pattern === 'noise' ? nodes.noise : nodes.stripes,
707
+ * }), [pattern])
708
+ *
709
+ * // An unregistered uniform, cast to its type
710
+ * const { colorNode } = useLocalNodes(({ uniforms }) => {
711
+ * const uValue = uniforms.myUniform as UniformNode<'float', number>
712
+ * return { colorNode: mix(colorA, colorB, uValue) }
713
+ * }, [])
714
+ * ```
715
+ */
716
+ declare function useLocalNodes(creator: LocalNodeInstaller, deps?: React.DependencyList): void;
717
+ declare function useLocalNodes<T extends Record<string, unknown>>(creator: LocalNodeCreator<T>, deps?: React.DependencyList): T;
718
+
719
+ /**
720
+ * A record of buffer-like values - allows mixed types (TypedArrays, BufferAttributes, TSL nodes)
721
+ */
722
+ type BufferRecordType<T extends BufferLike = BufferLike> = Record<string, T>;
723
+ /**
724
+ * Creator function that returns a record of buffers.
725
+ * Receives CreatorState with access to existing buffers, gpuStorage, uniforms, nodes, etc.
726
+ */
727
+ type BufferCreator<T extends Record<string, BufferLike>> = (state: CreatorState) => T;
728
+ /** Function signature for removeBuffers util */
729
+ type RemoveBuffersFn = (names: string | string[], scope?: string) => void;
730
+ /** Function signature for clearBuffers util */
731
+ type ClearBuffersFn = (scope?: string) => void;
732
+ /** Function signature for rebuildBuffers util */
733
+ type RebuildBuffersFn = (scope?: string) => void;
734
+ /** Function signature for disposeBuffers util - releases GPU resources */
735
+ type DisposeBuffersFn = (names: string | string[], scope?: string) => void;
736
+ /** Return type with utils included */
737
+ type BuffersWithUtils<T extends BufferRecordType | BufferStore = BufferRecordType> = T & {
738
+ removeBuffers: RemoveBuffersFn;
739
+ clearBuffers: ClearBuffersFn;
740
+ rebuildBuffers: RebuildBuffersFn;
741
+ disposeBuffers: DisposeBuffersFn;
742
+ };
743
+ declare function useBuffers(): BuffersWithUtils<BufferStore>;
744
+ declare function useBuffers(scope: string): BuffersWithUtils<Record<string, BufferLike>>;
745
+ declare function useBuffers<T extends Record<string, BufferLike>>(scope?: string): BuffersWithUtils<T>;
746
+ declare function useBuffers<T extends Record<string, BufferLike>>(creator: BufferCreator<T>): BuffersWithUtils<T>;
747
+ declare function useBuffers<T extends Record<string, BufferLike>>(creator: BufferCreator<T>, scope: string): BuffersWithUtils<T>;
748
+ /**
749
+ * Global rebuildBuffers function for HMR integration.
750
+ * Invalidates cached buffers and increments _hmrVersion to trigger re-creation.
751
+ * Call this when HMR is detected to refresh all buffer creators.
752
+ *
753
+ * Resolves to the primary store so shared TSL resources rebuild on the
754
+ * authoritative store.
755
+ *
756
+ * @param store - The R3F store (from useStore or context)
757
+ * @param scope - Optional scope to rebuild ('root' for root only, string for specific scope, undefined for all)
758
+ */
759
+ declare function rebuildAllBuffers(store: ReturnType<typeof useStore>, scope?: string): void;
760
+
761
+ /**
762
+ * A record of storage-like values - allows mixed types (StorageTexture, Storage3DTexture, TSL nodes)
763
+ */
764
+ type StorageRecordType<T extends StorageLike = StorageLike> = Record<string, T>;
765
+ /**
766
+ * Creator function that returns a record of GPU storage objects.
767
+ * Receives CreatorState with access to existing buffers, gpuStorage, uniforms, nodes, etc.
768
+ */
769
+ type StorageCreator<T extends Record<string, StorageLike>> = (state: CreatorState) => T;
770
+ /** Function signature for removeStorage util */
771
+ type RemoveStorageFn = (names: string | string[], scope?: string) => void;
772
+ /** Function signature for clearStorage util */
773
+ type ClearStorageFn = (scope?: string) => void;
774
+ /** Function signature for rebuildStorage util */
775
+ type RebuildStorageFn = (scope?: string) => void;
776
+ /** Function signature for disposeStorage util - releases GPU resources */
777
+ type DisposeStorageFn = (names: string | string[], scope?: string) => void;
778
+ /** Return type with utils included */
779
+ type StorageWithUtils<T extends StorageRecordType | StorageStore = StorageRecordType> = T & {
780
+ removeStorage: RemoveStorageFn;
781
+ clearStorage: ClearStorageFn;
782
+ rebuildStorage: RebuildStorageFn;
783
+ disposeStorage: DisposeStorageFn;
784
+ };
785
+ declare function useGPUStorage(): StorageWithUtils<StorageStore>;
786
+ declare function useGPUStorage(scope: string): StorageWithUtils<Record<string, StorageLike>>;
787
+ declare function useGPUStorage<T extends Record<string, StorageLike>>(scope?: string): StorageWithUtils<T>;
788
+ declare function useGPUStorage<T extends Record<string, StorageLike>>(creator: StorageCreator<T>): StorageWithUtils<T>;
789
+ declare function useGPUStorage<T extends Record<string, StorageLike>>(creator: StorageCreator<T>, scope: string): StorageWithUtils<T>;
790
+ /**
791
+ * Global rebuildStorage function for HMR integration.
792
+ * Invalidates cached storage and increments _hmrVersion to trigger re-creation.
793
+ * Call this when HMR is detected to refresh all storage creators.
794
+ *
795
+ * Resolves to the primary store so shared TSL resources rebuild on the
796
+ * authoritative store.
797
+ *
798
+ * @param store - The R3F store (from useStore or context)
799
+ * @param scope - Optional scope to rebuild ('root' for root only, string for specific scope, undefined for all)
800
+ */
801
+ declare function rebuildAllStorage(store: ReturnType<typeof useStore>, scope?: string): void;
802
+
803
+ /**
804
+ * Hook for managing WebGPU RenderPipeline with automatic scenePass setup.
805
+ *
806
+ * Features:
807
+ * - Creates RenderPipeline instance if not exists
808
+ * - Creates default scenePass (no MRT) automatically
809
+ * - Callbacks receive full RootState for flexibility
810
+ * - No auto-cleanup on unmount - use reset() for explicit cleanup
811
+ * - Scene/camera changes trigger scenePass recreation
812
+ * - A replaced scenePass is disposed once the new graph is installed, so rebuild() does not
813
+ * strand its render target (and MRT attachments) on the GPU
814
+ * - Any run that changes the output node sets RenderPipeline.needsUpdate, so rebuild() actually
815
+ * recompiles instead of silently keeping the first compiled graph
816
+ *
817
+ * @param mainCB - Main callback to configure outputNode and create effect passes
818
+ * @param setupCB - Optional setup callback to configure MRT on scenePass
819
+ * @returns { passes, renderPipeline, clearPasses, reset, rebuild }
820
+ *
821
+ * @example
822
+ * ```tsx
823
+ * // Simple effect
824
+ * useRenderPipeline(({ renderPipeline, passes }) => {
825
+ * renderPipeline.outputNode = bloom(passes.scenePass.getTextureNode())
826
+ * })
827
+ *
828
+ * // With MRT setup
829
+ * useRenderPipeline(
830
+ * ({ renderPipeline, passes }) => {
831
+ * const beauty = passes.scenePass.getTextureNode().toInspector('Color')
832
+ * const vel = passes.scenePass.getTextureNode('velocity')
833
+ * renderPipeline.outputNode = motionBlur(beauty, vel)
834
+ * },
835
+ * ({ passes }) => {
836
+ * passes.scenePass.setMRT(mrt({ output, velocity }))
837
+ * }
838
+ * )
839
+ *
840
+ * // Read-only access
841
+ * const { renderPipeline, passes } = useRenderPipeline()
842
+ * ```
843
+ */
844
+ declare function useRenderPipeline(mainCB?: RenderPipelineMainCallback, setupCB?: RenderPipelineSetupCallback): UseRenderPipelineReturn;
845
+
846
+ export { configureTSL, createScopedStore, rebuildAllBuffers, rebuildAllNodes, rebuildAllStorage, rebuildAllUniforms, useBuffers, useGPUStorage, useLocalNodes, useNodes, useRenderPipeline, useUniform, useUniforms };
847
+ export type { AppUniforms, BufferCreator, BufferLike, BufferRecord, BufferStore, BuffersWithUtils, ClearBuffersFn, ClearNodesFn, ClearStorageFn, ClearUniformsFn, CreatorState, DisposeBuffersFn, DisposeStorageFn, LocalNodeCreator, LocalNodeInstall, LocalNodeInstaller, NodeCreator, NodeLike, NodeRecord, NodeStore, NodesWithUtils, RebuildBuffersFn, RebuildNodesFn, RebuildStorageFn, RebuildUniformsFn, Register, RegisteredScopeUniforms, RegisteredScopes, RegisteredUniform, RegisteredUniforms, RemoveBuffersFn, RemoveNodesFn, RemoveStorageFn, RemoveUniformsFn, RootUniformInput, ScopeUniformInput, ScopedStoreType, StorageCreator, StorageLike, StorageRecord, StorageStore, StorageWithUtils, TSLConfig, TSLNode, TSLNodeLike, TSLRootState, UniformCreator, UniformValue, UniformsWithUtils };