@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.
Files changed (52) hide show
  1. package/LICENSE +201 -201
  2. package/NOTICE +4 -4
  3. package/README.md +2367 -1808
  4. package/dist/bed-mesh.d.ts +40 -0
  5. package/dist/bed-mesh.js +95 -0
  6. package/dist/blend.js +4 -4
  7. package/dist/colors.d.ts +6 -0
  8. package/dist/deform.d.ts +41 -10
  9. package/dist/deform.js +169 -87
  10. package/dist/displace.js +4 -4
  11. package/dist/elevation.d.ts +182 -0
  12. package/dist/elevation.js +155 -0
  13. package/dist/far.d.ts +66 -0
  14. package/dist/far.js +191 -0
  15. package/dist/furrows.d.ts +74 -0
  16. package/dist/furrows.js +157 -0
  17. package/dist/grid.d.ts +6 -3
  18. package/dist/ground-change.d.ts +1 -1
  19. package/dist/ground-change.js +232 -111
  20. package/dist/ground-contact-settings.d.ts +27 -0
  21. package/dist/ground-contact-settings.js +22 -0
  22. package/dist/ground-contact.d.ts +91 -0
  23. package/dist/ground-contact.js +241 -0
  24. package/dist/ground-model.d.ts +83 -1
  25. package/dist/imagery.js +4 -4
  26. package/dist/index.d.ts +120 -9
  27. package/dist/index.js +1 -1
  28. package/dist/layers-pbr.js +9 -9
  29. package/dist/overlays.js +4 -4
  30. package/dist/planet-complex.d.ts +31 -0
  31. package/dist/planet-craters.d.ts +28 -3
  32. package/dist/planet-rays.d.ts +37 -0
  33. package/dist/planet-recipes.d.ts +10 -1
  34. package/dist/planet-recipes.js +341 -115
  35. package/dist/planet.d.ts +5 -0
  36. package/dist/planet.js +112 -104
  37. package/dist/relief-lighting.d.ts +7 -0
  38. package/dist/relief-lighting.js +36 -23
  39. package/dist/rock-surface.d.ts +3 -0
  40. package/dist/rock-surface.js +28 -16
  41. package/dist/rvt.js +5 -5
  42. package/dist/scour.d.ts +90 -0
  43. package/dist/scour.js +153 -0
  44. package/dist/sculpt.js +4 -4
  45. package/dist/shader.d.ts +120 -5
  46. package/dist/shared/settings.d.ts +89 -89
  47. package/dist/{src-C6dtSaAb.js → src-Cr7Gm4n0.js} +359 -148
  48. package/dist/stream-window.d.ts +123 -0
  49. package/dist/stream.d.ts +95 -26
  50. package/dist/stream.js +440 -248
  51. package/dist/wet-ground.js +4 -4
  52. package/package.json +175 -148
