@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
@@ -1,7 +1,7 @@
1
- import { P as PatchCommand } from '../terrain-B3DkduPw.js';
2
- import { L as Layer, i as IPlacementOverride, I as ITerrainState } from '../types-CPYAc9vo.js';
3
- import { I as IEditorSnapshot, a as IAuthoringDocument, b as ISavedCamera, c as IFocusRequest, d as IFocusOutcome, e as IFocusBounds, f as IEnvironment, g as IEditorActivation, h as ISpatialQuery, i as ISpatialObservation, j as ICameraOperation, k as ICameraResult, l as IEnvironmentOperation, m as IEnvironmentResult, n as IAssetOperation, o as IAssetResult } from '../server-C8Ydr_IM.js';
4
- export { p as IAssetAdjust, q as IAssetBounds, r as IAssetKind, s as IAssetLimits, t as ICameraPose, u as ICameraSet, v as IFocusFraming, w as IFocusResolver, x as IFocusTarget, y as IHexColour, z as IPointObservation, A as IProfileObservation, B as IProfileSample, C as IProjectAsset, D as IReferenceObservation, E as ISavedSpatialReference, F as ISpatialControlPoint, G as ISpatialReference, H as ISurfaceMapping, J as ISurfaceMappings, K as ISurfaceReading, L as checkEnvironmentAssets, M as focusCamera, N as inspectSpatial, O as probeTerrain, P as runCameraOperation, Q as runEnvironmentOperation, R as sniff, S as surfaceSpace, T as validateAsset, U as validateAssets, V as validateCamera, W as validateCameras, X as validateEnvironment, Y as validateSpatialReference, Z as validateSurfaces } from '../server-C8Ydr_IM.js';
1
+ import { P as PatchCommand } from '../terrain-CAEaW0mQ.js';
2
+ import { L as Layer, i as IPlacementOverride, I as ITerrainState } from '../types-BpH00qsx.js';
3
+ import { I as IEditorSnapshot, a as IAuthoringDocument, b as ISavedCamera, c as IFocusRequest, d as IFocusOutcome, e as IFocusBounds, f as IEnvironment, g as IEditorActivation, h as ISpatialQuery, i as ISpatialObservation, j as ICameraOperation, k as ICameraResult, l as IEnvironmentOperation, m as IEnvironmentResult, n as IAssetOperation, o as IAssetResult } from '../server-B5RdvEAC.js';
4
+ export { p as IAssetAdjust, q as IAssetBounds, r as IAssetKind, s as IAssetLimits, t as ICameraPose, u as ICameraSet, v as IFocusFraming, w as IFocusResolver, x as IFocusTarget, y as IHexColour, z as IPointObservation, A as IProfileObservation, B as IProfileSample, C as IProjectAsset, D as IReferenceObservation, E as ISavedSpatialReference, F as ISpatialControlPoint, G as ISpatialReference, H as ISurfaceMapping, J as ISurfaceMappings, K as ISurfaceReading, L as checkEnvironmentAssets, M as focusCamera, N as inspectSpatial, O as probeTerrain, P as runCameraOperation, Q as runEnvironmentOperation, R as sniff, S as surfaceSpace, T as validateAsset, U as validateAssets, V as validateCamera, W as validateCameras, X as validateEnvironment, Y as validateSpatialReference, Z as validateSurfaces } from '../server-B5RdvEAC.js';
5
5
  import * as three from 'three';
6
6
  import { Quaternion, Vector3, Camera, Vector2, Object3D, Intersection, Mesh, Matrix4 } from 'three';
7
7
  import { TransformControls } from 'three/addons/controls/TransformControls.js';
@@ -54,6 +54,64 @@ declare function inspectImage(bytes: Uint8Array, format: string, limits: {
54
54
  maxDimension: number;
55
55
  }): IImageReport;
56
56
 
