@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.
- package/LICENSE +201 -201
- package/NOTICE +4 -4
- package/README.md +2367 -1808
- package/dist/bed-mesh.d.ts +40 -0
- package/dist/bed-mesh.js +95 -0
- package/dist/blend.js +4 -4
- package/dist/colors.d.ts +6 -0
- package/dist/deform.d.ts +41 -10
- package/dist/deform.js +169 -87
- package/dist/displace.js +4 -4
- package/dist/elevation.d.ts +182 -0
- package/dist/elevation.js +155 -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 +6 -3
- package/dist/ground-change.d.ts +1 -1
- package/dist/ground-change.js +232 -111
- 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 +83 -1
- package/dist/imagery.js +4 -4
- package/dist/index.d.ts +120 -9
- package/dist/index.js +1 -1
- package/dist/layers-pbr.js +9 -9
- package/dist/overlays.js +4 -4
- package/dist/planet-complex.d.ts +31 -0
- package/dist/planet-craters.d.ts +28 -3
- package/dist/planet-rays.d.ts +37 -0
- package/dist/planet-recipes.d.ts +10 -1
- package/dist/planet-recipes.js +341 -115
- package/dist/planet.d.ts +5 -0
- package/dist/planet.js +112 -104
- package/dist/relief-lighting.d.ts +7 -0
- package/dist/relief-lighting.js +36 -23
- package/dist/rock-surface.d.ts +3 -0
- package/dist/rock-surface.js +28 -16
- package/dist/rvt.js +5 -5
- package/dist/scour.d.ts +90 -0
- package/dist/scour.js +153 -0
- package/dist/sculpt.js +4 -4
- package/dist/shader.d.ts +120 -5
- package/dist/shared/settings.d.ts +89 -89
- package/dist/{src-C6dtSaAb.js → src-Cr7Gm4n0.js} +359 -148
- package/dist/stream-window.d.ts +123 -0
- package/dist/stream.d.ts +95 -26
- package/dist/stream.js +440 -248
- package/dist/wet-ground.js +4 -4
- package/package.json +175 -148
package/dist/rock-surface.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const n=(engine,version="
|
|
3
|
+
const n=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { n as e, t } from "./mesh-patch-BThCHCeS.js";
|
|
5
5
|
import { engineRuntime as __margoRuntime, meshRuntime as r } from "@margoengine/core/extension";
|
|
6
6
|
import { MargoError as i } from "@margoengine/math/errors";
|
|
@@ -50,14 +50,14 @@ var a = {
|
|
|
50
50
|
},
|
|
51
51
|
granite: {
|
|
52
52
|
color: [
|
|
53
|
-
.
|
|
54
|
-
.
|
|
55
|
-
.
|
|
53
|
+
.2709,
|
|
54
|
+
.224,
|
|
55
|
+
.1869
|
|
56
56
|
],
|
|
57
57
|
color2: [
|
|
58
|
-
.
|
|
59
|
-
.
|
|
60
|
-
.
|
|
58
|
+
.1678,
|
|
59
|
+
.171,
|
|
60
|
+
.1512
|
|
61
61
|
],
|
|
62
62
|
strata: 0,
|
|
63
63
|
porosity: .25
|
|
@@ -165,14 +165,20 @@ function f(e) {
|
|
|
165
165
|
o[t]
|
|
166
166
|
]);
|
|
167
167
|
}
|
|
168
|
-
var p = 0, m = 5, h = 3, g =
|
|
168
|
+
var p = 0, m = 5, h = 3, g = .005, _ = {
|
|
169
|
+
feldspar: 1.15,
|
|
170
|
+
quartz: .75,
|
|
171
|
+
biotite: .3,
|
|
172
|
+
quartzShare: .3,
|
|
173
|
+
biotiteShare: .06
|
|
174
|
+
}, v = _.biotiteShare * _.biotite + _.quartzShare * _.quartz + (1 - _.quartzShare - _.biotiteShare) * _.feldspar, y = class a {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
169
175
|
constructor(e = {}) {
|
|
170
176
|
this.name = "terrain:rocks:" + ++p, this.options = {}, this.disposed = !1, this.rows = /* @__PURE__ */ new Map(), this.dirty = !0, this.uploaded = "", this.capacity = 0, this.source = "", this.shader = "", this.binding = 40, this.entries = [], this.look = {
|
|
171
177
|
...s,
|
|
172
178
|
...d(e)
|
|
173
179
|
}, this.look.mossColor = [...this.look.mossColor], this.look.algaeColor = [...this.look.algaeColor];
|
|
174
180
|
}
|
|
175
|
-
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("
|
|
181
|
+
static attach(e, t = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
176
182
|
let r = new a(t);
|
|
177
183
|
return n(e).use(r), r;
|
|
178
184
|
}
|
|
@@ -235,6 +241,12 @@ fn rockPlanar(p:vec3f,w:vec3f,f:f32,o:f32)->RockPlanar {
|
|
|
235
241
|
var r:RockPlanar;r.value=w.x*a.x+w.y*b.x+w.z*c.x;
|
|
236
242
|
r.gradient=f*(w.x*vec3f(0,a.y,a.z)+w.y*vec3f(b.y,0,b.z)+w.z*vec3f(c.y,c.z,0));return r;
|
|
237
243
|
}
|
|
244
|
+
fn graniteGrain(q:vec2f,o:f32)->f32 {
|
|
245
|
+
let s=q*0.6+o*31.0;let bend=vec2f(terrainNoise(s).x,terrainNoise(s+17.3).x)-0.5;
|
|
246
|
+
let c=bitcast<vec2u>(vec2i(floor(q+0.9*bend)));
|
|
247
|
+
let h=f32(terrainHash(c.x+terrainHash(c.y+bitcast<u32>(i32(o*977.0))))>>8u)*5.9604645e-8;
|
|
248
|
+
return select(select(${(_.feldspar / v).toFixed(4)},${(_.quartz / v).toFixed(4)},h<${(_.biotiteShare + _.quartzShare).toFixed(3)}),${(_.biotite / v).toFixed(4)},h<${_.biotiteShare.toFixed(3)});
|
|
249
|
+
}
|
|
238
250
|
struct RockShade {color:vec3f,rough:f32,normal:vec3f}
|
|
239
251
|
fn rockShade(p:vec3f,n0:vec3f,id:f32,footprint:f32)->RockShade {
|
|
240
252
|
let m0=rockRow(id,0u);let m1=rockRow(id,1u);let m2=rockRow(id,2u);
|
|
@@ -266,15 +278,15 @@ fn rockShade(p:vec3f,n0:vec3f,id:f32,footprint:f32)->RockShade {
|
|
|
266
278
|
|
|
267
279
|
|
|
268
280
|
var fabric=0.0;
|
|
269
|
-
let gf=select(21.0
|
|
281
|
+
let gf=select(21.0/g0.y,${(1 / g).toFixed(1)},m2.w==1.0);
|
|
270
282
|
let keepGrain=clamp(2.0-4.0*footprint*gf,0.0,1.0);
|
|
271
283
|
if(keepGrain>0.0){
|
|
272
284
|
let grain=rockPlanar(p,w,gf,m2.z+53.79);
|
|
273
285
|
if(m2.w==1.0){
|
|
274
|
-
|
|
275
|
-
let
|
|
276
|
-
fabric=(
|
|
277
|
-
grad+=grain.gradient*(0.
|
|
286
|
+
|
|
287
|
+
let mineral=w.x*graniteGrain(p.yz*gf,m2.z)+w.y*graniteGrain(p.xz*gf,m2.z+0.37)+w.z*graniteGrain(p.xy*gf,m2.z+0.71);
|
|
288
|
+
fabric=(mineral-1.0)*keepGrain;
|
|
289
|
+
grad+=grain.gradient*(0.04/gf)*keepGrain;
|
|
278
290
|
}else if(m2.w==2.0){
|
|
279
291
|
let pit=1.0-smoothstep(0.20,0.36,grain.value);
|
|
280
292
|
fabric=-0.29*pit*keepGrain;
|
|
@@ -365,4 +377,4 @@ fn rockShade(p:vec3f,n0:vec3f,id:f32,footprint:f32)->RockShade {
|
|
|
365
377
|
}
|
|
366
378
|
};
|
|
367
379
|
//#endregion
|
|
368
|
-
export { s as ROCK_LOOK_DEFAULTS, a as ROCK_TYPES,
|
|
380
|
+
export { s as ROCK_LOOK_DEFAULTS, a as ROCK_TYPES, y as RockSurface };
|
package/dist/rvt.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
-
const t=(engine,version="
|
|
3
|
+
const t=(engine,version="1.0.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
4
|
import { t as e } from "./lighting-layout-CaDtDa5G.js";
|
|
5
5
|
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
6
6
|
import { MargoError as n } from "@margoengine/math/errors";
|
|
@@ -103,7 +103,7 @@ function g(e, t, n, r, i, a = new Float64Array(i * 2), o) {
|
|
|
103
103
|
let s = n * r * 2 ** (i - 1), c = (e) => e - Math.floor(e / s) * s;
|
|
104
104
|
return [c(t[0]), c(t[2])];
|
|
105
105
|
}
|
|
106
|
-
var _ = 0, v = class s {coreRequirement=__margoCoreRequirement("
|
|
106
|
+
var _ = 0, v = class s {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
107
107
|
#e;
|
|
108
108
|
#t;
|
|
109
109
|
#n;
|
|
@@ -162,7 +162,7 @@ var _ = 0, v = class s {coreRequirement=__margoCoreRequirement("0.1.0",import.me
|
|
|
162
162
|
4096
|
|
163
163
|
].includes(this.size) || !Number.isInteger(this.levels) || this.levels < 1 || this.levels > 12 || !(this.#e >= 0 && this.#e <= 1) || !["rgba8unorm-srgb", "rgba16float"].includes(this.#t) || n.decals !== void 0 && typeof n.decals != "function") throw RangeError("Invalid terrain virtual texture; give texel above 0, size 256, 512, 1024, 2048 or 4096, levels 1..12, steep 0..1, format rgba8unorm-srgb or rgba16float and decals as a function.");
|
|
164
164
|
}
|
|
165
|
-
static attach(e, n, r = {}) {__margoAssertCore(e,__margoCoreRequirement("
|
|
165
|
+
static attach(e, n, r = {}) {__margoAssertCore(e,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
166
166
|
let i = new s(n, r);
|
|
167
167
|
return t(e).use(i), i;
|
|
168
168
|
}
|
|
@@ -414,7 +414,7 @@ var _ = 0, v = class s {coreRequirement=__margoCoreRequirement("0.1.0",import.me
|
|
|
414
414
|
}
|
|
415
415
|
if (r.queue.writeBuffer(this.#s, 0, this.#D), this.#m !== n.layout) {
|
|
416
416
|
let t = u(n.code) + p;
|
|
417
|
-
c && (t = t.replace("pad:vec2f}", "pad:vec2f,anchor:vec2i}").replace("let xz=(vec2f(w)+0.5)*e;", "let xz=terrainBake.pad+(vec2f(w-terrainBake.anchor)+0.5)*e;")), this.#j.size && (t = o + t.replace("var c=terrainColorAt(vec3f(xz.x,height(xz),xz.y),normal);", `var c=terrainVirtualSurface(terrainColorAt(vec3f(xz.x,height(xz),xz.y),normal),xz${c ? "+frame.anchor.xy" : ""},terrainBake.texel);`));
|
|
417
|
+
n.code.includes("fn terrainFarNormalAt(") && (t = t.replace("let s=textureLoad(normalMap,min(vec2i(uv*vec2f(d)),d-vec2i(1)),0).xyz*2.0-1.0;", "let s=terrainFarNormalAt(xz,textureLoad(normalMap,min(vec2i(uv*vec2f(d)),d-vec2i(1)),0).xyz*2.0-1.0);")), c && (t = t.replace("pad:vec2f}", "pad:vec2f,anchor:vec2i}").replace("let xz=(vec2f(w)+0.5)*e;", "let xz=terrainBake.pad+(vec2f(w-terrainBake.anchor)+0.5)*e;")), this.#j.size && (t = o + t.replace("var c=terrainColorAt(vec3f(xz.x,height(xz),xz.y),normal);", `var c=terrainVirtualSurface(terrainColorAt(vec3f(xz.x,height(xz),xz.y),normal),xz${c ? "+frame.anchor.xy" : ""},terrainBake.texel);`));
|
|
418
418
|
for (let e of this.#j) t = e.shader(t, 2);
|
|
419
419
|
let i = r.createShaderModule({ code: t }), a = [n.layout, this.#d];
|
|
420
420
|
this.#j.size && a.push(r.createBindGroupLayout({ entries: e(t, [...this.#j].flatMap((e) => e.bindings()), 2) })), this.#p = r.createRenderPipeline({
|
package/dist/scour.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type GeometryData } from '@margoengine/geometry/utils';
|
|
2
|
+
import type { HeightField } from '@margoengine/math';
|
|
3
|
+
import type { HeightfieldOptions } from './heightfield.js';
|
|
4
|
+
/** A rectangle on the ground: minimum x, minimum z, maximum x, maximum z, metres. */
|
|
5
|
+
export type ScourBounds = readonly [number, number, number, number];
|
|
6
|
+
/** A building standing on the sand. */
|
|
7
|
+
export interface ScourBuilding {
|
|
8
|
+
/** World x and z of the middle of its footprint. */
|
|
9
|
+
readonly center: readonly [number, number];
|
|
10
|
+
/** Footprint along x and z before `yaw`, metres, each above 0. */
|
|
11
|
+
readonly size: readonly [number, number];
|
|
12
|
+
/** Height to the ridge, metres, above 0. */
|
|
13
|
+
readonly height: number;
|
|
14
|
+
/** Turn about +y, radians, as a mesh's. Default 0. */
|
|
15
|
+
readonly yaw?: number;
|
|
16
|
+
}
|
|
17
|
+
/** A plant, tussock or other small porous obstacle that grows a shadow dune. */
|
|
18
|
+
export interface ScourPlant {
|
|
19
|
+
/** World x and z of its middle. */
|
|
20
|
+
readonly center: readonly [number, number];
|
|
21
|
+
/** Width across the wind at the ground, metres, above 0. */
|
|
22
|
+
readonly width: number;
|
|
23
|
+
}
|
|
24
|
+
/** Settings of {@link windScour}. */
|
|
25
|
+
export interface WindScourOptions {
|
|
26
|
+
readonly buildings?: readonly ScourBuilding[];
|
|
27
|
+
readonly plants?: readonly ScourPlant[];
|
|
28
|
+
/** Direction the wind blows toward, degrees from +x toward +z (as `duneField`). Default 90 (toward +z). */
|
|
29
|
+
readonly wind?: number;
|
|
30
|
+
/** Wind speed at 10 m, m/s, 0 or more. Default 11. */
|
|
31
|
+
readonly speed?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Speed at which sand starts to move, m/s: nothing changes at or below it, and the forms are full from 1.3 times it.
|
|
34
|
+
* Default 6.6 (0.25 mm sand, as `BlownSand`).
|
|
35
|
+
*/
|
|
36
|
+
readonly threshold?: number;
|
|
37
|
+
/** Depth of the moat along a building's windward wall as a share of its height, 0 to 0.075. Default 0.06. */
|
|
38
|
+
readonly scour?: number;
|
|
39
|
+
/** Height of the deposit horseshoe and tails as a share of a building's height, 0 to 0.3. Default 0.07. */
|
|
40
|
+
readonly deposit?: number;
|
|
41
|
+
/** Length of a building's tails past its leeward wall, in units of {@link scourScale}, 0.5 to 8. Default 3. */
|
|
42
|
+
readonly tail?: number;
|
|
43
|
+
/** Slope of a shadow dune's sides, degrees, 5 to 32 (the repose angle of dry sand). Default 20. */
|
|
44
|
+
readonly slope?: number;
|
|
45
|
+
}
|
|
46
|
+
/** The ground change round obstacles, from {@link windScour}. */
|
|
47
|
+
export interface WindScour {
|
|
48
|
+
/** Change of the ground's height at world (x, z), metres: 0 inside building footprints and away from obstacles. */
|
|
49
|
+
change(x: number, z: number): number;
|
|
50
|
+
/** One rectangle for each building, then each plant: outside all of them `change` is 0. */
|
|
51
|
+
readonly bounds: readonly ScourBounds[];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The length that deposition round a building scales with, metres: B = w^(2/3) h^(1/3), `width` across the wind and
|
|
55
|
+
* `height` (Poppema et al. 2021).
|
|
56
|
+
*/
|
|
57
|
+
export declare function scourScale(width: number, height: number): number;
|
|
58
|
+
/**
|
|
59
|
+
* Length of the shadow dune behind an obstacle `width` metres across in a wind of `speed` m/s: the CFD wake lengths of
|
|
60
|
+
* Hesp and Smyth 2017 for a 1 m nebkha, straight between their speeds and held outside them, in proportion to the
|
|
61
|
+
* width (Hesp 1981 found width the stronger control up to 0.2 m).
|
|
62
|
+
*/
|
|
63
|
+
export declare function shadowDuneLength(width: number, speed: number): number;
|
|
64
|
+
/**
|
|
65
|
+
* The change of ground height that wind leaves round `buildings` (a scour moat on the windward side and flanks, a
|
|
66
|
+
* deposit horseshoe and two tails) and behind `plants` (shadow dunes). Add `change(x, z)` to a ground height, and give
|
|
67
|
+
* the ground mesh vertices there with {@link refinedHeightfieldGeometry}: the moat of a 3 m shed is about 1.4 m wide.
|
|
68
|
+
* Sand is not conserved between the moat and the deposits.
|
|
69
|
+
* @throws RangeError for a bad option.
|
|
70
|
+
*/
|
|
71
|
+
export declare function windScour(options?: WindScourOptions): WindScour;
|
|
72
|
+
/** Options of {@link refinedHeightfieldGeometry}. */
|
|
73
|
+
export interface RefinedHeightfieldOptions extends HeightfieldOptions {
|
|
74
|
+
/** Rectangles whose cells are split. */
|
|
75
|
+
readonly regions: readonly ScourBounds[];
|
|
76
|
+
/** Splits per cell side inside the regions, an integer 1 to 16. Default 5. */
|
|
77
|
+
readonly subdivide?: number;
|
|
78
|
+
/**
|
|
79
|
+
* Added to the height at every vertex, such as `windScour(...).change`. `height` is read only at the coarse grid's
|
|
80
|
+
* vertices, so without this the refined surface is the coarse one. Default none.
|
|
81
|
+
*/
|
|
82
|
+
readonly add?: HeightField;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* `heightfieldGeometry` with the cells that touch `regions` split `subdivide` times a side, for small forms on a large
|
|
86
|
+
* ground. The split cells lie on the coarse triangles (so grass or anything else placed on the coarse grid still stands
|
|
87
|
+
* on the mesh) until `add` moves them, and the cells round a region are fanned to its edge vertices: no cracks.
|
|
88
|
+
* @throws RangeError for a bad option, a non-finite height or more than 4,000,000 vertices.
|
|
89
|
+
*/
|
|
90
|
+
export declare function refinedHeightfieldGeometry(height: HeightField, options: RefinedHeightfieldOptions): GeometryData;
|
package/dist/scour.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { computeVertexNormals as e } from "@margoengine/geometry/utils";
|
|
2
|
+
//#region src/scour.ts
|
|
3
|
+
function t(e, t) {
|
|
4
|
+
return Math.cbrt(e * e * t);
|
|
5
|
+
}
|
|
6
|
+
var n = [
|
|
7
|
+
[7.5, 5.6],
|
|
8
|
+
[10, 2.9],
|
|
9
|
+
[15, 1.6],
|
|
10
|
+
[20, 1.3]
|
|
11
|
+
];
|
|
12
|
+
function r(e, t) {
|
|
13
|
+
let r = 0;
|
|
14
|
+
for (; r < n.length - 2 && t > n[r + 1][0];) r++;
|
|
15
|
+
let [i, a] = n[r], [o, s] = n[r + 1], c = Math.min(1, Math.max(0, (t - i) / (o - i)));
|
|
16
|
+
return e * (a + (s - a) * c);
|
|
17
|
+
}
|
|
18
|
+
var i = (e, t, n) => {
|
|
19
|
+
let r = Math.min(1, Math.max(0, (n - e) / (t - e)));
|
|
20
|
+
return r * r * (3 - 2 * r);
|
|
21
|
+
}, a = Math.exp(-9), o = (e, t) => Math.max(0, Math.exp(-((e / t) ** 2)) - a) / (1 - a), s = (e, t, n, r) => {
|
|
22
|
+
if (!(Number.isFinite(t) && t >= n && t <= r)) throw RangeError(`windScour: ${e} must be from ${n} to ${r}.`);
|
|
23
|
+
return t;
|
|
24
|
+
}, c = (e, t) => {
|
|
25
|
+
if (!(t?.length === 2 && Number.isFinite(t[0]) && Number.isFinite(t[1]))) throw RangeError(`windScour: ${e} must be two finite numbers.`);
|
|
26
|
+
}, l = (e, t) => {
|
|
27
|
+
if (!(Number.isFinite(t) && t > 0)) throw RangeError(`windScour: ${e} must be above 0.`);
|
|
28
|
+
return t;
|
|
29
|
+
};
|
|
30
|
+
function u(e, t, n, r, i, a, o) {
|
|
31
|
+
let s = Infinity, c = Infinity, l = -Infinity, u = -Infinity;
|
|
32
|
+
for (let d of [i, a]) for (let i of [-o, o]) {
|
|
33
|
+
let a = e + d * n - i * r, o = t + d * r + i * n;
|
|
34
|
+
s = Math.min(s, a), l = Math.max(l, a), c = Math.min(c, o), u = Math.max(u, o);
|
|
35
|
+
}
|
|
36
|
+
return [
|
|
37
|
+
s,
|
|
38
|
+
c,
|
|
39
|
+
l,
|
|
40
|
+
u
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
function d(e = {}) {
|
|
44
|
+
let n = s("wind", e.wind ?? 90, -3600, 3600) * Math.PI / 180, a = s("speed", e.speed ?? 11, 0, 200), d = l("threshold", e.threshold ?? 6.6), f = s("scour", e.scour ?? .06, 0, .075), p = s("deposit", e.deposit ?? .07, 0, .3), m = s("tail", e.tail ?? 3, .5, 8), h = Math.tan(s("slope", e.slope ?? 20, 5, 32) * Math.PI / 180), g = Math.cos(n), _ = Math.sin(n), v = i(d, 1.3 * d, a), y = (e.buildings ?? []).map((e) => {
|
|
45
|
+
c("a building center", e.center), c("a building size", e.size);
|
|
46
|
+
let [n, r] = e.center, a = l("a building size", e.size[0]) / 2, d = l("a building size", e.size[1]) / 2, h = l("a building height", e.height), v = s("a building yaw", e.yaw ?? 0, -1e3, 1e3), y = Math.cos(v), b = Math.sin(v), x = Math.abs(-y * _ - b * g) * a + Math.abs(-b * _ + y * g) * d, S = Math.abs(y * g - b * _) * a + Math.abs(b * g + y * _) * d, C = t(2 * x, h), w = m * C, T = .65 * h, E = .18 * h, D = .45 * h, O = .22 * C, k = .45 * h, A = .12, j = [-1, 1].flatMap((e) => [-1, 1].map((t) => [n + y * e * a + b * t * d, r - b * e * a + y * t * d])), M = (e, t) => {
|
|
47
|
+
let i = e - n, o = t - r;
|
|
48
|
+
return Math.hypot(Math.max(0, Math.abs(y * i - b * o) - a), Math.max(0, Math.abs(b * i + y * o) - d));
|
|
49
|
+
}, N = x + T + A * w + 3 * Math.max(D, O), P = u(n, r, g, _, -S - T - 3 * D - x, S + w, N + S);
|
|
50
|
+
return {
|
|
51
|
+
bounds: P,
|
|
52
|
+
distance: M,
|
|
53
|
+
at(e, t) {
|
|
54
|
+
if (e < P[0] || e > P[2] || t < P[1] || t > P[3]) return 0;
|
|
55
|
+
let a = M(e, t);
|
|
56
|
+
if (a <= 0) return 0;
|
|
57
|
+
let s = e - n, c = t - r, l = s * g + c * _, u = Math.abs(c * g - s * _), d = 0;
|
|
58
|
+
if (a < k) {
|
|
59
|
+
let n = (1 - .7 * i(-S, S + .3 * h, l)) * (1 - i(S, S + .4 * h, l)), r = Math.max(...j.map(([n, r]) => Math.exp(-((e - n) ** 2 + (t - r) ** 2) / (.5 * h) ** 2)));
|
|
60
|
+
d -= Math.min(.075 * h, f * h * n * (1 + .5 * r)) * Math.sin(Math.PI * a / k) ** 2;
|
|
61
|
+
}
|
|
62
|
+
let m = Math.hypot(Math.max(0, -S - l), Math.max(0, u - x)), v = l < -S ? m - T : u - (x + T + A * Math.max(0, l - S)), y = i(-S, S + C, l), b = (v < 0 ? E : D) * (1 - y) + O * y, N = 1 - i(S + .3 * w, S + w, l);
|
|
63
|
+
return d += p * h * o(v, b) * N, d;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}), b = r(1, a), x = (e.plants ?? []).map((e) => {
|
|
67
|
+
c("a plant center", e.center);
|
|
68
|
+
let [t, n] = e.center, r = l("a plant width", e.width) / 2, i = b * 2 * r;
|
|
69
|
+
return {
|
|
70
|
+
bounds: u(t, n, g, _, -r, i, r),
|
|
71
|
+
at(e, a) {
|
|
72
|
+
let o = e - t, s = a - n, c = o * g + s * _, l = Math.abs(s * g - o * _), u = c < 0 ? r - Math.hypot(c, l) : r * (1 - c / i) - l;
|
|
73
|
+
return u > 0 ? h * u : 0;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}), S = /* @__PURE__ */ new Map();
|
|
77
|
+
for (let e of x) {
|
|
78
|
+
let [t, n, r, i] = e.bounds;
|
|
79
|
+
for (let a = Math.floor(n / 4); a <= Math.floor(i / 4); a++) for (let n = Math.floor(t / 4); n <= Math.floor(r / 4); n++) {
|
|
80
|
+
let t = `${n},${a}`, r = S.get(t);
|
|
81
|
+
r ? r.push(e) : S.set(t, [e]);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
bounds: [...y.map((e) => e.bounds), ...x.map((e) => e.bounds)],
|
|
86
|
+
change(e, t) {
|
|
87
|
+
if (v <= 0) return 0;
|
|
88
|
+
let n = 0;
|
|
89
|
+
for (let r of S.get(`${Math.floor(e / 4)},${Math.floor(t / 4)}`) ?? []) n = Math.max(n, r.at(e, t));
|
|
90
|
+
let r = Infinity;
|
|
91
|
+
for (let i of y) r = Math.min(r, i.distance(e, t)), n += i.at(e, t);
|
|
92
|
+
return r > 0 ? v * n * i(0, .3, r) : 0;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function f(t, n) {
|
|
97
|
+
let { size: r, resolution: i = 192, regions: a, subdivide: o = 5, add: s } = n, c = n.origin ?? [-r?.[0] / 2, -r?.[1] / 2];
|
|
98
|
+
if (typeof t != "function" || !(r?.length === 2 && r[0] > 0 && r[1] > 0 && Number.isFinite(r[0] + r[1])) || !(c.length === 2 && Number.isFinite(c[0] + c[1])) || !(Number.isInteger(i) && i >= 1 && i <= 1024) || !(Number.isInteger(o) && o >= 1 && o <= 16) || !Array.isArray(a) || !a.every((e) => e?.length === 4 && e.every(Number.isFinite) && e[2] >= e[0] && e[3] >= e[1]) || s !== void 0 && typeof s != "function") throw RangeError("refinedHeightfieldGeometry: pass a height function, a positive size, a resolution 1 to 1024, subdivide 1 to 16 and regions as [minX, minZ, maxX, maxZ].");
|
|
99
|
+
let l = r[0] / i, u = r[1] / i, d = new Uint8Array(i * i);
|
|
100
|
+
for (let [e, t, n, r] of a) {
|
|
101
|
+
let a = Math.max(0, Math.floor((e - c[0]) / l)), s = Math.min(i - 1, Math.ceil((n - c[0]) / l) - 1), f = Math.max(0, Math.floor((t - c[1]) / u)), p = Math.min(i - 1, Math.ceil((r - c[1]) / u) - 1);
|
|
102
|
+
for (let e = f; e <= p; e++) for (let t = a; t <= s; t++) d[e * i + t] = +(o > 1);
|
|
103
|
+
}
|
|
104
|
+
let f = (e, t) => e >= 0 && t >= 0 && e < i && t < i && d[t * i + e] === 1, p = [], m = [], h = [], g = (e, t, n) => {
|
|
105
|
+
let i = p.length / 3, a = n + (s ? s(e, t) : 0);
|
|
106
|
+
if (!Number.isFinite(Math.fround(a))) throw RangeError("Terrain height must be finite");
|
|
107
|
+
if (i >= 4e6) throw RangeError("refinedHeightfieldGeometry: over 4,000,000 vertices; split fewer cells.");
|
|
108
|
+
return p.push(e, a, t), m.push((e - c[0]) / r[0], 1 - (t - c[1]) / r[1]), i;
|
|
109
|
+
}, _ = new Float64Array((i + 1) * (i + 1));
|
|
110
|
+
for (let e = 0; e <= i; e++) for (let n = 0; n <= i; n++) {
|
|
111
|
+
let r = c[0] + n * l, a = c[1] + e * u, o = t(r, a);
|
|
112
|
+
_[e * (i + 1) + n] = o, g(r, a, o);
|
|
113
|
+
}
|
|
114
|
+
let v = /* @__PURE__ */ new Map(), y = i * o + 1, b = (e, t) => {
|
|
115
|
+
if (e % o === 0 && t % o === 0) return t / o * (i + 1) + e / o;
|
|
116
|
+
let n = t * y + e, r = v.get(n);
|
|
117
|
+
if (r === void 0) {
|
|
118
|
+
let a = Math.min(i - 1, Math.floor(e / o)), s = Math.min(i - 1, Math.floor(t / o)), d = e / o - a, f = t / o - s, p = s * (i + 1) + a, m = _[p], h = _[p + 1], y = _[p + i + 1], b = _[p + i + 2], x = d + f <= 1 ? m + d * (h - m) + f * (y - m) : b + (1 - d) * (y - b) + (1 - f) * (h - b);
|
|
119
|
+
r = g(c[0] + e * l / o, c[1] + t * u / o, x), v.set(n, r);
|
|
120
|
+
}
|
|
121
|
+
return r;
|
|
122
|
+
}, x = (e, t, n) => {
|
|
123
|
+
let r = b(e, t), i = b(e + n, t), a = b(e, t + n), o = b(e + n, t + n);
|
|
124
|
+
h.push(r, a, i, i, a, o);
|
|
125
|
+
};
|
|
126
|
+
for (let e = 0; e < i; e++) for (let t = 0; t < i; t++) {
|
|
127
|
+
let n = t * o, r = e * o;
|
|
128
|
+
if (f(t, e)) {
|
|
129
|
+
for (let e = 0; e < o; e++) for (let t = 0; t < o; t++) x(n + t, r + e, 1);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
let a = f(t - 1, e), s = f(t, e + 1), d = f(t + 1, e), p = f(t, e - 1);
|
|
133
|
+
if (!(a || s || d || p)) {
|
|
134
|
+
x(n, r, o);
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
let m = [], v = (e, t) => {
|
|
138
|
+
let n = e ? o : 1;
|
|
139
|
+
for (let e = 0; e < n; e++) m.push(t(e * o / n));
|
|
140
|
+
};
|
|
141
|
+
v(a, (e) => b(n, r + e)), v(s, (e) => b(n + e, r + o)), v(d, (e) => b(n + o, r + o - e)), v(p, (e) => b(n + o - e, r));
|
|
142
|
+
let y = g(c[0] + (t + .5) * l, c[1] + (e + .5) * u, (_[e * (i + 1) + t + 1] + _[(e + 1) * (i + 1) + t]) / 2);
|
|
143
|
+
for (let e = 0; e < m.length; e++) h.push(y, m[e], m[(e + 1) % m.length]);
|
|
144
|
+
}
|
|
145
|
+
return e({
|
|
146
|
+
positions: Float32Array.from(p),
|
|
147
|
+
normals: new Float32Array(p.length),
|
|
148
|
+
uvs: Float32Array.from(m),
|
|
149
|
+
indices: Uint32Array.from(h)
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
//#endregion
|
|
153
|
+
export { f as refinedHeightfieldGeometry, t as scourScale, r as shadowDuneLength, d as windScour };
|
package/dist/sculpt.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// @margo/core-built
|
|
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="
|
|
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/sculpt.ts
|
|
@@ -52,7 +52,7 @@ async function o(e, t, n, r, i) {
|
|
|
52
52
|
c.destroy();
|
|
53
53
|
}
|
|
54
54
|
}
|
|
55
|
-
var s = class s {coreRequirement=__margoCoreRequirement("
|
|
55
|
+
var s = class s {coreRequirement=__margoCoreRequirement("1.0.0",import.meta.url);
|
|
56
56
|
#e;
|
|
57
57
|
#t;
|
|
58
58
|
#n;
|
|
@@ -78,7 +78,7 @@ var s = class s {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url)
|
|
|
78
78
|
...this.#_(t)
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
|
-
static async attach(t, n, r = {}) {__margoAssertCore(t,__margoCoreRequirement("
|
|
81
|
+
static async attach(t, n, r = {}) {__margoAssertCore(t,__margoCoreRequirement("1.0.0",import.meta.url));
|
|
82
82
|
if (!n || typeof n.invalidate != "function") throw TypeError("Invalid terrain sculpt; pass a Terrain attached to the engine.");
|
|
83
83
|
let i = new s(n, r);
|
|
84
84
|
e(t).use(i);
|
package/dist/shader.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const terrainVertex = "\nstruct Frame {camera:mat4x4f,eye:vec4f,light:vec4f,radiance:vec4f,grid:vec4f,offset:vec4f,map:vec4f,heights:vec4f}\nstruct Layer {heightSlope:vec4f,params:vec4f}\n@group(0) @binding(0) var<uniform> frame:Frame;\n@group(0) @binding(1) var heightMap:texture_2d<f32>;\n@group(0) @binding(2) var normalMap:texture_2d<f32>;\n@group(0) @binding(3) var terrainLayers:texture_2d_array<f32>;\n@group(0) @binding(4) var terrainSampler:sampler;\n@group(0) @binding(5) var<storage,read> layerRules:array<Layer>;\nfn height(position:vec2f)->f32 {\n let dimensions=vec2i(textureDimensions(heightMap));let uv=clamp((position-frame.map.xy)/frame.map.zw,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);\n let a=mix(textureLoad(heightMap,lo,0).r,textureLoad(heightMap,vec2i(hi.x,lo.y),0).r,t.x);let b=mix(textureLoad(heightMap,vec2i(lo.x,hi.y),0).r,textureLoad(heightMap,hi,0).r,t.x);\n return mix(a,b,t.y)*frame.heights.x+frame.heights.y;\n}\nstruct Output {@builtin(position) @invariant clip:vec4f,@location(0) position:vec3f,@location(1) normal:vec3f,@location(2) color:vec3f,@location(3) material:vec4f}\n@vertex fn vertex(@location(0) grid:vec2f)->Output {\n let
|
|
1
|
+
export declare const terrainVertex = "\nstruct Frame {camera:mat4x4f,eye:vec4f,light:vec4f,radiance:vec4f,grid:vec4f,offset:vec4f,map:vec4f,heights:vec4f,ambient:vec4f}\nstruct Layer {heightSlope:vec4f,params:vec4f}\n@group(0) @binding(0) var<uniform> frame:Frame;\n@group(0) @binding(1) var heightMap:texture_2d<f32>;\n@group(0) @binding(2) var normalMap:texture_2d<f32>;\n@group(0) @binding(3) var terrainLayers:texture_2d_array<f32>;\n@group(0) @binding(4) var terrainSampler:sampler;\n@group(0) @binding(5) var<storage,read> layerRules:array<Layer>;\nfn height(position:vec2f)->f32 {\n let dimensions=vec2i(textureDimensions(heightMap));let uv=clamp((position-frame.map.xy)/frame.map.zw,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);\n let a=mix(textureLoad(heightMap,lo,0).r,textureLoad(heightMap,vec2i(hi.x,lo.y),0).r,t.x);let b=mix(textureLoad(heightMap,vec2i(lo.x,hi.y),0).r,textureLoad(heightMap,hi,0).r,t.x);\n return mix(a,b,t.y)*frame.heights.x+frame.heights.y;\n}\nfn terrainCatmull(t:f32)->vec4f {let t2=t*t;let t3=t2*t;return vec4f(-0.5*t3+t2-0.5*t,1.5*t3-2.5*t2+1.0,-1.5*t3+2.0*t2+0.5*t,0.5*t3-0.5*t2);}\nfn terrainCubic(position:vec2f)->f32 {\n let dimensions=vec2i(textureDimensions(heightMap));let uv=clamp((position-frame.map.xy)/frame.map.zw,vec2f(0),vec2f(1));let pixel=uv*vec2f(dimensions-vec2i(1));let lo=vec2i(floor(pixel));\n let wx=terrainCatmull(fract(pixel.x));let wy=terrainCatmull(fract(pixel.y));var h=0.0;\n for(var j=0;j<4;j++){for(var k=0;k<4;k++){h+=wx[k]*wy[j]*textureLoad(heightMap,clamp(lo+vec2i(k-1,j-1),vec2i(0),dimensions-vec2i(1)),0).r;}}\n return h*frame.heights.x+frame.heights.y;\n}\nfn terrainMorph(g:vec2f)->vec3f {\n let edge=max(abs(g.x),abs(g.y));let morph=clamp((edge-frame.grid.w*0.5+2)/2,0.0,1.0);\n return vec3f(mix(g,round(g*0.5)*2,morph),select(0.0,1.0,edge<=frame.grid.w*0.25));\n}\nfn terrainMorphXZ(m:vec3f)->vec2f {return frame.grid.xy+m.xy*frame.grid.z+frame.offset.xy*m.z;}\nstruct Output {@builtin(position) @invariant clip:vec4f,@location(0) position:vec3f,@location(1) normal:vec3f,@location(2) color:vec3f,@location(3) material:vec4f}\n@vertex fn vertex(@location(0) grid:vec2f)->Output {\n var m=terrainMorph(grid);var lift=0.0;let centre=fract(grid.x)!=0.0;\n if(centre){\n let a=terrainMorph(grid-vec2f(0.5));let b=terrainMorph(grid+vec2f(0.5,-0.5));let c=terrainMorph(grid+vec2f(-0.5,0.5));let d=terrainMorph(grid+vec2f(0.5));\n let ad=(height(terrainMorphXZ(a))+height(terrainMorphXZ(d)))*0.5;let bc=(height(terrainMorphXZ(b))+height(terrainMorphXZ(c)))*0.5;\n let r=terrainCubic(terrainMorphXZ((a+b+c+d)*0.25));let along=abs(r-ad)<abs(r-bc);\n m=select((b+c)*0.5,(a+d)*0.5,along);lift=select(bc,ad,along);\n }\n let aligned=m.xy;let inner=m.z;\n let xz=frame.grid.xy+aligned*frame.grid.z+frame.offset.xy*inner;let position=vec3f(xz.x,select(height(xz),lift,centre),xz.y);\n let step=frame.map.zw/max(vec2f(textureDimensions(heightMap))-1,vec2f(1));\n var normal=normalize(vec3f((height(xz-vec2f(step.x,0))-height(xz+vec2f(step.x,0)))/(2*step.x),1,(height(xz-vec2f(0,step.y))-height(xz+vec2f(0,step.y)))/(2*step.y)));\n if(frame.offset.w>0.5){let dimensions=vec2i(textureDimensions(normalMap));let uv=clamp((xz-frame.map.xy)/frame.map.zw,vec2f(0),vec2f(1));let sampled=textureLoad(normalMap,min(vec2i(uv*vec2f(dimensions)),dimensions-vec2i(1)),0).xyz*2-1;if(dot(sampled,sampled)>0.00001){normal=normalize(sampled);}}\n var o:Output;o.position=position;o.clip=frame.camera*vec4f(position,1);o.normal=normal;o.color=vec3f(1);o.material=vec4f(0.9,0,1,1);return o;\n}\n";
|
|
2
2
|
/**
|
|
3
3
|
* The heightmap covers `frame.map` (origin, size); the outer clipmap rings reach past it, where heights and weights are
|
|
4
4
|
* only the clamped edge texels (a flat shelf with stretched walls). Fragments there are dropped, so the sky shows past
|
|
@@ -32,6 +32,14 @@ export declare const terrainOutside = "if(any(i.position.xz<frame.map.xy)||any(i
|
|
|
32
32
|
* A slope estimated across one pixel holds only what that pixel resolves: mips average the height, not its slope, and
|
|
33
33
|
* the relief is up to half a repeat high, so the estimate stayed large at every distance and drew pixel grain on far and
|
|
34
34
|
* grazing ground. Past a footprint of 1/64 of a repeat it falls with the longer side of the pixel on the ground.
|
|
35
|
+
* Magnified, the height holds nothing finer than 1/128 of a repeat (`terrainReliefLuma`): the layer's mip with 128
|
|
36
|
+
* texels a repeat through a cubic B-spline over 4 x 4 exact texel loads (the basis of Sigg and Hadwiger 2005, "Fast
|
|
37
|
+
* Third-Order Texture Filtering", GPU Gems 2 chapter 20, without their bilinear shortcut). A height read texel by texel
|
|
38
|
+
* through the sampler is texel noise times half a repeat (root mean square slopes of 1.2 on the biomes' tundra and 5.3
|
|
39
|
+
* on rock at 256 texels over 4 m), one slope a texel, and the hardware's rounded filter weights, which the screen
|
|
40
|
+
* derivative drew as black and white speckle, facets and dotted lines along texel edges (KB-TUNDRA-GROUNDNOISE1). The
|
|
41
|
+
* B-spline's slope is continuous and its weights exact. Between footprints of 1/128 and 1/64 of a repeat it hands over
|
|
42
|
+
* to the colour sample's own mip; past 1/64 the height is the colour sample's, as before.
|
|
35
43
|
*/
|
|
36
44
|
export declare const surfaceStruct: (code: string) => string;
|
|
37
45
|
/**
|
|
@@ -42,13 +50,120 @@ export declare const surfaceStruct: (code: string) => string;
|
|
|
42
50
|
* distance and across a rebase, and a terrain moved with its camera draws the same picture. `Frame.anchor` is
|
|
43
51
|
* (shift.x - origin.x, shift.z - origin.z, shift.y, 0): local + anchor.xy is the terrain-relative XZ that cache surfaces,
|
|
44
52
|
* overlays and imagery read. Applied once; the text without the option is unchanged.
|
|
53
|
+
*
|
|
54
|
+
* The lit draw also works relative to the eye (Cozzi and Ring 2011, "3D Engine Design for Virtual Globes", chapter 5,
|
|
55
|
+
* "Rendering relative to eye"; Ohlarik 2008, "Precisions, Precisions", AGI): local positions are whatever the origin's
|
|
56
|
+
* shift leaves them, so a rebase in place re-rounds every float32 number derived from them (the clip position, each
|
|
57
|
+
* texture coordinate and its screen derivatives), and the texture unit's sub-texel and level rounding, finer with 16x
|
|
58
|
+
* anisotropy, flipped 0.1 percent of pixels by one level (KB-TERRAIN-REBASEROUND1). The vertex writes `Output.eye`,
|
|
59
|
+
* the vertex less the eye, from `Frame.view` (the ring's lattice less the eye, and the eye's local Y, computed in
|
|
60
|
+
* float64 on the CPU) plus the ring-relative grid offset, and the clip position is `camera * (eye, 0)` plus
|
|
61
|
+
* `Frame.column` (the camera matrix times the local eye, float64): none of these depend on where the origin sits, so a
|
|
62
|
+
* rebase in place draws the same numbers. `eyeShader` reads the textures there.
|
|
45
63
|
*/
|
|
64
|
+
export declare const terrainClip = "o.clip=frame.camera*vec4f(position,1);";
|
|
65
|
+
/** The clip line of the floating-origin vertex (`originShader`): what `surface-height` replaces with its own. */
|
|
66
|
+
export declare const originClip = "let terrainE=frame.view.xy+aligned*frame.grid.z+frame.offset.xy*inner;o.eye=vec3f(terrainE.x,position.y-frame.view.z,terrainE.y);o.clip=frame.camera*vec4f(o.eye,0)+frame.column;";
|
|
46
67
|
export declare const originShader: (code: string) => string;
|
|
47
|
-
/**
|
|
48
|
-
export declare const
|
|
68
|
+
/** What an entry adds at its start to read the textures relative to the eye (`eyeShader`). */
|
|
69
|
+
export declare const eyeEntry = "terrainEyeOn=true;terrainEyeAt=i.eye;terrainEyeLocal=i.position;";
|
|
70
|
+
/**
|
|
71
|
+
* The floating-origin lit shader's last step, after every entry and effect has edited the text (so the text they look
|
|
72
|
+
* for is the one they always saw): each layer sample's point and screen derivatives come from `Output.eye`, the vertex
|
|
73
|
+
* less the eye, plus the layer's `eyeWrap` (the eye less the terrain's anchor less whole macro repeats, float64 on the
|
|
74
|
+
* CPU) and `eyeTone`, in place of the local position plus `wrap` and `tone`. A sample point that is not the fragment's
|
|
75
|
+
* own (a displaced one) keeps its offset from it. The same texel either way (the two differ by whole repeats); only
|
|
76
|
+
* the rounding no longer moves with the origin. Shaders without `eyeEntry` (the virtual texture's bake) keep the local
|
|
77
|
+
* reading. Applied once.
|
|
78
|
+
*/
|
|
79
|
+
export declare const eyeShader: (code: string) => string;
|
|
80
|
+
/**
|
|
81
|
+
* The fragment's shading normal `n` and layer colour `base` (the SSR capture entry starts the same way). With `rock`
|
|
82
|
+
* (`TerrainOptions.rock`) the normal first takes the rock structure and the colour its tone. The layer colour (its
|
|
83
|
+
* triplanar projections and slope rules) still reads the surface's own normal: the structure only shades. Picking the
|
|
84
|
+
* projections by the structure's normal laid a bench's near-level normal or a facet's tilt on a steep face, so the
|
|
85
|
+
* texture was projected along the face and drawn as streaks a few metres long down every rock wall
|
|
86
|
+
* (KB-TERRAIN-ROCKSMEAR1, Above the Treeline look round 10).
|
|
87
|
+
*/
|
|
88
|
+
export declare const terrainShading: (surface: boolean, rock?: boolean) => string;
|
|
89
|
+
/** `TerrainOptions.rock` resolved: every field set; `debris` -1 for none, `map` the structure texture's binding or -1. */
|
|
90
|
+
export interface RockShaderOptions {
|
|
91
|
+
readonly slope: readonly [number, number];
|
|
92
|
+
readonly strata: number;
|
|
93
|
+
readonly ledges: number;
|
|
94
|
+
readonly dip: readonly [number, number];
|
|
95
|
+
readonly facets: number;
|
|
96
|
+
readonly relief: number;
|
|
97
|
+
readonly gullies: number;
|
|
98
|
+
readonly tone: number;
|
|
99
|
+
readonly undulation: number;
|
|
100
|
+
readonly debris: number;
|
|
101
|
+
readonly map: number;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* `TerrainOptions.rock`: the structure of bare rock at 10 to 100 m, where a heightmap of 4 to 30 m cells draws a steep
|
|
105
|
+
* face as one smooth plane. All of it fades in with slope between `slope[0]` and `slope[1]` degrees and fades out where
|
|
106
|
+
* a pixel covers a good share of its feature, so far ground keeps its mean and nothing aliases.
|
|
107
|
+
* - Ledges: beds `strata` metres thick (planes tilted by `dip`, their heights warped by value noise so they wander and
|
|
108
|
+
* break off), each a riser and a bench. The shading normal takes the bench's or the riser's slope analytically: the
|
|
109
|
+
* height gradient times A = 0.25 on the top 30 percent of a bed and B = (1 - 0.3 A) / 0.7 below it, so the mean slope
|
|
110
|
+
* is the heightmap's (layered terrain of hard and soft beds: Benes and Forsbach 2001, "Layered data representation
|
|
111
|
+
* for visual simulation of terrain erosion", SCCG; Peytavie et al. 2009, "Arches: a framework for modeling complex
|
|
112
|
+
* terrains", Computer Graphics Forum 28(2)). Benches collect loose stone (`debris`) and each bed its own tone.
|
|
113
|
+
* - Facets: joint blocks `facets` metres across and 2.4 times as tall (the steep joint set of granite and gneiss makes
|
|
114
|
+
* ribs and columns), and buttresses 4.1 times that at 0.7 the strength, the nearest of the points scattered one to a
|
|
115
|
+
* cell over the 27 cells around the position
|
|
116
|
+
* (Worley 1996, "A cellular texture basis function", SIGGRAPH), each tilting the normal by up to `relief` in its own
|
|
117
|
+
* direction, with dark joints where the nearest two points are about equally near (F2 - F1). The tilt and tone blend
|
|
118
|
+
* into the second-nearest cell's across the edge (half each where F2 = F1, all the cell's own by F2 - F1 = 0.14 of a
|
|
119
|
+
* cell plus two pixels), so an edge is a rounded joint, never a hard switch; relief, tone and joint shade fall to 0.3
|
|
120
|
+
* as the pixel footprint grows from 0.006 to 0.04 of a block (about 170 to 25 pixels across it) and to none by 0.4,
|
|
121
|
+
* the prefiltering of normal detail with its footprint (Toksvig 2005; Bruneton and Neyret 2012), so blocks 200 to
|
|
122
|
+
* 600 m away do not read as low-poly shards (Above the Treeline look round 5).
|
|
123
|
+
* - Gullies: with `TerrainTextures.structure`, its R (drainage, 0 to 1) is carved `gullies` metres deep into the
|
|
124
|
+
* shading normal by its central differences, darkened as wet, shaded rock, and on gentler ground below the faces its
|
|
125
|
+
* floors and its G (debris fans) carry `debris`. `@margoengine/biomes/hydrology` `drainageStructure` makes one from
|
|
126
|
+
* multiple-flow routing (Freeman 1991) and rockfall travel (Statham 1976).
|
|
127
|
+
* - Undulation: on gentle ground (1 - the slope fade) three octaves of value noise, 48, 20 and 8 m, with heights
|
|
128
|
+
* `undulation` times 1, 0.45 and 0.2 (a fractal relief, amplitude about proportional to wavelength, as measured
|
|
129
|
+
* for natural terrain: Mark and Aronson 1984, "Scale-dependent fractal dimensions of topographic surfaces",
|
|
130
|
+
* Mathematical Geology 16(7); periglacial forms of 5 to 50 m: Ballantyne 2018, "Periglacial Geomorphology", Wiley),
|
|
131
|
+
* tilt the shading normal by their analytic gradient and shade hollows darker and crests lighter (up to 12 percent
|
|
132
|
+
* for the largest), each faded out between footprints of 0.05 and 0.25 of its wavelength (Norton, Rockwood and
|
|
133
|
+
* Skolmoski 1982, clamping), so far ground keeps its mean.
|
|
134
|
+
* The pcg3d hash is Jarzynski and Olano 2020, "Hash Functions for GPU Rendering", JCGT 9(3). With `floatingOrigin`
|
|
135
|
+
* every feature reads terrain-relative positions. Without the option the shader is unchanged.
|
|
136
|
+
*/
|
|
137
|
+
export declare const rockFunctions: (o: RockShaderOptions) => string;
|
|
49
138
|
/** The rule-weighted fragment shader; `surface` adds `TerrainLayer.bump` and `macro`. A function, so a bundle that
|
|
50
139
|
* never draws a terrain keeps no shader text. */
|
|
51
|
-
export declare const terrainFragment: (surface?: boolean) => string;
|
|
140
|
+
export declare const terrainFragment: (surface?: boolean, rock?: RockShaderOptions) => string;
|
|
141
|
+
/**
|
|
142
|
+
* `TerrainOptions.patches`: the painted weights resolved into patches with crisp edges, as alpine ground is turf, scree
|
|
143
|
+
* and rock side by side rather than their average. Each layer whose weight is above 0 gets a height, `contrast` times a
|
|
144
|
+
* value-noise fBm of its own (cells of `size` metres, three octaves, quintic fade, Perlin 2002 "Improving Noise",
|
|
145
|
+
* SIGGRAPH; cell values from the pcg3d hash of Jarzynski and Olano 2020, "Hash Functions for GPU Rendering", JCGT 9(3)),
|
|
146
|
+
* added to its weight; only the layers within 0.05 x contrast of the highest sum keep weight, by how far they reach
|
|
147
|
+
* past that line (height-based splatting: Mishkinis 2013, "Advanced Terrain Texture Splatting", Gamasutra; noise-masked
|
|
148
|
+
* splats as in Andersson 2007, "Terrain Rendering in Frostbite Using Procedural Shader Splatting", SIGGRAPH course).
|
|
149
|
+
* Weights of 1 and 0 stay as painted. Octaves finer than half a pixel's footprint fade to their mean, and the patched
|
|
150
|
+
* weights hand back to the painted ones as the footprint grows from 0.3 to 0.8 of a cell, so far ground neither
|
|
151
|
+
* aliases nor turns into the strongest layer alone. With `floatingOrigin` the noise reads terrain-relative positions.
|
|
152
|
+
* `warp` displaces the noise's coordinates by a value-noise field of 2.6 `size` cells, up to 1.1 `warp` `size` each way
|
|
153
|
+
* (domain distortion: Perlin 1985, "An image synthesizer", SIGGRAPH; Ebert, Musgrave et al. 2003, "Texturing and
|
|
154
|
+
* Modeling: A Procedural Approach", ch. 16), and adds an octave of 3.3 `size` cells, so patches come in clusters and
|
|
155
|
+
* strands of several sizes instead of one blob size. `terraces` adds to each layer a sine of the height, `terraces`
|
|
156
|
+
* metres a cycle with its phase wandering by noise and shifted per layer: patches stretch along the contours, as turf
|
|
157
|
+
* banks and gravel strips do on a grazed or frost-heaved slope. Only on slopes of about 10 to 35 degrees, in stretches
|
|
158
|
+
* that come and go (a noise of 14 cycles), and it fades where a cycle covers under about 3 pixels.
|
|
159
|
+
*/
|
|
160
|
+
/** `TerrainOptions.patches` resolved: every field set (`warp` 0 and `terraces` 0 draw the plain patches). */
|
|
161
|
+
export interface PatchShaderOptions {
|
|
162
|
+
readonly size: number;
|
|
163
|
+
readonly contrast: number;
|
|
164
|
+
readonly warp: number;
|
|
165
|
+
readonly terraces: number;
|
|
166
|
+
}
|
|
52
167
|
/**
|
|
53
168
|
* The fragment shader with `TerrainTextures.weights` (`count` textures, 2 to 4): layer i's weight is channel i % 4 of
|
|
54
169
|
* weight texture i / 4, sampled bilinearly once per fragment over the heightmap's bounds, in place of the height and
|
|
@@ -58,4 +173,4 @@ export declare const terrainFragment: (surface?: boolean) => string;
|
|
|
58
173
|
* texel's RGB by its A before relief and macro tone. A function, so importing anything else from this package keeps no
|
|
59
174
|
* shader text.
|
|
60
175
|
*/
|
|
61
|
-
export declare const weightedFragment: (count: number, surface?: boolean, tint?: boolean) => string;
|
|
176
|
+
export declare const weightedFragment: (count: number, surface?: boolean, tint?: boolean, patches?: PatchShaderOptions, rock?: RockShaderOptions) => string;
|