@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/planet.js CHANGED
@@ -1,4 +1,4 @@
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
3
 
4
4
  import { t as e } from "./lighting-layout-CaDtDa5G.js";
@@ -112,9 +112,9 @@ function f(e) {
112
112
  var p = `${n}
113
113
  struct Planet {viewProj:mat4x4f,center:vec4f,light:vec4f,radiance:vec4f,params:vec4f}
114
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}
115
+ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@location(1) normal:vec3f,@location(2) height:f32,@location(3) shade:f32}
116
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;
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
118
  }
119
119
  @fragment fn fragment(i:Output)->@location(0) vec4f {
120
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;
@@ -125,13 +125,14 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
125
125
  base=mix(base,vec3f(0.38,0.33,0.22),smoothstep(0.15,0.45,t));
126
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
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));
128
129
  }
129
130
  let nl=clamp(dot(n,planet.light.xyz),0.0,1.0);
130
131
  let linear=base/3.14159265*nl*planet.radiance.xyz*planet.light.w+base*planet.radiance.w;
131
132
  if(planet.params.y>0.5){return vec4f(linear,1);}
132
133
  return vec4f(toDisplay(linear),1);
133
134
  }
134
- `, m = class n {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
135
+ `, m = class n {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
135
136
  static {
136
137
  this.nextId = 0;
137
138
  }
@@ -166,18 +167,20 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
166
167
  #O;
167
168
  #k;
168
169
  #A;
170
+ #j;
171
+ #M;
169
172
  get owner() {
170
- return this.#y;
173
+ return this.#x;
171
174
  }
172
175
  get localCenter() {
173
- return this.#j();
176
+ return this.#N();
174
177
  }
175
178
  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
+ 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);
179
182
  }
180
- static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
183
+ static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
181
184
  let r = new n(t);
182
185
  return e.use(r), r;
183
186
  }
@@ -187,7 +190,7 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
187
190
  built: 0,
188
191
  resident: 0,
189
192
  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();
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();
191
194
  let { radius: t = 6371e3, center: r = [
192
195
  0,
193
196
  0,
@@ -209,34 +212,35 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
209
212
  if (typeof h != "function") throw TypeError("Invalid clock; pass a function returning milliseconds");
210
213
  if (!Number.isInteger(g) || g < 6 || g > 16384) throw RangeError("Invalid maxPatches; pass an integer 6 to 16384");
211
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");
212
216
  if (_ !== void 0 && (typeof _.onRebase != "function" || _.origin?.length !== 3)) throw TypeError("Invalid origin; pass a FloatingOrigin");
213
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({
214
218
  radius: t,
215
219
  relief: i,
216
220
  seed: a
217
- }), this.#n.set([
221
+ }), this.#n = e.brightness, this.#i.set([
218
222
  r[0],
219
223
  r[1],
220
224
  r[2]
221
- ]), _ && (this.#r.set([
225
+ ]), _ && (this.#a.set([
222
226
  _.origin[0],
223
227
  _.origin[1],
224
228
  _.origin[2]
225
- ]), this.#O = _.onRebase((e, t) => this.#r.set([
229
+ ]), this.#A = _.onRebase((e, t) => this.#a.set([
226
230
  t[0],
227
231
  t[1],
228
232
  t[2]
229
233
  ])));
230
234
  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));
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));
234
238
  }
235
239
  get center() {
236
240
  return [
237
- this.#n[0],
238
- this.#n[1],
239
- this.#n[2]
241
+ this.#i[0],
242
+ this.#i[1],
243
+ this.#i[2]
240
244
  ];
241
245
  }
242
246
  set center(e) {
@@ -245,14 +249,14 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
245
249
  1,
246
250
  2
247
251
  ].every((t) => Number.isFinite(e[t]))) throw RangeError("Invalid center; pass 3 finite numbers");
248
- this.#n.set([
252
+ this.#i.set([
249
253
  e[0],
250
254
  e[1],
251
255
  e[2]
252
256
  ]);
253
257
  }
254
258
  get patches() {
255
- return this.#s.map(({ face: e, level: t, x: n, y: r }) => ({
259
+ return this.#l.map(({ face: e, level: t, x: n, y: r }) => ({
256
260
  face: e,
257
261
  level: t,
258
262
  x: n,
@@ -262,30 +266,30 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
262
266
  heightAt(e) {
263
267
  let t = Math.hypot(e[0], e[1], e[2]);
264
268
  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);
269
+ return this.#P(e[0] / t, e[1] / t, e[2] / t, 0);
266
270
  }
267
271
  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);
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);
269
273
  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;
274
+ return a ? a - this.radius - this.#P(n / a, r / a, i / a, 0) : -this.radius;
271
275
  }
272
- #j() {
276
+ #N() {
273
277
  return [
274
- this.#n[0] - this.#r[0],
275
- this.#n[1] - this.#r[1],
276
- this.#n[2] - this.#r[2]
278
+ this.#i[0] - this.#a[0],
279
+ this.#i[1] - this.#a[1],
280
+ this.#i[2] - this.#a[2]
277
281
  ];
278
282
  }
279
- #M(e, t, n, r) {
280
- let i = this.#N(e, t, n, r);
283
+ #P(e, t, n, r) {
284
+ let i = this.#F(e, t, n, r);
281
285
  return this.seaLevel === !1 ? i : Math.max(i, this.seaLevel);
282
286
  }
283
- #N(e, t, n, r) {
287
+ #F(e, t, n, r) {
284
288
  let i = this.#t(e, t, n, r);
285
289
  if (!Number.isFinite(i)) throw RangeError("PlanetTerrain height returned a value that is not finite; return finite metres from height");
286
290
  return i;
287
291
  }
288
- #P(e, t, n, r) {
292
+ #I(e, t, n, r) {
289
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]));
290
294
  return {
291
295
  face: e,
@@ -301,25 +305,29 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
301
305
  used: -1
302
306
  };
303
307
  }
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 = [
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 = [
307
311
  0,
308
312
  0,
309
313
  0
310
314
  ], f = Infinity, p = -Infinity;
311
315
  for (let d = -1; d <= n; d++) for (let m = -1; m <= n; m++) {
312
316
  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));
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
+ }
315
323
  }
316
324
  let m = ((t / 2 + 1) * r + t / 2 + 1) * 3, h = e.center;
317
325
  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) => {
326
+ let g = p - f + 2 * e.spacing, _ = new Float32Array(this.#v), v = new Int8Array(this.#v), y = 0, b = (e, t, n) => {
319
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];
320
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];
321
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;
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;
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];
323
331
  }, x = (e, t) => (t + 1) * r + e + 1;
324
332
  for (let e = 0; e < n; e++) for (let t = 0; t < n; t++) b(e * n + t, x(t, e), 0);
325
333
  return [
@@ -329,27 +337,27 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
329
337
  (e) => x(t, e)
330
338
  ].forEach((e, t) => {
331
339
  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;
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;
333
341
  }
334
- #I() {
342
+ #R() {
335
343
  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);
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);
339
347
  }
340
348
  if (e < 0) return !1;
341
- let [t] = this.#a.splice(e, 1);
342
- return this.#o.push(t.slot), t.slot = -1, !0;
349
+ let [t] = this.#s.splice(e, 1);
350
+ return this.#c.push(t.slot), t.slot = -1, !0;
343
351
  }
344
352
  update(e, n) {
345
- if (this.#k) throw t("disposed", "PlanetTerrain is disposed; attach a new one");
353
+ if (this.#j) throw t("disposed", "PlanetTerrain is disposed; attach a new one");
346
354
  let [r, i] = n;
347
355
  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;
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;
353
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;
354
362
  if (!p) return !0;
355
363
  let o = Math.hypot(t, n, r);
@@ -357,7 +365,7 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
357
365
  let u = -(t * s + n * c + r * l) / d;
358
366
  return !(Math.acos(Math.max(-1, Math.min(1, u / o))) + Math.asin(i / o) < m && u - i > h);
359
367
  }, S = (t) => {
360
- if (t.used = this.#v, !x(t)) return;
368
+ if (t.used = this.#b, !x(t)) return;
361
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);
362
370
  if (t.level < this.maxLevel && t.spacing * u / a > this.screenError) {
363
371
  t.children ??= [
@@ -365,9 +373,9 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
365
373
  1,
366
374
  2,
367
375
  3
368
- ].map((e) => this.#P(t.face, t.level + 1, t.x * 2 + (e & 1), t.y * 2 + (e >> 1)));
376
+ ].map((e) => this.#I(t.face, t.level + 1, t.x * 2 + (e & 1), t.y * 2 + (e >> 1)));
369
377
  let e = !0;
370
- for (let n of t.children) n.used = this.#v, n.slot < 0 && (e = !1);
378
+ for (let n of t.children) n.used = this.#b, n.slot < 0 && (e = !1);
371
379
  if (e) {
372
380
  for (let e of t.children) S(e);
373
381
  return;
@@ -377,26 +385,26 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
377
385
  error: t.spacing * u / a
378
386
  });
379
387
  }
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);
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);
383
391
  }
384
392
  };
385
- for (let e of this.#i) e.used = this.#v, e.slot < 0 && this.#F(e);
393
+ for (let e of this.#o) e.used = this.#b, e.slot < 0 && this.#L(e);
386
394
  let C = !0;
387
395
  for (; C;) {
388
- this.#s.length = 0, v.length = 0;
389
- for (let e of this.#i) S(e);
396
+ this.#l.length = 0, v.length = 0;
397
+ for (let e of this.#o) S(e);
390
398
  v.sort((e, t) => t.error - e.error), C = !1;
391
399
  let e = (v[0]?.error ?? 0) / 2;
392
400
  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;
401
+ if (!b() || !this.#L(r)) break build;
402
+ r.used = this.#b, _++, C = !0;
395
403
  }
396
404
  }
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;
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;
398
406
  }
399
- #L(e, t) {
407
+ #z(e, t) {
400
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);
401
409
  if (!c) throw RangeError("Camera at target; move camera.position off camera.target");
402
410
  a /= c, o /= c, s /= c;
@@ -419,10 +427,10 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
419
427
  let n = 1 / Math.tan(e.fov * Math.PI / 360), r = e.near - e.far;
420
428
  _[0] = n / t, _[5] = n, _[10] = -e.near / r, _[11] = -1, _[14] = -e.far * e.near / r;
421
429
  }
422
- let v = this.#u;
430
+ let v = this.#f;
423
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];
424
432
  for (let e = 0; e < 4; e++) v[12 + e] = _[12 + e];
425
- let y = this.#d, b = (e) => [
433
+ let y = this.#p, b = (e) => [
426
434
  v[e],
427
435
  v[4 + e],
428
436
  v[8 + e],
@@ -441,62 +449,62 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
441
449
  });
442
450
  }
443
451
  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;
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;
446
454
  }
447
- #R(e) {
448
- let t = this.#f * d, n = e.limits.maxBufferSize;
455
+ #B(e) {
456
+ let t = this.#m * d, n = e.limits.maxBufferSize;
449
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`);
450
- this.#b = e, this.#x = e.createBuffer({
458
+ this.#S = e, this.#C = e.createBuffer({
451
459
  size: this.maxPatches * t,
452
460
  usage: 40
453
- }), this.#S = e.createBuffer({
461
+ }), this.#w = e.createBuffer({
454
462
  size: this.maxPatches * 16,
