@margoengine/terrain 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1808 -3
- package/dist/blend.d.ts +70 -0
- package/dist/blend.js +159 -0
- package/dist/colors.d.ts +164 -0
- package/dist/deform.d.ts +103 -0
- package/dist/deform.js +322 -0
- package/dist/displace.d.ts +60 -0
- package/dist/displace.js +254 -0
- package/dist/dunes.d.ts +86 -0
- package/dist/dunes.js +88 -0
- package/dist/errors-fc4i1pSL.js +7 -0
- package/dist/grid.d.ts +11 -0
- package/dist/ground-change-settings.d.ts +29 -0
- package/dist/ground-change-settings.js +69 -0
- package/dist/ground-change.d.ts +227 -0
- package/dist/ground-change.js +1291 -0
- package/dist/ground-model.d.ts +290 -0
- package/dist/hdr-format-DRhzGS7G.js +30 -0
- package/dist/height-grid.d.ts +65 -0
- package/dist/height-grid.js +74 -0
- package/dist/heightfield.d.ts +38 -0
- package/dist/holes.d.ts +37 -0
- package/dist/holes.js +97 -0
- package/dist/imagery.d.ts +109 -0
- package/dist/imagery.js +472 -0
- package/dist/index.d.ts +297 -0
- package/dist/index.js +1 -0
- package/dist/layers-pbr.d.ts +78 -0
- package/dist/layers-pbr.js +128 -0
- package/dist/lighting-layout-CaDtDa5G.js +34 -0
- package/dist/lighting-layout.d.ts +5 -0
- package/dist/mesh-patch-BThCHCeS.js +21 -0
- package/dist/mesh-patch.d.ts +14 -0
- package/dist/noise3-sQdnbz4B.js +11 -0
- package/dist/noise3.d.ts +3 -0
- package/dist/overlays.d.ts +74 -0
- package/dist/overlays.js +297 -0
- package/dist/planet-craters.d.ts +55 -0
- package/dist/planet-recipes.d.ts +75 -0
- package/dist/planet-recipes.js +216 -0
- package/dist/planet.d.ts +139 -0
- package/dist/planet.js +602 -0
- package/dist/query.d.ts +150 -0
- package/dist/query.js +243 -0
- package/dist/relief-lighting.d.ts +46 -0
- package/dist/relief-lighting.js +208 -0
- package/dist/require-webgpu--tUXPsmA.js +7 -0
- package/dist/rock-settings.d.ts +39 -0
- package/dist/rock-settings.js +84 -0
- package/dist/rock-surface.d.ts +111 -0
- package/dist/rock-surface.js +368 -0
- package/dist/rocks.d.ts +258 -0
- package/dist/rocks.js +488 -0
- package/dist/rvt.d.ts +136 -0
- package/dist/rvt.js +518 -0
- package/dist/sculpt.d.ts +103 -0
- package/dist/sculpt.js +361 -0
- package/dist/settings-B9BSdnhD.js +72 -0
- package/dist/settings.d.ts +81 -0
- package/dist/settings.js +152 -0
- package/dist/shader.d.ts +61 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/src-C6dtSaAb.js +1155 -0
- package/dist/stream.d.ts +100 -0
- package/dist/stream.js +339 -0
- package/dist/surface-height.d.ts +16 -0
- package/dist/wet-ground.d.ts +82 -0
- package/dist/wet-ground.js +147 -0
- package/package.json +148 -6
package/dist/stream.d.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import { Terrain, type TerrainOptions } from './index.js';
|
|
5
|
+
/** One loaded tile: `tileTexels` x `tileTexels` samples, row by row along +X from the tile's minimum corner, rows along +Z. */
|
|
6
|
+
export interface TerrainStreamTile {
|
|
7
|
+
/** Heights in heightmap units (world Y is `height * heightScale + heightOffset`), finite. */
|
|
8
|
+
readonly heights: Float32Array;
|
|
9
|
+
/**
|
|
10
|
+
* With `TerrainStreamOptions.weights` above 0: that many arrays of RGBA bytes (4 per texel), the painted layer
|
|
11
|
+
* weights of `TerrainTextures.weights` for this tile (layer i is channel i % 4 of array i / 4).
|
|
12
|
+
*/
|
|
13
|
+
readonly weights?: readonly Uint8Array[];
|
|
14
|
+
}
|
|
15
|
+
/** What changed in the heightmap: a band of rows uploaded, or the whole window moved (`shifted`). Texel rectangle. */
|
|
16
|
+
export interface TerrainStreamChange {
|
|
17
|
+
readonly shifted: boolean;
|
|
18
|
+
readonly u: number;
|
|
19
|
+
readonly v: number;
|
|
20
|
+
readonly width: number;
|
|
21
|
+
readonly depth: number;
|
|
22
|
+
}
|
|
23
|
+
/** {@link TerrainStream} options: the terrain's own options without `textures`, `origin` and `size`, plus these.
|
|
24
|
+
* The clipmap window defaults to `'camera'`, on the same world lattice as its tiles. */
|
|
25
|
+
export interface TerrainStreamOptions extends Omit<TerrainOptions, 'textures' | 'origin' | 'size'> {
|
|
26
|
+
/** World size of a tile side, above 0. */
|
|
27
|
+
readonly tileSize: number;
|
|
28
|
+
/** Height samples per tile side, an integer from 2 to 1,024. Adjacent tiles share no samples. */
|
|
29
|
+
readonly tileTexels: number;
|
|
30
|
+
/** Tiles per window side, an even integer from 2 to 16. Default 4. The window must fit the device's 2D texture limit. */
|
|
31
|
+
readonly window?: number;
|
|
32
|
+
/** World XZ of tile (0, 0)'s minimum corner (its first sample). Default [0, 0]. */
|
|
33
|
+
readonly origin?: readonly [number, number];
|
|
34
|
+
/**
|
|
35
|
+
* Loads tile (x, z) (any integers; tile (x, z) starts at `origin + [x, z] * tileSize`). Resolve undefined or null
|
|
36
|
+
* for no data there (the tile keeps `fill`). `signal` aborts when the tile leaves the window or the stream is disposed.
|
|
37
|
+
*/
|
|
38
|
+
readonly load: (x: number, z: number, signal: AbortSignal) => Promise<TerrainStreamTile | null | undefined>;
|
|
39
|
+
/** Makes the layer colour array (`TerrainTextures.layers`), at attachment and again after device restoration. */
|
|
40
|
+
readonly colors: (device: GPUDevice) => GPUTexture;
|
|
41
|
+
/** Painted weight textures per tile, 0 to 4 (enough for the layers, 4 each). Default 0: the height and slope rules. */
|
|
42
|
+
readonly weights?: number;
|
|
43
|
+
/** Texels uploaded per frame (one tile row at least). Default 65,536 (one 256 x 256 tile). */
|
|
44
|
+
readonly budget?: number;
|
|
45
|
+
/** Tile loads in flight at once, 1 to 64. Default 4. */
|
|
46
|
+
readonly concurrency?: number;
|
|
47
|
+
/** Height of texels whose tile is not loaded yet, finite. Default 0. */
|
|
48
|
+
readonly fill?: number;
|
|
49
|
+
/** Aborts every load; no new ones start, and `ready` rejects with its reason. */
|
|
50
|
+
readonly signal?: AbortSignal;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A {@link Terrain} whose heightmap (and painted weights) stream in tiles around the camera, so the world can be far
|
|
54
|
+
* bigger than one heightmap. Attach with {@link TerrainStream.attach}; it makes and owns the terrain (`terrain`).
|
|
55
|
+
* The window follows the engine camera: it moves a whole tile when the camera is more than 0.75 of a tile from its
|
|
56
|
+
* centre, so ground at least `(window / 2 - 0.75) * tileSize` from the camera on each axis is always in it (the
|
|
57
|
+
* terrain draws nothing past the window, as past any heightmap).
|
|
58
|
+
*/
|
|
59
|
+
export declare class TerrainStream implements EnginePlugin {
|
|
60
|
+
#private;
|
|
61
|
+
readonly name: string;
|
|
62
|
+
/** The terrain drawing the window. The stream owns it: `dispose()` disposes it. */
|
|
63
|
+
readonly terrain: Terrain;
|
|
64
|
+
/** Texels per window side (`window * tileTexels`). */
|
|
65
|
+
readonly columns: number;
|
|
66
|
+
/** CPU copy of the heightmap, `columns` x `columns`, row by row from `origin` (kept in place: the same array always). */
|
|
67
|
+
readonly heights: Float32Array;
|
|
68
|
+
/** CPU copies of the weight textures, RGBA bytes, as `heights`. */
|
|
69
|
+
readonly weights: readonly Uint8Array[];
|
|
70
|
+
disposed: boolean;
|
|
71
|
+
private constructor();
|
|
72
|
+
/**
|
|
73
|
+
* Makes a streamed terrain on `engine` (WebGPU) around its camera and starts loading. Returns it; `dispose()`
|
|
74
|
+
* removes it and its terrain.
|
|
75
|
+
* @throws RangeError or TypeError for bad options (the terrain's own included); MargoError `'unsupported'` on WebGL.
|
|
76
|
+
*/
|
|
77
|
+
static attach(engine: EngineKernel, options: TerrainStreamOptions): TerrainStream;
|
|
78
|
+
/** World XZ of the window's first texel (the terrain's `origin`); moves by whole tiles. */
|
|
79
|
+
get origin(): readonly [number, number];
|
|
80
|
+
/** Tile index of the window's minimum corner. */
|
|
81
|
+
get tile(): readonly [number, number];
|
|
82
|
+
/** Rises on every change to the heightmap: an upload or a window move. */
|
|
83
|
+
get version(): number;
|
|
84
|
+
/** Whether tile (x, z) is in the window and fully uploaded (loaded data, or no data at `fill`). */
|
|
85
|
+
resident(x: number, z: number): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Calls `callback` for every change to the heightmap, for a `TerrainQuery` over `heights`: refresh it over a band's
|
|
88
|
+
* rectangle, or build a new one (`origin` moved) when `shifted`. Returns the remover.
|
|
89
|
+
*/
|
|
90
|
+
onChange(callback: (change: TerrainStreamChange) => void): () => void;
|
|
91
|
+
/**
|
|
92
|
+
* Resolves once every tile of the current window is loaded and uploaded (uploads go in as frames are drawn).
|
|
93
|
+
* Rejects with a tile's load error, `signal`'s or the stream's abort reason, or MargoError `'disposed'`.
|
|
94
|
+
*/
|
|
95
|
+
ready(signal?: AbortSignal): Promise<void>;
|
|
96
|
+
setup(engine: Engine): void;
|
|
97
|
+
onPreUpdate(): void;
|
|
98
|
+
dispose(): void;
|
|
99
|
+
[Symbol.dispose]: () => void;
|
|
100
|
+
}
|
package/dist/stream.js
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const t=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./src-C6dtSaAb.js";
|
|
5
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
|
+
import { MargoError as n } from "@margoengine/math/errors";
|
|
7
|
+
//#region src/stream.ts
|
|
8
|
+
var r = [
|
|
9
|
+
0,
|
|
10
|
+
0,
|
|
11
|
+
0
|
|
12
|
+
], i = 0, a = (e, t, n) => Number.isInteger(e) && e >= t && e <= n, o = (e, t, n, r, i, a) => {
|
|
13
|
+
let o = Math.max(0, -r), s = Math.min(t, t - r);
|
|
14
|
+
for (let c = 0; c < t; c++) {
|
|
15
|
+
let l = i >= 0 ? c : t - 1 - c, u = l + i, d = l * t * n;
|
|
16
|
+
u < 0 || u >= t || s <= o ? e.fill(a, d, d + t * n) : (e.copyWithin(d + o * n, (u * t + o + r) * n, (u * t + s + r) * n), e.fill(a, d, d + o * n), e.fill(a, d + s * n, d + t * n));
|
|
17
|
+
}
|
|
18
|
+
}, s = class s {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
19
|
+
#e;
|
|
20
|
+
#t;
|
|
21
|
+
#n;
|
|
22
|
+
#r;
|
|
23
|
+
#i;
|
|
24
|
+
#a;
|
|
25
|
+
#o;
|
|
26
|
+
#s;
|
|
27
|
+
#c;
|
|
28
|
+
#l;
|
|
29
|
+
#u;
|
|
30
|
+
#d;
|
|
31
|
+
#f;
|
|
32
|
+
#p;
|
|
33
|
+
#m;
|
|
34
|
+
#h;
|
|
35
|
+
#g;
|
|
36
|
+
#_;
|
|
37
|
+
constructor(t, n) {
|
|
38
|
+
this.name = "terrain-stream:" + i++, this.disposed = !1, this.#r = [0, 0], this.#i = [0, 0], this.#a = /* @__PURE__ */ new Map(), this.#o = /* @__PURE__ */ new Set(), this.#s = /* @__PURE__ */ new Set(), this.#d = [], this.#g = 0, this.#_ = 0, this.#D = (e) => {
|
|
39
|
+
let t = this.columns, n = this.#n;
|
|
40
|
+
if (t > e.limits.maxTextureDimension2D) throw RangeError(`Terrain stream window of ${t} texels is above this device's 2D texture limit (${e.limits.maxTextureDimension2D}); use a smaller window or tileTexels.`);
|
|
41
|
+
this.#O();
|
|
42
|
+
let r = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST, i = GPUTextureUsage.COPY_SRC | GPUTextureUsage.COPY_DST;
|
|
43
|
+
this.#l = e, this.#u = e.createTexture({
|
|
44
|
+
size: [t, t],
|
|
45
|
+
format: "r32float",
|
|
46
|
+
usage: r
|
|
47
|
+
}), e.queue.writeTexture({ texture: this.#u }, this.heights, { bytesPerRow: t * 4 }, [t, t]), this.#f = e.createTexture({
|
|
48
|
+
size: [t, t],
|
|
49
|
+
format: "r32float",
|
|
50
|
+
usage: i
|
|
51
|
+
}), this.#m = e.createTexture({
|
|
52
|
+
size: [n, n],
|
|
53
|
+
format: "r32float",
|
|
54
|
+
usage: i
|
|
55
|
+
}), e.queue.writeTexture({ texture: this.#m }, new Float32Array(n * n).fill(this.#e.fill ?? 0), { bytesPerRow: n * 4 }, [n, n]), this.#d = this.weights.map((n) => {
|
|
56
|
+
let i = e.createTexture({
|
|
57
|
+
size: [t, t],
|
|
58
|
+
format: "rgba8unorm",
|
|
59
|
+
usage: r
|
|
60
|
+
});
|
|
61
|
+
return e.queue.writeTexture({ texture: i }, n, { bytesPerRow: t * 4 }, [t, t]), i;
|
|
62
|
+
}), this.weights.length && (this.#p = e.createTexture({
|
|
63
|
+
size: [t, t],
|
|
64
|
+
format: "rgba8unorm",
|
|
65
|
+
usage: i
|
|
66
|
+
}), this.#h = e.createTexture({
|
|
67
|
+
size: [n, n],
|
|
68
|
+
format: "rgba8unorm",
|
|
69
|
+
usage: i
|
|
70
|
+
}));
|
|
71
|
+
let a = this.#d;
|
|
72
|
+
return {
|
|
73
|
+
heightmap: this.#u,
|
|
74
|
+
layers: this.#e.colors(e),
|
|
75
|
+
...a.length ? { weights: a.length === 1 ? [a[0], a[0]] : a } : {}
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
let r = n;
|
|
79
|
+
if (!r || typeof r.load != "function" || typeof r.colors != "function") throw TypeError("Invalid terrain stream; pass load(x, z, signal) and colors(device) functions.");
|
|
80
|
+
let o = r.origin ?? [0, 0];
|
|
81
|
+
if (!(r.tileSize > 0 && Number.isFinite(Math.fround(r.tileSize))) || !a(r.tileTexels, 2, 1024) || !a(r.window ?? 4, 2, 16) || (r.window ?? 4) % 2 || !a(r.weights ?? 0, 0, 4) || r.weights && r.weights * 4 < (r.layers?.length ?? 0) || !a(r.budget ?? 65536, 1, 2 ** 26) || !a(r.concurrency ?? 4, 1, 64) || !Number.isFinite(Math.fround(r.fill ?? 0)) || !Array.isArray(o) || !o.every((e) => Number.isFinite(e))) throw RangeError("Invalid terrain stream options; give tileSize above 0, tileTexels 2..1024, an even window 2..16, weights 0..4 (4 layers each), budget and concurrency (1..64) whole numbers, and a finite fill and origin.");
|
|
82
|
+
this.#e = r, this.#n = r.tileTexels, this.#t = r.window ?? 4, this.columns = this.#t * this.#n;
|
|
83
|
+
let s = this.columns * this.columns;
|
|
84
|
+
this.heights = new Float32Array(s).fill(r.fill ?? 0), this.weights = Array.from({ length: r.weights ?? 0 }, () => new Uint8Array(s * 4));
|
|
85
|
+
let c = t.camera.position, l = r.floatingOrigin?.origin, u = l ? [
|
|
86
|
+
c[0] + l[0],
|
|
87
|
+
c[1] + l[1],
|
|
88
|
+
c[2] + l[2]
|
|
89
|
+
] : c;
|
|
90
|
+
this.#v(Math.round((u[0] - o[0]) / r.tileSize - this.#t / 2), Math.round((u[2] - o[1]) / r.tileSize - this.#t / 2));
|
|
91
|
+
let d = r.tileSize / this.#n, f = (this.columns - 1) * d, p = [
|
|
92
|
+
"tileSize",
|
|
93
|
+
"tileTexels",
|
|
94
|
+
"window",
|
|
95
|
+
"load",
|
|
96
|
+
"colors",
|
|
97
|
+
"weights",
|
|
98
|
+
"budget",
|
|
99
|
+
"concurrency",
|
|
100
|
+
"fill",
|
|
101
|
+
"signal"
|
|
102
|
+
], m = Object.fromEntries(Object.entries(r).filter(([e]) => !p.includes(e)));
|
|
103
|
+
this.terrain = new e({
|
|
104
|
+
...m,
|
|
105
|
+
clipmapAnchor: r.clipmapAnchor ?? "camera",
|
|
106
|
+
origin: this.#i,
|
|
107
|
+
size: [f, f],
|
|
108
|
+
textures: this.#D
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
static attach(e, n) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
112
|
+
let r = new s(e, n), i = t(e);
|
|
113
|
+
return i.use(r.terrain), i.use(r), r;
|
|
114
|
+
}
|
|
115
|
+
get origin() {
|
|
116
|
+
return this.#i;
|
|
117
|
+
}
|
|
118
|
+
get tile() {
|
|
119
|
+
return this.#r;
|
|
120
|
+
}
|
|
121
|
+
get version() {
|
|
122
|
+
return this.#_;
|
|
123
|
+
}
|
|
124
|
+
resident(e, t) {
|
|
125
|
+
return this.#a.get(e + "," + t)?.state === "resident";
|
|
126
|
+
}
|
|
127
|
+
onChange(e) {
|
|
128
|
+
if (typeof e != "function") throw TypeError("Invalid terrain stream listener; pass a function.");
|
|
129
|
+
return this.#s.add(e), () => this.#s.delete(e);
|
|
130
|
+
}
|
|
131
|
+
ready(e) {
|
|
132
|
+
return new Promise((t, n) => {
|
|
133
|
+
if (e?.aborted) return n(e.reason);
|
|
134
|
+
let r = () => {
|
|
135
|
+
this.#o.delete(i), n(e.reason);
|
|
136
|
+
}, i = {
|
|
137
|
+
resolve: () => (e?.removeEventListener("abort", r), t()),
|
|
138
|
+
reject: (t) => (e?.removeEventListener("abort", r), n(t))
|
|
139
|
+
};
|
|
140
|
+
e?.addEventListener("abort", r, { once: !0 }), this.#o.add(i), this.#E();
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
setup(e) {
|
|
144
|
+
if (this.disposed || !e.gpu) throw new n(this.disposed ? "disposed" : "unsupported", "Terrain streaming requires WebGPU and a live stream; attach a new TerrainStream to a WebGPU engine");
|
|
145
|
+
this.#c = e;
|
|
146
|
+
}
|
|
147
|
+
onPreUpdate() {
|
|
148
|
+
if (this.disposed || !this.#c || !this.#u) return;
|
|
149
|
+
let e = this.#c.camera.position, t = this.terrain.options.floatingOrigin ? this.terrain.shift : r, [n, i] = this.#e.origin ?? [0, 0], a = this.#t / 2, o = (e[0] + t[0] - n) / this.#e.tileSize - this.#r[0] - a, s = (e[2] + t[2] - i) / this.#e.tileSize - this.#r[1] - a;
|
|
150
|
+
if (Number.isFinite(o) && Number.isFinite(s)) {
|
|
151
|
+
let e = Math.abs(o) > .75 ? Math.round(o) : 0, t = Math.abs(s) > .75 ? Math.round(s) : 0;
|
|
152
|
+
(e || t) && this.#b(e, t);
|
|
153
|
+
}
|
|
154
|
+
let c = this.#r[0] + a + (Number.isFinite(o) ? o : 0), l = this.#r[1] + a + (Number.isFinite(s) ? s : 0);
|
|
155
|
+
this.#x(c, l), this.#w(c, l);
|
|
156
|
+
}
|
|
157
|
+
#v(e, t) {
|
|
158
|
+
let [n, r] = this.#e.origin ?? [0, 0];
|
|
159
|
+
this.#r[0] = e, this.#r[1] = t, this.#i[0] = n + e * this.#e.tileSize, this.#i[1] = r + t * this.#e.tileSize;
|
|
160
|
+
}
|
|
161
|
+
#y(e, t) {
|
|
162
|
+
return e >= this.#r[0] && t >= this.#r[1] && e < this.#r[0] + this.#t && t < this.#r[1] + this.#t;
|
|
163
|
+
}
|
|
164
|
+
#b(e, t) {
|
|
165
|
+
let n = this.#n, r = this.columns, i = e * n, a = t * n;
|
|
166
|
+
o(this.heights, r, 1, i, a, this.#e.fill ?? 0);
|
|
167
|
+
for (let e of this.weights) o(e, r, 4, i, a, 0);
|
|
168
|
+
this.#v(this.#r[0] + e, this.#r[1] + t);
|
|
169
|
+
for (let [e, t] of this.#a) {
|
|
170
|
+
let [n, r] = e.split(",").map(Number);
|
|
171
|
+
this.#y(n, r) || (t.state === "loading" && t.controller.abort(), this.#a.delete(e));
|
|
172
|
+
}
|
|
173
|
+
let s = this.#l, c = s.createCommandEncoder(), l = r - Math.abs(i), u = r - Math.abs(a), d = [[
|
|
174
|
+
this.#u,
|
|
175
|
+
this.#f,
|
|
176
|
+
this.#m
|
|
177
|
+
], ...this.#d.map((e) => [
|
|
178
|
+
e,
|
|
179
|
+
this.#p,
|
|
180
|
+
this.#h
|
|
181
|
+
])];
|
|
182
|
+
for (let [r, o, s] of d) {
|
|
183
|
+
if (l > 0 && u > 0) {
|
|
184
|
+
let e = [Math.max(0, -i), Math.max(0, -a)];
|
|
185
|
+
c.copyTextureToTexture({
|
|
186
|
+
texture: r,
|
|
187
|
+
origin: [Math.max(0, i), Math.max(0, a)]
|
|
188
|
+
}, {
|
|
189
|
+
texture: o,
|
|
190
|
+
origin: e
|
|
191
|
+
}, [l, u]), c.copyTextureToTexture({
|
|
192
|
+
texture: o,
|
|
193
|
+
origin: e
|
|
194
|
+
}, {
|
|
195
|
+
texture: r,
|
|
196
|
+
origin: e
|
|
197
|
+
}, [l, u]);
|
|
198
|
+
}
|
|
199
|
+
for (let i = 0; i < this.#t; i++) for (let a = 0; a < this.#t; a++) {
|
|
200
|
+
let o = a + e, l = i + t;
|
|
201
|
+
o >= 0 && l >= 0 && o < this.#t && l < this.#t || c.copyTextureToTexture({ texture: s }, {
|
|
202
|
+
texture: r,
|
|
203
|
+
origin: [a * n, i * n]
|
|
204
|
+
}, [n, n]);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
s.queue.submit([c.finish()]), this.#T({
|
|
208
|
+
shifted: !0,
|
|
209
|
+
u: 0,
|
|
210
|
+
v: 0,
|
|
211
|
+
width: r,
|
|
212
|
+
depth: r
|
|
213
|
+
}), this.#E();
|
|
214
|
+
}
|
|
215
|
+
#x(e, t) {
|
|
216
|
+
let n = this.#e, r = n.concurrency ?? 4;
|
|
217
|
+
if (this.#g >= r || this.#a.size >= this.#t * this.#t || n.signal?.aborted) return;
|
|
218
|
+
let i = [];
|
|
219
|
+
for (let n = 0; n < this.#t; n++) for (let r = 0; r < this.#t; r++) {
|
|
220
|
+
let a = this.#r[0] + r, o = this.#r[1] + n;
|
|
221
|
+
this.#a.has(a + "," + o) || i.push([
|
|
222
|
+
(a + .5 - e) ** 2 + (o + .5 - t) ** 2,
|
|
223
|
+
a,
|
|
224
|
+
o
|
|
225
|
+
]);
|
|
226
|
+
}
|
|
227
|
+
i.sort((e, t) => e[0] - t[0]);
|
|
228
|
+
for (let [, e, t] of i) {
|
|
229
|
+
if (this.#g >= r) break;
|
|
230
|
+
this.#S(e, t);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
#S(e, t) {
|
|
234
|
+
let n = e + "," + t, r = new AbortController(), i = {
|
|
235
|
+
state: "loading",
|
|
236
|
+
controller: r
|
|
237
|
+
}, a = this.#e.signal ? AbortSignal.any([this.#e.signal, r.signal]) : r.signal;
|
|
238
|
+
this.#a.set(n, i), this.#g++;
|
|
239
|
+
let o;
|
|
240
|
+
try {
|
|
241
|
+
o = Promise.resolve(this.#e.load(e, t, a));
|
|
242
|
+
} catch (e) {
|
|
243
|
+
o = Promise.reject(e);
|
|
244
|
+
}
|
|
245
|
+
o.then((e) => {
|
|
246
|
+
this.#a.get(n) !== i || this.disposed || this.#a.set(n, e ? {
|
|
247
|
+
state: "queued",
|
|
248
|
+
tile: this.#C(e),
|
|
249
|
+
row: 0
|
|
250
|
+
} : { state: "resident" });
|
|
251
|
+
}, (e) => {
|
|
252
|
+
this.#a.get(n) !== i || this.disposed || this.#a.set(n, {
|
|
253
|
+
state: "failed",
|
|
254
|
+
error: e
|
|
255
|
+
});
|
|
256
|
+
}).catch((e) => {
|
|
257
|
+
this.#a.get(n) === i && this.#a.set(n, {
|
|
258
|
+
state: "failed",
|
|
259
|
+
error: e
|
|
260
|
+
});
|
|
261
|
+
}).finally(() => {
|
|
262
|
+
this.#g--, this.#E();
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
#C(e) {
|
|
266
|
+
let t = this.#n * this.#n, n = this.weights.length;
|
|
267
|
+
if (!(e.heights instanceof Float32Array) || e.heights.length !== t || !e.heights.every(Number.isFinite) || n > 0 && (!Array.isArray(e.weights) || e.weights.length !== n || e.weights.some((e) => !(e instanceof Uint8Array) || e.length !== t * 4))) throw RangeError(`Invalid terrain stream tile; give ${t} finite float32 heights and ${n} weight arrays of ${t * 4} bytes.`);
|
|
268
|
+
return e;
|
|
269
|
+
}
|
|
270
|
+
#w(e, t) {
|
|
271
|
+
let n = this.#n, r = this.columns, i = this.#l.queue, a = this.#e.budget ?? 65536, o = !1;
|
|
272
|
+
for (; a > 0;) {
|
|
273
|
+
let s, c = Infinity;
|
|
274
|
+
for (let [n, r] of this.#a) {
|
|
275
|
+
if (r.state !== "queued") continue;
|
|
276
|
+
let [i, a] = n.split(",").map(Number), o = (i + .5 - e) ** 2 + (a + .5 - t) ** 2;
|
|
277
|
+
o < c && (c = o, s = {
|
|
278
|
+
key: n,
|
|
279
|
+
slot: r,
|
|
280
|
+
x: i,
|
|
281
|
+
z: a
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
if (!s) break;
|
|
285
|
+
let { slot: l, x: u, z: d } = s, f = l.row, p = Math.min(n, f + Math.max(1, Math.floor(a / n))), m = (u - this.#r[0]) * n, h = (d - this.#r[1]) * n + f, g = l.tile;
|
|
286
|
+
for (let e = f; e < p; e++) this.heights.set(g.heights.subarray(e * n, (e + 1) * n), (h + e - f) * r + m), this.weights.forEach((t, i) => t.set(g.weights[i].subarray(e * n * 4, (e + 1) * n * 4), ((h + e - f) * r + m) * 4));
|
|
287
|
+
i.writeTexture({
|
|
288
|
+
texture: this.#u,
|
|
289
|
+
origin: [m, h]
|
|
290
|
+
}, g.heights, {
|
|
291
|
+
offset: f * n * 4,
|
|
292
|
+
bytesPerRow: n * 4
|
|
293
|
+
}, [n, p - f]), this.#d.forEach((e, t) => i.writeTexture({
|
|
294
|
+
texture: e,
|
|
295
|
+
origin: [m, h]
|
|
296
|
+
}, g.weights[t], {
|
|
297
|
+
offset: f * n * 4,
|
|
298
|
+
bytesPerRow: n * 4
|
|
299
|
+
}, [n, p - f])), a -= (p - f) * n, l.row = p, p === n && (this.#a.set(s.key, { state: "resident" }), o = !0), this.#T({
|
|
300
|
+
shifted: !1,
|
|
301
|
+
u: m,
|
|
302
|
+
v: h,
|
|
303
|
+
width: n,
|
|
304
|
+
depth: p - f
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
o && this.#E();
|
|
308
|
+
}
|
|
309
|
+
#T(e) {
|
|
310
|
+
this.#_++, this.terrain.invalidate();
|
|
311
|
+
for (let t of [...this.#s]) t(e);
|
|
312
|
+
}
|
|
313
|
+
#E() {
|
|
314
|
+
if (!this.#o.size) return;
|
|
315
|
+
let e, t = !1, r = 0;
|
|
316
|
+
this.#e.signal?.aborted && (t = !0, e = this.#e.signal.reason), this.disposed && (t = !0, e = new n("disposed", "The terrain stream was disposed; attach a new one"));
|
|
317
|
+
for (let n of this.#a.values()) n.state === "failed" && !t ? (t = !0, e = n.error) : n.state === "resident" && r++;
|
|
318
|
+
if (!t && r < this.#t * this.#t) return;
|
|
319
|
+
let i = [...this.#o];
|
|
320
|
+
this.#o.clear();
|
|
321
|
+
for (let n of i) t ? n.reject(e) : n.resolve();
|
|
322
|
+
}
|
|
323
|
+
#D;
|
|
324
|
+
#O() {
|
|
325
|
+
this.#f?.destroy(), this.#p?.destroy(), this.#m?.destroy(), this.#h?.destroy(), this.#f = this.#p = this.#m = this.#h = void 0;
|
|
326
|
+
}
|
|
327
|
+
dispose() {
|
|
328
|
+
if (!this.disposed) {
|
|
329
|
+
this.disposed = !0;
|
|
330
|
+
for (let e of this.#a.values()) e.state === "loading" && e.controller.abort();
|
|
331
|
+
this.#E(), this.#a.clear(), this.#s.clear(), this.#c?.sceneOf(this)?.removePlugin(this.name), this.terrain.dispose(), this.#O(), this.#c = this.#l = this.#u = void 0, this.#d = [];
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
335
|
+
this.dispose();
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
//#endregion
|
|
339
|
+
export { s as TerrainStream };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface GridDraw {
|
|
2
|
+
readonly vertex: GPUBuffer;
|
|
3
|
+
readonly center: GPUBuffer;
|
|
4
|
+
readonly ring: GPUBuffer;
|
|
5
|
+
readonly centerCount: number;
|
|
6
|
+
readonly ringCount: number;
|
|
7
|
+
}
|
|
8
|
+
interface SurfaceHeight {
|
|
9
|
+
readonly texture: GPUTexture;
|
|
10
|
+
draw(encoder: GPUCommandEncoder, version: number, rings: readonly {
|
|
11
|
+
group: GPUBindGroup;
|
|
12
|
+
}[], geometry: GridDraw): void;
|
|
13
|
+
}
|
|
14
|
+
/** A world-height image of the clipmap triangles. UVs match the source texture including the half-texel border. */
|
|
15
|
+
export declare function surfaceHeight(device: GPUDevice, source: GPUTexture, layout: GPUBindGroupLayout, vertexCode: string, clip: (code: string) => string, floating: boolean): SurfaceHeight | undefined;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import { type SettingRow } from './shared/settings.js';
|
|
3
|
+
import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
5
|
+
import type { Terrain, TerrainLighting } from './index.js';
|
|
6
|
+
type Pair = readonly [number, number];
|
|
7
|
+
/** The look of wet ground; every field is live (one small uniform). */
|
|
8
|
+
export interface WetGroundLook {
|
|
9
|
+
/** Albedo of fully wet ground, as a share of dry (1 is no darkening), 0 to 1. Default 0.55. */
|
|
10
|
+
darkening?: number;
|
|
11
|
+
/** Share by which fully wet ground is smoother (shinier), 0 to 1. Default 0.5. */
|
|
12
|
+
gloss?: number;
|
|
13
|
+
/** Seconds over which wetness falls to 1/e once the water has gone, 1 to 604,800 (a week). Default 10,800 (3 hours). */
|
|
14
|
+
dryTime?: number;
|
|
15
|
+
}
|
|
16
|
+
/** Options of {@link WetGround.attach}. */
|
|
17
|
+
export interface WetGroundOptions extends WetGroundLook {
|
|
18
|
+
/**
|
|
19
|
+
* The wetting record, asked every frame (borrowed, never destroyed): an rgba32float 2D texture with TEXTURE_BINDING
|
|
20
|
+
* whose green channel is the seconds since the texel was last wet and blue 1 once it has been wet, as
|
|
21
|
+
* `Flood.wetTexture`. Undefined draws the ground dry.
|
|
22
|
+
*/
|
|
23
|
+
record: () => GPUTexture | undefined;
|
|
24
|
+
/** World [x, z] of the record's first texel's corner (a Flood's `origin`). */
|
|
25
|
+
origin: Pair;
|
|
26
|
+
/** World [width, depth] the record covers (a Flood's `size`). */
|
|
27
|
+
size: Pair;
|
|
28
|
+
/** The first of the two lighting-group bindings the effect declares. Default 116. */
|
|
29
|
+
binding?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Wet ground on a clipmap {@link Terrain} from a wetting record: darker and glossier where water has been, drying back
|
|
33
|
+
* over time. `WetGround.attach(engine, terrain, { record: () => flood.wetTexture, origin, size })`. WebGPU (and the
|
|
34
|
+
* WebGL2 compatibility layer).
|
|
35
|
+
* @throws TypeError for a missing record function or terrain; RangeError for an origin, size, look value or binding out of range.
|
|
36
|
+
* @throws MargoError `'disposed'` after `dispose()`, `'unsupported'` when the terrain's shader has no `terrainColor`.
|
|
37
|
+
*/
|
|
38
|
+
export declare class WetGround implements EnginePlugin, TerrainLighting, Disposable {
|
|
39
|
+
#private;
|
|
40
|
+
readonly name: string;
|
|
41
|
+
/** Whether the effect has been disposed. */
|
|
42
|
+
disposed: boolean;
|
|
43
|
+
/** The current look (live). */
|
|
44
|
+
readonly look: Required<WetGroundLook>;
|
|
45
|
+
readonly origin: Pair;
|
|
46
|
+
readonly size: Pair;
|
|
47
|
+
readonly binding: number;
|
|
48
|
+
constructor(terrain: Terrain, options: WetGroundOptions);
|
|
49
|
+
/** Attaches wet ground to `terrain` on `engine`; the record texture stays borrowed. */
|
|
50
|
+
static attach(engine: EngineKernel, terrain: Terrain, options: WetGroundOptions): WetGround;
|
|
51
|
+
/** Sets any of `darkening`, `gloss` and `dryTime` (others keep theirs); nothing is rebuilt. */
|
|
52
|
+
setLook(look: WetGroundLook): void;
|
|
53
|
+
/** The wetness 0..1 a record texel gives under the current look: blue x exp(-green / dryTime). */
|
|
54
|
+
wetness(sinceWet: number, wasWet: number): number;
|
|
55
|
+
get owner(): Engine | undefined;
|
|
56
|
+
setup(engine: Engine): void;
|
|
57
|
+
onDeviceRestored(engine: Engine): void;
|
|
58
|
+
/** The lighting group entries: the record (or a dry texel) and the look. */
|
|
59
|
+
bindings(): GPUBindGroupEntry[];
|
|
60
|
+
/** The terrain's lit shader with `terrainColor` darkened and the roughness lowered by the wetness. */
|
|
61
|
+
shader(source: string, group: number): string;
|
|
62
|
+
/** Detaches from the terrain (its shader returns to what it was) and frees the uniform; the record stays borrowed. */
|
|
63
|
+
dispose(): void;
|
|
64
|
+
[Symbol.dispose]: () => void;
|
|
65
|
+
}
|
|
66
|
+
/** One wet ground control. */
|
|
67
|
+
export interface WetGroundSetting extends SettingRow {
|
|
68
|
+
readonly group: 'wet';
|
|
69
|
+
readonly default: number;
|
|
70
|
+
}
|
|
71
|
+
/** Every wet ground control, in panel order; defaults are what WetGround uses without the option. */
|
|
72
|
+
export declare const WET_GROUND_SETTINGS: readonly WetGroundSetting[];
|
|
73
|
+
/** The rows that can change every frame (all of them). */
|
|
74
|
+
export declare const WET_GROUND_LIVE_SETTINGS: readonly WetGroundSetting[];
|
|
75
|
+
/** Every setting at its default, as a fresh object to edit (a slider panel's state). */
|
|
76
|
+
export declare function wetGroundSettingDefaults(): Record<string, number>;
|
|
77
|
+
/**
|
|
78
|
+
* Sets the given rows on `ground` (names left out keep their current value). Nothing is rebuilt.
|
|
79
|
+
* @throws TypeError for an unknown name; RangeError for a value out of range.
|
|
80
|
+
*/
|
|
81
|
+
export declare function applyWetGroundSettings(ground: WetGround, values?: Readonly<Record<string, number>>): void;
|
|
82
|
+
export {};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const o=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { t as e } from "./require-webgpu--tUXPsmA.js";
|
|
5
|
+
import { a as t, c as n, l as r, o as i, t as a } from "./settings-B9BSdnhD.js";
|
|
6
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
7
|
+
import { MargoError as s } from "@margoengine/math/errors";
|
|
8
|
+
//#region src/wet-ground.ts
|
|
9
|
+
var c = {
|
|
10
|
+
darkening: .55,
|
|
11
|
+
gloss: .5,
|
|
12
|
+
dryTime: 10800
|
|
13
|
+
}, l = {
|
|
14
|
+
darkening: [0, 1],
|
|
15
|
+
gloss: [0, 1],
|
|
16
|
+
dryTime: [1, 604800]
|
|
17
|
+
}, u = "@fragment fn fragment(i:Output)->@location(0) vec4f {", d = "fn terrainColor(position:vec3f,normal:vec3f)->vec3f {", f = (e, t) => Array.isArray(e) && e.length === 2 && e.every((e) => typeof e == "number" && Number.isFinite(e) && (!t || e > 0)), p = 0, m = class t {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
18
|
+
#e;
|
|
19
|
+
#t;
|
|
20
|
+
#n;
|
|
21
|
+
#r;
|
|
22
|
+
#i;
|
|
23
|
+
#a;
|
|
24
|
+
#o;
|
|
25
|
+
#s;
|
|
26
|
+
#c;
|
|
27
|
+
#l;
|
|
28
|
+
constructor(e, t) {
|
|
29
|
+
if (this.name = "wet-ground:" + p++, this.disposed = !1, this.#o = /* @__PURE__ */ new WeakMap(), this.#s = !0, this.#c = /* @__PURE__ */ new Float32Array(8), this.#l = /* @__PURE__ */ new Map(), !e || typeof e.addLighting != "function") throw TypeError("WetGround needs a clipmap Terrain (@margoengine/terrain); pass the terrain to attach");
|
|
30
|
+
if (!t || typeof t.record != "function") throw TypeError("WetGround needs record: a function returning the wetting record texture, such as () => flood.wetTexture");
|
|
31
|
+
if (!f(t.origin, !1) || !f(t.size, !0)) throw RangeError("WetGround needs a finite [x, z] origin and a positive finite [width, depth] size");
|
|
32
|
+
let n = t.binding ?? 116;
|
|
33
|
+
if (!Number.isInteger(n) || n < 0 || n > 998) throw RangeError("WetGround binding must be an integer 0 to 998");
|
|
34
|
+
this.#t = e, this.#e = t.record, this.origin = [t.origin[0], t.origin[1]], this.size = [t.size[0], t.size[1]], this.binding = n, this.look = { ...c }, this.setLook(t);
|
|
35
|
+
}
|
|
36
|
+
static attach(e, n, r) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
37
|
+
let i = new t(n, r);
|
|
38
|
+
return o(e).use(i), i;
|
|
39
|
+
}
|
|
40
|
+
setLook(e) {
|
|
41
|
+
if (this.disposed) throw new s("disposed", "This WetGround was disposed; attach a new one");
|
|
42
|
+
let t = { ...this.look };
|
|
43
|
+
for (let n of Object.keys(l)) {
|
|
44
|
+
let r = e[n];
|
|
45
|
+
if (r === void 0) continue;
|
|
46
|
+
let [i, a] = l[n];
|
|
47
|
+
if (typeof r != "number" || !Number.isFinite(r) || r < i || r > a) throw RangeError(`WetGround ${n} must be a number from ${i} to ${a}`);
|
|
48
|
+
t[n] = r;
|
|
49
|
+
}
|
|
50
|
+
Object.assign(this.look, t), this.#s = !0;
|
|
51
|
+
}
|
|
52
|
+
wetness(e, t) {
|
|
53
|
+
return t * Math.exp(-e / this.look.dryTime);
|
|
54
|
+
}
|
|
55
|
+
get owner() {
|
|
56
|
+
return this.#n;
|
|
57
|
+
}
|
|
58
|
+
setup(t) {
|
|
59
|
+
if (e(t, "WetGround"), this.disposed) throw new s("disposed", "This WetGround was disposed; attach a new one");
|
|
60
|
+
this.#n = t, this.#u(t.gpu), this.#r ??= this.#t.addLighting(this);
|
|
61
|
+
}
|
|
62
|
+
onDeviceRestored(e) {
|
|
63
|
+
this.#u(e.gpu);
|
|
64
|
+
}
|
|
65
|
+
#u(e) {
|
|
66
|
+
this.#i = e.createBuffer({
|
|
67
|
+
size: 32,
|
|
68
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST
|
|
69
|
+
}), this.#a = e.createTexture({
|
|
70
|
+
size: [1, 1],
|
|
71
|
+
format: "rgba32float",
|
|
72
|
+
usage: GPUTextureUsage.TEXTURE_BINDING
|
|
73
|
+
}), this.#o = /* @__PURE__ */ new WeakMap(), this.#s = !0;
|
|
74
|
+
}
|
|
75
|
+
bindings() {
|
|
76
|
+
let e = this.#n?.gpu;
|
|
77
|
+
if (!e || !this.#i || !this.#a) return [];
|
|
78
|
+
if (this.#s) {
|
|
79
|
+
let t = this.look;
|
|
80
|
+
this.#c.set([
|
|
81
|
+
this.origin[0],
|
|
82
|
+
this.origin[1],
|
|
83
|
+
this.size[0],
|
|
84
|
+
this.size[1],
|
|
85
|
+
t.darkening,
|
|
86
|
+
t.gloss,
|
|
87
|
+
t.dryTime,
|
|
88
|
+
0
|
|
89
|
+
]), e.queue.writeBuffer(this.#i, 0, this.#c), this.#s = !1;
|
|
90
|
+
}
|
|
91
|
+
let t = this.#e(), n = t && t.format === "rgba32float" ? t : this.#a;
|
|
92
|
+
if (t && t.format !== "rgba32float") throw TypeError(`WetGround's record must be rgba32float (Flood.wetTexture), not ${t.format}`);
|
|
93
|
+
let r = this.#o.get(n);
|
|
94
|
+
return r || this.#o.set(n, r = n.createView()), [{
|
|
95
|
+
binding: this.binding,
|
|
96
|
+
resource: r
|
|
97
|
+
}, {
|
|
98
|
+
binding: this.binding + 1,
|
|
99
|
+
resource: { buffer: this.#i }
|
|
100
|
+
}];
|
|
101
|
+
}
|
|
102
|
+
shader(e, t) {
|
|
103
|
+
let n = t + e, r = this.#l.get(n);
|
|
104
|
+
if (r) return r;
|
|
105
|
+
if (e.split(u).length !== 2 || e.split(d).length !== 2) throw new s("unsupported", "WetGround needs the terrain's lit fragment shader and its terrainColor; attach it before effects that replace them");
|
|
106
|
+
let i = "_w" + this.name.slice(this.name.indexOf(":") + 1), a = this.binding, o = `
|
|
107
|
+
@group(${t}) @binding(${a}) var wetRecord${i}:texture_2d<f32>;
|
|
108
|
+
@group(${t}) @binding(${a + 1}) var<uniform> wetLook${i}:array<vec4f,2>;
|
|
109
|
+
fn wetTexel${i}(c:vec2i)->f32 {let r=textureLoad(wetRecord${i},c,0);return r.z*exp(-r.y/max(wetLook${i}[1].z,1.0));}
|
|
110
|
+
fn wetAt${i}(p:vec3f)->f32 {
|
|
111
|
+
let n=vec2i(textureDimensions(wetRecord${i}));let L=wetLook${i};let uv=(p.xz-L[0].xy)/L[0].zw;
|
|
112
|
+
if(any(uv<vec2f(0))||any(uv>vec2f(1))){return 0.0;}
|
|
113
|
+
let f=uv*vec2f(n)-0.5;let lo=clamp(vec2i(floor(f)),vec2i(0),n-1);let hi=min(lo+1,n-1);let w=clamp(f-floor(f),vec2f(0),vec2f(1));
|
|
114
|
+
return mix(mix(wetTexel${i}(lo),wetTexel${i}(vec2i(hi.x,lo.y)),w.x),mix(wetTexel${i}(vec2i(lo.x,hi.y)),wetTexel${i}(hi),w.x),w.y);
|
|
115
|
+
}
|
|
116
|
+
` + e.replace(d, `fn terrainColorDry${i}(position:vec3f,normal:vec3f)->vec3f {`).replace(u, `fn wetGroundInner${i}(i:Output)->vec4f {`) + `\n${d}return terrainColorDry${i}(position,normal)*mix(1.0,wetLook${i}[1].x,wetAt${i}(position));}\n${u}var j=i;j.material.x*=1.0-wetLook${i}[1].y*wetAt${i}(i.position);return wetGroundInner${i}(j);}`;
|
|
117
|
+
return this.#l.size >= 16 && this.#l.clear(), this.#l.set(n, o), o;
|
|
118
|
+
}
|
|
119
|
+
dispose() {
|
|
120
|
+
if (this.disposed) return;
|
|
121
|
+
this.disposed = !0, this.#r?.(), this.#r = void 0, this.#i?.destroy(), this.#a?.destroy(), this.#i = this.#a = void 0, this.#l.clear();
|
|
122
|
+
let e = this.#n;
|
|
123
|
+
this.#n = void 0, this.#t = void 0, e?.sceneOf(this)?.removePlugin(this.name);
|
|
124
|
+
}
|
|
125
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
126
|
+
this.dispose();
|
|
127
|
+
}
|
|
128
|
+
}, h = (e, t, n, r, a, o, s) => i(e, "wet", t, n, r, a, o, s), g = [
|
|
129
|
+
h("wet.darkening", c.darkening, 0, 1, .01, "", "Albedo of fully wet ground as a share of dry (1 is no darkening)."),
|
|
130
|
+
h("wet.gloss", c.gloss, 0, 1, .01, "", "Share by which fully wet ground is smoother (shinier)."),
|
|
131
|
+
h("wet.dryTime", c.dryTime, 1, 604800, 1, "s", "Seconds over which wet ground dries to 1/e once the water has gone.")
|
|
132
|
+
], _ = t(g), v = r(g), y = {
|
|
133
|
+
"wet.darkening": "darkening",
|
|
134
|
+
"wet.gloss": "gloss",
|
|
135
|
+
"wet.dryTime": "dryTime"
|
|
136
|
+
};
|
|
137
|
+
function b() {
|
|
138
|
+
return n(g);
|
|
139
|
+
}
|
|
140
|
+
function x(e, t = {}) {
|
|
141
|
+
a(v, t, "wet ground", "the rows of WET_GROUND_SETTINGS in @margoengine/terrain/wet-ground");
|
|
142
|
+
let n = {};
|
|
143
|
+
for (let [e, r] of Object.entries(t)) n[y[e]] = r;
|
|
144
|
+
e.setLook(n);
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
export { _ as WET_GROUND_LIVE_SETTINGS, g as WET_GROUND_SETTINGS, m as WetGround, x as applyWetGroundSettings, b as wetGroundSettingDefaults };
|