@threenative/core 0.3.11 → 0.3.12
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 +1096 -26
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-CWuFRT7G.d.ts} +1 -1
- package/dist/chunk-2Z44XSI2.js +35 -0
- package/dist/chunk-5O73CDMR.js +1849 -0
- package/dist/chunk-6KLNISRJ.js +153 -0
- package/dist/chunk-7UUT3TY5.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DBELTTTM.js +615 -0
- package/dist/chunk-GMHATRPW.js +7701 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-N5LRP4BR.js +220 -0
- package/dist/chunk-OKXUCASC.js +2238 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-VAZM36TV.js +497 -0
- package/dist/{game-DzMcitDO.d.ts → game-D7pAzZUY.d.ts} +15 -3
- package/dist/hot.d.ts +3 -3
- package/dist/hot.js +2 -46
- package/dist/index.d.ts +329 -302
- package/dist/index.js +10566 -22552
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-B6BMqS1r.d.ts} +33 -3
- package/dist/playtest.d.ts +3 -3
- package/dist/playtest.js +24 -75
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-DvFVoAgj.d.ts} +13 -4
- package/dist/spells/index.d.ts +643 -0
- package/dist/spells/index.js +1558 -0
- package/dist/spells/ultra/index.d.ts +1600 -0
- package/dist/spells/ultra/index.js +6804 -0
- package/dist/terrain-jobs-worker.js +1 -123
- package/dist/two-sided-foliage-W523J3UM.js +1 -0
- package/dist/ui-layer.js +5 -384
- package/dist/worker.js +4 -41
- package/dist/world.d.ts +725 -259
- package/dist/world.js +16714 -18284
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +142 -47
- package/mcp/servers.mjs +9 -2
- package/mcp/terrain.mjs +8 -0
- package/package.json +14 -6
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/assets.ts +9 -2
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/frame-budget.ts +31 -20
- package/src/game-dev-tools.ts +93 -0
- package/src/game-frame-loop.ts +995 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1001 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +253 -1935
- package/src/index.ts +27 -2
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +24 -0
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quality.ts +10 -5
- package/src/look/world-environment.ts +76 -9
- package/src/loop.ts +29 -0
- package/src/model-lod.ts +42 -3
- package/src/particle-kinds.ts +515 -0
- package/src/picking.ts +17 -3
- package/src/playtest.ts +27 -1
- package/src/projection-apply.ts +4 -1
- package/src/projection-plan.ts +9 -6
- package/src/render/mesh-pool.ts +1 -1
- package/src/render/resolution-subrect.ts +2 -0
- package/src/render/rough-diffuse.ts +46 -12
- package/src/render/soft-shadow.ts +27 -12
- package/src/render/virtual-shadow-adaptive.ts +55 -0
- package/src/render/virtual-shadow-casters.ts +175 -0
- package/src/render/virtual-shadow-layers.ts +76 -0
- package/src/render/virtual-shadow-level.ts +224 -0
- package/src/render/virtual-shadow-markers.ts +193 -0
- package/src/render/virtual-shadow-options.ts +370 -0
- package/src/render/virtual-shadow-pages.ts +5 -0
- package/src/render/virtual-shadow.ts +213 -992
- package/src/render/world-impostor-surface.ts +293 -34
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer.ts +92 -37
- package/src/snow-field.ts +1 -1
- package/src/spells/cast-pool.ts +361 -0
- package/src/spells/index.ts +177 -0
- package/src/spells/mechanisms.ts +295 -0
- package/src/spells/reaction-field.ts +415 -0
- package/src/spells/spell-lights.ts +139 -0
- package/src/spells/spell-world.ts +187 -0
- package/src/spells/spell.ts +397 -0
- package/src/spells/spells.ts +283 -0
- package/src/spells/ultra/engine/camera.ts +184 -0
- package/src/spells/ultra/engine/quality.ts +96 -0
- package/src/spells/ultra/index.ts +52 -0
- package/src/spells/ultra/main.ts +305 -0
- package/src/spells/ultra/scene/arenaMaterial.ts +325 -0
- package/src/spells/ultra/spells/cast.ts +233 -0
- package/src/spells/ultra/spells/fire.ts +1267 -0
- package/src/spells/ultra/vfx/FluidVolume.ts +816 -0
- package/src/spells/ultra/vfx/FluidVolume.tsl.ts +855 -0
- package/src/spells/ultra/vfx/FxTarget.ts +61 -0
- package/src/spells/ultra/vfx/GroundField.ts +1399 -0
- package/src/spells/ultra/vfx/Particles.ts +1475 -0
- package/src/spells/ultra/vfx/Ribbons.ts +645 -0
- package/src/spells/ultra/vfx/Shockwaves.ts +286 -0
- package/src/spells/ultra/vfx/Volumes.ts +489 -0
- package/src/spells/ultra/vfx/common.ts +1195 -0
- package/src/spells/ultra/vfx/noise.ts +441 -0
- package/src/startup-readiness.ts +128 -34
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/world-bundle-safety.ts +401 -0
- package/src/world-cell-batches.ts +1216 -0
- package/src/world-cell-chunks.ts +1132 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1449 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +189 -0
- package/src/world-cell-placements.ts +694 -0
- package/src/world-cell-prewarm.ts +557 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +714 -0
- package/src/world-cell-render-main-cull.ts +484 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +246 -0
- package/src/world-cell-render.ts +1493 -0
- package/src/world-cell-residency.ts +745 -0
- package/src/world-cells.ts +1318 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +163 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +510 -0
- package/src/world-gpu-scene.ts +214 -2264
- package/src/world-gpu-shadow.ts +190 -0
- package/src/world-gpu-types.ts +381 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +106 -0
- package/src/world-noise.ts +241 -0
- package/src/world-package.ts +42 -0
- package/src/world-region.ts +1 -1
- package/src/world-terrain-splat.ts +365 -149
- package/src/world-tile-budget.ts +256 -0
- package/src/world-tile-geometry.ts +469 -0
- package/src/world-tile-lod.ts +408 -0
- package/src/world-tile-observation.ts +566 -0
- package/src/world-tile-stitch.ts +166 -0
- package/src/world-tiles.ts +170 -1745
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +35 -741
|
@@ -0,0 +1,1849 @@
|
|
|
1
|
+
import { Vector3, Sphere, Mesh, BufferAttribute, BufferGeometry, InstancedMesh, Object3D, Group, Box3, Matrix4, Frustum } from 'three';
|
|
2
|
+
|
|
3
|
+
// src/clustered-mesh.ts
|
|
4
|
+
var MIN_DISTANCE = 1e-4;
|
|
5
|
+
var DEFAULT_ERROR_PIXELS = 1;
|
|
6
|
+
var RECUT_FRACTION = 1e-3;
|
|
7
|
+
function pixelsPerUnit(camera, viewportHeight) {
|
|
8
|
+
const perspective = camera;
|
|
9
|
+
if (perspective.isPerspectiveCamera !== true || perspective.fov === void 0)
|
|
10
|
+
throw new Error(
|
|
11
|
+
"ClusteredMesh needs a perspective camera: a screen-space error has no meaning without one."
|
|
12
|
+
);
|
|
13
|
+
if (!(viewportHeight > 0))
|
|
14
|
+
throw new Error(`ClusteredMesh needs a positive viewport height, got ${viewportHeight}.`);
|
|
15
|
+
return viewportHeight / (2 * Math.tan(perspective.fov * Math.PI / 360));
|
|
16
|
+
}
|
|
17
|
+
function projectedError(error, spheres, cluster, camera, scale) {
|
|
18
|
+
const dx = camera.x - spheres[cluster * 4];
|
|
19
|
+
const dy = camera.y - spheres[cluster * 4 + 1];
|
|
20
|
+
const dz = camera.z - spheres[cluster * 4 + 2];
|
|
21
|
+
const distance = Math.sqrt(dx * dx + dy * dy + dz * dz) - spheres[cluster * 4 + 3];
|
|
22
|
+
return error * scale / Math.max(distance, MIN_DISTANCE);
|
|
23
|
+
}
|
|
24
|
+
function selectClusterCut(table, camera, scale, errorPixels, out) {
|
|
25
|
+
const clusters = table.ranges.length / 2;
|
|
26
|
+
let selected = 0;
|
|
27
|
+
for (let cluster = 0; cluster < clusters; cluster += 1) {
|
|
28
|
+
const own = projectedError(
|
|
29
|
+
table.errors[cluster * 2],
|
|
30
|
+
table.sourceSpheres,
|
|
31
|
+
cluster,
|
|
32
|
+
camera,
|
|
33
|
+
scale
|
|
34
|
+
);
|
|
35
|
+
if (own > errorPixels) continue;
|
|
36
|
+
const parent = projectedError(
|
|
37
|
+
table.errors[cluster * 2 + 1],
|
|
38
|
+
table.parentSpheres,
|
|
39
|
+
cluster,
|
|
40
|
+
camera,
|
|
41
|
+
scale
|
|
42
|
+
);
|
|
43
|
+
if (parent <= errorPixels) continue;
|
|
44
|
+
if (selected >= out.length)
|
|
45
|
+
throw new Error(
|
|
46
|
+
`ClusteredMesh selected more than the ${out.length} clusters level 0 holds; the table is inconsistent.`
|
|
47
|
+
);
|
|
48
|
+
out[selected] = cluster;
|
|
49
|
+
selected += 1;
|
|
50
|
+
}
|
|
51
|
+
return selected;
|
|
52
|
+
}
|
|
53
|
+
function finestIndexCount(table) {
|
|
54
|
+
let count = 0;
|
|
55
|
+
for (let cluster = 0; cluster * 2 < table.ranges.length; cluster += 1)
|
|
56
|
+
if (table.errors[cluster * 2] === 0)
|
|
57
|
+
count += table.ranges[cluster * 2 + 1];
|
|
58
|
+
return count;
|
|
59
|
+
}
|
|
60
|
+
var ClusteredMesh = class _ClusteredMesh extends Mesh {
|
|
61
|
+
/** The baked payload. The bake is the only thing that writes it; a copy takes the source's. */
|
|
62
|
+
table;
|
|
63
|
+
/** Screen-space error budget in pixels. Writable — it is the game's call. */
|
|
64
|
+
errorPixels;
|
|
65
|
+
/** Camera movement, in the mesh's own units, below which the previous cut is kept. */
|
|
66
|
+
recutDistance;
|
|
67
|
+
#cut;
|
|
68
|
+
#selection;
|
|
69
|
+
#cameraLocal = new Vector3();
|
|
70
|
+
#lastCut = new Vector3();
|
|
71
|
+
#drawnClusters = 0;
|
|
72
|
+
#drawnIndices = 0;
|
|
73
|
+
#hasCut = false;
|
|
74
|
+
#lastScale = 0;
|
|
75
|
+
#lastErrorPixels = 0;
|
|
76
|
+
constructor(geometry, surface, table, options = {}) {
|
|
77
|
+
super(geometry, surface);
|
|
78
|
+
assertTable(table);
|
|
79
|
+
this.errorPixels = options.errorPixels ?? DEFAULT_ERROR_PIXELS;
|
|
80
|
+
this.recutDistance = options.recutDistance ?? radiusOf(table) * RECUT_FRACTION;
|
|
81
|
+
this.#load(table, geometry);
|
|
82
|
+
this.#compact(this.#finestCut());
|
|
83
|
+
}
|
|
84
|
+
/** Points this mesh at `table`, and installs its cut on the geometry it is going to draw. */
|
|
85
|
+
#load(table, geometry) {
|
|
86
|
+
this.table = table;
|
|
87
|
+
this.#cut = new Uint32Array(finestIndexCount(table));
|
|
88
|
+
this.#selection = new Uint32Array(table.ranges.length / 2);
|
|
89
|
+
geometry.setIndex(new BufferAttribute(this.#cut, 1));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* A second mesh over the same body, cut for its own place in the scene.
|
|
93
|
+
*
|
|
94
|
+
* three's `Object3D.clone` builds through `new this.constructor()`, and this class's constructor
|
|
95
|
+
* wants a table — so every game that stood a second copy of a cooked model where it wanted a
|
|
96
|
+
* statue died in `assertTable` before drawing anything. The arguments are the source's own,
|
|
97
|
+
* which is what a copy is.
|
|
98
|
+
*/
|
|
99
|
+
clone(recursive = true) {
|
|
100
|
+
return new _ClusteredMesh(withOwnCut(this.geometry), this.material, this.table, {
|
|
101
|
+
errorPixels: this.errorPixels,
|
|
102
|
+
recutDistance: this.recutDistance
|
|
103
|
+
}).copy(this, recursive);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Becomes a copy of `source`, sharing the bake and the vertices and keeping a cut of its own.
|
|
107
|
+
*
|
|
108
|
+
* `Mesh.copy` shares the geometry outright, which this class cannot let it do: the cut lives in
|
|
109
|
+
* the geometry's index buffer, so two meshes on one geometry would overwrite each other's index
|
|
110
|
+
* every frame and both would draw whichever spoke last. The copy takes the vertex attributes by
|
|
111
|
+
* reference instead — one copy of them on the GPU, because three keys its attributes on the
|
|
112
|
+
* attribute — and the shared table, which is frozen at bake time and cannot go stale.
|
|
113
|
+
*/
|
|
114
|
+
copy(source, recursive = true) {
|
|
115
|
+
super.copy(source, recursive);
|
|
116
|
+
if (!(source instanceof _ClusteredMesh)) return this;
|
|
117
|
+
const geometry = withOwnCut(source.geometry);
|
|
118
|
+
this.#load(source.table, geometry);
|
|
119
|
+
this.geometry = geometry;
|
|
120
|
+
this.errorPixels = source.errorPixels;
|
|
121
|
+
this.recutDistance = source.recutDistance;
|
|
122
|
+
this.#hasCut = false;
|
|
123
|
+
this.#compact(this.#finestCut());
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/** Every cluster the bake produced from the source triangles — the mesh as authored. */
|
|
127
|
+
#finestCut() {
|
|
128
|
+
let selected = 0;
|
|
129
|
+
for (let cluster = 0; cluster * 2 < this.table.ranges.length; cluster += 1)
|
|
130
|
+
if (this.table.errors[cluster * 2] === 0) {
|
|
131
|
+
this.#selection[selected] = cluster;
|
|
132
|
+
selected += 1;
|
|
133
|
+
}
|
|
134
|
+
return selected;
|
|
135
|
+
}
|
|
136
|
+
/** Clusters in the current cut. */
|
|
137
|
+
get drawnClusters() {
|
|
138
|
+
return this.#drawnClusters;
|
|
139
|
+
}
|
|
140
|
+
/** Triangles the current cut submits. */
|
|
141
|
+
get drawnTriangles() {
|
|
142
|
+
return this.#drawnIndices / 3;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Chooses this frame's cut and compacts it into one index range.
|
|
146
|
+
*
|
|
147
|
+
* Called by the game before it renders, not from `onBeforeRender`: an empty cut has to skip the
|
|
148
|
+
* draw rather than submit a zero-count one, and by the time three calls `onBeforeRender` the draw
|
|
149
|
+
* is already on the list. A mesh this leaves invisible is made visible again by the next call,
|
|
150
|
+
* which is why the call belongs in the frame loop rather than in the renderer.
|
|
151
|
+
*
|
|
152
|
+
* @returns triangles the mesh will draw.
|
|
153
|
+
*/
|
|
154
|
+
update(camera, viewportHeight) {
|
|
155
|
+
const scale = pixelsPerUnit(camera, viewportHeight);
|
|
156
|
+
this.updateWorldMatrix(true, false);
|
|
157
|
+
camera.updateWorldMatrix(true, false);
|
|
158
|
+
this.#cameraLocal.setFromMatrixPosition(camera.matrixWorld);
|
|
159
|
+
this.worldToLocal(this.#cameraLocal);
|
|
160
|
+
const moved = this.#cameraLocal.distanceTo(this.#lastCut);
|
|
161
|
+
const settled = this.#hasCut && moved <= this.recutDistance && scale === this.#lastScale && this.errorPixels === this.#lastErrorPixels;
|
|
162
|
+
if (settled) return this.drawnTriangles;
|
|
163
|
+
this.#lastCut.copy(this.#cameraLocal);
|
|
164
|
+
this.#lastScale = scale;
|
|
165
|
+
this.#lastErrorPixels = this.errorPixels;
|
|
166
|
+
this.#hasCut = true;
|
|
167
|
+
this.#compact(
|
|
168
|
+
selectClusterCut(this.table, this.#cameraLocal, scale, this.errorPixels, this.#selection)
|
|
169
|
+
);
|
|
170
|
+
return this.drawnTriangles;
|
|
171
|
+
}
|
|
172
|
+
#compact(selected) {
|
|
173
|
+
let cursor = 0;
|
|
174
|
+
for (let slot = 0; slot < selected; slot += 1) {
|
|
175
|
+
const cluster = this.#selection[slot];
|
|
176
|
+
const start = this.table.ranges[cluster * 2];
|
|
177
|
+
const count = this.table.ranges[cluster * 2 + 1];
|
|
178
|
+
this.#cut.set(this.table.indices.subarray(start, start + count), cursor);
|
|
179
|
+
cursor += count;
|
|
180
|
+
}
|
|
181
|
+
this.#drawnClusters = selected;
|
|
182
|
+
this.#drawnIndices = cursor;
|
|
183
|
+
const index = this.geometry.getIndex();
|
|
184
|
+
if (index !== null && cursor > 0) {
|
|
185
|
+
index.addUpdateRange(0, cursor);
|
|
186
|
+
index.needsUpdate = true;
|
|
187
|
+
}
|
|
188
|
+
this.geometry.setDrawRange(0, cursor);
|
|
189
|
+
this.visible = cursor > 0;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
function withOwnCut(geometry) {
|
|
193
|
+
const shell = new BufferGeometry();
|
|
194
|
+
for (const [name, attribute] of Object.entries(geometry.attributes))
|
|
195
|
+
shell.setAttribute(name, attribute);
|
|
196
|
+
shell.boundingSphere = geometry.boundingSphere;
|
|
197
|
+
shell.boundingBox = geometry.boundingBox;
|
|
198
|
+
shell.groups = [...geometry.groups];
|
|
199
|
+
return shell;
|
|
200
|
+
}
|
|
201
|
+
function radiusOf(table) {
|
|
202
|
+
let radius = 0;
|
|
203
|
+
for (let cluster = 0; cluster * 4 < table.bounds.length; cluster += 1)
|
|
204
|
+
radius = Math.max(radius, table.bounds[cluster * 4 + 3]);
|
|
205
|
+
return radius;
|
|
206
|
+
}
|
|
207
|
+
function assertTable(table) {
|
|
208
|
+
const clusters = table.ranges.length / 2;
|
|
209
|
+
if (clusters === 0 || table.ranges.length % 2 !== 0)
|
|
210
|
+
throw new Error(`ClusteredMesh needs at least one cluster, got ${table.ranges.length / 2}.`);
|
|
211
|
+
const widths = [
|
|
212
|
+
["bounds", 4],
|
|
213
|
+
["cones", 4],
|
|
214
|
+
["errors", 2],
|
|
215
|
+
["parentSpheres", 4],
|
|
216
|
+
["sourceSpheres", 4]
|
|
217
|
+
];
|
|
218
|
+
for (const [name, width] of widths) {
|
|
219
|
+
const array = table[name];
|
|
220
|
+
if (array.length !== clusters * width)
|
|
221
|
+
throw new Error(
|
|
222
|
+
`ClusteredMesh table.${name} holds ${array.length} values for ${clusters} clusters; it needs ${clusters * width}.`
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
for (let cluster = 0; cluster < clusters; cluster += 1) {
|
|
226
|
+
const start = table.ranges[cluster * 2];
|
|
227
|
+
const count = table.ranges[cluster * 2 + 1];
|
|
228
|
+
if (count === 0 || count % 3 !== 0 || start + count > table.indices.length)
|
|
229
|
+
throw new Error(
|
|
230
|
+
`ClusteredMesh cluster ${cluster} names indices ${start}..${start + count}, which the ${table.indices.length}-index buffer does not hold.`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
var TN_VIRTUAL_GEOMETRY = "TN_virtual_geometry";
|
|
235
|
+
function typedFloat(array) {
|
|
236
|
+
return array instanceof Float32Array ? array : Float32Array.from(array);
|
|
237
|
+
}
|
|
238
|
+
function typedUint(array) {
|
|
239
|
+
return array instanceof Uint32Array ? array : Uint32Array.from(array);
|
|
240
|
+
}
|
|
241
|
+
var TABLE_FIELDS = [
|
|
242
|
+
"clusterBounds",
|
|
243
|
+
"clusterCones",
|
|
244
|
+
"clusterErrors",
|
|
245
|
+
"clusterParentSpheres",
|
|
246
|
+
"clusterRanges",
|
|
247
|
+
"clusterSourceSpheres",
|
|
248
|
+
"indices"
|
|
249
|
+
];
|
|
250
|
+
async function readTable(parser, def) {
|
|
251
|
+
const missing = TABLE_FIELDS.filter((field) => typeof def[field] !== "number");
|
|
252
|
+
if (missing.length > 0)
|
|
253
|
+
throw new Error(
|
|
254
|
+
`TN_VIRTUAL_GEOMETRY_INCOMPLETE: the ${TN_VIRTUAL_GEOMETRY} payload is missing ${missing.join(", ")}; re-bake the model with the current asset pipeline.`
|
|
255
|
+
);
|
|
256
|
+
const accessor = async (index) => (await parser.getDependency("accessor", index)).array;
|
|
257
|
+
const [bounds, cones, errors, parentSpheres, ranges, sourceSpheres, indices] = await Promise.all([
|
|
258
|
+
accessor(def.clusterBounds),
|
|
259
|
+
accessor(def.clusterCones),
|
|
260
|
+
accessor(def.clusterErrors),
|
|
261
|
+
accessor(def.clusterParentSpheres),
|
|
262
|
+
accessor(def.clusterRanges),
|
|
263
|
+
accessor(def.clusterSourceSpheres),
|
|
264
|
+
accessor(def.indices)
|
|
265
|
+
]);
|
|
266
|
+
return {
|
|
267
|
+
bounds: typedFloat(bounds),
|
|
268
|
+
cones: typedFloat(cones),
|
|
269
|
+
errors: typedFloat(errors),
|
|
270
|
+
indices: typedUint(indices),
|
|
271
|
+
parentSpheres: typedFloat(parentSpheres),
|
|
272
|
+
ranges: typedUint(ranges),
|
|
273
|
+
sourceSpheres: typedFloat(sourceSpheres)
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
var VirtualGeometryPlugin = class {
|
|
277
|
+
constructor(parser) {
|
|
278
|
+
this.parser = parser;
|
|
279
|
+
}
|
|
280
|
+
parser;
|
|
281
|
+
name = TN_VIRTUAL_GEOMETRY;
|
|
282
|
+
async afterRoot(result) {
|
|
283
|
+
const swaps = [];
|
|
284
|
+
for (const [object, association] of this.parser.associations) {
|
|
285
|
+
const plain = object;
|
|
286
|
+
if (plain.isMesh !== true || plain.isSkinnedMesh === true)
|
|
287
|
+
continue;
|
|
288
|
+
const { meshes, primitives } = association;
|
|
289
|
+
if (meshes === void 0 || primitives === void 0) continue;
|
|
290
|
+
const def = this.parser.json.meshes?.[meshes]?.primitives?.[primitives]?.extensions?.[TN_VIRTUAL_GEOMETRY];
|
|
291
|
+
if (def === void 0) continue;
|
|
292
|
+
const clustered = new ClusteredMesh(
|
|
293
|
+
plain.geometry,
|
|
294
|
+
plain.material,
|
|
295
|
+
await readTable(this.parser, def)
|
|
296
|
+
);
|
|
297
|
+
clustered.name = plain.name;
|
|
298
|
+
clustered.position.copy(plain.position);
|
|
299
|
+
clustered.quaternion.copy(plain.quaternion);
|
|
300
|
+
clustered.scale.copy(plain.scale);
|
|
301
|
+
clustered.castShadow = plain.castShadow;
|
|
302
|
+
clustered.receiveShadow = plain.receiveShadow;
|
|
303
|
+
swaps.push({ clustered, plain });
|
|
304
|
+
}
|
|
305
|
+
for (const { clustered, plain } of swaps) {
|
|
306
|
+
const parent = plain.parent;
|
|
307
|
+
if (parent === null) continue;
|
|
308
|
+
parent.add(clustered);
|
|
309
|
+
parent.remove(plain);
|
|
310
|
+
this.parser.associations.set(
|
|
311
|
+
clustered,
|
|
312
|
+
this.parser.associations.get(plain) ?? { meshes: void 0, primitives: void 0 }
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
function isBatchRoot(object) {
|
|
318
|
+
const candidate = object.batch;
|
|
319
|
+
return typeof candidate?.update === "function";
|
|
320
|
+
}
|
|
321
|
+
var trackedRoots = /* @__PURE__ */ new WeakMap();
|
|
322
|
+
function isGraphNode(root) {
|
|
323
|
+
const candidate = root;
|
|
324
|
+
return typeof candidate.traverse === "function" && typeof candidate.addEventListener === "function" && typeof candidate.removeEventListener === "function";
|
|
325
|
+
}
|
|
326
|
+
function hookSubtree(tracking, subtree) {
|
|
327
|
+
subtree.traverse((node) => {
|
|
328
|
+
if (node instanceof ClusteredMesh || isBatchRoot(node)) tracking.items.add(node);
|
|
329
|
+
if (tracking.hooked.has(node)) return;
|
|
330
|
+
tracking.hooked.add(node);
|
|
331
|
+
const link = node;
|
|
332
|
+
if (typeof link.addEventListener !== "function") return;
|
|
333
|
+
link.addEventListener("childadded", tracking.onAdded);
|
|
334
|
+
link.addEventListener("childremoved", tracking.onRemoved);
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
function unhookSubtree(tracking, subtree) {
|
|
338
|
+
subtree.traverse((node) => {
|
|
339
|
+
tracking.items.delete(node);
|
|
340
|
+
if (!tracking.hooked.has(node)) return;
|
|
341
|
+
tracking.hooked.delete(node);
|
|
342
|
+
const link = node;
|
|
343
|
+
if (typeof link.removeEventListener !== "function") return;
|
|
344
|
+
link.removeEventListener("childadded", tracking.onAdded);
|
|
345
|
+
link.removeEventListener("childremoved", tracking.onRemoved);
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
function trackRoot(root) {
|
|
349
|
+
const existing = trackedRoots.get(root);
|
|
350
|
+
if (existing !== void 0) return existing;
|
|
351
|
+
const tracking = {
|
|
352
|
+
hooked: /* @__PURE__ */ new WeakSet(),
|
|
353
|
+
items: /* @__PURE__ */ new Set(),
|
|
354
|
+
onAdded: (event) => {
|
|
355
|
+
const child = event.child;
|
|
356
|
+
if (typeof child === "object" && child !== null && isGraphNode(child))
|
|
357
|
+
hookSubtree(tracking, child);
|
|
358
|
+
},
|
|
359
|
+
onRemoved: (event) => {
|
|
360
|
+
const child = event.child;
|
|
361
|
+
if (typeof child === "object" && child !== null && typeof child.traverse === "function")
|
|
362
|
+
unhookSubtree(tracking, child);
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
trackedRoots.set(root, tracking);
|
|
366
|
+
hookSubtree(tracking, root);
|
|
367
|
+
return tracking;
|
|
368
|
+
}
|
|
369
|
+
function trianglesOf(object, camera, viewportHeight) {
|
|
370
|
+
if (object instanceof ClusteredMesh) return object.update(camera, viewportHeight);
|
|
371
|
+
if (isBatchRoot(object)) return object.batch.update(camera, viewportHeight);
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
function updateClusteredMeshes(root, camera, viewportHeight) {
|
|
375
|
+
if (!isGraphNode(root)) {
|
|
376
|
+
let triangles2 = 0;
|
|
377
|
+
root.traverse((object) => {
|
|
378
|
+
triangles2 += trianglesOf(object, camera, viewportHeight);
|
|
379
|
+
});
|
|
380
|
+
return triangles2;
|
|
381
|
+
}
|
|
382
|
+
let triangles = 0;
|
|
383
|
+
for (const item of trackRoot(root).items) triangles += trianglesOf(item, camera, viewportHeight);
|
|
384
|
+
return triangles;
|
|
385
|
+
}
|
|
386
|
+
function lodPixelScale(camera, viewportHeight, depth) {
|
|
387
|
+
const like = camera;
|
|
388
|
+
if (!(viewportHeight > 0))
|
|
389
|
+
throw new Error(`AutoLOD needs a positive viewport height, got ${String(viewportHeight)}.`);
|
|
390
|
+
const zoom = like.zoom ?? 1;
|
|
391
|
+
if (like.isOrthographicCamera === true) {
|
|
392
|
+
const height = (like.top ?? 1) - (like.bottom ?? -1);
|
|
393
|
+
if (!(height > 0))
|
|
394
|
+
throw new Error("AutoLOD needs an orthographic frustum with a positive height.");
|
|
395
|
+
return viewportHeight * zoom / height;
|
|
396
|
+
}
|
|
397
|
+
if (!(depth > 0)) return Number.POSITIVE_INFINITY;
|
|
398
|
+
return pixelsPerUnit(camera, viewportHeight) * zoom / depth;
|
|
399
|
+
}
|
|
400
|
+
function projectedLodError(worldError, camera, viewportHeight, depth) {
|
|
401
|
+
if (worldError <= 0) return 0;
|
|
402
|
+
return worldError * lodPixelScale(camera, viewportHeight, depth);
|
|
403
|
+
}
|
|
404
|
+
var LOD_BIAS_KEY = /* @__PURE__ */ Symbol.for("threenative.lodBias");
|
|
405
|
+
var lodBiasHost = globalThis;
|
|
406
|
+
var lodBiasState = lodBiasHost[LOD_BIAS_KEY] ?? { value: 1 };
|
|
407
|
+
lodBiasHost[LOD_BIAS_KEY] = lodBiasState;
|
|
408
|
+
function setLodBias(bias) {
|
|
409
|
+
lodBiasState.value = Number.isFinite(bias) && bias >= 1 ? bias : 1;
|
|
410
|
+
}
|
|
411
|
+
function biasedLodDistance(distance) {
|
|
412
|
+
return distance * lodBiasState.value;
|
|
413
|
+
}
|
|
414
|
+
var nearest = new Vector3();
|
|
415
|
+
function conservativeViewDepth(camera, center, radius, nearPlane) {
|
|
416
|
+
nearest.copy(center).applyMatrix4(camera.matrixWorldInverse);
|
|
417
|
+
const depth = -nearest.z - Math.max(0, radius);
|
|
418
|
+
return { depth, degenerate: depth <= nearPlane };
|
|
419
|
+
}
|
|
420
|
+
function worldSphere(local, object, out = new Sphere().copy(local), matrix = object.matrixWorld) {
|
|
421
|
+
out.center.copy(local.center).applyMatrix4(matrix);
|
|
422
|
+
const scale = matrix.getMaxScaleOnAxis();
|
|
423
|
+
out.radius = local.radius * scale;
|
|
424
|
+
return out;
|
|
425
|
+
}
|
|
426
|
+
function selectLodLevel(absoluteErrors, current, budgetPixels, hysteresis, views) {
|
|
427
|
+
if (absoluteErrors.length === 0) return 0;
|
|
428
|
+
const last = absoluteErrors.length - 1;
|
|
429
|
+
if (!(budgetPixels > 0) || views.length === 0) return 0;
|
|
430
|
+
const clampedCurrent = Math.min(Math.max(Math.trunc(current), 0), last);
|
|
431
|
+
let target = 0;
|
|
432
|
+
outer: for (let index = last; index >= 1; index -= 1) {
|
|
433
|
+
const error = absoluteErrors[index];
|
|
434
|
+
for (const view of views) {
|
|
435
|
+
if (view.degenerate || view.finest === true) continue outer;
|
|
436
|
+
if (projectedLodError(error, view.camera, view.viewportHeight, view.depth) > budgetPixels) {
|
|
437
|
+
continue outer;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
target = index;
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
if (target <= clampedCurrent) return target;
|
|
444
|
+
const threshold = (1 - hysteresis) * budgetPixels;
|
|
445
|
+
for (const view of views) {
|
|
446
|
+
if (view.degenerate || view.finest === true) return clampedCurrent;
|
|
447
|
+
const error = absoluteErrors[target];
|
|
448
|
+
if (projectedLodError(error, view.camera, view.viewportHeight, view.depth) >= threshold)
|
|
449
|
+
return clampedCurrent;
|
|
450
|
+
}
|
|
451
|
+
return target;
|
|
452
|
+
}
|
|
453
|
+
var TN_DISCRETE_LOD = "TN_discrete_lod";
|
|
454
|
+
var DISCRETE_LOD_SCHEMA_VERSION = 1;
|
|
455
|
+
var DISCRETE_LOD_DEFAULT_ERROR_PIXELS = 1;
|
|
456
|
+
var DISCRETE_LOD_DEFAULT_HYSTERESIS = 0.15;
|
|
457
|
+
function numberArray(value, label) {
|
|
458
|
+
if (!Array.isArray(value) || value.some((entry) => typeof entry !== "number"))
|
|
459
|
+
throw new Error(`${label} must be an array of numbers.`);
|
|
460
|
+
return value;
|
|
461
|
+
}
|
|
462
|
+
async function readPendingLevels(parser, def) {
|
|
463
|
+
if (def.schemaVersion !== DISCRETE_LOD_SCHEMA_VERSION)
|
|
464
|
+
throw new Error(
|
|
465
|
+
`schema version ${String(def.schemaVersion)} is not ${String(DISCRETE_LOD_SCHEMA_VERSION)}.`
|
|
466
|
+
);
|
|
467
|
+
const indices = numberArray(def.indices, "indices");
|
|
468
|
+
const counts = numberArray(def.counts, "counts");
|
|
469
|
+
const errors = numberArray(def.errors, "errors");
|
|
470
|
+
const absoluteErrors = numberArray(def.absoluteErrors, "absoluteErrors");
|
|
471
|
+
if (indices.length !== counts.length || counts.length !== errors.length || counts.length !== absoluteErrors.length)
|
|
472
|
+
throw new Error("the level arrays are not parallel.");
|
|
473
|
+
if (typeof def.lod0Triangles !== "number" || !(def.lod0Triangles > 0))
|
|
474
|
+
throw new Error("lod0Triangles must be a positive number.");
|
|
475
|
+
const resolved = [];
|
|
476
|
+
for (const index of indices) {
|
|
477
|
+
const accessor = await parser.getDependency("accessor", index);
|
|
478
|
+
const array = accessor.array;
|
|
479
|
+
resolved.push(array instanceof Uint32Array ? array : Uint32Array.from(array));
|
|
480
|
+
}
|
|
481
|
+
let previous = def.lod0Triangles;
|
|
482
|
+
let previousError = -1;
|
|
483
|
+
for (let level = 0; level < resolved.length; level += 1) {
|
|
484
|
+
const triangles = Math.floor(resolved[level].length / 3);
|
|
485
|
+
const error = errors[level];
|
|
486
|
+
if (triangles <= 0 || triangles >= previous)
|
|
487
|
+
throw new Error(`level ${String(level)} is not a reduction.`);
|
|
488
|
+
if (!Number.isFinite(error) || error < previousError)
|
|
489
|
+
throw new Error(`level ${String(level)} has non-monotonic error.`);
|
|
490
|
+
previous = triangles;
|
|
491
|
+
previousError = error;
|
|
492
|
+
}
|
|
493
|
+
return { absoluteErrors, indices: resolved, lod0Triangles: def.lod0Triangles };
|
|
494
|
+
}
|
|
495
|
+
function drawTriangles(geometry) {
|
|
496
|
+
const drawn = geometry.index?.count ?? geometry.getAttribute("position")?.count ?? 0;
|
|
497
|
+
return Math.floor(drawn / 3);
|
|
498
|
+
}
|
|
499
|
+
var ModelLod = class {
|
|
500
|
+
#mesh;
|
|
501
|
+
#chain;
|
|
502
|
+
#policy;
|
|
503
|
+
#rung;
|
|
504
|
+
/** Only the authority drives selection and activation; every other member merely reports hidden. */
|
|
505
|
+
#authority;
|
|
506
|
+
#current = 0;
|
|
507
|
+
/** The join container, resolved once from the rung and the instance graph. */
|
|
508
|
+
#container = null;
|
|
509
|
+
#containerResolved = false;
|
|
510
|
+
constructor(mesh, chain, policy, rung, authority = false) {
|
|
511
|
+
this.#mesh = mesh;
|
|
512
|
+
this.#chain = chain;
|
|
513
|
+
this.#policy = policy;
|
|
514
|
+
this.#rung = rung;
|
|
515
|
+
this.#authority = authority;
|
|
516
|
+
}
|
|
517
|
+
get index() {
|
|
518
|
+
return this.#current;
|
|
519
|
+
}
|
|
520
|
+
/** The full-detail geometry the chain was built against. */
|
|
521
|
+
get base() {
|
|
522
|
+
return this.#chain.base;
|
|
523
|
+
}
|
|
524
|
+
get triangles() {
|
|
525
|
+
const rung = this.#rung;
|
|
526
|
+
if (rung?.isHidden(this.#mesh)) return this.#authority ? rung.triangles : 0;
|
|
527
|
+
return drawTriangles(this.#chain.levels[this.#current]);
|
|
528
|
+
}
|
|
529
|
+
update(camera, viewportHeight) {
|
|
530
|
+
const rung = this.#rung;
|
|
531
|
+
if (rung !== void 0 && this.#authority) {
|
|
532
|
+
this.#updateJoined(rung, camera, viewportHeight);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
const world = worldSphere(this.#chain.sphere, this.#mesh, void 0, this.#mesh.matrixWorld);
|
|
536
|
+
const { depth, degenerate } = conservativeViewDepth(
|
|
537
|
+
camera,
|
|
538
|
+
world.center,
|
|
539
|
+
world.radius,
|
|
540
|
+
camera.near ?? 0
|
|
541
|
+
);
|
|
542
|
+
const view = {
|
|
543
|
+
camera,
|
|
544
|
+
degenerate,
|
|
545
|
+
depth: biasedLodDistance(depth),
|
|
546
|
+
viewportHeight
|
|
547
|
+
};
|
|
548
|
+
const index = selectLodLevel(
|
|
549
|
+
this.#chain.errors,
|
|
550
|
+
this.#current,
|
|
551
|
+
this.#policy.maxPixelError / Math.max(this.#mesh.matrixWorld.getMaxScaleOnAxis(), Number.EPSILON),
|
|
552
|
+
this.#policy.hysteresis,
|
|
553
|
+
[view]
|
|
554
|
+
);
|
|
555
|
+
if (index === this.#current) return;
|
|
556
|
+
this.#current = index;
|
|
557
|
+
this.#mesh.geometry = this.#chain.levels[index];
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* The join selection. The rung replaces every member, so its bounds set the depth and the
|
|
561
|
+
* instance's container supplies the world transform the rung geometry was baked against.
|
|
562
|
+
*/
|
|
563
|
+
#updateJoined(rung, camera, viewportHeight) {
|
|
564
|
+
if (!this.#containerResolved) {
|
|
565
|
+
this.#container = rung.findContainer(this.#mesh);
|
|
566
|
+
this.#containerResolved = true;
|
|
567
|
+
}
|
|
568
|
+
const container = this.#container;
|
|
569
|
+
if (container === null) return;
|
|
570
|
+
const world = worldSphere(rung.sphere, container, void 0, container.matrixWorld);
|
|
571
|
+
const { depth, degenerate } = conservativeViewDepth(
|
|
572
|
+
camera,
|
|
573
|
+
world.center,
|
|
574
|
+
world.radius,
|
|
575
|
+
camera.near ?? 0
|
|
576
|
+
);
|
|
577
|
+
const errors = [...this.#chain.errors, rung.error];
|
|
578
|
+
const joinedIndex = this.#chain.errors.length;
|
|
579
|
+
const view = {
|
|
580
|
+
camera,
|
|
581
|
+
degenerate,
|
|
582
|
+
depth: biasedLodDistance(depth),
|
|
583
|
+
viewportHeight
|
|
584
|
+
};
|
|
585
|
+
const index = selectLodLevel(
|
|
586
|
+
errors,
|
|
587
|
+
this.#current,
|
|
588
|
+
this.#policy.maxPixelError / Math.max(container.matrixWorld.getMaxScaleOnAxis(), Number.EPSILON),
|
|
589
|
+
this.#policy.hysteresis,
|
|
590
|
+
[view]
|
|
591
|
+
);
|
|
592
|
+
if (index === joinedIndex) {
|
|
593
|
+
if (rung.activate(container)) {
|
|
594
|
+
this.#current = index;
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
this.#current = 0;
|
|
598
|
+
this.#mesh.geometry = this.#chain.levels[0];
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
rung.deactivate(container);
|
|
602
|
+
if (index !== this.#current) {
|
|
603
|
+
this.#current = index;
|
|
604
|
+
this.#mesh.geometry = this.#chain.levels[index];
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
/** Reverses an active joined rung when the mesh is removed from the graph. */
|
|
608
|
+
release() {
|
|
609
|
+
if (this.#rung !== void 0 && this.#authority && this.#container !== null)
|
|
610
|
+
this.#rung.deactivate(this.#container);
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
var JOIN_STATE_KEY = /* @__PURE__ */ Symbol.for("threenative.discreteLodJoinState");
|
|
614
|
+
var joinHost = globalThis;
|
|
615
|
+
var joinState = joinHost[JOIN_STATE_KEY] ?? {
|
|
616
|
+
proxies: /* @__PURE__ */ new WeakSet(),
|
|
617
|
+
owners: /* @__PURE__ */ new WeakMap()
|
|
618
|
+
};
|
|
619
|
+
joinHost[JOIN_STATE_KEY] = joinState;
|
|
620
|
+
var joinedProxies = joinState.proxies;
|
|
621
|
+
var joinedOwners = joinState.owners;
|
|
622
|
+
function setModelLodLayers(mesh, mask) {
|
|
623
|
+
mesh.layers.mask = mask;
|
|
624
|
+
joinedOwners.get(mesh)?.traverse((node) => {
|
|
625
|
+
if (node.isMesh === true) node.layers.mask = mask;
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
function isLodJoinProxy(object) {
|
|
629
|
+
return joinedProxies.has(object);
|
|
630
|
+
}
|
|
631
|
+
function isDeforming(mesh) {
|
|
632
|
+
return mesh.isSkinnedMesh === true || Object.keys(mesh.geometry.morphAttributes).length > 0;
|
|
633
|
+
}
|
|
634
|
+
function isAnimatedInContainer(mesh, container, animated) {
|
|
635
|
+
let current = mesh;
|
|
636
|
+
while (current !== null && current !== container) {
|
|
637
|
+
if (animated.has(current)) return true;
|
|
638
|
+
current = current.parent;
|
|
639
|
+
}
|
|
640
|
+
return false;
|
|
641
|
+
}
|
|
642
|
+
function relativeLocalMatrix(node, container) {
|
|
643
|
+
const matrix = new Matrix4();
|
|
644
|
+
let current = node;
|
|
645
|
+
while (current !== null && current !== container) {
|
|
646
|
+
current.updateMatrix();
|
|
647
|
+
matrix.premultiply(current.matrix);
|
|
648
|
+
current = current.parent;
|
|
649
|
+
}
|
|
650
|
+
return matrix;
|
|
651
|
+
}
|
|
652
|
+
function isWithin(node, ancestor) {
|
|
653
|
+
let current = node;
|
|
654
|
+
while (current !== null) {
|
|
655
|
+
if (current === ancestor) return true;
|
|
656
|
+
current = current.parent;
|
|
657
|
+
}
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
function lowestCommonAncestor(members) {
|
|
661
|
+
if (members.length === 0) return null;
|
|
662
|
+
let ancestor = members[0].parent;
|
|
663
|
+
while (ancestor !== null) {
|
|
664
|
+
if (members.every((member) => isWithin(member, ancestor))) return ancestor;
|
|
665
|
+
ancestor = ancestor.parent;
|
|
666
|
+
}
|
|
667
|
+
return null;
|
|
668
|
+
}
|
|
669
|
+
function isJoinedRungDef(value) {
|
|
670
|
+
if (typeof value !== "object" || value === null) return false;
|
|
671
|
+
const def = value;
|
|
672
|
+
return typeof def.draws === "number" && Number.isFinite(def.draws) && typeof def.error === "number" && Number.isFinite(def.error) && typeof def.mesh === "string" && typeof def.primitives === "number" && typeof def.triangles === "number" && Array.isArray(def.sources) && def.sources.every((source) => typeof source === "string");
|
|
673
|
+
}
|
|
674
|
+
var JoinedRung = class {
|
|
675
|
+
name;
|
|
676
|
+
draws;
|
|
677
|
+
primitives;
|
|
678
|
+
triangles;
|
|
679
|
+
error;
|
|
680
|
+
sphere;
|
|
681
|
+
#prototype;
|
|
682
|
+
#geometries;
|
|
683
|
+
#memberCount;
|
|
684
|
+
#activations = /* @__PURE__ */ new WeakMap();
|
|
685
|
+
/** The authored local transform of every source member, for the runtime-moved-sibling guard. */
|
|
686
|
+
#baselines = /* @__PURE__ */ new WeakMap();
|
|
687
|
+
/** Members currently hidden by an activation, so a sibling knows it submits no draw this frame. */
|
|
688
|
+
#hidden = /* @__PURE__ */ new WeakSet();
|
|
689
|
+
constructor(options) {
|
|
690
|
+
this.name = options.def.mesh;
|
|
691
|
+
this.draws = options.def.draws;
|
|
692
|
+
this.primitives = options.def.primitives;
|
|
693
|
+
this.triangles = options.def.triangles;
|
|
694
|
+
this.error = options.def.error;
|
|
695
|
+
this.#prototype = options.prototype;
|
|
696
|
+
this.#geometries = new Set(options.geometries);
|
|
697
|
+
this.#memberCount = options.memberCount;
|
|
698
|
+
this.sphere = options.sphere;
|
|
699
|
+
}
|
|
700
|
+
/** True when a mesh is one of the members hidden by an active activation of this rung. */
|
|
701
|
+
isHidden(mesh) {
|
|
702
|
+
return this.#hidden.has(mesh);
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* The nearest ancestor whose subtree contains every member of this rung — the container the rung
|
|
706
|
+
* was baked against. Geometry identity, not node identity, so a clone resolves its own container.
|
|
707
|
+
*/
|
|
708
|
+
findContainer(mesh) {
|
|
709
|
+
let ancestor = mesh.parent;
|
|
710
|
+
while (ancestor !== null) {
|
|
711
|
+
let found = 0;
|
|
712
|
+
ancestor.traverse((node) => {
|
|
713
|
+
const candidate = node;
|
|
714
|
+
if (candidate.isMesh === true && candidate.geometry !== void 0 && this.#geometries.has(candidate.geometry))
|
|
715
|
+
found += 1;
|
|
716
|
+
});
|
|
717
|
+
if (found >= this.#memberCount) return ancestor;
|
|
718
|
+
ancestor = ancestor.parent;
|
|
719
|
+
}
|
|
720
|
+
return null;
|
|
721
|
+
}
|
|
722
|
+
/** Snapshots each source member's authored local transform at load, before any game script runs. */
|
|
723
|
+
captureBaselines(container, members) {
|
|
724
|
+
for (const member of members)
|
|
725
|
+
this.#baselines.set(member, relativeLocalMatrix(member, container));
|
|
726
|
+
}
|
|
727
|
+
activate(container) {
|
|
728
|
+
const members = [];
|
|
729
|
+
container.traverse((node) => {
|
|
730
|
+
const mesh = node;
|
|
731
|
+
if (mesh.isMesh === true && this.#geometries.has(mesh.geometry)) members.push(mesh);
|
|
732
|
+
});
|
|
733
|
+
if (members.length < this.#memberCount) return false;
|
|
734
|
+
const first = members[0];
|
|
735
|
+
if (first === void 0) return false;
|
|
736
|
+
if (members.some(
|
|
737
|
+
(member) => member.layers.mask !== first.layers.mask || member.castShadow !== first.castShadow || member.receiveShadow !== first.receiveShadow
|
|
738
|
+
)) {
|
|
739
|
+
this.deactivate(container);
|
|
740
|
+
return false;
|
|
741
|
+
}
|
|
742
|
+
for (const member of members) {
|
|
743
|
+
const current = relativeLocalMatrix(member, container);
|
|
744
|
+
const baseline = this.#baselines.get(member);
|
|
745
|
+
if (baseline === void 0) this.#baselines.set(member, current);
|
|
746
|
+
else if (!baseline.equals(current)) {
|
|
747
|
+
console.error(
|
|
748
|
+
`TN_DISCRETE_LOD_JOIN_INVALID: '${this.name}' cannot be shown as one unit; a source node moved after load.`
|
|
749
|
+
);
|
|
750
|
+
if (this.#activations.has(container)) this.deactivate(container);
|
|
751
|
+
return false;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
if (this.#activations.has(container)) return true;
|
|
755
|
+
const proxy = this.#prototype.clone(true);
|
|
756
|
+
proxy.name = `${this.name}__instance`;
|
|
757
|
+
proxy.visible = true;
|
|
758
|
+
proxy.traverse((node) => {
|
|
759
|
+
joinedProxies.add(node);
|
|
760
|
+
if (node.isMesh === true) {
|
|
761
|
+
node.raycast = () => void 0;
|
|
762
|
+
node.layers.mask = first.layers.mask;
|
|
763
|
+
node.castShadow = first.castShadow;
|
|
764
|
+
node.receiveShadow = first.receiveShadow;
|
|
765
|
+
}
|
|
766
|
+
});
|
|
767
|
+
const restored = [];
|
|
768
|
+
for (const member of members) restored.push({ mesh: member, visible: member.visible });
|
|
769
|
+
for (const { mesh } of restored) {
|
|
770
|
+
mesh.visible = false;
|
|
771
|
+
this.#hidden.add(mesh);
|
|
772
|
+
joinedOwners.set(mesh, proxy);
|
|
773
|
+
}
|
|
774
|
+
container.add(proxy);
|
|
775
|
+
this.#activations.set(container, { proxy, restored });
|
|
776
|
+
console.info(
|
|
777
|
+
`TN_DISCRETE_LOD_JOINED ${this.name}: ${String(this.primitives)} primitive(s) collapsed to ${String(this.draws)} draw(s)`
|
|
778
|
+
);
|
|
779
|
+
return true;
|
|
780
|
+
}
|
|
781
|
+
deactivate(container) {
|
|
782
|
+
const activation = this.#activations.get(container);
|
|
783
|
+
if (activation === void 0) return false;
|
|
784
|
+
for (const { mesh, visible } of activation.restored) {
|
|
785
|
+
mesh.visible = visible;
|
|
786
|
+
this.#hidden.delete(mesh);
|
|
787
|
+
joinedOwners.delete(mesh);
|
|
788
|
+
}
|
|
789
|
+
activation.proxy.removeFromParent();
|
|
790
|
+
this.#activations.delete(container);
|
|
791
|
+
return true;
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
var CHAINS_KEY = /* @__PURE__ */ Symbol.for("threenative.discreteLodChains");
|
|
795
|
+
var chainsHost = globalThis;
|
|
796
|
+
var carriedChains = chainsHost[CHAINS_KEY];
|
|
797
|
+
var chains = carriedChains ?? /* @__PURE__ */ new WeakMap();
|
|
798
|
+
chainsHost[CHAINS_KEY] = chains;
|
|
799
|
+
var CHAINS_VERSION_KEY = /* @__PURE__ */ Symbol.for("threenative.discreteLodChainsVersion");
|
|
800
|
+
var bumpChains = () => {
|
|
801
|
+
chainsHost[CHAINS_VERSION_KEY] = (chainsHost[CHAINS_VERSION_KEY] ?? 0) + 1;
|
|
802
|
+
};
|
|
803
|
+
var cloneHooks = /* @__PURE__ */ new WeakSet();
|
|
804
|
+
function preserveChainClones(registered) {
|
|
805
|
+
for (const source of registered.chain.levels) {
|
|
806
|
+
if (cloneHooks.has(source)) continue;
|
|
807
|
+
cloneHooks.add(source);
|
|
808
|
+
const clone = source.clone.bind(source);
|
|
809
|
+
source.clone = () => {
|
|
810
|
+
const copy = clone();
|
|
811
|
+
const current = chains.get(source) ?? registered;
|
|
812
|
+
const chain = current.chain;
|
|
813
|
+
const sourceIndex = chain.levels.indexOf(source);
|
|
814
|
+
const levels = chain.levels.map((level, index) => {
|
|
815
|
+
const geometry = index === sourceIndex ? copy : new BufferGeometry();
|
|
816
|
+
if (geometry !== copy) {
|
|
817
|
+
for (const [name, attribute] of Object.entries(copy.attributes))
|
|
818
|
+
geometry.setAttribute(name, attribute);
|
|
819
|
+
geometry.setIndex(level.index?.clone() ?? null);
|
|
820
|
+
geometry.boundingSphere = copy.boundingSphere;
|
|
821
|
+
geometry.boundingBox = copy.boundingBox;
|
|
822
|
+
}
|
|
823
|
+
return geometry;
|
|
824
|
+
});
|
|
825
|
+
const copied = {
|
|
826
|
+
chain: {
|
|
827
|
+
base: levels[0],
|
|
828
|
+
errors: [...chain.errors],
|
|
829
|
+
levels,
|
|
830
|
+
sphere: chain.sphere.clone()
|
|
831
|
+
},
|
|
832
|
+
policy: current.policy
|
|
833
|
+
};
|
|
834
|
+
for (const geometry of levels) chains.set(geometry, copied);
|
|
835
|
+
bumpChains();
|
|
836
|
+
preserveChainClones(copied);
|
|
837
|
+
const apply = copy.applyMatrix4.bind(copy);
|
|
838
|
+
copy.applyMatrix4 = (matrix) => {
|
|
839
|
+
apply(matrix);
|
|
840
|
+
const errors = copied.chain.errors;
|
|
841
|
+
const scale = matrix.getMaxScaleOnAxis();
|
|
842
|
+
for (let index = 1; index < errors.length; index += 1)
|
|
843
|
+
errors[index] = errors[index] * scale;
|
|
844
|
+
copied.chain.sphere.applyMatrix4(matrix);
|
|
845
|
+
return copy;
|
|
846
|
+
};
|
|
847
|
+
return copy;
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
var controllers = /* @__PURE__ */ new WeakMap();
|
|
852
|
+
function registerModelLodController(mesh, controller) {
|
|
853
|
+
controllers.get(mesh)?.release();
|
|
854
|
+
controllers.set(mesh, controller);
|
|
855
|
+
}
|
|
856
|
+
function isChained(object) {
|
|
857
|
+
const mesh = object;
|
|
858
|
+
const geometry = mesh.geometry;
|
|
859
|
+
return mesh.isMesh === true && geometry !== void 0 && chains.has(geometry);
|
|
860
|
+
}
|
|
861
|
+
function controllerFor(mesh) {
|
|
862
|
+
const existing = controllers.get(mesh);
|
|
863
|
+
if (existing !== void 0) return existing;
|
|
864
|
+
if (mesh.isInstancedMesh === true) return void 0;
|
|
865
|
+
const registered = chains.get(mesh.geometry);
|
|
866
|
+
if (registered === void 0) return void 0;
|
|
867
|
+
const controller = new ModelLod(
|
|
868
|
+
mesh,
|
|
869
|
+
registered.chain,
|
|
870
|
+
registered.policy,
|
|
871
|
+
registered.joined,
|
|
872
|
+
registered.authority === true
|
|
873
|
+
);
|
|
874
|
+
controllers.set(mesh, controller);
|
|
875
|
+
return controller;
|
|
876
|
+
}
|
|
877
|
+
var DiscreteLodPlugin = class {
|
|
878
|
+
name = TN_DISCRETE_LOD;
|
|
879
|
+
#failClosed;
|
|
880
|
+
#parser;
|
|
881
|
+
#associations;
|
|
882
|
+
#pending = /* @__PURE__ */ new Map();
|
|
883
|
+
#rungs = [];
|
|
884
|
+
/** A temporary holder for far-mesh prototypes, so widening reaches their joined geometry. */
|
|
885
|
+
#holder;
|
|
886
|
+
/**
|
|
887
|
+
* `failClosed` rejects the load with the same named error the log line carries, for a loader that
|
|
888
|
+
* has no other way to tell a game its chain was dropped (the Wasm engine's, PRD-617).
|
|
889
|
+
*/
|
|
890
|
+
constructor(options = {}) {
|
|
891
|
+
this.#failClosed = options.failClosed === true;
|
|
892
|
+
}
|
|
893
|
+
setParser(parser) {
|
|
894
|
+
this.#parser = parser;
|
|
895
|
+
}
|
|
896
|
+
async beforeRoot() {
|
|
897
|
+
const parser = this.#parser;
|
|
898
|
+
if (parser === void 0) return;
|
|
899
|
+
await Promise.all(
|
|
900
|
+
(parser.json.nodes ?? []).map((_, index) => parser.getDependency("node", index))
|
|
901
|
+
);
|
|
902
|
+
this.#associations = parser.associations;
|
|
903
|
+
}
|
|
904
|
+
#invalid(message) {
|
|
905
|
+
if (this.#failClosed) throw new Error(message);
|
|
906
|
+
console.error(message);
|
|
907
|
+
}
|
|
908
|
+
async afterRoot(result) {
|
|
909
|
+
const parser = this.#parser;
|
|
910
|
+
if (parser === void 0) return;
|
|
911
|
+
const associations = this.#associations ?? parser.associations;
|
|
912
|
+
for (const [object, association] of associations) {
|
|
913
|
+
if (association?.primitives !== void 0) continue;
|
|
914
|
+
const node = parser.json.nodes?.[association?.nodes ?? -1];
|
|
915
|
+
if (node?.mesh === void 0 || node.extensions?.EXT_mesh_gpu_instancing === void 0)
|
|
916
|
+
continue;
|
|
917
|
+
const parts = object instanceof InstancedMesh ? [object] : object.children;
|
|
918
|
+
for (const [primitives, mesh] of parts.entries()) {
|
|
919
|
+
if (!(mesh instanceof InstancedMesh)) continue;
|
|
920
|
+
associations.set(mesh, { ...association, meshes: node.mesh, primitives });
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
for (const [object, association] of associations) {
|
|
924
|
+
const mesh = object;
|
|
925
|
+
if (mesh.isMesh !== true) continue;
|
|
926
|
+
const { meshes, primitives } = association;
|
|
927
|
+
if (meshes === void 0 || primitives === void 0) continue;
|
|
928
|
+
const def = parser.json.meshes?.[meshes]?.primitives?.[primitives]?.extensions?.[TN_DISCRETE_LOD];
|
|
929
|
+
if (def === void 0) continue;
|
|
930
|
+
try {
|
|
931
|
+
this.#pending.set(mesh, await readPendingLevels(parser, def));
|
|
932
|
+
} catch (error) {
|
|
933
|
+
this.#invalid(
|
|
934
|
+
`TN_DISCRETE_LOD_INVALID: '${mesh.name || "unnamed"}' keeps full detail: ${error instanceof Error ? error.message : String(error)}`
|
|
935
|
+
);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
const metadata = parser.json.extensions?.[TN_DISCRETE_LOD];
|
|
939
|
+
const joined = metadata?.joined;
|
|
940
|
+
if (!Array.isArray(joined) || joined.length === 0) return;
|
|
941
|
+
for (const value of joined) {
|
|
942
|
+
if (!isJoinedRungDef(value)) {
|
|
943
|
+
this.#invalid(
|
|
944
|
+
"TN_DISCRETE_LOD_JOIN_INVALID: a joined rung record is malformed; its primitives keep full detail."
|
|
945
|
+
);
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
const farIndex = (parser.json.meshes ?? []).findIndex((mesh) => mesh.name === value.mesh);
|
|
949
|
+
if (farIndex < 0) {
|
|
950
|
+
this.#invalid(
|
|
951
|
+
`TN_DISCRETE_LOD_JOIN_INVALID: '${value.mesh}' is not one of the file's meshes; its primitives keep full detail.`
|
|
952
|
+
);
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
try {
|
|
956
|
+
const prototype = await parser.getDependency("mesh", farIndex);
|
|
957
|
+
if (!(prototype instanceof Object3D))
|
|
958
|
+
throw new Error("the far mesh dependency is not an Object3D.");
|
|
959
|
+
if (result.scene !== void 0) {
|
|
960
|
+
if (this.#holder === void 0) {
|
|
961
|
+
this.#holder = new Group();
|
|
962
|
+
this.#holder.name = "TN_LOD_JOIN_HOLDER";
|
|
963
|
+
result.scene.add(this.#holder);
|
|
964
|
+
}
|
|
965
|
+
this.#holder.add(prototype);
|
|
966
|
+
}
|
|
967
|
+
this.#rungs.push({ def: value, prototype });
|
|
968
|
+
} catch (error) {
|
|
969
|
+
console.error(
|
|
970
|
+
`TN_DISCRETE_LOD_JOIN_INVALID: '${value.mesh}' could not be read: ${error instanceof Error ? error.message : String(error)}`
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
if (this.#rungs.length === 0 && this.#holder !== void 0) {
|
|
975
|
+
this.#holder.removeFromParent();
|
|
976
|
+
this.#holder = void 0;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
/** Builds the derived geometries against the mesh's final (widened) attributes and registers. */
|
|
980
|
+
attach(_root, policy, instancedLod) {
|
|
981
|
+
const applied = {
|
|
982
|
+
hysteresis: policy?.hysteresis ?? DISCRETE_LOD_DEFAULT_HYSTERESIS,
|
|
983
|
+
maxPixelError: policy?.maxPixelError ?? DISCRETE_LOD_DEFAULT_ERROR_PIXELS
|
|
984
|
+
};
|
|
985
|
+
for (const { prototype } of this.#rungs) prototype.removeFromParent();
|
|
986
|
+
if (this.#holder !== void 0) {
|
|
987
|
+
this.#holder.removeFromParent();
|
|
988
|
+
this.#holder = void 0;
|
|
989
|
+
}
|
|
990
|
+
let appliedCount = 0;
|
|
991
|
+
for (const [mesh, pending] of this.#pending) {
|
|
992
|
+
const base = mesh.geometry;
|
|
993
|
+
const baseTriangles2 = base.index?.count ?? base.getAttribute("position")?.count ?? 0;
|
|
994
|
+
if (Math.floor(baseTriangles2 / 3) !== pending.lod0Triangles) {
|
|
995
|
+
this.#invalid(
|
|
996
|
+
`TN_DISCRETE_LOD_INVALID: '${mesh.name || "unnamed"}' keeps full detail: LOD0 triangles do not match the baked chain.`
|
|
997
|
+
);
|
|
998
|
+
continue;
|
|
999
|
+
}
|
|
1000
|
+
const levels = [base];
|
|
1001
|
+
base.computeBoundingSphere();
|
|
1002
|
+
base.computeBoundingBox();
|
|
1003
|
+
for (const indices of pending.indices) {
|
|
1004
|
+
const geometry = new BufferGeometry();
|
|
1005
|
+
for (const [name, attribute] of Object.entries(base.attributes)) {
|
|
1006
|
+
geometry.setAttribute(name, attribute);
|
|
1007
|
+
}
|
|
1008
|
+
geometry.setIndex(new BufferAttribute(indices, 1));
|
|
1009
|
+
if (base.boundingSphere !== null && base.boundingSphere !== void 0)
|
|
1010
|
+
geometry.boundingSphere = base.boundingSphere;
|
|
1011
|
+
if (base.boundingBox !== null && base.boundingBox !== void 0)
|
|
1012
|
+
geometry.boundingBox = base.boundingBox;
|
|
1013
|
+
levels.push(geometry);
|
|
1014
|
+
}
|
|
1015
|
+
const sphere = new Sphere();
|
|
1016
|
+
if (base.boundingSphere !== null && base.boundingSphere !== void 0)
|
|
1017
|
+
sphere.copy(base.boundingSphere);
|
|
1018
|
+
const registered = {
|
|
1019
|
+
chain: { base, errors: [0, ...pending.absoluteErrors], levels, sphere },
|
|
1020
|
+
policy: applied
|
|
1021
|
+
};
|
|
1022
|
+
for (const geometry of levels) chains.set(geometry, registered);
|
|
1023
|
+
bumpChains();
|
|
1024
|
+
preserveChainClones(registered);
|
|
1025
|
+
if (mesh instanceof InstancedMesh) {
|
|
1026
|
+
if (instancedLod === void 0)
|
|
1027
|
+
throw new Error(
|
|
1028
|
+
"TN_discrete_lod requires the instanced LOD attachment for InstancedMesh."
|
|
1029
|
+
);
|
|
1030
|
+
instancedLod(mesh, { autoLod: policy });
|
|
1031
|
+
} else {
|
|
1032
|
+
controllers.set(mesh, new ModelLod(mesh, registered.chain, applied));
|
|
1033
|
+
}
|
|
1034
|
+
appliedCount += 1;
|
|
1035
|
+
}
|
|
1036
|
+
this.#registerJoinedRungs(applied);
|
|
1037
|
+
return appliedCount;
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Resolves each loaded joined rung to its authored primitives and registers it on every member's
|
|
1041
|
+
* chain, marking one as the authority. A clone shares those geometries, so it adopts the same rung
|
|
1042
|
+
* with its own authority; the rung's per-container state keeps two instances independent.
|
|
1043
|
+
*
|
|
1044
|
+
* Refusal is the honest default: a rung whose sources are not all present, share no container, or
|
|
1045
|
+
* are skinned, morphed or animated is dropped rather than half-joined. The bake already made that
|
|
1046
|
+
* call; the runtime re-checks because the loaded graph, not the bake, is what it hides.
|
|
1047
|
+
*/
|
|
1048
|
+
#registerJoinedRungs(applied) {
|
|
1049
|
+
const parser = this.#parser;
|
|
1050
|
+
if (parser === void 0 || this.#rungs.length === 0) return;
|
|
1051
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
1052
|
+
const animated = /* @__PURE__ */ new WeakSet();
|
|
1053
|
+
const targeted = /* @__PURE__ */ new Set();
|
|
1054
|
+
for (const animation of parser.json.animations ?? []) {
|
|
1055
|
+
for (const channel of animation.channels ?? []) {
|
|
1056
|
+
if (typeof channel.target?.node === "number") targeted.add(channel.target.node);
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
for (const [object, association] of parser.associations) {
|
|
1060
|
+
if (targeted.size > 0 && association.nodes !== void 0 && targeted.has(association.nodes))
|
|
1061
|
+
animated.add(object);
|
|
1062
|
+
const mesh = object;
|
|
1063
|
+
if (mesh.isMesh !== true) continue;
|
|
1064
|
+
const { meshes, primitives } = association;
|
|
1065
|
+
if (meshes === void 0 || primitives === void 0) continue;
|
|
1066
|
+
const name = parser.json.meshes?.[meshes]?.name ?? "";
|
|
1067
|
+
byKey.set(`${name}#${String(primitives)}`, mesh);
|
|
1068
|
+
}
|
|
1069
|
+
for (const { def, prototype } of this.#rungs) {
|
|
1070
|
+
const members = [];
|
|
1071
|
+
let missing = false;
|
|
1072
|
+
for (const source of def.sources) {
|
|
1073
|
+
const mesh = byKey.get(source);
|
|
1074
|
+
if (mesh === void 0) {
|
|
1075
|
+
missing = true;
|
|
1076
|
+
break;
|
|
1077
|
+
}
|
|
1078
|
+
members.push(mesh);
|
|
1079
|
+
}
|
|
1080
|
+
const container = lowestCommonAncestor(members);
|
|
1081
|
+
const refused = missing || members.length < 2 || container === null || members.some(
|
|
1082
|
+
(member) => isDeforming(member) || isAnimatedInContainer(member, container, animated)
|
|
1083
|
+
);
|
|
1084
|
+
if (refused) {
|
|
1085
|
+
console.error(
|
|
1086
|
+
`TN_DISCRETE_LOD_JOIN_INVALID: '${def.mesh}' cannot be shown as one unit at runtime; its primitives keep full detail.`
|
|
1087
|
+
);
|
|
1088
|
+
continue;
|
|
1089
|
+
}
|
|
1090
|
+
const geometries = /* @__PURE__ */ new Set();
|
|
1091
|
+
for (const member of members) {
|
|
1092
|
+
let registered = chains.get(member.geometry);
|
|
1093
|
+
if (registered === void 0) {
|
|
1094
|
+
member.geometry.computeBoundingSphere();
|
|
1095
|
+
const sphere2 = new Sphere();
|
|
1096
|
+
if (member.geometry.boundingSphere !== null && member.geometry.boundingSphere !== void 0)
|
|
1097
|
+
sphere2.copy(member.geometry.boundingSphere);
|
|
1098
|
+
registered = {
|
|
1099
|
+
chain: { base: member.geometry, errors: [0], levels: [member.geometry], sphere: sphere2 },
|
|
1100
|
+
policy: applied
|
|
1101
|
+
};
|
|
1102
|
+
chains.set(member.geometry, registered);
|
|
1103
|
+
bumpChains();
|
|
1104
|
+
}
|
|
1105
|
+
for (const geometry of registered.chain.levels) geometries.add(geometry);
|
|
1106
|
+
}
|
|
1107
|
+
const box = new Box3().setFromObject(prototype);
|
|
1108
|
+
const sphere = new Sphere();
|
|
1109
|
+
box.getBoundingSphere(sphere);
|
|
1110
|
+
const rung = new JoinedRung({
|
|
1111
|
+
def,
|
|
1112
|
+
geometries,
|
|
1113
|
+
memberCount: members.length,
|
|
1114
|
+
prototype,
|
|
1115
|
+
sphere
|
|
1116
|
+
});
|
|
1117
|
+
rung.captureBaselines(container, members);
|
|
1118
|
+
const authority = members[0];
|
|
1119
|
+
for (const member of members) {
|
|
1120
|
+
const registered = chains.get(member.geometry);
|
|
1121
|
+
if (registered === void 0) continue;
|
|
1122
|
+
const withJoined = {
|
|
1123
|
+
authority: member === authority,
|
|
1124
|
+
chain: registered.chain,
|
|
1125
|
+
joined: rung,
|
|
1126
|
+
policy: registered.policy
|
|
1127
|
+
};
|
|
1128
|
+
for (const geometry of registered.chain.levels) chains.set(geometry, withJoined);
|
|
1129
|
+
bumpChains();
|
|
1130
|
+
controllers.set(
|
|
1131
|
+
member,
|
|
1132
|
+
new ModelLod(member, registered.chain, applied, rung, member === authority)
|
|
1133
|
+
);
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
};
|
|
1138
|
+
function baseGeometryOf(mesh) {
|
|
1139
|
+
const controller = controllers.get(mesh);
|
|
1140
|
+
if (controller !== void 0) return controller.base;
|
|
1141
|
+
return chains.get(mesh.geometry)?.chain.base ?? mesh.geometry;
|
|
1142
|
+
}
|
|
1143
|
+
function lodChainOf(geometry) {
|
|
1144
|
+
return chains.get(geometry)?.chain;
|
|
1145
|
+
}
|
|
1146
|
+
var trackedRoots2 = /* @__PURE__ */ new WeakMap();
|
|
1147
|
+
function isGraphNode2(root) {
|
|
1148
|
+
const candidate = root;
|
|
1149
|
+
return typeof candidate.traverse === "function" && typeof candidate.addEventListener === "function" && typeof candidate.removeEventListener === "function";
|
|
1150
|
+
}
|
|
1151
|
+
function hookSubtree2(tracking, subtree) {
|
|
1152
|
+
subtree.traverse((node) => {
|
|
1153
|
+
if (controllers.has(node) || isChained(node)) tracking.items.add(node);
|
|
1154
|
+
if (tracking.hooked.has(node)) return;
|
|
1155
|
+
tracking.hooked.add(node);
|
|
1156
|
+
const link = node;
|
|
1157
|
+
if (typeof link.addEventListener !== "function") return;
|
|
1158
|
+
link.addEventListener("childadded", tracking.onAdded);
|
|
1159
|
+
link.addEventListener("childremoved", tracking.onRemoved);
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
function unhookSubtree2(tracking, subtree) {
|
|
1163
|
+
const nodes = [];
|
|
1164
|
+
subtree.traverse((node) => nodes.push(node));
|
|
1165
|
+
for (const node of nodes) {
|
|
1166
|
+
tracking.items.delete(node);
|
|
1167
|
+
controllers.get(node)?.release();
|
|
1168
|
+
if (!tracking.hooked.has(node)) continue;
|
|
1169
|
+
tracking.hooked.delete(node);
|
|
1170
|
+
const link = node;
|
|
1171
|
+
if (typeof link.removeEventListener !== "function") continue;
|
|
1172
|
+
link.removeEventListener("childadded", tracking.onAdded);
|
|
1173
|
+
link.removeEventListener("childremoved", tracking.onRemoved);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
function trackRoot2(root) {
|
|
1177
|
+
const existing = trackedRoots2.get(root);
|
|
1178
|
+
if (existing !== void 0) return existing;
|
|
1179
|
+
const tracking = {
|
|
1180
|
+
hooked: /* @__PURE__ */ new WeakSet(),
|
|
1181
|
+
items: /* @__PURE__ */ new Set(),
|
|
1182
|
+
onAdded: (event) => {
|
|
1183
|
+
const child = event.child;
|
|
1184
|
+
if (typeof child === "object" && child !== null && isGraphNode2(child))
|
|
1185
|
+
hookSubtree2(tracking, child);
|
|
1186
|
+
},
|
|
1187
|
+
onRemoved: (event) => {
|
|
1188
|
+
const child = event.child;
|
|
1189
|
+
if (typeof child === "object" && child !== null && typeof child.traverse === "function")
|
|
1190
|
+
unhookSubtree2(tracking, child);
|
|
1191
|
+
}
|
|
1192
|
+
};
|
|
1193
|
+
trackedRoots2.set(root, tracking);
|
|
1194
|
+
hookSubtree2(tracking, root);
|
|
1195
|
+
return tracking;
|
|
1196
|
+
}
|
|
1197
|
+
function updateModelLods(root, camera, viewportHeight) {
|
|
1198
|
+
camera.updateMatrixWorld();
|
|
1199
|
+
if (!isGraphNode2(root)) {
|
|
1200
|
+
const entries = [];
|
|
1201
|
+
root.traverse((object) => {
|
|
1202
|
+
const entry = controllerFor(object);
|
|
1203
|
+
if (entry !== void 0) entries.push(entry);
|
|
1204
|
+
});
|
|
1205
|
+
for (const entry of entries) entry.update(camera, viewportHeight);
|
|
1206
|
+
let triangles2 = 0;
|
|
1207
|
+
for (const entry of entries) triangles2 += entry.triangles;
|
|
1208
|
+
return triangles2;
|
|
1209
|
+
}
|
|
1210
|
+
const tracking = trackRoot2(root);
|
|
1211
|
+
for (const mesh of tracking.items) {
|
|
1212
|
+
const entry = controllerFor(mesh);
|
|
1213
|
+
if (entry === void 0) {
|
|
1214
|
+
tracking.items.delete(mesh);
|
|
1215
|
+
continue;
|
|
1216
|
+
}
|
|
1217
|
+
entry.update(camera, viewportHeight);
|
|
1218
|
+
}
|
|
1219
|
+
let triangles = 0;
|
|
1220
|
+
for (const mesh of tracking.items) triangles += controllers.get(mesh)?.triangles ?? 0;
|
|
1221
|
+
return triangles;
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// src/engine-render-hook.ts
|
|
1225
|
+
var engineRenderHooks = /* @__PURE__ */ new WeakSet();
|
|
1226
|
+
function markEngineRenderHook(hook) {
|
|
1227
|
+
if (typeof hook === "function") engineRenderHooks.add(hook);
|
|
1228
|
+
}
|
|
1229
|
+
function isEngineRenderHook(hook) {
|
|
1230
|
+
return typeof hook === "function" && engineRenderHooks.has(hook);
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
// src/geometry-capture.ts
|
|
1234
|
+
var GEOMETRY_CAPTURE_DEFAULT_LIMIT = 50;
|
|
1235
|
+
var GEOMETRY_CAPTURE_MAX_LIMIT = 500;
|
|
1236
|
+
var GEOMETRY_CAPTURE_WALK_CAP = 5e4;
|
|
1237
|
+
var GEOMETRY_CAPTURE_TIMEOUT_MS = 2e3;
|
|
1238
|
+
var GEOMETRY_ASSET_KEY = "tnAssetPath";
|
|
1239
|
+
var GEOMETRY_CAPTURE_CAPABILITY = "runtime.geometry";
|
|
1240
|
+
var GEOMETRY_CAPTURE_SORTS = ["triangles", "draws", "projected"];
|
|
1241
|
+
var NO_OP_RENDER = () => void 0;
|
|
1242
|
+
function now() {
|
|
1243
|
+
return globalThis.performance?.now() ?? Date.now();
|
|
1244
|
+
}
|
|
1245
|
+
function isTriangleObject(object) {
|
|
1246
|
+
if (object.isPoints === true) return false;
|
|
1247
|
+
if (object.isLine === true || object.isLineSegments === true || object.isLineLoop === true) {
|
|
1248
|
+
return false;
|
|
1249
|
+
}
|
|
1250
|
+
return true;
|
|
1251
|
+
}
|
|
1252
|
+
function geometryTriangles(geometry, group) {
|
|
1253
|
+
if (geometry === void 0) return void 0;
|
|
1254
|
+
const available = geometry.index?.count ?? geometry.attributes?.position?.count;
|
|
1255
|
+
if (available === void 0) return void 0;
|
|
1256
|
+
const requested = group?.count ?? geometry.drawRange?.count;
|
|
1257
|
+
const count = requested === void 0 || !Number.isFinite(requested) ? available : Math.max(0, Math.min(requested, available));
|
|
1258
|
+
return Math.floor(count / 3);
|
|
1259
|
+
}
|
|
1260
|
+
function displayName(object) {
|
|
1261
|
+
return object.name === "" ? object.type : object.name;
|
|
1262
|
+
}
|
|
1263
|
+
function nodePath(object, root) {
|
|
1264
|
+
const parts = [];
|
|
1265
|
+
let node = object;
|
|
1266
|
+
while (node !== null && node !== root) {
|
|
1267
|
+
parts.push(node.name === "" ? node.type : node.name);
|
|
1268
|
+
node = node.parent;
|
|
1269
|
+
}
|
|
1270
|
+
parts.reverse();
|
|
1271
|
+
return `/${parts.join("/")}`;
|
|
1272
|
+
}
|
|
1273
|
+
function assetOf(object) {
|
|
1274
|
+
let node = object;
|
|
1275
|
+
while (node !== null) {
|
|
1276
|
+
const stamped = node.userData?.[GEOMETRY_ASSET_KEY];
|
|
1277
|
+
if (typeof stamped === "string" && stamped !== "") return stamped;
|
|
1278
|
+
node = node.parent;
|
|
1279
|
+
}
|
|
1280
|
+
return void 0;
|
|
1281
|
+
}
|
|
1282
|
+
function rowRootOf(object, root) {
|
|
1283
|
+
let node = object;
|
|
1284
|
+
let candidate = object;
|
|
1285
|
+
while (node.parent !== null && node !== root) {
|
|
1286
|
+
const stamped = node.userData?.[GEOMETRY_ASSET_KEY];
|
|
1287
|
+
if (typeof stamped === "string" && stamped !== "") return node;
|
|
1288
|
+
if (node.parent === root) candidate = node;
|
|
1289
|
+
node = node.parent;
|
|
1290
|
+
}
|
|
1291
|
+
return candidate;
|
|
1292
|
+
}
|
|
1293
|
+
var sphereScratch = new Sphere();
|
|
1294
|
+
var centreScratch = new Vector3();
|
|
1295
|
+
var projectScratch = new Vector3();
|
|
1296
|
+
function objectSphere(object, into) {
|
|
1297
|
+
const geometry = object.geometry;
|
|
1298
|
+
if (geometry === void 0) return false;
|
|
1299
|
+
if (geometry.boundingSphere === void 0 || geometry.boundingSphere === null) {
|
|
1300
|
+
geometry.computeBoundingSphere?.();
|
|
1301
|
+
}
|
|
1302
|
+
const local = geometry.boundingSphere;
|
|
1303
|
+
if (local === void 0 || local === null) return false;
|
|
1304
|
+
if (!Number.isFinite(local.radius)) return false;
|
|
1305
|
+
worldSphere(local, object, into);
|
|
1306
|
+
return Number.isFinite(into.radius);
|
|
1307
|
+
}
|
|
1308
|
+
function projectedCenter(centre, camera, viewportWidth, viewportHeight) {
|
|
1309
|
+
if (!(viewportWidth > 0) || !(viewportHeight > 0)) return void 0;
|
|
1310
|
+
const ndc = projectScratch.copy(centre).project(camera);
|
|
1311
|
+
if (!Number.isFinite(ndc.x) || !Number.isFinite(ndc.y)) return void 0;
|
|
1312
|
+
return [(ndc.x * 0.5 + 0.5) * viewportWidth, (-ndc.y * 0.5 + 0.5) * viewportHeight];
|
|
1313
|
+
}
|
|
1314
|
+
function projectedSize(sphere, camera, viewportHeight) {
|
|
1315
|
+
if (!(viewportHeight > 0)) return void 0;
|
|
1316
|
+
const like = camera;
|
|
1317
|
+
const near = like.near ?? 0.1;
|
|
1318
|
+
const distance = centreScratch.copy(sphere.center).distanceTo(camera.getWorldPosition(new Vector3()));
|
|
1319
|
+
let depth;
|
|
1320
|
+
let degenerate;
|
|
1321
|
+
try {
|
|
1322
|
+
const view = conservativeViewDepth(camera, sphere.center, sphere.radius, near);
|
|
1323
|
+
depth = view.depth;
|
|
1324
|
+
degenerate = view.degenerate;
|
|
1325
|
+
} catch {
|
|
1326
|
+
return void 0;
|
|
1327
|
+
}
|
|
1328
|
+
if (like.isOrthographicCamera !== true && degenerate) {
|
|
1329
|
+
return { distance, pixels: viewportHeight };
|
|
1330
|
+
}
|
|
1331
|
+
try {
|
|
1332
|
+
const scale = lodPixelScale(camera, viewportHeight, Math.max(depth, near));
|
|
1333
|
+
if (!Number.isFinite(scale)) return { distance, pixels: viewportHeight };
|
|
1334
|
+
return { distance, pixels: 2 * sphere.radius * scale };
|
|
1335
|
+
} catch {
|
|
1336
|
+
return void 0;
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
function rendererBackendIdentity(raw) {
|
|
1340
|
+
const backend = raw?.backend;
|
|
1341
|
+
const name = backend?.constructor?.name;
|
|
1342
|
+
return typeof name === "string" && name.length > 0 ? name : void 0;
|
|
1343
|
+
}
|
|
1344
|
+
var GeometryCapture = class {
|
|
1345
|
+
#pending;
|
|
1346
|
+
#armed;
|
|
1347
|
+
/**
|
|
1348
|
+
* Arms one capture and answers its report. A malformed request throws rather than quietly
|
|
1349
|
+
* choosing a scope the caller did not ask for. Concurrent requests coalesce onto one frame.
|
|
1350
|
+
*/
|
|
1351
|
+
request(request = {}) {
|
|
1352
|
+
const limit = request.limit ?? GEOMETRY_CAPTURE_DEFAULT_LIMIT;
|
|
1353
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > GEOMETRY_CAPTURE_MAX_LIMIT) {
|
|
1354
|
+
throw new Error(
|
|
1355
|
+
`TN_GEOMETRY_CAPTURE_LIMIT: limit must be an integer from 1 to ${String(GEOMETRY_CAPTURE_MAX_LIMIT)}, got ${String(request.limit)}.`
|
|
1356
|
+
);
|
|
1357
|
+
}
|
|
1358
|
+
const sort = request.sort ?? "triangles";
|
|
1359
|
+
if (!GEOMETRY_CAPTURE_SORTS.includes(sort)) {
|
|
1360
|
+
throw new Error(
|
|
1361
|
+
`TN_GEOMETRY_CAPTURE_SORT: sort must be one of ${GEOMETRY_CAPTURE_SORTS.join(", ")}, got ${String(request.sort)}.`
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
const timeoutMs = request.timeoutMs ?? GEOMETRY_CAPTURE_TIMEOUT_MS;
|
|
1365
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
1366
|
+
throw new Error(
|
|
1367
|
+
`TN_GEOMETRY_CAPTURE_TIMEOUT: timeoutMs must be a positive number, got ${String(request.timeoutMs)}.`
|
|
1368
|
+
);
|
|
1369
|
+
}
|
|
1370
|
+
const existing = this.#pending;
|
|
1371
|
+
if (existing !== void 0) {
|
|
1372
|
+
return new Promise((resolve) => {
|
|
1373
|
+
const previous = existing.settle;
|
|
1374
|
+
const merged = {
|
|
1375
|
+
...existing,
|
|
1376
|
+
limit: Math.max(existing.limit, limit),
|
|
1377
|
+
settle: (report) => {
|
|
1378
|
+
previous(report);
|
|
1379
|
+
resolve(report);
|
|
1380
|
+
}
|
|
1381
|
+
};
|
|
1382
|
+
this.#pending = merged;
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
return new Promise((resolve) => {
|
|
1386
|
+
const pending = {
|
|
1387
|
+
limit,
|
|
1388
|
+
requestedAtMs: now(),
|
|
1389
|
+
settle: resolve,
|
|
1390
|
+
sort
|
|
1391
|
+
};
|
|
1392
|
+
pending.timer = setTimeout(() => {
|
|
1393
|
+
this.#restore();
|
|
1394
|
+
this.#pending = void 0;
|
|
1395
|
+
resolve({
|
|
1396
|
+
reason: `TN_GEOMETRY_CAPTURE_NO_FRAME: no world frame was presented within ${String(timeoutMs)} ms.`,
|
|
1397
|
+
status: "unavailable"
|
|
1398
|
+
});
|
|
1399
|
+
}, timeoutMs);
|
|
1400
|
+
pending.timer.unref?.();
|
|
1401
|
+
this.#pending = pending;
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
/** True while a request is waiting, which is the only condition the render loop needs to test. */
|
|
1405
|
+
armed() {
|
|
1406
|
+
return this.#pending !== void 0;
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Installs the frame's submission hooks. Called immediately before the world render, and only
|
|
1410
|
+
* while `armed()`; an idle game never reaches it.
|
|
1411
|
+
*/
|
|
1412
|
+
beginFrame(frame) {
|
|
1413
|
+
if (this.#pending === void 0) return;
|
|
1414
|
+
this.#restore();
|
|
1415
|
+
const armed = {
|
|
1416
|
+
...frame,
|
|
1417
|
+
complete: true,
|
|
1418
|
+
inspected: 0,
|
|
1419
|
+
records: /* @__PURE__ */ new Map(),
|
|
1420
|
+
restore: []
|
|
1421
|
+
};
|
|
1422
|
+
this.#armed = armed;
|
|
1423
|
+
frame.root.traverse((object) => {
|
|
1424
|
+
if (!armed.complete) return;
|
|
1425
|
+
const rendered = object;
|
|
1426
|
+
if (rendered.geometry === void 0 && rendered.isBatchedMesh !== true) return;
|
|
1427
|
+
armed.inspected += 1;
|
|
1428
|
+
if (armed.inspected > GEOMETRY_CAPTURE_WALK_CAP) {
|
|
1429
|
+
armed.complete = false;
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
this.#hook(armed, object);
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
/**
|
|
1436
|
+
* Ends the frame: removes every hook, reconciles the rows against the measured pass totals and
|
|
1437
|
+
* answers the waiting request.
|
|
1438
|
+
*/
|
|
1439
|
+
finishFrame(passes) {
|
|
1440
|
+
const armed = this.#armed;
|
|
1441
|
+
const pending = this.#pending;
|
|
1442
|
+
this.#restore();
|
|
1443
|
+
if (armed === void 0 || pending === void 0) return;
|
|
1444
|
+
if (pending.timer !== void 0) clearTimeout(pending.timer);
|
|
1445
|
+
this.#pending = void 0;
|
|
1446
|
+
pending.settle(buildReport(armed, passes, pending));
|
|
1447
|
+
}
|
|
1448
|
+
/** Drops a pending capture and every hook. A scene exit is not a reason to report a stale frame. */
|
|
1449
|
+
cancel(reason) {
|
|
1450
|
+
const pending = this.#pending;
|
|
1451
|
+
this.#restore();
|
|
1452
|
+
this.#pending = void 0;
|
|
1453
|
+
if (pending === void 0) return;
|
|
1454
|
+
if (pending.timer !== void 0) clearTimeout(pending.timer);
|
|
1455
|
+
pending.settle({ reason, status: "unavailable" });
|
|
1456
|
+
}
|
|
1457
|
+
#hook(armed, object) {
|
|
1458
|
+
const rendered = object;
|
|
1459
|
+
const own = Object.hasOwn(object, "onBeforeRender") ? rendered.onBeforeRender : void 0;
|
|
1460
|
+
const previous = own ?? NO_OP_RENDER;
|
|
1461
|
+
const record = {
|
|
1462
|
+
byPass: /* @__PURE__ */ new Map(),
|
|
1463
|
+
draws: 0,
|
|
1464
|
+
instances: 1,
|
|
1465
|
+
materials: /* @__PURE__ */ new Set(),
|
|
1466
|
+
object,
|
|
1467
|
+
triangles: 0,
|
|
1468
|
+
trianglesAvailable: true,
|
|
1469
|
+
unavailable: /* @__PURE__ */ new Set()
|
|
1470
|
+
};
|
|
1471
|
+
armed.records.set(object, record);
|
|
1472
|
+
const instrumented = function instrumentedOnBeforeRender(renderer, scene, camera, geometry, material, group) {
|
|
1473
|
+
const kind = armed.activePassKind?.() ?? "main";
|
|
1474
|
+
const pass = record.byPass.get(kind) ?? { draws: 0, triangles: 0 };
|
|
1475
|
+
pass.draws += 1;
|
|
1476
|
+
record.draws += 1;
|
|
1477
|
+
record.materials.add(material);
|
|
1478
|
+
if (rendered.isInstancedMesh === true) {
|
|
1479
|
+
record.instances = Math.max(record.instances, Math.max(0, rendered.count ?? 0));
|
|
1480
|
+
}
|
|
1481
|
+
if (rendered.isBatchedMesh === true) {
|
|
1482
|
+
record.trianglesAvailable = false;
|
|
1483
|
+
record.unavailable.add("TN_GEOMETRY_BATCHED_MESH_SUBDRAWS");
|
|
1484
|
+
} else if (!isTriangleObject(rendered)) ; else {
|
|
1485
|
+
const triangles = geometryTriangles(
|
|
1486
|
+
geometry,
|
|
1487
|
+
group
|
|
1488
|
+
);
|
|
1489
|
+
if (triangles === void 0) {
|
|
1490
|
+
record.trianglesAvailable = false;
|
|
1491
|
+
record.unavailable.add("TN_GEOMETRY_NO_POSITION_COUNT");
|
|
1492
|
+
} else {
|
|
1493
|
+
const instances = rendered.isInstancedMesh === true ? Math.max(0, rendered.count ?? 0) : 1;
|
|
1494
|
+
const submitted = triangles * instances;
|
|
1495
|
+
record.triangles += submitted;
|
|
1496
|
+
pass.triangles += submitted;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
record.byPass.set(kind, pass);
|
|
1500
|
+
previous.call(this, renderer, scene, camera, geometry, material, group);
|
|
1501
|
+
};
|
|
1502
|
+
markEngineRenderHook(instrumented);
|
|
1503
|
+
rendered.onBeforeRender = instrumented;
|
|
1504
|
+
armed.restore.push(() => {
|
|
1505
|
+
if (rendered.onBeforeRender !== instrumented) return;
|
|
1506
|
+
if (own === void 0) delete rendered.onBeforeRender;
|
|
1507
|
+
else rendered.onBeforeRender = own;
|
|
1508
|
+
});
|
|
1509
|
+
}
|
|
1510
|
+
#restore() {
|
|
1511
|
+
const armed = this.#armed;
|
|
1512
|
+
if (armed === void 0) return;
|
|
1513
|
+
for (const restore of armed.restore) restore();
|
|
1514
|
+
armed.restore.length = 0;
|
|
1515
|
+
this.#armed = void 0;
|
|
1516
|
+
}
|
|
1517
|
+
};
|
|
1518
|
+
function meshDraftsOf(armed) {
|
|
1519
|
+
const drafts = [];
|
|
1520
|
+
for (const record of armed.records.values()) {
|
|
1521
|
+
const ownership = armed.ownership?.get(record.object);
|
|
1522
|
+
if (ownership === void 0) {
|
|
1523
|
+
drafts.push({
|
|
1524
|
+
draws: record.draws,
|
|
1525
|
+
record,
|
|
1526
|
+
source: record.object,
|
|
1527
|
+
trianglesSource: "renderer",
|
|
1528
|
+
...record.trianglesAvailable ? { triangles: record.triangles } : {}
|
|
1529
|
+
});
|
|
1530
|
+
continue;
|
|
1531
|
+
}
|
|
1532
|
+
if (ownership.kind === "exact") {
|
|
1533
|
+
const source = ownership.sources[0] ?? record.object;
|
|
1534
|
+
drafts.push({
|
|
1535
|
+
draws: record.draws,
|
|
1536
|
+
record,
|
|
1537
|
+
source,
|
|
1538
|
+
trianglesSource: "renderer",
|
|
1539
|
+
...record.trianglesAvailable ? { triangles: record.triangles } : {}
|
|
1540
|
+
});
|
|
1541
|
+
continue;
|
|
1542
|
+
}
|
|
1543
|
+
const owner = displayName(record.object);
|
|
1544
|
+
for (const source of ownership.sources) {
|
|
1545
|
+
const geometry = source.geometry;
|
|
1546
|
+
const own = geometryTriangles(geometry);
|
|
1547
|
+
drafts.push({
|
|
1548
|
+
batchOwner: owner,
|
|
1549
|
+
draws: 0,
|
|
1550
|
+
record,
|
|
1551
|
+
source,
|
|
1552
|
+
trianglesSource: ownership.kind === "materialBatch" ? "batchMembers" : "renderer",
|
|
1553
|
+
...own === void 0 ? {} : { triangles: own }
|
|
1554
|
+
});
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
return drafts;
|
|
1558
|
+
}
|
|
1559
|
+
function passTrianglesOf(draft, cost) {
|
|
1560
|
+
if (draft.batchOwner === void 0) return cost.triangles;
|
|
1561
|
+
return draft.triangles ?? 0;
|
|
1562
|
+
}
|
|
1563
|
+
function sum(values) {
|
|
1564
|
+
let total = 0;
|
|
1565
|
+
let any = false;
|
|
1566
|
+
for (const value of values) {
|
|
1567
|
+
if (value === void 0) continue;
|
|
1568
|
+
total += value;
|
|
1569
|
+
any = true;
|
|
1570
|
+
}
|
|
1571
|
+
return any ? total : void 0;
|
|
1572
|
+
}
|
|
1573
|
+
function buildReport(armed, passes, pending) {
|
|
1574
|
+
const drafts = meshDraftsOf(armed);
|
|
1575
|
+
const frustum = frustumOf(armed.camera);
|
|
1576
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1577
|
+
for (const draft of drafts) {
|
|
1578
|
+
const root = rowRootOf(draft.source, armed.root);
|
|
1579
|
+
const bucket = groups.get(root);
|
|
1580
|
+
if (bucket === void 0) groups.set(root, [draft]);
|
|
1581
|
+
else bucket.push(draft);
|
|
1582
|
+
}
|
|
1583
|
+
const rows = [];
|
|
1584
|
+
let index = 0;
|
|
1585
|
+
for (const [root, members] of groups) {
|
|
1586
|
+
rows.push(rowOf(root, members, armed, index, frustum));
|
|
1587
|
+
index += 1;
|
|
1588
|
+
}
|
|
1589
|
+
const sorted = rankRows(rows, pending.sort);
|
|
1590
|
+
const returned = sorted.slice(0, pending.limit);
|
|
1591
|
+
const asset = assetRowsOf(rows);
|
|
1592
|
+
const passRows = reconcile(passes, drafts);
|
|
1593
|
+
return {
|
|
1594
|
+
assets: asset,
|
|
1595
|
+
camera: cameraOf(armed.camera),
|
|
1596
|
+
capturedAtMs: now(),
|
|
1597
|
+
durationMs: now() - pending.requestedAtMs,
|
|
1598
|
+
generation: armed.generation,
|
|
1599
|
+
...armed.backend === void 0 ? {} : { backend: armed.backend },
|
|
1600
|
+
...armed.frame === void 0 ? {} : { frame: armed.frame },
|
|
1601
|
+
inspectedNodes: Math.min(armed.inspected, GEOMETRY_CAPTURE_WALK_CAP),
|
|
1602
|
+
inspectionComplete: armed.complete,
|
|
1603
|
+
limit: pending.limit,
|
|
1604
|
+
matched: rows.length,
|
|
1605
|
+
objects: returned,
|
|
1606
|
+
partialRanking: !armed.complete || rows.length > pending.limit,
|
|
1607
|
+
passes: passRows,
|
|
1608
|
+
returned: returned.length,
|
|
1609
|
+
rowsTruncated: rows.length > pending.limit,
|
|
1610
|
+
sort: pending.sort,
|
|
1611
|
+
status: "captured",
|
|
1612
|
+
viewport: { height: armed.viewportHeight, width: armed.viewportWidth },
|
|
1613
|
+
...armed.tick === void 0 ? {} : { tick: armed.tick }
|
|
1614
|
+
};
|
|
1615
|
+
}
|
|
1616
|
+
function rankRows(rows, sort) {
|
|
1617
|
+
const key = (row) => sort === "draws" ? row.draws : sort === "projected" ? row.projectedPixels : row.submittedTriangles;
|
|
1618
|
+
return [...rows].sort((left, right) => {
|
|
1619
|
+
const a = key(left);
|
|
1620
|
+
const b = key(right);
|
|
1621
|
+
if (a === void 0 && b === void 0) return left.path.localeCompare(right.path);
|
|
1622
|
+
if (a === void 0) return 1;
|
|
1623
|
+
if (b === void 0) return -1;
|
|
1624
|
+
if (a === b) return left.path.localeCompare(right.path);
|
|
1625
|
+
return b - a;
|
|
1626
|
+
});
|
|
1627
|
+
}
|
|
1628
|
+
function rowOf(root, members, armed, index, frustum) {
|
|
1629
|
+
const meshes = [];
|
|
1630
|
+
const submissions = {};
|
|
1631
|
+
const unavailable = /* @__PURE__ */ new Set();
|
|
1632
|
+
const materials = /* @__PURE__ */ new Set();
|
|
1633
|
+
const uniqueGeometries = /* @__PURE__ */ new Map();
|
|
1634
|
+
const bounds = new Sphere();
|
|
1635
|
+
let boundsKnown = false;
|
|
1636
|
+
let draws = 0;
|
|
1637
|
+
let copies = 0;
|
|
1638
|
+
let instances;
|
|
1639
|
+
let submitted;
|
|
1640
|
+
let selected;
|
|
1641
|
+
let full;
|
|
1642
|
+
let trianglesSource = "renderer";
|
|
1643
|
+
let batch;
|
|
1644
|
+
for (const member of members) {
|
|
1645
|
+
const source = member.source;
|
|
1646
|
+
const rendered = source;
|
|
1647
|
+
let memberTriangles;
|
|
1648
|
+
for (const [kind, cost] of member.record.byPass) {
|
|
1649
|
+
const into = submissions[kind] ?? { draws: 0, triangles: 0 };
|
|
1650
|
+
into.draws += member.batchOwner === void 0 ? cost.draws : 0;
|
|
1651
|
+
const contributed = member.triangles === void 0 ? 0 : passTrianglesOf(member, cost);
|
|
1652
|
+
into.triangles += contributed;
|
|
1653
|
+
if (member.triangles !== void 0) memberTriangles = (memberTriangles ?? 0) + contributed;
|
|
1654
|
+
submissions[kind] = into;
|
|
1655
|
+
}
|
|
1656
|
+
for (const reason of member.record.unavailable) unavailable.add(reason);
|
|
1657
|
+
for (const material of member.record.materials) materials.add(material);
|
|
1658
|
+
draws += member.draws;
|
|
1659
|
+
copies += member.batchOwner === void 0 ? member.record.instances : 1;
|
|
1660
|
+
if (rendered.isInstancedMesh === true) {
|
|
1661
|
+
instances = (instances ?? 0) + Math.max(0, rendered.count ?? 0);
|
|
1662
|
+
}
|
|
1663
|
+
if (memberTriangles !== void 0) submitted = (submitted ?? 0) + memberTriangles;
|
|
1664
|
+
if (member.trianglesSource === "batchMembers") trianglesSource = "batchMembers";
|
|
1665
|
+
const geometry = rendered.geometry;
|
|
1666
|
+
const own = geometryTriangles(geometry);
|
|
1667
|
+
if (own !== void 0) {
|
|
1668
|
+
selected = (selected ?? 0) + own;
|
|
1669
|
+
if (geometry !== void 0 && !uniqueGeometries.has(geometry))
|
|
1670
|
+
uniqueGeometries.set(geometry, own);
|
|
1671
|
+
}
|
|
1672
|
+
const base = baseTriangles(rendered);
|
|
1673
|
+
if (base !== void 0) full = (full ?? 0) + base;
|
|
1674
|
+
if (member.batchOwner !== void 0) {
|
|
1675
|
+
batch = {
|
|
1676
|
+
kind: member.trianglesSource === "batchMembers" ? "materialBatch" : "instancedBatch",
|
|
1677
|
+
members: members.length,
|
|
1678
|
+
owner: member.batchOwner,
|
|
1679
|
+
perMemberDrawsAvailable: false
|
|
1680
|
+
};
|
|
1681
|
+
}
|
|
1682
|
+
if (objectSphere(source, sphereScratch)) {
|
|
1683
|
+
if (boundsKnown) bounds.union(sphereScratch);
|
|
1684
|
+
else {
|
|
1685
|
+
bounds.copy(sphereScratch);
|
|
1686
|
+
boundsKnown = true;
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
meshes.push({
|
|
1690
|
+
draws: member.draws,
|
|
1691
|
+
id: `${String(armed.generation)}:${String(index)}:${String(meshes.length)}`,
|
|
1692
|
+
materials: member.record.materials.size,
|
|
1693
|
+
name: displayName(source),
|
|
1694
|
+
path: nodePath(source, armed.root),
|
|
1695
|
+
submissions: Object.fromEntries(
|
|
1696
|
+
[...member.record.byPass].map(([kind, cost]) => [
|
|
1697
|
+
kind,
|
|
1698
|
+
{
|
|
1699
|
+
draws: member.batchOwner === void 0 ? cost.draws : 0,
|
|
1700
|
+
triangles: member.triangles === void 0 ? 0 : passTrianglesOf(member, cost)
|
|
1701
|
+
}
|
|
1702
|
+
])
|
|
1703
|
+
),
|
|
1704
|
+
type: source.type,
|
|
1705
|
+
visible: source.visible,
|
|
1706
|
+
...member.batchOwner === void 0 ? {} : { batchOwner: member.batchOwner },
|
|
1707
|
+
...own === void 0 ? {} : { selectedDetailTriangles: own },
|
|
1708
|
+
...base === void 0 ? {} : { fullDetailTriangles: base },
|
|
1709
|
+
...memberTriangles === void 0 ? {} : { submittedTriangles: memberTriangles },
|
|
1710
|
+
...memberTriangles === void 0 ? {} : { trianglesSource: member.trianglesSource },
|
|
1711
|
+
...rendered.isInstancedMesh === true ? { instances: Math.max(0, rendered.count ?? 0) } : {},
|
|
1712
|
+
...member.record.unavailable.size === 0 ? {} : { unavailable: [...member.record.unavailable] }
|
|
1713
|
+
});
|
|
1714
|
+
}
|
|
1715
|
+
const projected = boundsKnown ? projectedSize(bounds, armed.camera, armed.viewportHeight) : void 0;
|
|
1716
|
+
const centre = boundsKnown ? projectedCenter(bounds.center, armed.camera, armed.viewportWidth, armed.viewportHeight) : void 0;
|
|
1717
|
+
const inFrustum = boundsKnown && frustum !== void 0 ? frustum.intersectsSphere(bounds) : void 0;
|
|
1718
|
+
if (!boundsKnown) unavailable.add("TN_GEOMETRY_NO_BOUNDS");
|
|
1719
|
+
const viewportArea = armed.viewportWidth * armed.viewportHeight;
|
|
1720
|
+
const lod = lodOf(root);
|
|
1721
|
+
return {
|
|
1722
|
+
copies,
|
|
1723
|
+
draws,
|
|
1724
|
+
generation: armed.generation,
|
|
1725
|
+
id: `${String(armed.generation)}:${String(index)}`,
|
|
1726
|
+
materials: materials.size,
|
|
1727
|
+
meshes,
|
|
1728
|
+
name: displayName(root),
|
|
1729
|
+
path: nodePath(root, armed.root),
|
|
1730
|
+
submissions,
|
|
1731
|
+
type: root.type,
|
|
1732
|
+
// Submission is a draw the renderer made, never a cost that happens to be known. A hidden
|
|
1733
|
+
// mesh's triangle buffer is still countable; it was not drawn.
|
|
1734
|
+
visibility: members.some((member) => member.record.draws > 0) ? "submitted" : "notSubmitted",
|
|
1735
|
+
...batch === void 0 ? {} : { batch },
|
|
1736
|
+
...assetOf(root) === void 0 ? {} : { asset: assetOf(root) },
|
|
1737
|
+
...full === void 0 ? {} : { fullDetailTriangles: full },
|
|
1738
|
+
...selected === void 0 ? {} : { selectedDetailTriangles: selected },
|
|
1739
|
+
...submitted === void 0 ? {} : { submittedTriangles: submitted },
|
|
1740
|
+
...submitted === void 0 ? {} : { trianglesSource },
|
|
1741
|
+
...instances === void 0 ? {} : { instances },
|
|
1742
|
+
...inFrustum === void 0 ? {} : { inFrustum },
|
|
1743
|
+
...lod === void 0 ? {} : { lod },
|
|
1744
|
+
...uniqueGeometries.size === 0 ? {} : {
|
|
1745
|
+
uniqueTriangles: [...uniqueGeometries.values()].reduce(
|
|
1746
|
+
(total, value) => total + value,
|
|
1747
|
+
0
|
|
1748
|
+
)
|
|
1749
|
+
},
|
|
1750
|
+
...projected === void 0 ? {} : {
|
|
1751
|
+
cameraDistance: projected.distance,
|
|
1752
|
+
projectedPixels: projected.pixels,
|
|
1753
|
+
...centre === void 0 ? {} : { projectedCenter: centre },
|
|
1754
|
+
...viewportArea > 0 ? { viewportFraction: Math.PI * (projected.pixels / 2) ** 2 / viewportArea } : {}
|
|
1755
|
+
},
|
|
1756
|
+
...unavailable.size === 0 ? {} : { unavailable: [...unavailable] }
|
|
1757
|
+
};
|
|
1758
|
+
}
|
|
1759
|
+
function baseTriangles(rendered) {
|
|
1760
|
+
if (rendered.geometry === void 0) return void 0;
|
|
1761
|
+
try {
|
|
1762
|
+
const base = baseGeometryOf(rendered);
|
|
1763
|
+
return geometryTriangles(base);
|
|
1764
|
+
} catch {
|
|
1765
|
+
return void 0;
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
function frustumOf(camera) {
|
|
1769
|
+
const matrix = new Matrix4();
|
|
1770
|
+
const projection = camera.projectionMatrix;
|
|
1771
|
+
if (projection === void 0) return void 0;
|
|
1772
|
+
matrix.multiplyMatrices(projection, camera.matrixWorldInverse);
|
|
1773
|
+
return new Frustum().setFromProjectionMatrix(matrix);
|
|
1774
|
+
}
|
|
1775
|
+
function lodOf(object) {
|
|
1776
|
+
const rendered = object;
|
|
1777
|
+
if (rendered.isLOD !== true) return void 0;
|
|
1778
|
+
const levels = rendered.levels?.length;
|
|
1779
|
+
return levels === void 0 ? void 0 : { levels };
|
|
1780
|
+
}
|
|
1781
|
+
function cameraOf(camera) {
|
|
1782
|
+
const like = camera;
|
|
1783
|
+
const position = camera.getWorldPosition(new Vector3());
|
|
1784
|
+
return {
|
|
1785
|
+
position: [position.x, position.y, position.z],
|
|
1786
|
+
type: like.isOrthographicCamera === true ? "orthographic" : like.isPerspectiveCamera === true ? "perspective" : "unknown",
|
|
1787
|
+
...like.fov === void 0 ? {} : { fov: like.fov },
|
|
1788
|
+
...like.zoom === void 0 ? {} : { zoom: like.zoom },
|
|
1789
|
+
...like.near === void 0 ? {} : { near: like.near },
|
|
1790
|
+
...like.far === void 0 ? {} : { far: like.far }
|
|
1791
|
+
};
|
|
1792
|
+
}
|
|
1793
|
+
function assetRowsOf(rows) {
|
|
1794
|
+
const byAsset = /* @__PURE__ */ new Map();
|
|
1795
|
+
for (const row of rows) {
|
|
1796
|
+
const key = row.asset ?? "unattributed";
|
|
1797
|
+
const bucket = byAsset.get(key);
|
|
1798
|
+
if (bucket === void 0) byAsset.set(key, [row]);
|
|
1799
|
+
else bucket.push(row);
|
|
1800
|
+
}
|
|
1801
|
+
return [...byAsset].map(([asset, group]) => {
|
|
1802
|
+
const unavailable = /* @__PURE__ */ new Set();
|
|
1803
|
+
for (const row of group) for (const reason of row.unavailable ?? []) unavailable.add(reason);
|
|
1804
|
+
return {
|
|
1805
|
+
asset,
|
|
1806
|
+
draws: group.reduce((total, row) => total + row.draws, 0),
|
|
1807
|
+
objects: group.length,
|
|
1808
|
+
...sum(group.map((row) => row.submittedTriangles)) === void 0 ? {} : { submittedTriangles: sum(group.map((row) => row.submittedTriangles)) },
|
|
1809
|
+
...sum(group.map((row) => row.uniqueTriangles)) === void 0 ? {} : { uniqueTriangles: sum(group.map((row) => row.uniqueTriangles)) },
|
|
1810
|
+
...unavailable.size === 0 ? {} : { unavailable: [...unavailable] }
|
|
1811
|
+
};
|
|
1812
|
+
}).sort((left, right) => (right.submittedTriangles ?? -1) - (left.submittedTriangles ?? -1));
|
|
1813
|
+
}
|
|
1814
|
+
function reconcile(passes, drafts) {
|
|
1815
|
+
const measured = /* @__PURE__ */ new Map();
|
|
1816
|
+
for (const pass of passes) {
|
|
1817
|
+
const into = measured.get(pass.kind) ?? { draws: 0, triangles: 0 };
|
|
1818
|
+
into.draws += pass.draws;
|
|
1819
|
+
into.triangles += pass.triangles;
|
|
1820
|
+
measured.set(pass.kind, into);
|
|
1821
|
+
}
|
|
1822
|
+
const attributed = /* @__PURE__ */ new Map();
|
|
1823
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1824
|
+
for (const draft of drafts) {
|
|
1825
|
+
for (const [kind, cost] of draft.record.byPass) {
|
|
1826
|
+
const into = attributed.get(kind) ?? { draws: 0, triangles: 0 };
|
|
1827
|
+
if (!seen.has(draft.record)) into.draws += cost.draws;
|
|
1828
|
+
into.triangles += draft.triangles === void 0 ? 0 : passTrianglesOf(draft, cost);
|
|
1829
|
+
attributed.set(kind, into);
|
|
1830
|
+
}
|
|
1831
|
+
seen.add(draft.record);
|
|
1832
|
+
}
|
|
1833
|
+
const kinds = /* @__PURE__ */ new Set([...measured.keys(), ...attributed.keys()]);
|
|
1834
|
+
return [...kinds].map((kind) => {
|
|
1835
|
+
const total = measured.get(kind) ?? { draws: 0, triangles: 0 };
|
|
1836
|
+
const rows = attributed.get(kind) ?? { draws: 0, triangles: 0 };
|
|
1837
|
+
return {
|
|
1838
|
+
attributedDraws: rows.draws,
|
|
1839
|
+
attributedTriangles: rows.triangles,
|
|
1840
|
+
draws: total.draws,
|
|
1841
|
+
kind,
|
|
1842
|
+
triangles: total.triangles,
|
|
1843
|
+
unattributedDraws: total.draws - rows.draws,
|
|
1844
|
+
unattributedTriangles: total.triangles - rows.triangles
|
|
1845
|
+
};
|
|
1846
|
+
});
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
export { ClusteredMesh, DISCRETE_LOD_DEFAULT_ERROR_PIXELS, DISCRETE_LOD_DEFAULT_HYSTERESIS, DiscreteLodPlugin, GEOMETRY_ASSET_KEY, GEOMETRY_CAPTURE_CAPABILITY, GEOMETRY_CAPTURE_DEFAULT_LIMIT, GEOMETRY_CAPTURE_MAX_LIMIT, GEOMETRY_CAPTURE_SORTS, GEOMETRY_CAPTURE_TIMEOUT_MS, GEOMETRY_CAPTURE_WALK_CAP, GeometryCapture, TN_DISCRETE_LOD, TN_VIRTUAL_GEOMETRY, VirtualGeometryPlugin, baseGeometryOf, biasedLodDistance, conservativeViewDepth, isEngineRenderHook, isLodJoinProxy, lodChainOf, lodPixelScale, markEngineRenderHook, pixelsPerUnit, projectedLodError, registerModelLodController, rendererBackendIdentity, selectClusterCut, selectLodLevel, setLodBias, setModelLodLayers, updateClusteredMeshes, updateModelLods, worldSphere };
|