455
463
  usage: 40
456
- }), this.#C = e.createBuffer({
457
- size: Math.ceil(this.#p.byteLength / 4) * 4,
464
+ }), this.#T = e.createBuffer({
465
+ size: Math.ceil(this.#h.byteLength / 4) * 4,
458
466
  usage: 24
459
467
  });
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({
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({
462
470
  size: 128,
463
471
  usage: 72
464
- }), this.#E = e.createBindGroupLayout({ entries: [{
472
+ }), this.#O = e.createBindGroupLayout({ entries: [{
465
473
  binding: 0,
466
474
  visibility: 3,
467
475
  buffer: { type: "uniform" }
468
- }] }), this.#T = e.createBindGroup({
469
- layout: this.#E,
476
+ }] }), this.#D = e.createBindGroup({
477
+ layout: this.#O,
470
478
  entries: [{
471
479
  binding: 0,
472
- resource: { buffer: this.#w }
480
+ resource: { buffer: this.#E }
473
481
  }]
474
- }), this.#a.length = 0, this.#o.length = 0;
475
- for (let e = this.maxPatches - 1; e >= 0; e--) this.#o.push(e);
482
+ }), this.#s.length = 0, this.#c.length = 0;
483
+ for (let e = this.maxPatches - 1; e >= 0; e--) this.#c.push(e);
476
484
  let i = (e) => {
477
485
  e.slot = -1, e.children?.forEach(i);
478
486
  };
