@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,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LOD sampling and blending: the level interpolations the pop measurement reads, the morph targets a
|
|
3
|
+
transition walks, and the surface delta that decides how coarse a level may be. Split out of
|
|
4
|
+
`world-tiles.ts` verbatim (PRD-797).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { BufferAttribute, type BufferGeometry, Vector3 } from "three";
|
|
8
|
+
import {
|
|
9
|
+
CONSTRUCTION_CHUNK_SAMPLES,
|
|
10
|
+
LOD_POP_THRESHOLD,
|
|
11
|
+
LOD_TRANSITION_FRAMES,
|
|
12
|
+
} from "./world-tile-budget.js";
|
|
13
|
+
import { refreshEdgeSamples, updateLevelSkirts } from "./world-tile-geometry.js";
|
|
14
|
+
import type { ILevelGeometry, ILodTransition, IResidentTile } from "./world-tiles.js";
|
|
15
|
+
export function levelHeight(
|
|
16
|
+
position: BufferAttribute,
|
|
17
|
+
resolution: number,
|
|
18
|
+
row: number,
|
|
19
|
+
column: number,
|
|
20
|
+
): number {
|
|
21
|
+
const value = position.getY(row * resolution + column);
|
|
22
|
+
if (!Number.isFinite(value)) throw new Error("TerrainTiles LOD geometry has an invalid height.");
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function interpolatedLevelHeight(
|
|
27
|
+
level: ILevelGeometry,
|
|
28
|
+
position: BufferAttribute,
|
|
29
|
+
x: number,
|
|
30
|
+
z: number,
|
|
31
|
+
): number {
|
|
32
|
+
const column = Math.max(0, Math.min(1, x)) * (level.resolution - 1);
|
|
33
|
+
const row = Math.max(0, Math.min(1, z)) * (level.resolution - 1);
|
|
34
|
+
const column0 = Math.floor(column);
|
|
35
|
+
const row0 = Math.floor(row);
|
|
36
|
+
const column1 = Math.min(level.resolution - 1, column0 + 1);
|
|
37
|
+
const row1 = Math.min(level.resolution - 1, row0 + 1);
|
|
38
|
+
const columnMix = column - column0;
|
|
39
|
+
const rowMix = row - row0;
|
|
40
|
+
const upperLeft = levelHeight(position, level.resolution, row0, column0);
|
|
41
|
+
const upperRight = levelHeight(position, level.resolution, row0, column1);
|
|
42
|
+
const lowerLeft = levelHeight(position, level.resolution, row1, column0);
|
|
43
|
+
const lowerRight = levelHeight(position, level.resolution, row1, column1);
|
|
44
|
+
const upper = upperLeft + (upperRight - upperLeft) * columnMix;
|
|
45
|
+
const lower = lowerLeft + (lowerRight - lowerLeft) * columnMix;
|
|
46
|
+
return upper + (lower - upper) * rowMix;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function interpolatedLevelNormal(
|
|
50
|
+
level: ILevelGeometry,
|
|
51
|
+
normal: BufferAttribute,
|
|
52
|
+
x: number,
|
|
53
|
+
z: number,
|
|
54
|
+
target: Vector3,
|
|
55
|
+
): Vector3 {
|
|
56
|
+
const column = Math.max(0, Math.min(1, x)) * (level.resolution - 1);
|
|
57
|
+
const row = Math.max(0, Math.min(1, z)) * (level.resolution - 1);
|
|
58
|
+
const column0 = Math.floor(column);
|
|
59
|
+
const row0 = Math.floor(row);
|
|
60
|
+
const column1 = Math.min(level.resolution - 1, column0 + 1);
|
|
61
|
+
const row1 = Math.min(level.resolution - 1, row0 + 1);
|
|
62
|
+
const columnMix = column - column0;
|
|
63
|
+
const rowMix = row - row0;
|
|
64
|
+
const upperLeft = row0 * level.resolution + column0;
|
|
65
|
+
const upperRight = row0 * level.resolution + column1;
|
|
66
|
+
const lowerLeft = row1 * level.resolution + column0;
|
|
67
|
+
const lowerRight = row1 * level.resolution + column1;
|
|
68
|
+
const blend = (upper: number, lower: number): number => upper + (lower - upper) * rowMix;
|
|
69
|
+
const interpolate = (
|
|
70
|
+
topLeft: number,
|
|
71
|
+
topRight: number,
|
|
72
|
+
bottomLeft: number,
|
|
73
|
+
bottomRight: number,
|
|
74
|
+
) =>
|
|
75
|
+
blend(
|
|
76
|
+
topLeft + (topRight - topLeft) * columnMix,
|
|
77
|
+
bottomLeft + (bottomRight - bottomLeft) * columnMix,
|
|
78
|
+
);
|
|
79
|
+
return target
|
|
80
|
+
.set(
|
|
81
|
+
interpolate(
|
|
82
|
+
normal.getX(upperLeft),
|
|
83
|
+
normal.getX(upperRight),
|
|
84
|
+
normal.getX(lowerLeft),
|
|
85
|
+
normal.getX(lowerRight),
|
|
86
|
+
),
|
|
87
|
+
interpolate(
|
|
88
|
+
normal.getY(upperLeft),
|
|
89
|
+
normal.getY(upperRight),
|
|
90
|
+
normal.getY(lowerLeft),
|
|
91
|
+
normal.getY(lowerRight),
|
|
92
|
+
),
|
|
93
|
+
interpolate(
|
|
94
|
+
normal.getZ(upperLeft),
|
|
95
|
+
normal.getZ(upperRight),
|
|
96
|
+
normal.getZ(lowerLeft),
|
|
97
|
+
normal.getZ(lowerRight),
|
|
98
|
+
),
|
|
99
|
+
)
|
|
100
|
+
.normalize();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export const morphSourceVersions = new WeakMap<
|
|
104
|
+
BufferGeometry,
|
|
105
|
+
readonly { attribute: BufferAttribute; version: number }[]
|
|
106
|
+
>();
|
|
107
|
+
|
|
108
|
+
/** Immutable stock morph buffers; shared sides keep the canonical surface used by seam jobs. */
|
|
109
|
+
export function* buildLodMorphTargets(
|
|
110
|
+
finer: ILevelGeometry,
|
|
111
|
+
coarserLevels: readonly ILevelGeometry[],
|
|
112
|
+
sharedEdges: number,
|
|
113
|
+
chunked: boolean,
|
|
114
|
+
previous?: BufferGeometry,
|
|
115
|
+
): Generator<void> {
|
|
116
|
+
const sources = [finer, ...coarserLevels].flatMap((level) =>
|
|
117
|
+
["position", "normal"].map((name) => {
|
|
118
|
+
const attribute = level.geometry.getAttribute(name) as BufferAttribute;
|
|
119
|
+
return { attribute, version: attribute.version };
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
const retained = previous === undefined ? undefined : morphSourceVersions.get(previous);
|
|
123
|
+
const boundaryOnly =
|
|
124
|
+
retained?.length === sources.length &&
|
|
125
|
+
sources.every(
|
|
126
|
+
({ attribute, version }, index) =>
|
|
127
|
+
retained[index]?.attribute === attribute && retained[index]?.version === version,
|
|
128
|
+
);
|
|
129
|
+
const position = finer.geometry.getAttribute("position");
|
|
130
|
+
const normal = finer.geometry.getAttribute("normal");
|
|
131
|
+
const positions: BufferAttribute[] = [];
|
|
132
|
+
const normals: BufferAttribute[] = [];
|
|
133
|
+
const coarseNormal = new Vector3();
|
|
134
|
+
const last = finer.resolution - 1;
|
|
135
|
+
for (const [target, coarser] of coarserLevels.entries()) {
|
|
136
|
+
const deltaPosition = boundaryOnly
|
|
137
|
+
? (finer.geometry.morphAttributes.position?.[target] as BufferAttribute).clone()
|
|
138
|
+
: new BufferAttribute(new Float32Array(position.count * 3), 3);
|
|
139
|
+
const deltaNormal = boundaryOnly
|
|
140
|
+
? (finer.geometry.morphAttributes.normal?.[target] as BufferAttribute).clone()
|
|
141
|
+
: new BufferAttribute(new Float32Array(position.count * 3), 3);
|
|
142
|
+
const coarsePosition = coarser.geometry.getAttribute("position") as BufferAttribute;
|
|
143
|
+
const coarseNormals = coarser.geometry.getAttribute("normal") as BufferAttribute;
|
|
144
|
+
for (let row = 0; row <= last; row += 1) {
|
|
145
|
+
// Mask changes only affect the perimeter when every source buffer is unchanged.
|
|
146
|
+
const columnStep = boundaryOnly && row > 0 && row < last ? last : 1;
|
|
147
|
+
for (let column = 0; column <= last; column += columnStep) {
|
|
148
|
+
const index = row * finer.resolution + column;
|
|
149
|
+
const pinned =
|
|
150
|
+
(row === 0 && sharedEdges & 1) ||
|
|
151
|
+
(row === last && sharedEdges & 2) ||
|
|
152
|
+
(column === 0 && sharedEdges & 4) ||
|
|
153
|
+
(column === last && sharedEdges & 8);
|
|
154
|
+
if (pinned) {
|
|
155
|
+
deltaPosition.setY(index, 0);
|
|
156
|
+
deltaNormal.setXYZ(index, 0, 0, 0);
|
|
157
|
+
} else {
|
|
158
|
+
deltaPosition.setY(
|
|
159
|
+
index,
|
|
160
|
+
interpolatedLevelHeight(coarser, coarsePosition, column / last, row / last) -
|
|
161
|
+
position.getY(index),
|
|
162
|
+
);
|
|
163
|
+
interpolatedLevelNormal(coarser, coarseNormals, column / last, row / last, coarseNormal);
|
|
164
|
+
deltaNormal.setXYZ(
|
|
165
|
+
index,
|
|
166
|
+
coarseNormal.x - normal.getX(index),
|
|
167
|
+
coarseNormal.y - normal.getY(index),
|
|
168
|
+
coarseNormal.z - normal.getZ(index),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
if (chunked && (index + 1) % (CONSTRUCTION_CHUNK_SAMPLES / 2) === 0) yield;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const edges = [
|
|
175
|
+
(index: number) => index,
|
|
176
|
+
(index: number) => last * finer.resolution + index,
|
|
177
|
+
(index: number) => index * finer.resolution,
|
|
178
|
+
(index: number) => index * finer.resolution + last,
|
|
179
|
+
];
|
|
180
|
+
for (const [edgeIndex, edge] of edges.entries())
|
|
181
|
+
for (let index = 0; index <= last; index += 1)
|
|
182
|
+
deltaPosition.setY(
|
|
183
|
+
finer.resolution ** 2 + edgeIndex * finer.resolution + index,
|
|
184
|
+
deltaPosition.getY(edge(index)),
|
|
185
|
+
);
|
|
186
|
+
positions.push(deltaPosition);
|
|
187
|
+
normals.push(deltaNormal);
|
|
188
|
+
}
|
|
189
|
+
finer.geometry.morphAttributes.position = positions;
|
|
190
|
+
finer.geometry.morphAttributes.normal = normals;
|
|
191
|
+
morphSourceVersions.set(finer.geometry, sources);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function updateLodMorph(
|
|
195
|
+
tile: IResidentTile,
|
|
196
|
+
transition: ILodTransition,
|
|
197
|
+
progress: number,
|
|
198
|
+
): void {
|
|
199
|
+
const from = tile.levels[transition.from];
|
|
200
|
+
const to = tile.levels[transition.to];
|
|
201
|
+
if (from === undefined || to === undefined)
|
|
202
|
+
throw new Error("TerrainTiles LOD transition references a missing level.");
|
|
203
|
+
const finer = from.resolution >= to.resolution ? from : to;
|
|
204
|
+
const coarser = finer === from ? to : from;
|
|
205
|
+
const target = tile.levels
|
|
206
|
+
.filter((level) => level !== finer && level.resolution <= finer.resolution)
|
|
207
|
+
.indexOf(coarser);
|
|
208
|
+
const influences = finer.mesh.morphTargetInfluences;
|
|
209
|
+
if (influences === undefined || influences[target] === undefined)
|
|
210
|
+
throw new Error("TerrainTiles LOD transition references a missing morph target.");
|
|
211
|
+
influences.fill(0);
|
|
212
|
+
influences[target] = finer === from ? progress : 1 - progress;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* A blend only ever moves a vertex between the two levels' surfaces, so the union of the two
|
|
217
|
+
* known bounds holds every frame of it. Widened once when the transition starts instead of
|
|
218
|
+
* recomputed per blend frame: `computeBoundingBox` + `computeBoundingSphere` walked the finer
|
|
219
|
+
* level's ~8,600 positions every frame of every transition (56 + 45 + 27 + 29 ms over a six-second
|
|
220
|
+
* walk). `#restoreLodTransition` puts the finer level's own bounds back when the blend ends.
|
|
221
|
+
*/
|
|
222
|
+
export function widenLevelBoundsForBlend(finer: ILevelGeometry, coarser: ILevelGeometry): void {
|
|
223
|
+
const box = finer.geometry.boundingBox;
|
|
224
|
+
const sphere = finer.geometry.boundingSphere;
|
|
225
|
+
const coarserBox = coarser.geometry.boundingBox;
|
|
226
|
+
if (box === null || sphere === null || coarserBox === null)
|
|
227
|
+
throw new Error("TerrainTiles LOD transition needs both levels' bounds to be known.");
|
|
228
|
+
box.union(coarserBox);
|
|
229
|
+
box.getBoundingSphere(sphere);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function updateLodTransitionGeometry(
|
|
233
|
+
tile: IResidentTile,
|
|
234
|
+
transition: ILodTransition,
|
|
235
|
+
progress: number,
|
|
236
|
+
): void {
|
|
237
|
+
const from = tile.levels[transition.from];
|
|
238
|
+
const to = tile.levels[transition.to];
|
|
239
|
+
if (from === undefined || to === undefined)
|
|
240
|
+
throw new Error("TerrainTiles LOD transition references a missing level.");
|
|
241
|
+
const finer = from.resolution >= to.resolution ? from : to;
|
|
242
|
+
const coarser = finer === from ? to : from;
|
|
243
|
+
const fromIsFiner = finer === from;
|
|
244
|
+
const position = finer.geometry.getAttribute("position");
|
|
245
|
+
const normalAttribute = finer.geometry.getAttribute("normal");
|
|
246
|
+
const coarsePosition = coarser.geometry.getAttribute("position") as BufferAttribute;
|
|
247
|
+
const coarseNormalAttribute = coarser.geometry.getAttribute("normal") as BufferAttribute;
|
|
248
|
+
const minimumX = tile.field.origin.x - tile.field.width / 2;
|
|
249
|
+
const minimumZ = tile.field.origin.z - tile.field.depth / 2;
|
|
250
|
+
const cellWidth = tile.field.width / (finer.resolution - 1);
|
|
251
|
+
const cellDepth = tile.field.depth / (finer.resolution - 1);
|
|
252
|
+
const fineNormal = new Vector3();
|
|
253
|
+
const coarseNormal = new Vector3();
|
|
254
|
+
const blendedNormal = new Vector3();
|
|
255
|
+
for (let row = 0; row < finer.resolution; row += 1) {
|
|
256
|
+
const z = minimumZ + row * cellDepth;
|
|
257
|
+
const normalizedZ = row / (finer.resolution - 1);
|
|
258
|
+
for (let column = 0; column < finer.resolution; column += 1) {
|
|
259
|
+
const x = minimumX + column * cellWidth;
|
|
260
|
+
const normalizedX = column / (finer.resolution - 1);
|
|
261
|
+
const index = row * finer.resolution + column;
|
|
262
|
+
const fineHeight = tile.field.heightAt(x, z);
|
|
263
|
+
const coarseHeight = interpolatedLevelHeight(
|
|
264
|
+
coarser,
|
|
265
|
+
coarsePosition,
|
|
266
|
+
normalizedX,
|
|
267
|
+
normalizedZ,
|
|
268
|
+
);
|
|
269
|
+
const startHeight = fromIsFiner ? fineHeight : coarseHeight;
|
|
270
|
+
const endHeight = fromIsFiner ? coarseHeight : fineHeight;
|
|
271
|
+
position.setY(index, startHeight + (endHeight - startHeight) * progress);
|
|
272
|
+
|
|
273
|
+
tile.field.normalAt(x, z, fineNormal);
|
|
274
|
+
interpolatedLevelNormal(
|
|
275
|
+
coarser,
|
|
276
|
+
coarseNormalAttribute,
|
|
277
|
+
normalizedX,
|
|
278
|
+
normalizedZ,
|
|
279
|
+
coarseNormal,
|
|
280
|
+
);
|
|
281
|
+
const startNormal = fromIsFiner ? fineNormal : coarseNormal;
|
|
282
|
+
const endNormal = fromIsFiner ? coarseNormal : fineNormal;
|
|
283
|
+
blendedNormal
|
|
284
|
+
.set(
|
|
285
|
+
startNormal.x + (endNormal.x - startNormal.x) * progress,
|
|
286
|
+
startNormal.y + (endNormal.y - startNormal.y) * progress,
|
|
287
|
+
startNormal.z + (endNormal.z - startNormal.z) * progress,
|
|
288
|
+
)
|
|
289
|
+
.normalize();
|
|
290
|
+
normalAttribute.setXYZ(index, blendedNormal.x, blendedNormal.y, blendedNormal.z);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
updateLevelSkirts(finer);
|
|
294
|
+
refreshEdgeSamples(finer);
|
|
295
|
+
position.needsUpdate = true;
|
|
296
|
+
normalAttribute.needsUpdate = true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function* surfaceHeightChunks(
|
|
300
|
+
level: ILevelGeometry,
|
|
301
|
+
chunked: boolean,
|
|
302
|
+
): Generator<void, Float32Array> {
|
|
303
|
+
const position = level.geometry.getAttribute("position");
|
|
304
|
+
const heights = new Float32Array(level.resolution * level.resolution);
|
|
305
|
+
for (let index = 0; index < heights.length; index += 1) {
|
|
306
|
+
heights[index] = position.getY(index);
|
|
307
|
+
if (chunked && (index + 1) % CONSTRUCTION_CHUNK_SAMPLES === 0) yield;
|
|
308
|
+
}
|
|
309
|
+
return heights;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function surfaceHeights(level: ILevelGeometry): Float32Array {
|
|
313
|
+
return surfaceHeightChunks(level, false).next().value as Float32Array;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function interpolatedSamplesHeight(
|
|
317
|
+
samples: Float32Array,
|
|
318
|
+
resolution: number,
|
|
319
|
+
x: number,
|
|
320
|
+
z: number,
|
|
321
|
+
): number {
|
|
322
|
+
const column = Math.max(0, Math.min(1, x)) * (resolution - 1);
|
|
323
|
+
const row = Math.max(0, Math.min(1, z)) * (resolution - 1);
|
|
324
|
+
const column0 = Math.floor(column);
|
|
325
|
+
const row0 = Math.floor(row);
|
|
326
|
+
const column1 = Math.min(resolution - 1, column0 + 1);
|
|
327
|
+
const row1 = Math.min(resolution - 1, row0 + 1);
|
|
328
|
+
const columnMix = column - column0;
|
|
329
|
+
const rowMix = row - row0;
|
|
330
|
+
const upperLeft = samples[row0 * resolution + column0] as number;
|
|
331
|
+
const upperRight = samples[row0 * resolution + column1] as number;
|
|
332
|
+
const lowerLeft = samples[row1 * resolution + column0] as number;
|
|
333
|
+
const lowerRight = samples[row1 * resolution + column1] as number;
|
|
334
|
+
const upper = upperLeft + (upperRight - upperLeft) * columnMix;
|
|
335
|
+
const lower = lowerLeft + (lowerRight - lowerLeft) * columnMix;
|
|
336
|
+
return upper + (lower - upper) * rowMix;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
export function* surfaceDeltaChunks(
|
|
340
|
+
samples: Float32Array,
|
|
341
|
+
resolution: number,
|
|
342
|
+
level: ILevelGeometry,
|
|
343
|
+
chunked: boolean,
|
|
344
|
+
): Generator<void, number> {
|
|
345
|
+
const sampleCount = Math.max(resolution, level.resolution);
|
|
346
|
+
const position = level.geometry.getAttribute("position") as BufferAttribute;
|
|
347
|
+
let maximum = 0;
|
|
348
|
+
for (let row = 0; row < sampleCount; row += 1) {
|
|
349
|
+
for (let column = 0; column < sampleCount; column += 1) {
|
|
350
|
+
const x = column / (sampleCount - 1);
|
|
351
|
+
const z = row / (sampleCount - 1);
|
|
352
|
+
maximum = Math.max(
|
|
353
|
+
maximum,
|
|
354
|
+
Math.abs(
|
|
355
|
+
interpolatedSamplesHeight(samples, resolution, x, z) -
|
|
356
|
+
interpolatedLevelHeight(level, position, x, z),
|
|
357
|
+
),
|
|
358
|
+
);
|
|
359
|
+
if (chunked && (row * sampleCount + column + 1) % CONSTRUCTION_CHUNK_SAMPLES === 0) yield;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return maximum;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function surfaceDeltaFromSamples(
|
|
366
|
+
samples: Float32Array,
|
|
367
|
+
resolution: number,
|
|
368
|
+
level: ILevelGeometry,
|
|
369
|
+
): number {
|
|
370
|
+
return surfaceDeltaChunks(samples, resolution, level, false).next().value as number;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* The coarsest level a tile may ever show. A level is selectable only when its height error
|
|
375
|
+
* against *every* finer level fits the pop bound, which is what keeps the per-frame displacement
|
|
376
|
+
* inside it: a transition walks one LOD_TRANSITION_FRAMES-th of the way between two levels, and
|
|
377
|
+
* an interrupted transition snaps back to a level endpoint, which is never further from the
|
|
378
|
+
* surface it replaced than the bound between any two selectable levels. A cliff therefore keeps
|
|
379
|
+
* its tile at a finer level instead of throwing out of the frame.
|
|
380
|
+
*/
|
|
381
|
+
export function* coarsestSelectableLevel(
|
|
382
|
+
levels: readonly ILevelGeometry[],
|
|
383
|
+
chunked: boolean,
|
|
384
|
+
): Generator<void, number> {
|
|
385
|
+
let coarsest = 0;
|
|
386
|
+
for (let index = 1; index < levels.length; index += 1) {
|
|
387
|
+
const level = levels[index];
|
|
388
|
+
if (level === undefined) break;
|
|
389
|
+
let error = 0;
|
|
390
|
+
for (let finerIndex = 0; finerIndex < index; finerIndex += 1) {
|
|
391
|
+
const finer = levels[finerIndex];
|
|
392
|
+
if (finer === undefined) break;
|
|
393
|
+
error = Math.max(
|
|
394
|
+
error,
|
|
395
|
+
yield* surfaceDeltaChunks(
|
|
396
|
+
yield* surfaceHeightChunks(finer, chunked),
|
|
397
|
+
finer.resolution,
|
|
398
|
+
level,
|
|
399
|
+
chunked,
|
|
400
|
+
),
|
|
401
|
+
);
|
|
402
|
+
}
|
|
403
|
+
level.surfaceError = error;
|
|
404
|
+
if (error > LOD_POP_THRESHOLD) break;
|
|
405
|
+
coarsest = index;
|
|
406
|
+
}
|
|
407
|
+
return coarsest;
|
|
408
|
+
}
|