@margoengine/terrain 0.0.0-stage → 0.1.0

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 (71) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1808 -3
  4. package/dist/blend.d.ts +70 -0
  5. package/dist/blend.js +159 -0
  6. package/dist/colors.d.ts +164 -0
  7. package/dist/deform.d.ts +103 -0
  8. package/dist/deform.js +322 -0
  9. package/dist/displace.d.ts +60 -0
  10. package/dist/displace.js +254 -0
  11. package/dist/dunes.d.ts +86 -0
  12. package/dist/dunes.js +88 -0
  13. package/dist/errors-fc4i1pSL.js +7 -0
  14. package/dist/grid.d.ts +11 -0
  15. package/dist/ground-change-settings.d.ts +29 -0
  16. package/dist/ground-change-settings.js +69 -0
  17. package/dist/ground-change.d.ts +227 -0
  18. package/dist/ground-change.js +1291 -0
  19. package/dist/ground-model.d.ts +290 -0
  20. package/dist/hdr-format-DRhzGS7G.js +30 -0
  21. package/dist/height-grid.d.ts +65 -0
  22. package/dist/height-grid.js +74 -0
  23. package/dist/heightfield.d.ts +38 -0
  24. package/dist/holes.d.ts +37 -0
  25. package/dist/holes.js +97 -0
  26. package/dist/imagery.d.ts +109 -0
  27. package/dist/imagery.js +472 -0
  28. package/dist/index.d.ts +297 -0
  29. package/dist/index.js +1 -0
  30. package/dist/layers-pbr.d.ts +78 -0
  31. package/dist/layers-pbr.js +128 -0
  32. package/dist/lighting-layout-CaDtDa5G.js +34 -0
  33. package/dist/lighting-layout.d.ts +5 -0
  34. package/dist/mesh-patch-BThCHCeS.js +21 -0
  35. package/dist/mesh-patch.d.ts +14 -0
  36. package/dist/noise3-sQdnbz4B.js +11 -0
  37. package/dist/noise3.d.ts +3 -0
  38. package/dist/overlays.d.ts +74 -0
  39. package/dist/overlays.js +297 -0
  40. package/dist/planet-craters.d.ts +55 -0
  41. package/dist/planet-recipes.d.ts +75 -0
  42. package/dist/planet-recipes.js +216 -0
  43. package/dist/planet.d.ts +139 -0
  44. package/dist/planet.js +602 -0
  45. package/dist/query.d.ts +150 -0
  46. package/dist/query.js +243 -0
  47. package/dist/relief-lighting.d.ts +46 -0
  48. package/dist/relief-lighting.js +208 -0
  49. package/dist/require-webgpu--tUXPsmA.js +7 -0
  50. package/dist/rock-settings.d.ts +39 -0
  51. package/dist/rock-settings.js +84 -0
  52. package/dist/rock-surface.d.ts +111 -0
  53. package/dist/rock-surface.js +368 -0
  54. package/dist/rocks.d.ts +258 -0
  55. package/dist/rocks.js +488 -0
  56. package/dist/rvt.d.ts +136 -0
  57. package/dist/rvt.js +518 -0
  58. package/dist/sculpt.d.ts +103 -0
  59. package/dist/sculpt.js +361 -0
  60. package/dist/settings-B9BSdnhD.js +72 -0
  61. package/dist/settings.d.ts +81 -0
  62. package/dist/settings.js +152 -0
  63. package/dist/shader.d.ts +61 -0
  64. package/dist/shared/settings.d.ts +89 -0
  65. package/dist/src-C6dtSaAb.js +1155 -0
  66. package/dist/stream.d.ts +100 -0
  67. package/dist/stream.js +339 -0
  68. package/dist/surface-height.d.ts +16 -0
  69. package/dist/wet-ground.d.ts +82 -0
  70. package/dist/wet-ground.js +147 -0
  71. package/package.json +148 -6
