@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.
Files changed (88) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +2366 -2
  4. package/dist/bed-mesh.d.ts +40 -0
  5. package/dist/bed-mesh.js +95 -0
  6. package/dist/blend.d.ts +70 -0
  7. package/dist/blend.js +159 -0
  8. package/dist/colors.d.ts +170 -0
  9. package/dist/deform.d.ts +134 -0
  10. package/dist/deform.js +404 -0
  11. package/dist/displace.d.ts +60 -0
  12. package/dist/displace.js +254 -0
  13. package/dist/dunes.d.ts +86 -0
  14. package/dist/dunes.js +88 -0
  15. package/dist/elevation.d.ts +182 -0
  16. package/dist/elevation.js +155 -0
  17. package/dist/errors-fc4i1pSL.js +7 -0
  18. package/dist/far.d.ts +66 -0
  19. package/dist/far.js +191 -0
  20. package/dist/furrows.d.ts +74 -0
  21. package/dist/furrows.js +157 -0
  22. package/dist/grid.d.ts +14 -0
  23. package/dist/ground-change-settings.d.ts +29 -0
  24. package/dist/ground-change-settings.js +69 -0
  25. package/dist/ground-change.d.ts +227 -0
  26. package/dist/ground-change.js +1412 -0
  27. package/dist/ground-contact-settings.d.ts +27 -0
  28. package/dist/ground-contact-settings.js +22 -0
  29. package/dist/ground-contact.d.ts +91 -0
  30. package/dist/ground-contact.js +241 -0
  31. package/dist/ground-model.d.ts +372 -0
  32. package/dist/hdr-format-DRhzGS7G.js +30 -0
  33. package/dist/height-grid.d.ts +65 -0
  34. package/dist/height-grid.js +74 -0
  35. package/dist/heightfield.d.ts +38 -0
  36. package/dist/holes.d.ts +37 -0
  37. package/dist/holes.js +97 -0
  38. package/dist/imagery.d.ts +109 -0
  39. package/dist/imagery.js +472 -0
  40. package/dist/index.d.ts +408 -0
  41. package/dist/index.js +1 -0
  42. package/dist/layers-pbr.d.ts +78 -0
  43. package/dist/layers-pbr.js +128 -0
  44. package/dist/lighting-layout-CaDtDa5G.js +34 -0
  45. package/dist/lighting-layout.d.ts +5 -0
  46. package/dist/mesh-patch-BThCHCeS.js +21 -0
  47. package/dist/mesh-patch.d.ts +14 -0
  48. package/dist/noise3-sQdnbz4B.js +11 -0
  49. package/dist/noise3.d.ts +3 -0
  50. package/dist/overlays.d.ts +74 -0
  51. package/dist/overlays.js +297 -0
  52. package/dist/planet-complex.d.ts +31 -0
  53. package/dist/planet-craters.d.ts +80 -0
  54. package/dist/planet-rays.d.ts +37 -0
  55. package/dist/planet-recipes.d.ts +84 -0
  56. package/dist/planet-recipes.js +442 -0
  57. package/dist/planet.d.ts +144 -0
  58. package/dist/planet.js +610 -0
  59. package/dist/query.d.ts +150 -0
  60. package/dist/query.js +243 -0
  61. package/dist/relief-lighting.d.ts +53 -0
  62. package/dist/relief-lighting.js +221 -0
  63. package/dist/require-webgpu--tUXPsmA.js +7 -0
  64. package/dist/rock-settings.d.ts +39 -0
  65. package/dist/rock-settings.js +84 -0
  66. package/dist/rock-surface.d.ts +114 -0
  67. package/dist/rock-surface.js +380 -0
  68. package/dist/rocks.d.ts +258 -0
  69. package/dist/rocks.js +488 -0
  70. package/dist/rvt.d.ts +136 -0
  71. package/dist/rvt.js +518 -0
  72. package/dist/scour.d.ts +90 -0
  73. package/dist/scour.js +153 -0
  74. package/dist/sculpt.d.ts +103 -0
  75. package/dist/sculpt.js +361 -0
  76. package/dist/settings-B9BSdnhD.js +72 -0
  77. package/dist/settings.d.ts +81 -0
  78. package/dist/settings.js +152 -0
  79. package/dist/shader.d.ts +176 -0
  80. package/dist/shared/settings.d.ts +89 -0
  81. package/dist/src-Cr7Gm4n0.js +1366 -0
  82. package/dist/stream-window.d.ts +123 -0
  83. package/dist/stream.d.ts +169 -0
  84. package/dist/stream.js +531 -0
  85. package/dist/surface-height.d.ts +16 -0
  86. package/dist/wet-ground.d.ts +82 -0
  87. package/dist/wet-ground.js +147 -0
  88. package/package.json +173 -4