479
- this.#i.forEach(i), this.#_ = Infinity;
487
+ this.#o.forEach(i), this.#y = Infinity;
480
488
  }
481
489
  registerPasses(e) {
482
490
  e.add({
483
491
  name: this.name + ":prepare",
484
492
  order: -6,
485
493
  execute: (e) => {
486
- e.backend === "webgpu" && this.#z(e);
494
+ e.backend === "webgpu" && this.#V(e);
487
495
  }
488
496
  }), e.add({
489
497
  name: this.name,
490
498
  order: 1,
491
499
  execute: (e) => {
492
- e.backend === "webgpu" && this.#B(e);
500
+ e.backend === "webgpu" && this.#H(e);
493
501
  }
494
502
  });
495
503
  }
496
- #z(e) {
504
+ #V(e) {
497
505
  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([
506
+ let t = this.#d, n = this.#x, a = e.camera.position, s = this.#N();
507
+ t.set(this.#f), t.set([
500
508
  s[0] - a[0],
501
509
  s[1] - a[1],
502
510
  s[2] - a[2],
@@ -506,15 +514,15 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
506
514
  +o(e.format),
507
515
  this.relief,
508
516
  +(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);
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);
510
518
  }
511
- #B(t) {
512
- if (!this.#s.length) return;
519
+ #H(t) {
520
+ if (!this.#l.length) return;
513
521
  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);
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);
516
524
  if (!s) {
517
- let a = t.device.createShaderModule({ code: n }), c = [this.#E];
525
+ let a = t.device.createShaderModule({ code: n }), c = [this.#O];
518
526
  r.length && c.push(t.device.createBindGroupLayout({ entries: e(n, r) })), s = t.device.createRenderPipeline({
519
527
  layout: t.device.createPipelineLayout({ bindGroupLayouts: c }),
520
528
  vertex: {
@@ -560,7 +568,7 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
560
568
  depthWriteEnabled: !0,
561
569
  depthCompare: "greater-equal"
562
570
  }
563
- }), this.#D.set(o, s);
571
+ }), this.#k.set(o, s);
564
572
  }
565
573
  let c = t.encoder.beginRenderPass({
566
574
  colorAttachments: [{
@@ -578,21 +586,21 @@ struct Output {@builtin(position) clip:vec4f,@location(0) position:vec3f,@locati
578
586
  } : {}
579
587
  }
580
588
  });
581
- a(c, t), c.setPipeline(s), c.setBindGroup(0, this.#T), r.length && c.setBindGroup(1, t.device.createBindGroup({
589
+ a(c, t), c.setPipeline(s), c.setBindGroup(0, this.#D), r.length && c.setBindGroup(1, t.device.createBindGroup({
582
590
  layout: s.getBindGroupLayout(1),
583
591
  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++;
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++;
586
594
  c.end();
587
595
  }
588
596
  onDeviceRestored(e) {
589
- this.#V(), this.#D.clear(), this.#R(e.gpu);
597
+ this.#U(), this.#k.clear(), this.#B(e.gpu);
590
598
  }
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;
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;
593
601
  }
594
602
  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);
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);
596
604
  }
