@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/render-target.ts
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
const HALF_FLOAT_TYPE = 1016; // three's HalfFloatType
|
|
9
9
|
const FLOAT_TYPE = 1015; // three's FloatType
|
|
10
|
+
const RG_FORMAT = 1030; // three's RGFormat
|
|
10
11
|
|
|
11
12
|
interface IRenderTargetLike {
|
|
12
13
|
readonly texture: { readonly type: number };
|
|
@@ -27,9 +28,9 @@ function halfToFloat(bits: number): number {
|
|
|
27
28
|
return bits & 0x8000 ? -magnitude : magnitude;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
|
-
/** Bytes a pixel in the rows `read` returns:
|
|
31
|
-
export function targetTexelBytes(type: number): number {
|
|
32
|
-
return type === FLOAT_TYPE ?
|
|
31
|
+
/** Bytes a pixel in the rows `read` returns: float32 channels for FloatType, else half; two for RGFormat, else four. */
|
|
32
|
+
export function targetTexelBytes(type: number, format?: number): number {
|
|
33
|
+
return (type === FLOAT_TYPE ? 4 : 2) * (format === RG_FORMAT ? 2 : 4);
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
/**
|
|
@@ -52,50 +53,190 @@ export function pixelsOfType(
|
|
|
52
53
|
|
|
53
54
|
/** The engine's two calls for a back end's render targets. */
|
|
54
55
|
export interface IRenderTargetEngine {
|
|
55
|
-
/**
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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;
|
|
61
|
+
/** Clears `target`'s colour (to the renderer's clear colour) and/or depth. */
|
|
62
|
+
clear(target: unknown, color: boolean, depth: boolean): void;
|
|
63
|
+
/**
|
|
64
|
+
* The region's rows in the target's format (see targetTexelBytes), top row first, of colour
|
|
65
|
+
* attachment `textureIndex` (three's index into `target.textures`).
|
|
66
|
+
*/
|
|
67
|
+
read(
|
|
68
|
+
target: unknown,
|
|
69
|
+
x: number,
|
|
70
|
+
y: number,
|
|
71
|
+
width: number,
|
|
72
|
+
height: number,
|
|
73
|
+
textureIndex?: number,
|
|
74
|
+
): Promise<Uint8Array>;
|
|
75
|
+
}
|
|
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 });
|
|
59
135
|
}
|
|
60
136
|
|
|
61
137
|
/**
|
|
62
|
-
* Adds three's setRenderTarget, getRenderTarget, getActiveCubeFace, getActiveMipmapLevel
|
|
63
|
-
* readRenderTargetPixelsAsync to a renderer facade's prototype,
|
|
64
|
-
* current target. `engineOf(renderer)` reaches the engine for that renderer.
|
|
138
|
+
* Adds three's setRenderTarget, getRenderTarget, getActiveCubeFace, getActiveMipmapLevel, getMRT, setMRT,
|
|
139
|
+
* initRenderTarget, clear, clearColor, clearDepth, clearStencil and readRenderTargetPixelsAsync to a renderer facade's prototype,
|
|
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.
|
|
65
145
|
*/
|
|
66
146
|
export function defineRenderTargets(
|
|
67
147
|
prototype: { render(scene: unknown, camera: unknown): void },
|
|
68
148
|
engineOf: (renderer: object) => IRenderTargetEngine,
|
|
69
149
|
): void {
|
|
70
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>();
|
|
71
154
|
const render = prototype.render;
|
|
72
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,
|
|
73
158
|
setRenderTarget(
|
|
74
159
|
this: object,
|
|
75
160
|
target: unknown = null,
|
|
76
161
|
activeCubeFace = 0,
|
|
77
162
|
activeMipmapLevel = 0,
|
|
78
163
|
): void {
|
|
79
|
-
|
|
164
|
+
const layers =
|
|
165
|
+
typeof target === "object" && target !== null ? layered.get(target)?.layers : undefined;
|
|
166
|
+
if (activeMipmapLevel !== 0)
|
|
167
|
+
throw new Error(
|
|
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
|
+
)
|
|
80
174
|
throw new Error(
|
|
81
|
-
|
|
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}`,
|
|
82
183
|
);
|
|
83
184
|
targets.set(this, target);
|
|
185
|
+
faces.set(this, activeCubeFace);
|
|
84
186
|
},
|
|
85
187
|
getRenderTarget(this: object): unknown {
|
|
86
188
|
return targets.get(this) ?? null;
|
|
87
189
|
},
|
|
88
|
-
getActiveCubeFace(): number {
|
|
89
|
-
return 0;
|
|
190
|
+
getActiveCubeFace(this: object): number {
|
|
191
|
+
return faces.get(this) ?? 0;
|
|
90
192
|
},
|
|
91
193
|
getActiveMipmapLevel(): number {
|
|
92
194
|
return 0;
|
|
93
195
|
},
|
|
94
196
|
render(this: object, scene: unknown, camera: unknown): void {
|
|
95
197
|
const target = targets.get(this);
|
|
198
|
+
const layer = target ? layered.get(target)?.layer : undefined;
|
|
96
199
|
if (target === null || target === undefined) render.call(this, scene, camera);
|
|
97
|
-
|
|
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;
|
|
98
221
|
},
|
|
222
|
+
// three allocates the target's textures here; the engine allocates them on the first draw.
|
|
223
|
+
initRenderTarget(): void {},
|
|
224
|
+
// ponytail: on the canvas clear() is a no-op, because the canvas render clears every frame. A target has no stencil.
|
|
225
|
+
clear(this: object, color = true, depth = true): void {
|
|
226
|
+
const target = targets.get(this);
|
|
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);
|
|
232
|
+
},
|
|
233
|
+
clearColor(this: { clear(color: boolean, depth: boolean): void }): void {
|
|
234
|
+
this.clear(true, false);
|
|
235
|
+
},
|
|
236
|
+
clearDepth(this: { clear(color: boolean, depth: boolean): void }): void {
|
|
237
|
+
this.clear(false, true);
|
|
238
|
+
},
|
|
239
|
+
clearStencil(): void {},
|
|
99
240
|
async readRenderTargetPixelsAsync(
|
|
100
241
|
this: object,
|
|
101
242
|
target: IRenderTargetLike,
|
|
@@ -103,9 +244,367 @@ export function defineRenderTargets(
|
|
|
103
244
|
y: number,
|
|
104
245
|
width: number,
|
|
105
246
|
height: number,
|
|
247
|
+
textureIndex = 0,
|
|
248
|
+
faceIndex = 0,
|
|
106
249
|
): Promise<Uint8Array | Uint16Array | Float32Array> {
|
|
107
|
-
|
|
250
|
+
if (faceIndex !== 0 || layered.has(target))
|
|
251
|
+
throw new Error(
|
|
252
|
+
"TN_NATIVE_RENDER_TARGET_UNSUPPORTED: a cube or 3D target is not read natively",
|
|
253
|
+
);
|
|
254
|
+
const bytes = await engineOf(this).read(target, x, y, width, height, textureIndex);
|
|
255
|
+
// The engine crops tightly, and so does three 0.187.0 (copyTextureToBuffer removes the row
|
|
256
|
+
// padding), so the array a game reads has the same tight stride on both back ends.
|
|
108
257
|
return pixelsOfType(bytes, target.texture.type);
|
|
109
258
|
},
|
|
110
259
|
});
|
|
111
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),
|