@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,566 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BufferAttribute,
|
|
3
|
+
BufferGeometry,
|
|
4
|
+
InterleavedBufferAttribute,
|
|
5
|
+
Matrix4,
|
|
6
|
+
Object3D,
|
|
7
|
+
} from "three";
|
|
8
|
+
|
|
9
|
+
import { Vector3 } from "three";
|
|
10
|
+
|
|
11
|
+
import type { ITerrainEdgeFrame } from "./terrain-jobs.js";
|
|
12
|
+
import { edgeWorldPoint } from "./terrain-jobs.js";
|
|
13
|
+
import type {
|
|
14
|
+
IEdgeSamples,
|
|
15
|
+
ILevelGeometry,
|
|
16
|
+
IResidentTile,
|
|
17
|
+
IStitchBridge,
|
|
18
|
+
NeighborPair,
|
|
19
|
+
} from "./world-tiles.js";
|
|
20
|
+
|
|
21
|
+
import type { Heightfield } from "./world-heightfield.js";
|
|
22
|
+
|
|
23
|
+
export const BRIDGE_COORDINATE_EPSILON = 1e-4;
|
|
24
|
+
|
|
25
|
+
export const BRIDGE_COORDINATE_RELATIVE_EPSILON = 2 ** -22;
|
|
26
|
+
|
|
27
|
+
export const BRIDGE_STRIP_INDEX_PATTERN = [0, 1, 2, 2, 1, 3, 2, 1, 0, 3, 1, 2] as const;
|
|
28
|
+
|
|
29
|
+
export type PositionAttribute = BufferAttribute | InterleavedBufferAttribute;
|
|
30
|
+
|
|
31
|
+
export function bridgeCoordinateMatches(actual: number, expected: number): boolean {
|
|
32
|
+
return (
|
|
33
|
+
Math.abs(actual - expected) <=
|
|
34
|
+
Math.max(
|
|
35
|
+
BRIDGE_COORDINATE_EPSILON,
|
|
36
|
+
Math.max(1, Math.abs(actual), Math.abs(expected)) * BRIDGE_COORDINATE_RELATIVE_EPSILON,
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** One field's own rectangle and side, which is all a world point along an edge needs. */
|
|
42
|
+
export function tileEdgeFrame(field: Heightfield, side: keyof IEdgeSamples): ITerrainEdgeFrame {
|
|
43
|
+
return { depth: field.depth, origin: field.origin, side, width: field.width };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function edgeVertexIndex(level: ILevelGeometry, side: keyof IEdgeSamples, index: number): number {
|
|
47
|
+
const row = side === "north" ? 0 : side === "south" ? level.resolution - 1 : index;
|
|
48
|
+
const column = side === "west" ? 0 : side === "east" ? level.resolution - 1 : index;
|
|
49
|
+
return row * level.resolution + column;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function opposingEdge(
|
|
53
|
+
a: IResidentTile,
|
|
54
|
+
b: IResidentTile,
|
|
55
|
+
): [keyof IEdgeSamples, keyof IEdgeSamples] {
|
|
56
|
+
if (a.tileX < b.tileX) return ["east", "west"];
|
|
57
|
+
if (a.tileX > b.tileX) return ["west", "east"];
|
|
58
|
+
if (a.tileZ < b.tileZ) return ["south", "north"];
|
|
59
|
+
return ["north", "south"];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function renderedLevel(tile: IResidentTile): ILevelGeometry | undefined {
|
|
63
|
+
return tile.levels.find(({ mesh }) => mesh.visible) ?? tile.levels[tile.lodLevel];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* An attribute's vertex count while every component it draws is finite, `NaN` once one is not.
|
|
68
|
+
* Every writer this package owns goes through `needsUpdate`, which bumps `version`, so a version
|
|
69
|
+
* alone cannot see a writer that reached past the attribute — and `NaN` never compares equal to the
|
|
70
|
+
* finite state the last reconcile recorded, so a settled seam still fails closed on it. The scan is
|
|
71
|
+
* the cost of that, against the per-sample walk it keeps off a settled frame, so it is validation:
|
|
72
|
+
* with `validate` off the count alone is compared, and a writer that reached past `needsUpdate`
|
|
73
|
+
* leaves a geometry the next reconcile reads the same way it always did.
|
|
74
|
+
*/
|
|
75
|
+
function positionFingerprint(position: BufferAttribute | undefined, validate: boolean): number {
|
|
76
|
+
if (position === undefined) return -1;
|
|
77
|
+
if (!validate) return position.count;
|
|
78
|
+
const values = position.array;
|
|
79
|
+
for (let index = 0; index < values.length; index += 1)
|
|
80
|
+
if (!Number.isFinite(values[index])) return Number.NaN;
|
|
81
|
+
return position.count;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* What a pair's seam was reconciled against: each side's rendered level, its buffers and where its
|
|
86
|
+
* mesh sits. Every writer to a level sets `needsUpdate`, which bumps the version, so an unchanged
|
|
87
|
+
* state means neither facing edge can have moved since — and an edge is sampled through
|
|
88
|
+
* `level.mesh.matrixWorld`, so a level mesh moved from outside changed what the observation reads
|
|
89
|
+
* without touching a single version. Its own transform is what that matrix composes from, and it is
|
|
90
|
+
* what any outside writer moves: an ancestor of it is either the tiles group, whose own movement the
|
|
91
|
+
* bridge state already observes, or the LOD this package owns and never moves, and a seam exists
|
|
92
|
+
* only where a bridge is. Numbers, not a string, and pushed rather than returned: this runs for
|
|
93
|
+
* every resident pair every frame (544 pairs on a 289-tile ring) into a reused buffer, and the
|
|
94
|
+
* intermediate array per level and per bridge was itself the cost the settled ring was measured on.
|
|
95
|
+
*/
|
|
96
|
+
export function pushLevelState(state: number[], level: ILevelGeometry): void {
|
|
97
|
+
const geometry = level.geometry;
|
|
98
|
+
const { mesh } = level;
|
|
99
|
+
state.push(
|
|
100
|
+
geometry.id,
|
|
101
|
+
(geometry.getAttribute("position") as BufferAttribute).version,
|
|
102
|
+
(geometry.getAttribute("normal") as BufferAttribute).version,
|
|
103
|
+
mesh.position.x,
|
|
104
|
+
mesh.position.y,
|
|
105
|
+
mesh.position.z,
|
|
106
|
+
mesh.quaternion.x,
|
|
107
|
+
mesh.quaternion.y,
|
|
108
|
+
mesh.quaternion.z,
|
|
109
|
+
mesh.quaternion.w,
|
|
110
|
+
mesh.scale.x,
|
|
111
|
+
mesh.scale.y,
|
|
112
|
+
mesh.scale.z,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Whether both sides of a pair have a rendered level, which is what its state needs to exist. */
|
|
117
|
+
export function pushPairState(state: number[], pair: NeighborPair): boolean {
|
|
118
|
+
const aLevel = renderedLevel(pair[0]);
|
|
119
|
+
const bLevel = renderedLevel(pair[1]);
|
|
120
|
+
if (aLevel === undefined || bLevel === undefined) return false;
|
|
121
|
+
pushLevelState(state, aLevel);
|
|
122
|
+
pushLevelState(state, bLevel);
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Everything a seam observation reads: the pair's state plus the bridge's placement, parent, draw
|
|
128
|
+
* range and buffers. A diagnostic exists to catch a bridge moved, detached, emptied or rewritten,
|
|
129
|
+
* so each of those has to change this state and force a fresh observation.
|
|
130
|
+
*/
|
|
131
|
+
export function pushBridgeState(state: number[], bridge: IStitchBridge, validate: boolean): void {
|
|
132
|
+
const { mesh } = bridge;
|
|
133
|
+
// The mesh's own geometry rather than the record's: a bridge retargeted to foreign geometry reads
|
|
134
|
+
// perfectly well from the record, and `bridgeCoverageContext` is the only thing that rejects it.
|
|
135
|
+
const geometry = mesh.geometry;
|
|
136
|
+
const position = geometry.getAttribute("position") as BufferAttribute | undefined;
|
|
137
|
+
const normal = geometry.getAttribute("normal") as BufferAttribute | undefined;
|
|
138
|
+
const index = geometry.getIndex();
|
|
139
|
+
mesh.updateMatrixWorld();
|
|
140
|
+
state.push(mesh.id, mesh.parent?.id ?? -1, mesh.visible ? 1 : 0);
|
|
141
|
+
const elements = mesh.matrixWorld.elements;
|
|
142
|
+
for (let element = 0; element < elements.length; element += 1)
|
|
143
|
+
state.push(elements[element] as number);
|
|
144
|
+
state.push(
|
|
145
|
+
geometry.id,
|
|
146
|
+
position?.version ?? -1,
|
|
147
|
+
positionFingerprint(position, validate),
|
|
148
|
+
normal?.id ?? -1,
|
|
149
|
+
index?.id ?? -1,
|
|
150
|
+
index?.version ?? -1,
|
|
151
|
+
geometry.drawRange.start,
|
|
152
|
+
geometry.drawRange.count,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function seamState(
|
|
157
|
+
pair: NeighborPair,
|
|
158
|
+
bridge: IStitchBridge | undefined,
|
|
159
|
+
validate: boolean,
|
|
160
|
+
): number[] | undefined {
|
|
161
|
+
const state: number[] = [];
|
|
162
|
+
if (!pushPairState(state, pair)) return undefined;
|
|
163
|
+
if (bridge !== undefined) pushBridgeState(state, bridge, validate);
|
|
164
|
+
return state;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function sameState(a: readonly number[] | undefined, b: readonly number[] | undefined): boolean {
|
|
168
|
+
if (a === undefined || b === undefined || a.length !== b.length) return false;
|
|
169
|
+
for (let index = 0; index < a.length; index += 1) if (a[index] !== b[index]) return false;
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function edgeVertexHeight(level: ILevelGeometry, side: keyof IEdgeSamples, index: number): number {
|
|
174
|
+
const value = level.geometry.getAttribute("position").getY(edgeVertexIndex(level, side, index));
|
|
175
|
+
if (!Number.isFinite(value))
|
|
176
|
+
throw new Error("TerrainTiles seam diagnostic edge height must be finite.");
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function edgeWorldVertexHeight(
|
|
181
|
+
level: ILevelGeometry,
|
|
182
|
+
side: keyof IEdgeSamples,
|
|
183
|
+
index: number,
|
|
184
|
+
target: Vector3,
|
|
185
|
+
): number {
|
|
186
|
+
const position = level.geometry.getAttribute("position");
|
|
187
|
+
const vertex = edgeVertexIndex(level, side, index);
|
|
188
|
+
const localX = position.getX(vertex);
|
|
189
|
+
const localY = position.getY(vertex);
|
|
190
|
+
const localZ = position.getZ(vertex);
|
|
191
|
+
if (!Number.isFinite(localX) || !Number.isFinite(localY) || !Number.isFinite(localZ))
|
|
192
|
+
throw new Error("TerrainTiles seam diagnostic bridge coordinates must be finite.");
|
|
193
|
+
target.set(localX, localY, localZ).applyMatrix4(level.mesh.matrixWorld);
|
|
194
|
+
if (!Number.isFinite(target.x) || !Number.isFinite(target.y) || !Number.isFinite(target.z))
|
|
195
|
+
throw new Error("TerrainTiles seam diagnostic bridge world coordinates must be finite.");
|
|
196
|
+
return target.y;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function edgeWorldHeight(
|
|
200
|
+
level: ILevelGeometry,
|
|
201
|
+
side: keyof IEdgeSamples,
|
|
202
|
+
normalized: number,
|
|
203
|
+
target: Vector3,
|
|
204
|
+
): number {
|
|
205
|
+
const position = Math.max(0, Math.min(1, normalized)) * (level.resolution - 1);
|
|
206
|
+
const lower = Math.floor(position);
|
|
207
|
+
const upper = Math.min(level.resolution - 1, lower + 1);
|
|
208
|
+
const mix = position - lower;
|
|
209
|
+
return (
|
|
210
|
+
edgeWorldVertexHeight(level, side, lower, target) * (1 - mix) +
|
|
211
|
+
edgeWorldVertexHeight(level, side, upper, target) * mix
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function bridgeEndpointWorldHeight(
|
|
216
|
+
position: PositionAttribute,
|
|
217
|
+
matrixWorld: Matrix4,
|
|
218
|
+
vertex: number,
|
|
219
|
+
expectedX: number,
|
|
220
|
+
expectedY: number,
|
|
221
|
+
expectedZ: number,
|
|
222
|
+
worldPosition: Vector3,
|
|
223
|
+
): number {
|
|
224
|
+
const localX = position.getX(vertex);
|
|
225
|
+
const localY = position.getY(vertex);
|
|
226
|
+
const localZ = position.getZ(vertex);
|
|
227
|
+
if (!Number.isFinite(localX) || !Number.isFinite(localY) || !Number.isFinite(localZ))
|
|
228
|
+
throw new Error("TerrainTiles seam diagnostic bridge coordinates must be finite.");
|
|
229
|
+
worldPosition.set(localX, localY, localZ).applyMatrix4(matrixWorld);
|
|
230
|
+
if (
|
|
231
|
+
!Number.isFinite(worldPosition.x) ||
|
|
232
|
+
!Number.isFinite(worldPosition.y) ||
|
|
233
|
+
!Number.isFinite(worldPosition.z)
|
|
234
|
+
)
|
|
235
|
+
throw new Error("TerrainTiles seam diagnostic bridge world coordinates must be finite.");
|
|
236
|
+
if (
|
|
237
|
+
!Number.isFinite(expectedX) ||
|
|
238
|
+
!Number.isFinite(expectedY) ||
|
|
239
|
+
!Number.isFinite(expectedZ) ||
|
|
240
|
+
!bridgeCoordinateMatches(worldPosition.x, expectedX) ||
|
|
241
|
+
!bridgeCoordinateMatches(worldPosition.y, expectedY) ||
|
|
242
|
+
!bridgeCoordinateMatches(worldPosition.z, expectedZ)
|
|
243
|
+
)
|
|
244
|
+
throw new Error(
|
|
245
|
+
"TerrainTiles seam diagnostic bridge topology does not match current neighboring edges.",
|
|
246
|
+
);
|
|
247
|
+
return worldPosition.y;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function bridgeEndpointHeight(
|
|
251
|
+
bridge: IStitchBridge,
|
|
252
|
+
position: PositionAttribute,
|
|
253
|
+
sample: number,
|
|
254
|
+
endpoint: number,
|
|
255
|
+
tile: IResidentTile,
|
|
256
|
+
side: keyof IEdgeSamples,
|
|
257
|
+
level: ILevelGeometry,
|
|
258
|
+
worldPosition: Vector3,
|
|
259
|
+
expectedWorldPosition: Vector3,
|
|
260
|
+
): number {
|
|
261
|
+
const sampleNormalized = sample / (bridge.resolution - 1);
|
|
262
|
+
const [expectedX, , expectedZ] = edgeWorldPoint(
|
|
263
|
+
tileEdgeFrame(tile.field, side),
|
|
264
|
+
sampleNormalized,
|
|
265
|
+
0,
|
|
266
|
+
);
|
|
267
|
+
const expectedY = edgeWorldHeight(level, side, sampleNormalized, expectedWorldPosition);
|
|
268
|
+
return bridgeEndpointWorldHeight(
|
|
269
|
+
position,
|
|
270
|
+
bridge.mesh.matrixWorld,
|
|
271
|
+
sample * 2 + endpoint,
|
|
272
|
+
expectedX,
|
|
273
|
+
expectedY,
|
|
274
|
+
expectedZ,
|
|
275
|
+
worldPosition,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export interface IBridgeEndpoint {
|
|
280
|
+
readonly level: ILevelGeometry;
|
|
281
|
+
readonly side: keyof IEdgeSamples;
|
|
282
|
+
readonly tile: IResidentTile;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function bridgeEndpointPair(
|
|
286
|
+
a: IResidentTile,
|
|
287
|
+
aSide: keyof IEdgeSamples,
|
|
288
|
+
b: IResidentTile,
|
|
289
|
+
bSide: keyof IEdgeSamples,
|
|
290
|
+
): readonly [IBridgeEndpoint, IBridgeEndpoint] {
|
|
291
|
+
const aLevel = renderedLevel(a);
|
|
292
|
+
const bLevel = renderedLevel(b);
|
|
293
|
+
if (aLevel === undefined || bLevel === undefined)
|
|
294
|
+
throw new Error(
|
|
295
|
+
"TerrainTiles seam diagnostic bridge topology has no rendered neighboring edge.",
|
|
296
|
+
);
|
|
297
|
+
const aEndpoint = { level: aLevel, side: aSide, tile: a };
|
|
298
|
+
const bEndpoint = { level: bLevel, side: bSide, tile: b };
|
|
299
|
+
return aLevel.resolution > bLevel.resolution ? [aEndpoint, bEndpoint] : [bEndpoint, aEndpoint];
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function validateBridgeTriangleTopology(
|
|
303
|
+
geometry: BufferGeometry,
|
|
304
|
+
expectedVertexCount?: number,
|
|
305
|
+
): PositionAttribute {
|
|
306
|
+
const position = geometry.getAttribute("position");
|
|
307
|
+
const index = geometry.getIndex();
|
|
308
|
+
const drawRange = geometry.drawRange;
|
|
309
|
+
const positionArray = position?.array;
|
|
310
|
+
if (
|
|
311
|
+
position === undefined ||
|
|
312
|
+
position.itemSize !== 3 ||
|
|
313
|
+
!Number.isInteger(position.count) ||
|
|
314
|
+
position.count < 6 ||
|
|
315
|
+
position.count % 2 !== 0 ||
|
|
316
|
+
positionArray === undefined ||
|
|
317
|
+
positionArray.length < position.count * position.itemSize ||
|
|
318
|
+
(expectedVertexCount !== undefined && position.count !== expectedVertexCount) ||
|
|
319
|
+
index === null ||
|
|
320
|
+
index.itemSize !== 1 ||
|
|
321
|
+
!Number.isInteger(index.count) ||
|
|
322
|
+
index.array.length !== index.count ||
|
|
323
|
+
!(index.array instanceof Uint16Array || index.array instanceof Uint32Array) ||
|
|
324
|
+
!Number.isInteger(drawRange.start) ||
|
|
325
|
+
drawRange.start !== 0 ||
|
|
326
|
+
(drawRange.count !== Number.POSITIVE_INFINITY &&
|
|
327
|
+
(!Number.isInteger(drawRange.count) ||
|
|
328
|
+
drawRange.count < index.count ||
|
|
329
|
+
drawRange.count % 3 !== 0))
|
|
330
|
+
)
|
|
331
|
+
throw new Error(
|
|
332
|
+
"TerrainTiles seam diagnostic bridge topology has invalid rendered triangle data.",
|
|
333
|
+
);
|
|
334
|
+
const vertexCount = position.count;
|
|
335
|
+
const stripResolution = vertexCount / 2;
|
|
336
|
+
const expectedIndexCount = (stripResolution - 1) * BRIDGE_STRIP_INDEX_PATTERN.length;
|
|
337
|
+
if (index.count !== expectedIndexCount)
|
|
338
|
+
throw new Error(
|
|
339
|
+
"TerrainTiles seam diagnostic bridge topology has invalid rendered triangle data.",
|
|
340
|
+
);
|
|
341
|
+
for (let vertex = 0; vertex < vertexCount; vertex += 1) {
|
|
342
|
+
if (
|
|
343
|
+
!Number.isFinite(position.getX(vertex)) ||
|
|
344
|
+
!Number.isFinite(position.getY(vertex)) ||
|
|
345
|
+
!Number.isFinite(position.getZ(vertex))
|
|
346
|
+
)
|
|
347
|
+
throw new Error("TerrainTiles seam diagnostic bridge coordinates must be finite.");
|
|
348
|
+
}
|
|
349
|
+
for (let offset = 0; offset < index.count; offset += 1) {
|
|
350
|
+
const segment = Math.floor(offset / BRIDGE_STRIP_INDEX_PATTERN.length);
|
|
351
|
+
const pair = segment * 2;
|
|
352
|
+
const expected =
|
|
353
|
+
pair + (BRIDGE_STRIP_INDEX_PATTERN[offset % BRIDGE_STRIP_INDEX_PATTERN.length] as number);
|
|
354
|
+
const vertex = index.getX(offset);
|
|
355
|
+
if (vertex !== expected)
|
|
356
|
+
throw new Error("TerrainTiles seam diagnostic bridge topology has invalid index data.");
|
|
357
|
+
}
|
|
358
|
+
return position;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export function seamCoverageDepth(a: IResidentTile, b: IResidentTile): number {
|
|
362
|
+
const aLevel = a.levels[a.lodLevel];
|
|
363
|
+
const bLevel = b.levels[b.lodLevel];
|
|
364
|
+
if (aLevel === undefined || bLevel === undefined) return 0;
|
|
365
|
+
return Math.min(aLevel.skirtDepth, bLevel.skirtDepth);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* One edge of one level, addressed in the position buffer directly.
|
|
370
|
+
*
|
|
371
|
+
* The seam diagnostic reads two edges once per sample of every pair, four times a frame, so the
|
|
372
|
+
* per-read cost is the per-frame cost: resolving the attribute and calling `getY` per vertex was
|
|
373
|
+
* ~40% of a settled 289-tile frame. The Y of edge vertex `index` sits at `first + index * span`
|
|
374
|
+
* in the interleaved array, and stepping an edge by one vertex steps the array by `span`.
|
|
375
|
+
*/
|
|
376
|
+
export interface IEdgeWalk {
|
|
377
|
+
readonly first: number;
|
|
378
|
+
readonly heights: Float32Array;
|
|
379
|
+
readonly resolution: number;
|
|
380
|
+
readonly span: number;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export function edgeWalk(level: ILevelGeometry, side: keyof IEdgeSamples): IEdgeWalk {
|
|
384
|
+
const position = level.geometry.getAttribute("position");
|
|
385
|
+
const array = position.array as Float32Array;
|
|
386
|
+
const last = level.resolution - 1;
|
|
387
|
+
const first =
|
|
388
|
+
side === "north" ? 0 : side === "south" ? last * level.resolution : side === "west" ? 0 : last;
|
|
389
|
+
return {
|
|
390
|
+
first: first * 3 + 1,
|
|
391
|
+
heights: array,
|
|
392
|
+
resolution: level.resolution,
|
|
393
|
+
span: (side === "north" || side === "south" ? 1 : level.resolution) * 3,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function edgeWalkHeight(walk: IEdgeWalk, index: number): number {
|
|
398
|
+
const lower = walk.heights[walk.first + index * walk.span] as number;
|
|
399
|
+
if (!Number.isFinite(lower))
|
|
400
|
+
throw new Error("TerrainTiles seam diagnostic edge height must be finite.");
|
|
401
|
+
return lower;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/** The walk's height at `normalized`, interpolating exactly as `edgeHeight` does. */
|
|
405
|
+
export function edgeWalkAt(walk: IEdgeWalk, normalized: number): number {
|
|
406
|
+
const position = normalized * (walk.resolution - 1);
|
|
407
|
+
const lower = Math.floor(position);
|
|
408
|
+
const mix = position - lower;
|
|
409
|
+
const lowerValue = edgeWalkHeight(walk, lower);
|
|
410
|
+
if (mix === 0) return lowerValue;
|
|
411
|
+
return (
|
|
412
|
+
lowerValue * (1 - mix) + edgeWalkHeight(walk, Math.min(walk.resolution - 1, lower + 1)) * mix
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** A pair's bridge, validated and placed once so the per-sample loop only measures. */
|
|
417
|
+
export interface IBridgeCoverage {
|
|
418
|
+
readonly bridge: IStitchBridge;
|
|
419
|
+
readonly coarser: IBridgeEndpoint;
|
|
420
|
+
readonly finer: IBridgeEndpoint;
|
|
421
|
+
readonly position: PositionAttribute;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function bridgeCoverageContext(
|
|
425
|
+
bridge: IStitchBridge,
|
|
426
|
+
a: IResidentTile,
|
|
427
|
+
aSide: keyof IEdgeSamples,
|
|
428
|
+
b: IResidentTile,
|
|
429
|
+
bSide: keyof IEdgeSamples,
|
|
430
|
+
): IBridgeCoverage {
|
|
431
|
+
if (bridge.mesh.geometry !== bridge.geometry)
|
|
432
|
+
throw new Error("TerrainTiles seam diagnostic bridge geometry is not attached to its mesh.");
|
|
433
|
+
if (!Number.isInteger(bridge.resolution) || bridge.resolution < 2)
|
|
434
|
+
throw new Error("TerrainTiles seam diagnostic bridge resolution is invalid.");
|
|
435
|
+
if (
|
|
436
|
+
bridge.keys[0] !== (a.key < b.key ? a.key : b.key) ||
|
|
437
|
+
bridge.keys[1] !== (a.key < b.key ? b.key : a.key)
|
|
438
|
+
)
|
|
439
|
+
throw new Error("TerrainTiles seam diagnostic bridge topology does not match its tile pair.");
|
|
440
|
+
const position = validateBridgeTriangleTopology(bridge.mesh.geometry, bridge.resolution * 2);
|
|
441
|
+
if (position.count !== bridge.resolution * 2)
|
|
442
|
+
throw new Error("TerrainTiles seam diagnostic bridge topology has invalid coverage.");
|
|
443
|
+
const [finer, coarser] = bridgeEndpointPair(a, aSide, b, bSide);
|
|
444
|
+
// Nothing inside the sample loop moves these, so deriving them per sample only repeated work:
|
|
445
|
+
// three world-matrix updates and a full revalidation of the bridge's own topology for every
|
|
446
|
+
// edge vertex of every pair, four times a frame.
|
|
447
|
+
bridge.mesh.updateWorldMatrix(true, false);
|
|
448
|
+
finer.level.mesh.updateWorldMatrix(true, false);
|
|
449
|
+
coarser.level.mesh.updateWorldMatrix(true, false);
|
|
450
|
+
return { bridge, coarser, finer, position };
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export function bridgeCoverageAt(
|
|
454
|
+
coverage: IBridgeCoverage,
|
|
455
|
+
normalized: number,
|
|
456
|
+
worldPosition: Vector3,
|
|
457
|
+
expectedWorldPosition: Vector3,
|
|
458
|
+
): number {
|
|
459
|
+
if (!Number.isFinite(normalized))
|
|
460
|
+
throw new Error("TerrainTiles seam diagnostic bridge sample coordinate must be finite.");
|
|
461
|
+
const { bridge, coarser, finer, position } = coverage;
|
|
462
|
+
const samplePosition = Math.max(0, Math.min(1, normalized)) * (bridge.resolution - 1);
|
|
463
|
+
const lower = Math.floor(samplePosition);
|
|
464
|
+
const upper = Math.min(bridge.resolution - 1, lower + 1);
|
|
465
|
+
const mix = samplePosition - lower;
|
|
466
|
+
const fine =
|
|
467
|
+
bridgeEndpointHeight(
|
|
468
|
+
bridge,
|
|
469
|
+
position,
|
|
470
|
+
lower,
|
|
471
|
+
0,
|
|
472
|
+
finer.tile,
|
|
473
|
+
finer.side,
|
|
474
|
+
finer.level,
|
|
475
|
+
worldPosition,
|
|
476
|
+
expectedWorldPosition,
|
|
477
|
+
) *
|
|
478
|
+
(1 - mix) +
|
|
479
|
+
bridgeEndpointHeight(
|
|
480
|
+
bridge,
|
|
481
|
+
position,
|
|
482
|
+
upper,
|
|
483
|
+
0,
|
|
484
|
+
finer.tile,
|
|
485
|
+
finer.side,
|
|
486
|
+
finer.level,
|
|
487
|
+
worldPosition,
|
|
488
|
+
expectedWorldPosition,
|
|
489
|
+
) *
|
|
490
|
+
mix;
|
|
491
|
+
const coarse =
|
|
492
|
+
bridgeEndpointHeight(
|
|
493
|
+
bridge,
|
|
494
|
+
position,
|
|
495
|
+
lower,
|
|
496
|
+
1,
|
|
497
|
+
coarser.tile,
|
|
498
|
+
coarser.side,
|
|
499
|
+
coarser.level,
|
|
500
|
+
worldPosition,
|
|
501
|
+
expectedWorldPosition,
|
|
502
|
+
) *
|
|
503
|
+
(1 - mix) +
|
|
504
|
+
bridgeEndpointHeight(
|
|
505
|
+
bridge,
|
|
506
|
+
position,
|
|
507
|
+
upper,
|
|
508
|
+
1,
|
|
509
|
+
coarser.tile,
|
|
510
|
+
coarser.side,
|
|
511
|
+
coarser.level,
|
|
512
|
+
worldPosition,
|
|
513
|
+
expectedWorldPosition,
|
|
514
|
+
) *
|
|
515
|
+
mix;
|
|
516
|
+
const covered = Math.abs(fine - coarse);
|
|
517
|
+
if (!Number.isFinite(covered))
|
|
518
|
+
throw new Error("TerrainTiles seam diagnostic bridge coverage must be finite.");
|
|
519
|
+
return covered;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export function seamObservation(
|
|
523
|
+
a: IResidentTile,
|
|
524
|
+
b: IResidentTile,
|
|
525
|
+
bridge: IStitchBridge | undefined,
|
|
526
|
+
owner: Object3D,
|
|
527
|
+
includeBridgeCoverage: boolean,
|
|
528
|
+
): { gap: number; visualGap: number } {
|
|
529
|
+
const [aSide, bSide] = opposingEdge(a, b);
|
|
530
|
+
const aLevel = renderedLevel(a);
|
|
531
|
+
const bLevel = renderedLevel(b);
|
|
532
|
+
if (aLevel === undefined || bLevel === undefined)
|
|
533
|
+
throw new Error("TerrainTiles seam diagnostic observation has no rendered level.");
|
|
534
|
+
const samples = Math.max(aLevel.resolution, bLevel.resolution);
|
|
535
|
+
const aWalk = edgeWalk(aLevel, aSide);
|
|
536
|
+
const bWalk = edgeWalk(bLevel, bSide);
|
|
537
|
+
const coverage =
|
|
538
|
+
includeBridgeCoverage &&
|
|
539
|
+
bridge !== undefined &&
|
|
540
|
+
bridge.mesh.parent === owner &&
|
|
541
|
+
bridge.mesh.visible
|
|
542
|
+
? bridgeCoverageContext(bridge, a, aSide, b, bSide)
|
|
543
|
+
: undefined;
|
|
544
|
+
const skirt = seamCoverageDepth(a, b);
|
|
545
|
+
const worldPosition = new Vector3();
|
|
546
|
+
const expectedWorldPosition = new Vector3();
|
|
547
|
+
let gap = 0;
|
|
548
|
+
let visualGap = 0;
|
|
549
|
+
for (let index = 0; index < samples; index += 1) {
|
|
550
|
+
const normalized = samples === 1 ? 0 : index / (samples - 1);
|
|
551
|
+
const currentGap = Math.abs(edgeWalkAt(aWalk, normalized) - edgeWalkAt(bWalk, normalized));
|
|
552
|
+
if (!Number.isFinite(currentGap))
|
|
553
|
+
throw new Error("TerrainTiles seam diagnostic observation must be finite.");
|
|
554
|
+
gap = Math.max(gap, currentGap);
|
|
555
|
+
const covered =
|
|
556
|
+
coverage === undefined
|
|
557
|
+
? 0
|
|
558
|
+
: bridgeCoverageAt(coverage, normalized, worldPosition, expectedWorldPosition);
|
|
559
|
+
visualGap = Math.max(visualGap, Math.max(0, currentGap - Math.max(skirt, covered)));
|
|
560
|
+
}
|
|
561
|
+
if (!Number.isFinite(visualGap))
|
|
562
|
+
throw new Error("TerrainTiles visual seam diagnostic observation must be finite.");
|
|
563
|
+
return { gap, visualGap };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export { edgeVertexIndex, positionFingerprint, sameState, edgeVertexHeight };
|