@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/editor/server.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { randomUUID, createHash } from 'crypto';
|
|
2
|
-
import { lstatSync, readFileSync, writeFileSync, renameSync, unlinkSync, watchFile, unwatchFile, openSync, fstatSync, readSync, closeSync, mkdirSync } from 'fs';
|
|
2
|
+
import { lstatSync, readFileSync, writeFileSync, renameSync, unlinkSync, watchFile, unwatchFile, openSync, fstatSync, readSync, closeSync, mkdirSync, existsSync } from 'fs';
|
|
3
3
|
import { isAbsolute, resolve, dirname, basename, join } from 'path';
|
|
4
|
+
import 'fs/promises';
|
|
5
|
+
import { pathToFileURL } from 'url';
|
|
4
6
|
import { Vector3, Matrix4, Quaternion } from 'three';
|
|
5
7
|
|
|
6
8
|
// src/editor/server.ts
|
|
@@ -260,6 +262,10 @@ function paintWeights(s, i, channel, weight) {
|
|
|
260
262
|
|
|
261
263
|
// src/core/erosion.ts
|
|
262
264
|
function thermal(height, n, size, { iterations = 20, talus = 32, rate = 0.22 } = {}, observations) {
|
|
265
|
+
if (!Number.isInteger(n) || n < 2)
|
|
266
|
+
throw RangeError("Thermal erosion requires grid resolution n >= 2");
|
|
267
|
+
if (!Number.isInteger(iterations) || iterations < 0 || iterations > 200)
|
|
268
|
+
throw RangeError("Thermal erosion iterations must be between 0 and 200");
|
|
263
269
|
const h = height.slice();
|
|
264
270
|
const delta = new Float64Array(h.length);
|
|
265
271
|
const limit = Math.tan(talus * Math.PI / 180) * (size / (n - 1));
|
|
@@ -268,18 +274,24 @@ function thermal(height, n, size, { iterations = 20, talus = 32, rate = 0.22 } =
|
|
|
268
274
|
for (let z = 0; z < n; z += 1) {
|
|
269
275
|
for (let x = 0; x < n; x += 1) {
|
|
270
276
|
const i = z * n + x;
|
|
277
|
+
const here = h[i];
|
|
271
278
|
let best = -1;
|
|
272
279
|
let diff = limit;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
280
|
+
if (x > 0 && here - h[i - 1] > diff) {
|
|
281
|
+
diff = here - h[i - 1];
|
|
282
|
+
best = i - 1;
|
|
283
|
+
}
|
|
284
|
+
if (x < n - 1 && here - h[i + 1] > diff) {
|
|
285
|
+
diff = here - h[i + 1];
|
|
286
|
+
best = i + 1;
|
|
287
|
+
}
|
|
288
|
+
if (z > 0 && here - h[i - n] > diff) {
|
|
289
|
+
diff = here - h[i - n];
|
|
290
|
+
best = i - n;
|
|
291
|
+
}
|
|
292
|
+
if (z < n - 1 && here - h[i + n] > diff) {
|
|
293
|
+
diff = here - h[i + n];
|
|
294
|
+
best = i + n;
|
|
283
295
|
}
|
|
284
296
|
if (best >= 0) {
|
|
285
297
|
const transfer = (diff - limit) * rate;
|
|
@@ -298,92 +310,177 @@ var brushes = /* @__PURE__ */ new Map();
|
|
|
298
310
|
function brush(radius) {
|
|
299
311
|
let cells = brushes.get(radius);
|
|
300
312
|
if (!cells) {
|
|
301
|
-
|
|
313
|
+
const dx = [];
|
|
314
|
+
const dz = [];
|
|
315
|
+
const raw = [];
|
|
302
316
|
let total = 0;
|
|
303
|
-
for (let
|
|
304
|
-
for (let
|
|
305
|
-
const d = Math.hypot(
|
|
317
|
+
for (let z = -radius; z <= radius; z += 1) {
|
|
318
|
+
for (let x = -radius; x <= radius; x += 1) {
|
|
319
|
+
const d = Math.hypot(x, z);
|
|
306
320
|
if (d > radius) continue;
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
|
|
321
|
+
const weight2 = 1 - d / (radius + 1);
|
|
322
|
+
dx.push(x);
|
|
323
|
+
dz.push(z);
|
|
324
|
+
raw.push(weight2);
|
|
325
|
+
total += weight2;
|
|
310
326
|
}
|
|
311
327
|
}
|
|
312
|
-
|
|
328
|
+
const weight = Float64Array.from(raw, (w) => w / total);
|
|
329
|
+
let full = 0;
|
|
330
|
+
for (let k = 0; k < weight.length; k += 1) full += weight[k];
|
|
331
|
+
cells = { dx: Int32Array.from(dx), dz: Int32Array.from(dz), full, radius, weight };
|
|
313
332
|
brushes.set(radius, cells);
|
|
314
333
|
}
|
|
315
334
|
return cells;
|
|
316
335
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
336
|
+
var MAX_AUTOMATIC_DROPLETS = 1025 * 1025;
|
|
337
|
+
var MAX_EXPLICIT_DROPLETS = 2e5;
|
|
338
|
+
function hydraulic(height, n, size, options = {}, observations) {
|
|
339
|
+
const {
|
|
340
|
+
droplets: requestedDroplets,
|
|
341
|
+
// A droplet has to be able to cross the world, so its step budget scales with the grid: a
|
|
342
|
+
// fixed 40 barely leaves a 257 world while a 65 world would walk off it in a few steps.
|
|
343
|
+
maxSteps = Math.round(Math.min(64, Math.max(24, n / 4))),
|
|
344
|
+
inertia = 0.2,
|
|
345
|
+
capacity = 4,
|
|
346
|
+
// Each step takes a small bite of the sediment the droplet can carry. A big bite carves
|
|
347
|
+
// fewer, deeper dimples that stand proud of their neighbours as spikes; many shallow bites
|
|
348
|
+
// over the same lines incise the same drainage with a smooth surface.
|
|
349
|
+
erosion = 0.03,
|
|
350
|
+
deposition = 0.3,
|
|
351
|
+
evaporation = 0.025,
|
|
352
|
+
seed = 1,
|
|
353
|
+
brushRadius = 3
|
|
354
|
+
} = options;
|
|
355
|
+
if (!Number.isInteger(n) || n < 2)
|
|
356
|
+
throw RangeError("Hydraulic erosion requires grid resolution n >= 2");
|
|
357
|
+
const droplets = requestedDroplets === void 0 ? Math.min(n * n, MAX_AUTOMATIC_DROPLETS) : requestedDroplets;
|
|
358
|
+
if (!Number.isSafeInteger(droplets) || droplets < 0)
|
|
359
|
+
throw RangeError("Hydraulic erosion droplets must be a non-negative integer");
|
|
360
|
+
if (requestedDroplets !== void 0 && droplets > MAX_EXPLICIT_DROPLETS)
|
|
361
|
+
throw RangeError(`Hydraulic erosion droplets must not exceed ${String(MAX_EXPLICIT_DROPLETS)}`);
|
|
362
|
+
if (!Number.isInteger(maxSteps) || maxSteps < 1 || maxSteps > 128)
|
|
363
|
+
throw RangeError("Hydraulic erosion maxSteps must be between 1 and 128");
|
|
335
364
|
const h = Float64Array.from(height);
|
|
336
365
|
const rnd = random(seed);
|
|
337
366
|
const cell = size / (n - 1);
|
|
338
|
-
const
|
|
367
|
+
const minSlope = 5e-3 * cell;
|
|
368
|
+
const oneMinusInertia = 1 - inertia;
|
|
369
|
+
const oneMinusEvaporation = 1 - evaporation;
|
|
370
|
+
const brushCells = brush(brushRadius);
|
|
371
|
+
const brushDx = brushCells.dx;
|
|
372
|
+
const brushDz = brushCells.dz;
|
|
373
|
+
const brushWeight = brushCells.weight;
|
|
374
|
+
const brushCount = brushDx.length;
|
|
375
|
+
const brushFull = brushCells.full;
|
|
376
|
+
const brushEdge = brushCells.radius;
|
|
377
|
+
const brushOffset = new Int32Array(brushCount);
|
|
378
|
+
for (let k = 0; k < brushCount; k += 1)
|
|
379
|
+
brushOffset[k] = brushDz[k] * n + brushDx[k];
|
|
380
|
+
const obsFlow = observations?.flow;
|
|
381
|
+
const obsSediment = observations?.sediment;
|
|
382
|
+
const obsDeposition = observations?.deposition;
|
|
339
383
|
const spread = (x, z, amount, floor = Number.NEGATIVE_INFINITY) => {
|
|
384
|
+
if (amount === 0) return 0;
|
|
385
|
+
const interior = x >= brushEdge && x < n - brushEdge && z >= brushEdge && z < n - brushEdge;
|
|
386
|
+
if (interior) {
|
|
387
|
+
const total2 = brushFull;
|
|
388
|
+
if (total2 <= 0) return 0;
|
|
389
|
+
const base = z * n + x;
|
|
390
|
+
if (amount < 0) {
|
|
391
|
+
let moved = 0;
|
|
392
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
393
|
+
const i = base + brushOffset[k];
|
|
394
|
+
const share = amount * brushWeight[k] / total2;
|
|
395
|
+
const avail = h[i] - floor;
|
|
396
|
+
const change = avail <= 0 ? -0 : -share < avail ? share : -avail;
|
|
397
|
+
h[i] = h[i] + change;
|
|
398
|
+
moved += change;
|
|
399
|
+
}
|
|
400
|
+
return moved;
|
|
401
|
+
}
|
|
402
|
+
if (obsDeposition !== void 0) {
|
|
403
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
404
|
+
const i = base + brushOffset[k];
|
|
405
|
+
const share = amount * brushWeight[k] / total2;
|
|
406
|
+
h[i] = h[i] + share;
|
|
407
|
+
obsDeposition[i] = obsDeposition[i] + share;
|
|
408
|
+
}
|
|
409
|
+
} else {
|
|
410
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
411
|
+
const i = base + brushOffset[k];
|
|
412
|
+
const share = amount * brushWeight[k] / total2;
|
|
413
|
+
h[i] = h[i] + share;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return amount;
|
|
417
|
+
}
|
|
340
418
|
let total = 0;
|
|
341
|
-
for (
|
|
342
|
-
const ix = x +
|
|
343
|
-
const iz = z +
|
|
344
|
-
if (ix >= 0 && ix < n && iz >= 0 && iz < n) total +=
|
|
419
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
420
|
+
const ix = x + brushDx[k];
|
|
421
|
+
const iz = z + brushDz[k];
|
|
422
|
+
if (ix >= 0 && ix < n && iz >= 0 && iz < n) total += brushWeight[k];
|
|
345
423
|
}
|
|
346
424
|
if (total <= 0) return 0;
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
425
|
+
if (amount < 0) {
|
|
426
|
+
let moved = 0;
|
|
427
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
428
|
+
const ix = x + brushDx[k];
|
|
429
|
+
const iz = z + brushDz[k];
|
|
430
|
+
if (ix < 0 || ix >= n || iz < 0 || iz >= n) continue;
|
|
431
|
+
const i = iz * n + ix;
|
|
432
|
+
const share = amount * brushWeight[k] / total;
|
|
433
|
+
const avail = h[i] - floor;
|
|
434
|
+
const change = avail <= 0 ? -0 : -share < avail ? share : -avail;
|
|
435
|
+
h[i] = h[i] + change;
|
|
436
|
+
moved += change;
|
|
437
|
+
}
|
|
438
|
+
return moved;
|
|
439
|
+
}
|
|
440
|
+
if (obsDeposition !== void 0) {
|
|
441
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
442
|
+
const ix = x + brushDx[k];
|
|
443
|
+
const iz = z + brushDz[k];
|
|
444
|
+
if (ix < 0 || ix >= n || iz < 0 || iz >= n) continue;
|
|
445
|
+
const i = iz * n + ix;
|
|
446
|
+
const share = amount * brushWeight[k] / total;
|
|
447
|
+
h[i] = h[i] + share;
|
|
448
|
+
obsDeposition[i] = obsDeposition[i] + share;
|
|
449
|
+
}
|
|
450
|
+
} else {
|
|
451
|
+
for (let k = 0; k < brushCount; k += 1) {
|
|
452
|
+
const ix = x + brushDx[k];
|
|
453
|
+
const iz = z + brushDz[k];
|
|
454
|
+
if (ix < 0 || ix >= n || iz < 0 || iz >= n) continue;
|
|
455
|
+
const i = iz * n + ix;
|
|
456
|
+
const share = amount * brushWeight[k] / total;
|
|
457
|
+
h[i] = h[i] + share;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return amount;
|
|
361
461
|
};
|
|
362
|
-
const
|
|
462
|
+
const probeOld = { value: 0, dx: 0, dz: 0, ix: 0, iz: 0 };
|
|
463
|
+
const probeNext = { value: 0, dx: 0, dz: 0, ix: 0, iz: 0 };
|
|
464
|
+
const get = (x, z, out) => {
|
|
363
465
|
const ix = Math.min(n - 2, Math.floor(x));
|
|
364
466
|
const iz = Math.min(n - 2, Math.floor(z));
|
|
365
467
|
const tx = x - ix;
|
|
366
468
|
const tz = z - iz;
|
|
367
469
|
const i = iz * n + ix;
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
tx
|
|
383
|
-
) / cell,
|
|
384
|
-
ix,
|
|
385
|
-
iz
|
|
386
|
-
};
|
|
470
|
+
const h00 = h[i];
|
|
471
|
+
const h10 = h[i + 1];
|
|
472
|
+
const h01 = h[i + n];
|
|
473
|
+
const h11 = h[i + n + 1];
|
|
474
|
+
const g10 = h10 - h00;
|
|
475
|
+
const g01 = h01 - h00;
|
|
476
|
+
const v0 = h00 + g10 * tx;
|
|
477
|
+
const v1 = h01 + (h11 - h01) * tx;
|
|
478
|
+
out.value = v0 + (v1 - v0) * tz;
|
|
479
|
+
out.dx = (g10 + (h11 - h01 - g10) * tz) / cell;
|
|
480
|
+
out.dz = (g01 + (h11 - h10 - g01) * tx) / cell;
|
|
481
|
+
out.ix = ix;
|
|
482
|
+
out.iz = iz;
|
|
483
|
+
return out;
|
|
387
484
|
};
|
|
388
485
|
for (let k = 0; k < droplets; k += 1) {
|
|
389
486
|
let x = rnd() * (n - 1 - 1e-3);
|
|
@@ -394,23 +491,30 @@ function hydraulic(height, n, size, {
|
|
|
394
491
|
let speed = 1;
|
|
395
492
|
let sediment = 0;
|
|
396
493
|
for (let step = 0; step < maxSteps; step += 1) {
|
|
397
|
-
const old = get(x, z);
|
|
398
|
-
if (
|
|
494
|
+
const old = get(x, z, probeOld);
|
|
495
|
+
if (obsFlow !== void 0 && obsSediment !== void 0) {
|
|
399
496
|
const tx = x - old.ix;
|
|
400
497
|
const tz = z - old.iz;
|
|
401
498
|
const i = old.iz * n + old.ix;
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
499
|
+
const w00 = (1 - tx) * (1 - tz);
|
|
500
|
+
const w10 = tx * (1 - tz);
|
|
501
|
+
const w01 = (1 - tx) * tz;
|
|
502
|
+
const w11 = tx * tz;
|
|
503
|
+
obsFlow[i] = obsFlow[i] + water * w00;
|
|
504
|
+
obsSediment[i] = obsSediment[i] + sediment * w00;
|
|
505
|
+
obsFlow[i + 1] = obsFlow[i + 1] + water * w10;
|
|
506
|
+
obsSediment[i + 1] = obsSediment[i + 1] + sediment * w10;
|
|
507
|
+
obsFlow[i + n] = obsFlow[i + n] + water * w01;
|
|
508
|
+
obsSediment[i + n] = obsSediment[i + n] + sediment * w01;
|
|
509
|
+
obsFlow[i + n + 1] = obsFlow[i + n + 1] + water * w11;
|
|
510
|
+
obsSediment[i + n + 1] = obsSediment[i + n + 1] + sediment * w11;
|
|
410
511
|
}
|
|
411
|
-
dx = dx * inertia - old.dx *
|
|
412
|
-
dz = dz * inertia - old.dz *
|
|
413
|
-
|
|
512
|
+
dx = dx * inertia - old.dx * oneMinusInertia;
|
|
513
|
+
dz = dz * inertia - old.dz * oneMinusInertia;
|
|
514
|
+
const largest = Math.max(Math.abs(dx), Math.abs(dz));
|
|
515
|
+
const scaledX = Math.abs(dx) / largest;
|
|
516
|
+
const scaledZ = Math.abs(dz) / largest;
|
|
517
|
+
let length = largest === 0 || largest === Number.POSITIVE_INFINITY ? largest : Math.sqrt(scaledX * scaledX + scaledZ * scaledZ) * largest;
|
|
414
518
|
if (length < 1e-9) {
|
|
415
519
|
const angle = rnd() * Math.PI * 2;
|
|
416
520
|
dx = Math.cos(angle);
|
|
@@ -425,31 +529,414 @@ function hydraulic(height, n, size, {
|
|
|
425
529
|
sediment = 0;
|
|
426
530
|
break;
|
|
427
531
|
}
|
|
428
|
-
const next = get(nx, nz);
|
|
532
|
+
const next = get(nx, nz, probeNext);
|
|
429
533
|
const dh = next.value - old.value;
|
|
430
|
-
const
|
|
534
|
+
const slope = -dh;
|
|
535
|
+
const cap = (slope > minSlope ? slope : minSlope) * speed * water * capacity;
|
|
431
536
|
if (dh > 0 || sediment > cap) {
|
|
432
|
-
const amount = dh > 0 ?
|
|
537
|
+
const amount = dh > 0 ? dh < sediment ? dh : sediment : (sediment - cap) * deposition;
|
|
433
538
|
spread(old.ix, old.iz, amount);
|
|
434
539
|
sediment -= amount;
|
|
435
540
|
} else {
|
|
436
|
-
const
|
|
541
|
+
const bite = (cap - sediment) * erosion;
|
|
542
|
+
const amount = bite < slope ? bite : slope;
|
|
437
543
|
sediment -= spread(old.ix, old.iz, -amount, next.value);
|
|
438
544
|
}
|
|
439
|
-
|
|
440
|
-
|
|
545
|
+
const sq = speed * speed - dh * 3;
|
|
546
|
+
speed = Math.sqrt(sq > 0.01 ? sq : 0.01);
|
|
547
|
+
water *= oneMinusEvaporation;
|
|
441
548
|
x = nx;
|
|
442
549
|
z = nz;
|
|
443
550
|
if (water < 0.02) break;
|
|
444
551
|
}
|
|
445
552
|
if (sediment > 0 && water < 0.02) {
|
|
446
|
-
const end = get(clamp(x, 0, n - 1.001), clamp(z, 0, n - 1.001));
|
|
553
|
+
const end = get(clamp(x, 0, n - 1.001), clamp(z, 0, n - 1.001), probeOld);
|
|
447
554
|
spread(end.ix, end.iz, sediment);
|
|
448
555
|
}
|
|
449
556
|
}
|
|
450
557
|
return Float32Array.from(h);
|
|
451
558
|
}
|
|
452
559
|
|
|
560
|
+
// src/core/fluvial.ts
|
|
561
|
+
var DX = Int8Array.of(-1, 0, 1, -1, 1, -1, 0, 1);
|
|
562
|
+
var DZ = Int8Array.of(-1, -1, -1, 0, 0, 1, 1, 1);
|
|
563
|
+
var DIST = Float64Array.from(DX, (dx, k) => Math.hypot(dx, DZ[k]));
|
|
564
|
+
var FILL_SLOPE = Math.tan(0.06 * Math.PI / 180);
|
|
565
|
+
var EPSILON = 1e-3;
|
|
566
|
+
function fillDepressions(h, n, epsilon = EPSILON) {
|
|
567
|
+
const total = n * n;
|
|
568
|
+
const keys = new Float64Array(total);
|
|
569
|
+
const cells = new Int32Array(total);
|
|
570
|
+
const closed = new Uint8Array(total);
|
|
571
|
+
let size = 0;
|
|
572
|
+
const push = (key, cell) => {
|
|
573
|
+
let k = size++;
|
|
574
|
+
while (k > 0) {
|
|
575
|
+
const parent = k - 1 >> 1;
|
|
576
|
+
if (keys[parent] <= key) break;
|
|
577
|
+
keys[k] = keys[parent];
|
|
578
|
+
cells[k] = cells[parent];
|
|
579
|
+
k = parent;
|
|
580
|
+
}
|
|
581
|
+
keys[k] = key;
|
|
582
|
+
cells[k] = cell;
|
|
583
|
+
};
|
|
584
|
+
const pop = () => {
|
|
585
|
+
const top = cells[0];
|
|
586
|
+
size -= 1;
|
|
587
|
+
const key = keys[size];
|
|
588
|
+
const cell = cells[size];
|
|
589
|
+
let k = 0;
|
|
590
|
+
for (; ; ) {
|
|
591
|
+
let child = 2 * k + 1;
|
|
592
|
+
if (child >= size) break;
|
|
593
|
+
if (child + 1 < size && keys[child + 1] < keys[child]) child += 1;
|
|
594
|
+
if (keys[child] >= key) break;
|
|
595
|
+
keys[k] = keys[child];
|
|
596
|
+
cells[k] = cells[child];
|
|
597
|
+
k = child;
|
|
598
|
+
}
|
|
599
|
+
keys[k] = key;
|
|
600
|
+
cells[k] = cell;
|
|
601
|
+
return top;
|
|
602
|
+
};
|
|
603
|
+
for (let z = 0; z < n; z += 1) {
|
|
604
|
+
for (let x = 0; x < n; x += 1) {
|
|
605
|
+
if (x > 0 && x < n - 1 && z > 0 && z < n - 1) continue;
|
|
606
|
+
const i = z * n + x;
|
|
607
|
+
closed[i] = 1;
|
|
608
|
+
push(h[i], i);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
while (size > 0) {
|
|
612
|
+
const c = pop();
|
|
613
|
+
const cx = c % n;
|
|
614
|
+
const cz = (c - cx) / n;
|
|
615
|
+
const level = h[c];
|
|
616
|
+
for (let k = 0; k < 8; k += 1) {
|
|
617
|
+
const x = cx + DX[k];
|
|
618
|
+
const z = cz + DZ[k];
|
|
619
|
+
if (x < 0 || z < 0 || x >= n || z >= n) continue;
|
|
620
|
+
const j = z * n + x;
|
|
621
|
+
if (closed[j]) continue;
|
|
622
|
+
closed[j] = 1;
|
|
623
|
+
if (h[j] <= level) h[j] = level + epsilon;
|
|
624
|
+
push(h[j], j);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
function createFlow(n) {
|
|
629
|
+
const total = n * n;
|
|
630
|
+
return {
|
|
631
|
+
receiver: new Int32Array(total),
|
|
632
|
+
order: new Int32Array(total),
|
|
633
|
+
cells: new Float64Array(total)
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
function routeFlow(h, n, flow) {
|
|
637
|
+
const { receiver, order, cells } = flow;
|
|
638
|
+
const total = n * n;
|
|
639
|
+
const head = new Int32Array(total).fill(-1);
|
|
640
|
+
const next = new Int32Array(total);
|
|
641
|
+
let roots = 0;
|
|
642
|
+
for (let z = 0; z < n; z += 1) {
|
|
643
|
+
for (let x = 0; x < n; x += 1) {
|
|
644
|
+
const i = z * n + x;
|
|
645
|
+
let best = -1;
|
|
646
|
+
if (x > 0 && x < n - 1 && z > 0 && z < n - 1) {
|
|
647
|
+
const here = h[i];
|
|
648
|
+
let bestSlope = 0;
|
|
649
|
+
for (let k = 0; k < 8; k += 1) {
|
|
650
|
+
const j = i + DZ[k] * n + DX[k];
|
|
651
|
+
const slope = (here - h[j]) / DIST[k];
|
|
652
|
+
if (slope > bestSlope) {
|
|
653
|
+
bestSlope = slope;
|
|
654
|
+
best = j;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
receiver[i] = best;
|
|
659
|
+
if (best < 0) order[roots++] = i;
|
|
660
|
+
else {
|
|
661
|
+
next[i] = head[best];
|
|
662
|
+
head[best] = i;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
for (let k = 0; k < total; k += 1) {
|
|
667
|
+
const i = order[k];
|
|
668
|
+
for (let d = head[i]; d >= 0; d = next[d]) order[roots++] = d;
|
|
669
|
+
}
|
|
670
|
+
cells.fill(1);
|
|
671
|
+
for (let k = total - 1; k >= 0; k -= 1) {
|
|
672
|
+
const i = order[k];
|
|
673
|
+
const r = receiver[i];
|
|
674
|
+
if (r >= 0) cells[r] = cells[r] + cells[i];
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
function fluvial(height, n, size, {
|
|
678
|
+
iterations = 40,
|
|
679
|
+
uplift = 1e-3,
|
|
680
|
+
erodibility = 5e-6,
|
|
681
|
+
timeStep = 2e4,
|
|
682
|
+
concavity = 0.45,
|
|
683
|
+
talus = 35,
|
|
684
|
+
relief
|
|
685
|
+
} = {}) {
|
|
686
|
+
const total = n * n;
|
|
687
|
+
const spacing = size / (n - 1);
|
|
688
|
+
const h = Float64Array.from(height);
|
|
689
|
+
const band = Math.min(4, n - 1);
|
|
690
|
+
for (let a = 0; a < n; a += 1)
|
|
691
|
+
for (let d = 1; d <= band; d += 1) {
|
|
692
|
+
h[a] = Math.min(h[a], h[d * n + a]);
|
|
693
|
+
h[(n - 1) * n + a] = Math.min(h[(n - 1) * n + a], h[(n - 1 - d) * n + a]);
|
|
694
|
+
h[a * n] = Math.min(h[a * n], h[a * n + d]);
|
|
695
|
+
h[a * n + n - 1] = Math.min(h[a * n + n - 1], h[a * n + n - 1 - d]);
|
|
696
|
+
}
|
|
697
|
+
fillDepressions(h, n, FILL_SLOPE * spacing);
|
|
698
|
+
let low = Number.POSITIVE_INFINITY;
|
|
699
|
+
let high = Number.NEGATIVE_INFINITY;
|
|
700
|
+
for (let i = 0; i < total; i += 1) {
|
|
701
|
+
low = Math.min(low, h[i]);
|
|
702
|
+
high = Math.max(high, h[i]);
|
|
703
|
+
}
|
|
704
|
+
const range2 = high - low || 1;
|
|
705
|
+
const rise = Float64Array.from(h, (v) => uplift * timeStep * (v - low) / range2);
|
|
706
|
+
const flow = createFlow(n);
|
|
707
|
+
const kdt = erodibility * timeStep;
|
|
708
|
+
const maxRise = Math.tan(talus * Math.PI / 180) * spacing;
|
|
709
|
+
const power = Float64Array.from(
|
|
710
|
+
{ length: total + 1 },
|
|
711
|
+
(_, c) => (c * spacing * spacing) ** concavity
|
|
712
|
+
);
|
|
713
|
+
for (let step = 0; step < iterations; step += 1) {
|
|
714
|
+
routeFlow(h, n, flow);
|
|
715
|
+
const { order, receiver, cells } = flow;
|
|
716
|
+
for (let k = 0; k < total; k += 1) {
|
|
717
|
+
const i = order[k];
|
|
718
|
+
const r = receiver[i];
|
|
719
|
+
if (r < 0) continue;
|
|
720
|
+
const delta = Math.abs(r - i);
|
|
721
|
+
const run = delta === 1 || delta === n ? 1 : Math.SQRT2;
|
|
722
|
+
const factor = kdt * power[cells[i]] / (spacing * run);
|
|
723
|
+
const below = h[r];
|
|
724
|
+
const solved = (h[i] + rise[i] + factor * below) / (1 + factor);
|
|
725
|
+
h[i] = Math.max(Math.min(solved, below + maxRise * run), below + EPSILON);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
for (let a = 0; a < n; a += 1) {
|
|
729
|
+
h[a] = h[n + a];
|
|
730
|
+
h[(n - 1) * n + a] = h[(n - 2) * n + a];
|
|
731
|
+
}
|
|
732
|
+
for (let a = 0; a < n; a += 1) {
|
|
733
|
+
h[a * n] = h[a * n + 1];
|
|
734
|
+
h[a * n + n - 1] = h[a * n + n - 2];
|
|
735
|
+
}
|
|
736
|
+
if (relief !== void 0) {
|
|
737
|
+
let lo = Number.POSITIVE_INFINITY;
|
|
738
|
+
let hi = Number.NEGATIVE_INFINITY;
|
|
739
|
+
for (const v of h) {
|
|
740
|
+
lo = Math.min(lo, v);
|
|
741
|
+
hi = Math.max(hi, v);
|
|
742
|
+
}
|
|
743
|
+
const gain = relief / (hi - lo || 1);
|
|
744
|
+
for (let i = 0; i < total; i += 1) h[i] = lo + (h[i] - lo) * gain;
|
|
745
|
+
}
|
|
746
|
+
return Float32Array.from(h);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// src/core/gully.ts
|
|
750
|
+
var STEP_SECONDS = 0.1;
|
|
751
|
+
var FLUX_KEEP = 0.99;
|
|
752
|
+
var DISSOLVE = 0.3;
|
|
753
|
+
var DEPOSIT = 0.02;
|
|
754
|
+
var EVAPORATE = 0.05;
|
|
755
|
+
var DEPTH_REF = 0.01;
|
|
756
|
+
var DEPTH_MAX = 0.3;
|
|
757
|
+
var MIN_TILT = 0.05;
|
|
758
|
+
var GRAVITY = 9.81;
|
|
759
|
+
var MAX_ITERATIONS = 200;
|
|
760
|
+
function gully(height, n, size, { iterations = 200, capacity = 0.05, rain = 5e-4 } = {}) {
|
|
761
|
+
if (!Number.isInteger(n) || n < 2)
|
|
762
|
+
throw new RangeError("Gully erosion requires grid resolution n >= 2");
|
|
763
|
+
if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS)
|
|
764
|
+
throw new RangeError(`Gully erosion iterations must be between 0 and ${MAX_ITERATIONS}`);
|
|
765
|
+
if (!Number.isFinite(capacity) || capacity < 0 || capacity > 1e6)
|
|
766
|
+
throw new RangeError("Gully erosion capacity must be between 0 and 1000000");
|
|
767
|
+
if (!Number.isFinite(rain) || rain < 0 || rain > 1)
|
|
768
|
+
throw new RangeError("Gully erosion rain must be between 0 and 1 metres per step");
|
|
769
|
+
const total = n * n;
|
|
770
|
+
let h = Float64Array.from(height);
|
|
771
|
+
let hTmp = new Float64Array(total);
|
|
772
|
+
const water = new Float64Array(total);
|
|
773
|
+
const waterBefore = new Float64Array(total);
|
|
774
|
+
const speed = new Float64Array(total);
|
|
775
|
+
const sediment = new Float64Array(total);
|
|
776
|
+
const sedTmp = new Float64Array(total);
|
|
777
|
+
const fluxL = new Float64Array(total);
|
|
778
|
+
const fluxR = new Float64Array(total);
|
|
779
|
+
const fluxT = new Float64Array(total);
|
|
780
|
+
const fluxB = new Float64Array(total);
|
|
781
|
+
const cell = size / (n - 1);
|
|
782
|
+
const cellArea = cell * cell;
|
|
783
|
+
const gain = STEP_SECONDS * GRAVITY * cell;
|
|
784
|
+
const evaporateKeep = Math.max(1 - EVAPORATE * STEP_SECONDS, 0);
|
|
785
|
+
const share = (flow, depth) => depth > 0 ? flow * STEP_SECONDS / (depth * cellArea) : 0;
|
|
786
|
+
for (let step = 0; step < iterations; step += 1) {
|
|
787
|
+
for (let z = 0; z < n; z += 1) {
|
|
788
|
+
for (let x = 0; x < n; x += 1) {
|
|
789
|
+
const i = z * n + x;
|
|
790
|
+
const depth = water[i] + rain;
|
|
791
|
+
const surface = h[i] + depth;
|
|
792
|
+
const ground = h[i];
|
|
793
|
+
const left = x > 0 ? h[i - 1] + water[i - 1] + rain : ground;
|
|
794
|
+
const right = x < n - 1 ? h[i + 1] + water[i + 1] + rain : ground;
|
|
795
|
+
const top = z > 0 ? h[i - n] + water[i - n] + rain : ground;
|
|
796
|
+
const bottom = z < n - 1 ? h[i + n] + water[i + n] + rain : ground;
|
|
797
|
+
let l = fluxL[i] * FLUX_KEEP + gain * (surface - left);
|
|
798
|
+
let r = fluxR[i] * FLUX_KEEP + gain * (surface - right);
|
|
799
|
+
let t = fluxT[i] * FLUX_KEEP + gain * (surface - top);
|
|
800
|
+
let b = fluxB[i] * FLUX_KEEP + gain * (surface - bottom);
|
|
801
|
+
if (l < 0) l = 0;
|
|
802
|
+
if (r < 0) r = 0;
|
|
803
|
+
if (t < 0) t = 0;
|
|
804
|
+
if (b < 0) b = 0;
|
|
805
|
+
const sum = l + r + t + b;
|
|
806
|
+
if (sum > 0) {
|
|
807
|
+
const keep = Math.min(1, depth * cellArea / (sum * STEP_SECONDS));
|
|
808
|
+
l *= keep;
|
|
809
|
+
r *= keep;
|
|
810
|
+
t *= keep;
|
|
811
|
+
b *= keep;
|
|
812
|
+
}
|
|
813
|
+
fluxL[i] = l;
|
|
814
|
+
fluxR[i] = r;
|
|
815
|
+
fluxT[i] = t;
|
|
816
|
+
fluxB[i] = b;
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
for (let z = 0; z < n; z += 1) {
|
|
820
|
+
for (let x = 0; x < n; x += 1) {
|
|
821
|
+
const i = z * n + x;
|
|
822
|
+
const inL = x > 0 ? fluxR[i - 1] : 0;
|
|
823
|
+
const inR = x < n - 1 ? fluxL[i + 1] : 0;
|
|
824
|
+
const inT = z > 0 ? fluxB[i - n] : 0;
|
|
825
|
+
const inB = z < n - 1 ? fluxT[i + n] : 0;
|
|
826
|
+
const before = water[i] + rain;
|
|
827
|
+
const after = Math.max(
|
|
828
|
+
0,
|
|
829
|
+
before + STEP_SECONDS * (inL + inR + inT + inB - fluxL[i] - fluxR[i] - fluxT[i] - fluxB[i]) / cellArea
|
|
830
|
+
);
|
|
831
|
+
const depth = 0.5 * (before + after);
|
|
832
|
+
const acrossX = inL - fluxL[i] + fluxR[i] - inR;
|
|
833
|
+
const acrossZ = inT - fluxT[i] + fluxB[i] - inB;
|
|
834
|
+
speed[i] = depth > 1e-4 ? Math.sqrt(acrossX * acrossX + acrossZ * acrossZ) * 0.5 / (cell * depth) : 0;
|
|
835
|
+
water[i] = after;
|
|
836
|
+
waterBefore[i] = before;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
for (let z = 0; z < n; z += 1) {
|
|
840
|
+
for (let x = 0; x < n; x += 1) {
|
|
841
|
+
const i = z * n + x;
|
|
842
|
+
const gx = (h[z * n + Math.min(x + 1, n - 1)] - h[z * n + Math.max(x - 1, 0)]) / (2 * cell);
|
|
843
|
+
const gy = (h[Math.min(z + 1, n - 1) * n + x] - h[Math.max(z - 1, 0) * n + x]) / (2 * cell);
|
|
844
|
+
const squared = gx * gx + gy * gy;
|
|
845
|
+
const sine = Math.max(Math.sqrt(squared / (1 + squared)), MIN_TILT);
|
|
846
|
+
const depth = water[i];
|
|
847
|
+
const transport = capacity * sine * speed[i] * Math.min(depth / DEPTH_REF, 1) * Math.max(1 - depth / DEPTH_MAX, 0);
|
|
848
|
+
let surface = h[i];
|
|
849
|
+
let load = sediment[i];
|
|
850
|
+
if (transport > load) {
|
|
851
|
+
const cut = DISSOLVE * (transport - load);
|
|
852
|
+
surface -= cut;
|
|
853
|
+
load += cut;
|
|
854
|
+
} else {
|
|
855
|
+
const drop = DEPOSIT * (load - transport);
|
|
856
|
+
surface += drop;
|
|
857
|
+
load -= drop;
|
|
858
|
+
}
|
|
859
|
+
hTmp[i] = surface;
|
|
860
|
+
sedTmp[i] = load;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
const passed = h;
|
|
864
|
+
h = hTmp;
|
|
865
|
+
hTmp = passed;
|
|
866
|
+
for (let z = 0; z < n; z += 1) {
|
|
867
|
+
for (let x = 0; x < n; x += 1) {
|
|
868
|
+
const i = z * n + x;
|
|
869
|
+
let load = sedTmp[i] * (1 - share(
|
|
870
|
+
fluxL[i] + fluxR[i] + fluxT[i] + fluxB[i],
|
|
871
|
+
waterBefore[i]
|
|
872
|
+
));
|
|
873
|
+
if (x > 0)
|
|
874
|
+
load += sedTmp[i - 1] * share(fluxR[i - 1], waterBefore[i - 1]);
|
|
875
|
+
if (x < n - 1)
|
|
876
|
+
load += sedTmp[i + 1] * share(fluxL[i + 1], waterBefore[i + 1]);
|
|
877
|
+
if (z > 0)
|
|
878
|
+
load += sedTmp[i - n] * share(fluxB[i - n], waterBefore[i - n]);
|
|
879
|
+
if (z < n - 1)
|
|
880
|
+
load += sedTmp[i + n] * share(fluxT[i + n], waterBefore[i + n]);
|
|
881
|
+
sediment[i] = load;
|
|
882
|
+
water[i] = water[i] * evaporateKeep;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
return Float32Array.from(h, (surface, i) => surface + sediment[i]);
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
// src/core/scree.ts
|
|
890
|
+
var NEIGHBOURS = [
|
|
891
|
+
[-1, -1],
|
|
892
|
+
[0, -1],
|
|
893
|
+
[1, -1],
|
|
894
|
+
[-1, 0],
|
|
895
|
+
[1, 0],
|
|
896
|
+
[-1, 1],
|
|
897
|
+
[0, 1],
|
|
898
|
+
[1, 1]
|
|
899
|
+
];
|
|
900
|
+
var DISTANCE = Float64Array.from(NEIGHBOURS, ([dx, dz]) => Math.hypot(dx, dz));
|
|
901
|
+
var MAX_ITERATIONS2 = 200;
|
|
902
|
+
function scree(height, n, size, { iterations = 200, talus = 35, rate = 0.05 } = {}) {
|
|
903
|
+
if (!Number.isInteger(n) || n < 2)
|
|
904
|
+
throw new RangeError("Scree slide requires grid resolution n >= 2");
|
|
905
|
+
if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS2)
|
|
906
|
+
throw new RangeError(`Scree slide iterations must be between 0 and ${MAX_ITERATIONS2}`);
|
|
907
|
+
if (!Number.isFinite(talus) || talus < 0 || talus > 89.9)
|
|
908
|
+
throw new RangeError("Scree slide talus must be between 0 and 89.9 degrees");
|
|
909
|
+
if (!Number.isFinite(rate) || rate < 0 || rate > 1)
|
|
910
|
+
throw new RangeError("Scree slide rate must be between 0 and 1");
|
|
911
|
+
const cell = size / (n - 1);
|
|
912
|
+
const limit = Float64Array.from(DISTANCE, (d) => Math.tan(talus * Math.PI / 180) * cell * d);
|
|
913
|
+
let source = Float64Array.from(height);
|
|
914
|
+
let target = new Float64Array(source.length);
|
|
915
|
+
for (let k = 0; k < iterations; k += 1) {
|
|
916
|
+
for (let z = 0; z < n; z += 1) {
|
|
917
|
+
for (let x = 0; x < n; x += 1) {
|
|
918
|
+
const i = z * n + x;
|
|
919
|
+
const here = source[i];
|
|
920
|
+
let received = 0;
|
|
921
|
+
for (let d = 0; d < 8; d += 1) {
|
|
922
|
+
const nx = x + NEIGHBOURS[d][0];
|
|
923
|
+
const nz = z + NEIGHBOURS[d][1];
|
|
924
|
+
if (nx < 0 || nz < 0 || nx >= n || nz >= n) continue;
|
|
925
|
+
const other = source[nz * n + nx];
|
|
926
|
+
const gap = limit[d];
|
|
927
|
+
if (other - here > gap) received += other - here - gap;
|
|
928
|
+
else if (here - other > gap) received -= here - other - gap;
|
|
929
|
+
}
|
|
930
|
+
target[i] = here + rate * received;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
const swap = source;
|
|
934
|
+
source = target;
|
|
935
|
+
target = swap;
|
|
936
|
+
}
|
|
937
|
+
return Float32Array.from(source);
|
|
938
|
+
}
|
|
939
|
+
|
|
453
940
|
// src/core/operations.ts
|
|
454
941
|
var OPERATION_TYPES = Object.freeze([
|
|
455
942
|
"noise",
|
|
@@ -870,7 +1357,7 @@ function applyOperation(s, layer) {
|
|
|
870
1357
|
talus: new Float32Array(s.height.length)
|
|
871
1358
|
};
|
|
872
1359
|
s.erosion ??= structuredClone(observations);
|
|
873
|
-
const eroded = p.method === "hydraulic" ? hydraulic(
|
|
1360
|
+
const eroded = p.method === "fluvial" ? fluvial(s.height, n, s.size, p) : p.method === "hydraulic" ? hydraulic(
|
|
874
1361
|
s.height,
|
|
875
1362
|
n,
|
|
876
1363
|
s.size,
|
|
@@ -885,7 +1372,7 @@ function applyOperation(s, layer) {
|
|
|
885
1372
|
evaporation: p.evaporation
|
|
886
1373
|
},
|
|
887
1374
|
observations
|
|
888
|
-
) : thermal(s.height, n, s.size, p, observations);
|
|
1375
|
+
) : 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);
|
|
889
1376
|
let influence = mask;
|
|
890
1377
|
if (p.at || p.points) influence = brushWeights(s, p, mask, seed);
|
|
891
1378
|
for (let i = 0; i < s.height.length; i += 1) {
|
|
@@ -1039,6 +1526,7 @@ function finalizeState(s) {
|
|
|
1039
1526
|
}
|
|
1040
1527
|
const wet = (i, x, z, h) => {
|
|
1041
1528
|
if (s.waters.some((water) => water.mask[i])) return true;
|
|
1529
|
+
if (s.waters.some((water) => water.kind === "ocean" && h < water.level + 0.35)) return true;
|
|
1042
1530
|
for (const river of s.rivers) {
|
|
1043
1531
|
const half = river.width / 2;
|
|
1044
1532
|
for (let k = 1; k < river.points.length; k += 1) {
|
|
@@ -1111,7 +1599,7 @@ function finalizeState(s) {
|
|
|
1111
1599
|
accepted.push({
|
|
1112
1600
|
id: `${rule.id}:candidate:${seed}:${attempt}`,
|
|
1113
1601
|
layer: rule.id,
|
|
1114
|
-
asset: rule.asset
|
|
1602
|
+
asset: rule.asset,
|
|
1115
1603
|
position: [x, h + (rule.offsetY ?? 0), z],
|
|
1116
1604
|
rotation: yaw,
|
|
1117
1605
|
scale: itemScale,
|
|
@@ -1123,7 +1611,7 @@ function finalizeState(s) {
|
|
|
1123
1611
|
bucket.push(index);
|
|
1124
1612
|
buckets.set(key, bucket);
|
|
1125
1613
|
}
|
|
1126
|
-
s.instances.push(
|
|
1614
|
+
for (const instance of accepted) s.instances.push(instance);
|
|
1127
1615
|
if (accepted.length < count)
|
|
1128
1616
|
s.diagnostics.push(
|
|
1129
1617
|
`Scatter '${rule.id}': placed ${accepted.length}/${count}; masks or spacing exhausted the attempt budget.`
|
|
@@ -1198,6 +1686,10 @@ var PARAMS = Object.freeze({
|
|
|
1198
1686
|
"method",
|
|
1199
1687
|
"iterations",
|
|
1200
1688
|
"talus",
|
|
1689
|
+
"uplift",
|
|
1690
|
+
"erodibility",
|
|
1691
|
+
"timeStep",
|
|
1692
|
+
"relief",
|
|
1201
1693
|
"rate",
|
|
1202
1694
|
"droplets",
|
|
1203
1695
|
"maxSteps",
|
|
@@ -1205,7 +1697,8 @@ var PARAMS = Object.freeze({
|
|
|
1205
1697
|
"capacity",
|
|
1206
1698
|
"erosion",
|
|
1207
1699
|
"deposition",
|
|
1208
|
-
"evaporation"
|
|
1700
|
+
"evaporation",
|
|
1701
|
+
"rain"
|
|
1209
1702
|
],
|
|
1210
1703
|
terrace: ["step", "softness", "strength", "offset"],
|
|
1211
1704
|
materials: ["base", "rules", "reset"],
|
|
@@ -1247,14 +1740,23 @@ function fail(message) {
|
|
|
1247
1740
|
}
|
|
1248
1741
|
function finiteTree(value, path = "value", depth = 0) {
|
|
1249
1742
|
if (depth > 32) fail(`${path}: nesting is too deep`);
|
|
1250
|
-
if (typeof value === "number"
|
|
1743
|
+
if (typeof value === "number") {
|
|
1744
|
+
if (!Number.isFinite(value)) fail(`${path} must be finite`);
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1251
1747
|
if (value === void 0 || typeof value === "function" || typeof value === "bigint" || typeof value === "symbol")
|
|
1252
1748
|
fail(`${path} is not JSON-serializable`);
|
|
1253
1749
|
if (value && typeof value === "object") {
|
|
1254
1750
|
if (ArrayBuffer.isView(value)) fail(`${path}: use a plain array in recipes`);
|
|
1255
|
-
for (const
|
|
1256
|
-
if (
|
|
1751
|
+
for (const key of Object.keys(value)) {
|
|
1752
|
+
if (key === "__proto__" || key === "prototype" || key === "constructor")
|
|
1257
1753
|
fail(`${path}: unsafe object key`);
|
|
1754
|
+
const entry = value[key];
|
|
1755
|
+
if (typeof entry === "number") {
|
|
1756
|
+
if (depth + 1 > 32) fail(`${path}.${key}: nesting is too deep`);
|
|
1757
|
+
if (!Number.isFinite(entry)) fail(`${path}.${key} must be finite`);
|
|
1758
|
+
continue;
|
|
1759
|
+
}
|
|
1258
1760
|
finiteTree(entry, `${path}.${key}`, depth + 1);
|
|
1259
1761
|
}
|
|
1260
1762
|
}
|
|
@@ -1504,7 +2006,8 @@ function validateLayer(layer) {
|
|
|
1504
2006
|
"rate",
|
|
1505
2007
|
"value",
|
|
1506
2008
|
"waterWidth",
|
|
1507
|
-
"maxGrade"
|
|
2009
|
+
"maxGrade",
|
|
2010
|
+
"rain"
|
|
1508
2011
|
])
|
|
1509
2012
|
if (params[key] !== void 0) num(params[key], key, 0, 1);
|
|
1510
2013
|
if (params.spacing !== void 0) num(params.spacing, "spacing", 0.01, 2);
|
|
@@ -1535,6 +2038,13 @@ function validateLayer(layer) {
|
|
|
1535
2038
|
if (params[key] !== void 0) num(params[key], key, key === "count" ? 0 : 1, max, true);
|
|
1536
2039
|
if (params.persistence !== void 0) num(params.persistence, "persistence", 0.01, 1);
|
|
1537
2040
|
if (params.lacunarity !== void 0) num(params.lacunarity, "lacunarity", 1, 4);
|
|
2041
|
+
for (const [key, min, max] of [
|
|
2042
|
+
["uplift", 1e-9, 1],
|
|
2043
|
+
["erodibility", 1e-9, 1],
|
|
2044
|
+
["timeStep", 1, 1e7],
|
|
2045
|
+
["relief", 1, 1e5]
|
|
2046
|
+
])
|
|
2047
|
+
if (params[key] !== void 0) num(params[key], key, min, max);
|
|
1538
2048
|
for (const key of ["minSlope", "maxSlope", "talus"])
|
|
1539
2049
|
if (params[key] !== void 0) num(params[key], key, 0, key === "talus" ? 89.9 : 90);
|
|
1540
2050
|
if (params.minHeight !== void 0 && params.maxHeight !== void 0 && params.minHeight > params.maxHeight)
|
|
@@ -1552,7 +2062,8 @@ function validateLayer(layer) {
|
|
|
1552
2062
|
if (rule.strength !== void 0) num(rule.strength, "rule.strength", 0, 1);
|
|
1553
2063
|
}
|
|
1554
2064
|
}
|
|
1555
|
-
if (params.method !== void 0)
|
|
2065
|
+
if (params.method !== void 0)
|
|
2066
|
+
enumeration(params.method, "method", ["thermal", "hydraulic", "fluvial", "gully", "scree"]);
|
|
1556
2067
|
if (params.mode !== void 0) enumeration(params.mode, "mode", ["fbm", "ridged", "billow"]);
|
|
1557
2068
|
if (params.shape !== void 0)
|
|
1558
2069
|
enumeration(
|
|
@@ -1580,6 +2091,10 @@ function validateLayer(layer) {
|
|
|
1580
2091
|
}
|
|
1581
2092
|
if (params.name !== void 0) identifier(params.name, "name");
|
|
1582
2093
|
if (params.asset !== void 0) identifier(params.asset, "asset");
|
|
2094
|
+
if (layer.type === "scatter" && typeof params.asset !== "string")
|
|
2095
|
+
fail(
|
|
2096
|
+
'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'
|
|
2097
|
+
);
|
|
1583
2098
|
if (["heightmap", "paste"].includes(layer.type) && !params.data)
|
|
1584
2099
|
fail(`${layer.type} requires heightmap data`);
|
|
1585
2100
|
const data = params.data;
|
|
@@ -1666,8 +2181,8 @@ var TerrainEvaluator = class {
|
|
|
1666
2181
|
validateDocument(doc);
|
|
1667
2182
|
validateConfig({ ...doc.config, resolution });
|
|
1668
2183
|
const keys = [];
|
|
1669
|
-
let key = JSON.stringify({ ...doc.config, resolution });
|
|
1670
|
-
for (const layer of doc.layers) {
|
|
2184
|
+
let key = this.limit > 0 ? JSON.stringify({ ...doc.config, resolution }) : "";
|
|
2185
|
+
for (const layer of this.limit > 0 ? doc.layers : []) {
|
|
1671
2186
|
key += `|${JSON.stringify(layer)}`;
|
|
1672
2187
|
keys.push(key);
|
|
1673
2188
|
}
|
|
@@ -2004,7 +2519,7 @@ var Terrain = class _Terrain {
|
|
|
2004
2519
|
biome(options = {}) {
|
|
2005
2520
|
return this._operation("biome", options);
|
|
2006
2521
|
}
|
|
2007
|
-
scatter(options
|
|
2522
|
+
scatter(options) {
|
|
2008
2523
|
return this._operation("scatter", options);
|
|
2009
2524
|
}
|
|
2010
2525
|
clear(options) {
|
|
@@ -2026,6 +2541,31 @@ var Terrain = class _Terrain {
|
|
|
2026
2541
|
return this._operation("paste", options);
|
|
2027
2542
|
}
|
|
2028
2543
|
};
|
|
2544
|
+
var MISSING_ASSETS = "terrain_generate needs @threenative/assets in the game project. Run: npm i -D @threenative/assets";
|
|
2545
|
+
function installedEntry(projectRoot, name) {
|
|
2546
|
+
let directory = resolve(projectRoot);
|
|
2547
|
+
while (true) {
|
|
2548
|
+
const manifestFile = join(directory, "node_modules", name, "package.json");
|
|
2549
|
+
if (existsSync(manifestFile)) {
|
|
2550
|
+
const manifest = JSON.parse(readFileSync(manifestFile, "utf8"));
|
|
2551
|
+
const root = manifest.exports?.["."];
|
|
2552
|
+
const relative2 = (typeof root === "string" ? root : root?.import) ?? manifest.main;
|
|
2553
|
+
return relative2 === void 0 ? void 0 : resolve(dirname(manifestFile), relative2);
|
|
2554
|
+
}
|
|
2555
|
+
const parent = dirname(directory);
|
|
2556
|
+
if (parent === directory) return void 0;
|
|
2557
|
+
directory = parent;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
async function loadCooker(projectRoot) {
|
|
2561
|
+
const entry = installedEntry(projectRoot, "@threenative/assets");
|
|
2562
|
+
if (entry === void 0 || !existsSync(entry)) throw new Error(MISSING_ASSETS);
|
|
2563
|
+
const loaded = await import(pathToFileURL(entry).href);
|
|
2564
|
+
if (typeof loaded.cookTerrainWorld !== "function") {
|
|
2565
|
+
throw new Error(`${MISSING_ASSETS} (the installed version has no cookTerrainWorld).`);
|
|
2566
|
+
}
|
|
2567
|
+
return loaded;
|
|
2568
|
+
}
|
|
2029
2569
|
|
|
2030
2570
|
// src/editor/images.ts
|
|
2031
2571
|
var SURFACE_CHANNELS = {
|
|
@@ -3209,6 +3749,7 @@ function inspectSpatial(state, revision2, query) {
|
|
|
3209
3749
|
// src/editor/server.ts
|
|
3210
3750
|
var PREFIX = "/terrain-editor/";
|
|
3211
3751
|
var MAX_BYTES = 64 * 1024 * 1024;
|
|
3752
|
+
var MAX_SMALL_BYTES = 1024 * 1024;
|
|
3212
3753
|
var MAX_REFERENCES = 16;
|
|
3213
3754
|
function validate(value) {
|
|
3214
3755
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
@@ -3270,12 +3811,26 @@ var EditorError = class extends Error {
|
|
|
3270
3811
|
var TerrainEditorDocument = class {
|
|
3271
3812
|
path;
|
|
3272
3813
|
#snapshot;
|
|
3814
|
+
#lastValidDocument = null;
|
|
3273
3815
|
#listeners = /* @__PURE__ */ new Set();
|
|
3274
3816
|
constructor(path) {
|
|
3275
3817
|
if (!isAbsolute(path)) throw new Error("Document path must be absolute");
|
|
3276
3818
|
this.path = resolve(path);
|
|
3277
|
-
|
|
3278
|
-
|
|
3819
|
+
try {
|
|
3820
|
+
const document = this.#read();
|
|
3821
|
+
this.#lastValidDocument = document;
|
|
3822
|
+
this.#snapshot = { document, revision: revision(document), diagnostic: null };
|
|
3823
|
+
} catch (error) {
|
|
3824
|
+
const fallback = {
|
|
3825
|
+
version: 1,
|
|
3826
|
+
recipe: new Terrain().toJSON()
|
|
3827
|
+
};
|
|
3828
|
+
this.#snapshot = {
|
|
3829
|
+
document: fallback,
|
|
3830
|
+
revision: revision(fallback),
|
|
3831
|
+
diagnostic: error instanceof Error ? error.message : String(error)
|
|
3832
|
+
};
|
|
3833
|
+
}
|
|
3279
3834
|
}
|
|
3280
3835
|
#read() {
|
|
3281
3836
|
const info = lstatSync(this.path);
|
|
@@ -3297,6 +3852,7 @@ var TerrainEditorDocument = class {
|
|
|
3297
3852
|
try {
|
|
3298
3853
|
const document = this.#read();
|
|
3299
3854
|
const nextRevision = revision(document);
|
|
3855
|
+
this.#lastValidDocument = document;
|
|
3300
3856
|
if (nextRevision === this.#snapshot.revision && this.#snapshot.diagnostic === null) return;
|
|
3301
3857
|
this.#snapshot = { document, revision: nextRevision, diagnostic: null };
|
|
3302
3858
|
} catch (error) {
|
|
@@ -3304,6 +3860,32 @@ var TerrainEditorDocument = class {
|
|
|
3304
3860
|
}
|
|
3305
3861
|
this.#emit();
|
|
3306
3862
|
}
|
|
3863
|
+
restoreLastValid() {
|
|
3864
|
+
if (!this.#lastValidDocument)
|
|
3865
|
+
throw new EditorError(409, "No valid save to restore; the document has never been valid");
|
|
3866
|
+
const target = this.#lastValidDocument;
|
|
3867
|
+
const temporary = `${this.path}.${randomUUID()}.tmp`;
|
|
3868
|
+
try {
|
|
3869
|
+
writeFileSync(temporary, `${JSON.stringify(target, null, 2)}
|
|
3870
|
+
`, { flag: "wx", mode: 384 });
|
|
3871
|
+
renameSync(temporary, this.path);
|
|
3872
|
+
} catch (error) {
|
|
3873
|
+
try {
|
|
3874
|
+
unlinkSync(temporary);
|
|
3875
|
+
} catch (cleanupError) {
|
|
3876
|
+
if (cleanupError.code !== "ENOENT")
|
|
3877
|
+
throw new AggregateError(
|
|
3878
|
+
[error, cleanupError],
|
|
3879
|
+
"Document save and temporary-file cleanup failed"
|
|
3880
|
+
);
|
|
3881
|
+
}
|
|
3882
|
+
throw error;
|
|
3883
|
+
}
|
|
3884
|
+
this.#lastValidDocument = target;
|
|
3885
|
+
this.#snapshot = { document: target, revision: revision(target), diagnostic: null };
|
|
3886
|
+
this.#emit();
|
|
3887
|
+
return this.snapshot();
|
|
3888
|
+
}
|
|
3307
3889
|
commit(input) {
|
|
3308
3890
|
this.refresh();
|
|
3309
3891
|
if (this.#snapshot.diagnostic)
|
|
@@ -3345,24 +3927,30 @@ var TerrainEditorDocument = class {
|
|
|
3345
3927
|
throw error;
|
|
3346
3928
|
}
|
|
3347
3929
|
this.#snapshot = { document: next, revision: nextRevision, diagnostic: null };
|
|
3930
|
+
this.#lastValidDocument = next;
|
|
3348
3931
|
this.#emit();
|
|
3349
3932
|
return this.snapshot();
|
|
3350
3933
|
}
|
|
3351
3934
|
};
|
|
3352
|
-
async function body(request) {
|
|
3353
|
-
if (request.headers["content-
|
|
3935
|
+
async function body(request, maxBytes = MAX_SMALL_BYTES, parse = true) {
|
|
3936
|
+
if (Number(request.headers["content-length"]) > maxBytes) {
|
|
3937
|
+
request.resume();
|
|
3938
|
+
throw new EditorError(413, `Request exceeds ${maxBytes / (1024 * 1024)} MiB`);
|
|
3939
|
+
}
|
|
3940
|
+
if (parse && request.headers["content-type"]?.split(";")[0] !== "application/json")
|
|
3354
3941
|
throw new EditorError(415, "Expected application/json");
|
|
3355
|
-
if (Number(request.headers["content-length"]) > MAX_BYTES)
|
|
3356
|
-
throw new EditorError(413, "Request exceeds 64 MiB");
|
|
3357
3942
|
const chunks = [];
|
|
3358
3943
|
let bytes = 0;
|
|
3359
|
-
for await (const chunk of request) {
|
|
3944
|
+
for await (const chunk of request.iterator({ destroyOnReturn: false })) {
|
|
3360
3945
|
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
3361
3946
|
bytes += buffer.length;
|
|
3362
|
-
if (bytes >
|
|
3363
|
-
|
|
3947
|
+
if (bytes > maxBytes) {
|
|
3948
|
+
request.resume();
|
|
3949
|
+
throw new EditorError(413, `Request exceeds ${maxBytes / (1024 * 1024)} MiB`);
|
|
3950
|
+
}
|
|
3951
|
+
if (parse) chunks.push(buffer);
|
|
3364
3952
|
}
|
|
3365
|
-
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
3953
|
+
return parse ? JSON.parse(Buffer.concat(chunks).toString("utf8")) : void 0;
|
|
3366
3954
|
}
|
|
3367
3955
|
function json(response, status, value) {
|
|
3368
3956
|
response.writeHead(status, {
|
|
@@ -3386,6 +3974,7 @@ function terrainEditor(options) {
|
|
|
3386
3974
|
const sessionId = randomUUID();
|
|
3387
3975
|
let origin;
|
|
3388
3976
|
let url;
|
|
3977
|
+
let exporting = false;
|
|
3389
3978
|
const clients = /* @__PURE__ */ new Set();
|
|
3390
3979
|
const unsubscribe = document.subscribe((snapshot) => {
|
|
3391
3980
|
for (const response of clients)
|
|
@@ -3478,6 +4067,44 @@ function terrainEditor(options) {
|
|
|
3478
4067
|
json(response, 200, document.snapshot());
|
|
3479
4068
|
return;
|
|
3480
4069
|
}
|
|
4070
|
+
if (request.method === "POST" && path === `${PREFIX}api/export`) {
|
|
4071
|
+
const input = await body(request, MAX_BYTES);
|
|
4072
|
+
const snapshot = document.snapshot();
|
|
4073
|
+
if (snapshot.diagnostic)
|
|
4074
|
+
throw new EditorError(409, "Cannot export an invalid disk save");
|
|
4075
|
+
if (!input || input.baseRevision !== snapshot.revision)
|
|
4076
|
+
throw new EditorError(409, "Stale export revision");
|
|
4077
|
+
if (!input.preparedWorld || typeof input.preparedWorld !== "object" || Array.isArray(input.preparedWorld))
|
|
4078
|
+
throw new Error("Expected preparedWorld glTF JSON");
|
|
4079
|
+
if (exporting) throw new EditorError(409, "An export is already running");
|
|
4080
|
+
exporting = true;
|
|
4081
|
+
let cooked;
|
|
4082
|
+
try {
|
|
4083
|
+
const cooker = await loadCooker(vite.config.root);
|
|
4084
|
+
cooked = await cooker.cookTerrainWorld({ preparedWorld: input.preparedWorld });
|
|
4085
|
+
} finally {
|
|
4086
|
+
exporting = false;
|
|
4087
|
+
}
|
|
4088
|
+
if (document.snapshot().revision !== snapshot.revision)
|
|
4089
|
+
throw new EditorError(409, "World changed while exporting");
|
|
4090
|
+
response.writeHead(200, {
|
|
4091
|
+
"content-type": "model/gltf-binary",
|
|
4092
|
+
"cache-control": "no-store"
|
|
4093
|
+
});
|
|
4094
|
+
response.end(cooked.worldGlb);
|
|
4095
|
+
return;
|
|
4096
|
+
}
|
|
4097
|
+
if (request.method === "POST" && path === `${PREFIX}api/reload`) {
|
|
4098
|
+
await body(request, MAX_SMALL_BYTES, false);
|
|
4099
|
+
document.refresh();
|
|
4100
|
+
json(response, 200, document.snapshot());
|
|
4101
|
+
return;
|
|
4102
|
+
}
|
|
4103
|
+
if (request.method === "POST" && path === `${PREFIX}api/restore`) {
|
|
4104
|
+
await body(request, MAX_SMALL_BYTES, false);
|
|
4105
|
+
json(response, 200, document.restoreLastValid());
|
|
4106
|
+
return;
|
|
4107
|
+
}
|
|
3481
4108
|
if (request.method === "GET" && path === `${PREFIX}api/events`) {
|
|
3482
4109
|
response.writeHead(200, {
|
|
3483
4110
|
"content-type": "text/event-stream",
|
|
@@ -3492,7 +4119,7 @@ function terrainEditor(options) {
|
|
|
3492
4119
|
return;
|
|
3493
4120
|
}
|
|
3494
4121
|
if (request.method === "POST" && path === `${PREFIX}api/document`) {
|
|
3495
|
-
json(response, 200, document.commit(await body(request)));
|
|
4122
|
+
json(response, 200, document.commit(await body(request, MAX_BYTES)));
|
|
3496
4123
|
return;
|
|
3497
4124
|
}
|
|
3498
4125
|
if (request.method === "POST" && path === `${PREFIX}api/cameras`) {
|
|
@@ -3537,7 +4164,7 @@ function terrainEditor(options) {
|
|
|
3537
4164
|
return;
|
|
3538
4165
|
}
|
|
3539
4166
|
if (request.method === "POST" && path === `${PREFIX}api/assets`) {
|
|
3540
|
-
const transaction = await body(request);
|
|
4167
|
+
const transaction = await body(request, MAX_BYTES);
|
|
3541
4168
|
if (!transaction || typeof transaction !== "object" || Array.isArray(transaction) || Object.keys(transaction).some((key) => !["baseRevision", "operation"].includes(key)))
|
|
3542
4169
|
throw new Error("Expected { baseRevision, operation }");
|
|
3543
4170
|
const asset = transaction;
|
|
@@ -3677,9 +4304,10 @@ function terrainEditor(options) {
|
|
|
3677
4304
|
}
|
|
3678
4305
|
throw new EditorError(405, "Unsupported terrain editor route/method");
|
|
3679
4306
|
} catch (error) {
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
4307
|
+
const status = error instanceof EditorError ? error.status : 400;
|
|
4308
|
+
json(response, status, {
|
|
4309
|
+
code: status,
|
|
4310
|
+
error: error instanceof Error ? error.message : String(error)
|
|
3683
4311
|
});
|
|
3684
4312
|
}
|
|
3685
4313
|
})();
|