@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/deform.js CHANGED
@@ -1,16 +1,17 @@
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);
4
- import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
5
- import { MargoError as t } from "@margoengine/math/errors";
3
+ const t=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
4
+ import { n as e } from "./mesh-patch-BThCHCeS.js";
5
+ import { engineRuntime as __margoRuntime, meshRuntime as n } from "@margoengine/core/extension";
6
+ import { MargoError as r } from "@margoengine/math/errors";
6
7
  //#region src/deform.ts
7
- var n = [
8
+ var i = [
8
9
  0,
9
10
  0,
10
11
  0
11
- ], r = 110, i = 1024, a = "\nstruct Window {origin:vec2i,previous:vec2i,size:u32,first:u32,fade:f32,texel:f32,rim:f32,count:u32,pad:vec2f}\nstruct Stamp {shape:vec4f,ends:vec4f,turn:vec4f,box:vec4i}\n@group(0) @binding(0) var<uniform> w:Window;\n@group(0) @binding(1) var<storage,read_write> marks:array<atomic<u32>>;\n@group(0) @binding(2) var<storage,read> stamps:array<Stamp>;\n@group(0) @binding(3) var relief:texture_storage_2d<rgba16float,write>;\nfn slot(world:vec2i)->u32 {let s=i32(w.size);let m=((world%s)+s)%s;return u32(m.y)*w.size+u32(m.x);}\nfn inside(world:vec2i,origin:vec2i)->bool {return all(world>=origin)&&all(world<origin+vec2i(i32(w.size)));}\n@compute @workgroup_size(8,8) fn age(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=vec2u(w.size))){return;}\n let k=id.y*w.size+id.x;let n=w.size*w.size;let s=i32(w.size);\n let world=w.origin+(((vec2i(id.xy)-w.origin)%s)+s)%s;\n if(w.first==1u||!inside(world,w.previous)){atomicStore(&marks[k],0u);atomicStore(&marks[n+k],0u);return;}\n if(w.fade>0.0){\n atomicStore(&marks[k],bitcast<u32>(max(bitcast<f32>(atomicLoad(&marks[k]))-w.fade,0.0)));\n atomicStore(&marks[n+k],bitcast<u32>(max(bitcast<f32>(atomicLoad(&marks[n+k]))-w.fade*w.rim,0.0)));\n }\n}\n@compute @workgroup_size(8,8) fn stamp(@builtin(global_invocation_id) id:vec3u) {\n if(id.z>=w.count){return;}\n let s=stamps[id.z];if(any(vec2i(id.xy)>=s.box.zw)){return;}\n let world=s.box.xy+vec2i(id.xy);if(!inside(world,w.origin)){return;}\n let p=(vec2f(id.xy)+0.5)*w.texel;var t:f32;\n if(s.shape.x<0.5){let d=p-s.ends.xy;t=length(vec2f(d.x*s.turn.x+d.y*s.turn.y,d.y*s.turn.x-d.x*s.turn.y)/s.shape.wz);}\n else{let ab=s.ends.zw-s.ends.xy;let h=clamp(dot(p-s.ends.xy,ab)/max(dot(ab,ab),1e-12),0.0,1.0);t=length(p-s.ends.xy-ab*h)/s.shape.z;}\n let depth=s.shape.y*(1.0-smoothstep(0.55,1.0,t));let raised=s.shape.y*w.rim*(1.0-smoothstep(0.0,0.45,abs(t-1.2)));\n let k=slot(world);\n if(depth>0.0){atomicMax(&marks[k],bitcast<u32>(depth));}\n if(raised>0.0){atomicMax(&marks[w.size*w.size+k],bitcast<u32>(raised));}\n}\nfn heightAt(c:vec2i)->f32 {\n let s=i32(w.size);let m=((c%s)+s)%s;let k=u32(m.y)*w.size+u32(m.x);\n let d=bitcast<f32>(atomicLoad(&marks[k]));let r=bitcast<f32>(atomicLoad(&marks[w.size*w.size+k]));\n return r*(1.0-smoothstep(0.0,0.02,d))-d;\n}\n@compute @workgroup_size(8,8) fn resolve(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=vec2u(w.size))){return;}\n let c=vec2i(id.xy);let d=bitcast<f32>(atomicLoad(&marks[id.y*w.size+id.x]));\n let gx=(heightAt(c+vec2i(1,0))-heightAt(c-vec2i(1,0)))/(2.0*w.texel);\n let gz=(heightAt(c+vec2i(0,1))-heightAt(c-vec2i(0,1)))/(2.0*w.texel);\n textureStore(relief,c,vec4f(heightAt(c),gx,gz,clamp(d/0.02,0.0,1.0)));\n}", o = (e, t) => `
12
+ ], a = 110, o = 1024, s = "\nstruct Window {origin:vec2i,previous:vec2i,size:u32,first:u32,fade:f32,texel:f32,rim:f32,count:u32,pad:vec2f}\nstruct Stamp {shape:vec4f,ends:vec4f,turn:vec4f,box:vec4i}\n@group(0) @binding(0) var<uniform> w:Window;\n@group(0) @binding(1) var<storage,read_write> marks:array<atomic<u32>>;\n@group(0) @binding(2) var<storage,read> stamps:array<Stamp>;\n@group(0) @binding(3) var relief:texture_storage_2d<rgba16float,write>;\nfn slot(world:vec2i)->u32 {let s=i32(w.size);let m=((world%s)+s)%s;return u32(m.y)*w.size+u32(m.x);}\nfn inside(world:vec2i,origin:vec2i)->bool {return all(world>=origin)&&all(world<origin+vec2i(i32(w.size)));}\n@compute @workgroup_size(8,8) fn age(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=vec2u(w.size))){return;}\n let k=id.y*w.size+id.x;let n=w.size*w.size;let s=i32(w.size);\n let world=w.origin+(((vec2i(id.xy)-w.origin)%s)+s)%s;\n if(w.first==1u||!inside(world,w.previous)){atomicStore(&marks[k],0u);atomicStore(&marks[n+k],0u);return;}\n if(w.fade>0.0){\n atomicStore(&marks[k],bitcast<u32>(max(bitcast<f32>(atomicLoad(&marks[k]))-w.fade,0.0)));\n atomicStore(&marks[n+k],bitcast<u32>(max(bitcast<f32>(atomicLoad(&marks[n+k]))-w.fade*w.rim,0.0)));\n }\n}\n@compute @workgroup_size(8,8) fn stamp(@builtin(global_invocation_id) id:vec3u) {\n if(id.z>=w.count){return;}\n let s=stamps[id.z];if(any(vec2i(id.xy)>=s.box.zw)){return;}\n let world=s.box.xy+vec2i(id.xy);if(!inside(world,w.origin)){return;}\n let p=(vec2f(id.xy)+0.5)*w.texel;var t:f32;\n if(s.shape.x<0.5){let d=p-s.ends.xy;t=length(vec2f(d.x*s.turn.x+d.y*s.turn.y,d.y*s.turn.x-d.x*s.turn.y)/s.shape.wz);}\n else{let ab=s.ends.zw-s.ends.xy;let h=clamp(dot(p-s.ends.xy,ab)/max(dot(ab,ab),1e-12),0.0,1.0);t=length(p-s.ends.xy-ab*h)/s.shape.z;}\n let depth=s.shape.y*(1.0-smoothstep(0.55,1.0,t));let raised=s.shape.y*w.rim*(1.0-smoothstep(0.0,0.45,abs(t-1.2)));\n let k=slot(world);\n if(depth>0.0){atomicMax(&marks[k],bitcast<u32>(depth));}\n if(raised>0.0){atomicMax(&marks[w.size*w.size+k],bitcast<u32>(raised));}\n}\nfn heightAt(c:vec2i)->f32 {\n let s=i32(w.size);let m=((c%s)+s)%s;let k=u32(m.y)*w.size+u32(m.x);\n let d=bitcast<f32>(atomicLoad(&marks[k]));let r=bitcast<f32>(atomicLoad(&marks[w.size*w.size+k]));\n return r*(1.0-smoothstep(0.0,0.02,d))-d;\n}\n@compute @workgroup_size(8,8) fn resolve(@builtin(global_invocation_id) id:vec3u) {\n if(any(id.xy>=vec2u(w.size))){return;}\n let c=vec2i(id.xy);let d=bitcast<f32>(atomicLoad(&marks[id.y*w.size+id.x]));\n let gx=(heightAt(c+vec2i(1,0))-heightAt(c-vec2i(1,0)))/(2.0*w.texel);\n let gz=(heightAt(c+vec2i(0,1))-heightAt(c-vec2i(0,1)))/(2.0*w.texel);\n textureStore(relief,c,vec4f(heightAt(c),gx,gz,clamp(d/0.02,0.0,1.0)));\n}", c = (e, t) => `
12
13
  struct TerrainDeformFrame {window:vec4f,params:vec4f,extra:vec4f,softness:array<vec4f,4>,layers:array<vec4f,4>}
13
- @group(${e}) @binding(${r}) var<uniform> terrainDeform:TerrainDeformFrame;
14
+ @group(${e}) @binding(${a}) var<uniform> terrainDeform:TerrainDeformFrame;
14
15
  @group(${e}) @binding(111) var terrainDeformMap:texture_2d<f32>;
15
16
  var<private> terrainDeformTint:f32=1.0;
16
17
  fn terrainDeformAt(xz:vec2f)->vec4f {
@@ -41,7 +42,18 @@ fn terrainDeformShift(i:Output)->Output {
41
42
  terrainDeformTint=1.0-terrainDeform.params.z*sample.w*soft;
42
43
  return j;
43
44
  }
44
- `, s = "@fragment fn fragment(i:Output)->@location(0) vec4f {", c = "fn terrainColor(position:vec3f,normal:vec3f)->vec3f {", l = 0, u = class u {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
45
+ `, l = 16384, u = "let terrainColorFootprint=max(length(dpdx(i.position)),length(dpdy(i.position)));", d = "n=s.normal;}";
46
+ function f(t, n, r) {
47
+ let i = e(t);
48
+ if (!i) return t;
49
+ let { code: o } = i, s = o.lastIndexOf(d), l = o.indexOf(u), f = s >= 0 ? s + 12 : l >= 0 ? l + 81 : i.at, p = (c(0, 0).replace(`@binding(${a})`, `@binding(${r})`).replace("@binding(111)", `@binding(${r + 1})`).replace("textureSampleLevel(terrainDeformMap,terrainSampler,xz/f.z,0.0)", "terrainDeformLoad(xz/f.z)") + "fn terrainDeformLoad(uv:vec2f)->vec4f {\n let size=vec2i(textureDimensions(terrainDeformMap));let p=fract(uv)*vec2f(size)-0.5;let lo=vec2i(floor(p));let t=p-vec2f(lo);\n let a=(lo+size)%size;let b=(lo+1)%size;\n return mix(mix(textureLoad(terrainDeformMap,a,0),textureLoad(terrainDeformMap,vec2i(b.x,a.y),0),t.x),mix(textureLoad(terrainDeformMap,vec2i(a.x,b.y),0),textureLoad(terrainDeformMap,b,0),t.x),t.y);\n}\n").replace(/terrainDeform[A-Za-z]*/g, (e) => `${e}_${n}`), m = `if(i.terrainId==${n + 1}.0){let d=terrainDeformShift_${n}(i);let g=normalize(i.normal);n=normalize(mix(n,g,terrainDeformAt_${n}(d.position.xz).w)+d.normal-g);i.position=d.position;i.color*=terrainDeformTint_${n};}`;
50
+ return p + o.slice(0, f) + m + o.slice(f);
51
+ }
52
+ function p(e, t, n) {
53
+ let r = (Math.imul(e, 2654435761) ^ Math.imul(t * 4 + n + 1, 2246822507)) >>> 0;
54
+ return r = Math.imul(r ^ r >>> 16, 2146121005) >>> 0, r = Math.imul(r ^ r >>> 15, 2221713035) >>> 0, ((r ^ r >>> 16) >>> 0) / 2147483647 - 1;
55
+ }
56
+ var m = "@fragment fn fragment(i:Output)->@location(0) vec4f {", h = "fn terrainColor(position:vec3f,normal:vec3f)->vec3f {", g = 0, _ = class e {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
45
57
  #e;
46
58
  #t;
47
59
  #n;
@@ -58,29 +70,36 @@ fn terrainDeformShift(i:Output)->Output {
58
70
  #p;
59
71
  #m;
60
72
  #h;
73
+ #g;
74
+ #_;
75
+ #v;
76
+ #y;
77
+ #b;
78
+ #x;
61
79
  constructor(e, t = {}) {
62
- this.terrain = e, this.options = t, this.name = "terrain-deform:" + l++, this.disposed = !1, this.#s = [], this.#u = /* @__PURE__ */ new Float64Array(3), this.#d = [], this.#f = !0, this.#p = /* @__PURE__ */ new Map(), this.#m = /* @__PURE__ */ new ArrayBuffer(48), this.#h = /* @__PURE__ */ new ArrayBuffer(i * 64);
63
- let n = e?.options?.layers?.length;
64
- if (!n || typeof e.addLighting != "function") throw TypeError("Invalid terrain deformation; pass a Terrain");
65
- let r = t ?? {}, a = (e) => typeof e == "number" && e >= 0 && e <= 1, o = (e) => typeof e == "number" && e > 0 && Number.isFinite(e);
66
- if (!Number.isInteger(r.size ?? 1024) || (r.size ?? 1024) < 64 || (r.size ?? 1024) > 2048 || !o(r.texel ?? .025) || !o(r.depth ?? .15) || !a(r.rim ?? .25) || !(typeof (r.fade ?? 120) == "number" && (r.fade ?? 120) >= 0 && Number.isFinite(r.fade ?? 120)) || !a(r.softness ?? 1) || !(r.layers === void 0 || Array.isArray(r.layers) && r.layers.length <= n && r.layers.every(a)) || !a(r.darken ?? .25) || !o(r.distance ?? 40) || !Number.isInteger(r.steps ?? 12) || (r.steps ?? 12) < 4 || (r.steps ?? 12) > 32 || r.follow !== void 0 && typeof r.follow != "function") throw RangeError("Invalid terrain deformation; give size 64..2048, texel, depth and distance above 0, rim, softness, darken and each layers entry 0..1 (at most one per terrain layer), fade 0 or more, steps 4..32 and follow as a function.");
67
- this.size = r.size ?? 1024, this.texel = e.options.floatingOrigin ? Math.fround(this.size * (r.texel ?? .025)) / this.size : r.texel ?? .025, this.depth = r.depth ?? .15;
80
+ this.options = t, this.name = "terrain-deform:" + g++, this.disposed = !1, this.#s = [], this.#u = /* @__PURE__ */ new Float64Array(3), this.#d = [], this.#f = [], this.#h = "", this.#g = "", this.#_ = 0, this.#v = !0, this.#y = /* @__PURE__ */ new Map(), this.#b = /* @__PURE__ */ new ArrayBuffer(48), this.#x = /* @__PURE__ */ new ArrayBuffer(o * 64);
81
+ let n = typeof e?.setGeometry == "function", r = n ? void 0 : e, i = n ? 0 : r?.options?.layers?.length ?? 0;
82
+ if (!n && (!i || typeof r.addLighting != "function")) throw TypeError("Invalid terrain deformation; pass a Terrain or a ground Mesh");
83
+ this.#p = r, this.#m = n ? e : void 0;
84
+ let a = t ?? {}, s = (e) => typeof e == "number" && e >= 0 && e <= 1, c = (e) => typeof e == "number" && e > 0 && Number.isFinite(e);
85
+ if (!Number.isInteger(a.size ?? 1024) || (a.size ?? 1024) < 64 || (a.size ?? 1024) > 2048 || !c(a.texel ?? .025) || !c(a.depth ?? .15) || !s(a.rim ?? .25) || !(typeof (a.fade ?? 120) == "number" && (a.fade ?? 120) >= 0 && Number.isFinite(a.fade ?? 120)) || a.persist !== void 0 && typeof a.persist != "boolean" || a.persist === !0 && (a.fade ?? 0) !== 0 || !s(a.softness ?? 1) || !(a.layers === void 0 || Array.isArray(a.layers) && a.layers.length <= i && a.layers.every(s)) || !s(a.darken ?? .25) || !c(a.distance ?? 40) || !Number.isInteger(a.steps ?? 12) || (a.steps ?? 12) < 4 || (a.steps ?? 12) > 32 || a.follow !== void 0 && typeof a.follow != "function") throw RangeError("Invalid terrain deformation; give size 64..2048, texel, depth and distance above 0, rim, softness, darken and each layers entry 0..1 (at most one per terrain layer), fade 0 or more, steps 4..32 and follow as a function; persist needs fade 0.");
86
+ this.size = a.size ?? 1024, this.texel = r?.options.floatingOrigin ? Math.fround(this.size * (a.texel ?? .025)) / this.size : a.texel ?? .025, this.depth = a.depth ?? .15;
68
87
  }
69
- static attach(t, n, r = {}) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
70
- let i = new u(n, r);
71
- return e(t).use(i), i;
88
+ static attach(n, r, i = {}) {__margoAssertCore(n,__margoCoreRequirement("1.0.0",import.meta.url));
89
+ let a = new e(r, i);
90
+ return t(n).use(a), a;
72
91
  }
73
92
  footprint(e, t, n = {}) {
74
93
  let r = n.length ?? .28, i = n.width ?? .11, a = n.angle ?? 0;
75
- this.#g([
94
+ this.#S([
76
95
  e,
77
96
  t,
78
97
  a
79
98
  ], r, i, n.depth);
80
99
  let o = Math.max(r, i) * .85, s = Math.floor((e - o) / this.texel), c = Math.floor((t - o) / this.texel), l = Math.ceil(2 * o / this.texel) + 2;
81
- return this.#v([
100
+ return this.#w([
82
101
  0,
83
- this.#_(n.depth),
102
+ this.#C(n.depth),
84
103
  i / 2,
85
104
  r / 2,
86
105
  e - s * this.texel,
@@ -99,16 +118,16 @@ fn terrainDeformShift(i:Output)->Output {
99
118
  }
100
119
  track(e, t, n = {}) {
101
120
  let r = n.width ?? .25;
102
- this.#g([
121
+ this.#S([
103
122
  e?.[0],
104
123
  e?.[1],
105
124
  t?.[0],
106
125
  t?.[1]
107
126
  ], r, r, n.depth);
108
- let i = this.#_(n.depth), a = r * .85, o = 188 * this.texel - 2 * a, s = Math.hypot(t[0] - e[0], t[1] - e[1]), c = Math.max(1, Math.ceil(s / o));
127
+ let i = this.#C(n.depth), a = r * .85, o = 188 * this.texel - 2 * a, s = Math.hypot(t[0] - e[0], t[1] - e[1]), c = Math.max(1, Math.ceil(s / o));
109
128
  for (let n = 0; n < c; n++) {
110
129
  let o = [e[0] + (t[0] - e[0]) * n / c, e[1] + (t[1] - e[1]) * n / c], s = [e[0] + (t[0] - e[0]) * (n + 1) / c, e[1] + (t[1] - e[1]) * (n + 1) / c], l = Math.floor((Math.min(o[0], s[0]) - a) / this.texel), u = Math.floor((Math.min(o[1], s[1]) - a) / this.texel), d = Math.ceil((Math.max(o[0], s[0]) + a) / this.texel), f = Math.ceil((Math.max(o[1], s[1]) + a) / this.texel), p = l * this.texel, m = u * this.texel;
111
- this.#v([
130
+ this.#w([
112
131
  1,
113
132
  i,
114
133
  r / 2,
@@ -134,8 +153,35 @@ fn terrainDeformShift(i:Output)->Output {
134
153
  for (let n = 1; n < e.length; n++) this.track(e[n - 1], e[n], t);
135
154
  return e.length === 1 && this.track(e[0], e[0], t), this;
136
155
  }
156
+ walk(e, t = {}) {
157
+ let n = t.step ?? .75, r = t.straddle ?? .08, i = t.toeOut ?? .12, a = t.length ?? .28, o = t.width ?? .11, s = t.jitter ?? .5, c = t.seed ?? 1;
158
+ if (!Array.isArray(e) || e.length < 2 || !e.every((e) => Number.isFinite(e?.[0]) && Number.isFinite(e?.[1])) || !(n > 0 && Number.isFinite(n)) || !(r >= 0 && Number.isFinite(r)) || !(Math.abs(i) <= 1.5) || !(s >= 0 && s <= 1) || !Number.isInteger(c)) throw RangeError("Invalid terrain walk; pass two or more finite [x, z] points, step above 0, straddle 0 or more, toeOut -1.5 to 1.5, jitter 0 to 1 and an integer seed.");
159
+ this.#S([i], a, o, t.depth);
160
+ let l = this.#C(t.depth), u = n / 2, d = 0;
161
+ for (let t = 1; t < e.length; t++) {
162
+ let [f, m] = e[t - 1], [h, g] = e[t], _ = Math.hypot(h - f, g - m);
163
+ if (!_) continue;
164
+ let v = Math.atan2(g - m, h - f), y = Math.cos(v), b = Math.sin(v);
165
+ for (; u <= _; d++) {
166
+ let e = d % 2 ? 1 : -1, t = r / 2 * e + .04 * s * p(c, d, 0), h = f + y * u - b * t, g = m + b * u + y * t, _ = v + e * i + .1 * s * p(c, d, 1), x = l * (1 - .35 * s * Math.abs(p(c, d, 2))), S = Math.cos(_), C = Math.sin(_);
167
+ this.footprint(h - S * a * .31, g - C * a * .31, {
168
+ angle: _,
169
+ length: a * .38,
170
+ width: o * .8,
171
+ depth: x * .85
172
+ }), this.footprint(h + S * a * .18, g + C * a * .18, {
173
+ angle: _,
174
+ length: a * .64,
175
+ width: o,
176
+ depth: x
177
+ }), u += n * (1 + .12 * s * p(c, d, 3));
178
+ }
179
+ u -= _;
180
+ }
181
+ return this;
182
+ }
137
183
  clear() {
138
- return this.#d = [], this.#f = !0, this;
184
+ return this.#d = [], this.#f.length = 0, this.#v = !0, this;
139
185
  }
140
186
  get texture() {
141
187
  return this.#i;
@@ -143,38 +189,54 @@ fn terrainDeformShift(i:Output)->Output {
143
189
  get pending() {
144
190
  return this.#d.length;
145
191
  }
146
- #g(e, t, n, r) {
192
+ #S(e, t, n, r) {
147
193
  let i = 188 * this.texel * .5;
148
194
  if (!e.every((e) => typeof e == "number" && Number.isFinite(e)) || !(t > 0 && t <= i) || !(n > 0 && n <= i) || !(r === void 0 || typeof r == "number" && r >= 0 && Number.isFinite(r))) throw RangeError(`Invalid terrain print; give finite positions, length and width above 0 up to ${i.toFixed(3)} world units and depth 0 or more.`);
149
195
  }
150
- #_(e) {
196
+ #C(e) {
151
197
  return Math.min(e ?? this.depth, this.depth);
152
198
  }
153
- #v(e) {
199
+ #w(e) {
200
+ if (this.options.persist) {
201
+ if (this.#f.length >= l) throw RangeError(`Terrain deformation keeps at most ${l} prints under persist; clear() or lay fewer`);
202
+ this.#f.push(e);
203
+ }
154
204
  this.#d.push(e);
155
205
  }
206
+ #T(e, t) {
207
+ let n = this.size, r = new Set(this.#d);
208
+ for (let i of this.#f) {
209
+ let [a, o, s, c] = [
210
+ i[12],
211
+ i[13],
212
+ i[14],
213
+ i[15]
214
+ ], l = a < e[0] + n && a + s > e[0] && o < e[1] + n && o + c > e[1], u = !!t && a >= t[0] && a + s <= t[0] + n && o >= t[1] && o + c <= t[1] + n;
215
+ l && !u && !r.has(i) && this.#d.push(i);
216
+ }
217
+ }
156
218
  setup(e) {
157
- if (this.disposed || !e.gpu) throw new t(this.disposed ? "disposed" : "unsupported", "Terrain deformation requires WebGPU and a live module; attach a new one to a WebGPU engine; see https://margoengine.com/errors#webgpu");
158
- let n = e.gpu, o = this.size, s = 8 * o * o;
159
- if (o > n.limits.maxTextureDimension2D || s > n.limits.maxStorageBufferBindingSize) throw new t("unsupported", "The terrain deformation map is larger than this device allows; pass a smaller size");
160
- this.owner = e, this.#e = n.createBuffer({
161
- size: s,
219
+ if (this.disposed || !e.gpu) throw new r(this.disposed ? "disposed" : "unsupported", "Terrain deformation requires WebGPU and a live module; attach a new one to a WebGPU engine; see https://margoengine.com/errors#webgpu");
220
+ let t = e.gpu, i = this.size, c = 8 * i * i;
221
+ if (i > t.limits.maxTextureDimension2D || c > t.limits.maxStorageBufferBindingSize) throw new r("unsupported", "The terrain deformation map is larger than this device allows; pass a smaller size");
222
+ this.owner = e, this.#e = t.createBuffer({
223
+ size: c,
162
224
  usage: 128
163
- }), this.#t = n.createBuffer({
164
- size: i * 64,
225
+ }), this.#t = t.createBuffer({
226
+ size: o * 64,
165
227
  usage: 136