57
+ /** A range bar over a number. */
58
+ interface ITweakRange {
59
+ readonly value: number;
60
+ readonly min: number;
61
+ readonly max: number;
62
+ /** The bar's step and the snapshot's rounding. Default: a hundredth of the range. */
63
+ readonly step?: number;
64
+ /** A heading drawn above the first knob that names it. */
65
+ readonly group?: string;
66
+ }
67
+ /** A picker over an sRGB hex colour. */
68
+ interface ITweakColor {
69
+ readonly color: number;
70
+ readonly group?: string;
71
+ }
72
+ /** One knob of a tweak box. */
73
+ type TweakKnob = ITweakRange | ITweakColor;
74
+ /** The page, the store and the clipboard a tweak box uses. A test passes its own. */
75
+ interface ITweakHost {
76
+ readonly document?: Document;
77
+ readonly storage?: Pick<Storage, "getItem" | "setItem" | "removeItem">;
78
+ readonly copy?: (text: string) => void;
79
+ }
80
+ interface ITweakBoxOptions<K extends string> {
81
+ /** The box's heading, and the `tweaks` name written into a snapshot. */
82
+ readonly title: string;
83
+ readonly knobs: Readonly<Record<K, TweakKnob>>;
84
+ /** Called once per knob when the box is made, with the stored or default value, and on each change. */
85
+ readonly onChange: (name: K, value: number, values: Readonly<Record<K, number>>) => void;
86
+ /** Where the values survive a reload (`localStorage`). Absent, nothing is stored. */
87
+ readonly storageKey?: string;
88
+ readonly host?: ITweakHost;
89
+ }
90
+ interface ITweakBox<K extends string> {
91
+ /** The live values: a number per knob, a colour as its sRGB hex number. */
92
+ readonly values: Readonly<Record<K, number>>;
93
+ /** Set one knob as its bar would. Throws on a value outside the knob's range. */
94
+ set(name: K, value: number): void;
95
+ /** The values as JSON text: numbers at their step, colours as `#rrggbb`. This is what Copy copies. */
96
+ snapshot(): string;
97
+ /** Put every knob back to the value its spec names, and forget the stored ones. */
98
+ reset(): void;
99
+ dispose(): void;
100
+ }
101
+ /**
102
+ * A box of range bars and colour pickers over the running game, for tuning numbers by eye.
103
+ *
104
+ * The game names its knobs and applies each change itself; the box owns the page elements, the
105
+ * stored values and the Copy button, whose JSON is what gets pasted back and written into the game's
106
+ * own source. With no `document` (a native launch, a test) it draws nothing and still holds the
107
+ * values, so the game code that reads them is the same on every target.
108
+ *
109
+ * @situation tune look numbers by eye with live range bars and colour pickers while the game runs
110
+ * @requires npm i -D @threenative/terrain
111
+ * @example const box = createTweakBox({ title: "Fog", knobs: { density: { min: 0, max: 0.1, value: 0.02 } }, onChange: (name, value) => console.info(name, value) });
112
+ */
113
+ declare function createTweakBox<K extends string>(options: ITweakBoxOptions<K>): ITweakBox<K>;
114
+
57
115
  /**
58
116
  * Recipe footprints are heights over X/Z, never independently transformed solids.
59
117
  * @requires npm i -D @threenative/terrain
@@ -383,4 +441,4 @@ declare function mountTerrainEditor(options: {
383
441
  dispose(): void;
384
442
  }>;
385
443
 
386
- export { IAssetOperation, IAssetResult, ICameraOperation, ICameraResult, type IEditorView, IEnvironment, IEnvironmentOperation, IEnvironmentResult, IFocusBounds, IFocusOutcome, IFocusRequest, type IImageReport, ISavedCamera, ISpatialObservation, ISpatialQuery, type ISurfaceChannel, type IViewCamera, type IViewEnvironment, type IViewerPose, SURFACE_CHANNELS, TerrainEditorController, createEditorSelection, editorPlacementCenter, editorPointToClient, editorSurfaceHeight, inspectImage, landformPose, mountTerrainEditor, pickEditorPlacement, pickEditorSurface, transformedLandform };
444
+ export { IAssetOperation, IAssetResult, ICameraOperation, ICameraResult, type IEditorView, IEnvironment, IEnvironmentOperation, IEnvironmentResult, IFocusBounds, IFocusOutcome, IFocusRequest, type IImageReport, ISavedCamera, ISpatialObservation, ISpatialQuery, type ISurfaceChannel, type ITweakBox, type ITweakBoxOptions, type ITweakColor, type ITweakHost, type ITweakRange, type IViewCamera, type IViewEnvironment, type IViewerPose, SURFACE_CHANNELS, TerrainEditorController, type TweakKnob, createEditorSelection, createTweakBox, editorPlacementCenter, editorPointToClient, editorSurfaceHeight, inspectImage, landformPose, mountTerrainEditor, pickEditorPlacement, pickEditorSurface, transformedLandform };
@@ -641,6 +641,146 @@ function fluvial(height, n, size, {
641
641
  return Float32Array.from(h);
642
642
  }
643
643
 
644
+ // src/core/gully.ts
645
+ var STEP_SECONDS = 0.1;
646
+ var FLUX_KEEP = 0.99;
647
+ var DISSOLVE = 0.3;
648
+ var DEPOSIT = 0.02;
649
+ var EVAPORATE = 0.05;
650
+ var DEPTH_REF = 0.01;
651
+ var DEPTH_MAX = 0.3;
652
+ var MIN_TILT = 0.05;
653
+ var GRAVITY = 9.81;
654
+ var MAX_ITERATIONS = 200;
655
+ function gully(height, n, size, { iterations = 200, capacity = 0.05, rain = 5e-4 } = {}) {
656
+ if (!Number.isInteger(n) || n < 2)
657
+ throw new RangeError("Gully erosion requires grid resolution n >= 2");
658
+ if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS)
659
+ throw new RangeError(`Gully erosion iterations must be between 0 and ${MAX_ITERATIONS}`);
660
+ if (!Number.isFinite(capacity) || capacity < 0 || capacity > 1e6)
661
+ throw new RangeError("Gully erosion capacity must be between 0 and 1000000");
662
+ if (!Number.isFinite(rain) || rain < 0 || rain > 1)
663
+ throw new RangeError("Gully erosion rain must be between 0 and 1 metres per step");
664
+ const total = n * n;
665
+ let h = Float64Array.from(height);
666
+ let hTmp = new Float64Array(total);
667
+ const water = new Float64Array(total);
668
+ const waterBefore = new Float64Array(total);
669
+ const speed = new Float64Array(total);
670
+ const sediment = new Float64Array(total);
671
+ const sedTmp = new Float64Array(total);
672
+ const fluxL = new Float64Array(total);
673
+ const fluxR = new Float64Array(total);
674
+ const fluxT = new Float64Array(total);
675
+ const fluxB = new Float64Array(total);
676
+ const cell = size / (n - 1);
677
+ const cellArea = cell * cell;
678
+ const gain = STEP_SECONDS * GRAVITY * cell;
679
+ const evaporateKeep = Math.max(1 - EVAPORATE * STEP_SECONDS, 0);
680
+ const share = (flow, depth) => depth > 0 ? flow * STEP_SECONDS / (depth * cellArea) : 0;
681
+ for (let step = 0; step < iterations; step += 1) {
682
+ for (let z = 0; z < n; z += 1) {
683
+ for (let x = 0; x < n; x += 1) {
684
+ const i = z * n + x;
685
+ const depth = water[i] + rain;
686
+ const surface = h[i] + depth;
687
+ const ground = h[i];
688
+ const left = x > 0 ? h[i - 1] + water[i - 1] + rain : ground;
689
+ const right = x < n - 1 ? h[i + 1] + water[i + 1] + rain : ground;
690
+ const top = z > 0 ? h[i - n] + water[i - n] + rain : ground;
691
+ const bottom = z < n - 1 ? h[i + n] + water[i + n] + rain : ground;
692
+ let l = fluxL[i] * FLUX_KEEP + gain * (surface - left);
693
+ let r = fluxR[i] * FLUX_KEEP + gain * (surface - right);
694
+ let t = fluxT[i] * FLUX_KEEP + gain * (surface - top);
695
+ let b = fluxB[i] * FLUX_KEEP + gain * (surface - bottom);
696
+ if (l < 0) l = 0;
697
+ if (r < 0) r = 0;
698
+ if (t < 0) t = 0;
699
+ if (b < 0) b = 0;
700
+ const sum = l + r + t + b;
701
+ if (sum > 0) {
702
+ const keep = Math.min(1, depth * cellArea / (sum * STEP_SECONDS));
703
+ l *= keep;
704
+ r *= keep;
705
+ t *= keep;
706
+ b *= keep;
707
+ }
708
+ fluxL[i] = l;
709
+ fluxR[i] = r;
710
+ fluxT[i] = t;
711
+ fluxB[i] = b;
712
+ }
713
+ }
714
+ for (let z = 0; z < n; z += 1) {
715
+ for (let x = 0; x < n; x += 1) {
716
+ const i = z * n + x;
717
+ const inL = x > 0 ? fluxR[i - 1] : 0;
718
+ const inR = x < n - 1 ? fluxL[i + 1] : 0;
719
+ const inT = z > 0 ? fluxB[i - n] : 0;
720
+ const inB = z < n - 1 ? fluxT[i + n] : 0;
721
+ const before = water[i] + rain;
722
+ const after = Math.max(
723
+ 0,
724
+ before + STEP_SECONDS * (inL + inR + inT + inB - fluxL[i] - fluxR[i] - fluxT[i] - fluxB[i]) / cellArea
725
+ );
726
+ const depth = 0.5 * (before + after);
727
+ const acrossX = inL - fluxL[i] + fluxR[i] - inR;
728
+ const acrossZ = inT - fluxT[i] + fluxB[i] - inB;
729
+ speed[i] = depth > 1e-4 ? Math.sqrt(acrossX * acrossX + acrossZ * acrossZ) * 0.5 / (cell * depth) : 0;
730
+ water[i] = after;
731
+ waterBefore[i] = before;
732
+ }
733
+ }
734
+ for (let z = 0; z < n; z += 1) {
735
+ for (let x = 0; x < n; x += 1) {
736
+ const i = z * n + x;
737
+ const gx = (h[z * n + Math.min(x + 1, n - 1)] - h[z * n + Math.max(x - 1, 0)]) / (2 * cell);
738
+ const gy = (h[Math.min(z + 1, n - 1) * n + x] - h[Math.max(z - 1, 0) * n + x]) / (2 * cell);
739
+ const squared = gx * gx + gy * gy;
740
+ const sine = Math.max(Math.sqrt(squared / (1 + squared)), MIN_TILT);
741
+ const depth = water[i];
742
+ const transport = capacity * sine * speed[i] * Math.min(depth / DEPTH_REF, 1) * Math.max(1 - depth / DEPTH_MAX, 0);
743
+ let surface = h[i];
744
+ let load = sediment[i];
745
+ if (transport > load) {
746
+ const cut = DISSOLVE * (transport - load);
747
+ surface -= cut;
748
+ load += cut;
749
+ } else {
750
+ const drop = DEPOSIT * (load - transport);
751
+ surface += drop;
752
+ load -= drop;
753
+ }
754
+ hTmp[i] = surface;
755
+ sedTmp[i] = load;
756
+ }
757
+ }
758
+ const passed = h;
759
+ h = hTmp;
760
+ hTmp = passed;
761
+ for (let z = 0; z < n; z += 1) {
762
+ for (let x = 0; x < n; x += 1) {
763
+ const i = z * n + x;
764
+ let load = sedTmp[i] * (1 - share(
765
+ fluxL[i] + fluxR[i] + fluxT[i] + fluxB[i],
766
+ waterBefore[i]
767
+ ));
768
+ if (x > 0)
769
+ load += sedTmp[i - 1] * share(fluxR[i - 1], waterBefore[i - 1]);
770
+ if (x < n - 1)
771
+ load += sedTmp[i + 1] * share(fluxL[i + 1], waterBefore[i + 1]);
772
+ if (z > 0)
773
+ load += sedTmp[i - n] * share(fluxB[i - n], waterBefore[i - n]);
774
+ if (z < n - 1)
775
+ load += sedTmp[i + n] * share(fluxT[i + n], waterBefore[i + n]);
776
+ sediment[i] = load;
777
+ water[i] = water[i] * evaporateKeep;
778
+ }
779
+ }
780
+ }
781
+ return Float32Array.from(h, (surface, i) => surface + sediment[i]);
782
+ }
783
+
644
784
  // src/core/masks.ts
645
785
  var MATERIAL_IDS = Object.freeze([
646
786
  "grass",
@@ -764,6 +904,57 @@ function paintWeights(s, i, channel, weight) {
764
904
  s.splat[base + c] = s.splat[base + c] * (1 - w) + (c === channel ? w : 0);
765
905
  }
766
906
 
907
+ // src/core/scree.ts
908
+ var NEIGHBOURS = [
909
+ [-1, -1],
910
+ [0, -1],
911
+ [1, -1],
912
+ [-1, 0],
913
+ [1, 0],
914
+ [-1, 1],
915
+ [0, 1],
916
+ [1, 1]
917
+ ];
918
+ var DISTANCE = Float64Array.from(NEIGHBOURS, ([dx, dz]) => Math.hypot(dx, dz));
919
+ var MAX_ITERATIONS2 = 200;
920
+ function scree(height, n, size, { iterations = 200, talus = 35, rate = 0.05 } = {}) {
921
+ if (!Number.isInteger(n) || n < 2)
922
+ throw new RangeError("Scree slide requires grid resolution n >= 2");
923
+ if (!Number.isInteger(iterations) || iterations < 0 || iterations > MAX_ITERATIONS2)
924
+ throw new RangeError(`Scree slide iterations must be between 0 and ${MAX_ITERATIONS2}`);
925
+ if (!Number.isFinite(talus) || talus < 0 || talus > 89.9)
926
+ throw new RangeError("Scree slide talus must be between 0 and 89.9 degrees");
927
+ if (!Number.isFinite(rate) || rate < 0 || rate > 1)
928
+ throw new RangeError("Scree slide rate must be between 0 and 1");
929
+ const cell = size / (n - 1);
930
+ const limit = Float64Array.from(DISTANCE, (d) => Math.tan(talus * Math.PI / 180) * cell * d);
931
+ let source = Float64Array.from(height);
932
+ let target = new Float64Array(source.length);
933
+ for (let k = 0; k < iterations; k += 1) {
934
+ for (let z = 0; z < n; z += 1) {
935
+ for (let x = 0; x < n; x += 1) {
936
+ const i = z * n + x;
937
+ const here = source[i];
938
+ let received = 0;
939
+ for (let d = 0; d < 8; d += 1) {
940
+ const nx = x + NEIGHBOURS[d][0];
941
+ const nz = z + NEIGHBOURS[d][1];
942
+ if (nx < 0 || nz < 0 || nx >= n || nz >= n) continue;
943
+ const other = source[nz * n + nx];
944
+ const gap = limit[d];
945
+ if (other - here > gap) received += other - here - gap;
946
+ else if (here - other > gap) received -= here - other - gap;
947
+ }
948
+ target[i] = here + rate * received;
949
+ }
950
+ }
951
+ const swap = source;
952
+ source = target;
953
+ target = swap;
954
+ }
955
+ return Float32Array.from(source);
956
+ }
957
+
767
958
  // src/core/operations.ts
768
959
  var OPERATION_TYPES = Object.freeze([
769
960
  "noise",
@@ -1199,7 +1390,7 @@ function applyOperation(s, layer) {
1199
1390
  evaporation: p.evaporation
1200
1391
  },
1201
1392
  observations
1202
- ) : thermal(s.height, n, s.size, p, observations);
1393
+ ) : 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);
1203
1394
  let influence = mask;
1204
1395
  if (p.at || p.points) influence = brushWeights(s, p, mask, seed);
1205
1396
  for (let i = 0; i < s.height.length; i += 1) {
@@ -1353,6 +1544,7 @@ function finalizeState(s) {
1353
1544
  }
1354
1545
  const wet = (i, x, z, h) => {
1355
1546
  if (s.waters.some((water) => water.mask[i])) return true;
1547
+ if (s.waters.some((water) => water.kind === "ocean" && h < water.level + 0.35)) return true;
1356
1548
  for (const river of s.rivers) {
1357
1549
  const half = river.width / 2;
1358
1550
  for (let k = 1; k < river.points.length; k += 1) {
@@ -1523,7 +1715,8 @@ var PARAMS = Object.freeze({
1523
1715
  "capacity",
1524
1716
  "erosion",
1525
1717
  "deposition",
1526
- "evaporation"
1718
+ "evaporation",
1719
+ "rain"
1527
1720
  ],
1528
1721
  terrace: ["step", "softness", "strength", "offset"],
1529
1722
  materials: ["base", "rules", "reset"],
@@ -1831,7 +2024,8 @@ function validateLayer(layer) {
1831
2024
  "rate",
1832
2025
  "value",
1833
2026
  "waterWidth",
1834
- "maxGrade"
2027
+ "maxGrade",
2028
+ "rain"
1835
2029
  ])
1836
2030
  if (params[key] !== void 0) num(params[key], key, 0, 1);
1837
2031
  if (params.spacing !== void 0) num(params.spacing, "spacing", 0.01, 2);
@@ -1887,7 +2081,7 @@ function validateLayer(layer) {
1887
2081
  }
1888
2082
  }
1889
2083
  if (params.method !== void 0)
1890
- enumeration(params.method, "method", ["thermal", "hydraulic", "fluvial"]);
2084
+ enumeration(params.method, "method", ["thermal", "hydraulic", "fluvial", "gully", "scree"]);
1891
2085
  if (params.mode !== void 0) enumeration(params.mode, "mode", ["fbm", "ridged", "billow"]);
1892
2086
  if (params.shape !== void 0)
1893
2087
  enumeration(
@@ -2827,7 +3021,7 @@ function encodeGLB(input) {
2827
3021
  }
2828
3022
  const binary = join(chunks);
2829
3023
  const doc = {
2830
- asset: { version: "2.0", generator: "@threenative/terrain 0.1.8" },
3024
+ asset: { version: "2.0", generator: "@threenative/terrain 0.1.9" },
2831
3025
  scene: 0,
2832
3026
  scenes: [{ nodes: meshes.map((_, i) => i) }],
2833
3027
  nodes: meshes.map((m, i) => ({ mesh: i, name: m.name ?? `Terrain ${i}` })),
@@ -7268,6 +7462,129 @@ function validateSurfaces(input, assets) {
7268
7462
  return out;
7269
7463
  }
7270
7464
 
7465
+ // src/editor/tweak-box.ts
7466
+ var isColor = (knob) => "color" in knob;
7467
+ var hex = (value) => `#${value.toString(16).padStart(6, "0")}`;
7468
+ function valid(name, knob, value) {
7469
+ if (typeof value !== "number" || !Number.isFinite(value)) return false;
7470
+ if (isColor(knob)) return Number.isInteger(value) && value >= 0 && value <= 16777215;
7471
+ if (!(knob.min < knob.max)) throw new RangeError(`Tweak '${name}': min must be below max.`);
7472
+ return value >= knob.min && value <= knob.max;
7473
+ }
7474
+ function createTweakBox(options) {
7475
+ const names = Object.keys(options.knobs);
7476
+ const spec = (name) => options.knobs[name];
7477
+ const initial = (name) => {
7478
+ const knob = spec(name);
7479
+ const value = isColor(knob) ? knob.color : knob.value;
7480
+ if (!valid(name, knob, value)) throw new RangeError(`Tweak '${name}': default out of range.`);
7481
+ return value;
7482
+ };
7483
+ const host = options.host ?? {
7484
+ document: globalThis.document,
7485
+ storage: globalThis.localStorage,
7486
+ copy: (text3) => void globalThis.navigator?.clipboard?.writeText(text3)
7487
+ };
7488
+ const values = Object.fromEntries(names.map((name) => [name, initial(name)]));
7489
+ const key = options.storageKey;
7490
+ try {
7491
+ const stored = key ? JSON.parse(host.storage?.getItem(key) ?? "{}") : {};
7492
+ for (const name of names) {
7493
+ const value = stored?.[name];
7494
+ if (valid(name, spec(name), value)) values[name] = value;
7495
+ }
7496
+ } catch {
7497
+ }
7498
+ const inputs = /* @__PURE__ */ new Map();
7499
+ const show = (name) => {
7500
+ const input = inputs.get(name);
7501
+ if (input) input.value = isColor(spec(name)) ? hex(values[name]) : String(values[name]);
7502
+ const label = input?.previousElementSibling;
7503
+ if (label)
7504
+ label.textContent = `${name} ${isColor(spec(name)) ? hex(values[name]) : Number(values[name].toPrecision(6))}`;
7505
+ };
7506
+ const set = (name, value) => {
7507
+ if (!(name in options.knobs))
7508
+ throw new RangeError(`Tweak '${name}' is not a knob of this box.`);
7509
+ if (!valid(name, spec(name), value))
7510
+ throw new RangeError(`Tweak '${name}': ${value} is out of range.`);
7511
+ values[name] = value;
7512
+ if (key) host.storage?.setItem(key, JSON.stringify(values));
7513
+ show(name);
7514
+ options.onChange(name, value, values);
7515
+ };
7516
+ const snapshot = () => JSON.stringify(
7517
+ {
7518
+ tweaks: options.title,
7519
+ values: Object.fromEntries(
7520
+ names.map((name) => {
7521
+ const knob = spec(name);
7522
+ if (isColor(knob)) return [name, hex(values[name])];
7523
+ const step = knob.step ?? (knob.max - knob.min) / 100;
7524
+ return [name, Number((Math.round(values[name] / step) * step).toPrecision(6))];
7525
+ })
7526
+ )
7527
+ },
7528
+ null,
7529
+ 1
7530
+ );
7531
+ const reset = () => {
7532
+ if (key) host.storage?.removeItem(key);
7533
+ for (const name of names) {
7534
+ values[name] = initial(name);
7535
+ show(name);
7536
+ options.onChange(name, values[name], values);
7537
+ }
7538
+ };
7539
+ const page = host.document;
7540
+ const box = page?.createElement("div");
7541
+ if (page && box) {
7542
+ box.style.cssText = "position:fixed;top:8px;right:8px;z-index:9999;width:270px;max-height:94vh;overflow:auto;padding:8px 10px;background:#111c;color:#eee;font:11px/1.3 system-ui,sans-serif;border-radius:6px";
7543
+ const row = (text3, bold = false) => {
7544
+ const element2 = page.createElement("div");
7545
+ element2.textContent = text3;
7546
+ if (bold) element2.style.cssText = "font-weight:700;margin:8px 0 2px";
7547
+ return box.appendChild(element2);
7548
+ };
7549
+ row(options.title, true);
7550
+ const button = (text3, act) => {
7551
+ const element2 = page.createElement("button");
7552
+ element2.textContent = text3;
7553
+ element2.style.cssText = "margin:2px 6px 6px 0;padding:4px 10px;cursor:pointer";
7554
+ element2.addEventListener("click", act);
7555
+ box.appendChild(element2);
7556
+ };
7557
+ button("Copy snapshot", () => host.copy?.(snapshot()));
7558
+ button("Reset", reset);
7559
+ let group;
7560
+ for (const name of names) {
7561
+ const knob = spec(name);
7562
+ if (knob.group && knob.group !== group) row(knob.group, true);
7563
+ group = knob.group ?? group;
7564
+ row(name);
7565
+ const input = page.createElement("input");
7566
+ input.type = isColor(knob) ? "color" : "range";
7567
+ if (!isColor(knob)) {
7568
+ input.min = String(knob.min);
7569
+ input.max = String(knob.max);
7570
+ input.step = String(knob.step ?? (knob.max - knob.min) / 100);
7571
+ }
7572
+ input.style.cssText = "width:100%;margin:0 0 4px";
7573
+ input.addEventListener(
7574
+ "input",
7575
+ () => set(name, isColor(knob) ? Number.parseInt(input.value.slice(1), 16) : Number(input.value))
7576
+ );
7577
+ input.addEventListener("keydown", (event) => event.stopPropagation());
7578
+ box.appendChild(input);
7579
+ inputs.set(name, input);
7580
+ show(name);
7581
+ }
7582
+ page.body.appendChild(box);
7583
+ }
7584
+ for (const name of names) options.onChange(name, values[name], values);
7585
+ return { values, set, snapshot, reset, dispose: () => box?.remove() };
7586
+ }
7587
+
7271
7588
  // src/editor/environment.ts
