@threenative/core 0.3.4 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/capabilities.json +1505 -79
- package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
- package/dist/{canvas-layer-DDmC_VVF.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
- package/dist/{game-CljaDv4D.d.ts → game-Bj8G1wmD.d.ts} +102 -11
- package/dist/hot.d.ts +4 -4
- package/dist/index.d.ts +712 -118
- package/dist/index.js +12423 -4955
- package/dist/{gpu-readback-CqJEfWNQ.d.ts → model-lod-BDAUYgsS.d.ts} +80 -5
- package/dist/playtest.d.ts +6 -10
- package/dist/playtest.js +47 -24
- package/dist/react-css.d.ts +62 -0
- package/dist/react-css.js +591 -0
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-CfsS2hxi.d.ts → renderer-9yEo9hPa.d.ts} +489 -22
- package/dist/terrain-jobs-worker.d.ts +2 -0
- package/dist/terrain-jobs-worker.js +130 -0
- package/dist/ui-layer.js +59 -8
- package/dist/world.d.ts +766 -73
- package/dist/world.js +9307 -3543
- package/gpl/recipes/export_world.py +18 -1
- package/mcp/blender-server.mjs +7 -4
- package/mcp/blender.mjs +3 -4
- package/mcp/engine-server.mjs +2 -2
- package/mcp/servers.mjs +4 -4
- package/package.json +15 -9
- package/patches/three@0.185.1-prd269-upgrade.patch +22418 -0
- package/patches/three@0.185.1.patch +6594 -700
- package/scripts/apply-three-patch.mjs +62 -4
- package/src/animation-blend.ts +264 -0
- package/src/animation-composition.ts +362 -0
- package/src/animation-layers.ts +260 -0
- package/src/animation-root-motion.ts +283 -0
- package/src/animation.ts +896 -0
- package/src/assets.ts +1303 -0
- package/src/atmosphere/index.ts +335 -0
- package/src/atmosphere/luts.ts +298 -0
- package/src/atmosphere/params.ts +323 -0
- package/src/audio.ts +912 -0
- package/src/billboard.ts +124 -0
- package/src/bone-lengths.ts +157 -0
- package/src/camera-shake.ts +116 -0
- package/src/canvas-layer.ts +47 -0
- package/src/clip-audit.ts +401 -0
- package/src/clustered-batch.ts +492 -0
- package/src/clustered-mesh.ts +638 -0
- package/src/compute-driven.ts +176 -0
- package/src/compute-timing.ts +318 -0
- package/src/config.ts +593 -0
- package/src/debug.ts +82 -0
- package/src/engine-render-hook.ts +25 -0
- package/src/entities.ts +79 -0
- package/src/entity-snapshot.ts +53 -0
- package/src/flight.ts +683 -0
- package/src/fluid-field.ts +748 -0
- package/src/fluid-particles.ts +1253 -0
- package/src/frame-budget.ts +1368 -0
- package/src/game.ts +2390 -0
- package/src/geometry-capture.ts +1098 -0
- package/src/gpu-class.ts +160 -0
- package/src/gpu-frame-observation.ts +424 -0
- package/src/gpu-readback.ts +143 -0
- package/src/gpu-scene-bvh.ts +464 -0
- package/src/grounding.ts +100 -0
- package/src/hot.ts +161 -0
- package/src/import-meta.d.ts +13 -0
- package/src/index.ts +1536 -0
- package/src/input.ts +690 -0
- package/src/instanced-batch-lod.ts +283 -0
- package/src/instanced-batch.ts +274 -0
- package/src/interpolation.ts +129 -0
- package/src/launch-diagnostics.ts +161 -0
- package/src/loop.ts +564 -0
- package/src/matrix-world.ts +245 -0
- package/src/merge-parts.ts +812 -0
- package/src/model-lod.ts +1315 -0
- package/src/net-protocol.ts +333 -0
- package/src/net-session.ts +477 -0
- package/src/net.ts +80 -0
- package/src/ocean/fft.ts +72 -0
- package/src/ocean/spectral.ts +728 -0
- package/src/particle-significance.ts +102 -0
- package/src/particles.ts +648 -0
- package/src/path-follow.ts +175 -0
- package/src/picking.ts +250 -0
- package/src/pipeline-census.ts +1566 -0
- package/src/platform.ts +210 -0
- package/src/playtest.ts +839 -0
- package/src/pointer-events.ts +411 -0
- package/src/pose-measure.ts +435 -0
- package/src/profiling/FrameCounters.ts +231 -0
- package/src/profiling/Spans.ts +548 -0
- package/src/profiling/opacity-convention.ts +21 -0
- package/src/profiling/render-list-validate.ts +175 -0
- package/src/profiling/render-trap-audit.ts +84 -0
- package/src/profiling/scene-warning.ts +190 -0
- package/src/profiling/span-probes.ts +191 -0
- package/src/profiling/unlit-foliage-warning.ts +149 -0
- package/src/projection-apply.ts +1529 -0
- package/src/projection-marker.ts +142 -0
- package/src/projection-plan.ts +1258 -0
- package/src/projection-skinned.ts +419 -0
- package/src/projection-stability.ts +409 -0
- package/src/projection-uniform.ts +328 -0
- package/src/random.ts +41 -0
- package/src/react-css.ts +604 -0
- package/src/react-glyphs.ts +114 -0
- package/src/react-host.ts +576 -0
- package/src/react-layout.ts +265 -0
- package/src/react.ts +60 -0
- package/src/render/alpha-antialiasing.ts +159 -0
- package/src/render/batched-velocity.ts +126 -0
- package/src/render/chain.ts +1021 -0
- package/src/render/daylight.ts +189 -0
- package/src/render/depth-pyramid.ts +596 -0
- package/src/render/foliage-alpha.ts +166 -0
- package/src/render/material-key.ts +60 -0
- package/src/render/mesh-pool.ts +144 -0
- package/src/render/probe-volume.ts +1668 -0
- package/src/render/resolution-subrect.ts +253 -0
- package/src/render/velocity.ts +442 -0
- package/src/render/virtual-shadow-pages.ts +827 -0
- package/src/render/virtual-shadow-partial.ts +174 -0
- package/src/render/virtual-shadow.ts +3330 -0
- package/src/render/world-impostor-surface.ts +722 -0
- package/src/render/world-impostor.ts +558 -0
- package/src/render-camera-cull.ts +441 -0
- package/src/render-pass-budget.ts +501 -0
- package/src/renderProjection.ts +758 -0
- package/src/renderer-config.ts +127 -0
- package/src/renderer.ts +1847 -0
- package/src/replay-protocol.ts +85 -0
- package/src/replay.ts +205 -0
- package/src/resolution-scaler.ts +819 -0
- package/src/rig-preparation.ts +271 -0
- package/src/ripple-field.ts +432 -0
- package/src/scale.ts +109 -0
- package/src/scene.ts +239 -0
- package/src/schedule.ts +155 -0
- package/src/skeletal-mesh.ts +61 -0
- package/src/skeleton.ts +100 -0
- package/src/snow-field.ts +744 -0
- package/src/softbody-topology.ts +269 -0
- package/src/softbody.ts +516 -0
- package/src/sprite-animator.ts +222 -0
- package/src/startup-readiness.ts +386 -0
- package/src/state.ts +66 -0
- package/src/static-transform.ts +274 -0
- package/src/storage-buffer.ts +253 -0
- package/src/streaming.ts +308 -0
- package/src/target-fps.ts +92 -0
- package/src/terrain-jobs-worker.ts +17 -0
- package/src/terrain-jobs.ts +353 -0
- package/src/textures.ts +39 -0
- package/src/tracers.ts +237 -0
- package/src/ui-bridge.ts +368 -0
- package/src/ui-hit-regions.ts +380 -0
- package/src/ui-layer.ts +83 -0
- package/src/ui-state.ts +220 -0
- package/src/version.ts +2 -0
- package/src/viewport.ts +253 -0
- package/src/warmup.ts +1357 -0
- package/src/water-surface.ts +384 -0
- package/src/wave-field.ts +524 -0
- package/src/world-capabilities.ts +131 -0
- package/src/world-cell-proxy.ts +392 -0
- package/src/world-cells.ts +10564 -0
- package/src/world-gpu-scene.ts +3354 -0
- package/src/world-heightmap.ts +82 -0
- package/src/world-package.ts +469 -0
- package/src/world-passes.ts +582 -0
- package/src/world-region.ts +17 -0
- package/src/world-terrain-splat.ts +564 -0
- package/src/world-tiles.ts +3826 -0
- package/src/world-topology.ts +370 -0
- package/src/world-validate.ts +48 -0
- package/src/world.ts +805 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { AttachedBindMode, Box3, type Object3D, Quaternion, Vector3 } from "three";
|
|
2
|
+
import type { Mesh, SkinnedMesh } from "three";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Brings every attached-mode skinned mesh under `root` to the bind state the renderer would give it.
|
|
6
|
+
*
|
|
7
|
+
* `SkinnedMesh` refreshes `bindMatrixInverse` in `updateMatrixWorld` — the renderer's call — and not
|
|
8
|
+
* in `updateWorldMatrix`, which is what a measurement makes. After a figure moves between a render
|
|
9
|
+
* and a measurement, three places the skin at the figure's old transform: measured that way, a
|
|
10
|
+
* finger bone of the shipped mannequin got a 3.8 m envelope sphere and GroundSnap held the figure
|
|
11
|
+
* 0.66 m above its floor. Re-deriving it here is exactly what `updateMatrixWorld` does.
|
|
12
|
+
*/
|
|
13
|
+
function syncSkinBindings(root: Object3D): void {
|
|
14
|
+
root.traverse((object) => {
|
|
15
|
+
const skinned = object as Partial<SkinnedMesh>;
|
|
16
|
+
if (skinned.isSkinnedMesh === true && skinned.bindMode === AttachedBindMode)
|
|
17
|
+
skinned.bindMatrixInverse?.copy(object.matrixWorld).invert();
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type ThreePoseVector = readonly [number, number, number];
|
|
22
|
+
export type ThreePoseQuaternion = readonly [number, number, number, number];
|
|
23
|
+
|
|
24
|
+
export interface IThreePoseBounds {
|
|
25
|
+
readonly min: ThreePoseVector;
|
|
26
|
+
readonly max: ThreePoseVector;
|
|
27
|
+
readonly size: ThreePoseVector;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** JSON-safe world-space measurements for one Three.js object and its visual bounds. */
|
|
31
|
+
export interface IThreePoseMeasurement {
|
|
32
|
+
readonly name: string;
|
|
33
|
+
readonly type: string;
|
|
34
|
+
readonly position: ThreePoseVector;
|
|
35
|
+
readonly quaternion: ThreePoseQuaternion;
|
|
36
|
+
readonly scale: ThreePoseVector;
|
|
37
|
+
readonly axes: {
|
|
38
|
+
readonly x: ThreePoseVector;
|
|
39
|
+
readonly y: ThreePoseVector;
|
|
40
|
+
readonly z: ThreePoseVector;
|
|
41
|
+
};
|
|
42
|
+
readonly bounds: IThreePoseBounds | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface IMeasureThreePoseOptions {
|
|
46
|
+
/**
|
|
47
|
+
* Objects whose geometry forms the reported bounds. Defaults to `object`.
|
|
48
|
+
* This path is precise; it walks every vertex. Do not call it in a frame loop — see
|
|
49
|
+
* `posedBounds`.
|
|
50
|
+
*/
|
|
51
|
+
readonly bounds?: readonly Object3D[] | false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function vector(value: Vector3): ThreePoseVector {
|
|
55
|
+
return [value.x, value.y, value.z];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Measure an Object3D in world space for attachment and animation diagnostics.
|
|
60
|
+
*
|
|
61
|
+
* Passing explicit `bounds` lets a probe measure a body without an attached weapon or
|
|
62
|
+
* invisible hitbox. The result is JSON-safe so it can cross the browser playtest bridge.
|
|
63
|
+
*/
|
|
64
|
+
export function measureThreePose(
|
|
65
|
+
object: Object3D,
|
|
66
|
+
options: IMeasureThreePoseOptions = {},
|
|
67
|
+
): IThreePoseMeasurement {
|
|
68
|
+
if (options.bounds !== undefined && options.bounds !== false && options.bounds.length === 0) {
|
|
69
|
+
throw new Error("measureThreePose bounds must contain at least one Object3D.");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A socket/bone probe needs its ancestors current, not every descendant bone. Avoiding that
|
|
73
|
+
// subtree walk matters when a registered entity reports several joints each debug sample.
|
|
74
|
+
object.updateWorldMatrix(true, options.bounds !== false);
|
|
75
|
+
if (options.bounds !== false)
|
|
76
|
+
for (const bounded of options.bounds ?? [object]) syncSkinBindings(bounded);
|
|
77
|
+
const worldPosition = object.getWorldPosition(new Vector3());
|
|
78
|
+
const worldQuaternion = object.getWorldQuaternion(new Quaternion());
|
|
79
|
+
const worldScale = object.getWorldScale(new Vector3());
|
|
80
|
+
const x = new Vector3(1, 0, 0).applyQuaternion(worldQuaternion).normalize();
|
|
81
|
+
const y = new Vector3(0, 1, 0).applyQuaternion(worldQuaternion).normalize();
|
|
82
|
+
const z = new Vector3(0, 0, 1).applyQuaternion(worldQuaternion).normalize();
|
|
83
|
+
|
|
84
|
+
let measuredBounds: IThreePoseBounds | null = null;
|
|
85
|
+
if (options.bounds !== false) {
|
|
86
|
+
const bounds = new Box3();
|
|
87
|
+
for (const bounded of options.bounds ?? [object]) bounds.expandByObject(bounded, true);
|
|
88
|
+
if (bounds.isEmpty()) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`measureThreePose could not measure bounds for '${object.name || object.type}'. Pass { bounds: false } for a geometry-free attachment point.`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
const size = bounds.getSize(new Vector3());
|
|
94
|
+
measuredBounds = { min: vector(bounds.min), max: vector(bounds.max), size: vector(size) };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
name: object.name,
|
|
99
|
+
type: object.type,
|
|
100
|
+
position: vector(worldPosition),
|
|
101
|
+
quaternion: [worldQuaternion.x, worldQuaternion.y, worldQuaternion.z, worldQuaternion.w],
|
|
102
|
+
scale: vector(worldScale),
|
|
103
|
+
axes: { x: vector(x), y: vector(y), z: vector(z) },
|
|
104
|
+
bounds: measuredBounds,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
interface IBoneSphere {
|
|
109
|
+
readonly bone: Object3D;
|
|
110
|
+
readonly radius: number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
interface IStaticSphere {
|
|
114
|
+
readonly object: Object3D;
|
|
115
|
+
readonly radius: number;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
interface IPosedBoundsResult {
|
|
119
|
+
readonly min: [number, number, number];
|
|
120
|
+
readonly max: [number, number, number];
|
|
121
|
+
readonly size: [number, number, number];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
interface IPosedBoundsEnvelope {
|
|
125
|
+
readonly meshes: readonly Object3D[];
|
|
126
|
+
readonly selection: "default" | "explicit";
|
|
127
|
+
readonly bones: readonly IBoneSphere[];
|
|
128
|
+
readonly statics: readonly IStaticSphere[];
|
|
129
|
+
biasY: number;
|
|
130
|
+
readonly raw: IRawBounds;
|
|
131
|
+
readonly result: IPosedBoundsResult;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface IRawBounds {
|
|
135
|
+
minX: number;
|
|
136
|
+
minY: number;
|
|
137
|
+
minZ: number;
|
|
138
|
+
maxX: number;
|
|
139
|
+
maxY: number;
|
|
140
|
+
maxZ: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const posedBoundsCache = new WeakMap<Object3D, IPosedBoundsEnvelope[]>();
|
|
144
|
+
|
|
145
|
+
function asMesh(object: Object3D): Mesh | undefined {
|
|
146
|
+
return (object as Mesh).isMesh === true ? (object as Mesh) : undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function asSkinnedMesh(mesh: Mesh): SkinnedMesh | undefined {
|
|
150
|
+
const skinned = mesh as SkinnedMesh;
|
|
151
|
+
return skinned.isSkinnedMesh === true && skinned.skeleton !== undefined ? skinned : undefined;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function collectMeshes(root: Object3D): Object3D[] {
|
|
155
|
+
const meshes: Object3D[] = [];
|
|
156
|
+
root.traverse((object) => {
|
|
157
|
+
if (asMesh(object) !== undefined) meshes.push(object);
|
|
158
|
+
});
|
|
159
|
+
return meshes;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function sameMeshes(left: readonly Object3D[], right: readonly Object3D[]): boolean {
|
|
163
|
+
if (left.length !== right.length) return false;
|
|
164
|
+
for (let index = 0; index < left.length; index += 1) {
|
|
165
|
+
if (left[index] !== right[index]) return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function staticSphereRadius(mesh: Mesh): number | undefined {
|
|
171
|
+
const geometry = mesh.geometry;
|
|
172
|
+
if (geometry.boundingSphere === null) geometry.computeBoundingSphere();
|
|
173
|
+
const sphere = geometry.boundingSphere;
|
|
174
|
+
if (sphere === null || !Number.isFinite(sphere.radius)) return undefined;
|
|
175
|
+
|
|
176
|
+
const elements = mesh.matrixWorld.elements;
|
|
177
|
+
const centre = sphere.center;
|
|
178
|
+
const worldCentreX =
|
|
179
|
+
elements[0] * centre.x + elements[4] * centre.y + elements[8] * centre.z + elements[12];
|
|
180
|
+
const worldCentreY =
|
|
181
|
+
elements[1] * centre.x + elements[5] * centre.y + elements[9] * centre.z + elements[13];
|
|
182
|
+
const worldCentreZ =
|
|
183
|
+
elements[2] * centre.x + elements[6] * centre.y + elements[10] * centre.z + elements[14];
|
|
184
|
+
const worldRadius =
|
|
185
|
+
sphere.radius *
|
|
186
|
+
Math.max(
|
|
187
|
+
Math.hypot(elements[0], elements[1], elements[2]),
|
|
188
|
+
Math.hypot(elements[4], elements[5], elements[6]),
|
|
189
|
+
Math.hypot(elements[8], elements[9], elements[10]),
|
|
190
|
+
);
|
|
191
|
+
const offset = Math.hypot(
|
|
192
|
+
worldCentreX - elements[12],
|
|
193
|
+
worldCentreY - elements[13],
|
|
194
|
+
worldCentreZ - elements[14],
|
|
195
|
+
);
|
|
196
|
+
return worldRadius + offset;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function preciseMinimumY(meshes: readonly Object3D[]): number {
|
|
200
|
+
let minimum = Number.POSITIVE_INFINITY;
|
|
201
|
+
const bounds = new Box3();
|
|
202
|
+
for (const object of meshes) {
|
|
203
|
+
const mesh = asMesh(object);
|
|
204
|
+
const position = mesh?.geometry.getAttribute("position");
|
|
205
|
+
if (
|
|
206
|
+
mesh &&
|
|
207
|
+
position &&
|
|
208
|
+
asSkinnedMesh(mesh) === undefined &&
|
|
209
|
+
!(mesh as Mesh & { isInstancedMesh?: boolean }).isInstancedMesh &&
|
|
210
|
+
mesh.children.length === 0 &&
|
|
211
|
+
(mesh.geometry.morphAttributes.position?.length ?? 0) === 0
|
|
212
|
+
) {
|
|
213
|
+
// Calibration needs only the exact lowest world Y. Avoid transforming every vertex
|
|
214
|
+
// in X/Z and updating six box extrema for every copy of the same static geometry.
|
|
215
|
+
const e = mesh.matrixWorld.elements;
|
|
216
|
+
if (e[1] === 0 && e[9] === 0) {
|
|
217
|
+
if (mesh.geometry.boundingBox === null) mesh.geometry.computeBoundingBox();
|
|
218
|
+
const box = mesh.geometry.boundingBox;
|
|
219
|
+
if (box) minimum = Math.min(minimum, (e[5] < 0 ? box.max.y : box.min.y) * e[5] + e[13]);
|
|
220
|
+
} else {
|
|
221
|
+
for (let i = 0; i < position.count; i++)
|
|
222
|
+
minimum = Math.min(
|
|
223
|
+
minimum,
|
|
224
|
+
position.getX(i) * e[1] + position.getY(i) * e[5] + position.getZ(i) * e[9] + e[13],
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
} else {
|
|
228
|
+
bounds.makeEmpty().expandByObject(object, true);
|
|
229
|
+
minimum = Math.min(minimum, bounds.min.y);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (!Number.isFinite(minimum))
|
|
233
|
+
throw new Error("posedBounds could not measure any mesh geometry.");
|
|
234
|
+
return minimum;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function rawBounds(envelope: IPosedBoundsEnvelope, target: IRawBounds): void {
|
|
238
|
+
target.minX = Number.POSITIVE_INFINITY;
|
|
239
|
+
target.minY = Number.POSITIVE_INFINITY;
|
|
240
|
+
target.minZ = Number.POSITIVE_INFINITY;
|
|
241
|
+
target.maxX = Number.NEGATIVE_INFINITY;
|
|
242
|
+
target.maxY = Number.NEGATIVE_INFINITY;
|
|
243
|
+
target.maxZ = Number.NEGATIVE_INFINITY;
|
|
244
|
+
|
|
245
|
+
for (let index = 0; index < envelope.bones.length; index += 1) {
|
|
246
|
+
const sphere = envelope.bones[index];
|
|
247
|
+
if (sphere === undefined) continue;
|
|
248
|
+
const elements = sphere.bone.matrixWorld.elements;
|
|
249
|
+
const x = elements[12];
|
|
250
|
+
const y = elements[13];
|
|
251
|
+
const z = elements[14];
|
|
252
|
+
const radius = sphere.radius;
|
|
253
|
+
target.minX = Math.min(target.minX, x - radius);
|
|
254
|
+
target.minY = Math.min(target.minY, y - radius);
|
|
255
|
+
target.minZ = Math.min(target.minZ, z - radius);
|
|
256
|
+
target.maxX = Math.max(target.maxX, x + radius);
|
|
257
|
+
target.maxY = Math.max(target.maxY, y + radius);
|
|
258
|
+
target.maxZ = Math.max(target.maxZ, z + radius);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
for (let index = 0; index < envelope.statics.length; index += 1) {
|
|
262
|
+
const sphere = envelope.statics[index];
|
|
263
|
+
if (sphere === undefined) continue;
|
|
264
|
+
const elements = sphere.object.matrixWorld.elements;
|
|
265
|
+
const x = elements[12];
|
|
266
|
+
const y = elements[13];
|
|
267
|
+
const z = elements[14];
|
|
268
|
+
const radius = sphere.radius;
|
|
269
|
+
target.minX = Math.min(target.minX, x - radius);
|
|
270
|
+
target.minY = Math.min(target.minY, y - radius);
|
|
271
|
+
target.minZ = Math.min(target.minZ, z - radius);
|
|
272
|
+
target.maxX = Math.max(target.maxX, x + radius);
|
|
273
|
+
target.maxY = Math.max(target.maxY, y + radius);
|
|
274
|
+
target.maxZ = Math.max(target.maxZ, z + radius);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function createEnvelope(
|
|
279
|
+
root: Object3D,
|
|
280
|
+
meshes: readonly Object3D[],
|
|
281
|
+
selection: IPosedBoundsEnvelope["selection"],
|
|
282
|
+
): IPosedBoundsEnvelope {
|
|
283
|
+
root.updateWorldMatrix(true, true);
|
|
284
|
+
syncSkinBindings(root);
|
|
285
|
+
for (const mesh of meshes) syncSkinBindings(mesh);
|
|
286
|
+
const radii = new Map<Object3D, number>();
|
|
287
|
+
const statics: IStaticSphere[] = [];
|
|
288
|
+
const vertex = new Vector3();
|
|
289
|
+
|
|
290
|
+
for (const object of meshes) {
|
|
291
|
+
const mesh = asMesh(object);
|
|
292
|
+
if (mesh === undefined) continue;
|
|
293
|
+
if (!addSkinnedMesh(mesh, radii, vertex)) {
|
|
294
|
+
const radius = staticSphereRadius(mesh);
|
|
295
|
+
if (radius !== undefined) statics.push({ object, radius });
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const bones: IBoneSphere[] = [];
|
|
300
|
+
for (const [bone, radius] of radii) bones.push({ bone, radius });
|
|
301
|
+
const result: IPosedBoundsResult = {
|
|
302
|
+
min: [0, 0, 0],
|
|
303
|
+
max: [0, 0, 0],
|
|
304
|
+
size: [0, 0, 0],
|
|
305
|
+
};
|
|
306
|
+
const raw: IRawBounds = {
|
|
307
|
+
minX: 0,
|
|
308
|
+
minY: 0,
|
|
309
|
+
minZ: 0,
|
|
310
|
+
maxX: 0,
|
|
311
|
+
maxY: 0,
|
|
312
|
+
maxZ: 0,
|
|
313
|
+
};
|
|
314
|
+
const envelope: IPosedBoundsEnvelope = {
|
|
315
|
+
meshes,
|
|
316
|
+
selection,
|
|
317
|
+
bones,
|
|
318
|
+
statics,
|
|
319
|
+
biasY: 0,
|
|
320
|
+
raw,
|
|
321
|
+
result,
|
|
322
|
+
};
|
|
323
|
+
rawBounds(envelope, raw);
|
|
324
|
+
const minimumY = preciseMinimumY(meshes);
|
|
325
|
+
envelope.biasY = minimumY - raw.minY;
|
|
326
|
+
return envelope;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function addSkinnedMesh(mesh: Mesh, radii: Map<Object3D, number>, vertex: Vector3): boolean {
|
|
330
|
+
const skinned = asSkinnedMesh(mesh);
|
|
331
|
+
const position = mesh.geometry.getAttribute("position");
|
|
332
|
+
const skinIndex = mesh.geometry.getAttribute("skinIndex");
|
|
333
|
+
const skinWeight = mesh.geometry.getAttribute("skinWeight");
|
|
334
|
+
if (
|
|
335
|
+
skinned === undefined ||
|
|
336
|
+
position === undefined ||
|
|
337
|
+
skinIndex === undefined ||
|
|
338
|
+
skinWeight === undefined
|
|
339
|
+
) {
|
|
340
|
+
return false;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
skinned.skeleton.update();
|
|
344
|
+
// Loop invariants are read once: on the native host each read crosses to the engine.
|
|
345
|
+
const bones = skinned.skeleton.bones;
|
|
346
|
+
const world = mesh.matrixWorld;
|
|
347
|
+
const heads = bones.map((bone) => bone.matrixWorld.elements);
|
|
348
|
+
for (let index = 0, count = position.count; index < count; index += 1) {
|
|
349
|
+
skinned.getVertexPosition(index, vertex);
|
|
350
|
+
vertex.applyMatrix4(world);
|
|
351
|
+
const dominant = dominantSkinIndex(skinIndex, skinWeight, index);
|
|
352
|
+
const bone = bones[dominant];
|
|
353
|
+
const head = heads[dominant];
|
|
354
|
+
if (bone === undefined || head === undefined) continue;
|
|
355
|
+
const radius = Math.hypot(vertex.x - head[12], vertex.y - head[13], vertex.z - head[14]);
|
|
356
|
+
if (radius > (radii.get(bone) ?? 0)) radii.set(bone, radius);
|
|
357
|
+
}
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function dominantSkinIndex(
|
|
362
|
+
indices: ReturnType<Mesh["geometry"]["getAttribute"]>,
|
|
363
|
+
weights: ReturnType<Mesh["geometry"]["getAttribute"]>,
|
|
364
|
+
vertex: number,
|
|
365
|
+
): number {
|
|
366
|
+
let dominant = indices.getX(vertex);
|
|
367
|
+
let bestWeight = weights.getX(vertex);
|
|
368
|
+
const secondWeight = weights.getY(vertex);
|
|
369
|
+
if (secondWeight > bestWeight) {
|
|
370
|
+
bestWeight = secondWeight;
|
|
371
|
+
dominant = indices.getY(vertex);
|
|
372
|
+
}
|
|
373
|
+
const thirdWeight = weights.getZ(vertex);
|
|
374
|
+
if (thirdWeight > bestWeight) {
|
|
375
|
+
bestWeight = thirdWeight;
|
|
376
|
+
dominant = indices.getZ(vertex);
|
|
377
|
+
}
|
|
378
|
+
if (weights.getW(vertex) > bestWeight) dominant = indices.getW(vertex);
|
|
379
|
+
return dominant;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function getEnvelope(
|
|
383
|
+
root: Object3D,
|
|
384
|
+
requestedMeshes: readonly Object3D[] | undefined,
|
|
385
|
+
): IPosedBoundsEnvelope {
|
|
386
|
+
if (requestedMeshes !== undefined && requestedMeshes.length === 0) {
|
|
387
|
+
throw new Error("posedBounds meshes must contain at least one Object3D.");
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
const selection = requestedMeshes === undefined ? "default" : "explicit";
|
|
391
|
+
const cached = posedBoundsCache.get(root);
|
|
392
|
+
if (cached !== undefined) {
|
|
393
|
+
for (const envelope of cached) {
|
|
394
|
+
if (envelope.selection !== selection) continue;
|
|
395
|
+
if (selection === "default") return envelope;
|
|
396
|
+
if (requestedMeshes !== undefined && sameMeshes(envelope.meshes, requestedMeshes)) {
|
|
397
|
+
return envelope;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const meshes = requestedMeshes === undefined ? collectMeshes(root) : [...requestedMeshes];
|
|
403
|
+
if (meshes.length === 0) throw new Error("posedBounds could not find any mesh geometry.");
|
|
404
|
+
const envelope = createEnvelope(root, meshes, selection);
|
|
405
|
+
if (cached === undefined) posedBoundsCache.set(root, [envelope]);
|
|
406
|
+
else cached.push(envelope);
|
|
407
|
+
return envelope;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Cheap world-space bounds for a posed model.
|
|
412
|
+
*
|
|
413
|
+
* The first call pays the precise vertex walk to build a conservative skin envelope. Later calls
|
|
414
|
+
* read one world-matrix translation per contributing bone and allocate nothing. The returned
|
|
415
|
+
* object is cached for the root and updated in place; copy it if it must outlive the next call.
|
|
416
|
+
*/
|
|
417
|
+
export function posedBounds(root: Object3D, meshes?: readonly Object3D[]): IThreePoseBounds {
|
|
418
|
+
const envelope = getEnvelope(root, meshes);
|
|
419
|
+
root.updateWorldMatrix(true, true);
|
|
420
|
+
rawBounds(envelope, envelope.raw);
|
|
421
|
+
const raw = envelope.raw;
|
|
422
|
+
const min = envelope.result.min;
|
|
423
|
+
const max = envelope.result.max;
|
|
424
|
+
const size = envelope.result.size;
|
|
425
|
+
min[0] = raw.minX;
|
|
426
|
+
min[1] = raw.minY + envelope.biasY;
|
|
427
|
+
min[2] = raw.minZ;
|
|
428
|
+
max[0] = raw.maxX;
|
|
429
|
+
max[1] = raw.maxY + envelope.biasY;
|
|
430
|
+
max[2] = raw.maxZ;
|
|
431
|
+
size[0] = max[0] - min[0];
|
|
432
|
+
size[1] = max[1] - min[1];
|
|
433
|
+
size[2] = max[2] - min[2];
|
|
434
|
+
return envelope.result;
|
|
435
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-boundary counters, default-off, on the frame budget's own record.
|
|
3
|
+
*
|
|
4
|
+
* Two of the three hypotheses about the render phase's unattributed time are about crossings and
|
|
5
|
+
* bytes — "the V8↔host boundary dominates" and "a buffer write per object per pass dominates" —
|
|
6
|
+
* and neither can be answered by a JS CPU profile, because the time is spent on the other side of a
|
|
7
|
+
* call the profiler attributes to a single frame. This counts the calls instead.
|
|
8
|
+
*
|
|
9
|
+
* What it counts, precisely, because a counter that overstates itself is worse than none:
|
|
10
|
+
*
|
|
11
|
+
* - `hostCalls` — every WebGPU method invoked on the device's command encoders and on its queue.
|
|
12
|
+
* On a native host each of those is one crossing; in a browser it is one API call. It is not
|
|
13
|
+
* every crossing a frame makes: `mapAsync`, `getCurrentTexture` and the presentation path are
|
|
14
|
+
* issued by three and by the host outside these objects, and they are named here rather than
|
|
15
|
+
* folded in so the number can be checked against a host that counts its own.
|
|
16
|
+
* - `gpuBytes` — the byte count handed to `queue.writeBuffer`, exactly. Texture uploads are not
|
|
17
|
+
* included: a `writeTexture` size is a function of the destination's format, and estimating it
|
|
18
|
+
* from `data.byteLength` would be a number nobody could reconcile.
|
|
19
|
+
* - `jsAllocBytes` — the change in `performance.memory.usedJSHeapSize`, where the platform has it.
|
|
20
|
+
* Absent on a host without `performance.memory`, never zero: an unmeasurable allocation rate and
|
|
21
|
+
* a frame that allocated nothing are different facts.
|
|
22
|
+
*
|
|
23
|
+
* Wrapping every method of a command encoder is not free — it is one extra JS call per command, and
|
|
24
|
+
* this is why the counters ride the same opt-in flag as the spans rather than being always on.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One frame's boundary counts, plus the simulation tick it closed on.
|
|
29
|
+
*
|
|
30
|
+
* Every boundary field is absent when the platform cannot report it. `simulationTick` is not a
|
|
31
|
+
* platform reading: it is the loop's fixed-step count, absent when the caller fed none, and it
|
|
32
|
+
* rides here so a plain frame-budget window can be joined to the simulation without the spans flag.
|
|
33
|
+
*/
|
|
34
|
+
export interface IFrameCounters {
|
|
35
|
+
readonly gpuBytes?: number;
|
|
36
|
+
readonly hostCalls?: number;
|
|
37
|
+
readonly jsAllocBytes?: number;
|
|
38
|
+
/** The loop's fixed-step count at the frame's end; absent when the caller did not feed one. */
|
|
39
|
+
readonly simulationTick?: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The slice of a WebGPU device this reads. Structural, so a test can stand in a fake. */
|
|
43
|
+
export interface ICounterDevice {
|
|
44
|
+
createCommandEncoder?(): unknown;
|
|
45
|
+
createRenderBundleEncoder?(): unknown;
|
|
46
|
+
queue?: unknown;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Methods never wrapped: lifecycle and events. `constructor` is skipped by name in the walk rather
|
|
51
|
+
* than listed here, because an object literal's `constructor` key collides with `Object`'s own.
|
|
52
|
+
*/
|
|
53
|
+
const SKIP: Record<string, true> = {
|
|
54
|
+
addEventListener: true,
|
|
55
|
+
destroy: true,
|
|
56
|
+
dispatchEvent: true,
|
|
57
|
+
onuncapturederror: true,
|
|
58
|
+
removeEventListener: true,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Every function-valued method name on an object and its prototype chain.
|
|
63
|
+
*
|
|
64
|
+
* Enumerated rather than listed because the set of encoder methods is the WebGPU spec's, and a
|
|
65
|
+
* hard-coded list silently stops counting the day a method is added.
|
|
66
|
+
*/
|
|
67
|
+
function methodNames(target: object): string[] {
|
|
68
|
+
const names: string[] = [];
|
|
69
|
+
const seen = new Set<string>();
|
|
70
|
+
let node: object | null = target;
|
|
71
|
+
while (node !== null && node !== Object.prototype) {
|
|
72
|
+
for (const name of Object.getOwnPropertyNames(node)) {
|
|
73
|
+
if (name === "constructor" || seen.has(name) || SKIP[name] === true) continue;
|
|
74
|
+
seen.add(name);
|
|
75
|
+
// Read the descriptor, never the property. A WebGPU interface puts `label`, `queue`,
|
|
76
|
+
// `features`, `limits` and `lost` on its prototype as accessors, and invoking one of those
|
|
77
|
+
// getters with the prototype as `this` throws `TypeError: Illegal invocation` in Chrome —
|
|
78
|
+
// measured, as a blank page with the flag on. A descriptor read cannot run user code, so
|
|
79
|
+
// an accessor is simply not a method and is skipped.
|
|
80
|
+
const descriptor = Object.getOwnPropertyDescriptor(node, name);
|
|
81
|
+
if (descriptor === undefined || typeof descriptor.value !== "function") continue;
|
|
82
|
+
names.push(name);
|
|
83
|
+
}
|
|
84
|
+
node = Object.getPrototypeOf(node) as object | null;
|
|
85
|
+
}
|
|
86
|
+
return names;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The function stored at `name` on `owner` or its prototype chain, found without reading the
|
|
91
|
+
* property. Returns `undefined` for an accessor, which is never a method worth counting.
|
|
92
|
+
*/
|
|
93
|
+
function resolveMethod(owner: object, name: string): ((...args: unknown[]) => unknown) | undefined {
|
|
94
|
+
let node: object | null = owner;
|
|
95
|
+
while (node !== null) {
|
|
96
|
+
const descriptor = Object.getOwnPropertyDescriptor(node, name);
|
|
97
|
+
if (descriptor !== undefined)
|
|
98
|
+
return typeof descriptor.value === "function"
|
|
99
|
+
? (descriptor.value as (...args: unknown[]) => unknown)
|
|
100
|
+
: undefined;
|
|
101
|
+
node = Object.getPrototypeOf(node) as object | null;
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The WebGPU device behind a raw three renderer, when the backend exposes one. */
|
|
107
|
+
export function counterDeviceOf(raw: unknown): unknown {
|
|
108
|
+
if (typeof raw !== "object" || raw === null || !("backend" in raw)) return undefined;
|
|
109
|
+
const backend = raw.backend;
|
|
110
|
+
if (typeof backend !== "object" || backend === null || !("device" in backend)) return undefined;
|
|
111
|
+
return backend.device;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Counts the frame's boundary crossings and bytes, and reads what the platform will say about
|
|
116
|
+
* allocation. Install once per renderer; `read` returns the counts since the previous `read`.
|
|
117
|
+
*/
|
|
118
|
+
export class FrameCounters {
|
|
119
|
+
#hostCalls = 0;
|
|
120
|
+
#gpuBytes = 0;
|
|
121
|
+
#lastHeapBytes: number | undefined;
|
|
122
|
+
readonly #restore: (() => void)[] = [];
|
|
123
|
+
readonly #wrappedEncoders = new WeakSet<object>();
|
|
124
|
+
|
|
125
|
+
private constructor(device: ICounterDevice) {
|
|
126
|
+
// A new encoder per pass, so an encoder's methods are wrapped the first time it is seen rather
|
|
127
|
+
// than once at install: a wrapper installed on one encoder would miss every later pass.
|
|
128
|
+
this.#wrap(device, ["createCommandEncoder", "createRenderBundleEncoder"], (_name, result) => {
|
|
129
|
+
if (typeof result === "object" && result !== null) this.#wrapEncoder(result);
|
|
130
|
+
});
|
|
131
|
+
const queue = device.queue;
|
|
132
|
+
if (typeof queue === "object" && queue !== null)
|
|
133
|
+
this.#wrap(queue, methodNames(queue), undefined);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Installs the counters on a device, or answers `undefined` when it has nothing to count. */
|
|
137
|
+
static install(device: unknown): FrameCounters | undefined {
|
|
138
|
+
if (typeof device !== "object" || device === null) return undefined;
|
|
139
|
+
const counterDevice = device as ICounterDevice;
|
|
140
|
+
if (typeof counterDevice.queue !== "object" || counterDevice.queue === null) return undefined;
|
|
141
|
+
return new FrameCounters(counterDevice);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
#wrapEncoder(encoder: object): void {
|
|
145
|
+
if (this.#wrappedEncoders.has(encoder)) return;
|
|
146
|
+
this.#wrappedEncoders.add(encoder);
|
|
147
|
+
this.#wrap(encoder, methodNames(encoder), undefined);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#wrap(
|
|
151
|
+
owner: object,
|
|
152
|
+
names: readonly string[],
|
|
153
|
+
after: ((name: string, result: unknown) => void) | undefined,
|
|
154
|
+
): void {
|
|
155
|
+
const target = owner as Record<string, unknown>;
|
|
156
|
+
for (const name of names) {
|
|
157
|
+
// Same rule on the way in: resolve the function through descriptors along the chain rather
|
|
158
|
+
// than through a property read, so an accessor anywhere on the chain cannot be invoked.
|
|
159
|
+
const original = resolveMethod(owner, name);
|
|
160
|
+
if (original === undefined) continue;
|
|
161
|
+
const wrapped = (...args: unknown[]): unknown => {
|
|
162
|
+
this.#hostCalls += 1;
|
|
163
|
+
if (name === "writeBuffer") {
|
|
164
|
+
// `writeBuffer(buffer, offset, data, dataOffset, size)`; `size` defaults to the rest of
|
|
165
|
+
// the source, exactly as the spec says, so the count matches the bytes the driver sees.
|
|
166
|
+
const data = args[2];
|
|
167
|
+
const dataOffset = typeof args[3] === "number" ? args[3] : 0;
|
|
168
|
+
const size = typeof args[4] === "number" ? args[4] : undefined;
|
|
169
|
+
const available =
|
|
170
|
+
typeof data === "object" &&
|
|
171
|
+
data !== null &&
|
|
172
|
+
"byteLength" in data &&
|
|
173
|
+
typeof data.byteLength === "number"
|
|
174
|
+
? data.byteLength
|
|
175
|
+
: 0;
|
|
176
|
+
this.#gpuBytes += size ?? Math.max(0, available - dataOffset);
|
|
177
|
+
}
|
|
178
|
+
const result = original.apply(owner, args);
|
|
179
|
+
after?.(name, result);
|
|
180
|
+
return result;
|
|
181
|
+
};
|
|
182
|
+
try {
|
|
183
|
+
target[name] = wrapped;
|
|
184
|
+
this.#restore.push(() => {
|
|
185
|
+
target[name] = original;
|
|
186
|
+
});
|
|
187
|
+
} catch {
|
|
188
|
+
// A native binding may expose a non-writable method. Losing that one from the count is a
|
|
189
|
+
// smaller error than throwing on a diagnostic install, and the report says how many were
|
|
190
|
+
// counted rather than claiming completeness.
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The counts since the previous read. */
|
|
196
|
+
read(): IFrameCounters {
|
|
197
|
+
const counters: IFrameCounters = {
|
|
198
|
+
gpuBytes: this.#gpuBytes,
|
|
199
|
+
hostCalls: this.#hostCalls,
|
|
200
|
+
};
|
|
201
|
+
this.#hostCalls = 0;
|
|
202
|
+
this.#gpuBytes = 0;
|
|
203
|
+
const heap = readHeapBytes();
|
|
204
|
+
if (heap !== undefined) {
|
|
205
|
+
const previous = this.#lastHeapBytes;
|
|
206
|
+
this.#lastHeapBytes = heap;
|
|
207
|
+
if (previous !== undefined && heap >= previous)
|
|
208
|
+
return { ...counters, jsAllocBytes: heap - previous };
|
|
209
|
+
return counters;
|
|
210
|
+
}
|
|
211
|
+
return counters;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Removes every wrapper. A game that disposes its renderer must not leave the counters behind. */
|
|
215
|
+
uninstall(): void {
|
|
216
|
+
for (let index = this.#restore.length - 1; index >= 0; index -= 1) this.#restore[index]?.();
|
|
217
|
+
this.#restore.length = 0;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The heap the platform will admit to, in bytes.
|
|
223
|
+
*
|
|
224
|
+
* Only Chromium publishes `performance.memory`, and only when its precise-memory flag is on; a
|
|
225
|
+
* host without it answers `undefined` and the field stays absent rather than reading zero.
|
|
226
|
+
*/
|
|
227
|
+
function readHeapBytes(): number | undefined {
|
|
228
|
+
const perf = globalThis.performance as { memory?: { usedJSHeapSize?: unknown } } | undefined;
|
|
229
|
+
const used = perf?.memory?.usedJSHeapSize;
|
|
230
|
+
return typeof used === "number" && Number.isFinite(used) ? used : undefined;
|
|
231
|
+
}
|