597
605
  [Symbol.dispose]() {
598
606
  this.dispose();
@@ -16,6 +16,11 @@ export interface TerrainReliefLightingOptions {
16
16
  softness?: number;
17
17
  /** World units per input texel value, finite. Default 1 or the Terrain's height scale. */
18
18
  heightScale?: number;
19
+ /**
20
+ * The terrain plugin rebakes by itself in the first frame after the engine light has turned more than half a degree
21
+ * from the baked sun (without a `direction` option) or after `refresh()`. Default false: call `rebake()`.
22
+ */
23
+ follow?: boolean;
19
24
  }
20
25
  /**
21
26
  * Enqueues a WebGPU terrain lighting bake from an existing sampled height texture.
@@ -36,6 +41,8 @@ export declare class TerrainReliefLighting implements EnginePlugin, TerrainLight
36
41
  /** Attach after Terrain and before disposal of its heightmap. Works with layer and local relief materials. */
37
42
  static attach(engine: EngineKernel, terrain: Terrain, options?: TerrainReliefLightingOptions): TerrainReliefLighting;
38
43
  setup(engine: Engine): void;
44
+ /** With `follow`, marks the heights as changed: the next frame rebakes once, however many edits there were. */
45
+ refresh(): void;
39
46
  /** Rebuild after height edits or a sun change. Options are merged with the last successful settings. */
40
47
  rebake(options?: TerrainReliefLightingOptions): void;
41
48
  shader(source: string, group: number): string;
@@ -1,6 +1,6 @@
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 e=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
3
+ const e=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
4
  import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
5
5
  import { MargoError as t } from "@margoengine/math/errors";
6
6
  //#region src/relief-lighting.ts
@@ -107,18 +107,24 @@ ${f}
107
107
  fn terrainReliefLight(map:texture_2d<f32>, mapSampler:sampler, uv:vec2f)->vec2f{
108
108
  let uvMap=terrainReliefUv(uv,vec2f(textureDimensions(map)));
109
109
  return textureSampleLevel(map,mapSampler,uvMap,0.0).rg;
110
- }`, m = 0, h = class n {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
110
+ }`, m = 0, h = class n {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
111
111
  #e;
112
112
  #t;
113
113
  #n;
114
114
  #r;
115
115
  #i;
116
116
  #a;
117
+ #o;
118
+ #s;
117
119
  constructor(e, t = {}) {
118
- if (this.terrain = e, this.name = "terrain-relief-lighting:" + m++, this.disposed = !1, this.#i = [], this.#a = /* @__PURE__ */ new Map(), !e || typeof e.addLighting != "function") throw TypeError("Terrain relief lighting needs a live clipmap Terrain.");
119
- this.#n = this.#o(t);
120
+ if (this.terrain = e, this.name = "terrain-relief-lighting:" + m++, this.disposed = !1, this.#i = [], this.#a = /* @__PURE__ */ new Map(), this.#o = [
121
+ 0,
122
+ 1,
123
+ 0
124
+ ], this.#s = !1, !e || typeof e.addLighting != "function") throw TypeError("Terrain relief lighting needs a live clipmap Terrain.");
125
+ this.#n = this.#c(t);
120
126
  }
121
- static attach(t, r, i = {}) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
127
+ static attach(t, r, i = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
122
128
  let a = new n(r, i);
123
129
  return e(t).use(a), a;
124
130
  }
@@ -127,34 +133,41 @@ fn terrainReliefLight(map:texture_2d<f32>, mapSampler:sampler, uv:vec2f)->vec2f{
127
133
  let n = e.gpu.createSampler({
128
134
  minFilter: "linear",
129
135
  magFilter: "linear"
130
- }), r = this.#s(e, this.#n);
136
+ }), r = this.#l(e, this.#n);
131
137
  try {
132
138
  this.owner = e, this.#r ??= this.terrain.addLighting(this);
133
139
  } catch (e) {
134
140
  throw this.owner = void 0, r.destroy(), e;
135
141
  }
136
- this.#t = n, this.#c(r);
142
+ this.#t = n, this.#d(r);
137
143
  }
138
- #o(e) {
144
+ #c(e) {
139
145
  return {
140
146
  ...e,
141
147
  direction: e.direction ? [...e.direction] : void 0
142
148
  };
143
149
  }
