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