166
- }), this.#n = n.createBuffer({
228
+ }), this.#n = t.createBuffer({
167
229
  size: 48,
168
230
  usage: 72
169
- }), this.#r = n.createBuffer({
231
+ }), this.#r = t.createBuffer({
170
232
  size: 176,
171
233
  usage: 72
172
- }), this.#i = n.createTexture({
173
- size: [o, o],
234
+ }), this.#i = t.createTexture({
235
+ size: [i, i],
174
236
  format: "rgba16float",
175
237
  usage: 13
176
238
  });
177
- let c = n.createBindGroupLayout({ entries: [
239
+ let l = t.createBindGroupLayout({ entries: [
178
240
  {
179
241
  binding: 0,
180
242
  visibility: 4,
@@ -198,19 +260,19 @@ fn terrainDeformShift(i:Output)->Output {
198
260
  format: "rgba16float"
199
261
  }
200
262
  }
201
- ] }), l = n.createShaderModule({ code: a }), u = n.createPipelineLayout({ bindGroupLayouts: [c] }), d = (e) => n.createComputePipeline({
202
- layout: u,
263
+ ] }), u = t.createShaderModule({ code: s }), d = t.createPipelineLayout({ bindGroupLayouts: [l] }), f = (e) => t.createComputePipeline({
264
+ layout: d,
203
265
  compute: {
204
- module: l,
266
+ module: u,
205
267
  entryPoint: e
206
268
  }
207
269
  });
208
270
  this.#o = {
209
- age: d("age"),
210
- stamp: d("stamp"),
211
- resolve: d("resolve")
212
- }, this.#a = n.createBindGroup({
213
- layout: c,
271
+ age: f("age"),
272
+ stamp: f("stamp"),
273
+ resolve: f("resolve")
274
+ }, this.#a = t.createBindGroup({
275
+ layout: l,
214
276
  entries: [
215
277
  {
216
278
  binding: 0,
@@ -230,93 +292,113 @@ fn terrainDeformShift(i:Output)->Output {
230
292
  }
231
293
  ]
232
294
  });
