@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,282 @@
|
|
|
1
|
+
import { BufferAttribute, BufferGeometry } from "three";
|
|
2
|
+
import type { Heightfield } from "./world-heightfield.js";
|
|
3
|
+
import { type IWaterLake, WATER_RIBBON_REACH, lakeReach } from "./world-water-bodies.js";
|
|
4
|
+
|
|
5
|
+
/** How a ribbon is cut; every value has the default `WaterBodies` and the Strata preview use. */
|
|
6
|
+
export interface IRiverRibbonOptions {
|
|
7
|
+
/** Vertices across each station. Default 25. */
|
|
8
|
+
readonly columns?: number;
|
|
9
|
+
/** Reach either side of the centreline, per metre of channel width. Default `WATER_RIBBON_REACH`. */
|
|
10
|
+
readonly reach?: number;
|
|
11
|
+
/** Deepest water the `metres` attribute records. Default unbounded. */
|
|
12
|
+
readonly maxDepth?: number;
|
|
13
|
+
/** Metres of depth lost per metre toward the ribbon's edge, so the edges read shallow. Default 0.2. */
|
|
14
|
+
readonly edgeSlope?: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The water surface of every river, as one geometry with one group per river.
|
|
19
|
+
*
|
|
20
|
+
* Each station is a row across the river at its own surface height, out to `reach` widths either side
|
|
21
|
+
* of the centreline, the reach `WaterBodies` floods; one more row a reach past each end stands where
|
|
22
|
+
* `WaterBodies` clamps a segment, so the water a swimmer finds past a river's end is drawn there too
|
|
23
|
+
* (cut at the end station, the surface stopped in a straight edge with its carved bed dry beyond).
|
|
24
|
+
* Quads dry on both rows against `field` are dropped, so the surface ends at the shore.
|
|
25
|
+
* Past an end where the ground falls below the end's bed, the water would run off: that cap is not
|
|
26
|
+
* drawn, and the end stays cut at its last station.
|
|
27
|
+
*
|
|
28
|
+
* Attributes for the game's water material: `flow` (the unit downstream heading), `flowUv` (metres
|
|
29
|
+
* down the centreline, metres across it) and `metres` (water depth over the ground). It decides no
|
|
30
|
+
* colour, ripple or opacity: the game shades it.
|
|
31
|
+
*
|
|
32
|
+
* @situation draw a river's water surface from the baked stations
|
|
33
|
+
* @situation a river's water stops in a straight edge, or a swimmer floats where no water is drawn
|
|
34
|
+
* @situation share one river shape between the drawn water and the swimmer
|
|
35
|
+
* @situation river water hangs over dry ground past the river's head or tail
|
|
36
|
+
* @constraint stations, width and reach come from the game's bake; the game's material shades the geometry
|
|
37
|
+
* @example const geometry = riverRibbonGeometry(rivers, field, { maxDepth: 1.6 });
|
|
38
|
+
*/
|
|
39
|
+
export function riverRibbonGeometry(
|
|
40
|
+
rivers: readonly { readonly points: readonly (readonly number[])[]; readonly width: number }[],
|
|
41
|
+
field: Heightfield,
|
|
42
|
+
options: IRiverRibbonOptions = {},
|
|
43
|
+
): BufferGeometry {
|
|
44
|
+
const columns = options.columns ?? 25;
|
|
45
|
+
if (!Number.isInteger(columns) || columns < 2)
|
|
46
|
+
throw new Error(
|
|
47
|
+
`TN_RIVER_RIBBON_INVALID: columns must be an integer of at least 2, received ${columns}.`,
|
|
48
|
+
);
|
|
49
|
+
const perWidth = options.reach ?? WATER_RIBBON_REACH;
|
|
50
|
+
const maxDepth = options.maxDepth ?? Number.POSITIVE_INFINITY;
|
|
51
|
+
const edgeSlope = options.edgeSlope ?? 0.2;
|
|
52
|
+
if (!(perWidth > 0) || !(maxDepth > 0) || !(edgeSlope >= 0))
|
|
53
|
+
throw new Error(
|
|
54
|
+
"TN_RIVER_RIBBON_INVALID: reach and maxDepth must be positive, edgeSlope not negative.",
|
|
55
|
+
);
|
|
56
|
+
const positions: number[] = [];
|
|
57
|
+
const flows: number[] = [];
|
|
58
|
+
const uvs: number[] = [];
|
|
59
|
+
const depths: number[] = [];
|
|
60
|
+
const indices: number[] = [];
|
|
61
|
+
const geometry = new BufferGeometry();
|
|
62
|
+
for (const river of rivers) {
|
|
63
|
+
if (!(river.width > 0) || river.points.length < 2)
|
|
64
|
+
throw new Error(
|
|
65
|
+
`TN_RIVER_RIBBON_INVALID: a river needs a positive width and two stations, received width ${river.width} and ${river.points.length} stations.`,
|
|
66
|
+
);
|
|
67
|
+
const start = indices.length;
|
|
68
|
+
ribbon(field, river.points, river.width * perWidth, columns, maxDepth, edgeSlope, {
|
|
69
|
+
depths,
|
|
70
|
+
flows,
|
|
71
|
+
indices,
|
|
72
|
+
positions,
|
|
73
|
+
uvs,
|
|
74
|
+
});
|
|
75
|
+
geometry.addGroup(start, indices.length - start, 0);
|
|
76
|
+
}
|
|
77
|
+
geometry.setAttribute("position", new BufferAttribute(new Float32Array(positions), 3));
|
|
78
|
+
geometry.setAttribute("flow", new BufferAttribute(new Float32Array(flows), 2));
|
|
79
|
+
geometry.setAttribute("flowUv", new BufferAttribute(new Float32Array(uvs), 2));
|
|
80
|
+
geometry.setAttribute("metres", new BufferAttribute(new Float32Array(depths), 1));
|
|
81
|
+
geometry.setIndex(indices);
|
|
82
|
+
geometry.computeVertexNormals();
|
|
83
|
+
geometry.computeBoundingBox();
|
|
84
|
+
geometry.computeBoundingSphere();
|
|
85
|
+
return geometry;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Metres the ground past an end may sit under its bed before the cap there counts as drained. */
|
|
89
|
+
const DRAIN_TOLERANCE = 0.05;
|
|
90
|
+
|
|
91
|
+
/** A station `reach` metres past `end`, away from `inner`, at the end's surface height. */
|
|
92
|
+
function beyond(
|
|
93
|
+
end: readonly number[],
|
|
94
|
+
inner: readonly number[],
|
|
95
|
+
reach: number,
|
|
96
|
+
): readonly number[] {
|
|
97
|
+
const dx = (end[0] ?? 0) - (inner[0] ?? 0);
|
|
98
|
+
const dz = (end[2] ?? 0) - (inner[2] ?? 0);
|
|
99
|
+
const length = Math.hypot(dx, dz) || 1;
|
|
100
|
+
return [
|
|
101
|
+
(end[0] ?? 0) + (dx / length) * reach,
|
|
102
|
+
end[1] ?? 0,
|
|
103
|
+
(end[2] ?? 0) + (dz / length) * reach,
|
|
104
|
+
];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function ribbon(
|
|
108
|
+
field: Heightfield,
|
|
109
|
+
stations: readonly (readonly number[])[],
|
|
110
|
+
reach: number,
|
|
111
|
+
columns: number,
|
|
112
|
+
maxDepth: number,
|
|
113
|
+
edgeSlope: number,
|
|
114
|
+
out: { positions: number[]; flows: number[]; uvs: number[]; depths: number[]; indices: number[] },
|
|
115
|
+
): void {
|
|
116
|
+
const points = [
|
|
117
|
+
beyond(stations[0] as readonly number[], stations[1] as readonly number[], reach),
|
|
118
|
+
...stations,
|
|
119
|
+
beyond(stations.at(-1) as readonly number[], stations.at(-2) as readonly number[], reach),
|
|
120
|
+
];
|
|
121
|
+
const base = out.positions.length / 3;
|
|
122
|
+
// A cap past a river that ends at the field's edge samples the edge: no ground is known past it.
|
|
123
|
+
const minX = field.origin.x - field.width / 2;
|
|
124
|
+
const minZ = field.origin.z - field.depth / 2;
|
|
125
|
+
const ground = (x: number, z: number): number =>
|
|
126
|
+
field.heightAt(
|
|
127
|
+
Math.min(minX + field.width, Math.max(minX, x)),
|
|
128
|
+
Math.min(minZ + field.depth, Math.max(minZ, z)),
|
|
129
|
+
);
|
|
130
|
+
const wet: number[] = [];
|
|
131
|
+
// A cap vertex over ground below the bed at its end station is drained: water there would run off,
|
|
132
|
+
// not stand at the end's level, so the quads that reach it are dropped and that end stays cut.
|
|
133
|
+
const last = points.length - 1;
|
|
134
|
+
const bedAt = (station: readonly number[]): number => ground(station[0] ?? 0, station[2] ?? 0);
|
|
135
|
+
const capBed = [
|
|
136
|
+
bedAt(stations[0] as readonly number[]),
|
|
137
|
+
bedAt(stations.at(-1) as readonly number[]),
|
|
138
|
+
];
|
|
139
|
+
const drained: boolean[] = [];
|
|
140
|
+
// Metres down the centreline, as Unreal builds a river's sections by distance along its spline and
|
|
141
|
+
// carries the flow per vertex (UE 5.8.3:
|
|
142
|
+
// Engine/Plugins/Experimental/Water/Source/Runtime/Private/WaterBodyRiverComponent.cpp:104-138).
|
|
143
|
+
let downstream = 0;
|
|
144
|
+
for (let k = 0; k < points.length; k += 1) {
|
|
145
|
+
const [x = 0, y = 0, z = 0] = points[k] as readonly number[];
|
|
146
|
+
if (k > 0) {
|
|
147
|
+
const [px = 0, , pz = 0] = points[k - 1] as readonly number[];
|
|
148
|
+
downstream += Math.hypot(x - px, z - pz);
|
|
149
|
+
}
|
|
150
|
+
const before = points[Math.max(0, k - 1)] as readonly number[];
|
|
151
|
+
const after = points[Math.min(points.length - 1, k + 1)] as readonly number[];
|
|
152
|
+
let dx = (after[0] ?? 0) - (before[0] ?? 0);
|
|
153
|
+
let dz = (after[2] ?? 0) - (before[2] ?? 0);
|
|
154
|
+
const length = Math.hypot(dx, dz) || 1;
|
|
155
|
+
dx /= length;
|
|
156
|
+
dz /= length;
|
|
157
|
+
for (let c = 0; c < columns; c += 1) {
|
|
158
|
+
const across = (c / (columns - 1) - 0.5) * 2 * reach;
|
|
159
|
+
const vx = x - dz * across;
|
|
160
|
+
const vz = z + dx * across;
|
|
161
|
+
out.positions.push(vx, y, vz);
|
|
162
|
+
out.flows.push(dx, dz);
|
|
163
|
+
out.uvs.push(downstream, across);
|
|
164
|
+
const below = ground(vx, vz);
|
|
165
|
+
const bed = k === 0 ? capBed[0] : k === last ? capBed[1] : undefined;
|
|
166
|
+
const dry = bed !== undefined && below < bed - DRAIN_TOLERANCE;
|
|
167
|
+
const depth = dry
|
|
168
|
+
? 0
|
|
169
|
+
: Math.min(
|
|
170
|
+
maxDepth,
|
|
171
|
+
Math.max(0, y - below),
|
|
172
|
+
Math.max(0, (reach - Math.abs(across)) * edgeSlope),
|
|
173
|
+
);
|
|
174
|
+
out.depths.push(depth);
|
|
175
|
+
wet.push(depth > 0 ? 1 : 0);
|
|
176
|
+
drained.push(dry);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
for (let k = 1; k < points.length; k += 1) {
|
|
180
|
+
for (let c = 1; c < columns; c += 1) {
|
|
181
|
+
const a = (k - 1) * columns + c - 1;
|
|
182
|
+
const d = k * columns + c - 1;
|
|
183
|
+
// A quad with a dry corner stays, so the game's depth ramp has dry ground to dissolve across;
|
|
184
|
+
// only a quad dry on both rows is dropped, which takes the surface off the hillside.
|
|
185
|
+
const rowA = (wet[a - 1] ?? 0) + (wet[a + 1] ?? 0) + (wet[d - 1] ?? 0) + (wet[d + 1] ?? 0);
|
|
186
|
+
const rowB = (wet[a] ?? 0) + (wet[a + 1] ?? 0) + (wet[d] ?? 0) + (wet[d + 1] ?? 0);
|
|
187
|
+
if (rowA === 0 && rowB === 0) continue;
|
|
188
|
+
if (drained[a] || drained[a + 1] || drained[d] || drained[d + 1]) continue;
|
|
189
|
+
// Wound so the face points up: along × across points down, so the pair is taken the other way.
|
|
190
|
+
out.indices.push(base + a, base + a + 1, base + d, base + a + 1, base + d + 1, base + d);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** How a lake's disc is meshed; every default is the Strata preview's. */
|
|
196
|
+
export interface ILakeSurfaceOptions {
|
|
197
|
+
/** Rings from the centre to the reach. Default 44. */
|
|
198
|
+
readonly rings?: number;
|
|
199
|
+
/** Vertices around each ring. Default 88. */
|
|
200
|
+
readonly spokes?: number;
|
|
201
|
+
/** How the ring spacing grows outwards; above 1 packs vertices into the near water. Default 1.7. */
|
|
202
|
+
readonly packing?: number;
|
|
203
|
+
/** Deepest water the `metres` attribute records. Default unbounded. */
|
|
204
|
+
readonly maxDepth?: number;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A lake's water surface: a level disc out to `lakeReach`, the footprint `WaterBodies` floods, with a
|
|
209
|
+
* `metres` attribute (water depth over the ground). Only cells with water at a corner get triangles,
|
|
210
|
+
* so the surface does not hover over dry hillside and the shore follows the ground's contour. The
|
|
211
|
+
* rings pack towards the middle, so an irregular basin costs vertices where the camera sees them.
|
|
212
|
+
* Returns `undefined` when the basin holds no water at its level.
|
|
213
|
+
*
|
|
214
|
+
* @situation draw a lake's water surface from the baked level and seed
|
|
215
|
+
* @situation a lake's drawn water and the swimmer's water disagree
|
|
216
|
+
* @constraint the lake row and field come from the game's bake; the game's material shades the geometry
|
|
217
|
+
* @example const geometry = lakeSurfaceGeometry({ ...lake, margin: 1.12 }, field, { maxDepth: 1.6 });
|
|
218
|
+
*/
|
|
219
|
+
export function lakeSurfaceGeometry(
|
|
220
|
+
lake: IWaterLake,
|
|
221
|
+
field: Heightfield,
|
|
222
|
+
options: ILakeSurfaceOptions = {},
|
|
223
|
+
): BufferGeometry | undefined {
|
|
224
|
+
const rings = options.rings ?? 44;
|
|
225
|
+
const spokes = options.spokes ?? 88;
|
|
226
|
+
const packing = options.packing ?? 1.7;
|
|
227
|
+
const maxDepth = options.maxDepth ?? Number.POSITIVE_INFINITY;
|
|
228
|
+
if (!Number.isInteger(rings) || rings < 2 || !Number.isInteger(spokes) || spokes < 3)
|
|
229
|
+
throw new Error(
|
|
230
|
+
`TN_LAKE_SURFACE_INVALID: rings must be an integer of at least 2 and spokes of at least 3, received ${rings} and ${spokes}.`,
|
|
231
|
+
);
|
|
232
|
+
if (!(packing > 0) || !(maxDepth > 0))
|
|
233
|
+
throw new Error("TN_LAKE_SURFACE_INVALID: packing and maxDepth must be positive.");
|
|
234
|
+
const reach = lakeReach(field, lake);
|
|
235
|
+
if (!(reach > 0)) return undefined;
|
|
236
|
+
const [cx = 0, cz = 0] = lake.at;
|
|
237
|
+
const minX = field.origin.x - field.width / 2;
|
|
238
|
+
const minZ = field.origin.z - field.depth / 2;
|
|
239
|
+
const count = (rings - 1) * spokes + 1;
|
|
240
|
+
const positions = new Float32Array(count * 3);
|
|
241
|
+
const depths = new Float32Array(count);
|
|
242
|
+
const place = (index: number, radius: number, spoke: number): void => {
|
|
243
|
+
const angle = (spoke / spokes) * Math.PI * 2;
|
|
244
|
+
const x = Math.max(minX, Math.min(minX + field.width, cx + Math.cos(angle) * radius));
|
|
245
|
+
const z = Math.max(minZ, Math.min(minZ + field.depth, cz + Math.sin(angle) * radius));
|
|
246
|
+
positions[index * 3] = x;
|
|
247
|
+
positions[index * 3 + 1] = lake.level;
|
|
248
|
+
positions[index * 3 + 2] = z;
|
|
249
|
+
depths[index] = Math.min(maxDepth, Math.max(0, lake.level - field.heightAt(x, z)));
|
|
250
|
+
};
|
|
251
|
+
place(0, 0, 0);
|
|
252
|
+
for (let ring = 1; ring < rings; ring += 1) {
|
|
253
|
+
const radius = reach * (ring / (rings - 1)) ** packing;
|
|
254
|
+
for (let spoke = 0; spoke < spokes; spoke += 1)
|
|
255
|
+
place(1 + (ring - 1) * spokes + spoke, radius, spoke);
|
|
256
|
+
}
|
|
257
|
+
const depth = (index: number): number => depths[index] ?? 0;
|
|
258
|
+
const indices: number[] = [];
|
|
259
|
+
// A cell with one wet corner stays, so the game's depth ramp has dry ground to fade across.
|
|
260
|
+
for (let spoke = 0; spoke < spokes; spoke += 1) {
|
|
261
|
+
const first = 1 + spoke;
|
|
262
|
+
const next = 1 + ((spoke + 1) % spokes);
|
|
263
|
+
if (depth(0) + depth(first) + depth(next) > 0) indices.push(0, first, next);
|
|
264
|
+
}
|
|
265
|
+
for (let ring = 2; ring < rings; ring += 1)
|
|
266
|
+
for (let spoke = 0; spoke < spokes; spoke += 1) {
|
|
267
|
+
const next = (spoke + 1) % spokes;
|
|
268
|
+
const inner = 1 + (ring - 2) * spokes + spoke;
|
|
269
|
+
const innerNext = 1 + (ring - 2) * spokes + next;
|
|
270
|
+
const outer = 1 + (ring - 1) * spokes + spoke;
|
|
271
|
+
const outerNext = 1 + (ring - 1) * spokes + next;
|
|
272
|
+
if (depth(inner) + depth(innerNext) + depth(outer) + depth(outerNext) <= 0) continue;
|
|
273
|
+
indices.push(inner, outer, innerNext, innerNext, outer, outerNext);
|
|
274
|
+
}
|
|
275
|
+
const geometry = new BufferGeometry();
|
|
276
|
+
geometry.setAttribute("position", new BufferAttribute(positions, 3));
|
|
277
|
+
geometry.setAttribute("metres", new BufferAttribute(depths, 1));
|
|
278
|
+
geometry.setIndex(indices);
|
|
279
|
+
geometry.computeVertexNormals();
|
|
280
|
+
geometry.computeBoundingSphere();
|
|
281
|
+
return geometry;
|
|
282
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Camera, Matrix4, type Vector3 } from "three";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The world's own records and the camera are in two different frames, and this is where they meet.
|
|
5
|
+
*
|
|
6
|
+
* `WorldCells` is an `Object3D`, and every placement, cell volume, terrain tile and GPU record under
|
|
7
|
+
* it is written in the package's own coordinates — the root's local frame. A camera is in world
|
|
8
|
+
* space. A game may move the root (`world.position.y = -11.45` sinks the whole ring), so a per-frame
|
|
9
|
+
* read that compares the two has to be taken in the root's frame: clip space from a local point is
|
|
10
|
+
* `projection × view × root`, and a world point read locally is the camera's world position through
|
|
11
|
+
* the root's inverse world matrix.
|
|
12
|
+
*
|
|
13
|
+
* The unmoved root is the common case, and it is free: its `matrixWorld` is the identity, so the
|
|
14
|
+
* multiply and the inverse are skipped and the numbers are exactly the ones they always were, with
|
|
15
|
+
* no scratch vector or matrix allocated either way.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The root's inverse world matrix, reused per read. */
|
|
19
|
+
const _inverse = new Matrix4();
|
|
20
|
+
|
|
21
|
+
/** Whether `matrix` is exactly the identity, so a local-frame read is the read it always was. */
|
|
22
|
+
function isIdentity(matrix: Matrix4): boolean {
|
|
23
|
+
const elements = matrix.elements;
|
|
24
|
+
for (let at = 0; at < 16; at += 1) if (elements[at] !== (at % 5 === 0 ? 1 : 0)) return false;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** `projection × view × root` into `out`: clip space, from a point in the root's local frame. */
|
|
29
|
+
export function localClip(out: Matrix4, camera: Camera, root: Matrix4 | undefined): Matrix4 {
|
|
30
|
+
out.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
|
|
31
|
+
if (root === undefined || isIdentity(root)) return out;
|
|
32
|
+
return out.multiply(root);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `point`, in world space, read in the root's local frame — in place, so a frame allocates nothing. */
|
|
36
|
+
export function localPoint(point: Vector3, root: Matrix4 | undefined): Vector3 {
|
|
37
|
+
if (root === undefined || isIdentity(root)) return point;
|
|
38
|
+
return point.applyMatrix4(_inverse.copy(root).invert());
|
|
39
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/** How the shore field reads open water; every default is the Strata coastal world's. */
|
|
2
|
+
export interface IShoreFieldOptions {
|
|
3
|
+
/** Water must be this many metres deep for open-sea exposure to flood through it. Default 2. */
|
|
4
|
+
readonly openDepth?: number;
|
|
5
|
+
/** Metres of shallow water that exposure still carries into, never across dry ground. Default 18. */
|
|
6
|
+
readonly carry?: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Per grid node: metres to the nearest dry node, and 1 where the open sea reaches. */
|
|
10
|
+
export interface IShoreField {
|
|
11
|
+
readonly distance: Float32Array;
|
|
12
|
+
readonly exposed: Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Distance to the shore and open-sea exposure for every node of a square height grid, measured once
|
|
17
|
+
* from the bake. `distance` is a two-pass chamfer distance to the nearest node at or above `level`
|
|
18
|
+
* (0 on dry ground). `exposed` floods from the grid's edge through water deeper than `openDepth`,
|
|
19
|
+
* then `carry` metres further into shallow water, so a sheltered lagoon behind a sand bar stays out
|
|
20
|
+
* of it. Both feed the game's surf and shore masks; neither decides a colour.
|
|
21
|
+
*
|
|
22
|
+
* @situation surf breaks only on water open to the sea, not in a sheltered lagoon
|
|
23
|
+
* @situation distance from each water cell to the shore, for a foam or depth mask
|
|
24
|
+
* @constraint heights are row-major, `resolution` by `resolution`, across `size` metres; measured once, never per frame
|
|
25
|
+
* @example const { distance, exposed } = shoreField(world.heights, world.resolution, world.size, world.waterLevel);
|
|
26
|
+
*/
|
|
27
|
+
export function shoreField(
|
|
28
|
+
heights: ArrayLike<number>,
|
|
29
|
+
resolution: number,
|
|
30
|
+
size: number,
|
|
31
|
+
level: number,
|
|
32
|
+
options: IShoreFieldOptions = {},
|
|
33
|
+
): IShoreField {
|
|
34
|
+
const n = resolution;
|
|
35
|
+
const openDepth = options.openDepth ?? 2;
|
|
36
|
+
const carry = options.carry ?? 18;
|
|
37
|
+
if (!Number.isInteger(n) || n < 2 || heights.length !== n * n)
|
|
38
|
+
throw new Error(
|
|
39
|
+
`TN_SHORE_FIELD_INVALID: heights must hold resolution² values, received ${heights.length} for ${n}.`,
|
|
40
|
+
);
|
|
41
|
+
if (!(size > 0) || !Number.isFinite(level) || !(openDepth >= 0) || !(carry >= 0))
|
|
42
|
+
throw new Error(
|
|
43
|
+
"TN_SHORE_FIELD_INVALID: size must be positive, level finite, openDepth and carry not negative.",
|
|
44
|
+
);
|
|
45
|
+
const cell = size / (n - 1);
|
|
46
|
+
const height = (i: number): number => heights[i] ?? 0;
|
|
47
|
+
const distance = new Float32Array(n * n);
|
|
48
|
+
for (let i = 0; i < n * n; i++) distance[i] = height(i) >= level ? 0 : 1e4;
|
|
49
|
+
const exposed = new Uint8Array(n * n);
|
|
50
|
+
const queue: number[] = [];
|
|
51
|
+
for (let i = 0; i < n * n; i++) {
|
|
52
|
+
const row = Math.floor(i / n);
|
|
53
|
+
const col = i % n;
|
|
54
|
+
if (
|
|
55
|
+
(row === 0 || col === 0 || row === n - 1 || col === n - 1) &&
|
|
56
|
+
height(i) < level - openDepth
|
|
57
|
+
) {
|
|
58
|
+
exposed[i] = 1;
|
|
59
|
+
queue.push(i);
|
|
60
|
+
}
|
|
61
|
+
if (col > 0) distance[i] = Math.min(distance[i] ?? 0, (distance[i - 1] ?? 0) + cell);
|
|
62
|
+
if (row > 0) distance[i] = Math.min(distance[i] ?? 0, (distance[i - n] ?? 0) + cell);
|
|
63
|
+
}
|
|
64
|
+
for (let i = n * n - 1; i >= 0; i--) {
|
|
65
|
+
if (i % n < n - 1) distance[i] = Math.min(distance[i] ?? 0, (distance[i + 1] ?? 0) + cell);
|
|
66
|
+
if (i < n * (n - 1)) distance[i] = Math.min(distance[i] ?? 0, (distance[i + n] ?? 0) + cell);
|
|
67
|
+
}
|
|
68
|
+
for (let head = 0; head < queue.length; head++) {
|
|
69
|
+
const i = queue[head] ?? 0;
|
|
70
|
+
for (const next of [i % n > 0 ? i - 1 : -1, i % n < n - 1 ? i + 1 : -1, i - n, i + n]) {
|
|
71
|
+
if (next < 0 || next >= exposed.length || exposed[next] || height(next) >= level - openDepth)
|
|
72
|
+
continue;
|
|
73
|
+
exposed[next] = 1;
|
|
74
|
+
queue.push(next);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (let pass = 0; pass < Math.ceil(carry / cell); pass++) {
|
|
78
|
+
const previous = exposed.slice();
|
|
79
|
+
for (let i = 0; i < exposed.length; i++) {
|
|
80
|
+
if (height(i) >= level) continue;
|
|
81
|
+
if (
|
|
82
|
+
(i % n > 0 && previous[i - 1]) ||
|
|
83
|
+
(i % n < n - 1 && previous[i + 1]) ||
|
|
84
|
+
previous[i - n] ||
|
|
85
|
+
previous[i + n]
|
|
86
|
+
)
|
|
87
|
+
exposed[i] = 1;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { distance, exposed };
|
|
91
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { type BufferGeometry, type Matrix4, Vector3 } from "three";
|
|
2
|
+
|
|
3
|
+
/** Ground height under a world point, or `null` where no ground answers. */
|
|
4
|
+
export type StaticGroundQuery = (x: number, z: number) => number | null;
|
|
5
|
+
|
|
6
|
+
const bottomFirst = new WeakMap<BufferGeometry, { xyz: Float32Array; reach: number }>();
|
|
7
|
+
|
|
8
|
+
function verticesBottomFirst(geometry: BufferGeometry): { xyz: Float32Array; reach: number } {
|
|
9
|
+
const cached = bottomFirst.get(geometry);
|
|
10
|
+
if (cached) return cached;
|
|
11
|
+
const position = geometry.getAttribute("position");
|
|
12
|
+
const order = Array.from({ length: position.count }, (_, i) => i).sort(
|
|
13
|
+
(a, b) => position.getY(a) - position.getY(b),
|
|
14
|
+
);
|
|
15
|
+
const xyz = new Float32Array(position.count * 3);
|
|
16
|
+
let reach = 0;
|
|
17
|
+
order.forEach((i, k) => {
|
|
18
|
+
xyz[k * 3] = position.getX(i);
|
|
19
|
+
xyz[k * 3 + 1] = position.getY(i);
|
|
20
|
+
xyz[k * 3 + 2] = position.getZ(i);
|
|
21
|
+
reach = Math.max(reach, Math.hypot(position.getX(i), position.getZ(i)));
|
|
22
|
+
});
|
|
23
|
+
const entry = { xyz, reach };
|
|
24
|
+
bottomFirst.set(geometry, entry);
|
|
25
|
+
return entry;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Exact lowest world Y of a static geometry under `matrix`, the precise measurement `GroundSnap`
|
|
30
|
+
* audits against, cheap enough for a load that poses hundreds of thousands of props. An upright
|
|
31
|
+
* pose reads the bounding box. A tilted pose scans the vertices bottom first (sorted once per
|
|
32
|
+
* geometry) and stops once no later vertex can be lower: `|x e1 + z e9| <= reach * hypot(e1, e9)`.
|
|
33
|
+
*
|
|
34
|
+
* @situation stand a scattered rock, tree or crate on the ground by its lowest point, not its origin
|
|
35
|
+
* @situation props hover or sink because their pivot is not at their base
|
|
36
|
+
* @constraint static geometry only; a skinned model uses `GroundSnap`; the sort is cached per geometry, so do not mutate its positions
|
|
37
|
+
* @example placed.position.y += ground - staticLowestY(geometry, matrix);
|
|
38
|
+
*/
|
|
39
|
+
export function staticLowestY(geometry: BufferGeometry, matrix: Matrix4): number {
|
|
40
|
+
const e = matrix.elements;
|
|
41
|
+
if (e[1] === 0 && e[9] === 0) {
|
|
42
|
+
if (geometry.boundingBox === null) geometry.computeBoundingBox();
|
|
43
|
+
const box = geometry.boundingBox;
|
|
44
|
+
if (box) return (e[5] < 0 ? box.max.y : box.min.y) * e[5] + e[13];
|
|
45
|
+
}
|
|
46
|
+
const { xyz, reach } = verticesBottomFirst(geometry);
|
|
47
|
+
const tilt = reach * Math.hypot(e[1], e[9]);
|
|
48
|
+
const upright = e[5] > 0;
|
|
49
|
+
let lowest = Number.POSITIVE_INFINITY;
|
|
50
|
+
for (let k = 0; k < xyz.length; k += 3) {
|
|
51
|
+
const y = xyz[k + 1] as number;
|
|
52
|
+
if (upright && y * e[5] + e[13] - tilt >= lowest) break;
|
|
53
|
+
lowest = Math.min(
|
|
54
|
+
lowest,
|
|
55
|
+
(xyz[k] as number) * e[1] + y * e[5] + (xyz[k + 2] as number) * e[9] + e[13],
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return lowest;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* How far the flat underside of a static geometry rises above the ground at its highest point.
|
|
63
|
+
* Snapping the lowest point leaves the downhill side of a flat-bottomed rock hanging; sinking the
|
|
64
|
+
* pose by this much buries the whole footprint. Returns 0 when the footprint is already buried.
|
|
65
|
+
*
|
|
66
|
+
* @situation a rock snapped to a slope shows a lip or a gap under its downhill side
|
|
67
|
+
* @constraint the footprint is every vertex at the geometry's lowest local y; throws `TN_STATIC_POSE_GROUND` where the ground does not answer
|
|
68
|
+
* @example matrix.elements[13] -= footprintGap(geometry, matrix, ground, 0.08);
|
|
69
|
+
*/
|
|
70
|
+
export function footprintGap(
|
|
71
|
+
geometry: BufferGeometry,
|
|
72
|
+
matrix: Matrix4,
|
|
73
|
+
ground: StaticGroundQuery,
|
|
74
|
+
margin = 0,
|
|
75
|
+
): number {
|
|
76
|
+
if (geometry.boundingBox === null) geometry.computeBoundingBox();
|
|
77
|
+
const base = geometry.boundingBox?.min.y ?? Number.POSITIVE_INFINITY;
|
|
78
|
+
const positions = geometry.getAttribute("position");
|
|
79
|
+
const point = new Vector3();
|
|
80
|
+
let gap = 0;
|
|
81
|
+
for (let i = 0; i < positions.count; i++) {
|
|
82
|
+
if (Math.abs(positions.getY(i) - base) > 1e-5) continue;
|
|
83
|
+
point.fromBufferAttribute(positions, i).applyMatrix4(matrix);
|
|
84
|
+
const height = ground(point.x, point.z);
|
|
85
|
+
if (height === null)
|
|
86
|
+
throw new Error(
|
|
87
|
+
`TN_STATIC_POSE_GROUND: no ground under the footprint at ${point.x}, ${point.z}.`,
|
|
88
|
+
);
|
|
89
|
+
gap = Math.max(gap, point.y - height + margin);
|
|
90
|
+
}
|
|
91
|
+
return gap;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** What `baseRingGap` measured around a wide static model. */
|
|
95
|
+
export interface IBaseRing {
|
|
96
|
+
/** The largest height of a sector's lowest rim vertex above its ground, in metres. */
|
|
97
|
+
readonly gap: number;
|
|
98
|
+
/** Sectors that held a rim vertex. */
|
|
99
|
+
readonly samples: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The highest gap between a wide model's rim and the ground, one sample per angular sector: in
|
|
104
|
+
* each sector, the lowest vertex beyond `rim` of that sector's reach. A narrow buried stem then
|
|
105
|
+
* cannot hold a wide base up in the air on the downhill side. Sink the pose by `gap` (plus any
|
|
106
|
+
* burial the game wants) to close it.
|
|
107
|
+
*
|
|
108
|
+
* @situation a cliff or crag model rests on a buried stem while its wide base hangs over the downhill ground
|
|
109
|
+
* @constraint throws `TN_STATIC_POSE_GROUND` where the ground does not answer; check `samples` before trusting `gap`
|
|
110
|
+
* @example const ring = baseRingGap(geometry, matrix, ground); if (ring.samples >= 4) matrix.elements[13] -= ring.gap + 0.5;
|
|
111
|
+
*/
|
|
112
|
+
export function baseRingGap(
|
|
113
|
+
geometry: BufferGeometry,
|
|
114
|
+
matrix: Matrix4,
|
|
115
|
+
ground: StaticGroundQuery,
|
|
116
|
+
options: { readonly sectors?: number; readonly rim?: number } = {},
|
|
117
|
+
): IBaseRing {
|
|
118
|
+
const count = options.sectors ?? 16;
|
|
119
|
+
const rim = options.rim ?? 0.75;
|
|
120
|
+
if (!Number.isInteger(count) || count < 1 || !(rim >= 0 && rim <= 1))
|
|
121
|
+
throw new Error(
|
|
122
|
+
"TN_STATIC_POSE_INVALID: sectors must be a positive integer and rim within 0..1.",
|
|
123
|
+
);
|
|
124
|
+
if (geometry.boundingBox === null) geometry.computeBoundingBox();
|
|
125
|
+
const box = geometry.boundingBox;
|
|
126
|
+
if (!box) throw new Error("TN_STATIC_POSE_INVALID: the geometry has no bounds.");
|
|
127
|
+
const centre = box.getCenter(new Vector3()).applyMatrix4(matrix);
|
|
128
|
+
const sectors = Array.from({ length: count }, () => ({ reach: 0, vertices: [] as Vector3[] }));
|
|
129
|
+
const vertices = geometry.getAttribute("position");
|
|
130
|
+
for (let i = 0; i < vertices.count; i++) {
|
|
131
|
+
const point = new Vector3().fromBufferAttribute(vertices, i).applyMatrix4(matrix);
|
|
132
|
+
const dx = point.x - centre.x;
|
|
133
|
+
const dz = point.z - centre.z;
|
|
134
|
+
const bin = Math.min(
|
|
135
|
+
count - 1,
|
|
136
|
+
Math.floor(((Math.atan2(dz, dx) + Math.PI) / (2 * Math.PI)) * count),
|
|
137
|
+
);
|
|
138
|
+
const sector = sectors[bin];
|
|
139
|
+
if (!sector) continue;
|
|
140
|
+
sector.reach = Math.max(sector.reach, Math.hypot(dx, dz));
|
|
141
|
+
sector.vertices.push(point);
|
|
142
|
+
}
|
|
143
|
+
let gap = Number.NEGATIVE_INFINITY;
|
|
144
|
+
let samples = 0;
|
|
145
|
+
for (const sector of sectors) {
|
|
146
|
+
let lowest: Vector3 | undefined;
|
|
147
|
+
for (const point of sector.vertices)
|
|
148
|
+
if (
|
|
149
|
+
Math.hypot(point.x - centre.x, point.z - centre.z) >= sector.reach * rim &&
|
|
150
|
+
(!lowest || point.y < lowest.y)
|
|
151
|
+
)
|
|
152
|
+
lowest = point;
|
|
153
|
+
if (!lowest) continue;
|
|
154
|
+
const contact = ground(lowest.x, lowest.z);
|
|
155
|
+
if (contact === null)
|
|
156
|
+
throw new Error(
|
|
157
|
+
`TN_STATIC_POSE_GROUND: no ground under the base ring at ${lowest.x}, ${lowest.z}.`,
|
|
158
|
+
);
|
|
159
|
+
gap = Math.max(gap, lowest.y - contact);
|
|
160
|
+
samples++;
|
|
161
|
+
}
|
|
162
|
+
return { gap, samples };
|
|
163
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type Matrix4, Quaternion, Vector3 } from "three";
|
|
2
|
+
import type { Heightfield } from "./world-heightfield.js";
|
|
3
|
+
|
|
4
|
+
/** The drawn surface's height under a world point, or `null` where the sampler cannot answer. */
|
|
5
|
+
export type SurfaceSampler = (x: number, z: number) => number | null;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `Heightfield.surfaceAt` under the transform the game draws the field's mesh with, so a prop or a
|
|
9
|
+
* player stands on the triangles the eye sees rather than on the bilinear field between them.
|
|
10
|
+
* Answers `null` outside the field and for a transform it does not model (any rotation, or an
|
|
11
|
+
* unequal scale), so the caller keeps its own fallback there.
|
|
12
|
+
*
|
|
13
|
+
* @situation stand props or a player on the drawn terrain triangles under a moved or scaled mesh
|
|
14
|
+
* @situation props float or sink where the terrain mesh is drawn under a transform
|
|
15
|
+
* @constraint the mesh is `field.toGeometry()` drawn upright under a uniform scale; read `matrixWorld` after it settles
|
|
16
|
+
* @example const ground = surfaceSampler(field, mesh.matrixWorld); const y = ground(x, z) ?? fallback;
|
|
17
|
+
*/
|
|
18
|
+
export function surfaceSampler(field: Heightfield, matrixWorld: Matrix4): SurfaceSampler {
|
|
19
|
+
const translation = new Vector3();
|
|
20
|
+
const rotation = new Quaternion();
|
|
21
|
+
const scale = new Vector3();
|
|
22
|
+
matrixWorld.decompose(translation, rotation, scale);
|
|
23
|
+
const upright =
|
|
24
|
+
Math.abs(rotation.x) < 1e-6 &&
|
|
25
|
+
Math.abs(rotation.y) < 1e-6 &&
|
|
26
|
+
Math.abs(rotation.z) < 1e-6 &&
|
|
27
|
+
Math.abs(rotation.w - 1) < 1e-6 &&
|
|
28
|
+
scale.x > 0 &&
|
|
29
|
+
scale.x === scale.y &&
|
|
30
|
+
scale.y === scale.z;
|
|
31
|
+
if (!upright) return () => null;
|
|
32
|
+
const { origin, width, depth } = field;
|
|
33
|
+
// A drawn vertex is float32 and the field's extent float64: the mesh's own edge can land a hair
|
|
34
|
+
// outside the ground it was built from. That much is the same ground; further out is not.
|
|
35
|
+
const slack = 1e-5;
|
|
36
|
+
const minX = origin.x - width / 2;
|
|
37
|
+
const maxX = origin.x + width / 2;
|
|
38
|
+
const minZ = origin.z - depth / 2;
|
|
39
|
+
const maxZ = origin.z + depth / 2;
|
|
40
|
+
return (worldX, worldZ) => {
|
|
41
|
+
const x = (worldX - translation.x) / scale.x;
|
|
42
|
+
const z = (worldZ - translation.z) / scale.z;
|
|
43
|
+
if (x < minX - slack || x > maxX + slack || z < minZ - slack || z > maxZ + slack) return null;
|
|
44
|
+
const clampedX = Math.min(maxX, Math.max(minX, x));
|
|
45
|
+
const clampedZ = Math.min(maxZ, Math.max(minZ, z));
|
|
46
|
+
return field.surfaceAt(clampedX, clampedZ) * scale.y + translation.y;
|
|
47
|
+
};
|
|
48
|
+
}
|