144
- #s(e, n) {
150
+ #l(e, n) {
145
151
  if (!this.terrain.heightmap) throw new t("state", "Attach the clipmap Terrain before its relief lighting.");
146
- let { direction: r } = e.lighting;
147
- return d(e.gpu, this.terrain.heightmap, this.terrain.options.size, {
152
+ let { direction: r } = e.lighting, i = n.direction ?? [
153
+ r[0],
154
+ r[1],
155
+ r[2]
156
+ ], a = d(e.gpu, this.terrain.heightmap, this.terrain.options.size, {
148
157
  ...n,
149
- direction: n.direction ?? [
150
- r[0],
151
- r[1],
152
- r[2]
153
- ],
158
+ direction: i,
154
159
  heightScale: n.heightScale ?? this.terrain.options.heightScale ?? 100
155
- });
160
+ }), o = Math.hypot(...i);
161
+ return this.#o = i.map((e) => e / o), this.#s = !1, a;
156
162
  }
157
- #c(e) {
163
+ refresh() {
164
+ this.#s = !0;
165
+ }
166
+ #u() {
167
+ let e = this.owner.lighting.direction, t = Math.hypot(e[0], e[1], e[2]);
168
+ return this.#n.direction || !(t > 0) ? !1 : (e[0] * this.#o[0] + e[1] * this.#o[1] + e[2] * this.#o[2]) / t < Math.cos(Math.PI / 360);
169
+ }
170
+ #d(e) {
158
171
  let t = this.#e;
