@threenative/terrain 0.1.7 → 0.1.9
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/AGENT_GUIDE.md +19 -4
- package/dist/editor/index.d.ts +218 -5
- package/dist/editor/index.js +4093 -1399
- package/dist/editor/server.d.ts +2 -2
- package/dist/editor/server.js +752 -124
- package/dist/editor/worker.d.ts +33 -1
- package/dist/editor/worker.js +642 -114
- package/dist/export.d.ts +11 -9
- package/dist/export.js +22 -34
- package/dist/index.d.ts +866 -73
- package/dist/index.js +3003 -523
- package/dist/kit.d.ts +57 -0
- package/dist/kit.js +2718 -0
- package/dist/mcp/cook-child.d.ts +2 -0
- package/dist/mcp/cook-child.js +5346 -0
- package/dist/mcp/server.d.ts +7 -0
- package/dist/mcp/server.js +3970 -0
- package/dist/{server-ByM2hmYJ.d.ts → server-B5RdvEAC.d.ts} +3 -2
- package/dist/starter.d.ts +143 -0
- package/dist/starter.js +1031 -0
- package/dist/{terrain-gPnkCU6S.d.ts → terrain-CAEaW0mQ.d.ts} +3 -2
- package/dist/three.d.ts +1 -1
- package/dist/{types-Ant8-Axc.d.ts → types-BpH00qsx.d.ts} +20 -4
- package/dist/world-package-B8QgHjAQ.d.ts +53 -0
- package/package.json +28 -6
- package/starter/alpine/README.md +77 -1
- package/starter/alpine/bake.mjs +4 -116
- package/starter/alpine/biome.data.json +134 -0
- package/starter/alpine/heightmap.r16 +0 -0
- package/starter/alpine/recipe.json +3 -66051
- package/starter/alpine/world.ts +45 -293
- package/starter/coastal/README.md +82 -3
- package/starter/coastal/assets.json +2 -1
- package/starter/coastal/bake.mjs +4 -116
- package/starter/coastal/biome.data.json +156 -0
- package/starter/coastal/heightmap.r16 +0 -0
- package/starter/coastal/recipe.json +3 -66051
- package/starter/coastal/sea.ts +51 -66
- package/starter/coastal/world.ts +53 -318
- package/starter/desert/README.md +77 -1
- package/starter/desert/bake.mjs +4 -116
- package/starter/desert/biome.data.json +124 -0
- package/starter/desert/heightmap.r16 +0 -0
- package/starter/desert/recipe.json +3 -66051
- package/starter/desert/world.ts +45 -293
- package/starter/forest/README.md +85 -5
- package/starter/forest/assets.json +34 -1
- package/starter/forest/bake.mjs +6 -116
- package/starter/forest/biome.data.json +173 -0
- package/starter/forest/heightmap.r16 +0 -0
- package/starter/forest/recipe.json +192 -4428
- package/starter/forest/sky.ts +11 -0
- package/starter/forest/water.ts +39 -48
- package/starter/forest/world.ts +92 -323
- package/starter/tundra/README.md +77 -1
- package/starter/tundra/bake.mjs +4 -116
- package/starter/tundra/biome.data.json +141 -0
- package/starter/tundra/heightmap.r16 +0 -0
- package/starter/tundra/recipe.json +18 -66066
- package/starter/tundra/water.ts +9 -28
- package/starter/tundra/world.ts +65 -319
- package/starter-assets.json +6 -0
- package/{starter-assets/credits.json → starter-credits.json} +12 -105
- package/starter-assets/bark_brown_02/bark_brown_02_diff_1k.jpg +0 -0
- package/starter-assets/bark_brown_02/bark_brown_02_nor_gl_1k.jpg +0 -0
- package/starter-assets/bark_brown_02/bark_brown_02_rough_1k.jpg +0 -0
- package/starter-assets/boulder_01/boulder-mid.glb +0 -0
- package/starter-assets/boulder_01/boulder-near.glb +0 -0
- package/starter-assets/cliff_side/cliff_side_ao_1k.jpg +0 -0
- package/starter-assets/cliff_side/cliff_side_diff_1k.jpg +0 -0
- package/starter-assets/cliff_side/cliff_side_nor_gl_1k.jpg +0 -0
- package/starter-assets/cliff_side/cliff_side_rough_1k.jpg +0 -0
- package/starter-assets/fern_02/fern_02_alpha_512.png +0 -0
- package/starter-assets/fern_02/fern_02_diff_512.jpg +0 -0
- package/starter-assets/fern_02/fern_02_nor_gl_512.jpg +0 -0
- package/starter-assets/fir_tree_01/fir-b-lod1.glb +0 -0
- package/starter-assets/fir_tree_01/fir-b-lod2.glb +0 -0
- package/starter-assets/fir_tree_01/fir-c-lod1.glb +0 -0
- package/starter-assets/fir_tree_01/fir-c-lod2.glb +0 -0
- package/starter-assets/fir_tree_01/fir_tree_01_twig_arm_1k.jpg +0 -0
- package/starter-assets/fir_tree_01/fir_tree_01_twig_diff_1k.jpg +0 -0
- package/starter-assets/fir_tree_01/fir_tree_01_twig_nor_gl_1k.jpg +0 -0
- package/starter-assets/forest_ground_04/forest_ground_04_ao_1k.jpg +0 -0
- package/starter-assets/forest_ground_04/forest_ground_04_diff_1k.jpg +0 -0
- package/starter-assets/forest_ground_04/forest_ground_04_nor_gl_1k.jpg +0 -0
- package/starter-assets/forest_ground_04/forest_ground_04_rough_1k.jpg +0 -0
- package/starter-assets/forest_leaves_02/forest_leaves_02_arm_512.jpg +0 -0
- package/starter-assets/forest_leaves_02/forest_leaves_02_diff_512.jpg +0 -0
- package/starter-assets/forest_leaves_02/forest_leaves_02_nor_gl_512.jpg +0 -0
- package/starter-assets/grass_medium_02/grass_medium_02_alpha_512.png +0 -0
- package/starter-assets/grass_medium_02/grass_medium_02_diff_512.jpg +0 -0
- package/starter-assets/grass_medium_02/grass_medium_02_nor_gl_512.jpg +0 -0
- package/starter-assets/hdri/kloofendal_48d_partly_cloudy_1k.hdr +0 -0
- package/starter-assets/leafy_grass/leafy_grass_ao_1k.jpg +0 -0
- package/starter-assets/leafy_grass/leafy_grass_diff_1k.jpg +0 -0
- package/starter-assets/leafy_grass/leafy_grass_nor_gl_1k.jpg +0 -0
- package/starter-assets/leafy_grass/leafy_grass_rough_1k.jpg +0 -0
- package/starter-assets/lichen_rock/lichen_rock_arm_512.jpg +0 -0
- package/starter-assets/lichen_rock/lichen_rock_diff_512.jpg +0 -0
- package/starter-assets/lichen_rock/lichen_rock_nor_gl_512.jpg +0 -0
- package/starter-assets/mossy_rock/mossy_rock_ao_1k.jpg +0 -0
- package/starter-assets/mossy_rock/mossy_rock_diff_1k.jpg +0 -0
- package/starter-assets/mossy_rock/mossy_rock_nor_gl_1k.jpg +0 -0
- package/starter-assets/mossy_rock/mossy_rock_rough_1k.jpg +0 -0
- package/starter-assets/needle-atlas.png +0 -0
- package/starter-assets/needle-surface.png +0 -0
- package/starter-assets/river_small_rocks/river_small_rocks_arm_512.jpg +0 -0
- package/starter-assets/river_small_rocks/river_small_rocks_diff_512.jpg +0 -0
- package/starter-assets/river_small_rocks/river_small_rocks_nor_gl_512.jpg +0 -0
- package/starter-assets/rock_boulder_dry/rock_boulder_dry_arm_512.jpg +0 -0
- package/starter-assets/rock_boulder_dry/rock_boulder_dry_diff_512.jpg +0 -0
- package/starter-assets/rock_boulder_dry/rock_boulder_dry_nor_gl_512.jpg +0 -0
- package/starter-assets/rocks/boulder-mid.glb +0 -0
- package/starter-assets/rocks/boulder-near.glb +0 -0
- package/starter-assets/rocks/boulder-prep.json +0 -37
- package/starter-assets/rocks/rock01-mid.glb +0 -0
- package/starter-assets/rocks/rock01-near.glb +0 -0
- package/starter-assets/rocks/rock01-prep.json +0 -37
- package/starter-assets/rocks/rock04-mid.glb +0 -0
- package/starter-assets/rocks/rock04-near.glb +0 -0
- package/starter-assets/rocks/rock04-prep.json +0 -37
- package/starter-assets/sand_01/sand_01_ao_1k.jpg +0 -0
- package/starter-assets/sand_01/sand_01_diff_1k.jpg +0 -0
- package/starter-assets/sand_01/sand_01_nor_gl_1k.jpg +0 -0
- package/starter-assets/sand_01/sand_01_rough_1k.jpg +0 -0
- package/starter-assets/snow_02/snow_02_ao_1k.jpg +0 -0
- package/starter-assets/snow_02/snow_02_diff_1k.jpg +0 -0
- package/starter-assets/snow_02/snow_02_nor_gl_1k.jpg +0 -0
- package/starter-assets/snow_02/snow_02_rough_1k.jpg +0 -0
package/dist/index.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { Heightfield } from '@threenative/core/world';
|
|
2
|
+
import { createRandom } from '@threenative/core';
|
|
3
|
+
import { ImprovedNoise } from 'three/addons/math/ImprovedNoise.js';
|
|
4
|
+
import { MathUtils } from 'three';
|
|
5
|
+
|
|
1
6
|
// src/core/math.ts
|
|
2
7
|
var clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));
|
|
3
8
|
var lerp = (a, b, t) => a + (b - a) * t;
|
|
@@ -6,9 +11,9 @@ var smoothstep = (a, b, x) => {
|
|
|
6
11
|
const t = clamp((x - a) / (b - a));
|
|
7
12
|
return t * t * (3 - 2 * t);
|
|
8
13
|
};
|
|
9
|
-
var hashString = (
|
|
14
|
+
var hashString = (text2) => {
|
|
10
15
|
let h = 2166136261;
|
|
11
|
-
for (const c of
|
|
16
|
+
for (const c of text2) h = Math.imul(h ^ c.charCodeAt(0), 16777619);
|
|
12
17
|
return h >>> 0;
|
|
13
18
|
};
|
|
14
19
|
function random(seed) {
|
|
@@ -121,10 +126,10 @@ function slopeAtIndex(grid, i) {
|
|
|
121
126
|
const gz = (grid.height[Math.min(z + 1, n - 1) * n + x] - grid.height[Math.max(z - 1, 0) * n + x]) / (dz * cell);
|
|
122
127
|
return Math.atan(Math.hypot(gx, gz)) * (180 / Math.PI);
|
|
123
128
|
}
|
|
124
|
-
function slopeQuantile(grid,
|
|
125
|
-
if (!(
|
|
129
|
+
function slopeQuantile(grid, quantile2) {
|
|
130
|
+
if (!(quantile2 >= 0 && quantile2 <= 1))
|
|
126
131
|
throw new Error(
|
|
127
|
-
`TN_TERRAIN_SLOPE_QUANTILE_INVALID: quantile must be in [0, 1], received ${String(
|
|
132
|
+
`TN_TERRAIN_SLOPE_QUANTILE_INVALID: quantile must be in [0, 1], received ${String(quantile2)}.`
|
|
128
133
|
);
|
|
129
134
|
const n = grid.resolution;
|
|
130
135
|
const slopes = [];
|
|
@@ -132,7 +137,7 @@ function slopeQuantile(grid, quantile) {
|
|
|
132
137
|
for (let x = 1; x < n - 1; x++) slopes.push(slopeAtIndex(grid, z * n + x));
|
|
133
138
|
if (slopes.length === 0) return 0;
|
|
134
139
|
slopes.sort((a, b) => a - b);
|
|
135
|
-
return slopes[Math.min(slopes.length - 1, Math.floor(slopes.length *
|
|
140
|
+
return slopes[Math.min(slopes.length - 1, Math.floor(slopes.length * quantile2))];
|
|
136
141
|
}
|
|
137
142
|
function falloff(distance, radius, softness = 0.6) {
|
|
138
143
|
const d = distance / radius;
|
|
@@ -145,12 +150,12 @@ function splinePoints(points, step = 3, smooth = true) {
|
|
|
145
150
|
const b = points[k];
|
|
146
151
|
const c = points[k + 1];
|
|
147
152
|
const d = points[Math.min(points.length - 1, k + 2)];
|
|
148
|
-
const
|
|
153
|
+
const count2 = Math.max(
|
|
149
154
|
1,
|
|
150
155
|
Math.min(512, Math.ceil(Math.hypot(c[0] - b[0], c[2] - b[2]) / step))
|
|
151
156
|
);
|
|
152
|
-
for (let j = 0; j <
|
|
153
|
-
const t = j /
|
|
157
|
+
for (let j = 0; j < count2; j += 1) {
|
|
158
|
+
const t = j / count2;
|
|
154
159
|
const t2 = t * t;
|
|
155
160
|
const t3 = t2 * t;
|
|
156
161
|
const cat = (dim) => 0.5 * (2 * b[dim] + (-a[dim] + c[dim]) * t + (2 * a[dim] - 5 * b[dim] + 4 * c[dim] - d[dim]) * t2 + (-a[dim] + 3 * b[dim] - 3 * c[dim] + d[dim]) * t3);
|
|
@@ -168,6 +173,10 @@ function splinePoints(points, step = 3, smooth = true) {
|
|
|
168
173
|
|
|
169
174
|
// src/core/erosion.ts
|
|
170
175
|
function thermal(height, n, size, { iterations = 20, talus = 32, rate = 0.22 } = {}, observations) {
|
|
176
|
+
if (!Number.isInteger(n) || n < 2)
|
|
177
|
+
throw RangeError("Thermal erosion requires grid resolution n >= 2");
|
|
178
|
+
if (!Number.isInteger(iterations) || iterations < 0 || iterations > 200)
|
|
179
|
+
throw RangeError("Thermal erosion iterations must be between 0 and 200");
|
|
171
180
|
const h = height.slice();
|
|
172
181
|
const delta = new Float64Array(h.length);
|
|
173
182
|
const limit = Math.tan(talus * Math.PI / 180) * (size / (n - 1));
|
|
@@ -176,18 +185,24 @@ function thermal(height, n, size, { iterations = 20, talus = 32, rate = 0.22 } =
|
|
|
176
185
|
for (let z = 0; z < n; z += 1) {
|
|
177
186
|
for (let x = 0; x < n; x += 1) {
|
|
178
187
|
const i = z * n + x;
|
|
188
|
+
const here = h[i];
|
|
179
189
|
let best = -1;
|
|
180
190
|
let diff = limit;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
+
if (x > 0 && here - h[i - 1] > diff) {
|
|
192
|
+
diff = here - h[i - 1];
|
|
193
|
+
best = i - 1;
|
|
194
|
+
}
|
|
195
|
+
if (x < n - 1 && here - h[i + 1] > diff) {
|
|
196
|
+
diff = here - h[i + 1];
|
|
197
|
+
best = i + 1;
|
|
198
|
+
}
|
|
199
|
+
if (z > 0 && here - h[i - n] > diff) {
|
|
200
|
+
diff = here - h[i - n];
|
|
201
|
+
best = i - n;
|
|
202
|
+
}
|
|
203
|
+
if (z < n - 1 && here - h[i + n] > diff) {
|
|
204
|
+
diff = here - h[i + n];
|
|
205
|
+
best = i + n;
|
|
191
206
|
}
|
|
192
207
|
if (best >= 0) {
|
|
193
208
|
const transfer = (diff - limit) * rate;
|
|
@@ -206,119 +221,211 @@ var brushes = /* @__PURE__ */ new Map();
|
|
|
206
221
|
function brush(radius) {
|
|
207
222
|
let cells = brushes.get(radius);
|
|
208
223
|
if (!cells) {
|
|
209
|
-
|
|
224
|
+
const dx = [];
|
|
225
|
+
const dz = [];
|
|
226
|
+
const raw = [];
|
|
210
227
|
let total = 0;
|
|
211
|
-
for (let
|
|
212
|
-
for (let
|
|
213
|
-
const d = Math.hypot(
|
|
228
|
+
for (let z = -radius; z <= radius; z += 1) {
|
|
229
|
+
for (let x = -radius; x <= radius; x += 1) {
|
|
230
|
+
const d = Math.hypot(x, z);
|
|
214
231
|
if (d > radius) continue;
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
232
|
+
const weight2 = 1 - d / (radius + 1);
|
|
233
|
+
dx.push(x);
|
|
234
|
+
dz.push(z);
|
|
235
|
+
raw.push(weight2);
|
|
236
|
+
total += weight2;
|
|
218
237
|
}
|
|
219
238
|
}
|
|
220
|
-
|
|
239
|
+
const weight = Float64Array.from(raw, (w) => w / total);
|
|
240
|
+
let full = 0;
|
|
241
|
+
for (let k = 0; k < weight.length; k += 1) full += weight[k];
|
|
242
|
+
cells = { dx: Int32Array.from(dx), dz: Int32Array.from(dz), full, radius, weight };
|
|
221
243
|
brushes.set(radius, cells);
|
|
222
244
|
}
|
|
223
245
|
return cells;
|
|
224
246
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
247
|
+
var MAX_AUTOMATIC_DROPLETS = 1025 * 1025;
|
|
248
|
+
var MAX_EXPLICIT_DROPLETS = 2e5;
|
|
249
|
+
function hydraulic(height, n, size, options = {}, observations) {
|
|
250
|
+
const {
|
|
251
|
+
droplets: requestedDroplets,
|
|
252
|
+
// A droplet has to be able to cross the world, so its step budget scales with the grid: a
|
|
253
|
+
// fixed 40 barely leaves a 257 world while a 65 world would walk off it in a few steps.
|
|
254
|
+
maxSteps = Math.round(Math.min(64, Math.max(24, n / 4))),
|
|
255
|
+
inertia = 0.2,
|
|
256
|
+
capacity = 4,
|
|
257
|
+
// Each step takes a small bite of the sediment the droplet can carry. A big bite carves
|
|
258
|
+
// fewer, deeper dimples that stand proud of their neighbours as spikes; many shallow bites
|
|
259
|
+
// over the same lines incise the same drainage with a smooth surface.
|
|
260
|
+
erosion = 0.03,
|
|
261
|
+
deposition = 0.3,
|
|
262
|
+
evaporation = 0.025,
|
|
263
|
+
seed = 1,
|
|
264
|
+
brushRadius = 3
|
|
265
|
+
} = options;
|
|
266
|
+
if (!Number.isInteger(n) || n < 2)
|
|
267
|
+
throw RangeError("Hydraulic erosion requires grid resolution n >= 2");
|
|
268
|
+
const droplets = requestedDroplets === void 0 ? Math.min(n * n, MAX_AUTOMATIC_DROPLETS) : requestedDroplets;
|
|
269
|
+
if (!Number.isSafeInteger(droplets) || droplets < 0)
|
|
270
|
+
throw RangeError("Hydraulic erosion droplets must be a non-negative integer");
|
|
271
|
+
if (requestedDroplets !== void 0 && droplets > MAX_EXPLICIT_DROPLETS)
|
|
272
|
+
throw RangeError(`Hydraulic erosion droplets must not exceed ${String(MAX_EXPLICIT_DROPLETS)}`);
|
|
273
|
+
if (!Number.isInteger(maxSteps) || maxSteps < 1 || maxSteps > 128)
|
|
274
|
+
throw RangeError("Hydraulic erosion maxSteps must be between 1 and 128");
|
|
243
275
|
const h = Float64Array.from(height);
|
|
244
276
|
const rnd = random(seed);
|
|
245
277
|
const cell = size / (n - 1);
|
|
246
|
-
const
|
|
278
|
+
const minSlope = 5e-3 * cell;
|
|
279
|
+
const oneMinusInertia = 1 - inertia;
|
|
280
|
+
const oneMinusEvaporation = 1 - evaporation;
|
|
281
|
+
const brushCells = brush(brushRadius);
|
|
282
|
+
const brushDx = brushCells.dx;
|
|
283
|
+
const brushDz = brushCells.dz;
|
|
284
|
+
const brushWeight = brushCells.weight;
|
|
285
|
+
const brushCount = brushDx.length;
|
|
286
|
+
const brushFull = brushCells.full;
|
|
287
|
+
const brushEdge = brushCells.radius;
|
|
288
|
+
const brushOffset = new Int32Array(brushCount);
|
|
289
|
+
for (let k = 0; k < brushCount; k += 1)
|
|
290
|
+
brushOffset[k] = brushDz[k] * n + brushDx[k];
|
|
291
|
+
const obsFlow = observations?.flow;
|
|
292
|
+
const obsSediment = observations?.sediment;
|
|
293
|
+
const obsDeposition = observations?.deposition;
|
|
247
294
|
const spread = (x, z, amount, floor = Number.NEGATIVE_INFINITY) => {
|
|
295
|
+
if (amount === 0) return 0;
|
|
296
|
+
const interior = x >= brushEdge && x < n - brushEdge && z >= brushEdge && z < n - brushEdge;
|
|
297
|
+
if (interior) {
|
|
298
|
+
const total2 = brushFull;
|
|
299
|
+
if (total2 <= 0) return 0;
|
|
300
|
+
const base = z * n + x;
|
|
301
|
+
if (amount < 0) {
|
|
302
|
+
let moved = 0;
|
|
303
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
304
|
+
const i = base + brushOffset[k];
|
|
305
|
+
const share2 = amount * brushWeight[k] / total2;
|
|
306
|
+
const avail = h[i] - floor;
|
|
307
|
+
const change = avail <= 0 ? -0 : -share2 < avail ? share2 : -avail;
|
|
308
|
+
h[i] = h[i] + change;
|
|
309
|
+
moved += change;
|
|
310
|
+
}
|
|
311
|
+
return moved;
|
|
312
|
+
}
|
|
313
|
+
if (obsDeposition !== void 0) {
|
|
314
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
315
|
+
const i = base + brushOffset[k];
|
|
316
|
+
const share2 = amount * brushWeight[k] / total2;
|
|
317
|
+
h[i] = h[i] + share2;
|
|
318
|
+
obsDeposition[i] = obsDeposition[i] + share2;
|
|
319
|
+
}
|
|
320
|
+
} else {
|
|
321
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
322
|
+
const i = base + brushOffset[k];
|
|
323
|
+
const share2 = amount * brushWeight[k] / total2;
|
|
324
|
+
h[i] = h[i] + share2;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return amount;
|
|
328
|
+
}
|
|
248
329
|
let total = 0;
|
|
249
|
-
for (
|
|
250
|
-
const ix = x +
|
|
251
|
-
const iz = z +
|
|
252
|
-
if (ix >= 0 && ix < n && iz >= 0 && iz < n) total +=
|
|
330
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
331
|
+
const ix = x + brushDx[k];
|
|
332
|
+
const iz = z + brushDz[k];
|
|
333
|
+
if (ix >= 0 && ix < n && iz >= 0 && iz < n) total += brushWeight[k];
|
|
253
334
|
}
|
|
254
335
|
if (total <= 0) return 0;
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
336
|
+
if (amount < 0) {
|
|
337
|
+
let moved = 0;
|
|
338
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
339
|
+
const ix = x + brushDx[k];
|
|
340
|
+
const iz = z + brushDz[k];
|
|
341
|
+
if (ix < 0 || ix >= n || iz < 0 || iz >= n) continue;
|
|
342
|
+
const i = iz * n + ix;
|
|
343
|
+
const share2 = amount * brushWeight[k] / total;
|
|
344
|
+
const avail = h[i] - floor;
|
|
345
|
+
const change = avail <= 0 ? -0 : -share2 < avail ? share2 : -avail;
|
|
346
|
+
h[i] = h[i] + change;
|
|
347
|
+
moved += change;
|
|
348
|
+
}
|
|
349
|
+
return moved;
|
|
350
|
+
}
|
|
351
|
+
if (obsDeposition !== void 0) {
|
|
352
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
353
|
+
const ix = x + brushDx[k];
|
|
354
|
+
const iz = z + brushDz[k];
|
|
355
|
+
if (ix < 0 || ix >= n || iz < 0 || iz >= n) continue;
|
|
356
|
+
const i = iz * n + ix;
|
|
357
|
+
const share2 = amount * brushWeight[k] / total;
|
|
358
|
+
h[i] = h[i] + share2;
|
|
359
|
+
obsDeposition[i] = obsDeposition[i] + share2;
|
|
360
|
+
}
|
|
361
|
+
} else {
|
|
362
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
363
|
+
const ix = x + brushDx[k];
|
|
364
|
+
const iz = z + brushDz[k];
|
|
365
|
+
if (ix < 0 || ix >= n || iz < 0 || iz >= n) continue;
|
|
366
|
+
const i = iz * n + ix;
|
|
367
|
+
const share2 = amount * brushWeight[k] / total;
|
|
368
|
+
h[i] = h[i] + share2;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return amount;
|
|
269
372
|
};
|
|
270
|
-
const
|
|
373
|
+
const probeOld = { value: 0, dx: 0, dz: 0, ix: 0, iz: 0 };
|
|
374
|
+
const probeNext = { value: 0, dx: 0, dz: 0, ix: 0, iz: 0 };
|
|
375
|
+
const get = (x, z, out) => {
|
|
271
376
|
const ix = Math.min(n - 2, Math.floor(x));
|
|
272
377
|
const iz = Math.min(n - 2, Math.floor(z));
|
|
273
378
|
const tx = x - ix;
|
|
274
379
|
const tz = z - iz;
|
|
275
380
|
const i = iz * n + ix;
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
tx
|
|
291
|
-
) / cell,
|
|
292
|
-
ix,
|
|
293
|
-
iz
|
|
294
|
-
};
|
|
381
|
+
const h00 = h[i];
|
|
382
|
+
const h10 = h[i + 1];
|
|
383
|
+
const h01 = h[i + n];
|
|
384
|
+
const h11 = h[i + n + 1];
|
|
385
|
+
const g10 = h10 - h00;
|
|
386
|
+
const g01 = h01 - h00;
|
|
387
|
+
const v0 = h00 + g10 * tx;
|
|
388
|
+
const v1 = h01 + (h11 - h01) * tx;
|
|
389
|
+
out.value = v0 + (v1 - v0) * tz;
|
|
390
|
+
out.dx = (g10 + (h11 - h01 - g10) * tz) / cell;
|
|
391
|
+
out.dz = (g01 + (h11 - h10 - g01) * tx) / cell;
|
|
392
|
+
out.ix = ix;
|
|
393
|
+
out.iz = iz;
|
|
394
|
+
return out;
|
|
295
395
|
};
|
|
296
396
|
for (let k = 0; k < droplets; k += 1) {
|
|
297
397
|
let x = rnd() * (n - 1 - 1e-3);
|
|
298
398
|
let z = rnd() * (n - 1 - 1e-3);
|
|
299
399
|
let dx = 0;
|
|
300
400
|
let dz = 0;
|
|
301
|
-
let
|
|
401
|
+
let water2 = 1;
|
|
302
402
|
let speed = 1;
|
|
303
403
|
let sediment = 0;
|
|
304
404
|
for (let step = 0; step < maxSteps; step += 1) {
|
|
305
|
-
const old = get(x, z);
|
|
306
|
-
if (
|
|
405
|
+
const old = get(x, z, probeOld);
|
|
406
|
+
if (obsFlow !== void 0 && obsSediment !== void 0) {
|
|
307
407
|
const tx = x - old.ix;
|
|
308
408
|
const tz = z - old.iz;
|
|
309
409
|
const i = old.iz * n + old.ix;
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
410
|
+
const w00 = (1 - tx) * (1 - tz);
|
|
411
|
+
const w10 = tx * (1 - tz);
|
|
412
|
+
const w01 = (1 - tx) * tz;
|
|
413
|
+
const w11 = tx * tz;
|
|
414
|
+
obsFlow[i] = obsFlow[i] + water2 * w00;
|
|
415
|
+
obsSediment[i] = obsSediment[i] + sediment * w00;
|
|
416
|
+
obsFlow[i + 1] = obsFlow[i + 1] + water2 * w10;
|
|
417
|
+
obsSediment[i + 1] = obsSediment[i + 1] + sediment * w10;
|
|
418
|
+
obsFlow[i + n] = obsFlow[i + n] + water2 * w01;
|
|
419
|
+
obsSediment[i + n] = obsSediment[i + n] + sediment * w01;
|
|
420
|
+
obsFlow[i + n + 1] = obsFlow[i + n + 1] + water2 * w11;
|
|
421
|
+
obsSediment[i + n + 1] = obsSediment[i + n + 1] + sediment * w11;
|
|
318
422
|
}
|
|
319
|
-
dx = dx * inertia - old.dx *
|
|
320
|
-
dz = dz * inertia - old.dz *
|
|
321
|
-
|
|
423
|
+
dx = dx * inertia - old.dx * oneMinusInertia;
|
|
424
|
+
dz = dz * inertia - old.dz * oneMinusInertia;
|
|
425
|
+
const largest = Math.max(Math.abs(dx), Math.abs(dz));
|
|
426
|
+
const scaledX = Math.abs(dx) / largest;
|
|
427
|
+
const scaledZ = Math.abs(dz) / largest;
|
|
428
|
+
let length = largest === 0 || largest === Number.POSITIVE_INFINITY ? largest : Math.sqrt(scaledX * scaledX + scaledZ * scaledZ) * largest;
|
|
322
429
|
if (length < 1e-9) {
|
|
323
430
|
const angle = rnd() * Math.PI * 2;
|
|
324
431
|
dx = Math.cos(angle);
|
|
@@ -333,31 +440,363 @@ function hydraulic(height, n, size, {
|
|
|
333
440
|
sediment = 0;
|
|
334
441
|
break;
|
|
335
442
|
}
|
|
336
|
-
const next = get(nx, nz);
|
|
443
|
+
const next = get(nx, nz, probeNext);
|
|
337
444
|
const dh = next.value - old.value;
|
|
338
|
-
const
|
|
445
|
+
const slope = -dh;
|
|
446
|
+
const cap = (slope > minSlope ? slope : minSlope) * speed * water2 * capacity;
|
|
339
447
|
if (dh > 0 || sediment > cap) {
|
|
340
|
-
const amount = dh > 0 ?
|
|
448
|
+
const amount = dh > 0 ? dh < sediment ? dh : sediment : (sediment - cap) * deposition;
|
|
341
449
|
spread(old.ix, old.iz, amount);
|
|
342
450
|
sediment -= amount;
|
|
343
451
|
} else {
|
|
344
|
-
const
|
|
452
|
+
const bite = (cap - sediment) * erosion;
|
|
453
|
+
const amount = bite < slope ? bite : slope;
|
|
345
454
|
sediment -= spread(old.ix, old.iz, -amount, next.value);
|
|
346
455
|
}
|
|
347
|
-
|
|
348
|
-
|
|
456
|
+
const sq = speed * speed - dh * 3;
|
|
457
|
+
speed = Math.sqrt(sq > 0.01 ? sq : 0.01);
|
|
458
|
+
water2 *= oneMinusEvaporation;
|
|
349
459
|
x = nx;
|
|
350
460
|
z = nz;
|
|
351
|
-
if (
|
|
461
|
+
if (water2 < 0.02) break;
|
|
352
462
|
}
|
|
353
|
-
if (sediment > 0 &&
|
|
354
|
-
const end = get(clamp(x, 0, n - 1.001), clamp(z, 0, n - 1.001));
|
|
463
|
+
if (sediment > 0 && water2 < 0.02) {
|
|
464
|
+
const end = get(clamp(x, 0, n - 1.001), clamp(z, 0, n - 1.001), probeOld);
|
|
355
465
|
spread(end.ix, end.iz, sediment);
|
|
356
466
|
}
|
|
357
467
|
}
|
|
358
468
|
return Float32Array.from(h);
|
|
359
469
|
}
|
|
360
470
|
|
|
471
|
+
// src/core/fluvial.ts
|
|
472
|
+
var DX = Int8Array.of(-1, 0, 1, -1, 1, -1, 0, 1);
|
|
473
|
+
var DZ = Int8Array.of(-1, -1, -1, 0, 0, 1, 1, 1);
|
|
474
|
+
var DIST = Float64Array.from(DX, (dx, k) => Math.hypot(dx, DZ[k]));
|
|
475
|
+
var FILL_SLOPE = Math.tan(0.06 * Math.PI / 180);
|
|
476
|
+
var EPSILON = 1e-3;
|
|
477
|
+
function fillDepressions(h, n, epsilon = EPSILON) {
|
|
478
|
+
const total = n * n;
|
|
479
|
+
const keys = new Float64Array(total);
|
|
480
|
+
const cells = new Int32Array(total);
|
|
481
|
+
const closed2 = new Uint8Array(total);
|
|
482
|
+
let size = 0;
|
|
483
|
+
const push = (key, cell) => {
|
|
484
|
+
let k = size++;
|
|
485
|
+
while (k > 0) {
|
|
486
|
+
const parent = k - 1 >> 1;
|
|
487
|
+
if (keys[parent] <= key) break;
|
|
488
|
+
keys[k] = keys[parent];
|
|
489
|
+
cells[k] = cells[parent];
|
|
490
|
+
k = parent;
|
|
491
|
+
}
|
|
492
|
+
keys[k] = key;
|
|
493
|
+
cells[k] = cell;
|
|
494
|
+
};
|
|
495
|
+
const pop = () => {
|
|
496
|
+
const top = cells[0];
|
|
497
|
+
size -= 1;
|
|
498
|
+
const key = keys[size];
|
|
499
|
+
const cell = cells[size];
|
|
500
|
+
let k = 0;
|
|
501
|
+
for (; ; ) {
|
|
502
|
+
let child = 2 * k + 1;
|
|
503
|
+
if (child >= size) break;
|
|
504
|
+
if (child + 1 < size && keys[child + 1] < keys[child]) child += 1;
|
|
505
|
+
if (keys[child] >= key) break;
|
|
506
|
+
keys[k] = keys[child];
|
|
507
|
+
cells[k] = cells[child];
|
|
508
|
+
k = child;
|
|
509
|
+
}
|
|
510
|
+
keys[k] = key;
|
|
511
|
+
cells[k] = cell;
|
|
512
|
+
return top;
|
|
513
|
+
};
|
|
514
|
+
for (let z = 0; z < n; z += 1) {
|
|
515
|
+
for (let x = 0; x < n; x += 1) {
|
|
516
|
+
if (x > 0 && x < n - 1 && z > 0 && z < n - 1) continue;
|
|
517
|
+
const i = z * n + x;
|
|
518
|
+
closed2[i] = 1;
|
|
519
|
+
push(h[i], i);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
while (size > 0) {
|
|
523
|
+
const c = pop();
|
|
524
|
+
const cx = c % n;
|
|
525
|
+
const cz = (c - cx) / n;
|
|
526
|
+
const level = h[c];
|
|
527
|
+
for (let k = 0; k < 8; k += 1) {
|
|
528
|
+
const x = cx + DX[k];
|
|
529
|
+
const z = cz + DZ[k];
|
|
530
|
+
if (x < 0 || z < 0 || x >= n || z >= n) continue;
|
|
531
|
+
const j = z * n + x;
|
|
532
|
+
if (closed2[j]) continue;
|
|
533
|
+
closed2[j] = 1;
|
|
534
|
+
if (h[j] <= level) h[j] = level + epsilon;
|
|
535
|
+
push(h[j], j);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
function createFlow(n) {
|
|
540
|
+
const total = n * n;
|
|
541
|
+
return {
|
|
542
|
+
receiver: new Int32Array(total),
|
|
543
|
+
order: new Int32Array(total),
|
|
544
|
+
cells: new Float64Array(total)
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
function routeFlow(h, n, flow) {
|
|
548
|
+
const { receiver, order, cells } = flow;
|
|
549
|
+
const total = n * n;
|
|
550
|
+
const head = new Int32Array(total).fill(-1);
|
|
551
|
+
const next = new Int32Array(total);
|
|
552
|
+
let roots = 0;
|
|
553
|
+
for (let z = 0; z < n; z += 1) {
|
|
554
|
+
for (let x = 0; x < n; x += 1) {
|
|
555
|
+
const i = z * n + x;
|
|
556
|
+
let best = -1;
|
|
557
|
+
if (x > 0 && x < n - 1 && z > 0 && z < n - 1) {
|
|
558
|
+
const here = h[i];
|
|
559
|
+
let bestSlope = 0;
|
|
560
|
+
for (let k = 0; k < 8; k += 1) {
|
|
561
|
+
const j = i + DZ[k] * n + DX[k];
|
|
562
|
+
const slope = (here - h[j]) / DIST[k];
|
|
563
|
+
if (slope > bestSlope) {
|
|
564
|
+
bestSlope = slope;
|
|
565
|
+
best = j;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
receiver[i] = best;
|
|
570
|
+
if (best < 0) order[roots++] = i;
|
|
571
|
+
else {
|
|
572
|
+
next[i] = head[best];
|
|
573
|
+
head[best] = i;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
for (let k = 0; k < total; k += 1) {
|
|
578
|
+
const i = order[k];
|
|
579
|
+
for (let d = head[i]; d >= 0; d = next[d]) order[roots++] = d;
|
|
580
|
+
}
|
|
581
|
+
cells.fill(1);
|
|
582
|
+
for (let k = total - 1; k >= 0; k -= 1) {
|
|
583
|
+
const i = order[k];
|
|
584
|
+
const r = receiver[i];
|
|
585
|
+
if (r >= 0) cells[r] = cells[r] + cells[i];
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function fluvial(height, n, size, {
|
|
589
|
+
iterations = 40,
|
|
590
|
+
uplift = 1e-3,
|
|
591
|
+
erodibility = 5e-6,
|
|
592
|
+
timeStep = 2e4,
|
|
593
|
+
concavity = 0.45,
|
|
594
|
+
talus = 35,
|
|
595
|
+
relief
|
|
596
|
+
} = {}) {
|
|
597
|
+
const total = n * n;
|
|
598
|
+
const spacing = size / (n - 1);
|
|
599
|
+
const h = Float64Array.from(height);
|
|
600
|
+
const band2 = Math.min(4, n - 1);
|
|
601
|
+
for (let a = 0; a < n; a += 1)
|
|
602
|
+
for (let d = 1; d <= band2; d += 1) {
|
|
603
|
+
h[a] = Math.min(h[a], h[d * n + a]);
|
|
604
|
+
h[(n - 1) * n + a] = Math.min(h[(n - 1) * n + a], h[(n - 1 - d) * n + a]);
|
|
605
|
+
h[a * n] = Math.min(h[a * n], h[a * n + d]);
|
|
606
|
+
h[a * n + n - 1] = Math.min(h[a * n + n - 1], h[a * n + n - 1 - d]);
|
|
607
|
+
}
|
|
608
|
+
fillDepressions(h, n, FILL_SLOPE * spacing);
|
|
609
|
+
let low = Number.POSITIVE_INFINITY;
|
|
610
|
+
let high = Number.NEGATIVE_INFINITY;
|
|
611
|
+
for (let i = 0; i < total; i += 1) {
|
|
612
|
+
low = Math.min(low, h[i]);
|
|
613
|
+
high = Math.max(high, h[i]);
|
|
614
|
+
}
|
|
615
|
+
const range2 = high - low || 1;
|
|
616
|
+
const rise = Float64Array.from(h, (v) => uplift * timeStep * (v - low) / range2);
|
|
617
|
+
const flow = createFlow(n);
|
|
618
|
+
const kdt = erodibility * timeStep;
|
|
619
|
+
const maxRise = Math.tan(talus * Math.PI / 180) * spacing;
|
|
620
|
+
const power = Float64Array.from(
|
|
621
|
+
{ length: total + 1 },
|
|
622
|
+
(_, c) => (c * spacing * spacing) ** concavity
|
|
623
|
+
);
|
|
624
|
+
for (let step = 0; step < iterations; step += 1) {
|
|
625
|
+
routeFlow(h, n, flow);
|
|
626
|
+
const { order, receiver, cells } = flow;
|
|
627
|
+
for (let k = 0; k < total; k += 1) {
|
|
628
|
+
const i = order[k];
|
|
629
|
+
const r = receiver[i];
|
|
630
|
+
if (r < 0) continue;
|
|
631
|
+
const delta = Math.abs(r - i);
|
|
632
|
+
const run = delta === 1 || delta === n ? 1 : Math.SQRT2;
|
|
633
|
+
const factor = kdt * power[cells[i]] / (spacing * run);
|
|
634
|
+
const below = h[r];
|
|
635
|
+
const solved = (h[i] + rise[i] + factor * below) / (1 + factor);
|
|
636
|
+
h[i] = Math.max(Math.min(solved, below + maxRise * run), below + EPSILON);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
for (let a = 0; a < n; a += 1) {
|
|
640
|
+
h[a] = h[n + a];
|
|
641
|
+
h[(n - 1) * n + a] = h[(n - 2) * n + a];
|
|
642
|
+
}
|
|
643
|
+
for (let a = 0; a < n; a += 1) {
|
|
644
|
+
h[a * n] = h[a * n + 1];
|
|
645
|
+
h[a * n + n - 1] = h[a * n + n - 2];
|
|
646
|
+
}
|
|
647
|
+
if (relief !== void 0) {
|
|
648
|
+
let lo = Number.POSITIVE_INFINITY;
|
|
649
|
+
let hi = Number.NEGATIVE_INFINITY;
|
|
650
|
+
for (const v of h) {
|
|
651
|
+
lo = Math.min(lo, v);
|
|
652
|
+
hi = Math.max(hi, v);
|
|
653
|
+
}
|
|
654
|
+
const gain = relief / (hi - lo || 1);
|
|
655
|
+
for (let i = 0; i < total; i += 1) h[i] = lo + (h[i] - lo) * gain;
|
|
656
|
+
}
|
|
657
|
+
return Float32Array.from(h);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
// src/core/gully.ts
|
|
661
|
+
var STEP_SECONDS = 0.1;
|
|
662
|
+
var FLUX_KEEP = 0.99;
|
|
663
|
+
var DISSOLVE = 0.3;
|
|
664
|
+
var DEPOSIT = 0.02;
|
|
665
|
+
var EVAPORATE = 0.05;
|
|
666
|
+
var DEPTH_REF = 0.01;
|
|
667
|
+
var DEPTH_MAX = 0.3;
|
|
668
|
+
var MIN_TILT = 0.05;
|
|
669
|
+
var GRAVITY = 9.81;
|
|
670
|
+
var MAX_ITERATIONS = 200;
|
|
671
|
+
function gully(height, n, size, { iterations = 200, capacity = 0.05, rain = 5e-4 } = {}) {
|
|
672
|
+
if (!Number.isInteger(n) || n < 2)
|
|
673
|
+
throw new RangeError("Gully erosion requires grid resolution n >= 2");
|
|
674
|
+
if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS)
|
|
675
|
+
throw new RangeError(`Gully erosion iterations must be between 0 and ${MAX_ITERATIONS}`);
|
|
676
|
+
if (!Number.isFinite(capacity) || capacity < 0 || capacity > 1e6)
|
|
677
|
+
throw new RangeError("Gully erosion capacity must be between 0 and 1000000");
|
|
678
|
+
if (!Number.isFinite(rain) || rain < 0 || rain > 1)
|
|
679
|
+
throw new RangeError("Gully erosion rain must be between 0 and 1 metres per step");
|
|
680
|
+
const total = n * n;
|
|
681
|
+
let h = Float64Array.from(height);
|
|
682
|
+
let hTmp = new Float64Array(total);
|
|
683
|
+
const water2 = new Float64Array(total);
|
|
684
|
+
const waterBefore = new Float64Array(total);
|
|
685
|
+
const speed = new Float64Array(total);
|
|
686
|
+
const sediment = new Float64Array(total);
|
|
687
|
+
const sedTmp = new Float64Array(total);
|
|
688
|
+
const fluxL = new Float64Array(total);
|
|
689
|
+
const fluxR = new Float64Array(total);
|
|
690
|
+
const fluxT = new Float64Array(total);
|
|
691
|
+
const fluxB = new Float64Array(total);
|
|
692
|
+
const cell = size / (n - 1);
|
|
693
|
+
const cellArea = cell * cell;
|
|
694
|
+
const gain = STEP_SECONDS * GRAVITY * cell;
|
|
695
|
+
const evaporateKeep = Math.max(1 - EVAPORATE * STEP_SECONDS, 0);
|
|
696
|
+
const share2 = (flow, depth) => depth > 0 ? flow * STEP_SECONDS / (depth * cellArea) : 0;
|
|
697
|
+
for (let step = 0; step < iterations; step += 1) {
|
|
698
|
+
for (let z = 0; z < n; z += 1) {
|
|
699
|
+
for (let x = 0; x < n; x += 1) {
|
|
700
|
+
const i = z * n + x;
|
|
701
|
+
const depth = water2[i] + rain;
|
|
702
|
+
const surface = h[i] + depth;
|
|
703
|
+
const ground = h[i];
|
|
704
|
+
const left = x > 0 ? h[i - 1] + water2[i - 1] + rain : ground;
|
|
705
|
+
const right = x < n - 1 ? h[i + 1] + water2[i + 1] + rain : ground;
|
|
706
|
+
const top = z > 0 ? h[i - n] + water2[i - n] + rain : ground;
|
|
707
|
+
const bottom = z < n - 1 ? h[i + n] + water2[i + n] + rain : ground;
|
|
708
|
+
let l = fluxL[i] * FLUX_KEEP + gain * (surface - left);
|
|
709
|
+
let r = fluxR[i] * FLUX_KEEP + gain * (surface - right);
|
|
710
|
+
let t = fluxT[i] * FLUX_KEEP + gain * (surface - top);
|
|
711
|
+
let b = fluxB[i] * FLUX_KEEP + gain * (surface - bottom);
|
|
712
|
+
if (l < 0) l = 0;
|
|
713
|
+
if (r < 0) r = 0;
|
|
714
|
+
if (t < 0) t = 0;
|
|
715
|
+
if (b < 0) b = 0;
|
|
716
|
+
const sum = l + r + t + b;
|
|
717
|
+
if (sum > 0) {
|
|
718
|
+
const keep = Math.min(1, depth * cellArea / (sum * STEP_SECONDS));
|
|
719
|
+
l *= keep;
|
|
720
|
+
r *= keep;
|
|
721
|
+
t *= keep;
|
|
722
|
+
b *= keep;
|
|
723
|
+
}
|
|
724
|
+
fluxL[i] = l;
|
|
725
|
+
fluxR[i] = r;
|
|
726
|
+
fluxT[i] = t;
|
|
727
|
+
fluxB[i] = b;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
for (let z = 0; z < n; z += 1) {
|
|
731
|
+
for (let x = 0; x < n; x += 1) {
|
|
732
|
+
const i = z * n + x;
|
|
733
|
+
const inL = x > 0 ? fluxR[i - 1] : 0;
|
|
734
|
+
const inR = x < n - 1 ? fluxL[i + 1] : 0;
|
|
735
|
+
const inT = z > 0 ? fluxB[i - n] : 0;
|
|
736
|
+
const inB = z < n - 1 ? fluxT[i + n] : 0;
|
|
737
|
+
const before = water2[i] + rain;
|
|
738
|
+
const after = Math.max(
|
|
739
|
+
0,
|
|
740
|
+
before + STEP_SECONDS * (inL + inR + inT + inB - fluxL[i] - fluxR[i] - fluxT[i] - fluxB[i]) / cellArea
|
|
741
|
+
);
|
|
742
|
+
const depth = 0.5 * (before + after);
|
|
743
|
+
const acrossX = inL - fluxL[i] + fluxR[i] - inR;
|
|
744
|
+
const acrossZ = inT - fluxT[i] + fluxB[i] - inB;
|
|
745
|
+
speed[i] = depth > 1e-4 ? Math.sqrt(acrossX * acrossX + acrossZ * acrossZ) * 0.5 / (cell * depth) : 0;
|
|
746
|
+
water2[i] = after;
|
|
747
|
+
waterBefore[i] = before;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
for (let z = 0; z < n; z += 1) {
|
|
751
|
+
for (let x = 0; x < n; x += 1) {
|
|
752
|
+
const i = z * n + x;
|
|
753
|
+
const gx = (h[z * n + Math.min(x + 1, n - 1)] - h[z * n + Math.max(x - 1, 0)]) / (2 * cell);
|
|
754
|
+
const gy = (h[Math.min(z + 1, n - 1) * n + x] - h[Math.max(z - 1, 0) * n + x]) / (2 * cell);
|
|
755
|
+
const squared = gx * gx + gy * gy;
|
|
756
|
+
const sine = Math.max(Math.sqrt(squared / (1 + squared)), MIN_TILT);
|
|
757
|
+
const depth = water2[i];
|
|
758
|
+
const transport = capacity * sine * speed[i] * Math.min(depth / DEPTH_REF, 1) * Math.max(1 - depth / DEPTH_MAX, 0);
|
|
759
|
+
let surface = h[i];
|
|
760
|
+
let load = sediment[i];
|
|
761
|
+
if (transport > load) {
|
|
762
|
+
const cut = DISSOLVE * (transport - load);
|
|
763
|
+
surface -= cut;
|
|
764
|
+
load += cut;
|
|
765
|
+
} else {
|
|
766
|
+
const drop = DEPOSIT * (load - transport);
|
|
767
|
+
surface += drop;
|
|
768
|
+
load -= drop;
|
|
769
|
+
}
|
|
770
|
+
hTmp[i] = surface;
|
|
771
|
+
sedTmp[i] = load;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
const passed = h;
|
|
775
|
+
h = hTmp;
|
|
776
|
+
hTmp = passed;
|
|
777
|
+
for (let z = 0; z < n; z += 1) {
|
|
778
|
+
for (let x = 0; x < n; x += 1) {
|
|
779
|
+
const i = z * n + x;
|
|
780
|
+
let load = sedTmp[i] * (1 - share2(
|
|
781
|
+
fluxL[i] + fluxR[i] + fluxT[i] + fluxB[i],
|
|
782
|
+
waterBefore[i]
|
|
783
|
+
));
|
|
784
|
+
if (x > 0)
|
|
785
|
+
load += sedTmp[i - 1] * share2(fluxR[i - 1], waterBefore[i - 1]);
|
|
786
|
+
if (x < n - 1)
|
|
787
|
+
load += sedTmp[i + 1] * share2(fluxL[i + 1], waterBefore[i + 1]);
|
|
788
|
+
if (z > 0)
|
|
789
|
+
load += sedTmp[i - n] * share2(fluxB[i - n], waterBefore[i - n]);
|
|
790
|
+
if (z < n - 1)
|
|
791
|
+
load += sedTmp[i + n] * share2(fluxT[i + n], waterBefore[i + n]);
|
|
792
|
+
sediment[i] = load;
|
|
793
|
+
water2[i] = water2[i] * evaporateKeep;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return Float32Array.from(h, (surface, i) => surface + sediment[i]);
|
|
798
|
+
}
|
|
799
|
+
|
|
361
800
|
// src/core/masks.ts
|
|
362
801
|
var MATERIAL_IDS = Object.freeze([
|
|
363
802
|
"grass",
|
|
@@ -481,6 +920,57 @@ function paintWeights(s, i, channel, weight) {
|
|
|
481
920
|
s.splat[base + c] = s.splat[base + c] * (1 - w) + (c === channel ? w : 0);
|
|
482
921
|
}
|
|
483
922
|
|
|
923
|
+
// src/core/scree.ts
|
|
924
|
+
var NEIGHBOURS = [
|
|
925
|
+
[-1, -1],
|
|
926
|
+
[0, -1],
|
|
927
|
+
[1, -1],
|
|
928
|
+
[-1, 0],
|
|
929
|
+
[1, 0],
|
|
930
|
+
[-1, 1],
|
|
931
|
+
[0, 1],
|
|
932
|
+
[1, 1]
|
|
933
|
+
];
|
|
934
|
+
var DISTANCE = Float64Array.from(NEIGHBOURS, ([dx, dz]) => Math.hypot(dx, dz));
|
|
935
|
+
var MAX_ITERATIONS2 = 200;
|
|
936
|
+
function scree(height, n, size, { iterations = 200, talus = 35, rate = 0.05 } = {}) {
|
|
937
|
+
if (!Number.isInteger(n) || n < 2)
|
|
938
|
+
throw new RangeError("Scree slide requires grid resolution n >= 2");
|
|
939
|
+
if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS2)
|
|
940
|
+
throw new RangeError(`Scree slide iterations must be between 0 and ${MAX_ITERATIONS2}`);
|
|
941
|
+
if (!Number.isFinite(talus) || talus < 0 || talus > 89.9)
|
|
942
|
+
throw new RangeError("Scree slide talus must be between 0 and 89.9 degrees");
|
|
943
|
+
if (!Number.isFinite(rate) || rate < 0 || rate > 1)
|
|
944
|
+
throw new RangeError("Scree slide rate must be between 0 and 1");
|
|
945
|
+
const cell = size / (n - 1);
|
|
946
|
+
const limit = Float64Array.from(DISTANCE, (d) => Math.tan(talus * Math.PI / 180) * cell * d);
|
|
947
|
+
let source = Float64Array.from(height);
|
|
948
|
+
let target = new Float64Array(source.length);
|
|
949
|
+
for (let k = 0; k < iterations; k += 1) {
|
|
950
|
+
for (let z = 0; z < n; z += 1) {
|
|
951
|
+
for (let x = 0; x < n; x += 1) {
|
|
952
|
+
const i = z * n + x;
|
|
953
|
+
const here = source[i];
|
|
954
|
+
let received = 0;
|
|
955
|
+
for (let d = 0; d < 8; d += 1) {
|
|
956
|
+
const nx = x + NEIGHBOURS[d][0];
|
|
957
|
+
const nz = z + NEIGHBOURS[d][1];
|
|
958
|
+
if (nx < 0 || nz < 0 || nx >= n || nz >= n) continue;
|
|
959
|
+
const other = source[nz * n + nx];
|
|
960
|
+
const gap = limit[d];
|
|
961
|
+
if (other - here > gap) received += other - here - gap;
|
|
962
|
+
else if (here - other > gap) received -= here - other - gap;
|
|
963
|
+
}
|
|
964
|
+
target[i] = here + rate * received;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
const swap = source;
|
|
968
|
+
source = target;
|
|
969
|
+
target = swap;
|
|
970
|
+
}
|
|
971
|
+
return Float32Array.from(source);
|
|
972
|
+
}
|
|
973
|
+
|
|
484
974
|
// src/core/operations.ts
|
|
485
975
|
var OPERATION_TYPES = Object.freeze([
|
|
486
976
|
"noise",
|
|
@@ -532,9 +1022,9 @@ function brushCenters(p, seed) {
|
|
|
532
1022
|
for (let k = 1; k < source.length; k += 1) {
|
|
533
1023
|
const a = source[k - 1];
|
|
534
1024
|
const b = source[k];
|
|
535
|
-
const
|
|
536
|
-
for (let j = 1; j <=
|
|
537
|
-
out.push([lerp(a[0], b[0], j /
|
|
1025
|
+
const count2 = Math.max(1, Math.ceil(Math.hypot(b[0] - a[0], b[1] - a[1]) / step));
|
|
1026
|
+
for (let j = 1; j <= count2; j += 1)
|
|
1027
|
+
out.push([lerp(a[0], b[0], j / count2), lerp(a[1], b[1], j / count2)]);
|
|
538
1028
|
}
|
|
539
1029
|
if (p.jitter) {
|
|
540
1030
|
const rnd = random(seed);
|
|
@@ -598,12 +1088,12 @@ function gradeToGround(s, points, p) {
|
|
|
598
1088
|
const next = profile.slice();
|
|
599
1089
|
for (let k = 0; k < profile.length; k += 1) {
|
|
600
1090
|
let sum = 0;
|
|
601
|
-
let
|
|
1091
|
+
let count2 = 0;
|
|
602
1092
|
for (let j = Math.max(0, k - window); j <= Math.min(profile.length - 1, k + window); j += 1) {
|
|
603
1093
|
sum += profile[j];
|
|
604
|
-
|
|
1094
|
+
count2 += 1;
|
|
605
1095
|
}
|
|
606
|
-
next[k] = sum /
|
|
1096
|
+
next[k] = sum / count2;
|
|
607
1097
|
}
|
|
608
1098
|
for (let k = 0; k < profile.length; k += 1) profile[k] = next[k];
|
|
609
1099
|
}
|
|
@@ -641,10 +1131,10 @@ function gradeToGround(s, points, p) {
|
|
|
641
1131
|
);
|
|
642
1132
|
for (let k = 0; k < points.length; k += 1) at(k)[1] = profile[k];
|
|
643
1133
|
}
|
|
644
|
-
function applySpline(s,
|
|
645
|
-
const p =
|
|
646
|
-
const river =
|
|
647
|
-
const ramp =
|
|
1134
|
+
function applySpline(s, layer2, mask) {
|
|
1135
|
+
const p = layer2.params;
|
|
1136
|
+
const river = layer2.type === "river";
|
|
1137
|
+
const ramp = layer2.type === "ramp";
|
|
648
1138
|
const seed = p.seed ?? s.seed;
|
|
649
1139
|
const raw = ramp ? [p.from, p.to] : p.points ?? [];
|
|
650
1140
|
const follows = followsTerrain(raw, p.followTerrain);
|
|
@@ -661,7 +1151,7 @@ function applySpline(s, layer, mask) {
|
|
|
661
1151
|
const shoulder = p.shoulder ?? half;
|
|
662
1152
|
const extent = half + shoulder;
|
|
663
1153
|
const depth = p.depth ?? 4;
|
|
664
|
-
const alpha =
|
|
1154
|
+
const alpha = layer2.opacity ?? 1;
|
|
665
1155
|
const n = s.resolution;
|
|
666
1156
|
const cell = s.size / (n - 1);
|
|
667
1157
|
const dist = new Float32Array(n * n).fill(Number.POSITIVE_INFINITY);
|
|
@@ -726,18 +1216,18 @@ function applySpline(s, layer, mask) {
|
|
|
726
1216
|
}
|
|
727
1217
|
if (river && p.water !== false)
|
|
728
1218
|
s.rivers.push({
|
|
729
|
-
id:
|
|
1219
|
+
id: layer2.id,
|
|
730
1220
|
points,
|
|
731
1221
|
width: (p.width ?? 12) * (p.waterWidth ?? 0.76),
|
|
732
1222
|
color: p.color,
|
|
733
1223
|
opacity: alpha
|
|
734
1224
|
});
|
|
735
1225
|
}
|
|
736
|
-
function applyOperation(s,
|
|
737
|
-
const { type, params: p } =
|
|
738
|
-
const opacity =
|
|
739
|
-
if (
|
|
740
|
-
const mask = rasterMask(
|
|
1226
|
+
function applyOperation(s, layer2) {
|
|
1227
|
+
const { type, params: p } = layer2;
|
|
1228
|
+
const opacity = layer2.opacity ?? 1;
|
|
1229
|
+
if (layer2.enabled === false || opacity <= 0) return;
|
|
1230
|
+
const mask = rasterMask(layer2.mask, s);
|
|
741
1231
|
const weight = (i) => (mask?.[i] ?? 1) * opacity;
|
|
742
1232
|
const n = s.resolution;
|
|
743
1233
|
const seed = p.seed ?? s.seed;
|
|
@@ -785,9 +1275,9 @@ function applyOperation(s, layer) {
|
|
|
785
1275
|
let sum = 0;
|
|
786
1276
|
let total = 0;
|
|
787
1277
|
for (let i = 0; i < s.height.length; i += 1) {
|
|
788
|
-
const
|
|
789
|
-
sum += s.height[i] *
|
|
790
|
-
total +=
|
|
1278
|
+
const share2 = covered[i] * opacity;
|
|
1279
|
+
sum += s.height[i] * share2;
|
|
1280
|
+
total += share2;
|
|
791
1281
|
}
|
|
792
1282
|
const target = total > 0 ? sum / total : 0;
|
|
793
1283
|
let delta = 0;
|
|
@@ -823,18 +1313,18 @@ function applyOperation(s, layer) {
|
|
|
823
1313
|
const i = z * n + x;
|
|
824
1314
|
if (w(i) === 0) continue;
|
|
825
1315
|
let total = 0;
|
|
826
|
-
let
|
|
1316
|
+
let count2 = 0;
|
|
827
1317
|
for (let dz = -1; dz <= 1; dz += 1) {
|
|
828
1318
|
for (let dx = -1; dx <= 1; dx += 1) {
|
|
829
1319
|
const xx = x + dx;
|
|
830
1320
|
const zz = z + dz;
|
|
831
1321
|
if (xx >= 0 && xx < n && zz >= 0 && zz < n) {
|
|
832
1322
|
total += old[zz * n + xx];
|
|
833
|
-
|
|
1323
|
+
count2 += 1;
|
|
834
1324
|
}
|
|
835
1325
|
}
|
|
836
1326
|
}
|
|
837
|
-
s.height[i] = lerp(old[i], total /
|
|
1327
|
+
s.height[i] = lerp(old[i], total / count2, w(i) * clamp(p.strength ?? 0.65));
|
|
838
1328
|
}
|
|
839
1329
|
}
|
|
840
1330
|
}
|
|
@@ -901,7 +1391,7 @@ function applyOperation(s, layer) {
|
|
|
901
1391
|
talus: new Float32Array(s.height.length)
|
|
902
1392
|
};
|
|
903
1393
|
s.erosion ??= structuredClone(observations);
|
|
904
|
-
const eroded = p.method === "hydraulic" ? hydraulic(
|
|
1394
|
+
const eroded = p.method === "fluvial" ? fluvial(s.height, n, s.size, p) : p.method === "hydraulic" ? hydraulic(
|
|
905
1395
|
s.height,
|
|
906
1396
|
n,
|
|
907
1397
|
s.size,
|
|
@@ -916,7 +1406,7 @@ function applyOperation(s, layer) {
|
|
|
916
1406
|
evaporation: p.evaporation
|
|
917
1407
|
},
|
|
918
1408
|
observations
|
|
919
|
-
) : thermal(s.height, n, s.size, p, observations);
|
|
1409
|
+
) : p.method === "gully" ? gully(s.height, n, s.size, p) : p.method === "scree" ? scree(s.height, n, s.size, p) : thermal(s.height, n, s.size, p, observations);
|
|
920
1410
|
let influence = mask;
|
|
921
1411
|
if (p.at || p.points) influence = brushWeights(s, p, mask, seed);
|
|
922
1412
|
for (let i = 0; i < s.height.length; i += 1) {
|
|
@@ -965,13 +1455,13 @@ function applyOperation(s, layer) {
|
|
|
965
1455
|
break;
|
|
966
1456
|
}
|
|
967
1457
|
case "scatter":
|
|
968
|
-
s.scatterRules.push({ id:
|
|
1458
|
+
s.scatterRules.push({ id: layer2.id, ...p, mask: layer2.mask, opacity, clear: [] });
|
|
969
1459
|
break;
|
|
970
1460
|
case "clear": {
|
|
971
1461
|
if (p.target === "scatter" || p.target === "all") {
|
|
972
1462
|
for (const rule of s.scatterRules)
|
|
973
1463
|
if (!p.asset || rule.asset === p.asset)
|
|
974
|
-
rule.clear.push({ mask:
|
|
1464
|
+
rule.clear.push({ mask: layer2.mask ?? { type: "all" }, opacity });
|
|
975
1465
|
}
|
|
976
1466
|
if (p.target === "biome" || p.target === "all")
|
|
977
1467
|
for (const [name, values] of Object.entries(s.biomes)) {
|
|
@@ -986,15 +1476,15 @@ function applyOperation(s, layer) {
|
|
|
986
1476
|
case "road":
|
|
987
1477
|
case "river":
|
|
988
1478
|
case "ramp":
|
|
989
|
-
applySpline(s,
|
|
1479
|
+
applySpline(s, layer2, mask);
|
|
990
1480
|
break;
|
|
991
1481
|
case "water":
|
|
992
|
-
s.waterRules.push({ id:
|
|
1482
|
+
s.waterRules.push({ id: layer2.id, ...p, mask: layer2.mask, opacity });
|
|
993
1483
|
break;
|
|
994
1484
|
case "heightmap": {
|
|
995
1485
|
if (p.at !== void 0 || p.size !== void 0 || p.rotation !== void 0 || p.falloff !== void 0) {
|
|
996
1486
|
applyOperation(s, {
|
|
997
|
-
...
|
|
1487
|
+
...layer2,
|
|
998
1488
|
type: "paste",
|
|
999
1489
|
params: { ...p, size: p.size ?? s.size, falloff: p.falloff ?? 0 }
|
|
1000
1490
|
});
|
|
@@ -1011,7 +1501,7 @@ function applyOperation(s, layer) {
|
|
|
1011
1501
|
break;
|
|
1012
1502
|
}
|
|
1013
1503
|
default:
|
|
1014
|
-
throw Error(`Unsupported operation: ${String(
|
|
1504
|
+
throw Error(`Unsupported operation: ${String(layer2.type)}`);
|
|
1015
1505
|
}
|
|
1016
1506
|
}
|
|
1017
1507
|
function finalizeState(s) {
|
|
@@ -1019,13 +1509,13 @@ function finalizeState(s) {
|
|
|
1019
1509
|
const cell = s.size / (n - 1);
|
|
1020
1510
|
s.waters = [];
|
|
1021
1511
|
s.instances = [];
|
|
1022
|
-
for (const
|
|
1512
|
+
for (const water2 of s.waterRules) {
|
|
1023
1513
|
const mask = new Uint8Array(n * n);
|
|
1024
1514
|
const queue = new Int32Array(n * n);
|
|
1025
|
-
const allowed = compileMask(
|
|
1026
|
-
const level =
|
|
1027
|
-
const at =
|
|
1028
|
-
const radius =
|
|
1515
|
+
const allowed = compileMask(water2.mask, s);
|
|
1516
|
+
const level = water2.level ?? 0;
|
|
1517
|
+
const at = water2.at ?? [0, 0];
|
|
1518
|
+
const radius = water2.radius ?? s.size;
|
|
1029
1519
|
let front = 0;
|
|
1030
1520
|
let back = 0;
|
|
1031
1521
|
const eligible = (i) => {
|
|
@@ -1040,7 +1530,7 @@ function finalizeState(s) {
|
|
|
1040
1530
|
back += 1;
|
|
1041
1531
|
}
|
|
1042
1532
|
};
|
|
1043
|
-
if (
|
|
1533
|
+
if (water2.kind === "ocean") {
|
|
1044
1534
|
for (let k = 0; k < n; k += 1) {
|
|
1045
1535
|
push(k);
|
|
1046
1536
|
push((n - 1) * n + k);
|
|
@@ -1062,14 +1552,15 @@ function finalizeState(s) {
|
|
|
1062
1552
|
if (z > 0) push(i - n);
|
|
1063
1553
|
if (z < n - 1) push(i + n);
|
|
1064
1554
|
}
|
|
1065
|
-
s.waters.push({ ...
|
|
1555
|
+
s.waters.push({ ...water2, level, mask });
|
|
1066
1556
|
if (!back)
|
|
1067
1557
|
s.diagnostics.push(
|
|
1068
|
-
`Water '${
|
|
1558
|
+
`Water '${water2.id}' has no flooded cells; its seed may be above the water level.`
|
|
1069
1559
|
);
|
|
1070
1560
|
}
|
|
1071
1561
|
const wet = (i, x, z, h) => {
|
|
1072
|
-
if (s.waters.some((
|
|
1562
|
+
if (s.waters.some((water2) => water2.mask[i])) return true;
|
|
1563
|
+
if (s.waters.some((water2) => water2.kind === "ocean" && h < water2.level + 0.35)) return true;
|
|
1073
1564
|
for (const river of s.rivers) {
|
|
1074
1565
|
const half = river.width / 2;
|
|
1075
1566
|
for (let k = 1; k < river.points.length; k += 1) {
|
|
@@ -1087,7 +1578,7 @@ function finalizeState(s) {
|
|
|
1087
1578
|
return false;
|
|
1088
1579
|
};
|
|
1089
1580
|
for (const rule of s.scatterRules) {
|
|
1090
|
-
const
|
|
1581
|
+
const count2 = Math.round((rule.count ?? 300) * (rule.opacity ?? 1));
|
|
1091
1582
|
const seed = (rule.seed ?? s.seed ^ hashString(rule.id)) >>> 0;
|
|
1092
1583
|
const rnd = random(seed);
|
|
1093
1584
|
const accept = compileMask(rule.mask, s);
|
|
@@ -1099,9 +1590,9 @@ function finalizeState(s) {
|
|
|
1099
1590
|
const gridSize = Math.max(distance, 0.1);
|
|
1100
1591
|
const buckets = /* @__PURE__ */ new Map();
|
|
1101
1592
|
const accepted = [];
|
|
1102
|
-
const maxAttempts =
|
|
1593
|
+
const maxAttempts = count2 * 40;
|
|
1103
1594
|
const scale = rule.scale ?? [0.8, 1.3];
|
|
1104
|
-
for (let attempt = 0; attempt < maxAttempts && accepted.length <
|
|
1595
|
+
for (let attempt = 0; attempt < maxAttempts && accepted.length < count2; attempt += 1) {
|
|
1105
1596
|
const x = (rnd() - 0.5) * s.size;
|
|
1106
1597
|
const z = (rnd() - 0.5) * s.size;
|
|
1107
1598
|
const acceptance = rnd();
|
|
@@ -1142,7 +1633,7 @@ function finalizeState(s) {
|
|
|
1142
1633
|
accepted.push({
|
|
1143
1634
|
id: `${rule.id}:candidate:${seed}:${attempt}`,
|
|
1144
1635
|
layer: rule.id,
|
|
1145
|
-
asset: rule.asset
|
|
1636
|
+
asset: rule.asset,
|
|
1146
1637
|
position: [x, h + (rule.offsetY ?? 0), z],
|
|
1147
1638
|
rotation: yaw,
|
|
1148
1639
|
scale: itemScale,
|
|
@@ -1154,10 +1645,10 @@ function finalizeState(s) {
|
|
|
1154
1645
|
bucket.push(index);
|
|
1155
1646
|
buckets.set(key, bucket);
|
|
1156
1647
|
}
|
|
1157
|
-
s.instances.push(
|
|
1158
|
-
if (accepted.length <
|
|
1648
|
+
for (const instance of accepted) s.instances.push(instance);
|
|
1649
|
+
if (accepted.length < count2)
|
|
1159
1650
|
s.diagnostics.push(
|
|
1160
|
-
`Scatter '${rule.id}': placed ${accepted.length}/${
|
|
1651
|
+
`Scatter '${rule.id}': placed ${accepted.length}/${count2}; masks or spacing exhausted the attempt budget.`
|
|
1161
1652
|
);
|
|
1162
1653
|
}
|
|
1163
1654
|
return s;
|
|
@@ -1229,6 +1720,10 @@ var PARAMS = Object.freeze({
|
|
|
1229
1720
|
"method",
|
|
1230
1721
|
"iterations",
|
|
1231
1722
|
"talus",
|
|
1723
|
+
"uplift",
|
|
1724
|
+
"erodibility",
|
|
1725
|
+
"timeStep",
|
|
1726
|
+
"relief",
|
|
1232
1727
|
"rate",
|
|
1233
1728
|
"droplets",
|
|
1234
1729
|
"maxSteps",
|
|
@@ -1236,7 +1731,8 @@ var PARAMS = Object.freeze({
|
|
|
1236
1731
|
"capacity",
|
|
1237
1732
|
"erosion",
|
|
1238
1733
|
"deposition",
|
|
1239
|
-
"evaporation"
|
|
1734
|
+
"evaporation",
|
|
1735
|
+
"rain"
|
|
1240
1736
|
],
|
|
1241
1737
|
terrace: ["step", "softness", "strength", "offset"],
|
|
1242
1738
|
materials: ["base", "rules", "reset"],
|
|
@@ -1278,14 +1774,23 @@ function fail(message) {
|
|
|
1278
1774
|
}
|
|
1279
1775
|
function finiteTree(value, path = "value", depth = 0) {
|
|
1280
1776
|
if (depth > 32) fail(`${path}: nesting is too deep`);
|
|
1281
|
-
if (typeof value === "number"
|
|
1777
|
+
if (typeof value === "number") {
|
|
1778
|
+
if (!Number.isFinite(value)) fail(`${path} must be finite`);
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1282
1781
|
if (value === void 0 || typeof value === "function" || typeof value === "bigint" || typeof value === "symbol")
|
|
1283
1782
|
fail(`${path} is not JSON-serializable`);
|
|
1284
1783
|
if (value && typeof value === "object") {
|
|
1285
1784
|
if (ArrayBuffer.isView(value)) fail(`${path}: use a plain array in recipes`);
|
|
1286
|
-
for (const
|
|
1287
|
-
if (
|
|
1785
|
+
for (const key of Object.keys(value)) {
|
|
1786
|
+
if (key === "__proto__" || key === "prototype" || key === "constructor")
|
|
1288
1787
|
fail(`${path}: unsafe object key`);
|
|
1788
|
+
const entry = value[key];
|
|
1789
|
+
if (typeof entry === "number") {
|
|
1790
|
+
if (depth + 1 > 32) fail(`${path}.${key}: nesting is too deep`);
|
|
1791
|
+
if (!Number.isFinite(entry)) fail(`${path}.${key} must be finite`);
|
|
1792
|
+
continue;
|
|
1793
|
+
}
|
|
1289
1794
|
finiteTree(entry, `${path}.${key}`, depth + 1);
|
|
1290
1795
|
}
|
|
1291
1796
|
}
|
|
@@ -1390,17 +1895,17 @@ function splinePoint(v, name, follows) {
|
|
|
1390
1895
|
if (typeof y !== "number") fail(`${name} must contain 3 numbers`);
|
|
1391
1896
|
if (!Number.isFinite(y) && !follows) fail(`${name} elevation must be finite`);
|
|
1392
1897
|
}
|
|
1393
|
-
function layerFollowsTerrain(
|
|
1394
|
-
if (!
|
|
1898
|
+
function layerFollowsTerrain(layer2) {
|
|
1899
|
+
if (!layer2 || typeof layer2 !== "object" || !["road", "river", "ramp"].includes(layer2.type))
|
|
1395
1900
|
return false;
|
|
1396
|
-
const params =
|
|
1397
|
-
const source =
|
|
1901
|
+
const params = layer2.params ?? {};
|
|
1902
|
+
const source = layer2.type === "ramp" ? [params.from, params.to] : params.points;
|
|
1398
1903
|
if (!Array.isArray(source)) return false;
|
|
1399
1904
|
return followsTerrain(source, params.followTerrain);
|
|
1400
1905
|
}
|
|
1401
|
-
function omittedElevations(
|
|
1402
|
-
if (!follows) return
|
|
1403
|
-
const params =
|
|
1906
|
+
function omittedElevations(layer2, follows) {
|
|
1907
|
+
if (!follows) return layer2;
|
|
1908
|
+
const params = layer2.params ?? {};
|
|
1404
1909
|
const zero = (p) => Array.isArray(p) && p.length === 3 ? [p[0], 0, p[2]] : p;
|
|
1405
1910
|
const points = params.points;
|
|
1406
1911
|
const fixes = {
|
|
@@ -1408,28 +1913,28 @@ function omittedElevations(layer, follows) {
|
|
|
1408
1913
|
...Array.isArray(params.from) ? { from: zero(params.from) } : {},
|
|
1409
1914
|
...Array.isArray(params.to) ? { to: zero(params.to) } : {}
|
|
1410
1915
|
};
|
|
1411
|
-
return Object.keys(fixes).length ? { ...
|
|
1916
|
+
return Object.keys(fixes).length ? { ...layer2, params: { ...params, ...fixes } } : layer2;
|
|
1412
1917
|
}
|
|
1413
|
-
function validateLayer(
|
|
1414
|
-
const follows = layerFollowsTerrain(
|
|
1415
|
-
finiteTree(omittedElevations(
|
|
1416
|
-
if (!
|
|
1417
|
-
for (const k of Object.keys(
|
|
1918
|
+
function validateLayer(layer2) {
|
|
1919
|
+
const follows = layerFollowsTerrain(layer2);
|
|
1920
|
+
finiteTree(omittedElevations(layer2, follows), "layer");
|
|
1921
|
+
if (!layer2 || typeof layer2 !== "object") fail("layer must be an object");
|
|
1922
|
+
for (const k of Object.keys(layer2))
|
|
1418
1923
|
if (!["id", "name", "type", "params", "enabled", "opacity", "mask"].includes(k))
|
|
1419
1924
|
fail(`Unknown layer field '${k}'`);
|
|
1420
|
-
identifier(
|
|
1421
|
-
enumeration(
|
|
1422
|
-
if (
|
|
1423
|
-
if (
|
|
1925
|
+
identifier(layer2.id, "layer.id");
|
|
1926
|
+
enumeration(layer2.type, "operation", OPERATION_TYPES);
|
|
1927
|
+
if (layer2.name !== void 0) identifier(layer2.name, "layer.name");
|
|
1928
|
+
if (layer2.enabled !== void 0 && typeof layer2.enabled !== "boolean")
|
|
1424
1929
|
fail("enabled must be boolean");
|
|
1425
|
-
if (
|
|
1426
|
-
if (
|
|
1427
|
-
const params =
|
|
1930
|
+
if (layer2.opacity !== void 0) num(layer2.opacity, "opacity", 0, 1);
|
|
1931
|
+
if (layer2.mask) validateMask(layer2.mask);
|
|
1932
|
+
const params = layer2.params ?? {};
|
|
1428
1933
|
if (!params || Array.isArray(params) || typeof params !== "object")
|
|
1429
1934
|
fail("params must be an object");
|
|
1430
1935
|
for (const k of Object.keys(params))
|
|
1431
|
-
if (!PARAMS[
|
|
1432
|
-
fail(`Unknown ${
|
|
1936
|
+
if (!PARAMS[layer2.type].includes(k) && k !== "seed")
|
|
1937
|
+
fail(`Unknown ${layer2.type} parameter '${k}'`);
|
|
1433
1938
|
for (const key of [
|
|
1434
1939
|
"island",
|
|
1435
1940
|
"mirrorX",
|
|
@@ -1449,7 +1954,7 @@ function validateLayer(layer) {
|
|
|
1449
1954
|
if (params.from !== void 0) splinePoint(params.from, "from", follows);
|
|
1450
1955
|
if (params.to !== void 0) splinePoint(params.to, "to", follows);
|
|
1451
1956
|
const points = params.points;
|
|
1452
|
-
const isSpline = ["road", "river"].includes(
|
|
1957
|
+
const isSpline = ["road", "river"].includes(layer2.type);
|
|
1453
1958
|
if (points) {
|
|
1454
1959
|
if (!Array.isArray(points) || !points.length || points.length > 8192)
|
|
1455
1960
|
fail("points must have 1..8192 entries");
|
|
@@ -1484,8 +1989,8 @@ function validateLayer(layer) {
|
|
|
1484
1989
|
);
|
|
1485
1990
|
}
|
|
1486
1991
|
if (isSpline && (!Array.isArray(points) || points.length < 2))
|
|
1487
|
-
fail(`${
|
|
1488
|
-
if (
|
|
1992
|
+
fail(`${layer2.type} requires at least two [x,y,z] points`);
|
|
1993
|
+
if (layer2.type === "ramp" && (!params.from || !params.to)) fail("ramp requires from and to");
|
|
1489
1994
|
for (const key of ["radius", "size"]) {
|
|
1490
1995
|
const value = params[key];
|
|
1491
1996
|
if (value === void 0) continue;
|
|
@@ -1494,7 +1999,7 @@ function validateLayer(layer) {
|
|
|
1494
1999
|
for (const entry of value) num(entry, key, 1e-3, 1e6);
|
|
1495
2000
|
} else num(value, key, 1e-3, 1e6);
|
|
1496
2001
|
}
|
|
1497
|
-
if (Array.isArray(params.radius) && !["stamp", "paste"].includes(
|
|
2002
|
+
if (Array.isArray(params.radius) && !["stamp", "paste"].includes(layer2.type))
|
|
1498
2003
|
fail("Brush radius must be one number");
|
|
1499
2004
|
for (const key of [
|
|
1500
2005
|
"amplitude",
|
|
@@ -1507,7 +2012,7 @@ function validateLayer(layer) {
|
|
|
1507
2012
|
"level",
|
|
1508
2013
|
"rotation"
|
|
1509
2014
|
])
|
|
1510
|
-
if (params[key] !== void 0 && !(key === "base" &&
|
|
2015
|
+
if (params[key] !== void 0 && !(key === "base" && layer2.type === "materials")) {
|
|
1511
2016
|
if (params[key] !== null || key !== "height") num(params[key], key);
|
|
1512
2017
|
}
|
|
1513
2018
|
for (const key of ["width", "step"])
|
|
@@ -1535,7 +2040,8 @@ function validateLayer(layer) {
|
|
|
1535
2040
|
"rate",
|
|
1536
2041
|
"value",
|
|
1537
2042
|
"waterWidth",
|
|
1538
|
-
"maxGrade"
|
|
2043
|
+
"maxGrade",
|
|
2044
|
+
"rain"
|
|
1539
2045
|
])
|
|
1540
2046
|
if (params[key] !== void 0) num(params[key], key, 0, 1);
|
|
1541
2047
|
if (params.spacing !== void 0) num(params.spacing, "spacing", 0.01, 2);
|
|
@@ -1543,12 +2049,12 @@ function validateLayer(layer) {
|
|
|
1543
2049
|
num(
|
|
1544
2050
|
params.strength,
|
|
1545
2051
|
"strength",
|
|
1546
|
-
|
|
1547
|
-
|
|
2052
|
+
layer2.type === "sculpt" ? -1e4 : 0,
|
|
2053
|
+
layer2.type === "sculpt" ? 1e4 : 1
|
|
1548
2054
|
);
|
|
1549
2055
|
if (params.scale !== void 0) {
|
|
1550
2056
|
const scale = params.scale;
|
|
1551
|
-
if (
|
|
2057
|
+
if (layer2.type === "scatter") {
|
|
1552
2058
|
if (Array.isArray(scale)) {
|
|
1553
2059
|
vec(scale, "scale", 2);
|
|
1554
2060
|
if (scale[0] > scale[1]) fail("scale range is inverted");
|
|
@@ -1566,6 +2072,13 @@ function validateLayer(layer) {
|
|
|
1566
2072
|
if (params[key] !== void 0) num(params[key], key, key === "count" ? 0 : 1, max, true);
|
|
1567
2073
|
if (params.persistence !== void 0) num(params.persistence, "persistence", 0.01, 1);
|
|
1568
2074
|
if (params.lacunarity !== void 0) num(params.lacunarity, "lacunarity", 1, 4);
|
|
2075
|
+
for (const [key, min, max] of [
|
|
2076
|
+
["uplift", 1e-9, 1],
|
|
2077
|
+
["erodibility", 1e-9, 1],
|
|
2078
|
+
["timeStep", 1, 1e7],
|
|
2079
|
+
["relief", 1, 1e5]
|
|
2080
|
+
])
|
|
2081
|
+
if (params[key] !== void 0) num(params[key], key, min, max);
|
|
1569
2082
|
for (const key of ["minSlope", "maxSlope", "talus"])
|
|
1570
2083
|
if (params[key] !== void 0) num(params[key], key, 0, key === "talus" ? 89.9 : 90);
|
|
1571
2084
|
if (params.minHeight !== void 0 && params.maxHeight !== void 0 && params.minHeight > params.maxHeight)
|
|
@@ -1573,7 +2086,7 @@ function validateLayer(layer) {
|
|
|
1573
2086
|
if (params.minSlope !== void 0 && params.maxSlope !== void 0 && params.minSlope > params.maxSlope)
|
|
1574
2087
|
fail("slope range is inverted");
|
|
1575
2088
|
if (params.material !== void 0) enumeration(params.material, "material", MATERIAL_IDS);
|
|
1576
|
-
if (
|
|
2089
|
+
if (layer2.type === "materials") {
|
|
1577
2090
|
if (params.base !== void 0) enumeration(params.base, "base", MATERIAL_IDS);
|
|
1578
2091
|
const rules = params.rules;
|
|
1579
2092
|
if (!Array.isArray(rules) || rules.length > 64) fail("materials requires a rules array");
|
|
@@ -1583,24 +2096,25 @@ function validateLayer(layer) {
|
|
|
1583
2096
|
if (rule.strength !== void 0) num(rule.strength, "rule.strength", 0, 1);
|
|
1584
2097
|
}
|
|
1585
2098
|
}
|
|
1586
|
-
if (params.method !== void 0)
|
|
2099
|
+
if (params.method !== void 0)
|
|
2100
|
+
enumeration(params.method, "method", ["thermal", "hydraulic", "fluvial", "gully", "scree"]);
|
|
1587
2101
|
if (params.mode !== void 0) enumeration(params.mode, "mode", ["fbm", "ridged", "billow"]);
|
|
1588
2102
|
if (params.shape !== void 0)
|
|
1589
2103
|
enumeration(
|
|
1590
2104
|
params.shape,
|
|
1591
2105
|
"shape",
|
|
1592
|
-
["stamp"].includes(
|
|
2106
|
+
["stamp"].includes(layer2.type) ? ["mountain", "crater", "ridge", "valley", "mesa", "dune"] : ["circle", "square"]
|
|
1593
2107
|
);
|
|
1594
2108
|
if (params.blend !== void 0)
|
|
1595
2109
|
enumeration(
|
|
1596
2110
|
params.blend,
|
|
1597
2111
|
"blend",
|
|
1598
|
-
["noise", "heightmap"].includes(
|
|
2112
|
+
["noise", "heightmap"].includes(layer2.type) ? ["add", "replace"] : ["add", "replace", "min", "max"]
|
|
1599
2113
|
);
|
|
1600
|
-
if (
|
|
2114
|
+
if (layer2.type === "clear")
|
|
1601
2115
|
enumeration(params.target, "clear.target", ["scatter", "paint", "biome", "all"]);
|
|
1602
|
-
if (
|
|
1603
|
-
if (
|
|
2116
|
+
if (layer2.type === "water") enumeration(params.kind ?? "lake", "water.kind", ["lake", "ocean"]);
|
|
2117
|
+
if (layer2.type === "river" && params.enforceDownhill && Array.isArray(points)) {
|
|
1604
2118
|
const river = points;
|
|
1605
2119
|
for (let i = 1; i < river.length; i += 1) {
|
|
1606
2120
|
const next = river[i];
|
|
@@ -1611,8 +2125,12 @@ function validateLayer(layer) {
|
|
|
1611
2125
|
}
|
|
1612
2126
|
if (params.name !== void 0) identifier(params.name, "name");
|
|
1613
2127
|
if (params.asset !== void 0) identifier(params.asset, "asset");
|
|
1614
|
-
if (
|
|
1615
|
-
fail(
|
|
2128
|
+
if (layer2.type === "scatter" && typeof params.asset !== "string")
|
|
2129
|
+
fail(
|
|
2130
|
+
'scatter requires an asset identifier; a document saved before `asset` was mandatory defaulted it to "pine", so add "asset": "pine" (or the asset it should place) to this layer'
|
|
2131
|
+
);
|
|
2132
|
+
if (["heightmap", "paste"].includes(layer2.type) && !params.data)
|
|
2133
|
+
fail(`${layer2.type} requires heightmap data`);
|
|
1616
2134
|
const data = params.data;
|
|
1617
2135
|
if (data) {
|
|
1618
2136
|
num(data.width, "data.width", 2, 4097, true);
|
|
@@ -1628,7 +2146,7 @@ function validateDocument(doc) {
|
|
|
1628
2146
|
finiteTree(
|
|
1629
2147
|
Array.isArray(doc.layers) ? {
|
|
1630
2148
|
...doc,
|
|
1631
|
-
layers: doc.layers.map((
|
|
2149
|
+
layers: doc.layers.map((layer2) => omittedElevations(layer2, layerFollowsTerrain(layer2)))
|
|
1632
2150
|
} : doc,
|
|
1633
2151
|
"recipe"
|
|
1634
2152
|
);
|
|
@@ -1637,10 +2155,10 @@ function validateDocument(doc) {
|
|
|
1637
2155
|
if (!Array.isArray(doc.layers) || doc.layers.length > 512)
|
|
1638
2156
|
fail("layers must be an array of at most 512 layers");
|
|
1639
2157
|
const ids = /* @__PURE__ */ new Set();
|
|
1640
|
-
for (const
|
|
1641
|
-
validateLayer(
|
|
1642
|
-
if (ids.has(
|
|
1643
|
-
ids.add(
|
|
2158
|
+
for (const layer2 of doc.layers) {
|
|
2159
|
+
validateLayer(layer2);
|
|
2160
|
+
if (ids.has(layer2.id)) fail(`Duplicate layer ID: ${layer2.id}`);
|
|
2161
|
+
ids.add(layer2.id);
|
|
1644
2162
|
}
|
|
1645
2163
|
}
|
|
1646
2164
|
|
|
@@ -1662,9 +2180,9 @@ var TerrainEvaluator = class {
|
|
|
1662
2180
|
validateDocument(doc);
|
|
1663
2181
|
validateConfig({ ...doc.config, resolution });
|
|
1664
2182
|
const keys = [];
|
|
1665
|
-
let key = JSON.stringify({ ...doc.config, resolution });
|
|
1666
|
-
for (const
|
|
1667
|
-
key += `|${JSON.stringify(
|
|
2183
|
+
let key = this.limit > 0 ? JSON.stringify({ ...doc.config, resolution }) : "";
|
|
2184
|
+
for (const layer2 of this.limit > 0 ? doc.layers : []) {
|
|
2185
|
+
key += `|${JSON.stringify(layer2)}`;
|
|
1668
2186
|
keys.push(key);
|
|
1669
2187
|
}
|
|
1670
2188
|
let state;
|
|
@@ -1683,9 +2201,9 @@ var TerrainEvaluator = class {
|
|
|
1683
2201
|
state ??= createState(doc.config, resolution);
|
|
1684
2202
|
for (let i = start; i < doc.layers.length; i++) {
|
|
1685
2203
|
signal?.throwIfAborted();
|
|
1686
|
-
const
|
|
1687
|
-
applyOperation(state,
|
|
1688
|
-
onProgress?.({ index: i, total: doc.layers.length, id:
|
|
2204
|
+
const layer2 = doc.layers[i];
|
|
2205
|
+
applyOperation(state, layer2);
|
|
2206
|
+
onProgress?.({ index: i, total: doc.layers.length, id: layer2.id });
|
|
1689
2207
|
const bytes = state.height.byteLength + state.splat.byteLength + Object.values(state.erosion ?? {}).reduce((n, a) => n + a.byteLength, 0) + Object.values(state.biomes).reduce((n, a) => n + a.byteLength, 0);
|
|
1690
2208
|
if (bytes <= this.limit) {
|
|
1691
2209
|
while (this.bytes + bytes > this.limit && this.cache.size) {
|
|
@@ -1779,15 +2297,15 @@ var Terrain = class _Terrain {
|
|
|
1779
2297
|
}
|
|
1780
2298
|
_id(type) {
|
|
1781
2299
|
let i = 1;
|
|
1782
|
-
while (this._doc.layers.some((
|
|
2300
|
+
while (this._doc.layers.some((layer2) => layer2.id === `${type}-${i}`)) i++;
|
|
1783
2301
|
return `${type}-${i}`;
|
|
1784
2302
|
}
|
|
1785
|
-
add(
|
|
1786
|
-
const next = { enabled: true, opacity: 1, ...structuredClone(
|
|
2303
|
+
add(layer2) {
|
|
2304
|
+
const next = { enabled: true, opacity: 1, ...structuredClone(layer2) };
|
|
1787
2305
|
next.id ??= this._id(next.type);
|
|
1788
2306
|
validateLayer(next);
|
|
1789
2307
|
return this._change(() => {
|
|
1790
|
-
const i = this._doc.layers.findIndex((
|
|
2308
|
+
const i = this._doc.layers.findIndex((layer3) => layer3.id === next.id);
|
|
1791
2309
|
if (i < 0) this._doc.layers.push(next);
|
|
1792
2310
|
else this._doc.layers[i] = next;
|
|
1793
2311
|
});
|
|
@@ -1798,18 +2316,18 @@ var Terrain = class _Terrain {
|
|
|
1798
2316
|
const data = params.data;
|
|
1799
2317
|
Object.assign(params, { data: { ...data, values: Array.from(data.values) } });
|
|
1800
2318
|
}
|
|
1801
|
-
const
|
|
2319
|
+
const layer2 = { id: id ?? this._id(type), type, params };
|
|
1802
2320
|
const optional = Object.fromEntries(
|
|
1803
2321
|
Object.entries({ name: label, enabled, opacity, mask }).filter(
|
|
1804
2322
|
([, value]) => value !== void 0
|
|
1805
2323
|
)
|
|
1806
2324
|
);
|
|
1807
|
-
return this.add(Object.assign(
|
|
2325
|
+
return this.add(Object.assign(layer2, optional));
|
|
1808
2326
|
}
|
|
1809
2327
|
layer(id) {
|
|
1810
|
-
const
|
|
1811
|
-
if (!
|
|
1812
|
-
return structuredClone(
|
|
2328
|
+
const layer2 = this._doc.layers.find((layer3) => layer3.id === id);
|
|
2329
|
+
if (!layer2) throw new Error(`Unknown layer '${id}'`);
|
|
2330
|
+
return structuredClone(layer2);
|
|
1813
2331
|
}
|
|
1814
2332
|
update(id, patch) {
|
|
1815
2333
|
const old = this.layer(id);
|
|
@@ -1829,16 +2347,16 @@ var Terrain = class _Terrain {
|
|
|
1829
2347
|
if (!Number.isInteger(index) || index < 0 || index >= this._doc.layers.length)
|
|
1830
2348
|
throw new RangeError("Layer index out of bounds");
|
|
1831
2349
|
return this._change(() => {
|
|
1832
|
-
const i = this._doc.layers.findIndex((
|
|
2350
|
+
const i = this._doc.layers.findIndex((layer3) => layer3.id === id);
|
|
1833
2351
|
if (i < 0) throw new Error(`Unknown layer '${id}'`);
|
|
1834
|
-
const
|
|
1835
|
-
this._doc.layers.splice(index, 0,
|
|
2352
|
+
const layer2 = this._doc.layers.splice(i, 1)[0];
|
|
2353
|
+
this._doc.layers.splice(index, 0, layer2);
|
|
1836
2354
|
});
|
|
1837
2355
|
}
|
|
1838
2356
|
remove(id) {
|
|
1839
2357
|
this.layer(id);
|
|
1840
2358
|
return this._change(() => {
|
|
1841
|
-
this._doc.layers = this._doc.layers.filter((
|
|
2359
|
+
this._doc.layers = this._doc.layers.filter((layer2) => layer2.id !== id);
|
|
1842
2360
|
});
|
|
1843
2361
|
}
|
|
1844
2362
|
setConfig(patch) {
|
|
@@ -2000,7 +2518,7 @@ var Terrain = class _Terrain {
|
|
|
2000
2518
|
biome(options = {}) {
|
|
2001
2519
|
return this._operation("biome", options);
|
|
2002
2520
|
}
|
|
2003
|
-
scatter(options
|
|
2521
|
+
scatter(options) {
|
|
2004
2522
|
return this._operation("scatter", options);
|
|
2005
2523
|
}
|
|
2006
2524
|
clear(options) {
|
|
@@ -2023,120 +2541,2132 @@ var Terrain = class _Terrain {
|
|
|
2023
2541
|
}
|
|
2024
2542
|
};
|
|
2025
2543
|
|
|
2026
|
-
// src/core/
|
|
2027
|
-
var
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2544
|
+
// src/core/presets.ts
|
|
2545
|
+
var LANDFORMS = {
|
|
2546
|
+
alpine: {
|
|
2547
|
+
summary: "Glacier-cut granite peaks: steep headwalls, sharp ar\xEAtes, dendritic gullies.",
|
|
2548
|
+
reliefRatio: 0.25,
|
|
2549
|
+
reference: "Longs Peak, Rocky Mountain National Park, Colorado (USGS 3DEP, DEM crop `alpine`)",
|
|
2550
|
+
target: { slopeP50: 27.3, slopeP90: 42.5, hypsometricIntegral: 0.452, curvatureKurtosis: 89.3 }
|
|
2551
|
+
},
|
|
2552
|
+
temperateMountains: {
|
|
2553
|
+
summary: "Forested mountain backdrop: rounded massifs cut by branching valleys.",
|
|
2554
|
+
reliefRatio: 0.17,
|
|
2555
|
+
reference: "Sprague Lake / Glacier Creek, Rocky Mountain National Park (DEM crop `forest`)",
|
|
2556
|
+
target: { slopeP50: 14.7, slopeP90: 28.1, hypsometricIntegral: 0.398, curvatureKurtosis: 13.7 }
|
|
2557
|
+
},
|
|
2558
|
+
rollingHills: {
|
|
2559
|
+
summary: "Low dendritic hills with convex ridges and gentle slopes.",
|
|
2560
|
+
reliefRatio: 0.03,
|
|
2561
|
+
reference: "Driftless Area, Wisconsin: fluvially dissected flat-lying bedrock, local relief up to about 115 m (WGNHS county reports); no DEM crop in the repo, so no measured target"
|
|
2562
|
+
},
|
|
2563
|
+
desertMesa: {
|
|
2564
|
+
summary: "Caprock mesas and buttes over a stepped, cliff-and-bench canyon landscape.",
|
|
2565
|
+
reliefRatio: 0.055,
|
|
2566
|
+
reference: "Valley of the Gods, Utah (DEM crop `desert`)",
|
|
2567
|
+
target: { slopeP50: 3.7, slopeP90: 16.9, hypsometricIntegral: 0.276, curvatureKurtosis: 203.9 }
|
|
2568
|
+
},
|
|
2569
|
+
coastalCliffs: {
|
|
2570
|
+
summary: "Island massif with steep rocky shore and drowned gullies.",
|
|
2571
|
+
reliefRatio: 0.075,
|
|
2572
|
+
reference: "Sand Beach / Great Head, Acadia National Park, Maine (DEM crop `coastal`)",
|
|
2573
|
+
target: { slopeP50: 1.6, slopeP90: 19.4, hypsometricIntegral: 0.11, curvatureKurtosis: 34.7 }
|
|
2574
|
+
},
|
|
2575
|
+
tundra: {
|
|
2576
|
+
summary: "Broad rounded uplands above the tree line with shallow glacial valleys.",
|
|
2577
|
+
reliefRatio: 0.15,
|
|
2578
|
+
reference: "Trail Ridge, Rocky Mountain National Park (DEM crop `tundra`)",
|
|
2579
|
+
target: { slopeP50: 17.1, slopeP90: 27.9, hypsometricIntegral: 0.473, curvatureKurtosis: 16.7 }
|
|
2580
|
+
},
|
|
2581
|
+
volcanic: {
|
|
2582
|
+
summary: "Stratovolcano cone with a summit crater and radial gullies.",
|
|
2583
|
+
reliefRatio: 0.18,
|
|
2584
|
+
reference: "Mayon Volcano, Philippines: summit 2469 m, slope about 1:2.5 above 500 m easing to 1:7.8 and 1:19.9 (JICA hazard report); no DEM crop in the repo, so no measured target"
|
|
2061
2585
|
}
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
const
|
|
2065
|
-
|
|
2066
|
-
const
|
|
2067
|
-
const
|
|
2068
|
-
const
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2586
|
+
};
|
|
2587
|
+
function landform(kind, options = {}) {
|
|
2588
|
+
const info = LANDFORMS[kind];
|
|
2589
|
+
if (!info) throw new TypeError(`Unknown landform kind: ${String(kind)}`);
|
|
2590
|
+
const { size = 5e3, resolution = 513, seed = 73 } = options;
|
|
2591
|
+
const s = size;
|
|
2592
|
+
const relief = options.relief ?? info.reliefRatio * s;
|
|
2593
|
+
const t = new Terrain({ size, resolution, seed });
|
|
2594
|
+
const uplift = (u) => u * (s / 5e3);
|
|
2595
|
+
const valleys = (u, talus, iterations) => ({
|
|
2596
|
+
id: "valleys",
|
|
2597
|
+
method: "fluvial",
|
|
2598
|
+
iterations,
|
|
2599
|
+
uplift: uplift(u),
|
|
2600
|
+
erodibility: 1e-5,
|
|
2601
|
+
talus,
|
|
2602
|
+
// Rescales the eroded surface, so the finished relief follows the request at any seed.
|
|
2603
|
+
relief
|
|
2604
|
+
});
|
|
2605
|
+
switch (kind) {
|
|
2606
|
+
case "alpine":
|
|
2607
|
+
t.noise({
|
|
2608
|
+
id: "massif",
|
|
2609
|
+
amplitude: relief * 3.01,
|
|
2610
|
+
scale: s * 0.535,
|
|
2611
|
+
octaves: 4,
|
|
2612
|
+
mode: "ridged",
|
|
2613
|
+
warp: s * 0.0706
|
|
2614
|
+
}).stamp({
|
|
2615
|
+
id: "peak",
|
|
2616
|
+
shape: "mountain",
|
|
2617
|
+
at: [0, 0],
|
|
2618
|
+
radius: s * 0.42,
|
|
2619
|
+
amplitude: relief * 7.83,
|
|
2620
|
+
roughness: 0.5
|
|
2621
|
+
}).erode(valleys(15e-5, 60.5, 49)).erode({ id: "talus", method: "thermal", iterations: 13, talus: 26.8 }).smooth({ id: "grain", iterations: 3 });
|
|
2622
|
+
break;
|
|
2623
|
+
case "temperateMountains":
|
|
2624
|
+
t.noise({
|
|
2625
|
+
id: "massif",
|
|
2626
|
+
amplitude: relief * 2.18,
|
|
2627
|
+
scale: s * 0.988,
|
|
2628
|
+
octaves: 4,
|
|
2629
|
+
mode: "fbm",
|
|
2630
|
+
warp: s * 0.0716
|
|
2631
|
+
}).stamp({
|
|
2632
|
+
id: "peak",
|
|
2633
|
+
shape: "mountain",
|
|
2634
|
+
at: [0, 0],
|
|
2635
|
+
radius: s * 0.42,
|
|
2636
|
+
amplitude: relief * 4.5,
|
|
2637
|
+
roughness: 0.5
|
|
2638
|
+
}).erode(valleys(413e-7, 30.2, 43)).smooth({ id: "grain", iterations: 3 });
|
|
2639
|
+
break;
|
|
2640
|
+
case "rollingHills":
|
|
2641
|
+
t.noise({
|
|
2642
|
+
id: "hills",
|
|
2643
|
+
amplitude: relief * 1.6,
|
|
2644
|
+
scale: s * 0.25,
|
|
2645
|
+
octaves: 4,
|
|
2646
|
+
mode: "fbm",
|
|
2647
|
+
warp: s * 0.05
|
|
2648
|
+
}).erode(valleys(1e-5, 12, 40)).smooth({ id: "grain", iterations: 2 });
|
|
2649
|
+
break;
|
|
2650
|
+
case "desertMesa":
|
|
2651
|
+
t.noise({
|
|
2652
|
+
id: "plateau",
|
|
2653
|
+
amplitude: relief * 1.75,
|
|
2654
|
+
scale: s * 0.447,
|
|
2655
|
+
octaves: 3,
|
|
2656
|
+
mode: "fbm",
|
|
2657
|
+
warp: s * 0.12
|
|
2658
|
+
}).erode(valleys(837e-6, 33.4, 28)).terrace({ id: "caprock", step: relief / 6, softness: 0.15, strength: 0.688 }).erode({ id: "talus", method: "thermal", iterations: 16, talus: 25 }).smooth({ id: "grain", iterations: 1 });
|
|
2659
|
+
break;
|
|
2660
|
+
case "coastalCliffs":
|
|
2661
|
+
t.noise({
|
|
2662
|
+
id: "island",
|
|
2663
|
+
amplitude: relief * 1.11,
|
|
2664
|
+
scale: s * 0.962,
|
|
2665
|
+
octaves: 6,
|
|
2666
|
+
mode: "ridged",
|
|
2667
|
+
warp: s * 0.0456,
|
|
2668
|
+
island: true,
|
|
2669
|
+
coastDepth: relief * 0.111
|
|
2670
|
+
}).erode(valleys(775e-7, 64.8, 55)).erode({ id: "talus", method: "thermal", iterations: 8, talus: 32.7 }).smooth({ id: "grain", iterations: 3 });
|
|
2671
|
+
break;
|
|
2672
|
+
case "tundra":
|
|
2673
|
+
t.noise({
|
|
2674
|
+
id: "uplands",
|
|
2675
|
+
amplitude: relief * 3.59,
|
|
2676
|
+
scale: s * 0.837,
|
|
2677
|
+
octaves: 4,
|
|
2678
|
+
mode: "fbm",
|
|
2679
|
+
warp: s * 0.097
|
|
2680
|
+
}).stamp({
|
|
2681
|
+
id: "ridge",
|
|
2682
|
+
shape: "mountain",
|
|
2683
|
+
at: [0, 0],
|
|
2684
|
+
radius: s * 0.42,
|
|
2685
|
+
amplitude: relief * 3.95,
|
|
2686
|
+
roughness: 0.5
|
|
2687
|
+
}).erode(valleys(869e-7, 30.1, 30)).erode({ id: "talus", method: "thermal", iterations: 8, talus: 26.2 });
|
|
2688
|
+
break;
|
|
2689
|
+
case "volcanic":
|
|
2690
|
+
t.noise({ id: "rubble", amplitude: relief * 0.04, scale: s * 0.1, octaves: 4, mode: "fbm" }).stamp({
|
|
2691
|
+
id: "cone",
|
|
2692
|
+
shape: "mountain",
|
|
2693
|
+
at: [0, 0],
|
|
2694
|
+
radius: s * 0.45,
|
|
2695
|
+
amplitude: relief,
|
|
2696
|
+
roughness: 0.15
|
|
2697
|
+
}).stamp({
|
|
2698
|
+
id: "crater",
|
|
2699
|
+
shape: "crater",
|
|
2700
|
+
at: [0, 0],
|
|
2701
|
+
radius: s * 0.07,
|
|
2702
|
+
amplitude: relief * 0.08,
|
|
2703
|
+
blend: "add"
|
|
2704
|
+
}).erode(valleys(3e-4, 38, 15)).erode({ id: "talus", method: "thermal", iterations: 8, talus: 36 }).smooth({ id: "grain", iterations: 1 });
|
|
2705
|
+
break;
|
|
2706
|
+
}
|
|
2707
|
+
return t;
|
|
2708
|
+
}
|
|
2709
|
+
function landformBackdrop(kind, options = {}) {
|
|
2710
|
+
const { size = 5e3, resolution = 513, seed = 73, centerHeight = 0 } = options;
|
|
2711
|
+
const wide = 2 * (resolution - 1) + 1;
|
|
2712
|
+
if (!RESOLUTIONS.includes(wide))
|
|
2713
|
+
throw new RangeError(`Backdrop resolution ${resolution} needs a ${wide}-sample source`);
|
|
2714
|
+
const relief = options.relief ?? LANDFORMS[kind].reliefRatio * size;
|
|
2715
|
+
const source = landform(kind, {
|
|
2716
|
+
size: 2 * size,
|
|
2717
|
+
resolution: wide,
|
|
2718
|
+
seed,
|
|
2719
|
+
relief: relief * 1.4
|
|
2720
|
+
}).evaluate();
|
|
2721
|
+
const h = source.height;
|
|
2722
|
+
const half = (resolution - 1) / 2;
|
|
2723
|
+
const stride = 8;
|
|
2724
|
+
let best = Number.POSITIVE_INFINITY;
|
|
2725
|
+
let bx = half;
|
|
2726
|
+
let bz = half;
|
|
2727
|
+
for (let cz = half; cz <= wide - 1 - half; cz += stride)
|
|
2728
|
+
for (let cx = half; cx <= wide - 1 - half; cx += stride) {
|
|
2729
|
+
let lo = Number.POSITIVE_INFINITY;
|
|
2730
|
+
let hi = Number.NEGATIVE_INFINITY;
|
|
2731
|
+
for (let z = cz - half; z <= cz + half; z += 2 * stride)
|
|
2732
|
+
for (let x = cx - half; x <= cx + half; x += 2 * stride) {
|
|
2733
|
+
const v = h[z * wide + x];
|
|
2734
|
+
lo = Math.min(lo, v);
|
|
2735
|
+
hi = Math.max(hi, v);
|
|
2086
2736
|
}
|
|
2087
|
-
|
|
2737
|
+
const centre2 = h[cz * wide + cx];
|
|
2738
|
+
const rough = Math.abs(h[cz * wide + cx + 8] - centre2) + Math.abs(h[(cz + 8) * wide + cx] - centre2);
|
|
2739
|
+
const score = (centre2 - lo) / (hi - lo || 1) + rough / (hi - lo || 1);
|
|
2740
|
+
if (score < best) {
|
|
2741
|
+
best = score;
|
|
2742
|
+
bx = cx;
|
|
2743
|
+
bz = cz;
|
|
2744
|
+
}
|
|
2088
2745
|
}
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
const
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2746
|
+
const heights = new Float32Array(resolution * resolution);
|
|
2747
|
+
let low = Number.POSITIVE_INFINITY;
|
|
2748
|
+
let high = Number.NEGATIVE_INFINITY;
|
|
2749
|
+
for (let z = 0; z < resolution; z += 1)
|
|
2750
|
+
for (let x = 0; x < resolution; x += 1) {
|
|
2751
|
+
const v = h[(bz - half + z) * wide + bx - half + x];
|
|
2752
|
+
heights[z * resolution + x] = v;
|
|
2753
|
+
low = Math.min(low, v);
|
|
2754
|
+
high = Math.max(high, v);
|
|
2098
2755
|
}
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2756
|
+
const gain = relief / (high - low || 1);
|
|
2757
|
+
const centre = heights[half * resolution + half];
|
|
2758
|
+
for (let i = 0; i < heights.length; i += 1)
|
|
2759
|
+
heights[i] = centerHeight + (heights[i] - centre) * gain;
|
|
2760
|
+
return { size, resolution, heights };
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
// src/core/biome.ts
|
|
2764
|
+
var SPEC_FIELDS = [
|
|
2765
|
+
"look",
|
|
2766
|
+
"bake",
|
|
2767
|
+
"landform",
|
|
2768
|
+
"sizeMeters",
|
|
2769
|
+
"seed",
|
|
2770
|
+
"stream",
|
|
2771
|
+
"cover",
|
|
2772
|
+
"foliage",
|
|
2773
|
+
"grass",
|
|
2774
|
+
"layers",
|
|
2775
|
+
"water",
|
|
2776
|
+
"spawn",
|
|
2777
|
+
"species",
|
|
2778
|
+
"aliases"
|
|
2779
|
+
];
|
|
2780
|
+
var COVER_FIELDS = [
|
|
2781
|
+
"ground",
|
|
2782
|
+
"seed",
|
|
2783
|
+
"attempts",
|
|
2784
|
+
"stoneRadius",
|
|
2785
|
+
"clearings",
|
|
2786
|
+
"understorey"
|
|
2787
|
+
];
|
|
2788
|
+
var UNDERSTOREY_FIELDS = [
|
|
2789
|
+
"attempts",
|
|
2790
|
+
"litter",
|
|
2791
|
+
"fern",
|
|
2792
|
+
"bush",
|
|
2793
|
+
"flower",
|
|
2794
|
+
"deadfall",
|
|
2795
|
+
"shade",
|
|
2796
|
+
"shadeRadius"
|
|
2797
|
+
];
|
|
2798
|
+
var COVER_GROUNDS = ["wood", "crag", "mesa", "mats"];
|
|
2799
|
+
var STREAMS = ["ordered", "seeded"];
|
|
2800
|
+
var DRIFT_FIELDS = ["field", "frequency", "offset", "amplitude", "band"];
|
|
2801
|
+
var DRIFT_FIELDS_NAMES = ["stand", "mats"];
|
|
2802
|
+
var FOLIAGE_FIELDS = [
|
|
2803
|
+
"species",
|
|
2804
|
+
"density",
|
|
2805
|
+
"slope",
|
|
2806
|
+
"height",
|
|
2807
|
+
"spacing",
|
|
2808
|
+
"collision",
|
|
2809
|
+
"stands",
|
|
2810
|
+
"grassThreshold",
|
|
2811
|
+
"standThreshold",
|
|
2812
|
+
"far"
|
|
2813
|
+
];
|
|
2814
|
+
var FAR_FIELDS = [
|
|
2815
|
+
"cell",
|
|
2816
|
+
"spacing",
|
|
2817
|
+
"spacingAt",
|
|
2818
|
+
"slope",
|
|
2819
|
+
"meadowSlope",
|
|
2820
|
+
"reach",
|
|
2821
|
+
"treelineJitter",
|
|
2822
|
+
"stand",
|
|
2823
|
+
"seed",
|
|
2824
|
+
"impostor"
|
|
2825
|
+
];
|
|
2826
|
+
var GRASS_FIELDS = [
|
|
2827
|
+
"species",
|
|
2828
|
+
"density",
|
|
2829
|
+
"slope",
|
|
2830
|
+
"height",
|
|
2831
|
+
"spacing",
|
|
2832
|
+
"slopeFalloff",
|
|
2833
|
+
"heightFalloff",
|
|
2834
|
+
"mats",
|
|
2835
|
+
"waterMargin",
|
|
2836
|
+
"beach",
|
|
2837
|
+
"detailSpacing",
|
|
2838
|
+
"drift",
|
|
2839
|
+
"bladeScale",
|
|
2840
|
+
"bladeJitter",
|
|
2841
|
+
"scrubChance",
|
|
2842
|
+
"scrubScale",
|
|
2843
|
+
"scrubJitter",
|
|
2844
|
+
"scrubBush",
|
|
2845
|
+
"shoreGround",
|
|
2846
|
+
"cobbledBed"
|
|
2847
|
+
];
|
|
2848
|
+
var BAKE_FIELDS = ["file", "key"];
|
|
2849
|
+
var LAYER_FIELDS = ["layer", "from", "to", "shedFrom", "shedTo"];
|
|
2850
|
+
var LAYER_REQUIRED = ["layer", "from", "to"];
|
|
2851
|
+
var WATER_FIELDS = ["kind", "width", "radius", "depth", "level"];
|
|
2852
|
+
var WATER_REQUIRED = ["kind"];
|
|
2853
|
+
var SPAWN_FIELDS = ["at", "cameraOffset", "focus"];
|
|
2854
|
+
var LANDFORM_KINDS = Object.keys(LANDFORMS);
|
|
2855
|
+
var COLLISION_CLASSES = ["none", "trunk", "hull"];
|
|
2856
|
+
var WATER_KINDS = ["river", "lake", "ocean"];
|
|
2857
|
+
function fail2(biome, field, problem) {
|
|
2858
|
+
throw new TypeError(`${biome}: biome field '${field}' ${problem}.`);
|
|
2859
|
+
}
|
|
2860
|
+
function closed(value, biome, field, fields, required = fields) {
|
|
2861
|
+
const at = (name) => field === "" ? name : `${field}.${name}`;
|
|
2862
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
2863
|
+
fail2(biome, field || "<file>", "must be an object");
|
|
2864
|
+
const record = value;
|
|
2865
|
+
for (const key of Object.keys(record))
|
|
2866
|
+
if (!fields.includes(key)) fail2(biome, at(key), "is not a field a biome has");
|
|
2867
|
+
for (const key of required) if (!(key in record)) fail2(biome, at(key), "is missing");
|
|
2868
|
+
return record;
|
|
2869
|
+
}
|
|
2870
|
+
function count(value, biome, field, min = 0) {
|
|
2871
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
2872
|
+
fail2(biome, field, "must be a finite number");
|
|
2873
|
+
if (value < min) fail2(biome, field, `must be at least ${min}`);
|
|
2874
|
+
return value;
|
|
2875
|
+
}
|
|
2876
|
+
function positive(value, biome, field) {
|
|
2877
|
+
const number = count(value, biome, field);
|
|
2878
|
+
if (number === 0) fail2(biome, field, "must be greater than 0");
|
|
2879
|
+
return number;
|
|
2880
|
+
}
|
|
2881
|
+
function whole(value, biome, field, min = 0) {
|
|
2882
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value))
|
|
2883
|
+
fail2(biome, field, "must be a whole number");
|
|
2884
|
+
if (value < min) fail2(biome, field, `must be at least ${min}`);
|
|
2885
|
+
return value;
|
|
2886
|
+
}
|
|
2887
|
+
function share(value, biome, field) {
|
|
2888
|
+
const number = count(value, biome, field);
|
|
2889
|
+
if (number > 1) fail2(biome, field, "must be at most 1");
|
|
2890
|
+
return number;
|
|
2891
|
+
}
|
|
2892
|
+
function flag(value, biome, field) {
|
|
2893
|
+
if (typeof value !== "boolean") fail2(biome, field, "must be true or false");
|
|
2894
|
+
return value;
|
|
2895
|
+
}
|
|
2896
|
+
function text(value, biome, field) {
|
|
2897
|
+
if (typeof value !== "string" || value.trim() === "")
|
|
2898
|
+
fail2(biome, field, "must be a non-empty string");
|
|
2899
|
+
return value;
|
|
2900
|
+
}
|
|
2901
|
+
function oneOf(value, biome, field, options) {
|
|
2902
|
+
if (typeof value !== "string" || !options.includes(value))
|
|
2903
|
+
fail2(biome, field, `must be one of ${options.join(", ")}`);
|
|
2904
|
+
return value;
|
|
2905
|
+
}
|
|
2906
|
+
function intervalOf(value, biome, field, size, min = 0, max = Number.POSITIVE_INFINITY) {
|
|
2907
|
+
if (!Array.isArray(value) || value.length !== size)
|
|
2908
|
+
fail2(biome, field, `must be an array of ${size} numbers`);
|
|
2909
|
+
const numbers = value.map((entry, index) => count(entry, biome, `${field}[${index}]`, min));
|
|
2910
|
+
if (numbers[0] > numbers[1])
|
|
2911
|
+
fail2(biome, field, "must ascend");
|
|
2912
|
+
for (const [index, entry] of numbers.entries())
|
|
2913
|
+
if (entry > max) fail2(biome, `${field}[${index}]`, `must be at most ${max}`);
|
|
2914
|
+
return numbers;
|
|
2915
|
+
}
|
|
2916
|
+
function band(value, biome, field) {
|
|
2917
|
+
if (!Array.isArray(value) || value.length !== 2)
|
|
2918
|
+
fail2(biome, field, "must be an array of 2 numbers");
|
|
2919
|
+
const [threshold = 0, span = 0] = value.map(
|
|
2920
|
+
(entry, index) => share(entry, biome, `${field}[${index}]`)
|
|
2921
|
+
);
|
|
2922
|
+
if (span === 0) fail2(biome, `${field}[1]`, "must be greater than 0");
|
|
2923
|
+
return [threshold, span];
|
|
2924
|
+
}
|
|
2925
|
+
function pair(value, biome, field, min = 0, max = Number.POSITIVE_INFINITY) {
|
|
2926
|
+
const [first = 0, second = 0] = intervalOf(value, biome, field, 2, min, max);
|
|
2927
|
+
return [first, second];
|
|
2928
|
+
}
|
|
2929
|
+
function pointOf(value, biome, field, size) {
|
|
2930
|
+
if (!Array.isArray(value) || value.length !== size)
|
|
2931
|
+
fail2(biome, field, `must be an array of ${size} numbers`);
|
|
2932
|
+
return value.map((entry, index) => {
|
|
2933
|
+
if (typeof entry !== "number" || !Number.isFinite(entry))
|
|
2934
|
+
fail2(biome, `${field}[${index}]`, "must be a finite number");
|
|
2935
|
+
return entry ?? 0;
|
|
2936
|
+
});
|
|
2937
|
+
}
|
|
2938
|
+
function point2(value, biome, field) {
|
|
2939
|
+
const [x = 0, z = 0] = pointOf(value, biome, field, 2);
|
|
2940
|
+
return [x, z];
|
|
2941
|
+
}
|
|
2942
|
+
function point3(value, biome, field) {
|
|
2943
|
+
const [x = 0, y = 0, z = 0] = pointOf(value, biome, field, 3);
|
|
2944
|
+
return [x, y, z];
|
|
2945
|
+
}
|
|
2946
|
+
function far(value, biome, field) {
|
|
2947
|
+
if (value === null) return null;
|
|
2948
|
+
const far2 = closed(value, biome, field, FAR_FIELDS);
|
|
2111
2949
|
return {
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2950
|
+
cell: positive(far2.cell, biome, `${field}.cell`),
|
|
2951
|
+
spacing: pair(far2.spacing, biome, `${field}.spacing`),
|
|
2952
|
+
spacingAt: pair(far2.spacingAt, biome, `${field}.spacingAt`),
|
|
2953
|
+
slope: pair(far2.slope, biome, `${field}.slope`, 0, 90),
|
|
2954
|
+
meadowSlope: pair(far2.meadowSlope, biome, `${field}.meadowSlope`, 0, 90),
|
|
2955
|
+
reach: positive(far2.reach, biome, `${field}.reach`),
|
|
2956
|
+
treelineJitter: count(far2.treelineJitter, biome, `${field}.treelineJitter`),
|
|
2957
|
+
stand: pair(far2.stand, biome, `${field}.stand`, 0, 1),
|
|
2958
|
+
seed: whole(far2.seed, biome, `${field}.seed`),
|
|
2959
|
+
impostor: count(far2.impostor, biome, `${field}.impostor`)
|
|
2122
2960
|
};
|
|
2123
2961
|
}
|
|
2124
|
-
function
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2962
|
+
function frequencies(value, biome, field) {
|
|
2963
|
+
const [x = 0, z = 0] = pointOf(value, biome, field, 2);
|
|
2964
|
+
if (x <= 0 || z <= 0) fail2(biome, field, "must be two positive frequencies");
|
|
2965
|
+
return [x, z];
|
|
2966
|
+
}
|
|
2967
|
+
function driftRow(value, biome, field) {
|
|
2968
|
+
const row = closed(value, biome, field, DRIFT_FIELDS);
|
|
2969
|
+
return {
|
|
2970
|
+
field: oneOf(row.field, biome, `${field}.field`, DRIFT_FIELDS_NAMES),
|
|
2971
|
+
frequency: frequencies(row.frequency, biome, `${field}.frequency`),
|
|
2972
|
+
offset: share(row.offset, biome, `${field}.offset`),
|
|
2973
|
+
amplitude: share(row.amplitude, biome, `${field}.amplitude`),
|
|
2974
|
+
band: band(row.band, biome, `${field}.band`)
|
|
2975
|
+
};
|
|
2976
|
+
}
|
|
2977
|
+
function clearing(value, biome, field) {
|
|
2978
|
+
if (!Array.isArray(value) || value.length !== 3)
|
|
2979
|
+
fail2(biome, field, "must be an array of 3 numbers");
|
|
2980
|
+
const [x = 0, z = 0, radius = 0] = pointOf(value, biome, field, 3);
|
|
2981
|
+
if (radius <= 0) fail2(biome, `${field}[2]`, "must be greater than 0");
|
|
2982
|
+
return [x, z, radius];
|
|
2983
|
+
}
|
|
2984
|
+
function understorey(value, biome) {
|
|
2985
|
+
const row = closed(value, biome, "cover.understorey", UNDERSTOREY_FIELDS);
|
|
2986
|
+
return {
|
|
2987
|
+
attempts: whole(row.attempts, biome, "cover.understorey.attempts"),
|
|
2988
|
+
litter: count(row.litter, biome, "cover.understorey.litter"),
|
|
2989
|
+
fern: count(row.fern, biome, "cover.understorey.fern"),
|
|
2990
|
+
bush: count(row.bush, biome, "cover.understorey.bush"),
|
|
2991
|
+
flower: count(row.flower, biome, "cover.understorey.flower"),
|
|
2992
|
+
deadfall: count(row.deadfall, biome, "cover.understorey.deadfall"),
|
|
2993
|
+
shade: share(row.shade, biome, "cover.understorey.shade"),
|
|
2994
|
+
shadeRadius: count(row.shadeRadius, biome, "cover.understorey.shadeRadius")
|
|
2995
|
+
};
|
|
2996
|
+
}
|
|
2997
|
+
function cover(value, biome) {
|
|
2998
|
+
const row = closed(value, biome, "cover", COVER_FIELDS);
|
|
2999
|
+
const clearings = row.clearings;
|
|
3000
|
+
if (!Array.isArray(clearings)) fail2(biome, "cover.clearings", "must be an array");
|
|
3001
|
+
return {
|
|
3002
|
+
ground: oneOf(row.ground, biome, "cover.ground", COVER_GROUNDS),
|
|
3003
|
+
seed: whole(row.seed, biome, "cover.seed"),
|
|
3004
|
+
attempts: whole(row.attempts, biome, "cover.attempts", 1),
|
|
3005
|
+
stoneRadius: positive(row.stoneRadius, biome, "cover.stoneRadius"),
|
|
3006
|
+
clearings: clearings.map((entry, index) => clearing(entry, biome, `cover.clearings[${index}]`)),
|
|
3007
|
+
understorey: understorey(row.understorey, biome)
|
|
3008
|
+
};
|
|
3009
|
+
}
|
|
3010
|
+
function foliage(value, biome, field) {
|
|
3011
|
+
const row = closed(value, biome, field, FOLIAGE_FIELDS);
|
|
3012
|
+
const height = row.height === null ? null : pair(row.height, biome, `${field}.height`);
|
|
3013
|
+
return {
|
|
3014
|
+
species: text(row.species, biome, `${field}.species`),
|
|
3015
|
+
density: count(row.density, biome, `${field}.density`),
|
|
3016
|
+
slope: pair(row.slope, biome, `${field}.slope`, 0, 90),
|
|
3017
|
+
height,
|
|
3018
|
+
spacing: pair(row.spacing, biome, `${field}.spacing`),
|
|
3019
|
+
collision: oneOf(row.collision, biome, `${field}.collision`, COLLISION_CLASSES),
|
|
3020
|
+
stands: flag(row.stands, biome, `${field}.stands`),
|
|
3021
|
+
grassThreshold: share(row.grassThreshold, biome, `${field}.grassThreshold`),
|
|
3022
|
+
standThreshold: share(row.standThreshold, biome, `${field}.standThreshold`),
|
|
3023
|
+
far: far(row.far, biome, `${field}.far`)
|
|
3024
|
+
};
|
|
3025
|
+
}
|
|
3026
|
+
function grass(value, biome, field) {
|
|
3027
|
+
const row = closed(value, biome, field, GRASS_FIELDS);
|
|
3028
|
+
const density = count(row.density, biome, `${field}.density`);
|
|
3029
|
+
if (density > 1) fail2(biome, `${field}.density`, "must be at most 1");
|
|
3030
|
+
return {
|
|
3031
|
+
species: text(row.species, biome, `${field}.species`),
|
|
3032
|
+
density,
|
|
3033
|
+
slope: pair(row.slope, biome, `${field}.slope`, 0, 90),
|
|
3034
|
+
height: row.height === null ? null : pair(row.height, biome, `${field}.height`),
|
|
3035
|
+
spacing: positive(row.spacing, biome, `${field}.spacing`),
|
|
3036
|
+
slopeFalloff: count(row.slopeFalloff, biome, `${field}.slopeFalloff`),
|
|
3037
|
+
heightFalloff: count(row.heightFalloff, biome, `${field}.heightFalloff`),
|
|
3038
|
+
mats: row.mats === null ? null : band(row.mats, biome, `${field}.mats`),
|
|
3039
|
+
waterMargin: count(row.waterMargin, biome, `${field}.waterMargin`),
|
|
3040
|
+
beach: row.beach === null ? null : pair(row.beach, biome, `${field}.beach`),
|
|
3041
|
+
detailSpacing: positive(row.detailSpacing, biome, `${field}.detailSpacing`),
|
|
3042
|
+
drift: driftRow(row.drift, biome, `${field}.drift`),
|
|
3043
|
+
bladeScale: count(row.bladeScale, biome, `${field}.bladeScale`),
|
|
3044
|
+
bladeJitter: count(row.bladeJitter, biome, `${field}.bladeJitter`),
|
|
3045
|
+
scrubChance: share(row.scrubChance, biome, `${field}.scrubChance`),
|
|
3046
|
+
scrubScale: count(row.scrubScale, biome, `${field}.scrubScale`),
|
|
3047
|
+
scrubJitter: count(row.scrubJitter, biome, `${field}.scrubJitter`),
|
|
3048
|
+
scrubBush: flag(row.scrubBush, biome, `${field}.scrubBush`),
|
|
3049
|
+
shoreGround: flag(row.shoreGround, biome, `${field}.shoreGround`),
|
|
3050
|
+
cobbledBed: flag(row.cobbledBed, biome, `${field}.cobbledBed`)
|
|
3051
|
+
};
|
|
3052
|
+
}
|
|
3053
|
+
function layer(value, biome, field) {
|
|
3054
|
+
const row = closed(value, biome, field, LAYER_FIELDS, LAYER_REQUIRED);
|
|
3055
|
+
const from = count(row.from, biome, `${field}.from`);
|
|
3056
|
+
const to = count(row.to, biome, `${field}.to`);
|
|
3057
|
+
if (from >= to) fail2(biome, field, "must end above where it starts");
|
|
3058
|
+
const shedFrom = row.shedFrom === void 0 ? void 0 : count(row.shedFrom, biome, `${field}.shedFrom`, 0);
|
|
3059
|
+
const shedTo = row.shedTo === void 0 ? void 0 : count(row.shedTo, biome, `${field}.shedTo`, 0);
|
|
3060
|
+
for (const [key, entry] of [
|
|
3061
|
+
["shedFrom", shedFrom],
|
|
3062
|
+
["shedTo", shedTo]
|
|
3063
|
+
])
|
|
3064
|
+
if (entry !== void 0 && entry > 1) fail2(biome, `${field}.${key}`, "must be at most 1");
|
|
3065
|
+
if (shedFrom !== void 0 && shedTo !== void 0 && shedFrom >= shedTo)
|
|
3066
|
+
fail2(biome, field, "must shed from a gentler slope than it sheds to");
|
|
3067
|
+
return {
|
|
3068
|
+
layer: text(row.layer, biome, `${field}.layer`),
|
|
3069
|
+
from,
|
|
3070
|
+
to,
|
|
3071
|
+
shedFrom,
|
|
3072
|
+
shedTo
|
|
3073
|
+
};
|
|
3074
|
+
}
|
|
3075
|
+
function water(value, biome, field) {
|
|
3076
|
+
const row = closed(value, biome, field, WATER_FIELDS, WATER_REQUIRED);
|
|
3077
|
+
const kind = oneOf(row.kind, biome, `${field}.kind`, WATER_KINDS);
|
|
3078
|
+
const width = row.width === void 0 ? void 0 : positive(row.width, biome, `${field}.width`);
|
|
3079
|
+
const radius = row.radius === void 0 ? void 0 : positive(row.radius, biome, `${field}.radius`);
|
|
3080
|
+
const depth = row.depth === void 0 ? void 0 : positive(row.depth, biome, `${field}.depth`);
|
|
3081
|
+
const level = row.level === void 0 ? void 0 : count(row.level, biome, `${field}.level`);
|
|
3082
|
+
if (kind === "river" && width === void 0) fail2(biome, `${field}.width`, "is missing");
|
|
3083
|
+
if (kind === "lake" && radius === void 0) fail2(biome, `${field}.radius`, "is missing");
|
|
3084
|
+
if (kind === "ocean" && level === void 0) fail2(biome, `${field}.level`, "is missing");
|
|
3085
|
+
return { kind, width, radius, depth, level };
|
|
3086
|
+
}
|
|
3087
|
+
function bake(value, biome) {
|
|
3088
|
+
const row = closed(value, biome, "bake", BAKE_FIELDS);
|
|
3089
|
+
return {
|
|
3090
|
+
file: text(row.file, biome, "bake.file"),
|
|
3091
|
+
key: row.key === null ? null : text(row.key, biome, "bake.key")
|
|
3092
|
+
};
|
|
3093
|
+
}
|
|
3094
|
+
function spawn(value, biome) {
|
|
3095
|
+
const row = closed(value, biome, "spawn", SPAWN_FIELDS);
|
|
3096
|
+
return {
|
|
3097
|
+
at: point2(row.at, biome, "spawn.at"),
|
|
3098
|
+
cameraOffset: point3(row.cameraOffset, biome, "spawn.cameraOffset"),
|
|
3099
|
+
focus: point2(row.focus, biome, "spawn.focus")
|
|
3100
|
+
};
|
|
3101
|
+
}
|
|
3102
|
+
function species(value, biome) {
|
|
3103
|
+
if (!Array.isArray(value) || value.length === 0)
|
|
3104
|
+
fail2(biome, "species", "must be a non-empty array of asset ids");
|
|
3105
|
+
const names = value.map((name, index) => text(name, biome, `species[${index}]`));
|
|
3106
|
+
for (const [index, name] of names.entries())
|
|
3107
|
+
if (names.indexOf(name) !== index) fail2(biome, `species[${index}]`, `repeats '${name}'`);
|
|
3108
|
+
return names;
|
|
3109
|
+
}
|
|
3110
|
+
function validateBiome(spec, name) {
|
|
3111
|
+
if (typeof name !== "string" || name.trim() === "")
|
|
3112
|
+
throw new TypeError("validateBiome needs the biome's name");
|
|
3113
|
+
const biome = name;
|
|
3114
|
+
const root = closed(spec, biome, "", SPEC_FIELDS);
|
|
3115
|
+
const aliases = root.aliases;
|
|
3116
|
+
if (!Array.isArray(aliases) || aliases.length === 0)
|
|
3117
|
+
fail2(biome, "aliases", "must be a non-empty array of words");
|
|
3118
|
+
const rows = (value, field, one) => {
|
|
3119
|
+
if (!Array.isArray(value)) fail2(biome, field, "must be an array");
|
|
3120
|
+
return value.map((entry, index) => one(entry, `${field}[${index}]`));
|
|
3121
|
+
};
|
|
3122
|
+
return {
|
|
3123
|
+
look: text(root.look, biome, "look"),
|
|
3124
|
+
bake: bake(root.bake, biome),
|
|
3125
|
+
landform: oneOf(root.landform, biome, "landform", LANDFORM_KINDS),
|
|
3126
|
+
sizeMeters: positive(root.sizeMeters, biome, "sizeMeters"),
|
|
3127
|
+
seed: whole(root.seed, biome, "seed"),
|
|
3128
|
+
stream: oneOf(root.stream, biome, "stream", STREAMS),
|
|
3129
|
+
cover: cover(root.cover, biome),
|
|
3130
|
+
foliage: rows(root.foliage, "foliage", (entry, at) => foliage(entry, biome, at)),
|
|
3131
|
+
grass: rows(root.grass, "grass", (entry, at) => grass(entry, biome, at)),
|
|
3132
|
+
layers: rows(root.layers, "layers", (entry, at) => layer(entry, biome, at)),
|
|
3133
|
+
water: rows(root.water, "water", (entry, at) => water(entry, biome, at)),
|
|
3134
|
+
spawn: spawn(root.spawn, biome),
|
|
3135
|
+
species: species(root.species, biome),
|
|
3136
|
+
aliases: aliases.map(
|
|
3137
|
+
(word, index) => text(word, biome, `aliases[${index}]`).toLowerCase()
|
|
3138
|
+
)
|
|
3139
|
+
};
|
|
3140
|
+
}
|
|
3141
|
+
|
|
3142
|
+
// src/core/bands.ts
|
|
3143
|
+
function measureBands(layers, heights) {
|
|
3144
|
+
if (heights.length === 0) throw new RangeError("Terrain elevation samples are empty");
|
|
3145
|
+
let min = Number.POSITIVE_INFINITY;
|
|
3146
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
3147
|
+
for (let index = 0; index < heights.length; index += 1) {
|
|
3148
|
+
const height = heights[index];
|
|
3149
|
+
if (!Number.isFinite(height)) throw new RangeError("Terrain elevation sample is nonfinite");
|
|
3150
|
+
min = Math.min(min, height);
|
|
3151
|
+
max = Math.max(max, height);
|
|
3152
|
+
}
|
|
3153
|
+
const span = max - min;
|
|
3154
|
+
if (!Number.isFinite(span)) throw new RangeError("Terrain elevation range is nonfinite");
|
|
3155
|
+
const resolve = (fraction) => {
|
|
3156
|
+
const height = min + fraction * span;
|
|
3157
|
+
if (!Number.isFinite(height)) throw new RangeError("Terrain elevation band is nonfinite");
|
|
3158
|
+
return height;
|
|
3159
|
+
};
|
|
3160
|
+
let snowBand = null;
|
|
3161
|
+
let snowShed = null;
|
|
3162
|
+
const bands = {};
|
|
3163
|
+
for (const layer2 of layers) {
|
|
3164
|
+
const band2 = [resolve(layer2.from), resolve(layer2.to)];
|
|
3165
|
+
if (layer2.layer === "snow") {
|
|
3166
|
+
snowBand = band2;
|
|
3167
|
+
if (layer2.shedFrom !== void 0 && layer2.shedTo !== void 0)
|
|
3168
|
+
snowShed = [layer2.shedFrom, layer2.shedTo];
|
|
3169
|
+
} else {
|
|
3170
|
+
bands[layer2.layer] = band2;
|
|
3171
|
+
}
|
|
3172
|
+
}
|
|
3173
|
+
return { bands, elevationRange: [min, max], snowBand, snowShed };
|
|
3174
|
+
}
|
|
3175
|
+
var noise = new ImprovedNoise();
|
|
3176
|
+
function clamp01(value) {
|
|
3177
|
+
return Math.min(1, Math.max(0, value));
|
|
3178
|
+
}
|
|
3179
|
+
function fieldWeight(field, x, z) {
|
|
3180
|
+
let sum = field.base;
|
|
3181
|
+
for (const octave of field.octaves)
|
|
3182
|
+
sum += octave.gain * noise.noise(x * octave.frequency, octave.offset, z * octave.frequency);
|
|
3183
|
+
return clamp01(sum);
|
|
3184
|
+
}
|
|
3185
|
+
var STAND_FIELD = {
|
|
3186
|
+
base: 0.5,
|
|
3187
|
+
octaves: [
|
|
3188
|
+
{ gain: 0.3, frequency: 75e-4, offset: 3.1 },
|
|
3189
|
+
{ gain: 0.2, frequency: 0.026, offset: 8.7 }
|
|
3190
|
+
]
|
|
3191
|
+
};
|
|
3192
|
+
var MATS_FIELD = {
|
|
3193
|
+
base: 0.52,
|
|
3194
|
+
octaves: [
|
|
3195
|
+
{ gain: 0.85, frequency: 0.043, offset: 11 },
|
|
3196
|
+
{ gain: 0.15, frequency: 0.11, offset: 19 }
|
|
3197
|
+
]
|
|
3198
|
+
};
|
|
3199
|
+
|
|
3200
|
+
// src/core/water.ts
|
|
3201
|
+
var EMPTY = [];
|
|
3202
|
+
function createSegmentIndex(segments, cellSize) {
|
|
3203
|
+
if (!(cellSize > 0) || !Number.isFinite(cellSize))
|
|
3204
|
+
throw new Error(
|
|
3205
|
+
`TN_TERRAIN_WATER_CELL_INVALID: cellSize must be a positive finite number, received ${String(cellSize)}.`
|
|
3206
|
+
);
|
|
3207
|
+
const cells = /* @__PURE__ */ new Map();
|
|
3208
|
+
for (const segment of segments) {
|
|
3209
|
+
const { a, b, reach } = segment;
|
|
3210
|
+
if (!(reach >= 0) || !Number.isFinite(reach))
|
|
3211
|
+
throw new Error(
|
|
3212
|
+
`TN_TERRAIN_WATER_REACH_INVALID: reach must be a non-negative finite number, received ${String(reach)}.`
|
|
3213
|
+
);
|
|
3214
|
+
const fromX = Math.floor((Math.min(a[0], b[0]) - reach) / cellSize);
|
|
3215
|
+
const toX = Math.floor((Math.max(a[0], b[0]) + reach) / cellSize);
|
|
3216
|
+
const fromZ = Math.floor((Math.min(a[1], b[1]) - reach) / cellSize);
|
|
3217
|
+
const toZ = Math.floor((Math.max(a[1], b[1]) + reach) / cellSize);
|
|
3218
|
+
for (let cx = fromX; cx <= toX; cx++)
|
|
3219
|
+
for (let cz = fromZ; cz <= toZ; cz++) {
|
|
3220
|
+
const key = `${cx}:${cz}`;
|
|
3221
|
+
const list = cells.get(key);
|
|
3222
|
+
if (list) list.push(segment.data);
|
|
3223
|
+
else cells.set(key, [segment.data]);
|
|
3224
|
+
}
|
|
3225
|
+
}
|
|
3226
|
+
return {
|
|
3227
|
+
nearby: (x, z) => cells.get(`${Math.floor(x / cellSize)}:${Math.floor(z / cellSize)}`) ?? EMPTY
|
|
3228
|
+
};
|
|
3229
|
+
}
|
|
3230
|
+
|
|
3231
|
+
// src/core/scatter-ordered.ts
|
|
3232
|
+
function orderedScatterRules(spec) {
|
|
3233
|
+
const tree = spec.foliage[0];
|
|
3234
|
+
const grass2 = spec.grass[0];
|
|
3235
|
+
if (tree === void 0 || grass2 === void 0)
|
|
3236
|
+
throw new RangeError(`${spec.look}: an ordered biome needs one tree and one ground cover`);
|
|
3237
|
+
if (spec.cover === null)
|
|
3238
|
+
throw new RangeError(`${spec.look}: an ordered biome needs its cover block`);
|
|
3239
|
+
if (tree.species !== "spruce" && tree.species !== "sapling")
|
|
3240
|
+
throw new RangeError(`${spec.look}: no ordered rule for the tree '${tree.species}'`);
|
|
3241
|
+
const { cover: cover2 } = spec;
|
|
3242
|
+
const drift = grass2.drift;
|
|
3243
|
+
if (drift === null) throw new RangeError(`${spec.look}: an ordered biome needs its cover drift`);
|
|
3244
|
+
return {
|
|
3245
|
+
ground: cover2.ground,
|
|
3246
|
+
seed: cover2.seed,
|
|
3247
|
+
attempts: cover2.attempts,
|
|
3248
|
+
drift: {
|
|
3249
|
+
field: drift.field === "mats" ? MATS_FIELD : STAND_FIELD,
|
|
3250
|
+
frequency: drift.frequency,
|
|
3251
|
+
amplitude: drift.amplitude,
|
|
3252
|
+
offset: drift.offset,
|
|
3253
|
+
band: drift.band
|
|
3254
|
+
},
|
|
3255
|
+
bladeScale: grass2.bladeScale,
|
|
3256
|
+
bladeJitter: grass2.bladeJitter,
|
|
3257
|
+
scrubChance: grass2.scrubChance,
|
|
3258
|
+
scrubScale: grass2.scrubScale,
|
|
3259
|
+
scrubJitter: grass2.scrubJitter,
|
|
3260
|
+
scrubBush: grass2.scrubBush,
|
|
3261
|
+
shoreGround: grass2.shoreGround,
|
|
3262
|
+
cobbledBed: grass2.cobbledBed,
|
|
3263
|
+
stoneRadius: cover2.stoneRadius,
|
|
3264
|
+
understorey: cover2.understorey,
|
|
3265
|
+
treeAsset: tree.species,
|
|
3266
|
+
treeCount: tree.density,
|
|
3267
|
+
treeSpacing: tree.spacing,
|
|
3268
|
+
treeStands: tree.stands,
|
|
3269
|
+
treeSlopeLimit: tree.slope[1],
|
|
3270
|
+
treeHeightCeiling: tree.height === null ? null : tree.height[1],
|
|
3271
|
+
treeGrassThreshold: tree.grassThreshold,
|
|
3272
|
+
treeStandThreshold: tree.standThreshold,
|
|
3273
|
+
grassCell: grass2.spacing,
|
|
3274
|
+
grassDensity: grass2.density,
|
|
3275
|
+
grassSlopeLimit: grass2.slope[1],
|
|
3276
|
+
grassSlopeFalloff: grass2.slopeFalloff,
|
|
3277
|
+
grassWaterMargin: grass2.waterMargin,
|
|
3278
|
+
grassBeach: grass2.beach,
|
|
3279
|
+
grassHeight: grass2.height === null ? null : [grass2.height[1], grass2.heightFalloff],
|
|
3280
|
+
grassMats: grass2.mats,
|
|
3281
|
+
grassDetailSpacing: grass2.detailSpacing
|
|
3282
|
+
};
|
|
3283
|
+
}
|
|
3284
|
+
var tundraCover = (x, z) => fieldWeight(MATS_FIELD, x, z);
|
|
3285
|
+
function grassWeight(data, x, z) {
|
|
3286
|
+
const rules = data.rules;
|
|
3287
|
+
const height = clampedHeight(data, x, z);
|
|
3288
|
+
if (rules.grassBeach && data.waterLevel !== null)
|
|
3289
|
+
return clamp012((height - data.waterLevel - rules.grassBeach[0]) / rules.grassBeach[1]) * clamp012((rules.grassSlopeLimit - slopeDegrees(data, x, z)) / rules.grassSlopeFalloff);
|
|
3290
|
+
if (data.waterLevel !== null && height < data.waterLevel + rules.grassWaterMargin) return 0;
|
|
3291
|
+
const slope = slopeDegrees(data, x, z);
|
|
3292
|
+
if (rules.grassDensity === 0) return 0;
|
|
3293
|
+
const meadow = clamp012((rules.grassSlopeLimit - slope) / rules.grassSlopeFalloff);
|
|
3294
|
+
const fade = rules.grassHeight === null ? 1 : clamp012((rules.grassHeight[0] - height) / rules.grassHeight[1]);
|
|
3295
|
+
const mats = rules.grassMats === null ? 1 : clamp012((forestWeight(x * 9.8, z * 8.7) - rules.grassMats[0]) / rules.grassMats[1]);
|
|
3296
|
+
return meadow * rules.grassDensity * fade * mats;
|
|
3297
|
+
}
|
|
3298
|
+
function depositAtIndex(data, vertex) {
|
|
3299
|
+
const sediment = data.erosion?.sediment[vertex] ?? 0;
|
|
3300
|
+
const hydraulic2 = (data.erosion?.deposition[vertex] ?? 0) / (1 + Math.sqrt(sediment));
|
|
3301
|
+
return Math.min(1, hydraulic2 * 0.25 + (data.erosion?.talus[vertex] ?? 0) * 0.3);
|
|
3302
|
+
}
|
|
3303
|
+
function clamp012(value) {
|
|
3304
|
+
return Math.min(1, Math.max(0, value));
|
|
3305
|
+
}
|
|
3306
|
+
function clampedHeight(data, x, z) {
|
|
3307
|
+
const limit = data.size / 2 - 1e-3;
|
|
3308
|
+
return data.field.heightAt(
|
|
3309
|
+
Math.min(limit, Math.max(-limit, x)),
|
|
3310
|
+
Math.min(limit, Math.max(-limit, z))
|
|
3311
|
+
);
|
|
3312
|
+
}
|
|
3313
|
+
function slopeDegrees(data, x, z) {
|
|
3314
|
+
const step = data.size / (data.resolution - 1);
|
|
3315
|
+
const dx = clampedHeight(data, x + step, z) - clampedHeight(data, x - step, z);
|
|
3316
|
+
const dz = clampedHeight(data, x, z + step) - clampedHeight(data, x, z - step);
|
|
3317
|
+
return Math.atan(Math.hypot(dx, dz) / (2 * step)) * 180 / Math.PI;
|
|
3318
|
+
}
|
|
3319
|
+
function forestWeight(x, z) {
|
|
3320
|
+
return fieldWeight(STAND_FIELD, x, z);
|
|
3321
|
+
}
|
|
3322
|
+
var fieldNoise = new ImprovedNoise();
|
|
3323
|
+
var sample01 = (x, y, z) => clamp012(fieldNoise.noise(x, y, z) * 0.5 + 0.5);
|
|
3324
|
+
function coverDriftWeight(drift, x, z) {
|
|
3325
|
+
const [frequencyX, frequencyZ] = drift.frequency;
|
|
3326
|
+
const [threshold, span] = drift.band;
|
|
3327
|
+
const weight = fieldWeight(drift.field, x * frequencyX, z * frequencyZ);
|
|
3328
|
+
return drift.offset + drift.amplitude * clamp012((weight - threshold) / span);
|
|
3329
|
+
}
|
|
3330
|
+
var GRADED_UNDERSTOREY = {
|
|
3331
|
+
attempts: 14e3,
|
|
3332
|
+
litter: 1,
|
|
3333
|
+
fern: 1,
|
|
3334
|
+
bush: 1,
|
|
3335
|
+
flower: 1,
|
|
3336
|
+
deadfall: 0
|
|
3337
|
+
};
|
|
3338
|
+
var GRASS_FULL = 22;
|
|
3339
|
+
var GRASS_THIN = 62;
|
|
3340
|
+
function scatterProps(data, focus, clearings = []) {
|
|
3341
|
+
const { spec, rules } = data;
|
|
3342
|
+
const temperate = rules.ground === "wood";
|
|
3343
|
+
const desert = rules.ground === "mesa";
|
|
3344
|
+
const tundra = rules.ground === "mats";
|
|
3345
|
+
const alpine = rules.ground === "crag";
|
|
3346
|
+
const treeLimit = rules.treeCount;
|
|
3347
|
+
const placements = [];
|
|
3348
|
+
const counts = Object.fromEntries(
|
|
3349
|
+
[
|
|
3350
|
+
"boulder",
|
|
3351
|
+
"bush",
|
|
3352
|
+
"deadfall",
|
|
3353
|
+
"fern",
|
|
3354
|
+
"grass",
|
|
3355
|
+
"litter",
|
|
3356
|
+
"poppy",
|
|
3357
|
+
"sapling",
|
|
3358
|
+
"scrub",
|
|
3359
|
+
"spruce",
|
|
3360
|
+
"riverrock",
|
|
3361
|
+
"scree",
|
|
3362
|
+
"cliff",
|
|
3363
|
+
"mountain",
|
|
3364
|
+
"volcanic",
|
|
3365
|
+
"reveal"
|
|
3366
|
+
].map((name) => [name, 0])
|
|
3367
|
+
);
|
|
3368
|
+
const random2 = createRandom(rules.seed);
|
|
3369
|
+
const half = data.size / 2;
|
|
3370
|
+
const inside = (x, z) => Math.abs(x) < half - 3 && Math.abs(z) < half - 3;
|
|
3371
|
+
const riverIndex = createSegmentIndex(
|
|
3372
|
+
spec.water.some((body) => body.kind === "river") ? (data.rivers ?? []).flatMap(
|
|
3373
|
+
(river) => river.points.flatMap((b, index) => {
|
|
3374
|
+
const a = river.points[index - 1];
|
|
3375
|
+
return a ? [
|
|
3376
|
+
{
|
|
3377
|
+
a: [a[0] ?? 0, a[2] ?? 0],
|
|
3378
|
+
b: [b[0] ?? 0, b[2] ?? 0],
|
|
3379
|
+
reach: river.width * 2.5,
|
|
3380
|
+
data: { a, b, reach: river.width * 2.5 }
|
|
3381
|
+
}
|
|
3382
|
+
] : [];
|
|
3383
|
+
})
|
|
3384
|
+
) : [],
|
|
3385
|
+
32
|
|
3386
|
+
);
|
|
3387
|
+
const nearRiver = (x, z) => riverIndex.nearby(x, z).some(({ a, b, reach }) => {
|
|
3388
|
+
const dx = (b[0] ?? 0) - (a[0] ?? 0);
|
|
3389
|
+
const dz = (b[2] ?? 0) - (a[2] ?? 0);
|
|
3390
|
+
const t = clamp012(
|
|
3391
|
+
((x - (a[0] ?? 0)) * dx + (z - (a[2] ?? 0)) * dz) / (dx * dx + dz * dz || 1)
|
|
3392
|
+
);
|
|
3393
|
+
const distance = Math.hypot(x - (a[0] ?? 0) - t * dx, z - (a[2] ?? 0) - t * dz);
|
|
3394
|
+
const level = (a[1] ?? 0) + t * ((b[1] ?? 0) - (a[1] ?? 0));
|
|
3395
|
+
return distance < reach && clampedHeight(data, x, z) < level + 0.35;
|
|
3396
|
+
});
|
|
3397
|
+
const wet = (x, z) => data.waterLevel !== null && clampedHeight(data, x, z) < data.waterLevel + 0.35 || tundra && nearRiver(x, z) || (data.lakes ?? []).some(
|
|
3398
|
+
(lake) => Math.hypot(x - (lake.at[0] ?? 0), z - (lake.at[1] ?? 0)) < lake.radius && clampedHeight(data, x, z) < lake.level + 0.35
|
|
3399
|
+
);
|
|
3400
|
+
const depositsAt = (x, z) => {
|
|
3401
|
+
const column = Math.max(
|
|
3402
|
+
0,
|
|
3403
|
+
Math.min(data.resolution - 1, Math.round((x / data.size + 0.5) * (data.resolution - 1)))
|
|
3404
|
+
);
|
|
3405
|
+
const row = Math.max(
|
|
3406
|
+
0,
|
|
3407
|
+
Math.min(data.resolution - 1, Math.round((z / data.size + 0.5) * (data.resolution - 1)))
|
|
3408
|
+
);
|
|
3409
|
+
return depositAtIndex(data, row * data.resolution + column);
|
|
3410
|
+
};
|
|
3411
|
+
let poseDraw = random2;
|
|
3412
|
+
let placing = true;
|
|
3413
|
+
const put = (asset, x, z, scale, suffix = "") => {
|
|
3414
|
+
if (!inside(x, z) || asset !== "riverrock" && asset !== "pebble" && wet(x, z)) return;
|
|
3415
|
+
if (data.erosion && asset === "scree" && depositsAt(x, z) < 0.08) {
|
|
3416
|
+
const slope = slopeDegrees(data, x, z);
|
|
3417
|
+
if (!(alpine || desert) || slope < 18 || slope > 38) return;
|
|
3418
|
+
}
|
|
3419
|
+
if (asset === "cliff") {
|
|
3420
|
+
const reach = 18 * Math.SQRT1_2 * scale;
|
|
3421
|
+
if (data.waterLevel !== null || [-reach, 0, reach].some(
|
|
3422
|
+
(dx) => [-reach, 0, reach].some(
|
|
3423
|
+
(dz) => grassWeight(data, x + dx, z + dz) > 0.05 || slopeDegrees(data, x + dx, z + dz) < 43
|
|
3424
|
+
)
|
|
3425
|
+
))
|
|
3426
|
+
return;
|
|
3427
|
+
}
|
|
3428
|
+
if (alpine && asset === "mountain" && (Math.abs(x) > half - 48 || Math.abs(z) > half - 48))
|
|
3429
|
+
return;
|
|
3430
|
+
if (desert && ["boulder", "scree", "volcanic", "reveal"].includes(asset)) {
|
|
3431
|
+
const height = clampedHeight(data, x, z);
|
|
3432
|
+
const slope = slopeDegrees(data, x, z);
|
|
3433
|
+
const neighbours = [
|
|
3434
|
+
[-18, 0],
|
|
3435
|
+
[18, 0],
|
|
3436
|
+
[0, -18],
|
|
3437
|
+
[0, 18]
|
|
3438
|
+
].map(([dx, dz]) => clampedHeight(data, x + (dx ?? 0), z + (dz ?? 0)));
|
|
3439
|
+
const foot = slope < 35 && Math.max(...neighbours) > height + 8;
|
|
3440
|
+
const rim = slope < 24 && Math.min(...neighbours) < height - 8;
|
|
3441
|
+
if (!foot && !rim) return;
|
|
3442
|
+
}
|
|
3443
|
+
const index = counts[asset] ?? 0;
|
|
3444
|
+
const crag = asset === "mountain" || asset === "volcanic" || asset === "reveal";
|
|
3445
|
+
const bedded = temperate && ["boulder", "riverrock", "scree", "pebble", "deadfall"].includes(asset);
|
|
3446
|
+
const normal = crag || bedded ? data.field.normalAt(x, z) : void 0;
|
|
3447
|
+
if (crag && normal) {
|
|
3448
|
+
normal.y += 0.65;
|
|
3449
|
+
normal.normalize();
|
|
3450
|
+
}
|
|
3451
|
+
const rotation = crag && normal ? Math.atan2(normal.x, normal.z) + (poseDraw() - 0.5) * 0.6 : poseDraw() * Math.PI * 2;
|
|
3452
|
+
if (!placing) return;
|
|
3453
|
+
placements.push({
|
|
3454
|
+
asset,
|
|
3455
|
+
id: `temperate-${asset}:${index}${suffix}`,
|
|
3456
|
+
layer: `temperate-${asset}`,
|
|
3457
|
+
alignToNormal: crag || bedded || temperate && (asset === "spruce" || asset === "sapling"),
|
|
3458
|
+
normal: (crag || bedded) && normal ? normal.toArray() : temperate && (asset === "spruce" || asset === "sapling") ? [Math.sin(x * 1.17 + z) * 0.045, 1, Math.cos(z * 1.31 - x) * 0.045] : [0, 1, 0],
|
|
3459
|
+
position: [x, clampedHeight(data, x, z), z],
|
|
3460
|
+
rotation,
|
|
3461
|
+
scale
|
|
3462
|
+
});
|
|
3463
|
+
counts[asset] = index + 1;
|
|
3464
|
+
};
|
|
3465
|
+
const nearEye = (x, z) => clearings.some(([cx, cz, radius]) => Math.hypot(x - cx, z - cz) < radius * 1.6);
|
|
3466
|
+
const cells = /* @__PURE__ */ new Map();
|
|
3467
|
+
const CELL = 7;
|
|
3468
|
+
const crowded = (x, z, reach) => {
|
|
3469
|
+
const cx = Math.floor(x / CELL);
|
|
3470
|
+
const cz = Math.floor(z / CELL);
|
|
3471
|
+
for (let dz = -1; dz <= 1; dz++)
|
|
3472
|
+
for (let dx = -1; dx <= 1; dx++)
|
|
3473
|
+
for (const [tx, tz, tr] of cells.get(`${cx + dx},${cz + dz}`) ?? [])
|
|
3474
|
+
if (Math.hypot(tx - x, tz - z) < Math.max(reach, tr)) return true;
|
|
3475
|
+
return false;
|
|
3476
|
+
};
|
|
3477
|
+
const shaded = (x, z, radius) => {
|
|
3478
|
+
const cx = Math.floor(x / CELL);
|
|
3479
|
+
const cz = Math.floor(z / CELL);
|
|
3480
|
+
const ring = Math.ceil(radius * 2 / CELL);
|
|
3481
|
+
for (let dz = -ring; dz <= ring; dz++)
|
|
3482
|
+
for (let dx = -ring; dx <= ring; dx++)
|
|
3483
|
+
for (const [tx, tz, , crown] of cells.get(`${cx + dx},${cz + dz}`) ?? [])
|
|
3484
|
+
if (Math.hypot(tx - x, tz - z) < radius * crown) return true;
|
|
3485
|
+
return false;
|
|
3486
|
+
};
|
|
3487
|
+
const treeAsset = rules.treeAsset;
|
|
3488
|
+
const hollow = (x, z) => {
|
|
3489
|
+
const here = clampedHeight(data, x, z);
|
|
3490
|
+
const around = [-26, -13, 13, 26].map(
|
|
3491
|
+
(d) => (clampedHeight(data, x + d, z) + clampedHeight(data, x, z + d)) / 2
|
|
3492
|
+
);
|
|
3493
|
+
return clamp012(((Math.max(...around) - here) / 3.5 + 0.15) * 0.5);
|
|
3494
|
+
};
|
|
3495
|
+
for (let tries = 0; (counts[treeAsset] ?? 0) < treeLimit && tries < rules.attempts; tries++) {
|
|
3496
|
+
const x = (random2() - 0.5) * data.size;
|
|
3497
|
+
const z = (random2() - 0.5) * data.size;
|
|
3498
|
+
if (!inside(x, z) || wet(x, z) || nearEye(x, z) || slopeDegrees(data, x, z) > rules.treeSlopeLimit || data.waterLevel !== null && clampedHeight(data, x, z) < data.waterLevel + rules.grassWaterMargin || rules.treeHeightCeiling !== null && clampedHeight(data, x, z) > rules.treeHeightCeiling || grassWeight(data, x, z) < rules.treeGrassThreshold || forestWeight(x, z) < rules.treeStandThreshold)
|
|
3499
|
+
continue;
|
|
3500
|
+
const stand = forestWeight(x, z);
|
|
3501
|
+
const reach = rules.treeStands ? rules.treeSpacing[1] - rules.treeSpacing[0] * clamp012((stand - 0.28) / 0.5) : rules.treeSpacing[0];
|
|
3502
|
+
if (crowded(x, z, reach)) continue;
|
|
3503
|
+
const key = `${Math.floor(x / CELL)},${Math.floor(z / CELL)}`;
|
|
3504
|
+
const bucket = cells.get(key) ?? [];
|
|
3505
|
+
cells.set(key, bucket);
|
|
3506
|
+
const age = sample01(x * 0.021, 7, z * 0.021);
|
|
3507
|
+
const scale = temperate ? 0.42 + 1.18 * age ** 1.6 + random2() * 0.16 : (tundra ? 0.25 : 0.55) + random2() * 0.35;
|
|
3508
|
+
const standing = !BANK.some(([dx, dz]) => wet(x + dx, z + dz) || nearRiver(x + dx, z + dz));
|
|
3509
|
+
bucket.push([x, z, reach, standing ? scale : 0]);
|
|
3510
|
+
put(treeAsset, x, z, scale);
|
|
3511
|
+
const edge = stand < 0.57;
|
|
3512
|
+
for (let i = 0; i < (tundra ? 0 : edge ? 2 : 1); i++) {
|
|
3513
|
+
const angle = random2() * Math.PI * 2;
|
|
3514
|
+
const reachOut = 2 + random2() * 4;
|
|
3515
|
+
const sx = x + Math.cos(angle) * reachOut;
|
|
3516
|
+
const sz = z + Math.sin(angle) * reachOut;
|
|
3517
|
+
if (!nearEye(sx, sz) && grassWeight(data, sx, sz) > 0.25)
|
|
3518
|
+
put("sapling", sx, sz, 0.55 + random2() * 0.9);
|
|
3519
|
+
}
|
|
3520
|
+
if (temperate)
|
|
3521
|
+
for (let i = 0; i < 2; i++)
|
|
3522
|
+
put("fern", x + (random2() - 0.5) * 7, z + (random2() - 0.5) * 7, 0.6 + random2() * 0.75);
|
|
3523
|
+
}
|
|
3524
|
+
if (temperate) {
|
|
3525
|
+
const understoreyPass = (draw, row2) => {
|
|
3526
|
+
for (let i = 0; i < row2.attempts; i++) {
|
|
3527
|
+
const x = (draw() - 0.5) * data.size;
|
|
3528
|
+
const z = (draw() - 0.5) * data.size;
|
|
3529
|
+
if (!inside(x, z) || wet(x, z) || nearEye(x, z)) continue;
|
|
3530
|
+
if (slopeDegrees(data, x, z) > 30) continue;
|
|
3531
|
+
const stand = forestWeight(x, z);
|
|
3532
|
+
const damp = hollow(x, z);
|
|
3533
|
+
const open = grassWeight(data, x, z);
|
|
3534
|
+
if (open < 0.28) continue;
|
|
3535
|
+
if (stand > 0.44 && draw() < ((stand - 0.4) * 1.9 + damp * 0.25) * row2.litter)
|
|
3536
|
+
put("litter", x, z, 0.65 + draw() * 1.1);
|
|
3537
|
+
if (stand > 0.4 && damp > 0.25 && draw() < damp * 0.55 * row2.fern)
|
|
3538
|
+
put("fern", x, z, 0.7 + draw() * 0.8);
|
|
3539
|
+
if (stand < 0.66 && draw() < (0.05 + damp * 0.16 + (stand < 0.5 ? 0.06 : 0)) * row2.bush)
|
|
3540
|
+
put("bush", x, z, 0.5 + draw() * 1.1);
|
|
3541
|
+
if (stand < 0.58 && damp > 0.18 && draw() < (0.02 + damp * 0.05) * row2.flower)
|
|
3542
|
+
put("poppy", x, z, 0.75 + draw() * 0.6);
|
|
3543
|
+
if (row2.deadfall > 0 && stand > 0.5 && draw() < 0.012 * row2.deadfall)
|
|
3544
|
+
put("deadfall", x, z, 0.7 + draw() * 0.9);
|
|
3545
|
+
}
|
|
3546
|
+
};
|
|
3547
|
+
const row = rules.understorey;
|
|
3548
|
+
const graded = row.attempts === GRADED_UNDERSTOREY.attempts && row.litter === 1 && row.fern === 1 && row.bush === 1 && row.flower === 1 && row.deadfall === 0;
|
|
3549
|
+
placing = graded;
|
|
3550
|
+
understoreyPass(random2, { ...row, ...GRADED_UNDERSTOREY });
|
|
3551
|
+
placing = true;
|
|
3552
|
+
if (!graded) {
|
|
3553
|
+
poseDraw = createRandom(rules.seed + 13);
|
|
3554
|
+
understoreyPass(poseDraw, row);
|
|
3555
|
+
poseDraw = random2;
|
|
3556
|
+
}
|
|
3557
|
+
}
|
|
3558
|
+
if (tundra || desert) {
|
|
3559
|
+
for (let i = 0; i < (tundra ? 1100 : 900); i++) {
|
|
3560
|
+
const cx = (random2() - 0.5) * data.size;
|
|
3561
|
+
const cz = (random2() - 0.5) * data.size;
|
|
3562
|
+
if (!inside(cx, cz)) continue;
|
|
3563
|
+
const bias = tundra ? clamp012((tundraCover(cx, cz) - 0.42) / 0.3) : clamp012((hollow(cx, cz) - 0.1) * 1.5);
|
|
3564
|
+
if (random2() > 0.3 + bias * 0.65) continue;
|
|
3565
|
+
const radius = tundra ? 1.2 + random2() * 3 : 1.8 + random2() * 4.5;
|
|
3566
|
+
const tufts = tundra ? 8 + Math.floor(random2() * 14) : 5 + Math.floor(random2() * 9);
|
|
3567
|
+
for (let k = 0; k < tufts; k++) {
|
|
3568
|
+
const angle = random2() * Math.PI * 2;
|
|
3569
|
+
const out = radius * Math.sqrt(random2());
|
|
3570
|
+
cover2(cx + Math.cos(angle) * out, cz + Math.sin(angle) * out, 0.85);
|
|
3571
|
+
}
|
|
3572
|
+
}
|
|
3573
|
+
}
|
|
3574
|
+
if (alpine) {
|
|
3575
|
+
for (let z = -half + 48; z < half - 48; z += 24)
|
|
3576
|
+
for (let x = -half + 48; x < half - 48; x += 24) {
|
|
3577
|
+
const sx = x + (random2() - 0.5) * 12;
|
|
3578
|
+
const sz = z + (random2() - 0.5) * 12;
|
|
3579
|
+
if (slopeDegrees(data, sx, sz) < 40 || clampedHeight(data, sx, sz) < 65 || random2() > 0.55)
|
|
3580
|
+
continue;
|
|
3581
|
+
const metres = 2 + random2() * 3;
|
|
3582
|
+
put("mountain", sx, sz, metres / 24);
|
|
3583
|
+
const fall = data.field.normalAt(sx, sz);
|
|
3584
|
+
const length = Math.hypot(fall.x, fall.z) || 1;
|
|
3585
|
+
const overlapX = sx + fall.x / length * metres * 0.28;
|
|
3586
|
+
const overlapZ = sz + fall.z / length * metres * 0.28;
|
|
3587
|
+
if (clampedHeight(data, overlapX, overlapZ) > 42 && slopeDegrees(data, overlapX, overlapZ) > 28)
|
|
3588
|
+
put("mountain", overlapX, overlapZ, metres * 0.85 / 24);
|
|
3589
|
+
for (let k = 0; k < 28; k++) {
|
|
3590
|
+
const down = 16 + random2() * 24;
|
|
3591
|
+
const across = (random2() - 0.5) * down;
|
|
3592
|
+
const tx = sx + (fall.x * down - fall.z * across) / length;
|
|
3593
|
+
const tz = sz + (fall.z * down + fall.x * across) / length;
|
|
3594
|
+
if (slopeDegrees(data, tx, tz) < 38) put("scree", tx, tz, 0.08 + random2() * 0.28);
|
|
3595
|
+
}
|
|
3596
|
+
}
|
|
3597
|
+
}
|
|
3598
|
+
if (desert) {
|
|
3599
|
+
for (let z = -half + 18; z < half - 18; z += 9)
|
|
3600
|
+
for (let x = -half + 18; x < half - 18; x += 9) {
|
|
3601
|
+
const sx = x + (random2() - 0.5) * 7;
|
|
3602
|
+
const sz = z + (random2() - 0.5) * 7;
|
|
3603
|
+
if (random2() < 0.7) put("volcanic", sx, sz, 0.06 + random2() * 0.12);
|
|
3604
|
+
if (random2() < 0.5) put("reveal", sx, sz, 0.15 + random2() * 0.2);
|
|
3605
|
+
}
|
|
3606
|
+
}
|
|
3607
|
+
for (let i = 0; i < (temperate ? 1100 : 2400); i++) {
|
|
3608
|
+
const x = (random2() - 0.5) * data.size;
|
|
3609
|
+
const z = (random2() - 0.5) * data.size;
|
|
3610
|
+
if (!inside(x, z) || wet(x, z)) continue;
|
|
3611
|
+
const slope = slopeDegrees(data, x, z);
|
|
3612
|
+
const deposits = depositsAt(x, z);
|
|
3613
|
+
if (slope < (tundra ? 3 : desert ? 8 : 15) && deposits < 0.18 || random2() > 0.38 || nearEye(x, z))
|
|
3614
|
+
continue;
|
|
3615
|
+
if (slope > 43) {
|
|
3616
|
+
if (temperate && random2() < 0.22) put("cliff", x, z, 0.65 + random2() * 0.5);
|
|
3617
|
+
continue;
|
|
3618
|
+
}
|
|
3619
|
+
if (deposits > 0.12 && slope < 40 && random2() < 0.65) {
|
|
3620
|
+
for (let k = 0; k < 3; k++)
|
|
3621
|
+
put("scree", x + (random2() - 0.5) * 8, z + (random2() - 0.5) * 8, 0.12 + random2() * 0.3);
|
|
3622
|
+
}
|
|
3623
|
+
if (alpine && slope > 18 && slope < 38) {
|
|
3624
|
+
for (let k = 0; k < 8; k++) {
|
|
3625
|
+
const sx = x + (random2() - 0.5) * 14;
|
|
3626
|
+
const sz = z + (random2() - 0.5) * 14;
|
|
3627
|
+
if (slopeDegrees(data, sx, sz) < 42) put("scree", sx, sz, 0.12 + random2() * 0.22);
|
|
3628
|
+
}
|
|
3629
|
+
}
|
|
3630
|
+
for (let k = 0; k < 2 + Math.floor(random2() * 3); k++)
|
|
3631
|
+
put("boulder", x + (random2() - 0.5) * 8, z + (random2() - 0.5) * 8, 0.45 + random2() * 0.8);
|
|
3632
|
+
}
|
|
3633
|
+
if (temperate && data.waterLevel !== null) {
|
|
3634
|
+
for (let i = 0; i < 2200; i++) {
|
|
3635
|
+
const x = (random2() - 0.5) * data.size;
|
|
3636
|
+
const z = (random2() - 0.5) * data.size;
|
|
3637
|
+
const above = clampedHeight(data, x, z) - data.waterLevel;
|
|
3638
|
+
if (above < 0.4 || above > 4.8 || slopeDegrees(data, x, z) > 38 || random2() > 0.28) continue;
|
|
3639
|
+
put("riverrock", x, z, 0.8 + random2() * 2.4);
|
|
3640
|
+
if (random2() < 0.22) put("boulder", x + 1.2, z - 1.2, 0.3 + random2() * 0.5);
|
|
3641
|
+
}
|
|
3642
|
+
}
|
|
3643
|
+
for (const river of data.rivers ?? []) {
|
|
3644
|
+
for (let i = 1; i < river.points.length; i++) {
|
|
3645
|
+
const a = river.points[i - 1];
|
|
3646
|
+
const b = river.points[i];
|
|
3647
|
+
if (!a || !b) continue;
|
|
3648
|
+
const dx = (b[0] ?? 0) - (a[0] ?? 0);
|
|
3649
|
+
const dz = (b[2] ?? 0) - (a[2] ?? 0);
|
|
3650
|
+
const length = Math.hypot(dx, dz);
|
|
3651
|
+
for (let at = 0; at < length; at += 2.4) {
|
|
3652
|
+
const side = random2() < 0.5 ? -1 : 1;
|
|
3653
|
+
const reach = river.width * (0.42 + random2() * 0.3) * side;
|
|
3654
|
+
put(
|
|
3655
|
+
"riverrock",
|
|
3656
|
+
(a[0] ?? 0) + dx * at / length - dz / length * reach,
|
|
3657
|
+
(a[2] ?? 0) + dz * at / length + dx / length * reach,
|
|
3658
|
+
0.5 + random2() * 1.2
|
|
3659
|
+
);
|
|
3660
|
+
}
|
|
3661
|
+
}
|
|
3662
|
+
}
|
|
3663
|
+
const shadeDraw = createRandom(rules.seed + 14);
|
|
3664
|
+
function coverAt(x, z, density) {
|
|
3665
|
+
if (!inside(x, z) || wet(x, z) || slopeDegrees(data, x, z) > 34 || (tundra ? clamp012((tundraCover(x, z) - 0.47) / 0.3) : grassWeight(data, x, z)) < 0.22)
|
|
3666
|
+
return;
|
|
3667
|
+
const drift = coverDriftWeight(rules.drift, x, z);
|
|
3668
|
+
if (random2() > density * drift) return;
|
|
3669
|
+
const shore = rules.shoreGround && clampedHeight(data, x, z) < (data.waterLevel ?? 0) + rules.grassWaterMargin;
|
|
3670
|
+
put("grass", x, z, (shore ? 2.2 : rules.bladeScale) + random2() * rules.bladeJitter);
|
|
3671
|
+
if (desert) {
|
|
3672
|
+
for (let tuft = 0; tuft < 3; tuft++)
|
|
3673
|
+
put(
|
|
3674
|
+
"grass",
|
|
3675
|
+
x + (random2() - 0.5) * 1.4,
|
|
3676
|
+
z + (random2() - 0.5) * 1.4,
|
|
3677
|
+
0.65 + random2() * 0.55
|
|
3678
|
+
);
|
|
3679
|
+
}
|
|
3680
|
+
if (random2() < rules.scrubChance)
|
|
3681
|
+
put(
|
|
3682
|
+
"scrub",
|
|
3683
|
+
x + (random2() - 0.5),
|
|
3684
|
+
z + (random2() - 0.5),
|
|
3685
|
+
rules.scrubScale + random2() * rules.scrubJitter
|
|
3686
|
+
);
|
|
3687
|
+
if (rules.scrubBush && random2() < 0.02)
|
|
3688
|
+
put("bush", x + (random2() - 0.5) * 2, z + (random2() - 0.5) * 2, 0.6 + random2() * 0.45);
|
|
3689
|
+
}
|
|
3690
|
+
function cover2(x, z, density) {
|
|
3691
|
+
placing = !(rules.understorey.shade > 0 && shaded(x, z, rules.understorey.shadeRadius) && shadeDraw() < rules.understorey.shade);
|
|
3692
|
+
coverAt(x, z, density);
|
|
3693
|
+
placing = true;
|
|
3694
|
+
}
|
|
3695
|
+
for (let z = -half + 3; z < half - 3; z += rules.grassCell)
|
|
3696
|
+
for (let x = -half + 3; x < half - 3; x += rules.grassCell)
|
|
3697
|
+
cover2(
|
|
3698
|
+
x + (random2() - 0.5) * (temperate ? 1.5 : desert ? 6 : tundra ? 1.7 : 3),
|
|
3699
|
+
z + (random2() - 0.5) * (temperate ? 1.5 : desert ? 6 : tundra ? 1.7 : 3),
|
|
3700
|
+
0.7
|
|
3701
|
+
);
|
|
3702
|
+
const eyes = [focus, ...clearings.map(([x, z]) => ({ x, z }))];
|
|
3703
|
+
for (const eye of eyes) {
|
|
3704
|
+
const eyeStep = rules.grassDetailSpacing;
|
|
3705
|
+
const eyeReach = temperate ? GRASS_THIN : GRASS_FULL + 22;
|
|
3706
|
+
if (tundra) {
|
|
3707
|
+
for (let i = 0; i < Math.ceil(Math.PI * eyeReach ** 2 / eyeStep ** 2); i++) {
|
|
3708
|
+
const angle = random2() * Math.PI * 2;
|
|
3709
|
+
const distance = Math.sqrt(random2()) * eyeReach;
|
|
3710
|
+
const density = Math.max(
|
|
3711
|
+
0.08,
|
|
3712
|
+
1 - Math.max(0, distance - GRASS_FULL) / (eyeReach - GRASS_FULL)
|
|
3713
|
+
);
|
|
3714
|
+
cover2(eye.x + Math.cos(angle) * distance, eye.z + Math.sin(angle) * distance, density);
|
|
3715
|
+
}
|
|
3716
|
+
continue;
|
|
3717
|
+
}
|
|
3718
|
+
for (let dz = -eyeReach; dz < eyeReach; dz += eyeStep)
|
|
3719
|
+
for (let dx = -eyeReach; dx < eyeReach; dx += eyeStep) {
|
|
3720
|
+
const distance = Math.hypot(dx, dz);
|
|
3721
|
+
if (distance > eyeReach) continue;
|
|
3722
|
+
const density = Math.max(
|
|
3723
|
+
0.08,
|
|
3724
|
+
1 - Math.max(0, distance - GRASS_FULL) / (eyeReach - GRASS_FULL)
|
|
3725
|
+
);
|
|
3726
|
+
cover2(eye.x + dx + (random2() - 0.5) * 0.6, eye.z + dz + (random2() - 0.5) * 0.6, density);
|
|
3727
|
+
}
|
|
3728
|
+
for (let p = 0; p < (temperate ? 14 : 0); p++) {
|
|
3729
|
+
const angle = random2() * Math.PI * 2;
|
|
3730
|
+
const reach = random2() * 32;
|
|
3731
|
+
const px = eye.x + Math.cos(angle) * reach;
|
|
3732
|
+
const pz = eye.z + Math.sin(angle) * reach;
|
|
3733
|
+
if (forestWeight(px, pz) > 0.62 && !nearEye(px, pz)) continue;
|
|
3734
|
+
const radius = 2.5 + random2() * 3.5;
|
|
3735
|
+
for (let k = 0; k < 100; k++) {
|
|
3736
|
+
const x = px + (random2() - 0.5) * radius * 2;
|
|
3737
|
+
const z = pz + (random2() - 0.5) * radius * 2;
|
|
3738
|
+
if (Math.hypot(x - px, z - pz) > radius || slopeDegrees(data, x, z) > 28 || grassWeight(data, x, z) < 0.4)
|
|
3739
|
+
continue;
|
|
3740
|
+
put("poppy", x, z, 0.8 + random2() * 0.4);
|
|
3741
|
+
}
|
|
3742
|
+
}
|
|
3743
|
+
}
|
|
3744
|
+
if (temperate) {
|
|
3745
|
+
const dressing = createRandom(rules.seed + 12);
|
|
3746
|
+
const outcropSlope = Math.min(
|
|
3747
|
+
30,
|
|
3748
|
+
slopeQuantile(
|
|
3749
|
+
{ height: data.field.heights, resolution: data.resolution, size: data.size },
|
|
3750
|
+
0.98
|
|
3751
|
+
)
|
|
3752
|
+
);
|
|
3753
|
+
for (let z = -half + 12; z < half - 12; z += 12)
|
|
3754
|
+
for (let x = -half + 12; x < half - 12; x += 12) {
|
|
3755
|
+
const sx = x + (dressing() - 0.5) * 10;
|
|
3756
|
+
const sz = z + (dressing() - 0.5) * 10;
|
|
3757
|
+
const slope = slopeDegrees(data, sx, sz);
|
|
3758
|
+
const height = clampedHeight(data, sx, sz);
|
|
3759
|
+
const shore = data.waterLevel !== null && height > data.waterLevel + 0.4 && height < data.waterLevel + 7;
|
|
3760
|
+
if (nearEye(sx, sz) || wet(sx, sz)) continue;
|
|
3761
|
+
if (shore && slope > 12 && dressing() < 0.65)
|
|
3762
|
+
put("mountain", sx, sz, (5 + dressing() * 10) / 24, ":outcrop");
|
|
3763
|
+
else if (slope > outcropSlope && slope < 64 && dressing() < 0.6)
|
|
3764
|
+
put("mountain", sx, sz, (10 + dressing() * 10) / 24, ":outcrop");
|
|
3765
|
+
}
|
|
3766
|
+
}
|
|
3767
|
+
if (rules.cobbledBed)
|
|
3768
|
+
for (const river of data.rivers ?? [])
|
|
3769
|
+
for (let i = 1; i < river.points.length; i++) {
|
|
3770
|
+
const [ax = 0, ay = 0, az = 0] = river.points[i - 1] ?? [];
|
|
3771
|
+
const [bx = 0, by = 0, bz = 0] = river.points[i] ?? [];
|
|
3772
|
+
const length = Math.hypot(bx - ax, bz - az) || 1;
|
|
3773
|
+
const ux = (bx - ax) / length;
|
|
3774
|
+
const uz = (bz - az) / length;
|
|
3775
|
+
for (let at = 0; at < length; at += 0.6) {
|
|
3776
|
+
const level = ay + (by - ay) * at / length;
|
|
3777
|
+
const across = (o) => [ax + ux * at - uz * o, az + uz * at + ux * o];
|
|
3778
|
+
const depth = (o) => level - clampedHeight(data, ...across(o));
|
|
3779
|
+
for (let n = 0; n < 6; n++) {
|
|
3780
|
+
const o = (random2() - 0.5) * 18;
|
|
3781
|
+
const d = depth(o);
|
|
3782
|
+
if (d > 0.05 && d < 1.6)
|
|
3783
|
+
put("pebble", ...across(o), (0.08 + random2() ** 2 * 0.22) / rules.stoneRadius);
|
|
3784
|
+
}
|
|
3785
|
+
for (const side of [-1, 1]) {
|
|
3786
|
+
let edge = 0;
|
|
3787
|
+
while (edge < 12 && depth(side * edge) > 0) edge += 0.5;
|
|
3788
|
+
const o = side * (edge - 1 - random2() * 1.5);
|
|
3789
|
+
const d = depth(o);
|
|
3790
|
+
if (random2() < 0.08 && d > 0.12 && d < 0.5)
|
|
3791
|
+
put("pebble", ...across(o), (d + 0.12 + random2() * 0.2) / 1.6);
|
|
3792
|
+
const [x, z] = across(side * edge);
|
|
3793
|
+
const run = clamp012((sample01(x * 0.06, 11, z * 0.06) - 0.42) / 0.2);
|
|
3794
|
+
if (random2() < run * 0.45)
|
|
3795
|
+
for (let k = random2() < 0.4 ? 2 : 1; k > 0; k--)
|
|
3796
|
+
put(
|
|
3797
|
+
"riverrock",
|
|
3798
|
+
...across(side * (edge - 1.1 + random2() * 1.2)),
|
|
3799
|
+
0.6 + random2() ** 1.5 * 1.6
|
|
3800
|
+
);
|
|
3801
|
+
}
|
|
3802
|
+
}
|
|
3803
|
+
}
|
|
3804
|
+
const drowned = ({ asset, position: [x, , z] }) => GROWING.has(asset) && (TRUNKS.has(asset) ? BANK : BANK.slice(0, 1)).some(
|
|
3805
|
+
([dx, dz]) => wet(x + dx, z + dz) || nearRiver(x + dx, z + dz)
|
|
3806
|
+
);
|
|
3807
|
+
const growing = {};
|
|
3808
|
+
const kept = placements.filter((placement) => !drowned(placement)).map((placement) => {
|
|
3809
|
+
if (!GROWING.has(placement.asset)) return placement;
|
|
3810
|
+
const index = growing[placement.asset] ?? 0;
|
|
3811
|
+
growing[placement.asset] = index + 1;
|
|
3812
|
+
return { ...placement, id: `temperate-${placement.asset}:${index}` };
|
|
3813
|
+
});
|
|
3814
|
+
for (const asset of GROWING) if (asset in counts) counts[asset] = growing[asset] ?? 0;
|
|
3815
|
+
return { counts, placements: kept };
|
|
3816
|
+
}
|
|
3817
|
+
var GROWING = /* @__PURE__ */ new Set([
|
|
3818
|
+
"spruce",
|
|
3819
|
+
"sapling",
|
|
3820
|
+
"bush",
|
|
3821
|
+
"fern",
|
|
3822
|
+
"grass",
|
|
3823
|
+
"scrub",
|
|
3824
|
+
"poppy",
|
|
3825
|
+
"litter",
|
|
3826
|
+
"deadfall"
|
|
3827
|
+
]);
|
|
3828
|
+
var TRUNKS = /* @__PURE__ */ new Set(["spruce", "sapling"]);
|
|
3829
|
+
var BANK = [
|
|
3830
|
+
[0, 0],
|
|
3831
|
+
[1, 0],
|
|
3832
|
+
[-1, 0],
|
|
3833
|
+
[0, 1],
|
|
3834
|
+
[0, -1]
|
|
3835
|
+
];
|
|
3836
|
+
|
|
3837
|
+
// src/core/world-package.ts
|
|
3838
|
+
var HEIGHTMAP_MAX = 65535;
|
|
3839
|
+
var RECORD_VALUES = 8;
|
|
3840
|
+
var COMPONENTS = ["r", "g", "b", "a"];
|
|
3841
|
+
var PLANES = MATERIAL_IDS.length / COMPONENTS.length;
|
|
3842
|
+
var SCALE_TOLERANCE = 1e-3;
|
|
3843
|
+
var SLOTS = MATERIAL_IDS.map((id, channel) => ({
|
|
3844
|
+
channel,
|
|
3845
|
+
component: COMPONENTS[channel % COMPONENTS.length],
|
|
3846
|
+
componentIndex: channel % COMPONENTS.length,
|
|
3847
|
+
id,
|
|
3848
|
+
plane: Math.floor(channel / COMPONENTS.length)
|
|
3849
|
+
}));
|
|
3850
|
+
function poseOf(item) {
|
|
3851
|
+
if (item.transform === void 0)
|
|
3852
|
+
return {
|
|
3853
|
+
position: item.position,
|
|
3854
|
+
quaternion: [0, Math.sin(item.rotation / 2), 0, Math.cos(item.rotation / 2)],
|
|
3855
|
+
scale: [item.scale, item.scale, item.scale]
|
|
3856
|
+
};
|
|
3857
|
+
const { position, quaternion, scale } = item.transform;
|
|
3858
|
+
return { position, quaternion, scale };
|
|
3859
|
+
}
|
|
3860
|
+
function uniformScale(asset, id, scale) {
|
|
3861
|
+
const mean = (scale[0] + scale[1] + scale[2]) / 3;
|
|
3862
|
+
const magnitude = Math.abs(mean);
|
|
3863
|
+
const deviation = Math.max(
|
|
3864
|
+
Math.abs(scale[0] - mean),
|
|
3865
|
+
Math.abs(scale[1] - mean),
|
|
3866
|
+
Math.abs(scale[2] - mean)
|
|
3867
|
+
);
|
|
3868
|
+
if (!(magnitude > 0) || deviation > magnitude * SCALE_TOLERANCE)
|
|
3869
|
+
throw Error(
|
|
3870
|
+
`World package: placement '${id}' of asset '${asset}' is scaled ${JSON.stringify(scale)}; a placement record carries one uniform scale.`
|
|
3871
|
+
);
|
|
3872
|
+
return mean;
|
|
3873
|
+
}
|
|
3874
|
+
function writePlacementRecord(out, offset, item, extent) {
|
|
3875
|
+
if (item.transform === void 0 && !Number.isFinite(item.rotation))
|
|
3876
|
+
throw Error(`World package: placement '${item.id}' has a non-finite rotation.`);
|
|
3877
|
+
const { position, quaternion, scale } = poseOf(item);
|
|
3878
|
+
if (!Number.isFinite(position[0]) || !Number.isFinite(position[1]) || !Number.isFinite(position[2]))
|
|
3879
|
+
throw Error(
|
|
3880
|
+
`World package: placement '${item.id}' has a non-finite position [${position.join(", ")}].`
|
|
3881
|
+
);
|
|
3882
|
+
if (position[0] < extent.minX || position[0] > extent.minX + extent.sizeX || position[2] < extent.minZ || position[2] > extent.minZ + extent.sizeZ)
|
|
3883
|
+
throw Error(
|
|
3884
|
+
`World package: placement '${item.id}' position [${position[0]}, ${position[2]}] is outside extent bounds [${extent.minX}..${extent.minX + extent.sizeX}, ${extent.minZ}..${extent.minZ + extent.sizeZ}].`
|
|
3885
|
+
);
|
|
3886
|
+
const length = Math.hypot(...quaternion);
|
|
3887
|
+
if (!(length > 0)) throw Error(`World package: placement '${item.id}' has a zero quaternion.`);
|
|
3888
|
+
out[offset] = position[0];
|
|
3889
|
+
out[offset + 1] = position[1];
|
|
3890
|
+
out[offset + 2] = position[2];
|
|
3891
|
+
out[offset + 3] = quaternion[0] / length;
|
|
3892
|
+
out[offset + 4] = quaternion[1] / length;
|
|
3893
|
+
out[offset + 5] = quaternion[2] / length;
|
|
3894
|
+
out[offset + 6] = quaternion[3] / length;
|
|
3895
|
+
out[offset + 7] = uniformScale(item.asset, item.id, scale);
|
|
3896
|
+
}
|
|
3897
|
+
function placementCells(state, assets, cellSize, extent) {
|
|
3898
|
+
const cells = /* @__PURE__ */ new Map();
|
|
3899
|
+
const cellOf = (value, min, size) => Math.min(
|
|
3900
|
+
Math.max(1, Math.ceil(size / cellSize)) - 1,
|
|
3901
|
+
Math.max(0, Math.floor((value - min) / cellSize))
|
|
3902
|
+
);
|
|
3903
|
+
for (const item of state.instances) {
|
|
3904
|
+
if (assets[item.asset] === void 0)
|
|
3905
|
+
throw Error(
|
|
3906
|
+
`World package: placement '${item.id}' names asset '${item.asset}', which options.assets does not.`
|
|
3907
|
+
);
|
|
3908
|
+
const x = cellOf(item.position[0], extent.minX, extent.sizeX);
|
|
3909
|
+
const z = cellOf(item.position[2], extent.minZ, extent.sizeZ);
|
|
3910
|
+
const key = `${String(x)},${String(z)}`;
|
|
3911
|
+
let cell = cells.get(key);
|
|
3912
|
+
if (cell === void 0) {
|
|
3913
|
+
cell = { items: /* @__PURE__ */ new Map(), x, z };
|
|
3914
|
+
cells.set(key, cell);
|
|
3915
|
+
}
|
|
3916
|
+
const bucket = cell.items.get(item.asset);
|
|
3917
|
+
if (bucket === void 0) cell.items.set(item.asset, [item]);
|
|
3918
|
+
else bucket.push(item);
|
|
3919
|
+
}
|
|
3920
|
+
const buffer = new ArrayBuffer(state.instances.length * RECORD_VALUES * 4);
|
|
3921
|
+
const records = new Float32Array(buffer);
|
|
3922
|
+
const ordered = [];
|
|
3923
|
+
const ids = [];
|
|
3924
|
+
let written = 0;
|
|
3925
|
+
const byId = (left, right) => left[0] < right[0] ? -1 : 1;
|
|
3926
|
+
for (const cell of [...cells.values()].sort((a, b) => a.x - b.x || a.z - b.z)) {
|
|
3927
|
+
const runs = [];
|
|
3928
|
+
for (const [asset, items] of [...cell.items.entries()].sort(byId)) {
|
|
3929
|
+
runs.push({ asset, count: items.length, offset: written });
|
|
3930
|
+
for (const item of items) {
|
|
3931
|
+
writePlacementRecord(records, written * RECORD_VALUES, item, extent);
|
|
3932
|
+
ids.push(item.id);
|
|
3933
|
+
written += 1;
|
|
3934
|
+
}
|
|
3935
|
+
}
|
|
3936
|
+
ordered.push({ runs, x: cell.x, z: cell.z });
|
|
3937
|
+
}
|
|
3938
|
+
return { bytes: new Uint8Array(buffer), cells: ordered, ids };
|
|
3939
|
+
}
|
|
3940
|
+
function bakeWorldPackage(state, options) {
|
|
3941
|
+
const { size, resolution } = state;
|
|
3942
|
+
const cellSize = options.cellSize ?? 64;
|
|
3943
|
+
if (!Number.isFinite(cellSize) || cellSize <= 0)
|
|
3944
|
+
throw Error(
|
|
3945
|
+
`World package: cellSize must be a positive finite number, got ${String(cellSize)}.`
|
|
3946
|
+
);
|
|
3947
|
+
const { assets, layers, names } = options;
|
|
3948
|
+
const heightmapName = names?.heightmap ?? "heightmap.u16";
|
|
3949
|
+
const placementsName = names?.placements ?? "placements.bin";
|
|
3950
|
+
const placementIdsName = names?.placementIds ?? "placement-ids.json";
|
|
3951
|
+
const splatName = names?.splat ?? "splat.rgba";
|
|
3952
|
+
const extent = { minX: -size / 2, minZ: -size / 2, sizeX: size, sizeZ: size };
|
|
3953
|
+
let heightMin = Number.POSITIVE_INFINITY;
|
|
3954
|
+
let heightMax = Number.NEGATIVE_INFINITY;
|
|
3955
|
+
for (const height of state.height) {
|
|
3956
|
+
if (!Number.isFinite(height)) throw Error("World package: terrain heights must be finite.");
|
|
3957
|
+
heightMin = Math.min(heightMin, height);
|
|
3958
|
+
heightMax = Math.max(heightMax, height);
|
|
3959
|
+
}
|
|
3960
|
+
if (heightMax - heightMin < 1e-6) heightMax = heightMin + 1;
|
|
3961
|
+
const heightmap = new Uint8Array(resolution * resolution * 2);
|
|
3962
|
+
const heights = new DataView(heightmap.buffer);
|
|
3963
|
+
for (let i = 0; i < state.height.length; i += 1)
|
|
3964
|
+
heights.setUint16(
|
|
3965
|
+
i * 2,
|
|
3966
|
+
Math.round(
|
|
3967
|
+
(state.height[i] - heightMin) / (heightMax - heightMin) * HEIGHTMAP_MAX
|
|
3968
|
+
),
|
|
3969
|
+
true
|
|
3970
|
+
);
|
|
3971
|
+
const placements = placementCells(state, assets, cellSize, extent);
|
|
3972
|
+
return {
|
|
3973
|
+
files: {
|
|
3974
|
+
[heightmapName]: heightmap,
|
|
3975
|
+
[placementsName]: placements.bytes,
|
|
3976
|
+
[placementIdsName]: new TextEncoder().encode(`${JSON.stringify(placements.ids)}
|
|
3977
|
+
`),
|
|
3978
|
+
[splatName]: splatBytes(state, resolution)
|
|
3979
|
+
},
|
|
3980
|
+
manifest: {
|
|
3981
|
+
assets,
|
|
3982
|
+
cellSize,
|
|
3983
|
+
cells: placements.cells,
|
|
3984
|
+
extent,
|
|
3985
|
+
placements: placementsName,
|
|
3986
|
+
placementIds: placementIdsName,
|
|
3987
|
+
terrain: {
|
|
3988
|
+
columns: resolution,
|
|
3989
|
+
heightMax,
|
|
3990
|
+
heightMin,
|
|
3991
|
+
heightmap: heightmapName,
|
|
3992
|
+
layers: { splat: splatName, ...layers },
|
|
3993
|
+
rows: resolution,
|
|
3994
|
+
spacing: size / (resolution - 1)
|
|
3995
|
+
},
|
|
3996
|
+
version: 1
|
|
3997
|
+
},
|
|
3998
|
+
splat: {
|
|
3999
|
+
masks: Object.fromEntries(SLOTS.map(({ component, id, plane }) => [id, [plane, component]])),
|
|
4000
|
+
planes: PLANES,
|
|
4001
|
+
size: resolution
|
|
4002
|
+
}
|
|
4003
|
+
};
|
|
4004
|
+
}
|
|
4005
|
+
function splatBytes(state, resolution) {
|
|
4006
|
+
const planeBytes = resolution * resolution * 4;
|
|
4007
|
+
if (state.splat.length !== resolution * resolution * MATERIAL_IDS.length)
|
|
4008
|
+
throw Error(
|
|
4009
|
+
`World package: splat has ${String(state.splat.length)} weights, expected ${String(resolution * resolution * MATERIAL_IDS.length)}.`
|
|
4010
|
+
);
|
|
4011
|
+
const bytes = new Uint8Array(planeBytes * PLANES);
|
|
4012
|
+
for (let row = 0; row < resolution; row += 1) {
|
|
4013
|
+
const source = (resolution - 1 - row) * resolution;
|
|
4014
|
+
const rowOffset = row * resolution * 4;
|
|
4015
|
+
for (let column = 0; column < resolution; column += 1) {
|
|
4016
|
+
const sample = (source + column) * MATERIAL_IDS.length;
|
|
4017
|
+
const rowCol = rowOffset + column * 4;
|
|
4018
|
+
for (const slot of SLOTS) {
|
|
4019
|
+
const weight = state.splat[sample + slot.channel];
|
|
4020
|
+
bytes[slot.plane * planeBytes + rowCol + slot.componentIndex] = Math.round(
|
|
4021
|
+
(weight <= 0 ? 0 : weight >= 1 ? 1 : weight) * 255
|
|
4022
|
+
);
|
|
4023
|
+
}
|
|
4024
|
+
}
|
|
4025
|
+
}
|
|
4026
|
+
return bytes;
|
|
4027
|
+
}
|
|
4028
|
+
|
|
4029
|
+
// src/core/bake-biome.ts
|
|
4030
|
+
var BAKE_BIOME_STAGES = [
|
|
4031
|
+
"surfaceSample",
|
|
4032
|
+
"densityFilter",
|
|
4033
|
+
"selfPrune",
|
|
4034
|
+
"meshSpawn"
|
|
4035
|
+
];
|
|
4036
|
+
function withImpostorDistances(assets, spec) {
|
|
4037
|
+
if (assets === void 0) return void 0;
|
|
4038
|
+
const distances = /* @__PURE__ */ new Map();
|
|
4039
|
+
for (const foliage2 of spec.foliage) {
|
|
4040
|
+
const distance = foliage2.far?.impostor;
|
|
4041
|
+
if (distance === void 0) continue;
|
|
4042
|
+
if (!Number.isFinite(distance) || distance <= 0)
|
|
4043
|
+
throw new Error(
|
|
4044
|
+
`${spec.look}: ${foliage2.species}.far.impostor must be a positive distance, got ${String(distance)}.`
|
|
4045
|
+
);
|
|
4046
|
+
distances.set(foliage2.species, distance);
|
|
4047
|
+
}
|
|
4048
|
+
if (distances.size === 0) return assets;
|
|
4049
|
+
const filled = {};
|
|
4050
|
+
for (const [species2, asset] of Object.entries(assets)) {
|
|
4051
|
+
const distance = distances.get(species2);
|
|
4052
|
+
if (distance === void 0 || asset.impostorDistance !== void 0) {
|
|
4053
|
+
filled[species2] = asset;
|
|
4054
|
+
continue;
|
|
4055
|
+
}
|
|
4056
|
+
const gate = asset.lods?.reduce((furthest, lod) => Math.max(furthest, lod.distance), 0) ?? 0;
|
|
4057
|
+
if (distance <= gate)
|
|
4058
|
+
throw new Error(
|
|
4059
|
+
`${species2}: impostorDistance (${String(distance)}) must follow every authored LOD gate (furthest ${String(gate)}), or the impostor replaces a level that still draws.`
|
|
4060
|
+
);
|
|
4061
|
+
filled[species2] = { ...asset, impostorDistance: distance };
|
|
4062
|
+
}
|
|
4063
|
+
return filled;
|
|
4064
|
+
}
|
|
4065
|
+
function bakeBiome(options) {
|
|
4066
|
+
const spec = validateBiome(options.spec, options.spec.look);
|
|
4067
|
+
const scatter = (options.stream ?? spec.stream) === "ordered" ? scatterProps(
|
|
4068
|
+
orderedField(options.state, spec),
|
|
4069
|
+
{ x: spec.spawn.focus[0], z: spec.spawn.focus[1] },
|
|
4070
|
+
spec.cover.clearings
|
|
4071
|
+
) : null;
|
|
4072
|
+
const state = scatter === null ? options.state : { ...options.state, instances: scatter.placements };
|
|
4073
|
+
const assets = withImpostorDistances(options.assets, spec);
|
|
4074
|
+
const baked = assets === void 0 ? null : bakeWorldPackage(state, {
|
|
4075
|
+
assets,
|
|
4076
|
+
...options.cellSize === void 0 ? {} : { cellSize: options.cellSize },
|
|
4077
|
+
...options.layers === void 0 ? {} : { layers: options.layers },
|
|
4078
|
+
...options.names === void 0 ? {} : { names: options.names }
|
|
4079
|
+
});
|
|
4080
|
+
const counts = /* @__PURE__ */ new Map();
|
|
4081
|
+
if (baked !== null) {
|
|
4082
|
+
let placed = 0;
|
|
4083
|
+
for (const cell of baked.manifest.cells)
|
|
4084
|
+
for (const run of cell.runs) {
|
|
4085
|
+
placed += run.count;
|
|
4086
|
+
counts.set(run.asset, (counts.get(run.asset) ?? 0) + run.count);
|
|
4087
|
+
}
|
|
4088
|
+
if (placed !== state.instances.length)
|
|
4089
|
+
throw new Error(
|
|
4090
|
+
`${spec.look}: the package carries ${String(placed)} placements for ${String(state.instances.length)} instances.`
|
|
4091
|
+
);
|
|
4092
|
+
} else if (scatter !== null) {
|
|
4093
|
+
for (const [asset, count2] of Object.entries(scatter.counts)) counts.set(asset, count2);
|
|
4094
|
+
}
|
|
4095
|
+
return {
|
|
4096
|
+
package: baked,
|
|
4097
|
+
bands: measureBands(spec.layers, options.state.height),
|
|
4098
|
+
placements: [...counts.entries()].map(([asset, count2]) => ({ asset, count: count2 })),
|
|
4099
|
+
scatter
|
|
4100
|
+
};
|
|
4101
|
+
}
|
|
4102
|
+
function orderedField(state, spec) {
|
|
4103
|
+
return {
|
|
4104
|
+
erosion: state.erosion,
|
|
4105
|
+
field: new Heightfield({
|
|
4106
|
+
rows: state.resolution,
|
|
4107
|
+
columns: state.resolution,
|
|
4108
|
+
width: state.size,
|
|
4109
|
+
depth: state.size,
|
|
4110
|
+
origin: { x: 0, z: 0 },
|
|
4111
|
+
heights: Float32Array.from(state.height)
|
|
4112
|
+
}),
|
|
4113
|
+
resolution: state.resolution,
|
|
4114
|
+
size: state.size,
|
|
4115
|
+
waterLevel: state.waters.find((water2) => water2.kind === "ocean")?.level ?? null,
|
|
4116
|
+
rules: orderedScatterRules(spec),
|
|
4117
|
+
spec,
|
|
4118
|
+
rivers: state.rivers,
|
|
4119
|
+
lakes: state.waters.flatMap(
|
|
4120
|
+
(water2) => water2.kind === "lake" && water2.at !== void 0 && water2.radius !== void 0 ? [{ at: water2.at, radius: water2.radius, level: water2.level ?? 0 }] : []
|
|
4121
|
+
)
|
|
4122
|
+
};
|
|
4123
|
+
}
|
|
4124
|
+
function collarSampler(collar) {
|
|
4125
|
+
const at = (x, z) => collar.heightAt(x, z);
|
|
4126
|
+
const paint = (attribute, x, z, value) => {
|
|
4127
|
+
const quad = collar.locate(x, z);
|
|
4128
|
+
if (quad === void 0) return;
|
|
4129
|
+
for (const index of [
|
|
4130
|
+
collar.index(quad.lower, quad.from),
|
|
4131
|
+
collar.index(quad.lower, quad.to),
|
|
4132
|
+
collar.index(quad.upper, quad.from),
|
|
4133
|
+
collar.index(quad.upper, quad.to)
|
|
4134
|
+
])
|
|
4135
|
+
attribute.setX(index, Math.max(attribute.getX(index), value));
|
|
4136
|
+
};
|
|
4137
|
+
return { at, paint };
|
|
4138
|
+
}
|
|
4139
|
+
var macroNoise = new ImprovedNoise();
|
|
4140
|
+
function farForestPlacements(options) {
|
|
4141
|
+
const rules = options.rules;
|
|
4142
|
+
const half = options.size / 2;
|
|
4143
|
+
const collar = collarSampler(options.collar);
|
|
4144
|
+
const random2 = createRandom(rules.seed);
|
|
4145
|
+
const rivers = createSegmentIndex(
|
|
4146
|
+
(options.rivers ?? []).flatMap(
|
|
4147
|
+
(river) => river.points.flatMap((to, index) => {
|
|
4148
|
+
const from = river.points[index - 1];
|
|
4149
|
+
if (!from) return [];
|
|
4150
|
+
const a = [from[0] ?? 0, from[2] ?? 0];
|
|
4151
|
+
const b = [to[0] ?? 0, to[2] ?? 0];
|
|
4152
|
+
const reach = river.width * 2.5;
|
|
4153
|
+
return [{ a, b, reach, data: { a, b, reach } }];
|
|
4154
|
+
})
|
|
4155
|
+
),
|
|
4156
|
+
32
|
|
4157
|
+
);
|
|
4158
|
+
const inRiver = (x, z) => rivers.nearby(x, z).some(({ a, b, reach }) => {
|
|
4159
|
+
const dx = b[0] - a[0];
|
|
4160
|
+
const dz = b[1] - a[1];
|
|
4161
|
+
const t = MathUtils.clamp(
|
|
4162
|
+
((x - a[0]) * dx + (z - a[1]) * dz) / (dx * dx + dz * dz || 1),
|
|
4163
|
+
0,
|
|
4164
|
+
1
|
|
4165
|
+
);
|
|
4166
|
+
return Math.hypot(x - a[0] - t * dx, z - a[1] - t * dz) < reach;
|
|
4167
|
+
});
|
|
4168
|
+
const sea = options.seaLevel ?? Number.NEGATIVE_INFINITY;
|
|
4169
|
+
const wet = (x, z, height) => height < sea + 0.35 || inRiver(x, z) || (options.lakes ?? []).some(
|
|
4170
|
+
(lake) => Math.hypot(x - (lake.at[0] ?? 0), z - (lake.at[1] ?? 0)) < lake.radius && height < lake.level + 0.35
|
|
4171
|
+
);
|
|
4172
|
+
const canopy = options.collar.geometry.getAttribute("canopy");
|
|
4173
|
+
const placements = [];
|
|
4174
|
+
const [slopeFrom, slopeTo] = rules.slope;
|
|
4175
|
+
const [standFrom, standTo] = rules.stand;
|
|
4176
|
+
const [treeFrom, treeTo] = options.treeline;
|
|
4177
|
+
for (let gz = -rules.reach; gz < rules.reach; gz += rules.cell)
|
|
4178
|
+
for (let gx = -rules.reach; gx < rules.reach; gx += rules.cell) {
|
|
4179
|
+
const x = gx + (0.1 + 0.8 * random2()) * rules.cell;
|
|
4180
|
+
const z = gz + (0.1 + 0.8 * random2()) * rules.cell;
|
|
4181
|
+
const keep = random2();
|
|
4182
|
+
const past = Math.max(Math.abs(x), Math.abs(z)) - half;
|
|
4183
|
+
if (past < 2) continue;
|
|
4184
|
+
const spacing = MathUtils.lerp(
|
|
4185
|
+
rules.spacing[0],
|
|
4186
|
+
rules.spacing[1],
|
|
4187
|
+
MathUtils.smoothstep(past, rules.spacingAt[0], rules.spacingAt[1])
|
|
4188
|
+
);
|
|
4189
|
+
const spaced = Math.min(1, (rules.cell / spacing) ** 2);
|
|
4190
|
+
if (keep >= spaced) continue;
|
|
4191
|
+
if ((options.clear ?? []).some(([cx, cz, r]) => Math.hypot(x - cx, z - cz) < r)) continue;
|
|
4192
|
+
const height = collar.at(x, z);
|
|
4193
|
+
if (height === null) continue;
|
|
4194
|
+
const step = 4;
|
|
4195
|
+
const dx = (collar.at(x + step, z) ?? height) - (collar.at(x - step, z) ?? height);
|
|
4196
|
+
const dz = (collar.at(x, z + step) ?? height) - (collar.at(x, z - step) ?? height);
|
|
4197
|
+
const slope = MathUtils.radToDeg(Math.atan(Math.hypot(dx, dz) / (2 * step)));
|
|
4198
|
+
const flat = 1 - MathUtils.smoothstep(slope, slopeFrom, slopeTo);
|
|
4199
|
+
if (flat <= 0) continue;
|
|
4200
|
+
const meadow = 1 - (1 - MathUtils.smoothstep(macroNoise.noise(x * 21e-4, 5.3, z * 21e-4), -0.3, -0.12)) * (1 - MathUtils.smoothstep(slope, rules.meadowSlope[0], rules.meadowSlope[1]));
|
|
4201
|
+
if (meadow <= 0) continue;
|
|
4202
|
+
const around = ((collar.at(x + 40, z) ?? height) + (collar.at(x - 40, z) ?? height) + (collar.at(x, z + 40) ?? height) + (collar.at(x, z - 40) ?? height)) / 4;
|
|
4203
|
+
const damp = MathUtils.clamp((around - height) / 8 + 0.3, 0, 1);
|
|
4204
|
+
const stand = MathUtils.smoothstep(
|
|
4205
|
+
fieldWeight(STAND_FIELD, x, z) + (damp - 0.3) * 0.2,
|
|
4206
|
+
standFrom,
|
|
4207
|
+
standTo
|
|
4208
|
+
);
|
|
4209
|
+
if (stand <= 0) continue;
|
|
4210
|
+
const jitter = macroNoise.noise(x * 0.011, 17.7, z * 0.011) * rules.treelineJitter;
|
|
4211
|
+
const alpine = 1 - MathUtils.smoothstep(height + jitter, treeFrom, treeTo);
|
|
4212
|
+
if (alpine <= 0 || wet(x, z, height)) continue;
|
|
4213
|
+
if (random2() >= meadow * stand * alpine * flat) continue;
|
|
4214
|
+
const age = MathUtils.clamp(macroNoise.noise(x * 0.021, 7, z * 0.021) * 0.5 + 0.5, 0, 1);
|
|
4215
|
+
const scale = (0.55 + 1.05 * age ** 1.6 + random2() * 0.16) * (0.6 + 0.4 * Math.min(stand, alpine)) * (1 + 0.25 * damp);
|
|
4216
|
+
const index = placements.length;
|
|
4217
|
+
placements.push({
|
|
4218
|
+
asset: options.asset,
|
|
4219
|
+
id: `temperate-${options.asset}:far-${index}`,
|
|
4220
|
+
layer: options.layer,
|
|
4221
|
+
alignToNormal: false,
|
|
4222
|
+
normal: [0, 1, 0],
|
|
4223
|
+
position: [x, 0, z],
|
|
4224
|
+
rotation: random2() * Math.PI * 2,
|
|
4225
|
+
scale
|
|
4226
|
+
});
|
|
4227
|
+
if (canopy) collar.paint(canopy, x, z, 1);
|
|
4228
|
+
}
|
|
4229
|
+
if (canopy) canopy.needsUpdate = true;
|
|
4230
|
+
return placements;
|
|
4231
|
+
}
|
|
4232
|
+
|
|
4233
|
+
// src/core/massif.ts
|
|
4234
|
+
function perimeterBase(n, edge, window, decay) {
|
|
4235
|
+
const loop = 4 * (n - 1);
|
|
4236
|
+
const at = (k) => {
|
|
4237
|
+
const m = (k % loop + loop) % loop;
|
|
4238
|
+
const side = Math.floor(m / (n - 1));
|
|
4239
|
+
const t = m % (n - 1);
|
|
4240
|
+
return side === 0 ? [0, t] : side === 1 ? [t, n - 1] : side === 2 ? [n - 1, n - 1 - t] : [n - 1 - t, 0];
|
|
4241
|
+
};
|
|
4242
|
+
const raw = Float64Array.from({ length: loop }, (_, k) => {
|
|
4243
|
+
const [row, column] = at(k);
|
|
4244
|
+
return edge(row * n + column);
|
|
4245
|
+
});
|
|
4246
|
+
const prefix = new Float64Array(3 * loop + 1);
|
|
4247
|
+
for (let k = 0; k < 3 * loop; k++)
|
|
4248
|
+
prefix[k + 1] = prefix[k] + raw[k % loop];
|
|
4249
|
+
const half = Math.max(1, Math.round(window / 2));
|
|
4250
|
+
const smooth = Float64Array.from(
|
|
4251
|
+
{ length: loop },
|
|
4252
|
+
(_, k) => (prefix[loop + k + half + 1] - prefix[loop + k - half]) / (2 * half + 1)
|
|
4253
|
+
);
|
|
4254
|
+
const index = (row, column) => {
|
|
4255
|
+
if (row === 0) return column;
|
|
4256
|
+
if (column === n - 1) return n - 1 + row;
|
|
4257
|
+
if (row === n - 1) return 2 * (n - 1) + (n - 1 - column);
|
|
4258
|
+
return 3 * (n - 1) + (n - 1 - row);
|
|
4259
|
+
};
|
|
4260
|
+
const s = (row, column) => smooth[index(row, column) % loop];
|
|
4261
|
+
const r = (row, column) => raw[index(row, column) % loop] - smooth[index(row, column) % loop];
|
|
4262
|
+
const base = new Float32Array(n * n);
|
|
4263
|
+
const last = n - 1;
|
|
4264
|
+
for (let row = 0; row < n; row++) {
|
|
4265
|
+
const v = row / last;
|
|
4266
|
+
for (let column = 0; column < n; column++) {
|
|
4267
|
+
const u = column / last;
|
|
4268
|
+
const coons = (1 - v) * s(0, column) + v * s(last, column) + (1 - u) * s(row, 0) + u * s(row, last) - ((1 - u) * (1 - v) * s(0, 0) + u * (1 - v) * s(0, last) + (1 - u) * v * s(last, 0) + u * v * s(last, last));
|
|
4269
|
+
const detail = r(0, column) * Math.exp(-row / decay) + r(last, column) * Math.exp(-(last - row) / decay) + r(row, 0) * Math.exp(-column / decay) + r(row, last) * Math.exp(-(last - column) / decay);
|
|
4270
|
+
base[row * n + column] = coons + detail;
|
|
4271
|
+
}
|
|
4272
|
+
}
|
|
4273
|
+
for (let k = 0; k < loop; k++) {
|
|
4274
|
+
const [row, column] = at(k);
|
|
4275
|
+
base[row * n + column] = raw[k];
|
|
4276
|
+
}
|
|
4277
|
+
return base;
|
|
4278
|
+
}
|
|
4279
|
+
function liftMassif(tile) {
|
|
4280
|
+
const n = tile.resolution;
|
|
4281
|
+
if (tile.heights.length !== n * n)
|
|
4282
|
+
throw new RangeError(`Massif ${tile.id}: ${n}^2 heights expected`);
|
|
4283
|
+
const spacing = tile.size / (n - 1);
|
|
4284
|
+
const base = perimeterBase(n, (i) => tile.heights[i], 200 / spacing, 50 / spacing);
|
|
4285
|
+
return Float32Array.from(
|
|
4286
|
+
{ length: n * n },
|
|
4287
|
+
(_, i) => tile.heights[i] - base[i]
|
|
4288
|
+
);
|
|
4289
|
+
}
|
|
4290
|
+
var catmullRom = (a, b, c, d, t) => b + 0.5 * t * (c - a + t * (2 * a - 5 * b + 4 * c - d + t * (3 * (b - c) + d - a)));
|
|
4291
|
+
function faceDirection(lifted, n, spacing) {
|
|
4292
|
+
let x = 0;
|
|
4293
|
+
let z = 0;
|
|
4294
|
+
const step = Math.max(1, Math.floor(n / 128));
|
|
4295
|
+
for (let row = step; row < n - step; row += step)
|
|
4296
|
+
for (let column = step; column < n - step; column += step) {
|
|
4297
|
+
if (lifted[row * n + column] < 50) continue;
|
|
4298
|
+
const dx = (lifted[row * n + column + step] - lifted[row * n + column - step]) / (2 * step * spacing);
|
|
4299
|
+
const dz = (lifted[(row + step) * n + column] - lifted[(row - step) * n + column]) / (2 * step * spacing);
|
|
4300
|
+
const g2 = dx * dx + dz * dz;
|
|
4301
|
+
if (g2 < 1) continue;
|
|
4302
|
+
const g = Math.sqrt(g2);
|
|
4303
|
+
x -= dx / g * g2;
|
|
4304
|
+
z -= dz / g * g2;
|
|
4305
|
+
}
|
|
4306
|
+
return Math.atan2(z, x);
|
|
4307
|
+
}
|
|
4308
|
+
var KEEP_OUT_RAMP = 600;
|
|
4309
|
+
function placeMassif(terrain, options) {
|
|
4310
|
+
const draw = random(options.seed * 2654435761 ^ 1835103091);
|
|
4311
|
+
const tiles = Array.isArray(options.tile) ? options.tile : [options.tile];
|
|
4312
|
+
if (tiles.length === 0) throw new RangeError("placeMassif needs at least one tile");
|
|
4313
|
+
const tile = tiles[Math.floor(draw() * tiles.length) % tiles.length];
|
|
4314
|
+
const lifted = liftMassif(tile);
|
|
4315
|
+
const n = tile.resolution;
|
|
4316
|
+
const spacing = tile.size / (n - 1);
|
|
4317
|
+
let summit = 0;
|
|
4318
|
+
for (let i = 1; i < lifted.length; i++)
|
|
4319
|
+
if (lifted[i] > lifted[summit]) summit = i;
|
|
4320
|
+
const tall = lifted[summit];
|
|
4321
|
+
if (!(tall > 0)) throw new RangeError(`Massif ${tile.id} has no relief above its border`);
|
|
4322
|
+
const mirror = draw() < 0.5;
|
|
4323
|
+
const local = (i) => {
|
|
4324
|
+
const x = (i % n / (n - 1) - 0.5) * tile.size;
|
|
4325
|
+
return [mirror ? -x : x, (Math.floor(i / n) / (n - 1) - 0.5) * tile.size];
|
|
4326
|
+
};
|
|
4327
|
+
const [sx, sz] = local(summit);
|
|
4328
|
+
const state = terrain.evaluate();
|
|
4329
|
+
const N = state.resolution;
|
|
4330
|
+
const cell = state.size / (N - 1);
|
|
4331
|
+
const halfWorld = state.size / 2;
|
|
4332
|
+
const at = options.at !== void 0 ? [options.at[0], options.at[1]] : (() => {
|
|
4333
|
+
const [b0, b1] = options.bearing ?? [0, 360];
|
|
4334
|
+
const [d0, d1] = options.distance ?? [tile.size / 2, halfWorld - tile.size / 4];
|
|
4335
|
+
const bearing = (b0 + draw() * (b1 - b0)) * Math.PI / 180;
|
|
4336
|
+
const distance = d0 + draw() * (d1 - d0);
|
|
4337
|
+
return [Math.cos(bearing) * distance, Math.sin(bearing) * distance];
|
|
4338
|
+
})();
|
|
4339
|
+
if (options.at !== void 0) {
|
|
4340
|
+
draw();
|
|
4341
|
+
draw();
|
|
4342
|
+
}
|
|
4343
|
+
const faceTile = faceDirection(lifted, n, spacing);
|
|
4344
|
+
const face = mirror ? Math.PI - faceTile : faceTile;
|
|
4345
|
+
const toCentre = options.face !== void 0 ? options.face * Math.PI / 180 : Math.atan2(-at[1], -at[0]);
|
|
4346
|
+
const jitter = (draw() - 0.5) * 40 * Math.PI / 180;
|
|
4347
|
+
const turn = toCentre + jitter - face;
|
|
4348
|
+
const pick = draw();
|
|
4349
|
+
const keepOut = options.keepOut ?? 320;
|
|
4350
|
+
const cos = Math.cos(turn);
|
|
4351
|
+
const sin = Math.sin(turn);
|
|
4352
|
+
const toTile = (x, z) => {
|
|
4353
|
+
const dx = x - at[0];
|
|
4354
|
+
const dz = z - at[1];
|
|
4355
|
+
const mx = sx + dx * cos + dz * sin;
|
|
4356
|
+
const mz = sz - dx * sin + dz * cos;
|
|
4357
|
+
return [(mirror ? -mx : mx) / tile.size + 0.5, mz / tile.size + 0.5];
|
|
4358
|
+
};
|
|
4359
|
+
const toWorld = (u, v) => {
|
|
4360
|
+
const mx = (mirror ? -1 : 1) * (u - 0.5) * tile.size - sx;
|
|
4361
|
+
const mz = (v - 0.5) * tile.size - sz;
|
|
4362
|
+
return [at[0] + mx * cos - mz * sin, at[1] + mx * sin + mz * cos];
|
|
4363
|
+
};
|
|
4364
|
+
const hostAt = (x, z) => sampleGrid(state.height, N, N, (x + halfWorld) / state.size, (z + halfWorld) / state.size);
|
|
4365
|
+
const hostSmooth = (x, z) => {
|
|
4366
|
+
const gx = clamp((x + halfWorld) / cell, 0, N - 1);
|
|
4367
|
+
const gz = clamp((z + halfWorld) / cell, 0, N - 1);
|
|
4368
|
+
const ix = Math.min(N - 2, Math.floor(gx));
|
|
4369
|
+
const iz = Math.min(N - 2, Math.floor(gz));
|
|
4370
|
+
const tx = gx - ix;
|
|
4371
|
+
const tz = gz - iz;
|
|
4372
|
+
const h = (r, c) => state.height[clamp(r, 0, N - 1) * N + clamp(c, 0, N - 1)];
|
|
4373
|
+
const row = (r) => catmullRom(h(r, ix - 1), h(r, ix), h(r, ix + 1), h(r, ix + 2), tx);
|
|
4374
|
+
return catmullRom(row(iz - 1), row(iz), row(iz + 1), row(iz + 2), tz);
|
|
4375
|
+
};
|
|
4376
|
+
const B = 129;
|
|
4377
|
+
const hostEdge = new Float32Array(B * B);
|
|
4378
|
+
for (let row = 0; row < B; row++)
|
|
4379
|
+
for (let column = 0; column < B; column++)
|
|
4380
|
+
if (row === 0 || column === 0 || row === B - 1 || column === B - 1)
|
|
4381
|
+
hostEdge[row * B + column] = hostAt(...toWorld(column / (B - 1), row / (B - 1)));
|
|
4382
|
+
const hostBase = perimeterBase(
|
|
4383
|
+
B,
|
|
4384
|
+
(i) => hostEdge[i],
|
|
4385
|
+
200 / (tile.size / (B - 1)),
|
|
4386
|
+
1e-6
|
|
4387
|
+
);
|
|
4388
|
+
const foot = sampleGrid(hostBase, B, B, ...toTile(at[0], at[1]));
|
|
4389
|
+
let floor = Number.POSITIVE_INFINITY;
|
|
4390
|
+
for (let i = 0; i < B * B; i++) {
|
|
4391
|
+
const row = Math.floor(i / B);
|
|
4392
|
+
const column = i % B;
|
|
4393
|
+
if (row === 0 || column === 0 || row === B - 1 || column === B - 1)
|
|
4394
|
+
floor = Math.min(floor, hostEdge[i]);
|
|
4395
|
+
}
|
|
4396
|
+
let scale;
|
|
4397
|
+
if (options.top) {
|
|
4398
|
+
const top = options.top[0] + pick * (options.top[1] - options.top[0]);
|
|
4399
|
+
scale = Number.POSITIVE_INFINITY;
|
|
4400
|
+
for (let row = 0; row < n; row += 4)
|
|
4401
|
+
for (let column = 0; column < n; column += 4) {
|
|
4402
|
+
const rise = lifted[row * n + column];
|
|
4403
|
+
if (rise < 1) continue;
|
|
4404
|
+
const u = column / (n - 1);
|
|
4405
|
+
const v = row / (n - 1);
|
|
4406
|
+
const [x, z] = toWorld(u, v);
|
|
4407
|
+
if (Math.abs(x) > halfWorld || Math.abs(z) > halfWorld) continue;
|
|
4408
|
+
scale = Math.min(scale, (top - sampleGrid(hostBase, B, B, u, v)) / rise);
|
|
4409
|
+
}
|
|
4410
|
+
} else {
|
|
4411
|
+
const [h0, h1] = options.height ?? [0.85 * tall, 1.15 * tall];
|
|
4412
|
+
scale = (h0 + pick * (h1 - h0)) / tall;
|
|
4413
|
+
}
|
|
4414
|
+
if (!(scale > 0))
|
|
4415
|
+
throw new RangeError(`Massif ${tile.id}: top under its foot at ${foot.toFixed(0)} m`);
|
|
4416
|
+
const height = scale * tall;
|
|
4417
|
+
const factor = Math.max(1, Math.round(cell / spacing));
|
|
4418
|
+
const coarseN = Math.floor((n - 1) / factor) + 1;
|
|
4419
|
+
const coarse = new Float32Array(coarseN * coarseN);
|
|
4420
|
+
const tent = (d) => factor - Math.abs(d);
|
|
4421
|
+
for (let row = 0; row < coarseN; row++)
|
|
4422
|
+
for (let column = 0; column < coarseN; column++) {
|
|
4423
|
+
let sum = 0;
|
|
4424
|
+
let total = 0;
|
|
4425
|
+
for (let dr = 1 - factor; dr < factor; dr++)
|
|
4426
|
+
for (let dc = 1 - factor; dc < factor; dc++) {
|
|
4427
|
+
const r = row * factor + dr;
|
|
4428
|
+
const c = column * factor + dc;
|
|
4429
|
+
if (r < 0 || c < 0 || r >= n || c >= n) continue;
|
|
4430
|
+
const w = tent(dr) * tent(dc);
|
|
4431
|
+
sum += w * lifted[r * n + c];
|
|
4432
|
+
total += w;
|
|
4433
|
+
}
|
|
4434
|
+
coarse[row * coarseN + column] = sum / total;
|
|
4435
|
+
}
|
|
4436
|
+
const finalAt = (x, z, grid, gridN) => {
|
|
4437
|
+
const [u, v] = toTile(x, z);
|
|
4438
|
+
if (u <= 0 || u >= 1 || v <= 0 || v >= 1) return [0, 0];
|
|
4439
|
+
const edge = Math.min(u, v, 1 - u, 1 - v);
|
|
4440
|
+
const reach = Math.max(Math.abs(x), Math.abs(z));
|
|
4441
|
+
const weight = smoothstep(0, 0.04, edge) * smoothstep(keepOut, keepOut + KEEP_OUT_RAMP, reach);
|
|
4442
|
+
if (weight === 0) return [0, 0];
|
|
4443
|
+
const raw = sampleGrid(hostBase, B, B, u, v) + scale * sampleGrid(grid, gridN, gridN, u, v);
|
|
4444
|
+
const value = (raw + floor + Math.sqrt((raw - floor) ** 2 + 40 ** 2)) / 2;
|
|
4445
|
+
return [value, weight];
|
|
4446
|
+
};
|
|
4447
|
+
const delta = new Float32Array(N * N);
|
|
4448
|
+
for (let row = 0; row < N; row++)
|
|
4449
|
+
for (let column = 0; column < N; column++) {
|
|
4450
|
+
const x = column * cell - halfWorld;
|
|
4451
|
+
const z = row * cell - halfWorld;
|
|
4452
|
+
const [value, weight] = finalAt(x, z, coarse, coarseN);
|
|
4453
|
+
if (weight > 0) {
|
|
4454
|
+
const host = state.height[row * N + column];
|
|
4455
|
+
delta[row * N + column] = weight * (value - host);
|
|
4456
|
+
}
|
|
4457
|
+
}
|
|
4458
|
+
const corners = [toWorld(0, 0), toWorld(1, 0), toWorld(0, 1), toWorld(1, 1)];
|
|
4459
|
+
const x0 = clamp(Math.min(...corners.map((p) => p[0])), -halfWorld, halfWorld);
|
|
4460
|
+
const x1 = clamp(Math.max(...corners.map((p) => p[0])), -halfWorld, halfWorld);
|
|
4461
|
+
const z0 = clamp(Math.min(...corners.map((p) => p[1])), -halfWorld, halfWorld);
|
|
4462
|
+
const z1 = clamp(Math.max(...corners.map((p) => p[1])), -halfWorld, halfWorld);
|
|
4463
|
+
const columns = Math.floor((x1 - x0) / spacing) + 1;
|
|
4464
|
+
const rows = Math.floor((z1 - z0) / spacing) + 1;
|
|
4465
|
+
const detail = new Float32Array(columns * rows);
|
|
4466
|
+
for (let row = 0; row < rows; row++)
|
|
4467
|
+
for (let column = 0; column < columns; column++) {
|
|
4468
|
+
const x = x0 + column * spacing;
|
|
4469
|
+
const z = z0 + row * spacing;
|
|
4470
|
+
const host = hostSmooth(x, z);
|
|
4471
|
+
const [value, weight] = finalAt(x, z, lifted, n);
|
|
4472
|
+
detail[row * columns + column] = host + weight * (value - host);
|
|
4473
|
+
}
|
|
4474
|
+
const id = options.id ?? "massif";
|
|
4475
|
+
return {
|
|
4476
|
+
terrain: terrain.heightmap({
|
|
4477
|
+
id,
|
|
4478
|
+
data: { width: N, height: N, values: Array.from(delta) },
|
|
4479
|
+
blend: "add"
|
|
4480
|
+
}),
|
|
4481
|
+
placement: {
|
|
4482
|
+
tile: tile.id,
|
|
4483
|
+
at,
|
|
4484
|
+
rotation: turn * 180 / Math.PI,
|
|
4485
|
+
mirror,
|
|
4486
|
+
height,
|
|
4487
|
+
detail: { origin: [x0, z0], spacing, columns, rows, heights: detail }
|
|
4488
|
+
}
|
|
4489
|
+
};
|
|
4490
|
+
}
|
|
4491
|
+
|
|
4492
|
+
// src/core/stats.ts
|
|
4493
|
+
var CHANNEL_AREA_M2 = 1e5;
|
|
4494
|
+
var quantile = (sorted, q) => sorted[Math.min(sorted.length - 1, Math.floor(q * sorted.length))];
|
|
4495
|
+
function terrainStats(height, n, size) {
|
|
4496
|
+
const spacing = size / (n - 1);
|
|
4497
|
+
let low = Number.POSITIVE_INFINITY;
|
|
4498
|
+
let high = Number.NEGATIVE_INFINITY;
|
|
4499
|
+
let sum = 0;
|
|
4500
|
+
for (let i = 0; i < height.length; i += 1) {
|
|
4501
|
+
const v = height[i];
|
|
4502
|
+
low = Math.min(low, v);
|
|
4503
|
+
high = Math.max(high, v);
|
|
4504
|
+
sum += v;
|
|
4505
|
+
}
|
|
4506
|
+
const relief = high - low;
|
|
4507
|
+
const interior = (n - 2) * (n - 2);
|
|
4508
|
+
const slopes = new Float32Array(interior);
|
|
4509
|
+
const laplacian = new Float32Array(interior);
|
|
4510
|
+
let lapSum = 0;
|
|
4511
|
+
let k = 0;
|
|
4512
|
+
for (let z = 1; z < n - 1; z += 1) {
|
|
4513
|
+
for (let x = 1; x < n - 1; x += 1) {
|
|
4514
|
+
const i = z * n + x;
|
|
4515
|
+
const here = height[i];
|
|
4516
|
+
const east = height[i + 1];
|
|
4517
|
+
const west = height[i - 1];
|
|
4518
|
+
const south = height[i + n];
|
|
4519
|
+
const north = height[i - n];
|
|
4520
|
+
slopes[k] = Math.atan(Math.hypot(east - west, south - north) / (2 * spacing)) * 180 / Math.PI;
|
|
4521
|
+
laplacian[k] = (east + west + south + north - 4 * here) / (spacing * spacing);
|
|
4522
|
+
lapSum += laplacian[k];
|
|
4523
|
+
k += 1;
|
|
4524
|
+
}
|
|
4525
|
+
}
|
|
4526
|
+
const lapMean = lapSum / interior;
|
|
4527
|
+
let m2 = 0;
|
|
4528
|
+
let m4 = 0;
|
|
4529
|
+
for (let i = 0; i < interior; i += 1) {
|
|
4530
|
+
const d = laplacian[i] - lapMean;
|
|
4531
|
+
m2 += d * d;
|
|
4532
|
+
m4 += d * d * d * d;
|
|
4533
|
+
}
|
|
4534
|
+
m2 /= interior;
|
|
4535
|
+
m4 /= interior;
|
|
4536
|
+
slopes.sort();
|
|
4537
|
+
const filled = Float64Array.from(height);
|
|
4538
|
+
fillDepressions(filled, n);
|
|
4539
|
+
const flow = createFlow(n);
|
|
4540
|
+
routeFlow(filled, n, flow);
|
|
4541
|
+
const channelCells = CHANNEL_AREA_M2 / (spacing * spacing);
|
|
4542
|
+
let channels = 0;
|
|
4543
|
+
for (let i = 0; i < flow.cells.length; i += 1)
|
|
4544
|
+
if (flow.cells[i] >= channelCells) channels += 1;
|
|
4545
|
+
const areaKm2 = size * size / 1e6;
|
|
4546
|
+
return {
|
|
4547
|
+
relief,
|
|
4548
|
+
hypsometricIntegral: relief > 0 ? (sum / height.length - low) / relief : 0,
|
|
4549
|
+
slopeP50: quantile(slopes, 0.5),
|
|
4550
|
+
slopeP90: quantile(slopes, 0.9),
|
|
4551
|
+
drainageDensity: channels * spacing / 1e3 / areaKm2,
|
|
4552
|
+
curvatureKurtosis: m2 > 0 ? m4 / (m2 * m2) - 3 : 0
|
|
4553
|
+
};
|
|
4554
|
+
}
|
|
4555
|
+
|
|
4556
|
+
// src/core/bake.ts
|
|
4557
|
+
var toLinear = (color) => color <= 0.04045 ? color / 12.92 : ((color + 0.055) / 1.055) ** 2.4;
|
|
4558
|
+
function bakeMesh(state, {
|
|
4559
|
+
step = 1,
|
|
4560
|
+
x = 0,
|
|
4561
|
+
z = 0,
|
|
4562
|
+
cells = state.resolution - 1,
|
|
4563
|
+
skirtDepth = 0,
|
|
4564
|
+
palette
|
|
4565
|
+
} = {}) {
|
|
4566
|
+
if (!Number.isFinite(skirtDepth) || skirtDepth < 0)
|
|
4567
|
+
throw new RangeError("skirtDepth must be finite and nonnegative");
|
|
4568
|
+
const n = state.resolution;
|
|
4569
|
+
if (!Number.isInteger(n) || n < 2 || !Number.isFinite(state.size) || state.size <= 0)
|
|
4570
|
+
throw new RangeError("Invalid heightfield extent");
|
|
4571
|
+
if (!Number.isInteger(step) || step < 1 || (step & step - 1) !== 0 || cells % step !== 0)
|
|
4572
|
+
throw new RangeError("step must be a power of two dividing chunk cells");
|
|
4573
|
+
if (![x, z, cells].every(Number.isInteger) || x < 0 || z < 0 || cells < 1 || x + cells >= n || z + cells >= n)
|
|
4574
|
+
throw new RangeError("Chunk outside heightfield");
|
|
4575
|
+
if (state.height.length !== n * n || !state.height.every(Number.isFinite))
|
|
4576
|
+
throw new RangeError("Invalid heightfield samples");
|
|
4577
|
+
if (palette && (palette.length !== 8 || Array.from(palette).some(
|
|
4578
|
+
(rgb) => !Array.isArray(rgb) || rgb.length !== 3 || Array.from(rgb).some((value) => !Number.isFinite(value) || value < 0 || value > 1)
|
|
4579
|
+
)))
|
|
4580
|
+
throw new RangeError("palette requires eight finite sRGB triples in 0..1");
|
|
4581
|
+
if (palette && (state.splat.length !== n * n * 8 || !state.splat.every(Number.isFinite)))
|
|
4582
|
+
throw new RangeError("Invalid splat samples");
|
|
4583
|
+
const side = cells / step + 1;
|
|
4584
|
+
const base = side * side;
|
|
4585
|
+
const ring = [];
|
|
4586
|
+
if (skirtDepth > 0) {
|
|
4587
|
+
for (let k2 = 0; k2 < side - 1; k2++) ring.push(k2);
|
|
4588
|
+
for (let k2 = 0; k2 < side - 1; k2++) ring.push(k2 * side + side - 1);
|
|
4589
|
+
for (let k2 = side - 1; k2 > 0; k2--) ring.push((side - 1) * side + k2);
|
|
4590
|
+
for (let k2 = side - 1; k2 > 0; k2--) ring.push(k2 * side);
|
|
4591
|
+
}
|
|
4592
|
+
const count2 = base + ring.length;
|
|
4593
|
+
const positions = new Float32Array(count2 * 3);
|
|
4594
|
+
const normals = new Float32Array(count2 * 3);
|
|
4595
|
+
const uvs = new Float32Array(count2 * 2);
|
|
4596
|
+
const indices = new Uint32Array((side - 1) ** 2 * 6 + ring.length * 6);
|
|
4597
|
+
const colors = palette ? new Float32Array(count2 * 3) : void 0;
|
|
4598
|
+
const cell = state.size / (n - 1);
|
|
4599
|
+
for (let zz = 0; zz < side; zz++)
|
|
4600
|
+
for (let xx = 0; xx < side; xx++) {
|
|
4601
|
+
const gx = x + xx * step;
|
|
4602
|
+
const gz = z + zz * step;
|
|
4603
|
+
const i = gz * n + gx;
|
|
4604
|
+
const j = zz * side + xx;
|
|
4605
|
+
const dx = (state.height[gz * n + Math.min(n - 1, gx + 1)] - state.height[gz * n + Math.max(0, gx - 1)]) / ((gx > 0 && gx < n - 1 ? 2 : 1) * cell);
|
|
4606
|
+
const dz = (state.height[Math.min(n - 1, gz + 1) * n + gx] - state.height[Math.max(0, gz - 1) * n + gx]) / ((gz > 0 && gz < n - 1 ? 2 : 1) * cell);
|
|
4607
|
+
const length = Math.hypot(dx, 1, dz);
|
|
4608
|
+
positions.set([world(state, gx), state.height[i], world(state, gz)], j * 3);
|
|
4609
|
+
normals.set([-dx / length, 1 / length, -dz / length], j * 3);
|
|
4610
|
+
if (colors && palette)
|
|
4611
|
+
for (let k2 = 0; k2 < 3; k2++) {
|
|
4612
|
+
let color = 0;
|
|
4613
|
+
for (let c = 0; c < 8; c++)
|
|
4614
|
+
color += toLinear(palette[c]?.[k2]) * state.splat[i * 8 + c];
|
|
4615
|
+
colors[j * 3 + k2] = color;
|
|
4616
|
+
}
|
|
4617
|
+
uvs.set([gx / (n - 1), 1 - gz / (n - 1)], j * 2);
|
|
4618
|
+
}
|
|
4619
|
+
let k = 0;
|
|
4620
|
+
for (let zz = 0; zz < side - 1; zz++)
|
|
4621
|
+
for (let xx = 0; xx < side - 1; xx++) {
|
|
4622
|
+
const a = zz * side + xx;
|
|
4623
|
+
const b = a + 1;
|
|
4624
|
+
const c = a + side;
|
|
4625
|
+
const d = c + 1;
|
|
4626
|
+
indices.set([a, c, b, b, c, d], k);
|
|
4627
|
+
k += 6;
|
|
4628
|
+
}
|
|
4629
|
+
for (let j = 0; j < ring.length; j++) {
|
|
4630
|
+
const top = ring[j];
|
|
4631
|
+
const bottom = base + j;
|
|
4632
|
+
positions.set(positions.subarray(top * 3, top * 3 + 3), bottom * 3);
|
|
4633
|
+
positions[bottom * 3 + 1] = positions[bottom * 3 + 1] - skirtDepth;
|
|
4634
|
+
normals.set(normals.subarray(top * 3, top * 3 + 3), bottom * 3);
|
|
4635
|
+
colors?.set(colors.subarray(top * 3, top * 3 + 3), bottom * 3);
|
|
4636
|
+
uvs.set(uvs.subarray(top * 2, top * 2 + 2), bottom * 2);
|
|
4637
|
+
const next = (j + 1) % ring.length;
|
|
4638
|
+
indices.set([top, ring[next], bottom, ring[next], base + next, bottom], k);
|
|
4639
|
+
k += 6;
|
|
4640
|
+
}
|
|
4641
|
+
return {
|
|
4642
|
+
name: `terrain_${x}_${z}_step${step}`,
|
|
4643
|
+
positions,
|
|
4644
|
+
normals,
|
|
4645
|
+
colors,
|
|
4646
|
+
uvs,
|
|
4647
|
+
indices,
|
|
4648
|
+
side,
|
|
4649
|
+
topVertexCount: base,
|
|
4650
|
+
bounds: { x, z, cells, step },
|
|
4651
|
+
skirtDepth
|
|
4652
|
+
};
|
|
4653
|
+
}
|
|
4654
|
+
function bakeTerrain(state, {
|
|
4655
|
+
chunkCells = Math.min(64, state.resolution - 1),
|
|
4656
|
+
lodSteps = [1, 2, 4],
|
|
4657
|
+
skirtDepth = 6,
|
|
4658
|
+
palette
|
|
4659
|
+
} = {}) {
|
|
4660
|
+
if (!Number.isInteger(chunkCells) || chunkCells < 1 || (state.resolution - 1) % chunkCells !== 0)
|
|
4661
|
+
throw new RangeError("chunkCells must divide resolution - 1");
|
|
4662
|
+
if (!lodSteps.length) throw new RangeError("lodSteps must not be empty");
|
|
4663
|
+
const lods = lodSteps.map((step) => {
|
|
4664
|
+
const chunks = [];
|
|
4665
|
+
for (let z = 0; z < state.resolution - 1; z += chunkCells)
|
|
4666
|
+
for (let x = 0; x < state.resolution - 1; x += chunkCells)
|
|
4667
|
+
chunks.push(bakeMesh(state, { x, z, cells: chunkCells, step, skirtDepth, palette }));
|
|
4668
|
+
return { step, chunks };
|
|
4669
|
+
});
|
|
2140
4670
|
return {
|
|
2141
4671
|
version: 1,
|
|
2142
4672
|
size: state.size,
|
|
@@ -2159,8 +4689,8 @@ function validateBakedMesh(mesh) {
|
|
|
2159
4689
|
(array) => array instanceof Float32Array
|
|
2160
4690
|
) || !(mesh.indices instanceof Uint32Array))
|
|
2161
4691
|
throw new TypeError("Baked mesh requires Float32 attributes and Uint32 indices");
|
|
2162
|
-
const
|
|
2163
|
-
if (!
|
|
4692
|
+
const count2 = mesh.positions.length / 3;
|
|
4693
|
+
if (!count2 || !Number.isInteger(count2) || mesh.normals.length !== count2 * 3 || mesh.uvs.length !== count2 * 2 || mesh.colors && mesh.colors.length !== count2 * 3 || !mesh.indices.length || mesh.indices.length % 3)
|
|
2164
4694
|
throw new RangeError("Baked mesh attribute counts do not agree");
|
|
2165
4695
|
for (const array of [
|
|
2166
4696
|
mesh.positions,
|
|
@@ -2169,7 +4699,7 @@ function validateBakedMesh(mesh) {
|
|
|
2169
4699
|
...mesh.colors ? [mesh.colors] : []
|
|
2170
4700
|
])
|
|
2171
4701
|
if (!array.every(Number.isFinite)) throw new RangeError("Baked mesh attributes must be finite");
|
|
2172
|
-
if (mesh.indices.some((index) => index >=
|
|
4702
|
+
if (mesh.indices.some((index) => index >= count2))
|
|
2173
4703
|
throw new RangeError("Baked mesh index outside vertex range");
|
|
2174
4704
|
}
|
|
2175
4705
|
|
|
@@ -2412,10 +4942,12 @@ async function decodeHeightPNG(input, options = {}) {
|
|
|
2412
4942
|
throw Error("Heightmap import supports non-interlaced 8/16-bit grayscale PNG only");
|
|
2413
4943
|
}
|
|
2414
4944
|
if (type === "tEXt") {
|
|
2415
|
-
const
|
|
2416
|
-
if (
|
|
4945
|
+
const text2 = decoder.decode(data);
|
|
4946
|
+
if (text2.startsWith("StrataHeightRange\0")) {
|
|
2417
4947
|
try {
|
|
2418
|
-
|
|
4948
|
+
const parsed = JSON.parse(text2.slice(18));
|
|
4949
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw Error();
|
|
4950
|
+
metadata = parsed;
|
|
2419
4951
|
} catch {
|
|
2420
4952
|
throw Error("Invalid PNG height range metadata");
|
|
2421
4953
|
}
|
|
@@ -2517,7 +5049,7 @@ function encodeGLB(input) {
|
|
|
2517
5049
|
}
|
|
2518
5050
|
const binary = join(chunks);
|
|
2519
5051
|
const doc = {
|
|
2520
|
-
asset: { version: "2.0", generator: "
|
|
5052
|
+
asset: { version: "2.0", generator: "@threenative/terrain 0.1.9" },
|
|
2521
5053
|
scene: 0,
|
|
2522
5054
|
scenes: [{ nodes: meshes.map((_, i) => i) }],
|
|
2523
5055
|
nodes: meshes.map((m, i) => ({ mesh: i, name: m.name ?? `Terrain ${i}` })),
|
|
@@ -2628,6 +5160,7 @@ async function makeExport(state, recipe, kind) {
|
|
|
2628
5160
|
});
|
|
2629
5161
|
if (kind === "project")
|
|
2630
5162
|
return result("terrain-recipe.json", "application/json", json("", recipe).bytes);
|
|
5163
|
+
if (!state) throw new Error("Wait for initial build");
|
|
2631
5164
|
if (kind === "png")
|
|
2632
5165
|
return result("terrain-height16.png", "image/png", await encodeHeightPNG(state));
|
|
2633
5166
|
if (kind === "glb") return result("terrain.glb", "model/gltf-binary", encodeGLB(bakeMesh(state)));
|
|
@@ -2704,7 +5237,7 @@ async function makeExport(state, recipe, kind) {
|
|
|
2704
5237
|
"No navigation mesh"
|
|
2705
5238
|
]
|
|
2706
5239
|
};
|
|
2707
|
-
const
|
|
5240
|
+
const water2 = state.waters.map((w, i) => {
|
|
2708
5241
|
const { mask, ...rest } = w;
|
|
2709
5242
|
return { ...rest, maskFile: `water-${i}.mask8` };
|
|
2710
5243
|
});
|
|
@@ -2712,216 +5245,163 @@ async function makeExport(state, recipe, kind) {
|
|
|
2712
5245
|
json("manifest.json", manifest),
|
|
2713
5246
|
json("recipe.json", recipe),
|
|
2714
5247
|
json("placements.json", baked.instances),
|
|
2715
|
-
json("water.json", { bodies:
|
|
5248
|
+
json("water.json", { bodies: water2, rivers: state.rivers }),
|
|
2716
5249
|
{ name: "collision.f32le", bytes: collision },
|
|
2717
5250
|
{ name: "height16.png", bytes: await encodeHeightPNG(state) },
|
|
2718
5251
|
...splatFiles,
|
|
2719
5252
|
...baked.lods.map((l) => ({ name: `lod-${l.step}.glb`, bytes: encodeGLB(l.chunks) }))
|
|
2720
5253
|
];
|
|
2721
|
-
for (let i = 0; i <
|
|
5254
|
+
for (let i = 0; i < water2.length; i++)
|
|
2722
5255
|
files.push({
|
|
2723
|
-
name:
|
|
5256
|
+
name: water2[i]?.maskFile,
|
|
2724
5257
|
bytes: new Uint8Array(state.waters[i]?.mask ?? [])
|
|
2725
5258
|
});
|
|
2726
5259
|
return result("terrain-runtime.zip", "application/zip", encodeZIP(files));
|
|
2727
5260
|
}
|
|
2728
5261
|
|
|
2729
|
-
// src/core/
|
|
2730
|
-
var
|
|
2731
|
-
var
|
|
2732
|
-
var
|
|
2733
|
-
|
|
2734
|
-
|
|
2735
|
-
|
|
2736
|
-
channel,
|
|
2737
|
-
component: COMPONENTS[channel % COMPONENTS.length],
|
|
2738
|
-
id,
|
|
2739
|
-
plane: Math.floor(channel / COMPONENTS.length)
|
|
2740
|
-
}));
|
|
2741
|
-
function poseOf(item) {
|
|
2742
|
-
if (item.transform === void 0)
|
|
2743
|
-
return {
|
|
2744
|
-
position: item.position,
|
|
2745
|
-
quaternion: [0, Math.sin(item.rotation / 2), 0, Math.cos(item.rotation / 2)],
|
|
2746
|
-
scale: [item.scale, item.scale, item.scale]
|
|
2747
|
-
};
|
|
2748
|
-
const { position, quaternion, scale } = item.transform;
|
|
2749
|
-
return { position, quaternion, scale };
|
|
2750
|
-
}
|
|
2751
|
-
function uniformScale(asset, id, scale) {
|
|
2752
|
-
const mean = (scale[0] + scale[1] + scale[2]) / 3;
|
|
2753
|
-
const deviation = Math.max(
|
|
2754
|
-
Math.abs(scale[0] - mean),
|
|
2755
|
-
Math.abs(scale[1] - mean),
|
|
2756
|
-
Math.abs(scale[2] - mean)
|
|
2757
|
-
);
|
|
2758
|
-
if (!(mean > 0) || deviation > mean * SCALE_TOLERANCE)
|
|
5262
|
+
// src/core/prop-package.ts
|
|
5263
|
+
var PROP_CELL_SIZE = 128;
|
|
5264
|
+
var DEFAULT_KIND = "local";
|
|
5265
|
+
var KINDS = ["far", "local", "meadow"];
|
|
5266
|
+
function compilePropBuckets(entries, options = {}) {
|
|
5267
|
+
const cellSize = options.cellSize ?? PROP_CELL_SIZE;
|
|
5268
|
+
if (!Number.isFinite(cellSize) || cellSize <= 0)
|
|
2759
5269
|
throw Error(
|
|
2760
|
-
`
|
|
5270
|
+
`Prop package: cellSize must be a positive finite number, got ${String(cellSize)}.`
|
|
2761
5271
|
);
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
2768
|
-
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
position[2],
|
|
2774
|
-
quaternion[0] / length,
|
|
2775
|
-
quaternion[1] / length,
|
|
2776
|
-
quaternion[2] / length,
|
|
2777
|
-
quaternion[3] / length,
|
|
2778
|
-
uniformScale(item.asset, item.id, scale)
|
|
2779
|
-
];
|
|
2780
|
-
}
|
|
2781
|
-
function placementCells(state, assets, cellSize, extent) {
|
|
2782
|
-
const cells = /* @__PURE__ */ new Map();
|
|
2783
|
-
const across = Math.max(1, Math.ceil(extent.sizeX / cellSize));
|
|
2784
|
-
const cellOf = (value, min) => Math.min(across - 1, Math.max(0, Math.floor((value - min) / cellSize)));
|
|
2785
|
-
for (const item of state.instances) {
|
|
2786
|
-
if (assets[item.asset] === void 0)
|
|
5272
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
5273
|
+
for (const entry of entries) {
|
|
5274
|
+
const kind = entry.kind ?? DEFAULT_KIND;
|
|
5275
|
+
if (!KINDS.includes(kind))
|
|
5276
|
+
throw Error(`Prop package: '${entry.bucket}' names unknown kind '${String(kind)}'.`);
|
|
5277
|
+
if (!Number.isFinite(entry.reach) && entry.reach !== Number.POSITIVE_INFINITY)
|
|
5278
|
+
throw Error(`Prop package: '${entry.placement.id}' has a non-finite reach.`);
|
|
5279
|
+
const seen = buckets.get(entry.bucket);
|
|
5280
|
+
if (seen === void 0)
|
|
5281
|
+
buckets.set(entry.bucket, { bucket: entry.bucket, entries: [entry], kind });
|
|
5282
|
+
else if (seen.kind !== kind)
|
|
2787
5283
|
throw Error(
|
|
2788
|
-
`
|
|
5284
|
+
`Prop package: bucket '${entry.bucket}' mixes kinds '${seen.kind}' and '${kind}'.`
|
|
2789
5285
|
);
|
|
2790
|
-
|
|
2791
|
-
const z = cellOf(item.position[2], extent.minZ);
|
|
2792
|
-
const key = `${String(x)},${String(z)}`;
|
|
2793
|
-
let cell = cells.get(key);
|
|
2794
|
-
if (cell === void 0) {
|
|
2795
|
-
cell = { items: /* @__PURE__ */ new Map(), x, z };
|
|
2796
|
-
cells.set(key, cell);
|
|
2797
|
-
}
|
|
2798
|
-
const bucket = cell.items.get(item.asset);
|
|
2799
|
-
if (bucket === void 0) cell.items.set(item.asset, [item]);
|
|
2800
|
-
else bucket.push(item);
|
|
2801
|
-
}
|
|
2802
|
-
const buffer = new ArrayBuffer(state.instances.length * RECORD_VALUES * 4);
|
|
2803
|
-
const records = new Float32Array(buffer);
|
|
2804
|
-
const ordered = [];
|
|
2805
|
-
let written = 0;
|
|
2806
|
-
const byId = (left, right) => left[0] < right[0] ? -1 : 1;
|
|
2807
|
-
for (const cell of [...cells.values()].sort((a, b) => a.x - b.x || a.z - b.z)) {
|
|
2808
|
-
const runs = [];
|
|
2809
|
-
for (const [asset, items] of [...cell.items.entries()].sort(byId)) {
|
|
2810
|
-
runs.push({ asset, count: items.length, offset: written });
|
|
2811
|
-
for (const item of items) {
|
|
2812
|
-
records.set(placementRecord(item), written * RECORD_VALUES);
|
|
2813
|
-
written += 1;
|
|
2814
|
-
}
|
|
2815
|
-
}
|
|
2816
|
-
ordered.push({ runs, x: cell.x, z: cell.z });
|
|
5286
|
+
else seen.entries.push(entry);
|
|
2817
5287
|
}
|
|
2818
|
-
return { bytes: new Uint8Array(buffer), cells: ordered };
|
|
2819
|
-
}
|
|
2820
|
-
function bakeWorldPackage(state, options) {
|
|
2821
|
-
const { size, resolution } = state;
|
|
2822
|
-
const { assets, cellSize = 64, layers, names } = options;
|
|
2823
|
-
const heightmapName = names?.heightmap ?? "heightmap.u16";
|
|
2824
|
-
const placementsName = names?.placements ?? "placements.bin";
|
|
2825
|
-
const splatName = names?.splat ?? "splat.rgba";
|
|
2826
|
-
const extent = { minX: -size / 2, minZ: -size / 2, sizeX: size, sizeZ: size };
|
|
2827
|
-
let heightMin = Number.POSITIVE_INFINITY;
|
|
2828
|
-
let heightMax = Number.NEGATIVE_INFINITY;
|
|
2829
|
-
for (const height of state.height) {
|
|
2830
|
-
if (!Number.isFinite(height)) throw Error("World package: terrain heights must be finite.");
|
|
2831
|
-
heightMin = Math.min(heightMin, height);
|
|
2832
|
-
heightMax = Math.max(heightMax, height);
|
|
2833
|
-
}
|
|
2834
|
-
if (heightMax - heightMin < 1e-6) heightMax = heightMin + 1;
|
|
2835
|
-
const heightmap = new Uint8Array(resolution * resolution * 2);
|
|
2836
|
-
const heights = new DataView(heightmap.buffer);
|
|
2837
|
-
for (let i = 0; i < state.height.length; i += 1)
|
|
2838
|
-
heights.setUint16(
|
|
2839
|
-
i * 2,
|
|
2840
|
-
Math.round(
|
|
2841
|
-
(state.height[i] - heightMin) / (heightMax - heightMin) * HEIGHTMAP_MAX
|
|
2842
|
-
),
|
|
2843
|
-
true
|
|
2844
|
-
);
|
|
2845
|
-
const placements = placementCells(state, assets, cellSize, extent);
|
|
2846
5288
|
return {
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
layers: { splat: splatName, ...layers },
|
|
2864
|
-
rows: resolution,
|
|
2865
|
-
spacing: size / (resolution - 1)
|
|
2866
|
-
},
|
|
2867
|
-
version: 1
|
|
5289
|
+
buckets: [...buckets.values()].map((acc) => bucketPackage(acc, cellSize, options.bounds)),
|
|
5290
|
+
cellSize
|
|
5291
|
+
};
|
|
5292
|
+
}
|
|
5293
|
+
function bucketPackage(acc, cellSize, bounds) {
|
|
5294
|
+
const extent = bucketExtent(
|
|
5295
|
+
acc.entries.map((entry) => entry.placement.position),
|
|
5296
|
+
cellSize,
|
|
5297
|
+
bounds
|
|
5298
|
+
);
|
|
5299
|
+
const modelOf = (entry) => entry.key ?? entry.placement.asset;
|
|
5300
|
+
const placements = placementCells(
|
|
5301
|
+
{
|
|
5302
|
+
instances: acc.entries.map(
|
|
5303
|
+
(entry) => entry.key === void 0 ? entry.placement : { ...entry.placement, asset: entry.key }
|
|
5304
|
+
)
|
|
2868
5305
|
},
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
5306
|
+
// The compile validates membership only; the game owns every asset's model, bounds and LODs.
|
|
5307
|
+
Object.fromEntries(acc.entries.map((entry) => [modelOf(entry), true])),
|
|
5308
|
+
cellSize,
|
|
5309
|
+
extent
|
|
5310
|
+
);
|
|
5311
|
+
const reach = new Map(acc.entries.map((entry) => [entry.placement.id, entry.reach]));
|
|
5312
|
+
for (const id of placements.ids)
|
|
5313
|
+
if (!reach.has(id)) throw Error(`Prop package: '${id}' lost its reach in the cell pass.`);
|
|
5314
|
+
return {
|
|
5315
|
+
bucket: acc.bucket,
|
|
5316
|
+
cells: placements.cells,
|
|
5317
|
+
extent,
|
|
5318
|
+
ids: placements.ids,
|
|
5319
|
+
kind: acc.kind,
|
|
5320
|
+
placements: placements.bytes,
|
|
5321
|
+
reach: Float32Array.from(placements.ids, (id) => reach.get(id))
|
|
2874
5322
|
};
|
|
2875
5323
|
}
|
|
2876
|
-
function
|
|
2877
|
-
const
|
|
2878
|
-
if (
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
const
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2888
|
-
|
|
2889
|
-
bytes[slot.plane * planeBytes + row * resolution * 4 + column * 4 + COMPONENTS.indexOf(slot.component)] = Math.round(Math.min(1, Math.max(0, weight)) * 255);
|
|
2890
|
-
}
|
|
2891
|
-
}
|
|
5324
|
+
function bucketExtent(positions, cellSize, bounds) {
|
|
5325
|
+
const first = positions[0];
|
|
5326
|
+
if (first === void 0) throw Error("Prop package: a bucket holds no placement.");
|
|
5327
|
+
let { minX, minZ } = bounds ?? { minX: first[0], minZ: first[2] };
|
|
5328
|
+
let maxX = bounds === void 0 ? first[0] : bounds.minX + bounds.sizeX;
|
|
5329
|
+
let maxZ = bounds === void 0 ? first[2] : bounds.minZ + bounds.sizeZ;
|
|
5330
|
+
for (const [x, , z] of positions) {
|
|
5331
|
+
if (!Number.isFinite(x) || !Number.isFinite(z))
|
|
5332
|
+
throw Error(`Prop package: a placement sits at a non-finite [${String(x)}, ${String(z)}].`);
|
|
5333
|
+
minX = Math.min(minX, x);
|
|
5334
|
+
minZ = Math.min(minZ, z);
|
|
5335
|
+
maxX = Math.max(maxX, x);
|
|
5336
|
+
maxZ = Math.max(maxZ, z);
|
|
2892
5337
|
}
|
|
2893
|
-
|
|
5338
|
+
minX = Math.floor(minX / cellSize) * cellSize;
|
|
5339
|
+
minZ = Math.floor(minZ / cellSize) * cellSize;
|
|
5340
|
+
maxX = (Math.floor(maxX / cellSize) + 1) * cellSize;
|
|
5341
|
+
maxZ = (Math.floor(maxZ / cellSize) + 1) * cellSize;
|
|
5342
|
+
return { minX, minZ, sizeX: maxX - minX, sizeZ: maxZ - minZ };
|
|
2894
5343
|
}
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
5344
|
+
function propAssetOf(modelKey) {
|
|
5345
|
+
const at = modelKey.indexOf(":");
|
|
5346
|
+
return at < 0 ? modelKey : modelKey.slice(0, at);
|
|
5347
|
+
}
|
|
5348
|
+
function decodePropBucket(bucket) {
|
|
5349
|
+
const floats = new Float32Array(
|
|
5350
|
+
bucket.placements.buffer,
|
|
5351
|
+
bucket.placements.byteOffset,
|
|
5352
|
+
bucket.placements.byteLength / 4
|
|
5353
|
+
);
|
|
5354
|
+
if (floats.length !== bucket.ids.length * 8)
|
|
2900
5355
|
throw new Error(
|
|
2901
|
-
`
|
|
5356
|
+
`TN_PROP_BUCKET_INVALID: bucket '${bucket.bucket}' holds ${floats.length / 8} records for ${bucket.ids.length} IDs.`
|
|
2902
5357
|
);
|
|
2903
|
-
const
|
|
2904
|
-
for (const
|
|
2905
|
-
const
|
|
2906
|
-
|
|
5358
|
+
const keys = new Array(bucket.ids.length);
|
|
5359
|
+
for (const cell of bucket.cells)
|
|
5360
|
+
for (const run of cell.runs)
|
|
5361
|
+
for (let index = 0; index < run.count; index++) keys[run.offset + index] = run.asset;
|
|
5362
|
+
return bucket.ids.map((id, index) => {
|
|
5363
|
+
const key = keys[index];
|
|
5364
|
+
if (key === void 0)
|
|
2907
5365
|
throw new Error(
|
|
2908
|
-
`
|
|
5366
|
+
`TN_PROP_BUCKET_INVALID: bucket '${bucket.bucket}' record '${id}' sits in no run.`
|
|
2909
5367
|
);
|
|
2910
|
-
const
|
|
2911
|
-
const
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
}
|
|
2922
|
-
|
|
2923
|
-
|
|
2924
|
-
}
|
|
5368
|
+
const at = index * 8;
|
|
5369
|
+
const f = (k) => floats[at + k];
|
|
5370
|
+
return {
|
|
5371
|
+
id,
|
|
5372
|
+
key,
|
|
5373
|
+
asset: propAssetOf(key),
|
|
5374
|
+
position: [f(0), f(1), f(2)],
|
|
5375
|
+
quaternion: [f(3), f(4), f(5), f(6)],
|
|
5376
|
+
scale: f(7),
|
|
5377
|
+
reach: bucket.reach[index] ?? bucket.reach[0] ?? Number.POSITIVE_INFINITY
|
|
5378
|
+
};
|
|
5379
|
+
});
|
|
5380
|
+
}
|
|
5381
|
+
function propPackageFile(world2, bucket, suffix) {
|
|
5382
|
+
return `${world2}.${bucket.replace(/[^a-zA-Z0-9]+/g, "-")}.${suffix}`;
|
|
5383
|
+
}
|
|
5384
|
+
function propPackageFiles(pkg, world2) {
|
|
5385
|
+
const files = {};
|
|
5386
|
+
const buckets = pkg.buckets.map((bucket) => {
|
|
5387
|
+
const names = {
|
|
5388
|
+
ids: propPackageFile(world2, bucket.bucket, "ids.json"),
|
|
5389
|
+
placements: propPackageFile(world2, bucket.bucket, "props.bin"),
|
|
5390
|
+
reach: propPackageFile(world2, bucket.bucket, "reach.bin")
|
|
5391
|
+
};
|
|
5392
|
+
files[names.placements] = bucket.placements;
|
|
5393
|
+
files[names.reach] = new Uint8Array(bucket.reach.buffer, bucket.reach.byteOffset);
|
|
5394
|
+
files[names.ids] = new TextEncoder().encode(JSON.stringify(bucket.ids));
|
|
5395
|
+
return {
|
|
5396
|
+
bucket: bucket.bucket,
|
|
5397
|
+
cells: bucket.cells,
|
|
5398
|
+
extent: bucket.extent,
|
|
5399
|
+
kind: bucket.kind,
|
|
5400
|
+
...names
|
|
5401
|
+
};
|
|
5402
|
+
});
|
|
5403
|
+
const manifest = { buckets, cellSize: pkg.cellSize, version: 1 };
|
|
5404
|
+
return { files, manifest: new TextEncoder().encode(JSON.stringify(manifest)) };
|
|
2925
5405
|
}
|
|
2926
5406
|
|
|
2927
|
-
export { MATERIAL_IDS, Mask, PARAMS, RESOLUTIONS, Terrain, TerrainEvaluator, applyPlacementOverrides, bakeMesh, bakeTerrain, bakeWorldPackage, createSegmentIndex, decodeHeightPNG, decodeRAW16, encodeGLB, encodeHeightPNG, encodeRAW16, encodeSplatPNGs, encodeZIP, gradientAt, makeExport, sampleHeight, slopeAtIndex, slopeQuantile, splinePoints, validateDocument, validatePlacementOverrides };
|
|
5407
|
+
export { BAKE_BIOME_STAGES, LANDFORMS, MATERIAL_IDS, MATS_FIELD, Mask, PARAMS, PROP_CELL_SIZE, RESOLUTIONS, STAND_FIELD, Terrain, TerrainEvaluator, applyPlacementOverrides, bakeBiome, bakeMesh, bakeTerrain, bakeWorldPackage, collarSampler, compilePropBuckets, createSegmentIndex, decodeHeightPNG, decodePropBucket, decodeRAW16, encodeGLB, encodeHeightPNG, encodeRAW16, encodeSplatPNGs, encodeZIP, farForestPlacements, fieldWeight, gradientAt, landform, landformBackdrop, liftMassif, makeExport, measureBands, orderedScatterRules, placeMassif, propAssetOf, propPackageFile, propPackageFiles, sampleHeight, scatterProps, slopeAtIndex, slopeQuantile, splinePoints, terrainStats, validateBiome, validateDocument, validatePlacementOverrides };
|