@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,798 @@
|
|
|
1
|
+
import { Box3, Matrix4, Object3D, Quaternion, Vector3 } from "three";
|
|
2
|
+
import { InstancedBatch } from "./instanced-batch.js";
|
|
3
|
+
import { biasedLodDistance } from "./model-lod.js";
|
|
4
|
+
import {
|
|
5
|
+
type IAssetPart,
|
|
6
|
+
type IBuildJob,
|
|
7
|
+
type IResidentCell,
|
|
8
|
+
type IWorldCellsFollow,
|
|
9
|
+
crossesGate,
|
|
10
|
+
} from "./world-cell-data.js";
|
|
11
|
+
import {
|
|
12
|
+
PLACEMENT_RECORD_BYTES,
|
|
13
|
+
PLACEMENT_RECORD_FLOATS,
|
|
14
|
+
cullDistance,
|
|
15
|
+
} from "./world-cell-impostors.js";
|
|
16
|
+
import type { IAssetState } from "./world-cell-models.js";
|
|
17
|
+
import { LEVEL_REFILTER_STEP_METRES } from "./world-cell-options.js";
|
|
18
|
+
import { placedBox } from "./world-cell-records.js";
|
|
19
|
+
import {
|
|
20
|
+
STALE_ENTRY,
|
|
21
|
+
STALE_HEADER,
|
|
22
|
+
STALE_QUERY,
|
|
23
|
+
patchRows,
|
|
24
|
+
staleRows,
|
|
25
|
+
} from "./world-cell-stale.js";
|
|
26
|
+
import { levelAtGates } from "./world-gpu-scene.js";
|
|
27
|
+
import {
|
|
28
|
+
type IWorldAsset,
|
|
29
|
+
type IWorldCell,
|
|
30
|
+
type IWorldRun,
|
|
31
|
+
cellPlacements,
|
|
32
|
+
} from "./world-package.js";
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* One empty batch per (level, part), so a placement is filtered and drawn together and an emptied
|
|
36
|
+
* level or a part nobody placed costs one missing mesh rather than the whole cell's run.
|
|
37
|
+
*/
|
|
38
|
+
export function newBatches(asset: IAssetState): InstancedBatch[][] {
|
|
39
|
+
const batches: InstancedBatch[][] = [];
|
|
40
|
+
for (const parts of asset.levels) {
|
|
41
|
+
const levelBatches: InstancedBatch[] = [];
|
|
42
|
+
for (const part of parts)
|
|
43
|
+
levelBatches.push(new InstancedBatch({ geometry: part.geometry, material: part.material }));
|
|
44
|
+
batches.push(levelBatches);
|
|
45
|
+
}
|
|
46
|
+
return batches;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** How many meshes a build will publish: one per (level, part). */
|
|
50
|
+
export function publishCount(batches: readonly InstancedBatch[][]): number {
|
|
51
|
+
let total = 0;
|
|
52
|
+
for (const levelBatches of batches) total += levelBatches.length;
|
|
53
|
+
return total;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* One empty bounds box per `(level, part)`, beside the batches `addPlacements` measures. Written as a
|
|
58
|
+
* loop because this file is held to a rule that the array method's name is a material's texture
|
|
59
|
+
* property.
|
|
60
|
+
*/
|
|
61
|
+
export function newBoxes(batches: readonly InstancedBatch[][]): Box3[][] {
|
|
62
|
+
const boxes: Box3[][] = [];
|
|
63
|
+
for (const levelBatches of batches) {
|
|
64
|
+
const level: Box3[] = [];
|
|
65
|
+
for (const _batch of levelBatches) level.push(new Box3());
|
|
66
|
+
boxes.push(level);
|
|
67
|
+
}
|
|
68
|
+
return boxes;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** One more block in a `patchRows` plan, merged into the run before it when the two are contiguous. */
|
|
72
|
+
function extendPlan(plan: number[], source: number, count: number): void {
|
|
73
|
+
const last = plan.length - 2;
|
|
74
|
+
const runSource = plan[last];
|
|
75
|
+
const runCount = plan[last + 1] as number;
|
|
76
|
+
const joins =
|
|
77
|
+
runSource !== undefined &&
|
|
78
|
+
(source === -1 ? runSource === -1 : runSource >= 0 && runSource + runCount === source);
|
|
79
|
+
if (joins) plan[last + 1] = runCount + count;
|
|
80
|
+
else plan.push(source, count);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The owned engine's stale scan (`bindings_scene.cpp`); see `world-cell-stale.ts`. */
|
|
84
|
+
interface IStaleEngine {
|
|
85
|
+
__tnStaleRows?: (blocks: Float64Array, plan: Float64Array) => number;
|
|
86
|
+
__tnStaleEntries?: (gates: Float64Array, query: Float64Array) => ArrayLike<number>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** What the placement build asks its world for. Every read is the world's own state, not a copy. */
|
|
90
|
+
export interface ICellPlacementsHost {
|
|
91
|
+
extent(): { readonly cellSize: number; readonly minX: number; readonly minZ: number };
|
|
92
|
+
/** The point `placementReach` is measured from. */
|
|
93
|
+
follow(): IWorldCellsFollow;
|
|
94
|
+
/** The point levels and `maxDistance` are measured from this update; see `updateEye`. */
|
|
95
|
+
eye(): Vector3;
|
|
96
|
+
/** Bumped by every admission and eviction, so a residency change owes a refilter. */
|
|
97
|
+
residencyEpoch(): number;
|
|
98
|
+
asset(id: string): IAssetState | undefined;
|
|
99
|
+
canonical(id: string): string;
|
|
100
|
+
queueBuild(asset: IAssetState, cell: IResidentCell, run: IWorldRun, replace?: boolean): void;
|
|
101
|
+
/** The renderer's own switch: the dispatch grades levels itself while it is on. */
|
|
102
|
+
gpuOn(): boolean;
|
|
103
|
+
/** Whether this asset can append a whole-asset impostor, so a placement root is wanted. */
|
|
104
|
+
rootsNeeded(asset: IAssetState): boolean;
|
|
105
|
+
/** The resident ring: what the refilter pass scans. */
|
|
106
|
+
resident(): Iterable<IResidentCell>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Where a cell's placement records become drawable batches, and when a walk makes that answer stale.
|
|
111
|
+
*
|
|
112
|
+
* The build is the same build `world-cells.ts` ran: one slice of a run's records at a time, the
|
|
113
|
+
* CPU `maxDistance`/`placementReach` filters at the position the slice started from, the level a
|
|
114
|
+
* shared gate rule picks, the fade a swap stamps, and the per-placement bounds the shadow levels
|
|
115
|
+
* are invalidated by. `refilter` is the pass that queues a rebuild when the follow point has moved
|
|
116
|
+
* a gate, and `stale` says whether it runs at all.
|
|
117
|
+
*
|
|
118
|
+
* The record filters, the published levels, the world's gate list and the refilter points live here;
|
|
119
|
+
* everything else — the renderer's own GPU switch, the resident ring, the build queue — is asked of
|
|
120
|
+
* the host.
|
|
121
|
+
*/
|
|
122
|
+
export class CellPlacements {
|
|
123
|
+
readonly #host: ICellPlacementsHost;
|
|
124
|
+
/** The authored placement records, immutable for the world's life. */
|
|
125
|
+
readonly #placements: ArrayBuffer;
|
|
126
|
+
readonly #placementReach: Float32Array | undefined;
|
|
127
|
+
/** Per cell and asset, the reach range the load measured; empty when no reach was authored. */
|
|
128
|
+
readonly #reachRanges: ReadonlyMap<
|
|
129
|
+
IWorldCell,
|
|
130
|
+
Map<string, { min: number; max: number; minY: number; maxY: number }>
|
|
131
|
+
>;
|
|
132
|
+
readonly #lodHysteresis: number;
|
|
133
|
+
readonly #rebuildsPerUpdate: number;
|
|
134
|
+
/** Each record's last published level, kept only when a walking level hysteresis is on. */
|
|
135
|
+
readonly #placementLevels: Uint32Array | undefined;
|
|
136
|
+
/** Each record's last published levels, `(min + 1) | (max + 1) << 4` (0: none yet). */
|
|
137
|
+
readonly #placementRanges: Uint8Array | undefined;
|
|
138
|
+
/** Every distinct gate distance the world's assets have, ascending; see `noteGate`. */
|
|
139
|
+
readonly #gates: number[] = [];
|
|
140
|
+
/** The reused hit list; see `#collectStale`. */
|
|
141
|
+
readonly #stale: Array<{
|
|
142
|
+
cell: IResidentCell;
|
|
143
|
+
distance: number;
|
|
144
|
+
id: string;
|
|
145
|
+
}> = [];
|
|
146
|
+
/** Per cell, its block of the stale scan's slab, dropped when the cell's batches change. */
|
|
147
|
+
readonly #rows = new WeakMap<IResidentCell, { rows: Float64Array; batches: number }>();
|
|
148
|
+
/** The resident cells' blocks laid end to end, and the cells in block order; see `#collectStale`. */
|
|
149
|
+
#slab = new Float64Array(0);
|
|
150
|
+
#slabCells: IResidentCell[] = [];
|
|
151
|
+
/** Where each cell's block sits in the slab the last pack sent; an unchanged block is not sent again. */
|
|
152
|
+
#slabAt = new Map<IResidentCell, { at: number; rows: Float64Array }>();
|
|
153
|
+
#slabEpoch = -1;
|
|
154
|
+
#slabStale = true;
|
|
155
|
+
/** Asset ids by the index the slab names them with; the index only ever grows. */
|
|
156
|
+
readonly #assetIds: string[] = [];
|
|
157
|
+
readonly #assetIndex = new Map<string, number>();
|
|
158
|
+
/** The owned engine's `__tnStaleEntries` host; on upstream three it has no such method. */
|
|
159
|
+
readonly #engine = new Object3D() as Object3D & IStaleEngine;
|
|
160
|
+
/** Stale cell-assets the passes found, cumulative; see `IWorldCellsStats.refilterEntries`. */
|
|
161
|
+
#staleEntries = 0;
|
|
162
|
+
/** Refilter passes run, whether or not they queued anything; see `IWorldCellsStats.refilters`. */
|
|
163
|
+
#refilters = 0;
|
|
164
|
+
/** Replacement builds that entered `addPlacements`; initial admission is not CPU refilter work. */
|
|
165
|
+
#levelRefilters = 0;
|
|
166
|
+
/** The follow point the last refilter pass ran for; see `stale`. */
|
|
167
|
+
readonly #refilterPoint = new Vector3(Number.NaN, 0, Number.NaN);
|
|
168
|
+
/** The LOD eye the last refilter pass ran for; see `stale`. */
|
|
169
|
+
readonly #refilterEye = new Vector3(Number.NaN, 0, Number.NaN);
|
|
170
|
+
/** The residency epoch the last refilter pass saw; see `stale`. */
|
|
171
|
+
#refilterEpoch = -1;
|
|
172
|
+
/** The last pass found more stale cell-assets than `rebuildsPerUpdate` could queue. */
|
|
173
|
+
#refilterOwed = false;
|
|
174
|
+
#rebuilds = 0;
|
|
175
|
+
readonly #position = new Vector3();
|
|
176
|
+
readonly #rotation = new Quaternion();
|
|
177
|
+
readonly #scale = new Vector3();
|
|
178
|
+
readonly #matrix = new Matrix4();
|
|
179
|
+
readonly #recordBounds = new Box3();
|
|
180
|
+
readonly #instance = new Matrix4();
|
|
181
|
+
/** The authored bounds' centre, placed: the source sphere's centre. See `addPlacements`. */
|
|
182
|
+
readonly #sourceCentre = new Vector3();
|
|
183
|
+
/** The world grid, read once: a package's origin and cell side do not change for a world's life. */
|
|
184
|
+
#grid: { readonly cellSize: number; readonly minX: number; readonly minZ: number } | undefined;
|
|
185
|
+
|
|
186
|
+
constructor(
|
|
187
|
+
options: {
|
|
188
|
+
readonly placements: ArrayBuffer;
|
|
189
|
+
readonly placementReach: Float32Array | undefined;
|
|
190
|
+
readonly reachRanges: ReadonlyMap<
|
|
191
|
+
IWorldCell,
|
|
192
|
+
Map<string, { min: number; max: number; minY: number; maxY: number }>
|
|
193
|
+
>;
|
|
194
|
+
readonly lodHysteresis: number;
|
|
195
|
+
readonly rebuildsPerUpdate: number;
|
|
196
|
+
},
|
|
197
|
+
host: ICellPlacementsHost,
|
|
198
|
+
) {
|
|
199
|
+
this.#host = host;
|
|
200
|
+
this.#placements = options.placements;
|
|
201
|
+
this.#placementReach = options.placementReach;
|
|
202
|
+
this.#reachRanges = options.reachRanges;
|
|
203
|
+
this.#lodHysteresis = options.lodHysteresis;
|
|
204
|
+
this.#rebuildsPerUpdate = options.rebuildsPerUpdate;
|
|
205
|
+
this.#placementLevels =
|
|
206
|
+
options.lodHysteresis > 0
|
|
207
|
+
? new Uint32Array(options.placements.byteLength / PLACEMENT_RECORD_BYTES)
|
|
208
|
+
: undefined;
|
|
209
|
+
this.#placementRanges =
|
|
210
|
+
this.#placementLevels === undefined
|
|
211
|
+
? undefined
|
|
212
|
+
: new Uint8Array(this.#placementLevels.length);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
get placementReach(): Float32Array | undefined {
|
|
216
|
+
return this.#placementReach;
|
|
217
|
+
}
|
|
218
|
+
/** Replacement builds that entered `addPlacements`; see `IWorldCellsStats.levelRefilters`. */
|
|
219
|
+
get levelRefilters(): number {
|
|
220
|
+
return this.#levelRefilters;
|
|
221
|
+
}
|
|
222
|
+
/** Rebuilds a refilter pass queued; see `IWorldCellsStats.rebuilds`. */
|
|
223
|
+
get rebuilds(): number {
|
|
224
|
+
return this.#rebuilds;
|
|
225
|
+
}
|
|
226
|
+
/** Refilter passes run, whether or not they queued anything. */
|
|
227
|
+
get refilters(): number {
|
|
228
|
+
return this.#refilters;
|
|
229
|
+
}
|
|
230
|
+
/** Stale cell-assets the passes found, cumulative. */
|
|
231
|
+
get refilterEntries(): number {
|
|
232
|
+
return this.#staleEntries;
|
|
233
|
+
}
|
|
234
|
+
/** Whether the last pass left work it could not queue. */
|
|
235
|
+
get owed(): boolean {
|
|
236
|
+
return this.#refilterOwed;
|
|
237
|
+
}
|
|
238
|
+
/** The far entry of the asset's finest gate, or none: the eye's blend limit. */
|
|
239
|
+
get firstGate(): number {
|
|
240
|
+
return this.#gates[0] ?? Number.POSITIVE_INFINITY;
|
|
241
|
+
}
|
|
242
|
+
/** The next pass owes a rerun even at a standing follow point. */
|
|
243
|
+
owe(): void {
|
|
244
|
+
this.#refilterOwed = true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** Borrowed scratch box: consume its transformed volume before the next bounds query. */
|
|
248
|
+
#boundsAt(bounds: IWorldAsset["bounds"], matrix: Matrix4): Box3 {
|
|
249
|
+
return placedBox(this.#recordBounds, bounds.min, bounds.max, matrix);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Does this update have to run the `maxDistance`/`lods` refilter pass?
|
|
254
|
+
*
|
|
255
|
+
* Three things, and the pass runs when any of them holds: the follow point has moved
|
|
256
|
+
* `LEVEL_REFILTER_STEP_METRES` since the last one, so a walk re-derives its brackets every two
|
|
257
|
+
* metres instead of every half; residency changed, because a cell that arrived or left is the
|
|
258
|
+
* bracket the pass is about; and the last pass found more stale cell-assets than
|
|
259
|
+
* `rebuildsPerUpdate` could queue, so the ones it could not queue are still owed and a still
|
|
260
|
+
* follow point has to keep coming back for them. Anything else is the same brackets, so the pass
|
|
261
|
+
* is skipped and the world does nothing for that update.
|
|
262
|
+
*/
|
|
263
|
+
followY(): number {
|
|
264
|
+
return this.#placementReach === undefined ? 0 : (this.#host.follow().position.y ?? 0);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
stepMetres(): number {
|
|
268
|
+
return this.#placementReach === undefined ? LEVEL_REFILTER_STEP_METRES : 0.75;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
stale(x: number, z: number): boolean {
|
|
272
|
+
if (this.#refilterOwed) return true;
|
|
273
|
+
if (this.#refilterEpoch !== this.#host.residencyEpoch()) return true;
|
|
274
|
+
return (
|
|
275
|
+
Math.hypot(
|
|
276
|
+
x - this.#refilterPoint.x,
|
|
277
|
+
this.followY() - this.#refilterPoint.y,
|
|
278
|
+
z - this.#refilterPoint.z,
|
|
279
|
+
) >= this.stepMetres() ||
|
|
280
|
+
// The GPU dispatch grades levels from the camera every frame; only the CPU path rebuilds.
|
|
281
|
+
(this.#host.gpuOn() === false &&
|
|
282
|
+
this.#host.eye().distanceTo(this.#refilterEye) >= this.stepMetres())
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Filter one slice of a run's placements into the batches they draw from, applying the CPU
|
|
288
|
+
* maxDistance/reach filters at the build position and switching level where an asset's `lods` say
|
|
289
|
+
* to. GPU admission retains maxDistance candidates for the dispatch; placementReach stays here
|
|
290
|
+
* because it is an explicit per-record source boundary.
|
|
291
|
+
*/
|
|
292
|
+
addPlacements(job: IBuildJob, from: number, to: number): void {
|
|
293
|
+
const { asset, filterX, filterZ, lodX, lodY, lodZ } = job;
|
|
294
|
+
if (job.replacement && job.next === 0) this.#levelRefilters += 1;
|
|
295
|
+
if (job.records === undefined) job.records = cellPlacements(this.#placements, job.run);
|
|
296
|
+
const records = job.records;
|
|
297
|
+
const inner = cullDistance(asset.definition.maxDistance);
|
|
298
|
+
// Half the diagonal of the asset's authored bounds: the sphere the dispatch culls this placement
|
|
299
|
+
// with, and the one number about its extent that comes from the package rather than from here.
|
|
300
|
+
const authored = asset.definition.bounds;
|
|
301
|
+
const boundsRadius =
|
|
302
|
+
0.5 *
|
|
303
|
+
Math.hypot(
|
|
304
|
+
(authored.max[0] as number) - (authored.min[0] as number),
|
|
305
|
+
(authored.max[1] as number) - (authored.min[1] as number),
|
|
306
|
+
(authored.max[2] as number) - (authored.min[2] as number),
|
|
307
|
+
);
|
|
308
|
+
// Decided once per slice, not per placement: the wide half needs the placement root only for an
|
|
309
|
+
// asset that can append a whole-asset impostor. See `ICellBatch.wideRoot`.
|
|
310
|
+
const wantRoots = this.#host.rootsNeeded(asset);
|
|
311
|
+
for (let index = from; index < to; index += 1) {
|
|
312
|
+
const base = index * PLACEMENT_RECORD_FLOATS;
|
|
313
|
+
const x = records[base] as number;
|
|
314
|
+
const y = records[base + 1] as number;
|
|
315
|
+
const z = records[base + 2] as number;
|
|
316
|
+
// Streaming reach from the follow point; level and `maxDistance` from the LOD eye, which is
|
|
317
|
+
// the camera the GPU dispatch measures from too. See `IWorldCellsLoadOptions.lodFrom`.
|
|
318
|
+
const reachDistance =
|
|
319
|
+
this.#placementReach === undefined
|
|
320
|
+
? Math.hypot(x - filterX, z - filterZ)
|
|
321
|
+
: Math.hypot(x - filterX, y - job.filterY, z - filterZ);
|
|
322
|
+
const distance =
|
|
323
|
+
this.#placementReach === undefined
|
|
324
|
+
? Math.hypot(x - lodX, z - lodZ)
|
|
325
|
+
: Math.hypot(x - lodX, y - lodY, z - lodZ);
|
|
326
|
+
if (this.#host.gpuOn() === false && inner !== undefined && distance > inner) continue;
|
|
327
|
+
const reach = this.#placementReach?.[job.run.offset + index];
|
|
328
|
+
if (reach !== undefined && reachDistance > reach) continue;
|
|
329
|
+
this.#position.set(x, y, z);
|
|
330
|
+
this.#rotation.set(
|
|
331
|
+
records[base + 3] as number,
|
|
332
|
+
records[base + 4] as number,
|
|
333
|
+
records[base + 5] as number,
|
|
334
|
+
records[base + 6] as number,
|
|
335
|
+
);
|
|
336
|
+
this.#scale.setScalar(records[base + 7] as number);
|
|
337
|
+
this.#matrix.compose(this.#position, this.#rotation, this.#scale);
|
|
338
|
+
const scale = this.#scale.x;
|
|
339
|
+
// The placement transform, then the part's own offset inside the model: a bark primitive at
|
|
340
|
+
// the trunk and a needles primitive higher up both land in the one instance matrix.
|
|
341
|
+
// The level is picked by the shared gate rule: authored world-metre gates directly, and a
|
|
342
|
+
// whole-asset impostor's terminal gate scaled by this placement's own scale. The bias is the
|
|
343
|
+
// same adaptive multiplier the dispatch carries, so a biased walk crosses a switch here too.
|
|
344
|
+
let level = levelAtGates(
|
|
345
|
+
{
|
|
346
|
+
distances: asset.distances,
|
|
347
|
+
impostor: asset.impostor !== undefined && asset.definition.impostorDistance === undefined,
|
|
348
|
+
},
|
|
349
|
+
biasedLodDistance(distance),
|
|
350
|
+
scale,
|
|
351
|
+
);
|
|
352
|
+
if (this.#placementLevels !== undefined && asset.definition.lods !== undefined) {
|
|
353
|
+
const record = job.run.offset + index;
|
|
354
|
+
let previous = this.#placementLevels[record] as number;
|
|
355
|
+
// Outward gates remain authored; returning inward retains the previous level until the margin.
|
|
356
|
+
if (level > previous) previous = level;
|
|
357
|
+
while (
|
|
358
|
+
previous > level &&
|
|
359
|
+
biasedLodDistance(distance) <
|
|
360
|
+
(asset.distances[previous] as number) * (1 - this.#lodHysteresis)
|
|
361
|
+
)
|
|
362
|
+
previous--;
|
|
363
|
+
level = previous;
|
|
364
|
+
this.#placementLevels[record] = level;
|
|
365
|
+
}
|
|
366
|
+
const selected = [level];
|
|
367
|
+
if (this.#lodHysteresis > 0) {
|
|
368
|
+
const d = biasedLodDistance(distance);
|
|
369
|
+
for (let adjacent = 0; adjacent < asset.levels.length; adjacent++) {
|
|
370
|
+
if (adjacent === level) continue;
|
|
371
|
+
const near = asset.distances[adjacent] ?? 0;
|
|
372
|
+
const far = asset.distances[adjacent + 1] ?? Number.POSITIVE_INFINITY;
|
|
373
|
+
const slack =
|
|
374
|
+
Math.max(near, Number.isFinite(far) ? far : near) * this.#lodHysteresis +
|
|
375
|
+
(asset.distances[1] ?? 6) / 8;
|
|
376
|
+
if (d >= near - slack && d <= far + slack) selected.push(adjacent);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const minPublished = Math.min(...selected);
|
|
380
|
+
const maxPublished = Math.max(...selected);
|
|
381
|
+
// A refilter that lands after the walk entered a dither band would swap a tree's look in one
|
|
382
|
+
// frame. UE 5.8.3 fades every LOD change over `lod.TemporalLag` (0.5 s,
|
|
383
|
+
// Engine/Source/Runtime/Engine/Private/SceneManagement.cpp:28): a record that was one whole
|
|
384
|
+
// level and now publishes two names that level, and the swap stamps when the fade starts.
|
|
385
|
+
let fadeFrom = -1;
|
|
386
|
+
if (this.#placementRanges !== undefined) {
|
|
387
|
+
const record = job.run.offset + index;
|
|
388
|
+
const before = this.#placementRanges[record] as number;
|
|
389
|
+
const beforeMin = (before & 15) - 1;
|
|
390
|
+
const beforeMax = (before >> 4) - 1;
|
|
391
|
+
if (
|
|
392
|
+
before !== 0 &&
|
|
393
|
+
beforeMin === beforeMax &&
|
|
394
|
+
minPublished !== maxPublished &&
|
|
395
|
+
beforeMin >= minPublished &&
|
|
396
|
+
beforeMin <= maxPublished
|
|
397
|
+
)
|
|
398
|
+
fadeFrom = beforeMin;
|
|
399
|
+
this.#placementRanges[record] = (minPublished + 1) | ((maxPublished + 1) << 4);
|
|
400
|
+
}
|
|
401
|
+
// The record's own world bounds: the placement widened by the asset's authored bounds at the
|
|
402
|
+
// scale it is drawn at. Read here because this loop already holds the placement and the scale,
|
|
403
|
+
// and it is the one number the shadow levels' invalidation is tested against — the cell box is
|
|
404
|
+
// 64 m of it, so a level whose window is 48 m wide was redrawing over ground nothing in it.
|
|
405
|
+
const bounds = asset.definition.bounds;
|
|
406
|
+
const placedBox = this.#boundsAt(bounds, this.#matrix);
|
|
407
|
+
job.placed ??= [];
|
|
408
|
+
job.placed.push(
|
|
409
|
+
index,
|
|
410
|
+
placedBox.min.x,
|
|
411
|
+
placedBox.min.y,
|
|
412
|
+
placedBox.min.z,
|
|
413
|
+
placedBox.max.x,
|
|
414
|
+
placedBox.max.y,
|
|
415
|
+
placedBox.max.z,
|
|
416
|
+
);
|
|
417
|
+
// The far shadow of an asset that can bake a whole-asset impostor draws one quad per placement
|
|
418
|
+
// at the placement root: the parts' own offsets are baked into the atlas, so composing a source
|
|
419
|
+
// part's offset again would apply it twice. One root record per placement, beside the per-part
|
|
420
|
+
// batches and read only by the wide half; see `ICellBatch.wideRoot`.
|
|
421
|
+
for (level of selected) {
|
|
422
|
+
if (wantRoots) {
|
|
423
|
+
job.roots ??= [];
|
|
424
|
+
const roots = job.roots;
|
|
425
|
+
let root = roots[level];
|
|
426
|
+
if (root === undefined) {
|
|
427
|
+
const anchor = asset.levels[level]?.[0];
|
|
428
|
+
if (anchor !== undefined) {
|
|
429
|
+
root = new InstancedBatch({ geometry: anchor.geometry, material: anchor.material });
|
|
430
|
+
roots[level] = root;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
root?.add(this.#matrix);
|
|
434
|
+
}
|
|
435
|
+
// The GPU scene's source record for this placement: the placement's own transform, the level it
|
|
436
|
+
// reached, and a sphere the dispatch tests against the camera's planes. The part offset is the
|
|
437
|
+
// key's own, so one record serves every part of the level — see `#placeSources`.
|
|
438
|
+
if (this.#host.gpuOn()) {
|
|
439
|
+
const list = job.sources ?? [];
|
|
440
|
+
job.sources = list;
|
|
441
|
+
// The sphere is the asset's authored bounds *as placed*, centred where the bounds' centre
|
|
442
|
+
// lands under the placement's own transform: a sphere at the placement instead is not a
|
|
443
|
+
// bound of the placement, and every prop whose model does not straddle the origin lost the
|
|
444
|
+
// half of itself that reaches away from it — a post, a stump, a fern, all of them culled at
|
|
445
|
+
// the edge of the view with their bases outside it. The CPU path's own gate is the cell's box
|
|
446
|
+
// over the same widened bounds, and it is conservative in exactly the way this is now. The
|
|
447
|
+
// centre is the matrix applied to the authored centre, so the placement's quaternion rotates
|
|
448
|
+
// it: composing the offsets by hand dropped the rotation and put a turned off-centre prop's
|
|
449
|
+
// sphere where the unturned one would be. The radius is rotation-invariant and takes the
|
|
450
|
+
// scale alone, which is uniform.
|
|
451
|
+
const centre = this.#sourceCentre
|
|
452
|
+
.set(
|
|
453
|
+
((bounds.min[0] as number) + (bounds.max[0] as number)) * 0.5,
|
|
454
|
+
((bounds.min[1] as number) + (bounds.max[1] as number)) * 0.5,
|
|
455
|
+
((bounds.min[2] as number) + (bounds.max[2] as number)) * 0.5,
|
|
456
|
+
)
|
|
457
|
+
.applyMatrix4(this.#matrix);
|
|
458
|
+
list.push({
|
|
459
|
+
level,
|
|
460
|
+
matrix: this.#matrix.clone(),
|
|
461
|
+
// `boundsRadius` is already the asset's authored bounds radius (half the diagonal), so the
|
|
462
|
+
// placement's sphere radius is that times its own uniform scale — halving it again put the
|
|
463
|
+
// sphere inside the prop and culled the half of it that reaches away from the placement.
|
|
464
|
+
radius: boundsRadius * Math.abs(scale),
|
|
465
|
+
// The magnitude, finite-sanitised: the dispatch's impostor gate takes `|scale|` exactly as
|
|
466
|
+
// `levelAtGates` does, so a mirrored placement reaches the atlas the same way.
|
|
467
|
+
scale: Number.isFinite(scale) ? Math.abs(scale) : 1,
|
|
468
|
+
x: centre.x,
|
|
469
|
+
y: centre.y,
|
|
470
|
+
z: centre.z,
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
// `#recordBounds` still holds `placedBox`: the authored bounds (model-part offsets included)
|
|
474
|
+
// under the whole placement transform, so rotated, mirrored and off-centre casters cannot fall
|
|
475
|
+
// outside visibility/shadow regions. Measured once per placement, not once per level.
|
|
476
|
+
const parts = asset.levels[level] as readonly IAssetPart[];
|
|
477
|
+
const levelBatches = job.batches?.[level] as InstancedBatch[];
|
|
478
|
+
const levelBoxes = job.boxes?.[level] as Box3[] | undefined;
|
|
479
|
+
for (const [part, entry] of parts.entries()) {
|
|
480
|
+
this.#instance.multiplyMatrices(this.#matrix, entry.local);
|
|
481
|
+
(levelBatches[part] as InstancedBatch).add(this.#instance);
|
|
482
|
+
const range = job.lodRanges?.[level]?.[part];
|
|
483
|
+
if (range !== undefined) range.push(minPublished, maxPublished, fadeFrom, 0);
|
|
484
|
+
const box = levelBoxes?.[part];
|
|
485
|
+
if (box === undefined) continue;
|
|
486
|
+
box.union(this.#recordBounds);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Build one (level, part) batch into a mesh. One mesh is one unit, because this is where every
|
|
494
|
+
* instance matrix is written and the batch's bounds are first computed — the part of admission the
|
|
495
|
+
* renderer pays for again on the frame it first draws the result. The mesh joins the cell in
|
|
496
|
+
* {@link #swap}, with the rest of the job, so it is never half a replacement.
|
|
497
|
+
*
|
|
498
|
+
* ponytail: a hard switch, no crossfade — a placement crosses a level boundary by being rebuilt
|
|
499
|
+
* into the other level's batch, so the swap pops. A blend needs a per-instance mix the
|
|
500
|
+
* InstancedBatch has no slot for; add one when a game's swap is visible enough to pay for it.
|
|
501
|
+
*/
|
|
502
|
+
/**
|
|
503
|
+
* Fold one batch's own filter point into the cell's bracket, which only ever widens. See
|
|
504
|
+
* `IResidentCell.filterNear` and `#staleIn`.
|
|
505
|
+
*/
|
|
506
|
+
widenFilterRange(cell: IResidentCell, filterX: number, filterZ: number): void {
|
|
507
|
+
const [near, far] = this.#span(cell, filterX, filterZ);
|
|
508
|
+
if (near < cell.filterNear) cell.filterNear = near;
|
|
509
|
+
if (far > cell.filterFar) cell.filterFar = far;
|
|
510
|
+
this.cellBatchesChanged(cell);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/** The cell's batches or bracket changed, so its block of the stale scan is rebuilt. */
|
|
514
|
+
cellBatchesChanged(cell: IResidentCell): void {
|
|
515
|
+
this.#rows.delete(cell);
|
|
516
|
+
this.#slabStale = true;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
buildOne(job: IBuildJob, batches: readonly InstancedBatch[][]): void {
|
|
520
|
+
const { asset } = job;
|
|
521
|
+
const { cell } = job;
|
|
522
|
+
this.widenFilterRange(cell, job.lodX, job.lodZ);
|
|
523
|
+
let part = job.published;
|
|
524
|
+
for (const [level, levelBatches] of batches.entries()) {
|
|
525
|
+
if (part >= levelBatches.length) {
|
|
526
|
+
part -= levelBatches.length;
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
const box = job.boxes?.[level]?.[part];
|
|
530
|
+
job.fresh.push({
|
|
531
|
+
asset: asset.id,
|
|
532
|
+
batch: levelBatches[part] as InstancedBatch,
|
|
533
|
+
caster: undefined,
|
|
534
|
+
casterSegment: -1,
|
|
535
|
+
gpu: undefined,
|
|
536
|
+
wide: undefined,
|
|
537
|
+
wideSegment: -1,
|
|
538
|
+
wideRoot: job.roots?.[level],
|
|
539
|
+
lastFilterY: job.filterY,
|
|
540
|
+
lastFilterX: job.filterX,
|
|
541
|
+
lastFilterZ: job.filterZ,
|
|
542
|
+
lastLodX: job.lodX,
|
|
543
|
+
lastLodY: job.lodY,
|
|
544
|
+
lastLodZ: job.lodZ,
|
|
545
|
+
level,
|
|
546
|
+
lodRanges: job.lodRanges?.[level]?.[part]?.length
|
|
547
|
+
? Float32Array.from(job.lodRanges[level]?.[part] ?? [])
|
|
548
|
+
: undefined,
|
|
549
|
+
part,
|
|
550
|
+
run: job.run,
|
|
551
|
+
segment: -1,
|
|
552
|
+
shared: undefined,
|
|
553
|
+
// Where these records are, for the shadow invalidation this entry's swap hands over.
|
|
554
|
+
shadowBounds: box === undefined || box.isEmpty() ? undefined : box,
|
|
555
|
+
threshold: this.#reachRanges.get(cell.cell)?.has(asset.id)
|
|
556
|
+
? Math.min(asset.threshold ?? Number.POSITIVE_INFINITY, 6)
|
|
557
|
+
: asset.threshold,
|
|
558
|
+
});
|
|
559
|
+
job.published += 1;
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* The `[nearest, farthest]` distance from `(x, z)` to the rectangle a cell's placements lie in.
|
|
566
|
+
*
|
|
567
|
+
* ponytail: the rectangle is the compiler's contract — a run's records belong to the cell they
|
|
568
|
+
* are filed under — so one interval covers every placement in a cell. A hand-edited package that
|
|
569
|
+
* files a placement under a neighbouring cell keeps it at the level its own distance selects; a
|
|
570
|
+
* validator check on the placement buffer is the fix if that ever has to be caught.
|
|
571
|
+
*/
|
|
572
|
+
#span(cell: IResidentCell, x: number, z: number): readonly [number, number] {
|
|
573
|
+
this.#grid ??= this.#host.extent();
|
|
574
|
+
const { cellSize, minX, minZ } = this.#grid;
|
|
575
|
+
const left = minX + cell.x * cellSize;
|
|
576
|
+
const near = minZ + cell.z * cellSize;
|
|
577
|
+
const right = left + cellSize;
|
|
578
|
+
const far = near + cellSize;
|
|
579
|
+
return [
|
|
580
|
+
Math.hypot(Math.max(left - x, x - right, 0), Math.max(near - z, z - far, 0)),
|
|
581
|
+
Math.hypot(
|
|
582
|
+
Math.max(Math.abs(x - left), Math.abs(x - right)),
|
|
583
|
+
Math.max(Math.abs(z - near), Math.abs(z - far)),
|
|
584
|
+
),
|
|
585
|
+
];
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Record that one more gate distance exists, keeping the world's list ascending so
|
|
590
|
+
* {@link crossesGate} can binary search it. A gate is never withdrawn: an asset whose ring left
|
|
591
|
+
* still leaves its distances behind, which costs a cell the gate test does not reject and never a
|
|
592
|
+
* rebuild the pass owes.
|
|
593
|
+
*/
|
|
594
|
+
noteGate(gate: number): void {
|
|
595
|
+
const gates = this.#gates;
|
|
596
|
+
if (gates.includes(gate)) return;
|
|
597
|
+
let at = gates.length;
|
|
598
|
+
while (at > 0 && (gates[at - 1] as number) > gate) at -= 1;
|
|
599
|
+
gates.splice(at, 0, gate);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Queue a refilter for every batch whose follow point has moved far enough to have crossed a CPU
|
|
604
|
+
* source-admission boundary: maxDistance/lod gates on the CPU path, or placementReach in either
|
|
605
|
+
* path. GPU maxDistance/lod gates stay in the dispatch, so they do not enter this replacement
|
|
606
|
+
* queue.
|
|
607
|
+
*
|
|
608
|
+
* Two things keep that pass from costing a frame. A cell's placements all sit inside one
|
|
609
|
+
* rectangle, so the two points that matter — where the batch was built and where the follow point
|
|
610
|
+
* is now — bracket every distance a placement of that cell can have had; with no gate of the
|
|
611
|
+
* asset in that span the level and cull answer is the same and the rebuild could change nothing,
|
|
612
|
+
* and the position the batch was built from is kept so the next move is still measured from it.
|
|
613
|
+
* What is genuinely stale is queued nearest cell first, `rebuildsPerUpdate` of them per call, and
|
|
614
|
+
* the queue spends the frame's admission budget on them like any other build — so a refilter that
|
|
615
|
+
* cannot be afforded this frame leaves the cell drawing the level it has, not a hole.
|
|
616
|
+
*/
|
|
617
|
+
refilter(x: number, z: number): void {
|
|
618
|
+
this.#refilterPoint.set(x, this.followY(), z);
|
|
619
|
+
this.#refilterEye.copy(this.#host.eye());
|
|
620
|
+
this.#refilterEpoch = this.#host.residencyEpoch();
|
|
621
|
+
this.#refilters += 1;
|
|
622
|
+
const stale = this.#stale;
|
|
623
|
+
stale.length = 0;
|
|
624
|
+
this.#collectStale(x, z);
|
|
625
|
+
// Only what was found, and usually a handful of entries.
|
|
626
|
+
if (stale.length > 1) stale.sort((a, b) => a.distance - b.distance || a.id.localeCompare(b.id));
|
|
627
|
+
// What the cap could not queue is still owed, and `stale` reads that: a standing follow
|
|
628
|
+
// point has to keep coming back for it, or a walk that stopped mid-drain never finished.
|
|
629
|
+
this.#refilterOwed = stale.length > this.#rebuildsPerUpdate;
|
|
630
|
+
for (const job of stale.slice(0, this.#rebuildsPerUpdate)) {
|
|
631
|
+
const asset = this.#host.asset(job.id);
|
|
632
|
+
if (asset === undefined || asset.levels.length === 0) continue;
|
|
633
|
+
// Every run of the group, not just the one that happens to carry the canonical's name: a cell
|
|
634
|
+
// holding two names for one model has two stale answers, and rebuilding only the named one
|
|
635
|
+
// left the other drawing the level its own distance has moved out of.
|
|
636
|
+
for (const run of job.cell.cell.runs) {
|
|
637
|
+
if (this.#host.canonical(run.asset) !== job.id) continue;
|
|
638
|
+
this.#host.queueBuild(asset, job.cell, run, true);
|
|
639
|
+
this.#rebuilds += 1;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* Every resident cell's stale assets, into the reused list, nearest placement first.
|
|
646
|
+
*
|
|
647
|
+
* A cell is gated as a whole before one of its batches is looked at: its own bracket is derived
|
|
648
|
+
* once, unioned with every distance any of its batches was filtered from, and one binary search
|
|
649
|
+
* over the world's gates decides it. No gate in that range means no asset of the cell can have
|
|
650
|
+
* changed side of one, so its batches are not walked at all — which is the whole cost of a pass in
|
|
651
|
+
* which the follow point has not yet reached anything that switches.
|
|
652
|
+
*/
|
|
653
|
+
#collectStale(x: number, z: number): void {
|
|
654
|
+
const epoch = this.#host.residencyEpoch();
|
|
655
|
+
if (this.#slabStale || this.#slabEpoch !== epoch) this.#packSlab(epoch);
|
|
656
|
+
const eye = this.#host.eye();
|
|
657
|
+
const query = new Float64Array(STALE_QUERY + this.#gates.length);
|
|
658
|
+
query.set([
|
|
659
|
+
x,
|
|
660
|
+
this.followY(),
|
|
661
|
+
z,
|
|
662
|
+
eye.x,
|
|
663
|
+
eye.y,
|
|
664
|
+
eye.z,
|
|
665
|
+
this.#host.gpuOn() ? 1 : 0,
|
|
666
|
+
this.#gates.length,
|
|
667
|
+
]);
|
|
668
|
+
query.set(this.#gates, STALE_QUERY);
|
|
669
|
+
const gates = this.#gateTable();
|
|
670
|
+
const found =
|
|
671
|
+
typeof this.#engine.__tnStaleEntries === "function"
|
|
672
|
+
? this.#engine.__tnStaleEntries(gates, query)
|
|
673
|
+
: staleRows(this.#slab, gates, query);
|
|
674
|
+
for (let at = 0; at < found.length; at += 3) {
|
|
675
|
+
this.#stale.push({
|
|
676
|
+
cell: this.#slabCells[found[at] as number] as IResidentCell,
|
|
677
|
+
distance: found[at + 2] as number,
|
|
678
|
+
id: this.#assetIds[found[at + 1] as number] as string,
|
|
679
|
+
});
|
|
680
|
+
this.#staleEntries += 1;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
/**
|
|
685
|
+
* The resident cells' blocks end to end; only a changed cell builds its block again, and only the
|
|
686
|
+
* changed blocks are sent: an unchanged one is a `[source, count]` run of the slab already held
|
|
687
|
+
* (see `patchRows`).
|
|
688
|
+
*/
|
|
689
|
+
#packSlab(epoch: number): void {
|
|
690
|
+
const plan: number[] = [];
|
|
691
|
+
const changed: Float64Array[] = [];
|
|
692
|
+
const cells: IResidentCell[] = [];
|
|
693
|
+
const placed = new Map<IResidentCell, { at: number; rows: Float64Array }>();
|
|
694
|
+
let length = 0;
|
|
695
|
+
let sent = 0;
|
|
696
|
+
for (const cell of this.#host.resident()) {
|
|
697
|
+
const rows = this.#rowsOf(cell);
|
|
698
|
+
const held = this.#slabAt.get(cell);
|
|
699
|
+
const source = held !== undefined && held.rows === rows ? held.at : -1;
|
|
700
|
+
extendPlan(plan, source, rows.length);
|
|
701
|
+
if (source === -1) {
|
|
702
|
+
changed.push(rows);
|
|
703
|
+
sent += rows.length;
|
|
704
|
+
}
|
|
705
|
+
placed.set(cell, { at: length, rows });
|
|
706
|
+
cells.push(cell);
|
|
707
|
+
length += rows.length;
|
|
708
|
+
}
|
|
709
|
+
const blocks = new Float64Array(sent);
|
|
710
|
+
let at = 0;
|
|
711
|
+
for (const block of changed) {
|
|
712
|
+
blocks.set(block, at);
|
|
713
|
+
at += block.length;
|
|
714
|
+
}
|
|
715
|
+
if (typeof this.#engine.__tnStaleEntries === "function") {
|
|
716
|
+
// A scan without its rows would read an empty slab and find nothing stale, silently.
|
|
717
|
+
if (typeof this.#engine.__tnStaleRows !== "function")
|
|
718
|
+
throw new Error("TN_WORLD_STALE: the engine has __tnStaleEntries but no __tnStaleRows");
|
|
719
|
+
this.#engine.__tnStaleRows(blocks, Float64Array.from(plan));
|
|
720
|
+
} else this.#slab = patchRows(this.#slab, blocks, plan);
|
|
721
|
+
this.#slabAt = placed;
|
|
722
|
+
this.#slabCells = cells;
|
|
723
|
+
this.#slabEpoch = epoch;
|
|
724
|
+
this.#slabStale = false;
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/** One cell's block: its rectangle and bracket, then each batch entry's filter state. */
|
|
728
|
+
#rowsOf(cell: IResidentCell): Float64Array {
|
|
729
|
+
const cached = this.#rows.get(cell);
|
|
730
|
+
if (cached !== undefined && cached.batches === cell.batches.length) return cached.rows;
|
|
731
|
+
this.#grid ??= this.#host.extent();
|
|
732
|
+
const { cellSize, minX, minZ } = this.#grid;
|
|
733
|
+
const reach = this.#reachRanges.get(cell.cell);
|
|
734
|
+
const rows = new Float64Array(STALE_HEADER + cell.batches.length * STALE_ENTRY);
|
|
735
|
+
rows.set([
|
|
736
|
+
minX + cell.x * cellSize,
|
|
737
|
+
minZ + cell.z * cellSize,
|
|
738
|
+
cellSize,
|
|
739
|
+
cell.filterNear,
|
|
740
|
+
cell.filterFar,
|
|
741
|
+
reach === undefined ? 0 : 1,
|
|
742
|
+
cell.batches.length,
|
|
743
|
+
0,
|
|
744
|
+
]);
|
|
745
|
+
let at = STALE_HEADER;
|
|
746
|
+
for (const entry of cell.batches) {
|
|
747
|
+
const range = reach?.get(entry.asset);
|
|
748
|
+
rows.set(
|
|
749
|
+
[
|
|
750
|
+
this.#assetSlot(entry.asset),
|
|
751
|
+
entry.lastFilterX,
|
|
752
|
+
entry.lastFilterY,
|
|
753
|
+
entry.lastFilterZ,
|
|
754
|
+
entry.lastLodX,
|
|
755
|
+
entry.lastLodY,
|
|
756
|
+
entry.lastLodZ,
|
|
757
|
+
entry.threshold ?? Number.NaN,
|
|
758
|
+
range?.min ?? Number.NaN,
|
|
759
|
+
range?.max ?? Number.NaN,
|
|
760
|
+
range?.minY ?? Number.NaN,
|
|
761
|
+
range?.maxY ?? Number.NaN,
|
|
762
|
+
],
|
|
763
|
+
at,
|
|
764
|
+
);
|
|
765
|
+
at += STALE_ENTRY;
|
|
766
|
+
}
|
|
767
|
+
this.#rows.set(cell, { rows, batches: cell.batches.length });
|
|
768
|
+
return rows;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
#assetSlot(id: string): number {
|
|
772
|
+
let slot = this.#assetIndex.get(id);
|
|
773
|
+
if (slot === undefined) {
|
|
774
|
+
slot = this.#assetIds.length;
|
|
775
|
+
this.#assetIds.push(id);
|
|
776
|
+
this.#assetIndex.set(id, slot);
|
|
777
|
+
}
|
|
778
|
+
return slot;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/** Each slab asset's gates, read live: `[count, [at, count]..., values]`, `at` -1 for a gone asset. */
|
|
782
|
+
#gateTable(): Float64Array {
|
|
783
|
+
const ids = this.#assetIds;
|
|
784
|
+
let values = 0;
|
|
785
|
+
for (const id of ids) values += this.#host.asset(id)?.gates.length ?? 0;
|
|
786
|
+
const table = new Float64Array(1 + ids.length * 2 + values);
|
|
787
|
+
table[0] = ids.length;
|
|
788
|
+
let at = 1 + ids.length * 2;
|
|
789
|
+
for (const [slot, id] of ids.entries()) {
|
|
790
|
+
const gates = this.#host.asset(id)?.gates;
|
|
791
|
+
table[1 + slot * 2] = gates === undefined ? -1 : at;
|
|
792
|
+
table[2 + slot * 2] = gates?.length ?? 0;
|
|
793
|
+
if (gates !== undefined) table.set(gates, at);
|
|
794
|
+
at += gates?.length ?? 0;
|
|
795
|
+
}
|
|
796
|
+
return table;
|
|
797
|
+
}
|
|
798
|
+
}
|