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