@threenative/three-native 0.0.0-stage → 0.3.5
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/README.md +24 -2
- package/package.json +42 -3
- package/src/addons/buffer-geometry-utils.ts +14 -0
- package/src/addons/data-utils.ts +63 -0
- package/src/addons/decal-geometry.ts +25 -0
- package/src/addons/decal.ts +218 -0
- package/src/addons/gltf-loader-web.ts +323 -0
- package/src/addons/hdr-loader.ts +71 -0
- package/src/addons/hdr.ts +139 -0
- package/src/addons/lut-cube-loader-web.ts +17 -0
- package/src/addons/lut-cube-loader.ts +149 -0
- package/src/addons/merge-geometries.ts +374 -0
- package/src/addons/mesh-bvh-webgpu.ts +13 -0
- package/src/addons/mesh-bvh.ts +50 -0
- package/src/addons/post-effects-web.ts +22 -0
- package/src/addons/post-effects.ts +173 -0
- package/src/audio.ts +564 -0
- package/src/blend-mode.ts +33 -0
- package/src/browser-backend.ts +2156 -0
- package/src/browser-entry.ts +305 -0
- package/src/browser-math.ts +448 -0
- package/src/browser-renderer.ts +527 -0
- package/src/browser-tsl.ts +650 -0
- package/src/catalog.ts +497 -0
- package/src/fixture-format.ts +721 -0
- package/src/fixture-native.ts +426 -0
- package/src/fixture-protocol.ts +315 -0
- package/src/math-utils.ts +65 -0
- package/src/object-surface.ts +250 -0
- package/src/pass-node.ts +286 -0
- package/src/property-binding.ts +93 -0
- package/src/quad-mesh.ts +54 -0
- package/src/reflector.ts +89 -0
- package/src/refused.ts +27 -0
- package/src/render-target.ts +107 -0
- package/src/rtt.ts +158 -0
- package/src/shadow-map.ts +21 -0
- package/src/storage.ts +206 -0
- package/src/texture-sources.ts +252 -0
- package/src/tsl-extras.ts +51 -0
- package/src/tsl-struct.ts +20 -0
- package/src/tsl-uniforms.ts +234 -0
- package/src/update-ranges.ts +109 -0
- package/src/wgsl-fn.ts +55 -0
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
import { type IEngineRef, TSL_NODE, engineRef } from "./browser-backend.js";
|
|
2
|
+
import { markNormalTarget } from "./pass-node.js";
|
|
3
|
+
import { defineQuadMesh } from "./quad-mesh.js";
|
|
4
|
+
import { type IRttClasses, defineRtt } from "./rtt.js";
|
|
5
|
+
import { defineStorage } from "./storage.js";
|
|
6
|
+
import { defineTslExtras } from "./tsl-extras.js";
|
|
7
|
+
import { defineStruct } from "./tsl-struct.js";
|
|
8
|
+
import { type ConstClasses, liveUniforms, uniformLanes, withConstValues } from "./tsl-uniforms.js";
|
|
9
|
+
import { defineWgslFn } from "./wgsl-fn.js";
|
|
10
|
+
/**
|
|
11
|
+
* TSL on the Wasm engine (PRD-540): three's `three/tsl` authoring functions and node methods as JS
|
|
12
|
+
* wrappers over node ids, built by the engine's one TSL name table (`tn_tsl_call`, shared with the
|
|
13
|
+
* V8 back end). A node is an engine graph node; nothing here builds shader code in JavaScript.
|
|
14
|
+
*
|
|
15
|
+
* The statement forms (Fn, If, Else, Loop, toVar, assign) run the game's callbacks here, between
|
|
16
|
+
* the engine's `scope:open` and `scope:close`, so their statements land in the engine's open body
|
|
17
|
+
* (tn::abi::TslScopes, shared with the V8 back end).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** One argument as `tn_tsl_call` takes it. */
|
|
21
|
+
export type TslArgValue =
|
|
22
|
+
| { readonly kind: "node"; readonly node: number }
|
|
23
|
+
| { readonly kind: "number"; readonly number: number }
|
|
24
|
+
| { readonly kind: "string" | "named"; readonly text: string }
|
|
25
|
+
| { readonly kind: "rgb" | "vector"; readonly numbers: readonly number[] }
|
|
26
|
+
| { readonly kind: "other" }
|
|
27
|
+
| { readonly kind: "handle"; readonly ref: IEngineRef };
|
|
28
|
+
|
|
29
|
+
/** The engine side of TSL: one call by name, node release, and a material's node slot. */
|
|
30
|
+
export interface ITslRuntime {
|
|
31
|
+
call(name: string, receiver: number | null, args: readonly TslArgValue[]): number;
|
|
32
|
+
release(node: number): void;
|
|
33
|
+
set(material: IEngineRef, path: string, node: number): void;
|
|
34
|
+
/** three's `uniform.value = x`: the uniform's lanes, with no program change. */
|
|
35
|
+
setUniform(node: number, lanes: readonly number[]): void;
|
|
36
|
+
/** A live post effect's scalar uniform, written first when `value` is given; answers its value. */
|
|
37
|
+
effectParameter(node: number, name: string, value?: number): number;
|
|
38
|
+
/** three's RenderPipeline: the post graph the web host draws between scene and output, or none. */
|
|
39
|
+
setPost(node: number | null): void;
|
|
40
|
+
/** three's `renderer.compute(node)`: queues the kernel; it runs ahead of the next draw or read. */
|
|
41
|
+
compute(node: number): void;
|
|
42
|
+
/** `renderer.getArrayBufferAsync(attribute)`: the storage buffer's bytes after every queued kernel. */
|
|
43
|
+
readStorage(attribute: IEngineRef): Promise<ArrayBuffer>;
|
|
44
|
+
/** `renderer.deleteAttribute(attribute)`: drops the attribute's GPU copy. */
|
|
45
|
+
deleteStorage(attribute: IEngineRef): void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Module functions the shared table answers (engine/abi/tsl_call.cpp). */
|
|
49
|
+
const FUNCTIONS = [
|
|
50
|
+
"float",
|
|
51
|
+
"int",
|
|
52
|
+
"uint",
|
|
53
|
+
"atomicLoad",
|
|
54
|
+
"vec2",
|
|
55
|
+
"vec3",
|
|
56
|
+
"vec4",
|
|
57
|
+
"uniform",
|
|
58
|
+
"attribute",
|
|
59
|
+
"uv",
|
|
60
|
+
"texture",
|
|
61
|
+
"texture3D",
|
|
62
|
+
"add",
|
|
63
|
+
"sub",
|
|
64
|
+
"mul",
|
|
65
|
+
"div",
|
|
66
|
+
"negate",
|
|
67
|
+
"lessThan",
|
|
68
|
+
"greaterThan",
|
|
69
|
+
"equal",
|
|
70
|
+
"notEqual",
|
|
71
|
+
"lessThanEqual",
|
|
72
|
+
"greaterThanEqual",
|
|
73
|
+
"and",
|
|
74
|
+
"or",
|
|
75
|
+
"not",
|
|
76
|
+
"all",
|
|
77
|
+
"any",
|
|
78
|
+
"abs",
|
|
79
|
+
"sin",
|
|
80
|
+
"cos",
|
|
81
|
+
"floor",
|
|
82
|
+
"fract",
|
|
83
|
+
"sqrt",
|
|
84
|
+
"exp",
|
|
85
|
+
"exp2",
|
|
86
|
+
"log2",
|
|
87
|
+
"log",
|
|
88
|
+
"normalize",
|
|
89
|
+
"length",
|
|
90
|
+
"atan",
|
|
91
|
+
"mod",
|
|
92
|
+
"fwidth",
|
|
93
|
+
"saturation",
|
|
94
|
+
"mat2",
|
|
95
|
+
"hash",
|
|
96
|
+
"min",
|
|
97
|
+
"max",
|
|
98
|
+
"pow",
|
|
99
|
+
"step",
|
|
100
|
+
"dot",
|
|
101
|
+
"distance",
|
|
102
|
+
"cross",
|
|
103
|
+
"mix",
|
|
104
|
+
"clamp",
|
|
105
|
+
"smoothstep",
|
|
106
|
+
"select",
|
|
107
|
+
"nodeObject",
|
|
108
|
+
"color",
|
|
109
|
+
"ivec2",
|
|
110
|
+
"textureLoad",
|
|
111
|
+
"reflect",
|
|
112
|
+
"dFdx",
|
|
113
|
+
"dFdy",
|
|
114
|
+
"lengthSq",
|
|
115
|
+
"viewportSharedTexture",
|
|
116
|
+
"viewportDepthTexture",
|
|
117
|
+
"linearDepth",
|
|
118
|
+
"convertToTexture",
|
|
119
|
+
"rtt",
|
|
120
|
+
"varying",
|
|
121
|
+
// The live post effects (lane-531's table entries); post-effects.ts publishes them as three's addons.
|
|
122
|
+
"ao",
|
|
123
|
+
"denoise",
|
|
124
|
+
"smaa",
|
|
125
|
+
"bloom",
|
|
126
|
+
"traa",
|
|
127
|
+
"film",
|
|
128
|
+
"lut3D",
|
|
129
|
+
"renderOutput",
|
|
130
|
+
"struct",
|
|
131
|
+
"wgslFunction",
|
|
132
|
+
"lightShadowMatrix",
|
|
133
|
+
"luminance",
|
|
134
|
+
"viewZToOrthographicDepth",
|
|
135
|
+
"viewZToPerspectiveDepth",
|
|
136
|
+
"logarithmicDepthToViewZ",
|
|
137
|
+
"oneMinus",
|
|
138
|
+
"mx_noise_float",
|
|
139
|
+
"mx_worley_noise_vec2",
|
|
140
|
+
"pmremTexture",
|
|
141
|
+
"reflector",
|
|
142
|
+
"transformDirection",
|
|
143
|
+
"transformNormalToView",
|
|
144
|
+
"property",
|
|
145
|
+
] as const;
|
|
146
|
+
/** The inputs TSL exports as values (tn::abi::tslConstants), each built once, when first read. */
|
|
147
|
+
const CONSTANTS = [
|
|
148
|
+
"viewportLinearDepth",
|
|
149
|
+
"frameGroup",
|
|
150
|
+
"renderGroup",
|
|
151
|
+
"objectGroup",
|
|
152
|
+
"cameraPosition",
|
|
153
|
+
"cameraNear",
|
|
154
|
+
"cameraFar",
|
|
155
|
+
"screenSize",
|
|
156
|
+
"depth",
|
|
157
|
+
"cameraProjectionMatrix",
|
|
158
|
+
"cameraWorldMatrix",
|
|
159
|
+
"positionGeometry",
|
|
160
|
+
"normalWorld",
|
|
161
|
+
"positionLocal",
|
|
162
|
+
"positionWorld",
|
|
163
|
+
"normalViewGeometry",
|
|
164
|
+
"cameraViewMatrix",
|
|
165
|
+
"instanceIndex",
|
|
166
|
+
"vertexIndex",
|
|
167
|
+
"screenUV",
|
|
168
|
+
"materialColor",
|
|
169
|
+
"materialEmissive",
|
|
170
|
+
"materialMetalness",
|
|
171
|
+
"materialRoughness",
|
|
172
|
+
"time",
|
|
173
|
+
"deltaTime",
|
|
174
|
+
"normalView",
|
|
175
|
+
"positionViewDirection",
|
|
176
|
+
"screenCoordinate",
|
|
177
|
+
"normalGeometry",
|
|
178
|
+
"tangentGeometry",
|
|
179
|
+
"normalWorldGeometry",
|
|
180
|
+
] as const;
|
|
181
|
+
/** Node methods the shared table answers, with the receiver passed apart. */
|
|
182
|
+
const METHODS = [
|
|
183
|
+
"compute",
|
|
184
|
+
"add",
|
|
185
|
+
"sub",
|
|
186
|
+
"mul",
|
|
187
|
+
"div",
|
|
188
|
+
"negate",
|
|
189
|
+
"lessThan",
|
|
190
|
+
"greaterThan",
|
|
191
|
+
"equal",
|
|
192
|
+
"notEqual",
|
|
193
|
+
"lessThanEqual",
|
|
194
|
+
"greaterThanEqual",
|
|
195
|
+
"and",
|
|
196
|
+
"or",
|
|
197
|
+
"not",
|
|
198
|
+
"all",
|
|
199
|
+
"any",
|
|
200
|
+
"toFloat",
|
|
201
|
+
"toInt",
|
|
202
|
+
"toUint",
|
|
203
|
+
"abs",
|
|
204
|
+
"sin",
|
|
205
|
+
"cos",
|
|
206
|
+
"floor",
|
|
207
|
+
"fract",
|
|
208
|
+
"sqrt",
|
|
209
|
+
"exp",
|
|
210
|
+
"exp2",
|
|
211
|
+
"log2",
|
|
212
|
+
"log",
|
|
213
|
+
"normalize",
|
|
214
|
+
"length",
|
|
215
|
+
"dFdx",
|
|
216
|
+
"dFdy",
|
|
217
|
+
"sign",
|
|
218
|
+
"cbrt",
|
|
219
|
+
"atan",
|
|
220
|
+
"mod",
|
|
221
|
+
"fwidth",
|
|
222
|
+
"min",
|
|
223
|
+
"max",
|
|
224
|
+
"pow",
|
|
225
|
+
"step",
|
|
226
|
+
"dot",
|
|
227
|
+
"distance",
|
|
228
|
+
"cross",
|
|
229
|
+
"reflect",
|
|
230
|
+
"lengthSq",
|
|
231
|
+
"mix",
|
|
232
|
+
"clamp",
|
|
233
|
+
"smoothstep",
|
|
234
|
+
"select",
|
|
235
|
+
"sample",
|
|
236
|
+
"__rtt",
|
|
237
|
+
"oneMinus",
|
|
238
|
+
"dispose",
|
|
239
|
+
"newStruct",
|
|
240
|
+
"invoke",
|
|
241
|
+
"renderOutput",
|
|
242
|
+
"get",
|
|
243
|
+
"flipX",
|
|
244
|
+
"flipY",
|
|
245
|
+
"flipZ",
|
|
246
|
+
"flipW",
|
|
247
|
+
"level",
|
|
248
|
+
"transformDirection",
|
|
249
|
+
] as const;
|
|
250
|
+
/** three's swizzles: every 1-4 lane combination of xyzw, rgba or stpq, as xyzw lanes. */
|
|
251
|
+
const SWIZZLES: Readonly<Record<string, string>> = (() => {
|
|
252
|
+
const out: Record<string, string> = {};
|
|
253
|
+
for (const set of ["xyzw", "rgba", "stpq"]) {
|
|
254
|
+
const grow = (alias: string, lanes: string): void => {
|
|
255
|
+
if (alias.length > 0) out[alias] = lanes;
|
|
256
|
+
if (alias.length < 4) for (let i = 0; i < 4; i++) grow(alias + set[i], lanes + "xyzw"[i]);
|
|
257
|
+
};
|
|
258
|
+
grow("", "");
|
|
259
|
+
}
|
|
260
|
+
return out;
|
|
261
|
+
})();
|
|
262
|
+
interface ITslNode {
|
|
263
|
+
readonly [TSL_NODE]: number;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function isTslNode(value: unknown): value is ITslNode {
|
|
267
|
+
return typeof value === "object" && value !== null && TSL_NODE in value;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Arguments that cross as the engine object itself: pmremTexture prefilters its texture and texture3D
|
|
272
|
+
* samples it (not a map the material names), and reflector takes its target and virtual camera.
|
|
273
|
+
*/
|
|
274
|
+
function takesEngineObject(name: string, index: number): boolean {
|
|
275
|
+
return (
|
|
276
|
+
(index === 0 &&
|
|
277
|
+
(name === "pmremTexture" || name === "texture3D" || name === "lightShadowMatrix")) ||
|
|
278
|
+
(index < 2 && (name === "reflector" || name === "__rtt"))
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The engine class `storage()` builds attributes from. */
|
|
283
|
+
interface IBufferAttributeClass {
|
|
284
|
+
// quality-allow: three's class name, so a back end passes its class map.
|
|
285
|
+
// biome-ignore lint/style/useNamingConvention: three's class name, so a back end passes its class map.
|
|
286
|
+
readonly BufferAttribute: never;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Defines the TSL exports over `runtime`. `sync` pushes edited Color/VectorN uniform values; the
|
|
291
|
+
* renderer calls it before each frame. With `classes`, constants answer r185's ConstNode.value.
|
|
292
|
+
*/
|
|
293
|
+
export function defineTsl(
|
|
294
|
+
runtime: ITslRuntime,
|
|
295
|
+
classes?: ConstClasses,
|
|
296
|
+
): {
|
|
297
|
+
exports: Record<string, unknown>;
|
|
298
|
+
sync(frame?: { camera?: unknown; scene?: unknown }): void;
|
|
299
|
+
} {
|
|
300
|
+
const released = new FinalizationRegistry<number>((node) => runtime.release(node));
|
|
301
|
+
const prototype: Record<string, unknown> = {};
|
|
302
|
+
const wrap = (node: number): ITslNode => {
|
|
303
|
+
const object = Object.create(prototype) as ITslNode;
|
|
304
|
+
Object.defineProperty(object, TSL_NODE, { value: node, configurable: true });
|
|
305
|
+
released.register(object, node, object);
|
|
306
|
+
return object;
|
|
307
|
+
};
|
|
308
|
+
const argument = (name: string, index: number, value: unknown): TslArgValue => {
|
|
309
|
+
if (isTslNode(value)) return { kind: "node", node: value[TSL_NODE] };
|
|
310
|
+
// An omitted optional input (denoise's normal node) has no TSL meaning of its own, and ao's and
|
|
311
|
+
// denoise's camera is the render camera, whose matrices the engine's effect reads each frame.
|
|
312
|
+
if (value === null || value === undefined) return { kind: "other" };
|
|
313
|
+
if (
|
|
314
|
+
(name === "traa" && index === 3) ||
|
|
315
|
+
(name === "ao" && index === 2) ||
|
|
316
|
+
(name === "denoise" && index === 3)
|
|
317
|
+
)
|
|
318
|
+
return { kind: "other" };
|
|
319
|
+
if (typeof value === "number") return { kind: "number", number: value };
|
|
320
|
+
if (typeof value === "string") return { kind: "string", text: value };
|
|
321
|
+
// texture(engineTexture, uv) samples that texture itself; a plain object names a material map,
|
|
322
|
+
// and textureLoad also takes a texture node.
|
|
323
|
+
if (
|
|
324
|
+
index === 0 &&
|
|
325
|
+
name === "texture" &&
|
|
326
|
+
typeof value === "object" &&
|
|
327
|
+
engineRef(value) !== undefined
|
|
328
|
+
)
|
|
329
|
+
return { kind: "handle", ref: engineRef(value) as IEngineRef };
|
|
330
|
+
if (index === 0 && (name === "texture" || name === "textureLoad") && typeof value === "object")
|
|
331
|
+
return { kind: "named", text: String((value as { name?: unknown } | null)?.name ?? "") };
|
|
332
|
+
// A three value is the engine's (`mesh.position`) or JS's own (`new Vector3()`).
|
|
333
|
+
if (typeof value === "object" && value !== null) {
|
|
334
|
+
const object = value as Record<string, unknown>;
|
|
335
|
+
if (takesEngineObject(name, index) && engineRef(value) !== undefined)
|
|
336
|
+
return { kind: "handle", ref: engineRef(value) as IEngineRef };
|
|
337
|
+
// TSL's nodeObject turns a three Color or VectorN into its constant: vec3(new Vector3(1, 2, 3)).
|
|
338
|
+
if (object.isColor === true) return { kind: "rgb", numbers: uniformLanes(object) };
|
|
339
|
+
if (object.isVector2 === true || object.isVector3 === true || object.isVector4 === true)
|
|
340
|
+
return { kind: "vector", numbers: uniformLanes(object) };
|
|
341
|
+
}
|
|
342
|
+
throw new TypeError(
|
|
343
|
+
`TN_TSL ${name}: argument ${index} is not a TSL node, number, string or three value`,
|
|
344
|
+
);
|
|
345
|
+
};
|
|
346
|
+
const call = (name: string, receiver: number | null, args: readonly unknown[]): number =>
|
|
347
|
+
runtime.call(
|
|
348
|
+
name,
|
|
349
|
+
receiver,
|
|
350
|
+
args.map((value, index) => argument(name, index, value)),
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
for (const name of METHODS)
|
|
354
|
+
prototype[name] = function (this: ITslNode, ...args: unknown[]) {
|
|
355
|
+
const made = wrap(call(name, this[TSL_NODE], args));
|
|
356
|
+
// three's ComputeNode flag, which a game (GPUParticles3D) checks before queueing the kernel.
|
|
357
|
+
if (name === "compute") Object.defineProperty(made, "isComputeNode", { value: true });
|
|
358
|
+
return made;
|
|
359
|
+
};
|
|
360
|
+
for (const [alias, lanes] of Object.entries(SWIZZLES))
|
|
361
|
+
Object.defineProperty(prototype, alias, {
|
|
362
|
+
get(this: ITslNode) {
|
|
363
|
+
return wrap(call(`swizzle:${lanes}`, this[TSL_NODE], []));
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
// The statement forms: a callback runs between scope:open and scope:close, which closes even when
|
|
367
|
+
// the callback throws; its statements land in the engine's open body.
|
|
368
|
+
let depth = 0;
|
|
369
|
+
const node = (id: number): TslArgValue => ({ kind: "node", node: id });
|
|
370
|
+
const capture = (form: string, callback: unknown, input?: unknown): number => {
|
|
371
|
+
if (typeof callback !== "function") throw new TypeError(`TN_TSL ${form}: expected a callback`);
|
|
372
|
+
runtime.release(runtime.call("scope:open", null, []));
|
|
373
|
+
depth++;
|
|
374
|
+
let returned: TslArgValue[] = [];
|
|
375
|
+
try {
|
|
376
|
+
const result: unknown = callback(input);
|
|
377
|
+
if (result !== undefined) returned = [argument("scope:close", 0, result)];
|
|
378
|
+
} catch (error) {
|
|
379
|
+
depth--;
|
|
380
|
+
runtime.release(runtime.call("scope:close", null, []));
|
|
381
|
+
throw error;
|
|
382
|
+
}
|
|
383
|
+
depth--;
|
|
384
|
+
return runtime.call("scope:close", null, returned);
|
|
385
|
+
};
|
|
386
|
+
// A statement's body crosses into the statement's own node, so its handle is released after the call.
|
|
387
|
+
const consume = (body: number, made: number): number => {
|
|
388
|
+
runtime.release(body);
|
|
389
|
+
return made;
|
|
390
|
+
};
|
|
391
|
+
const statement = (form: string): void => {
|
|
392
|
+
if (depth === 0) throw new TypeError(`TN_TSL ${form}: statement outside Fn`);
|
|
393
|
+
};
|
|
394
|
+
prototype.toVar = function (this: ITslNode) {
|
|
395
|
+
// Outside a function body there is no stack to declare a variable in (rain's fragment node names its
|
|
396
|
+
// terms this way); the expression itself is the value, read where it is used.
|
|
397
|
+
if (depth === 0) return this;
|
|
398
|
+
return wrap(runtime.call("toVar", this[TSL_NODE], []));
|
|
399
|
+
};
|
|
400
|
+
prototype.assign = function (this: ITslNode, value: unknown) {
|
|
401
|
+
runtime.release(call("assign", this[TSL_NODE], [value]));
|
|
402
|
+
return this;
|
|
403
|
+
};
|
|
404
|
+
// r185's `<op>Assign`: the shared scopes assign op(this, value) to the variable.
|
|
405
|
+
for (const form of ["addAssign", "subAssign", "mulAssign", "divAssign"]) {
|
|
406
|
+
prototype[form] = function (this: ITslNode, value: unknown) {
|
|
407
|
+
runtime.release(call(form, this[TSL_NODE], [value]));
|
|
408
|
+
return this;
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
// `setGroup` answers the node it was called on, so the uniform keeps the live `.value` it was built with.
|
|
412
|
+
prototype.setGroup = function (this: ITslNode, group: unknown) {
|
|
413
|
+
runtime.release(call("setGroup", this[TSL_NODE], [group]));
|
|
414
|
+
return this;
|
|
415
|
+
};
|
|
416
|
+
prototype.Else = function (this: ITslNode, callback: unknown) {
|
|
417
|
+
statement("Else");
|
|
418
|
+
const body = capture("Else", callback);
|
|
419
|
+
return wrap(consume(body, runtime.call("Else", this[TSL_NODE], [node(body)])));
|
|
420
|
+
};
|
|
421
|
+
prototype.ElseIf = function (this: ITslNode, condition: unknown, callback: unknown) {
|
|
422
|
+
statement("ElseIf");
|
|
423
|
+
const test = argument("ElseIf", 0, condition);
|
|
424
|
+
const body = capture("ElseIf", callback);
|
|
425
|
+
return wrap(consume(body, runtime.call("ElseIf", this[TSL_NODE], [test, node(body)])));
|
|
426
|
+
};
|
|
427
|
+
prototype.__effect = function (this: ITslNode, name: string, value?: number) {
|
|
428
|
+
return runtime.effectParameter(this[TSL_NODE], name, value);
|
|
429
|
+
};
|
|
430
|
+
// A uniform under the name a material binds: the same wrapper now names the renamed node, so its
|
|
431
|
+
// live `.value` (tsl-uniforms.ts) writes the node the game goes on to use.
|
|
432
|
+
prototype.setName = function (this: ITslNode, label: unknown) {
|
|
433
|
+
const previous = this[TSL_NODE];
|
|
434
|
+
const renamed = call("setName", previous, [label]);
|
|
435
|
+
Object.defineProperty(this, TSL_NODE, { value: renamed, configurable: true });
|
|
436
|
+
released.unregister(this);
|
|
437
|
+
released.register(this, renamed, this);
|
|
438
|
+
runtime.release(previous);
|
|
439
|
+
return this;
|
|
440
|
+
};
|
|
441
|
+
const exports: Record<string, unknown> = {};
|
|
442
|
+
for (const name of FUNCTIONS)
|
|
443
|
+
exports[name] = (...args: unknown[]) => wrap(call(name, null, args));
|
|
444
|
+
exports.rtt = defineRtt(
|
|
445
|
+
exports.rtt as Parameters<typeof defineRtt>[0],
|
|
446
|
+
classes && "Mesh" in classes
|
|
447
|
+
? ({
|
|
448
|
+
// quality-allow: the back end's class map is untyped; rtt narrows it to the classes it names.
|
|
449
|
+
...(classes as unknown as IRttClasses),
|
|
450
|
+
// quality-allow: three's class name, so a back end passes its class map.
|
|
451
|
+
// biome-ignore lint/style/useNamingConvention: three's class name, so a back end passes its class map.
|
|
452
|
+
QuadMesh: defineQuadMesh(classes as never),
|
|
453
|
+
} as IRttClasses)
|
|
454
|
+
: undefined,
|
|
455
|
+
);
|
|
456
|
+
exports.wgslFn = defineWgslFn(exports.wgslFunction as Parameters<typeof defineWgslFn>[0]);
|
|
457
|
+
exports.struct = defineStruct(exports.struct as Parameters<typeof defineStruct>[0]);
|
|
458
|
+
// Each call binds its own inputs (positional, an array, nodes or one named object) before the callback
|
|
459
|
+
// builds the graph; setLayout only chains, since the engine inlines.
|
|
460
|
+
exports.Fn = (callback: unknown) => {
|
|
461
|
+
if (typeof callback !== "function") throw new TypeError("TN_TSL Fn: expected a callback");
|
|
462
|
+
const inline = (...args: unknown[]) => {
|
|
463
|
+
const first = args[0];
|
|
464
|
+
const inputs =
|
|
465
|
+
args.length === 1 &&
|
|
466
|
+
first !== null &&
|
|
467
|
+
typeof first === "object" &&
|
|
468
|
+
!isTslNode(first) &&
|
|
469
|
+
(Array.isArray(first) || first.constructor === Object)
|
|
470
|
+
? first
|
|
471
|
+
: args;
|
|
472
|
+
const asNode = (value: unknown) =>
|
|
473
|
+
isTslNode(value) ? value : wrap(call("nodeObject", null, [value]));
|
|
474
|
+
const parameters = Array.isArray(inputs)
|
|
475
|
+
? inputs.map(asNode)
|
|
476
|
+
: Object.fromEntries(Object.entries(inputs).map(([key, value]) => [key, asNode(value)]));
|
|
477
|
+
return wrap(capture("Fn", callback, parameters));
|
|
478
|
+
};
|
|
479
|
+
return Object.assign(inline, { setLayout: () => inline });
|
|
480
|
+
};
|
|
481
|
+
for (const name of ["atomicAdd", "atomicStore"])
|
|
482
|
+
exports[name] = (...args: unknown[]) => {
|
|
483
|
+
statement(name);
|
|
484
|
+
return wrap(call(name, null, args));
|
|
485
|
+
};
|
|
486
|
+
// r185's Break(), Continue() and Return(): a loop or kernel ends early (statements, like atomicAdd).
|
|
487
|
+
for (const name of ["Break", "Continue", "Return"])
|
|
488
|
+
exports[name] = (...args: unknown[]) => {
|
|
489
|
+
statement(name);
|
|
490
|
+
return wrap(call(name, null, args));
|
|
491
|
+
};
|
|
492
|
+
exports.If = (condition: unknown, callback: unknown) => {
|
|
493
|
+
statement("If");
|
|
494
|
+
const test = argument("If", 0, condition);
|
|
495
|
+
const body = capture("If", callback);
|
|
496
|
+
return wrap(consume(body, runtime.call("If", null, [test, node(body)])));
|
|
497
|
+
};
|
|
498
|
+
// Loop(count, ({ i }) => ...) or three's Loop({ type, start, end, name }, ({ i }) => ...); several
|
|
499
|
+
// parameters nest one loop in the next, named i, j, k... unless a parameter names its own.
|
|
500
|
+
const loop = (
|
|
501
|
+
count: unknown,
|
|
502
|
+
defaultName: string,
|
|
503
|
+
callback: (indices: Record<string, unknown>) => unknown,
|
|
504
|
+
) => {
|
|
505
|
+
let end = count;
|
|
506
|
+
let type = "int";
|
|
507
|
+
let name = defaultName;
|
|
508
|
+
const extra: TslArgValue[] = [];
|
|
509
|
+
if (typeof count === "object" && count !== null && !isTslNode(count)) {
|
|
510
|
+
const params = count as Record<string, unknown>;
|
|
511
|
+
// The engine counts up while the index is below the end, by a whole step: "<=" is "<" one further.
|
|
512
|
+
if (params.condition !== undefined && params.condition !== "<" && params.condition !== "<=")
|
|
513
|
+
throw new TypeError(
|
|
514
|
+
'TN_TSL Loop: condition is not supported; the loop runs while the index is below ("<") or at most ("<=") the end',
|
|
515
|
+
);
|
|
516
|
+
if (params.update !== undefined && typeof params.update !== "number")
|
|
517
|
+
throw new TypeError("TN_TSL Loop: update is not supported; give a whole number step");
|
|
518
|
+
end = params.end;
|
|
519
|
+
if (params.condition === "<=")
|
|
520
|
+
end = isTslNode(end)
|
|
521
|
+
? // quality-allow: isTslNode proved a TSL node, whose operator methods its value type does not declare.
|
|
522
|
+
(end as unknown as { add(n: number): unknown }).add(1)
|
|
523
|
+
: Number(end) + 1;
|
|
524
|
+
if (params.type !== undefined) type = String(params.type);
|
|
525
|
+
if (params.name !== undefined) name = String(params.name);
|
|
526
|
+
if (params.start !== undefined || params.update !== undefined)
|
|
527
|
+
extra.push(argument("Loop", 3, params.start ?? 0));
|
|
528
|
+
if (params.update !== undefined) extra.push({ kind: "number", number: params.update });
|
|
529
|
+
}
|
|
530
|
+
const index = wrap(runtime.call("Loop:index", null, [{ kind: "string", text: type }]));
|
|
531
|
+
const body = capture("Loop", callback, { [name]: index });
|
|
532
|
+
return wrap(
|
|
533
|
+
consume(
|
|
534
|
+
body,
|
|
535
|
+
runtime.call("Loop", null, [
|
|
536
|
+
argument("Loop", 0, end),
|
|
537
|
+
node(index[TSL_NODE]),
|
|
538
|
+
node(body),
|
|
539
|
+
...extra,
|
|
540
|
+
]),
|
|
541
|
+
),
|
|
542
|
+
);
|
|
543
|
+
};
|
|
544
|
+
exports.Loop = (...parameters: unknown[]) => {
|
|
545
|
+
statement("Loop");
|
|
546
|
+
const callback = parameters.pop();
|
|
547
|
+
if (typeof callback !== "function" || parameters.length === 0)
|
|
548
|
+
throw new TypeError("TN_TSL Loop: expected a callback");
|
|
549
|
+
const level = (at: number, outer: Record<string, unknown>): unknown =>
|
|
550
|
+
loop(parameters[at], "ijklmnopqrstuvwxyz"[at] ?? "i", (own) => {
|
|
551
|
+
const indices = { ...outer, ...own };
|
|
552
|
+
if (at + 1 < parameters.length) {
|
|
553
|
+
level(at + 1, indices);
|
|
554
|
+
return undefined;
|
|
555
|
+
}
|
|
556
|
+
return callback(indices);
|
|
557
|
+
});
|
|
558
|
+
return level(0, {});
|
|
559
|
+
};
|
|
560
|
+
// three's storage(attribute, type, count) over an engine BufferAttribute (storage.ts): setName names the
|
|
561
|
+
// binding, element(index) reads or assigns one element of the attribute's own GPU buffer.
|
|
562
|
+
let storageSerial = 0;
|
|
563
|
+
const makeStorage = (attribute: unknown, type: unknown) => {
|
|
564
|
+
const ref =
|
|
565
|
+
typeof attribute === "object" && attribute !== null ? engineRef(attribute) : undefined;
|
|
566
|
+
if (ref === undefined || typeof type !== "string")
|
|
567
|
+
throw new TypeError("TN_TSL storage: expected an engine BufferAttribute and an element type");
|
|
568
|
+
let label = `nodeStorageBuffer${++storageSerial}`;
|
|
569
|
+
let atomic = false;
|
|
570
|
+
const elements = new Set<WeakRef<ITslNode>>();
|
|
571
|
+
const buffer = {
|
|
572
|
+
toAtomic() {
|
|
573
|
+
if (type !== "int" && type !== "uint")
|
|
574
|
+
throw new TypeError("TN_TSL toAtomic: expected int or uint elements");
|
|
575
|
+
atomic = true;
|
|
576
|
+
for (const element of elements) {
|
|
577
|
+
const target = element.deref();
|
|
578
|
+
if (target) runtime.release(call("storage:atomic", target[TSL_NODE], []));
|
|
579
|
+
}
|
|
580
|
+
elements.clear();
|
|
581
|
+
return buffer;
|
|
582
|
+
},
|
|
583
|
+
toReadOnly: () => buffer,
|
|
584
|
+
setName(name: unknown) {
|
|
585
|
+
label = String(name);
|
|
586
|
+
return buffer;
|
|
587
|
+
},
|
|
588
|
+
element(index: unknown) {
|
|
589
|
+
const handle: TslArgValue = { kind: "handle", ref };
|
|
590
|
+
const element = wrap(
|
|
591
|
+
runtime.call("storage:object", null, [
|
|
592
|
+
handle,
|
|
593
|
+
{ kind: "string", text: type },
|
|
594
|
+
{ kind: "string", text: label },
|
|
595
|
+
argument("storage:object", 3, index),
|
|
596
|
+
{ kind: "number", number: Number(atomic) },
|
|
597
|
+
]),
|
|
598
|
+
);
|
|
599
|
+
if (!atomic) elements.add(new WeakRef(element));
|
|
600
|
+
return element;
|
|
601
|
+
},
|
|
602
|
+
};
|
|
603
|
+
return buffer;
|
|
604
|
+
};
|
|
605
|
+
if (classes !== undefined && "BufferAttribute" in classes)
|
|
606
|
+
Object.assign(
|
|
607
|
+
exports,
|
|
608
|
+
defineStorage(
|
|
609
|
+
{
|
|
610
|
+
storage: makeStorage as never,
|
|
611
|
+
get instanceIndex() {
|
|
612
|
+
return exports.instanceIndex;
|
|
613
|
+
},
|
|
614
|
+
get vertexIndex() {
|
|
615
|
+
return exports.vertexIndex;
|
|
616
|
+
},
|
|
617
|
+
varying: (value) => (exports.varying as (value: unknown) => unknown)(value),
|
|
618
|
+
},
|
|
619
|
+
// quality-allow: the engine's BufferAttribute class arrives untyped from the back end's class map.
|
|
620
|
+
(classes as unknown as IBufferAttributeClass).BufferAttribute,
|
|
621
|
+
),
|
|
622
|
+
);
|
|
623
|
+
for (const name of CONSTANTS) {
|
|
624
|
+
let made: ITslNode | undefined;
|
|
625
|
+
Object.defineProperty(exports, name, {
|
|
626
|
+
enumerable: true,
|
|
627
|
+
get: () => {
|
|
628
|
+
made ??= wrap(call(`constant:${name}`, null, []));
|
|
629
|
+
return made;
|
|
630
|
+
},
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
const live = liveUniforms(
|
|
634
|
+
exports.uniform as (value: unknown, type?: unknown) => ITslNode,
|
|
635
|
+
(node, lanes) => runtime.setUniform(node[TSL_NODE], lanes),
|
|
636
|
+
);
|
|
637
|
+
exports.uniform = live.uniform;
|
|
638
|
+
exports.uniformArray = live.uniformArray;
|
|
639
|
+
exports.reference = live.reference;
|
|
640
|
+
if (classes !== undefined) withConstValues(exports, classes);
|
|
641
|
+
Object.assign(exports, defineTslExtras(exports as never));
|
|
642
|
+
// mrt()'s `normal: vec4(normalView, 0)` names the engine's normal target (pass-node.ts), as on the V8 player.
|
|
643
|
+
// Only the two-argument zero-alpha form can name it, so normalView is built then and not at install.
|
|
644
|
+
const vec4 = exports.vec4 as (...args: unknown[]) => object;
|
|
645
|
+
exports.vec4 = (...args: unknown[]) =>
|
|
646
|
+
args.length === 2 && args[1] === 0
|
|
647
|
+
? (markNormalTarget(vec4, exports.normalView) as typeof vec4)(...args)
|
|
648
|
+
: vec4(...args);
|
|
649
|
+
return { exports, sync: live.sync };
|
|
650
|
+
}
|