@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,1291 @@
1
+ // @margo/core-built 0.1.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+ const i=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { t as e } from "./require-webgpu--tUXPsmA.js";
5
+ import { n as t, t as n } from "./mesh-patch-BThCHCeS.js";
6
+ import { i as r } from "./settings-B9BSdnhD.js";
7
+ import { engineRuntime as __margoRuntime, meshRuntime as a } from "@margoengine/core/extension";
8
+ import { MargoError as o } from "@margoengine/math/errors";
9
+ //#region src/ground-model.ts
10
+ var s = {
11
+ gully: 60,
12
+ bank: 60,
13
+ scour: 30,
14
+ slide: 12,
15
+ mud: 90,
16
+ ash: 120,
17
+ lava: 120,
18
+ burn: 600,
19
+ crater: 6
20
+ }, c = [
21
+ "mud",
22
+ "ash",
23
+ "char",
24
+ "lava",
25
+ "heat",
26
+ "wet",
27
+ "soil",
28
+ "ashen"
29
+ ];
30
+ function l(e, t, n) {
31
+ let r = e.at, i = r.length;
32
+ if (n <= 0) return t[0];
33
+ if (n >= e.length) return t[i - 1];
34
+ let a = 0, o = i - 1;
35
+ for (; o - a > 1;) {
36
+ let e = a + o >> 1;
37
+ r[e] <= n ? a = e : o = e;
38
+ }
39
+ let s = r[o] - r[a], c = s > 0 ? (n - r[a]) / s : 0;
40
+ return t[a] + (t[o] - t[a]) * c;
41
+ }
42
+ function u(e, t, n, r, i) {
43
+ if (!(e > 0)) return !1;
44
+ let a = Math.floor(n / e), o = Math.imul(a, 2) + Math.imul(t, 2654435761) >>> 0, s = (w(o) >>> 8) / 16777216;
45
+ return (w(o + 1 >>> 0) >>> 8) / 16777216 >= .75 ? !1 : ((n - (a + .15 + .7 * s) * e) / (.8 * i)) ** 2 + (r / .5) ** 2 < 1;
46
+ }
47
+ function d(e, t) {
48
+ let n = e.at, r = 0, i = n.length - 1;
49
+ for (; i - r > 1;) {
50
+ let e = r + i >> 1;
51
+ n[e] <= t ? r = e : i = e;
52
+ }
53
+ return r;
54
+ }
55
+ function f(e, t, n) {
56
+ if (n >= t[t.length - 1]) return e.length;
57
+ if (n <= t[0]) return 0;
58
+ let r = 1;
59
+ for (; t[r] <= n;) r++;
60
+ let i = t[r] - t[r - 1];
61
+ return e.at[r - 1] + (i > 0 ? (n - t[r - 1]) / i : 1) * (e.at[r] - e.at[r - 1]);
62
+ }
63
+ var p = (e) => typeof e == "number" && Number.isFinite(e), m = (e) => Array.isArray(e) && e.length === 2 && p(e[0]) && p(e[1]), h = (e) => e < 0 ? 0 : e > 1 ? 1 : e, g = (e, t, n) => {
64
+ let r = h((n - e) / (t - e));
65
+ return r * r * (3 - 2 * r);
66
+ };
67
+ function _(e, t) {
68
+ throw RangeError(`Invalid ground ${e} event; ${t}`);
69
+ }
70
+ function v(e, t, n, r, i, a = !1) {
71
+ let o = e[t] ?? n;
72
+ return (!p(o) || o < r || o > i || a && o === r) && _(String(e.kind), `${t} must be a number ${a ? "above" : "from"} ${r}${i < Infinity ? ` to ${i}` : ""}.`), o;
73
+ }
74
+ function y(e, t, n) {
75
+ (!Array.isArray(n) || n.length < 2 || !n.every(m)) && _(e, `${t} must be 2 or more [x, z] points.`);
76
+ let r = new Float64Array(n.length * 2), i = new Float64Array(n.length);
77
+ return n.forEach((e, t) => {
78
+ r[t * 2] = e[0], r[t * 2 + 1] = e[1], t && (i[t] = i[t - 1] + Math.hypot(e[0] - n[t - 1][0], e[1] - n[t - 1][1]));
79
+ }), i[i.length - 1] > 0 || _(e, `${t} must have some length.`), {
80
+ points: r,
81
+ at: i,
82
+ length: i[i.length - 1]
83
+ };
84
+ }
85
+ function b(e, t) {
86
+ let n = Infinity, r = Infinity, i = -Infinity, a = -Infinity;
87
+ for (let t = 0; t < e.points.length; t += 2) n = Math.min(n, e.points[t]), i = Math.max(i, e.points[t]), r = Math.min(r, e.points[t + 1]), a = Math.max(a, e.points[t + 1]);
88
+ return [
89
+ n - t,
90
+ r - t,
91
+ i + t,
92
+ a + t
93
+ ];
94
+ }
95
+ var x = {
96
+ s: 0,
97
+ d: 0,
98
+ side: 0,
99
+ end: 0
100
+ };
101
+ function S(e, t, n, r = x) {
102
+ let i = e.points, a = Infinity;
103
+ for (let o = 0; o + 3 < i.length; o += 2) {
104
+ let s = i[o], c = i[o + 1], l = i[o + 2] - s, u = i[o + 3] - c, d = l * l + u * u, f = d > 0 ? ((t - s) * l + (n - c) * u) / d : 0, p = f < 0 ? 0 : f > 1 ? 1 : f, m = t - s - l * p, h = n - c - u * p, g = m * m + h * h;
105
+ g < a && (a = g, r.s = e.at[o / 2] + p * Math.sqrt(d), r.side = l * h - u * m < 0 ? 1 : -1, r.end = f < 0 && o === 0 ? -1 : +(f > 1 && o + 4 === i.length));
106
+ }
107
+ return r.d = Math.sqrt(a), r;
108
+ }
109
+ function C(e, t) {
110
+ let n = e.points, r = 0;
111
+ for (; r + 2 < e.at.length && e.at[r + 1] < t;) r++;
112
+ let i = n[r * 2], a = n[r * 2 + 1], o = n[r * 2 + 2] - i, s = n[r * 2 + 3] - a, c = Math.hypot(o, s) || 1, l = (t - e.at[r]) / c;
113
+ return [
114
+ i + o * l,
115
+ a + s * l,
116
+ o / c,
117
+ s / c
118
+ ];
119
+ }
120
+ var w = (e) => {
121
+ let t = e >>> 0;
122
+ return t ^= t >>> 16, t = Math.imul(t, 2146121005), t ^= t >>> 15, t = Math.imul(t, 2221713035), t ^= t >>> 16, t >>> 0;
123
+ };
124
+ function T(e, t, n, r) {
125
+ let i = n / 11, a = Math.floor(e / i), o = Math.floor(t / i), s = 0;
126
+ for (let n = a - 1; n <= a + 1; n++) for (let a = o - 1; a <= o + 1; a++) {
127
+ let o = w(w(n + Math.imul(a, 668265261) + Math.imul(r, 2654435761) >>> 0));
128
+ if ((o & 65535) / 65536 > .45) continue;
129
+ let c = (o >>> 16 & 255) / 256, l = (o >>> 24) / 256, u = i * (.12 + .18 * c), d = (n + .5 + (l - .5) * .6) * i, f = (a + .5 + (c - .5) * .6) * i, p = Math.max(Math.abs(e - d), Math.abs(t - f)) / u;
130
+ p < 1 && (s = Math.max(s, .8 * u * Math.min(1, 4 * (1 - p))));
131
+ }
132
+ return s;
133
+ }
134
+ function E(e, t, n, r) {
135
+ let i = n / 6, a = Math.floor(e / i), o = Math.floor(t / i), s = w(w(a + Math.imul(o, 374761393) + Math.imul(r + 5, 2654435761) >>> 0));
136
+ if ((s & 65535) / 65536 > .3) return 0;
137
+ let c = i * (.1 + .15 * ((s >>> 16 & 255) / 256)), l = Math.hypot(e - (a + .5) * i, t - (o + .5) * i) / c;
138
+ return l < 1 ? -.35 * c * (1 - l * l) + .05 * c * g(.8, 1, l) : l < 1.3 ? .05 * c * g(1.3, 1, l) : 0;
139
+ }
140
+ function D(e, t, n) {
141
+ let r = Math.floor(e), i = Math.floor(t), a = e - r, o = t - i, s = a * a * (3 - 2 * a), c = o * o * (3 - 2 * o), l = w(i + Math.imul(n, 2654435761) >>> 0), u = w(i + 1 + Math.imul(n, 2654435761) >>> 0), d = (e, t) => (w(e + t >>> 0) >>> 8) / 16777216, f = d(r, l) + (d(r + 1, l) - d(r, l)) * s;
142
+ return f + (d(r, u) + (d(r + 1, u) - d(r, u)) * s - f) * c;
143
+ }
144
+ var O = (e, t, n, r) => D(e / n, t / n, r) * .67 + D(e * 2.1 / n, t * 2.1 / n, r + 7) * .33 - .5, k = (e, t) => h((t - e.start) / e.duration), A = (e) => (1 - Math.exp(-5 * e)) / (1 - Math.exp(-5)), j = (e) => 1 - (1 - e) * (1 - e);
145
+ function M(e) {
146
+ let t = e * 3.6;
147
+ return 1 + 8.729 * (1 - Math.exp(-.03 * t)) ** 2.155;
148
+ }
149
+ var N = (e) => .0779 * Math.sqrt(Math.max(e, 0) / 3600), P = {
150
+ v: .5,
151
+ u: .8
152
+ }, F = (e, t) => e >= 1 ? 0 : t ? 1 - e * e * e * e : 1 - e;
153
+ function I(e) {
154
+ let t = Math.sqrt(Math.max(e * e - 1, 0)), n = e > 1 ? (e - t) / (e + t) : 1, r = (1 + n) / 2;
155
+ return {
156
+ a: r,
157
+ b: r / e,
158
+ c: r - n
159
+ };
160
+ }
161
+ function L(e, t, n) {
162
+ if (typeof e != "object" || !e || !(e.kind in s)) throw TypeError(`Invalid ground event; kind must be one of ${Object.keys(s).join(", ")}.`);
163
+ let r = e, i = e.kind, a = v(r, "start", 0, 0, 1e9), o = v(r, "duration", s[i], 0, 1e9, !0), c = v(r, "roughness", .3, 0, 1), l = r.seed ?? 1;
164
+ Number.isInteger(l) || _(i, "seed must be an integer.");
165
+ let u = {
166
+ roughness: c,
167
+ seed: l
168
+ }, d = a + o, f = (n, r, s = {}) => ({
169
+ kind: i,
170
+ start: a,
171
+ duration: o,
172
+ settle: r,
173
+ bounds: n,
174
+ order: t,
175
+ source: e,
176
+ o: u,
177
+ ...s
178
+ }), h = (e, t) => {
179
+ let n = r[e] ?? t;
180
+ return n !== "left" && n !== "right" && _(i, `${e} must be 'left' or 'right'.`), n === "left" ? 1 : -1;
181
+ };
182
+ switch (i) {
183
+ case "gully": {
184
+ let e = y(i, "path", r.path);
185
+ u.width = v(r, "width", 4, 0, 1e5, !0), u.depth = v(r, "depth", 1.5, 0, 1e4, !0);
186
+ let t = r.shape ?? "v";
187
+ t !== "v" && t !== "u" && _(i, "shape must be 'v' or 'u'."), u.shape = +(t === "u"), u.deposit = v(r, "deposit", .6, 0, 1), u.fan = v(r, "fan", 3 * u.width, 0, 1e5, !0);
188
+ let n = C(e, 0);
189
+ return f(b(e, Math.max(u.width, u.fan) * 1.6), d, {
190
+ line: e,
191
+ dir: [-n[2], -n[3]]
192
+ });
193
+ }
194
+ case "bank": {
195
+ let e = y(i, "path", r.path);
196
+ return u.river = h("river", "left"), u.retreat = v(r, "retreat", 6, 0, 1e5, !0), p(r.bed) || _(i, "bed (world Y of the channel bed) is required."), u.bed = r.bed, u.face = v(r, "face", 60, 10, 89) * Math.PI / 180, f(b(e, (u.retreat + Math.max(n - u.bed, 0) / Math.tan(u.face)) * 1.3 + 1), d, { line: e });
197
+ }
198
+ case "scour": {
199
+ m(r.center) || _(i, "center must be an [x, z] point."), u.radius = v(r, "radius", 5, 0, 1e5, !0), u.depth = v(r, "depth", 2, 0, 1e4, !0), u.deposit = v(r, "deposit", .5, 0, 1);
200
+ let e = r.center, t;
201
+ if (r.flow !== void 0) {
202
+ (!m(r.flow) || !(Math.hypot(r.flow[0], r.flow[1]) > 0)) && _(i, "flow must be a non-zero [x, z].");
203
+ let e = r.flow, n = Math.hypot(e[0], e[1]);
204
+ t = [e[0] / n, e[1] / n];
205
+ }
206
+ let n = u.radius * (t ? 3.4 : 1.4);
207
+ return f([
208
+ e[0] - n,
209
+ e[1] - n,
210
+ e[0] + n,
211
+ e[1] + n
212
+ ], d, { dir: t });
213
+ }
214
+ case "crater": {
215
+ m(r.center) || _(i, "center must be an [x, z] point."), u.radius = v(r, "radius", 30, 0, 1e5, !0), u.depth = v(r, "depth", 10, 0, 1e4, !0), u.heave = v(r, "heave", .4 * u.depth, 0, 1e4), u.heaveTime = v(r, "heaveTime", u.heave > 0 ? Math.sqrt(2 * u.heave / 9.80665) : .001, 0, 60, !0), u.domeSpread = v(r, "domeSpread", 1.3, 1, 3), u.ejecta = v(r, "ejecta", .7, 0, 1.5), u.rim = u.ejecta * u.depth / (.704 / .992 * 4);
216
+ let e = r.center, t = u.radius * 5.5;
217
+ return f([
218
+ e[0] - t,
219
+ e[1] - t,
220
+ e[0] + t,
221
+ e[1] + t
222
+ ], d + u.heaveTime);
223
+ }
224
+ case "slide": {
225
+ (!m(r.from) || !m(r.to)) && _(i, "from and to must be [x, z] points.");
226
+ let e = r.from, t = r.to, n = Math.hypot(t[0] - e[0], t[1] - e[1]);
227
+ n > 0 || _(i, "from and to must differ."), u.width = v(r, "width", 20, 0, 1e5, !0), u.depth = v(r, "depth", 3, 0, 1e4, !0), u.length = v(r, "length", n * .4, 0, n, !0), u.bulking = v(r, "bulking", 1.15, 1, 2), u.distance = n;
228
+ let a = u.width;
229
+ return f([
230
+ Math.min(e[0], t[0]) - a,
231
+ Math.min(e[1], t[1]) - a,
232
+ Math.max(e[0], t[0]) + a,
233
+ Math.max(e[1], t[1]) + a
234
+ ], d, { dir: [(t[0] - e[0]) / n, (t[1] - e[1]) / n] });
235
+ }
236
+ case "mud": {
237
+ let e = y(i, "shore", r.shore);
238
+ return u.land = h("land", "left"), u.reach = v(r, "reach", 400, 0, 1e6, !0), u.thickness = v(r, "thickness", .25, 0, 100), u.decay = v(r, "decay", u.reach / 3, 0, 1e6, !0), u.drying = v(r, "drying", 1800, 0, 1e9, !0), f(b(e, u.reach * 1.1), d + u.drying * 5.6, { line: e });
239
+ }
240
+ case "ash": {
241
+ m(r.vent) || _(i, "vent must be an [x, z] point."), u.thickness = v(r, "thickness", .1, 0, 100, !0), u.half = v(r, "half", 400, 0, 1e6, !0);
242
+ let e = r.wind ?? [0, 0];
243
+ m(e) || _(i, "wind must be an [x, z] m/s vector.");
244
+ let t = Math.hypot(e[0], e[1]);
245
+ u.elongation = v(r, "elongation", Math.min(12, 1 + .25 * t), 1, 12);
246
+ let n = r.vent, a = u.half * Math.max(Math.log2(u.thickness / 2e-4), 1), o = a * (2 * u.elongation - 1) + a;
247
+ return f([
248
+ n[0] - o,
249
+ n[1] - o,
250
+ n[0] + o,
251
+ n[1] + o
252
+ ], d, { dir: t > 0 ? [e[0] / t, e[1] / t] : [1, 0] });
253
+ }
254
+ case "lava": {
255
+ let e = y(i, "path", r.path);
256
+ u.width = v(r, "width", 12, 0, 1e5, !0), u.thickness = v(r, "thickness", 2, 0, 1e3, !0), u.levee = v(r, "levee", .35, 0, 1), u.cooling = v(r, "cooling", 1, 0, 1e6, !0), u.glowCrust = v(r, "glowCrust", .03, 0, 10, !0), u.skylights = v(r, "skylights", 200, 0, 1e5);
257
+ let t = e.at.length, n = (e, n, a, o) => {
258
+ let s = r[e];
259
+ if (s !== void 0) return (!Array.isArray(s) || s.length !== t || !s.every((e) => p(e) && e >= n && e <= a && !(o && e === n))) && _(i, `${e} must hold one number ${o ? "above" : "from"} ${n} to ${a} per path point.`), Float64Array.from(s);
260
+ }, a = {
261
+ arrival: n("arrival", 0, 1e9, !1),
262
+ width: n("widths", 0, 1e5, !0),
263
+ thickness: n("thicknesses", 0, 1e3, !0),
264
+ glow: n("glows", 0, 1, !1),
265
+ levee: n("levees", 0, 1, !1),
266
+ roof: n("roofs", 0, 1e9, !1),
267
+ drain: n("drains", 0, 1e9, !1)
268
+ }, o = a.arrival;
269
+ if (o) for (let e = 1; e < t; e++) o[e] < o[e - 1] && _(i, "arrival must never decrease along the path.");
270
+ let s = (5.6 * u.glowCrust / .0779) ** 2 * 3600 / u.cooling, c = b(e, a.width ? Math.max(...a.width) : u.width), l = -Infinity;
271
+ if (r.pond !== void 0) {
272
+ let e = r.pond;
273
+ (typeof e != "object" || !e || !m(e.center)) && _(i, "pond.center must be an [x, z] point.");
274
+ let t = e.center, n = v({
275
+ ...e,
276
+ kind: i
277
+ }, "start", 0, 0, 1e9), a = v({
278
+ ...e,
279
+ kind: i
280
+ }, "end", n + 1, n, 1e10, !0), o = v({
281
+ ...e,
282
+ kind: i
283
+ }, "flux", 1, 0, 1e5, !0), s = v({
284
+ ...e,
285
+ kind: i
286
+ }, "viscosity", 300, 0, 0xe8d4a51000, !0), d = v({
287
+ ...e,
288
+ kind: i
289
+ }, "density", 2600, 0, 1e4, !0);
290
+ u.pondX = t[0], u.pondZ = t[1], u.pondStart = n, u.pondEnd = a, u.pondFlux = o, u.pondK = .715 * (9.80665 * o ** 3 / (s / d * 3)) ** (1 / 8);
291
+ let f = u.pondK * Math.sqrt(a - n);
292
+ c = [
293
+ Math.min(c[0], t[0] - f),
294
+ Math.min(c[1], t[1] - f),
295
+ Math.max(c[2], t[0] + f),
296
+ Math.max(c[3], t[1] + f)
297
+ ], l = a;
298
+ }
299
+ return o ? {
300
+ ...f(c, Math.max(o[t - 1], l) + s, {
301
+ line: e,
302
+ profile: a
303
+ }),
304
+ start: o[0],
305
+ duration: Math.max(o[t - 1] - o[0], 1e-6)
306
+ } : f(c, Math.max(d, l) + s, {
307
+ line: e,
308
+ profile: a
309
+ });
310
+ }
311
+ case "burn": {
312
+ m(r.ignition) || _(i, "ignition must be an [x, z] point.");
313
+ let e = r.wind ?? [0, 0];
314
+ m(e) || _(i, "wind must be an [x, z] m/s vector."), u.rate = v(r, "rate", .3, 0, 1e3, !0), u.severity = v(r, "severity", 1, 0, 1), u.ashing = v(r, "ashing", 900, 0, 1e9, !0), u.consume = v(r, "consume", .03, 0, 1);
315
+ let t = Math.hypot(e[0], e[1]), n = I(M(t));
316
+ u.a = n.a, u.b = n.b, u.c = n.c;
317
+ let a = r.ignition, s = u.rate * o + 2;
318
+ return f([
319
+ a[0] - s,
320
+ a[1] - s,
321
+ a[0] + s,
322
+ a[1] + s
323
+ ], d + u.ashing * 5.6, { dir: t > 0 ? [e[0] / t, e[1] / t] : [1, 0] });
324
+ }
325
+ }
326
+ }
327
+ function R(e, t) {
328
+ let n = e.line.length * t, r = 0;
329
+ for (let i = 0; i < 256; i++) {
330
+ let a = z(e, (i + .5) / 256 * n, t);
331
+ r += a.width * a.depth * P[e.o.shape ? "u" : "v"];
332
+ }
333
+ return r * n / 256;
334
+ }
335
+ function z(e, t, n) {
336
+ let r = e.line.length, i = r * n;
337
+ if (t > i) return {
338
+ width: 0,
339
+ depth: 0
340
+ };
341
+ let a = (i - t) / r, o = (1 - Math.exp(-4 * a)) / (1 - Math.exp(-4)), s = g(0, e.o.width * .5, i - t);
342
+ return {
343
+ width: e.o.width * (.55 + .45 * o),
344
+ depth: e.o.depth * s * (.6 + .4 * o)
345
+ };
346
+ }
347
+ function B(e, t) {
348
+ return 2 / 3 * Math.PI * (e.o.length / 2) * (e.o.width / 2) * e.o.depth * V(t);
349
+ }
350
+ var V = (e) => g(0, 1 / 3, e), H = (e) => e <= 0 || e >= 1 ? 0 : Math.sqrt(e) * (1 - e) ** .35, U = 0;
351
+ for (let e = 0; e < 4096; e++) U += H((e + .5) / 4096) / 4096;
352
+ function W(e, t, n, r, i, a) {
353
+ if (r <= e.start) return;
354
+ let o = k(e, r), s = e.o, c = s.roughness;
355
+ switch (e.kind) {
356
+ case "gully": {
357
+ let r = S(e.line, t, n), i = 0, l = 0;
358
+ if (r.end <= 0) {
359
+ let a = z(e, r.s, o);
360
+ if (a.depth > 0) {
361
+ let e = a.width * (1 + c * .5 * O(t, n, s.width * 2, s.seed)), o = r.d / (e / 2), u = a.depth * F(o, s.shape);
362
+ u > 0 && (i -= u, l = g(0, .15, u));
363
+ }
364
+ }
365
+ let u = s.fan, d = e.line.points[0], f = e.line.points[1], p = t - d, m = n - f, h = (p * p + m * m) / (u * u);
366
+ if (h < 1 && p * e.dir[0] + m * e.dir[1] >= 0 && s.deposit > 0) {
367
+ let t = s.deposit * ee(e, o) / (Math.PI * u * u / 4) * (1 - h);
368
+ i += t, l = Math.max(l, g(0, .05, t));
369
+ }
370
+ a[0] += i, q(a, "soil", l);
371
+ return;
372
+ }
373
+ case "bank": {
374
+ let r = S(e.line, t, n);
375
+ if (r.side === s.river && r.end === 0) return;
376
+ let l = r.side === s.river ? -r.d : r.d, u = s.retreat * o * (1 + c * .6 * O(t, n, s.retreat * 3, s.seed)), d = s.bed + Math.tan(s.face) * Math.max(l - u, 0), f = Math.min(d - i, 0);
377
+ f < 0 && o > 0 && (a[0] += f, q(a, "soil", g(0, .15, -f)));
378
+ return;
379
+ }
380
+ case "scour": {
381
+ let r = A(o), i = s.radius * (.5 + .5 * r), l = e.source.center, u = t - l[0], d = n - l[1], f = Math.hypot(u, d) / (i * (1 + c * .4 * O(t, n, s.radius, s.seed))), p = 0, m = 0;
382
+ if (f < 1 && (p -= s.depth * r * (1 - f * f) ** 1.5, m = g(0, .1, -p)), e.dir && s.deposit > 0) {
383
+ let a = i, o = l[0] + e.dir[0] * i * 1.6, c = l[1] + e.dir[1] * i * 1.6, u = ((t - o) ** 2 + (n - c) ** 2) / (a * a);
384
+ if (u < 1) {
385
+ let e = s.deposit * (Math.PI * i * i * s.depth * r / 2.5) / (Math.PI * a * a / 2) * (1 - u);
386
+ p += e, m = Math.max(m, g(0, .05, e));
387
+ }
388
+ }
389
+ a[0] += p, q(a, "soil", m);
390
+ return;
391
+ }
392
+ case "crater": {
393
+ let i = e.source.center, o = t - i[0], l = n - i[1], u = Math.hypot(o, l), d = r - e.start, f = c / .35, p = u > 0 ? o / u : 1, m = u > 0 ? l / u : 0, _ = (e, t) => O(p * e * s.radius, m * e * s.radius, s.radius, s.seed + t), v = s.radius * (1 + c * .15 * O(t, n, s.radius * .5, s.seed) + f * (.22 * _(1.2, 11) + .1 * _(3, 13)));
394
+ if (d < s.heaveTime) {
395
+ let e = d / s.heaveTime, t = u / (s.domeSpread * v);
396
+ t < 1 && (a[0] += s.heave * j(e) * (1 - t * t) ** 2);
397
+ return;
398
+ }
399
+ let y = j(h((d - s.heaveTime) / e.duration)), b = v * (.5 + .5 * y), x = u / (s.domeSpread * v), S = u / b, C = x < 1 ? s.heave * (1 - y) * (1 - x * x) ** 2 : 0, w = 0;
400
+ if (S < 1) {
401
+ let e = 1 - S * S, t = e * 3 + Math.min(f, 1) * 1.2 * _(2, 17), n = t - Math.floor(t), r = Math.min(Math.max((Math.floor(t) + g(.3, .7, n)) / 3, 0), 1), i = e + Math.min(f, 1) * .6 * (r - e);
402
+ C += -s.depth * y * i + s.rim * y * g(.85, 1, S), w = 1;
403
+ } else if (S < 5) {
404
+ let e = 1 + Math.min(f, 1) * (1.2 * _(6, 19) + .9 * O(t, n, s.radius * .6, s.seed + 23)), r = s.rim * y * Math.max(e, 0) * (S ** -3 - 1 / 125) / (1 - 1 / 125);
405
+ C += r, w = f > 0 ? g(.02, .4, r) : g(0, .05, r), f > 0 && S < 4 && (C += y * Math.min(f, 1) * E(t, n, s.radius, s.seed));
406
+ }
407
+ f > 0 && S < 1.6 && (C += y * Math.min(f, 1) * T(t, n, s.radius, s.seed) * (S < 1 ? 1 : g(1.6, 1, S))), a[0] += C, q(a, "soil", w);
408
+ return;
409
+ }
410
+ case "slide": {
411
+ let r = e.source.from, [i, l] = e.dir, u = t - r[0], d = n - r[1], f = u * i + d * l, p = d * i - u * l, m = 0, h = 0, _ = (f - s.length / 2) / (s.length / 2), v = p / (s.width / 2) * (1 + c * .5 * O(t, n, s.width, s.seed)), y = 1 - _ * _ - v * v, b = V(o);
412
+ y > 0 && b > 0 && (m -= s.depth * Math.sqrt(y) * b, h = g(0, .1, s.depth * Math.sqrt(y) * b));
413
+ let x = s.length * .85, S = x + (s.distance - x) * j(o) - x;
414
+ if (S > 0) {
415
+ let r = s.width * .65, i = (f - x) / S, a = p / r * (1 + c * .4 * O(t + 31, n, s.width, s.seed)), l = 1 - a * a;
416
+ if (i > 0 && i < 1 && l > 0) {
417
+ let t = s.bulking * B(e, o) / (S * U * r * (4 / 3)) * H(i) * l;
418
+ m += t, h = Math.max(h, g(0, .05, t));
419
+ }
420
+ }
421
+ a[0] += m, q(a, "soil", h);
422
+ return;
423
+ }
424
+ case "mud": {
425
+ let i = S(e.line, t, n);
426
+ if (i.side !== s.land || i.end !== 0) return;
427
+ let l = s.reach * j(o) * (1 + c * .25 * O(t, n, s.reach * .3, s.seed));
428
+ if (i.d > l) return;
429
+ let u = s.thickness * Math.exp(-i.d / s.decay) * (1 - g(l * .9, l, i.d)), d = e.start + e.duration * (1 - Math.sqrt(Math.max(1 - i.d / s.reach, 0))), f = o < 1 ? 1 : Math.exp(-(r - Math.max(d, e.start + e.duration)) / s.drying);
430
+ a[0] += u, q(a, "mud", g(0, .02, u) + .6 * (1 - g(0, .02, u)) * f), a[6] = Math.max(a[6], f * g(0, .005, u + .004));
431
+ return;
432
+ }
433
+ case "ash": {
434
+ let r = e.source.vent, [i, l] = e.dir, u = t - r[0], d = n - r[1], f = u * i + d * l, p = d * i - u * l, m = s.elongation, h = m === 1 ? Math.hypot(f, p) : (-2 * f * (m - 1) + Math.sqrt(4 * f * f * (m - 1) ** 2 + 4 * (2 * m - 1) * (f * f + m * m * p * p))) / (2 * (2 * m - 1)), _ = s.thickness * o * 2 ** (-(h * (1 + c * .3 * O(t, n, s.half * .5, s.seed))) / s.half);
435
+ if (_ < 1e-5) return;
436
+ a[0] += _, q(a, "ash", g(0, .004, _));
437
+ return;
438
+ }
439
+ case "lava": {
440
+ s.pondK && G(e, t, n, r, a);
441
+ let i = S(e.line, t, n);
442
+ if (i.end > 0) return;
443
+ let p = e.line, m = p.length, h = e.profile, _ = h.arrival, v = _ ? f(p, _, r) : m * (1 - (1 - o) ** 1.5), y = h.width ? l(p, h.width, i.s) : s.width, b = y / 2 * (1 + c * .35 * O(t, n, y * 1.5, s.seed)), x = i.d / b;
444
+ if (x >= 1 || i.s > v + b * .5) return;
445
+ let C = _ ? l(p, _, i.s) : e.start + e.duration * (1 - Math.max(1 - Math.min(i.s, m) / m, 0) ** (1 / 1.5)), w = h.roof ? h.roof[d(p, i.s)] : Infinity, T = w < 1e9 ? g(C, Math.max(w, C + 1e-6), r) : 0, E = T >= 1 && u(s.skylights, s.seed, i.s, x, b), D = !E && T > 0 && x >= .82 * (1 - T), k = h.levee ? l(p, h.levee, i.s) : s.levee, A = (1 - x ** 16) * (E ? .5 : 1), j = D && x < .82 ? k : k * Math.exp(-(((x - .82) / .1) ** 2)), M = 1 - g(v - b * .5, v + b * .5, i.s), P = (h.thickness ? l(p, h.thickness, i.s) : s.thickness) * (A + j) * M;
446
+ if (P <= 0) return;
447
+ let F = E ? h.drain ? h.drain[d(p, i.s)] : 1e9 : 0, I = Math.max(r - (E ? F < 1e9 ? F : r : C), 0) * s.cooling, L = D ? 0 : Math.exp(-N(I) / s.glowCrust) * g(0, .1, P) * (h.glow ? l(p, h.glow, i.s) : 1);
448
+ a[0] += P, q(a, "lava", g(0, .08, P)), a[5] = Math.max(a[5] * (1 - g(0, .08, P)), L);
449
+ return;
450
+ }
451
+ case "burn": {
452
+ let i = e.source.ignition, [o, l] = e.dir, u = t - i[0], d = n - i[1], f = u * o + d * l, p = d * o - u * l, m = s.b * s.b * (s.a * s.a - s.c * s.c), h = 2 * f * s.c * s.b * s.b, _ = -(f * f * s.b * s.b + p * p * s.a * s.a), v = (-h + Math.sqrt(Math.max(h * h - 4 * m * _, 0))) / (2 * m), y = s.rate * Math.min(r - e.start, e.duration), b = v * (1 + c * .25 * O(t, n, 12, s.seed));
453
+ if (b > y) return;
454
+ let x = g(0, .6, y - b) * s.severity, S = r - (e.start + b / s.rate);
455
+ a[0] -= s.consume * x, q(a, "char", x), a[8] = Math.max(a[8], 1 - Math.exp(-Math.max(S, 0) / s.ashing));
456
+ return;
457
+ }
458
+ }
459
+ }
460
+ function G(e, t, n, r, i) {
461
+ let a = e.o, o = Math.min(r, a.pondEnd) - a.pondStart;
462
+ if (!(o > 0)) return;
463
+ let s = a.pondK * Math.sqrt(o), c = Math.hypot(t - a.pondX, n - a.pondZ);
464
+ if (c >= s) return;
465
+ let l = a.pondFlux * o / (.75 * Math.PI * s * s) * Math.cbrt(1 - (c / s) ** 2);
466
+ if (l <= 0) return;
467
+ let u = e.profile, d = a.pondStart + (c / a.pondK) ** 2, f = Math.max(r - d, 0) * a.cooling, p = u.glow ? u.glow[u.glow.length - 1] : 1, m = Math.exp(-N(f) / a.glowCrust) * g(0, .1, l) * p;
468
+ i[0] += l, q(i, "lava", g(0, .08, l)), i[5] = Math.max(i[5] * (1 - g(0, .08, l)), m);
469
+ }
470
+ var K = {
471
+ mud: 1,
472
+ ash: 2,
473
+ char: 3,
474
+ lava: 4,
475
+ soil: 7
476
+ };
477
+ function q(e, t, n) {
478
+ let r = h(n);
479
+ if (!(r <= 0)) {
480
+ for (let t of [
481
+ 1,
482
+ 2,
483
+ 3,
484
+ 4,
485
+ 7
486
+ ]) e[t] *= 1 - r;
487
+ e[K[t]] += r, t !== "lava" && (e[5] *= 1 - r);
488
+ }
489
+ }
490
+ var J = /* @__PURE__ */ new WeakMap();
491
+ function ee(e, t) {
492
+ let n = J.get(e);
493
+ if (n && n.p === t) return n.v;
494
+ let r = R(e, t);
495
+ return J.set(e, {
496
+ p: t,
497
+ v: r
498
+ }), r;
499
+ }
500
+ //#endregion
501
+ //#region src/ground-change.ts
502
+ var Y = 112, X = "@fragment fn fragment(i:Output)->@location(0) vec4f {", Z = "fn terrainColor(position:vec3f,normal:vec3f)->vec3f {", Q = {
503
+ soil: [
504
+ .3,
505
+ .21,
506
+ .13
507
+ ],
508
+ mud: [
509
+ .24,
510
+ .2,
511
+ .15
512
+ ],
513
+ ash: [
514
+ .36,
515
+ .35,
516
+ .34
517
+ ],
518
+ char: [
519
+ .025,
520
+ .022,
521
+ .02
522
+ ],
523
+ ashen: [
524
+ .5,
525
+ .49,
526
+ .47
527
+ ],
528
+ lava: [
529
+ .04,
530
+ .036,
531
+ .034
532
+ ],
533
+ glow: 12,
534
+ cracks: .7,
535
+ crackSize: .8,
536
+ wetDarkening: .55,
537
+ wetGloss: .5,
538
+ rain: null
539
+ }, te = Q, ne = [
540
+ "soil",
541
+ "mud",
542
+ "ash",
543
+ "char",
544
+ "ashen",
545
+ "lava"
546
+ ], re = [
547
+ "cracks",
548
+ "wetDarkening",
549
+ "wetGloss"
550
+ ];
551
+ function $(e) {
552
+ for (let t of ne) {
553
+ let n = e[t];
554
+ if (n !== void 0 && !(Array.isArray(n) && n.length === 3 && n.every((e) => typeof e == "number" && e >= 0 && e <= 1))) throw RangeError(`Invalid ground change ${t}; give three numbers from 0 to 1 (linear RGB).`);
555
+ }
556
+ for (let t of re) {
557
+ let n = e[t];
558
+ if (n !== void 0 && !(typeof n == "number" && n >= 0 && n <= 1)) throw RangeError(`Invalid ground change ${t}; give a number from 0 to 1.`);
559
+ }
560
+ if (e.glow !== void 0 && !(typeof e.glow == "number" && e.glow >= 0 && e.glow <= 100)) throw RangeError("Invalid ground change glow; give a number from 0 to 100.");
561
+ if (e.crackSize !== void 0 && !(typeof e.crackSize == "number" && e.crackSize > 0 && e.crackSize <= 100)) throw RangeError("Invalid ground change crackSize; give a number above 0 up to 100.");
562
+ if (e.rain !== void 0 && e.rain !== null && !(typeof e.rain == "number" && e.rain >= 0 && e.rain <= 1)) throw RangeError("Invalid ground change rain; give a number from 0 to 1, or null to follow the weather.");
563
+ }
564
+ var ie = 0, ae = class s {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
565
+ #e;
566
+ #t;
567
+ #n;
568
+ #r;
569
+ #i;
570
+ #a;
571
+ #o;
572
+ #s;
573
+ #c;
574
+ #l;
575
+ #u;
576
+ #d;
577
+ #f;
578
+ #p;
579
+ #m;
580
+ #h;
581
+ #g;
582
+ #_;
583
+ #v;
584
+ #y;
585
+ #b;
586
+ #x;
587
+ #S;
588
+ #C;
589
+ #w;
590
+ #T;
591
+ #E;
592
+ #D;
593
+ #O;
594
+ #k;
595
+ #A;
596
+ #j;
597
+ #M;
598
+ #N;
599
+ #P;
600
+ #F;
601
+ #I;
602
+ #L;
603
+ #R;
604
+ #z;
605
+ #B;
606
+ #V;
607
+ #H;
608
+ #U;
609
+ #W;
610
+ #G;
611
+ constructor(e, t) {
612
+ this.options = e, this.name = "ground-change:" + ie++, this.disposed = !1, this.#e = [], this.#t = /* @__PURE__ */ new Map(), this.#n = 0, this.#r = 0, this.#a = 0, this.#u = NaN, this.#d = [], this.#p = /* @__PURE__ */ new Set(), this.#b = !1, this.#w = [], this.#T = /* @__PURE__ */ new Float32Array(), this.#E = /* @__PURE__ */ new Map(), this.#k = /* @__PURE__ */ new Uint8Array(), this.#j = /* @__PURE__ */ new Uint8Array(), this.#L = /* @__PURE__ */ new Float32Array(32), this.#R = !0, this.#z = [], this.#B = "", this.#V = "", this.#H = 44, this.#U = /* @__PURE__ */ new Float64Array(9);
613
+ let n = se(t) ? t : void 0, i = e?.grid;
614
+ if (n) {
615
+ let t = n.heightmap;
616
+ if (n.disposed || !t) throw new o("state", "GroundChange needs an attached Terrain; attach the Terrain to its engine first");
617
+ if (e.base !== void 0) throw RangeError("A terrain's GroundChange takes its base from the heightmap; leave out base.");
618
+ i ??= {
619
+ size: n.options.size,
620
+ origin: n.options.origin ?? [0, 0],
621
+ resolution: [t.width - 1, t.height - 1]
622
+ };
623
+ }
624
+ let a = i?.resolution ?? 192, [s, c] = typeof a == "number" ? [a, a] : Array.isArray(a) && a.length === 2 ? a : [NaN, NaN], l = (e) => Number.isInteger(e) && e >= 1 && e <= 4096;
625
+ if (!i || !Array.isArray(i.size) || i.size.length !== 2 || !i.size.every((e) => typeof e == "number" && e > 0 && e <= 1e6) || !l(s) || !l(c) || i.origin !== void 0 && !(Array.isArray(i.origin) && i.origin.length === 2 && i.origin.every((e) => Number.isFinite(e)))) throw RangeError("Invalid ground change grid; give size [w, d] above 0 (up to 1e6), an integer resolution 1 to 4,096 (or one along X and one along Z) and a finite origin.");
626
+ let u = e.time ?? 0;
627
+ if (!(typeof u == "number" && u >= 0 && u <= 1e9)) throw RangeError("Invalid ground change time; give 0 to 1e9 seconds.");
628
+ if (e.clock !== void 0 && typeof e.clock != "function") throw TypeError("Invalid ground change clock; pass a function returning film seconds.");
629
+ if (e.shadows !== void 0 && typeof e.shadows?.invalidate != "function") throw TypeError("Invalid ground change shadows; pass a Shadows (anything with invalidate()).");
630
+ $(e), this.side = s + 1, this.sides = [s + 1, c + 1], this.size = [i.size[0], i.size[1]], this.origin = [i.origin?.[0] ?? -i.size[0] / 2, i.origin?.[1] ?? -i.size[1] / 2];
631
+ let d = this.sides[0] * this.sides[1];
632
+ this.#o = new Float32Array(d), this.#s = new Uint8Array(d * 4), this.#c = new Uint8Array(d * 4), this.#i = new Float32Array(d);
633
+ let f = e.base;
634
+ if (typeof f == "function") for (let e = 0; e < this.sides[1]; e++) for (let t = 0; t < this.side; t++) {
635
+ let [n, r] = this.#q(t, e), i = f(n, r);
636
+ if (!Number.isFinite(i)) throw RangeError("Ground change base heights must be finite.");
637
+ this.#i[e * this.side + t] = i;
638
+ }
639
+ else if (f !== void 0) {
640
+ if (!(f instanceof Float32Array) || f.length !== d || !f.every(Number.isFinite)) throw RangeError(`Invalid ground change base; give ${d} finite heights, or a height function.`);
641
+ this.#i.set(f);
642
+ }
643
+ this.#a = this.#i.reduce((e, t) => Math.max(e, t), -Infinity), this.look = {
644
+ ...Q,
645
+ ...oe(e)
646
+ }, e.rain !== void 0 && e.rain !== null && r(this.look, "rain"), this.#l = u, n ? this.#g = n : this.#h = t, this.#v = n ? new Promise((e) => this.#K = e) : Promise.resolve();
647
+ for (let t of e.events ?? []) this.add(t);
648
+ }
649
+ #K;
650
+ static attach(e, t, n = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
651
+ let r = new s(n, t);
652
+ return i(e).use(r), r;
653
+ }
654
+ get ready() {
655
+ return this.#v;
656
+ }
657
+ #q(e, t) {
658
+ return [this.origin[0] + e / (this.sides[0] - 1) * this.size[0], this.origin[1] + t / (this.sides[1] - 1) * this.size[1]];
659
+ }
660
+ #J(e) {
661
+ let t = this.sides[0] - 1, n = this.sides[1] - 1, r = Math.max(0, Math.floor((e[0] - this.origin[0]) / this.size[0] * t)), i = Math.max(0, Math.floor((e[1] - this.origin[1]) / this.size[1] * n)), a = Math.min(t, Math.ceil((e[2] - this.origin[0]) / this.size[0] * t)), o = Math.min(n, Math.ceil((e[3] - this.origin[1]) / this.size[1] * n));
662
+ return r > a || i > o ? void 0 : {
663
+ x0: r,
664
+ z0: i,
665
+ x1: a,
666
+ z1: o
667
+ };
668
+ }
669
+ #Y() {
670
+ this.#a = this.#i.reduce((e, t) => Math.max(e, t), -Infinity);
671
+ let e = Number.isFinite(this.#a) ? this.#a : 0;
672
+ for (let [t, n] of this.#t) {
673
+ let r = L(n.source, n.order, e);
674
+ this.#t.set(t, r), this.#e[this.#e.indexOf(n)] = r;
675
+ }
676
+ }
677
+ add(e) {
678
+ this.#Z();
679
+ let t = L(e, this.#r++, Number.isFinite(this.#a) ? this.#a : 0), n = this.#n++;
680
+ this.#t.set(n, t), this.#e.push(t), this.#e.sort((e, t) => e.start - t.start || e.order - t.order);
681
+ let r = this.#J(t.bounds);
682
+ return r && this.#d.push(r), n;
683
+ }
684
+ remove(e) {
685
+ let t = this.#t.get(e);
686
+ if (!t) return !1;
687
+ this.#t.delete(e), this.#e.splice(this.#e.indexOf(t), 1);
688
+ let n = this.#J(t.bounds);
689
+ return n && this.#d.push(n), !0;
690
+ }
691
+ clear() {
692
+ for (let e of [...this.#t.keys()]) this.remove(e);
693
+ }
694
+ get events() {
695
+ return this.#e.map((e) => e.source);
696
+ }
697
+ get time() {
698
+ return this.#l;
699
+ }
700
+ setTime(e) {
701
+ if (!(typeof e == "number" && e >= 0 && e <= 1e9)) throw RangeError("Invalid ground change time; give 0 to 1e9 seconds.");
702
+ return this.#l = e, this;
703
+ }
704
+ advanceTime(e) {
705
+ e > 0 && (this.#l = Math.min(this.#l + e, 1e9));
706
+ }
707
+ onPreUpdate(e) {
708
+ this.options.clock ? this.setTime(this.options.clock()) : this.advanceTime(e);
709
+ }
710
+ setLook(e) {
711
+ if (typeof e != "object" || !e) throw TypeError("Ground change look must be an object");
712
+ $(e);
713
+ for (let [t, n] of Object.entries(e)) n !== void 0 && t in Q && (this.look[t] = Array.isArray(n) ? [...n] : n, t === "rain" && r(this.look, "rain", n !== null));
714
+ this.#R = !0;
715
+ }
716
+ onChange(e) {
717
+ return this.#p.add(e), () => this.#p.delete(e);
718
+ }
719
+ update() {
720
+ this.#Z();
721
+ let e = this.#l, t = this.#u, n = Math.min(t, e), r = Math.max(t, e), i, a = (e) => {
722
+ e && (i = i ? {
723
+ x0: Math.min(i.x0, e.x0),
724
+ z0: Math.min(i.z0, e.z0),
725
+ x1: Math.max(i.x1, e.x1),
726
+ z1: Math.max(i.z1, e.z1)
727
+ } : e);
728
+ };
729
+ for (let e of this.#d) a(e);
730
+ this.#d = [];
731
+ for (let i of this.#e) (Number.isNaN(t) ? i.start < e : i.start < r && i.settle > n && t !== e) && a(this.#J(i.bounds));
732
+ if (this.#u = e, !i) return;
733
+ this.#X(i, e);
734
+ let o = this.#f;
735
+ this.#f = o ? {
736
+ x0: Math.min(o.x0, i.x0),
737
+ z0: Math.min(o.z0, i.z0),
738
+ x1: Math.max(o.x1, i.x1),
739
+ z1: Math.max(o.z1, i.z1)
740
+ } : i;
741
+ for (let e of this.#p) e(i);
742
+ return i;
743
+ }
744
+ #X(e, t) {
745
+ let n = this.side, r = this.#U, i = this.#q(e.x0, e.z0), a = this.#q(e.x1, e.z1), o = this.#e.filter((e) => e.start < t && e.bounds[0] <= a[0] && e.bounds[2] >= i[0] && e.bounds[1] <= a[1] && e.bounds[3] >= i[1]);
746
+ for (let i = e.z0; i <= e.z1; i++) for (let a = e.x0; a <= e.x1; a++) {
747
+ let e = i * n + a, [s, c] = this.#q(a, i);
748
+ r.fill(0);
749
+ for (let n of o) s >= n.bounds[0] && s <= n.bounds[2] && c >= n.bounds[1] && c <= n.bounds[3] && W(n, s, c, t, this.#i[e], r);
750
+ this.#o[e] = r[0];
751
+ let l = (e) => Math.round((e < 0 ? 0 : e > 1 ? 1 : e) * 255);
752
+ this.#s[e * 4] = l(r[1]), this.#s[e * 4 + 1] = l(r[2]), this.#s[e * 4 + 2] = l(r[3]), this.#s[e * 4 + 3] = l(r[4]), this.#c[e * 4] = l(r[5]), this.#c[e * 4 + 1] = l(r[6]), this.#c[e * 4 + 2] = l(r[7]), this.#c[e * 4 + 3] = l(r[8]);
753
+ }
754
+ }
755
+ get change() {
756
+ return this.#o;
757
+ }
758
+ get base() {
759
+ return this.#i;
760
+ }
761
+ get heights() {
762
+ let e = new Float32Array(this.#i.length);
763
+ for (let t = 0; t < e.length; t++) e[t] = this.#i[t] + this.#o[t];
764
+ return e;
765
+ }
766
+ get covers() {
767
+ return {
768
+ surface: this.#s,
769
+ state: this.#c
770
+ };
771
+ }
772
+ get textures() {
773
+ return {
774
+ surface: this.#P,
775
+ state: this.#F
776
+ };
777
+ }
778
+ sample(e, t, n = this.#l) {
779
+ if (!Number.isFinite(e) || !Number.isFinite(t) || !(n >= 0)) throw RangeError("Invalid ground sample; give finite x, z and t of 0 or more.");
780
+ let r = this.sides[0] - 1, i = this.sides[1] - 1, a = Math.min(Math.max((e - this.origin[0]) / this.size[0] * r, 0), r), o = Math.min(Math.max((t - this.origin[1]) / this.size[1] * i, 0), i), s = Math.min(Math.floor(a), r - 1), l = Math.min(Math.floor(o), i - 1), u = a - s, d = o - l, f = (e, t) => this.#i[Math.min(t, i) * this.side + Math.min(e, r)], p = (f(s, l) * (1 - u) + f(s + 1, l) * u) * (1 - d) + (f(s, l + 1) * (1 - u) + f(s + 1, l + 1) * u) * d, m = /* @__PURE__ */ new Float64Array(9);
781
+ for (let r of this.#e) e >= r.bounds[0] && e <= r.bounds[2] && t >= r.bounds[1] && t <= r.bounds[3] && W(r, e, t, n, p, m);
782
+ let h = {
783
+ height: p + m[0],
784
+ change: m[0]
785
+ };
786
+ return c.forEach((e, t) => h[e] = Math.min(Math.max(m[t + 1], 0), 1)), h;
787
+ }
788
+ #Z() {
789
+ if (this.disposed) throw new o("disposed", "This GroundChange was disposed; create a new one");
790
+ }
791
+ setup(t) {
792
+ if (e(t, "GroundChange"), this.#g) return this.#ee(t);
793
+ if (this.sides[0] !== this.sides[1]) throw TypeError("GroundChange on a mesh needs a square grid (one resolution), as heightfieldGeometry builds.");
794
+ let n = this.#h ? a(this.#h) : void 0;
795
+ if (!n) throw TypeError("GroundChange needs a mesh; use GroundChange.attach(engine, mesh, options) or new GroundChange(options, mesh)");
796
+ if (n.check(), this.disposed || this.#m && this.#m !== t || n.engine !== t) throw new o(this.disposed ? "disposed" : "attached", "GroundChange's mesh belongs to another engine or it is already attached; attach one per mesh, to its engine");
797
+ let r = n.geometry, i = this.side;
798
+ if (!this.#M) {
799
+ if (!r || r.vertices.length / 6 !== i * i) throw TypeError(`GroundChange needs the mesh's heightfieldGeometry on the same grid (${i * i} vertices).`);
800
+ let e = new Int32Array(i * i).fill(-1), t = r.vertices, n = .001;
801
+ for (let r = 0; r < i * i; r++) {
802
+ let a = (t[r * 6] - this.origin[0]) / this.size[0] * (i - 1), o = (t[r * 6 + 2] - this.origin[1]) / this.size[1] * (i - 1), s = Math.round(a), c = Math.round(o);
803
+ if (Math.abs(a - s) > n || Math.abs(o - c) > n || s < 0 || c < 0 || s >= i || c >= i || e[c * i + s] >= 0) throw TypeError("GroundChange: the mesh is not a heightfield on this grid; build it with heightfieldGeometry on the same size, origin and resolution.");
804
+ e[c * i + s] = r;
805
+ }
806
+ this.#M = e, this.#N = new Float32Array(i * i * 3);
807
+ for (let n = 0; n < i * i; n++) {
808
+ let r = e[n];
809
+ this.#i[n] = t[r * 6 + 1], this.#N.set([
810
+ t[r * 6 + 3],
811
+ t[r * 6 + 4],
812
+ t[r * 6 + 5]
813
+ ], n * 3);
814
+ }
815
+ this.#Y(), this.#G = r.bounds.slice(), this.#u = NaN, this.#d.push({
816
+ x0: 0,
817
+ z0: 0,
818
+ x1: i - 1,
819
+ z1: i - 1
820
+ });
821
+ }
822
+ this.#m = t;
823
+ let s = t.gpu, c = () => s.createTexture({
824
+ size: [i, i],
825
+ format: "rgba8unorm",
826
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
827
+ });
828
+ if (i > s.limits.maxTextureDimension2D) throw new o("unsupported", "The ground change grid is larger than this device allows; use a smaller resolution");
829
+ this.#P = c(), this.#F = c(), this.#I = s.createBuffer({
830
+ size: 128,
831
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
832
+ }), this.#R = !0, this.#B = "", s.queue.writeTexture({ texture: this.#P }, this.#s, { bytesPerRow: i * 4 }, [i, i]), s.queue.writeTexture({ texture: this.#F }, this.#c, { bytesPerRow: i * 4 }, [i, i]);
833
+ }
834
+ registerPasses(e) {
835
+ this.#g ? e.add({
836
+ name: this.name,
837
+ order: -7,
838
+ execute: (e) => {
839
+ if (!(e.backend !== "webgpu" || this.disposed)) {
840
+ if (this.#y !== void 0) {
841
+ let e = this.#y;
842
+ throw this.#y = void 0, e;
843
+ }
844
+ this.#_ && (this.update(), this.#ne(e.device), this.#se(e.device));
845
+ }
846
+ }
847
+ }) : e.add({
848
+ name: this.name,
849
+ order: -1,
850
+ execute: (e) => {
851
+ if (e.backend !== "webgpu" || this.disposed || !this.#h) return;
852
+ let t = a(this.#h);
853
+ if (this.update(), this.#Q(e.device, t), this.#B !== e.shader) {
854
+ for (this.#B = e.shader, this.#H = 44; [
855
+ 0,
856
+ 1,
857
+ 2
858
+ ].some((t) => e.bindings.some((e) => e.binding === this.#H + t));) this.#H++;
859
+ this.#z = [
860
+ {
861
+ binding: this.#H,
862
+ resource: this.#P.createView()
863
+ },
864
+ {
865
+ binding: this.#H + 1,
866
+ resource: this.#F.createView()
867
+ },
868
+ {
869
+ binding: this.#H + 2,
870
+ resource: { buffer: this.#I }
871
+ }
872
+ ], this.#V = this.#ce(e.shader, t.id);
873
+ }
874
+ this.#se(e.device), this.#V !== this.#B && (e.bindings.push(...this.#z), e.shader = this.#V);
875
+ }
876
+ });
877
+ }
878
+ #Q(e, t) {
879
+ let n = this.#f, r = t.geometry;
880
+ if (!n || !r || !this.#M) return;
881
+ let i = this.#m.geometryBuffers(r);
882
+ if (!i) return;
883
+ this.#f = void 0, this.#W = i.vertex, this.#oe(e, n);
884
+ let a = this.side, o = r.vertices, s = this.#M, c = this.#i, l = this.#o, u = this.#N, d = this.size[0] / (a - 1), f = this.size[1] / (a - 1), p = {
885
+ x0: Math.max(n.x0 - 1, 0),
886
+ z0: Math.max(n.z0 - 1, 0),
887
+ x1: Math.min(n.x1 + 1, a - 1),
888
+ z1: Math.min(n.z1 + 1, a - 1)
889
+ };
890
+ for (let t = p.z0; t <= p.z1; t++) {
891
+ let n = Infinity, r = -Infinity;
892
+ for (let e = p.x0; e <= p.x1; e++) {
893
+ let i = t * a + e, p = s[i], m = (l[t * a + Math.min(e + 1, a - 1)] - l[t * a + Math.max(e - 1, 0)]) / (d * (Math.min(e + 1, a - 1) - Math.max(e - 1, 0))), h = (l[Math.min(t + 1, a - 1) * a + e] - l[Math.max(t - 1, 0) * a + e]) / (f * (Math.min(t + 1, a - 1) - Math.max(t - 1, 0)));
894
+ if (o[p * 6 + 1] = c[i] + l[i], m === 0 && h === 0) o[p * 6 + 3] = u[i * 3], o[p * 6 + 4] = u[i * 3 + 1], o[p * 6 + 5] = u[i * 3 + 2];
895
+ else {
896
+ let e = Math.max(u[i * 3 + 1], 1e-4), t = -u[i * 3] / e + m, n = -u[i * 3 + 2] / e + h, r = Math.hypot(t, 1, n);
897
+ o[p * 6 + 3] = -t / r, o[p * 6 + 4] = 1 / r, o[p * 6 + 5] = -n / r;
898
+ }
899
+ n = Math.min(n, p), r = Math.max(r, p);
900
+ }
901
+ e.queue.writeBuffer(i.vertex, n * 24, o, n * 6, (r - n + 1) * 6);
902
+ }
903
+ this.#$(t, p);
904
+ let m = n.x1 - n.x0 + 1, h = n.z1 - n.z0 + 1, g = {
905
+ offset: (n.z0 * a + n.x0) * 4,
906
+ bytesPerRow: a * 4
907
+ };
908
+ e.queue.writeTexture({
909
+ texture: this.#P,
910
+ origin: [n.x0, n.z0]
911
+ }, this.#s, g, [m, h]), e.queue.writeTexture({
912
+ texture: this.#F,
913
+ origin: [n.x0, n.z0]
914
+ }, this.#c, g, [m, h]), this.options.shadows?.invalidate();
915
+ }
916
+ #$(e, t) {
917
+ let n = e.geometry, r = n.bounds, i = n.vertices, a = this.#M, o = this.side, s = r[3];
918
+ for (let e = t.z0; e <= t.z1; e++) for (let n = t.x0; n <= t.x1; n++) {
919
+ let t = a[e * o + n];
920
+ s = Math.max(s, Math.hypot(i[t * 6] - r[0], i[t * 6 + 1] - r[1], i[t * 6 + 2] - r[2]));
921
+ }
922
+ s <= r[3] || (r[3] = s, r[3] < s && new Uint32Array(r.buffer, r.byteOffset, 4)[3]++, this.#m.store.bounds[e.id * 4 + 3] = r[3]);
923
+ }
924
+ #ee(e) {
925
+ let t = this.#g;
926
+ if (this.disposed || this.#m && this.#m !== e || t.disposed) throw new o(this.disposed ? "disposed" : "attached", "GroundChange's terrain belongs to another engine or it is already attached; attach one per terrain, to its engine");
927
+ let n = e.gpu, [r, i] = this.sides, a = t.heightmap;
928
+ if (a) {
929
+ let e = GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST;
930
+ if (a.format !== "r32float" || (a.usage & e) !== e) throw TypeError("GroundChange on a Terrain needs an r32float heightmap with COPY_SRC and COPY_DST usage; create it so in the textures factory.");
931
+ if (a.width !== r || a.height !== i) throw TypeError(`GroundChange: the terrain's heightmap is ${a.width} x ${a.height} texels, the grid ${r} x ${i}; leave out grid to take the heightmap's.`);
932
+ }
933
+ if (r > n.limits.maxTextureDimension2D || i > n.limits.maxTextureDimension2D) throw new o("unsupported", "The ground change grid is larger than this device allows; use a smaller heightmap");
934
+ this.#m = e, this.#C = n.createTexture({
935
+ size: [
936
+ r,
937
+ i,
938
+ 3
939
+ ],
940
+ format: "rgba8unorm",
941
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST
942
+ }), this.#I = n.createBuffer({
943
+ size: 128,
944
+ usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
945
+ }), this.#R = !0, this.#w = [{
946
+ binding: Y,
947
+ resource: this.#C.createView({ dimension: "2d-array" })
948
+ }, {
949
+ binding: 113,
950
+ resource: { buffer: this.#I }
951
+ }], this.#se(n);
952
+ let s = new Uint8Array(this.#o.buffer, this.#o.byteOffset, this.#o.byteLength);
953
+ [
954
+ this.#s,
955
+ this.#c,
956
+ s
957
+ ].forEach((e, t) => n.queue.writeTexture({
958
+ texture: this.#C,
959
+ origin: [
960
+ 0,
961
+ 0,
962
+ t
963
+ ]
964
+ }, e, { bytesPerRow: r * 4 }, [r, i])), this.#x ??= t.addLighting(this), this.#S ??= t.onDispose(() => this.dispose()), !this.#_ && !this.#b && a && this.#te(n, a);
965
+ }
966
+ #te(e, t) {
967
+ let [n, r] = this.sides, i = Math.ceil(n * 4 / 256) * 256, a = e.createBuffer({
968
+ size: i * r,
969
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
970
+ }), o = e.createCommandEncoder();
971
+ o.copyTextureToBuffer({ texture: t }, {
972
+ buffer: a,
973
+ bytesPerRow: i,
974
+ rowsPerImage: r
975
+ }, [n, r]), e.queue.submit([o.finish()]), this.#b = !0, a.mapAsync(GPUMapMode.READ).then(() => {
976
+ let e = new Float32Array(n * r), t = new Float32Array(a.getMappedRange());
977
+ for (let a = 0; a < r; a++) e.set(t.subarray(a * i / 4, a * i / 4 + n), a * n);
978
+ if (a.destroy(), this.#b = !1, this.disposed) return;
979
+ let o = this.#g.options, s = o.heightScale ?? 100, c = o.heightOffset ?? 0;
980
+ for (let t = 0; t < e.length; t++) this.#i[t] = e[t] * s + c;
981
+ this.#_ = e, this.#Y(), this.#u = NaN, this.#d.push({
982
+ x0: 0,
983
+ z0: 0,
984
+ x1: n - 1,
985
+ z1: r - 1
986
+ }), this.#K?.();
987
+ }, (t) => {
988
+ a.destroy(), this.#b = !1, !this.disposed && this.#m?.gpu === e && (this.#y = t);
989
+ });
990
+ }
991
+ #ne(e) {
992
+ let t = this.#f, n = this.#g, r = n?.heightmap, i = this.#_;
993
+ if (!t || !n || !r || !i || !this.#C) return;
994
+ this.#f = void 0, this.#oe(e, t);
995
+ let a = this.sides[0], o = t.x1 - t.x0 + 1, s = t.z1 - t.z0 + 1, c = n.options.heightScale ?? 100, l = this.#o;
996
+ this.#T.length < o * s && (this.#T = new Float32Array(o * s));
997
+ let u = this.#T;
998
+ for (let e = 0; e < s; e++) for (let n = 0; n < o; n++) {
999
+ let r = (t.z0 + e) * a + t.x0 + n;
1000
+ u[e * o + n] = l[r] === 0 ? i[r] : i[r] + l[r] / c;
1001
+ }
1002
+ e.queue.writeTexture({
1003
+ texture: r,
1004
+ origin: [t.x0, t.z0]
1005
+ }, u, { bytesPerRow: o * 4 }, [o, s]);
1006
+ let d = {
1007
+ offset: (t.z0 * a + t.x0) * 4,
1008
+ bytesPerRow: a * 4
1009
+ }, f = new Uint8Array(l.buffer, l.byteOffset, l.byteLength);
1010
+ [
1011
+ this.#s,
1012
+ this.#c,
1013
+ f
1014
+ ].forEach((n, r) => e.queue.writeTexture({
1015
+ texture: this.#C,
1016
+ origin: [
1017
+ t.x0,
1018
+ t.z0,
1019
+ r
1020
+ ]
1021
+ }, n, d, [o, s])), n.invalidate(), this.options.shadows?.invalidate();
1022
+ }
1023
+ scatterGround() {
1024
+ this.#Z();
1025
+ let [e, t] = this.sides, n = this.size[0] / (e - 1), r = this.size[1] / (t - 1);
1026
+ return {
1027
+ heightmap: () => this.#re()?.[0],
1028
+ densityMap: () => this.#re()?.[1],
1029
+ bounds: [
1030
+ this.origin[0] - n / 2,
1031
+ this.origin[1] - r / 2,
1032
+ this.origin[0] + this.size[0] + n / 2,
1033
+ this.origin[1] + this.size[1] + r / 2
1034
+ ]
1035
+ };
1036
+ }
1037
+ #re() {
1038
+ let e = this.#m?.gpu;
1039
+ if (!this.disposed && e) {
1040
+ if (!this.#D || !this.#O) {
1041
+ let [t, n] = this.sides, r = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC;
1042
+ this.#D = e.createTexture({
1043
+ size: [t, n],
1044
+ format: "r32float",
1045
+ usage: r
1046
+ }), this.#O = e.createTexture({
1047
+ size: [t, n],
1048
+ format: "r8unorm",
1049
+ usage: r
1050
+ }), this.#oe(e, {
1051
+ x0: 0,
1052
+ z0: 0,
1053
+ x1: t - 1,
1054
+ z1: n - 1
1055
+ });
1056
+ }
1057
+ return [this.#D, this.#O];
1058
+ }
1059
+ }
1060
+ plantCovers() {
1061
+ let { bounds: e } = this.scatterGround();
1062
+ return {
1063
+ covers: () => this.#ie(),
1064
+ bounds: e
1065
+ };
1066
+ }
1067
+ #ie() {
1068
+ let e = this.#m?.gpu;
1069
+ if (!this.disposed && e) {
1070
+ if (!this.#A) {
1071
+ let [t, n] = this.sides;
1072
+ this.#A = e.createTexture({
1073
+ size: [t, n],
1074
+ format: "rgba8unorm",
1075
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC
1076
+ }), this.#ae(e, {
1077
+ x0: 0,
1078
+ z0: 0,
1079
+ x1: t - 1,
1080
+ z1: n - 1
1081
+ });
1082
+ }
1083
+ return this.#A;
1084
+ }
1085
+ }
1086
+ #ae(e, t) {
1087
+ if (!this.#A) return;
1088
+ let n = this.sides[0], r = t.x1 - t.x0 + 1, i = t.z1 - t.z0 + 1, a = this.#s, o = this.#c;
1089
+ this.#j.length < r * i * 4 && (this.#j = new Uint8Array(r * i * 4));
1090
+ let s = this.#j;
1091
+ for (let e = 0; e < i; e++) for (let i = 0; i < r; i++) {
1092
+ let c = ((t.z0 + e) * n + t.x0 + i) * 4, l = (e * r + i) * 4;
1093
+ s[l] = a[c], s[l + 1] = a[c + 1], s[l + 2] = a[c + 2], s[l + 3] = o[c + 1];
1094
+ }
1095
+ e.queue.writeTexture({
1096
+ texture: this.#A,
1097
+ origin: [t.x0, t.z0]
1098
+ }, s, { bytesPerRow: r * 4 }, [r, i]);
1099
+ }
1100
+ #oe(e, t) {
1101
+ if (this.#ae(e, t), !this.#D || !this.#O) return;
1102
+ let n = this.sides[0], r = t.x1 - t.x0 + 1, i = t.z1 - t.z0 + 1, a = r * i, o = this.#i, s = this.#o, c = this.#s, l = this.#c;
1103
+ this.#T.length < a && (this.#T = new Float32Array(a)), this.#k.length < a && (this.#k = new Uint8Array(a));
1104
+ let u = this.#T, d = this.#k;
1105
+ for (let e = 0; e < i; e++) for (let i = 0; i < r; i++) {
1106
+ let a = (t.z0 + e) * n + t.x0 + i, f = e * r + i;
1107
+ u[f] = o[a] + s[a], d[f] = 255 - Math.max(c[a * 4], c[a * 4 + 1], c[a * 4 + 2], c[a * 4 + 3], l[a * 4 + 2]);
1108
+ }
1109
+ e.queue.writeTexture({
1110
+ texture: this.#D,
1111
+ origin: [t.x0, t.z0]
1112
+ }, u, { bytesPerRow: r * 4 }, [r, i]), e.queue.writeTexture({
1113
+ texture: this.#O,
1114
+ origin: [t.x0, t.z0]
1115
+ }, d, { bytesPerRow: r }, [r, i]);
1116
+ }
1117
+ get owner() {
1118
+ return this.#m;
1119
+ }
1120
+ bindings() {
1121
+ return this.#w;
1122
+ }
1123
+ shader(e, t) {
1124
+ let r = t + e, i = this.#E.get(r);
1125
+ if (i) return i;
1126
+ if (e.split(X).length !== 2 || e.split(Z).length !== 2) throw new o("unsupported", "GroundChange needs the terrain's lit fragment shader and its terrainColor; attach it before effects that replace them");
1127
+ let a = "_" + this.name.slice(this.name.indexOf(":") + 1), s = `
1128
+ @group(${t}) @binding(${Y}) var groundCover${a}:texture_2d_array<f32>;
1129
+ @group(${t}) @binding(113) var<uniform> groundLook${a}:array<vec4f,8>;
1130
+ var<private> groundGlow${a}:vec3f;
1131
+ var<private> groundGloss${a}:f32;
1132
+ fn groundTexel${a}(f:vec2f,layer:i32)->vec4f {
1133
+ let n=vec2i(groundLook${a}[7].xy);let lo=min(vec2i(floor(f)),n);let hi=min(lo+1,n);let w=f-floor(f);
1134
+ return mix(mix(textureLoad(groundCover${a},lo,layer,0),textureLoad(groundCover${a},vec2i(hi.x,lo.y),layer,0),w.x),mix(textureLoad(groundCover${a},vec2i(lo.x,hi.y),layer,0),textureLoad(groundCover${a},hi,layer,0),w.x),w.y);
1135
+ }
1136
+ fn groundDelta${a}(c:vec2i)->f32 {
1137
+ let n=vec2i(groundLook${a}[7].xy);let u=vec4u(round(textureLoad(groundCover${a},clamp(c,vec2i(0),n),2,0)*255.0));
1138
+ return bitcast<f32>(u.x|(u.y<<8u)|(u.z<<16u)|(u.w<<24u));
1139
+ }
1140
+ fn groundSlope${a}(c:vec2i)->vec2f {
1141
+ let n=vec2i(groundLook${a}[7].xy);let step=frame.map.zw/vec2f(n);let lo=max(c-1,vec2i(0));let hi=min(c+1,n);
1142
+ return vec2f((groundDelta${a}(vec2i(hi.x,c.y))-groundDelta${a}(vec2i(lo.x,c.y)))/(f32(hi.x-lo.x)*step.x),(groundDelta${a}(vec2i(c.x,hi.y))-groundDelta${a}(vec2i(c.x,lo.y)))/(f32(hi.y-lo.y)*step.y));
1143
+ }
1144
+ fn groundShade${a}(p:vec3f,color:vec3f)->vec3f {
1145
+ let L=groundLook${a};let uv=(p.xz-frame.map.xy)/frame.map.zw;
1146
+ if(any(uv<vec2f(0))||any(uv>vec2f(1))){return color;}
1147
+ let f=uv*L[7].xy;let a=groundTexel${a}(f,0);let st=groundTexel${a}(f,1);
1148
+ var c=mix(color,L[0].rgb,st.b);
1149
+ c=mix(c,mix(L[3].rgb,L[4].rgb,st.a),a.b);
1150
+ c=mix(c,L[2].rgb,a.g);
1151
+ c=mix(c,L[1].rgb,a.r);
1152
+ c=mix(c,L[5].rgb,a.a);
1153
+ let wet=max(st.g,L[0].w*clamp(st.b+a.r+a.g,0.0,1.0));
1154
+ c*=mix(1.0,L[1].w,wet);
1155
+ groundGloss${a}=L[2].w*wet;
1156
+
1157
+ let heat=st.r*a.a;
1158
+ if(heat>0.0){
1159
+ let q=terrainNoise(p.xz/max(L[5].w,0.001)).x;let q2=terrainNoise(p.xz/max(L[5].w,0.001)*2.3+vec2f(17.0)).x;
1160
+ let crack=1.0-smoothstep(0.03,0.12,abs(q-0.5)*0.7+abs(q2-0.5)*0.3);
1161
+ let open=mix(1.0,crack,L[4].w*smoothstep(0.9,0.3,heat));
1162
+ groundGlow${a}=mix(vec3f(0.9,0.06,0.005),vec3f(1.0,0.5,0.1),heat)*heat*heat*L[3].w*open;
1163
+ }
1164
+ return c;
1165
+ }
1166
+
1167
+ fn groundTilt${a}(i:Output)->Output {
1168
+ var j=i;if(frame.offset.w<0.5){return j;}
1169
+ let uv=(i.position.xz-frame.map.xy)/frame.map.zw;if(any(uv<vec2f(0))||any(uv>vec2f(1))){return j;}
1170
+ let f=uv*groundLook${a}[7].xy;let lo=vec2i(floor(f));let w=f-floor(f);
1171
+ let g=mix(mix(groundSlope${a}(lo),groundSlope${a}(lo+vec2i(1,0)),w.x),mix(groundSlope${a}(lo+vec2i(0,1)),groundSlope${a}(lo+vec2i(1,1)),w.x),w.y);
1172
+ if(g.x==0.0&&g.y==0.0){return j;}
1173
+ let n=normalize(i.normal);let ny=max(n.y,1e-4);let h=vec2f(-n.x/ny+g.x,-n.z/ny+g.y);
1174
+ j.normal=normalize(vec3f(-h.x,1.0,-h.y));return j;
1175
+ }
1176
+ `, c = e.replace(Z, `fn terrainColorUnchanged${a}(position:vec3f,normal:vec3f)->vec3f {`).replace(X, `fn groundChangeInner${a}(i:Output)->vec4f {`).replace("let a=i.material.x*i.material.x;", `let groundRough${a}=i.material.x*(1.0-groundGloss${a});let a=groundRough${a}*groundRough${a};`).replace(/let linear=([^;]+);/, `let linear=$1+groundGlow${a};`);
1177
+ return c = (c.includes("fn terrainNoise(") ? "" : n) + s + c + `\n${Z}return groundShade${a}(position,terrainColorUnchanged${a}(position,normal));}\n${X}return groundChangeInner${a}(groundTilt${a}(i));}`, this.#E.size >= 16 && this.#E.clear(), this.#E.set(r, c), c;
1178
+ }
1179
+ #se(e) {
1180
+ let t = this.look, n = this.#L, r = t.rain ?? 0, i = [
1181
+ ...t.soil,
1182
+ r,
1183
+ ...t.mud,
1184
+ t.wetDarkening,
1185
+ ...t.ash,
1186
+ t.wetGloss,
1187
+ ...t.char,
1188
+ t.glow,
1189
+ ...t.ashen,
1190
+ t.cracks,
1191
+ ...t.lava,
1192
+ t.crackSize,
1193
+ this.origin[0],
1194
+ this.origin[1],
1195
+ this.size[0],
1196
+ this.size[1],
1197
+ this.sides[0] - 1,
1198
+ this.sides[1] - 1,
1199
+ 0,
1200
+ 0
1201
+ ], a = !this.#R;
1202
+ for (let e = 0; e < 32; e++) n[e] !== Math.fround(i[e]) && (n[e] = i[e], a = !1);
1203
+ a || (this.#R = !1, e.queue.writeBuffer(this.#I, 0, n));
1204
+ }
1205
+ #ce(e, r) {
1206
+ let i = t(e);
1207
+ if (!i) return e;
1208
+ let { code: a, at: o } = i, s = "_" + r, c = this.#H, l = a.indexOf(`if(i.terrainId==${r + 1}.0){let s=terrainSurface`);
1209
+ l >= 0 && (o = a.indexOf("n=s.normal;}", l) + 12);
1210
+ let u = `
1211
+ @group(0) @binding(${c}) var groundSurface${s}:texture_2d<f32>;
1212
+ @group(0) @binding(${c + 1}) var groundState${s}:texture_2d<f32>;
1213
+ @group(0) @binding(${c + 2}) var<uniform> groundLook${s}:array<vec4f,8>;
1214
+ var<private> groundGlow${s}:vec3f;
1215
+ fn groundTexel${s}(t:texture_2d<f32>,f:vec2f)->vec4f {
1216
+ let n=vec2i(groundLook${s}[7].xy);let lo=vec2i(floor(f));let hi=min(lo+1,n);let w=f-floor(f);
1217
+ return mix(mix(textureLoad(t,lo,0),textureLoad(t,vec2i(hi.x,lo.y),0),w.x),mix(textureLoad(t,vec2i(lo.x,hi.y),0),textureLoad(t,hi,0),w.x),w.y);
1218
+ }
1219
+ fn groundShade${s}(p:vec3f,color:vec3f,rough:f32)->vec4f {
1220
+ let L=groundLook${s};let g=L[6];let uv=(p.xz-g.xy)/g.zw;
1221
+ if(any(uv<vec2f(0))||any(uv>vec2f(1))){return vec4f(color,rough);}
1222
+ let f=uv*L[7].xy;let a=groundTexel${s}(groundSurface${s},f);let st=groundTexel${s}(groundState${s},f);
1223
+ var c=mix(color,L[0].rgb,st.b);
1224
+ c=mix(c,mix(L[3].rgb,L[4].rgb,st.a),a.b);
1225
+ c=mix(c,L[2].rgb,a.g);
1226
+ c=mix(c,L[1].rgb,a.r);
1227
+ c=mix(c,L[5].rgb,a.a);
1228
+ let wet=max(st.g,L[0].w*clamp(st.b+a.r+a.g,0.0,1.0));
1229
+ c*=mix(1.0,L[1].w,wet);
1230
+
1231
+ let heat=st.r*a.a;
1232
+ if(heat>0.0){
1233
+ let q=terrainNoise(p.xz/max(L[5].w,0.001)).x;let q2=terrainNoise(p.xz/max(L[5].w,0.001)*2.3+vec2f(17.0)).x;
1234
+ let crack=1.0-smoothstep(0.03,0.12,abs(q-0.5)*0.7+abs(q2-0.5)*0.3);
1235
+ let open=mix(1.0,crack,L[4].w*smoothstep(0.9,0.3,heat));
1236
+ groundGlow${s}=mix(vec3f(0.9,0.06,0.005),vec3f(1.0,0.5,0.1),heat)*heat*heat*L[3].w*open;
1237
+ }
1238
+ return vec4f(c,rough*(1.0-L[2].w*wet));
1239
+ }
1240
+ `;
1241
+ a = (a.includes("fn terrainNoise(") ? "" : n) + u + a.slice(0, o) + `if(i.terrainId==${r + 1}.0){let gs=groundShade${s}(i.position,i.color,i.material.x);i.color=gs.rgb;i.material.x=gs.a;}` + a.slice(o);
1242
+ let d = a.replace(/let linear=([^;]+);/, `let linear=$1+groundGlow${s};`);
1243
+ return d === a ? a : d;
1244
+ }
1245
+ onDeviceRestored(e) {
1246
+ this.#le(), this.#m = void 0, this.setup(e), this.#d.push({
1247
+ x0: 0,
1248
+ z0: 0,
1249
+ x1: this.sides[0] - 1,
1250
+ z1: this.sides[1] - 1
1251
+ });
1252
+ }
1253
+ #le() {
1254
+ this.#P?.destroy(), this.#F?.destroy(), this.#I?.destroy(), this.#C?.destroy(), this.#D?.destroy(), this.#O?.destroy(), this.#A?.destroy(), this.#A = void 0, this.#P = this.#F = this.#I = this.#C = void 0, this.#D = this.#O = void 0, this.#w = [], this.#z = [], this.#B = "", this.#W = void 0;
1255
+ }
1256
+ dispose() {
1257
+ if (this.disposed) return;
1258
+ let e = this.#g, t = e && !e.disposed ? e.heightmap : void 0, n = this.#m;
1259
+ if (t && this.#_ && n && !n.disposed && n.gpu) {
1260
+ let [r, i] = this.sides;
1261
+ n.gpu.queue.writeTexture({ texture: t }, this.#_, { bytesPerRow: r * 4 }, [r, i]), e.invalidate(), this.options.shadows?.invalidate();
1262
+ }
1263
+ this.#x?.(), this.#x = void 0, this.#S?.(), this.#S = void 0, this.#g = void 0, this.#E.clear();
1264
+ let r = this.#h ? a(this.#h) : void 0, i = r?.geometry, o = this.#M;
1265
+ if (i && o && this.#N) {
1266
+ let e = i.vertices;
1267
+ for (let t = 0; t < o.length; t++) {
1268
+ let n = o[t];
1269
+ e[n * 6 + 1] = this.#i[t], e[n * 6 + 3] = this.#N[t * 3], e[n * 6 + 4] = this.#N[t * 3 + 1], e[n * 6 + 5] = this.#N[t * 3 + 2];
1270
+ }
1271
+ let t = this.#m, n = t && !t.disposed && t.meshes.get(r.id) === r && r.geometry === i;
1272
+ n && this.#W && t.gpu?.queue.writeBuffer(this.#W, 0, e), this.#G && (i.bounds.set(this.#G), n && t.store.bounds.set(this.#G, r.id * 4)), this.options.shadows?.invalidate();
1273
+ }
1274
+ this.disposed = !0, this.#le();
1275
+ let s = this.#m;
1276
+ this.#m = void 0, this.#h = void 0, this.#p.clear(), s?.sceneOf(this)?.removePlugin(this.name);
1277
+ }
1278
+ [Symbol.dispose ?? Symbol()]() {
1279
+ this.dispose();
1280
+ }
1281
+ };
1282
+ function oe(e) {
1283
+ let t = {};
1284
+ for (let [n, r] of Object.entries(e)) r !== void 0 && n in Q && (t[n] = Array.isArray(r) ? [...r] : r);
1285
+ return t;
1286
+ }
1287
+ function se(e) {
1288
+ return typeof e?.addLighting == "function";
1289
+ }
1290
+ //#endregion
1291
+ export { c as COVER_CHANNELS, s as GROUND_DURATIONS, te as GROUND_LOOK_DEFAULTS, ae as GroundChange, N as crustThickness, D as groundNoise, R as gullyVolume, M as lengthToBreadth, L as resolveEvent, j as runoutProgress, W as sampleEvent, A as scourProgress, B as slideVolume };