package/dist/stream.js CHANGED
@@ -1,21 +1,24 @@
1
- // @margo/core-built 0.1.0
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="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
- import { t as e } from "./src-C6dtSaAb.js";
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
- 0,
10
- 0,
11
- 0
12
- ], i = 0, a = (e, t, n) => Number.isInteger(e) && e >= t && e <= n, o = (e, t, n, r, i, a) => {
13
- let o = Math.max(0, -r), s = Math.min(t, t - r);
14
- for (let c = 0; c < t; c++) {
15
- let l = i >= 0 ? c : t - 1 - c, u = l + i, d = l * t * n;
16
- u < 0 || u >= t || s <= o ? e.fill(a, d, d + t * n) : (e.copyWithin(d + o * n, (u * t + o + r) * n, (u * t + s + r) * n), e.fill(a, d, d + o * n), e.fill(a, d + s * n, d + t * n));
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
- }, s = class s {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
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
- #p;
33
- #m;
34
- #h;
35
- #g;
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(r.fill ?? 0), this.weights = Array.from({ length: r.weights ?? 0 }, () => new Uint8Array(s * 4));
85
- let c = t.camera.position, l = r.floatingOrigin?.origin, u = l ? [
86
- c[0] + l[0],
87
- c[1] + l[1],
88
- c[2] + l[2]
89
- ] : c;
90
- this.#v(Math.round((u[0] - o[0]) / r.tileSize - this.#t / 2), Math.round((u[2] - o[1]) / r.tileSize - this.#t / 2));
91
- let d = r.tileSize / this.#n, f = (this.columns - 1) * d, p = [
92
- "tileSize",
93
- "tileTexels",
94
- "window",
95
- "load",
96
- "colors",
97
- "weights",
98
- "budget",
99
- "concurrency",
100
- "fill",
101
- "signal"
102
- ], m = Object.fromEntries(Object.entries(r).filter(([e]) => !p.includes(e)));
103
- this.terrain = new e({
104
- ...m,
105
- clipmapAnchor: r.clipmapAnchor ?? "camera",
106
- origin: this.#i,
107
- size: [f, f],
108
- textures: this.#D
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
- static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
112
- let r = new s(e, n), i = t(e);
113
- return i.use(r.terrain), i.use(r), r;
114
- }
115
- get origin() {
116
- return this.#i;
117
- }
118
- get tile() {
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.#a.get(e + "," + t)?.state === "resident";
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.#s.add(e), () => this.#s.delete(e);
90
+ return this.#a.add(e), () => this.#a.delete(e);
130
91
  }
131
- ready(e) {
132
- return new Promise((t, n) => {
133
- if (e?.aborted) return n(e.reason);
134
- let r = () => {
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
- setup(e) {
144
- 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");
145
- this.#c = e;
146
- }
147
- onPreUpdate() {
148
- if (this.disposed || !this.#c || !this.#u) return;
149
- let e = this.#c.camera.position, t = this.terrain.options.floatingOrigin ? this.terrain.shift : r, [n, i] = this.#e.origin ?? [0, 0], a = this.#t / 2, o = (e[0] + t[0] - n) / this.#e.tileSize - this.#r[0] - a, s = (e[2] + t[2] - i) / this.#e.tileSize - this.#r[1] - a;
150
- if (Number.isFinite(o) && Number.isFinite(s)) {
151
- let e = Math.abs(o) > .75 ? Math.round(o) : 0, t = Math.abs(s) > .75 ? Math.round(s) : 0;
152
- (e || t) && this.#b(e, t);
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
- let c = this.#r[0] + a + (Number.isFinite(o) ? o : 0), l = this.#r[1] + a + (Number.isFinite(s) ? s : 0);
155
- this.#x(c, l), this.#w(c, l);
156
- }
157
- #v(e, t) {
158
- let [n, r] = this.#e.origin ?? [0, 0];
159
- this.#r[0] = e, this.#r[1] = t, this.#i[0] = n + e * this.#e.tileSize, this.#i[1] = r + t * this.#e.tileSize;
160
- }
161
- #y(e, t) {
162
- return e >= this.#r[0] && t >= this.#r[1] && e < this.#r[0] + this.#t && t < this.#r[1] + this.#t;
163
- }
164
- #b(e, t) {
165
- let n = this.#n, r = this.columns, i = e * n, a = t * n;
166
- o(this.heights, r, 1, i, a, this.#e.fill ?? 0);
167
- for (let e of this.weights) o(e, r, 4, i, a, 0);
168
- this.#v(this.#r[0] + e, this.#r[1] + t);
169
- for (let [e, t] of this.#a) {
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.#y(n, r) || (t.state === "loading" && t.controller.abort(), this.#a.delete(e));
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 s = this.#l, c = s.createCommandEncoder(), l = r - Math.abs(i), u = r - Math.abs(a), d = [[
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
- this.#m
177
- ], ...this.#d.map((e) => [
178
- e,
179
- this.#p,
180
- this.#h
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 [r, o, s] of d) {
183
- if (l > 0 && u > 0) {
184
- let e = [Math.max(0, -i), Math.max(0, -a)];
185
- c.copyTextureToTexture({
186
- texture: r,
187
- origin: [Math.max(0, i), Math.max(0, a)]
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: o,
152
+ texture: c,
190
153
  origin: e
191
- }, [l, u]), c.copyTextureToTexture({
192
- texture: o,
154
+ }, [u, d]), l.copyTextureToTexture({
155
+ texture: c,
193
156
  origin: e
194
157
  }, {
195
- texture: r,
196
- origin: e
197
- }, [l, u]);
158
+ texture: i,
159
+ origin: [
160
+ e[0],
161
+ e[1],
162
+ a
163
+ ]
164
+ }, [u, d]);
198
165
  }
199
- for (let i = 0; i < this.#t; i++) for (let a = 0; a < this.#t; a++) {
200
- let o = a + e, l = i + t;
201
- o >= 0 && l >= 0 && o < this.#t && l < this.#t || c.copyTextureToTexture({ texture: s }, {
202
- texture: r,
203
- origin: [a * n, i * n]
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
- s.queue.submit([c.finish()]), this.#T({
178
+ c.queue.submit([l.finish()]), this.#y({
208
179
  shifted: !0,
209
180
  u: 0,
210
181
  v: 0,
211
- width: r,
212
- depth: r
213
- }), this.#E();
182
+ width: a,
183
+ depth: a
184
+ }), this.#n();
214
185
  }
215
- #x(e, t) {
216
- let n = this.#e, r = n.concurrency ?? 4;
217
- if (this.#g >= r || this.#a.size >= this.#t * this.#t || n.signal?.aborted) return;
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 < this.#t; n++) for (let r = 0; r < this.#t; r++) {
220
- let a = this.#r[0] + r, o = this.#r[1] + n;
221
- this.#a.has(a + "," + o) || i.push([
222
- (a + .5 - e) ** 2 + (o + .5 - t) ** 2,
223
- a,
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 (this.#g >= r) break;
230
- this.#S(e, t);
200
+ if (n-- <= 0) break;
201
+ this.#g(e, t);
231
202
  }
232
203
  }
233
- #S(e, t) {
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.#a.set(n, i), this.#g++;
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.#a.get(n) !== i || this.disposed || this.#a.set(n, e ? {
217
+ this.#r.get(n) !== i || this.#o || (this.#i.delete(n), this.#r.set(n, e ? {
247
218
  state: "queued",
248
- tile: this.#C(e),
219
+ tile: this.#v(e),
249
220
  row: 0
250
- } : { state: "resident" });
221
+ } : {
222
+ state: "resident",
223
+ data: !1
224
+ }));
251
225
  }, (e) => {
252
- this.#a.get(n) !== i || this.disposed || this.#a.set(n, {
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.#a.get(n) === i && this.#a.set(n, {
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.#g--, this.#E();
233
+ this.loading--, this.#n();
263
234
  });