@@ -0,0 +1,39 @@
1
+ import { type SettingRow } from './shared/settings.js';
2
+ import type { RockLook, RockSurface } from './rock-surface.js';
3
+ import type { CliffOptions, SeaRockOptions } from './rocks.js';
4
+ /** A colour value: linear RGB. */
5
+ export type RockSettingColor = readonly [number, number, number];
6
+ /** One value of a setting: a number, a colour, or null where `nullable`. */
7
+ export type RockSettingValue = number | RockSettingColor | null;
8
+ /** Values by setting name; a name left out takes its default. */
9
+ export type RockSettingValues = Readonly<Record<string, RockSettingValue>>;
10
+ /** The panel a setting sits in. */
11
+ export type RockSettingGroup = 'detail' | 'moss' | 'tide' | 'wet' | 'cliff' | 'sea';
12
+ /** One control: the shared row shape with the rocks' groups. */
13
+ export interface RockSetting extends SettingRow {
14
+ readonly group: RockSettingGroup;
15
+ readonly default: RockSettingValue;
16
+ }
17
+ /** Every rock control, in panel order. Defaults are the values the rocks use without the option. */
18
+ export declare const ROCK_SETTINGS: readonly RockSetting[];
19
+ /** The rows that can change every frame (the look). */
20
+ export declare const ROCK_LIVE_SETTINGS: readonly RockSetting[];
21
+ /** Every setting at its default, as a fresh object to edit (a slider panel's state). */
22
+ export declare function rockSettingDefaults(): Record<string, RockSettingValue>;
23
+ /** The look the given live rows make (names left out, and shape rows, are not in it). */
24
+ export declare function rockLookOf(values: RockSettingValues): RockLook;
25
+ /**
26
+ * The options of `buildCliff` and `buildSeaRock` the given shape rows make (names left out keep the builders'
27
+ * defaults; look rows are not in them).
28
+ */
29
+ export declare function rockShapeOf(values: RockSettingValues): {
30
+ cliff: CliffOptions;
31
+ seaRock: SeaRockOptions;
32
+ };
33
+ /**
34
+ * Sets the given look rows on `surface` (names left out keep their current value); a number for `wet.rain` holds it
35
+ * against the weather, null hands it back. Nothing is rebuilt; shape rows are checked and left for `rockShapeOf`.
36
+ * @throws TypeError for an unknown name; RangeError for a value out of range.
37
+ * @example applyRockSettings(surface, { 'detail.bump': 1.4, 'wet.rain': null });
38
+ */
39
+ export declare function applyRockSettings(surface: RockSurface, values?: RockSettingValues): void;
@@ -0,0 +1,84 @@
1
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, t as s } from "./settings-B9BSdnhD.js";
2
+ //#region src/rock-settings.ts
3
+ var c = (e, t, n, r, i, o, s, c, l = !0) => a(e, t, n, r, i, o, s, c, l), l = (e, t, n, r) => i(e, t, n, 0, 1, r), u = (e, t, n, r, i, a, o, s) => c(e, t, n, r, i, a, o, s + " Rebuilds the meshes.", !1), d = [
4
+ c("detail.bump", "detail", 1, 0, 3, .01, "", "Strength of the fine relief that holds the rock up close."),
5
+ c("detail.scale", "detail", 1.6, .05, 50, .05, "m", "Wavelength of the largest fine relief; five finer octaves follow."),
6
+ c("detail.cracks", "detail", .6, 0, 1, .01, "", "How many dark joint cracks show."),
7
+ c("detail.variation", "detail", .5, 0, 1, .01, "", "Broad tone variation over the rock."),
8
+ c("detail.strata", "detail", .6, 0, 1, .01, "", "How strongly the beds differ in tone and the bedding lines show."),
9
+ c("detail.roughness", "detail", .85, .3, 1, .01, "", "Surface roughness of dry rock."),
10
+ c("moss.amount", "moss", .35, 0, 1, .01, "", "Moss or lichen on rock faces that look up."),
11
+ l("moss.color", "moss", [
12
+ .07,
13
+ .1,
14
+ .03
15
+ ], "Moss colour (grey-green lichen is about [0.2, 0.22, 0.16])."),
16
+ c("tide.height", "tide", 1.5, 0, 30, .05, "m", "Top of the wet tide band over each rock's sea level."),
17
+ c("tide.algae", "tide", .6, 0, 1, .01, "", "Dark algae and weed over the lower tide band."),
18
+ l("tide.color", "tide", [
19
+ .035,
20
+ .045,
21
+ .02
22
+ ], "Algae colour."),
23
+ c("wet.darkening", "wet", .6, 0, 1, .01, "", "Albedo of fully wet rock against dry (1 is no darkening)."),
24
+ c("wet.gloss", "wet", .4, 0, 1, .01, "", "Share by which wet rock is smoother (shinier)."),
25
+ { ...o(c("wet.rain", "wet", 0, 0, 1, .01, "", "Wetness from rain, 0 dry to 1 soaked. Null follows the weather."), "rainRate") },
26
+ u("cliff.overhang", "cliff", 2.5, 0, 100, .1, "m", "How far the top of a cliff face leans out past its foot."),
27
+ u("cliff.proud", "cliff", .6, 0, 20, .05, "m", "How far a cliff face stands in front of the heightfield."),
28
+ u("cliff.strata", "cliff", 1.4, 0, 100, .05, "m", "Bed thickness of the cliff strata (0 massive rock)."),
29
+ u("cliff.ledges", "cliff", .5, 0, 20, .05, "m", "How far hard beds stand out of soft ones."),
30
+ u("cliff.dip", "cliff", 0, -60, 60, 1, "degrees", "Tilt of the beds along the face."),
31
+ u("cliff.joints", "cliff", 3, 0, 200, .1, "m", "Spacing of the vertical joints (0 none)."),
32
+ u("cliff.crackDepth", "cliff", .35, 0, 20, .05, "m", "Depth of a joint crack."),
33
+ u("cliff.roughness", "cliff", .35, 0, 20, .05, "m", "Size of the rough bumps over the face."),
34
+ u("cliff.cell", "cliff", .5, .05, 20, .05, "m", "Polygonizer grid spacing (smaller is finer and slower)."),
35
+ u("sea.notchDepth", "sea", .6, 0, 20, .05, "m", "Depth of the wave-cut notch at the sea surface."),
36
+ u("sea.notchHeight", "sea", 1.2, .1, 20, .05, "m", "Height of the notch (the wave and tide band)."),
37
+ u("sea.lobes", "sea", .22, 0, 1, .01, "", "Lumpiness of a stack's outline, as a share of its radius."),
38
+ u("sea.strata", "sea", .9, 0, 100, .05, "m", "Bed thickness of the sea rocks."),
39
+ u("sea.joints", "sea", 7, 0, 64, 1, "", "Vertical joints around a sea rock."),
40
+ u("sea.roughness", "sea", .25, 0, 20, .05, "m", "Size of the rough bumps over a sea rock.")
41
+ ], f = e(d), p = r(d);
42
+ function m() {
43
+ return t(d);
44
+ }
45
+ var h = {
46
+ "detail.bump": "bump",
47
+ "detail.scale": "scale",
48
+ "detail.cracks": "cracks",
49
+ "detail.variation": "variation",
50
+ "detail.strata": "strata",
51
+ "detail.roughness": "roughness",
52
+ "moss.amount": "moss",
53
+ "moss.color": "mossColor",
54
+ "tide.height": "tideHeight",
55
+ "tide.algae": "algae",
56
+ "tide.color": "algaeColor",
57
+ "wet.darkening": "wetDarkening",
58
+ "wet.gloss": "wetGloss",
59
+ "wet.rain": "rain"
60
+ }, g = (e) => s(p, e, "rock", "ROCK_SETTINGS in @margoengine/terrain/rock-settings");
61
+ function _(e) {
62
+ g(e);
63
+ let t = {};
64
+ for (let [n, r] of Object.entries(e)) n in h && (t[h[n]] = Array.isArray(r) ? [...r] : r);
65
+ return t;
66
+ }
67
+ function v(e) {
68
+ g(e);
69
+ let t = {}, n = {};
70
+ for (let [r, i] of Object.entries(e)) {
71
+ let [e, a] = r.split(".");
72
+ e === "cliff" ? t[a] = i : e === "sea" && (n[a] = i);
73
+ }
74
+ return {
75
+ cliff: t,
76
+ seaRock: n
77
+ };
78
+ }
79
+ function y(e, t = {}) {
80
+ let r = _(t);
81
+ e.setLook(r), "rain" in r && n(e.look, "rain", r.rain !== null);
82
+ }
83
+ //#endregion
84
+ export { f as ROCK_LIVE_SETTINGS, d as ROCK_SETTINGS, y as applyRockSettings, _ as rockLookOf, m as rockSettingDefaults, v as rockShapeOf };
@@ -0,0 +1,111 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { EngineKernel, Mesh } from '@margoengine/core';
3
+ import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
4
+ /** A colour: linear RGB, 0 to 1. */
5
+ export type RockColor = readonly [number, number, number];
6
+ /** The look every rock of the surface shares; every field is live (`setLook`, or `applyRockSettings`). */
7
+ export interface RockLook {
8
+ /** Strength of the fine relief, 0 to 3. Default 1. */
9
+ bump?: number;
10
+ /** Wavelength of the largest relief, metres, 0.05 to 50 (five finer octaves follow, each half of the last). Default 1.6. */
11
+ scale?: number;
12
+ /** How many dark joint cracks show, 0 to 1. Default 0.6. */
13
+ cracks?: number;
14
+ /** Broad tone variation over the rock, 0 to 1. Default 0.5. */
15
+ variation?: number;
16
+ /** How strongly the beds differ in tone and the bedding lines show, 0 to 1. Default 0.6. */
17
+ strata?: number;
18
+ /** Surface roughness of dry rock, 0.3 to 1. Default 0.85. */
19
+ roughness?: number;
20
+ /** Moss or lichen on faces that look up, 0 to 1. Default 0.35. */
21
+ moss?: number;
22
+ /** Moss colour. Default [0.07, 0.1, 0.03]. */
23
+ mossColor?: RockColor;
24
+ /** Top of the wet tide band over each rock's sea level, metres, 0 to 30. Default 1.5. */
25
+ tideHeight?: number;
26
+ /** Dark algae and weed over the lower tide band, 0 to 1. Default 0.6. */
27
+ algae?: number;
28
+ /** Algae colour. Default [0.035, 0.045, 0.02]. */
29
+ algaeColor?: RockColor;
30
+ /** Albedo of fully wet rock against dry, 0 to 1 (1 is no darkening). Default 0.6. */
31
+ wetDarkening?: number;
32
+ /** Share by which wet rock is smoother, 0 to 1. Default 0.4. */
33
+ wetGloss?: number;
34
+ /** Wetness from rain, 0 dry to 1 soaked, or null to follow the weather (a `WeatherDrive` writes it). Default null. */
35
+ rain?: number | null;
36
+ }
37
+ /** The rock type of one mesh: its colours and bedding. Set when it is added; add it again to change it. */
38
+ export interface RockStyle {
39
+ /** A named rock type giving every field below (the fields given override it). Default 'sandstone'. */
40
+ type?: RockType;
41
+ /** Main bed colour, linear RGB. */
42
+ color?: RockColor;
43
+ /** Second bed colour, linear RGB. */
44
+ color2?: RockColor;
45
+ /** Bed thickness for the banding, metres, 0 (massive rock, no bands) to 100. */
46
+ strata?: number;
47
+ /** World height of the water this rock stands in (the tide band is over it). Default 0. */
48
+ seaLevel?: number;
49
+ /** How readily it soaks up rain, 0 to 1 (granite and basalt less than sandstone and chalk). */
50
+ porosity?: number;
51
+ /** Seed of its pattern, an unsigned 32-bit integer. Default 0. */
52
+ seed?: number;
53
+ }
54
+ /** The named rock types of {@link RockStyle.type}. */
55
+ export type RockType = 'sandstone' | 'limestone' | 'chalk' | 'granite' | 'basalt' | 'shale';
56
+ /** Colours (linear albedo), bed thickness and porosity of each rock type. */
57
+ export declare const ROCK_TYPES: Readonly<Record<RockType, Required<Omit<RockStyle, 'type' | 'seaLevel' | 'seed'>>>>;
58
+ /** The look `RockSurface` starts from. */
59
+ export declare const ROCK_LOOK_DEFAULTS: Readonly<Required<RockLook>>;
60
+ /**
61
+ * Rock shading for meshes on core's standard material: add each cliff, stack or boulder with its rock type, and every
62
+ * pixel of it is shaded as rock from its world position (relief, beds, cracks, moss, tide band, rain). It replaces the
63
+ * mesh material's colour, roughness and metalness; other meshes draw as they were.
64
+ */
65
+ export declare class RockSurface implements EnginePlugin {
66
+ readonly name: string;
67
+ /** Options it was made with (none yet; kept for the weather drive's target shape). */
68
+ readonly options: object;
69
+ /** The live look; write it through `setLook`. `look.rain` null follows the weather. */
70
+ readonly look: Required<RockLook>;
71
+ private engine?;
72
+ private disposed;
73
+ private readonly rows;
74
+ private dirty;
75
+ private uploaded;
76
+ private buffer?;
77
+ private capacity;
78
+ private source;
79
+ private shader;
80
+ private binding;
81
+ private entries;
82
+ constructor(look?: RockLook);
83
+ /** Shades rock on `engine` (a WebGPU engine, or WebGPU on the WebGL2 layer). */
84
+ static attach(engine: EngineKernel, look?: RockLook): RockSurface;
85
+ setup(engine: Engine): void;
86
+ /** Changes the look (fields left out keep their value). @throws RangeError for a value out of range. */
87
+ setLook(look: RockLook): this;
88
+ /**
89
+ * Shades `mesh` as rock of `style`. Adding it again changes its style. The mesh must belong to this engine and draw
90
+ * with core's standard material.
91
+ */
92
+ add(mesh: Mesh, style?: RockStyle): this;
93
+ /** Stops shading `mesh` as rock; false when it was not added. */
94
+ remove(mesh: Mesh): boolean;
95
+ /** Whether `mesh` is shaded as rock. */
96
+ has(mesh: Mesh): boolean;
97
+ /** The look as the five shared rows of the buffer (rain null is dry until a weather writes it). */
98
+ private globals;
99
+ /**
100
+ * The WGSL added to core's shader. `rockShade` returns the rock's colour (rgb) and roughness (a) and tilts the normal:
101
+ * triplanar weights |n|^4; per octave the three projections' value noise and its analytic slopes, summed as a world
102
+ * gradient, its tangential part (the surface gradient) taken off the normal; the beds from world height.
103
+ */
104
+ private code;
105
+ private patch;
106
+ private upload;
107
+ registerPasses(graph: FrameGraph): void;
108
+ onDeviceRestored(engine: Engine): void;
109
+ dispose(): void;
110
+ [Symbol.dispose]: () => void;
111
+ }
@@ -0,0 +1,368 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const n=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { n as e, t } from "./mesh-patch-BThCHCeS.js";
5
+ import { engineRuntime as __margoRuntime, meshRuntime as r } from "@margoengine/core/extension";
6
+ import { MargoError as i } from "@margoengine/math/errors";
7
+ //#region src/rock-surface.ts
8
+ var a = {
9
+ sandstone: {
10
+ color: [
11
+ .36,
12
+ .25,
13
+ .15
14
+ ],
15
+ color2: [
16
+ .45,
17
+ .33,
18
+ .21
19
+ ],
20
+ strata: 1.2,
21
+ porosity: .9
22
+ },
23
+ limestone: {
24
+ color: [
25
+ .42,
26
+ .4,
27
+ .35
28
+ ],
29
+ color2: [
30
+ .5,
31
+ .48,
32
+ .42
33
+ ],
34
+ strata: .8,
35
+ porosity: .6
36
+ },
37
+ chalk: {
38
+ color: [
39
+ .66,
40
+ .65,
41
+ .6
42
+ ],
43
+ color2: [
44
+ .58,
45
+ .57,
46
+ .53
47
+ ],
48
+ strata: 1.5,
49
+ porosity: 1
50
+ },
51
+ granite: {
52
+ color: [
53
+ .25,
54
+ .219,
55
+ .192
56
+ ],
57
+ color2: [
58
+ .36,
59
+ .316,
60
+ .266
61
+ ],
62
+ strata: 0,
63
+ porosity: .25
64
+ },
65
+ basalt: {
66
+ color: [
67
+ .07,
68
+ .07,
69
+ .075
70
+ ],
71
+ color2: [
72
+ .1,
73
+ .095,
74
+ .09
75
+ ],
76
+ strata: 0,
77
+ porosity: .3
78
+ },
79
+ shale: {
80
+ color: [
81
+ .16,
82
+ .15,
83
+ .14
84
+ ],
85
+ color2: [
86
+ .22,
87
+ .2,
88
+ .18
89
+ ],
90
+ strata: .25,
91
+ porosity: .5
92
+ }
93
+ }, o = {
94
+ sandstone: 0,
95
+ limestone: 0,
96
+ chalk: 0,
97
+ granite: 1,
98
+ basalt: 2,
99
+ shale: 3
100
+ }, s = {
101
+ bump: 1,
102
+ scale: 1.6,
103
+ cracks: .6,
104
+ variation: .5,
105
+ strata: .6,
106
+ roughness: .85,
107
+ moss: .35,
108
+ mossColor: [
109
+ .07,
110
+ .1,
111
+ .03
112
+ ],
113
+ tideHeight: 1.5,
114
+ algae: .6,
115
+ algaeColor: [
116
+ .035,
117
+ .045,
118
+ .02
119
+ ],
120
+ wetDarkening: .6,
121
+ wetGloss: .4,
122
+ rain: null
123
+ }, c = {
124
+ bump: [0, 3],
125
+ scale: [.05, 50],
126
+ cracks: [0, 1],
127
+ variation: [0, 1],
128
+ strata: [0, 1],
129
+ roughness: [.3, 1],
130
+ moss: [0, 1],
131
+ tideHeight: [0, 30],
132
+ algae: [0, 1],
133
+ wetDarkening: [0, 1],
134
+ wetGloss: [0, 1],
135
+ rain: [0, 1]
136
+ }, l = /* @__PURE__ */ new Set(["mossColor", "algaeColor"]), u = (e) => Array.isArray(e) && e.length === 3 && e.every((e) => typeof e == "number" && e >= 0 && e <= 1);
137
+ function d(e) {
138
+ if (typeof e != "object" || !e) throw RangeError("Rock look must be an object");
139
+ for (let [t, n] of Object.entries(e)) {
140
+ if (n === void 0) continue;
141
+ if (l.has(t)) {
142
+ if (!u(n)) throw RangeError(`Invalid rock look ${t}; pass linear RGB, three numbers 0 to 1`);
143
+ continue;
144
+ }
145
+ let e = c[t];
146
+ if (!e) throw TypeError(`Unknown rock look ${t}; use a field of RockLook`);
147
+ if ((t !== "rain" || n !== null) && (typeof n != "number" || !(n >= e[0] && n <= e[1]))) throw RangeError(`Invalid rock look ${t}; pass a number from ${e[0]} to ${e[1]}`);
148
+ }
149
+ return e;
150
+ }
151
+ function f(e) {
152
+ if (e !== void 0 && (typeof e != "object" || !e)) throw RangeError("Rock style must be an object");
153
+ let t = e?.type ?? "sandstone";
154
+ if (!Object.hasOwn(a, t)) throw RangeError(`Unknown rock type ${String(t)}; use one of ${Object.keys(a).join(", ")}`);
155
+ let n = a[t], r = e?.color ?? n.color, i = e?.color2 ?? n.color2, s = e?.strata ?? n.strata, c = e?.porosity ?? n.porosity, l = e?.seaLevel ?? 0, d = e?.seed ?? 0;
156
+ if (!u(r) || !u(i) || typeof s != "number" || !(s >= 0 && s <= 100) || typeof c != "number" || !(c >= 0 && c <= 1) || typeof l != "number" || !Number.isFinite(Math.fround(l)) || !Number.isInteger(d) || d < 0 || d > 4294967295) throw RangeError("Invalid rock style; colours are linear RGB 0 to 1, strata 0 to 100 m, porosity 0 to 1, seaLevel finite, seed a uint32");
157
+ return new Float32Array([
158
+ ...r,
159
+ 1,
160
+ ...i,
161
+ s,
162
+ l,
163
+ c,
164
+ d % 65521 * .6180339,
165
+ o[t]
166
+ ]);
167
+ }
168
+ var p = 0, m = 5, h = 3, g = class a {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
169
+ constructor(e = {}) {
170
+ this.name = "terrain:rocks:" + ++p, this.options = {}, this.disposed = !1, this.rows = /* @__PURE__ */ new Map(), this.dirty = !0, this.uploaded = "", this.capacity = 0, this.source = "", this.shader = "", this.binding = 40, this.entries = [], this.look = {
171
+ ...s,
172
+ ...d(e)
173
+ }, this.look.mossColor = [...this.look.mossColor], this.look.algaeColor = [...this.look.algaeColor];
174
+ }
175
+ static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
176
+ let r = new a(t);
177
+ return n(e).use(r), r;
178
+ }
179
+ setup(e) {
180
+ if (this.disposed || !e.gpu) throw new i(this.disposed ? "disposed" : "unsupported", "Rock shading requires WebGPU and a live module; attach a new RockSurface to a WebGPU engine; see https://margoengine.com/errors#webgpu");
181
+ this.engine = e;
182
+ }
183
+ setLook(e) {
184
+ d(e);
185
+ for (let [t, n] of Object.entries(e)) n !== void 0 && (this.look[t] = Array.isArray(n) ? [...n] : n);
186
+ return this;
187
+ }
188
+ add(e, t) {
189
+ if (this.disposed) throw new i("disposed", "This RockSurface is disposed; attach a new one");
190
+ let n = r(e);
191
+ if (n.check(), this.engine && n.engine !== this.engine) throw new i("attached", "The mesh belongs to another engine; add meshes of the surface's engine");
192
+ return this.rows.set(e, {
193
+ id: n.id,
194
+ row: f(t)
195
+ }), this.dirty = !0, this;
196
+ }
197
+ remove(e) {
198
+ let t = this.rows.delete(e);
199
+ return t && (this.dirty = !0), t;
200
+ }
201
+ has(e) {
202
+ return this.rows.has(e);
203
+ }
204
+ globals() {
205
+ let e = this.look;
206
+ return new Float32Array([
207
+ e.bump,
208
+ e.scale,
209
+ e.cracks,
210
+ e.variation,
211
+ e.strata,
212
+ e.moss,
213
+ e.tideHeight,
214
+ e.algae,
215
+ ...e.mossColor,
216
+ e.roughness,
217
+ ...e.algaeColor,
218
+ e.wetDarkening,
219
+ e.wetGloss,
220
+ e.rain ?? 0,
221
+ 0,
222
+ 0
223
+ ]);
224
+ }
225
+ code(e) {
226
+ return `
227
+ @group(0) @binding(${e}) var<storage,read> rockRows:array<vec4f>;
228
+ fn rockRow(id:f32,k:u32)->vec4f {
229
+ let i=${m}u+u32(max(id-1.0,0.0))*${h}u+k;if(id<1.0||i>=arrayLength(&rockRows)){return vec4f(0);}return rockRows[i];
230
+ }
231
+ fn rockHash(x:f32)->f32 {return f32(terrainHash(bitcast<u32>(i32(floor(x)))+0x9e3779b9u)>>8u)*5.9604645e-8;}
232
+ struct RockPlanar {value:f32,gradient:vec3f}
233
+ fn rockPlanar(p:vec3f,w:vec3f,f:f32,o:f32)->RockPlanar {
234
+ let a=terrainNoise(p.yz*f+o);let b=terrainNoise(p.xz*f+o*1.7);let c=terrainNoise(p.xy*f+o*2.3);
235
+ var r:RockPlanar;r.value=w.x*a.x+w.y*b.x+w.z*c.x;
236
+ r.gradient=f*(w.x*vec3f(0,a.y,a.z)+w.y*vec3f(b.y,0,b.z)+w.z*vec3f(c.y,c.z,0));return r;
237
+ }
238
+ struct RockShade {color:vec3f,rough:f32,normal:vec3f}
239
+ fn rockShade(p:vec3f,n0:vec3f,id:f32,footprint:f32)->RockShade {
240
+ let m0=rockRow(id,0u);let m1=rockRow(id,1u);let m2=rockRow(id,2u);
241
+ let g0=rockRows[0];let g1=rockRows[1];let g2=rockRows[2];let g3=rockRows[3];let g4=rockRows[4];
242
+ var w=pow(abs(n0),vec3f(4));w/=max(w.x+w.y+w.z,1e-6);
243
+ var grad=vec3f(0);var detail=0.0;var amp=1.0;var f=1.0/g0.y;var total=0.0;
244
+ for(var o=0;o<6;o++){
245
+ let keep=clamp(2.0-3.0*footprint*f,0.0,1.0);
246
+ let s=rockPlanar(p,w,f,m2.z+f32(o)*7.31);
247
+ grad+=s.gradient*(0.12*g0.y*amp*keep);detail+=(s.value-0.5)*amp*keep;total+=amp;amp*=0.6;f*=2.0;
248
+ }
249
+ detail/=total;
250
+ let broad=rockPlanar(p,w,0.11/g0.y,m2.z+3.0).value;
251
+
252
+ let cf=0.9/g0.y;let cn=rockPlanar(p,w,cf,m2.z+11.0);let ridge=1.0-abs(2.0*cn.value-1.0);
253
+ let pieces=smoothstep(0.5,0.7,rockPlanar(p,w,0.37/g0.y,m2.z+5.0).value);
254
+ let crack=smoothstep(0.94,0.99,ridge)*pieces*g0.z*clamp(2.0-3.0*footprint*cf,0.0,1.0);
255
+ var col=m0.rgb;
256
+ if(m1.w>0.0){
257
+ let b=(p.y+0.5*m1.w*(terrainNoise(p.xz*0.05/max(m1.w,0.1)+m2.z).x-0.5))/m1.w;
258
+ col=mix(m0.rgb,m1.rgb,rockHash(b+m2.z)*g1.x);
259
+ let line=1.0-smoothstep(0.0,0.07,fract(b));
260
+ col*=1.0-0.35*g1.x*line*clamp(2.0-6.0*footprint/m1.w,0.0,1.0);
261
+ }
262
+ let tone=rockPlanar(p,w,0.31/g0.y,m2.z+17.0).value;
263
+ col=mix(col,m1.rgb,g0.w*smoothstep(0.3,0.8,tone)*0.6);
264
+ col*=1.0+g0.w*(broad-0.5)*1.2+0.6*detail;
265
+ col*=1.0-0.55*crack;
266
+
267
+
268
+ var fabric=0.0;
269
+ let gf=select(21.0,13.0,m2.w==1.0)/g0.y;
270
+ let keepGrain=clamp(2.0-4.0*footprint*gf,0.0,1.0);
271
+ if(keepGrain>0.0){
272
+ let grain=rockPlanar(p,w,gf,m2.z+53.79);
273
+ if(m2.w==1.0){
274
+ let quartz=smoothstep(0.53,0.77,grain.value);
275
+ let mica=1.0-smoothstep(0.22,0.43,grain.value);
276
+ fabric=(0.44*quartz-0.57*mica)*keepGrain;
277
+ grad+=grain.gradient*(0.11/gf)*keepGrain;
278
+ }else if(m2.w==2.0){
279
+ let pit=1.0-smoothstep(0.20,0.36,grain.value);
280
+ fabric=-0.29*pit*keepGrain;
281
+ grad+=grain.gradient*(0.35/gf)*pit*keepGrain;
282
+ }else if(m2.w==3.0&&m1.w>0.0){
283
+ let lf=5.0/m1.w;
284
+ let bed=p.y*lf+(grain.value-0.5)*0.26+m2.z;
285
+ let seam=1.0-smoothstep(0.015,0.22,abs(fract(bed)-0.5));
286
+ fabric=-0.32*seam*g1.x*clamp(2.0-6.0*footprint*lf,0.0,1.0)*keepGrain;
287
+ }else{
288
+ fabric=0.22*(grain.value-0.5)*keepGrain;
289
+ }
290
+ }
291
+ col*=1.0+fabric;
292
+ let gt=grad-n0*dot(n0,grad);
293
+ let n=normalize(n0-g0.x*gt);
294
+ var rough=g2.w*(1.0+0.1*detail)-0.15*fabric+0.1*crack;
295
+ let above=p.y-m2.x;
296
+
297
+ let moss=g1.y*smoothstep(0.5,0.85,n.y)*smoothstep(0.35,0.6,broad+0.2*detail)*smoothstep(g1.z,g1.z+0.6,above);
298
+ col=mix(col,g2.rgb*(0.85+0.3*broad),moss);rough=mix(rough,0.95,moss);
299
+ let tideTop=g1.z*(1.0+0.25*(broad-0.5));
300
+ var tide=0.0;
301
+ if(tideTop>0.0){
302
+ tide=1.0-smoothstep(tideTop*0.8,tideTop+0.05,above);
303
+ let algae=g1.w*(1.0-smoothstep(0.0,tideTop*0.55,above))*smoothstep(0.3,0.55,broad+0.3*detail);
304
+ col=mix(col,g3.rgb,algae*tide);
305
+ }
306
+ let wet=max(tide,g4.y*m2.y);
307
+ col*=mix(1.0,g3.w,wet);rough=mix(rough,rough*(1.0-g4.x),wet);
308
+ var r:RockShade;r.color=col;r.rough=clamp(rough,0.05,1.0);r.normal=n;return r;
309
+ }
310
+ `;
311
+ }
312
+ patch(n, r) {
313
+ let i = e(n);
314
+ if (!i) return n;
315
+ let { code: a, at: o } = i;
316
+ return (a.includes("fn terrainNoise(") ? "" : t) + this.code(r) + a.slice(0, o) + "let rockFootprint=max(length(dpdx(i.position)),length(dpdy(i.position)));{if(rockRow(i.terrainId,0u).w>0.0){let r=rockShade(i.position,n,i.terrainId,rockFootprint);i.color=r.color;i.material.x=r.rough;i.material.y=0.0;n=r.normal;}}" + a.slice(o);
317
+ }
318
+ upload(e) {
319
+ let t = 0;
320
+ for (let e of this.rows.values()) t = Math.max(t, e.id + 1);
321
+ (!this.buffer || t > this.capacity) && (this.buffer?.destroy(), this.capacity = Math.max(16, 2 ** Math.ceil(Math.log2(Math.max(t, 1)))), this.buffer = e.createBuffer({
322
+ size: (m + this.capacity * h) * 16,
323
+ usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
324
+ }), this.entries = []);
325
+ let n = new Float32Array((m + this.capacity * h) * 4);
326
+ n.set(this.globals());
327
+ for (let e of this.rows.values()) n.set(e.row, (m + e.id * h) * 4);
328
+ e.queue.writeBuffer(this.buffer, 0, n), this.dirty = !1;
329
+ }
330
+ registerPasses(e) {
331
+ e.add({
332
+ name: this.name,
333
+ order: -.5,
334
+ execute: (e) => {
335
+ if (e.backend !== "webgpu" || !this.engine || this.disposed || this.rows.size === 0) return;
336
+ for (let e of this.rows.keys()) r(e).engine !== this.engine && (this.rows.delete(e), this.dirty = !0);
337
+ if (this.rows.size === 0) return;
338
+ let t = JSON.stringify(this.look);
339
+ if ((this.dirty || t !== this.uploaded) && (this.uploaded = t, this.upload(e.device)), this.source !== e.shader) {
340
+ this.source = e.shader;
341
+ let t = new Set(e.bindings.map((e) => e.binding));
342
+ for (let n of e.shader.matchAll(/@group\(0\) @binding\((\d+)\)/g)) t.add(Number(n[1]));
343
+ let n = 40;
344
+ for (; t.has(n);) n++;
345
+ this.binding = n, this.entries = [], this.shader = this.patch(e.shader, n);
346
+ }
347
+ this.shader !== this.source && (this.entries.length || (this.entries = [{
348
+ binding: this.binding,
349
+ resource: { buffer: this.buffer }
350
+ }]), e.bindings.push(...this.entries), e.shader = this.shader);
351
+ }
352
+ });
353
+ }
354
+ onDeviceRestored(e) {
355
+ this.buffer?.destroy(), this.buffer = void 0, this.capacity = 0, this.entries = [], this.dirty = !0, this.engine = void 0, this.setup(e);
356
+ }
357
+ dispose() {
358
+ if (this.disposed) return;
359
+ this.disposed = !0, this.buffer?.destroy(), this.buffer = void 0, this.entries = [], this.rows.clear();
360
+ let e = this.engine;
361
+ this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
362
+ }
363
+ [Symbol.dispose ?? Symbol()]() {
364
+ this.dispose();
365
+ }
366
+ };
367
+ //#endregion
368
+ export { s as ROCK_LOOK_DEFAULTS, a as ROCK_TYPES, g as RockSurface };