159
172
  if (this.#e = e, this.#i = [{
160
173
  binding: i,
@@ -169,16 +182,16 @@ fn terrainReliefLight(map:texture_2d<f32>, mapSampler:sampler, uv:vec2f)->vec2f{
169
182
  }
170
183
  rebake(e = {}) {
171
184
  if (this.disposed || !this.owner) throw new t(this.disposed ? "disposed" : "state", "Attach live Terrain relief lighting before rebaking.");
172
- let n = this.#o({
185
+ let n = this.#c({
173
186
  ...this.#n,
174
187
  ...e
175
188
  });
176
- this.#c(this.#s(this.owner, n)), this.#n = n;
189
+ this.#d(this.#l(this.owner, n)), this.#n = n;
177
190
  }
178
191
  shader(e, n) {
179
192
  let r = n + e, o = this.#a.get(r);
180
193
  if (o) return o;
181
- let s = /let direct=[^;]+;/g, c = "+base*frame.radiance.w";
194
+ let s = /let direct=[^;]+;/g, c = "+base*frame.radiance.w*frame.ambient.rgb";
182
195
  if ([...e.matchAll(s)].length !== 1 || e.split(c).length !== 2) throw new t("unsupported", "Terrain relief lighting needs the terrain lit shader and its sun and sky terms.");
183
196
  let l = `
184
197
  @group(${n}) @binding(${i}) var terrainReliefMap:texture_2d<f32>;
@@ -192,7 +205,7 @@ fn terrainReliefApplied(uv:vec2f)->vec2f{
192
205
  return this.#a.size >= 16 && this.#a.clear(), this.#a.set(r, l), l;
193
206
  }
194
207
  bindings() {
195
- return this.#i;
208
+ return this.#n.follow && this.owner && this.#e && (this.#s || this.#u()) && this.#d(this.#l(this.owner, this.#n)), this.#i;
196
209
  }
197
210
  onDeviceRestored(e) {
198
211
  this.#e?.destroy(), this.#e = void 0, this.setup(e);
@@ -100,6 +100,9 @@ export declare class RockSurface implements EnginePlugin {
100
100
  * The WGSL added to core's shader. `rockShade` returns the rock's colour (rgb) and roughness (a) and tilts the normal:
101
101
  * triplanar weights |n|^4; per octave the three projections' value noise and its analytic slopes, summed as a world
102
102
  * gradient, its tangential part (the surface gradient) taken off the normal; the beds from world height.
103
+ * graniteGrain puts one crystal in each cell of a lattice whose edges a low noise bends, so grains are irregular and
104
+ * interlock; the cell's hash picks its mineral, and the result is its brightness over granite's mean.
105
+ * Granite's crystals are millimetres whatever the rock's relief scale: cells of GRANITE_GRAIN metres.
103
106
  */
104
107
  private code;
105
108
  private patch;