@threenative/core 0.3.11 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/capabilities.json +1988 -204
- package/dist/{assets-YUbXa3-Y.d.ts → assets-CfMKnOsK.d.ts} +78 -1
- package/dist/camera-shake-CZKOfGn4.d.ts +46 -0
- package/dist/{canvas-layer-vCtanIeB.d.ts → canvas-layer-G7ttVy7y.d.ts} +1 -1
- package/dist/chunk-4SRAQFTU.js +133 -0
- package/dist/chunk-CFGTPFUH.js +231 -0
- package/dist/chunk-DIPIJICH.js +8916 -0
- package/dist/chunk-EEABDLUC.js +2237 -0
- package/dist/chunk-EFWZWLPY.js +1895 -0
- package/dist/chunk-GXY7B3JG.js +46 -0
- package/dist/chunk-GYUZJQ7U.js +280 -0
- package/dist/chunk-H4J6IAJE.js +504 -0
- package/dist/chunk-KIRUKTBQ.js +187 -0
- package/dist/chunk-QWJEMUUT.js +94 -0
- package/dist/chunk-SV4QVUTQ.js +31 -0
- package/dist/chunk-U4YHUKKL.js +615 -0
- package/dist/chunk-VP43GIWW.js +18 -0
- package/dist/chunk-XDTZ4QMO.js +160 -0
- package/dist/default-tone-mapping-GgKPTiBP.d.ts +4 -0
- package/dist/{game-DzMcitDO.d.ts → game-k8K3hZai.d.ts} +85 -9
- package/dist/hot.d.ts +5 -4
- package/dist/hot.js +5 -52
- package/dist/index.d.ts +633 -1351
- package/dist/index.js +12873 -23068
- package/dist/{model-lod-CTpZ6imz.d.ts → model-lod-DPTzeCcD.d.ts} +35 -3
- package/dist/playtest.d.ts +5 -4
- package/dist/playtest.js +25 -75
- package/dist/quality-BYqKGAwo.d.ts +868 -0
- package/dist/react-css.js +1 -215
- package/dist/react.d.ts +3 -2
- package/dist/render-environment.d.ts +170 -0
- package/dist/render-environment.js +352 -0
- package/dist/{renderer-rZm0hVqq.d.ts → renderer-BtOSseYf.d.ts} +42 -35
- 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 +6 -384
- package/dist/worker.js +13 -43
- package/dist/world.d.ts +988 -268
- package/dist/world.js +17464 -18290
- package/mcp/animation-server.mjs +1 -1
- package/mcp/animation.mjs +2 -3
- package/mcp/engine-server.mjs +245 -52
- package/mcp/servers.mjs +10 -3
- package/mcp/terrain.mjs +8 -0
- package/package.json +19 -7
- package/patches/three@0.187.0-prd269-upgrade.patch +2102 -0
- package/patches/three@0.187.0.patch +2004 -407
- package/src/animation-batch.ts +193 -0
- package/src/animation-layers.ts +7 -3
- package/src/animation.ts +209 -29
- package/src/asset-cache.ts +3 -3
- package/src/asset-memo.ts +202 -0
- package/src/assets.ts +437 -125
- package/src/auto-batch.ts +190 -44
- package/src/card-lod.ts +152 -0
- package/src/compressed-texture-preparation.ts +1008 -0
- package/src/compute-driven.ts +1 -1
- package/src/config.ts +8 -0
- package/src/debug.ts +103 -22
- package/src/dev-global.ts +54 -0
- package/src/engine-hooks.ts +37 -0
- package/src/frame-budget.ts +50 -23
- package/src/game-boot-phase.ts +269 -0
- package/src/game-dev-tools.ts +86 -0
- package/src/game-frame-loop.ts +958 -0
- package/src/game-renderer-metrics.ts +77 -0
- package/src/game-startup.ts +1076 -0
- package/src/game-ui.ts +84 -0
- package/src/game-validate.ts +70 -0
- package/src/game.ts +198 -2264
- package/src/gamepad-mappings.ts +269 -0
- package/src/generated/console-markers.ts +13 -0
- package/src/geometry-capture.ts +28 -4
- package/src/gpu-readings.ts +98 -0
- package/src/gpu-scene-bvh.ts +149 -36
- package/src/hot.ts +4 -7
- package/src/index.ts +141 -9
- package/src/input.ts +237 -25
- package/src/instanced-batch-lod.ts +3 -8
- package/src/interpolation.ts +171 -40
- package/src/launch-diagnostics.ts +22 -12
- package/src/look/ao-admission.ts +25 -0
- package/src/look/auto-exposure.ts +5 -16
- package/src/look/default-tone-mapping.ts +41 -5
- package/src/look/filmic-tone-mapping.ts +15 -13
- package/src/look/local-exposure.ts +2 -7
- package/src/look/look-diagnostics.ts +3 -3
- package/src/look/quad-pass.ts +21 -0
- package/src/look/quality.ts +14 -5
- package/src/look/unreal-bloom.ts +4 -13
- package/src/look/world-environment.ts +113 -103
- package/src/loop.ts +29 -0
- package/src/merge-parts.ts +20 -24
- package/src/model-lod.ts +139 -17
- package/src/particle-kinds.ts +515 -0
- package/src/particle-significance.ts +12 -0
- package/src/particles.ts +19 -10
- package/src/picking.ts +17 -3
- package/src/platform.ts +23 -4
- package/src/playtest.ts +27 -1
- package/src/profiling/FrameStages.ts +5 -6
- package/src/profiling/Spans.ts +3 -17
- package/src/profiling/debug-telemetry.ts +2 -6
- package/src/profiling/render-list-validate.ts +6 -15
- package/src/projection-apply.ts +4 -1
- package/src/projection-marker.ts +2 -1
- package/src/projection-plan.ts +9 -6
- package/src/quiet-markers.ts +3 -10
- package/src/render/cpu-bvh.ts +100 -0
- package/src/render/depth-pyramid.ts +6 -14
- package/src/render/environment/environmentContribution.ts +202 -0
- package/src/render/environment/environmentRadiance.ts +44 -0
- package/src/render/environment/environmentSampleCache.ts +56 -0
- package/src/render/environment/environmentSampling.ts +201 -0
- package/src/render/environment/environmentSnapshot.ts +71 -0
- package/src/render/environment/gate.ts +57 -0
- package/src/render/mesh-pool.ts +4 -20
- package/src/render/probe-trace.ts +872 -0
- package/src/render/probe-volume.ts +780 -34
- package/src/render/radiance-cache.ts +17 -0
- 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-partial.ts +13 -0
- package/src/render/virtual-shadow.ts +231 -1008
- package/src/render/world-impostor-surface.ts +402 -40
- package/src/render/world-impostor.ts +5 -2
- package/src/render-camera-cull.ts +98 -3
- package/src/render-environment.ts +41 -0
- package/src/render-pass-budget.ts +34 -7
- package/src/renderProjection.ts +22 -0
- package/src/renderer-config.ts +36 -0
- package/src/renderer-facts.ts +37 -0
- package/src/renderer.ts +144 -65
- package/src/resolution-scaler.ts +3 -2
- package/src/scene-entry-inspectors.ts +61 -0
- package/src/snow-field.ts +2 -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-offscreen-bytes.ts +78 -0
- package/src/startup-readiness.ts +128 -34
- package/src/static-transform.ts +25 -19
- package/src/streamed-textures.ts +112 -0
- package/src/streaming.ts +68 -4
- package/src/texture-streaming.ts +331 -0
- package/src/version.ts +1 -1
- package/src/warmup.ts +56 -8
- package/src/water-surface.ts +9 -3
- package/src/worker.ts +12 -2
- package/src/world-bundle-safety.ts +446 -0
- package/src/world-cell-batches.ts +1178 -0
- package/src/world-cell-chunks.ts +1128 -0
- package/src/world-cell-data.ts +995 -0
- package/src/world-cell-impostors.ts +1461 -0
- package/src/world-cell-lod-bias.ts +151 -0
- package/src/world-cell-models.ts +1074 -0
- package/src/world-cell-options.ts +177 -0
- package/src/world-cell-placements.ts +798 -0
- package/src/world-cell-prewarm.ts +592 -0
- package/src/world-cell-readiness.ts +357 -0
- package/src/world-cell-records.ts +401 -0
- package/src/world-cell-render-bundles.ts +459 -0
- package/src/world-cell-render-gpu-dress.ts +710 -0
- package/src/world-cell-render-main-cull.ts +540 -0
- package/src/world-cell-render-shadow-claims.ts +198 -0
- package/src/world-cell-render-util.ts +259 -0
- package/src/world-cell-render.ts +1501 -0
- package/src/world-cell-residency.ts +754 -0
- package/src/world-cell-stale.ts +238 -0
- package/src/world-cells.ts +1335 -10418
- package/src/world-chunk-shadow.ts +308 -0
- package/src/world-collar.ts +315 -0
- package/src/world-gpu-flags.ts +131 -0
- package/src/world-gpu-readback.ts +881 -0
- package/src/world-gpu-reference.ts +524 -0
- package/src/world-gpu-scene.ts +256 -2286
- package/src/world-gpu-shadow.ts +203 -0
- package/src/world-gpu-types.ts +409 -0
- package/src/world-gpu-validation.ts +518 -0
- package/src/world-heightfield.ts +802 -0
- package/src/world-load-limiter.ts +111 -0
- package/src/world-model-sections.ts +80 -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-river-ribbon.ts +282 -0
- package/src/world-root-frame.ts +39 -0
- package/src/world-shore-field.ts +91 -0
- package/src/world-static-pose.ts +163 -0
- package/src/world-surface-sampler.ts +48 -0
- package/src/world-terrain-splat.ts +361 -153
- package/src/world-tile-budget.ts +249 -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 +247 -1755
- package/src/world-validate.ts +6 -14
- package/src/world-warm.ts +51 -0
- package/src/world-water-bodies.ts +359 -0
- package/src/world.ts +48 -741
|
@@ -0,0 +1,1128 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Box3,
|
|
3
|
+
type BufferGeometry,
|
|
4
|
+
type Camera,
|
|
5
|
+
InstancedMesh,
|
|
6
|
+
Material,
|
|
7
|
+
Matrix4,
|
|
8
|
+
Mesh,
|
|
9
|
+
Object3D,
|
|
10
|
+
} from "three";
|
|
11
|
+
import { markEngineRenderHook } from "./engine-render-hook.js";
|
|
12
|
+
import { mergeByMaterial } from "./merge-parts.js";
|
|
13
|
+
import {
|
|
14
|
+
LOD_NEAR_PLANE,
|
|
15
|
+
isLodJoinProxy,
|
|
16
|
+
lodChainOf,
|
|
17
|
+
selectLodFor,
|
|
18
|
+
setModelLodLayers,
|
|
19
|
+
} from "./model-lod.js";
|
|
20
|
+
import { displacesVertices } from "./projection-plan.js";
|
|
21
|
+
import type { IRendererLike } from "./renderer.js";
|
|
22
|
+
import { markStatic } from "./static-transform.js";
|
|
23
|
+
import { addInSlices, loadAll } from "./streaming.js";
|
|
24
|
+
import { warmUpScene, warmUpSceneWithCompletion, yieldToHost } from "./warmup.js";
|
|
25
|
+
import type { SharedBatch } from "./world-cell-batches.js";
|
|
26
|
+
import type { ICellProxyState, IResidentCell } from "./world-cell-data.js";
|
|
27
|
+
import type { IAssetState } from "./world-cell-models.js";
|
|
28
|
+
import {
|
|
29
|
+
CellProxySelection,
|
|
30
|
+
cellProxyGeometrySafe,
|
|
31
|
+
cellProxyMaterialMatches,
|
|
32
|
+
cellProxyMaterialSafe,
|
|
33
|
+
cellProxySourceSafe,
|
|
34
|
+
cellProxyWorldScale,
|
|
35
|
+
measureCellProxy,
|
|
36
|
+
prepareCellProxy,
|
|
37
|
+
} from "./world-cell-proxy.js";
|
|
38
|
+
import {
|
|
39
|
+
CHUNK_NAME,
|
|
40
|
+
VIRTUAL_SHADOW_CASTER_LAYER,
|
|
41
|
+
VIRTUAL_SHADOW_WIDE_CASTER_LAYER,
|
|
42
|
+
buildChunkShadowProxies,
|
|
43
|
+
} from "./world-chunk-shadow.js";
|
|
44
|
+
import type { IWorldCell } from "./world-package.js";
|
|
45
|
+
import type { IAdmissionBudget } from "./world-tiles.js";
|
|
46
|
+
|
|
47
|
+
interface ICellChunksOptions {
|
|
48
|
+
readonly loadModel: ((url: string) => Promise<Object3D>) | undefined;
|
|
49
|
+
readonly byteBudget: number;
|
|
50
|
+
readonly logicalBase: string;
|
|
51
|
+
readonly receiveShadow: boolean;
|
|
52
|
+
readonly placementReach: Float32Array | undefined;
|
|
53
|
+
readonly autoLodMaxPixelError: number;
|
|
54
|
+
readonly chunkMergeMaxTriangles: number;
|
|
55
|
+
readonly castShadowLevels: number;
|
|
56
|
+
readonly bundlesWanted: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ICellChunksHost {
|
|
60
|
+
disposeProxyModel(state: ICellProxyState): void;
|
|
61
|
+
disposeLoaded(model: Object3D | undefined): number;
|
|
62
|
+
releaseModelPaths(paths: string[]): void;
|
|
63
|
+
retainModelPaths(paths: readonly string[], into: string[]): void;
|
|
64
|
+
addFailures(count: number): void;
|
|
65
|
+
sourceBytes(): number;
|
|
66
|
+
load(path: string, wanted: () => boolean): Promise<Object3D | undefined>;
|
|
67
|
+
cellLive(cell: IResidentCell, generation: number): boolean;
|
|
68
|
+
add(object: Object3D): void;
|
|
69
|
+
intersectsObject(mesh: Mesh): boolean;
|
|
70
|
+
gpuSceneOn(): boolean;
|
|
71
|
+
asset(id: string): IAssetState | undefined;
|
|
72
|
+
invalidateBundles(key: string): void;
|
|
73
|
+
worldSphere(
|
|
74
|
+
bounds: Parameters<typeof import("./model-lod.js").worldSphere>[0],
|
|
75
|
+
): ReturnType<typeof import("./model-lod.js").worldSphere>;
|
|
76
|
+
reportCellFailure(
|
|
77
|
+
stage: "asset" | "attach" | "chunk",
|
|
78
|
+
key: string,
|
|
79
|
+
paths: readonly string[],
|
|
80
|
+
error: unknown,
|
|
81
|
+
): void;
|
|
82
|
+
parent(): Object3D | null;
|
|
83
|
+
/** The scene this world sits in, or `undefined` while it is in none: nothing projects it then. */
|
|
84
|
+
targetScene(): Object3D | undefined;
|
|
85
|
+
addChunk(cell: IResidentCell, object: Object3D): void;
|
|
86
|
+
publishChunkBounds(cell: IResidentCell, box: Box3 | undefined, proxies: number): void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Bytes one merged chunk group may hold: 4 MiB, which is where a single first-draw upload stops
|
|
91
|
+
* being a few milliseconds and becomes a frame.
|
|
92
|
+
*
|
|
93
|
+
* The 228.8 MB of `createAttribute` buffers a browser walk measured — 826 buffers, 33 over 1 MB,
|
|
94
|
+
* the largest 23.2 / 18.1 / 17.2 / 16.4 MB — all arrived on the first draw of a merged chunk mesh,
|
|
95
|
+
* and that first draw took up to 230 ms. A group is split rather than grown, so each of those
|
|
96
|
+
* uploads is bounded and none of them is a stall.
|
|
97
|
+
*/
|
|
98
|
+
const CHUNK_MERGE_MAX_BYTES = 4 * 1024 * 1024;
|
|
99
|
+
/** Position (12) + normal (12) + uv (8) in float32: the three channels a merged group keeps. */
|
|
100
|
+
const CHUNK_MERGE_BYTES_PER_VERTEX = 32;
|
|
101
|
+
/** Vertices in 4 MiB of the above. The budget a chunk's material group is split on. */
|
|
102
|
+
const CHUNK_MERGE_MAX_VERTICES = CHUNK_MERGE_MAX_BYTES / CHUNK_MERGE_BYTES_PER_VERTEX;
|
|
103
|
+
/**
|
|
104
|
+
* Triangles a chunk's merged material group may reach before one of its `InstancedMesh` is left
|
|
105
|
+
* instanced instead. A merged group is one draw; past this many triangles the instanced draw is the
|
|
106
|
+
* cheaper of the two, so the mesh is kept rather than expanded. It is the same 4 MiB counted in the
|
|
107
|
+
* worst case, a de-indexed triangle of three 32-byte vertices, so the cap and the vertex budget above
|
|
108
|
+
* are the one size read in two units. See `IWorldCellsLoadOptions.chunkMergeMaxTriangles`.
|
|
109
|
+
*/
|
|
110
|
+
export const CHUNK_MERGE_MAX_TRIANGLES = Math.floor(
|
|
111
|
+
CHUNK_MERGE_MAX_BYTES / (3 * CHUNK_MERGE_BYTES_PER_VERTEX),
|
|
112
|
+
);
|
|
113
|
+
/**
|
|
114
|
+
* How long one chunk's warm-up may take before the chunk is attached uncompiled. The engine's own
|
|
115
|
+
* loading-screen bound for a single pipeline is 2,000 ms. A cell proxy that times out is refused;
|
|
116
|
+
* its staged geometry remains owned until the original compile settles.
|
|
117
|
+
*/
|
|
118
|
+
const CHUNK_WARM_TIMEOUT_MS = 2000;
|
|
119
|
+
|
|
120
|
+
/** GPU resources already handed to a `dispose`, so no second path can tear the same one down. */
|
|
121
|
+
const tornDown = new WeakSet<object>();
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Release one GPU resource: at most once per resource, and never by throwing.
|
|
125
|
+
*
|
|
126
|
+
* Exactly once is the streaming contract: an asset's geometry is shared by every cell batch drawn
|
|
127
|
+
* from it, so several residency paths can reach the same object, and a second teardown is the
|
|
128
|
+
* renderer's problem, not the game's.
|
|
129
|
+
*
|
|
130
|
+
* Never throwing is the other half. Three disposes a geometry by deleting each of its attributes,
|
|
131
|
+
* and `WebGPUAttributeUtils.destroyAttribute` reads the GPU buffer off the attribute and calls
|
|
132
|
+
* `destroy` on it with no guard — so disposing a geometry whose upload never produced that buffer
|
|
133
|
+
* throws `TypeError: Cannot read properties of undefined (reading 'destroy')` from wherever the
|
|
134
|
+
* teardown was called. A cell leaving range is the most ordinary thing a streaming world does, so
|
|
135
|
+
* that has to land as a counted failure here, not as a dead frame.
|
|
136
|
+
*
|
|
137
|
+
* @returns `true` when the teardown itself threw, for the caller to count.
|
|
138
|
+
*/
|
|
139
|
+
export function release(target: { dispose: () => void } | undefined): boolean {
|
|
140
|
+
if (target === undefined || tornDown.has(target)) return false;
|
|
141
|
+
tornDown.add(target);
|
|
142
|
+
try {
|
|
143
|
+
target.dispose();
|
|
144
|
+
} catch {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function resolveRelative(baseUrl: string, relative: string): string {
|
|
151
|
+
if (/^(?:[a-z]+:)?\/\//iu.test(relative) || relative.startsWith("data:")) return relative;
|
|
152
|
+
return `${baseUrl}${relative.replace(/^\//u, "")}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** What one chunk's merge did, for `TN_WORLD_CHUNK_MERGE`. */
|
|
156
|
+
interface IChunkMerge {
|
|
157
|
+
/** Meshes the chunk held before, and the ones its subtree submits after. */
|
|
158
|
+
readonly meshes: number;
|
|
159
|
+
readonly draws: number;
|
|
160
|
+
/**
|
|
161
|
+
* Bytes the merged groups hold: exactly what the first draw of those meshes uploads, and the
|
|
162
|
+
* number this merge exists to keep small.
|
|
163
|
+
*/
|
|
164
|
+
readonly bytes: number;
|
|
165
|
+
/** Instanced meshes baked into a material's group, and the ones left instanced. */
|
|
166
|
+
readonly expanded: number;
|
|
167
|
+
readonly keptInstanced: number;
|
|
168
|
+
/** Teardowns of the consumed shapes that threw, for the caller to count. */
|
|
169
|
+
readonly failed: number;
|
|
170
|
+
/** Shadow-only proxies, per side for merged and retained parts; 0 when nothing casts. */
|
|
171
|
+
readonly shadowDraws: number;
|
|
172
|
+
/**
|
|
173
|
+
* The opaque meshes a proxy covers. They keep drawing the main pass and keep receiving, and
|
|
174
|
+
* they stop casting: the depth pass reads positions, not materials, so it reads the proxy instead.
|
|
175
|
+
*/
|
|
176
|
+
readonly shadowCovered: readonly Mesh[];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function meshesOf(root: Object3D): number {
|
|
180
|
+
let meshes = 0;
|
|
181
|
+
root.traverse((object) => {
|
|
182
|
+
if ((object as Mesh).isMesh === true) meshes += 1;
|
|
183
|
+
});
|
|
184
|
+
return meshes;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* One chunk's static meshes merged by material, in place, before the chunk is added.
|
|
189
|
+
*
|
|
190
|
+
* A hand-placed chunk is a bridge or a yard, not a prop: its mesh nodes are packed a few dozen to a
|
|
191
|
+
* few hundred per file over a handful of materials, and each one is its own draw — in the main pass
|
|
192
|
+
* and again in every shadow level that redraws it. Merging by material is what the cook already did
|
|
193
|
+
* for repeated meshes (hence the `InstancedMesh`es in a loaded chunk), so this is the same fix one
|
|
194
|
+
* step further out: one draw per material per chunk instead of one per node.
|
|
195
|
+
*
|
|
196
|
+
* Nothing here decides how anything looks. The merged mesh draws the game's own material and holds
|
|
197
|
+
* the geometry exactly as authored, baked into the chunk root's space; what changes is how many
|
|
198
|
+
* calls it takes to draw. `mergeByMaterial` does the bake and owns the refusals, so a group it
|
|
199
|
+
* cannot merge (a material whose meshes disagree about `uv`) leaves the whole chunk as it was and is
|
|
200
|
+
* counted as a failure — a chunk never disappears over a draw-call saving.
|
|
201
|
+
*
|
|
202
|
+
* Left alone: a skinned mesh (posed per frame), one carrying morph targets (the bake drops them), a
|
|
203
|
+
* multi-material mesh (one draw per material already), and anything carrying a baked AutoLOD chain,
|
|
204
|
+
* whose own `ModelLod` swaps its geometry and would be swapped for nothing. No chunk node is
|
|
205
|
+
* animation-targeted: the chunk path takes `gltf.scene` and never a clip list.
|
|
206
|
+
*
|
|
207
|
+
* `disposeSources` says who owns the shapes the bake consumed. The merged buffers are the chunk's
|
|
208
|
+
* own and are released with it on eviction; a shape nothing draws any more is released here, unless
|
|
209
|
+
* the loader cache still holds it — `createAssetLoader` keys models by path and hands the same scene
|
|
210
|
+
* back on the next load, so a cached shape is not this world's to tear down. A geometry a kept mesh
|
|
211
|
+
* still draws is never released either way.
|
|
212
|
+
*
|
|
213
|
+
* `castShadow` says the chunk's shadow bill exists at all — the same answer the caller hands the
|
|
214
|
+
* chunk's nodes — and only then is it worth one position-only proxy per `side`. See
|
|
215
|
+
* `buildChunkShadowProxies`, which draws every side of every chunk with the world's one material for
|
|
216
|
+
* that side.
|
|
217
|
+
*
|
|
218
|
+
* @returns what the merge did, or `undefined` when the bake refused the whole chunk.
|
|
219
|
+
*/
|
|
220
|
+
function mergeChunk(
|
|
221
|
+
chunk: Object3D,
|
|
222
|
+
maxTriangles: number,
|
|
223
|
+
disposeSources: boolean,
|
|
224
|
+
castShadow: boolean,
|
|
225
|
+
proxyMaterials: Map<number, Material>,
|
|
226
|
+
): IChunkMerge | undefined {
|
|
227
|
+
chunk.updateMatrixWorld(true);
|
|
228
|
+
const meshes: Mesh[] = [];
|
|
229
|
+
const skipped = new Set<Mesh>();
|
|
230
|
+
let instanced = 0;
|
|
231
|
+
chunk.traverse((object) => {
|
|
232
|
+
const mesh = object as Mesh & { isInstancedMesh?: boolean };
|
|
233
|
+
if (mesh.isMesh !== true) return;
|
|
234
|
+
meshes.push(mesh);
|
|
235
|
+
// A chain is baked into the geometry the mesh draws, so a merged buffer would carry a `ModelLod`
|
|
236
|
+
// that has nothing to swap: the mesh keeps its own geometry and its own level selection.
|
|
237
|
+
if (lodChainOf(mesh.geometry) !== undefined) {
|
|
238
|
+
skipped.add(mesh);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (Object.keys(mesh.geometry.morphAttributes).length > 0) {
|
|
242
|
+
skipped.add(mesh);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (mesh.isInstancedMesh === true) instanced += 1;
|
|
246
|
+
});
|
|
247
|
+
const before = meshes.length;
|
|
248
|
+
let result: Mesh[];
|
|
249
|
+
try {
|
|
250
|
+
result = mergeByMaterial(chunk, {
|
|
251
|
+
expandInstancedUnderTriangles: maxTriangles,
|
|
252
|
+
label: `world chunk ${chunk.name}`,
|
|
253
|
+
maxGroupVertices: CHUNK_MERGE_MAX_VERTICES,
|
|
254
|
+
skip: (mesh) => skipped.has(mesh),
|
|
255
|
+
});
|
|
256
|
+
} catch {
|
|
257
|
+
return undefined;
|
|
258
|
+
}
|
|
259
|
+
const kept = new Set(skipped);
|
|
260
|
+
let keptInstanced = 0;
|
|
261
|
+
for (const mesh of result) {
|
|
262
|
+
if ((mesh as Mesh & { isInstancedMesh?: boolean }).isInstancedMesh !== true) continue;
|
|
263
|
+
kept.add(mesh);
|
|
264
|
+
keptInstanced += 1;
|
|
265
|
+
}
|
|
266
|
+
const held = new Set<BufferGeometry>();
|
|
267
|
+
for (const mesh of kept) held.add(mesh.geometry);
|
|
268
|
+
let failed = 0;
|
|
269
|
+
// Only what the bake consumed leaves its place; a kept mesh draws the same either way, so it is
|
|
270
|
+
// not re-parented and the chunk's authored hierarchy survives around the merged groups.
|
|
271
|
+
for (const mesh of meshes) {
|
|
272
|
+
if (kept.has(mesh)) continue;
|
|
273
|
+
if (disposeSources && !held.has(mesh.geometry) && release(mesh.geometry)) failed += 1;
|
|
274
|
+
mesh.removeFromParent();
|
|
275
|
+
}
|
|
276
|
+
for (const mesh of result)
|
|
277
|
+
if (!(mesh as Mesh & { isInstancedMesh?: boolean }).isInstancedMesh === true) chunk.add(mesh);
|
|
278
|
+
// Only what the bake produced is covered: a kept mesh was never merged, so it still holds its own
|
|
279
|
+
// per-material draw and its own materials the proxy knows nothing about.
|
|
280
|
+
const shadow = castShadow
|
|
281
|
+
? buildChunkShadowProxies(
|
|
282
|
+
chunk,
|
|
283
|
+
result.filter((mesh) => !kept.has(mesh)),
|
|
284
|
+
`world chunk ${chunk.name}`,
|
|
285
|
+
proxyMaterials,
|
|
286
|
+
)
|
|
287
|
+
: undefined;
|
|
288
|
+
// AutoLOD and morphs remain their own draws. Only unmodified static instance silhouettes qualify.
|
|
289
|
+
const toRoot = chunk.matrixWorld.clone().invert();
|
|
290
|
+
const retained = castShadow
|
|
291
|
+
? buildChunkShadowProxies(
|
|
292
|
+
chunk,
|
|
293
|
+
[...kept].filter(
|
|
294
|
+
(mesh) =>
|
|
295
|
+
mesh instanceof InstancedMesh &&
|
|
296
|
+
mesh.count > 0 &&
|
|
297
|
+
mesh.layers.isEnabled(0) &&
|
|
298
|
+
lodChainOf(mesh.geometry) === undefined &&
|
|
299
|
+
Object.keys(mesh.geometry.morphAttributes).length === 0 &&
|
|
300
|
+
mesh.morphTexture === null &&
|
|
301
|
+
mesh.customDepthMaterial === undefined &&
|
|
302
|
+
mesh.customDistanceMaterial === undefined &&
|
|
303
|
+
mesh.onBeforeRender === Object3D.prototype.onBeforeRender &&
|
|
304
|
+
!Array.isArray(mesh.material) &&
|
|
305
|
+
!displacesVertices(mesh.material) &&
|
|
306
|
+
mesh.material.visible &&
|
|
307
|
+
mesh.material.allowOverride &&
|
|
308
|
+
mesh.material.onBeforeCompile === Material.prototype.onBeforeCompile &&
|
|
309
|
+
Reflect.get(mesh.material, "isShaderMaterial") !== true &&
|
|
310
|
+
mesh.material.shadowSide === null &&
|
|
311
|
+
(mesh.material.clippingPlanes?.length ?? 0) === 0 &&
|
|
312
|
+
Reflect.get(mesh.material, "vertexNode") == null &&
|
|
313
|
+
Reflect.get(mesh.material, "depthNode") == null &&
|
|
314
|
+
Reflect.get(mesh.material, "castShadowNode") == null &&
|
|
315
|
+
Reflect.get(mesh.material, "wireframe") !== true &&
|
|
316
|
+
Reflect.get(mesh.geometry, "indirect") == null &&
|
|
317
|
+
Reflect.get(mesh, "casterInstanceScale") == null &&
|
|
318
|
+
new Matrix4().multiplyMatrices(toRoot, mesh.matrixWorld).determinant() >= 0,
|
|
319
|
+
),
|
|
320
|
+
`world chunk ${chunk.name} retained`,
|
|
321
|
+
proxyMaterials,
|
|
322
|
+
true,
|
|
323
|
+
)
|
|
324
|
+
: undefined;
|
|
325
|
+
return {
|
|
326
|
+
bytes: mergedBytes(result, kept),
|
|
327
|
+
draws: meshesOf(chunk),
|
|
328
|
+
expanded: instanced - keptInstanced,
|
|
329
|
+
failed,
|
|
330
|
+
keptInstanced,
|
|
331
|
+
meshes: before,
|
|
332
|
+
shadowCovered: [...(shadow?.covered ?? []), ...(retained?.covered ?? [])],
|
|
333
|
+
shadowDraws: (shadow?.proxies.length ?? 0) + (retained?.proxies.length ?? 0),
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Bytes the bake's own meshes hold, indices included: the upload their first draw would have paid.
|
|
339
|
+
* A kept mesh is not one of them — it was never merged, and it draws the same either way.
|
|
340
|
+
*/
|
|
341
|
+
function mergedBytes(merged: readonly Mesh[], kept: ReadonlySet<Mesh>): number {
|
|
342
|
+
let bytes = 0;
|
|
343
|
+
for (const mesh of merged) {
|
|
344
|
+
if (kept.has(mesh)) continue;
|
|
345
|
+
for (const attribute of Object.values(mesh.geometry.attributes))
|
|
346
|
+
bytes += attribute.array.byteLength;
|
|
347
|
+
const index = mesh.geometry.getIndex();
|
|
348
|
+
if (index !== null) bytes += index.array.byteLength;
|
|
349
|
+
}
|
|
350
|
+
return bytes;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Triangles one geometry submits; the count the `TN_WORLD_LOD_CHAIN` marker reports per level. */
|
|
354
|
+
export function levelTriangles(geometry: BufferGeometry): number {
|
|
355
|
+
const drawn = geometry.index?.count ?? geometry.getAttribute("position")?.count ?? 0;
|
|
356
|
+
return Math.floor(drawn / 3);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export class CellChunks {
|
|
360
|
+
#camera: Camera | undefined;
|
|
361
|
+
/**
|
|
362
|
+
* The renderer and camera of the last frame that ran, kept so a chunk streamed in between frames
|
|
363
|
+
* can be compiled before it is attached. `update(renderer, camera)` is the only place either is
|
|
364
|
+
* known, and the admission that loads a chunk is not a place that has them.
|
|
365
|
+
*/
|
|
366
|
+
#renderer: IRendererLike | undefined;
|
|
367
|
+
|
|
368
|
+
readonly #hlod: boolean;
|
|
369
|
+
readonly #cellProxies = new Set<ICellProxyState>();
|
|
370
|
+
#hlodBytes = 0;
|
|
371
|
+
#hlodRefused = 0;
|
|
372
|
+
#hlodReason = "";
|
|
373
|
+
#hlodFailures = 0;
|
|
374
|
+
/**
|
|
375
|
+
* Per-side surfaces shared world-wide, separately for merged and retained proxies. See
|
|
376
|
+
* `worldOwned`: a chunk teardown must not release another chunk's shadow surface.
|
|
377
|
+
*/
|
|
378
|
+
readonly #proxyMaterials = new Map<number, Material>();
|
|
379
|
+
|
|
380
|
+
#proxyObserved(state: ICellProxyState): boolean {
|
|
381
|
+
return state.prepared !== undefined && state.observed.size === state.prepared.meshes.length;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
releaseCellProxy(state: ICellProxyState): void {
|
|
385
|
+
if (!this.#cellProxies.delete(state)) return;
|
|
386
|
+
state.prepared?.root.removeFromParent();
|
|
387
|
+
for (const mesh of state.prepared?.meshes ?? []) {
|
|
388
|
+
mesh.onBeforeRender = Mesh.prototype.onBeforeRender;
|
|
389
|
+
mesh.onAfterRender = Mesh.prototype.onAfterRender;
|
|
390
|
+
this.#host.addFailures(release(mesh.geometry) ? 1 : 0);
|
|
391
|
+
}
|
|
392
|
+
// Cached model ownership uses the same global path holders as chunks and source LODs.
|
|
393
|
+
// A compilation never races this release: cancellation retains everything until settlement.
|
|
394
|
+
this.#host.disposeProxyModel(state);
|
|
395
|
+
this.#host.releaseModelPaths(state.retainedPaths);
|
|
396
|
+
this.#hlodBytes -= state.bytes;
|
|
397
|
+
state.bytes = 0;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
#beginCellProxy(cell: IResidentCell): void {
|
|
401
|
+
const proxy = cell.cell.proxy;
|
|
402
|
+
if (proxy === undefined) return;
|
|
403
|
+
// Reserve resident headroom before IO; decoded source and fixed copies must fit it.
|
|
404
|
+
// Temporary pressure retries through the existing admission loop.
|
|
405
|
+
const reserve = Math.min(
|
|
406
|
+
CHUNK_MERGE_MAX_BYTES * 2,
|
|
407
|
+
this.#byteBudget - this.#host.sourceBytes() - this.#hlodBytes,
|
|
408
|
+
);
|
|
409
|
+
if (reserve <= 0) return;
|
|
410
|
+
const state: ICellProxyState = {
|
|
411
|
+
cell,
|
|
412
|
+
selector: new CellProxySelection(proxy),
|
|
413
|
+
retainedPaths: [],
|
|
414
|
+
pending: true,
|
|
415
|
+
compiling: false,
|
|
416
|
+
cancelled: false,
|
|
417
|
+
compiled: false,
|
|
418
|
+
active: false,
|
|
419
|
+
bytes: 0,
|
|
420
|
+
observed: new Set(),
|
|
421
|
+
hidden: new Map(),
|
|
422
|
+
mainMasks: new Map(),
|
|
423
|
+
};
|
|
424
|
+
const source = this.#cellProxySource(state);
|
|
425
|
+
// A retained neighboring key or a cheaper source LOD cannot benefit. Reconsider after later
|
|
426
|
+
// residency changes without allocating another copy or running a proxy load meanwhile.
|
|
427
|
+
if (
|
|
428
|
+
!source.compatible ||
|
|
429
|
+
proxy.triangles >= source.triangles ||
|
|
430
|
+
proxy.materialGroups > source.draws
|
|
431
|
+
)
|
|
432
|
+
return;
|
|
433
|
+
cell.proxy = state;
|
|
434
|
+
state.bytes = reserve;
|
|
435
|
+
this.#hlodBytes += reserve;
|
|
436
|
+
this.#cellProxies.add(state);
|
|
437
|
+
const path = resolveRelative(this.#logicalBase, proxy.glb);
|
|
438
|
+
if (this.#loadModel === undefined) this.#host.retainModelPaths([path], state.retainedPaths);
|
|
439
|
+
void this.#host
|
|
440
|
+
.load(path, () => this.#host.cellLive(cell, cell.generation))
|
|
441
|
+
.then(
|
|
442
|
+
(model) => {
|
|
443
|
+
state.pending = false;
|
|
444
|
+
state.model = model;
|
|
445
|
+
if (
|
|
446
|
+
!this.#host.cellLive(cell, cell.generation) ||
|
|
447
|
+
state.cancelled ||
|
|
448
|
+
model === undefined
|
|
449
|
+
) {
|
|
450
|
+
state.cancelled = true;
|
|
451
|
+
this.releaseCellProxy(state);
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
try {
|
|
455
|
+
if (measureCellProxy(model, proxy, true) > state.bytes)
|
|
456
|
+
throw new Error("Cell proxy exceeds its reserved decoded budget.");
|
|
457
|
+
} catch (error) {
|
|
458
|
+
this.#hlodReason = `${state.cell.key}: ${String(error instanceof Error ? error.message : error).slice(0, 160)}`;
|
|
459
|
+
state.cancelled = true;
|
|
460
|
+
this.#hlodRefused++;
|
|
461
|
+
this.releaseCellProxy(state);
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
// Preparation waits for an admission unit on a rendered update.
|
|
465
|
+
},
|
|
466
|
+
() => {
|
|
467
|
+
state.pending = false;
|
|
468
|
+
state.cancelled = true;
|
|
469
|
+
this.#hlodFailures++;
|
|
470
|
+
this.releaseCellProxy(state);
|
|
471
|
+
},
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
#prepareCellProxy(state: ICellProxyState): void {
|
|
476
|
+
try {
|
|
477
|
+
const prepared = prepareCellProxy(
|
|
478
|
+
state.model as Object3D,
|
|
479
|
+
state.cell.cell.proxy as NonNullable<IWorldCell["proxy"]>,
|
|
480
|
+
state.bytes,
|
|
481
|
+
);
|
|
482
|
+
state.prepared = prepared;
|
|
483
|
+
if (prepared.bytes > state.bytes)
|
|
484
|
+
throw new Error("Cell proxy exceeds its reserved decoded budget.");
|
|
485
|
+
this.#hlodBytes += prepared.bytes - state.bytes;
|
|
486
|
+
state.bytes = prepared.bytes;
|
|
487
|
+
prepared.root.name = `world-cell-proxy:${state.cell.key}`;
|
|
488
|
+
for (const mesh of prepared.meshes) mesh.receiveShadow = this.#receiveShadow;
|
|
489
|
+
this.#host.add(prepared.root);
|
|
490
|
+
} catch (error) {
|
|
491
|
+
this.#hlodReason = `${state.cell.key}: ${String(error instanceof Error ? error.message : error).slice(0, 160)}`;
|
|
492
|
+
state.cancelled = true;
|
|
493
|
+
this.#hlodRefused++;
|
|
494
|
+
this.releaseCellProxy(state);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
#compileCellProxy(state: ICellProxyState, renderer: IRendererLike, camera: Camera): void {
|
|
499
|
+
const prepared = state.prepared;
|
|
500
|
+
if (prepared === undefined) return;
|
|
501
|
+
const targetScene = this.#host.targetScene();
|
|
502
|
+
if (targetScene === undefined) return;
|
|
503
|
+
state.compiling = true;
|
|
504
|
+
// No timeout counts as cancellation. This original promise owns every staged resource until it
|
|
505
|
+
// settles, even if the cell/world has already left. Refusal keeps source and releases afterward.
|
|
506
|
+
void Promise.resolve()
|
|
507
|
+
.then(() =>
|
|
508
|
+
warmUpSceneWithCompletion(renderer, prepared.root, camera, {
|
|
509
|
+
budgetMs: CHUNK_WARM_TIMEOUT_MS,
|
|
510
|
+
renderPasses: false,
|
|
511
|
+
targetScene,
|
|
512
|
+
}),
|
|
513
|
+
)
|
|
514
|
+
.then(
|
|
515
|
+
async ({ report, completion }) => {
|
|
516
|
+
if (
|
|
517
|
+
report.unsupported ||
|
|
518
|
+
report.timedOut ||
|
|
519
|
+
report.abandoned > 0 ||
|
|
520
|
+
(renderer.warmUpWorldPass !== undefined && report.passes?.includes("main") !== true)
|
|
521
|
+
) {
|
|
522
|
+
// The report honors the 2 s budget, but Three may still be compiling a pipeline. Keep
|
|
523
|
+
// the staged geometry and its reservation alive until that original promise settles.
|
|
524
|
+
state.cancelled = true;
|
|
525
|
+
this.#hlodFailures++;
|
|
526
|
+
await completion.catch(() => undefined);
|
|
527
|
+
state.compiling = false;
|
|
528
|
+
this.releaseCellProxy(state);
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
await completion;
|
|
532
|
+
renderer.warmUpScopeSettled?.(
|
|
533
|
+
`cell:${state.cell.key}:${String(state.cell.generation)}`,
|
|
534
|
+
prepared.root,
|
|
535
|
+
);
|
|
536
|
+
state.compiling = false;
|
|
537
|
+
if (state.cancelled || !this.#host.cellLive(state.cell, state.cell.generation)) {
|
|
538
|
+
state.cancelled = true;
|
|
539
|
+
this.releaseCellProxy(state);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
this.#uploadAttributes(prepared.root);
|
|
543
|
+
state.compiled = true;
|
|
544
|
+
// Three calls render hooks during compile too, and may swallow pipeline errors. Observe a
|
|
545
|
+
// matching main-camera backend submission only AFTER this compile settles. Zero-count
|
|
546
|
+
// submission proves readiness of that route, not full-triangle execution or visual quality.
|
|
547
|
+
for (const mesh of prepared.meshes) {
|
|
548
|
+
let before: number | undefined;
|
|
549
|
+
const calls = (): number | undefined => {
|
|
550
|
+
try {
|
|
551
|
+
const info = renderer.info as { render?: { drawCalls?: number; calls?: number } };
|
|
552
|
+
const value = info.render?.drawCalls ?? info.render?.calls;
|
|
553
|
+
return Number.isSafeInteger(value) && (value as number) >= 0 ? value : undefined;
|
|
554
|
+
} catch {
|
|
555
|
+
return undefined;
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
mesh.onBeforeRender = (_raw, _scene, drawnCamera) => {
|
|
559
|
+
before =
|
|
560
|
+
drawnCamera === this.#camera && renderer.compiling !== true ? calls() : undefined;
|
|
561
|
+
};
|
|
562
|
+
markEngineRenderHook(mesh.onBeforeRender);
|
|
563
|
+
mesh.onAfterRender = (_raw, _scene, drawnCamera) => {
|
|
564
|
+
const after = calls();
|
|
565
|
+
if (
|
|
566
|
+
drawnCamera === this.#camera &&
|
|
567
|
+
renderer.compiling !== true &&
|
|
568
|
+
before !== undefined &&
|
|
569
|
+
after !== undefined &&
|
|
570
|
+
after > before
|
|
571
|
+
)
|
|
572
|
+
state.observed.add(mesh);
|
|
573
|
+
before = undefined;
|
|
574
|
+
};
|
|
575
|
+
markEngineRenderHook(mesh.onAfterRender);
|
|
576
|
+
}
|
|
577
|
+
},
|
|
578
|
+
() => {
|
|
579
|
+
state.compiling = false;
|
|
580
|
+
state.cancelled = true;
|
|
581
|
+
this.#hlodFailures++;
|
|
582
|
+
this.releaseCellProxy(state);
|
|
583
|
+
},
|
|
584
|
+
)
|
|
585
|
+
.catch(() => {
|
|
586
|
+
state.compiling = false;
|
|
587
|
+
state.cancelled = true;
|
|
588
|
+
this.#hlodFailures++;
|
|
589
|
+
this.releaseCellProxy(state);
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
#cellSourceDrawn(mesh: Mesh): boolean {
|
|
594
|
+
if (
|
|
595
|
+
this.#camera === undefined ||
|
|
596
|
+
(mesh instanceof InstancedMesh && mesh.count === 0) ||
|
|
597
|
+
!this.#camera.layers.test(mesh.layers) ||
|
|
598
|
+
Array.isArray(mesh.material) ||
|
|
599
|
+
!mesh.material.visible
|
|
600
|
+
)
|
|
601
|
+
return false;
|
|
602
|
+
for (let object: Object3D | null = mesh; object !== null; object = object.parent)
|
|
603
|
+
if (!object.visible) return false;
|
|
604
|
+
if (mesh.frustumCulled && !this.#host.intersectsObject(mesh)) return false;
|
|
605
|
+
return true;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
#cellProxySource(state: ICellProxyState): {
|
|
609
|
+
compatible: boolean;
|
|
610
|
+
triangles: number;
|
|
611
|
+
draws: number;
|
|
612
|
+
meshes: Mesh[];
|
|
613
|
+
} {
|
|
614
|
+
const cell = state.cell;
|
|
615
|
+
const meshes = [...cell.mainChunkMeshes];
|
|
616
|
+
for (const chunk of cell.chunks)
|
|
617
|
+
chunk.traverse((node) => {
|
|
618
|
+
if (node instanceof Mesh && isLodJoinProxy(node)) meshes.push(node);
|
|
619
|
+
});
|
|
620
|
+
const materials = new Set<Material>();
|
|
621
|
+
const staticInstances = new Set<Mesh>();
|
|
622
|
+
for (const [mesh, baseline] of cell.chunkInstances)
|
|
623
|
+
if (
|
|
624
|
+
mesh.instanceMatrix === baseline.matrix &&
|
|
625
|
+
mesh.instanceMatrix.version === baseline.version &&
|
|
626
|
+
mesh.count === baseline.count
|
|
627
|
+
)
|
|
628
|
+
staticInstances.add(mesh);
|
|
629
|
+
let triangles = 0;
|
|
630
|
+
let draws = 0;
|
|
631
|
+
let compatible =
|
|
632
|
+
cellLoaded(cell) && (state.cell.cell.proxy?.scope !== "cell" || !this.#host.gpuSceneOn());
|
|
633
|
+
for (const mesh of meshes) {
|
|
634
|
+
if (
|
|
635
|
+
!cellProxySourceSafe(mesh, staticInstances) ||
|
|
636
|
+
Array.isArray(mesh.material) ||
|
|
637
|
+
!cellProxyGeometrySafe(mesh.geometry) ||
|
|
638
|
+
!cellProxyMaterialSafe(mesh.material as Material)
|
|
639
|
+
)
|
|
640
|
+
compatible = false;
|
|
641
|
+
else materials.add(mesh.material as Material);
|
|
642
|
+
if (this.#cellSourceDrawn(mesh)) {
|
|
643
|
+
triangles +=
|
|
644
|
+
levelTriangles(mesh.geometry) * (mesh instanceof InstancedMesh ? mesh.count : 1);
|
|
645
|
+
draws++;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
if (state.cell.cell.proxy?.scope === "cell") {
|
|
649
|
+
const counts = new Map<SharedBatch, number>();
|
|
650
|
+
const costs = new Map<SharedBatch, number>();
|
|
651
|
+
for (const run of cell.cell.runs) {
|
|
652
|
+
const asset = this.#host.asset(run.asset);
|
|
653
|
+
if (
|
|
654
|
+
asset === undefined ||
|
|
655
|
+
!asset.hlodSafe ||
|
|
656
|
+
asset.impostor !== undefined ||
|
|
657
|
+
asset.definition.maxDistance !== undefined ||
|
|
658
|
+
this.#placementReach !== undefined
|
|
659
|
+
) {
|
|
660
|
+
compatible = false;
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
for (const [part, shape] of (asset.levels[0] ?? []).entries()) {
|
|
664
|
+
const entries = cell.batches.filter((entry) => entry.run === run && entry.part === part);
|
|
665
|
+
if (
|
|
666
|
+
entries.length !== 1 ||
|
|
667
|
+
entries[0]?.level !== 0 ||
|
|
668
|
+
entries[0]?.batch.count !== run.count ||
|
|
669
|
+
!cellProxyMaterialSafe(shape.material)
|
|
670
|
+
) {
|
|
671
|
+
compatible = false;
|
|
672
|
+
continue;
|
|
673
|
+
}
|
|
674
|
+
materials.add(shape.material);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
for (const entry of cell.batches) {
|
|
678
|
+
if (entry.shared === undefined || entry.segment < 0 || entry.level !== 0) {
|
|
679
|
+
compatible = false;
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
counts.set(entry.shared, (counts.get(entry.shared) ?? 0) + entry.batch.count);
|
|
683
|
+
costs.set(
|
|
684
|
+
entry.shared,
|
|
685
|
+
(costs.get(entry.shared) ?? 0) + levelTriangles(entry.batch.geometry) * entry.batch.count,
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
for (const [shared, count] of counts) {
|
|
689
|
+
// Retain the segment, hence near restoration is a flag write with no budget dependency.
|
|
690
|
+
// A neighboring holder keeps this key drawn; such a key cannot be retired for this cell.
|
|
691
|
+
if (shared.live !== count) compatible = false;
|
|
692
|
+
else {
|
|
693
|
+
if (this.#cellSourceDrawn(shared.mesh)) {
|
|
694
|
+
draws++;
|
|
695
|
+
triangles += costs.get(shared) ?? 0;
|
|
696
|
+
}
|
|
697
|
+
meshes.push(shared.mesh);
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
for (const mesh of state.prepared?.meshes ?? []) {
|
|
702
|
+
if (
|
|
703
|
+
![...materials].some((material) =>
|
|
704
|
+
cellProxyMaterialMatches(mesh.material as Material, material),
|
|
705
|
+
)
|
|
706
|
+
)
|
|
707
|
+
compatible = false;
|
|
708
|
+
}
|
|
709
|
+
return { compatible, triangles, draws, meshes };
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
restoreCellProxySource(state: ICellProxyState): void {
|
|
713
|
+
for (const [mesh, visible] of state.hidden) mesh.visible = visible;
|
|
714
|
+
state.hidden.clear();
|
|
715
|
+
for (const [mesh, mask] of state.mainMasks) setModelLodLayers(mesh, mask);
|
|
716
|
+
state.mainMasks.clear();
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
showCellProxy(state: ICellProxyState, active: boolean, meshes?: readonly Mesh[]): void {
|
|
720
|
+
const prepared = state.prepared;
|
|
721
|
+
if (prepared === undefined) return;
|
|
722
|
+
const changed = state.active !== active;
|
|
723
|
+
// AutoLOD owns chunk visibility. Retire only its main pass, retaining the selected source
|
|
724
|
+
// geometry as a caster; joined rungs inherit this same pass ownership on activation.
|
|
725
|
+
this.restoreCellProxySource(state);
|
|
726
|
+
if (active)
|
|
727
|
+
for (const mesh of meshes ?? this.#cellProxySource(state).meshes) {
|
|
728
|
+
if (mesh.layers.isEnabled(0)) {
|
|
729
|
+
if (state.cell.mainChunkMeshes.includes(mesh) || isLodJoinProxy(mesh)) {
|
|
730
|
+
state.mainMasks.set(mesh, mesh.layers.mask);
|
|
731
|
+
let mask = mesh.layers.mask & ~1;
|
|
732
|
+
if (mesh.castShadow)
|
|
733
|
+
mask |= (1 << VIRTUAL_SHADOW_CASTER_LAYER) | (1 << VIRTUAL_SHADOW_WIDE_CASTER_LAYER);
|
|
734
|
+
setModelLodLayers(mesh, mask);
|
|
735
|
+
} else {
|
|
736
|
+
state.hidden.set(mesh, mesh.visible);
|
|
737
|
+
mesh.visible = false;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
prepared.root.visible = active || !this.#proxyObserved(state);
|
|
742
|
+
prepared.meshes.forEach((mesh, index) => {
|
|
743
|
+
const range = prepared.ranges[index];
|
|
744
|
+
if (range !== undefined) mesh.geometry.setDrawRange(range.start, active ? range.count : 0);
|
|
745
|
+
});
|
|
746
|
+
state.active = active;
|
|
747
|
+
if (changed) {
|
|
748
|
+
this.#host.invalidateBundles(state.cell.key);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
stepCellProxies(
|
|
753
|
+
cells: Iterable<IResidentCell>,
|
|
754
|
+
matrixWorld: Matrix4,
|
|
755
|
+
budget: IAdmissionBudget,
|
|
756
|
+
renderer?: IRendererLike,
|
|
757
|
+
camera?: Camera,
|
|
758
|
+
): void {
|
|
759
|
+
if (!this.#hlod) return;
|
|
760
|
+
for (const cell of cells) {
|
|
761
|
+
if (cell.cell.proxy === undefined) continue;
|
|
762
|
+
if (cell.proxy === undefined) {
|
|
763
|
+
if (cellLoaded(cell)) budget.admit(() => this.#beginCellProxy(cell));
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
const state = cell.proxy;
|
|
767
|
+
if (state.cancelled || state.pending) continue;
|
|
768
|
+
if (state.model !== undefined && state.prepared === undefined)
|
|
769
|
+
budget.admit(() => this.#prepareCellProxy(state));
|
|
770
|
+
if (state.cancelled || state.prepared === undefined) continue;
|
|
771
|
+
if (!state.compiled && !state.compiling && renderer !== undefined && camera !== undefined)
|
|
772
|
+
budget.admit(() => this.#compileCellProxy(state, renderer, camera));
|
|
773
|
+
this.restoreCellProxySource(state);
|
|
774
|
+
const source = this.#cellProxySource(state);
|
|
775
|
+
const scale = cellProxyWorldScale(matrixWorld);
|
|
776
|
+
const sphere = this.#host.worldSphere(state.prepared.bounds);
|
|
777
|
+
sphere.radius = state.prepared.bounds.radius * scale;
|
|
778
|
+
const view =
|
|
779
|
+
camera === undefined
|
|
780
|
+
? undefined
|
|
781
|
+
: selectLodFor(camera, sphere, Reflect.get(camera, "near") ?? LOD_NEAR_PLANE);
|
|
782
|
+
const height = renderer?.domElement.height;
|
|
783
|
+
const selected = state.selector.select({
|
|
784
|
+
sourceReady: cellLoaded(cell),
|
|
785
|
+
proxyReady: state.compiled && this.#proxyObserved(state),
|
|
786
|
+
sourceCompatible: source.compatible && scale > 0,
|
|
787
|
+
sourceTriangles: source.triangles,
|
|
788
|
+
replaceableSourceDraws: source.draws,
|
|
789
|
+
views:
|
|
790
|
+
camera !== undefined &&
|
|
791
|
+
height !== undefined &&
|
|
792
|
+
Number.isFinite(height) &&
|
|
793
|
+
height > 0 &&
|
|
794
|
+
view !== undefined
|
|
795
|
+
? [
|
|
796
|
+
{
|
|
797
|
+
camera,
|
|
798
|
+
viewportHeight: height,
|
|
799
|
+
...view,
|
|
800
|
+
finest: Reflect.get(camera, "isOrthographicCamera") === true || view.degenerate,
|
|
801
|
+
},
|
|
802
|
+
]
|
|
803
|
+
: [],
|
|
804
|
+
// Collapsing a valid Three transform retains detail; a zero error scale cannot select LOD.
|
|
805
|
+
errorScale: scale === 0 ? 1 : scale,
|
|
806
|
+
maxPixelError: this.#autoLodMaxPixelError,
|
|
807
|
+
});
|
|
808
|
+
this.showCellProxy(state, selected === "proxy", source.meshes);
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
startChunkLoad(cell: IResidentCell): void {
|
|
813
|
+
const generation = cell.generation;
|
|
814
|
+
const paths: string[] = [];
|
|
815
|
+
for (const chunk of cell.cell.chunks ?? [])
|
|
816
|
+
paths.push(resolveRelative(this.#logicalBase, chunk));
|
|
817
|
+
// A chunk's source is the same loader cache entry an asset's level would be: held while this
|
|
818
|
+
// cell is resident and handed back with the last cell naming the path, through the one
|
|
819
|
+
// `#modelPaths` map. A `loadModel` override parses per load and reaches no cache, so it owns
|
|
820
|
+
// nothing here either; see `#releaseModelPaths`.
|
|
821
|
+
if (this.#loadModel === undefined) this.#host.retainModelPaths(paths, cell.retainedModelPaths);
|
|
822
|
+
loadAll(paths, (path) => this.#host.load(path, () => this.#host.cellLive(cell, generation)), {
|
|
823
|
+
marker: false,
|
|
824
|
+
}).then(
|
|
825
|
+
(models) => {
|
|
826
|
+
void this.#attachChunks(
|
|
827
|
+
cell,
|
|
828
|
+
generation,
|
|
829
|
+
models.filter((model): model is Object3D => model !== undefined),
|
|
830
|
+
);
|
|
831
|
+
},
|
|
832
|
+
(error: unknown) => {
|
|
833
|
+
cell.chunkFailed = true;
|
|
834
|
+
this.#host.addFailures(1);
|
|
835
|
+
this.#host.reportCellFailure("chunk", cell.key, paths, error);
|
|
836
|
+
},
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
/**
|
|
841
|
+
* Hand the chunk's own buffers to the renderer now, and report how many it took.
|
|
842
|
+
*
|
|
843
|
+
* A compile builds pipelines, not geometry, so the bake's new attributes reached the device on
|
|
844
|
+
* whichever frame first drew them — 826 buffers and 228.8 MB on an 8 s walk, 33 of them over a
|
|
845
|
+
* megabyte, lining up exactly with the p95 and max frame spikes. The backend's own attribute
|
|
846
|
+
* creation is idempotent, so this is a move rather than a second upload, and a renderer with no
|
|
847
|
+
* such seam answers 0 and behaves as it did.
|
|
848
|
+
*/
|
|
849
|
+
#uploadAttributes(chunk: Object3D): number {
|
|
850
|
+
const upload = this.#renderer?.uploadAttributes;
|
|
851
|
+
if (upload === undefined) return 0;
|
|
852
|
+
const geometries = new Set<BufferGeometry>();
|
|
853
|
+
chunk.traverse((object) => {
|
|
854
|
+
const mesh = object as Mesh;
|
|
855
|
+
if (mesh.isMesh === true) geometries.add(mesh.geometry);
|
|
856
|
+
});
|
|
857
|
+
return upload(geometries);
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
/**
|
|
861
|
+
* Hand this chunk's own textures to the device now, and report how many it took.
|
|
862
|
+
*
|
|
863
|
+
* The same argument as the buffers above, one resource class over: a compile builds pipelines, not
|
|
864
|
+
* pixels, so the bake's textures reached the device inside `_renderObjectDirect` on the frame the
|
|
865
|
+
* chunk first drew — 87 ms of a 94 ms first draw in one frame of a Machinefall map-walk, and 79 ms
|
|
866
|
+
* of 131 ms in another. A texture the world already shares with what is on screen is one early
|
|
867
|
+
* return inside three and no work here.
|
|
868
|
+
*/
|
|
869
|
+
#uploadTextures(chunk: Object3D): number {
|
|
870
|
+
const upload = this.#renderer?.uploadTextures;
|
|
871
|
+
if (upload === undefined) return 0;
|
|
872
|
+
return upload(chunk);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Compile a merged chunk before anything can draw it, and report whether the compile came back.
|
|
877
|
+
*
|
|
878
|
+
* A chunk's first draw is the most expensive frame a streaming world has: its own first draw built
|
|
879
|
+
* nodes and a render pipeline synchronously, and one chunk cost **248 ms of a 291 ms walk** in the
|
|
880
|
+
* census that found it. The engine already owns this work — `warmUpScene` drives the renderer's
|
|
881
|
+
* own `compileAsync`, and this is that same call with the world as the target scene, so the chunk's
|
|
882
|
+
* materials resolve the same lights, environment and clipping the real frame will resolve them
|
|
883
|
+
* against. A pipeline compiled against no lights would be a different pipeline, and the frame would
|
|
884
|
+
* still pay for it.
|
|
885
|
+
*
|
|
886
|
+
* It is bounded by `within`, the warm-up's own bound: a compile that never settles is abandoned and
|
|
887
|
+
* the chunk is attached anyway, because a missing chunk is worse than a slow frame. A world that is
|
|
888
|
+
* in no scene is skipped outright — nothing projects it, so a compile would build nothing, which is
|
|
889
|
+
* the same answer `load()` gets, where the loading screen is about to draw it anyway.
|
|
890
|
+
*/
|
|
891
|
+
async #warmChunk(chunk: Object3D, scopeLabel: string): Promise<boolean> {
|
|
892
|
+
const renderer = this.#renderer;
|
|
893
|
+
const camera = this.#camera;
|
|
894
|
+
if (renderer === undefined || camera === undefined || this.#host.parent() === null)
|
|
895
|
+
return false;
|
|
896
|
+
const targetScene = this.#host.targetScene();
|
|
897
|
+
if (targetScene === undefined) return false;
|
|
898
|
+
// The rejected compile is a pipeline this warm-up could not build, not a reason to fail the
|
|
899
|
+
// admission: the frame that needs it will try again and fail there, where the error belongs.
|
|
900
|
+
return warmUpScene(renderer, chunk, camera, {
|
|
901
|
+
budgetMs: CHUNK_WARM_TIMEOUT_MS,
|
|
902
|
+
renderPasses: false,
|
|
903
|
+
targetScene,
|
|
904
|
+
yieldFrame: yieldToHost,
|
|
905
|
+
}).then(
|
|
906
|
+
(report) => {
|
|
907
|
+
const settled =
|
|
908
|
+
!report.unsupported &&
|
|
909
|
+
report.observed.status === "complete" &&
|
|
910
|
+
!report.timedOut &&
|
|
911
|
+
report.abandoned === 0 &&
|
|
912
|
+
(renderer.warmUpWorldPass === undefined || report.passes?.includes("main") === true);
|
|
913
|
+
if (settled) renderer.warmUpScopeSettled?.(scopeLabel, chunk);
|
|
914
|
+
return settled;
|
|
915
|
+
},
|
|
916
|
+
() => false,
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
async #attachChunks(
|
|
921
|
+
cell: IResidentCell,
|
|
922
|
+
generation: number,
|
|
923
|
+
models: readonly Object3D[],
|
|
924
|
+
): Promise<void> {
|
|
925
|
+
const live = (): boolean => this.#host.cellLive(cell, generation);
|
|
926
|
+
let attached = 0;
|
|
927
|
+
// Merged and shaped, not yet in the world: the compile below runs between the two, and a cell
|
|
928
|
+
// that left the ring in the middle takes its prepared chunks with it.
|
|
929
|
+
const prepared: { readonly object: Object3D; readonly proxies: number }[] = [];
|
|
930
|
+
try {
|
|
931
|
+
const report = await addInSlices(
|
|
932
|
+
models,
|
|
933
|
+
(object) => {
|
|
934
|
+
if (!live()) return;
|
|
935
|
+
object.name = CHUNK_NAME;
|
|
936
|
+
// One draw per material before the chunk is ever added, so the main pass and every shadow
|
|
937
|
+
// level that redraws it submit the chunk's materials rather than its nodes. A refusal
|
|
938
|
+
// leaves the chunk exactly as authored and is counted: it never costs the chunk itself.
|
|
939
|
+
const merge = mergeChunk(
|
|
940
|
+
object,
|
|
941
|
+
this.#chunkMergeMaxTriangles,
|
|
942
|
+
// A `loadModel` override parses per load, so a chunk's shapes are this world's to
|
|
943
|
+
// release; `assets.model` caches by path and hands the same scene back, so they are not.
|
|
944
|
+
this.#loadModel !== undefined,
|
|
945
|
+
this.#castShadowLevels > 0,
|
|
946
|
+
this.#proxyMaterials,
|
|
947
|
+
);
|
|
948
|
+
// Where the frame budget counts this chunk's main-pass draws (`RenderPassBudget`). Written
|
|
949
|
+
// after the bake and over the whole subtree, because that is the only point where all three
|
|
950
|
+
// outcomes exist at once: a merged group, an instanced mesh the bake kept, and a chunk it
|
|
951
|
+
// refused whole are all one chunk's draws. The proxies the bake built are off layer 0 and
|
|
952
|
+
// keep the `proxies` origin they were given.
|
|
953
|
+
object.traverse((node) => {
|
|
954
|
+
if ((node as Mesh).isMesh !== true || node.layers.isEnabled(0) === false) return;
|
|
955
|
+
node.userData.tnDrawSource = "chunks";
|
|
956
|
+
});
|
|
957
|
+
// The buffers the bake just made, uploaded here rather than on the chunk's first draw,
|
|
958
|
+
// which is the frame that uploads them today: 228.8 MB over 826 `createAttribute` calls
|
|
959
|
+
// in a browser walk, and up to 230 ms inside the frames that first submitted them.
|
|
960
|
+
// `addInSlices` is the admission budget, so this is one chunk per update, never a batch.
|
|
961
|
+
const uploaded = this.#uploadAttributes(object);
|
|
962
|
+
this.#uploadTextures(object);
|
|
963
|
+
if (merge === undefined) this.#host.addFailures(1);
|
|
964
|
+
else {
|
|
965
|
+
this.#host.addFailures(merge.failed);
|
|
966
|
+
console.info(
|
|
967
|
+
`TN_WORLD_CHUNK_MERGE meshes=${String(merge.meshes)} draws=${String(merge.draws)} ` +
|
|
968
|
+
`bytes=${String(merge.bytes)} uploaded=${String(uploaded)} ` +
|
|
969
|
+
`instancedExpanded=${String(merge.expanded)} keptInstanced=${String(merge.keptInstanced)} ` +
|
|
970
|
+
`shadowDraws=${String(merge.shadowDraws)}`,
|
|
971
|
+
);
|
|
972
|
+
}
|
|
973
|
+
// Hand-placed chunks are the buildings and set dressing: full shape, so they cast with the
|
|
974
|
+
// near scatter and receive like everything else when the game asks for shadows. A merged
|
|
975
|
+
// opaque mesh the proxy covers keeps receiving and stops casting: the depth pass reads the
|
|
976
|
+
// proxy, so casting from both would pay the bill the proxy was built to remove. The proxy
|
|
977
|
+
// itself is off layer 0 and already carries what the level cameras need of it.
|
|
978
|
+
if (this.#castShadowLevels > 0 || this.#receiveShadow) {
|
|
979
|
+
const covered = new Set(merge?.shadowCovered);
|
|
980
|
+
object.traverse((node) => {
|
|
981
|
+
if (node.layers.isEnabled(0) === false) return;
|
|
982
|
+
node.castShadow = this.#castShadowLevels > 0 && !covered.has(node as Mesh);
|
|
983
|
+
node.receiveShadow = this.#receiveShadow;
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
prepared.push({ object, proxies: merge?.shadowDraws ?? 0 });
|
|
987
|
+
},
|
|
988
|
+
{ marker: false, while: live },
|
|
989
|
+
);
|
|
990
|
+
if (report.stopped)
|
|
991
|
+
for (let i = report.added; i < models.length; i += 1)
|
|
992
|
+
this.#host.addFailures(this.#host.disposeLoaded(models[i] as Object3D));
|
|
993
|
+
for (const { object, proxies } of prepared) {
|
|
994
|
+
// The compile is the whole point of the wait, and the wait is the only thing between the
|
|
995
|
+
// merge and the attach. Everything the world does not own is released when the cell that
|
|
996
|
+
// asked for the chunk is gone, so a chunk that arrives after its cell left is torn down here
|
|
997
|
+
// rather than compiled and thrown at a world that is not looking.
|
|
998
|
+
if (live())
|
|
999
|
+
await this.#warmChunk(object, `chunk:${cell.key}:${String(cell.chunks.length)}`);
|
|
1000
|
+
if (!live()) {
|
|
1001
|
+
this.#host.addFailures(this.#host.disposeLoaded(object));
|
|
1002
|
+
continue;
|
|
1003
|
+
}
|
|
1004
|
+
cell.chunks.push(object);
|
|
1005
|
+
object.traverse((node) => {
|
|
1006
|
+
if (node instanceof Mesh && node.layers.isEnabled(0)) cell.mainChunkMeshes.push(node);
|
|
1007
|
+
if (node instanceof InstancedMesh && node.layers.isEnabled(0))
|
|
1008
|
+
cell.chunkInstances.set(node, {
|
|
1009
|
+
matrix: node.instanceMatrix,
|
|
1010
|
+
version: node.instanceMatrix.version,
|
|
1011
|
+
count: node.count,
|
|
1012
|
+
});
|
|
1013
|
+
});
|
|
1014
|
+
// The chunk's own box, from the merged geometry's boxes rather than its vertices: a chunk
|
|
1015
|
+
// is loaded once and both consumers of it are only asked to be no wider than the chunk. Read
|
|
1016
|
+
// before `#addChunk`, which may hand part of the subtree to a cell's record and leave the
|
|
1017
|
+
// rest behind, so the box is the chunk's whatever ends up where.
|
|
1018
|
+
const box =
|
|
1019
|
+
proxies > 0 || this.#bundlesWanted ? new Box3().setFromObject(object, false) : undefined;
|
|
1020
|
+
this.#host.addChunk(cell, object);
|
|
1021
|
+
// A loaded chunk is placed once and never rewritten: its transforms, geometry and material
|
|
1022
|
+
// are the ones the export gave it, so it is the one subtree here with nothing to announce.
|
|
1023
|
+
markStatic(object);
|
|
1024
|
+
// The proxies' shadow pipelines are the one thing a compile cannot build — three creates a
|
|
1025
|
+
// shadow variant on the first pass that draws the caster, and the pass is the levels'. So
|
|
1026
|
+
// the levels are told a caster arrived, exactly as a prewarmed caster cluster tells them:
|
|
1027
|
+
// the first level whose window covers the chunk submits the proxy and builds it. With one
|
|
1028
|
+
// material per `side` world-wide, that is one pipeline for the world's first chunk of each
|
|
1029
|
+
// side and a node binding for every chunk after it, not a compile per chunk.
|
|
1030
|
+
this.#host.publishChunkBounds(cell, box, proxies);
|
|
1031
|
+
attached += 1;
|
|
1032
|
+
}
|
|
1033
|
+
} catch (error) {
|
|
1034
|
+
cell.chunkFailed = true;
|
|
1035
|
+
this.#host.addFailures(1);
|
|
1036
|
+
this.#host.reportCellFailure(
|
|
1037
|
+
"attach",
|
|
1038
|
+
cell.key,
|
|
1039
|
+
Array.from(cell.cell.chunks ?? [], (chunk) => resolveRelative(this.#logicalBase, chunk)),
|
|
1040
|
+
error,
|
|
1041
|
+
);
|
|
1042
|
+
for (let i = attached; i < models.length; i += 1)
|
|
1043
|
+
this.#host.addFailures(this.#host.disposeLoaded(models[i] as Object3D));
|
|
1044
|
+
}
|
|
1045
|
+
if (this.#host.cellLive(cell, generation) && !cell.chunkFailed) cell.chunksLoaded = true;
|
|
1046
|
+
}
|
|
1047
|
+
get renderer(): IRendererLike | undefined {
|
|
1048
|
+
return this.#renderer;
|
|
1049
|
+
}
|
|
1050
|
+
set renderer(value: IRendererLike | undefined) {
|
|
1051
|
+
this.#renderer = value;
|
|
1052
|
+
}
|
|
1053
|
+
get camera(): Camera | undefined {
|
|
1054
|
+
return this.#camera;
|
|
1055
|
+
}
|
|
1056
|
+
set camera(value: Camera | undefined) {
|
|
1057
|
+
this.#camera = value;
|
|
1058
|
+
}
|
|
1059
|
+
readonly #host: ICellChunksHost;
|
|
1060
|
+
readonly #loadModel: ((url: string) => Promise<Object3D>) | undefined;
|
|
1061
|
+
readonly #byteBudget: number;
|
|
1062
|
+
readonly #logicalBase: string;
|
|
1063
|
+
readonly #receiveShadow: boolean;
|
|
1064
|
+
readonly #placementReach: Float32Array | undefined;
|
|
1065
|
+
readonly #autoLodMaxPixelError: number;
|
|
1066
|
+
readonly #chunkMergeMaxTriangles: number;
|
|
1067
|
+
readonly #castShadowLevels: number;
|
|
1068
|
+
readonly #bundlesWanted: boolean;
|
|
1069
|
+
constructor(hlod: boolean, options: ICellChunksOptions, host: ICellChunksHost) {
|
|
1070
|
+
this.#hlod = hlod;
|
|
1071
|
+
this.#host = host;
|
|
1072
|
+
this.#loadModel = options.loadModel;
|
|
1073
|
+
this.#byteBudget = options.byteBudget;
|
|
1074
|
+
this.#logicalBase = options.logicalBase;
|
|
1075
|
+
this.#receiveShadow = options.receiveShadow;
|
|
1076
|
+
this.#placementReach = options.placementReach;
|
|
1077
|
+
this.#autoLodMaxPixelError = options.autoLodMaxPixelError;
|
|
1078
|
+
this.#chunkMergeMaxTriangles = options.chunkMergeMaxTriangles;
|
|
1079
|
+
this.#castShadowLevels = options.castShadowLevels;
|
|
1080
|
+
this.#bundlesWanted = options.bundlesWanted;
|
|
1081
|
+
}
|
|
1082
|
+
get bytes(): number {
|
|
1083
|
+
return this.#hlodBytes;
|
|
1084
|
+
}
|
|
1085
|
+
restoreSources(): void {
|
|
1086
|
+
for (const state of this.#cellProxies) this.restoreCellProxySource(state);
|
|
1087
|
+
}
|
|
1088
|
+
releaseMaterials(): void {
|
|
1089
|
+
for (const material of this.#proxyMaterials.values())
|
|
1090
|
+
this.#host.addFailures(release(material) ? 1 : 0);
|
|
1091
|
+
this.#proxyMaterials.clear();
|
|
1092
|
+
}
|
|
1093
|
+
stats() {
|
|
1094
|
+
return {
|
|
1095
|
+
on: this.#hlod,
|
|
1096
|
+
loaded: [...this.#cellProxies].filter(
|
|
1097
|
+
(state) => state.prepared !== undefined && !state.cancelled,
|
|
1098
|
+
).length,
|
|
1099
|
+
compiled: [...this.#cellProxies].filter((state) => state.compiled && !state.cancelled).length,
|
|
1100
|
+
observed: [...this.#cellProxies].filter(
|
|
1101
|
+
(state) => this.#proxyObserved(state) && !state.cancelled,
|
|
1102
|
+
).length,
|
|
1103
|
+
active: [...this.#cellProxies].filter((state) => state.active).length,
|
|
1104
|
+
pending: [...this.#cellProxies].filter(
|
|
1105
|
+
(state) =>
|
|
1106
|
+
state.pending ||
|
|
1107
|
+
state.compiling ||
|
|
1108
|
+
(!state.cancelled && !state.compiled && state.prepared !== undefined),
|
|
1109
|
+
).length,
|
|
1110
|
+
bytes: this.#hlodBytes,
|
|
1111
|
+
refused: this.#hlodRefused,
|
|
1112
|
+
reason: this.#hlodReason,
|
|
1113
|
+
failures: this.#hlodFailures,
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
evictProxy(cell: IResidentCell): void {
|
|
1117
|
+
if (cell.proxy !== undefined) {
|
|
1118
|
+
this.showCellProxy(cell.proxy, false);
|
|
1119
|
+
cell.proxy.cancelled = true;
|
|
1120
|
+
cell.proxy.prepared?.root.removeFromParent();
|
|
1121
|
+
if (!cell.proxy.pending && !cell.proxy.compiling) this.releaseCellProxy(cell.proxy);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
export function cellLoaded(cell: IResidentCell): boolean {
|
|
1127
|
+
return cell.chunksLoaded && cell.completedRuns.size === cell.cell.runs.length;
|
|
1128
|
+
}
|