@threenative/terrain 0.1.8 → 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.
Files changed (37) hide show
  1. package/dist/editor/index.d.ts +63 -5
  2. package/dist/editor/index.js +323 -6
  3. package/dist/editor/server.d.ts +2 -2
  4. package/dist/editor/server.js +198 -4
  5. package/dist/editor/worker.d.ts +1 -1
  6. package/dist/editor/worker.js +199 -5
  7. package/dist/export.d.ts +1 -1
  8. package/dist/index.d.ts +146 -7
  9. package/dist/index.js +638 -39
  10. package/dist/kit.d.ts +3 -2
  11. package/dist/kit.js +198 -4
  12. package/dist/mcp/cook-child.js +1766 -125
  13. package/dist/mcp/server.js +728 -37
  14. package/dist/{server-C8Ydr_IM.d.ts → server-B5RdvEAC.d.ts} +1 -1
  15. package/dist/starter.d.ts +1 -1
  16. package/dist/starter.js +2 -1
  17. package/dist/{terrain-B3DkduPw.d.ts → terrain-CAEaW0mQ.d.ts} +1 -1
  18. package/dist/three.d.ts +1 -1
  19. package/dist/{types-CPYAc9vo.d.ts → types-BpH00qsx.d.ts} +10 -3
  20. package/dist/{world-package-B7KGYIou.d.ts → world-package-B8QgHjAQ.d.ts} +9 -50
  21. package/package.json +5 -5
  22. package/starter/alpine/biome.data.json +22 -2
  23. package/starter/alpine/world.ts +1 -1
  24. package/starter/coastal/README.md +5 -2
  25. package/starter/coastal/assets.json +2 -1
  26. package/starter/coastal/biome.data.json +23 -2
  27. package/starter/coastal/sea.ts +2 -50
  28. package/starter/desert/biome.data.json +12 -2
  29. package/starter/desert/world.ts +1 -1
  30. package/starter/forest/README.md +1 -1
  31. package/starter/forest/assets.json +2 -1
  32. package/starter/forest/biome.data.json +37 -14
  33. package/starter/forest/water.ts +9 -28
  34. package/starter/forest/world.ts +6 -1
  35. package/starter/tundra/biome.data.json +12 -2
  36. package/starter/tundra/water.ts +9 -28
  37. package/starter/tundra/world.ts +1 -1
package/dist/index.js CHANGED
@@ -657,6 +657,146 @@ function fluvial(height, n, size, {
657
657
  return Float32Array.from(h);
658
658
  }
659
659
 
660
+ // src/core/gully.ts
661
+ var STEP_SECONDS = 0.1;
662
+ var FLUX_KEEP = 0.99;
663
+ var DISSOLVE = 0.3;
664
+ var DEPOSIT = 0.02;
665
+ var EVAPORATE = 0.05;
666
+ var DEPTH_REF = 0.01;
667
+ var DEPTH_MAX = 0.3;
668
+ var MIN_TILT = 0.05;
669
+ var GRAVITY = 9.81;
670
+ var MAX_ITERATIONS = 200;
671
+ function gully(height, n, size, { iterations = 200, capacity = 0.05, rain = 5e-4 } = {}) {
672
+ if (!Number.isInteger(n) || n < 2)
673
+ throw new RangeError("Gully erosion requires grid resolution n >= 2");
674
+ if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS)
675
+ throw new RangeError(`Gully erosion iterations must be between 0 and ${MAX_ITERATIONS}`);
676
+ if (!Number.isFinite(capacity) || capacity < 0 || capacity > 1e6)
677
+ throw new RangeError("Gully erosion capacity must be between 0 and 1000000");
678
+ if (!Number.isFinite(rain) || rain < 0 || rain > 1)
679
+ throw new RangeError("Gully erosion rain must be between 0 and 1 metres per step");
680
+ const total = n * n;
681
+ let h = Float64Array.from(height);
682
+ let hTmp = new Float64Array(total);
683
+ const water2 = new Float64Array(total);
684
+ const waterBefore = new Float64Array(total);
685
+ const speed = new Float64Array(total);
686
+ const sediment = new Float64Array(total);
687
+ const sedTmp = new Float64Array(total);
688
+ const fluxL = new Float64Array(total);
689
+ const fluxR = new Float64Array(total);
690
+ const fluxT = new Float64Array(total);
691
+ const fluxB = new Float64Array(total);
692
+ const cell = size / (n - 1);
693
+ const cellArea = cell * cell;
694
+ const gain = STEP_SECONDS * GRAVITY * cell;
695
+ const evaporateKeep = Math.max(1 - EVAPORATE * STEP_SECONDS, 0);
696
+ const share2 = (flow, depth) => depth > 0 ? flow * STEP_SECONDS / (depth * cellArea) : 0;
697
+ for (let step = 0; step < iterations; step += 1) {
698
+ for (let z = 0; z < n; z += 1) {
699
+ for (let x = 0; x < n; x += 1) {
700
+ const i = z * n + x;
701
+ const depth = water2[i] + rain;
702
+ const surface = h[i] + depth;
703
+ const ground = h[i];
704
+ const left = x > 0 ? h[i - 1] + water2[i - 1] + rain : ground;
705
+ const right = x < n - 1 ? h[i + 1] + water2[i + 1] + rain : ground;
706
+ const top = z > 0 ? h[i - n] + water2[i - n] + rain : ground;
707
+ const bottom = z < n - 1 ? h[i + n] + water2[i + n] + rain : ground;
708
+ let l = fluxL[i] * FLUX_KEEP + gain * (surface - left);
709
+ let r = fluxR[i] * FLUX_KEEP + gain * (surface - right);
710
+ let t = fluxT[i] * FLUX_KEEP + gain * (surface - top);
711
+ let b = fluxB[i] * FLUX_KEEP + gain * (surface - bottom);
712
+ if (l < 0) l = 0;
713
+ if (r < 0) r = 0;
714
+ if (t < 0) t = 0;
715
+ if (b < 0) b = 0;
716
+ const sum = l + r + t + b;
717
+ if (sum > 0) {
718
+ const keep = Math.min(1, depth * cellArea / (sum * STEP_SECONDS));
719
+ l *= keep;
720
+ r *= keep;
721
+ t *= keep;
722
+ b *= keep;
723
+ }
724
+ fluxL[i] = l;
725
+ fluxR[i] = r;
726
+ fluxT[i] = t;
727
+ fluxB[i] = b;
728
+ }
729
+ }
730
+ for (let z = 0; z < n; z += 1) {
731
+ for (let x = 0; x < n; x += 1) {
732
+ const i = z * n + x;
733
+ const inL = x > 0 ? fluxR[i - 1] : 0;
734
+ const inR = x < n - 1 ? fluxL[i + 1] : 0;
735
+ const inT = z > 0 ? fluxB[i - n] : 0;
736
+ const inB = z < n - 1 ? fluxT[i + n] : 0;
737
+ const before = water2[i] + rain;
738
+ const after = Math.max(
739
+ 0,
740
+ before + STEP_SECONDS * (inL + inR + inT + inB - fluxL[i] - fluxR[i] - fluxT[i] - fluxB[i]) / cellArea
741
+ );
742
+ const depth = 0.5 * (before + after);
743
+ const acrossX = inL - fluxL[i] + fluxR[i] - inR;
744
+ const acrossZ = inT - fluxT[i] + fluxB[i] - inB;
745
+ speed[i] = depth > 1e-4 ? Math.sqrt(acrossX * acrossX + acrossZ * acrossZ) * 0.5 / (cell * depth) : 0;
746
+ water2[i] = after;
747
+ waterBefore[i] = before;
748
+ }
749
+ }
750
+ for (let z = 0; z < n; z += 1) {
751
+ for (let x = 0; x < n; x += 1) {
752
+ const i = z * n + x;
753
+ const gx = (h[z * n + Math.min(x + 1, n - 1)] - h[z * n + Math.max(x - 1, 0)]) / (2 * cell);
754
+ const gy = (h[Math.min(z + 1, n - 1) * n + x] - h[Math.max(z - 1, 0) * n + x]) / (2 * cell);
755
+ const squared = gx * gx + gy * gy;
756
+ const sine = Math.max(Math.sqrt(squared / (1 + squared)), MIN_TILT);
757
+ const depth = water2[i];
758
+ const transport = capacity * sine * speed[i] * Math.min(depth / DEPTH_REF, 1) * Math.max(1 - depth / DEPTH_MAX, 0);
759
+ let surface = h[i];
760
+ let load = sediment[i];
761
+ if (transport > load) {
762
+ const cut = DISSOLVE * (transport - load);
763
+ surface -= cut;
764
+ load += cut;
765
+ } else {
766
+ const drop = DEPOSIT * (load - transport);
767
+ surface += drop;
768
+ load -= drop;
769
+ }
770
+ hTmp[i] = surface;
771
+ sedTmp[i] = load;
772
+ }
773
+ }
774
+ const passed = h;
775
+ h = hTmp;
776
+ hTmp = passed;
777
+ for (let z = 0; z < n; z += 1) {
778
+ for (let x = 0; x < n; x += 1) {
779
+ const i = z * n + x;
780
+ let load = sedTmp[i] * (1 - share2(
781
+ fluxL[i] + fluxR[i] + fluxT[i] + fluxB[i],
782
+ waterBefore[i]
783
+ ));
784
+ if (x > 0)
785
+ load += sedTmp[i - 1] * share2(fluxR[i - 1], waterBefore[i - 1]);
786
+ if (x < n - 1)
787
+ load += sedTmp[i + 1] * share2(fluxL[i + 1], waterBefore[i + 1]);
788
+ if (z > 0)
789
+ load += sedTmp[i - n] * share2(fluxB[i - n], waterBefore[i - n]);
790
+ if (z < n - 1)
791
+ load += sedTmp[i + n] * share2(fluxT[i + n], waterBefore[i + n]);
792
+ sediment[i] = load;
793
+ water2[i] = water2[i] * evaporateKeep;
794
+ }
795
+ }
796
+ }
797
+ return Float32Array.from(h, (surface, i) => surface + sediment[i]);
798
+ }
799
+
660
800
  // src/core/masks.ts
