@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
package/dist/stream.js
ADDED
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const t=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./src-Cr7Gm4n0.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
|
+
import { MargoError as n } from "@margoengine/math/errors";
|
|
7
|
+
//#region src/stream-window.ts
|
|
8
|
+
var r = (e, t) => {
|
|
9
|
+
let n = new Uint8Array(e * 4);
|
|
10
|
+
for (let e = 0; e < n.length; e++) n[e] = t[e % 4];
|
|
11
|
+
return n;
|
|
12
|
+
}, i = (e, t, n, r, i, a) => {
|
|
13
|
+
let o = (t, r) => {
|
|
14
|
+
if (typeof a == "number") e.fill(a, t, r);
|
|
15
|
+
else for (let i = t; i < r; i++) e[i] = a[(i - t) % n];
|
|
16
|
+
}, s = Math.max(0, -r), c = Math.min(t, t - r);
|
|
17
|
+
for (let a = 0; a < t; a++) {
|
|
18
|
+
let l = i >= 0 ? a : t - 1 - a, u = l + i, d = l * t * n;
|
|
19
|
+
u < 0 || u >= t || c <= s ? o(d, d + t * n) : (e.copyWithin(d + s * n, (u * t + s + r) * n, (u * t + c + r) * n), o(d, d + s * n), o(d + c * n, d + t * n));
|
|
20
|
+
}
|
|
21
|
+
}, a = class {
|
|
22
|
+
#e;
|
|
23
|
+
#t;
|
|
24
|
+
#n;
|
|
25
|
+
#r;
|
|
26
|
+
#i;
|
|
27
|
+
#a;
|
|
28
|
+
#o;
|
|
29
|
+
#s;
|
|
30
|
+
#c;
|
|
31
|
+
#l;
|
|
32
|
+
#u;
|
|
33
|
+
#d;
|
|
34
|
+
#f;
|
|
35
|
+
constructor(e, t, n, i, a) {
|
|
36
|
+
this.tile = [0, 0], this.origin = [0, 0], this.loading = 0, this.version = 0, this.#r = /* @__PURE__ */ new Map(), this.#i = /* @__PURE__ */ new Map(), this.#a = /* @__PURE__ */ new Set(), this.#o = !1;
|
|
37
|
+
let o = e;
|
|
38
|
+
this.#e = o, this.#t = i, this.#n = a, this.columns = o.window * o.tileTexels;
|
|
39
|
+
let s = this.columns * this.columns;
|
|
40
|
+
this.heights = new Float32Array(s).fill(o.fill), this.weights = Array.from({ length: o.weights }, () => new Uint8Array(s * 4));
|
|
41
|
+
let c = [
|
|
42
|
+
128,
|
|
43
|
+
255,
|
|
44
|
+
128,
|
|
45
|
+
255
|
|
46
|
+
], l = [
|
|
47
|
+
0,
|
|
48
|
+
0,
|
|
49
|
+
0,
|
|
50
|
+
0
|
|
51
|
+
];
|
|
52
|
+
this.#l = this.weights.map((e, t) => ({
|
|
53
|
+
data: e,
|
|
54
|
+
fill: l,
|
|
55
|
+
format: "rgba8unorm",
|
|
56
|
+
layer: 0,
|
|
57
|
+
pick: (e) => e.weights[t]
|
|
58
|
+
})), o.normals && (this.normals = r(s, c), this.#l.push({
|
|
59
|
+
data: this.normals,
|
|
60
|
+
fill: c,
|
|
61
|
+
format: "rgba8unorm",
|
|
62
|
+
layer: 0,
|
|
63
|
+
pick: (e) => e.normals
|
|
64
|
+
})), o.tint && (this.tint = new Uint8Array(s * 4), this.#l.push({
|
|
65
|
+
data: this.tint,
|
|
66
|
+
fill: l,
|
|
67
|
+
format: "rgba8unorm-srgb",
|
|
68
|
+
layer: 0,
|
|
69
|
+
pick: (e) => e.tint
|
|
70
|
+
})), this.#p(Math.round((t - o.lattice[0]) / o.tileSize - o.window / 2), Math.round((n - o.lattice[1]) / o.tileSize - o.window / 2));
|
|
71
|
+
}
|
|
72
|
+
get textures() {
|
|
73
|
+
let e = this.weights.length, t = this.#l;
|
|
74
|
+
return {
|
|
75
|
+
height: this.#c,
|
|
76
|
+
weights: t.slice(0, e).map((e) => e.texture),
|
|
77
|
+
normals: this.normals ? t[e].texture : void 0,
|
|
78
|
+
tint: this.tint ? t[t.length - 1].texture : void 0
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
resident(e, t) {
|
|
82
|
+
return this.#r.get(e + "," + t)?.state === "resident";
|
|
83
|
+
}
|
|
84
|
+
holds(e, t) {
|
|
85
|
+
let n = this.#r.get(e + "," + t);
|
|
86
|
+
return n?.state === "resident" && n.data;
|
|
87
|
+
}
|
|
88
|
+
onChange(e) {
|
|
89
|
+
if (typeof e != "function") throw TypeError("Invalid terrain stream listener; pass a function.");
|
|
90
|
+
return this.#a.add(e), () => this.#a.delete(e);
|
|
91
|
+
}
|
|
92
|
+
retry() {
|
|
93
|
+
let e = 0;
|
|
94
|
+
for (let [t, n] of this.#r) (n.state === "failed" || n.state === "waiting") && (n.state === "waiting" && clearTimeout(n.timer), this.#r.delete(t), this.#i.delete(t), e++);
|
|
95
|
+
return e;
|
|
96
|
+
}
|
|
97
|
+
state() {
|
|
98
|
+
let e = 0, t;
|
|
99
|
+
for (let n of this.#r.values()) n.state === "failed" ? t ??= { error: n.error } : n.state === "resident" && e++;
|
|
100
|
+
return {
|
|
101
|
+
missing: e < this.#e.window ** 2,
|
|
102
|
+
failed: t
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
follow(e, t) {
|
|
106
|
+
let n = this.#e, r = n.window / 2, i = (e - n.lattice[0]) / n.tileSize - this.tile[0] - r, a = (t - n.lattice[1]) / n.tileSize - this.tile[1] - r;
|
|
107
|
+
if (Number.isFinite(i) && Number.isFinite(a)) {
|
|
108
|
+
let e = Math.abs(i) > .75 ? Math.round(i) : 0, t = Math.abs(a) > .75 ? Math.round(a) : 0;
|
|
109
|
+
return (e || t) && this.#h(e, t), [this.tile[0] + r + i - e, this.tile[1] + r + a - t];
|
|
110
|
+
}
|
|
111
|
+
return [this.tile[0] + r, this.tile[1] + r];
|
|
112
|
+
}
|
|
113
|
+
#p(e, t) {
|
|
114
|
+
let n = this.#e;
|
|
115
|
+
this.tile[0] = e, this.tile[1] = t, this.origin[0] = n.lattice[0] + e * n.tileSize, this.origin[1] = n.lattice[1] + t * n.tileSize;
|
|
116
|
+
}
|
|
117
|
+
#m(e, t) {
|
|
118
|
+
let n = this.#e.window;
|
|
119
|
+
return e >= this.tile[0] && t >= this.tile[1] && e < this.tile[0] + n && t < this.tile[1] + n;
|
|
120
|
+
}
|
|
121
|
+
#h(e, t) {
|
|
122
|
+
let n = this.#e.tileTexels, r = this.#e.window, a = this.columns, o = e * n, s = t * n;
|
|
123
|
+
i(this.heights, a, 1, o, s, this.#e.fill);
|
|
124
|
+
for (let e of this.#l) i(e.data, a, 4, o, s, e.fill);
|
|
125
|
+
this.#p(this.tile[0] + e, this.tile[1] + t);
|
|
126
|
+
for (let [e, t] of this.#r) {
|
|
127
|
+
let [n, r] = e.split(",").map(Number);
|
|
128
|
+
this.#m(n, r) || (t.state === "loading" && t.controller.abort(), t.state === "waiting" && clearTimeout(t.timer), this.#r.delete(e), this.#i.delete(e));
|
|
129
|
+
}
|
|
130
|
+
let c = this.#s, l = c.createCommandEncoder(), u = a - Math.abs(o), d = a - Math.abs(s), f = [[
|
|
131
|
+
this.#c,
|
|
132
|
+
0,
|
|
133
|
+
this.#u,
|
|
134
|
+
this.#f
|
|
135
|
+
], ...this.#l.map((e) => [
|
|
136
|
+
e.texture,
|
|
137
|
+
e.layer,
|
|
138
|
+
this.#d,
|
|
139
|
+
e.fillTexture
|
|
140
|
+
])];
|
|
141
|
+
for (let [i, a, c, p] of f) {
|
|
142
|
+
if (u > 0 && d > 0) {
|
|
143
|
+
let e = [Math.max(0, -o), Math.max(0, -s)];
|
|
144
|
+
l.copyTextureToTexture({
|
|
145
|
+
texture: i,
|
|
146
|
+
origin: [
|
|
147
|
+
Math.max(0, o),
|
|
148
|
+
Math.max(0, s),
|
|
149
|
+
a
|
|
150
|
+
]
|
|
151
|
+
}, {
|
|
152
|
+
texture: c,
|
|
153
|
+
origin: e
|
|
154
|
+
}, [u, d]), l.copyTextureToTexture({
|
|
155
|
+
texture: c,
|
|
156
|
+
origin: e
|
|
157
|
+
}, {
|
|
158
|
+
texture: i,
|
|
159
|
+
origin: [
|
|
160
|
+
e[0],
|
|
161
|
+
e[1],
|
|
162
|
+
a
|
|
163
|
+
]
|
|
164
|
+
}, [u, d]);
|
|
165
|
+
}
|
|
166
|
+
for (let o = 0; o < r; o++) for (let s = 0; s < r; s++) {
|
|
167
|
+
let c = s + e, u = o + t;
|
|
168
|
+
c >= 0 && u >= 0 && c < r && u < r || l.copyTextureToTexture({ texture: p }, {
|
|
169
|
+
texture: i,
|
|
170
|
+
origin: [
|
|
171
|
+
s * n,
|
|
172
|
+
o * n,
|
|
173
|
+
a
|
|
174
|
+
]
|
|
175
|
+
}, [n, n]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
c.queue.submit([l.finish()]), this.#y({
|
|
179
|
+
shifted: !0,
|
|
180
|
+
u: 0,
|
|
181
|
+
v: 0,
|
|
182
|
+
width: a,
|
|
183
|
+
depth: a
|
|
184
|
+
}), this.#n();
|
|
185
|
+
}
|
|
186
|
+
schedule(e, t, n) {
|
|
187
|
+
let r = this.#e.window;
|
|
188
|
+
if (n <= 0 || this.#r.size >= r * r || this.#e.signal?.aborted) return;
|
|
189
|
+
let i = [];
|
|
190
|
+
for (let n = 0; n < r; n++) for (let a = 0; a < r; a++) {
|
|
191
|
+
let r = this.tile[0] + a, o = this.tile[1] + n;
|
|
192
|
+
this.#r.has(r + "," + o) || i.push([
|
|
193
|
+
(r + .5 - e) ** 2 + (o + .5 - t) ** 2,
|
|
194
|
+
r,
|
|
195
|
+
o
|
|
196
|
+
]);
|
|
197
|
+
}
|
|
198
|
+
i.sort((e, t) => e[0] - t[0]);
|
|
199
|
+
for (let [, e, t] of i) {
|
|
200
|
+
if (n-- <= 0) break;
|
|
201
|
+
this.#g(e, t);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
#g(e, t) {
|
|
205
|
+
let n = e + "," + t, r = new AbortController(), i = {
|
|
206
|
+
state: "loading",
|
|
207
|
+
controller: r
|
|
208
|
+
}, a = this.#e.signal ? AbortSignal.any([this.#e.signal, r.signal]) : r.signal;
|
|
209
|
+
this.#r.set(n, i), this.loading++;
|
|
210
|
+
let o;
|
|
211
|
+
try {
|
|
212
|
+
o = Promise.resolve(this.#e.load(e, t, a));
|
|
213
|
+
} catch (e) {
|
|
214
|
+
o = Promise.reject(e);
|
|
215
|
+
}
|
|
216
|
+
o.then((e) => {
|
|
217
|
+
this.#r.get(n) !== i || this.#o || (this.#i.delete(n), this.#r.set(n, e ? {
|
|
218
|
+
state: "queued",
|
|
219
|
+
tile: this.#v(e),
|
|
220
|
+
row: 0
|
|
221
|
+
} : {
|
|
222
|
+
state: "resident",
|
|
223
|
+
data: !1
|
|
224
|
+
}));
|
|
225
|
+
}, (e) => {
|
|
226
|
+
this.#r.get(n) !== i || this.#o || this.#_(n, e, a.aborted);
|
|
227
|
+
}).catch((e) => {
|
|
228
|
+
this.#r.get(n) === i && this.#r.set(n, {
|
|
229
|
+
state: "failed",
|
|
230
|
+
error: e
|
|
231
|
+
});
|
|
232
|
+
}).finally(() => {
|
|
233
|
+
this.loading--, this.#n();
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
#_(e, t, n) {
|
|
237
|
+
let r = this.#i.get(e) ?? 0;
|
|
238
|
+
if (n || r >= this.#e.retries) {
|
|
239
|
+
this.#r.set(e, {
|
|
240
|
+
state: "failed",
|
|
241
|
+
error: t
|
|
242
|
+
});
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
this.#i.set(e, r + 1);
|
|
246
|
+
let i = Math.random() * Math.min(3e4, this.#e.retryDelay * 2 ** r), a = {
|
|
247
|
+
state: "waiting",
|
|
248
|
+
timer: setTimeout(() => {
|
|
249
|
+
this.#r.get(e) === a && this.#r.delete(e);
|
|
250
|
+
}, i)
|
|
251
|
+
};
|
|
252
|
+
this.#r.set(e, a);
|
|
253
|
+
}
|
|
254
|
+
#v(e) {
|
|
255
|
+
let t = this.#e.tileTexels, n = t * t, r = this.weights.length, i = (e) => e instanceof Uint8Array && e.length === n * 4;
|
|
256
|
+
if (!(e.heights instanceof Float32Array) || e.heights.length !== n || !e.heights.every(Number.isFinite) || r > 0 && (!Array.isArray(e.weights) || e.weights.length !== r || e.weights.some((e) => !i(e))) || this.normals && !i(e.normals) || e.tint !== void 0 && (!this.tint || !i(e.tint))) throw RangeError(`Invalid terrain stream tile; give ${n} finite float32 heights, ${r} weight arrays of ${n * 4} bytes` + (this.normals ? `, normals of ${n * 4} bytes` : "") + (this.tint ? ` and, if any, a tint of ${n * 4} bytes.` : " and no tint (the stream has no tint option)."));
|
|
257
|
+
return e;
|
|
258
|
+
}
|
|
259
|
+
upload(e, t, n) {
|
|
260
|
+
let i = this.#e.tileTexels, a = this.columns, o = this.#s.queue, s = n, c = !1;
|
|
261
|
+
for (; s > 0;) {
|
|
262
|
+
let n, l = Infinity;
|
|
263
|
+
for (let [r, i] of this.#r) {
|
|
264
|
+
if (i.state !== "queued") continue;
|
|
265
|
+
let [a, o] = r.split(",").map(Number), s = (a + .5 - e) ** 2 + (o + .5 - t) ** 2;
|
|
266
|
+
s < l && (l = s, n = {
|
|
267
|
+
key: r,
|
|
268
|
+
slot: i,
|
|
269
|
+
x: a,
|
|
270
|
+
z: o
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
if (!n) break;
|
|
274
|
+
let { slot: u, x: d, z: f } = n, p = u.row, m = Math.min(i, p + Math.max(1, Math.floor(s / i))), h = (d - this.tile[0]) * i, g = (f - this.tile[1]) * i + p, _ = u.tile;
|
|
275
|
+
for (let e = p; e < m; e++) {
|
|
276
|
+
this.heights.set(_.heights.subarray(e * i, (e + 1) * i), (g + e - p) * a + h);
|
|
277
|
+
for (let t of this.#l) {
|
|
278
|
+
let n = t.pick(_), r = ((g + e - p) * a + h) * 4;
|
|
279
|
+
if (n) t.data.set(n.subarray(e * i * 4, (e + 1) * i * 4), r);
|
|
280
|
+
else for (let e = 0; e < i * 4; e++) t.data[r + e] = t.fill[e % 4];
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
o.writeTexture({
|
|
284
|
+
texture: this.#c,
|
|
285
|
+
origin: [h, g]
|
|
286
|
+
}, _.heights, {
|
|
287
|
+
offset: p * i * 4,
|
|
288
|
+
bytesPerRow: i * 4
|
|
289
|
+
}, [i, m - p]);
|
|
290
|
+
for (let e of this.#l) o.writeTexture({
|
|
291
|
+
texture: e.texture,
|
|
292
|
+
origin: [
|
|
293
|
+
h,
|
|
294
|
+
g,
|
|
295
|
+
e.layer
|
|
296
|
+
]
|
|
297
|
+
}, e.pick(_) ?? (e.fillTile ??= r(i * i, e.fill)), {
|
|
298
|
+
offset: p * i * 4,
|
|
299
|
+
bytesPerRow: i * 4
|
|
300
|
+
}, [i, m - p]);
|
|
301
|
+
s -= (m - p) * i, u.row = m, m === i && (this.#r.set(n.key, {
|
|
302
|
+
state: "resident",
|
|
303
|
+
data: !0
|
|
304
|
+
}), c = !0), this.#y({
|
|
305
|
+
shifted: !1,
|
|
306
|
+
u: h,
|
|
307
|
+
v: g,
|
|
308
|
+
width: i,
|
|
309
|
+
depth: m - p
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
return c && this.#n(), n - s;
|
|
313
|
+
}
|
|
314
|
+
#y(e) {
|
|
315
|
+
this.version++, this.#t();
|
|
316
|
+
for (let t of [...this.#a]) t(e);
|
|
317
|
+
}
|
|
318
|
+
create(e) {
|
|
319
|
+
let t = this.columns, n = this.#e.tileTexels;
|
|
320
|
+
if (t > e.limits.maxTextureDimension2D) throw RangeError(`Terrain stream window of ${t} texels is above this device's 2D texture limit (${e.limits.maxTextureDimension2D}); use a smaller window or tileTexels.`);
|
|
321
|
+
this.release();
|
|
322
|
+
let i = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST, a = GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST;
|
|
323
|
+
this.#s = e, this.#c = e.createTexture({
|
|
324
|
+
size: [t, t],
|
|
325
|
+
format: "r32float",
|
|
326
|
+
usage: i
|
|
327
|
+
}), e.queue.writeTexture({ texture: this.#c }, this.heights, { bytesPerRow: t * 4 }, [t, t]), this.#u = e.createTexture({
|
|
328
|
+
size: [t, t],
|
|
329
|
+
format: "r32float",
|
|
330
|
+
usage: a
|
|
331
|
+
}), this.#f = e.createTexture({
|
|
332
|
+
size: [n, n],
|
|
333
|
+
format: "r32float",
|
|
334
|
+
usage: a
|
|
335
|
+
}), e.queue.writeTexture({ texture: this.#f }, new Float32Array(n * n).fill(this.#e.fill), { bytesPerRow: n * 4 }, [n, n]);
|
|
336
|
+
let o = this.#e.weightArray ? this.weights.length : 0, s;
|
|
337
|
+
for (let c = 0; c < this.#l.length; c++) {
|
|
338
|
+
let l = this.#l[c];
|
|
339
|
+
l.layer = c < o ? c : 0, l.texture = c < o ? s ??= e.createTexture({
|
|
340
|
+
size: [
|
|
341
|
+
t,
|
|
342
|
+
t,
|
|
343
|
+
o
|
|
344
|
+
],
|
|
345
|
+
format: l.format,
|
|
346
|
+
usage: i,
|
|
347
|
+
textureBindingViewDimension: "2d-array"
|
|
348
|
+
}) : e.createTexture({
|
|
349
|
+
size: [t, t],
|
|
350
|
+
format: l.format,
|
|
351
|
+
usage: i
|
|
352
|
+
}), e.queue.writeTexture({
|
|
353
|
+
texture: l.texture,
|
|
354
|
+
origin: [
|
|
355
|
+
0,
|
|
356
|
+
0,
|
|
357
|
+
l.layer
|
|
358
|
+
]
|
|
359
|
+
}, l.data, { bytesPerRow: t * 4 }, [t, t]), l.fillTexture = e.createTexture({
|
|
360
|
+
size: [n, n],
|
|
361
|
+
format: l.format,
|
|
362
|
+
usage: a
|
|
363
|
+
}), e.queue.writeTexture({ texture: l.fillTexture }, r(n * n, l.fill), { bytesPerRow: n * 4 }, [n, n]);
|
|
364
|
+
}
|
|
365
|
+
this.#l.length && (this.#d = e.createTexture({
|
|
366
|
+
size: [t, t],
|
|
367
|
+
format: "rgba8unorm",
|
|
368
|
+
usage: a
|
|
369
|
+
}));
|
|
370
|
+
}
|
|
371
|
+
release() {
|
|
372
|
+
this.#u?.destroy(), this.#d?.destroy();
|
|
373
|
+
for (let e of this.#l) e.fillTexture?.destroy();
|
|
374
|
+
this.#f?.destroy(), this.#u = this.#d = this.#f = void 0;
|
|
375
|
+
for (let e of this.#l) e.fillTexture = void 0;
|
|
376
|
+
}
|
|
377
|
+
destroyTextures() {
|
|
378
|
+
this.#c?.destroy();
|
|
379
|
+
for (let e of this.#l) e.texture?.destroy();
|
|
380
|
+
}
|
|
381
|
+
dispose() {
|
|
382
|
+
this.#o = !0;
|
|
383
|
+
for (let e of this.#r.values()) e.state === "loading" ? e.controller.abort() : e.state === "waiting" && clearTimeout(e.timer);
|
|
384
|
+
this.#r.clear(), this.#i.clear(), this.#a.clear(), this.release(), this.#s = this.#c = void 0;
|
|
385
|
+
for (let e of this.#l) e.texture = void 0;
|
|
386
|
+
}
|
|
387
|
+
}, o = [
|
|
388
|
+
0,
|
|
389
|
+
0,
|
|
390
|
+
0
|
|
391
|
+
], s = 0, c = (e, t, n) => Number.isInteger(e) && e >= t && e <= n, l = class r {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
392
|
+
#e;
|
|
393
|
+
#t;
|
|
394
|
+
#n;
|
|
395
|
+
#r;
|
|
396
|
+
#i;
|
|
397
|
+
#a;
|
|
398
|
+
#o;
|
|
399
|
+
constructor(t, n) {
|
|
400
|
+
this.name = "terrain-stream:" + s++, this.disposed = !1, this.#i = /* @__PURE__ */ new Set(), this.#o = !1, this.#c = (e) => {
|
|
401
|
+
this.#t.create(e), this.#n?.destroyTextures(), this.#n?.create(e), this.#o = !0;
|
|
402
|
+
let t = this.#t.textures, n = t.weights, r = this.#r?.source(e);
|
|
403
|
+
return {
|
|
404
|
+
heightmap: t.height,
|
|
405
|
+
layers: this.#e.colors(e),
|
|
406
|
+
...n.length ? { weights: n.length === 1 ? [n[0], n[0]] : n } : {},
|
|
407
|
+
...t.normals ? { normalmap: t.normals } : {},
|
|
408
|
+
...t.tint ? { tint: t.tint } : {},
|
|
409
|
+
...r ? { source: r } : {}
|
|
410
|
+
};
|
|
411
|
+
};
|
|
412
|
+
let r = n;
|
|
413
|
+
if (!r || typeof r.load != "function" || typeof r.colors != "function") throw TypeError("Invalid terrain stream; pass load(x, z, signal) and colors(device) functions.");
|
|
414
|
+
let i = r.origin ?? [0, 0];
|
|
415
|
+
if (!(r.tileSize > 0 && Number.isFinite(Math.fround(r.tileSize))) || !c(r.tileTexels, 2, 1024) || !c(r.window ?? 4, 2, 16) || (r.window ?? 4) % 2 || !c(r.weights ?? 0, 0, 4) || r.weights && r.weights * 4 < (r.layers?.length ?? 0) || !c(r.budget ?? 65536, 1, 2 ** 26) || !c(r.concurrency ?? 4, 1, 64) || !Number.isFinite(Math.fround(r.fill ?? 0)) || !c(r.retries ?? 2, 0, 16) || !((r.retryDelay ?? 250) >= 0 && Number.isFinite(r.retryDelay ?? 250)) || !Array.isArray(i) || !i.every((e) => Number.isFinite(e))) throw RangeError("Invalid terrain stream options; give tileSize above 0, tileTexels 2..1024, an even window 2..16, weights 0..4 (4 layers each), budget and concurrency (1..64) whole numbers, retries 0..16, a retryDelay of 0 ms or more, and a finite fill and origin.");
|
|
416
|
+
if (r.far !== void 0 && (typeof r.far?.plan != "function" || typeof r.far.bind != "function")) throw TypeError("Invalid terrain stream far ring; pass terrainFar(...) from @margoengine/terrain/far.");
|
|
417
|
+
this.#e = r;
|
|
418
|
+
let o = {
|
|
419
|
+
tileSize: r.tileSize,
|
|
420
|
+
tileTexels: r.tileTexels,
|
|
421
|
+
window: r.window ?? 4
|
|
422
|
+
}, l = r.far?.plan(o), u = t.camera.position, d = r.floatingOrigin?.origin, f = d ? [
|
|
423
|
+
u[0] + d[0],
|
|
424
|
+
u[1] + d[1],
|
|
425
|
+
u[2] + d[2]
|
|
426
|
+
] : u, p = (e, t, n) => new a({
|
|
427
|
+
...e,
|
|
428
|
+
lattice: [i[0], i[1]],
|
|
429
|
+
load: t,
|
|
430
|
+
weights: r.weights ?? 0,
|
|
431
|
+
normals: !!r.normals,
|
|
432
|
+
tint: !!r.tint,
|
|
433
|
+
weightArray: n,
|
|
434
|
+
fill: r.fill ?? 0,
|
|
435
|
+
retries: r.retries ?? 2,
|
|
436
|
+
retryDelay: r.retryDelay ?? 250,
|
|
437
|
+
signal: r.signal
|
|
438
|
+
}, f[0], f[2], () => this.terrain.invalidate(), () => this.#s());
|
|
439
|
+
this.#t = p(o, r.load, !1), this.columns = this.#t.columns, this.heights = this.#t.heights, this.weights = this.#t.weights, this.normals = this.#t.normals, this.tint = this.#t.tint, l && (this.far = this.#n = p(l, r.far.load, !0));
|
|
440
|
+
let m = r.tileSize / r.tileTexels, h = (this.columns - 1) * m, g = [
|
|
441
|
+
"tileSize",
|
|
442
|
+
"tileTexels",
|
|
443
|
+
"window",
|
|
444
|
+
"load",
|
|
445
|
+
"colors",
|
|
446
|
+
"weights",
|
|
447
|
+
"budget",
|
|
448
|
+
"concurrency",
|
|
449
|
+
"fill",
|
|
450
|
+
"signal",
|
|
451
|
+
"retries",
|
|
452
|
+
"retryDelay",
|
|
453
|
+
"normals",
|
|
454
|
+
"tint",
|
|
455
|
+
"far"
|
|
456
|
+
], _ = Object.fromEntries(Object.entries(r).filter(([e]) => !g.includes(e))), v = l && Math.ceil(Math.log2(l.reach / ((r.cellSize ?? 1) * ((r.resolution ?? 64) / 2 - 1)))) + 1;
|
|
457
|
+
this.terrain = new e({
|
|
458
|
+
..._,
|
|
459
|
+
...v && r.levels === void 0 ? { levels: Math.min(12, Math.max(6, v)) } : {},
|
|
460
|
+
clipmapAnchor: r.clipmapAnchor ?? "camera",
|
|
461
|
+
origin: this.#t.origin,
|
|
462
|
+
size: [h, h],
|
|
463
|
+
textures: this.#c
|
|
464
|
+
}), this.#r = r.far?.bind(this.#t, this.#n, r);
|
|
465
|
+
}
|
|
466
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
467
|
+
let i = new r(e, n), a = t(e);
|
|
468
|
+
return a.use(i.terrain), a.use(i), i;
|
|
469
|
+
}
|
|
470
|
+
get origin() {
|
|
471
|
+
return this.#t.origin;
|
|
472
|
+
}
|
|
473
|
+
get tile() {
|
|
474
|
+
return this.#t.tile;
|
|
475
|
+
}
|
|
476
|
+
get version() {
|
|
477
|
+
return this.#t.version;
|
|
478
|
+
}
|
|
479
|
+
resident(e, t) {
|
|
480
|
+
return this.#t.resident(e, t);
|
|
481
|
+
}
|
|
482
|
+
retry() {
|
|
483
|
+
return this.#t.retry() + (this.#n?.retry() ?? 0);
|
|
484
|
+
}
|
|
485
|
+
onChange(e) {
|
|
486
|
+
return this.#t.onChange(e);
|
|
487
|
+
}
|
|
488
|
+
ready(e) {
|
|
489
|
+
return new Promise((t, n) => {
|
|
490
|
+
if (e?.aborted) return n(e.reason);
|
|
491
|
+
let r = () => {
|
|
492
|
+
this.#i.delete(i), n(e.reason);
|
|
493
|
+
}, i = {
|
|
494
|
+
resolve: () => (e?.removeEventListener("abort", r), t()),
|
|
495
|
+
reject: (t) => (e?.removeEventListener("abort", r), n(t))
|
|
496
|
+
};
|
|
497
|
+
e?.addEventListener("abort", r, { once: !0 }), this.#i.add(i), this.#s();
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
setup(e) {
|
|
501
|
+
if (this.disposed || !e.gpu) throw new n(this.disposed ? "disposed" : "unsupported", "Terrain streaming requires WebGPU and a live stream; attach a new TerrainStream to a WebGPU engine");
|
|
502
|
+
this.#a = e;
|
|
503
|
+
}
|
|
504
|
+
onPreUpdate(e = 0) {
|
|
505
|
+
if (this.disposed || !this.#a || !this.#o) return;
|
|
506
|
+
let t = this.#a.camera.position, n = this.#e.floatingOrigin ? this.terrain.shift : o, r = t[0] + n[0], i = t[2] + n[2], a = this.#t, s = this.#n, c = a.follow(r, i), l = s?.follow(r, i), u = this.#e.concurrency ?? 4, d = this.#e.budget ?? 65536;
|
|
507
|
+
s?.schedule(l[0], l[1], u - s.loading - a.loading), a.schedule(c[0], c[1], u - a.loading - (s?.loading ?? 0)), a.upload(c[0], c[1], d - (s?.upload(l[0], l[1], d) ?? 0)), this.#r?.update(e, r, i) && this.terrain.invalidate();
|
|
508
|
+
}
|
|
509
|
+
#s() {
|
|
510
|
+
if (!this.#i.size) return;
|
|
511
|
+
let e, t = !1, r = !1;
|
|
512
|
+
this.#e.signal?.aborted && (t = !0, e = this.#e.signal.reason), this.disposed && (t = !0, e = new n("disposed", "The terrain stream was disposed; attach a new one"));
|
|
513
|
+
for (let n of this.#n ? [this.#t, this.#n] : [this.#t]) {
|
|
514
|
+
let i = n.state();
|
|
515
|
+
i.failed && !t && (t = !0, e = i.failed.error), r ||= i.missing;
|
|
516
|
+
}
|
|
517
|
+
if (!t && r) return;
|
|
518
|
+
let i = [...this.#i];
|
|
519
|
+
this.#i.clear();
|
|
520
|
+
for (let n of i) t ? n.reject(e) : n.resolve();
|
|
521
|
+
}
|
|
522
|
+
#c;
|
|
523
|
+
dispose() {
|
|
524
|
+
this.disposed || (this.disposed = !0, this.#s(), this.#a?.sceneOf(this)?.removePlugin(this.name), this.terrain.dispose(), this.#r?.dispose(), this.#n?.destroyTextures(), this.#t.dispose(), this.#n?.dispose(), this.#a = void 0, this.#o = !1);
|
|
525
|
+
}
|
|
526
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
527
|
+
this.dispose();
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
//#endregion
|
|
531
|
+
export { l as TerrainStream };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface GridDraw {
|
|
2
|
+
readonly vertex: GPUBuffer;
|
|
3
|
+
readonly center: GPUBuffer;
|
|
4
|
+
readonly ring: GPUBuffer;
|
|
5
|
+
readonly centerCount: number;
|
|
6
|
+
readonly ringCount: number;
|
|
7
|
+
}
|
|
8
|
+
interface SurfaceHeight {
|
|
9
|
+
readonly texture: GPUTexture;
|
|
10
|
+
draw(encoder: GPUCommandEncoder, version: number, rings: readonly {
|
|
11
|
+
group: GPUBindGroup;
|
|
12
|
+
}[], geometry: GridDraw): void;
|
|
13
|
+
}
|
|
14
|
+
/** A world-height image of the clipmap triangles. UVs match the source texture including the half-texel border. */
|
|
15
|
+
export declare function surfaceHeight(device: GPUDevice, source: GPUTexture, layout: GPUBindGroupLayout, vertexCode: string, clip: (code: string) => string, floating: boolean): SurfaceHeight | undefined;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { type SettingRow } from './shared/settings.js';
|
|
3
|
+
import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
5
|
+
import type { Terrain, TerrainLighting } from './index.js';
|
|
6
|
+
type Pair = readonly [number, number];
|
|
7
|
+
/** The look of wet ground; every field is live (one small uniform). */
|
|
8
|
+
export interface WetGroundLook {
|
|
9
|
+
/** Albedo of fully wet ground, as a share of dry (1 is no darkening), 0 to 1. Default 0.55. */
|
|
10
|
+
darkening?: number;
|
|
11
|
+
/** Share by which fully wet ground is smoother (shinier), 0 to 1. Default 0.5. */
|
|
12
|
+
gloss?: number;
|
|
13
|
+
/** Seconds over which wetness falls to 1/e once the water has gone, 1 to 604,800 (a week). Default 10,800 (3 hours). */
|
|
14
|
+
dryTime?: number;
|
|
15
|
+
}
|
|
16
|
+
/** Options of {@link WetGround.attach}. */
|
|
17
|
+
export interface WetGroundOptions extends WetGroundLook {
|
|
18
|
+
/**
|
|
19
|
+
* The wetting record, asked every frame (borrowed, never destroyed): an rgba32float 2D texture with TEXTURE_BINDING
|
|
20
|
+
* whose green channel is the seconds since the texel was last wet and blue 1 once it has been wet, as
|
|
21
|
+
* `Flood.wetTexture`. Undefined draws the ground dry.
|
|
22
|
+
*/
|
|
23
|
+
record: () => GPUTexture | undefined;
|
|
24
|
+
/** World [x, z] of the record's first texel's corner (a Flood's `origin`). */
|
|
25
|
+
origin: Pair;
|
|
26
|
+
/** World [width, depth] the record covers (a Flood's `size`). */
|
|
27
|
+
size: Pair;
|
|
28
|
+
/** The first of the two lighting-group bindings the effect declares. Default 116. */
|
|
29
|
+
binding?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Wet ground on a clipmap {@link Terrain} from a wetting record: darker and glossier where water has been, drying back
|
|
33
|
+
* over time. `WetGround.attach(engine, terrain, { record: () => flood.wetTexture, origin, size })`. WebGPU (and the
|
|
34
|
+
* WebGL2 compatibility layer).
|
|
35
|
+
* @throws TypeError for a missing record function or terrain; RangeError for an origin, size, look value or binding out of range.
|
|
36
|
+
* @throws MargoError `'disposed'` after `dispose()`, `'unsupported'` when the terrain's shader has no `terrainColor`.
|
|
37
|
+
*/
|
|
38
|
+
export declare class WetGround implements EnginePlugin, TerrainLighting, Disposable {
|
|
39
|
+
#private;
|
|
40
|
+
readonly name: string;
|
|
41
|
+
/** Whether the effect has been disposed. */
|
|
42
|
+
disposed: boolean;
|
|
43
|
+
/** The current look (live). */
|
|
44
|
+
readonly look: Required<WetGroundLook>;
|
|
45
|
+
readonly origin: Pair;
|
|
46
|
+
readonly size: Pair;
|
|
47
|
+
readonly binding: number;
|
|
48
|
+
constructor(terrain: Terrain, options: WetGroundOptions);
|
|
49
|
+
/** Attaches wet ground to `terrain` on `engine`; the record texture stays borrowed. */
|
|
50
|
+
static attach(engine: EngineKernel, terrain: Terrain, options: WetGroundOptions): WetGround;
|
|
51
|
+
/** Sets any of `darkening`, `gloss` and `dryTime` (others keep theirs); nothing is rebuilt. */
|
|
52
|
+
setLook(look: WetGroundLook): void;
|
|
53
|
+
/** The wetness 0..1 a record texel gives under the current look: blue x exp(-green / dryTime). */
|
|
54
|
+
wetness(sinceWet: number, wasWet: number): number;
|
|
55
|
+
get owner(): Engine | undefined;
|
|
56
|
+
setup(engine: Engine): void;
|
|
57
|
+
onDeviceRestored(engine: Engine): void;
|
|
58
|
+
/** The lighting group entries: the record (or a dry texel) and the look. */
|
|
59
|
+
bindings(): GPUBindGroupEntry[];
|
|
60
|
+
/** The terrain's lit shader with `terrainColor` darkened and the roughness lowered by the wetness. */
|
|
61
|
+
shader(source: string, group: number): string;
|
|
62
|
+
/** Detaches from the terrain (its shader returns to what it was) and frees the uniform; the record stays borrowed. */
|
|
63
|
+
dispose(): void;
|
|
64
|
+
[Symbol.dispose]: () => void;
|
|
65
|
+
}
|
|
66
|
+
/** One wet ground control. */
|
|
67
|
+
export interface WetGroundSetting extends SettingRow {
|
|
68
|
+
readonly group: 'wet';
|
|
69
|
+
readonly default: number;
|
|
70
|
+
}
|
|
71
|
+
/** Every wet ground control, in panel order; defaults are what WetGround uses without the option. */
|
|
72
|
+
export declare const WET_GROUND_SETTINGS: readonly WetGroundSetting[];
|
|
73
|
+
/** The rows that can change every frame (all of them). */
|
|
74
|
+
export declare const WET_GROUND_LIVE_SETTINGS: readonly WetGroundSetting[];
|
|
75
|
+
/** Every setting at its default, as a fresh object to edit (a slider panel's state). */
|
|
76
|
+
export declare function wetGroundSettingDefaults(): Record<string, number>;
|
|
77
|
+
/**
|
|
78
|
+
* Sets the given rows on `ground` (names left out keep their current value). Nothing is rebuilt.
|
|
79
|
+
* @throws TypeError for an unknown name; RangeError for a value out of range.
|
|
80
|
+
*/
|
|
81
|
+
export declare function applyWetGroundSettings(ground: WetGround, values?: Readonly<Record<string, number>>): void;
|
|
82
|
+
export {};
|