@threenative/three-native 0.3.11 → 0.3.13
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/package.json +2 -2
- package/src/addons/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +132 -26
- package/src/addons/screen-space.ts +61 -24
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +33 -12
- package/src/browser-abi-types.ts +621 -0
- package/src/browser-attribute-sync.ts +249 -0
- package/src/browser-backend.ts +266 -1650
- package/src/browser-entry.ts +79 -22
- package/src/browser-loaders.ts +12 -0
- package/src/browser-marshal.ts +79 -0
- package/src/browser-renderer.ts +297 -87
- package/src/browser-tsl.ts +146 -23
- package/src/browser-value-views.ts +634 -0
- package/src/browser-wasm-runtime.ts +525 -0
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +23 -6
- package/src/edges-geometry.ts +111 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/generated/web-frame-fields.ts +84 -0
- package/src/gpu-stages.ts +17 -16
- package/src/grid-helper.ts +49 -0
- package/src/object-surface.ts +47 -6
- package/src/pass-node.ts +354 -10
- package/src/render-target.ts +515 -16
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +80 -23
- package/src/tsl-extras.ts +50 -0
- package/src/tsl-uniforms.ts +294 -63
- package/src/update-ranges.ts +50 -1
package/src/pass-node.ts
CHANGED
|
@@ -25,10 +25,44 @@ interface IPassTarget {
|
|
|
25
25
|
readonly texture: unknown;
|
|
26
26
|
readonly width: number;
|
|
27
27
|
readonly height: number;
|
|
28
|
+
/** The pass's colour attachments, in layout order; an offscreen engine target keeps its own list. */
|
|
29
|
+
readonly textures?: readonly IPassAttachment[];
|
|
30
|
+
/** The frame's MSAA sample count, as the engine draws it. */
|
|
31
|
+
readonly samples?: number;
|
|
28
32
|
setSize(width: number, height: number): void;
|
|
29
33
|
dispose(): void;
|
|
30
34
|
}
|
|
31
35
|
|
|
36
|
+
/**
|
|
37
|
+
* One colour attachment of the frame pass, as a game reads it (three's Texture): its name, format, type
|
|
38
|
+
* and colour space. The engine owns the GPU texture, so this descriptor names the target the engine
|
|
39
|
+
* draws and the renderer reads back; it is not a sampled texture handle.
|
|
40
|
+
*/
|
|
41
|
+
export interface IPassAttachment {
|
|
42
|
+
name: string;
|
|
43
|
+
format: number;
|
|
44
|
+
type: number;
|
|
45
|
+
colorSpace: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The frame pass's render target: three's RenderTarget surface over the colour attachments the engine
|
|
50
|
+
* draws. `textures` is the MRT layout (its own colour first), `width`/`height` the size the engine draws
|
|
51
|
+
* at, and `samples` the frame's MSAA count; `dispose` dispatches three's event.
|
|
52
|
+
*/
|
|
53
|
+
export interface IFrameTarget {
|
|
54
|
+
readonly texture: IPassAttachment;
|
|
55
|
+
readonly textures: readonly IPassAttachment[];
|
|
56
|
+
readonly width: number;
|
|
57
|
+
readonly height: number;
|
|
58
|
+
readonly samples: number;
|
|
59
|
+
setSize(width: number, height: number): void;
|
|
60
|
+
dispose(): void;
|
|
61
|
+
addEventListener(type: string, listener: unknown): void;
|
|
62
|
+
removeEventListener(type: string, listener: unknown): void;
|
|
63
|
+
dispatchEvent(event: { type: string }): void;
|
|
64
|
+
}
|
|
65
|
+
|
|
32
66
|
/** What an offscreen pass needs of a back end: its RenderTarget, and the surface before a first frame. */
|
|
33
67
|
export interface IPassHost {
|
|
34
68
|
// quality-allow: three's class name, so a back end passes its class map.
|
|
@@ -39,6 +73,100 @@ export interface IPassHost {
|
|
|
39
73
|
options: { type: number },
|
|
40
74
|
) => IPassTarget;
|
|
41
75
|
surface?(): ISurface;
|
|
76
|
+
/** The frame's MSAA sample count: the renderer's, which the game may not have made yet. */
|
|
77
|
+
samples?(): number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const RGBA_FORMAT = 1023; // three's RGBAFormat
|
|
81
|
+
const NO_COLOR_SPACE = ""; // three's NoColorSpace
|
|
82
|
+
|
|
83
|
+
/** The colour attachment names of a frame pass, for the renderer's readback by attachment index. */
|
|
84
|
+
const ATTACHMENT_NAMES = Symbol.for("threenative.passAttachmentNames");
|
|
85
|
+
|
|
86
|
+
/** The name of a frame pass's colour attachment `index`, or undefined for any other target. */
|
|
87
|
+
export function frameAttachmentName(target: unknown, index: number): string | undefined {
|
|
88
|
+
if (typeof target !== "object" || target === null) return undefined;
|
|
89
|
+
return (target as { [ATTACHMENT_NAMES]?: readonly string[] })[ATTACHMENT_NAMES]?.[index];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The frame pass's colour attachments: three's RenderTarget surface over the targets the engine draws.
|
|
94
|
+
* The engine owns their GPU memory, so a resize and a dispose belong to it; this object answers what a
|
|
95
|
+
* game reads (the layout, the size, the sample count) and dispatches three's `dispose` event.
|
|
96
|
+
*/
|
|
97
|
+
class FrameTarget implements IFrameTarget {
|
|
98
|
+
readonly texture: IPassAttachment = {
|
|
99
|
+
name: "output",
|
|
100
|
+
format: RGBA_FORMAT,
|
|
101
|
+
type: HALF_FLOAT_TYPE,
|
|
102
|
+
colorSpace: NO_COLOR_SPACE,
|
|
103
|
+
};
|
|
104
|
+
#attachments: IPassAttachment[] = [this.texture];
|
|
105
|
+
readonly #events = new Map<string, Set<unknown>>();
|
|
106
|
+
readonly #host: IPassHost | undefined;
|
|
107
|
+
|
|
108
|
+
constructor(host: IPassHost | undefined) {
|
|
109
|
+
this.#host = host;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get textures(): readonly IPassAttachment[] {
|
|
113
|
+
return this.#attachments;
|
|
114
|
+
}
|
|
115
|
+
get width(): number {
|
|
116
|
+
return this.#host?.surface?.()?.width ?? 1;
|
|
117
|
+
}
|
|
118
|
+
get height(): number {
|
|
119
|
+
return this.#host?.surface?.()?.height ?? 1;
|
|
120
|
+
}
|
|
121
|
+
get samples(): number {
|
|
122
|
+
return this.#host?.samples?.() ?? 1;
|
|
123
|
+
}
|
|
124
|
+
// The readback names an attachment by its index in `textures`; a facade has no `indexOf` identity to publish.
|
|
125
|
+
get [ATTACHMENT_NAMES](): readonly string[] {
|
|
126
|
+
return this.#attachments.map((attachment) => attachment.name);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** three's RenderTarget.setSize: the engine resizes its own pass, so the layout size follows it. */
|
|
130
|
+
setSize(_width: number, _height: number): void {}
|
|
131
|
+
|
|
132
|
+
/** three's MRT layout: the pass's output first, then each further named target, in author order. */
|
|
133
|
+
setLayout(names: readonly string[]): void {
|
|
134
|
+
const kept = new Map(this.#attachments.map((attachment) => [attachment.name, attachment]));
|
|
135
|
+
this.#attachments = names.map((name) => kept.get(name) ?? attachment(name));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** three's PassNode.getTexture: a name the pass did not hold becomes a further colour attachment. */
|
|
139
|
+
named(name: string): IPassAttachment {
|
|
140
|
+
let found = this.#attachments.find((attachment) => attachment.name === name);
|
|
141
|
+
if (found === undefined) {
|
|
142
|
+
found = attachment(name);
|
|
143
|
+
this.#attachments.push(found);
|
|
144
|
+
}
|
|
145
|
+
return found;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
addEventListener(type: string, listener: unknown): void {
|
|
149
|
+
const listeners = this.#events.get(type) ?? new Set<unknown>();
|
|
150
|
+
listeners.add(listener);
|
|
151
|
+
this.#events.set(type, listeners);
|
|
152
|
+
}
|
|
153
|
+
removeEventListener(type: string, listener: unknown): void {
|
|
154
|
+
this.#events.get(type)?.delete(listener);
|
|
155
|
+
}
|
|
156
|
+
dispatchEvent(event: { type: string }): void {
|
|
157
|
+
for (const listener of this.#events.get(event.type) ?? []) {
|
|
158
|
+
if (typeof listener === "function") (listener as (event: unknown) => void)(event);
|
|
159
|
+
else (listener as { handleEvent(event: unknown): void }).handleEvent(event);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
dispose(): void {
|
|
164
|
+
this.dispatchEvent({ type: "dispose" });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function attachment(name: string): IPassAttachment {
|
|
169
|
+
return { name, format: RGBA_FORMAT, type: HALF_FLOAT_TYPE, colorSpace: NO_COLOR_SPACE };
|
|
42
170
|
}
|
|
43
171
|
|
|
44
172
|
/** Draws `scene` through `camera` into `target`, at the call. */
|
|
@@ -48,11 +176,15 @@ interface IPassTsl {
|
|
|
48
176
|
texture(source: unknown, uv: unknown): unknown;
|
|
49
177
|
uv(): unknown;
|
|
50
178
|
readonly normalView: unknown;
|
|
179
|
+
/** three's diffuseColor node, when the engine's TSL table has one. */
|
|
180
|
+
readonly diffuseColor?: unknown;
|
|
51
181
|
/** The TSL constructors the surface decode composes; a table that lacks them refuses by name. */
|
|
52
182
|
readonly floor?: (value: unknown) => unknown;
|
|
53
183
|
readonly vec4?: (...values: unknown[]) => unknown;
|
|
54
184
|
readonly cameraNear?: IPassMath;
|
|
55
185
|
readonly cameraFar?: IPassMath;
|
|
186
|
+
/** The engine's mrt(names, ...nodes): a material mrtNode of named outputs. */
|
|
187
|
+
readonly mrt?: (names: string, ...nodes: unknown[]) => object;
|
|
56
188
|
}
|
|
57
189
|
|
|
58
190
|
/** The node arithmetic getViewZNode composes. */
|
|
@@ -68,8 +200,54 @@ interface IMRTSlot {
|
|
|
68
200
|
readonly name: string;
|
|
69
201
|
}
|
|
70
202
|
|
|
203
|
+
/**
|
|
204
|
+
* The slot names the engine's scene pass already owns: its colour, depth, velocity and surface targets.
|
|
205
|
+
* A custom slot keeps its own name and cannot take one of these, because the pass answers that name
|
|
206
|
+
* with its own target (getTextureNode).
|
|
207
|
+
*/
|
|
208
|
+
const RESERVED_SLOTS: ReadonlySet<string> = new Set([
|
|
209
|
+
"output",
|
|
210
|
+
"depth",
|
|
211
|
+
"velocity",
|
|
212
|
+
"normal",
|
|
213
|
+
"metalness",
|
|
214
|
+
"roughness",
|
|
215
|
+
"diffuseColor",
|
|
216
|
+
"emissive",
|
|
217
|
+
]);
|
|
218
|
+
|
|
219
|
+
/** The pass's own `normalView` spelling of its normal target, which the scene pass draws itself. */
|
|
220
|
+
const passNormals = new WeakSet<object>();
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Whether a material's `mrtNode` writes this output. The engine's scene pass draws its own colour, depth, velocity,
|
|
224
|
+
* normal and surface targets every frame, so the pass's own spellings of them (a slot marker, `normal: normalView`)
|
|
225
|
+
* stay with the pass. Any other node, under any name, is the material's: a render target's texture of that name
|
|
226
|
+
* takes it (an impostor bake writes `output` and `normal`, PRD-858).
|
|
227
|
+
*/
|
|
228
|
+
export function isCustomMrtOutput(name: string, value: unknown): boolean {
|
|
229
|
+
if (value === null || typeof value !== "object") return false;
|
|
230
|
+
if ((value as { isMRTSlot?: boolean }).isMRTSlot === true) return false;
|
|
231
|
+
return !(name === "normal" && (passNormals.has(value) || "__tnNormalView" in value));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** WebGPU's guaranteed colour-attachment count, and what a device's limit reports here when it answers. */
|
|
235
|
+
function maxColorAttachments(): number {
|
|
236
|
+
const limit = (
|
|
237
|
+
globalThis as {
|
|
238
|
+
navigator?: { gpu?: { limits?: { maxColorAttachments?: unknown } } };
|
|
239
|
+
}
|
|
240
|
+
).navigator?.gpu?.limits?.maxColorAttachments;
|
|
241
|
+
return typeof limit === "number" && Number.isSafeInteger(limit) && limit > 0 ? limit : 8;
|
|
242
|
+
}
|
|
243
|
+
|
|
71
244
|
interface IMRTNode {
|
|
72
245
|
readonly isMRTNode: true;
|
|
246
|
+
/** Set on the material form, whose output nodes are a RenderTarget's attachments; the scene pass keeps its slots. */
|
|
247
|
+
readonly isMaterialMrt?: true;
|
|
248
|
+
/** three's MRTNode.outputNodes: the node each named output writes. */
|
|
249
|
+
readonly outputNodes: Readonly<Record<string, unknown>>;
|
|
250
|
+
/** The same record under the facade's earlier name, which the Wasm backend's setter reads. */
|
|
73
251
|
readonly outputs: Readonly<Record<string, unknown>>;
|
|
74
252
|
/**
|
|
75
253
|
* three's per-output blend state. The engine's scene pass keeps the material's own blending on
|
|
@@ -115,30 +293,122 @@ export function definePass(
|
|
|
115
293
|
const slot = (name: string): IMRTSlot => Object.freeze({ isMRTSlot: true, name });
|
|
116
294
|
const output = slot("output");
|
|
117
295
|
const normalView = tsl.normalView;
|
|
296
|
+
if (normalView !== null && typeof normalView === "object") passNormals.add(normalView);
|
|
118
297
|
const metalness = slot("metalness");
|
|
119
298
|
const roughness = slot("roughness");
|
|
120
|
-
|
|
299
|
+
// three's diffuseColor is a node a material reads (`diffuseColor.a`) and the pass's slot in mrt(); the engine's node
|
|
300
|
+
// carries the slot marker, so both spellings hold.
|
|
301
|
+
const engineDiffuse = tsl.diffuseColor;
|
|
302
|
+
const diffuseColor: IMRTSlot =
|
|
303
|
+
engineDiffuse !== null && typeof engineDiffuse === "object"
|
|
304
|
+
? (Object.defineProperties(engineDiffuse, {
|
|
305
|
+
isMRTSlot: { value: true },
|
|
306
|
+
name: { value: "diffuseColor" },
|
|
307
|
+
}) as IMRTSlot)
|
|
308
|
+
: slot("diffuseColor");
|
|
121
309
|
const emissive = slot("emissive");
|
|
122
310
|
|
|
311
|
+
/** A scene pass output: one of the slots, or `normal` as normalView (or vec4(normalView, 0)). */
|
|
312
|
+
const passOutput = (name: string, value: unknown): boolean =>
|
|
313
|
+
(value as IMRTSlot | null)?.isMRTSlot === true ||
|
|
314
|
+
(name === "normal" &&
|
|
315
|
+
(value === normalView ||
|
|
316
|
+
(value as { __tnNormalView?: unknown } | null)?.__tnNormalView === normalView));
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Outputs that are TSL nodes (not the scene pass's slots): a material's mrtNode, written into a RenderTarget made
|
|
320
|
+
* with `count`, each output to the texture of its name. `output` keeps the material's own colour there. The scene
|
|
321
|
+
* pass draws its own targets every frame, so a record carries them beside the material's nodes (three's templates
|
|
322
|
+
* spell `normal` as normalView there) and the material writes only the nodes: `mrtNode` skips the reserved names.
|
|
323
|
+
*/
|
|
324
|
+
function materialMrt(
|
|
325
|
+
outputs: Record<string, unknown>,
|
|
326
|
+
nodes: [string, unknown][],
|
|
327
|
+
engineMrt: NonNullable<IPassTsl["mrt"]>,
|
|
328
|
+
): IMRTNode {
|
|
329
|
+
// A pass slot object beside material nodes is a wiring error: only the pass draws that target. The normal target
|
|
330
|
+
// as normalView is not a slot object, so it stays the spelling a material's mrt() carries.
|
|
331
|
+
for (const [name, value] of Object.entries(outputs))
|
|
332
|
+
if (value !== output && (value as IMRTSlot | null)?.isMRTSlot === true)
|
|
333
|
+
throw new Error(
|
|
334
|
+
`TN_NATIVE_MRT_UNSUPPORTED: ${name} mixes a scene pass slot with material outputs`,
|
|
335
|
+
);
|
|
336
|
+
if (nodes.some(([name]) => name.includes(",")))
|
|
337
|
+
throw new Error("TN_NATIVE_MRT_UNSUPPORTED: an mrt() output name holds a comma");
|
|
338
|
+
const node = engineMrt(
|
|
339
|
+
nodes.map(([name]) => name).join(","),
|
|
340
|
+
...nodes.map(([, value]) => value),
|
|
341
|
+
);
|
|
342
|
+
const blendModes: Record<string, unknown> = {};
|
|
343
|
+
// Defined, not assigned: a node may be a proxy whose set trap means `uniform.value = x`.
|
|
344
|
+
return Object.defineProperties(node, {
|
|
345
|
+
isMRTNode: { value: true },
|
|
346
|
+
// A material's mrt(): its output nodes go to a RenderTarget's textures, never to the scene pass's targets.
|
|
347
|
+
isMaterialMrt: { value: true },
|
|
348
|
+
outputs: { value: Object.freeze({ ...outputs }) },
|
|
349
|
+
// The material's blending writes every attachment, as three's does when the renderer has no MRT.
|
|
350
|
+
blendModes: { value: blendModes },
|
|
351
|
+
setBlendMode: {
|
|
352
|
+
value: (name: string, blend: unknown): IMRTNode => {
|
|
353
|
+
blendModes[name] = blend;
|
|
354
|
+
return node as IMRTNode;
|
|
355
|
+
},
|
|
356
|
+
},
|
|
357
|
+
getBlendMode: { value: (name: string): unknown => blendModes[name] },
|
|
358
|
+
has: { value: (name: string): boolean => name in outputs },
|
|
359
|
+
}) as IMRTNode;
|
|
360
|
+
}
|
|
361
|
+
|
|
123
362
|
function mrt(outputs: Record<string, unknown>): IMRTNode {
|
|
363
|
+
const entries = Object.entries(outputs);
|
|
364
|
+
const limit = maxColorAttachments();
|
|
365
|
+
if (entries.length > limit)
|
|
366
|
+
throw new Error(
|
|
367
|
+
`TN_NATIVE_MRT_UNSUPPORTED: ${entries.length} colour attachments exceed the device limit ${limit}`,
|
|
368
|
+
);
|
|
124
369
|
const named: Record<string, unknown> = {};
|
|
125
|
-
|
|
370
|
+
const nodes = Object.entries(outputs).filter(([name, value]) => !passOutput(name, value));
|
|
371
|
+
if (nodes.length > 0 && tsl.mrt !== undefined) return materialMrt(outputs, nodes, tsl.mrt);
|
|
372
|
+
for (const [name, value] of entries) {
|
|
126
373
|
const normal =
|
|
127
374
|
name === "normal" &&
|
|
128
375
|
(value === normalView ||
|
|
129
376
|
(value as { __tnNormalView?: unknown } | null)?.__tnNormalView === normalView);
|
|
377
|
+
if (normal) {
|
|
378
|
+
named[name] = normalView;
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const engineSlot = value as IMRTSlot | null;
|
|
382
|
+
// A name the engine's pass owns keeps its exact spelling: its own slot, or the refusal it always gave.
|
|
383
|
+
if (RESERVED_SLOTS.has(name)) {
|
|
384
|
+
if (engineSlot?.isMRTSlot === true && engineSlot.name === name) {
|
|
385
|
+
named[name] = value;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
throw new Error(
|
|
389
|
+
`TN_NATIVE_MRT_UNSUPPORTED: ${name} must be one of output, normal: normalView or vec4(normalView, 0), metalness, roughness, diffuseColor, emissive`,
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
// An engine target under a custom name is a wiring error, not a node.
|
|
130
393
|
if (
|
|
131
|
-
|
|
132
|
-
(
|
|
394
|
+
engineSlot?.isMRTSlot === true ||
|
|
395
|
+
(value !== null && typeof value === "object" && "isMRTSlot" in value)
|
|
133
396
|
)
|
|
134
397
|
throw new Error(
|
|
135
398
|
`TN_NATIVE_MRT_UNSUPPORTED: ${name} must be one of output, normal: normalView or vec4(normalView, 0), metalness, roughness, diffuseColor, emissive`,
|
|
136
399
|
);
|
|
137
|
-
|
|
400
|
+
// Any other name is a custom colour attachment: three's mrt() takes any node there.
|
|
401
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
402
|
+
throw new Error(
|
|
403
|
+
`TN_NATIVE_MRT_UNSUPPORTED: the '${name}' slot must be a shader node, got ${value === null ? "null" : Array.isArray(value) ? "an array" : typeof value}`,
|
|
404
|
+
);
|
|
405
|
+
named[name] = value;
|
|
138
406
|
}
|
|
407
|
+
const frozen = Object.freeze(named);
|
|
139
408
|
const node: IMRTNode = Object.freeze({
|
|
140
409
|
isMRTNode: true as const,
|
|
141
|
-
|
|
410
|
+
outputNodes: frozen,
|
|
411
|
+
outputs: frozen,
|
|
142
412
|
blendModes: {} as Record<string, unknown>,
|
|
143
413
|
setBlendMode(name: string, blend: unknown): IMRTNode {
|
|
144
414
|
node.blendModes[name] = blend;
|
|
@@ -180,10 +450,15 @@ export function definePass(
|
|
|
180
450
|
interface IOffscreen {
|
|
181
451
|
[drawPass](surface: ISurface, draw: PassDraw): void;
|
|
182
452
|
}
|
|
453
|
+
/** A pass the engine's frame draws: its updateBefore is the frame's hook. */
|
|
454
|
+
interface IPassFrame {
|
|
455
|
+
updateBefore(frame: unknown): unknown;
|
|
456
|
+
}
|
|
183
457
|
const offscreen = new Set<IOffscreen>();
|
|
458
|
+
const frameNodes = new Set<IPassFrame>();
|
|
184
459
|
let lastSurface: ISurface | undefined;
|
|
185
460
|
|
|
186
|
-
class PassNode implements IOffscreen {
|
|
461
|
+
class PassNode implements IOffscreen, IPassFrame {
|
|
187
462
|
static readonly COLOR = "color";
|
|
188
463
|
static readonly DEPTH = "depth";
|
|
189
464
|
readonly isPassNode = true;
|
|
@@ -192,6 +467,7 @@ export function definePass(
|
|
|
192
467
|
#camera: unknown;
|
|
193
468
|
#scale = 1;
|
|
194
469
|
#target: IPassTarget | null = null;
|
|
470
|
+
#frameTarget: FrameTarget | null = null;
|
|
195
471
|
#offscreen: boolean;
|
|
196
472
|
#disposed = false;
|
|
197
473
|
#viewZ = new Map<string, unknown>();
|
|
@@ -202,6 +478,7 @@ export function definePass(
|
|
|
202
478
|
this.#offscreen = scope !== framePass;
|
|
203
479
|
if (!this.#offscreen) {
|
|
204
480
|
onTarget?.(scene, camera);
|
|
481
|
+
frameNodes.add(this);
|
|
205
482
|
return;
|
|
206
483
|
}
|
|
207
484
|
if (scope !== PassNode.COLOR)
|
|
@@ -230,9 +507,40 @@ export function definePass(
|
|
|
230
507
|
if (!this.#offscreen) onTarget?.(this.#scene, value);
|
|
231
508
|
}
|
|
232
509
|
|
|
233
|
-
/**
|
|
234
|
-
|
|
235
|
-
|
|
510
|
+
/**
|
|
511
|
+
* The pass's target: an offscreen pass's own RenderTarget, or the frame pass's colour attachments.
|
|
512
|
+
* The frame pass draws into the engine's frame, so this answers its layout, size and sample count.
|
|
513
|
+
*/
|
|
514
|
+
get renderTarget(): IPassTarget | IFrameTarget | undefined {
|
|
515
|
+
if (this.#offscreen) return this.#target ?? undefined;
|
|
516
|
+
this.#frameTarget ??= new FrameTarget(host);
|
|
517
|
+
return this.#frameTarget;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* The texture of one of the pass's colour attachments, as three's PassNode.getTexture(name) answers it.
|
|
522
|
+
* A name the MRT does not write becomes a further attachment descriptor, as three pushes one.
|
|
523
|
+
*/
|
|
524
|
+
getTexture(name = "output"): IPassAttachment {
|
|
525
|
+
if (this.#offscreen)
|
|
526
|
+
throw new Error(
|
|
527
|
+
"TN_NATIVE_PASS_TEXTURE_UNSUPPORTED: an offscreen PassNode's colour is a node (getTextureNode()) with no JS texture",
|
|
528
|
+
);
|
|
529
|
+
if (name === "depth")
|
|
530
|
+
throw new Error(
|
|
531
|
+
"TN_NATIVE_PASS_TEXTURE_UNSUPPORTED: the frame pass's depth is a node (getTextureNode('depth')), not a JS texture",
|
|
532
|
+
);
|
|
533
|
+
const target = this.renderTarget as FrameTarget;
|
|
534
|
+
return name === "output" ? target.texture : target.named(name);
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* three's PassNode.updateBefore: the pass's frame. The engine draws the frame pass itself, so this
|
|
539
|
+
* body is the safe point a game wraps to run code at the pass's frame (the water proof reads back
|
|
540
|
+
* the attachment the pass last drew). An offscreen pass draws in updateSecondaryPasses.
|
|
541
|
+
*/
|
|
542
|
+
updateBefore(_frame: unknown): unknown {
|
|
543
|
+
return undefined;
|
|
236
544
|
}
|
|
237
545
|
|
|
238
546
|
setResolutionScale(scale: number): this {
|
|
@@ -268,9 +576,20 @@ export function definePass(
|
|
|
268
576
|
setMRT(value: IMRTNode | null): this {
|
|
269
577
|
if (value !== null && value?.isMRTNode !== true)
|
|
270
578
|
throw new Error("TN_NATIVE_MRT_UNSUPPORTED: setMRT takes mrt()");
|
|
579
|
+
// The material form's output nodes are written into a RenderTarget by the material itself, so a scene pass
|
|
580
|
+
// cannot lay them out: three's setMRT takes the pass's own slots plus the custom colour attachments.
|
|
581
|
+
if (value !== null && value.isMaterialMrt === true)
|
|
582
|
+
throw new Error(
|
|
583
|
+
"TN_NATIVE_MRT_UNSUPPORTED: a material's mrt() writes node outputs; set it on material.mrtNode",
|
|
584
|
+
);
|
|
271
585
|
if (value !== null && this.#offscreen)
|
|
272
586
|
throw new Error("TN_NATIVE_MRT_UNSUPPORTED: an offscreen PassNode draws colour only");
|
|
273
587
|
this.#mrt = value;
|
|
588
|
+
// The pass holds one colour attachment per output the MRT names, its own colour first (three's
|
|
589
|
+
// PassNode pushes a texture per MRT output; the engine draws that same layout).
|
|
590
|
+
const names = value === null ? [] : Object.keys(value.outputNodes);
|
|
591
|
+
this.#frameTarget ??= new FrameTarget(host);
|
|
592
|
+
this.#frameTarget.setLayout(["output", ...names.filter((name) => name !== "output")]);
|
|
274
593
|
return this;
|
|
275
594
|
}
|
|
276
595
|
|
|
@@ -300,6 +619,13 @@ export function definePass(
|
|
|
300
619
|
return tsl.texture({ name: "albedo" }, tsl.uv());
|
|
301
620
|
if (name === "emissive" && this.#mrt?.outputs.emissive === emissive)
|
|
302
621
|
return tsl.texture({ name: "emissive" }, tsl.uv());
|
|
622
|
+
// The draws that composited over the frame, lit as they were drawn. The engine draws them while the post graph
|
|
623
|
+
// reads this name (Renderer::planAux), so the read is the request and no MRT output names it: the occlusion and
|
|
624
|
+
// the gather belong to the opaque surface, and the composite restores what a translucent sheet drew over it.
|
|
625
|
+
if (name === "translucent") return tsl.texture({ name: "translucent" }, tsl.uv());
|
|
626
|
+
// Each lit draw's indirect diffuse light (hemisphere, ambient, environment, irradiance node; no direct light),
|
|
627
|
+
// drawn while the post graph reads the name (Renderer::planAux), so an occlusion can darken that share alone.
|
|
628
|
+
if (name === "indirectDiffuse") return tsl.texture({ name: "indirectDiffuse" }, tsl.uv());
|
|
303
629
|
if (
|
|
304
630
|
(name === "metalness" || name === "roughness") &&
|
|
305
631
|
this.#mrt?.outputs.normal === normalView &&
|
|
@@ -311,6 +637,9 @@ export function definePass(
|
|
|
311
637
|
(name === "roughness" && this.#mrt?.outputs.roughness === roughness)
|
|
312
638
|
)
|
|
313
639
|
return tsl.texture({ name }, tsl.uv());
|
|
640
|
+
// A custom slot the MRT names is a further colour target the engine draws (pass-node.ts).
|
|
641
|
+
if (this.#mrt !== null && Object.hasOwn(this.#mrt.outputs, name))
|
|
642
|
+
return tsl.texture({ name }, tsl.uv());
|
|
314
643
|
throw new Error(
|
|
315
644
|
`TN_NATIVE_PASS_TEXTURE_UNSUPPORTED: the native scene pass has no '${name}' target`,
|
|
316
645
|
);
|
|
@@ -339,12 +668,26 @@ export function definePass(
|
|
|
339
668
|
offscreen.delete(this);
|
|
340
669
|
this.#target?.dispose();
|
|
341
670
|
this.#target = null;
|
|
671
|
+
frameNodes.delete(this);
|
|
672
|
+
this.#frameTarget?.dispose();
|
|
673
|
+
this.#frameTarget = null;
|
|
342
674
|
}
|
|
343
675
|
}
|
|
344
676
|
|
|
345
677
|
const pass = (scene: unknown, camera: unknown): PassNode =>
|
|
346
678
|
new PassNode(framePass, scene, camera);
|
|
347
679
|
|
|
680
|
+
/**
|
|
681
|
+
* Runs every live frame pass's `updateBefore` at each render, as three's node update system does.
|
|
682
|
+
* The engine draws the frame pass itself, so a game's wrapper runs at the pass's frame point; a
|
|
683
|
+
* wrapper that must run once a frame (the water proof's copy) guards that itself, as three's
|
|
684
|
+
* PassNode does not count frames for it either.
|
|
685
|
+
*/
|
|
686
|
+
function updateFramePasses(frame?: { frameId?: number }): void {
|
|
687
|
+
if (frameNodes.size === 0) return;
|
|
688
|
+
for (const node of [...frameNodes]) node.updateBefore(frame);
|
|
689
|
+
}
|
|
690
|
+
|
|
348
691
|
/**
|
|
349
692
|
* Draws every live offscreen pass into its target, at the surface size, before the frame that
|
|
350
693
|
* samples them. A back end calls it once per frame with its drawing buffer and its target draw;
|
|
@@ -365,6 +708,7 @@ export function definePass(
|
|
|
365
708
|
pass,
|
|
366
709
|
PassNode,
|
|
367
710
|
updateSecondaryPasses,
|
|
711
|
+
updateFramePasses,
|
|
368
712
|
mrt,
|
|
369
713
|
output,
|
|
370
714
|
normalView,
|