@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,484 @@
|
|
|
1
|
+
import { Box3, type Camera, Matrix4, type Object3D, Quaternion, Vector3 } from "three";
|
|
2
|
+
import { Frustum } from "three";
|
|
3
|
+
import type { BundleGroup } from "three/webgpu";
|
|
4
|
+
import type { IRendererLike } from "./renderer.js";
|
|
5
|
+
import type { SharedBatch } from "./world-cell-batches.js";
|
|
6
|
+
import { sameSquares } from "./world-cell-batches.js";
|
|
7
|
+
import { levelTriangles } from "./world-cell-chunks.js";
|
|
8
|
+
import type {
|
|
9
|
+
ICamera,
|
|
10
|
+
IKeyDescriptor,
|
|
11
|
+
IResidentCell,
|
|
12
|
+
IShadowRegion,
|
|
13
|
+
IWorldCellsStats,
|
|
14
|
+
} from "./world-cell-data.js";
|
|
15
|
+
import { clusterOf } from "./world-cell-data.js";
|
|
16
|
+
import { PLACEMENT_RECORD_FLOATS } from "./world-cell-impostors.js";
|
|
17
|
+
import {
|
|
18
|
+
GPU_KEY_SUFFIX,
|
|
19
|
+
MAIN_CULL_MARKER_MS,
|
|
20
|
+
MAIN_CULL_PAD_METRES,
|
|
21
|
+
} from "./world-cell-render-util.js";
|
|
22
|
+
import type { ICellRenderHost } from "./world-cell-render.js";
|
|
23
|
+
import type { WorldGpuScene } from "./world-gpu-scene.js";
|
|
24
|
+
import {
|
|
25
|
+
type IWorldAsset,
|
|
26
|
+
type IWorldCell,
|
|
27
|
+
type IWorldPackage,
|
|
28
|
+
cellPlacements,
|
|
29
|
+
} from "./world-package.js";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The main pass's coarse cull: one volume per resident visibility cell, one frustum a frame, and the
|
|
33
|
+
* per-batch window each key's mesh narrows itself to.
|
|
34
|
+
*
|
|
35
|
+
* This is the part of the renderer that answers "which squares are in this frame" and what it costs.
|
|
36
|
+
* The answer is derived from the camera alone, so it holds nothing of the world but the volumes it
|
|
37
|
+
* has admitted and the counters that report what the frame paid — see `IWorldCellsStats.mainCull`.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
/** What the cull asks the renderer it runs in. */
|
|
41
|
+
export interface ICellMainCullHost {
|
|
42
|
+
render(): ICellRenderHost;
|
|
43
|
+
/** Every shared batch, which is what a frame narrows. */
|
|
44
|
+
shared(): Map<string, SharedBatch>;
|
|
45
|
+
gpuScene(): WorldGpuScene;
|
|
46
|
+
/** Every resident cell's frozen chunk record, and the square whose answer shows it. */
|
|
47
|
+
chunkGroups(): Map<string, { readonly cluster: string; readonly group: BundleGroup }>;
|
|
48
|
+
/** Reset the bundle eligibility counters at the top of the pass. */
|
|
49
|
+
beginBundleFrame(): void;
|
|
50
|
+
recordCpuMain(shared: SharedBatch, outcome: "settled" | "narrowed" | "repacked"): void;
|
|
51
|
+
dressGpu(shared: SharedBatch, key: IKeyDescriptor): void;
|
|
52
|
+
dressShadowKey(shared: SharedBatch, key: string): void;
|
|
53
|
+
applyGpuMain(dressed: readonly { asset: string; shared: SharedBatch }[]): void;
|
|
54
|
+
/** Whether the prewarm has already named every key the scene should draw. */
|
|
55
|
+
gpuSeeded(): boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The grid and the tables a cull derives its volumes from, fixed for the world's life. */
|
|
59
|
+
export interface ICellMainCullSettings {
|
|
60
|
+
readonly cellSize: number;
|
|
61
|
+
readonly minX: number;
|
|
62
|
+
readonly minZ: number;
|
|
63
|
+
readonly manifest: IWorldPackage;
|
|
64
|
+
readonly placements: ArrayBuffer;
|
|
65
|
+
readonly cellsPerCullCell: number;
|
|
66
|
+
readonly mainCullToldAt: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The two scratch objects a cull builds its frustum in. */
|
|
70
|
+
const _cullFrustum = new Frustum();
|
|
71
|
+
const _cullProjScreen = new Matrix4();
|
|
72
|
+
|
|
73
|
+
export class CellMainCull {
|
|
74
|
+
readonly #host: ICellMainCullHost;
|
|
75
|
+
|
|
76
|
+
readonly #cellSize: number;
|
|
77
|
+
|
|
78
|
+
readonly #minX: number;
|
|
79
|
+
|
|
80
|
+
readonly #minZ: number;
|
|
81
|
+
|
|
82
|
+
readonly #manifest: IWorldPackage;
|
|
83
|
+
|
|
84
|
+
readonly #placements: ArrayBuffer;
|
|
85
|
+
|
|
86
|
+
readonly #cellsPerCullCell: number;
|
|
87
|
+
|
|
88
|
+
constructor(settings: ICellMainCullSettings, host: ICellMainCullHost) {
|
|
89
|
+
this.#host = host;
|
|
90
|
+
this.#cellSize = settings.cellSize;
|
|
91
|
+
this.#minX = settings.minX;
|
|
92
|
+
this.#minZ = settings.minZ;
|
|
93
|
+
this.#manifest = settings.manifest;
|
|
94
|
+
this.#placements = settings.placements;
|
|
95
|
+
this.#cellsPerCullCell = settings.cellsPerCullCell;
|
|
96
|
+
this.#mainCullToldAt = settings.mainCullToldAt;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** When `TN_WORLD_MAIN_CULL` was last printed; see {@link #reportMainCull}. */
|
|
100
|
+
#mainCullToldAt = 0;
|
|
101
|
+
|
|
102
|
+
/** Window repacks since the last marker; see {@link #reportMainCull}. */
|
|
103
|
+
#mainCullRepacks = 0;
|
|
104
|
+
|
|
105
|
+
/** Frusta the main cull has built, one a frame; see `IWorldCellsStats.mainCull`. */
|
|
106
|
+
#mainCullFrustums = 0;
|
|
107
|
+
|
|
108
|
+
/** Windows main batches have had to re-derive, cumulative; see `IWorldCellsStats.mainCull`. */
|
|
109
|
+
#mainCullWindows = 0;
|
|
110
|
+
|
|
111
|
+
/** Window repacks, cumulative; see `IWorldCellsStats.mainCull`. */
|
|
112
|
+
#mainCullPacks = 0;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* One AABB per resident visibility cell, and how many resident world cells hold it. Shared by
|
|
116
|
+
* every key, so the main cull is O(visibility cells) a frame rather than O(keys × cells); see
|
|
117
|
+
* {@link #cullMainPass}.
|
|
118
|
+
*/
|
|
119
|
+
readonly #cullCells = new Map<string, { cells: number; box: Box3 }>();
|
|
120
|
+
|
|
121
|
+
/** The visibility cells the last cull kept and the frame's set, trading places; see `#cullMainPass`. */
|
|
122
|
+
#visibleSquares: Set<string> = new Set();
|
|
123
|
+
|
|
124
|
+
#cullScratch: Set<string> = new Set();
|
|
125
|
+
|
|
126
|
+
/** Bumped only when `#visibleSquares` changed, so a settled camera repacks no batch at all. */
|
|
127
|
+
#visibleEpoch = 0;
|
|
128
|
+
|
|
129
|
+
readonly #position = new Vector3();
|
|
130
|
+
|
|
131
|
+
readonly #rotation = new Quaternion();
|
|
132
|
+
|
|
133
|
+
readonly #scale = new Vector3();
|
|
134
|
+
|
|
135
|
+
readonly #matrix = new Matrix4();
|
|
136
|
+
|
|
137
|
+
/** Reused transformed authored bounds for admission and conservative visibility volumes. */
|
|
138
|
+
readonly #recordBounds = new Box3();
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Narrow every main batch's draw window to the world-grid squares `camera`'s frustum covers, once
|
|
142
|
+
* per frame, for the whole set at once.
|
|
143
|
+
*
|
|
144
|
+
* This used to be each main mesh's own `onBeforeRender`, which three never calls for an invisible
|
|
145
|
+
* mesh — so the moment a batch that draws nothing was hidden, the one thing that could show it
|
|
146
|
+
* again when the camera turned back was gone with it, and the world lost its trees for good. The
|
|
147
|
+
* decision therefore moved to the frame's own driver: the render-cadence compute dispatch, which
|
|
148
|
+
* the engine already runs once per rendered frame — on the same readiness gate residency itself
|
|
149
|
+
* sits behind, so a frame that skips this skipped the world's records too — and which now hands the
|
|
150
|
+
* render camera to the world, so it reaches every main mesh whether it is drawn or not.
|
|
151
|
+
*
|
|
152
|
+
* The camera rules are the ones the hook had, and they are the whole of what decides a window:
|
|
153
|
+
*
|
|
154
|
+
* - **An orthographic camera never repacks.** A virtual-shadow level's camera is a
|
|
155
|
+
* `DirectionalLightShadow`'s own, which three makes orthographic, so a level's window — one shadow
|
|
156
|
+
* slice of the ring, deliberately not the main view's — can never overwrite the main pass's. A
|
|
157
|
+
* game whose own camera is orthographic keeps the old behaviour: everything resident is drawn, and
|
|
158
|
+
* nothing pops. That is a lost saving, never a wrong picture.
|
|
159
|
+
* - **The render camera is the one that decides.** The engine dispatches this with the camera it
|
|
160
|
+
* is about to draw the main pass with, so the second-perspective-camera race the hook had to
|
|
161
|
+
* arbitrate per mesh cannot arise: a mirror or a minimap is drawn with its own camera and is
|
|
162
|
+
* served the main view's window, which is the honest trade for a world with one main view.
|
|
163
|
+
* - **A set that did not change touches nothing.** The frustum, the visible cells and the epoch
|
|
164
|
+
* are the frame's, derived once for the whole pass; a batch whose records have not moved since it
|
|
165
|
+
* packed at that epoch compares two integers and returns. A camera at rest costs one frustum and
|
|
166
|
+
* one test per resident visibility cell a frame, and nothing at all per mesh.
|
|
167
|
+
*/
|
|
168
|
+
cullMainPass(camera: Camera | undefined): void {
|
|
169
|
+
if (camera === undefined || (camera as ICamera).isOrthographicCamera === true) return;
|
|
170
|
+
this.#host.beginBundleFrame();
|
|
171
|
+
this.#mainCullFrustums += 1;
|
|
172
|
+
const frustum = _cullFrustum.setFromProjectionMatrix(
|
|
173
|
+
_cullProjScreen.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse),
|
|
174
|
+
);
|
|
175
|
+
// The visible visibility cells, for the whole pass, into a set that is reused and compared
|
|
176
|
+
// rather than allocated: two sets trading places, so a settled camera allocates nothing at all.
|
|
177
|
+
const next = this.#cullScratch;
|
|
178
|
+
next.clear();
|
|
179
|
+
for (const [cell, entry] of this.#cullCells) {
|
|
180
|
+
// An occupied cell whose placements have not been decoded yet holds nothing to draw, and its
|
|
181
|
+
// Y range is still empty — a volume that cannot be tested against ±Infinity is not one this
|
|
182
|
+
// frame's answer may rest on.
|
|
183
|
+
if (entry.box.isEmpty() === false && frustum.intersectsBox(entry.box)) next.add(cell);
|
|
184
|
+
}
|
|
185
|
+
if (!sameSquares(next, this.#visibleSquares)) {
|
|
186
|
+
this.#visibleEpoch += 1;
|
|
187
|
+
const was = this.#visibleSquares;
|
|
188
|
+
this.#visibleSquares = next;
|
|
189
|
+
this.#cullScratch = was;
|
|
190
|
+
this.#cullScratch.clear();
|
|
191
|
+
}
|
|
192
|
+
// A chunk's record is frozen, so its cell's answer is the only thing that can take it out of the
|
|
193
|
+
// frame without re-recording it: a write per resident cell that has chunks, at the same
|
|
194
|
+
// granularity every other coarse gate in this pass uses. See the `bundles` option.
|
|
195
|
+
for (const entry of this.#host.chunkGroups().values()) {
|
|
196
|
+
const visible = this.#visibleSquares.has(entry.cluster);
|
|
197
|
+
if (entry.group.visible !== visible) entry.group.visible = visible;
|
|
198
|
+
}
|
|
199
|
+
const gpu = this.#host.gpuScene();
|
|
200
|
+
const dressed: Array<{ asset: string; shared: SharedBatch }> = [];
|
|
201
|
+
const version = gpu.version;
|
|
202
|
+
for (const shared of [...this.#host.shared().values()]) {
|
|
203
|
+
if (shared.role !== "main") continue;
|
|
204
|
+
// A dressed batch draws from the GPU scene's own buffers, so the frame's per-instance answer is
|
|
205
|
+
// the dispatch's and this mesh's own window would be a second, stale copy of it.
|
|
206
|
+
if (gpu.on && shared.gpu !== undefined) {
|
|
207
|
+
this.#host.dressGpu(shared, shared.gpu);
|
|
208
|
+
// A bundled mesh is in a render list three fixed when it recorded the bundle, and the
|
|
209
|
+
// dispatch has already culled per instance: there is nothing coarse left to hide, and a
|
|
210
|
+
// `visible` written here is one the replay never reads. See `#bundleIn`.
|
|
211
|
+
if (shared.bundled === false) dressed.push({ asset: shared.gpu.asset, shared });
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
// A batch the GPU scene does not own, and every one of a world without it: its own window is
|
|
215
|
+
// the CPU path's answer, and it is a per-frame answer a frozen render list never re-reads. So
|
|
216
|
+
// the record answers the gate instead of the gate writing `visible` into it — a batch the gate
|
|
217
|
+
// shows joins, one it hides leaves, and a window that moved is one more record, because the
|
|
218
|
+
// record holds the layout the move made. The same trade a cell's chunk record makes, at the
|
|
219
|
+
// granularity this gate already uses. See `#bundleIn`.
|
|
220
|
+
const outcome = shared.cullFrom(this.#visibleSquares, this.#visibleEpoch);
|
|
221
|
+
this.#host.recordCpuMain(shared, outcome);
|
|
222
|
+
if (outcome === "settled") continue;
|
|
223
|
+
this.#mainCullWindows += 1;
|
|
224
|
+
if (outcome === "repacked") {
|
|
225
|
+
this.#mainCullRepacks += 1;
|
|
226
|
+
this.#mainCullPacks += 1;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
// A regrow late in the loop replaces the scene's `drawn` — and, a non-grouped key, its `args` —
|
|
230
|
+
// after an earlier mesh in the same loop was dressed against the generation before it, so that
|
|
231
|
+
// mesh would read a buffer the dispatch no longer writes for one frame. One more pass over the
|
|
232
|
+
// same settled set, only when the scene changed under the first, rebinds every mesh to the final
|
|
233
|
+
// buffers. Registration is idempotent and every key is already minted and sized, so this pass
|
|
234
|
+
// mints nothing and cannot grow the scene again.
|
|
235
|
+
if (gpu.on && gpu.version !== version) {
|
|
236
|
+
for (const shared of [...this.#host.shared().values()])
|
|
237
|
+
if (shared.role === "main" && shared.gpu !== undefined)
|
|
238
|
+
this.#host.dressGpu(shared, shared.gpu);
|
|
239
|
+
// And every shadow key, for the same reason: minting one grew the twin buffers every other
|
|
240
|
+
// key was dressed against, so a sibling dressed earlier in the loop above is holding the
|
|
241
|
+
// generation the dispatch replaced. The main key's own dress brings its twin with it; this is
|
|
242
|
+
// what closes the seam for the keys whose main key was dressed before the grow.
|
|
243
|
+
for (const shared of [...this.#host.shared().values()])
|
|
244
|
+
if (shared.role === "key")
|
|
245
|
+
this.#host.dressShadowKey(shared, shared.mesh.name.slice(0, -GPU_KEY_SUFFIX.length));
|
|
246
|
+
}
|
|
247
|
+
// After every key has been dressed, so the asset's answer is the union across its levels and
|
|
248
|
+
// parts.
|
|
249
|
+
this.#host.applyGpuMain(dressed);
|
|
250
|
+
// After the coarse gate, and only for the main camera: an orthographic one — every shadow
|
|
251
|
+
// level's own — returned at the top, so a level's render never dispatches over the main pass's
|
|
252
|
+
// compaction.
|
|
253
|
+
if (gpu.on) gpu.dispatch(this.#host.render().renderer() as IRendererLike, camera);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Borrowed scratch box: consume its transformed volume before the next bounds query. */
|
|
257
|
+
#boundsAt(bounds: IWorldAsset["bounds"], matrix: Matrix4): Box3 {
|
|
258
|
+
this.#recordBounds.min.fromArray(bounds.min);
|
|
259
|
+
this.#recordBounds.max.fromArray(bounds.max);
|
|
260
|
+
return this.#recordBounds.applyMatrix4(matrix);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The one volume per resident visibility cell, shared by every key: its own XZ footprint, and in Y
|
|
265
|
+
* the range its resident placements actually reach once their assets' own bounds are added to them,
|
|
266
|
+
* padded by `MAIN_CULL_PAD_METRES`.
|
|
267
|
+
*
|
|
268
|
+
* It is derived here, on admission, and never a frame — a volume walked every frame to answer a
|
|
269
|
+
* question about placements that never move is a frame's work for an admission's answer.
|
|
270
|
+
*
|
|
271
|
+
* It is deliberately not per key. Every key drew the same cell set, so deriving it per key meant
|
|
272
|
+
* ~230 frusta and ~230 sets a frame to learn the same answer; one volume per cell is what makes the
|
|
273
|
+
* pass O(cells) instead of O(keys × cells).
|
|
274
|
+
*
|
|
275
|
+
* The Y is what makes it a volume rather than a slab. It used to be the whole terrain height range
|
|
276
|
+
* widened by the tallest asset bound in the world — ~100 m of Y on a map with a 10 m relief, which
|
|
277
|
+
* made every cell's bounding sphere a ~90 m radius that a view beside the world still cleared, and
|
|
278
|
+
* handed the vertex stage the whole ring. This is per placement instead: a sub-square of flat
|
|
279
|
+
* ground with ferns on it is the height of its ferns.
|
|
280
|
+
*/
|
|
281
|
+
|
|
282
|
+
cullResident(cell: IWorldCell): void {
|
|
283
|
+
const per = this.#cellsPerCullCell;
|
|
284
|
+
const side = per * this.#cellSize;
|
|
285
|
+
const first = Math.floor(cell.x / per);
|
|
286
|
+
const firstZ = Math.floor(cell.z / per);
|
|
287
|
+
const name = clusterOf(cell.x, cell.z, per);
|
|
288
|
+
let entry = this.#cullCells.get(name);
|
|
289
|
+
if (entry === undefined) {
|
|
290
|
+
// XZ from the grid, padded, and an empty Y: this cell's own placements are unioned in below,
|
|
291
|
+
// and a cell holding none of them yet is never visible rather than visible by its footprint.
|
|
292
|
+
const box = new Box3(
|
|
293
|
+
new Vector3(
|
|
294
|
+
this.#minX + first * side - MAIN_CULL_PAD_METRES,
|
|
295
|
+
Number.POSITIVE_INFINITY,
|
|
296
|
+
this.#minZ + firstZ * side - MAIN_CULL_PAD_METRES,
|
|
297
|
+
),
|
|
298
|
+
new Vector3(
|
|
299
|
+
this.#minX + (first + 1) * side + MAIN_CULL_PAD_METRES,
|
|
300
|
+
Number.NEGATIVE_INFINITY,
|
|
301
|
+
this.#minZ + (firstZ + 1) * side + MAIN_CULL_PAD_METRES,
|
|
302
|
+
),
|
|
303
|
+
);
|
|
304
|
+
entry = { box, cells: 0 };
|
|
305
|
+
this.#cullCells.set(name, entry);
|
|
306
|
+
}
|
|
307
|
+
entry.cells += 1;
|
|
308
|
+
const box = entry.box;
|
|
309
|
+
for (const run of cell.runs) {
|
|
310
|
+
const bounds = this.#manifest.assets[this.#host.render().canonical(run.asset)]?.bounds;
|
|
311
|
+
if (bounds === undefined || run.count === 0) continue;
|
|
312
|
+
const records = cellPlacements(this.#placements, run);
|
|
313
|
+
for (let index = 0; index < run.count; index += 1) {
|
|
314
|
+
const at = index * PLACEMENT_RECORD_FLOATS;
|
|
315
|
+
// The placement, widened by its own asset's bounds at the scale it is drawn at, and by the
|
|
316
|
+
// pad — a union, so the pad is counted once however many placements there are.
|
|
317
|
+
const scale = records[at + 7] as number;
|
|
318
|
+
this.#position.fromArray(records, at);
|
|
319
|
+
this.#rotation.fromArray(records, at + 3);
|
|
320
|
+
this.#scale.setScalar(scale);
|
|
321
|
+
this.#matrix.compose(this.#position, this.#rotation, this.#scale);
|
|
322
|
+
box.union(
|
|
323
|
+
this.#boundsAt(bounds, this.#matrix).expandByScalar(
|
|
324
|
+
MAIN_CULL_PAD_METRES * Math.max(1, Math.abs(scale)),
|
|
325
|
+
),
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Drop the visibility cell an evicted cell sat in once nothing resident is left in it. Records are
|
|
333
|
+
* cleared before this runs, so a batch can never be holding a cell the cull no longer knows: an
|
|
334
|
+
* unknown cell is not in the visible set, and that would be a tree nobody draws.
|
|
335
|
+
*
|
|
336
|
+
* The box goes with it, so a cell that comes back derives its volume again — which is what keeps a
|
|
337
|
+
* released asset's height out of a cell that no longer holds it.
|
|
338
|
+
*/
|
|
339
|
+
cullEvicted(cell: IResidentCell): void {
|
|
340
|
+
const name = clusterOf(cell.x, cell.z, this.#cellsPerCullCell);
|
|
341
|
+
const entry = this.#cullCells.get(name);
|
|
342
|
+
if (entry === undefined) return;
|
|
343
|
+
entry.cells -= 1;
|
|
344
|
+
if (entry.cells <= 0) this.#cullCells.delete(name);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* `TN_WORLD_MAIN_CULL`, every `MAIN_CULL_MARKER_MS`: what the main pass holds, what it is drawing,
|
|
349
|
+
* how many of its meshes are submitted at all, and how many times the window moved to get there.
|
|
350
|
+
*
|
|
351
|
+
* `instances` is every live record the main batches hold — the whole resident ring, which is what
|
|
352
|
+
* three's whole-mesh test used to hand the vertex stage — and `drawn` is what the cull left in the
|
|
353
|
+
* windows. The gap between them is the saving, `repacks` says what it cost to get: a settled camera
|
|
354
|
+
* repacks nothing, so a walk that repacks dozens of times a second is a window that is chasing the
|
|
355
|
+
* camera, not one that is thrashing. `hidden` and `castersHidden` are the batches with nothing to
|
|
356
|
+
* draw that are now not submitted at all: in three r185's WebGPU path a zero-count `InstancedMesh`
|
|
357
|
+
* still runs the whole per-draw JS chain, so an empty batch was CPU a frame for no draw — the number
|
|
358
|
+
* is the part of the census that no longer costs anything.
|
|
359
|
+
*
|
|
360
|
+
* `dressed=N/M` is the GPU scene's own claim, repeated every five seconds: a scene that is on over
|
|
361
|
+
* meshes nothing is dressed into compacts a set the CPU path is still drawing, and `keys` alone
|
|
362
|
+
* cannot say so — a ring built before the scene came up has no keys at all and looks healthy.
|
|
363
|
+
*/
|
|
364
|
+
reportMainCull(): void {
|
|
365
|
+
const now = this.#host.render().now();
|
|
366
|
+
if (now - this.#mainCullToldAt < MAIN_CULL_MARKER_MS) return;
|
|
367
|
+
this.#mainCullToldAt = now;
|
|
368
|
+
const repacks = this.#mainCullRepacks;
|
|
369
|
+
this.#mainCullRepacks = 0;
|
|
370
|
+
let instances = 0;
|
|
371
|
+
let drawn = 0;
|
|
372
|
+
let hidden = 0;
|
|
373
|
+
let castersHidden = 0;
|
|
374
|
+
let clusterWithheld = 0;
|
|
375
|
+
let clusterRecords = 0;
|
|
376
|
+
let clusterMissing = 0;
|
|
377
|
+
let wideWithheld = 0;
|
|
378
|
+
let wideRecords = 0;
|
|
379
|
+
let wideMissing = 0;
|
|
380
|
+
// What the two caster layers hold between them, per layer: the resident triangle bill a shadow
|
|
381
|
+
// level that picked that layer submits. Two numbers that must not be equal is the whole claim —
|
|
382
|
+
// the wide half draws the asset's coarsest level, so it is the one that falls.
|
|
383
|
+
let clusterTriangles = 0;
|
|
384
|
+
let wideTriangles = 0;
|
|
385
|
+
let dressed = 0;
|
|
386
|
+
let mainMeshes = 0;
|
|
387
|
+
for (const shared of this.#host.shared().values()) {
|
|
388
|
+
if (shared.role !== "main") {
|
|
389
|
+
if (shared.mesh.visible === false) castersHidden += 1;
|
|
390
|
+
if (shared.mesh.mainAdmitted === false && shared.live > 0) {
|
|
391
|
+
if (shared.role === "cluster") {
|
|
392
|
+
clusterWithheld += 1;
|
|
393
|
+
clusterRecords += shared.live;
|
|
394
|
+
clusterMissing += Math.max(0, shared.live - shared.gpuPlaced);
|
|
395
|
+
} else if (shared.role === "wide") {
|
|
396
|
+
wideWithheld += 1;
|
|
397
|
+
wideRecords += shared.live;
|
|
398
|
+
wideMissing += Math.max(0, shared.live - shared.gpuPlaced);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
// Zero records add zero, so an empty batch needs no test of its own here.
|
|
402
|
+
const triangles = levelTriangles(shared.mesh.geometry) * shared.live;
|
|
403
|
+
if (shared.role === "cluster") clusterTriangles += triangles;
|
|
404
|
+
else wideTriangles += triangles;
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
mainMeshes += 1;
|
|
408
|
+
if (shared.gpu !== undefined) dressed += 1;
|
|
409
|
+
instances += shared.live;
|
|
410
|
+
drawn += shared.drawn;
|
|
411
|
+
if (shared.mesh.visible === false) hidden += 1;
|
|
412
|
+
}
|
|
413
|
+
const stats = this.#host.render().stats();
|
|
414
|
+
console.info(
|
|
415
|
+
`TN_WORLD_MAIN_CULL instances=${String(instances)} drawn=${String(drawn)} ` +
|
|
416
|
+
`hidden=${String(hidden)} castersHidden=${String(castersHidden)} repacks=${String(repacks)} ` +
|
|
417
|
+
`refilters=${String(stats.refilters)} levelRefilters=${String(stats.levelRefilters)} ` +
|
|
418
|
+
`lodFrom=${stats.lodFrom} ` +
|
|
419
|
+
`clusterTris=${String(clusterTriangles)} wideTris=${String(wideTriangles)} ` +
|
|
420
|
+
`dressed=${String(dressed)}/${String(mainMeshes)}`,
|
|
421
|
+
);
|
|
422
|
+
console.info(
|
|
423
|
+
`TN_WORLD_SHADOW_ADMISSION clusterWithheld=${String(clusterWithheld)} ` +
|
|
424
|
+
`clusterRecords=${String(clusterRecords)} clusterMissing=${String(clusterMissing)} ` +
|
|
425
|
+
`wideWithheld=${String(wideWithheld)} wideRecords=${String(wideRecords)} ` +
|
|
426
|
+
`wideMissing=${String(wideMissing)} seeded=${String(this.#host.gpuSeeded())} ` +
|
|
427
|
+
`queued=${String(this.#host.render().queuedBuilds())}`,
|
|
428
|
+
);
|
|
429
|
+
const hlod = stats.hlod;
|
|
430
|
+
let proxyDraws = 0;
|
|
431
|
+
let replacedSources = 0;
|
|
432
|
+
for (const cell of this.#host.render().residentCells()) {
|
|
433
|
+
const state = cell.proxy;
|
|
434
|
+
if (state === undefined || !state.active) continue;
|
|
435
|
+
proxyDraws += state.prepared?.meshes.length ?? 0;
|
|
436
|
+
replacedSources += state.hidden.size + state.mainMasks.size;
|
|
437
|
+
}
|
|
438
|
+
const why = hlod.reason === "" ? "" : ` reason=${JSON.stringify(hlod.reason)}`;
|
|
439
|
+
console.info(
|
|
440
|
+
`TN_WORLD_HLOD on=${String(hlod.on)} loaded=${String(hlod.loaded)} ` +
|
|
441
|
+
`compiled=${String(hlod.compiled)} observed=${String(hlod.observed)} ` +
|
|
442
|
+
`active=${String(hlod.active)} pending=${String(hlod.pending)} ` +
|
|
443
|
+
`draws=${String(proxyDraws)} replaced=${String(replacedSources)} ` +
|
|
444
|
+
`bytes=${String(hlod.bytes)} refused=${String(hlod.refused)} failures=${String(hlod.failures)}${why}`,
|
|
445
|
+
);
|
|
446
|
+
// The bundles on their own line, because the pair reads against a different question: how many
|
|
447
|
+
// objects are recorded, and how many times the recording was thrown away and redone. A walk
|
|
448
|
+
// streaming and culling holds `records` at the keys and chunks that came and went; a walk that
|
|
449
|
+
// repacks it every frame is drawing a moving set, which is the bug a bundle cannot have. `reason`
|
|
450
|
+
// is on it because the default is now on: a reader has to be able to tell a run that turned it
|
|
451
|
+
// off from a run where recording never happened.
|
|
452
|
+
const bundles = stats.bundle;
|
|
453
|
+
console.info(
|
|
454
|
+
`TN_WORLD_BUNDLE ${bundles.on ? "on" : "off"} reason=${bundles.reason} ` +
|
|
455
|
+
`children=${String(bundles.children)} cpuMeshes=${String(bundles.cpuMeshes)} gpuMeshes=${String(bundles.gpuMeshes)} records=${String(bundles.records)}`,
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
mainCullStats(): IWorldCellsStats["mainCull"] {
|
|
460
|
+
return {
|
|
461
|
+
frustums: this.#mainCullFrustums,
|
|
462
|
+
repacks: this.#mainCullPacks,
|
|
463
|
+
visibleEpoch: this.#visibleEpoch,
|
|
464
|
+
windows: this.#mainCullWindows,
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
/**
|
|
468
|
+
* Whether the frame's own frustum holds this object. It is the frustum the last cull built, which
|
|
469
|
+
* is why it lives here: a caller that answered from a copy no frame wrote would hold everything.
|
|
470
|
+
*/
|
|
471
|
+
intersectsObject(mesh: Object3D): boolean {
|
|
472
|
+
return _cullFrustum.intersectsObject(mesh);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/** The volume each resident visibility cell covers, which the chunk bounds widen on admission. */
|
|
476
|
+
cells(): Map<string, { cells: number; box: Box3 }> {
|
|
477
|
+
return this.#cullCells;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/** The visibility cells the last cull kept, which the GPU dress's coarse gate reads. */
|
|
481
|
+
get visible(): Set<string> {
|
|
482
|
+
return this.#visibleSquares;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import type { SharedBatch } from "./world-cell-batches.js";
|
|
2
|
+
import type { IAssetPart, ICellBatch, IResidentCell } from "./world-cell-data.js";
|
|
3
|
+
import type { ICellRenderHost } from "./world-cell-render.js";
|
|
4
|
+
import type { WorldGpuScene } from "./world-gpu-scene.js";
|
|
5
|
+
import type { IWorldPackage } from "./world-package.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Who claims a shadow: whether a level casts at all, which shape it casts with, and the block one
|
|
9
|
+
* cell takes in its caster cluster or in the key's wide mesh.
|
|
10
|
+
*
|
|
11
|
+
* These are the decisions the caster halves are made of, and the main cull, the swap and the LOD
|
|
12
|
+
* path read them as well as this module's own claims. They keep no state: a claim is a segment in a
|
|
13
|
+
* `SharedBatch`, and every batch is the world's.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** What a claim needs from the renderer it is made for. */
|
|
17
|
+
export interface IShadowClaimHost {
|
|
18
|
+
render(): ICellRenderHost;
|
|
19
|
+
manifest(): IWorldPackage;
|
|
20
|
+
smallCasterMetres(): number;
|
|
21
|
+
castShadowLevels(): number;
|
|
22
|
+
shadowMinLevel(): number;
|
|
23
|
+
/** Authored parts are kept as their own casters, so a generated impostor does not replace them. */
|
|
24
|
+
preserveAuthoredParts(): boolean;
|
|
25
|
+
gpuScene(): WorldGpuScene;
|
|
26
|
+
/** Resident placements per canonical asset, which a released key's block goes back from. */
|
|
27
|
+
resident(): Map<string, number>;
|
|
28
|
+
/** The caster cluster one cell's records go to, minted on first use. */
|
|
29
|
+
casterFor(
|
|
30
|
+
assetId: string,
|
|
31
|
+
entry: ICellBatch,
|
|
32
|
+
cell: IResidentCell,
|
|
33
|
+
level?: number,
|
|
34
|
+
): SharedBatch | undefined;
|
|
35
|
+
/** The key's whole-asset wide caster, minted on first use. */
|
|
36
|
+
wideFor(assetId: string, entry: ICellBatch, level?: number): SharedBatch | undefined;
|
|
37
|
+
/** A block in `shared` for one cell's records, grown when the batch is full. */
|
|
38
|
+
segmentIn(shared: SharedBatch, count: number, cluster?: string): number | undefined;
|
|
39
|
+
/** Fresh meshes already charged against this update's allowance. */
|
|
40
|
+
freshCount(): number;
|
|
41
|
+
/** Record that a key's placed count changed, which the caster's own gate reads. */
|
|
42
|
+
countPlaced(entry: ICellBatch, placed: number): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether this level's own batches cast. Only the caster clusters ask; a main mesh never casts, so
|
|
47
|
+
* the shadow map holds exactly the cluster meshes a level's window covers.
|
|
48
|
+
*/
|
|
49
|
+
export function casts(host: IShadowClaimHost, level: number): boolean {
|
|
50
|
+
return host.castShadowLevels() > level;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* An asset too small to resolve in any window but the finest one's, so its wide caster goes on
|
|
55
|
+
* that level's layer alone. Measured on the authored bounds the package carries, which is the one
|
|
56
|
+
* number a world has for every asset it never loaded a tree for.
|
|
57
|
+
*/
|
|
58
|
+
export function smallCaster(host: IShadowClaimHost, assetId: string): boolean {
|
|
59
|
+
const bounds = host.manifest().assets[assetId]?.bounds;
|
|
60
|
+
if (bounds === undefined) return false;
|
|
61
|
+
return bounds.max[1] - bounds.min[1] < host.smallCasterMetres();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Whether this entry owes a wide caster. Generated whole-asset impostors use only part 0; preserved
|
|
66
|
+
* authored parts retain each part's own caster and transform.
|
|
67
|
+
*/
|
|
68
|
+
export function wideOwed(host: IShadowClaimHost, assetId: string, part: number): boolean {
|
|
69
|
+
if (part === 0) return true;
|
|
70
|
+
return host.preserveAuthoredParts() || host.render().asset(assetId)?.impostor === undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The level a cell batch casts with: its own when that level casts, otherwise the coarsest
|
|
75
|
+
* casting level, when that level holds the same parts — a chain's derived levels do, authored
|
|
76
|
+
* `lods` need not — so a tree past `castLevels` keeps its shadow in the last shape that has a
|
|
77
|
+
* caster rather than leaving a hole in the map (PRD-541). `undefined`: it casts nothing.
|
|
78
|
+
*/
|
|
79
|
+
export function castLevelOf(host: IShadowClaimHost, entry: ICellBatch): number | undefined {
|
|
80
|
+
const floor = shadowFloorOf(host, entry.asset);
|
|
81
|
+
if (entry.level < floor) return floor;
|
|
82
|
+
if (casts(host, entry.level)) return entry.level;
|
|
83
|
+
if (host.castShadowLevels() === 0) return undefined;
|
|
84
|
+
const asset = host.render().asset(entry.asset);
|
|
85
|
+
if (asset === undefined || asset.definition.lods !== undefined) return undefined;
|
|
86
|
+
const level = Math.min(entry.level, host.castShadowLevels()) - 1;
|
|
87
|
+
return asset.levels[level]?.[entry.part] === undefined ? undefined : level;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The level this asset's finer placements cast in (`shadows.minLevel`), clamped to a casting
|
|
92
|
+
* level. 0 when a finer level's parts do not line up with it, since a caster draws its own entry's
|
|
93
|
+
* part index and matrices in that level's shape, and when the shadow levels draw the GPU keys.
|
|
94
|
+
*/
|
|
95
|
+
export function shadowFloorOf(host: IShadowClaimHost, assetId: string): number {
|
|
96
|
+
const asset = host.render().asset(assetId);
|
|
97
|
+
if (asset === undefined || keysForShadow(host)) return 0;
|
|
98
|
+
const floor = Math.min(
|
|
99
|
+
host.shadowMinLevel(),
|
|
100
|
+
host.castShadowLevels() - 1,
|
|
101
|
+
asset.levels.length - 1,
|
|
102
|
+
);
|
|
103
|
+
const coarse = asset.levels[floor];
|
|
104
|
+
if (floor <= 0 || coarse === undefined) return 0;
|
|
105
|
+
for (let level = 0; level < floor; level++) {
|
|
106
|
+
const parts = asset.levels[level];
|
|
107
|
+
if (parts?.length !== coarse.length) return 0;
|
|
108
|
+
for (const [index, part] of parts.entries())
|
|
109
|
+
if (!part.local.equals((coarse[index] as IAssetPart).local)) return 0;
|
|
110
|
+
}
|
|
111
|
+
return floor;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Whether a shadow level draws this world's keys instead of its two caster halves: the GPU scene
|
|
116
|
+
* is up *and* a provider registered on it, which `?tnShadowGpuKeys=1` is what asks for. Both, or
|
|
117
|
+
* neither — a provider with the scene off is a world whose shadow maps have nothing to draw, so
|
|
118
|
+
* the world keeps the caster path until both are true.
|
|
119
|
+
*/
|
|
120
|
+
export function keysForShadow(host: IShadowClaimHost): boolean {
|
|
121
|
+
return host.gpuScene().on && host.gpuScene().shadowKeys;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Claim this cell's block in its caster cluster, when the level casts and it does not have one.
|
|
126
|
+
* `false` means the frame's fresh allowance is spent and the swap waits, so the main mesh and its
|
|
127
|
+
* cluster are never half attached.
|
|
128
|
+
*/
|
|
129
|
+
export function claimCaster(
|
|
130
|
+
host: IShadowClaimHost,
|
|
131
|
+
assetId: string,
|
|
132
|
+
entry: ICellBatch,
|
|
133
|
+
cell: IResidentCell,
|
|
134
|
+
): boolean {
|
|
135
|
+
const level = castLevelOf(host, entry);
|
|
136
|
+
if (entry.casterSegment >= 0 || level === undefined) return true;
|
|
137
|
+
// Nothing to claim, and nothing to wait for: a world whose levels draw its keys mints no caster
|
|
138
|
+
// half at all (`#casterFor`), so reading that as a spent allowance refused every swap in the
|
|
139
|
+
// world — the main mesh was never attached, no cell published its placements, and the main pass
|
|
140
|
+
// drew nothing while its meshes sat there prewarmed and empty.
|
|
141
|
+
if (keysForShadow(host)) return true;
|
|
142
|
+
const caster = host.casterFor(assetId, entry, cell, level);
|
|
143
|
+
const at = caster === undefined ? undefined : host.segmentIn(caster, entry.batch.count);
|
|
144
|
+
if (caster === undefined || at === undefined) return false;
|
|
145
|
+
entry.caster = caster;
|
|
146
|
+
entry.casterSegment = at;
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Claim this cell's block in its key's wide caster mesh, when the level casts and it does not
|
|
152
|
+
* have one. `false` means the frame's fresh allowance is spent and the swap waits, so the
|
|
153
|
+
* clusters and the wide half are never half attached.
|
|
154
|
+
*/
|
|
155
|
+
export function claimWide(host: IShadowClaimHost, assetId: string, entry: ICellBatch): boolean {
|
|
156
|
+
const level = castLevelOf(host, entry);
|
|
157
|
+
if (entry.wideSegment >= 0 || level === undefined) return true;
|
|
158
|
+
// A part the asset's one whole-asset representation already covers: the entry keeps its near
|
|
159
|
+
// main and cluster, and the far shadow reads part 0's one quad; see `#wideOwed`.
|
|
160
|
+
if (!wideOwed(host, assetId, entry.part)) return true;
|
|
161
|
+
// As `#claimCaster`: the key twin is this level's wide half, and there is no mesh to wait on.
|
|
162
|
+
if (keysForShadow(host)) return true;
|
|
163
|
+
const wide = host.wideFor(assetId, entry, level);
|
|
164
|
+
// The impostor's far half draws the placement root, not `placement * part local`, so its block
|
|
165
|
+
// is sized to the root records the build kept beside the part's own; see `ICellBatch.wideRoot`.
|
|
166
|
+
const count = entry.wideRoot?.count ?? entry.batch.count;
|
|
167
|
+
const at = wide === undefined ? undefined : host.segmentIn(wide, count);
|
|
168
|
+
if (wide === undefined || at === undefined) return false;
|
|
169
|
+
entry.wide = wide;
|
|
170
|
+
entry.wideSegment = at;
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function clearSegment(host: IShadowClaimHost, entry: ICellBatch): void {
|
|
175
|
+
if (entry.shared !== undefined && entry.segment >= 0) entry.shared.clear(entry.segment);
|
|
176
|
+
// The source records go back with the block that held them, on every path a cell's records leave:
|
|
177
|
+
// a refilter's replacement, an eviction, and a queued build dropped with its cell. Releasing one
|
|
178
|
+
// twice is a no-op, which is what lets a level's parts share a single list.
|
|
179
|
+
if (entry.gpu !== undefined) {
|
|
180
|
+
const placed = entry.gpu.length;
|
|
181
|
+
for (const at of entry.gpu) host.gpuScene().release(at);
|
|
182
|
+
const left = (host.resident().get(entry.asset) ?? 0) - placed;
|
|
183
|
+
if (left > 0) host.resident().set(entry.asset, left);
|
|
184
|
+
else host.resident().delete(entry.asset);
|
|
185
|
+
// Before the halves go below, which is what clears the handles this reads.
|
|
186
|
+
host.countPlaced(entry, -placed);
|
|
187
|
+
}
|
|
188
|
+
entry.gpu = undefined;
|
|
189
|
+
if (entry.caster !== undefined && entry.casterSegment >= 0)
|
|
190
|
+
entry.caster.clear(entry.casterSegment);
|
|
191
|
+
if (entry.wide !== undefined && entry.wideSegment >= 0) entry.wide.clear(entry.wideSegment);
|
|
192
|
+
entry.shared = undefined;
|
|
193
|
+
entry.segment = -1;
|
|
194
|
+
entry.caster = undefined;
|
|
195
|
+
entry.casterSegment = -1;
|
|
196
|
+
entry.wide = undefined;
|
|
197
|
+
entry.wideSegment = -1;
|
|
198
|
+
}
|