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