@threenative/terrain 0.1.6 → 0.1.8
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 +160 -5
- package/dist/editor/index.js +3783 -1406
- package/dist/editor/server.d.ts +2 -2
- package/dist/editor/server.js +555 -121
- package/dist/editor/worker.d.ts +33 -1
- package/dist/editor/worker.js +444 -110
- package/dist/export.d.ts +11 -9
- package/dist/export.js +22 -34
- package/dist/index.d.ts +727 -73
- package/dist/index.js +2434 -553
- package/dist/kit.d.ts +56 -0
- package/dist/kit.js +2524 -0
- package/dist/mcp/cook-child.d.ts +2 -0
- package/dist/mcp/cook-child.js +3705 -0
- package/dist/mcp/server.d.ts +7 -0
- package/dist/mcp/server.js +3279 -0
- package/dist/{server-ByM2hmYJ.d.ts → server-C8Ydr_IM.d.ts} +3 -2
- package/dist/starter.d.ts +143 -0
- package/dist/starter.js +1030 -0
- package/dist/{terrain-gPnkCU6S.d.ts → terrain-B3DkduPw.d.ts} +3 -2
- package/dist/three.d.ts +1 -1
- package/dist/{types-Ant8-Axc.d.ts → types-CPYAc9vo.d.ts} +13 -4
- package/dist/world-package-B7KGYIou.d.ts +94 -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 +114 -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 +77 -1
- package/starter/coastal/bake.mjs +4 -116
- package/starter/coastal/biome.data.json +135 -0
- package/starter/coastal/heightmap.r16 +0 -0
- package/starter/coastal/recipe.json +3 -66051
- package/starter/coastal/sea.ts +49 -16
- 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 +114 -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 +84 -4
- package/starter/forest/assets.json +32 -0
- package/starter/forest/bake.mjs +6 -116
- package/starter/forest/biome.data.json +150 -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 +30 -20
- package/starter/forest/world.ts +86 -322
- package/starter/tundra/README.md +77 -1
- package/starter/tundra/bake.mjs +4 -116
- package/starter/tundra/biome.data.json +131 -0
- package/starter/tundra/heightmap.r16 +0 -0
- package/starter/tundra/recipe.json +18 -66066
- 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,223 @@ 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
|
+
|
|
453
749
|
// src/core/operations.ts
|
|
454
750
|
var OPERATION_TYPES = Object.freeze([
|
|
455
751
|
"noise",
|
|
@@ -870,7 +1166,7 @@ function applyOperation(s, layer) {
|
|
|
870
1166
|
talus: new Float32Array(s.height.length)
|
|
871
1167
|
};
|
|
872
1168
|
s.erosion ??= structuredClone(observations);
|
|
873
|
-
const eroded = p.method === "hydraulic" ? hydraulic(
|
|
1169
|
+
const eroded = p.method === "fluvial" ? fluvial(s.height, n, s.size, p) : p.method === "hydraulic" ? hydraulic(
|
|
874
1170
|
s.height,
|
|
875
1171
|
n,
|
|
876
1172
|
s.size,
|
|
@@ -1111,7 +1407,7 @@ function finalizeState(s) {
|
|
|
1111
1407
|
accepted.push({
|
|
1112
1408
|
id: `${rule.id}:candidate:${seed}:${attempt}`,
|
|
1113
1409
|
layer: rule.id,
|
|
1114
|
-
asset: rule.asset
|
|
1410
|
+
asset: rule.asset,
|
|
1115
1411
|
position: [x, h + (rule.offsetY ?? 0), z],
|
|
1116
1412
|
rotation: yaw,
|
|
1117
1413
|
scale: itemScale,
|
|
@@ -1123,7 +1419,7 @@ function finalizeState(s) {
|
|
|
1123
1419
|
bucket.push(index);
|
|
1124
1420
|
buckets.set(key, bucket);
|
|
1125
1421
|
}
|
|
1126
|
-
s.instances.push(
|
|
1422
|
+
for (const instance of accepted) s.instances.push(instance);
|
|
1127
1423
|
if (accepted.length < count)
|
|
1128
1424
|
s.diagnostics.push(
|
|
1129
1425
|
`Scatter '${rule.id}': placed ${accepted.length}/${count}; masks or spacing exhausted the attempt budget.`
|
|
@@ -1198,6 +1494,10 @@ var PARAMS = Object.freeze({
|
|
|
1198
1494
|
"method",
|
|
1199
1495
|
"iterations",
|
|
1200
1496
|
"talus",
|
|
1497
|
+
"uplift",
|
|
1498
|
+
"erodibility",
|
|
1499
|
+
"timeStep",
|
|
1500
|
+
"relief",
|
|
1201
1501
|
"rate",
|
|
1202
1502
|
"droplets",
|
|
1203
1503
|
"maxSteps",
|
|
@@ -1247,14 +1547,23 @@ function fail(message) {
|
|
|
1247
1547
|
}
|
|
1248
1548
|
function finiteTree(value, path = "value", depth = 0) {
|
|
1249
1549
|
if (depth > 32) fail(`${path}: nesting is too deep`);
|
|
1250
|
-
if (typeof value === "number"
|
|
1550
|
+
if (typeof value === "number") {
|
|
1551
|
+
if (!Number.isFinite(value)) fail(`${path} must be finite`);
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1251
1554
|
if (value === void 0 || typeof value === "function" || typeof value === "bigint" || typeof value === "symbol")
|
|
1252
1555
|
fail(`${path} is not JSON-serializable`);
|
|
1253
1556
|
if (value && typeof value === "object") {
|
|
1254
1557
|
if (ArrayBuffer.isView(value)) fail(`${path}: use a plain array in recipes`);
|
|
1255
|
-
for (const
|
|
1256
|
-
if (
|
|
1558
|
+
for (const key of Object.keys(value)) {
|
|
1559
|
+
if (key === "__proto__" || key === "prototype" || key === "constructor")
|
|
1257
1560
|
fail(`${path}: unsafe object key`);
|
|
1561
|
+
const entry = value[key];
|
|
1562
|
+
if (typeof entry === "number") {
|
|
1563
|
+
if (depth + 1 > 32) fail(`${path}.${key}: nesting is too deep`);
|
|
1564
|
+
if (!Number.isFinite(entry)) fail(`${path}.${key} must be finite`);
|
|
1565
|
+
continue;
|
|
1566
|
+
}
|
|
1258
1567
|
finiteTree(entry, `${path}.${key}`, depth + 1);
|
|
1259
1568
|
}
|
|
1260
1569
|
}
|
|
@@ -1535,6 +1844,13 @@ function validateLayer(layer) {
|
|
|
1535
1844
|
if (params[key] !== void 0) num(params[key], key, key === "count" ? 0 : 1, max, true);
|
|
1536
1845
|
if (params.persistence !== void 0) num(params.persistence, "persistence", 0.01, 1);
|
|
1537
1846
|
if (params.lacunarity !== void 0) num(params.lacunarity, "lacunarity", 1, 4);
|
|
1847
|
+
for (const [key, min, max] of [
|
|
1848
|
+
["uplift", 1e-9, 1],
|
|
1849
|
+
["erodibility", 1e-9, 1],
|
|
1850
|
+
["timeStep", 1, 1e7],
|
|
1851
|
+
["relief", 1, 1e5]
|
|
1852
|
+
])
|
|
1853
|
+
if (params[key] !== void 0) num(params[key], key, min, max);
|
|
1538
1854
|
for (const key of ["minSlope", "maxSlope", "talus"])
|
|
1539
1855
|
if (params[key] !== void 0) num(params[key], key, 0, key === "talus" ? 89.9 : 90);
|
|
1540
1856
|
if (params.minHeight !== void 0 && params.maxHeight !== void 0 && params.minHeight > params.maxHeight)
|
|
@@ -1552,7 +1868,8 @@ function validateLayer(layer) {
|
|
|
1552
1868
|
if (rule.strength !== void 0) num(rule.strength, "rule.strength", 0, 1);
|
|
1553
1869
|
}
|
|
1554
1870
|
}
|
|
1555
|
-
if (params.method !== void 0)
|
|
1871
|
+
if (params.method !== void 0)
|
|
1872
|
+
enumeration(params.method, "method", ["thermal", "hydraulic", "fluvial"]);
|
|
1556
1873
|
if (params.mode !== void 0) enumeration(params.mode, "mode", ["fbm", "ridged", "billow"]);
|
|
1557
1874
|
if (params.shape !== void 0)
|
|
1558
1875
|
enumeration(
|
|
@@ -1580,6 +1897,10 @@ function validateLayer(layer) {
|
|
|
1580
1897
|
}
|
|
1581
1898
|
if (params.name !== void 0) identifier(params.name, "name");
|
|
1582
1899
|
if (params.asset !== void 0) identifier(params.asset, "asset");
|
|
1900
|
+
if (layer.type === "scatter" && typeof params.asset !== "string")
|
|
1901
|
+
fail(
|
|
1902
|
+
'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'
|
|
1903
|
+
);
|
|
1583
1904
|
if (["heightmap", "paste"].includes(layer.type) && !params.data)
|
|
1584
1905
|
fail(`${layer.type} requires heightmap data`);
|
|
1585
1906
|
const data = params.data;
|
|
@@ -1666,8 +1987,8 @@ var TerrainEvaluator = class {
|
|
|
1666
1987
|
validateDocument(doc);
|
|
1667
1988
|
validateConfig({ ...doc.config, resolution });
|
|
1668
1989
|
const keys = [];
|
|
1669
|
-
let key = JSON.stringify({ ...doc.config, resolution });
|
|
1670
|
-
for (const layer of doc.layers) {
|
|
1990
|
+
let key = this.limit > 0 ? JSON.stringify({ ...doc.config, resolution }) : "";
|
|
1991
|
+
for (const layer of this.limit > 0 ? doc.layers : []) {
|
|
1671
1992
|
key += `|${JSON.stringify(layer)}`;
|
|
1672
1993
|
keys.push(key);
|
|
1673
1994
|
}
|
|
@@ -2004,7 +2325,7 @@ var Terrain = class _Terrain {
|
|
|
2004
2325
|
biome(options = {}) {
|
|
2005
2326
|
return this._operation("biome", options);
|
|
2006
2327
|
}
|
|
2007
|
-
scatter(options
|
|
2328
|
+
scatter(options) {
|
|
2008
2329
|
return this._operation("scatter", options);
|
|
2009
2330
|
}
|
|
2010
2331
|
clear(options) {
|
|
@@ -2026,6 +2347,31 @@ var Terrain = class _Terrain {
|
|
|
2026
2347
|
return this._operation("paste", options);
|
|
2027
2348
|
}
|
|
2028
2349
|
};
|
|
2350
|
+
var MISSING_ASSETS = "terrain_generate needs @threenative/assets in the game project. Run: npm i -D @threenative/assets";
|
|
2351
|
+
function installedEntry(projectRoot, name) {
|
|
2352
|
+
let directory = resolve(projectRoot);
|
|
2353
|
+
while (true) {
|
|
2354
|
+
const manifestFile = join(directory, "node_modules", name, "package.json");
|
|
2355
|
+
if (existsSync(manifestFile)) {
|
|
2356
|
+
const manifest = JSON.parse(readFileSync(manifestFile, "utf8"));
|
|
2357
|
+
const root = manifest.exports?.["."];
|
|
2358
|
+
const relative2 = (typeof root === "string" ? root : root?.import) ?? manifest.main;
|
|
2359
|
+
return relative2 === void 0 ? void 0 : resolve(dirname(manifestFile), relative2);
|
|
2360
|
+
}
|
|
2361
|
+
const parent = dirname(directory);
|
|
2362
|
+
if (parent === directory) return void 0;
|
|
2363
|
+
directory = parent;
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
async function loadCooker(projectRoot) {
|
|
2367
|
+
const entry = installedEntry(projectRoot, "@threenative/assets");
|
|
2368
|
+
if (entry === void 0 || !existsSync(entry)) throw new Error(MISSING_ASSETS);
|
|
2369
|
+
const loaded = await import(pathToFileURL(entry).href);
|
|
2370
|
+
if (typeof loaded.cookTerrainWorld !== "function") {
|
|
2371
|
+
throw new Error(`${MISSING_ASSETS} (the installed version has no cookTerrainWorld).`);
|
|
2372
|
+
}
|
|
2373
|
+
return loaded;
|
|
2374
|
+
}
|
|
2029
2375
|
|
|
2030
2376
|
// src/editor/images.ts
|
|
2031
2377
|
var SURFACE_CHANNELS = {
|
|
@@ -3209,6 +3555,7 @@ function inspectSpatial(state, revision2, query) {
|
|
|
3209
3555
|
// src/editor/server.ts
|
|
3210
3556
|
var PREFIX = "/terrain-editor/";
|
|
3211
3557
|
var MAX_BYTES = 64 * 1024 * 1024;
|
|
3558
|
+
var MAX_SMALL_BYTES = 1024 * 1024;
|
|
3212
3559
|
var MAX_REFERENCES = 16;
|
|
3213
3560
|
function validate(value) {
|
|
3214
3561
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
@@ -3270,12 +3617,26 @@ var EditorError = class extends Error {
|
|
|
3270
3617
|
var TerrainEditorDocument = class {
|
|
3271
3618
|
path;
|
|
3272
3619
|
#snapshot;
|
|
3620
|
+
#lastValidDocument = null;
|
|
3273
3621
|
#listeners = /* @__PURE__ */ new Set();
|
|
3274
3622
|
constructor(path) {
|
|
3275
3623
|
if (!isAbsolute(path)) throw new Error("Document path must be absolute");
|
|
3276
3624
|
this.path = resolve(path);
|
|
3277
|
-
|
|
3278
|
-
|
|
3625
|
+
try {
|
|
3626
|
+
const document = this.#read();
|
|
3627
|
+
this.#lastValidDocument = document;
|
|
3628
|
+
this.#snapshot = { document, revision: revision(document), diagnostic: null };
|
|
3629
|
+
} catch (error) {
|
|
3630
|
+
const fallback = {
|
|
3631
|
+
version: 1,
|
|
3632
|
+
recipe: new Terrain().toJSON()
|
|
3633
|
+
};
|
|
3634
|
+
this.#snapshot = {
|
|
3635
|
+
document: fallback,
|
|
3636
|
+
revision: revision(fallback),
|
|
3637
|
+
diagnostic: error instanceof Error ? error.message : String(error)
|
|
3638
|
+
};
|
|
3639
|
+
}
|
|
3279
3640
|
}
|
|
3280
3641
|
#read() {
|
|
3281
3642
|
const info = lstatSync(this.path);
|
|
@@ -3297,6 +3658,7 @@ var TerrainEditorDocument = class {
|
|
|
3297
3658
|
try {
|
|
3298
3659
|
const document = this.#read();
|
|
3299
3660
|
const nextRevision = revision(document);
|
|
3661
|
+
this.#lastValidDocument = document;
|
|
3300
3662
|
if (nextRevision === this.#snapshot.revision && this.#snapshot.diagnostic === null) return;
|
|
3301
3663
|
this.#snapshot = { document, revision: nextRevision, diagnostic: null };
|
|
3302
3664
|
} catch (error) {
|
|
@@ -3304,6 +3666,32 @@ var TerrainEditorDocument = class {
|
|
|
3304
3666
|
}
|
|
3305
3667
|
this.#emit();
|
|
3306
3668
|
}
|
|
3669
|
+
restoreLastValid() {
|
|
3670
|
+
if (!this.#lastValidDocument)
|
|
3671
|
+
throw new EditorError(409, "No valid save to restore; the document has never been valid");
|
|
3672
|
+
const target = this.#lastValidDocument;
|
|
3673
|
+
const temporary = `${this.path}.${randomUUID()}.tmp`;
|
|
3674
|
+
try {
|
|
3675
|
+
writeFileSync(temporary, `${JSON.stringify(target, null, 2)}
|
|
3676
|
+
`, { flag: "wx", mode: 384 });
|
|
3677
|
+
renameSync(temporary, this.path);
|
|
3678
|
+
} catch (error) {
|
|
3679
|
+
try {
|
|
3680
|
+
unlinkSync(temporary);
|
|
3681
|
+
} catch (cleanupError) {
|
|
3682
|
+
if (cleanupError.code !== "ENOENT")
|
|
3683
|
+
throw new AggregateError(
|
|
3684
|
+
[error, cleanupError],
|
|
3685
|
+
"Document save and temporary-file cleanup failed"
|
|
3686
|
+
);
|
|
3687
|
+
}
|
|
3688
|
+
throw error;
|
|
3689
|
+
}
|
|
3690
|
+
this.#lastValidDocument = target;
|
|
3691
|
+
this.#snapshot = { document: target, revision: revision(target), diagnostic: null };
|
|
3692
|
+
this.#emit();
|
|
3693
|
+
return this.snapshot();
|
|
3694
|
+
}
|
|
3307
3695
|
commit(input) {
|
|
3308
3696
|
this.refresh();
|
|
3309
3697
|
if (this.#snapshot.diagnostic)
|
|
@@ -3345,24 +3733,30 @@ var TerrainEditorDocument = class {
|
|
|
3345
3733
|
throw error;
|
|
3346
3734
|
}
|
|
3347
3735
|
this.#snapshot = { document: next, revision: nextRevision, diagnostic: null };
|
|
3736
|
+
this.#lastValidDocument = next;
|
|
3348
3737
|
this.#emit();
|
|
3349
3738
|
return this.snapshot();
|
|
3350
3739
|
}
|
|
3351
3740
|
};
|
|
3352
|
-
async function body(request) {
|
|
3353
|
-
if (request.headers["content-
|
|
3741
|
+
async function body(request, maxBytes = MAX_SMALL_BYTES, parse = true) {
|
|
3742
|
+
if (Number(request.headers["content-length"]) > maxBytes) {
|
|
3743
|
+
request.resume();
|
|
3744
|
+
throw new EditorError(413, `Request exceeds ${maxBytes / (1024 * 1024)} MiB`);
|
|
3745
|
+
}
|
|
3746
|
+
if (parse && request.headers["content-type"]?.split(";")[0] !== "application/json")
|
|
3354
3747
|
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
3748
|
const chunks = [];
|
|
3358
3749
|
let bytes = 0;
|
|
3359
|
-
for await (const chunk of request) {
|
|
3750
|
+
for await (const chunk of request.iterator({ destroyOnReturn: false })) {
|
|
3360
3751
|
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
3361
3752
|
bytes += buffer.length;
|
|
3362
|
-
if (bytes >
|
|
3363
|
-
|
|
3753
|
+
if (bytes > maxBytes) {
|
|
3754
|
+
request.resume();
|
|
3755
|
+
throw new EditorError(413, `Request exceeds ${maxBytes / (1024 * 1024)} MiB`);
|
|
3756
|
+
}
|
|
3757
|
+
if (parse) chunks.push(buffer);
|
|
3364
3758
|
}
|
|
3365
|
-
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
3759
|
+
return parse ? JSON.parse(Buffer.concat(chunks).toString("utf8")) : void 0;
|
|
3366
3760
|
}
|
|
3367
3761
|
function json(response, status, value) {
|
|
3368
3762
|
response.writeHead(status, {
|
|
@@ -3386,6 +3780,7 @@ function terrainEditor(options) {
|
|
|
3386
3780
|
const sessionId = randomUUID();
|
|
3387
3781
|
let origin;
|
|
3388
3782
|
let url;
|
|
3783
|
+
let exporting = false;
|
|
3389
3784
|
const clients = /* @__PURE__ */ new Set();
|
|
3390
3785
|
const unsubscribe = document.subscribe((snapshot) => {
|
|
3391
3786
|
for (const response of clients)
|
|
@@ -3478,6 +3873,44 @@ function terrainEditor(options) {
|
|
|
3478
3873
|
json(response, 200, document.snapshot());
|
|
3479
3874
|
return;
|
|
3480
3875
|
}
|
|
3876
|
+
if (request.method === "POST" && path === `${PREFIX}api/export`) {
|
|
3877
|
+
const input = await body(request, MAX_BYTES);
|
|
3878
|
+
const snapshot = document.snapshot();
|
|
3879
|
+
if (snapshot.diagnostic)
|
|
3880
|
+
throw new EditorError(409, "Cannot export an invalid disk save");
|
|
3881
|
+
if (!input || input.baseRevision !== snapshot.revision)
|
|
3882
|
+
throw new EditorError(409, "Stale export revision");
|
|
3883
|
+
if (!input.preparedWorld || typeof input.preparedWorld !== "object" || Array.isArray(input.preparedWorld))
|
|
3884
|
+
throw new Error("Expected preparedWorld glTF JSON");
|
|
3885
|
+
if (exporting) throw new EditorError(409, "An export is already running");
|
|
3886
|
+
exporting = true;
|
|
3887
|
+
let cooked;
|
|
3888
|
+
try {
|
|
3889
|
+
const cooker = await loadCooker(vite.config.root);
|
|
3890
|
+
cooked = await cooker.cookTerrainWorld({ preparedWorld: input.preparedWorld });
|
|
3891
|
+
} finally {
|
|
3892
|
+
exporting = false;
|
|
3893
|
+
}
|
|
3894
|
+
if (document.snapshot().revision !== snapshot.revision)
|
|
3895
|
+
throw new EditorError(409, "World changed while exporting");
|
|
3896
|
+
response.writeHead(200, {
|
|
3897
|
+
"content-type": "model/gltf-binary",
|
|
3898
|
+
"cache-control": "no-store"
|
|
3899
|
+
});
|
|
3900
|
+
response.end(cooked.worldGlb);
|
|
3901
|
+
return;
|
|
3902
|
+
}
|
|
3903
|
+
if (request.method === "POST" && path === `${PREFIX}api/reload`) {
|
|
3904
|
+
await body(request, MAX_SMALL_BYTES, false);
|
|
3905
|
+
document.refresh();
|
|
3906
|
+
json(response, 200, document.snapshot());
|
|
3907
|
+
return;
|
|
3908
|
+
}
|
|
3909
|
+
if (request.method === "POST" && path === `${PREFIX}api/restore`) {
|
|
3910
|
+
await body(request, MAX_SMALL_BYTES, false);
|
|
3911
|
+
json(response, 200, document.restoreLastValid());
|
|
3912
|
+
return;
|
|
3913
|
+
}
|
|
3481
3914
|
if (request.method === "GET" && path === `${PREFIX}api/events`) {
|
|
3482
3915
|
response.writeHead(200, {
|
|
3483
3916
|
"content-type": "text/event-stream",
|
|
@@ -3492,7 +3925,7 @@ function terrainEditor(options) {
|
|
|
3492
3925
|
return;
|
|
3493
3926
|
}
|
|
3494
3927
|
if (request.method === "POST" && path === `${PREFIX}api/document`) {
|
|
3495
|
-
json(response, 200, document.commit(await body(request)));
|
|
3928
|
+
json(response, 200, document.commit(await body(request, MAX_BYTES)));
|
|
3496
3929
|
return;
|
|
3497
3930
|
}
|
|
3498
3931
|
if (request.method === "POST" && path === `${PREFIX}api/cameras`) {
|
|
@@ -3537,7 +3970,7 @@ function terrainEditor(options) {
|
|
|
3537
3970
|
return;
|
|
3538
3971
|
}
|
|
3539
3972
|
if (request.method === "POST" && path === `${PREFIX}api/assets`) {
|
|
3540
|
-
const transaction = await body(request);
|
|
3973
|
+
const transaction = await body(request, MAX_BYTES);
|
|
3541
3974
|
if (!transaction || typeof transaction !== "object" || Array.isArray(transaction) || Object.keys(transaction).some((key) => !["baseRevision", "operation"].includes(key)))
|
|
3542
3975
|
throw new Error("Expected { baseRevision, operation }");
|
|
3543
3976
|
const asset = transaction;
|
|
@@ -3677,9 +4110,10 @@ function terrainEditor(options) {
|
|
|
3677
4110
|
}
|
|
3678
4111
|
throw new EditorError(405, "Unsupported terrain editor route/method");
|
|
3679
4112
|
} catch (error) {
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
4113
|
+
const status = error instanceof EditorError ? error.status : 400;
|
|
4114
|
+
json(response, status, {
|
|
4115
|
+
code: status,
|
|
4116
|
+
error: error instanceof Error ? error.message : String(error)
|
|
3683
4117
|
});
|
|
3684
4118
|
}
|
|
3685
4119
|
})();
|