661
801
  var MATERIAL_IDS = Object.freeze([
662
802
  "grass",
@@ -780,6 +920,57 @@ function paintWeights(s, i, channel, weight) {
780
920
  s.splat[base + c] = s.splat[base + c] * (1 - w) + (c === channel ? w : 0);
781
921
  }
782
922
 
923
+ // src/core/scree.ts
924
+ var NEIGHBOURS = [
925
+ [-1, -1],
926
+ [0, -1],
927
+ [1, -1],
928
+ [-1, 0],
929
+ [1, 0],
930
+ [-1, 1],
931
+ [0, 1],
932
+ [1, 1]
933
+ ];
934
+ var DISTANCE = Float64Array.from(NEIGHBOURS, ([dx, dz]) => Math.hypot(dx, dz));
935
+ var MAX_ITERATIONS2 = 200;
936
+ function scree(height, n, size, { iterations = 200, talus = 35, rate = 0.05 } = {}) {
937
+ if (!Number.isInteger(n) || n < 2)
938
+ throw new RangeError("Scree slide requires grid resolution n >= 2");
939
+ if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS2)
940
+ throw new RangeError(`Scree slide iterations must be between 0 and ${MAX_ITERATIONS2}`);
941
+ if (!Number.isFinite(talus) || talus < 0 || talus > 89.9)
942
+ throw new RangeError("Scree slide talus must be between 0 and 89.9 degrees");
943
+ if (!Number.isFinite(rate) || rate < 0 || rate > 1)
944
+ throw new RangeError("Scree slide rate must be between 0 and 1");
945
+ const cell = size / (n - 1);
946
+ const limit = Float64Array.from(DISTANCE, (d) => Math.tan(talus * Math.PI / 180) * cell * d);
947
+ let source = Float64Array.from(height);
948
+ let target = new Float64Array(source.length);
949
+ for (let k = 0; k < iterations; k += 1) {
950
+ for (let z = 0; z < n; z += 1) {
951
+ for (let x = 0; x < n; x += 1) {
952
+ const i = z * n + x;
953
+ const here = source[i];
954
+ let received = 0;
955
+ for (let d = 0; d < 8; d += 1) {
956
+ const nx = x + NEIGHBOURS[d][0];
957
+ const nz = z + NEIGHBOURS[d][1];
958
+ if (nx < 0 || nz < 0 || nx >= n || nz >= n) continue;
959
+ const other = source[nz * n + nx];
960
+ const gap = limit[d];
961
+ if (other - here > gap) received += other - here - gap;
962
+ else if (here - other > gap) received -= here - other - gap;
963
+ }
964
+ target[i] = here + rate * received;
965
+ }
966
+ }
967
+ const swap = source;
968
+ source = target;
969
+ target = swap;
970
+ }
971
+ return Float32Array.from(source);
972
+ }
973
+
783
974
  // src/core/operations.ts
784
975
  var OPERATION_TYPES = Object.freeze([
785
976
  "noise",
@@ -1215,7 +1406,7 @@ function applyOperation(s, layer2) {
1215
1406
  evaporation: p.evaporation
1216
1407
  },
1217
1408
  observations
1218
- ) : thermal(s.height, n, s.size, p, observations);
1409
+ ) : p.method === "gully" ? gully(s.height, n, s.size, p) : p.method === "scree" ? scree(s.height, n, s.size, p) : thermal(s.height, n, s.size, p, observations);
1219
1410
  let influence = mask;
1220
1411
  if (p.at || p.points) influence = brushWeights(s, p, mask, seed);
1221
1412
  for (let i = 0; i < s.height.length; i += 1) {
@@ -1369,6 +1560,7 @@ function finalizeState(s) {
1369
1560
  }
1370
1561
  const wet = (i, x, z, h) => {
1371
1562
  if (s.waters.some((water2) => water2.mask[i])) return true;
1563
+ if (s.waters.some((water2) => water2.kind === "ocean" && h < water2.level + 0.35)) return true;
1372
1564
  for (const river of s.rivers) {
1373
1565
  const half = river.width / 2;
1374
1566
  for (let k = 1; k < river.points.length; k += 1) {
@@ -1539,7 +1731,8 @@ var PARAMS = Object.freeze({
1539
1731
  "capacity",
1540
1732
  "erosion",
1541
1733
  "deposition",
1542
- "evaporation"
1734
+ "evaporation",
1735
+ "rain"
1543
1736
  ],
1544
1737
  terrace: ["step", "softness", "strength", "offset"],
1545
1738
  materials: ["base", "rules", "reset"],
@@ -1847,7 +2040,8 @@ function validateLayer(layer2) {
1847
2040
  "rate",
1848
2041
  "value",
1849
2042
  "waterWidth",
1850
- "maxGrade"
2043
+ "maxGrade",
2044
+ "rain"
1851
2045
  ])
1852
2046
  if (params[key] !== void 0) num(params[key], key, 0, 1);
1853
2047
  if (params.spacing !== void 0) num(params.spacing, "spacing", 0.01, 2);
@@ -1903,7 +2097,7 @@ function validateLayer(layer2) {
1903
2097
  }
1904
2098
  }
1905
2099
  if (params.method !== void 0)
1906
- enumeration(params.method, "method", ["thermal", "hydraulic", "fluvial"]);
2100
+ enumeration(params.method, "method", ["thermal", "hydraulic", "fluvial", "gully", "scree"]);
1907
2101
  if (params.mode !== void 0) enumeration(params.mode, "mode", ["fbm", "ridged", "billow"]);
1908
2102
  if (params.shape !== void 0)
1909
2103
  enumeration(
@@ -2583,7 +2777,24 @@ var SPEC_FIELDS = [
2583
2777
  "species",
2584
2778
  "aliases"
2585
2779
  ];
2586
- var COVER_FIELDS = ["ground", "seed", "attempts", "stoneRadius", "clearings"];
2780
+ var COVER_FIELDS = [
2781
+ "ground",
2782
+ "seed",
2783
+ "attempts",
2784
+ "stoneRadius",
2785
+ "clearings",
2786
+ "understorey"
2787
+ ];
2788
+ var UNDERSTOREY_FIELDS = [
2789
+ "attempts",
2790
+ "litter",
2791
+ "fern",
2792
+ "bush",
2793
+ "flower",
2794
+ "deadfall",
2795
+ "shade",
2796
+ "shadeRadius"
2797
+ ];
2587
2798
  var COVER_GROUNDS = ["wood", "crag", "mesa", "mats"];
2588
2799
  var STREAMS = ["ordered", "seeded"];
2589
2800
  var DRIFT_FIELDS = ["field", "frequency", "offset", "amplitude", "band"];
@@ -2770,6 +2981,19 @@ function clearing(value, biome, field) {
2770
2981
  if (radius <= 0) fail2(biome, `${field}[2]`, "must be greater than 0");
2771
2982
  return [x, z, radius];
2772
2983
  }
2984
+ function understorey(value, biome) {
2985
+ const row = closed(value, biome, "cover.understorey", UNDERSTOREY_FIELDS);
2986
+ return {
2987
+ attempts: whole(row.attempts, biome, "cover.understorey.attempts"),
2988
+ litter: count(row.litter, biome, "cover.understorey.litter"),
2989
+ fern: count(row.fern, biome, "cover.understorey.fern"),
2990
+ bush: count(row.bush, biome, "cover.understorey.bush"),
2991
+ flower: count(row.flower, biome, "cover.understorey.flower"),
2992
+ deadfall: count(row.deadfall, biome, "cover.understorey.deadfall"),
2993
+ shade: share(row.shade, biome, "cover.understorey.shade"),
2994
+ shadeRadius: count(row.shadeRadius, biome, "cover.understorey.shadeRadius")
2995
+ };
2996
+ }
2773
2997
  function cover(value, biome) {
2774
2998
  const row = closed(value, biome, "cover", COVER_FIELDS);
2775
2999
  const clearings = row.clearings;
@@ -2779,7 +3003,8 @@ function cover(value, biome) {
2779
3003
  seed: whole(row.seed, biome, "cover.seed"),
2780
3004
  attempts: whole(row.attempts, biome, "cover.attempts", 1),
2781
3005
  stoneRadius: positive(row.stoneRadius, biome, "cover.stoneRadius"),
2782
- clearings: clearings.map((entry, index) => clearing(entry, biome, `cover.clearings[${index}]`))
3006
+ clearings: clearings.map((entry, index) => clearing(entry, biome, `cover.clearings[${index}]`)),
3007
+ understorey: understorey(row.understorey, biome)
2783
3008
  };
2784
3009
  }
2785
3010
  function foliage(value, biome, field) {
@@ -3036,6 +3261,7 @@ function orderedScatterRules(spec) {
3036
3261
  shoreGround: grass2.shoreGround,
3037
3262
  cobbledBed: grass2.cobbledBed,
3038
3263
  stoneRadius: cover2.stoneRadius,
3264
+ understorey: cover2.understorey,
3039
3265
  treeAsset: tree.species,
3040
3266
  treeCount: tree.density,
3041
3267
  treeSpacing: tree.spacing,
@@ -3101,6 +3327,14 @@ function coverDriftWeight(drift, x, z) {
3101
3327
  const weight = fieldWeight(drift.field, x * frequencyX, z * frequencyZ);
3102
3328
  return drift.offset + drift.amplitude * clamp012((weight - threshold) / span);
3103
3329
  }
3330
+ var GRADED_UNDERSTOREY = {
3331
+ attempts: 14e3,
3332
+ litter: 1,
3333
+ fern: 1,
3334
+ bush: 1,
3335
+ flower: 1,
3336
+ deadfall: 0
3337
+ };
3104
3338
  var GRASS_FULL = 22;
3105
3339
  var GRASS_THIN = 62;
3106
3340
  function scatterProps(data, focus, clearings = []) {
@@ -3115,6 +3349,7 @@ function scatterProps(data, focus, clearings = []) {
3115
3349
  [
3116
3350
  "boulder",
3117
3351
  "bush",
3352
+ "deadfall",
3118
3353
  "fern",
3119
3354
  "grass",
3120
3355
  "litter",
@@ -3173,6 +3408,8 @@ function scatterProps(data, focus, clearings = []) {
3173
3408
  );
3174
3409
  return depositAtIndex(data, row * data.resolution + column);
3175
3410
  };
3411
+ let poseDraw = random2;
3412
+ let placing = true;
3176
3413
  const put = (asset, x, z, scale, suffix = "") => {
3177
3414
  if (!inside(x, z) || asset !== "riverrock" && asset !== "pebble" && wet(x, z)) return;
3178
3415
  if (data.erosion && asset === "scree" && depositsAt(x, z) < 0.08) {
@@ -3205,12 +3442,14 @@ function scatterProps(data, focus, clearings = []) {
3205
3442
  }
3206
3443
  const index = counts[asset] ?? 0;
3207
3444
  const crag = asset === "mountain" || asset === "volcanic" || asset === "reveal";
3208
- const bedded = temperate && ["boulder", "riverrock", "scree", "pebble"].includes(asset);
3445
+ const bedded = temperate && ["boulder", "riverrock", "scree", "pebble", "deadfall"].includes(asset);
3209
3446
  const normal = crag || bedded ? data.field.normalAt(x, z) : void 0;
3210
3447
  if (crag && normal) {
3211
3448
  normal.y += 0.65;
3212
3449
  normal.normalize();
3213
3450
  }
3451
+ const rotation = crag && normal ? Math.atan2(normal.x, normal.z) + (poseDraw() - 0.5) * 0.6 : poseDraw() * Math.PI * 2;
3452
+ if (!placing) return;
3214
3453
  placements.push({
3215
3454
  asset,
3216
3455
  id: `temperate-${asset}:${index}${suffix}`,
@@ -3218,7 +3457,7 @@ function scatterProps(data, focus, clearings = []) {
3218
3457
  alignToNormal: crag || bedded || temperate && (asset === "spruce" || asset === "sapling"),
3219
3458
  normal: (crag || bedded) && normal ? normal.toArray() : temperate && (asset === "spruce" || asset === "sapling") ? [Math.sin(x * 1.17 + z) * 0.045, 1, Math.cos(z * 1.31 - x) * 0.045] : [0, 1, 0],
3220
3459
  position: [x, clampedHeight(data, x, z), z],
3221
- rotation: crag && normal ? Math.atan2(normal.x, normal.z) + (random2() - 0.5) * 0.6 : random2() * Math.PI * 2,
3460
+ rotation,
3222
3461
  scale
3223
3462
  });
3224
3463
  counts[asset] = index + 1;
@@ -3235,6 +3474,16 @@ function scatterProps(data, focus, clearings = []) {
3235
3474
  if (Math.hypot(tx - x, tz - z) < Math.max(reach, tr)) return true;
3236
3475
  return false;
3237
3476
  };
3477
+ const shaded = (x, z, radius) => {
3478
+ const cx = Math.floor(x / CELL);
3479
+ const cz = Math.floor(z / CELL);
3480
+ const ring = Math.ceil(radius * 2 / CELL);
3481
+ for (let dz = -ring; dz <= ring; dz++)
3482
+ for (let dx = -ring; dx <= ring; dx++)
3483
+ for (const [tx, tz, , crown] of cells.get(`${cx + dx},${cz + dz}`) ?? [])
3484
+ if (Math.hypot(tx - x, tz - z) < radius * crown) return true;
3485
+ return false;
3486
+ };
3238
3487
  const treeAsset = rules.treeAsset;
3239
3488
  const hollow = (x, z) => {
3240
3489
  const here = clampedHeight(data, x, z);
@@ -3253,15 +3502,12 @@ function scatterProps(data, focus, clearings = []) {
3253
3502
  if (crowded(x, z, reach)) continue;
3254
3503
  const key = `${Math.floor(x / CELL)},${Math.floor(z / CELL)}`;
3255
3504
  const bucket = cells.get(key) ?? [];
3256
- bucket.push([x, z, reach]);
3257
3505
  cells.set(key, bucket);
3258
3506
  const age = sample01(x * 0.021, 7, z * 0.021);
3259
- put(
3260
- treeAsset,
3261
- x,
3262
- z,
3263
- temperate ? 0.42 + 1.18 * age ** 1.6 + random2() * 0.16 : (tundra ? 0.25 : 0.55) + random2() * 0.35
3264
- );
3507
+ const scale = temperate ? 0.42 + 1.18 * age ** 1.6 + random2() * 0.16 : (tundra ? 0.25 : 0.55) + random2() * 0.35;
3508
+ const standing = !BANK.some(([dx, dz]) => wet(x + dx, z + dz) || nearRiver(x + dx, z + dz));
3509
+ bucket.push([x, z, reach, standing ? scale : 0]);
3510
+ put(treeAsset, x, z, scale);
3265
3511
  const edge = stand < 0.57;
3266
3512
  for (let i = 0; i < (tundra ? 0 : edge ? 2 : 1); i++) {
3267
3513
  const angle = random2() * Math.PI * 2;
@@ -3276,23 +3522,37 @@ function scatterProps(data, focus, clearings = []) {
3276
3522
  put("fern", x + (random2() - 0.5) * 7, z + (random2() - 0.5) * 7, 0.6 + random2() * 0.75);
3277
3523
  }
3278
3524
  if (temperate) {
3279
- for (let i = 0; i < 14e3; i++) {
3280
- const x = (random2() - 0.5) * data.size;
3281
- const z = (random2() - 0.5) * data.size;
3282
- if (!inside(x, z) || wet(x, z) || nearEye(x, z)) continue;
3283
- if (slopeDegrees(data, x, z) > 30) continue;
3284
- const stand = forestWeight(x, z);
3285
- const damp = hollow(x, z);
3286
- const open = grassWeight(data, x, z);
3287
- if (open < 0.28) continue;
3288
- if (stand > 0.44 && random2() < (stand - 0.4) * 1.9 + damp * 0.25)
3289
- put("litter", x, z, 0.65 + random2() * 1.1);
3290
- if (stand > 0.4 && damp > 0.25 && random2() < damp * 0.55)
3291
- put("fern", x, z, 0.7 + random2() * 0.8);
3292
- if (stand < 0.66 && random2() < 0.05 + damp * 0.16 + (stand < 0.5 ? 0.06 : 0))
3293
- put("bush", x, z, 0.5 + random2() * 1.1);
3294
- if (stand < 0.58 && damp > 0.18 && random2() < 0.02 + damp * 0.05)
3295
- put("poppy", x, z, 0.75 + random2() * 0.6);
3525
+ const understoreyPass = (draw, row2) => {
3526
+ for (let i = 0; i < row2.attempts; i++) {
3527
+ const x = (draw() - 0.5) * data.size;
3528
+ const z = (draw() - 0.5) * data.size;
3529
+ if (!inside(x, z) || wet(x, z) || nearEye(x, z)) continue;
3530
+ if (slopeDegrees(data, x, z) > 30) continue;
3531
+ const stand = forestWeight(x, z);
3532
+ const damp = hollow(x, z);
3533
+ const open = grassWeight(data, x, z);
3534
+ if (open < 0.28) continue;
3535
+ if (stand > 0.44 && draw() < ((stand - 0.4) * 1.9 + damp * 0.25) * row2.litter)
3536
+ put("litter", x, z, 0.65 + draw() * 1.1);
3537
+ if (stand > 0.4 && damp > 0.25 && draw() < damp * 0.55 * row2.fern)
3538
+ put("fern", x, z, 0.7 + draw() * 0.8);
3539
+ if (stand < 0.66 && draw() < (0.05 + damp * 0.16 + (stand < 0.5 ? 0.06 : 0)) * row2.bush)
3540
+ put("bush", x, z, 0.5 + draw() * 1.1);
3541
+ if (stand < 0.58 && damp > 0.18 && draw() < (0.02 + damp * 0.05) * row2.flower)
3542
+ put("poppy", x, z, 0.75 + draw() * 0.6);
3543
+ if (row2.deadfall > 0 && stand > 0.5 && draw() < 0.012 * row2.deadfall)
3544
+ put("deadfall", x, z, 0.7 + draw() * 0.9);
3545
+ }
3546
+ };
3547
+ const row = rules.understorey;
3548
+ const graded = row.attempts === GRADED_UNDERSTOREY.attempts && row.litter === 1 && row.fern === 1 && row.bush === 1 && row.flower === 1 && row.deadfall === 0;
3549
+ placing = graded;
3550
+ understoreyPass(random2, { ...row, ...GRADED_UNDERSTOREY });
3551
+ placing = true;
3552
+ if (!graded) {
3553
+ poseDraw = createRandom(rules.seed + 13);
3554
+ understoreyPass(poseDraw, row);
3555
+ poseDraw = random2;
3296
3556
  }
3297
3557
  }
3298
3558
  if (tundra || desert) {
@@ -3400,7 +3660,8 @@ function scatterProps(data, focus, clearings = []) {
3400
3660
  }
3401
3661
  }
3402
3662
  }
3403
- function cover2(x, z, density) {
3663
+ const shadeDraw = createRandom(rules.seed + 14);
3664
+ function coverAt(x, z, density) {
3404
3665
  if (!inside(x, z) || wet(x, z) || slopeDegrees(data, x, z) > 34 || (tundra ? clamp012((tundraCover(x, z) - 0.47) / 0.3) : grassWeight(data, x, z)) < 0.22)
3405
3666
  return;
3406
3667
  const drift = coverDriftWeight(rules.drift, x, z);
@@ -3426,6 +3687,11 @@ function scatterProps(data, focus, clearings = []) {
3426
3687
  if (rules.scrubBush && random2() < 0.02)
3427
3688
  put("bush", x + (random2() - 0.5) * 2, z + (random2() - 0.5) * 2, 0.6 + random2() * 0.45);
3428
3689
  }
3690
+ function cover2(x, z, density) {
3691
+ placing = !(rules.understorey.shade > 0 && shaded(x, z, rules.understorey.shadeRadius) && shadeDraw() < rules.understorey.shade);
3692
+ coverAt(x, z, density);
3693
+ placing = true;
3694
+ }
3429
3695
  for (let z = -half + 3; z < half - 3; z += rules.grassCell)
3430
3696
  for (let x = -half + 3; x < half - 3; x += rules.grassCell)
3431
3697
  cover2(
@@ -3548,7 +3814,17 @@ function scatterProps(data, focus, clearings = []) {
3548
3814
  for (const asset of GROWING) if (asset in counts) counts[asset] = growing[asset] ?? 0;
3549
3815
  return { counts, placements: kept };
3550
3816
  }
3551
- var GROWING = /* @__PURE__ */ new Set(["spruce", "sapling", "bush", "fern", "grass", "scrub", "poppy", "litter"]);
3817
+ var GROWING = /* @__PURE__ */ new Set([
3818
+ "spruce",
3819
+ "sapling",
3820
+ "bush",
3821
+ "fern",
3822
+ "grass",
3823
+ "scrub",
3824
+ "poppy",
3825
+ "litter",
3826
+ "deadfall"
3827
+ ]);
3552
3828
  var TRUNKS = /* @__PURE__ */ new Set(["spruce", "sapling"]);
3553
3829
  var BANK = [
3554
3830
  [0, 0],
@@ -3757,6 +4033,35 @@ var BAKE_BIOME_STAGES = [
3757
4033
  "selfPrune",
3758
4034
  "meshSpawn"
3759
4035
  ];
4036
+ function withImpostorDistances(assets, spec) {
4037
+ if (assets === void 0) return void 0;
4038
+ const distances = /* @__PURE__ */ new Map();
4039
+ for (const foliage2 of spec.foliage) {
4040
+ const distance = foliage2.far?.impostor;
4041
+ if (distance === void 0) continue;
4042
+ if (!Number.isFinite(distance) || distance <= 0)
4043
+ throw new Error(
4044
+ `${spec.look}: ${foliage2.species}.far.impostor must be a positive distance, got ${String(distance)}.`
4045
+ );
4046
+ distances.set(foliage2.species, distance);
4047
+ }
4048
+ if (distances.size === 0) return assets;
4049
+ const filled = {};
4050
+ for (const [species2, asset] of Object.entries(assets)) {
4051
+ const distance = distances.get(species2);
4052
+ if (distance === void 0 || asset.impostorDistance !== void 0) {
4053
+ filled[species2] = asset;
4054
+ continue;
4055
+ }
4056
+ const gate = asset.lods?.reduce((furthest, lod) => Math.max(furthest, lod.distance), 0) ?? 0;
4057
+ if (distance <= gate)
4058
+ throw new Error(
4059
+ `${species2}: impostorDistance (${String(distance)}) must follow every authored LOD gate (furthest ${String(gate)}), or the impostor replaces a level that still draws.`
4060
+ );
4061
+ filled[species2] = { ...asset, impostorDistance: distance };
4062
+ }
4063
+ return filled;
4064
+ }
3760
4065
  function bakeBiome(options) {
3761
4066
  const spec = validateBiome(options.spec, options.spec.look);
3762
4067
  const scatter = (options.stream ?? spec.stream) === "ordered" ? scatterProps(
@@ -3765,8 +4070,9 @@ function bakeBiome(options) {
3765
4070
  spec.cover.clearings
3766
4071
  ) : null;
3767
4072
  const state = scatter === null ? options.state : { ...options.state, instances: scatter.placements };
3768
- const baked = options.assets === void 0 ? null : bakeWorldPackage(state, {
3769
- assets: options.assets,
4073
+ const assets = withImpostorDistances(options.assets, spec);
4074
+ const baked = assets === void 0 ? null : bakeWorldPackage(state, {
4075
+ assets,
3770
4076
  ...options.cellSize === void 0 ? {} : { cellSize: options.cellSize },
3771
4077
  ...options.layers === void 0 ? {} : { layers: options.layers },
3772
4078
  ...options.names === void 0 ? {} : { names: options.names }
@@ -3859,7 +4165,8 @@ function farForestPlacements(options) {
3859
4165
  );
3860
4166
  return Math.hypot(x - a[0] - t * dx, z - a[1] - t * dz) < reach;
3861
4167
  });
3862
- const wet = (x, z, height) => inRiver(x, z) || (options.lakes ?? []).some(
4168
+ const sea = options.seaLevel ?? Number.NEGATIVE_INFINITY;
4169
+ const wet = (x, z, height) => height < sea + 0.35 || inRiver(x, z) || (options.lakes ?? []).some(
3863
4170
  (lake) => Math.hypot(x - (lake.at[0] ?? 0), z - (lake.at[1] ?? 0)) < lake.radius && height < lake.level + 0.35
3864
4171
  );
3865
4172
  const canopy = options.collar.geometry.getAttribute("canopy");
@@ -3923,6 +4230,265 @@ function farForestPlacements(options) {
3923
4230
  return placements;
3924
4231
  }
3925
4232
 
4233
+ // src/core/massif.ts
4234
+ function perimeterBase(n, edge, window, decay) {
4235
+ const loop = 4 * (n - 1);
4236
+ const at = (k) => {
4237
+ const m = (k % loop + loop) % loop;
4238
+ const side = Math.floor(m / (n - 1));
4239
+ const t = m % (n - 1);
4240
+ return side === 0 ? [0, t] : side === 1 ? [t, n - 1] : side === 2 ? [n - 1, n - 1 - t] : [n - 1 - t, 0];
4241
+ };
4242
+ const raw = Float64Array.from({ length: loop }, (_, k) => {
4243
+ const [row, column] = at(k);
4244
+ return edge(row * n + column);
4245
+ });
4246
+ const prefix = new Float64Array(3 * loop + 1);
4247
+ for (let k = 0; k < 3 * loop; k++)
4248
+ prefix[k + 1] = prefix[k] + raw[k % loop];
4249
+ const half = Math.max(1, Math.round(window / 2));
4250
+ const smooth = Float64Array.from(
4251
+ { length: loop },
4252
+ (_, k) => (prefix[loop + k + half + 1] - prefix[loop + k - half]) / (2 * half + 1)
4253
+ );
4254
+ const index = (row, column) => {
4255
+ if (row === 0) return column;
4256
+ if (column === n - 1) return n - 1 + row;
4257
+ if (row === n - 1) return 2 * (n - 1) + (n - 1 - column);
4258
+ return 3 * (n - 1) + (n - 1 - row);
4259
+ };
4260
+ const s = (row, column) => smooth[index(row, column) % loop];
4261
+ const r = (row, column) => raw[index(row, column) % loop] - smooth[index(row, column) % loop];
4262
+ const base = new Float32Array(n * n);
4263
+ const last = n - 1;
4264
+ for (let row = 0; row < n; row++) {
4265
+ const v = row / last;
4266
+ for (let column = 0; column < n; column++) {
4267
+ const u = column / last;
4268
+ const coons = (1 - v) * s(0, column) + v * s(last, column) + (1 - u) * s(row, 0) + u * s(row, last) - ((1 - u) * (1 - v) * s(0, 0) + u * (1 - v) * s(0, last) + (1 - u) * v * s(last, 0) + u * v * s(last, last));
4269
+ const detail = r(0, column) * Math.exp(-row / decay) + r(last, column) * Math.exp(-(last - row) / decay) + r(row, 0) * Math.exp(-column / decay) + r(row, last) * Math.exp(-(last - column) / decay);
4270
+ base[row * n + column] = coons + detail;
4271
+ }
4272
+ }
4273
+ for (let k = 0; k < loop; k++) {
4274
+ const [row, column] = at(k);
4275
+ base[row * n + column] = raw[k];
4276
+ }
4277
+ return base;
4278
+ }
4279
+ function liftMassif(tile) {
4280
+ const n = tile.resolution;
4281
+ if (tile.heights.length !== n * n)
4282
+ throw new RangeError(`Massif ${tile.id}: ${n}^2 heights expected`);
4283
+ const spacing = tile.size / (n - 1);
4284
+ const base = perimeterBase(n, (i) => tile.heights[i], 200 / spacing, 50 / spacing);
4285
+ return Float32Array.from(
4286
+ { length: n * n },
4287
+ (_, i) => tile.heights[i] - base[i]
4288
+ );
4289
+ }
4290
+ var catmullRom = (a, b, c, d, t) => b + 0.5 * t * (c - a + t * (2 * a - 5 * b + 4 * c - d + t * (3 * (b - c) + d - a)));
4291
+ function faceDirection(lifted, n, spacing) {
4292
+ let x = 0;
4293
+ let z = 0;
4294
+ const step = Math.max(1, Math.floor(n / 128));
4295
+ for (let row = step; row < n - step; row += step)
4296
+ for (let column = step; column < n - step; column += step) {
4297
+ if (lifted[row * n + column] < 50) continue;
4298
+ const dx = (lifted[row * n + column + step] - lifted[row * n + column - step]) / (2 * step * spacing);
4299
+ const dz = (lifted[(row + step) * n + column] - lifted[(row - step) * n + column]) / (2 * step * spacing);
4300
+ const g2 = dx * dx + dz * dz;
4301
+ if (g2 < 1) continue;
4302
+ const g = Math.sqrt(g2);
4303
+ x -= dx / g * g2;
4304
+ z -= dz / g * g2;
4305
+ }
4306
+ return Math.atan2(z, x);
4307
+ }
4308
+ var KEEP_OUT_RAMP = 600;
4309
+ function placeMassif(terrain, options) {
4310
+ const draw = random(options.seed * 2654435761 ^ 1835103091);
4311
+ const tiles = Array.isArray(options.tile) ? options.tile : [options.tile];
4312
+ if (tiles.length === 0) throw new RangeError("placeMassif needs at least one tile");
4313
+ const tile = tiles[Math.floor(draw() * tiles.length) % tiles.length];
4314
+ const lifted = liftMassif(tile);
4315
+ const n = tile.resolution;
4316
+ const spacing = tile.size / (n - 1);
4317
+ let summit = 0;
4318
+ for (let i = 1; i < lifted.length; i++)
4319
+ if (lifted[i] > lifted[summit]) summit = i;
4320
+ const tall = lifted[summit];
4321
+ if (!(tall > 0)) throw new RangeError(`Massif ${tile.id} has no relief above its border`);
4322
+ const mirror = draw() < 0.5;
4323
+ const local = (i) => {
4324
+ const x = (i % n / (n - 1) - 0.5) * tile.size;
4325
+ return [mirror ? -x : x, (Math.floor(i / n) / (n - 1) - 0.5) * tile.size];
4326
+ };
4327
+ const [sx, sz] = local(summit);
4328
+ const state = terrain.evaluate();
4329
+ const N = state.resolution;
4330
+ const cell = state.size / (N - 1);
4331
+ const halfWorld = state.size / 2;
4332
+ const at = options.at !== void 0 ? [options.at[0], options.at[1]] : (() => {
4333
+ const [b0, b1] = options.bearing ?? [0, 360];
4334
+ const [d0, d1] = options.distance ?? [tile.size / 2, halfWorld - tile.size / 4];
4335
+ const bearing = (b0 + draw() * (b1 - b0)) * Math.PI / 180;
4336
+ const distance = d0 + draw() * (d1 - d0);
4337
+ return [Math.cos(bearing) * distance, Math.sin(bearing) * distance];
4338
+ })();
4339
+ if (options.at !== void 0) {
4340
+ draw();
4341
+ draw();
4342
+ }
4343
+ const faceTile = faceDirection(lifted, n, spacing);
4344
+ const face = mirror ? Math.PI - faceTile : faceTile;
4345
+ const toCentre = options.face !== void 0 ? options.face * Math.PI / 180 : Math.atan2(-at[1], -at[0]);
4346
+ const jitter = (draw() - 0.5) * 40 * Math.PI / 180;
4347
+ const turn = toCentre + jitter - face;
4348
+ const pick = draw();
4349
+ const keepOut = options.keepOut ?? 320;
4350
+ const cos = Math.cos(turn);
4351
+ const sin = Math.sin(turn);
4352
+ const toTile = (x, z) => {
4353
+ const dx = x - at[0];
4354
+ const dz = z - at[1];
4355
+ const mx = sx + dx * cos + dz * sin;
4356
+ const mz = sz - dx * sin + dz * cos;
4357
+ return [(mirror ? -mx : mx) / tile.size + 0.5, mz / tile.size + 0.5];
4358
+ };
4359
+ const toWorld = (u, v) => {
4360
+ const mx = (mirror ? -1 : 1) * (u - 0.5) * tile.size - sx;
4361
+ const mz = (v - 0.5) * tile.size - sz;
4362
+ return [at[0] + mx * cos - mz * sin, at[1] + mx * sin + mz * cos];
4363
+ };
4364
+ const hostAt = (x, z) => sampleGrid(state.height, N, N, (x + halfWorld) / state.size, (z + halfWorld) / state.size);
4365
+ const hostSmooth = (x, z) => {
4366
+ const gx = clamp((x + halfWorld) / cell, 0, N - 1);
4367
+ const gz = clamp((z + halfWorld) / cell, 0, N - 1);
4368
+ const ix = Math.min(N - 2, Math.floor(gx));
4369
+ const iz = Math.min(N - 2, Math.floor(gz));
4370
+ const tx = gx - ix;
4371
+ const tz = gz - iz;
4372
+ const h = (r, c) => state.height[clamp(r, 0, N - 1) * N + clamp(c, 0, N - 1)];
4373
+ const row = (r) => catmullRom(h(r, ix - 1), h(r, ix), h(r, ix + 1), h(r, ix + 2), tx);
4374
+ return catmullRom(row(iz - 1), row(iz), row(iz + 1), row(iz + 2), tz);
4375
+ };
4376
+ const B = 129;
4377
+ const hostEdge = new Float32Array(B * B);
4378
+ for (let row = 0; row < B; row++)
4379
+ for (let column = 0; column < B; column++)
4380
+ if (row === 0 || column === 0 || row === B - 1 || column === B - 1)
4381
+ hostEdge[row * B + column] = hostAt(...toWorld(column / (B - 1), row / (B - 1)));
4382
+ const hostBase = perimeterBase(
4383
+ B,
4384
+ (i) => hostEdge[i],
4385
+ 200 / (tile.size / (B - 1)),
4386
+ 1e-6
4387
+ );
4388
+ const foot = sampleGrid(hostBase, B, B, ...toTile(at[0], at[1]));
4389
+ let floor = Number.POSITIVE_INFINITY;
4390
+ for (let i = 0; i < B * B; i++) {
4391
+ const row = Math.floor(i / B);
4392
+ const column = i % B;
4393
+ if (row === 0 || column === 0 || row === B - 1 || column === B - 1)
4394
+ floor = Math.min(floor, hostEdge[i]);
4395
+ }
4396
+ let scale;
4397
+ if (options.top) {
4398
+ const top = options.top[0] + pick * (options.top[1] - options.top[0]);
4399
+ scale = Number.POSITIVE_INFINITY;
4400
+ for (let row = 0; row < n; row += 4)
4401
+ for (let column = 0; column < n; column += 4) {
4402
+ const rise = lifted[row * n + column];
4403
+ if (rise < 1) continue;
4404
+ const u = column / (n - 1);
4405
+ const v = row / (n - 1);
4406
+ const [x, z] = toWorld(u, v);
4407
+ if (Math.abs(x) > halfWorld || Math.abs(z) > halfWorld) continue;
4408
+ scale = Math.min(scale, (top - sampleGrid(hostBase, B, B, u, v)) / rise);
4409
+ }
4410
+ } else {
4411
+ const [h0, h1] = options.height ?? [0.85 * tall, 1.15 * tall];
4412
+ scale = (h0 + pick * (h1 - h0)) / tall;
4413
+ }
4414
+ if (!(scale > 0))
4415
+ throw new RangeError(`Massif ${tile.id}: top under its foot at ${foot.toFixed(0)} m`);
4416
+ const height = scale * tall;
4417
+ const factor = Math.max(1, Math.round(cell / spacing));
4418
+ const coarseN = Math.floor((n - 1) / factor) + 1;
4419
+ const coarse = new Float32Array(coarseN * coarseN);
4420
+ const tent = (d) => factor - Math.abs(d);
4421
+ for (let row = 0; row < coarseN; row++)
4422
+ for (let column = 0; column < coarseN; column++) {
4423
+ let sum = 0;
4424
+ let total = 0;
4425
+ for (let dr = 1 - factor; dr < factor; dr++)
4426
+ for (let dc = 1 - factor; dc < factor; dc++) {
4427
+ const r = row * factor + dr;
4428
+ const c = column * factor + dc;
4429
+ if (r < 0 || c < 0 || r >= n || c >= n) continue;
4430
+ const w = tent(dr) * tent(dc);
4431
+ sum += w * lifted[r * n + c];
4432
+ total += w;
4433
+ }
4434
+ coarse[row * coarseN + column] = sum / total;
4435
+ }
4436
+ const finalAt = (x, z, grid, gridN) => {
4437
+ const [u, v] = toTile(x, z);
4438
+ if (u <= 0 || u >= 1 || v <= 0 || v >= 1) return [0, 0];
4439
+ const edge = Math.min(u, v, 1 - u, 1 - v);
4440
+ const reach = Math.max(Math.abs(x), Math.abs(z));
4441
+ const weight = smoothstep(0, 0.04, edge) * smoothstep(keepOut, keepOut + KEEP_OUT_RAMP, reach);
4442
+ if (weight === 0) return [0, 0];
4443
+ const raw = sampleGrid(hostBase, B, B, u, v) + scale * sampleGrid(grid, gridN, gridN, u, v);
4444
+ const value = (raw + floor + Math.sqrt((raw - floor) ** 2 + 40 ** 2)) / 2;
4445
+ return [value, weight];
4446
+ };
4447
+ const delta = new Float32Array(N * N);
4448
+ for (let row = 0; row < N; row++)
4449
+ for (let column = 0; column < N; column++) {
4450
+ const x = column * cell - halfWorld;
4451
+ const z = row * cell - halfWorld;
4452
+ const [value, weight] = finalAt(x, z, coarse, coarseN);
4453
+ if (weight > 0) {
4454
+ const host = state.height[row * N + column];
4455
+ delta[row * N + column] = weight * (value - host);
4456
+ }
4457
+ }
4458
+ const corners = [toWorld(0, 0), toWorld(1, 0), toWorld(0, 1), toWorld(1, 1)];
4459
+ const x0 = clamp(Math.min(...corners.map((p) => p[0])), -halfWorld, halfWorld);
4460
+ const x1 = clamp(Math.max(...corners.map((p) => p[0])), -halfWorld, halfWorld);
4461
+ const z0 = clamp(Math.min(...corners.map((p) => p[1])), -halfWorld, halfWorld);
4462
+ const z1 = clamp(Math.max(...corners.map((p) => p[1])), -halfWorld, halfWorld);
4463
+ const columns = Math.floor((x1 - x0) / spacing) + 1;
4464
+ const rows = Math.floor((z1 - z0) / spacing) + 1;
4465
+ const detail = new Float32Array(columns * rows);
4466
+ for (let row = 0; row < rows; row++)
4467
+ for (let column = 0; column < columns; column++) {
4468
+ const x = x0 + column * spacing;
4469
+ const z = z0 + row * spacing;
4470
+ const host = hostSmooth(x, z);
4471
+ const [value, weight] = finalAt(x, z, lifted, n);
4472
+ detail[row * columns + column] = host + weight * (value - host);
4473
+ }
4474
+ const id = options.id ?? "massif";
4475
+ return {
4476
+ terrain: terrain.heightmap({
4477
+ id,
4478
+ data: { width: N, height: N, values: Array.from(delta) },
4479
+ blend: "add"
4480
+ }),
4481
+ placement: {
4482
+ tile: tile.id,
4483
+ at,
4484
+ rotation: turn * 180 / Math.PI,
4485
+ mirror,
4486
+ height,
4487
+ detail: { origin: [x0, z0], spacing, columns, rows, heights: detail }
4488
+ }
4489
+ };
4490
+ }
4491
+
3926
4492
  // src/core/stats.ts
3927
4493
  var CHANNEL_AREA_M2 = 1e5;
3928
4494
  var quantile = (sorted, q) => sorted[Math.min(sorted.length - 1, Math.floor(q * sorted.length))];
@@ -4483,7 +5049,7 @@ function encodeGLB(input) {
4483
5049
  }
4484
5050
  const binary = join(chunks);
4485
5051
  const doc = {
4486
- asset: { version: "2.0", generator: "@threenative/terrain 0.1.8" },
5052
+ asset: { version: "2.0", generator: "@threenative/terrain 0.1.9" },
4487
5053
  scene: 0,
4488
5054
  scenes: [{ nodes: meshes.map((_, i) => i) }],
4489
5055
  nodes: meshes.map((m, i) => ({ mesh: i, name: m.name ?? `Terrain ${i}` })),
@@ -4779,6 +5345,39 @@ function propAssetOf(modelKey) {
4779
5345
  const at = modelKey.indexOf(":");
4780
5346
  return at < 0 ? modelKey : modelKey.slice(0, at);
4781
5347
  }
5348
+ function decodePropBucket(bucket) {
5349
+ const floats = new Float32Array(
5350
+ bucket.placements.buffer,
5351
+ bucket.placements.byteOffset,
5352
+ bucket.placements.byteLength / 4
5353
+ );
5354
+ if (floats.length !== bucket.ids.length * 8)
5355
+ throw new Error(
5356
+ `TN_PROP_BUCKET_INVALID: bucket '${bucket.bucket}' holds ${floats.length / 8} records for ${bucket.ids.length} IDs.`
5357
+ );
5358
+ const keys = new Array(bucket.ids.length);
5359
+ for (const cell of bucket.cells)
5360
+ for (const run of cell.runs)
5361
+ for (let index = 0; index < run.count; index++) keys[run.offset + index] = run.asset;
5362
+ return bucket.ids.map((id, index) => {
5363
+ const key = keys[index];
5364
+ if (key === void 0)
5365
+ throw new Error(
5366
+ `TN_PROP_BUCKET_INVALID: bucket '${bucket.bucket}' record '${id}' sits in no run.`
5367
+ );
5368
+ const at = index * 8;
5369
+ const f = (k) => floats[at + k];
5370
+ return {
5371
+ id,
5372
+ key,
5373
+ asset: propAssetOf(key),
5374
+ position: [f(0), f(1), f(2)],
5375
+ quaternion: [f(3), f(4), f(5), f(6)],
5376
+ scale: f(7),
5377
+ reach: bucket.reach[index] ?? bucket.reach[0] ?? Number.POSITIVE_INFINITY
5378
+ };
5379
+ });
5380
+ }
4782
5381
  function propPackageFile(world2, bucket, suffix) {
4783
5382
  return `${world2}.${bucket.replace(/[^a-zA-Z0-9]+/g, "-")}.${suffix}`;
4784
5383
  }
@@ -4805,4 +5404,4 @@ function propPackageFiles(pkg, world2) {
4805
5404
  return { files, manifest: new TextEncoder().encode(JSON.stringify(manifest)) };
4806
5405
  }
4807
5406
 
4808
- export { BAKE_BIOME_STAGES, LANDFORMS, MATERIAL_IDS, MATS_FIELD, Mask, PARAMS, PROP_CELL_SIZE, RESOLUTIONS, STAND_FIELD, Terrain, TerrainEvaluator, applyPlacementOverrides, bakeBiome, bakeMesh, bakeTerrain, bakeWorldPackage, collarSampler, compilePropBuckets, createSegmentIndex, decodeHeightPNG, decodeRAW16, encodeGLB, encodeHeightPNG, encodeRAW16, encodeSplatPNGs, encodeZIP, farForestPlacements, fieldWeight, gradientAt, landform, landformBackdrop, makeExport, measureBands, orderedScatterRules, propAssetOf, propPackageFile, propPackageFiles, sampleHeight, scatterProps, slopeAtIndex, slopeQuantile, splinePoints, terrainStats, validateBiome, validateDocument, validatePlacementOverrides };
5407
+ export { BAKE_BIOME_STAGES, LANDFORMS, MATERIAL_IDS, MATS_FIELD, Mask, PARAMS, PROP_CELL_SIZE, RESOLUTIONS, STAND_FIELD, Terrain, TerrainEvaluator, applyPlacementOverrides, bakeBiome, bakeMesh, bakeTerrain, bakeWorldPackage, collarSampler, compilePropBuckets, createSegmentIndex, decodeHeightPNG, decodePropBucket, decodeRAW16, encodeGLB, encodeHeightPNG, encodeRAW16, encodeSplatPNGs, encodeZIP, farForestPlacements, fieldWeight, gradientAt, landform, landformBackdrop, liftMassif, makeExport, measureBands, orderedScatterRules, placeMassif, propAssetOf, propPackageFile, propPackageFiles, sampleHeight, scatterProps, slopeAtIndex, slopeQuantile, splinePoints, terrainStats, validateBiome, validateDocument, validatePlacementOverrides };