233
- let f = this.options, p = /* @__PURE__ */ new Float32Array(44), m = this.terrain.options.layers.length;
234
- p.set([
295
+ let p = this.options, m = /* @__PURE__ */ new Float32Array(44), h = this.#p?.options.layers.length ?? 0;
296
+ m.set([
235
297
  1e30,
236
298
  1e30,
237
- o * this.texel,
238
- o
239
- ], 0), p.set([
240
- f.distance ?? 40,
241
- f.steps ?? 12,
242
- f.darken ?? .25,
299
+ i * this.texel,
300
+ i
301
+ ], 0), m.set([
302
+ p.distance ?? 40,
303
+ p.steps ?? 12,
304
+ p.darken ?? .25,
243
305
  this.depth
244
- ], 4), p.set([
245
- this.depth * (f.rim ?? .25),
246
- f.softness ?? 1,
306
+ ], 4), m.set([
307
+ this.depth * (p.rim ?? .25),
308
+ p.softness ?? 1,
247
309
  0,
248
310
  0
249
311
  ], 8);
250
- for (let e = 0; e < m; e++) p[12 + e] = f.layers?.[e] ?? f.softness ?? 1, p[28 + e] = 1;
251
- n.queue.writeBuffer(this.#r, 0, p), this.#l = void 0, this.#f = !0, this.#s = [{
252
- binding: r,
312
+ for (let e = 0; e < h; e++) m[12 + e] = p.layers?.[e] ?? p.softness ?? 1, m[28 + e] = 1;
313
+ if (t.queue.writeBuffer(this.#r, 0, m), this.#l = void 0, this.#v = !0, this.#s = [{
314
+ binding: a,
253
315
  resource: { buffer: this.#r }
254
316
  }, {
255
317
  binding: 111,
256
318
  resource: this.#i.createView()
257
- }], this.#c ??= this.terrain.addLighting(this);
319
+ }], this.#p) this.#c ??= this.#p.addLighting(this);
320
+ else if (n(this.#m).engine !== e) throw new r("attached", "The ground mesh belongs to another engine; attach to the mesh's engine");
321
+ this.#h = "";
258
322
  }
259
323
  registerPasses(e) {
260
324
  e.add({
261
325
  name: this.name,
262
326
  order: -5,
263
327
  execute: (e) => {
264
- e.backend === "webgpu" && !(e.view?.index ?? 0) && this.#y(e);
328
+ e.backend === "webgpu" && !(e.view?.index ?? 0) && this.#E(e);
329
+ }
330
+ }), this.#m && e.add({
331
+ name: this.name + ":mesh",
332
+ order: -.5,
333
+ execute: (e) => {
334
+ if (e.backend !== "webgpu" || this.disposed || !this.#s.length) return;
335
+ let t = n(this.#m);
336
+ if (t.check(), this.#h !== e.shader) {
337
+ let n = 44;
338
+ for (; e.bindings.some((e) => e.binding === n || e.binding === n + 1);) n++;
339
+ this.#h = e.shader, this.#_ = n, this.#g = f(e.shader, t.id, n);
340
+ }
341
+ this.#g !== e.shader && (this.#s.forEach((t, n) => e.bindings.push({
342
+ binding: this.#_ + n,
343
+ resource: t.resource
344
+ })), e.shader = this.#g);
265
345
  }
266
346
  });
267
347
  }
