webgpu-biome 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 +21 -0
- package/README.md +89 -2
- package/lib/canopy.d.ts +75 -0
- package/lib/canopy.d.ts.map +1 -0
- package/lib/canopy.js +295 -0
- package/lib/canopy.js.map +1 -0
- package/lib/climate.d.ts +68 -0
- package/lib/climate.d.ts.map +1 -0
- package/lib/climate.js +105 -0
- package/lib/climate.js.map +1 -0
- package/lib/detail.d.ts +55 -0
- package/lib/detail.d.ts.map +1 -0
- package/lib/detail.js +225 -0
- package/lib/detail.js.map +1 -0
- package/lib/ecotone.d.ts +166 -0
- package/lib/ecotone.d.ts.map +1 -0
- package/lib/ecotone.js +1007 -0
- package/lib/ecotone.js.map +1 -0
- package/lib/engine.d.ts +102 -0
- package/lib/engine.d.ts.map +1 -0
- package/lib/engine.js +773 -0
- package/lib/engine.js.map +1 -0
- package/lib/epiphytes.d.ts +19 -0
- package/lib/epiphytes.d.ts.map +1 -0
- package/lib/epiphytes.js +180 -0
- package/lib/epiphytes.js.map +1 -0
- package/lib/ground.d.ts +43 -0
- package/lib/ground.d.ts.map +1 -0
- package/lib/ground.js +450 -0
- package/lib/ground.js.map +1 -0
- package/lib/horizon.d.ts +20 -0
- package/lib/horizon.d.ts.map +1 -0
- package/lib/horizon.js +625 -0
- package/lib/horizon.js.map +1 -0
- package/lib/index.d.ts +21 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +18 -0
- package/lib/index.js.map +1 -0
- package/lib/particles.d.ts +12 -0
- package/lib/particles.d.ts.map +1 -0
- package/lib/particles.js +124 -0
- package/lib/particles.js.map +1 -0
- package/lib/post.d.ts +23 -0
- package/lib/post.d.ts.map +1 -0
- package/lib/post.js +225 -0
- package/lib/post.js.map +1 -0
- package/lib/recipe-specs.d.ts +51 -0
- package/lib/recipe-specs.d.ts.map +1 -0
- package/lib/recipe-specs.js +562 -0
- package/lib/recipe-specs.js.map +1 -0
- package/lib/recipes.d.ts +9 -0
- package/lib/recipes.d.ts.map +1 -0
- package/lib/recipes.js +279 -0
- package/lib/recipes.js.map +1 -0
- package/lib/sandbox.d.ts +183 -0
- package/lib/sandbox.d.ts.map +1 -0
- package/lib/sandbox.js +1060 -0
- package/lib/sandbox.js.map +1 -0
- package/lib/shadows.d.ts +71 -0
- package/lib/shadows.d.ts.map +1 -0
- package/lib/shadows.js +532 -0
- package/lib/shadows.js.map +1 -0
- package/lib/sky.d.ts +16 -0
- package/lib/sky.d.ts.map +1 -0
- package/lib/sky.js +132 -0
- package/lib/sky.js.map +1 -0
- package/lib/substrate.d.ts +124 -0
- package/lib/substrate.d.ts.map +1 -0
- package/lib/substrate.js +988 -0
- package/lib/substrate.js.map +1 -0
- package/lib/turf.d.ts +60 -0
- package/lib/turf.d.ts.map +1 -0
- package/lib/turf.js +278 -0
- package/lib/turf.js.map +1 -0
- package/lib/water.d.ts +41 -0
- package/lib/water.d.ts.map +1 -0
- package/lib/water.js +210 -0
- package/lib/water.js.map +1 -0
- package/package.json +63 -3
package/lib/substrate.js
ADDED
|
@@ -0,0 +1,988 @@
|
|
|
1
|
+
// Shared terrain, hydrology, and edaphic (soil) substrate for webgpu-biome.
|
|
2
|
+
//
|
|
3
|
+
// Computes surface normals, slope, aspect, multi-scale topographic concavity,
|
|
4
|
+
// Topographic Wetness Index (moisture), soil pH, fertility, and wind exposure.
|
|
5
|
+
// Implements `webgpu-grass`'s `HeightSource` using a single read-only SSBO (`vec4`)
|
|
6
|
+
// plus one RGBA8 `edaphicTex` so terrain height/normals cost zero sampled texture slots.
|
|
7
|
+
import * as THREE from "three/webgpu";
|
|
8
|
+
import { clamp, float, floor, fract, int, min, mix, normalize, storage, texture, uniform, vec2, vec3, vec4 } from "three/tsl";
|
|
9
|
+
export const TERRAIN_SHAPES = [
|
|
10
|
+
"woodland-meadow",
|
|
11
|
+
"coastal-dunes",
|
|
12
|
+
"montane-ridge",
|
|
13
|
+
"karst-gorge",
|
|
14
|
+
"glacial-tarn",
|
|
15
|
+
"palm-lagoon",
|
|
16
|
+
];
|
|
17
|
+
function clamp01(v) {
|
|
18
|
+
return v < 0 ? 0 : v > 1 ? 1 : v;
|
|
19
|
+
}
|
|
20
|
+
/** Deterministic integer-grid multiple-flow-direction (MFD) runoff accumulation across `heights`. */
|
|
21
|
+
function computeMfdFlowAccumulation(heights, nx, nz, cell) {
|
|
22
|
+
const n = nx * nz;
|
|
23
|
+
const flow = new Float32Array(n);
|
|
24
|
+
flow.fill(1);
|
|
25
|
+
if (nx < 3 || nz < 3)
|
|
26
|
+
return flow;
|
|
27
|
+
const order = new Int32Array(n);
|
|
28
|
+
for (let k = 0; k < n; k++)
|
|
29
|
+
order[k] = k;
|
|
30
|
+
order.sort((a, b) => heights[b] - heights[a]);
|
|
31
|
+
const diagDist = cell * Math.SQRT2;
|
|
32
|
+
const dxOff = [-1, 0, 1, -1, 1, -1, 0, 1];
|
|
33
|
+
const dzOff = [-1, -1, -1, 0, 0, 1, 1, 1];
|
|
34
|
+
const dists = [diagDist, cell, diagDist, cell, cell, diagDist, cell, diagDist];
|
|
35
|
+
const wArr = new Float64Array(8);
|
|
36
|
+
const idxArr = new Int32Array(8);
|
|
37
|
+
for (let oi = 0; oi < n; oi++) {
|
|
38
|
+
const k = order[oi];
|
|
39
|
+
const i = k % nx;
|
|
40
|
+
const j = (k - i) / nx;
|
|
41
|
+
if (i === 0 || i === nx - 1 || j === 0 || j === nz - 1)
|
|
42
|
+
continue;
|
|
43
|
+
const h0 = heights[k];
|
|
44
|
+
let wSum = 0;
|
|
45
|
+
let count = 0;
|
|
46
|
+
for (let d = 0; d < 8; d++) {
|
|
47
|
+
const ni = i + dxOff[d];
|
|
48
|
+
const nj = j + dzOff[d];
|
|
49
|
+
const nk = nj * nx + ni;
|
|
50
|
+
const drop = h0 - heights[nk];
|
|
51
|
+
if (drop > 1e-4) {
|
|
52
|
+
const slope = drop / dists[d];
|
|
53
|
+
const w = slope * slope;
|
|
54
|
+
idxArr[count] = nk;
|
|
55
|
+
wArr[count] = w;
|
|
56
|
+
wSum += w;
|
|
57
|
+
count++;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (wSum > 0) {
|
|
61
|
+
const fk = flow[k];
|
|
62
|
+
for (let c = 0; c < count; c++) {
|
|
63
|
+
flow[idxArr[c]] += fk * (wArr[c] / wSum);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return flow;
|
|
68
|
+
}
|
|
69
|
+
/** Deterministic CPU hydraulic droplet erosion, alluvial deposition, MFD channel incision, thermal talus, and micro-topography. */
|
|
70
|
+
function erodeAndSculptHeightfield(heights, nx, nz, x0, z0, cell, waterY, geo) {
|
|
71
|
+
const n = nx * nz;
|
|
72
|
+
if (nx < 16 || nz < 16)
|
|
73
|
+
return;
|
|
74
|
+
const width = (nx - 1) * cell;
|
|
75
|
+
const depth = (nz - 1) * cell;
|
|
76
|
+
const raw = heights.slice();
|
|
77
|
+
const bilerpAt = (fx, fz) => {
|
|
78
|
+
const cx = Math.min(nx - 1.002, Math.max(0.001, fx));
|
|
79
|
+
const cz = Math.min(nz - 1.002, Math.max(0.001, fz));
|
|
80
|
+
const i0 = Math.floor(cx);
|
|
81
|
+
const j0 = Math.floor(cz);
|
|
82
|
+
const u = cx - i0;
|
|
83
|
+
const v = cz - j0;
|
|
84
|
+
const k00 = j0 * nx + i0;
|
|
85
|
+
const h00 = heights[k00];
|
|
86
|
+
const h10 = heights[k00 + 1];
|
|
87
|
+
const h01 = heights[k00 + nx];
|
|
88
|
+
const h11 = heights[k00 + nx + 1];
|
|
89
|
+
const gx = ((h10 - h00) * (1 - v) + (h11 - h01) * v) / cell;
|
|
90
|
+
const gz = ((h01 - h00) * (1 - u) + (h11 - h10) * u) / cell;
|
|
91
|
+
const h = (h00 + (h10 - h00) * u) * (1 - v) + (h01 + (h11 - h01) * u) * v;
|
|
92
|
+
return { h, gx, gz };
|
|
93
|
+
};
|
|
94
|
+
// Pass A: Lagrangian hydraulic droplets carving upslope rills & depositing alluvial slope-toe fans.
|
|
95
|
+
let rngState = ((geo.seed ?? 1337) ^ 0x9e3779b9) >>> 0;
|
|
96
|
+
const nextRand = () => {
|
|
97
|
+
rngState = (Math.imul(rngState, 1664525) + 1013904223) >>> 0;
|
|
98
|
+
return rngState / 4294967296;
|
|
99
|
+
};
|
|
100
|
+
const erosionStrength = geo.erosionStrength ?? 1.0;
|
|
101
|
+
const numDroplets = Math.min(3400, Math.round(n * 0.032));
|
|
102
|
+
const inertia = 0.22;
|
|
103
|
+
const sedimentCapacityFactor = 2.6 * erosionStrength;
|
|
104
|
+
const minSlope = 0.014;
|
|
105
|
+
const erodeSpeed = 0.24;
|
|
106
|
+
const depositSpeed = 0.32;
|
|
107
|
+
const evaporate = 0.025;
|
|
108
|
+
const gravity = 4.0;
|
|
109
|
+
const borderCells = Math.max(2, Math.ceil(6 / cell));
|
|
110
|
+
if (erosionStrength > 0.01 && nx > borderCells * 2 + 2 && nz > borderCells * 2 + 2) {
|
|
111
|
+
for (let d = 0; d < numDroplets; d++) {
|
|
112
|
+
let px = borderCells + nextRand() * (nx - 1 - borderCells * 2);
|
|
113
|
+
let pz = borderCells + nextRand() * (nz - 1 - borderCells * 2);
|
|
114
|
+
let dirX = 0;
|
|
115
|
+
let dirZ = 0;
|
|
116
|
+
let speed = 1;
|
|
117
|
+
let water = 1;
|
|
118
|
+
let sediment = 0;
|
|
119
|
+
for (let step = 0; step < 34; step++) {
|
|
120
|
+
const cur = bilerpAt(px, pz);
|
|
121
|
+
if (cur.h < waterY - 0.1)
|
|
122
|
+
break;
|
|
123
|
+
dirX = dirX * inertia - cur.gx * (1 - inertia);
|
|
124
|
+
dirZ = dirZ * inertia - cur.gz * (1 - inertia);
|
|
125
|
+
const len = Math.hypot(dirX, dirZ);
|
|
126
|
+
if (len < 1e-5)
|
|
127
|
+
break;
|
|
128
|
+
dirX /= len;
|
|
129
|
+
dirZ /= len;
|
|
130
|
+
const nxPos = px + dirX;
|
|
131
|
+
const nzPos = pz + dirZ;
|
|
132
|
+
if (nxPos < borderCells || nxPos > nx - 1 - borderCells || nzPos < borderCells || nzPos > nz - 1 - borderCells)
|
|
133
|
+
break;
|
|
134
|
+
const next = bilerpAt(nxPos, nzPos);
|
|
135
|
+
const deltaH = next.h - cur.h;
|
|
136
|
+
const capacity = Math.max(-deltaH, minSlope) * speed * water * sedimentCapacityFactor;
|
|
137
|
+
const i0 = Math.floor(px);
|
|
138
|
+
const j0 = Math.floor(pz);
|
|
139
|
+
const u = px - i0;
|
|
140
|
+
const v = pz - j0;
|
|
141
|
+
const k00 = j0 * nx + i0;
|
|
142
|
+
if (sediment > capacity || deltaH > 0) {
|
|
143
|
+
const amount = deltaH > 0 ? Math.min(deltaH, sediment) : (sediment - capacity) * depositSpeed;
|
|
144
|
+
sediment -= amount;
|
|
145
|
+
heights[k00] += amount * (1 - u) * (1 - v);
|
|
146
|
+
heights[k00 + 1] += amount * u * (1 - v);
|
|
147
|
+
heights[k00 + nx] += amount * (1 - u) * v;
|
|
148
|
+
heights[k00 + nx + 1] += amount * u * v;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const rawAmount = Math.min((capacity - sediment) * erodeSpeed, -deltaH * 0.85 + 0.025, 0.14);
|
|
152
|
+
// Clamp cumulative local scour so steep features never carve unrealistic pits.
|
|
153
|
+
const centerDiff = raw[k00] - heights[k00];
|
|
154
|
+
const amount = centerDiff < 0.34 ? rawAmount : rawAmount * 0.15;
|
|
155
|
+
sediment += amount;
|
|
156
|
+
heights[k00] -= amount * 0.44;
|
|
157
|
+
heights[k00 - 1] -= amount * 0.14;
|
|
158
|
+
heights[k00 + 1] -= amount * 0.14;
|
|
159
|
+
heights[k00 - nx] -= amount * 0.14;
|
|
160
|
+
heights[k00 + nx] -= amount * 0.14;
|
|
161
|
+
}
|
|
162
|
+
speed = Math.sqrt(Math.max(0.05, speed * speed + Math.max(-deltaH, -2) * gravity));
|
|
163
|
+
water *= 1 - evaporate;
|
|
164
|
+
px = nxPos;
|
|
165
|
+
pz = nzPos;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// Pass B: D-infinity/MFD runoff channel incision carving continuous dendritic valley/gully flushes.
|
|
170
|
+
const channelIncise = geo.channelIncise ?? 0.095;
|
|
171
|
+
if (channelIncise > 0) {
|
|
172
|
+
const flow = computeMfdFlowAccumulation(heights, nx, nz, cell);
|
|
173
|
+
const inciseBuf = new Float32Array(n);
|
|
174
|
+
for (let j = 2; j < nz - 2; j++) {
|
|
175
|
+
for (let i = 2; i < nx - 2; i++) {
|
|
176
|
+
const k = j * nx + i;
|
|
177
|
+
if (heights[k] <= waterY + 0.06)
|
|
178
|
+
continue;
|
|
179
|
+
const f = flow[k];
|
|
180
|
+
if (f <= 12)
|
|
181
|
+
continue;
|
|
182
|
+
const dx = (heights[k + 1] - heights[k - 1]) / (2 * cell);
|
|
183
|
+
const dz = (heights[k + nx] - heights[k - nx]) / (2 * cell);
|
|
184
|
+
const sl = Math.hypot(dx, dz);
|
|
185
|
+
const slopeGate = smoothstepNum(0.04, 0.22, sl);
|
|
186
|
+
inciseBuf[k] = Math.min(0.42, channelIncise * slopeGate * Math.log1p((f - 12) * 0.09));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// Slight lateral cross-diffusion gives natural U/V flush shoulder transitions.
|
|
190
|
+
for (let j = 2; j < nz - 2; j++) {
|
|
191
|
+
for (let i = 2; i < nx - 2; i++) {
|
|
192
|
+
const k = j * nx + i;
|
|
193
|
+
const avg = (inciseBuf[k] * 4 + inciseBuf[k - 1] + inciseBuf[k + 1] + inciseBuf[k - nx] + inciseBuf[k + nx]) * 0.125;
|
|
194
|
+
heights[k] -= avg;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
// Pass C: 3-pass thermal talus relaxation building smooth angle-of-repose scree aprons without erasing sheer cliffs.
|
|
199
|
+
const talusTan = Math.tan(((geo.talusDeg ?? 36) * Math.PI) / 180) * cell;
|
|
200
|
+
const cliffPreserveTan = Math.tan((48 * Math.PI) / 180) * cell;
|
|
201
|
+
const dx4 = [-1, 1, 0, 0];
|
|
202
|
+
const dz4 = [0, 0, -1, 1];
|
|
203
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
204
|
+
for (let j = 2; j < nz - 2; j++) {
|
|
205
|
+
for (let i = 2; i < nx - 2; i++) {
|
|
206
|
+
const k = j * nx + i;
|
|
207
|
+
const h = heights[k];
|
|
208
|
+
let maxDiff = 0;
|
|
209
|
+
let targetK = -1;
|
|
210
|
+
for (let d = 0; d < 4; d++) {
|
|
211
|
+
const nk = (j + dz4[d]) * nx + (i + dx4[d]);
|
|
212
|
+
const diff = h - heights[nk];
|
|
213
|
+
if (diff > maxDiff) {
|
|
214
|
+
maxDiff = diff;
|
|
215
|
+
targetK = nk;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (targetK >= 0 && maxDiff > talusTan && maxDiff < cliffPreserveTan) {
|
|
219
|
+
const shift = (maxDiff - talusTan) * 0.18;
|
|
220
|
+
heights[k] -= shift;
|
|
221
|
+
heights[targetK] += shift;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// Pass D: Sub-meter geomorphic micro-topography (undercut shoreline bank scarps, slope terracettes, tussock/frost hummocks).
|
|
227
|
+
const scarpAmp = geo.bankScarpAmp ?? 0;
|
|
228
|
+
const terrAmp = geo.terracetteAmp ?? 0;
|
|
229
|
+
const humAmp = geo.hummockAmp ?? 0;
|
|
230
|
+
for (let j = 1; j < nz - 1; j++) {
|
|
231
|
+
const z = z0 + j * cell;
|
|
232
|
+
for (let i = 1; i < nx - 1; i++) {
|
|
233
|
+
const x = x0 + i * cell;
|
|
234
|
+
const k = j * nx + i;
|
|
235
|
+
const h = heights[k];
|
|
236
|
+
let delta = 0;
|
|
237
|
+
if (scarpAmp > 0 && h > waterY - 0.14 && h < waterY + 0.72) {
|
|
238
|
+
const along = 0.5 + 0.30 * Math.sin(x * 0.085 + z * 0.068) + 0.20 * Math.cos(x * 0.21 - z * 0.17);
|
|
239
|
+
const scarpSection = smoothstepNum(0.56, 0.76, along);
|
|
240
|
+
if (scarpSection > 0.01) {
|
|
241
|
+
// Replace uniform bank ramp with crisp turf/peat bank cutback shelf (~0.3 m step above waterY).
|
|
242
|
+
const rel = h - waterY;
|
|
243
|
+
const shelfLift = smoothstepNum(0.06, 0.38, rel) * (1 - smoothstepNum(0.45, 0.72, rel));
|
|
244
|
+
const undercutToe = smoothstepNum(-0.12, 0.02, rel) * (1 - smoothstepNum(0.02, 0.14, rel));
|
|
245
|
+
delta += scarpAmp * scarpSection * (shelfLift * 0.95 - undercutToe * 0.52);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (terrAmp > 0 || humAmp > 0) {
|
|
249
|
+
const dx = (raw[k + 1] - raw[k - 1]) / (2 * cell);
|
|
250
|
+
const dz = (raw[k + nx] - raw[k - nx]) / (2 * cell);
|
|
251
|
+
const sl = Math.hypot(dx, dz);
|
|
252
|
+
const sDeg = (Math.atan(sl) * 180) / Math.PI;
|
|
253
|
+
if (terrAmp > 0 && sDeg > 8.5 && sDeg < 29 && h > waterY + 0.35) {
|
|
254
|
+
const terrWeight = smoothstepNum(8.5, 13, sDeg) * (1 - smoothstepNum(24, 30, sDeg));
|
|
255
|
+
delta += terrAmp * terrWeight * Math.sin(h * 4.2 + 0.55 * Math.sin(x * 0.13 + z * 0.11));
|
|
256
|
+
}
|
|
257
|
+
if (humAmp > 0 && sDeg < 13 && h > waterY + 0.1) {
|
|
258
|
+
const flatGate = 1 - smoothstepNum(6, 13, sDeg);
|
|
259
|
+
const mound = 0.5 +
|
|
260
|
+
0.5 * Math.sin(x * 1.45 + 0.7 * Math.cos(z * 0.6)) * Math.cos(z * 1.35 + 0.6 * Math.sin(x * 0.5));
|
|
261
|
+
delta += humAmp * flatGate * (mound - 0.35);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
heights[k] = h + delta;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// Smoothly taper all terrain deltas to zero over 14 m from perimeter to guarantee exact C0 horizon continuity.
|
|
268
|
+
const fadeMargin = Math.min(14, Math.min(width, depth) * 0.14);
|
|
269
|
+
for (let j = 0; j < nz; j++) {
|
|
270
|
+
const zDist = Math.min(j, nz - 1 - j) * cell;
|
|
271
|
+
for (let i = 0; i < nx; i++) {
|
|
272
|
+
const xDist = Math.min(i, nx - 1 - i) * cell;
|
|
273
|
+
const edgeFade = smoothstepNum(0, fadeMargin, Math.min(xDist, zDist));
|
|
274
|
+
const k = j * nx + i;
|
|
275
|
+
heights[k] = raw[k] + (heights[k] - raw[k]) * edgeFade;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
function recomputeLocalTerrainDerivatives(grid, iMin, iMax, jMin, jMax) {
|
|
280
|
+
const { nx, nz, cell, heights, normals, slopeDeg } = grid;
|
|
281
|
+
const at = (i, j) => {
|
|
282
|
+
const ci = Math.min(nx - 1, Math.max(0, i));
|
|
283
|
+
const cj = Math.min(nz - 1, Math.max(0, j));
|
|
284
|
+
return heights[cj * nx + ci];
|
|
285
|
+
};
|
|
286
|
+
const j0 = Math.max(0, jMin);
|
|
287
|
+
const j1 = Math.min(nz - 1, jMax);
|
|
288
|
+
const i0 = Math.max(0, iMin);
|
|
289
|
+
const i1 = Math.min(nx - 1, iMax);
|
|
290
|
+
for (let j = j0; j <= j1; j++) {
|
|
291
|
+
for (let i = i0; i <= i1; i++) {
|
|
292
|
+
const k = j * nx + i;
|
|
293
|
+
const dx = (at(i + 1, j) - at(i - 1, j)) / (2 * cell);
|
|
294
|
+
const dz = (at(i, j + 1) - at(i, j - 1)) / (2 * cell);
|
|
295
|
+
const len = Math.hypot(dx, 1, dz);
|
|
296
|
+
const ny = 1 / len;
|
|
297
|
+
normals[k * 3] = -dx / len;
|
|
298
|
+
normals[k * 3 + 1] = ny;
|
|
299
|
+
normals[k * 3 + 2] = -dz / len;
|
|
300
|
+
slopeDeg[k] = (Math.acos(Math.min(1, Math.max(-1, ny))) * 180) / Math.PI;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Stamp subtle veteran tree root-plate heave mounds (`trunkRadius >= 0.18 m`) into terrain heights,
|
|
306
|
+
* refresh local normals/slope, and lift trees + nearby terrestrial flora so bases ride naturally on the heave.
|
|
307
|
+
*/
|
|
308
|
+
export function applyRootHeaveMounds(grid, trees, treeCount, flora, groundFloraCount = 0, waterLevel) {
|
|
309
|
+
if (treeCount <= 0)
|
|
310
|
+
return;
|
|
311
|
+
const TREE_STRIDE = 10;
|
|
312
|
+
const FLORA_STRIDE = 8;
|
|
313
|
+
const { nx, nz, x0, z0, cell, width, depth, heights } = grid;
|
|
314
|
+
const margin = Math.min(12, Math.min(width, depth) * 0.12);
|
|
315
|
+
// Save pre-heave heights to look up delta for terrestrial flora.
|
|
316
|
+
const preHeights = flora && groundFloraCount > 0 ? heights.slice() : null;
|
|
317
|
+
const bilerpFrom = (arr, x, z) => {
|
|
318
|
+
const fx = Math.min(nx - 1.001, Math.max(0, (x - x0) / cell));
|
|
319
|
+
const fz = Math.min(nz - 1.001, Math.max(0, (z - z0) / cell));
|
|
320
|
+
const i0 = Math.floor(fx);
|
|
321
|
+
const j0 = Math.floor(fz);
|
|
322
|
+
const i1 = Math.min(nx - 1, i0 + 1);
|
|
323
|
+
const j1 = Math.min(nz - 1, j0 + 1);
|
|
324
|
+
const tx = fx - i0;
|
|
325
|
+
const tz = fz - j0;
|
|
326
|
+
return ((arr[j0 * nx + i0] + (arr[j0 * nx + i1] - arr[j0 * nx + i0]) * tx) * (1 - tz) +
|
|
327
|
+
(arr[j1 * nx + i0] + (arr[j1 * nx + i1] - arr[j1 * nx + i0]) * tx) * tz);
|
|
328
|
+
};
|
|
329
|
+
for (let t = 0; t < treeCount; t++) {
|
|
330
|
+
const o = t * TREE_STRIDE;
|
|
331
|
+
const tx = trees[o];
|
|
332
|
+
const tz = trees[o + 2];
|
|
333
|
+
const treeH = trees[o + 4];
|
|
334
|
+
const trunkR = trees[o + 7];
|
|
335
|
+
if (!Number.isFinite(trunkR) || trunkR < 0.18 || (Number.isFinite(treeH) && treeH < 6.5))
|
|
336
|
+
continue;
|
|
337
|
+
const dEdge = Math.min(tx - x0, x0 + width - tx, tz - z0, z0 + depth - tz);
|
|
338
|
+
const edgeFade = smoothstepNum(0, margin, dEdge);
|
|
339
|
+
if (edgeFade <= 0.01)
|
|
340
|
+
continue;
|
|
341
|
+
const heaveR = Math.max(3.6, trunkR * 6.5 + 1.8);
|
|
342
|
+
const amp = Math.min(0.115, trunkR * 0.24) * edgeFade;
|
|
343
|
+
const flatCoreR = Math.max(1.35, trunkR * 2.8);
|
|
344
|
+
const iMin = Math.max(1, Math.floor((tx - heaveR - x0) / cell));
|
|
345
|
+
const iMax = Math.min(nx - 2, Math.ceil((tx + heaveR - x0) / cell));
|
|
346
|
+
const jMin = Math.max(1, Math.floor((tz - heaveR - z0) / cell));
|
|
347
|
+
const jMax = Math.min(nz - 2, Math.ceil((tz + heaveR - z0) / cell));
|
|
348
|
+
for (let j = jMin; j <= jMax; j++) {
|
|
349
|
+
const gz = z0 + j * cell;
|
|
350
|
+
for (let i = iMin; i <= iMax; i++) {
|
|
351
|
+
const gx = x0 + i * cell;
|
|
352
|
+
const dx = gx - tx;
|
|
353
|
+
const dz = gz - tz;
|
|
354
|
+
const r = Math.hypot(dx, dz);
|
|
355
|
+
if (r >= heaveR)
|
|
356
|
+
continue;
|
|
357
|
+
// Flat-topped gentle collar plateau inside `flatCoreR` so 1 m ground triangles stay planar beneath trunks.
|
|
358
|
+
const tFall = r <= flatCoreR ? 0 : (r - flatCoreR) / Math.max(1e-4, heaveR - flatCoreR);
|
|
359
|
+
const hAdd = amp * (0.5 + 0.5 * Math.cos(Math.PI * tFall));
|
|
360
|
+
heights[j * nx + i] += hAdd;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
recomputeLocalTerrainDerivatives(grid, iMin - 1, iMax + 1, jMin - 1, jMax + 1);
|
|
364
|
+
trees[o + 1] = bilerpFrom(heights, tx, tz);
|
|
365
|
+
}
|
|
366
|
+
if (flora && preHeights && groundFloraCount > 0) {
|
|
367
|
+
for (let f = 0; f < groundFloraCount; f++) {
|
|
368
|
+
const fo = f * FLORA_STRIDE;
|
|
369
|
+
const fx = flora[fo];
|
|
370
|
+
const fy = flora[fo + 1];
|
|
371
|
+
const fz = flora[fo + 2];
|
|
372
|
+
// Skip aquatic/emergent flora standing in water.
|
|
373
|
+
if (waterLevel !== undefined && fy < waterLevel + 0.03)
|
|
374
|
+
continue;
|
|
375
|
+
const dh = bilerpFrom(heights, fx, fz) - bilerpFrom(preHeights, fx, fz);
|
|
376
|
+
if (Math.abs(dh) > 1e-4) {
|
|
377
|
+
flora[fo + 1] = fy + dh;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
/** Bake terrain heights, normals, concavity, TWI moisture, soil pH, fertility, and exposure. */
|
|
383
|
+
export function bakeSubstrateGrid(opts) {
|
|
384
|
+
const { x0, z0, nx, nz, cell, heightAt } = opts;
|
|
385
|
+
const n = nx * nz;
|
|
386
|
+
const width = (nx - 1) * cell;
|
|
387
|
+
const depth = (nz - 1) * cell;
|
|
388
|
+
const heights = new Float32Array(n);
|
|
389
|
+
const normals = new Float32Array(n * 3);
|
|
390
|
+
const slopeDeg = new Float32Array(n);
|
|
391
|
+
const concavity = new Float32Array(n);
|
|
392
|
+
const moisture = new Float32Array(n);
|
|
393
|
+
const soilPh = new Float32Array(n);
|
|
394
|
+
const fertility = new Float32Array(n);
|
|
395
|
+
const exposure = new Float32Array(n);
|
|
396
|
+
for (let j = 0; j < nz; j++) {
|
|
397
|
+
const z = z0 + j * cell;
|
|
398
|
+
for (let i = 0; i < nx; i++) {
|
|
399
|
+
const x = x0 + i * cell;
|
|
400
|
+
heights[j * nx + i] = heightAt(x, z);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
const at = (i, j) => {
|
|
404
|
+
const ci = Math.min(nx - 1, Math.max(0, i));
|
|
405
|
+
const cj = Math.min(nz - 1, Math.max(0, j));
|
|
406
|
+
return heights[cj * nx + ci];
|
|
407
|
+
};
|
|
408
|
+
// Ring offsets for 3 m (micro-hollow) and 11 m (macro-valley) concavity.
|
|
409
|
+
const rSmall = Math.max(1, Math.round(3 / cell));
|
|
410
|
+
const rLarge = Math.max(rSmall + 1, Math.round(11 / cell));
|
|
411
|
+
const ringAvg = (i, j, r) => {
|
|
412
|
+
const d = Math.max(1, Math.round(r * 0.7071));
|
|
413
|
+
return ((at(i - r, j) +
|
|
414
|
+
at(i + r, j) +
|
|
415
|
+
at(i, j - r) +
|
|
416
|
+
at(i, j + r) +
|
|
417
|
+
at(i - d, j - d) +
|
|
418
|
+
at(i + d, j - d) +
|
|
419
|
+
at(i - d, j + d) +
|
|
420
|
+
at(i + d, j + d)) *
|
|
421
|
+
0.125);
|
|
422
|
+
};
|
|
423
|
+
// Find baseline height range when waterTableY is omitted.
|
|
424
|
+
let minH = Infinity;
|
|
425
|
+
let maxH = -Infinity;
|
|
426
|
+
for (let k = 0; k < n; k++) {
|
|
427
|
+
const h = heights[k];
|
|
428
|
+
if (h < minH)
|
|
429
|
+
minH = h;
|
|
430
|
+
if (h > maxH)
|
|
431
|
+
maxH = h;
|
|
432
|
+
}
|
|
433
|
+
const waterY = opts.waterTableY ?? minH - 0.5;
|
|
434
|
+
if (opts.geomorphology) {
|
|
435
|
+
erodeAndSculptHeightfield(heights, nx, nz, x0, z0, cell, waterY, opts.geomorphology);
|
|
436
|
+
minH = Infinity;
|
|
437
|
+
maxH = -Infinity;
|
|
438
|
+
for (let k = 0; k < n; k++) {
|
|
439
|
+
const h = heights[k];
|
|
440
|
+
if (h < minH)
|
|
441
|
+
minH = h;
|
|
442
|
+
if (h > maxH)
|
|
443
|
+
maxH = h;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const spanH = Math.max(1, maxH - minH);
|
|
447
|
+
const flowAcc = computeMfdFlowAccumulation(heights, nx, nz, cell);
|
|
448
|
+
for (let j = 0; j < nz; j++) {
|
|
449
|
+
const z = z0 + j * cell;
|
|
450
|
+
for (let i = 0; i < nx; i++) {
|
|
451
|
+
const x = x0 + i * cell;
|
|
452
|
+
const k = j * nx + i;
|
|
453
|
+
const h = heights[k];
|
|
454
|
+
const dx = (at(i + 1, j) - at(i - 1, j)) / (2 * cell);
|
|
455
|
+
const dz = (at(i, j + 1) - at(i, j - 1)) / (2 * cell);
|
|
456
|
+
const len = Math.hypot(dx, 1, dz);
|
|
457
|
+
const ny = 1 / len;
|
|
458
|
+
normals[k * 3] = -dx / len;
|
|
459
|
+
normals[k * 3 + 1] = ny;
|
|
460
|
+
normals[k * 3 + 2] = -dz / len;
|
|
461
|
+
const sDeg = (Math.acos(Math.min(1, Math.max(-1, ny))) * 180) / Math.PI;
|
|
462
|
+
slopeDeg[k] = sDeg;
|
|
463
|
+
// Positive concavity = hollow / valley (neighbours higher than point).
|
|
464
|
+
const cSmall = ringAvg(i, j, rSmall) - h;
|
|
465
|
+
const cLarge = ringAvg(i, j, rLarge) - h;
|
|
466
|
+
const conc = 0.45 * cSmall + 0.55 * cLarge;
|
|
467
|
+
concavity[k] = conc;
|
|
468
|
+
// Topographic Wetness Index: multi-flow runoff accumulation + concavity + gentle slopes + water-table proximity.
|
|
469
|
+
const slopeDrain = Math.pow(Math.max(0, ny), 2.5);
|
|
470
|
+
const concTerm = 0.5 + 0.5 * Math.tanh(conc * 0.45);
|
|
471
|
+
const flowTerm = clamp01(Math.log1p(flowAcc[k] * cell) / 5.4);
|
|
472
|
+
const elevAboveWater = Math.max(0, h - waterY);
|
|
473
|
+
const elevFactor = 1 - clamp01(elevAboveWater / (spanH * 0.85));
|
|
474
|
+
const bias = opts.moistureBias ? opts.moistureBias(x, z, h, conc, sDeg) : 0;
|
|
475
|
+
const twi = clamp01(0.32 * concTerm + 0.24 * flowTerm + 0.28 * elevFactor + 0.16 * slopeDrain - Math.max(0, (sDeg - 22) * 0.015) + bias);
|
|
476
|
+
moisture[k] = twi;
|
|
477
|
+
soilPh[k] = clamp01(opts.basePhAt ? opts.basePhAt(x, z, h) : 0.55 + 0.15 * concTerm - 0.12 * clamp01((h - minH) / spanH));
|
|
478
|
+
fertility[k] = clamp01(opts.baseFertilityAt ? opts.baseFertilityAt(x, z, h) : 0.35 + 0.45 * twi - Math.max(0, (sDeg - 20) * 0.018));
|
|
479
|
+
exposure[k] = clamp01(opts.exposureAt ? opts.exposureAt(x, z, h, sDeg) : 0.25 + 0.45 * clamp01((h - minH) / spanH) + 0.3 * (1 - concTerm));
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return {
|
|
483
|
+
nx,
|
|
484
|
+
nz,
|
|
485
|
+
x0,
|
|
486
|
+
z0,
|
|
487
|
+
cell,
|
|
488
|
+
width,
|
|
489
|
+
depth,
|
|
490
|
+
heights,
|
|
491
|
+
normals,
|
|
492
|
+
slopeDeg,
|
|
493
|
+
concavity,
|
|
494
|
+
moisture,
|
|
495
|
+
soilPh,
|
|
496
|
+
fertility,
|
|
497
|
+
exposure,
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
function smoothstepNum(e0, e1, x) {
|
|
501
|
+
const span = e1 - e0;
|
|
502
|
+
const t = Math.abs(span) < 1e-6 ? (x >= e1 ? 1 : 0) : clamp01((x - e0) / span);
|
|
503
|
+
return t * t * (3 - 2 * t);
|
|
504
|
+
}
|
|
505
|
+
/** Continuous analytical height function for all 6 preset terrain shapes (with slope-aligned micro-erosion). */
|
|
506
|
+
export function evalTerrainShapeHeight(shape, x, z) {
|
|
507
|
+
if (shape === "coastal-dunes") {
|
|
508
|
+
const coastSlope = Math.max(0, (z + 80) * 0.018);
|
|
509
|
+
const ridge1 = 6.2 * Math.exp(-Math.pow((z + 32 - 18 * Math.sin(x / 46)) / 24, 2));
|
|
510
|
+
const ridge2 = 8.8 * Math.exp(-Math.pow((z - 68 + 15 * Math.cos(x / 58)) / 32, 2));
|
|
511
|
+
const hummocks = 1.6 * Math.sin(x / 19 + 0.4) * Math.cos(z / 17 - 0.2) + 0.55 * Math.sin((x - z) / 8.5);
|
|
512
|
+
const slack = -2.1 * Math.exp(-Math.pow((z - 14 - 12 * Math.sin(x / 38)) / 18, 2));
|
|
513
|
+
const crestRipple = 0.24 * Math.sin(z * 0.22 + 1.3 * Math.sin(x * 0.08)) * smoothstepNum(2.2, 6.2, ridge1 + ridge2);
|
|
514
|
+
return coastSlope + ridge1 + ridge2 + hummocks + slack + crestRipple;
|
|
515
|
+
}
|
|
516
|
+
if (shape === "montane-ridge") {
|
|
517
|
+
const valleyCoord = x - 24 * Math.sin(z / 72) + 3.6 * Math.sin(z / 17) + 1.8 * Math.cos(x / 11);
|
|
518
|
+
const valley = -4.2 * Math.exp(-Math.pow(valleyCoord / 36, 2));
|
|
519
|
+
const flankD = Math.abs(valleyCoord);
|
|
520
|
+
const flank = 0.085 * flankD;
|
|
521
|
+
const peak = 21 * Math.exp(-Math.pow((z + 52) / 62, 2)) * (0.72 + 0.28 * Math.cos(x / 54));
|
|
522
|
+
const crag = 4.6 * Math.sin(x / 21 + z / 27) + 2.1 * Math.cos(x / 9.5 - z / 11.5);
|
|
523
|
+
const rawH = valley + flank + peak + crag;
|
|
524
|
+
// Hydraulic rill gullies running downslope perpendicular to valley strike + crisp bedrock strata benches on high crags.
|
|
525
|
+
const rillMask = smoothstepNum(16, 48, flankD);
|
|
526
|
+
const rill = -0.52 * rillMask * Math.pow(Math.sin(z * 0.145 + 0.65 * Math.sin(x * 0.045)), 2) * (0.7 + 0.3 * Math.cos(x * 0.08));
|
|
527
|
+
const bench = 0.36 * Math.sin(rawH * 1.15) * smoothstepNum(5, 16, rawH);
|
|
528
|
+
const endSill = 2.6 * smoothstepNum(130, 155, Math.abs(z));
|
|
529
|
+
return rawH + rill + bench + endSill;
|
|
530
|
+
}
|
|
531
|
+
if (shape === "karst-gorge") {
|
|
532
|
+
const gorgeDist = Math.abs(x + 22 * Math.sin(z / 48));
|
|
533
|
+
const plateau = 2.6 + 11.5 * clamp01((gorgeDist - 14) / 68);
|
|
534
|
+
const benches = 1.6 * Math.tanh(Math.sin(plateau * 0.95) * 2.2);
|
|
535
|
+
const bluff = 3.2 * Math.exp(-Math.pow((z + 68) / 48, 2));
|
|
536
|
+
const cove = -6.0 * Math.exp(-Math.pow(gorgeDist / 26, 2));
|
|
537
|
+
const micro = 0.48 * Math.sin(x / 13 - z / 17) + 0.28 * Math.cos((x + z) / 8);
|
|
538
|
+
const grike = -0.26 * smoothstepNum(16, 44, gorgeDist) * Math.pow(Math.sin(z * 0.18 + 0.4 * Math.sin(x * 0.07)), 2);
|
|
539
|
+
return plateau + benches + bluff + cove + micro + grike;
|
|
540
|
+
}
|
|
541
|
+
if (shape === "glacial-tarn") {
|
|
542
|
+
const r = Math.hypot(x * 0.95, z - 8);
|
|
543
|
+
const basin = -4.2 * Math.exp(-Math.pow(r / 48, 2.4));
|
|
544
|
+
const rim = 5.4 * (1 - Math.exp(-Math.pow(Math.max(0, r - 36) / 44, 1.8)));
|
|
545
|
+
const esker = 4.2 * Math.exp(-Math.pow((z + 76 - 20 * Math.sin(x / 52)) / 30, 2));
|
|
546
|
+
const hummock = 1.8 * Math.sin(x / 23 + 0.7) * Math.cos(z / 19 - 0.4) + 0.55 * Math.cos((x - z) / 10.5);
|
|
547
|
+
// Natural outer moraine enclosure keeps tarn & winter ice cleanly inside terrain banks before +/-160 m perimeter.
|
|
548
|
+
const outerRim = 3.2 * smoothstepNum(116, 150, Math.max(Math.abs(x), Math.abs(z)));
|
|
549
|
+
return basin + rim + esker + hummock + outerRim;
|
|
550
|
+
}
|
|
551
|
+
if (shape === "palm-lagoon") {
|
|
552
|
+
const chan = x - 28 * Math.sin(z / 54);
|
|
553
|
+
const lagoon = -3.4 * Math.exp(-Math.pow(chan / 32, 2));
|
|
554
|
+
const spits = 2.6 * Math.exp(-Math.pow((Math.abs(chan) - 44) / 22, 2));
|
|
555
|
+
const bluff = 5.2 * Math.exp(-Math.pow((z + 78) / 42, 2)) * clamp01(Math.abs(chan) / 40);
|
|
556
|
+
const swale = 0.75 * Math.sin(x / 25 + z / 29) + 0.3 * Math.cos(x / 11 - z / 13);
|
|
557
|
+
return lagoon + spits + bluff + swale;
|
|
558
|
+
}
|
|
559
|
+
// "woodland-meadow"
|
|
560
|
+
const woodRise = 4.4 * (1 / (1 + Math.exp((z + 8 - 14 * Math.sin(x / 44)) / 24)));
|
|
561
|
+
const hollow = -1.6 * Math.exp(-Math.pow((z - 38) / 28, 2)) * Math.exp(-Math.pow((x + 18) / 56, 2));
|
|
562
|
+
const roll = 1.4 * Math.sin(x / 27 + 0.5) * Math.cos(z / 31 - 0.3) + 0.38 * Math.sin((x + z) / 11);
|
|
563
|
+
const outerBank = 1.8 * smoothstepNum(122, 152, Math.max(Math.abs(x), Math.abs(z)));
|
|
564
|
+
return woodRise + hollow + roll + outerBank;
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Seamless 5.5 km distant-horizon extension beyond `half = side * 0.5`.
|
|
568
|
+
* Exactly matches `evalTerrainShapeHeight` along the `|x|, |z| <= half` boundary (`C0` continuity).
|
|
569
|
+
*/
|
|
570
|
+
export function evalHorizonTerrainHeight(shape, x, z, half, waterLevel) {
|
|
571
|
+
const cx = Math.min(half, Math.max(-half, x));
|
|
572
|
+
const cz = Math.min(half, Math.max(-half, z));
|
|
573
|
+
const dOut = Math.hypot(x - cx, z - cz);
|
|
574
|
+
const boundaryH = evalTerrainShapeHeight(shape, cx, cz);
|
|
575
|
+
if (dOut <= 1e-6)
|
|
576
|
+
return boundaryH;
|
|
577
|
+
const t = smoothstepNum(0, 260, dOut);
|
|
578
|
+
const macroNoise = 0.55 * Math.sin(x * 0.0021 + z * 0.0017) +
|
|
579
|
+
0.3 * Math.cos(x * 0.0049 - z * 0.0037) +
|
|
580
|
+
0.15 * Math.sin((x - z) * 0.0095);
|
|
581
|
+
const ridgeDetail = Math.abs(Math.sin(x * 0.0031 - z * 0.0027 + macroNoise * 0.9));
|
|
582
|
+
let outerH = boundaryH;
|
|
583
|
+
if (shape === "coastal-dunes") {
|
|
584
|
+
const seaFloor = (waterLevel ?? -1.5) - 4.8;
|
|
585
|
+
const seaMix = smoothstepNum(36, -62, z);
|
|
586
|
+
const heathHills = 7 + 16 * (0.5 + 0.5 * macroNoise) + 6 * ridgeDetail;
|
|
587
|
+
const farHeadland = 26 * Math.exp(-Math.pow((Math.abs(x) - 2100) / 720, 2)) * Math.exp(-Math.pow((z + 1700) / 900, 2));
|
|
588
|
+
outerH = heathHills * (1 - seaMix) + (seaFloor + farHeadland * 1.1) * seaMix;
|
|
589
|
+
}
|
|
590
|
+
else if (shape === "palm-lagoon") {
|
|
591
|
+
const seaFloor = (waterLevel ?? -0.85) - 5.2;
|
|
592
|
+
const island1 = 44 * Math.exp(-Math.pow((x + 1650) / 560, 2) - Math.pow((z + 1950) / 480, 2));
|
|
593
|
+
const island2 = 34 * Math.exp(-Math.pow((x - 1900) / 680, 2) - Math.pow((z + 1550) / 520, 2));
|
|
594
|
+
const spit = 8 * Math.exp(-Math.pow((Math.hypot(x - 420, z + 820) - 920) / 140, 2));
|
|
595
|
+
const inlandBluff = smoothstepNum(60, 420, z) * (10 + 18 * (0.5 + 0.5 * macroNoise));
|
|
596
|
+
outerH = seaFloor + island1 * (0.75 + 0.25 * ridgeDetail) + island2 + spit + inlandBluff;
|
|
597
|
+
}
|
|
598
|
+
else if (shape === "montane-ridge") {
|
|
599
|
+
const distKm = Math.hypot(x, z) * 0.001;
|
|
600
|
+
const southGap = 1 - 0.55 * Math.exp(-Math.pow(x / 520, 2)) * smoothstepNum(120, 900, z);
|
|
601
|
+
const rangeLift = (32 + 125 * smoothstepNum(0.25, 2.4, distKm)) * southGap;
|
|
602
|
+
const massifNorth = 68 * Math.exp(-Math.pow((z + 1650) / 1150, 2));
|
|
603
|
+
const massifEast = 48 * Math.exp(-Math.pow((x - 1550) / 980, 2));
|
|
604
|
+
const massifWest = 54 * Math.exp(-Math.pow((x + 1600) / 1020, 2));
|
|
605
|
+
const peaks = (rangeLift + massifNorth + massifEast + massifWest) * (0.52 + 0.48 * ridgeDetail);
|
|
606
|
+
outerH = Math.max(-4, peaks + 12 * macroNoise);
|
|
607
|
+
}
|
|
608
|
+
else if (shape === "karst-gorge") {
|
|
609
|
+
const seaFloor = (waterLevel ?? -1.1) - 6.0;
|
|
610
|
+
const channelOpen = smoothstepNum(-80, -720, z) * smoothstepNum(360, 120, Math.abs(x));
|
|
611
|
+
const karstMassif = 18 + 38 * (0.45 + 0.55 * ridgeDetail) + 12 * Math.tanh(Math.sin(macroNoise * 4));
|
|
612
|
+
outerH = karstMassif * (1 - channelOpen) + seaFloor * channelOpen;
|
|
613
|
+
}
|
|
614
|
+
else if (shape === "glacial-tarn") {
|
|
615
|
+
const shieldHills = 8 + 26 * (0.45 + 0.55 * macroNoise) + 8 * ridgeDetail;
|
|
616
|
+
outerH = shieldHills;
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
// "woodland-meadow"
|
|
620
|
+
const downs = 5 + 22 * (0.5 + 0.5 * macroNoise) + 6 * ridgeDetail;
|
|
621
|
+
const woodNorth = smoothstepNum(-120, -900, z) * 10;
|
|
622
|
+
outerH = downs + woodNorth;
|
|
623
|
+
}
|
|
624
|
+
return boundaryH + (outerH - boundaryH) * t;
|
|
625
|
+
}
|
|
626
|
+
/** Generate one of the six preset terrain substrates. */
|
|
627
|
+
export function makePresetTerrain(shape, side = 320, cell = 1) {
|
|
628
|
+
const nx = Math.round(side / cell) + 1;
|
|
629
|
+
const nz = nx;
|
|
630
|
+
const half = ((nx - 1) * cell) * 0.5;
|
|
631
|
+
const heightAt = (x, z) => evalTerrainShapeHeight(shape, x, z);
|
|
632
|
+
if (shape === "coastal-dunes") {
|
|
633
|
+
return bakeSubstrateGrid({
|
|
634
|
+
x0: -half,
|
|
635
|
+
z0: -half,
|
|
636
|
+
nx,
|
|
637
|
+
nz,
|
|
638
|
+
cell,
|
|
639
|
+
waterTableY: -0.6,
|
|
640
|
+
heightAt,
|
|
641
|
+
geomorphology: {
|
|
642
|
+
seed: 101,
|
|
643
|
+
erosionStrength: 0.75,
|
|
644
|
+
channelIncise: 0.06,
|
|
645
|
+
talusDeg: 32,
|
|
646
|
+
bankScarpAmp: 0.16,
|
|
647
|
+
terracetteAmp: 0.045,
|
|
648
|
+
hummockAmp: 0.11,
|
|
649
|
+
},
|
|
650
|
+
basePhAt(x, z, h) {
|
|
651
|
+
// Acidic leached inland heath vs slightly shell-buffered foredunes.
|
|
652
|
+
const inland = clamp01((z + 60) / 180);
|
|
653
|
+
return clamp01(0.58 - 0.34 * inland - 0.08 * clamp01(h / 10) + 0.05 * Math.sin(x / 41));
|
|
654
|
+
},
|
|
655
|
+
baseFertilityAt(_x, z, h) {
|
|
656
|
+
const slackBonus = clamp01(1 - (h + 1) / 5);
|
|
657
|
+
const sheltered = clamp01((z + 40) / 180) * 0.22;
|
|
658
|
+
return clamp01(0.14 + 0.42 * slackBonus + sheltered);
|
|
659
|
+
},
|
|
660
|
+
exposureAt(x, z, h, slopeDeg) {
|
|
661
|
+
const seaward = clamp01(1 - (z + 100) / 220);
|
|
662
|
+
return clamp01(0.28 + 0.42 * seaward + 0.25 * clamp01(h / 12) + 0.06 * Math.sin(x / 31) + Math.max(0, slopeDeg - 15) * 0.01);
|
|
663
|
+
},
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
if (shape === "montane-ridge") {
|
|
667
|
+
return bakeSubstrateGrid({
|
|
668
|
+
x0: -half,
|
|
669
|
+
z0: -half,
|
|
670
|
+
nx,
|
|
671
|
+
nz,
|
|
672
|
+
cell,
|
|
673
|
+
waterTableY: -3.2,
|
|
674
|
+
heightAt,
|
|
675
|
+
geomorphology: {
|
|
676
|
+
seed: 202,
|
|
677
|
+
erosionStrength: 1.25,
|
|
678
|
+
channelIncise: 0.15,
|
|
679
|
+
talusDeg: 39,
|
|
680
|
+
bankScarpAmp: 0.24,
|
|
681
|
+
terracetteAmp: 0.06,
|
|
682
|
+
hummockAmp: 0.06,
|
|
683
|
+
},
|
|
684
|
+
basePhAt(_x, _z, h) {
|
|
685
|
+
return clamp01(0.62 - 0.26 * clamp01((h + 2) / 24));
|
|
686
|
+
},
|
|
687
|
+
baseFertilityAt(_x, _z, h) {
|
|
688
|
+
return clamp01(0.76 - 0.56 * clamp01((h + 3) / 24));
|
|
689
|
+
},
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
if (shape === "karst-gorge") {
|
|
693
|
+
return bakeSubstrateGrid({
|
|
694
|
+
x0: -half,
|
|
695
|
+
z0: -half,
|
|
696
|
+
nx,
|
|
697
|
+
nz,
|
|
698
|
+
cell,
|
|
699
|
+
waterTableY: -1.1,
|
|
700
|
+
heightAt,
|
|
701
|
+
geomorphology: {
|
|
702
|
+
seed: 303,
|
|
703
|
+
erosionStrength: 1.05,
|
|
704
|
+
channelIncise: 0.11,
|
|
705
|
+
talusDeg: 38,
|
|
706
|
+
bankScarpAmp: 0.18,
|
|
707
|
+
terracetteAmp: 0.04,
|
|
708
|
+
hummockAmp: 0.02,
|
|
709
|
+
},
|
|
710
|
+
basePhAt(_x, _z, h) {
|
|
711
|
+
return clamp01(0.68 + 0.18 * clamp01(h / 14));
|
|
712
|
+
},
|
|
713
|
+
baseFertilityAt(_x, _z, h) {
|
|
714
|
+
return clamp01(0.42 - 0.28 * clamp01(h / 14));
|
|
715
|
+
},
|
|
716
|
+
moistureBias(_x, _z, h, conc) {
|
|
717
|
+
// Fractured karst limestone drains rapidly on open plateaus, retaining moisture in concave grikes/valley floors.
|
|
718
|
+
return -0.24 * smoothstepNum(1.2, 6.0, h) * (1 - clamp01(conc * 2.5));
|
|
719
|
+
},
|
|
720
|
+
exposureAt(x, z, h, slopeDeg) {
|
|
721
|
+
const coveNear = clamp01(1 - Math.abs(x + 22 * Math.sin(z / 48)) / 70);
|
|
722
|
+
return clamp01(0.38 + 0.32 * clamp01(h / 15) + 0.18 * coveNear + Math.max(0, slopeDeg - 18) * 0.01);
|
|
723
|
+
},
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
if (shape === "glacial-tarn") {
|
|
727
|
+
return bakeSubstrateGrid({
|
|
728
|
+
x0: -half,
|
|
729
|
+
z0: -half,
|
|
730
|
+
nx,
|
|
731
|
+
nz,
|
|
732
|
+
cell,
|
|
733
|
+
waterTableY: -1.2,
|
|
734
|
+
heightAt,
|
|
735
|
+
geomorphology: {
|
|
736
|
+
seed: 404,
|
|
737
|
+
erosionStrength: 0.95,
|
|
738
|
+
channelIncise: 0.09,
|
|
739
|
+
talusDeg: 36,
|
|
740
|
+
bankScarpAmp: 0.30,
|
|
741
|
+
terracetteAmp: 0.04,
|
|
742
|
+
hummockAmp: 0.14,
|
|
743
|
+
},
|
|
744
|
+
basePhAt(_x, _z, h) {
|
|
745
|
+
return clamp01(0.22 + 0.18 * clamp01((4 - h) / 10));
|
|
746
|
+
},
|
|
747
|
+
baseFertilityAt(_x, _z, h) {
|
|
748
|
+
return clamp01(0.56 - 0.3 * clamp01(h / 12));
|
|
749
|
+
},
|
|
750
|
+
});
|
|
751
|
+
}
|
|
752
|
+
if (shape === "palm-lagoon") {
|
|
753
|
+
return bakeSubstrateGrid({
|
|
754
|
+
x0: -half,
|
|
755
|
+
z0: -half,
|
|
756
|
+
nx,
|
|
757
|
+
nz,
|
|
758
|
+
cell,
|
|
759
|
+
waterTableY: -0.85,
|
|
760
|
+
heightAt,
|
|
761
|
+
geomorphology: {
|
|
762
|
+
seed: 505,
|
|
763
|
+
erosionStrength: 0.65,
|
|
764
|
+
channelIncise: 0.055,
|
|
765
|
+
talusDeg: 31,
|
|
766
|
+
bankScarpAmp: 0.18,
|
|
767
|
+
terracetteAmp: 0.025,
|
|
768
|
+
hummockAmp: 0.03,
|
|
769
|
+
},
|
|
770
|
+
basePhAt(_x, _z, h) {
|
|
771
|
+
return clamp01(0.54 + 0.18 * clamp01((h + 2) / 9));
|
|
772
|
+
},
|
|
773
|
+
baseFertilityAt(_x, _z, h) {
|
|
774
|
+
return clamp01(0.45 + 0.35 * clamp01((3 - h) / 6));
|
|
775
|
+
},
|
|
776
|
+
moistureBias(_x, _z, h, conc, slopeDeg) {
|
|
777
|
+
// Porous coral sandspits drain dry above seep swales; concave brackish hollows stay moist.
|
|
778
|
+
return -0.32 * smoothstepNum(0.35, 2.8, h + 0.85) * (1 - clamp01(conc * 3.0)) - Math.max(0, slopeDeg - 12) * 0.008;
|
|
779
|
+
},
|
|
780
|
+
exposureAt(x, _z, h) {
|
|
781
|
+
return clamp01(0.34 + 0.32 * clamp01(h / 8) + 0.12 * Math.sin(x / 40));
|
|
782
|
+
},
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
// Default: "woodland-meadow" — rolling woodland rise to the north (-z), meadow hollow in the centre/south.
|
|
786
|
+
return bakeSubstrateGrid({
|
|
787
|
+
x0: -half,
|
|
788
|
+
z0: -half,
|
|
789
|
+
nx,
|
|
790
|
+
nz,
|
|
791
|
+
cell,
|
|
792
|
+
waterTableY: -1.6,
|
|
793
|
+
heightAt,
|
|
794
|
+
geomorphology: {
|
|
795
|
+
seed: 606,
|
|
796
|
+
erosionStrength: 0.9,
|
|
797
|
+
channelIncise: 0.085,
|
|
798
|
+
talusDeg: 33,
|
|
799
|
+
bankScarpAmp: 0.22,
|
|
800
|
+
terracetteAmp: 0.06,
|
|
801
|
+
hummockAmp: 0.08,
|
|
802
|
+
},
|
|
803
|
+
basePhAt(x, z, _h) {
|
|
804
|
+
const acidPatch = Math.exp(-Math.pow((x - 52) / 48, 2) - Math.pow((z + 44) / 42, 2));
|
|
805
|
+
return clamp01(0.62 - 0.28 * acidPatch + 0.06 * Math.sin(x / 50 + z / 60));
|
|
806
|
+
},
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
/** Bilinear sample of all terrain & edaphic channels at world `(x, z)`. */
|
|
810
|
+
export function sampleSubstrateCpu(grid, x, z) {
|
|
811
|
+
const fx = Math.min(grid.nx - 1.001, Math.max(0, (x - grid.x0) / grid.cell));
|
|
812
|
+
const fz = Math.min(grid.nz - 1.001, Math.max(0, (z - grid.z0) / grid.cell));
|
|
813
|
+
const i0 = Math.floor(fx);
|
|
814
|
+
const j0 = Math.floor(fz);
|
|
815
|
+
const i1 = Math.min(grid.nx - 1, i0 + 1);
|
|
816
|
+
const j1 = Math.min(grid.nz - 1, j0 + 1);
|
|
817
|
+
const tx = fx - i0;
|
|
818
|
+
const tz = fz - j0;
|
|
819
|
+
const bilerp = (arr) => {
|
|
820
|
+
const a = arr[j0 * grid.nx + i0];
|
|
821
|
+
const b = arr[j0 * grid.nx + i1];
|
|
822
|
+
const c = arr[j1 * grid.nx + i0];
|
|
823
|
+
const d = arr[j1 * grid.nx + i1];
|
|
824
|
+
return (a + (b - a) * tx) * (1 - tz) + (c + (d - c) * tx) * tz;
|
|
825
|
+
};
|
|
826
|
+
const bilerp3 = (arr, comp) => {
|
|
827
|
+
const a = arr[(j0 * grid.nx + i0) * 3 + comp];
|
|
828
|
+
const b = arr[(j0 * grid.nx + i1) * 3 + comp];
|
|
829
|
+
const c = arr[(j1 * grid.nx + i0) * 3 + comp];
|
|
830
|
+
const d = arr[(j1 * grid.nx + i1) * 3 + comp];
|
|
831
|
+
return (a + (b - a) * tx) * (1 - tz) + (c + (d - c) * tx) * tz;
|
|
832
|
+
};
|
|
833
|
+
const nxVal = bilerp3(grid.normals, 0);
|
|
834
|
+
const nyVal = bilerp3(grid.normals, 1);
|
|
835
|
+
const nzVal = bilerp3(grid.normals, 2);
|
|
836
|
+
const nLen = Math.hypot(nxVal, nyVal, nzVal) || 1;
|
|
837
|
+
const nnx = nxVal / nLen;
|
|
838
|
+
const nny = nyVal / nLen;
|
|
839
|
+
const nnz = nzVal / nLen;
|
|
840
|
+
return {
|
|
841
|
+
height: bilerp(grid.heights),
|
|
842
|
+
normal: [nnx, nny, nnz],
|
|
843
|
+
slopeDeg: bilerp(grid.slopeDeg),
|
|
844
|
+
aspectRad: Math.atan2(nnx, nnz),
|
|
845
|
+
concavity: bilerp(grid.concavity),
|
|
846
|
+
moisture: bilerp(grid.moisture),
|
|
847
|
+
soilPh: bilerp(grid.soilPh),
|
|
848
|
+
fertility: bilerp(grid.fertility),
|
|
849
|
+
exposure: bilerp(grid.exposure),
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Compute litter-adjusted topsoil pH and fertility without mutating `grid`.
|
|
854
|
+
*/
|
|
855
|
+
export function deriveLitterSoil(grid, litter, coniferShare, cnx, cnz) {
|
|
856
|
+
const { nx, nz, x0, z0, cell, width, depth } = grid;
|
|
857
|
+
const soilPh = new Float32Array(grid.soilPh.length);
|
|
858
|
+
const fertility = new Float32Array(grid.fertility.length);
|
|
859
|
+
for (let j = 0; j < nz; j++) {
|
|
860
|
+
const z = z0 + j * cell;
|
|
861
|
+
const v = clamp01((z - z0) / Math.max(1e-4, depth)) * (cnz - 1);
|
|
862
|
+
const cj0 = Math.floor(v);
|
|
863
|
+
const cj1 = Math.min(cnz - 1, cj0 + 1);
|
|
864
|
+
const tz = v - cj0;
|
|
865
|
+
for (let i = 0; i < nx; i++) {
|
|
866
|
+
const x = x0 + i * cell;
|
|
867
|
+
const u = clamp01((x - x0) / Math.max(1e-4, width)) * (cnx - 1);
|
|
868
|
+
const ci0 = Math.floor(u);
|
|
869
|
+
const ci1 = Math.min(cnx - 1, ci0 + 1);
|
|
870
|
+
const tx = u - ci0;
|
|
871
|
+
const idx00 = cj0 * cnx + ci0;
|
|
872
|
+
const idx10 = cj0 * cnx + ci1;
|
|
873
|
+
const idx01 = cj1 * cnx + ci0;
|
|
874
|
+
const idx11 = cj1 * cnx + ci1;
|
|
875
|
+
const lit = (litter[idx00] + (litter[idx10] - litter[idx00]) * tx) * (1 - tz) + (litter[idx01] + (litter[idx11] - litter[idx01]) * tx) * tz;
|
|
876
|
+
const con = (coniferShare[idx00] + (coniferShare[idx10] - coniferShare[idx00]) * tx) * (1 - tz) +
|
|
877
|
+
(coniferShare[idx01] + (coniferShare[idx11] - coniferShare[idx01]) * tx) * tz;
|
|
878
|
+
const k = j * nx + i;
|
|
879
|
+
soilPh[k] = clamp01(grid.soilPh[k] - lit * con * 0.24);
|
|
880
|
+
fertility[k] = clamp01(grid.fertility[k] + lit * (1 - con * 0.65) * 0.18);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
return { soilPh, fertility };
|
|
884
|
+
}
|
|
885
|
+
/**
|
|
886
|
+
* Feedback from tree litter onto topsoil chemistry: conifer needle mats acidify topsoil
|
|
887
|
+
* while broadleaf litter boosts humus fertility in the upper soil horizon.
|
|
888
|
+
*/
|
|
889
|
+
export function applyLitterEdaphics(grid, litter, coniferShare, cnx, cnz) {
|
|
890
|
+
const { soilPh, fertility } = deriveLitterSoil(grid, litter, coniferShare, cnx, cnz);
|
|
891
|
+
grid.soilPh.set(soilPh);
|
|
892
|
+
grid.fertility.set(fertility);
|
|
893
|
+
}
|
|
894
|
+
/** Create GPU buffers and TSL samplers for a baked `SubstrateGrid`. */
|
|
895
|
+
export function createSubstrate(grid) {
|
|
896
|
+
const { nx, nz, x0, z0, cell, width, depth } = grid;
|
|
897
|
+
const n = nx * nz;
|
|
898
|
+
const heightData = new Float32Array(n * 4);
|
|
899
|
+
for (let k = 0; k < n; k++) {
|
|
900
|
+
heightData[k * 4] = grid.heights[k];
|
|
901
|
+
heightData[k * 4 + 1] = grid.normals[k * 3];
|
|
902
|
+
heightData[k * 4 + 2] = grid.normals[k * 3 + 1];
|
|
903
|
+
heightData[k * 4 + 3] = grid.normals[k * 3 + 2];
|
|
904
|
+
}
|
|
905
|
+
const heightAttr = new THREE.StorageBufferAttribute(heightData, 4);
|
|
906
|
+
const posts = storage(heightAttr, "vec4", n).toReadOnly();
|
|
907
|
+
const edaphicData = new Uint8Array(n * 4);
|
|
908
|
+
const edaphicTex = new THREE.DataTexture(edaphicData, nx, nz, THREE.RGBAFormat, THREE.UnsignedByteType);
|
|
909
|
+
edaphicTex.colorSpace = THREE.NoColorSpace;
|
|
910
|
+
edaphicTex.magFilter = edaphicTex.minFilter = THREE.LinearFilter;
|
|
911
|
+
edaphicTex.wrapS = edaphicTex.wrapT = THREE.ClampToEdgeWrapping;
|
|
912
|
+
edaphicTex.generateMipmaps = false;
|
|
913
|
+
// Filterable HalfFloat heightmap (`r = height m`) so `trees` `world.ground` costs 0 storage buffers.
|
|
914
|
+
const halfData = new Uint16Array(n * 4);
|
|
915
|
+
for (let k = 0; k < n; k++) {
|
|
916
|
+
halfData[k * 4] = THREE.DataUtils.toHalfFloat(grid.heights[k]);
|
|
917
|
+
halfData[k * 4 + 3] = THREE.DataUtils.toHalfFloat(1);
|
|
918
|
+
}
|
|
919
|
+
const heightTex = new THREE.DataTexture(halfData, nx, nz, THREE.RGBAFormat, THREE.HalfFloatType);
|
|
920
|
+
heightTex.colorSpace = THREE.NoColorSpace;
|
|
921
|
+
heightTex.magFilter = heightTex.minFilter = THREE.LinearFilter;
|
|
922
|
+
heightTex.wrapS = heightTex.wrapT = THREE.ClampToEdgeWrapping;
|
|
923
|
+
heightTex.generateMipmaps = false;
|
|
924
|
+
heightTex.needsUpdate = true;
|
|
925
|
+
// The current soil: the grid's own until an edit hands over new arrays. The texture and the CPU sampler
|
|
926
|
+
// both read it, so they agree after any number of edits.
|
|
927
|
+
let current = grid;
|
|
928
|
+
const refreshEdaphicTexture = (soilOverride) => {
|
|
929
|
+
if (soilOverride)
|
|
930
|
+
current = { ...grid, soilPh: soilOverride.soilPh, fertility: soilOverride.fertility };
|
|
931
|
+
const ph = current.soilPh;
|
|
932
|
+
const fert = current.fertility;
|
|
933
|
+
for (let k = 0; k < n; k++) {
|
|
934
|
+
edaphicData[k * 4] = Math.round(clamp01(grid.moisture[k]) * 255);
|
|
935
|
+
edaphicData[k * 4 + 1] = Math.round(clamp01(ph[k]) * 255);
|
|
936
|
+
edaphicData[k * 4 + 2] = Math.round(clamp01(fert[k]) * 255);
|
|
937
|
+
edaphicData[k * 4 + 3] = Math.round(clamp01(grid.exposure[k]) * 255);
|
|
938
|
+
}
|
|
939
|
+
edaphicTex.needsUpdate = true;
|
|
940
|
+
};
|
|
941
|
+
refreshEdaphicTexture();
|
|
942
|
+
const uOrigin = uniform(vec4(x0, z0, cell, 1 / Math.max(1e-4, width)));
|
|
943
|
+
return {
|
|
944
|
+
get grid() {
|
|
945
|
+
return current;
|
|
946
|
+
},
|
|
947
|
+
bounds: { x0, z0, width, depth },
|
|
948
|
+
edaphicTex,
|
|
949
|
+
heightTex,
|
|
950
|
+
sampleCpu(x, z) {
|
|
951
|
+
return sampleSubstrateCpu(current, x, z);
|
|
952
|
+
},
|
|
953
|
+
sampleHeightTex(xz) {
|
|
954
|
+
const u = clamp(xz.x.sub(uOrigin.x).div(uOrigin.z).add(0.5).div(nx), float(0.5 / nx), float(1 - 0.5 / nx));
|
|
955
|
+
const v = clamp(xz.y.sub(uOrigin.y).div(uOrigin.z).add(0.5).div(nz), float(0.5 / nz), float(1 - 0.5 / nz));
|
|
956
|
+
return texture(heightTex, vec2(u, v)).level(float(0)).r;
|
|
957
|
+
},
|
|
958
|
+
sample(xz) {
|
|
959
|
+
const p = clamp(xz.sub(vec2(uOrigin.x, uOrigin.y)).div(uOrigin.z), vec2(0), vec2(nx - 1.001, nz - 1.001));
|
|
960
|
+
const pi = floor(p);
|
|
961
|
+
const pf = fract(p);
|
|
962
|
+
const i0 = int(pi.x);
|
|
963
|
+
const j0 = int(pi.y);
|
|
964
|
+
const i1 = int(min(pi.x.add(1), float(nx - 1)));
|
|
965
|
+
const j1 = int(min(pi.y.add(1), float(nz - 1)));
|
|
966
|
+
const a = posts.element(j0.mul(nx).add(i0));
|
|
967
|
+
const b = posts.element(j0.mul(nx).add(i1));
|
|
968
|
+
const c = posts.element(j1.mul(nx).add(i0));
|
|
969
|
+
const d = posts.element(j1.mul(nx).add(i1));
|
|
970
|
+
const h = mix(mix(a.x, b.x, pf.x), mix(c.x, d.x, pf.x), pf.y);
|
|
971
|
+
const norm = mix(mix(vec3(a.y, a.z, a.w), vec3(b.y, b.z, b.w), pf.x), mix(vec3(c.y, c.z, c.w), vec3(d.y, d.z, d.w), pf.x), pf.y);
|
|
972
|
+
return { height: h, normal: normalize(norm) };
|
|
973
|
+
},
|
|
974
|
+
sampleEdaphic(xz) {
|
|
975
|
+
const u = clamp(xz.x.sub(uOrigin.x).div(uOrigin.z).add(0.5).div(nx), float(0.5 / nx), float(1 - 0.5 / nx));
|
|
976
|
+
const v = clamp(xz.y.sub(uOrigin.y).div(uOrigin.z).add(0.5).div(nz), float(0.5 / nz), float(1 - 0.5 / nz));
|
|
977
|
+
const s = texture(edaphicTex, vec2(u, v)).level(float(0));
|
|
978
|
+
return { moisture: s.r, soilPh: s.g, fertility: s.b, exposure: s.a };
|
|
979
|
+
},
|
|
980
|
+
refreshEdaphicTexture,
|
|
981
|
+
dispose() {
|
|
982
|
+
heightAttr.dispose();
|
|
983
|
+
edaphicTex.dispose();
|
|
984
|
+
heightTex.dispose();
|
|
985
|
+
},
|
|
986
|
+
};
|
|
987
|
+
}
|
|
988
|
+
//# sourceMappingURL=substrate.js.map
|