7272
7589
  var SECTIONS = {
7273
7590
  sun: ["azimuth", "elevation", "intensity", "colour"],
@@ -7998,4 +8315,4 @@ async function mountTerrainEditor(options) {
7998
8315
  };
7999
8316
  }
8000
8317
 
8001
- export { SURFACE_CHANNELS, TerrainEditorController, checkEnvironmentAssets, createEditorSelection, editorPlacementCenter, editorPointToClient, editorSurfaceHeight, focusCamera, inspectImage, inspectSpatial, landformPose, mountTerrainEditor, pickEditorPlacement, pickEditorSurface, probeTerrain, runCameraOperation, runEnvironmentOperation, sniff, surfaceSpace, transformedLandform, validateAsset, validateAssets, validateCamera, validateCameras, validateEnvironment, validateSpatialReference, validateSurfaces };
8318
+ export { SURFACE_CHANNELS, TerrainEditorController, checkEnvironmentAssets, createEditorSelection, createTweakBox, editorPlacementCenter, editorPointToClient, editorSurfaceHeight, focusCamera, inspectImage, inspectSpatial, landformPose, mountTerrainEditor, pickEditorPlacement, pickEditorSurface, probeTerrain, runCameraOperation, runEnvironmentOperation, sniff, surfaceSpace, transformedLandform, validateAsset, validateAssets, validateCamera, validateCameras, validateEnvironment, validateSpatialReference, validateSurfaces };
@@ -1,3 +1,3 @@
1
1
  import 'vite';
2
- import '../types-CPYAc9vo.js';
3
- export { a as IAuthoringDocument, g as IEditorActivation, I as IEditorSnapshot, _ as TerrainEditorDocument, $ as terrainEditor } from '../server-C8Ydr_IM.js';
2
+ import '../types-BpH00qsx.js';
3
+ export { a as IAuthoringDocument, g as IEditorActivation, I as IEditorSnapshot, _ as TerrainEditorDocument, $ as terrainEditor } from '../server-B5RdvEAC.js';