268
- #y(e) {
348
+ #E(e) {
269
349
  if (this.disposed || !this.#o) return;
270
- let t = this.size, r = !!this.terrain.options.floatingOrigin, a = r ? this.terrain.shift : n, o = this.options.follow?.() ?? (r ? [e.camera.position[0] + a[0], e.camera.position[2] + a[2]] : [e.camera.position[0], e.camera.position[2]]);
271
- if (!Number.isFinite(o[0]) || !Number.isFinite(o[1])) return;
272
- let s = [Math.floor(o[0] / this.texel) - t / 2, Math.floor(o[1] / this.texel) - t / 2], c = this.#l ?? s, l = this.#f, u = r && (this.#u[0] !== a[0] || this.#u[2] !== a[2]), d = c[0] !== s[0] || c[1] !== s[1] || !this.#l, f = this.options.fade ?? 120, p = f > 0 ? this.depth * Math.max(e.delta, 0) / f : 0, m = Math.min(this.#d.length, i);
273
- if (u || d && r) {
274
- let n = t * this.texel, r = (e) => e - Math.floor(e / n) * n;
275
- e.device.queue.writeBuffer(this.#r, 0, new Float32Array([(s[0] + t / 2) * this.texel - a[0], (s[1] + t / 2) * this.texel - a[2]])), e.device.queue.writeBuffer(this.#r, 40, new Float32Array([r(a[0]), r(a[2])])), this.#u.set([
276
- a[0],
277
- a[1],
278
- a[2]
350
+ let t = this.size, n = !!this.#p?.options.floatingOrigin, r = n ? this.#p.shift : i, a = this.options.follow?.() ?? (n ? [e.camera.position[0] + r[0], e.camera.position[2] + r[2]] : [e.camera.position[0], e.camera.position[2]]);
351
+ if (!Number.isFinite(a[0]) || !Number.isFinite(a[1])) return;
352
+ let s = [Math.floor(a[0] / this.texel) - t / 2, Math.floor(a[1] / this.texel) - t / 2], c = this.#l ?? s, l = this.#v, u = n && (this.#u[0] !== r[0] || this.#u[2] !== r[2]), d = c[0] !== s[0] || c[1] !== s[1] || !this.#l, f = this.options.fade ?? (this.options.persist ? 0 : 120), p = f > 0 ? this.depth * Math.max(e.delta, 0) / f : 0;
353
+ this.options.persist && (l || d) && this.#T(s, l ? void 0 : c);
354
+ let m = Math.min(this.#d.length, o);
355
+ if (u || d && n) {
356
+ let n = t * this.texel, i = (e) => e - Math.floor(e / n) * n;
357
+ e.device.queue.writeBuffer(this.#r, 0, new Float32Array([(s[0] + t / 2) * this.texel - r[0], (s[1] + t / 2) * this.texel - r[2]])), e.device.queue.writeBuffer(this.#r, 40, new Float32Array([i(r[0]), i(r[2])])), this.#u.set([
358
+ r[0],
359
+ r[1],
360
+ r[2]
279
361
  ]);
280
362
  }
281
363
  if (!l && !d && !p && !m) return;
282
- let h = new Int32Array(this.#m), g = new Float32Array(this.#m), _ = new Uint32Array(this.#m);
364
+ let h = new Int32Array(this.#b), g = new Float32Array(this.#b), _ = new Uint32Array(this.#b);
283
365
  h.set([
284
366
  s[0],
285
367
  s[1],
286
368
  c[0],
287
369
  c[1]
288
- ]), _[4] = t, _[5] = +l, g[6] = p, g[7] = this.texel, g[8] = this.options.rim ?? .25, _[9] = m, e.device.queue.writeBuffer(this.#n, 0, this.#m), d && !r && e.device.queue.writeBuffer(this.#r, 0, new Float32Array([(s[0] + t / 2) * this.texel, (s[1] + t / 2) * this.texel])), d && (this.#l = s);
370
+ ]), _[4] = t, _[5] = +l, g[6] = p, g[7] = this.texel, g[8] = this.options.rim ?? .25, _[9] = m, e.device.queue.writeBuffer(this.#n, 0, this.#b), d && !n && e.device.queue.writeBuffer(this.#r, 0, new Float32Array([(s[0] + t / 2) * this.texel, (s[1] + t / 2) * this.texel])), d && (this.#l = s);
289
371
  let v = e.encoder.beginComputePass(), y = Math.ceil(t / 8);
290
372
  if (v.setBindGroup(0, this.#a), (l || d || p) && (v.setPipeline(this.#o.age), v.dispatchWorkgroups(y, y)), m) {
291
- let t = this.#d.splice(0, m), n = new Float32Array(this.#h), r = new Int32Array(this.#h), i = 1;
373
+ let t = this.#d.splice(0, m), n = new Float32Array(this.#x), r = new Int32Array(this.#x), i = 1;
292
374
  t.forEach((e, t) => {
293
375
  n.set(e.slice(0, 12), t * 16), r.set(e.slice(12), t * 16 + 12), i = Math.max(i, e[14], e[15]);
294
- }), e.device.queue.writeBuffer(this.#t, 0, this.#h, 0, m * 64), v.setPipeline(this.#o.stamp), v.dispatchWorkgroups(Math.ceil(i / 8), Math.ceil(i / 8), m);
376
+ }), e.device.queue.writeBuffer(this.#t, 0, this.#x, 0, m * 64), v.setPipeline(this.#o.stamp), v.dispatchWorkgroups(Math.ceil(i / 8), Math.ceil(i / 8), m);
295
377
  }
296
- v.setPipeline(this.#o.resolve), v.dispatchWorkgroups(y, y), v.end(), this.#f = !1;
378
+ v.setPipeline(this.#o.resolve), v.dispatchWorkgroups(y, y), v.end(), this.#v = !1;
297
379
  }
298
380
  bindings() {
299
381
  return this.#s;
300
382
  }
301
- shader(e, n) {
302
- let r = n + e, i = this.#p.get(r);
383
+ shader(e, t) {
384
+ let n = t + e, i = this.#y.get(n);
303
385
  if (i) return i;
304
- if (e.split(s).length !== 2) throw new t("unsupported", "Terrain deformation needs the terrain's lit fragment shader; attach it before effects that replace it");
305
- let a = [...e.matchAll(/var terrainWeights(\d):texture_2d<f32>;/g)].length, l = e.replace(s, "fn terrainDeformInner(i:Output)->vec4f {");
306
- return l.split(c).length === 2 && (l = l.replace(c, "fn terrainColorUndeformed(position:vec3f,normal:vec3f)->vec3f {")), l = (this.terrain.options.floatingOrigin ? o(n, a).replace("xz/f.z,0.0)", "(xz+terrainDeform.extra.zw)/f.z,0.0)") : o(n, a)) + l + (l.includes("terrainColorUndeformed") ? `\n${c}return terrainColorUndeformed(position,normal)*terrainDeformTint;}` : "") + `\n${s}return terrainDeformInner(terrainDeformShift(i));}`, this.#p.size >= 16 && this.#p.clear(), this.#p.set(r, l), l;
386
+ if (e.split(m).length !== 2) throw new r("unsupported", "Terrain deformation needs the terrain's lit fragment shader; attach it before effects that replace it");
387
+ let a = [...e.matchAll(/var terrainWeights(\d):texture_2d<f32>;/g)].length, o = e.replace(m, "fn terrainDeformInner(i:Output)->vec4f {");
388
+ return o.split(h).length === 2 && (o = o.replace(h, "fn terrainColorUndeformed(position:vec3f,normal:vec3f)->vec3f {")), o = (this.#p.options.floatingOrigin ? c(t, a).replace("xz/f.z,0.0)", "(xz+terrainDeform.extra.zw)/f.z,0.0)") : c(t, a)) + o + (o.includes("terrainColorUndeformed") ? `\n${h}return terrainColorUndeformed(position,normal)*terrainDeformTint;}` : "") + `\n${m}return terrainDeformInner(terrainDeformShift(i));}`, this.#y.size >= 16 && this.#y.clear(), this.#y.set(n, o), o;
307
389
  }
308
390
  onDeviceRestored(e) {
309
- this.#b(), this.setup(e);
391
+ this.#D(), this.setup(e);
310
392
  }
311
- #b() {
393
+ #D() {
312
394
  this.#e?.destroy(), this.#t?.destroy(), this.#n?.destroy(), this.#r?.destroy(), this.#i?.destroy(), this.#e = this.#t = this.#n = this.#r = this.#i = void 0, this.#a = this.#o = void 0, this.#s = [];
313
395
  }
314
396
  dispose() {
315
- this.disposed || (this.disposed = !0, this.#c?.(), this.#c = void 0, this.owner?.sceneOf(this)?.removePlugin(this.name), this.#b(), this.#p.clear(), this.#d = [], this.owner = void 0);
397
+ this.disposed || (this.disposed = !0, this.#c?.(), this.#c = void 0, this.owner?.sceneOf(this)?.removePlugin(this.name), this.#D(), this.#y.clear(), this.#d = [], this.owner = void 0);
316
398
  }
317
399
  [Symbol.dispose ?? Symbol()]() {
318
400
  this.dispose();
319
401
  }
320
402
  };
321
403
  //#endregion
322
- export { u as TerrainDeformation };
404
+ export { _ as TerrainDeformation };
package/dist/displace.js CHANGED
@@ -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/displace.ts
@@ -138,7 +138,7 @@ fn terrainDisplace(position:vec3f,normal:vec3f)->vec3f {
138
138
  h=max(h,textureLoad(relief,(vec2i(id.xy)*${e ? 1 : 2}+vec2i(x,y))%m,i32(id.z),0).${e ? "a" : "r"});
139
139
  }}
140
140
  textureStore(maxima,vec2i(id.xy),i32(id.z),vec4f(h));
141
- }`, f = 0, p = class r {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
141
+ }`, f = 0, p = class r {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
142
142
  #e;
143
143
  #t;
144
144
  #n;
@@ -152,7 +152,7 @@ fn terrainDisplace(position:vec3f,normal:vec3f)->vec3f {
152
152
  let i = n ?? {}, a = i.layers ?? [], o = (e) => typeof e == "number" && e >= 0 && e <= 1;
153
153
  if (!o(i.depth ?? .05) || !Array.isArray(a) || a.length > r || !a.every((e) => o(e?.depth ?? 0)) || !((i.distance ?? 30) > 0 && Number.isFinite(i.distance ?? 30)) || !Number.isInteger(i.steps ?? 24) || (i.steps ?? 24) < 4 || (i.steps ?? 24) > 64 || typeof (i.shadows ?? !0) != "boolean") throw RangeError("Invalid terrain displacement; give depth 0..1, at most one layers entry per terrain layer with depth 0..1, distance above 0, steps 4..64 and shadows true or false.");
154
154
  }
155
- static attach(t, n, i, a = {}) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
155
+ static attach(t, n, i, a = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
156
156
  let o = new r(n, i, a);
157
157
  return e(t).use(o), o;
158
158
  }
@@ -0,0 +1,182 @@
1
+ import { type GridFill, type GridHeightField } from './height-grid.js';
2
+ /** One decoded raster page: `openTIFF(bytes).read(0)` from `@margoengine/decoders/tiff` has this shape. */
3
+ export interface ElevationRaster {
4
+ /** Samples across. */
5
+ readonly width: number;
6
+ /** Samples down. */
7
+ readonly height: number;
8
+ /** Interleaved samples, row 0 first (a Float32Array of a float GeoTIFF, an Int16Array of SRTM, any numbers). */
9
+ readonly samples: ArrayLike<number>;
10
+ /** Samples per pixel (bands, interleaved). Default 1. */
11
+ readonly samplesPerPixel?: number;
12
+ }
13
+ /** Where the raster lies: `readGeoTIFF(page)` from `@margoengine/decoders/geotiff` has this shape. */
14
+ export interface ElevationGeoreference {
15
+ /** Raster (i, j) to longitude and latitude: lon = a i + b j + c, lat = d i + e j + f. */
16
+ readonly transform: readonly [number, number, number, number, number, number] | null;
17
+ /** `'area'` (sample at the cell centre) or `'point'` (sample at the raster point). */
18
+ readonly raster: 'area' | 'point';
19
+ /** A sample value that means no data, or null. */
20
+ readonly noData: number | null;
21
+ /** The coordinate system; only `'geographic'` (degrees) is read. */
22
+ readonly crs: {
23
+ readonly model: string;
24
+ readonly epsg: number | null;
25
+ readonly citation: string | null;
26
+ };
27
+ /** Unit of the samples, null when the file does not say (then metres are assumed). */
28
+ readonly verticalUnits: 'metre' | 'foot' | 'us-foot' | null;
29
+ }
30
+ /** A latitude and longitude box, degrees (north positive, east positive). */
31
+ export interface GeoWindow {
32
+ readonly south: number;
33
+ readonly west: number;
34
+ readonly north: number;
35
+ readonly east: number;
36
+ }
37
+ /** Options of {@link readElevation}. */
38
+ export interface ElevationOptions {
39
+ /** Keeps only the samples whose cells meet this box (plus one ring, so a lookup reaches its edge). Default the whole raster. */
40
+ readonly window?: GeoWindow;
41
+ /** Which band of a multi-band raster. Default 0. */
42
+ readonly band?: number;
43
+ /**
44
+ * The vertical datum the heights are measured from, as text. A GeoTIFF rarely says; name it from the data's own
45
+ * description (for example `'Lake Superior Low Water Datum, 183.2 m IGLD 1985'` for the NOAA NCEI Great Lakes grids).
46
+ * Default `'unknown'`.
47
+ */
48
+ readonly datum?: string;
49
+ /**
50
+ * Multiplies every sample to give metres up: -1 for a file of positive depths. Default 1, or 0.3048 for feet and
51
+ * 1200 / 3937 for US survey feet when the file's VerticalUnits says so.
52
+ */
53
+ readonly scale?: number;
54
+ }
55
+ /** A cropped elevation grid in geographic coordinates. */
56
+ export interface Elevation {
57
+ /** Samples across (west to east). */
58
+ readonly width: number;
59
+ /** Samples down (north to south). */
60
+ readonly height: number;
61
+ /** Heights in metres up from {@link Elevation.datum}, row 0 the north row; NaN where the data has none. */
62
+ readonly heights: Float32Array;
63
+ /** Longitude of the first column's samples and latitude of the first row's, degrees. */
64
+ readonly west: number;
65
+ readonly north: number;
66
+ /** Degrees between neighbouring samples, along a row (east) and down a column (south), both positive. */
67
+ readonly step: readonly [number, number];
68
+ /** Longitude of the last column's samples and latitude of the last row's, degrees. */
69
+ readonly east: number;
70
+ readonly south: number;
71
+ /** The vertical datum, as given. */
72
+ readonly datum: string;
73
+ /** The horizontal datum: the file's EPSG code and name. */
74
+ readonly crs: {
75
+ readonly epsg: number | null;
76
+ readonly citation: string | null;
77
+ };
78
+ /** Samples with no data. */
79
+ readonly missing: number;
80
+ /** Lowest and highest valid height, metres. */
81
+ readonly min: number;
82
+ readonly max: number;
83
+ /** Bilinear height at a latitude and longitude, metres; NaN outside the samples or next to a missing one. */
84
+ at(lat: number, lon: number): number;
85
+ }
86
+ /**
87
+ * Crops a georeferenced raster band to a latitude and longitude window and names its vertical datum.
88
+ * @example
89
+ * import { openTIFF } from '@margoengine/decoders/tiff';
90
+ * import { readGeoTIFF } from '@margoengine/decoders/geotiff';
91
+ * const page = openTIFF(bytes).read(0);
92
+ * const lake = readElevation(page, readGeoTIFF(page), {
93
+ * window: { south: 46.9, west: -86.0, north: 47.5, east: -85.0 },
94
+ * datum: 'Lake Superior Low Water Datum, 183.2 m IGLD 1985',
95
+ * });
96
+ * lake.at(46.9983, -85.11); // metres below the datum are negative
97
+ * @throws RangeError for a raster that is not georeferenced in latitude and longitude, is rotated, has the wrong
98
+ * number of samples, or a window that misses it.
99
+ */
100
+ export declare function readElevation(raster: ElevationRaster, georeference: ElevationGeoreference, options?: ElevationOptions): Elevation;
101
+ /** An ellipsoid: semi-major axis `a`, metres, and flattening `f`. */
102
+ export interface Ellipsoid {
103
+ readonly a: number;
104
+ readonly f: number;
105
+ }
106
+ /** GRS 80 (Moritz 1980), the ellipsoid of NAD 83 (EPSG:4269). */
107
+ export declare const GRS80: Ellipsoid;
108
+ /** WGS 84 (NIMA TR8350.2), the ellipsoid of GPS (EPSG:4326). Within 0.1 mm of GRS 80. */
109
+ export declare const WGS84: Ellipsoid;
110
+ /** A site on the earth: latitude and longitude, degrees, and height above the ellipsoid, metres. */
111
+ export interface Site {
112
+ readonly lat: number;
113
+ readonly lon: number;
114
+ readonly height?: number;
115
+ }
116
+ /** A local tangent plane at a site, in metres: x east, y up, z south (north is -z), the site at the origin. */
117
+ export interface LocalFrame {
118
+ readonly site: Required<Site>;
119
+ readonly ellipsoid: Ellipsoid;
120
+ /** World [x, y, z] metres of a latitude, longitude and ellipsoid height. */
121
+ toLocal(lat: number, lon: number, height?: number): [number, number, number];
122
+ /** [lat, lon, height] of world (x, y, z) metres; `y` defaults to 0 (a point on the plane). */
123
+ toGeographic(x: number, z: number, y?: number): [number, number, number];
124
+ }
125
+ /**
126
+ * The local east, north, up frame at a site (Hofmann-Wellenhof et al. 2008, section 8.2.2), turned to the engine's axes:
127
+ * x east, y up, z south. Exact on the ellipsoid in both directions (Bowring's 1976 inverse, iterated to below 0.1 mm).
128
+ * @example
129
+ * const frame = localFrame({ lat: 46.9983, lon: -85.11 });
130
+ * frame.toLocal(47.0073, -85.11); // about [0, -0.08, -1000]: 1 km north, 8 cm below the plane by the curve
131
+ * @throws RangeError for a latitude outside -90 to 90, a non-finite longitude or height, or a bad ellipsoid.
132
+ */
133
+ export declare function localFrame(site: Site, ellipsoid?: Ellipsoid): LocalFrame;
134
+ /** Options of {@link elevationField}. */
135
+ export interface ElevationFieldOptions {
136
+ /** The site at the world origin, or a frame from {@link localFrame}. */
137
+ readonly site: Site | LocalFrame;
138
+ /**
139
+ * World [x, z] extent of the grid, metres. Default the largest box the elevation covers whose centre is the site's
140
+ * (so the world origin is the site), a whole number of `spacing` each side, so the site is a texel.
141
+ */
142
+ readonly size?: readonly [number, number];
143
+ /** World x and z of the grid's minimum corner. Default `-size / 2` (the site at the centre). */
144
+ readonly origin?: readonly [number, number];
145
+ /** Metres between texels of the resampled grid. Default the source's own spacing north to south at the site. */
146
+ readonly spacing?: number;
147
+ /** How a missing sample is filled, as `gridHeightField`'s `fill`. Default `'nearest'`. */
148
+ readonly fill?: GridFill;
149
+ /**
150
+ * `false` (default): y is the height above the datum, so a level water surface at the datum stays flat at y = 0 across
151
+ * the whole window. `true`: y is the tangent plane's up, so the ground falls away by about d^2 / 2R (70 m at 30 km).
152
+ */
153
+ readonly curvature?: boolean;
154
+ /**
155
+ * Relief finer than the source cells, metres added to each resampled texel at its world (x, z): the engine's own
156
+ * procedural detail, never surveyed data. The result's `detailed` is true when it is given. Default none.
157
+ */
158
+ readonly detail?: (x: number, z: number) => number;
159
+ }
160
+ /** A height function in local metres resampled from an {@link Elevation}. */
161
+ export interface ElevationField extends GridHeightField {
162
+ /** The frame its world coordinates are in. */
163
+ readonly frame: LocalFrame;
164
+ /** The vertical datum heights are measured from (`'tangent plane'` when `curvature` is true). */
165
+ readonly datum: string;
166
+ /** Metres between the source samples (east, south) at the site: the finest surveyed relief the field holds. */
167
+ readonly surveyed: readonly [number, number];
168
+ /** Metres between the resampled texels. */
169
+ readonly spacing: number;
170
+ /** Whether procedural `detail` was added (relief below `surveyed` is then not survey data). */
171
+ readonly detailed: boolean;
172
+ }
173
+ /**
174
+ * Resamples an elevation onto a site's local tangent plane: a height function `(x, z) => y` in metres for `Terrain`,
175
+ * `heightfield`, `TerrainQuery` or the physics `TerrainBed` (its `elevations`), x east and z south of the site.
176
+ * @example
177
+ * const bed = elevationField(lake, { site: { lat: 46.9983, lon: -85.11 }, spacing: 90 });
178
+ * bed(0, 0); // the depth at the site, metres below the datum
179
+ * @throws RangeError for a size or origin the elevation does not cover sensibly, a spacing that is not positive, or more
180
+ * than 67,108,864 texels.
181
+ */
182
+ export declare function elevationField(elevation: Elevation, options: ElevationFieldOptions): ElevationField;