264
235
  }
265
- #C(e) {
266
- let t = this.#n * this.#n, n = this.weights.length;
267
- if (!(e.heights instanceof Float32Array) || e.heights.length !== t || !e.heights.every(Number.isFinite) || n > 0 && (!Array.isArray(e.weights) || e.weights.length !== n || e.weights.some((e) => !(e instanceof Uint8Array) || e.length !== t * 4))) throw RangeError(`Invalid terrain stream tile; give ${t} finite float32 heights and ${n} weight arrays of ${t * 4} bytes.`);
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
- #w(e, t) {
271
- let n = this.#n, r = this.columns, i = this.#l.queue, a = this.#e.budget ?? 65536, o = !1;
272
- for (; a > 0;) {
273
- let s, c = Infinity;
274
- for (let [n, r] of this.#a) {
275
- if (r.state !== "queued") continue;
276
- let [i, a] = n.split(",").map(Number), o = (i + .5 - e) ** 2 + (a + .5 - t) ** 2;
277
- o < c && (c = o, s = {
278
- key: n,
279
- slot: r,
280
- x: i,
281
- z: a
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 (!s) break;
285
- let { slot: l, x: u, z: d } = s, f = l.row, p = Math.min(n, f + Math.max(1, Math.floor(a / n))), m = (u - this.#r[0]) * n, h = (d - this.#r[1]) * n + f, g = l.tile;
286
- for (let e = f; e < p; e++) this.heights.set(g.heights.subarray(e * n, (e + 1) * n), (h + e - f) * r + m), this.weights.forEach((t, i) => t.set(g.weights[i].subarray(e * n * 4, (e + 1) * n * 4), ((h + e - f) * r + m) * 4));
287
- i.writeTexture({
288
- texture: this.#u,
289
- origin: [m, h]
290
- }, g.heights, {
291
- offset: f * n * 4,
292
- bytesPerRow: n * 4
293
- }, [n, p - f]), this.#d.forEach((e, t) => i.writeTexture({
294
- texture: e,
295
- origin: [m, h]
296
- }, g.weights[t], {
297
- offset: f * n * 4,
298
- bytesPerRow: n * 4
299
- }, [n, p - f])), a -= (p - f) * n, l.row = p, p === n && (this.#a.set(s.key, { state: "resident" }), o = !0), this.#T({
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: m,
302
- v: h,
303
- width: n,
304
- depth: p - f
306
+ u: h,
307
+ v: g,
308
+ width: i,
309
+ depth: m - p
305
310
  });
306
311
  }
307
- o && this.#E();
312
+ return c && this.#n(), n - s;
308
313
  }
309
- #T(e) {
310
- this.#_++, this.terrain.invalidate();
311
- for (let t of [...this.#s]) t(e);
314
+ #y(e) {
315
+ this.version++, this.#t();
316
+ for (let t of [...this.#a]) t(e);
312
317
  }
313
- #E() {
314
- if (!this.#o.size) return;
315
- let e, t = !1, r = 0;
316
- 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"));
317
- for (let n of this.#a.values()) n.state === "failed" && !t ? (t = !0, e = n.error) : n.state === "resident" && r++;
318
- if (!t && r < this.#t * this.#t) return;
319
- let i = [...this.#o];
320
- this.#o.clear();
321
- for (let n of i) t ? n.reject(e) : n.resolve();
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
- #D;
324
- #O() {
325
- this.#f?.destroy(), this.#p?.destroy(), this.#m?.destroy(), this.#h?.destroy(), this.#f = this.#p = this.#m = this.#h = void 0;
377
+ destroyTextures() {
378
+ this.#c?.destroy();
379
+ for (let e of this.#l) e.texture?.destroy();
326
380
  }
327
381
  dispose() {
328
- if (!this.disposed) {
329
- this.disposed = !0;
330
- for (let e of this.#a.values()) e.state === "loading" && e.controller.abort();
331
- this.#E(), this.#a.clear(), this.#s.clear(), this.#c?.sceneOf(this)?.removePlugin(this.name), this.terrain.dispose(), this.#O(), this.#c = this.#l = this.#u = void 0, this.#d = [];
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 { s as TerrainStream };
531
+ export { l as TerrainStream };