package/dist/planet.js ADDED
@@ -0,0 +1,610 @@
1
+ // @margo/core-built 1.0.0
2
+ import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
3
+
4
+ import { t as e } from "./lighting-layout-CaDtDa5G.js";
5
+ import { t } from "./errors-fc4i1pSL.js";
6
+ import { a as n, i as r, n as i, r as a, t as o } from "./hdr-format-DRhzGS7G.js";
7
+ import { n as s } from "./noise3-sQdnbz4B.js";
8
+ //#region src/planet.ts
9
+ var c = [
10
+ [
11
+ 1,
12
+ 0,
13
+ 0,
14
+ 0,
15
+ 0,
16
+ -1,
17
+ 0,
18
+ 1,
19
+ 0
20
+ ],
21
+ [
22
+ -1,
23
+ 0,
24
+ 0,
25
+ 0,
26
+ 0,
27
+ 1,
28
+ 0,
29
+ 1,
30
+ 0
31
+ ],
32
+ [
33
+ 0,
34
+ 1,
35
+ 0,
36
+ 1,
37
+ 0,
38
+ 0,
39
+ 0,
40
+ 0,
41
+ -1
42
+ ],
43
+ [
44
+ 0,
45
+ -1,
46
+ 0,
47
+ 1,
48
+ 0,
49
+ 0,
50
+ 0,
51
+ 0,
52
+ 1
53
+ ],
54
+ [
55
+ 0,
56
+ 0,
57
+ 1,
58
+ 1,
59
+ 0,
60
+ 0,
61
+ 0,
62
+ 1,
63
+ 0
64
+ ],
65
+ [
66
+ 0,
67
+ 0,
68
+ -1,
69
+ -1,
70
+ 0,
71
+ 0,
72
+ 0,
73
+ 1,
74
+ 0
75
+ ]
76
+ ];
77
+ function l(e, t, n, r = [
78
+ 0,
79
+ 0,
80
+ 0
81
+ ]) {
82
+ let i = c[e];
83
+ if (!i) throw RangeError("Invalid cube face; pass 0 to 5");
84
+ let a = Math.tan(t * Math.PI / 4), o = Math.tan(n * Math.PI / 4), s = i[0] + i[3] * a + i[6] * o, l = i[1] + i[4] * a + i[7] * o, u = i[2] + i[5] * a + i[8] * o, d = Math.hypot(s, l, u);
85
+ return r[0] = s / d, r[1] = l / d, r[2] = u / d, r;
86
+ }
87
+ function u({ radius: e = 6371e3, relief: t = 8e3, seed: n = 1 } = {}) {
88
+ if (!(e > 0) || !Number.isFinite(e) || !(t >= 0) || !Number.isFinite(t) || !Number.isInteger(n)) throw RangeError("Invalid planetNoise options; pass radius above 0, relief 0 or more and an integer seed");
89
+ return (r, i, a, o) => {
90
+ let c = 0, l = e / 2, u = t / 2;
91
+ for (let t = 0; t < 24 && l >= 2 * o; t++) {
92
+ let o = e / l;
93
+ c += u * s(r * o + t * 7.31, i * o - t * 3.17, a * o + t * 5.79, n + t), l /= 2, u /= 2;
94
+ }
95
+ return c;
96
+ };
97
+ }
98
+ var d = 20;
99
+ function f(e) {
100
+ let t = e + 1, n = [], r = (e, t, r, i) => n.push(e, t, i, e, i, r);
101
+ for (let n = 0; n < e; n++) for (let i = 0; i < e; i++) r(n * t + i, n * t + i + 1, (n + 1) * t + i, (n + 1) * t + i + 1);
102
+ return [
103
+ (e) => e,
104
+ (n) => e * t + n,
105
+ (e) => e * t,
106
+ (n) => n * t + e
107
+ ].forEach((n, i) => {
108
+ let a = t * t + i * t;
109
+ for (let t = 0; t < e; t++) r(n(t), n(t + 1), a + t, a + t + 1);
110
+ }), new Uint16Array(n);
111
+ }
112
+ var p = `${n}
113
+ struct Planet {viewProj:mat4x4f,center:vec4f,light:vec4f,radiance:vec4f,params:vec4f}
114
+ @group(0) @binding(0) var<uniform> planet:Planet;
115
+ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@location(1) normal:vec3f,@location(2) height:f32,@location(3) shade:f32}
116
+ @vertex fn vertex(@location(0) local:vec3f,@location(1) normal:vec4f,@location(2) height:f32,@location(3) offset:vec3f)->Output {
117
+ var o:Output;let p=offset+local;o.position=p;o.clip=planet.viewProj*vec4f(p,1);o.normal=normal.xyz;o.height=height;o.shade=normal.w;return o;
118
+ }
119
+ @fragment fn fragment(i:Output)->@location(0) vec4f {
120
+ let up=normalize(i.position-planet.center.xyz);var n=normalize(i.normal);let relief=max(planet.params.z,1.0);let sea=planet.params.x;
121
+ let t=(i.height-sea)/relief;let slope=1-clamp(dot(n,up),0.0,1.0);var base:vec3f;
122
+ if(planet.params.w>0.5&&i.height<sea){base=mix(vec3f(0.035,0.12,0.22),vec3f(0.005,0.025,0.07),smoothstep(0.0,0.3,-t));n=up;}
123
+ else {
124
+ base=mix(vec3f(0.62,0.56,0.40),vec3f(0.16,0.27,0.09),smoothstep(0.002,0.02,t));
125
+ base=mix(base,vec3f(0.38,0.33,0.22),smoothstep(0.15,0.45,t));
126
+ base=mix(base,vec3f(0.33,0.31,0.29),max(smoothstep(0.3,0.5,slope),smoothstep(0.45,0.65,t)));
127
+ base=mix(base,vec3f(0.85,0.87,0.9),smoothstep(0.68,0.75,t)*(1-smoothstep(0.35,0.55,slope)));
128
+ base=mix(base,vec3f(0.74,0.72,0.69),max(i.shade,0.0))*(1+min(i.shade,0.0));
129
+ }
130
+ let nl=clamp(dot(n,planet.light.xyz),0.0,1.0);
131
+ let linear=base/3.14159265*nl*planet.radiance.xyz*planet.light.w+base*planet.radiance.w;
132
+ if(planet.params.y>0.5){return vec4f(linear,1);}
133
+ return vec4f(toDisplay(linear),1);
134
+ }
135
+ `, m = class n {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
136
+ static {
137
+ this.nextId = 0;
138
+ }
139
+ #e;
140
+ #t;
141
+ #n;
142
+ #r;
143
+ #i;
144
+ #a;
145
+ #o;
146
+ #s;
147
+ #c;
148
+ #l;
149
+ #u;
150
+ #d;
151
+ #f;
152
+ #p;
153
+ #m;
154
+ #h;
155
+ #g;
156
+ #_;
157
+ #v;
158
+ #y;
159
+ #b;
160
+ #x;
161
+ #S;
162
+ #C;
163
+ #w;
164
+ #T;
165
+ #E;
166
+ #D;
167
+ #O;
168
+ #k;
169
+ #A;
170
+ #j;
171
+ #M;
172
+ get owner() {
173
+ return this.#x;
174
+ }
175
+ get localCenter() {
176
+ return this.#N();
177
+ }
178
+ addLighting(e) {
179
+ if (this.#j || !e || typeof e.shader != "function" || typeof e.bindings != "function") throw TypeError("Invalid planet lighting; pass a live effect with shader() and bindings().");
180
+ if (e.owner && this.#x && e.owner !== this.#x) throw t("attached", "Planet lighting belongs to another engine; use the same engine.");
181
+ return this.#M.add(e), () => this.#M.delete(e);
182
+ }
183
+ static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
184
+ let r = new n(t);
185
+ return e.use(r), r;
186
+ }
187
+ constructor(e = {}) {
188
+ this.name = "terrain-planet:" + n.nextId++, this.stats = {
189
+ drawn: 0,
190
+ built: 0,
191
+ resident: 0,
192
+ level: 0
193
+ }, this.#i = /* @__PURE__ */ new Float64Array(3), this.#a = /* @__PURE__ */ new Float64Array(3), this.#o = [], this.#s = [], this.#c = [], this.#l = [], this.#d = /* @__PURE__ */ new Float32Array(32), this.#f = /* @__PURE__ */ new Float64Array(16), this.#p = /* @__PURE__ */ new Float64Array(24), this.#y = Infinity, this.#b = 0, this.#k = /* @__PURE__ */ new Map(), this.#j = !1, this.#M = /* @__PURE__ */ new Set();
194
+ let { radius: t = 6371e3, center: r = [
195
+ 0,
196
+ 0,
197
+ 0
198
+ ], relief: i = 8e3, seed: a = 1, seaLevel: o = 0, resolution: s = 32, maxLevel: c = 20, screenError: l = 4, budget: p = 4, maxBuilds: m, clock: h = () => performance.now(), maxPatches: g = 2048, origin: _ } = e;
199
+ if (!(t > 0) || !Number.isFinite(t)) throw RangeError("Invalid radius; pass metres above 0");
200
+ if (r.length !== 3 || ![
201
+ 0,
202
+ 1,
203
+ 2
204
+ ].every((e) => Number.isFinite(r[e]))) throw RangeError("Invalid center; pass 3 finite numbers");
205
+ if (!(i >= 0) || !Number.isFinite(i)) throw RangeError("Invalid relief; pass metres, 0 or more");
206
+ if (o !== !1 && !Number.isFinite(o)) throw RangeError("Invalid seaLevel; pass metres or false");
207
+ if (!Number.isInteger(s) || s < 4 || s > 64 || s % 2) throw RangeError("Invalid resolution; pass an even integer 4 to 64");
208
+ if (!Number.isInteger(c) || c < 0 || c > 24) throw RangeError("Invalid maxLevel; pass an integer 0 to 24");
209
+ if (!(l > 0) || !Number.isFinite(l)) throw RangeError("Invalid screenError; pass pixels above 0");
210
+ if (!(p >= 0)) throw RangeError("Invalid budget; pass milliseconds, 0 or more");
211
+ if (m !== void 0 && !(m >= 1)) throw RangeError("Invalid maxBuilds; pass a count, 1 or more");
212
+ if (typeof h != "function") throw TypeError("Invalid clock; pass a function returning milliseconds");
213
+ if (!Number.isInteger(g) || g < 6 || g > 16384) throw RangeError("Invalid maxPatches; pass an integer 6 to 16384");
214
+ if (e.height !== void 0 && typeof e.height != "function") throw TypeError("Invalid height; pass a function");
215
+ if (e.brightness !== void 0 && typeof e.brightness != "function") throw TypeError("Invalid brightness; pass a function");
216
+ if (_ !== void 0 && (typeof _.onRebase != "function" || _.origin?.length !== 3)) throw TypeError("Invalid origin; pass a FloatingOrigin");
217
+ this.radius = t, this.relief = i, this.seaLevel = o, this.resolution = s, this.maxLevel = c, this.screenError = l, this.budget = p, this.maxBuilds = m, this.#e = h, this.maxPatches = g, this.#t = e.height ?? u({
218
+ radius: t,
219
+ relief: i,
220
+ seed: a
221
+ }), this.#n = e.brightness, this.#i.set([
222
+ r[0],
223
+ r[1],
224
+ r[2]
225
+ ]), _ && (this.#a.set([
226
+ _.origin[0],
227
+ _.origin[1],
228
+ _.origin[2]
229
+ ]), this.#A = _.onRebase((e, t) => this.#a.set([
230
+ t[0],
231
+ t[1],
232
+ t[2]
233
+ ])));
234
+ let v = s + 1;
235
+ this.#m = v * v + 4 * v, this.#h = f(s), this.#g = new Float64Array((v + 2) * (v + 2) * 3), this.#r = new Int8Array((v + 2) * (v + 2)), this.#_ = new Float64Array((v + 2) * (v + 2)), this.#v = /* @__PURE__ */ new ArrayBuffer(this.#m * d), this.#u = new Float32Array(g * 4);
236
+ for (let e = g - 1; e >= 0; e--) this.#c.push(e);
237
+ for (let e = 0; e < 6; e++) this.#o.push(this.#I(e, 0, 0, 0));
238
+ }
239
+ get center() {
240
+ return [
241
+ this.#i[0],
242
+ this.#i[1],
243
+ this.#i[2]
244
+ ];
245
+ }
246
+ set center(e) {
247
+ if (e?.length !== 3 || ![
248
+ 0,
249
+ 1,
250
+ 2
251
+ ].every((t) => Number.isFinite(e[t]))) throw RangeError("Invalid center; pass 3 finite numbers");
252
+ this.#i.set([
253
+ e[0],
254
+ e[1],
255
+ e[2]
256
+ ]);
257
+ }
258
+ get patches() {
259
+ return this.#l.map(({ face: e, level: t, x: n, y: r }) => ({
260
+ face: e,
261
+ level: t,
262
+ x: n,
263
+ y: r
264
+ }));
265
+ }
266
+ heightAt(e) {
267
+ let t = Math.hypot(e[0], e[1], e[2]);
268
+ if (!(t > 0) || !Number.isFinite(t)) throw RangeError("Invalid direction; pass 3 finite numbers, not all 0");
269
+ return this.#P(e[0] / t, e[1] / t, e[2] / t, 0);
270
+ }
271
+ altitude(e) {
272
+ let t = this.#N(), n = e[0] - t[0], r = e[1] - t[1], i = e[2] - t[2], a = Math.hypot(n, r, i);
273
+ if (!Number.isFinite(a)) throw RangeError("Invalid position; pass 3 finite numbers");
274
+ return a ? a - this.radius - this.#P(n / a, r / a, i / a, 0) : -this.radius;
275
+ }
276
+ #N() {
277
+ return [
278
+ this.#i[0] - this.#a[0],
279
+ this.#i[1] - this.#a[1],
280
+ this.#i[2] - this.#a[2]
281
+ ];
282
+ }
283
+ #P(e, t, n, r) {
284
+ let i = this.#F(e, t, n, r);
285
+ return this.seaLevel === !1 ? i : Math.max(i, this.seaLevel);
286
+ }
287
+ #F(e, t, n, r) {
288
+ let i = this.#t(e, t, n, r);
289
+ if (!Number.isFinite(i)) throw RangeError("PlanetTerrain height returned a value that is not finite; return finite metres from height");
290
+ return i;
291
+ }
292
+ #I(e, t, n, r) {
293
+ let i = 2 / 2 ** t, a = -1 + n * i, o = -1 + r * i, s = l(e, a, o), c = l(e, a + i, o), u = l(e, a, o + i), d = (e, t) => Math.acos(Math.min(1, e[0] * t[0] + e[1] * t[1] + e[2] * t[2]));
294
+ return {
295
+ face: e,
296
+ level: t,
297
+ x: n,
298
+ y: r,
299
+ spacing: this.radius * Math.max(d(s, c), d(s, u)) / this.resolution,
300
+ center: /* @__PURE__ */ new Float64Array(3),
301
+ bound: 0,
302
+ min: 0,
303
+ max: 0,
304
+ slot: -1,
305
+ used: -1
306
+ };
307
+ }
308
+ #L(e) {
309
+ if (!this.#c.length && !this.#R()) return !1;
310
+ let t = this.resolution, n = t + 1, r = n + 2, i = 2 / 2 ** e.level, a = -1 + e.x * i, o = -1 + e.y * i, s = this.#g, c = this.#_, u = [
311
+ 0,
312
+ 0,
313
+ 0
314
+ ], f = Infinity, p = -Infinity;
315
+ for (let d = -1; d <= n; d++) for (let m = -1; m <= n; m++) {
316
+ l(e.face, a + m / t * i, o + d / t * i, u);
317
+ let h = this.#F(u[0], u[1], u[2], e.spacing), g = this.seaLevel === !1 ? h : Math.max(h, this.seaLevel), _ = (d + 1) * r + m + 1, v = this.radius + g;
318
+ if (c[_] = h, s[_ * 3] = u[0] * v, s[_ * 3 + 1] = u[1] * v, s[_ * 3 + 2] = u[2] * v, m >= 0 && d >= 0 && m < n && d < n && (g < f && (f = g), g > p && (p = g), this.#n)) {
319
+ let t = this.#n(u[0], u[1], u[2], e.spacing);
320
+ if (!Number.isFinite(t)) throw RangeError("PlanetTerrain brightness returned a value that is not finite; return -1 to 1");
321
+ this.#r[_] = Math.round(Math.max(-1, Math.min(1, t)) * 127);
322
+ }
323
+ }
324
+ let m = ((t / 2 + 1) * r + t / 2 + 1) * 3, h = e.center;
325
+ h[0] = s[m], h[1] = s[m + 1], h[2] = s[m + 2];
326
+ let g = p - f + 2 * e.spacing, _ = new Float32Array(this.#v), v = new Int8Array(this.#v), y = 0, b = (e, t, n) => {
327
+ let i = Math.hypot(s[t * 3], s[t * 3 + 1], s[t * 3 + 2]), a = (i - n) / i, o = s[t * 3] * a - h[0], l = s[t * 3 + 1] * a - h[1], u = s[t * 3 + 2] * a - h[2];
328
+ y = Math.max(y, Math.hypot(o, l, u)), _[e * 5] = o, _[e * 5 + 1] = l, _[e * 5 + 2] = u, _[e * 5 + 4] = c[t];
329
+ let f = (t + 1) * 3, p = (t - 1) * 3, m = (t + r) * 3, g = (t - r) * 3, b = s[f] - s[p], x = s[f + 1] - s[p + 1], S = s[f + 2] - s[p + 2], C = s[m] - s[g], w = s[m + 1] - s[g + 1], T = s[m + 2] - s[g + 2], E = x * T - S * w, D = S * C - b * T, O = b * w - x * C, k = Math.hypot(E, D, O) || 1;
330
+ v[e * d + 12] = Math.round(E / k * 127), v[e * d + 13] = Math.round(D / k * 127), v[e * d + 14] = Math.round(O / k * 127), v[e * d + 15] = this.#r[t];
331
+ }, x = (e, t) => (t + 1) * r + e + 1;
332
+ for (let e = 0; e < n; e++) for (let t = 0; t < n; t++) b(e * n + t, x(t, e), 0);
333
+ return [
334
+ (e) => x(e, 0),
335
+ (e) => x(e, t),
336
+ (e) => x(0, e),
337
+ (e) => x(t, e)
338
+ ].forEach((e, t) => {
339
+ for (let r = 0; r < n; r++) b(n * n + t * n + r, e(r), g);
340
+ }), e.bound = y, e.min = f, e.max = p, e.slot = this.#c.pop(), this.#s.push(e), f < this.#y && (this.#y = f), this.#S?.queue.writeBuffer(this.#C, e.slot * this.#m * d, this.#v), this.stats.built++, !0;
341
+ }
342
+ #R() {
343
+ let e = -1;
344
+ for (let t = 0; t < this.#s.length; t++) {
345
+ let n = this.#s[t];
346
+ n.level && n.used < this.#b && (e < 0 || n.used < this.#s[e].used) && (e = t);
347
+ }
348
+ if (e < 0) return !1;
349
+ let [t] = this.#s.splice(e, 1);
350
+ return this.#c.push(t.slot), t.slot = -1, !0;
351
+ }
352
+ update(e, n) {
353
+ if (this.#j) throw t("disposed", "PlanetTerrain is disposed; attach a new one");
354
+ let [r, i] = n;
355
+ if (!(r > 0) || !(i > 0)) throw RangeError("Invalid size; pass pixels above 0");
356
+ this.#b++, this.stats.built = 0, this.#l.length = 0;
357
+ let a = e.position, o = this.#N(), s = a[0] - o[0], c = a[1] - o[1], l = a[2] - o[2];
358
+ this.#z(e, r / i);
359
+ let u = i / 2 / Math.tan(e.fov * Math.PI / 360), d = Math.hypot(s, c, l), f = this.radius + Math.min(this.#y, this.seaLevel === !1 ? Infinity : this.seaLevel) - 1, p = d > f && f > 0, m = p ? Math.asin(f / d) : 0, h = p ? (d * d - f * f) / d : 0, g = this.maxBuilds === void 0 ? this.#e() : 0, _ = 0, v = [], y = this.maxBuilds, b = y === void 0 ? () => _ === 0 || this.#e() - g < this.budget : () => _ < y, x = (e) => {
360
+ let t = e.center[0] - s, n = e.center[1] - c, r = e.center[2] - l, i = e.bound, a = this.#p;
361
+ for (let e = 0; e < 24; e += 4) if (a[e] * t + a[e + 1] * n + a[e + 2] * r + a[e + 3] < -i) return !1;
362
+ if (!p) return !0;
363
+ let o = Math.hypot(t, n, r);
364
+ if (o <= i) return !0;
365
+ let u = -(t * s + n * c + r * l) / d;
366
+ return !(Math.acos(Math.max(-1, Math.min(1, u / o))) + Math.asin(i / o) < m && u - i > h);
367
+ }, S = (t) => {
368
+ if (t.used = this.#b, !x(t)) return;
369
+ let n = t.center[0] - s, r = t.center[1] - c, i = t.center[2] - l, a = Math.max(Math.hypot(n, r, i) - t.bound, e.near);
370
+ if (t.level < this.maxLevel && t.spacing * u / a > this.screenError) {
371
+ t.children ??= [
372
+ 0,
373
+ 1,
374
+ 2,
375
+ 3
376
+ ].map((e) => this.#I(t.face, t.level + 1, t.x * 2 + (e & 1), t.y * 2 + (e >> 1)));
377
+ let e = !0;
378
+ for (let n of t.children) n.used = this.#b, n.slot < 0 && (e = !1);
379
+ if (e) {
380
+ for (let e of t.children) S(e);
381
+ return;
382
+ }
383
+ v.push({
384
+ node: t,
385
+ error: t.spacing * u / a
386
+ });
387
+ }
388
+ if (this.#l.length < this.maxPatches) {
389
+ let e = this.#l.length * 4;
390
+ this.#u[e] = n, this.#u[e + 1] = r, this.#u[e + 2] = i, this.#l.push(t);
391
+ }
392
+ };
393
+ for (let e of this.#o) e.used = this.#b, e.slot < 0 && this.#L(e);
394
+ let C = !0;
395
+ for (; C;) {
396
+ this.#l.length = 0, v.length = 0;
397
+ for (let e of this.#o) S(e);
398
+ v.sort((e, t) => t.error - e.error), C = !1;
399
+ let e = (v[0]?.error ?? 0) / 2;
400
+ build: for (let { node: t, error: n } of v) for (let r of n >= e ? t.children : []) if (r.slot < 0) {
401
+ if (!b() || !this.#L(r)) break build;
402
+ r.used = this.#b, _++, C = !0;
403
+ }
404
+ }
405
+ return this.stats.drawn = this.#l.length, this.stats.resident = this.#s.length, this.stats.level = this.#l.reduce((e, t) => Math.max(e, t.level), 0), this.stats;
406
+ }
407
+ #z(e, t) {
408
+ let n = e.position, r = e.target, i = e.up, a = n[0] - r[0], o = n[1] - r[1], s = n[2] - r[2], c = Math.hypot(a, o, s);
409
+ if (!c) throw RangeError("Camera at target; move camera.position off camera.target");
410
+ a /= c, o /= c, s /= c;
411
+ let l = i[1] * s - i[2] * o, u = i[2] * a - i[0] * s, d = i[0] * o - i[1] * a, f = Math.hypot(l, u, d);
412
+ if (!f) throw RangeError("Vertical camera; set camera.up off the view direction");
413
+ l /= f, u /= f, d /= f;
414
+ let p = o * d - s * u, m = s * l - a * d, h = a * u - o * l, g = [
415
+ l,
416
+ u,
417
+ d,
418
+ p,
419
+ m,
420
+ h,
421
+ a,
422
+ o,
423
+ s
424
+ ], _ = /* @__PURE__ */ new Float64Array(16);
425
+ if (e.project) e.project(_, t, !0);
426
+ else {
427
+ let n = 1 / Math.tan(e.fov * Math.PI / 360), r = e.near - e.far;
428
+ _[0] = n / t, _[5] = n, _[10] = -e.near / r, _[11] = -1, _[14] = -e.far * e.near / r;
429
+ }
430
+ let v = this.#f;
431
+ for (let e = 0; e < 3; e++) for (let t = 0; t < 4; t++) v[e * 4 + t] = _[t] * g[e] + _[4 + t] * g[3 + e] + _[8 + t] * g[6 + e];
432
+ for (let e = 0; e < 4; e++) v[12 + e] = _[12 + e];
433
+ let y = this.#p, b = (e) => [
434
+ v[e],
435
+ v[4 + e],
436
+ v[8 + e],
437
+ v[12 + e]
438
+ ], x = b(0), S = b(1), C = b(2), w = b(3);
439
+ [
440
+ w.map((e, t) => e + x[t]),
441
+ w.map((e, t) => e - x[t]),
442
+ w.map((e, t) => e + S[t]),
443
+ w.map((e, t) => e - S[t]),
444
+ C,
445
+ w.map((e, t) => e - C[t])
446
+ ].forEach((e, t) => {
447
+ let n = Math.hypot(e[0], e[1], e[2]) || 1;
448
+ for (let r = 0; r < 4; r++) y[t * 4 + r] = e[r] / n;
449
+ });
450
+ }
451
+ setup(e) {
452
+ if (this.#j || !e.gpu) throw t(this.#j ? "disposed" : "unsupported", "PlanetTerrain requires WebGPU and a live planet; attach a new PlanetTerrain to a WebGPU engine; see https://margoengine.com/errors#webgpu");
453
+ this.#B(e.gpu), this.#x = e;
454
+ }
455
+ #B(e) {
456
+ let t = this.#m * d, n = e.limits.maxBufferSize;
457
+ if (this.maxPatches * t > n) throw RangeError(`PlanetTerrain needs ${this.maxPatches * t} bytes of patch vertices, above this device's maxBufferSize (${n}); pass maxPatches at most ${Math.floor(n / t)} or a lower resolution`);
458
+ this.#S = e, this.#C = e.createBuffer({
459
+ size: this.maxPatches * t,
460
+ usage: 40
461
+ }), this.#w = e.createBuffer({
462
+ size: this.maxPatches * 16,
463
+ usage: 40
464
+ }), this.#T = e.createBuffer({
465
+ size: Math.ceil(this.#h.byteLength / 4) * 4,
466
+ usage: 24
467
+ });
468
+ let r = new Uint16Array(this.#T.size / 2);
469
+ r.set(this.#h), e.queue.writeBuffer(this.#T, 0, r), this.#E = e.createBuffer({
470
+ size: 128,
471
+ usage: 72
472
+ }), this.#O = e.createBindGroupLayout({ entries: [{
473
+ binding: 0,
474
+ visibility: 3,
475
+ buffer: { type: "uniform" }
476
+ }] }), this.#D = e.createBindGroup({
477
+ layout: this.#O,
478
+ entries: [{
479
+ binding: 0,
480
+ resource: { buffer: this.#E }
481
+ }]
482
+ }), this.#s.length = 0, this.#c.length = 0;
483
+ for (let e = this.maxPatches - 1; e >= 0; e--) this.#c.push(e);
484
+ let i = (e) => {
485
+ e.slot = -1, e.children?.forEach(i);
486
+ };
487
+ this.#o.forEach(i), this.#y = Infinity;
488
+ }
489
+ registerPasses(e) {
490
+ e.add({
491
+ name: this.name + ":prepare",
492
+ order: -6,
493
+ execute: (e) => {
494
+ e.backend === "webgpu" && this.#V(e);
495
+ }
496
+ }), e.add({
497
+ name: this.name,
498
+ order: 1,
499
+ execute: (e) => {
500
+ e.backend === "webgpu" && this.#H(e);
501
+ }
502
+ });
503
+ }
504
+ #V(e) {
505
+ this.update(e.camera, r(e));
506
+ let t = this.#d, n = this.#x, a = e.camera.position, s = this.#N();
507
+ t.set(this.#f), t.set([
508
+ s[0] - a[0],
509
+ s[1] - a[1],
510
+ s[2] - a[2],
511
+ this.radius
512
+ ], 16), i(t, 20, n.lighting.direction), t.set(n.lighting.radiance, 24), t.set([
513
+ this.seaLevel === !1 ? 0 : this.seaLevel,
514
+ +o(e.format),
515
+ this.relief,
516
+ +(this.seaLevel !== !1)
517
+ ], 28), e.device.queue.writeBuffer(this.#E, 0, t), this.#l.length && e.device.queue.writeBuffer(this.#w, 0, this.#u, 0, this.#l.length * 4);
518
+ }
519
+ #H(t) {
520
+ if (!this.#l.length) return;
521
+ let n = p, r = [];
522
+ for (let e of this.#M) n = e.shader(n, 1), r.push(...e.bindings());
523
+ let i = t.descriptor.depthStencil.format, o = t.descriptor.multisample?.count + ":" + t.format + ":" + i + n, s = this.#k.get(o);
524
+ if (!s) {
525
+ let a = t.device.createShaderModule({ code: n }), c = [this.#O];
526
+ r.length && c.push(t.device.createBindGroupLayout({ entries: e(n, r) })), s = t.device.createRenderPipeline({
527
+ layout: t.device.createPipelineLayout({ bindGroupLayouts: c }),
528
+ vertex: {
529
+ module: a,
530
+ entryPoint: "vertex",
531
+ buffers: [{
532
+ arrayStride: d,
533
+ attributes: [
534
+ {
535
+ shaderLocation: 0,
536
+ offset: 0,
537
+ format: "float32x3"
538
+ },
539
+ {
540
+ shaderLocation: 1,
541
+ offset: 12,
542
+ format: "snorm8x4"
543
+ },
544
+ {
545
+ shaderLocation: 2,
546
+ offset: 16,
547
+ format: "float32"
548
+ }
549
+ ]
550
+ }, {
551
+ arrayStride: 16,
552
+ stepMode: "instance",
553
+ attributes: [{
554
+ shaderLocation: 3,
555
+ offset: 0,
556
+ format: "float32x3"
557
+ }]
558
+ }]
559
+ },
560
+ fragment: {
561
+ module: a,
562
+ entryPoint: "fragment",
563
+ targets: [{ format: t.format }]
564
+ },
565
+ multisample: t.descriptor.multisample,
566
+ depthStencil: {
567
+ format: i,
568
+ depthWriteEnabled: !0,
569
+ depthCompare: "greater-equal"
570
+ }
571
+ }), this.#k.set(o, s);
572
+ }
573
+ let c = t.encoder.beginRenderPass({
574
+ colorAttachments: [{
575
+ view: t.color,
576
+ loadOp: "load",
577
+ storeOp: "store"
578
+ }],
579
+ depthStencilAttachment: {
580
+ view: t.depth,
581
+ depthLoadOp: "load",
582
+ depthStoreOp: "store",
583
+ ...i.includes("stencil") ? {
584
+ stencilLoadOp: "load",
585
+ stencilStoreOp: "store"
586
+ } : {}
587
+ }
588
+ });
589
+ a(c, t), c.setPipeline(s), c.setBindGroup(0, this.#D), r.length && c.setBindGroup(1, t.device.createBindGroup({
590
+ layout: s.getBindGroupLayout(1),
591
+ entries: r
592
+ })), c.setVertexBuffer(0, this.#C), c.setVertexBuffer(1, this.#w), c.setIndexBuffer(this.#T, "uint16");
593
+ for (let e = 0; e < this.#l.length; e++) c.drawIndexed(this.#h.length, 1, 0, this.#l[e].slot * this.#m, e), this.#x.stats.drawCalls++;
594
+ c.end();
595
+ }
596
+ onDeviceRestored(e) {
597
+ this.#U(), this.#k.clear(), this.#B(e.gpu);
598
+ }
599
+ #U() {
600
+ this.#C?.destroy(), this.#w?.destroy(), this.#T?.destroy(), this.#E?.destroy(), this.#C = this.#w = this.#T = this.#E = void 0;
601
+ }
602
+ dispose() {
603
+ this.#j || (this.#j = !0, this.#A?.(), this.#U(), this.#k.clear(), this.#M.clear(), this.#x?.sceneOf(this)?.removePlugin(this.name), this.#x = this.#S = void 0);
604
+ }
605
+ [Symbol.dispose]() {
606
+ this.dispose();
607
+ }
608
+ };
609
+ //#endregion
610
+ export { m as PlanetTerrain, l as cubeSphere, u as planetNoise };