@threenative/core 0.3.3 → 0.3.5
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 +2019 -59
- package/dist/{assets-CYKk2WTu.d.ts → assets-CqvE429w.d.ts} +45 -3
- package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
- package/dist/{canvas-layer-C1SnMoJ-.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
- package/dist/{game-D_6r-k4Y.d.ts → game-Bj8G1wmD.d.ts} +242 -17
- package/dist/hot.d.ts +5 -5
- package/dist/index.d.ts +1131 -105
- package/dist/index.js +15201 -4782
- package/dist/model-lod-BDAUYgsS.d.ts +197 -0
- package/dist/playtest.d.ts +6 -4
- package/dist/playtest.js +84 -11
- package/dist/react-css.d.ts +62 -0
- package/dist/react-css.js +591 -0
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-Cy4qeBOA.d.ts → renderer-9yEo9hPa.d.ts} +676 -21
- package/dist/terrain-jobs-worker.d.ts +2 -0
- package/dist/terrain-jobs-worker.js +130 -0
- package/dist/ui-layer.d.ts +5 -3
- package/dist/ui-layer.js +59 -8
- package/dist/world.d.ts +1596 -17
- package/dist/world.js +17035 -521
- package/gpl/fixtures/make_world_fixture.py +184 -0
- package/gpl/recipes/_common.py +11 -0
- package/gpl/recipes/decimate.py +11 -5
- package/gpl/recipes/export_world.py +699 -0
- package/mcp/blender-server.mjs +61 -4
- package/mcp/blender.mjs +3 -4
- package/mcp/engine-server.mjs +55 -21
- package/mcp/servers.mjs +4 -4
- package/package.json +15 -9
- package/patches/three@0.185.1-prd269-upgrade.patch +22418 -0
- package/patches/three@0.185.1.patch +8246 -389
- package/scripts/apply-three-patch.mjs +123 -41
- package/src/animation-blend.ts +264 -0
- package/src/animation-composition.ts +362 -0
- package/src/animation-layers.ts +260 -0
- package/src/animation-root-motion.ts +283 -0
- package/src/animation.ts +896 -0
- package/src/assets.ts +1303 -0
- package/src/atmosphere/index.ts +335 -0
- package/src/atmosphere/luts.ts +298 -0
- package/src/atmosphere/params.ts +323 -0
- package/src/audio.ts +912 -0
- package/src/billboard.ts +124 -0
- package/src/bone-lengths.ts +157 -0
- package/src/camera-shake.ts +116 -0
- package/src/canvas-layer.ts +47 -0
- package/src/clip-audit.ts +401 -0
- package/src/clustered-batch.ts +492 -0
- package/src/clustered-mesh.ts +638 -0
- package/src/compute-driven.ts +176 -0
- package/src/compute-timing.ts +318 -0
- package/src/config.ts +593 -0
- package/src/debug.ts +82 -0
- package/src/engine-render-hook.ts +25 -0
- package/src/entities.ts +79 -0
- package/src/entity-snapshot.ts +53 -0
- package/src/flight.ts +683 -0
- package/src/fluid-field.ts +748 -0
- package/src/fluid-particles.ts +1253 -0
- package/src/frame-budget.ts +1368 -0
- package/src/game.ts +2390 -0
- package/src/geometry-capture.ts +1098 -0
- package/src/gpu-class.ts +160 -0
- package/src/gpu-frame-observation.ts +424 -0
- package/src/gpu-readback.ts +143 -0
- package/src/gpu-scene-bvh.ts +464 -0
- package/src/grounding.ts +100 -0
- package/src/hot.ts +161 -0
- package/src/import-meta.d.ts +13 -0
- package/src/index.ts +1536 -0
- package/src/input.ts +690 -0
- package/src/instanced-batch-lod.ts +283 -0
- package/src/instanced-batch.ts +274 -0
- package/src/interpolation.ts +129 -0
- package/src/launch-diagnostics.ts +161 -0
- package/src/loop.ts +564 -0
- package/src/matrix-world.ts +245 -0
- package/src/merge-parts.ts +812 -0
- package/src/model-lod.ts +1315 -0
- package/src/net-protocol.ts +333 -0
- package/src/net-session.ts +477 -0
- package/src/net.ts +80 -0
- package/src/ocean/fft.ts +72 -0
- package/src/ocean/spectral.ts +728 -0
- package/src/particle-significance.ts +102 -0
- package/src/particles.ts +648 -0
- package/src/path-follow.ts +175 -0
- package/src/picking.ts +250 -0
- package/src/pipeline-census.ts +1566 -0
- package/src/platform.ts +210 -0
- package/src/playtest.ts +839 -0
- package/src/pointer-events.ts +411 -0
- package/src/pose-measure.ts +435 -0
- package/src/profiling/FrameCounters.ts +231 -0
- package/src/profiling/Spans.ts +548 -0
- package/src/profiling/opacity-convention.ts +21 -0
- package/src/profiling/render-list-validate.ts +175 -0
- package/src/profiling/render-trap-audit.ts +84 -0
- package/src/profiling/scene-warning.ts +190 -0
- package/src/profiling/span-probes.ts +191 -0
- package/src/profiling/unlit-foliage-warning.ts +149 -0
- package/src/projection-apply.ts +1529 -0
- package/src/projection-marker.ts +142 -0
- package/src/projection-plan.ts +1258 -0
- package/src/projection-skinned.ts +419 -0
- package/src/projection-stability.ts +409 -0
- package/src/projection-uniform.ts +328 -0
- package/src/random.ts +41 -0
- package/src/react-css.ts +604 -0
- package/src/react-glyphs.ts +114 -0
- package/src/react-host.ts +576 -0
- package/src/react-layout.ts +265 -0
- package/src/react.ts +60 -0
- package/src/render/alpha-antialiasing.ts +159 -0
- package/src/render/batched-velocity.ts +126 -0
- package/src/render/chain.ts +1021 -0
- package/src/render/daylight.ts +189 -0
- package/src/render/depth-pyramid.ts +596 -0
- package/src/render/foliage-alpha.ts +166 -0
- package/src/render/material-key.ts +60 -0
- package/src/render/mesh-pool.ts +144 -0
- package/src/render/probe-volume.ts +1668 -0
- package/src/render/resolution-subrect.ts +253 -0
- package/src/render/velocity.ts +442 -0
- package/src/render/virtual-shadow-pages.ts +827 -0
- package/src/render/virtual-shadow-partial.ts +174 -0
- package/src/render/virtual-shadow.ts +3330 -0
- package/src/render/world-impostor-surface.ts +722 -0
- package/src/render/world-impostor.ts +558 -0
- package/src/render-camera-cull.ts +441 -0
- package/src/render-pass-budget.ts +501 -0
- package/src/renderProjection.ts +758 -0
- package/src/renderer-config.ts +127 -0
- package/src/renderer.ts +1847 -0
- package/src/replay-protocol.ts +85 -0
- package/src/replay.ts +205 -0
- package/src/resolution-scaler.ts +819 -0
- package/src/rig-preparation.ts +271 -0
- package/src/ripple-field.ts +432 -0
- package/src/scale.ts +109 -0
- package/src/scene.ts +239 -0
- package/src/schedule.ts +155 -0
- package/src/skeletal-mesh.ts +61 -0
- package/src/skeleton.ts +100 -0
- package/src/snow-field.ts +744 -0
- package/src/softbody-topology.ts +269 -0
- package/src/softbody.ts +516 -0
- package/src/sprite-animator.ts +222 -0
- package/src/startup-readiness.ts +386 -0
- package/src/state.ts +66 -0
- package/src/static-transform.ts +274 -0
- package/src/storage-buffer.ts +253 -0
- package/src/streaming.ts +308 -0
- package/src/target-fps.ts +92 -0
- package/src/terrain-jobs-worker.ts +17 -0
- package/src/terrain-jobs.ts +353 -0
- package/src/textures.ts +39 -0
- package/src/tracers.ts +237 -0
- package/src/ui-bridge.ts +368 -0
- package/src/ui-hit-regions.ts +380 -0
- package/src/ui-layer.ts +83 -0
- package/src/ui-state.ts +220 -0
- package/src/version.ts +2 -0
- package/src/viewport.ts +253 -0
- package/src/warmup.ts +1357 -0
- package/src/water-surface.ts +384 -0
- package/src/wave-field.ts +524 -0
- package/src/world-capabilities.ts +131 -0
- package/src/world-cell-proxy.ts +392 -0
- package/src/world-cells.ts +10564 -0
- package/src/world-gpu-scene.ts +3354 -0
- package/src/world-heightmap.ts +82 -0
- package/src/world-package.ts +469 -0
- package/src/world-passes.ts +582 -0
- package/src/world-region.ts +17 -0
- package/src/world-terrain-splat.ts +564 -0
- package/src/world-tiles.ts +3826 -0
- package/src/world-topology.ts +370 -0
- package/src/world-validate.ts +48 -0
- package/src/world.ts +805 -0
- package/dist/gpu-readback-CMklJs6r.d.ts +0 -112
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import type { Node } from "three/src/nodes/Nodes.js";
|
|
2
|
+
import type StorageBufferNode from "three/src/nodes/accessors/StorageBufferNode.js";
|
|
3
|
+
import {
|
|
4
|
+
Fn,
|
|
5
|
+
If,
|
|
6
|
+
Return,
|
|
7
|
+
atomicAdd,
|
|
8
|
+
atomicLoad,
|
|
9
|
+
clamp,
|
|
10
|
+
float,
|
|
11
|
+
instanceIndex,
|
|
12
|
+
instancedArray,
|
|
13
|
+
log,
|
|
14
|
+
max,
|
|
15
|
+
min,
|
|
16
|
+
uint,
|
|
17
|
+
vec4,
|
|
18
|
+
} from "three/tsl";
|
|
19
|
+
import type { ComputeNode } from "three/webgpu";
|
|
20
|
+
import type { IRendererLike } from "./renderer.js";
|
|
21
|
+
|
|
22
|
+
export interface IWorldErosionOptions {
|
|
23
|
+
readonly depositionRate: number;
|
|
24
|
+
readonly erosionRate: number;
|
|
25
|
+
readonly evaporation: number;
|
|
26
|
+
readonly iterations: number;
|
|
27
|
+
readonly rainfall: number;
|
|
28
|
+
readonly sedimentCapacity: number;
|
|
29
|
+
readonly timeStep: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface IWorldPassCpuOptions {
|
|
33
|
+
readonly cellDepth: number;
|
|
34
|
+
readonly cellWidth: number;
|
|
35
|
+
readonly columns: number;
|
|
36
|
+
readonly erosion: IWorldErosionOptions;
|
|
37
|
+
readonly heights: Float32Array;
|
|
38
|
+
readonly rows: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface IWorldPassCpuResult {
|
|
42
|
+
readonly flow: Float32Array;
|
|
43
|
+
readonly heights: Float32Array;
|
|
44
|
+
readonly moisture: Float32Array;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface IWorldGpuPassOptions extends IWorldPassCpuOptions {
|
|
48
|
+
readonly dispatchBudget: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface IWorldGpuPasses {
|
|
52
|
+
readonly flow: StorageBufferNode<"float">;
|
|
53
|
+
readonly height: StorageBufferNode<"float">;
|
|
54
|
+
readonly moisture: StorageBufferNode<"float">;
|
|
55
|
+
readonly queue: BoundedWorldPassQueue;
|
|
56
|
+
readonly stages: readonly IWorldPassStage[];
|
|
57
|
+
dispose(): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function finite(name: string, value: number, minimum = 0): number {
|
|
61
|
+
if (!Number.isFinite(value) || value < minimum)
|
|
62
|
+
throw new Error(`World passes ${name} must be finite and at least ${minimum}.`);
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function gridCount(name: string, value: number): number {
|
|
67
|
+
if (!Number.isInteger(value) || value < 2)
|
|
68
|
+
throw new Error(`World passes ${name} must be an integer of at least 2.`);
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const CARDINAL = [
|
|
73
|
+
[-1, 0],
|
|
74
|
+
[1, 0],
|
|
75
|
+
[0, -1],
|
|
76
|
+
[0, 1],
|
|
77
|
+
] as const;
|
|
78
|
+
|
|
79
|
+
function neighbourIndex(
|
|
80
|
+
index: number,
|
|
81
|
+
direction: number,
|
|
82
|
+
rows: number,
|
|
83
|
+
columns: number,
|
|
84
|
+
): number | undefined {
|
|
85
|
+
const pair = CARDINAL[direction];
|
|
86
|
+
if (pair === undefined) return undefined;
|
|
87
|
+
const column = (index % columns) + pair[0];
|
|
88
|
+
const row = Math.floor(index / columns) + pair[1];
|
|
89
|
+
if (column < 0 || column >= columns || row < 0 || row >= rows) return undefined;
|
|
90
|
+
return row * columns + column;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Deterministic scalar reference for the TSL hydraulic and flow passes. */
|
|
94
|
+
export function simulateWorldPassesCpu(options: IWorldPassCpuOptions): IWorldPassCpuResult {
|
|
95
|
+
const rows = gridCount("rows", options.rows);
|
|
96
|
+
const columns = gridCount("columns", options.columns);
|
|
97
|
+
const count = rows * columns;
|
|
98
|
+
if (options.heights.length !== count)
|
|
99
|
+
throw new Error(`World passes expected ${count} heights, received ${options.heights.length}.`);
|
|
100
|
+
const cellWidth = finite("cellWidth", options.cellWidth, Number.EPSILON);
|
|
101
|
+
const cellDepth = finite("cellDepth", options.cellDepth, Number.EPSILON);
|
|
102
|
+
const tuning = options.erosion;
|
|
103
|
+
if (!Number.isInteger(tuning.iterations) || tuning.iterations < 0)
|
|
104
|
+
throw new Error("World passes iterations must be a non-negative integer.");
|
|
105
|
+
const rainfall = finite("rainfall", tuning.rainfall);
|
|
106
|
+
const evaporation = finite("evaporation", tuning.evaporation);
|
|
107
|
+
if (evaporation > 1) throw new Error("World passes evaporation must not exceed 1.");
|
|
108
|
+
const timeStep = finite("timeStep", tuning.timeStep, Number.EPSILON);
|
|
109
|
+
const sedimentCapacity = finite("sedimentCapacity", tuning.sedimentCapacity);
|
|
110
|
+
const erosionRate = finite("erosionRate", tuning.erosionRate);
|
|
111
|
+
const depositionRate = finite("depositionRate", tuning.depositionRate);
|
|
112
|
+
let heights = options.heights.slice();
|
|
113
|
+
for (const height of heights) finite("height sample", height, Number.NEGATIVE_INFINITY);
|
|
114
|
+
let water = new Float32Array(count);
|
|
115
|
+
let sediment = new Float32Array(count);
|
|
116
|
+
const outflow = new Float32Array(count * CARDINAL.length);
|
|
117
|
+
|
|
118
|
+
for (let iteration = 0; iteration < tuning.iterations; iteration += 1) {
|
|
119
|
+
outflow.fill(0);
|
|
120
|
+
for (let index = 0; index < count; index += 1) {
|
|
121
|
+
water[index] = (water[index] as number) + rainfall * timeStep;
|
|
122
|
+
const head = (heights[index] as number) + (water[index] as number);
|
|
123
|
+
let totalDrop = 0;
|
|
124
|
+
const drops = [0, 0, 0, 0];
|
|
125
|
+
for (let direction = 0; direction < CARDINAL.length; direction += 1) {
|
|
126
|
+
const neighbour = neighbourIndex(index, direction, rows, columns);
|
|
127
|
+
if (neighbour === undefined) continue;
|
|
128
|
+
const drop = Math.max(
|
|
129
|
+
0,
|
|
130
|
+
head - ((heights[neighbour] as number) + (water[neighbour] as number)),
|
|
131
|
+
);
|
|
132
|
+
drops[direction] = drop;
|
|
133
|
+
totalDrop += drop;
|
|
134
|
+
}
|
|
135
|
+
if (totalDrop === 0) continue;
|
|
136
|
+
const available = Math.min(water[index] as number, totalDrop * timeStep);
|
|
137
|
+
for (let direction = 0; direction < CARDINAL.length; direction += 1)
|
|
138
|
+
outflow[index * CARDINAL.length + direction] =
|
|
139
|
+
available * ((drops[direction] as number) / totalDrop);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const nextWater = new Float32Array(count);
|
|
143
|
+
const nextSediment = new Float32Array(count);
|
|
144
|
+
const nextHeights = heights.slice();
|
|
145
|
+
for (let index = 0; index < count; index += 1) {
|
|
146
|
+
let incomingWater = 0;
|
|
147
|
+
let outgoingWater = 0;
|
|
148
|
+
for (let direction = 0; direction < CARDINAL.length; direction += 1) {
|
|
149
|
+
outgoingWater += outflow[index * CARDINAL.length + direction] as number;
|
|
150
|
+
const neighbour = neighbourIndex(index, direction, rows, columns);
|
|
151
|
+
if (neighbour === undefined) continue;
|
|
152
|
+
incomingWater += outflow[neighbour * CARDINAL.length + (direction ^ 1)] as number;
|
|
153
|
+
}
|
|
154
|
+
const remainingWater = Math.max(
|
|
155
|
+
0,
|
|
156
|
+
((water[index] as number) + incomingWater - outgoingWater) * (1 - evaporation * timeStep),
|
|
157
|
+
);
|
|
158
|
+
nextWater[index] = remainingWater;
|
|
159
|
+
const speed = outgoingWater / (timeStep * Math.min(cellWidth, cellDepth));
|
|
160
|
+
const capacity = sedimentCapacity * speed * outgoingWater;
|
|
161
|
+
let localSediment = sediment[index] as number;
|
|
162
|
+
if (localSediment < capacity) {
|
|
163
|
+
const eroded = Math.min(
|
|
164
|
+
capacity - localSediment,
|
|
165
|
+
(capacity - localSediment) * erosionRate * timeStep,
|
|
166
|
+
);
|
|
167
|
+
nextHeights[index] = (nextHeights[index] as number) - eroded;
|
|
168
|
+
localSediment += eroded;
|
|
169
|
+
} else {
|
|
170
|
+
const deposited = (localSediment - capacity) * depositionRate * timeStep;
|
|
171
|
+
nextHeights[index] = (nextHeights[index] as number) + deposited;
|
|
172
|
+
localSediment -= deposited;
|
|
173
|
+
}
|
|
174
|
+
const transported = Math.min(localSediment, localSediment * outgoingWater);
|
|
175
|
+
nextSediment[index] = (nextSediment[index] as number) + localSediment - transported;
|
|
176
|
+
if (outgoingWater === 0) continue;
|
|
177
|
+
for (let direction = 0; direction < CARDINAL.length; direction += 1) {
|
|
178
|
+
const neighbour = neighbourIndex(index, direction, rows, columns);
|
|
179
|
+
const amount = outflow[index * CARDINAL.length + direction] as number;
|
|
180
|
+
if (neighbour !== undefined)
|
|
181
|
+
nextSediment[neighbour] =
|
|
182
|
+
(nextSediment[neighbour] as number) + transported * (amount / outgoingWater);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
heights = nextHeights;
|
|
186
|
+
water = nextWater;
|
|
187
|
+
sediment = nextSediment;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const flow = accumulateFlow(heights, rows, columns);
|
|
191
|
+
const moisture = new Float32Array(count);
|
|
192
|
+
for (let index = 0; index < count; index += 1)
|
|
193
|
+
moisture[index] = Math.min(
|
|
194
|
+
1,
|
|
195
|
+
(flow[index] as number) * 0.7 + Math.min(1, Math.max(0, water[index] as number)) * 0.3,
|
|
196
|
+
);
|
|
197
|
+
return { flow, heights, moisture };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function accumulateFlow(heights: Float32Array, rows: number, columns: number): Float32Array {
|
|
201
|
+
// Strictly downhill routing is acyclic. Accumulate each source once in descending-height order
|
|
202
|
+
// instead of walking every source all the way to its sink; the latter turns a 1,025x1,025
|
|
203
|
+
// quality observation into billions of repeated neighbour visits.
|
|
204
|
+
const targets = new Int32Array(heights.length);
|
|
205
|
+
targets.fill(-1);
|
|
206
|
+
const order = Array.from({ length: heights.length }, (_, index) => index).sort(
|
|
207
|
+
(a, b) => (heights[b] as number) - (heights[a] as number) || a - b,
|
|
208
|
+
);
|
|
209
|
+
for (let source = 0; source < heights.length; source += 1) {
|
|
210
|
+
let destination = source;
|
|
211
|
+
let lowest = heights[source] as number;
|
|
212
|
+
for (let direction = 0; direction < CARDINAL.length; direction += 1) {
|
|
213
|
+
const neighbour = neighbourIndex(source, direction, rows, columns);
|
|
214
|
+
if (neighbour === undefined) continue;
|
|
215
|
+
const candidate = heights[neighbour] as number;
|
|
216
|
+
if (candidate < lowest) {
|
|
217
|
+
lowest = candidate;
|
|
218
|
+
destination = neighbour;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (destination !== source) targets[source] = destination;
|
|
222
|
+
}
|
|
223
|
+
const accumulated = new Float64Array(heights.length);
|
|
224
|
+
accumulated.fill(1);
|
|
225
|
+
for (const source of order) {
|
|
226
|
+
const destination = targets[source] as number;
|
|
227
|
+
if (destination >= 0)
|
|
228
|
+
accumulated[destination] =
|
|
229
|
+
(accumulated[destination] as number) + (accumulated[source] as number);
|
|
230
|
+
}
|
|
231
|
+
const scale = Math.log1p(heights.length);
|
|
232
|
+
return Float32Array.from(accumulated, (value) => Math.log1p(value) / scale);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
type FloatStorage = StorageBufferNode<"float">;
|
|
236
|
+
|
|
237
|
+
interface IErosionRoles {
|
|
238
|
+
readonly heightSource: FloatStorage;
|
|
239
|
+
readonly heightTarget: FloatStorage;
|
|
240
|
+
readonly sedimentSource: FloatStorage;
|
|
241
|
+
readonly sedimentTarget: FloatStorage;
|
|
242
|
+
readonly waterSource: FloatStorage;
|
|
243
|
+
readonly waterTarget: FloatStorage;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function computeNode(name: string, count: number, body: () => Node): ComputeNode {
|
|
247
|
+
const node = Fn(body)().compute(count);
|
|
248
|
+
node.setName(name);
|
|
249
|
+
return node;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Build the TSL counterpart of the scalar reference without dispatching it. */
|
|
253
|
+
export function createWorldGpuPasses(options: IWorldGpuPassOptions): IWorldGpuPasses {
|
|
254
|
+
const rows = gridCount("rows", options.rows);
|
|
255
|
+
const columns = gridCount("columns", options.columns);
|
|
256
|
+
const count = rows * columns;
|
|
257
|
+
if (options.heights.length !== count)
|
|
258
|
+
throw new Error(`World passes expected ${count} heights, received ${options.heights.length}.`);
|
|
259
|
+
finite("cellWidth", options.cellWidth, Number.EPSILON);
|
|
260
|
+
finite("cellDepth", options.cellDepth, Number.EPSILON);
|
|
261
|
+
simulateWorldPassesCpu({ ...options, erosion: { ...options.erosion, iterations: 0 } });
|
|
262
|
+
const initial = instancedArray(options.heights, "float").toReadOnly();
|
|
263
|
+
const heightA = instancedArray(count, "float");
|
|
264
|
+
const heightB = instancedArray(count, "float");
|
|
265
|
+
const waterA = instancedArray(count, "float");
|
|
266
|
+
const waterB = instancedArray(count, "float");
|
|
267
|
+
const sedimentA = instancedArray(count, "float");
|
|
268
|
+
const sedimentB = instancedArray(count, "float");
|
|
269
|
+
const sedimentTemporary = instancedArray(count, "float");
|
|
270
|
+
const outflow = instancedArray(count, "vec4");
|
|
271
|
+
const flowAtomic = instancedArray(count, "uint");
|
|
272
|
+
const flowFrontierA = instancedArray(count, "uint");
|
|
273
|
+
const flowFrontierB = instancedArray(count, "uint");
|
|
274
|
+
const flow = instancedArray(count, "float");
|
|
275
|
+
const moisture = instancedArray(count, "float");
|
|
276
|
+
const allStorage = [
|
|
277
|
+
initial,
|
|
278
|
+
heightA,
|
|
279
|
+
heightB,
|
|
280
|
+
waterA,
|
|
281
|
+
waterB,
|
|
282
|
+
sedimentA,
|
|
283
|
+
sedimentB,
|
|
284
|
+
sedimentTemporary,
|
|
285
|
+
outflow,
|
|
286
|
+
flowAtomic,
|
|
287
|
+
flowFrontierA,
|
|
288
|
+
flowFrontierB,
|
|
289
|
+
flow,
|
|
290
|
+
moisture,
|
|
291
|
+
];
|
|
292
|
+
const guard = (body: () => void): Node =>
|
|
293
|
+
Fn<void>(() => {
|
|
294
|
+
If(instanceIndex.greaterThanEqual(count), () => Return());
|
|
295
|
+
body();
|
|
296
|
+
})();
|
|
297
|
+
const at = (index: Node<"int">, x: number, z: number) => {
|
|
298
|
+
const column = clamp(float(index.mod(columns)).add(x), 0, columns - 1).toInt();
|
|
299
|
+
const row = clamp(float(index.div(columns)).add(z), 0, rows - 1).toInt();
|
|
300
|
+
return row.mul(columns).add(column);
|
|
301
|
+
};
|
|
302
|
+
const synthesis = computeNode("world.synthesis", count, () =>
|
|
303
|
+
guard(() => {
|
|
304
|
+
const index = instanceIndex.toInt();
|
|
305
|
+
heightA.element(index).assign(initial.element(index));
|
|
306
|
+
waterA.element(index).assign(0);
|
|
307
|
+
sedimentA.element(index).assign(0);
|
|
308
|
+
flowAtomic.element(index).assign(uint(1));
|
|
309
|
+
flowFrontierA.element(index).assign(uint(1));
|
|
310
|
+
flowFrontierB.element(index).assign(uint(0));
|
|
311
|
+
}),
|
|
312
|
+
);
|
|
313
|
+
const erosionNodes: ComputeNode[] = [];
|
|
314
|
+
const tuning = options.erosion;
|
|
315
|
+
const rain = tuning.rainfall * tuning.timeStep;
|
|
316
|
+
const makeIteration = (roles: IErosionRoles, iteration: number): void => {
|
|
317
|
+
const flux = computeNode(`world.erosion.${iteration}.flux`, count, () =>
|
|
318
|
+
guard(() => {
|
|
319
|
+
const index = instanceIndex.toInt();
|
|
320
|
+
const water = roles.waterSource.element(index).add(rain);
|
|
321
|
+
const head = roles.heightSource.element(index).add(water).toVar();
|
|
322
|
+
const drop = (x: number, z: number) => {
|
|
323
|
+
const neighbour = at(index, x, z);
|
|
324
|
+
return max(
|
|
325
|
+
0,
|
|
326
|
+
head.sub(
|
|
327
|
+
roles.heightSource
|
|
328
|
+
.element(neighbour)
|
|
329
|
+
.add(roles.waterSource.element(neighbour))
|
|
330
|
+
.add(rain),
|
|
331
|
+
),
|
|
332
|
+
);
|
|
333
|
+
};
|
|
334
|
+
const drops = vec4(drop(-1, 0), drop(1, 0), drop(0, -1), drop(0, 1)).toVar();
|
|
335
|
+
const totalDrop = drops.x.add(drops.y).add(drops.z).add(drops.w);
|
|
336
|
+
const available = min(water, totalDrop.mul(tuning.timeStep));
|
|
337
|
+
const scale = available.div(max(totalDrop, 1e-6));
|
|
338
|
+
outflow.element(index).assign(drops.mul(scale));
|
|
339
|
+
}),
|
|
340
|
+
);
|
|
341
|
+
const erode = computeNode(`world.erosion.${iteration}.erode`, count, () =>
|
|
342
|
+
guard(() => {
|
|
343
|
+
const index = instanceIndex.toInt();
|
|
344
|
+
const fluxValue = outflow.element(index).toVar();
|
|
345
|
+
const outgoing = fluxValue.x.add(fluxValue.y).add(fluxValue.z).add(fluxValue.w).toVar();
|
|
346
|
+
const incoming = outflow
|
|
347
|
+
.element(at(index, -1, 0))
|
|
348
|
+
.y.add(outflow.element(at(index, 1, 0)).x)
|
|
349
|
+
.add(outflow.element(at(index, 0, -1)).w)
|
|
350
|
+
.add(outflow.element(at(index, 0, 1)).z);
|
|
351
|
+
const water = max(
|
|
352
|
+
0,
|
|
353
|
+
roles.waterSource
|
|
354
|
+
.element(index)
|
|
355
|
+
.add(rain)
|
|
356
|
+
.add(incoming)
|
|
357
|
+
.sub(outgoing)
|
|
358
|
+
.mul(1 - tuning.evaporation * tuning.timeStep),
|
|
359
|
+
);
|
|
360
|
+
roles.waterTarget.element(index).assign(water);
|
|
361
|
+
const speed = outgoing.div(
|
|
362
|
+
tuning.timeStep * Math.min(options.cellWidth, options.cellDepth),
|
|
363
|
+
);
|
|
364
|
+
const capacity = float(tuning.sedimentCapacity).mul(speed).mul(outgoing);
|
|
365
|
+
const sediment = roles.sedimentSource.element(index).toVar();
|
|
366
|
+
const height = roles.heightSource.element(index).toVar();
|
|
367
|
+
If(sediment.lessThan(capacity), () => {
|
|
368
|
+
const amount = capacity
|
|
369
|
+
.sub(sediment)
|
|
370
|
+
.mul(tuning.erosionRate * tuning.timeStep)
|
|
371
|
+
.min(capacity.sub(sediment));
|
|
372
|
+
height.subAssign(amount);
|
|
373
|
+
sediment.addAssign(amount);
|
|
374
|
+
}).Else(() => {
|
|
375
|
+
const amount = sediment.sub(capacity).mul(tuning.depositionRate * tuning.timeStep);
|
|
376
|
+
height.addAssign(amount);
|
|
377
|
+
sediment.subAssign(amount);
|
|
378
|
+
});
|
|
379
|
+
roles.heightTarget.element(index).assign(height);
|
|
380
|
+
sedimentTemporary.element(index).assign(sediment);
|
|
381
|
+
}),
|
|
382
|
+
);
|
|
383
|
+
const transport = computeNode(`world.erosion.${iteration}.transport`, count, () =>
|
|
384
|
+
guard(() => {
|
|
385
|
+
const index = instanceIndex.toInt();
|
|
386
|
+
const ownFlux = outflow.element(index).toVar();
|
|
387
|
+
const ownOut = ownFlux.x.add(ownFlux.y).add(ownFlux.z).add(ownFlux.w);
|
|
388
|
+
const retained = sedimentTemporary.element(index).mul(min(1, ownOut).oneMinus()).toVar();
|
|
389
|
+
const gather = (x: number, z: number, component: "x" | "y" | "z" | "w") => {
|
|
390
|
+
const neighbour = at(index, x, z);
|
|
391
|
+
const neighbourFlux = outflow.element(neighbour).toVar();
|
|
392
|
+
const total = neighbourFlux.x
|
|
393
|
+
.add(neighbourFlux.y)
|
|
394
|
+
.add(neighbourFlux.z)
|
|
395
|
+
.add(neighbourFlux.w);
|
|
396
|
+
return sedimentTemporary
|
|
397
|
+
.element(neighbour)
|
|
398
|
+
.mul(min(1, total))
|
|
399
|
+
.mul(neighbourFlux[component].div(max(total, 1e-6)));
|
|
400
|
+
};
|
|
401
|
+
retained.addAssign(gather(-1, 0, "y"));
|
|
402
|
+
retained.addAssign(gather(1, 0, "x"));
|
|
403
|
+
retained.addAssign(gather(0, -1, "w"));
|
|
404
|
+
retained.addAssign(gather(0, 1, "z"));
|
|
405
|
+
roles.sedimentTarget.element(index).assign(retained);
|
|
406
|
+
}),
|
|
407
|
+
);
|
|
408
|
+
erosionNodes.push(flux, erode, transport);
|
|
409
|
+
};
|
|
410
|
+
for (let iteration = 0; iteration < tuning.iterations; iteration += 1)
|
|
411
|
+
makeIteration(
|
|
412
|
+
iteration % 2 === 0
|
|
413
|
+
? {
|
|
414
|
+
heightSource: heightA,
|
|
415
|
+
heightTarget: heightB,
|
|
416
|
+
sedimentSource: sedimentA,
|
|
417
|
+
sedimentTarget: sedimentB,
|
|
418
|
+
waterSource: waterA,
|
|
419
|
+
waterTarget: waterB,
|
|
420
|
+
}
|
|
421
|
+
: {
|
|
422
|
+
heightSource: heightB,
|
|
423
|
+
heightTarget: heightA,
|
|
424
|
+
sedimentSource: sedimentB,
|
|
425
|
+
sedimentTarget: sedimentA,
|
|
426
|
+
waterSource: waterB,
|
|
427
|
+
waterTarget: waterA,
|
|
428
|
+
},
|
|
429
|
+
iteration,
|
|
430
|
+
);
|
|
431
|
+
if (erosionNodes.length === 0)
|
|
432
|
+
erosionNodes.push(
|
|
433
|
+
computeNode("world.erosion.bypass", count, () =>
|
|
434
|
+
guard(() =>
|
|
435
|
+
heightA.element(instanceIndex.toInt()).assign(heightA.element(instanceIndex.toInt())),
|
|
436
|
+
),
|
|
437
|
+
),
|
|
438
|
+
);
|
|
439
|
+
const height = tuning.iterations % 2 === 0 ? heightA : heightB;
|
|
440
|
+
const water = tuning.iterations % 2 === 0 ? waterA : waterB;
|
|
441
|
+
const flowNodes: ComputeNode[] = [];
|
|
442
|
+
const makeFlowStep = (
|
|
443
|
+
source: StorageBufferNode<"uint">,
|
|
444
|
+
target: StorageBufferNode<"uint">,
|
|
445
|
+
step: number,
|
|
446
|
+
): void => {
|
|
447
|
+
flowNodes.push(
|
|
448
|
+
computeNode(`world.flow.${step}.clear`, count, () =>
|
|
449
|
+
guard(() => target.element(instanceIndex.toInt()).assign(uint(0))),
|
|
450
|
+
),
|
|
451
|
+
computeNode(`world.flow.${step}.route`, count, () =>
|
|
452
|
+
guard(() => {
|
|
453
|
+
const current = instanceIndex.toInt();
|
|
454
|
+
const amount = atomicLoad(source.element(current));
|
|
455
|
+
const best = height.element(current).toVar();
|
|
456
|
+
const destination = current.toVar();
|
|
457
|
+
const consider = (x: number, z: number): void => {
|
|
458
|
+
const neighbour = at(current, x, z);
|
|
459
|
+
const candidate = height.element(neighbour);
|
|
460
|
+
If(candidate.lessThan(best), () => {
|
|
461
|
+
best.assign(candidate);
|
|
462
|
+
destination.assign(neighbour);
|
|
463
|
+
});
|
|
464
|
+
};
|
|
465
|
+
consider(-1, 0);
|
|
466
|
+
consider(1, 0);
|
|
467
|
+
consider(0, -1);
|
|
468
|
+
consider(0, 1);
|
|
469
|
+
If(destination.notEqual(current).and(amount.greaterThan(0)), () => {
|
|
470
|
+
atomicAdd(target.element(destination), amount);
|
|
471
|
+
atomicAdd(flowAtomic.element(destination), amount);
|
|
472
|
+
});
|
|
473
|
+
}),
|
|
474
|
+
),
|
|
475
|
+
);
|
|
476
|
+
};
|
|
477
|
+
for (let step = 0; step < rows + columns; step += 1)
|
|
478
|
+
makeFlowStep(
|
|
479
|
+
step % 2 === 0 ? flowFrontierA : flowFrontierB,
|
|
480
|
+
step % 2 === 0 ? flowFrontierB : flowFrontierA,
|
|
481
|
+
step,
|
|
482
|
+
);
|
|
483
|
+
const normalizeFlow = computeNode("world.flow.normalize", count, () =>
|
|
484
|
+
guard(() => {
|
|
485
|
+
const index = instanceIndex.toInt();
|
|
486
|
+
flow
|
|
487
|
+
.element(index)
|
|
488
|
+
.assign(log(float(atomicLoad(flowAtomic.element(index))).add(1)).div(Math.log1p(count)));
|
|
489
|
+
}),
|
|
490
|
+
);
|
|
491
|
+
const deriveMoisture = computeNode("world.moisture", count, () =>
|
|
492
|
+
guard(() => {
|
|
493
|
+
const index = instanceIndex.toInt();
|
|
494
|
+
moisture.element(index).assign(
|
|
495
|
+
clamp(
|
|
496
|
+
flow
|
|
497
|
+
.element(index)
|
|
498
|
+
.mul(0.7)
|
|
499
|
+
.add(clamp(water.element(index), 0, 1).mul(0.3)),
|
|
500
|
+
0,
|
|
501
|
+
1,
|
|
502
|
+
),
|
|
503
|
+
);
|
|
504
|
+
}),
|
|
505
|
+
);
|
|
506
|
+
const stages: readonly IWorldPassStage[] = [
|
|
507
|
+
{ name: "synthesis", nodes: [synthesis] },
|
|
508
|
+
{ name: "erosion", nodes: erosionNodes },
|
|
509
|
+
{ name: "flow", nodes: [...flowNodes, normalizeFlow] },
|
|
510
|
+
{ name: "moisture", nodes: [deriveMoisture] },
|
|
511
|
+
];
|
|
512
|
+
return {
|
|
513
|
+
flow,
|
|
514
|
+
height,
|
|
515
|
+
moisture,
|
|
516
|
+
queue: new BoundedWorldPassQueue({ dispatchBudget: options.dispatchBudget, stages }),
|
|
517
|
+
stages,
|
|
518
|
+
dispose: () => {
|
|
519
|
+
for (const node of stages.flatMap((stage) => stage.nodes))
|
|
520
|
+
(node as { dispose?: () => void }).dispose?.();
|
|
521
|
+
for (const buffer of allStorage) buffer.value.dispose();
|
|
522
|
+
},
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
export interface IWorldPassStage {
|
|
527
|
+
/** Physical pass name, used in fail-closed diagnostics. */
|
|
528
|
+
readonly name: "synthesis" | "erosion" | "flow" | "moisture";
|
|
529
|
+
/** Dispatches within a stage remain in this order. */
|
|
530
|
+
readonly nodes: readonly unknown[];
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
export interface IBoundedWorldPassQueueOptions {
|
|
534
|
+
/** Maximum compute dispatches submitted by one fixed step. */
|
|
535
|
+
readonly dispatchBudget: number;
|
|
536
|
+
/** Physical order is synthesis → erosion → flow → moisture. */
|
|
537
|
+
readonly stages: readonly IWorldPassStage[];
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const STAGE_ORDER = ["synthesis", "erosion", "flow", "moisture"] as const;
|
|
541
|
+
|
|
542
|
+
/** @internal Fixed-order, bounded scheduling shared by the world field's GPU passes. */
|
|
543
|
+
export class BoundedWorldPassQueue {
|
|
544
|
+
readonly dispatchBudget: number;
|
|
545
|
+
readonly #nodes: readonly unknown[];
|
|
546
|
+
#cursor = 0;
|
|
547
|
+
|
|
548
|
+
constructor(options: IBoundedWorldPassQueueOptions) {
|
|
549
|
+
if (!Number.isInteger(options.dispatchBudget) || options.dispatchBudget <= 0)
|
|
550
|
+
throw new Error("World passes dispatchBudget must be a positive integer.");
|
|
551
|
+
if (options.stages.length !== STAGE_ORDER.length)
|
|
552
|
+
throw new Error("World passes require synthesis, erosion, flow, and moisture stages.");
|
|
553
|
+
options.stages.forEach((stage, index) => {
|
|
554
|
+
const expected = STAGE_ORDER[index];
|
|
555
|
+
if (stage.name !== expected)
|
|
556
|
+
throw new Error(`World passes expected '${expected}' at stage ${index}.`);
|
|
557
|
+
if (stage.nodes.length === 0)
|
|
558
|
+
throw new Error(`World passes ${stage.name} stage requires at least one node.`);
|
|
559
|
+
});
|
|
560
|
+
this.dispatchBudget = options.dispatchBudget;
|
|
561
|
+
this.#nodes = options.stages.flatMap((stage) => stage.nodes);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
get complete(): boolean {
|
|
565
|
+
return this.#cursor === this.#nodes.length;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
get dispatched(): number {
|
|
569
|
+
return this.#cursor;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** Submit at most one fixed step's budget, preserving the declared physical pass order. */
|
|
573
|
+
process(renderer: Pick<IRendererLike, "compute">): number {
|
|
574
|
+
const end = Math.min(this.#nodes.length, this.#cursor + this.dispatchBudget);
|
|
575
|
+
const start = this.#cursor;
|
|
576
|
+
while (this.#cursor < end) {
|
|
577
|
+
renderer.compute(this.#nodes[this.#cursor]);
|
|
578
|
+
this.#cursor += 1;
|
|
579
|
+
}
|
|
580
|
+
return this.#cursor - start;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { IHeightfieldRegionBounds } from "./world.js";
|
|
2
|
+
|
|
3
|
+
/** The smallest window covering both; `current` may be absent. Internal to the world modules. */
|
|
4
|
+
export function unionBounds(
|
|
5
|
+
current: IHeightfieldRegionBounds | undefined,
|
|
6
|
+
next: IHeightfieldRegionBounds,
|
|
7
|
+
): IHeightfieldRegionBounds {
|
|
8
|
+
if (current === undefined) return next;
|
|
9
|
+
const column = Math.min(current.column, next.column);
|
|
10
|
+
const row = Math.min(current.row, next.row);
|
|
11
|
+
return {
|
|
12
|
+
column,
|
|
13
|
+
columns: Math.max(current.column + current.columns, next.column + next.columns) - column,
|
|
14
|
+
row,
|
|
15
|
+
rows: Math.max(current.row + current.rows, next.row + next.rows) - row,
|
|
16
|
+
};
|
|
17
|
+
}
|