@margoengine/terrain 0.1.0 → 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 -201
- package/NOTICE +4 -4
- package/README.md +2367 -1808
- package/dist/bed-mesh.d.ts +40 -0
- package/dist/bed-mesh.js +95 -0
- package/dist/blend.js +4 -4
- package/dist/colors.d.ts +6 -0
- package/dist/deform.d.ts +41 -10
- package/dist/deform.js +169 -87
- package/dist/displace.js +4 -4
- package/dist/elevation.d.ts +182 -0
- package/dist/elevation.js +155 -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 +6 -3
- package/dist/ground-change.d.ts +1 -1
- package/dist/ground-change.js +232 -111
- 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 +83 -1
- package/dist/imagery.js +4 -4
- package/dist/index.d.ts +120 -9
- package/dist/index.js +1 -1
- package/dist/layers-pbr.js +9 -9
- package/dist/overlays.js +4 -4
- package/dist/planet-complex.d.ts +31 -0
- package/dist/planet-craters.d.ts +28 -3
- package/dist/planet-rays.d.ts +37 -0
- package/dist/planet-recipes.d.ts +10 -1
- package/dist/planet-recipes.js +341 -115
- package/dist/planet.d.ts +5 -0
- package/dist/planet.js +112 -104
- package/dist/relief-lighting.d.ts +7 -0
- package/dist/relief-lighting.js +36 -23
- package/dist/rock-surface.d.ts +3 -0
- package/dist/rock-surface.js +28 -16
- package/dist/rvt.js +5 -5
- package/dist/scour.d.ts +90 -0
- package/dist/scour.js +153 -0
- package/dist/sculpt.js +4 -4
- package/dist/shader.d.ts +120 -5
- package/dist/shared/settings.d.ts +89 -89
- package/dist/{src-C6dtSaAb.js → src-Cr7Gm4n0.js} +359 -148
- package/dist/stream-window.d.ts +123 -0
- package/dist/stream.d.ts +95 -26
- package/dist/stream.js +440 -248
- package/dist/wet-ground.js +4 -4
- package/package.json +175 -148
package/dist/stream.js
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const t=(engine,version="
|
|
4
|
-
import { t as e } from "./src-
|
|
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
5
|
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
6
|
import { MargoError as n } from "@margoengine/math/errors";
|
|
7
|
-
//#region src/stream.ts
|
|
8
|
-
var r =
|
|
9
|
-
|
|
10
|
-
0
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
let o =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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));
|
|
17
20
|
}
|
|
18
|
-
},
|
|
21
|
+
}, a = class {
|
|
19
22
|
#e;
|
|
20
23
|
#t;
|
|
21
24
|
#n;
|
|
@@ -29,213 +32,181 @@ var r = [
|
|
|
29
32
|
#u;
|
|
30
33
|
#d;
|
|
31
34
|
#f;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
#_;
|
|
37
|
-
constructor(t, n) {
|
|
38
|
-
this.name = "terrain-stream:" + i++, this.disposed = !1, this.#r = [0, 0], this.#i = [0, 0], this.#a = /* @__PURE__ */ new Map(), this.#o = /* @__PURE__ */ new Set(), this.#s = /* @__PURE__ */ new Set(), this.#d = [], this.#g = 0, this.#_ = 0, this.#D = (e) => {
|
|
39
|
-
let t = this.columns, n = this.#n;
|
|
40
|
-
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.`);
|
|
41
|
-
this.#O();
|
|
42
|
-
let r = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST, i = GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST;
|
|
43
|
-
this.#l = e, this.#u = e.createTexture({
|
|
44
|
-
size: [t, t],
|
|
45
|
-
format: "r32float",
|
|
46
|
-
usage: r
|
|
47
|
-
}), e.queue.writeTexture({ texture: this.#u }, this.heights, { bytesPerRow: t * 4 }, [t, t]), this.#f = e.createTexture({
|
|
48
|
-
size: [t, t],
|
|
49
|
-
format: "r32float",
|
|
50
|
-
usage: i
|
|
51
|
-
}), this.#m = e.createTexture({
|
|
52
|
-
size: [n, n],
|
|
53
|
-
format: "r32float",
|
|
54
|
-
usage: i
|
|
55
|
-
}), e.queue.writeTexture({ texture: this.#m }, new Float32Array(n * n).fill(this.#e.fill ?? 0), { bytesPerRow: n * 4 }, [n, n]), this.#d = this.weights.map((n) => {
|
|
56
|
-
let i = e.createTexture({
|
|
57
|
-
size: [t, t],
|
|
58
|
-
format: "rgba8unorm",
|
|
59
|
-
usage: r
|
|
60
|
-
});
|
|
61
|
-
return e.queue.writeTexture({ texture: i }, n, { bytesPerRow: t * 4 }, [t, t]), i;
|
|
62
|
-
}), this.weights.length && (this.#p = e.createTexture({
|
|
63
|
-
size: [t, t],
|
|
64
|
-
format: "rgba8unorm",
|
|
65
|
-
usage: i
|
|
66
|
-
}), this.#h = e.createTexture({
|
|
67
|
-
size: [n, n],
|
|
68
|
-
format: "rgba8unorm",
|
|
69
|
-
usage: i
|
|
70
|
-
}));
|
|
71
|
-
let a = this.#d;
|
|
72
|
-
return {
|
|
73
|
-
heightmap: this.#u,
|
|
74
|
-
layers: this.#e.colors(e),
|
|
75
|
-
...a.length ? { weights: a.length === 1 ? [a[0], a[0]] : a } : {}
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
let r = n;
|
|
79
|
-
if (!r || typeof r.load != "function" || typeof r.colors != "function") throw TypeError("Invalid terrain stream; pass load(x, z, signal) and colors(device) functions.");
|
|
80
|
-
let o = r.origin ?? [0, 0];
|
|
81
|
-
if (!(r.tileSize > 0 && Number.isFinite(Math.fround(r.tileSize))) || !a(r.tileTexels, 2, 1024) || !a(r.window ?? 4, 2, 16) || (r.window ?? 4) % 2 || !a(r.weights ?? 0, 0, 4) || r.weights && r.weights * 4 < (r.layers?.length ?? 0) || !a(r.budget ?? 65536, 1, 2 ** 26) || !a(r.concurrency ?? 4, 1, 64) || !Number.isFinite(Math.fround(r.fill ?? 0)) || !Array.isArray(o) || !o.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, and a finite fill and origin.");
|
|
82
|
-
this.#e = r, this.#n = r.tileTexels, this.#t = r.window ?? 4, this.columns = this.#t * this.#n;
|
|
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;
|
|
83
39
|
let s = this.columns * this.columns;
|
|
84
|
-
this.heights = new Float32Array(s).fill(
|
|
85
|
-
let c =
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
"
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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));
|
|
110
71
|
}
|
|
111
|
-
|
|
112
|
-
let
|
|
113
|
-
return
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return this.#r;
|
|
120
|
-
}
|
|
121
|
-
get version() {
|
|
122
|
-
return this.#_;
|
|
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
|
+
};
|
|
123
80
|
}
|
|
124
81
|
resident(e, t) {
|
|
125
|
-
return this.#
|
|
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;
|
|
126
87
|
}
|
|
127
88
|
onChange(e) {
|
|
128
89
|
if (typeof e != "function") throw TypeError("Invalid terrain stream listener; pass a function.");
|
|
129
|
-
return this.#
|
|
90
|
+
return this.#a.add(e), () => this.#a.delete(e);
|
|
130
91
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
this.#o.delete(i), n(e.reason);
|
|
136
|
-
}, i = {
|
|
137
|
-
resolve: () => (e?.removeEventListener("abort", r), t()),
|
|
138
|
-
reject: (t) => (e?.removeEventListener("abort", r), n(t))
|
|
139
|
-
};
|
|
140
|
-
e?.addEventListener("abort", r, { once: !0 }), this.#o.add(i), this.#E();
|
|
141
|
-
});
|
|
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;
|
|
142
96
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.#
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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];
|
|
153
110
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return e >= this
|
|
163
|
-
}
|
|
164
|
-
#
|
|
165
|
-
let n = this.#
|
|
166
|
-
|
|
167
|
-
for (let e of this
|
|
168
|
-
this.#
|
|
169
|
-
for (let [e, t] of this.#
|
|
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) {
|
|
170
127
|
let [n, r] = e.split(",").map(Number);
|
|
171
|
-
this.#
|
|
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));
|
|
172
129
|
}
|
|
173
|
-
let
|
|
130
|
+
let c = this.#s, l = c.createCommandEncoder(), u = a - Math.abs(o), d = a - Math.abs(s), f = [[
|
|
131
|
+
this.#c,
|
|
132
|
+
0,
|
|
174
133
|
this.#u,
|
|
175
|
-
this.#f
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
e,
|
|
179
|
-
this.#
|
|
180
|
-
|
|
134
|
+
this.#f
|
|
135
|
+
], ...this.#l.map((e) => [
|
|
136
|
+
e.texture,
|
|
137
|
+
e.layer,
|
|
138
|
+
this.#d,
|
|
139
|
+
e.fillTexture
|
|
181
140
|
])];
|
|
182
|
-
for (let [
|
|
183
|
-
if (
|
|
184
|
-
let e = [Math.max(0, -
|
|
185
|
-
|
|
186
|
-
texture:
|
|
187
|
-
origin: [
|
|
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
|
+
]
|
|
188
151
|
}, {
|
|
189
|
-
texture:
|
|
152
|
+
texture: c,
|
|
190
153
|
origin: e
|
|
191
|
-
}, [
|
|
192
|
-
texture:
|
|
154
|
+
}, [u, d]), l.copyTextureToTexture({
|
|
155
|
+
texture: c,
|
|
193
156
|
origin: e
|
|
194
157
|
}, {
|
|
195
|
-
texture:
|
|
196
|
-
origin:
|
|
197
|
-
|
|
158
|
+
texture: i,
|
|
159
|
+
origin: [
|
|
160
|
+
e[0],
|
|
161
|
+
e[1],
|
|
162
|
+
a
|
|
163
|
+
]
|
|
164
|
+
}, [u, d]);
|
|
198
165
|
}
|
|
199
|
-
for (let
|
|
200
|
-
let
|
|
201
|
-
|
|
202
|
-
texture:
|
|
203
|
-
origin: [
|
|
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
|
+
]
|
|
204
175
|
}, [n, n]);
|
|
205
176
|
}
|
|
206
177
|
}
|
|
207
|
-
|
|
178
|
+
c.queue.submit([l.finish()]), this.#y({
|
|
208
179
|
shifted: !0,
|
|
209
180
|
u: 0,
|
|
210
181
|
v: 0,
|
|
211
|
-
width:
|
|
212
|
-
depth:
|
|
213
|
-
}), this.#
|
|
182
|
+
width: a,
|
|
183
|
+
depth: a
|
|
184
|
+
}), this.#n();
|
|
214
185
|
}
|
|
215
|
-
|
|
216
|
-
let
|
|
217
|
-
if (
|
|
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;
|
|
218
189
|
let i = [];
|
|
219
|
-
for (let n = 0; n <
|
|
220
|
-
let
|
|
221
|
-
this.#
|
|
222
|
-
(
|
|
223
|
-
|
|
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,
|
|
224
195
|
o
|
|
225
196
|
]);
|
|
226
197
|
}
|
|
227
198
|
i.sort((e, t) => e[0] - t[0]);
|
|
228
199
|
for (let [, e, t] of i) {
|
|
229
|
-
if (
|
|
230
|
-
this.#
|
|
200
|
+
if (n-- <= 0) break;
|
|
201
|
+
this.#g(e, t);
|
|
231
202
|
}
|
|
232
203
|
}
|
|
233
|
-
#
|
|
204
|
+
#g(e, t) {
|
|
234
205
|
let n = e + "," + t, r = new AbortController(), i = {
|
|
235
206
|
state: "loading",
|
|
236
207
|
controller: r
|
|
237
208
|
}, a = this.#e.signal ? AbortSignal.any([this.#e.signal, r.signal]) : r.signal;
|
|
238
|
-
this.#
|
|
209
|
+
this.#r.set(n, i), this.loading++;
|
|
239
210
|
let o;
|
|
240
211
|
try {
|
|
241
212
|
o = Promise.resolve(this.#e.load(e, t, a));
|
|
@@ -243,97 +214,318 @@ var r = [
|
|
|
243
214
|
o = Promise.reject(e);
|
|
244
215
|
}
|
|
245
216
|
o.then((e) => {
|
|
246
|
-
this.#
|
|
217
|
+
this.#r.get(n) !== i || this.#o || (this.#i.delete(n), this.#r.set(n, e ? {
|
|
247
218
|
state: "queued",
|
|
248
|
-
tile: this.#
|
|
219
|
+
tile: this.#v(e),
|
|
249
220
|
row: 0
|
|
250
|
-
} : {
|
|
221
|
+
} : {
|
|
222
|
+
state: "resident",
|
|
223
|
+
data: !1
|
|
224
|
+
}));
|
|
251
225
|
}, (e) => {
|
|
252
|
-
this.#
|
|
253
|
-
state: "failed",
|
|
254
|
-
error: e
|
|
255
|
-
});
|
|
226
|
+
this.#r.get(n) !== i || this.#o || this.#_(n, e, a.aborted);
|
|
256
227
|
}).catch((e) => {
|
|
257
|
-
this.#
|
|
228
|
+
this.#r.get(n) === i && this.#r.set(n, {
|
|
258
229
|
state: "failed",
|
|
259
230
|
error: e
|
|
260
231
|
});
|
|
261
232
|
}).finally(() => {
|
|
262
|
-
this
|
|
233
|
+
this.loading--, this.#n();
|
|
263
234
|
});
|
|
264
235
|
}
|
|
265
|
-
#
|
|
266
|
-
let
|
|
267
|
-
if (
|
|
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)."));
|
|
268
257
|
return e;
|
|
269
258
|
}
|
|
270
|
-
|
|
271
|
-
let
|
|
272
|
-
for (;
|
|
273
|
-
let
|
|
274
|
-
for (let [
|
|
275
|
-
if (
|
|
276
|
-
let [
|
|
277
|
-
|
|
278
|
-
key:
|
|
279
|
-
slot:
|
|
280
|
-
x:
|
|
281
|
-
z:
|
|
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
|
|
282
271
|
});
|
|
283
272
|
}
|
|
284
|
-
if (!
|
|
285
|
-
let { slot:
|
|
286
|
-
for (let e =
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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({
|
|
300
305
|
shifted: !1,
|
|
301
|
-
u:
|
|
302
|
-
v:
|
|
303
|
-
width:
|
|
304
|
-
depth:
|
|
306
|
+
u: h,
|
|
307
|
+
v: g,
|
|
308
|
+
width: i,
|
|
309
|
+
depth: m - p
|
|
305
310
|
});
|
|
306
311
|
}
|
|
307
|
-
|
|
312
|
+
return c && this.#n(), n - s;
|
|
308
313
|
}
|
|
309
|
-
#
|
|
310
|
-
this
|
|
311
|
-
for (let t of [...this.#
|
|
314
|
+
#y(e) {
|
|
315
|
+
this.version++, this.#t();
|
|
316
|
+
for (let t of [...this.#a]) t(e);
|
|
312
317
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
this
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
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;
|
|
322
376
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
377
|
+
destroyTextures() {
|
|
378
|
+
this.#c?.destroy();
|
|
379
|
+
for (let e of this.#l) e.texture?.destroy();
|
|
326
380
|
}
|
|
327
381
|
dispose() {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
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;
|
|
332
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);
|
|
333
525
|
}
|
|
334
526
|
[Symbol.dispose ?? Symbol()]() {
|
|
335
527
|
this.dispose();
|
|
336
528
|
}
|
|
337
529
|
};
|
|
338
530
|
//#endregion
|
|
339
|
-
export {
|
|
531
|
+
export { l as TerrainStream };
|