@threenative/three-native 0.3.12 → 0.3.15
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 +1 -1
- package/src/addons/ccd-ik-solver.ts +170 -0
- package/src/addons/gltf-loader-web.ts +104 -17
- package/src/addons/screen-space.ts +43 -21
- package/src/animation-utils.ts +366 -0
- package/src/box2.ts +172 -0
- package/src/bridge-meter.ts +23 -9
- package/src/browser-abi-types.ts +260 -43
- package/src/browser-attribute-sync.ts +69 -0
- package/src/browser-backend.ts +75 -8
- package/src/browser-entry.ts +51 -12
- package/src/browser-loaders.ts +12 -0
- package/src/browser-renderer.ts +194 -118
- package/src/browser-tsl.ts +93 -16
- package/src/browser-value-views.ts +18 -2
- package/src/browser-wasm-runtime.ts +72 -30
- package/src/buffer-attributes.ts +19 -0
- package/src/compressed-texture.ts +116 -0
- package/src/debug-telemetry.ts +20 -6
- package/src/edges-geometry.ts +111 -0
- package/src/generated/console-markers.ts +14 -0
- package/src/generated/web-frame-fields.ts +85 -0
- package/src/gpu-stages.ts +17 -16
- package/src/grid-helper.ts +49 -0
- package/src/object-surface.ts +49 -6
- package/src/pass-node.ts +26 -6
- package/src/render-target.ts +478 -12
- package/src/shadow-node.ts +24 -1
- package/src/storage.ts +16 -5
- package/src/texture-sources.ts +12 -6
- package/src/tsl-uniforms.ts +146 -57
- package/src/update-ranges.ts +27 -1
package/src/render-target.ts
CHANGED
|
@@ -53,8 +53,11 @@ export function pixelsOfType(
|
|
|
53
53
|
|
|
54
54
|
/** The engine's two calls for a back end's render targets. */
|
|
55
55
|
export interface IRenderTargetEngine {
|
|
56
|
-
/**
|
|
57
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Draws `root` through `camera` into `layer` of `target` (over its contents when the renderer's autoClear is false);
|
|
58
|
+
* throws the engine's refusal, which names a layer outside the target's `depth`.
|
|
59
|
+
*/
|
|
60
|
+
draw(target: unknown, root: unknown, camera: unknown, layer: number): void;
|
|
58
61
|
/** Clears `target`'s colour (to the renderer's clear colour) and/or depth. */
|
|
59
62
|
clear(target: unknown, color: boolean, depth: boolean): void;
|
|
60
63
|
/**
|
|
@@ -71,48 +74,161 @@ export interface IRenderTargetEngine {
|
|
|
71
74
|
): Promise<Uint8Array>;
|
|
72
75
|
}
|
|
73
76
|
|
|
77
|
+
/** A cube or 3D target's 2D layer target and its layer count (see defineLayeredTargets). */
|
|
78
|
+
const layered = new WeakMap<object, { readonly layer: object; readonly layers: number }>();
|
|
79
|
+
|
|
80
|
+
interface IViewportLike {
|
|
81
|
+
readonly x: number;
|
|
82
|
+
readonly y: number;
|
|
83
|
+
readonly z: number;
|
|
84
|
+
readonly w: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A layered target draws whole layers: a partial viewport or a scissor is refused, not ignored. */
|
|
88
|
+
function assertWholeLayer(target: {
|
|
89
|
+
readonly width: number;
|
|
90
|
+
readonly height: number;
|
|
91
|
+
readonly viewport: IViewportLike;
|
|
92
|
+
readonly scissorTest: boolean;
|
|
93
|
+
}): void {
|
|
94
|
+
const { viewport } = target;
|
|
95
|
+
if (
|
|
96
|
+
target.scissorTest ||
|
|
97
|
+
viewport.x !== 0 ||
|
|
98
|
+
viewport.y !== 0 ||
|
|
99
|
+
viewport.z !== target.width ||
|
|
100
|
+
viewport.w !== target.height
|
|
101
|
+
)
|
|
102
|
+
throw new Error(
|
|
103
|
+
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a cube or 3D target draws whole layers natively (full viewport, no scissor)",
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
/** The layered textures each renderer has created on the GPU (three's initTexture before the first copy). */
|
|
107
|
+
const initialized = new WeakMap<object, WeakSet<object>>();
|
|
108
|
+
|
|
109
|
+
interface ILayeredRenderer {
|
|
110
|
+
initTexture(texture: unknown): void;
|
|
111
|
+
copyTextureToTexture(
|
|
112
|
+
source: unknown,
|
|
113
|
+
destination: unknown,
|
|
114
|
+
srcRegion: IRegionLike | null,
|
|
115
|
+
dstPosition: IPositionLike | null,
|
|
116
|
+
): void;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Copies a layered target's layer target into its texture at `face` (a cube face or a 3D slice). */
|
|
120
|
+
function copyLayer(
|
|
121
|
+
renderer: object,
|
|
122
|
+
target: { readonly texture: object },
|
|
123
|
+
layer: object,
|
|
124
|
+
face: number,
|
|
125
|
+
): void {
|
|
126
|
+
const gpu = renderer as ILayeredRenderer;
|
|
127
|
+
const created = initialized.get(renderer) ?? new WeakSet<object>();
|
|
128
|
+
initialized.set(renderer, created);
|
|
129
|
+
if (!created.has(target.texture)) {
|
|
130
|
+
gpu.initTexture(target.texture);
|
|
131
|
+
created.add(target.texture);
|
|
132
|
+
}
|
|
133
|
+
const texture = (layer as { readonly texture: object }).texture;
|
|
134
|
+
gpu.copyTextureToTexture(texture, target.texture, null, { x: 0, y: 0, z: face });
|
|
135
|
+
}
|
|
136
|
+
|
|
74
137
|
/**
|
|
75
|
-
* Adds three's setRenderTarget, getRenderTarget, getActiveCubeFace, getActiveMipmapLevel,
|
|
76
|
-
* clearColor, clearDepth, clearStencil and readRenderTargetPixelsAsync to a renderer facade's prototype,
|
|
138
|
+
* Adds three's setRenderTarget, getRenderTarget, getActiveCubeFace, getActiveMipmapLevel, getMRT, setMRT,
|
|
139
|
+
* initRenderTarget, clear, clearColor, clearDepth, clearStencil and readRenderTargetPixelsAsync to a renderer facade's prototype,
|
|
77
140
|
* and routes its render() into the current target. `engineOf(renderer)` reaches the engine for that renderer.
|
|
141
|
+
* A cube face or 3D slice (`activeCubeFace`) draws into the target's 2D layer target, then is copied into
|
|
142
|
+
* its texture at that layer.
|
|
143
|
+
* ponytail: one layer copy per draw; render into a layer view when a profile shows cube or volume
|
|
144
|
+
* captures cost.
|
|
78
145
|
*/
|
|
79
146
|
export function defineRenderTargets(
|
|
80
147
|
prototype: { render(scene: unknown, camera: unknown): void },
|
|
81
148
|
engineOf: (renderer: object) => IRenderTargetEngine,
|
|
82
149
|
): void {
|
|
83
150
|
const targets = new WeakMap<object, unknown>();
|
|
151
|
+
// three's activeCubeFace: a cube face, a 3D slice, or the layer of an array target (`depth` > 1).
|
|
152
|
+
const faces = new WeakMap<object, number>();
|
|
153
|
+
const mrts = new WeakMap<object, unknown>();
|
|
84
154
|
const render = prototype.render;
|
|
85
155
|
Object.assign(prototype, {
|
|
156
|
+
// three's WebGPURenderer works in WebGPU clip space (z in [0, 1]); CubeCamera orients its faces by it.
|
|
157
|
+
coordinateSystem: 2001,
|
|
86
158
|
setRenderTarget(
|
|
87
159
|
this: object,
|
|
88
160
|
target: unknown = null,
|
|
89
161
|
activeCubeFace = 0,
|
|
90
162
|
activeMipmapLevel = 0,
|
|
91
163
|
): void {
|
|
92
|
-
|
|
164
|
+
const layers =
|
|
165
|
+
typeof target === "object" && target !== null ? layered.get(target)?.layers : undefined;
|
|
166
|
+
if (activeMipmapLevel !== 0)
|
|
93
167
|
throw new Error(
|
|
94
|
-
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a
|
|
168
|
+
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a mip level other than 0 is not drawn natively",
|
|
169
|
+
);
|
|
170
|
+
if (
|
|
171
|
+
layers === undefined &&
|
|
172
|
+
(!Number.isInteger(activeCubeFace) || activeCubeFace < 0 || activeCubeFace > 255)
|
|
173
|
+
)
|
|
174
|
+
throw new Error(
|
|
175
|
+
`TN_NATIVE_RENDER_TARGET_UNSUPPORTED: layer ${activeCubeFace} is not 0 to 255 natively`,
|
|
176
|
+
);
|
|
177
|
+
if (
|
|
178
|
+
layers !== undefined &&
|
|
179
|
+
!(Number.isInteger(activeCubeFace) && activeCubeFace >= 0 && activeCubeFace < layers)
|
|
180
|
+
)
|
|
181
|
+
throw new RangeError(
|
|
182
|
+
`TN_NATIVE_RENDER_TARGET_LAYER: layer ${activeCubeFace} is outside 0 to ${layers - 1}`,
|
|
95
183
|
);
|
|
96
184
|
targets.set(this, target);
|
|
185
|
+
faces.set(this, activeCubeFace);
|
|
97
186
|
},
|
|
98
187
|
getRenderTarget(this: object): unknown {
|
|
99
188
|
return targets.get(this) ?? null;
|
|
100
189
|
},
|
|
101
|
-
getActiveCubeFace(): number {
|
|
102
|
-
return 0;
|
|
190
|
+
getActiveCubeFace(this: object): number {
|
|
191
|
+
return faces.get(this) ?? 0;
|
|
103
192
|
},
|
|
104
193
|
getActiveMipmapLevel(): number {
|
|
105
194
|
return 0;
|
|
106
195
|
},
|
|
107
196
|
render(this: object, scene: unknown, camera: unknown): void {
|
|
108
197
|
const target = targets.get(this);
|
|
198
|
+
const layer = target ? layered.get(target)?.layer : undefined;
|
|
109
199
|
if (target === null || target === undefined) render.call(this, scene, camera);
|
|
110
|
-
|
|
200
|
+
// A cube face or 3D slice draws its 2D layer target and copies it in; an array target's layer
|
|
201
|
+
// draws natively.
|
|
202
|
+
else if (layer === undefined)
|
|
203
|
+
engineOf(this).draw(target, scene, camera, faces.get(this) ?? 0);
|
|
204
|
+
else {
|
|
205
|
+
assertWholeLayer(target as Parameters<typeof assertWholeLayer>[0]);
|
|
206
|
+
engineOf(this).draw(layer, scene, camera, 0);
|
|
207
|
+
copyLayer(this, target as { texture: object }, layer, faces.get(this) ?? 0);
|
|
208
|
+
}
|
|
209
|
+
},
|
|
210
|
+
// three's renderer-wide MRT. A material's own mrtNode draws natively; a renderer-wide one is refused by name.
|
|
211
|
+
getMRT(this: object): unknown {
|
|
212
|
+
return mrts.get(this) ?? null;
|
|
213
|
+
},
|
|
214
|
+
setMRT(this: object, mrt: unknown): object {
|
|
215
|
+
if (mrt !== null && mrt !== undefined)
|
|
216
|
+
throw new Error(
|
|
217
|
+
"TN_NATIVE_MRT_UNSUPPORTED: renderer.setMRT takes null natively; set mrtNode on the material or the pass",
|
|
218
|
+
);
|
|
219
|
+
mrts.set(this, null);
|
|
220
|
+
return this;
|
|
111
221
|
},
|
|
222
|
+
// three allocates the target's textures here; the engine allocates them on the first draw.
|
|
223
|
+
initRenderTarget(): void {},
|
|
112
224
|
// ponytail: on the canvas clear() is a no-op, because the canvas render clears every frame. A target has no stencil.
|
|
113
225
|
clear(this: object, color = true, depth = true): void {
|
|
114
226
|
const target = targets.get(this);
|
|
115
|
-
if (target
|
|
227
|
+
if (target === null || target === undefined) return;
|
|
228
|
+
const layer = layered.get(target as object)?.layer;
|
|
229
|
+
engineOf(this).clear(layer ?? target, color, depth);
|
|
230
|
+
if (layer !== undefined && color)
|
|
231
|
+
copyLayer(this, target as { texture: object }, layer, faces.get(this) ?? 0);
|
|
116
232
|
},
|
|
117
233
|
clearColor(this: { clear(color: boolean, depth: boolean): void }): void {
|
|
118
234
|
this.clear(true, false);
|
|
@@ -131,9 +247,9 @@ export function defineRenderTargets(
|
|
|
131
247
|
textureIndex = 0,
|
|
132
248
|
faceIndex = 0,
|
|
133
249
|
): Promise<Uint8Array | Uint16Array | Float32Array> {
|
|
134
|
-
if (faceIndex !== 0)
|
|
250
|
+
if (faceIndex !== 0 || layered.has(target))
|
|
135
251
|
throw new Error(
|
|
136
|
-
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a cube
|
|
252
|
+
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a cube or 3D target is not read natively",
|
|
137
253
|
);
|
|
138
254
|
const bytes = await engineOf(this).read(target, x, y, width, height, textureIndex);
|
|
139
255
|
// The engine crops tightly, and so does three 0.187.0 (copyTextureToBuffer removes the row
|
|
@@ -142,3 +258,353 @@ export function defineRenderTargets(
|
|
|
142
258
|
},
|
|
143
259
|
});
|
|
144
260
|
}
|
|
261
|
+
|
|
262
|
+
/** three's Box2 and Box3 as a texture copy reads them: a region by its corners. */
|
|
263
|
+
export interface IRegionLike {
|
|
264
|
+
readonly isBox3?: boolean;
|
|
265
|
+
readonly min: { readonly x: number; readonly y: number; readonly z?: number };
|
|
266
|
+
readonly max: { readonly x: number; readonly y: number; readonly z?: number };
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** A Vector2 or Vector3: the destination origin of a texture copy, z the array layer. */
|
|
270
|
+
export interface IPositionLike {
|
|
271
|
+
readonly x: number;
|
|
272
|
+
readonly y: number;
|
|
273
|
+
readonly z?: number;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* three's copyTextureToTexture arguments as both back ends' engine call takes them: the source origin and extent
|
|
278
|
+
* (a negative width takes the source's image size), the destination origin (z the array layer), and the two levels.
|
|
279
|
+
*/
|
|
280
|
+
export function textureCopyArguments(
|
|
281
|
+
srcRegion: IRegionLike | null,
|
|
282
|
+
dstPosition: IPositionLike | null,
|
|
283
|
+
srcLevel: number,
|
|
284
|
+
dstLevel: number,
|
|
285
|
+
): [number, number, number, number, number, number, number, number, number, number, number] {
|
|
286
|
+
const { min, max } = srcRegion ?? {};
|
|
287
|
+
// A Box2 has no z: its depth is one layer, a Box3's its own.
|
|
288
|
+
const size =
|
|
289
|
+
min && max ? [max.x - min.x, max.y - min.y, (max.z ?? 1) - (min.z ?? 0)] : [-1, -1, 1];
|
|
290
|
+
return [
|
|
291
|
+
min?.x ?? 0,
|
|
292
|
+
min?.y ?? 0,
|
|
293
|
+
min?.z ?? 0,
|
|
294
|
+
size[0] as number,
|
|
295
|
+
size[1] as number,
|
|
296
|
+
srcRegion?.isBox3 ? (size[2] as number) : 1,
|
|
297
|
+
dstPosition?.x ?? 0,
|
|
298
|
+
dstPosition?.y ?? 0,
|
|
299
|
+
dstPosition?.z ?? 0,
|
|
300
|
+
srcLevel,
|
|
301
|
+
dstLevel,
|
|
302
|
+
];
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
type Constructor = new (...args: never[]) => object;
|
|
306
|
+
|
|
307
|
+
interface IObject3DLike {
|
|
308
|
+
readonly children: readonly object[];
|
|
309
|
+
readonly layers: { mask: number };
|
|
310
|
+
parent: object | null;
|
|
311
|
+
add(object: object): void;
|
|
312
|
+
remove(object: object): void;
|
|
313
|
+
updateMatrixWorld(force?: boolean): void;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
interface ICubeFaceCamera {
|
|
317
|
+
readonly layers: { mask: number };
|
|
318
|
+
readonly up: { set(x: number, y: number, z: number): void };
|
|
319
|
+
lookAt(x: number, y: number, z: number): void;
|
|
320
|
+
updateMatrixWorld(force?: boolean): void;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
interface IRendererLike {
|
|
324
|
+
coordinateSystem?: number;
|
|
325
|
+
autoClear?: boolean;
|
|
326
|
+
reversedDepthBuffer?: boolean;
|
|
327
|
+
xr?: { enabled: boolean };
|
|
328
|
+
getRenderTarget(): unknown;
|
|
329
|
+
getActiveCubeFace(): number;
|
|
330
|
+
getActiveMipmapLevel(): number;
|
|
331
|
+
setRenderTarget(target: unknown, activeCubeFace?: number, activeMipmapLevel?: number): void;
|
|
332
|
+
render(scene: unknown, camera: unknown): void;
|
|
333
|
+
clearDepth(): void;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
interface ITargetTexture {
|
|
337
|
+
type: number;
|
|
338
|
+
minFilter: number;
|
|
339
|
+
magFilter: number;
|
|
340
|
+
generateMipmaps: boolean;
|
|
341
|
+
isRenderTargetTexture?: boolean;
|
|
342
|
+
needsPMREMUpdate?: boolean;
|
|
343
|
+
mipmapsAutoUpdate?: boolean;
|
|
344
|
+
dispose(): void;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
interface ITargetOptions {
|
|
348
|
+
readonly type?: number;
|
|
349
|
+
readonly minFilter?: number;
|
|
350
|
+
readonly magFilter?: number;
|
|
351
|
+
readonly generateMipmaps?: boolean;
|
|
352
|
+
readonly [option: string]: unknown;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const LINEAR_FILTER = 1006; // three's LinearFilter
|
|
356
|
+
const UNSIGNED_BYTE_TYPE = 1009; // three's UnsignedByteType
|
|
357
|
+
const WEBGL_COORDINATES = 2000; // three's WebGLCoordinateSystem
|
|
358
|
+
const WEBGPU_COORDINATES = 2001; // three's WebGPUCoordinateSystem
|
|
359
|
+
|
|
360
|
+
/** RenderTarget._setTextureOptions (three r187 core/RenderTarget.js): the options a target's texture takes. */
|
|
361
|
+
function setTextureOptions(texture: ITargetTexture, options: ITargetOptions): void {
|
|
362
|
+
texture.type = options.type ?? UNSIGNED_BYTE_TYPE;
|
|
363
|
+
texture.minFilter = options.minFilter ?? LINEAR_FILTER;
|
|
364
|
+
texture.magFilter = options.magFilter ?? LINEAR_FILTER;
|
|
365
|
+
texture.generateMipmaps = options.generateMipmaps ?? false;
|
|
366
|
+
texture.isRenderTargetTexture = true;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* three r187's CubeRenderTarget, RenderTarget3D and CubeCamera over the engine classes, shared by both back
|
|
371
|
+
* ends (PRD-854). Each target draws a layer into a 2D engine RenderTarget of the same options, which the
|
|
372
|
+
* renderer copies into the texture at that layer (defineRenderTargets): a CubeTexture of six faces, or a
|
|
373
|
+
* Data3DTexture of `depth` slices. CubeCamera is three's own, over the engine Object3D and PerspectiveCamera.
|
|
374
|
+
*/
|
|
375
|
+
export function defineLayeredTargets(
|
|
376
|
+
classes: Readonly<
|
|
377
|
+
Record<
|
|
378
|
+
| "RenderTarget"
|
|
379
|
+
| "CubeTexture"
|
|
380
|
+
| "Data3DTexture"
|
|
381
|
+
| "Object3D"
|
|
382
|
+
| "PerspectiveCamera"
|
|
383
|
+
| "Vector4",
|
|
384
|
+
Constructor
|
|
385
|
+
>
|
|
386
|
+
>,
|
|
387
|
+
) {
|
|
388
|
+
const Vector4 = classes.Vector4 as new (
|
|
389
|
+
x: number,
|
|
390
|
+
y: number,
|
|
391
|
+
z: number,
|
|
392
|
+
w: number,
|
|
393
|
+
) => IViewportLike;
|
|
394
|
+
const RenderTarget = classes.RenderTarget as new (
|
|
395
|
+
width: number,
|
|
396
|
+
height: number,
|
|
397
|
+
options: ITargetOptions,
|
|
398
|
+
) => { setSize(width: number, height: number): void; dispose(): void };
|
|
399
|
+
const CubeTexture = classes.CubeTexture as new (images: readonly object[]) => ITargetTexture;
|
|
400
|
+
const Data3DTexture = classes.Data3DTexture as new (
|
|
401
|
+
data: null,
|
|
402
|
+
width: number,
|
|
403
|
+
height: number,
|
|
404
|
+
depth: number,
|
|
405
|
+
) => ITargetTexture;
|
|
406
|
+
const Object3D = classes.Object3D as new () => IObject3DLike;
|
|
407
|
+
const PerspectiveCamera = classes.PerspectiveCamera as new (
|
|
408
|
+
fov: number,
|
|
409
|
+
aspect: number,
|
|
410
|
+
near: number,
|
|
411
|
+
far: number,
|
|
412
|
+
) => ICubeFaceCamera;
|
|
413
|
+
|
|
414
|
+
/** three's CubeRenderTarget(size, options): six square faces, each drawn with `setRenderTarget(target, face)`. */
|
|
415
|
+
class CubeRenderTarget {
|
|
416
|
+
readonly isRenderTarget = true;
|
|
417
|
+
readonly isWebGLCubeRenderTarget = true;
|
|
418
|
+
readonly isCubeRenderTarget = true;
|
|
419
|
+
width: number;
|
|
420
|
+
height: number;
|
|
421
|
+
readonly depth = 1;
|
|
422
|
+
readonly texture: ITargetTexture;
|
|
423
|
+
readonly viewport: IViewportLike;
|
|
424
|
+
readonly scissor: IViewportLike;
|
|
425
|
+
scissorTest = false;
|
|
426
|
+
readonly #layer: ReturnType<typeof layerTarget>;
|
|
427
|
+
|
|
428
|
+
constructor(size = 1, options: ITargetOptions = {}) {
|
|
429
|
+
this.width = this.height = size;
|
|
430
|
+
this.viewport = new Vector4(0, 0, size, size);
|
|
431
|
+
this.scissor = new Vector4(0, 0, size, size);
|
|
432
|
+
const image = { width: size, height: size, depth: 1 };
|
|
433
|
+
this.texture = new CubeTexture([image, image, image, image, image, image]);
|
|
434
|
+
setTextureOptions(this.texture, options);
|
|
435
|
+
this.#layer = layerTarget(this, size, size, 6, options);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
setSize(width: number, height: number): void {
|
|
439
|
+
if (width !== this.width || height !== this.height)
|
|
440
|
+
throw new Error(
|
|
441
|
+
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a CubeRenderTarget keeps its size natively",
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
fromEquirectangularTexture(): never {
|
|
446
|
+
throw new Error(
|
|
447
|
+
"TN_NATIVE_UNSUPPORTED_CUBERENDERTARGET: fromEquirectangularTexture is not available natively; set the equirectangular texture as the scene background",
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
dispose(): void {
|
|
452
|
+
this.#layer.dispose();
|
|
453
|
+
this.texture.dispose();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** three's RenderTarget3D(width, height, depth, options): `depth` slices, each drawn with `setRenderTarget(target, slice)`. */
|
|
458
|
+
class RenderTarget3D {
|
|
459
|
+
readonly isRenderTarget = true;
|
|
460
|
+
readonly isRenderTarget3D = true;
|
|
461
|
+
width: number;
|
|
462
|
+
height: number;
|
|
463
|
+
readonly depth: number;
|
|
464
|
+
readonly texture: ITargetTexture;
|
|
465
|
+
readonly viewport: IViewportLike;
|
|
466
|
+
readonly scissor: IViewportLike;
|
|
467
|
+
scissorTest = false;
|
|
468
|
+
readonly #layer: ReturnType<typeof layerTarget>;
|
|
469
|
+
|
|
470
|
+
constructor(width = 1, height = 1, depth = 1, options: ITargetOptions = {}) {
|
|
471
|
+
this.width = width;
|
|
472
|
+
this.height = height;
|
|
473
|
+
this.viewport = new Vector4(0, 0, width, height);
|
|
474
|
+
this.scissor = new Vector4(0, 0, width, height);
|
|
475
|
+
this.depth = depth;
|
|
476
|
+
this.texture = new Data3DTexture(null, width, height, depth);
|
|
477
|
+
setTextureOptions(this.texture, options);
|
|
478
|
+
this.#layer = layerTarget(this, width, height, depth, options);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
setSize(width: number, height: number, depth = this.depth): void {
|
|
482
|
+
if (width !== this.width || height !== this.height || depth !== this.depth)
|
|
483
|
+
throw new Error(
|
|
484
|
+
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a RenderTarget3D keeps its size natively",
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
dispose(): void {
|
|
489
|
+
this.#layer.dispose();
|
|
490
|
+
this.texture.dispose();
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function layerTarget(
|
|
495
|
+
target: object,
|
|
496
|
+
width: number,
|
|
497
|
+
height: number,
|
|
498
|
+
layers: number,
|
|
499
|
+
options: ITargetOptions,
|
|
500
|
+
) {
|
|
501
|
+
const layer = new RenderTarget(width, height, { ...options, generateMipmaps: false });
|
|
502
|
+
layered.set(target, { layer, layers });
|
|
503
|
+
return layer;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const fov = -90; // negative fov to not invert the faces (three's CubeCamera)
|
|
507
|
+
const aspect = 1;
|
|
508
|
+
|
|
509
|
+
/** three r187's CubeCamera (cameras/CubeCamera.js), ported as written. */
|
|
510
|
+
class CubeCamera extends Object3D {
|
|
511
|
+
readonly type = "CubeCamera";
|
|
512
|
+
renderTarget: CubeRenderTarget;
|
|
513
|
+
coordinateSystem: number | null = null;
|
|
514
|
+
activeMipmapLevel = 0;
|
|
515
|
+
|
|
516
|
+
constructor(near: number, far: number, renderTarget: CubeRenderTarget) {
|
|
517
|
+
super();
|
|
518
|
+
this.renderTarget = renderTarget;
|
|
519
|
+
for (let face = 0; face < 6; face += 1) {
|
|
520
|
+
const camera = new PerspectiveCamera(fov, aspect, near, far);
|
|
521
|
+
// three shares the CubeCamera's Layers with each face; an engine camera owns its Layers, so the mask is
|
|
522
|
+
// copied here and again on update().
|
|
523
|
+
camera.layers.mask = this.layers.mask;
|
|
524
|
+
this.add(camera);
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
updateCoordinateSystem(): void {
|
|
529
|
+
const coordinateSystem = this.coordinateSystem;
|
|
530
|
+
const cameras = this.children.concat() as ICubeFaceCamera[];
|
|
531
|
+
const [cameraPX, cameraNX, cameraPY, cameraNY, cameraPZ, cameraNZ] = cameras as [
|
|
532
|
+
ICubeFaceCamera,
|
|
533
|
+
ICubeFaceCamera,
|
|
534
|
+
ICubeFaceCamera,
|
|
535
|
+
ICubeFaceCamera,
|
|
536
|
+
ICubeFaceCamera,
|
|
537
|
+
ICubeFaceCamera,
|
|
538
|
+
];
|
|
539
|
+
for (const camera of cameras) this.remove(camera as object);
|
|
540
|
+
if (coordinateSystem === WEBGL_COORDINATES) {
|
|
541
|
+
cameraPX.up.set(0, 1, 0);
|
|
542
|
+
cameraPX.lookAt(1, 0, 0);
|
|
543
|
+
cameraNX.up.set(0, 1, 0);
|
|
544
|
+
cameraNX.lookAt(-1, 0, 0);
|
|
545
|
+
cameraPY.up.set(0, 0, -1);
|
|
546
|
+
cameraPY.lookAt(0, 1, 0);
|
|
547
|
+
cameraNY.up.set(0, 0, 1);
|
|
548
|
+
cameraNY.lookAt(0, -1, 0);
|
|
549
|
+
cameraPZ.up.set(0, 1, 0);
|
|
550
|
+
cameraPZ.lookAt(0, 0, 1);
|
|
551
|
+
cameraNZ.up.set(0, 1, 0);
|
|
552
|
+
cameraNZ.lookAt(0, 0, -1);
|
|
553
|
+
} else if (coordinateSystem === WEBGPU_COORDINATES) {
|
|
554
|
+
cameraPX.up.set(0, -1, 0);
|
|
555
|
+
cameraPX.lookAt(-1, 0, 0);
|
|
556
|
+
cameraNX.up.set(0, -1, 0);
|
|
557
|
+
cameraNX.lookAt(1, 0, 0);
|
|
558
|
+
cameraPY.up.set(0, 0, 1);
|
|
559
|
+
cameraPY.lookAt(0, 1, 0);
|
|
560
|
+
cameraNY.up.set(0, 0, -1);
|
|
561
|
+
cameraNY.lookAt(0, -1, 0);
|
|
562
|
+
cameraPZ.up.set(0, -1, 0);
|
|
563
|
+
cameraPZ.lookAt(0, 0, 1);
|
|
564
|
+
cameraNZ.up.set(0, -1, 0);
|
|
565
|
+
cameraNZ.lookAt(0, 0, -1);
|
|
566
|
+
} else {
|
|
567
|
+
throw new Error(
|
|
568
|
+
`THREE.CubeCamera.updateCoordinateSystem(): Invalid coordinate system: ${coordinateSystem}`,
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
for (const camera of cameras) {
|
|
572
|
+
this.add(camera as object);
|
|
573
|
+
camera.updateMatrixWorld();
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
update(renderer: IRendererLike, scene: unknown): void {
|
|
578
|
+
if (this.parent === null) this.updateMatrixWorld();
|
|
579
|
+
const { renderTarget, activeMipmapLevel } = this;
|
|
580
|
+
if (this.coordinateSystem !== renderer.coordinateSystem) {
|
|
581
|
+
this.coordinateSystem = renderer.coordinateSystem ?? null;
|
|
582
|
+
this.updateCoordinateSystem();
|
|
583
|
+
}
|
|
584
|
+
const cameras = this.children;
|
|
585
|
+
for (const camera of cameras as readonly ICubeFaceCamera[])
|
|
586
|
+
camera.layers.mask = this.layers.mask;
|
|
587
|
+
const currentRenderTarget = renderer.getRenderTarget();
|
|
588
|
+
const currentActiveCubeFace = renderer.getActiveCubeFace();
|
|
589
|
+
const currentActiveMipmapLevel = renderer.getActiveMipmapLevel();
|
|
590
|
+
const currentXrEnabled = renderer.xr?.enabled;
|
|
591
|
+
if (renderer.xr !== undefined) renderer.xr.enabled = false;
|
|
592
|
+
const reversedDepthBuffer = renderer.reversedDepthBuffer === true;
|
|
593
|
+
for (let face = 0; face < 6; face += 1) {
|
|
594
|
+
renderer.setRenderTarget(renderTarget, face, activeMipmapLevel);
|
|
595
|
+
if (reversedDepthBuffer && renderer.autoClear === false) renderer.clearDepth();
|
|
596
|
+
renderer.render(scene, cameras[face]);
|
|
597
|
+
}
|
|
598
|
+
renderer.setRenderTarget(
|
|
599
|
+
currentRenderTarget,
|
|
600
|
+
currentActiveCubeFace,
|
|
601
|
+
currentActiveMipmapLevel,
|
|
602
|
+
);
|
|
603
|
+
if (renderer.xr !== undefined && currentXrEnabled !== undefined)
|
|
604
|
+
renderer.xr.enabled = currentXrEnabled;
|
|
605
|
+
renderTarget.texture.needsPMREMUpdate = true;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
return { CubeRenderTarget, RenderTarget3D, CubeCamera };
|
|
610
|
+
}
|
package/src/shadow-node.ts
CHANGED
|
@@ -3,6 +3,8 @@ let nextId = 0;
|
|
|
3
3
|
const installed = new Set<ShadowBaseNode>();
|
|
4
4
|
const dispatchKey = Symbol.for("threenative.virtualShadowCommand");
|
|
5
5
|
const bridgeKey = Symbol.for("threenative.virtualShadow");
|
|
6
|
+
/** Core's VirtualShadowNode reads this for `settled` when the engine runs its clipmap. */
|
|
7
|
+
const settledKey = Symbol.for("threenative.virtualShadowSettled");
|
|
6
8
|
/** Core's registration counter for the discrete LOD chains (see `bumpChains` in core's model-lod). */
|
|
7
9
|
const chainsVersionKey = Symbol.for("threenative.discreteLodChainsVersion");
|
|
8
10
|
/** The facade's count of `mesh.geometry` assignments (browser-backend's setter): a swap moves no hierarchy. */
|
|
@@ -11,6 +13,7 @@ const geometryAssignmentsKey = Symbol.for("threenative.geometryAssignments");
|
|
|
11
13
|
interface IBridgeSlots {
|
|
12
14
|
[dispatchKey]?: unknown;
|
|
13
15
|
[bridgeKey]?: unknown;
|
|
16
|
+
[settledKey]?: boolean;
|
|
14
17
|
}
|
|
15
18
|
interface IShadowObject {
|
|
16
19
|
id: number;
|
|
@@ -25,6 +28,7 @@ export function detachVirtualShadow(value: unknown): void {
|
|
|
25
28
|
value.dispatch = undefined;
|
|
26
29
|
value.lods.clear();
|
|
27
30
|
delete (value as IBridgeSlots)[dispatchKey];
|
|
31
|
+
delete (value as IBridgeSlots)[settledKey];
|
|
28
32
|
}
|
|
29
33
|
/**
|
|
30
34
|
* Records what the light-root walk depends on and answers whether the last one still holds. Its answer
|
|
@@ -48,11 +52,30 @@ function walkStillCurrent(node: ShadowBaseNode, root: IShadowObject): boolean {
|
|
|
48
52
|
node.walkedAssignments = assignments;
|
|
49
53
|
return current;
|
|
50
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Core's VirtualShadowNode counts frames in updateBefore, which the engine never runs, so the engine reports each world
|
|
57
|
+
* render here: `pending` (web frame field 88, from the frame snapshot) is the levels its clipmaps still owe a render (`deferred`), -1 when there is no virtual shadow.
|
|
58
|
+
* ponytail: one total for every node, exact for the one sun a game has; per light when a game has two.
|
|
59
|
+
*/
|
|
60
|
+
export function reportVirtualShadowFrame(pending: number): void {
|
|
61
|
+
if (installed.size === 0 || pending < 0) return;
|
|
62
|
+
for (const node of installed) {
|
|
63
|
+
const stats = (node as { stats?: { frame: number; held: number; deferred: number } }).stats;
|
|
64
|
+
if (node.disposed || stats === undefined) continue;
|
|
65
|
+
stats.frame += 1;
|
|
66
|
+
stats.held = 0;
|
|
67
|
+
stats.deferred = pending;
|
|
68
|
+
(node as IBridgeSlots)[settledKey] = pending === 0;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
51
72
|
export function syncVirtualShadowLods(): void {
|
|
52
73
|
const chains = (globalThis as Record<symbol, unknown>)[
|
|
53
74
|
Symbol.for("threenative.discreteLodChains")
|
|
54
75
|
] as WeakMap<object, { chain: { levels: object[]; errors: number[] } }> | undefined;
|
|
55
|
-
|
|
76
|
+
// Core sets the version with its first registration and never unsets it: before that, every walk finds nothing.
|
|
77
|
+
// A scene that adds or removes objects each frame (the stress lab's `play`) walked every proxy for nothing (PRD-871).
|
|
78
|
+
if (!chains || (globalThis as Record<symbol, unknown>)[chainsVersionKey] === undefined) return;
|
|
56
79
|
for (const node of installed) {
|
|
57
80
|
if (node.disposed) {
|
|
58
81
|
installed.delete(node);
|
package/src/storage.ts
CHANGED
|
@@ -101,12 +101,15 @@ export function defineStorage(
|
|
|
101
101
|
itemSize: number,
|
|
102
102
|
) => { count: number; itemSize: number; needsUpdate: boolean },
|
|
103
103
|
) {
|
|
104
|
-
|
|
104
|
+
// `flags`: the type flags three's class chain carries — `IndirectStorageBufferAttribute` extends
|
|
105
|
+
// `StorageBufferAttribute`, so an indirect record buffer is a storage buffer too. `Default` is the array
|
|
106
|
+
// a class allocates when the caller passes a count: an indirect record is uint32 words.
|
|
107
|
+
const make = (flags: readonly string[], Default: TypedArrayClass = Float32Array) => {
|
|
105
108
|
const Attribute = class extends BufferAttribute {
|
|
106
109
|
constructor(
|
|
107
110
|
count: number | StorageArray,
|
|
108
111
|
itemSize: number,
|
|
109
|
-
typeClass: TypedArrayClass =
|
|
112
|
+
typeClass: TypedArrayClass = Default,
|
|
110
113
|
) {
|
|
111
114
|
const array = ArrayBuffer.isView(count)
|
|
112
115
|
? (count as StorageArray)
|
|
@@ -118,11 +121,18 @@ export function defineStorage(
|
|
|
118
121
|
this.needsUpdate = true;
|
|
119
122
|
}
|
|
120
123
|
};
|
|
121
|
-
Object.defineProperty(Attribute.prototype, flag, { value: true });
|
|
124
|
+
for (const flag of flags) Object.defineProperty(Attribute.prototype, flag, { value: true });
|
|
122
125
|
return Attribute;
|
|
123
126
|
};
|
|
124
|
-
const StorageBufferAttribute = make("isStorageBufferAttribute");
|
|
125
|
-
const StorageInstancedBufferAttribute = make("isStorageInstancedBufferAttribute");
|
|
127
|
+
const StorageBufferAttribute = make(["isStorageBufferAttribute"]);
|
|
128
|
+
const StorageInstancedBufferAttribute = make(["isStorageInstancedBufferAttribute"]);
|
|
129
|
+
// three's IndirectStorageBufferAttribute (renderers/common/IndirectStorageBufferAttribute.js): a
|
|
130
|
+
// uint32 storage attribute a kernel fills with indirect draw records, and the attribute a geometry's
|
|
131
|
+
// `setIndirect` takes. Its flag matters to the renderer that reads `geometry.indirect`.
|
|
132
|
+
const IndirectStorageBufferAttribute = make(
|
|
133
|
+
["isIndirectStorageBufferAttribute", "isStorageBufferAttribute"],
|
|
134
|
+
Uint32Array,
|
|
135
|
+
);
|
|
126
136
|
|
|
127
137
|
const storage = (
|
|
128
138
|
value: {
|
|
@@ -168,6 +178,7 @@ export function defineStorage(
|
|
|
168
178
|
return {
|
|
169
179
|
StorageBufferAttribute,
|
|
170
180
|
StorageInstancedBufferAttribute,
|
|
181
|
+
IndirectStorageBufferAttribute,
|
|
171
182
|
storage,
|
|
172
183
|
attributeArray: array(StorageBufferAttribute),
|
|
173
184
|
instancedArray: array(StorageInstancedBufferAttribute),
|
package/src/texture-sources.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type EngineValue,
|
|
3
|
+
type IBrowserRuntime,
|
|
4
|
+
type IEngineRef,
|
|
5
|
+
engineRef,
|
|
6
|
+
} from "./browser-backend.js";
|
|
1
7
|
/**
|
|
2
8
|
* three's texture sources on the browser back end: the typed array a `DataTexture` was built
|
|
3
9
|
* from, a 2D canvas, and an image (`ImageBitmap`, `<img>`, `ImageData`). The engine holds a copy of
|
|
@@ -5,12 +11,7 @@
|
|
|
5
11
|
* before an upload. The V8 player's equivalent is `runtime-native/src/engine/player/core-textures.mjs`;
|
|
6
12
|
* the browser decodes images itself, so no bitmap needs the engine decoder here.
|
|
7
13
|
*/
|
|
8
|
-
import {
|
|
9
|
-
type EngineValue,
|
|
10
|
-
type IBrowserRuntime,
|
|
11
|
-
type IEngineRef,
|
|
12
|
-
engineRef,
|
|
13
|
-
} from "./browser-backend.js";
|
|
14
|
+
import { defineCompressedTexture } from "./compressed-texture.js";
|
|
14
15
|
|
|
15
16
|
type EngineClass = new (...args: unknown[]) => object;
|
|
16
17
|
|
|
@@ -84,6 +85,10 @@ export function defineTextureSources(
|
|
|
84
85
|
const BaseLayers = classes.DataArrayTexture as EngineClass | undefined;
|
|
85
86
|
const BaseData = classes.DataTexture as EngineClass;
|
|
86
87
|
const BaseCanvas = classes.CanvasTexture as EngineClass | undefined;
|
|
88
|
+
const CompressedTexture = defineCompressedTexture(
|
|
89
|
+
classes.CompressedTexture as EngineClass | undefined,
|
|
90
|
+
runtime,
|
|
91
|
+
);
|
|
87
92
|
if (BaseCanvas === undefined) throw new TypeError("TN_BROWSER_UNBOUND: CanvasTexture");
|
|
88
93
|
const sources = new WeakMap<object, unknown>();
|
|
89
94
|
const prototype = BaseData.prototype as object;
|
|
@@ -315,6 +320,7 @@ export function defineTextureSources(
|
|
|
315
320
|
|
|
316
321
|
return {
|
|
317
322
|
CanvasTexture,
|
|
323
|
+
...(CompressedTexture === undefined ? {} : { CompressedTexture }),
|
|
318
324
|
DataTexture,
|
|
319
325
|
...(BaseVolume === undefined ? {} : { Data3DTexture }),
|
|
320
326
|
...(BaseLayers === undefined ? {} : { DataArrayTexture }),
|