@margoengine/terrain 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +2366 -2
- package/dist/bed-mesh.d.ts +40 -0
- package/dist/bed-mesh.js +95 -0
- package/dist/blend.d.ts +70 -0
- package/dist/blend.js +159 -0
- package/dist/colors.d.ts +170 -0
- package/dist/deform.d.ts +134 -0
- package/dist/deform.js +404 -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/elevation.d.ts +182 -0
- package/dist/elevation.js +155 -0
- package/dist/errors-fc4i1pSL.js +7 -0
- package/dist/far.d.ts +66 -0
- package/dist/far.js +191 -0
- package/dist/furrows.d.ts +74 -0
- package/dist/furrows.js +157 -0
- package/dist/grid.d.ts +14 -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 +1412 -0
- package/dist/ground-contact-settings.d.ts +27 -0
- package/dist/ground-contact-settings.js +22 -0
- package/dist/ground-contact.d.ts +91 -0
- package/dist/ground-contact.js +241 -0
- package/dist/ground-model.d.ts +372 -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 +408 -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-complex.d.ts +31 -0
- package/dist/planet-craters.d.ts +80 -0
- package/dist/planet-rays.d.ts +37 -0
- package/dist/planet-recipes.d.ts +84 -0
- package/dist/planet-recipes.js +442 -0
- package/dist/planet.d.ts +144 -0
- package/dist/planet.js +610 -0
- package/dist/query.d.ts +150 -0
- package/dist/query.js +243 -0
- package/dist/relief-lighting.d.ts +53 -0
- package/dist/relief-lighting.js +221 -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 +114 -0
- package/dist/rock-surface.js +380 -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/scour.d.ts +90 -0
- package/dist/scour.js +153 -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 +176 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/src-Cr7Gm4n0.js +1366 -0
- package/dist/stream-window.d.ts +123 -0
- package/dist/stream.d.ts +169 -0
- package/dist/stream.js +531 -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 +173 -4
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Geometry } from '@margoengine/core';
|
|
2
|
+
import type { Bed } from '@margoengine/math/fields';
|
|
3
|
+
/** What a {@link BedMesh} update rewrote. */
|
|
4
|
+
export interface BedPatch {
|
|
5
|
+
/** Vertices whose elevation changed. */
|
|
6
|
+
readonly changed: number;
|
|
7
|
+
/** Vertices whose position or normal was rewritten (the changed ones and their neighbours). */
|
|
8
|
+
readonly rewritten: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A mesh of a terrain bed. Build it once from the bed, then call {@link BedMesh.update} after the world steps and set
|
|
12
|
+
* the returned geometry on the mesh when it changed.
|
|
13
|
+
* ```ts
|
|
14
|
+
* const bedMesh = new BedMesh(terrain.bed);
|
|
15
|
+
* const mesh = engine.createMesh().setGeometry(bedMesh.geometry()).setMaterial(mud);
|
|
16
|
+
* // each frame, after world.step():
|
|
17
|
+
* if (bedMesh.update(terrain.bed).changed) mesh.setGeometry(bedMesh.geometry());
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class BedMesh {
|
|
21
|
+
/** Vertex positions, xyz m, in the bed's layout (element r + (rows + 1) c). */
|
|
22
|
+
readonly positions: Float32Array;
|
|
23
|
+
/** Unit vertex normals. */
|
|
24
|
+
readonly normals: Float32Array;
|
|
25
|
+
/** Triangles, the kernel's split along (r + 1, c) to (r, c + 1), counter-clockwise seen from above. */
|
|
26
|
+
readonly indices: Uint32Array;
|
|
27
|
+
/** The elevations the mesh holds, f64. */
|
|
28
|
+
readonly elevations: Float64Array;
|
|
29
|
+
private readonly shape;
|
|
30
|
+
private cached?;
|
|
31
|
+
constructor(bed: Bed);
|
|
32
|
+
/** The current geometry (a new Geometry after an update that changed something). */
|
|
33
|
+
geometry(): Geometry;
|
|
34
|
+
/**
|
|
35
|
+
* Takes the bed's current elevations and rewrites the vertices that changed and the normals around them.
|
|
36
|
+
* @throws RangeError when the bed's grid is not the one this mesh was built from.
|
|
37
|
+
*/
|
|
38
|
+
update(bed: Bed): BedPatch;
|
|
39
|
+
private normalAt;
|
|
40
|
+
}
|
package/dist/bed-mesh.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Geometry as e } from "@margoengine/core";
|
|
2
|
+
//#region src/bed-mesh.ts
|
|
3
|
+
var t = (e) => {
|
|
4
|
+
let { columns: t, rows: n, width: r, depth: i, centre: a } = e;
|
|
5
|
+
if (!Number.isInteger(t) || !Number.isInteger(n) || t < 1 || n < 1) throw RangeError("A bed mesh needs whole numbers of columns and rows from 1 up");
|
|
6
|
+
if (!(r > 0 && i > 0 && Number.isFinite(r) && Number.isFinite(i))) throw RangeError("A bed mesh needs a finite width and depth above zero, m");
|
|
7
|
+
if (!a || a.length !== 2 || !a.every(Number.isFinite)) throw RangeError("A bed mesh centre is [x, z], two finite metres");
|
|
8
|
+
let o = (t + 1) * (n + 1);
|
|
9
|
+
if (o > 11184810) throw RangeError("A bed mesh has at most 11,184,810 vertices; use fewer cells or tiles");
|
|
10
|
+
return o;
|
|
11
|
+
}, n = class {
|
|
12
|
+
constructor(e) {
|
|
13
|
+
let n = t(e);
|
|
14
|
+
if (e.elevations.length !== n) throw RangeError(`A bed of ${e.columns} by ${e.rows} cells has ${n} elevations, got ${e.elevations.length}`);
|
|
15
|
+
this.shape = {
|
|
16
|
+
columns: e.columns,
|
|
17
|
+
rows: e.rows,
|
|
18
|
+
width: e.width,
|
|
19
|
+
depth: e.depth,
|
|
20
|
+
centre: [...e.centre]
|
|
21
|
+
};
|
|
22
|
+
let { columns: r, rows: i, width: a, depth: o, centre: s } = this.shape;
|
|
23
|
+
this.positions = new Float32Array(n * 3), this.normals = new Float32Array(n * 3), this.elevations = new Float64Array(n);
|
|
24
|
+
for (let t = 0; t <= r; t++) for (let n = 0; n <= i; n++) {
|
|
25
|
+
let c = n + (i + 1) * t, l = e.elevations[c];
|
|
26
|
+
if (!Number.isFinite(l)) throw RangeError("A bed mesh needs finite elevations");
|
|
27
|
+
this.elevations[c] = l, this.positions[3 * c] = s[0] - a / 2 + a * t / r, this.positions[3 * c + 1] = l, this.positions[3 * c + 2] = s[1] - o / 2 + o * n / i;
|
|
28
|
+
}
|
|
29
|
+
this.indices = new Uint32Array(r * i * 6);
|
|
30
|
+
let c = 0;
|
|
31
|
+
for (let e = 0; e < r; e++) for (let t = 0; t < i; t++) {
|
|
32
|
+
let n = (e, t) => e + (i + 1) * t;
|
|
33
|
+
this.indices.set([
|
|
34
|
+
n(t, e),
|
|
35
|
+
n(t + 1, e),
|
|
36
|
+
n(t, e + 1),
|
|
37
|
+
n(t + 1, e + 1),
|
|
38
|
+
n(t, e + 1),
|
|
39
|
+
n(t + 1, e)
|
|
40
|
+
], c), c += 6;
|
|
41
|
+
}
|
|
42
|
+
for (let e = 0; e < n; e++) this.normalAt(e);
|
|
43
|
+
}
|
|
44
|
+
geometry() {
|
|
45
|
+
return this.cached ??= new e(this.positions, this.normals, this.indices);
|
|
46
|
+
}
|
|
47
|
+
update(e) {
|
|
48
|
+
let t = this.shape;
|
|
49
|
+
if (e.columns !== t.columns || e.rows !== t.rows || e.width !== t.width || e.depth !== t.depth || e.centre[0] !== t.centre[0] || e.centre[1] !== t.centre[1]) throw RangeError("BedMesh.update needs the bed it was built from; build a new BedMesh for another grid");
|
|
50
|
+
let n = t.rows + 1, r = e.elevations, i = /* @__PURE__ */ new Set(), a = 0;
|
|
51
|
+
for (let e = 0; e < this.elevations.length; e++) {
|
|
52
|
+
let o = r[e];
|
|
53
|
+
if (o === this.elevations[e]) continue;
|
|
54
|
+
if (!Number.isFinite(o)) throw RangeError("A bed mesh needs finite elevations");
|
|
55
|
+
this.elevations[e] = o, this.positions[3 * e + 1] = o, a++;
|
|
56
|
+
let s = e % n, c = (e - s) / n;
|
|
57
|
+
for (let e = -1; e <= 1; e++) for (let r = -1; r <= 1; r++) {
|
|
58
|
+
let a = s + r, o = c + e;
|
|
59
|
+
a >= 0 && a < n && o >= 0 && o <= t.columns && i.add(a + n * o);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (let e of i) this.normalAt(e);
|
|
63
|
+
return a && (this.cached = void 0), {
|
|
64
|
+
changed: a,
|
|
65
|
+
rewritten: i.size
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
normalAt(e) {
|
|
69
|
+
let { columns: t, rows: n } = this.shape, r = n + 1, i = e % r, a = (e - i) / r, o = this.positions, s = (e, t) => e + r * t, c = [
|
|
70
|
+
0,
|
|
71
|
+
0,
|
|
72
|
+
0
|
|
73
|
+
], l = (e, t, n) => {
|
|
74
|
+
let r = o[3 * t] - o[3 * e], i = o[3 * t + 1] - o[3 * e + 1], a = o[3 * t + 2] - o[3 * e + 2], s = o[3 * n] - o[3 * e], l = o[3 * n + 1] - o[3 * e + 1], u = o[3 * n + 2] - o[3 * e + 2];
|
|
75
|
+
c[0] += i * u - a * l, c[1] += a * s - r * u, c[2] += r * l - i * s;
|
|
76
|
+
};
|
|
77
|
+
for (let r = a - 1; r <= a; r++) for (let a = i - 1; a <= i; a++) {
|
|
78
|
+
if (r < 0 || a < 0 || r >= t || a >= n) continue;
|
|
79
|
+
let i = [
|
|
80
|
+
s(a, r),
|
|
81
|
+
s(a + 1, r),
|
|
82
|
+
s(a, r + 1)
|
|
83
|
+
], o = [
|
|
84
|
+
s(a + 1, r + 1),
|
|
85
|
+
s(a, r + 1),
|
|
86
|
+
s(a + 1, r)
|
|
87
|
+
];
|
|
88
|
+
i.includes(e) && l(i[0], i[1], i[2]), o.includes(e) && l(o[0], o[1], o[2]);
|
|
89
|
+
}
|
|
90
|
+
let u = Math.hypot(c[0], c[1], c[2]) || 1, d = c[1] < 0 ? -1 : 1;
|
|
91
|
+
this.normals[3 * e] = d * c[0] / u, this.normals[3 * e + 1] = d * c[1] / u, this.normals[3 * e + 2] = d * c[2] / u;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
//#endregion
|
|
95
|
+
export { n as BedMesh };
|
package/dist/blend.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel, Mesh } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import type { Terrain } from './index.js';
|
|
5
|
+
import type { TerrainVirtualTexture } from './rvt.js';
|
|
6
|
+
/** How a mesh fades into the ground under it. Invalid values throw RangeError. */
|
|
7
|
+
export interface TerrainBlendOptions {
|
|
8
|
+
/** Height above the ground, in world units, over which the mesh fades into the ground, above 0. Default 0.5. */
|
|
9
|
+
height?: number;
|
|
10
|
+
/**
|
|
11
|
+
* How much value noise moves the fade's top and bottom, as a share of `height`, 0 to 1: at 1 the line wanders by up
|
|
12
|
+
* to half of `height` up and down, so it never reads as a ruled line. Default 0.5.
|
|
13
|
+
*/
|
|
14
|
+
noise?: number;
|
|
15
|
+
/** How far the mesh's shading normal turns to the ground's where it is faded, 0 to 1. Default 1. */
|
|
16
|
+
normal?: number;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Mesh to terrain blending: where a mesh on core's standard material (a rock, a cliff, a wall) meets the terrain, its
|
|
20
|
+
* lowest `height` fades into the ground under it, colour from the terrain's virtual texture, shading normal from the
|
|
21
|
+
* heightmap, roughness the terrain's, so no hard seam line shows where the two surfaces cross.
|
|
22
|
+
*/
|
|
23
|
+
export declare class TerrainBlend implements EnginePlugin {
|
|
24
|
+
readonly terrain: Terrain;
|
|
25
|
+
readonly virtualTexture: TerrainVirtualTexture;
|
|
26
|
+
readonly name: string;
|
|
27
|
+
private engine?;
|
|
28
|
+
private disposed;
|
|
29
|
+
private readonly defaults;
|
|
30
|
+
private readonly rows;
|
|
31
|
+
private dirty;
|
|
32
|
+
private buffer?;
|
|
33
|
+
private capacity;
|
|
34
|
+
private source;
|
|
35
|
+
private shader;
|
|
36
|
+
private binding;
|
|
37
|
+
private heights?;
|
|
38
|
+
private cache?;
|
|
39
|
+
private entries;
|
|
40
|
+
constructor(terrain: Terrain, virtualTexture: TerrainVirtualTexture, options?: TerrainBlendOptions);
|
|
41
|
+
/** Blends meshes into `terrain`, reading the ground's colour from `virtualTexture` (attach both first). */
|
|
42
|
+
static attach(engine: EngineKernel, terrain: Terrain, virtualTexture: TerrainVirtualTexture, options?: TerrainBlendOptions): TerrainBlend;
|
|
43
|
+
setup(engine: Engine): void;
|
|
44
|
+
/**
|
|
45
|
+
* Fades `mesh` into the ground (its own `options` over the attachment's). Adding it again changes its settings.
|
|
46
|
+
* The mesh must belong to this engine and draw with core's standard material.
|
|
47
|
+
*/
|
|
48
|
+
add(mesh: Mesh, options?: TerrainBlendOptions): this;
|
|
49
|
+
/** Stops blending `mesh`; false when it was not blended. */
|
|
50
|
+
remove(mesh: Mesh): boolean;
|
|
51
|
+
/** Whether `mesh` is blended. */
|
|
52
|
+
has(mesh: Mesh): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* The WGSL added to core's shader. Per pixel of a blended mesh: the ground height under it, bilinear in the
|
|
55
|
+
* heightmap as the terrain's vertex shader reads it; how far above it the pixel is, moved by value noise; the fade
|
|
56
|
+
* `1 - smoothstep(0, height, above)`, times the virtual texture's coverage. The colour is the virtual texture's at the
|
|
57
|
+
* ground point under the pixel, read at the pixel's own footprint (its world width from the position's screen
|
|
58
|
+
* derivatives, taken once at the top of the block where control flow is uniform); the normal is the heightmap's
|
|
59
|
+
* central differences, as the terrain's vertex normal.
|
|
60
|
+
*/
|
|
61
|
+
private code;
|
|
62
|
+
/** Core's standard shader with the blend: the declarations above it, the per-mesh fade at the top of the block. */
|
|
63
|
+
private patch;
|
|
64
|
+
/** Writes the per-mesh rows (height, noise, normal, 0 by mesh id; height 0 is not blended), growing the buffer. */
|
|
65
|
+
private upload;
|
|
66
|
+
registerPasses(graph: FrameGraph): void;
|
|
67
|
+
onDeviceRestored(engine: Engine): void;
|
|
68
|
+
dispose(): void;
|
|
69
|
+
[Symbol.dispose]: () => void;
|
|
70
|
+
}
|
package/dist/blend.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const n=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { n as e, t } from "./mesh-patch-BThCHCeS.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime, meshRuntime as r } from "@margoengine/core/extension";
|
|
6
|
+
import { MargoError as i } from "@margoengine/math/errors";
|
|
7
|
+
//#region src/blend.ts
|
|
8
|
+
var a = {
|
|
9
|
+
height: .5,
|
|
10
|
+
noise: .5,
|
|
11
|
+
normal: 1
|
|
12
|
+
}, o = 0, s = (e) => {
|
|
13
|
+
let t = String(Math.fround(e));
|
|
14
|
+
return /[.e]/.test(t) ? t : t + ".0";
|
|
15
|
+
};
|
|
16
|
+
function c(e, t) {
|
|
17
|
+
if (e !== void 0 && (typeof e != "object" || !e)) throw RangeError("Terrain blend options must be an object");
|
|
18
|
+
let n = e?.height ?? t.height, r = e?.noise ?? t.noise, i = e?.normal ?? t.normal;
|
|
19
|
+
if (typeof n != "number" || !(n > 0) || !Number.isFinite(Math.fround(n)) || typeof r != "number" || !(r >= 0 && r <= 1) || typeof i != "number" || !(i >= 0 && i <= 1)) throw RangeError("Invalid terrain blend options; height must be finite and above 0, noise and normal between 0 and 1.");
|
|
20
|
+
return {
|
|
21
|
+
height: n,
|
|
22
|
+
noise: r,
|
|
23
|
+
normal: i
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
var l = class l {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
27
|
+
constructor(e, t, n = {}) {
|
|
28
|
+
if (this.terrain = e, this.virtualTexture = t, this.name = "terrain:blend:" + ++o, this.disposed = !1, this.rows = /* @__PURE__ */ new Map(), this.dirty = !0, this.capacity = 0, this.source = "", this.shader = "", this.binding = 40, this.entries = [], !e || typeof e.options != "object" || !t || typeof t.entries != "function") throw TypeError("TerrainBlend needs a Terrain and its TerrainVirtualTexture");
|
|
29
|
+
this.defaults = c(n, a);
|
|
30
|
+
}
|
|
31
|
+
static attach(e, t, r, i = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
32
|
+
let a = new l(t, r, i);
|
|
33
|
+
return n(e).use(a), a;
|
|
34
|
+
}
|
|
35
|
+
setup(e) {
|
|
36
|
+
if (this.disposed || !e.gpu) throw new i(this.disposed ? "disposed" : "unsupported", "Terrain blending requires WebGPU and a live module; attach a new TerrainBlend to a WebGPU engine; see https://margoengine.com/errors#webgpu");
|
|
37
|
+
if (this.virtualTexture.owner !== e || this.virtualTexture.disposed) throw new i("attached", "Terrain blending reads a terrain virtual texture attached to the same engine; attach TerrainVirtualTexture first");
|
|
38
|
+
this.engine = e;
|
|
39
|
+
}
|
|
40
|
+
add(e, t) {
|
|
41
|
+
if (this.disposed) throw new i("disposed", "This TerrainBlend is disposed; attach a new one");
|
|
42
|
+
let n = r(e);
|
|
43
|
+
if (n.check(), this.engine && n.engine !== this.engine) throw new i("attached", "The mesh belongs to another engine; blend meshes of the terrain's engine");
|
|
44
|
+
return this.rows.set(e, {
|
|
45
|
+
id: n.id,
|
|
46
|
+
...c(t, this.defaults)
|
|
47
|
+
}), this.dirty = !0, this;
|
|
48
|
+
}
|
|
49
|
+
remove(e) {
|
|
50
|
+
let t = this.rows.delete(e);
|
|
51
|
+
return t && (this.dirty = !0), t;
|
|
52
|
+
}
|
|
53
|
+
has(e) {
|
|
54
|
+
return this.rows.has(e);
|
|
55
|
+
}
|
|
56
|
+
code(e) {
|
|
57
|
+
let t = this.terrain.options, n = !!t.floatingOrigin, r = n ? "terrainVirtual.map.x" : s(t.origin?.[0] ?? 0), i = n ? "terrainVirtual.map.y" : s(t.origin?.[1] ?? 0), a = s(t.size[0]), o = s(t.size[1]), c = s(t.heightScale ?? 100), l = n ? "terrainVirtual.map.z" : s(t.heightOffset ?? 0);
|
|
58
|
+
return `
|
|
59
|
+
@group(0) @binding(${e}) var terrainBlendHeights:texture_2d<f32>;
|
|
60
|
+
@group(0) @binding(${e + 1}) var<storage,read> terrainBlendMeshes:array<vec4f>;
|
|
61
|
+
${this.virtualTexture.declarations(0, e + 2)}
|
|
62
|
+
struct TerrainBlendGround {color:vec3f,normal:vec3f,weight:f32}
|
|
63
|
+
fn terrainBlendHeight(position:vec2f)->f32 {
|
|
64
|
+
let dimensions=vec2i(textureDimensions(terrainBlendHeights));let uv=clamp((position-vec2f(${r},${i}))/vec2f(${a},${o}),vec2f(0),vec2f(1));let pixel=uv*vec2f(dimensions-vec2i(1));let lo=vec2i(floor(pixel));let hi=min(lo+vec2i(1),dimensions-vec2i(1));let t=fract(pixel);
|
|
65
|
+
let a=mix(textureLoad(terrainBlendHeights,lo,0).r,textureLoad(terrainBlendHeights,vec2i(hi.x,lo.y),0).r,t.x);let b=mix(textureLoad(terrainBlendHeights,vec2i(lo.x,hi.y),0).r,textureLoad(terrainBlendHeights,hi,0).r,t.x);
|
|
66
|
+
return mix(a,b,t.y)*${c}+${l};
|
|
67
|
+
}
|
|
68
|
+
fn terrainBlendSettings(id:f32)->vec4f {
|
|
69
|
+
let k=u32(max(id-1.0,0.0));if(k>=arrayLength(&terrainBlendMeshes)){return vec4f(0);}return terrainBlendMeshes[k];
|
|
70
|
+
}
|
|
71
|
+
fn terrainBlendGround(p:vec3f,s:vec4f,footprint:f32)->TerrainBlendGround {
|
|
72
|
+
var g:TerrainBlendGround;g.weight=0.0;
|
|
73
|
+
if(any(p.xz<vec2f(${r},${i}))||any(p.xz>vec2f(${r},${i})+vec2f(${a},${o}))){return g;}
|
|
74
|
+
let h=terrainBlendHeight(p.xz);
|
|
75
|
+
let jitter=(terrainNoise(${n ? "(p.xz+terrainVirtual.anchor.xy)" : "p.xz"}*(2.0/s.x)).x-0.5)*s.y*s.x;
|
|
76
|
+
let fade=1.0-smoothstep(0.0,s.x,p.y-h+jitter);
|
|
77
|
+
if(fade<=0.0){return g;}
|
|
78
|
+
let ground=terrainVirtualColor(vec3f(p.x,h,p.z),footprint);
|
|
79
|
+
let step=vec2f(${a},${o})/max(vec2f(textureDimensions(terrainBlendHeights))-1.0,vec2f(1));
|
|
80
|
+
g.normal=normalize(vec3f((terrainBlendHeight(p.xz-vec2f(step.x,0))-terrainBlendHeight(p.xz+vec2f(step.x,0)))/(2*step.x),1,(terrainBlendHeight(p.xz-vec2f(0,step.y))-terrainBlendHeight(p.xz+vec2f(0,step.y)))/(2*step.y)));
|
|
81
|
+
g.color=ground.rgb;g.weight=fade*ground.a;return g;
|
|
82
|
+
}
|
|
83
|
+
`;
|
|
84
|
+
}
|
|
85
|
+
patch(n, r) {
|
|
86
|
+
let i = e(n);
|
|
87
|
+
if (!i) return n;
|
|
88
|
+
let { code: a, at: o } = i, c = s(this.terrain.options.roughness ?? .9);
|
|
89
|
+
return (a.includes("fn terrainNoise(") ? "" : t) + this.code(r) + a.slice(0, o) + `let terrainBlendFootprint=max(length(dpdx(i.position)),length(dpdy(i.position)));{let s=terrainBlendSettings(i.terrainId);if(s.x>0.0){let g=terrainBlendGround(i.position,s,terrainBlendFootprint);if(g.weight>0.0){i.color=mix(i.color,g.color,g.weight);i.material.x=mix(i.material.x,${c},g.weight);i.material.y*=1.0-g.weight;n=normalize(mix(n,g.normal,g.weight*s.z));}}}` + a.slice(o);
|
|
90
|
+
}
|
|
91
|
+
upload(e) {
|
|
92
|
+
let t = 0;
|
|
93
|
+
for (let e of this.rows.values()) t = Math.max(t, e.id + 1);
|
|
94
|
+
(!this.buffer || t > this.capacity) && (this.buffer?.destroy(), this.capacity = Math.max(16, 2 ** Math.ceil(Math.log2(Math.max(t, 1)))), this.buffer = e.createBuffer({
|
|
95
|
+
size: this.capacity * 16,
|
|
96
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST
|
|
97
|
+
}), this.cache = void 0);
|
|
98
|
+
let n = new Float32Array(this.capacity * 4);
|
|
99
|
+
for (let e of this.rows.values()) n.set([
|
|
100
|
+
Math.fround(e.height),
|
|
101
|
+
e.noise,
|
|
102
|
+
e.normal,
|
|
103
|
+
0
|
|
104
|
+
], e.id * 4);
|
|
105
|
+
e.queue.writeBuffer(this.buffer, 0, n), this.dirty = !1;
|
|
106
|
+
}
|
|
107
|
+
registerPasses(e) {
|
|
108
|
+
e.add({
|
|
109
|
+
name: this.name,
|
|
110
|
+
order: -1,
|
|
111
|
+
execute: (e) => {
|
|
112
|
+
if (e.backend !== "webgpu" || !this.engine) return;
|
|
113
|
+
let t = this.terrain.heightmap, n = this.virtualTexture.texture;
|
|
114
|
+
if (t && n && !this.virtualTexture.disposed && this.rows.size !== 0) {
|
|
115
|
+
for (let e of this.rows.keys()) r(e).engine !== this.engine && (this.rows.delete(e), this.dirty = !0);
|
|
116
|
+
if (this.dirty && this.upload(e.device), this.source !== e.shader) {
|
|
117
|
+
this.source = e.shader;
|
|
118
|
+
let t = new Set(e.bindings.map((e) => e.binding));
|
|
119
|
+
for (let n of e.shader.matchAll(/@group\(0\) @binding\((\d+)\)/g)) t.add(Number(n[1]));
|
|
120
|
+
let n = 40;
|
|
121
|
+
for (; [
|
|
122
|
+
0,
|
|
123
|
+
1,
|
|
124
|
+
2,
|
|
125
|
+
3,
|
|
126
|
+
4
|
|
127
|
+
].some((e) => t.has(n + e));) n++;
|
|
128
|
+
this.binding = n, this.cache = void 0, this.shader = this.patch(e.shader, n);
|
|
129
|
+
}
|
|
130
|
+
this.shader !== this.source && ((this.heights !== t || this.cache !== n || this.entries[0]?.binding !== this.binding) && (this.heights = t, this.cache = n, this.entries = [
|
|
131
|
+
{
|
|
132
|
+
binding: this.binding,
|
|
133
|
+
resource: t.createView()
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
binding: this.binding + 1,
|
|
137
|
+
resource: { buffer: this.buffer }
|
|
138
|
+
},
|
|
139
|
+
...this.virtualTexture.entries(this.binding + 2)
|
|
140
|
+
]), e.bindings.push(...this.entries), e.shader = this.shader);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
onDeviceRestored(e) {
|
|
146
|
+
this.buffer?.destroy(), this.buffer = this.heights = this.cache = void 0, this.capacity = 0, this.entries = [], this.dirty = !0, this.engine = void 0, this.setup(e);
|
|
147
|
+
}
|
|
148
|
+
dispose() {
|
|
149
|
+
if (this.disposed) return;
|
|
150
|
+
this.disposed = !0, this.buffer?.destroy(), this.buffer = this.heights = this.cache = void 0, this.entries = [], this.rows.clear();
|
|
151
|
+
let e = this.engine;
|
|
152
|
+
this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
|
|
153
|
+
}
|
|
154
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
155
|
+
this.dispose();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
//#endregion
|
|
159
|
+
export { l as TerrainBlend };
|
package/dist/colors.d.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { EngineKernel, Mesh } from '@margoengine/core';
|
|
2
|
+
import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
|
|
3
|
+
import type { Vec3 } from '@margoengine/math';
|
|
4
|
+
type Range = readonly [number, number];
|
|
5
|
+
/**
|
|
6
|
+
* Height/slope material bands for {@link TerrainColors}. Colors are linear RGB in 0..1; ranges increase. Every option
|
|
7
|
+
* has a default, so `TerrainColors.attach(engine, mesh)` gives sand at sea level (world Y 0, as `@margoengine/water`),
|
|
8
|
+
* grass over the rest and rock on slopes steeper than about 35 degrees.
|
|
9
|
+
*/
|
|
10
|
+
export interface TerrainColorOptions {
|
|
11
|
+
/** Beach color below `shore`. Default [0.5, 0.42, 0.29], dry sand (albedo about 0.4). */
|
|
12
|
+
sand?: Vec3;
|
|
13
|
+
/** Low and high meadow colors, blended over `meadowHeight`. Default [0.133, 0.263, 0.033] and [0.274, 0.342, 0.064]. */
|
|
14
|
+
meadow?: readonly [Vec3, Vec3];
|
|
15
|
+
/** Rock color above `stoneHeight` and on steep slopes. Default [0.253, 0.223, 0.187], grey rock. */
|
|
16
|
+
stone?: Vec3;
|
|
17
|
+
/** World heights over which sand gives way to meadow and stone. Default [0.3, 1.5]: beaches up to 1.5 m above sea level. */
|
|
18
|
+
shore?: Range;
|
|
19
|
+
/** World heights over which the low meadow color gives way to the high one. Default: the mesh's lowest to highest point. */
|
|
20
|
+
meadowHeight?: Range;
|
|
21
|
+
/**
|
|
22
|
+
* World heights over which meadow gives way to stone. Default: above the mesh's highest point, so only slopes are
|
|
23
|
+
* stone; pass a range for rocky peaks.
|
|
24
|
+
*/
|
|
25
|
+
stoneHeight?: Range;
|
|
26
|
+
/** Surface normal Y (0 vertical, 1 flat) over which steep stone gives way to meadow. Default [0.75, 0.85]: 32 to 41 degrees. */
|
|
27
|
+
stoneNormal?: Range;
|
|
28
|
+
/** World heights over which the wet band dries. Default [-0.1, 0.4], just above sea level. */
|
|
29
|
+
wetHeight?: Range;
|
|
30
|
+
/** Albedo multiplier of fully wet ground, 0..1. Wet ground is also up to 40% smoother. Default 0.6. */
|
|
31
|
+
wetDarkening?: number;
|
|
32
|
+
/**
|
|
33
|
+
* With `wetness`: seconds over which sand the water has left dries to 1/e of its moisture, 1 to 604,800. Default 900
|
|
34
|
+
* (15 minutes): the band behind the swash stays dark for minutes and pales over the next hour.
|
|
35
|
+
*/
|
|
36
|
+
wetDryTime?: number;
|
|
37
|
+
/**
|
|
38
|
+
* With `wetness`: seconds over which the glossy water film on sand the water has left drains to 1/e, 0.1 to 3,600.
|
|
39
|
+
* Default 20: the sheen behind the backwash lasts tens of seconds; the sand stays dark after it goes.
|
|
40
|
+
*/
|
|
41
|
+
wetGlossTime?: number;
|
|
42
|
+
/** Brightness variation, 0..1 (default 0): two scales of smooth noise darken colors by up to twice this. */
|
|
43
|
+
variation?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Per-pixel material detail, 0..1 (default 1). Noise moves each band border by up to 30% of its range; sand, meadow
|
|
46
|
+
* and stone blend by procedural height maps, so sand settles between grass tufts and grass between rock bumps; steep
|
|
47
|
+
* stone gets triplanar strata with a small normal perturbation. Fine detail fades out between 20 and 60 m from the
|
|
48
|
+
* camera. 0 removes all of it from the shader.
|
|
49
|
+
*/
|
|
50
|
+
detail?: number;
|
|
51
|
+
/**
|
|
52
|
+
* Moving wet band from a borrowed water-depth texture (red channel, meters) over world XZ `origin`/`size`. Terrain
|
|
53
|
+
* keeps a moisture record over that grid, so sand the swash has left stays wet and dries over `wetDryTime`.
|
|
54
|
+
*/
|
|
55
|
+
wetness?: {
|
|
56
|
+
texture: () => GPUTexture | undefined;
|
|
57
|
+
origin: Range;
|
|
58
|
+
size: Range;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Wind-built sand surface on the sand band, on by default with `detail` above 0 (`false` removes it): impact ripples
|
|
62
|
+
* across the wind, streaks along it, and sorting by slope (lee slip faces paler and smooth, windward faces darker
|
|
63
|
+
* and rippled), so dune faces read at a distance. See {@link SandSurface}.
|
|
64
|
+
*/
|
|
65
|
+
sandSurface?: SandSurface | false;
|
|
66
|
+
}
|
|
67
|
+
/** Wind-built sand detail of {@link TerrainColorOptions.sandSurface}. Every field has a default. */
|
|
68
|
+
export interface SandSurface {
|
|
69
|
+
/** Direction the wind blows toward, world `[x, z]`, any length above 0. Default [1, 0] (toward +x). */
|
|
70
|
+
wind?: readonly [number, number];
|
|
71
|
+
/** Ripple spacing, metres, 0.02 to 1. Default 0.1 (impact ripples of fine to medium sand are 5 to 20 cm apart). */
|
|
72
|
+
ripple?: number;
|
|
73
|
+
/** Ripple relief, 0 (flat) to 2. Default 1: height 1/15 of the spacing, lee 19 and stoss 5.5 degrees. */
|
|
74
|
+
ripples?: number;
|
|
75
|
+
/** Streak contrast along the wind, 0 to 1. Default 1: albedo varies by up to 8% either way. */
|
|
76
|
+
streaks?: number;
|
|
77
|
+
/** Sorting by slope, 0 to 1. Default 1: lee slip faces up to 10% paler, windward faces up to 7% darker. */
|
|
78
|
+
sorting?: number;
|
|
79
|
+
/**
|
|
80
|
+
* Broad scour bands along the wind, 0 to 1. Default 0 (none). They are 8 to 15 m wide and about 80 m long, vary the
|
|
81
|
+
* albedo by up to 15% either way at 1 and never fade with distance (they are wider than a pixel at any range), so a
|
|
82
|
+
* dune face hundreds of metres away still shows the wind's work after the ripples and fine streaks clamp out.
|
|
83
|
+
*/
|
|
84
|
+
broad?: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Dry sand albedo, linear RGB: [0.5, 0.42, 0.29], luminance 0.43, inside the measured 0.35 to 0.45 of dry sand (T. R.
|
|
88
|
+
* Oke, "Boundary Layer Climates", 2nd ed., 1987, table 1.1). The {@link TerrainColorOptions.sand} default; a scene of
|
|
89
|
+
* dry sand takes it rather than a brighter guess (KB-SKY-AGXSAND1).
|
|
90
|
+
*/
|
|
91
|
+
export declare const DRY_SAND: Readonly<Vec3>;
|
|
92
|
+
/** The look fields {@link TerrainColors.setLook} changes while the scene runs, with no shader rebuild. */
|
|
93
|
+
export type TerrainLook = Pick<TerrainColorOptions, 'sand' | 'meadow' | 'stone' | 'shore' | 'meadowHeight' | 'stoneHeight' | 'stoneNormal' | 'wetHeight' | 'wetDarkening' | 'wetDryTime' | 'wetGlossTime'> & {
|
|
94
|
+
sandSurface?: SandSurface;
|
|
95
|
+
};
|
|
96
|
+
/** Where {@link TerrainColors.setLook} takes `null` to mean the default. */
|
|
97
|
+
export type TerrainLookUpdate = {
|
|
98
|
+
[K in keyof TerrainLook]?: TerrainLook[K] | null;
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* What the wet-sand record of {@link TerrainColors} holds `seconds` after the water left fully wet sand: the moisture
|
|
102
|
+
* and surface film as shares of fully wet, and the share of the full `wetDarkening` the sand still shows.
|
|
103
|
+
*/
|
|
104
|
+
export declare function wetSandAfter(seconds: number, dryTime?: number, glossTime?: number): {
|
|
105
|
+
moisture: number;
|
|
106
|
+
film: number;
|
|
107
|
+
darkening: number;
|
|
108
|
+
};
|
|
109
|
+
/** Height/slope colors on one ordinary mesh, shaded per pixel inside the scene's PBR and shadow shader. */
|
|
110
|
+
export declare class TerrainColors implements EnginePlugin {
|
|
111
|
+
private readonly mesh;
|
|
112
|
+
readonly name: string;
|
|
113
|
+
private engine?;
|
|
114
|
+
private source;
|
|
115
|
+
private shader;
|
|
116
|
+
private disposed;
|
|
117
|
+
private readonly surfaceCode;
|
|
118
|
+
private readonly wetCode;
|
|
119
|
+
private readonly wetness?;
|
|
120
|
+
private dry?;
|
|
121
|
+
private dryEntry?;
|
|
122
|
+
private wet?;
|
|
123
|
+
private wetEntry?;
|
|
124
|
+
private wetBinding;
|
|
125
|
+
private lookBinding;
|
|
126
|
+
private given;
|
|
127
|
+
private look;
|
|
128
|
+
private lookBuffer?;
|
|
129
|
+
private lookEntry?;
|
|
130
|
+
private lookDirty;
|
|
131
|
+
private readonly sandOn;
|
|
132
|
+
private readonly wetSource;
|
|
133
|
+
private readonly wetTarget;
|
|
134
|
+
private readonly wetSize;
|
|
135
|
+
private record?;
|
|
136
|
+
private recordNext?;
|
|
137
|
+
private recordGroup?;
|
|
138
|
+
private recordPipeline?;
|
|
139
|
+
private dryingBuffer?;
|
|
140
|
+
private readonly drying;
|
|
141
|
+
private readonly recordSource;
|
|
142
|
+
private readonly recordTarget;
|
|
143
|
+
private elapsed;
|
|
144
|
+
private dryTime;
|
|
145
|
+
private glossTime;
|
|
146
|
+
constructor(mesh: Mesh, given?: TerrainColorOptions);
|
|
147
|
+
static attach(engine: EngineKernel, mesh: Mesh, options?: TerrainColorOptions): TerrainColors;
|
|
148
|
+
setup(engine: Engine): void;
|
|
149
|
+
/** Whether the sand surface was compiled in (not `sandSurface: false`, and `detail` above 0). */
|
|
150
|
+
get sandSurfaceOn(): boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Changes the look while the scene runs: the colours, height and slope ranges, wet darkening and the sand surface's
|
|
153
|
+
* wind, ripples, streaks, sorting and broad bands. A field left out keeps its value. Nothing is rebuilt: the next frame
|
|
154
|
+
* writes one small uniform, so any of it can be keyed over a shot. `detail`, `variation`, `wetness` and whether
|
|
155
|
+
* `sandSurface` is on were compiled in at construction and stay.
|
|
156
|
+
* @throws TypeError for `sandSurface` fields when the sand surface was built off; RangeError for a value out of range.
|
|
157
|
+
*/
|
|
158
|
+
setLook(look: TerrainLookUpdate): void;
|
|
159
|
+
/** Adds the mesh-ID varying once, then this mesh's surface call inside the shared per-pixel block. */
|
|
160
|
+
private patch;
|
|
161
|
+
registerPasses(graph: FrameGraph): void;
|
|
162
|
+
/** Adds the frame's seconds to the wet-sand record's next update. */
|
|
163
|
+
onPreUpdate(delta: number): void;
|
|
164
|
+
/** Frees the water snapshot and the wet-sand record (a new size, a lost device or disposal). */
|
|
165
|
+
private dropRecord;
|
|
166
|
+
onDeviceRestored(engine: Engine): void;
|
|
167
|
+
dispose(): void;
|
|
168
|
+
[Symbol.dispose]: () => void;
|
|
169
|
+
}
|
|
170
|
+
export {};
|
package/dist/deform.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel, Mesh } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin, type FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
import type { Terrain, TerrainLighting } from './index.js';
|
|
5
|
+
/** Settings of {@link TerrainDeformation.attach}, fixed at attachment. Invalid values throw RangeError. */
|
|
6
|
+
export interface TerrainDeformationOptions {
|
|
7
|
+
/** Texels along each side of the deformation map, 64 to 2,048. Default 1,024. */
|
|
8
|
+
size?: number;
|
|
9
|
+
/** World width of a texel, above 0. Default 0.025 (2.5 cm): 1,024 texels cover 25.6 m around the camera. */
|
|
10
|
+
texel?: number;
|
|
11
|
+
/** Deepest a print may go, world units above 0 (and the depth a print takes by default). Default 0.15. */
|
|
12
|
+
depth?: number;
|
|
13
|
+
/** Height of the rim pushed up around a print, as a share of its depth, 0 to 1. Default 0.25. */
|
|
14
|
+
rim?: number;
|
|
15
|
+
/** Seconds a print of the full `depth` takes to fill back in (shallower ones sooner); 0 keeps prints. Default 120. */
|
|
16
|
+
fade?: number;
|
|
17
|
+
/** How deep the ground takes prints, 0 (rock) to 1. Default 1. With painted weights, `layers` overrides it. */
|
|
18
|
+
softness?: number;
|
|
19
|
+
/** With painted `TerrainTextures.weights`: each layer's softness, 0 to 1, in layer order (missing: `softness`). */
|
|
20
|
+
layers?: readonly number[];
|
|
21
|
+
/** Darkening of the ground pressed down, 0 to 1 (packed snow, wet mud). Default 0.25. */
|
|
22
|
+
darken?: number;
|
|
23
|
+
/** World distance from the camera within which prints show, fading over the last quarter, above 0. Default 40. */
|
|
24
|
+
distance?: number;
|
|
25
|
+
/** Most view-ray steps a pixel takes through the prints, 4 to 32. Default 12. */
|
|
26
|
+
steps?: number;
|
|
27
|
+
/**
|
|
28
|
+
* World XZ the map centres on each frame (a player, a vehicle), float64 world coordinates also with the terrain's
|
|
29
|
+
* `floatingOrigin`. Default the camera's position.
|
|
30
|
+
*/
|
|
31
|
+
follow?: () => readonly [number, number];
|
|
32
|
+
/**
|
|
33
|
+
* Keeps every print for good: one the window leaves is pressed again when the window comes back to it, so prints
|
|
34
|
+
* laid once stay under a moving camera. Needs `fade` 0 (its default with `persist`). At most 16,384 prints and track
|
|
35
|
+
* pieces. Default false.
|
|
36
|
+
*/
|
|
37
|
+
persist?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** Settings of {@link TerrainDeformation.walk}. Lengths are world units. */
|
|
40
|
+
export interface TerrainWalk {
|
|
41
|
+
/** Distance along the path from one footfall to the next, above 0. Default 0.75. */
|
|
42
|
+
step?: number;
|
|
43
|
+
/** Distance between the left and the right feet's lines, 0 or more. Default 0.08. */
|
|
44
|
+
straddle?: number;
|
|
45
|
+
/** How far each foot turns outward, radians, -1.5 to 1.5. Default 0.12 (7 degrees). */
|
|
46
|
+
toeOut?: number;
|
|
47
|
+
/** Length of a foot, above 0. Default 0.28. */
|
|
48
|
+
length?: number;
|
|
49
|
+
/** Width of a foot, above 0. Default 0.11. */
|
|
50
|
+
width?: number;
|
|
51
|
+
/** Depth, 0 to the deformation's `depth`. Default the deformation's `depth`. */
|
|
52
|
+
depth?: number;
|
|
53
|
+
/** How unevenly the walker steps (spacing, line, heading and depth), 0 to 1. Default 0.5. */
|
|
54
|
+
jitter?: number;
|
|
55
|
+
/** Seed of the unevenness, an integer. Default 1. */
|
|
56
|
+
seed?: number;
|
|
57
|
+
}
|
|
58
|
+
/** A footprint: an ellipse pressed at a point. */
|
|
59
|
+
export interface TerrainFootprint {
|
|
60
|
+
/** Heading in radians about +Y, 0 along +X. Default 0. */
|
|
61
|
+
angle?: number;
|
|
62
|
+
/** Length along the heading, world units above 0. Default 0.28. */
|
|
63
|
+
length?: number;
|
|
64
|
+
/** Width across the heading, world units above 0. Default 0.11. */
|
|
65
|
+
width?: number;
|
|
66
|
+
/** Depth, 0 to the deformation's `depth` (deeper is clamped). Default the deformation's `depth`. */
|
|
67
|
+
depth?: number;
|
|
68
|
+
}
|
|
69
|
+
/** A track: a band of `width` pressed along a line (a tyre, a sledge, a body dragged). */
|
|
70
|
+
export interface TerrainTrack {
|
|
71
|
+
/** Width of the band, world units above 0. Default 0.25. */
|
|
72
|
+
width?: number;
|
|
73
|
+
/** Depth, 0 to the deformation's `depth` (deeper is clamped). Default the deformation's `depth`. */
|
|
74
|
+
depth?: number;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Deformable ground: footprints, walking trails, tyre tracks and trails pressed into a terrain or a ground mesh, kept
|
|
78
|
+
* in a deformation map that follows the camera and filling back in over `fade` seconds (or kept, `persist`). Prints are
|
|
79
|
+
* shown by the ground's lit shader (parallax, the print's slope as normal, darkening); its geometry, shadow and SSR
|
|
80
|
+
* capture are unchanged. `TerrainDeformation.attach(engine, ground, options)`, after the ground's other effects; on a
|
|
81
|
+
* mesh drawn by core's standard shader (as `TerrainColors` draws one) the prints go over its colours. WebGPU only.
|
|
82
|
+
* @throws RangeError from the constructor when an option is out of range, and from a print with a bad size.
|
|
83
|
+
* @throws MargoError `'unsupported'` on a WebGL engine or a map larger than the device allows, `'disposed'` after
|
|
84
|
+
* `dispose()`.
|
|
85
|
+
*/
|
|
86
|
+
export declare class TerrainDeformation implements EnginePlugin, TerrainLighting {
|
|
87
|
+
#private;
|
|
88
|
+
readonly options: TerrainDeformationOptions;
|
|
89
|
+
readonly name: string;
|
|
90
|
+
disposed: boolean;
|
|
91
|
+
owner?: Engine;
|
|
92
|
+
/** Texels along each side of the map. */
|
|
93
|
+
readonly size: number;
|
|
94
|
+
/** World width of a texel. */
|
|
95
|
+
readonly texel: number;
|
|
96
|
+
/** Deepest a print goes. */
|
|
97
|
+
readonly depth: number;
|
|
98
|
+
constructor(ground: Terrain | Mesh, options?: TerrainDeformationOptions);
|
|
99
|
+
/** Attaches deformation to a terrain or a ground mesh, after its other effects. `dispose()` removes it. */
|
|
100
|
+
static attach(engine: EngineKernel, ground: Terrain | Mesh, options?: TerrainDeformationOptions): TerrainDeformation;
|
|
101
|
+
/** Presses a footprint centred at world (x, z). Returns this. */
|
|
102
|
+
footprint(x: number, z: number, print?: TerrainFootprint): this;
|
|
103
|
+
/** Presses a track of `width` from world (x, z) `from` to `to` (rounded ends). Returns this. */
|
|
104
|
+
track(from: readonly [number, number], to: readonly [number, number], track?: TerrainTrack): this;
|
|
105
|
+
/** Presses a track through each world (x, z) point in turn (a trail). Returns this. */
|
|
106
|
+
trail(points: readonly (readonly [number, number])[], track?: TerrainTrack): this;
|
|
107
|
+
/**
|
|
108
|
+
* Footprints of someone walking through each world (x, z) point in turn: left and right feet in turn, a heel and a
|
|
109
|
+
* forefoot each, toes turned out, unevenly spaced. Returns this.
|
|
110
|
+
* @throws RangeError for fewer than two points or a setting out of range.
|
|
111
|
+
*/
|
|
112
|
+
walk(points: readonly (readonly [number, number])[], walk?: TerrainWalk): this;
|
|
113
|
+
/** Smooths every print away at the next frame, kept ones too. Returns this. */
|
|
114
|
+
clear(): this;
|
|
115
|
+
/**
|
|
116
|
+
* The relief the terrain's shader reads, while attached: an `rgba16float` texture of `size` texels a side holding
|
|
117
|
+
* the ground's height change (negative in a print, positive on a rim), its x and z slopes and the pressed share (0 to
|
|
118
|
+
* 1), addressed toroidally (world texel (i, j) at texel (i mod size, j mod size)) and valid within `size / 2` texels
|
|
119
|
+
* of the centre. Usage `TEXTURE_BINDING | STORAGE_BINDING | COPY_SRC`, so other materials can sample it and tests
|
|
120
|
+
* can read it back. A new one after device restoration.
|
|
121
|
+
*/
|
|
122
|
+
get texture(): GPUTexture | undefined;
|
|
123
|
+
/** Prints waiting for the next frame. */
|
|
124
|
+
get pending(): number;
|
|
125
|
+
setup(engine: Engine): void;
|
|
126
|
+
registerPasses(graph: FrameGraph): void;
|
|
127
|
+
/** The group-1 entries the patched shader reads. */
|
|
128
|
+
bindings(): GPUBindGroupEntry[];
|
|
129
|
+
/** The terrain's lit shader shading each near fragment where its view ray meets the printed ground (bindings in `group`). */
|
|
130
|
+
shader(source: string, group: number): string;
|
|
131
|
+
onDeviceRestored(engine: Engine): void;
|
|
132
|
+
dispose(): void;
|
|
133
|
+
[Symbol.dispose]: () => void;
|
|
134
|
+
}
|