roamwright 0.1.1 → 0.2.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/CONTRIBUTING.md +39 -8
- package/README.md +53 -24
- package/examples/viaduct-camera.js +40 -0
- package/examples/viaduct-landscape.js +81 -0
- package/examples/viaduct.css +75 -0
- package/examples/viaduct.html +47 -0
- package/examples/viaduct.js +332 -0
- package/geometry/creased-sheet.js +127 -0
- package/geometry/ear.js +97 -0
- package/geometry/erosion.js +106 -0
- package/geometry/face-grid.js +143 -0
- package/geometry/garment-tags.js +11 -0
- package/geometry/hair-gathers.js +174 -0
- package/geometry/hair-locks.js +156 -0
- package/geometry/hair-patch.js +42 -0
- package/geometry/hair-shell.js +186 -0
- package/geometry/hair-tags.js +6 -0
- package/geometry/head-profile.js +138 -0
- package/geometry/head-shape.js +499 -0
- package/geometry/heightfield.js +68 -0
- package/geometry/outline-grid.js +46 -0
- package/geometry/path-field.js +65 -0
- package/geometry/sagging-tube.js +58 -0
- package/geometry/sampled-surface.js +101 -0
- package/geometry/scatter-grid.js +32 -0
- package/geometry/sculpt.js +263 -0
- package/geometry/shore-grid.js +67 -0
- package/geometry/strata-field.js +339 -0
- package/geometry/strata-tiles.js +215 -0
- package/geometry/stream.js +94 -0
- package/geometry/terrain-chunks.js +300 -0
- package/geometry/terrain-extension.js +44 -0
- package/math/cell-edge.js +44 -0
- package/math/cloth-textures.js +83 -0
- package/math/cloud-textures.js +112 -0
- package/math/color.js +29 -0
- package/math/mountain-ranges.js +463 -0
- package/math/particle-noise.js +48 -0
- package/math/periodic.js +20 -0
- package/math/rock-texture.js +56 -0
- package/math/surface-textures.js +241 -0
- package/motion/arm-reach.js +74 -0
- package/motion/biped-stride.js +276 -0
- package/motion/crowd-alarms.js +127 -0
- package/motion/crowd-routine.js +183 -0
- package/motion/daily-clock.js +59 -0
- package/motion/instance-sight.js +84 -0
- package/motion/quadruped.js +332 -0
- package/motion/weather.js +59 -0
- package/motion/wind.js +27 -0
- package/package.json +6 -4
- package/rendering/caster-motion.js +79 -0
- package/rendering/cell-lod.js +175 -0
- package/rendering/cloud-dome.js +371 -0
- package/rendering/cloud-field.js +83 -0
- package/rendering/compilation-schedule.js +34 -0
- package/rendering/contact-shadows.js +79 -0
- package/rendering/crowd-skinning.js +133 -0
- package/rendering/crust.js +28 -0
- package/rendering/daylight.js +50 -0
- package/rendering/far-shadow.js +186 -0
- package/rendering/flocks.js +323 -0
- package/rendering/garment-material.js +110 -0
- package/rendering/glow-lines.js +74 -0
- package/rendering/glow-shell.js +90 -0
- package/rendering/hair-strands.js +67 -0
- package/rendering/height-haze.js +129 -0
- package/rendering/height-probe.js +91 -0
- package/rendering/interior-mapping.js +19 -0
- package/rendering/light-budget.js +222 -0
- package/rendering/light-points.js +51 -0
- package/rendering/light-shafts.js +131 -0
- package/rendering/local-lights.js +28 -0
- package/rendering/low-light.js +13 -0
- package/rendering/material-lod.js +110 -0
- package/rendering/particle-emitters.js +223 -0
- package/rendering/particles.js +297 -0
- package/rendering/planar-reflection.js +175 -0
- package/rendering/post-shaders.js +21 -1
- package/rendering/post.js +4 -0
- package/rendering/rain-rings.js +22 -0
- package/rendering/road-surface.js +63 -0
- package/rendering/sea-shaders.js +230 -0
- package/rendering/shader-warmup.js +197 -0
- package/rendering/shadow-alignment.js +28 -0
- package/rendering/shadow-depth.js +129 -0
- package/rendering/sky-occlusion.js +187 -0
- package/rendering/sky.js +169 -0
- package/rendering/stage-lights.js +209 -0
- package/rendering/static-point-shadow.js +119 -0
- package/rendering/strata-material.js +93 -0
- package/rendering/stream-flow.js +36 -0
- package/rendering/surface-light.js +22 -0
- package/rendering/surface-projection.js +69 -0
- package/rendering/talus.js +26 -0
- package/rendering/terrain-rock.js +48 -0
- package/rendering/texture-fills.js +68 -0
- package/rendering/viewport.js +9 -0
- package/rendering/wakes.js +99 -0
- package/rendering/water-material.js +288 -0
- package/runtime/device-quality.js +27 -0
- package/runtime/diagnostic-log.js +80 -0
- package/runtime/events.js +14 -0
- package/runtime/first-use.js +3 -1
- package/runtime/frame-yield.js +14 -0
- package/runtime/grid-cache.js +88 -0
- package/runtime/grid-rows.js +9 -0
- package/runtime/module-preload.js +15 -0
- package/runtime/search.js +69 -0
- package/runtime/spatial-audio.js +57 -0
- package/scripts/serve.py +26 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { clamp, lerp, sstep } from '../math/scalar.js';
|
|
2
|
+
import { createNoise } from '../math/noise.js';
|
|
3
|
+
import { createHash, mulberry32 } from '../math/random.js';
|
|
4
|
+
import { SRGB2LIN } from '../math/color.js';
|
|
5
|
+
|
|
6
|
+
// Heightfields are square and centred on the origin. All lengths and elevations are metres.
|
|
7
|
+
// Exposure is the caller's rise/run attenuated to zero wherever rock must be excluded.
|
|
8
|
+
export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1, 2], slopeRange = [0.8, 1.08], bedRange = [-80, 460] }) {
|
|
9
|
+
const stride = cells + 1, half = cells * spacing / 2;
|
|
10
|
+
const { fbm, vnoise } = createNoise(seed), hash2 = createHash(seed);
|
|
11
|
+
let heights, colours;
|
|
12
|
+
function setGround(input) {
|
|
13
|
+
heights = input.heights;
|
|
14
|
+
colours = input.colours;
|
|
15
|
+
}
|
|
16
|
+
let mask = null, // 0..1, how much of a cliff stands here
|
|
17
|
+
nrm = null, // the terrain normal smoothed over about 12 m, the direction the rock is pushed out
|
|
18
|
+
band = null; // 0..1, the character of the band of cliffs a cell belongs to (relief, colour)
|
|
19
|
+
function makeGrids() {
|
|
20
|
+
mask ??= new Float32Array(stride * stride);
|
|
21
|
+
nrm ??= new Float32Array(stride * stride * 3);
|
|
22
|
+
band ??= new Float32Array(stride * stride);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// The beds, bottom to top: packages of massive hard beds, of thin soft ones, and of middling ones.
|
|
26
|
+
const BED = { y0: bedRange[0], step: 0.1, top: [], thick: [], hard: [], tint: [], index: null };
|
|
27
|
+
(() => {
|
|
28
|
+
const R = mulberry32(seed * 7 + 311);
|
|
29
|
+
let y = BED.y0;
|
|
30
|
+
while (y < bedRange[1]) {
|
|
31
|
+
const kind = R(),
|
|
32
|
+
n = kind < 0.36 ? 1 + ((R() * 2) | 0) : kind < 0.66 ? 3 + ((R() * 4) | 0) : 2 + ((R() * 3) | 0);
|
|
33
|
+
for (let i = 0; i < n; i++) {
|
|
34
|
+
const t = kind < 0.36 ? lerp(3.5, 8, R()) : kind < 0.66 ? lerp(0.9, 2.2, R()) : lerp(2.2, 4.5, R());
|
|
35
|
+
const hard = kind < 0.36 ? lerp(0.78, 1, R()) : kind < 0.66 ? lerp(0.12, 0.5, R()) : lerp(0.5, 0.88, R());
|
|
36
|
+
BED.top.push(y + t);
|
|
37
|
+
BED.thick.push(t);
|
|
38
|
+
BED.hard.push(hard);
|
|
39
|
+
BED.tint.push(R());
|
|
40
|
+
y += t;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
BED.index = new Int16Array(Math.ceil((y - BED.y0) / BED.step));
|
|
44
|
+
for (let i = 0, b = 0; i < BED.index.length; i++) {
|
|
45
|
+
const yy = BED.y0 + (i + 0.5) * BED.step;
|
|
46
|
+
while (BED.top[b] < yy) b++;
|
|
47
|
+
BED.index[i] = b;
|
|
48
|
+
}
|
|
49
|
+
})();
|
|
50
|
+
function bedAt(yy) {
|
|
51
|
+
return BED.index[clamp(((yy - BED.y0) / BED.step) | 0, 0, BED.index.length - 1)];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function planCliffShape(input) {
|
|
55
|
+
setGround(input);
|
|
56
|
+
const normals = input.normals;
|
|
57
|
+
makeGrids();
|
|
58
|
+
band.fill(0);
|
|
59
|
+
// Blur the supplied exposure so each band falls away through a soft edge.
|
|
60
|
+
const raw = new Float32Array(stride * stride),
|
|
61
|
+
tmp = new Float32Array(stride * stride);
|
|
62
|
+
for (let j = 0; j <= cells; j++)
|
|
63
|
+
for (let i = 0; i <= cells; i++) {
|
|
64
|
+
const k = j * stride + i,
|
|
65
|
+
x = i * spacing - half,
|
|
66
|
+
z = j * spacing - half,
|
|
67
|
+
exposure = input.exposure(k, x, z);
|
|
68
|
+
raw[k] = exposure;
|
|
69
|
+
}
|
|
70
|
+
blur(raw, tmp, 1);
|
|
71
|
+
blur(raw, tmp, 1);
|
|
72
|
+
for (let k = 0; k < mask.length; k++) mask[k] = sstep(slopeRange[0], slopeRange[1], raw[k]);
|
|
73
|
+
// bands: neighbouring cliff cells grouped; a speck of a few cells is dropped, and each band gets a character of its own
|
|
74
|
+
const id = new Int32Array(stride * stride).fill(-1),
|
|
75
|
+
stack = [];
|
|
76
|
+
let bands = 0;
|
|
77
|
+
for (let k0 = 0; k0 < mask.length; k0++) {
|
|
78
|
+
if (mask[k0] < 0.5 || id[k0] >= 0) continue;
|
|
79
|
+
const connected = [];
|
|
80
|
+
id[k0] = bands;
|
|
81
|
+
stack.push(k0);
|
|
82
|
+
while (stack.length) {
|
|
83
|
+
const k = stack.pop(),
|
|
84
|
+
i = k % stride,
|
|
85
|
+
j = (k / stride) | 0;
|
|
86
|
+
connected.push(k);
|
|
87
|
+
for (let dj = -1; dj <= 1; dj++)
|
|
88
|
+
for (let di = -1; di <= 1; di++) {
|
|
89
|
+
const a = i + di,
|
|
90
|
+
b = j + dj,
|
|
91
|
+
kk = b * stride + a;
|
|
92
|
+
if (a < 0 || b < 0 || a > cells || b > cells || id[kk] >= 0 || mask[kk] < 0.5) continue;
|
|
93
|
+
id[kk] = bands;
|
|
94
|
+
stack.push(kk);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const ch = hash2(bands, 0, 532);
|
|
98
|
+
for (const k of connected) band[k] = connected.length < 7 ? -1 : ch;
|
|
99
|
+
bands++;
|
|
100
|
+
}
|
|
101
|
+
for (let k = 0; k < mask.length; k++)
|
|
102
|
+
if (band[k] < 0) {
|
|
103
|
+
band[k] = 0;
|
|
104
|
+
mask[k] *= 0.3;
|
|
105
|
+
} // a speck: a little rock, no cliff
|
|
106
|
+
for (let pass = 0; pass < 3; pass++) {
|
|
107
|
+
// each band's character reaches out over its soft edge
|
|
108
|
+
tmp.set(band);
|
|
109
|
+
for (let j = 1; j < cells; j++)
|
|
110
|
+
for (let i = 1; i < cells; i++) {
|
|
111
|
+
const k = j * stride + i;
|
|
112
|
+
if (mask[k] >= 0.5 || mask[k] === 0) continue;
|
|
113
|
+
let s = 0,
|
|
114
|
+
n = 0;
|
|
115
|
+
for (const kk of [k - 1, k + 1, k - stride, k + stride])
|
|
116
|
+
if (tmp[kk] > 0) {
|
|
117
|
+
s += tmp[kk];
|
|
118
|
+
n++;
|
|
119
|
+
}
|
|
120
|
+
if (n) band[k] = s / n;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// the smoothed normal
|
|
124
|
+
for (let c = 0; c < 3; c++) {
|
|
125
|
+
for (let k = 0; k < raw.length; k++) raw[k] = normals[k * 3 + c];
|
|
126
|
+
blur(raw, tmp, 1);
|
|
127
|
+
blur(raw, tmp, 1);
|
|
128
|
+
blur(raw, tmp, 1);
|
|
129
|
+
for (let k = 0; k < raw.length; k++) nrm[k * 3 + c] = raw[k];
|
|
130
|
+
}
|
|
131
|
+
for (let k = 0; k < raw.length; k++) {
|
|
132
|
+
const l = Math.hypot(nrm[k * 3], nrm[k * 3 + 1], nrm[k * 3 + 2]) || 1;
|
|
133
|
+
nrm[k * 3] /= l;
|
|
134
|
+
nrm[k * 3 + 1] /= l;
|
|
135
|
+
nrm[k * 3 + 2] /= l;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function blur(a, t, r) {
|
|
139
|
+
// separable box blur of radius r cells, in place
|
|
140
|
+
for (let pass = 0; pass < 2; pass++) {
|
|
141
|
+
const src = pass ? t : a,
|
|
142
|
+
dst = pass ? a : t,
|
|
143
|
+
sx = pass ? stride : 1,
|
|
144
|
+
sy = pass ? 1 : stride;
|
|
145
|
+
for (let j = 0; j <= cells; j++)
|
|
146
|
+
for (let i = 0; i <= cells; i++) {
|
|
147
|
+
let s = 0,
|
|
148
|
+
n = 0;
|
|
149
|
+
for (let d = -r; d <= r; d++) {
|
|
150
|
+
const ii = i + d;
|
|
151
|
+
if (ii < 0 || ii > cells) continue;
|
|
152
|
+
s += src[j * sy + ii * sx];
|
|
153
|
+
n++;
|
|
154
|
+
}
|
|
155
|
+
dst[j * sy + i * sx] = s / n;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Bilinear reads of the grids, and the terrain's own height as its finest mesh draws it (two triangles a cell, split the same way).
|
|
161
|
+
const cell = { i: 0, j: 0, tx: 0, tz: 0 };
|
|
162
|
+
function locate(x, z) {
|
|
163
|
+
let fx = (x + half) / spacing,
|
|
164
|
+
fz = (z + half) / spacing;
|
|
165
|
+
fx = clamp(fx, 0, cells - 1e-4);
|
|
166
|
+
fz = clamp(fz, 0, cells - 1e-4);
|
|
167
|
+
cell.i = fx | 0;
|
|
168
|
+
cell.j = fz | 0;
|
|
169
|
+
cell.tx = fx - cell.i;
|
|
170
|
+
cell.tz = fz - cell.j;
|
|
171
|
+
return cell.j * stride + cell.i;
|
|
172
|
+
}
|
|
173
|
+
function bilerp(g, k, n, c) {
|
|
174
|
+
const { tx, tz } = cell,
|
|
175
|
+
a = g[k * n + c],
|
|
176
|
+
b = g[(k + 1) * n + c],
|
|
177
|
+
d = g[(k + stride) * n + c],
|
|
178
|
+
e = g[(k + stride + 1) * n + c];
|
|
179
|
+
return a + (b - a) * tx + (d - a) * tz + (a - b - d + e) * tx * tz;
|
|
180
|
+
}
|
|
181
|
+
function meshHeight(x, z) {
|
|
182
|
+
const k = locate(x, z),
|
|
183
|
+
{ tx, tz } = cell,
|
|
184
|
+
a = heights[k],
|
|
185
|
+
b = heights[k + 1],
|
|
186
|
+
c = heights[k + stride],
|
|
187
|
+
d = heights[k + stride + 1];
|
|
188
|
+
return tx + tz <= 1 ? a + (b - a) * tx + (c - a) * tz : d + (c - d) * (1 - tx) + (b - d) * (1 - tz);
|
|
189
|
+
}
|
|
190
|
+
function cliffMask(x, z) {
|
|
191
|
+
return mask ? bilerp(mask, locate(x, z), 1, 0) : 0;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Joints: the nearest fracture-bounded block in a jittered grid of `size` metres. Writes the block's plane offset at (x, z), and the distance
|
|
195
|
+
// to its nearest edge, to `jnt`.
|
|
196
|
+
const jnt = { off: 0, edge: 0, id: 0 };
|
|
197
|
+
function joints(x, z, size, seed, depth) {
|
|
198
|
+
const gx = x / size,
|
|
199
|
+
gz = z / size,
|
|
200
|
+
ix = Math.floor(gx),
|
|
201
|
+
iz = Math.floor(gz);
|
|
202
|
+
let d1 = 1e9,
|
|
203
|
+
d2 = 1e9,
|
|
204
|
+
ax = 0,
|
|
205
|
+
az = 0,
|
|
206
|
+
bx = 0,
|
|
207
|
+
bz = 0,
|
|
208
|
+
id = 0;
|
|
209
|
+
for (let b = -1; b <= 1; b++)
|
|
210
|
+
for (let a = -1; a <= 1; a++) {
|
|
211
|
+
const cx = ix + a,
|
|
212
|
+
cz = iz + b,
|
|
213
|
+
sx = cx + 0.1 + 0.8 * hash2(cx, cz, seed),
|
|
214
|
+
sz = cz + 0.1 + 0.8 * hash2(cx, cz, seed + 1);
|
|
215
|
+
const d = (sx - gx) ** 2 + (sz - gz) ** 2;
|
|
216
|
+
if (d < d1) {
|
|
217
|
+
d2 = d1;
|
|
218
|
+
bx = ax;
|
|
219
|
+
bz = az;
|
|
220
|
+
d1 = d;
|
|
221
|
+
ax = sx;
|
|
222
|
+
az = sz;
|
|
223
|
+
id = cx * 7919 + cz;
|
|
224
|
+
} else if (d < d2) {
|
|
225
|
+
d2 = d;
|
|
226
|
+
bx = sx;
|
|
227
|
+
bz = sz;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const ex = bx - ax,
|
|
231
|
+
ez = bz - az,
|
|
232
|
+
el = Math.hypot(ex, ez) || 1;
|
|
233
|
+
jnt.edge = (((bx + ax) * 0.5 - gx) * ex) / el + (((bz + az) * 0.5 - gz) * ez) / el; // to the bisector between the two nearest seeds
|
|
234
|
+
jnt.edge *= size;
|
|
235
|
+
const h1 = hash2(id, 3, seed + 2),
|
|
236
|
+
h2 = hash2(id, 4, seed + 2) - 0.5,
|
|
237
|
+
h3 = hash2(id, 5, seed + 2) - 0.5;
|
|
238
|
+
jnt.off = depth * (0.35 + 0.65 * h1) + (h2 * (gx - ax) + h3 * (gz - az)) * size * 0.16 * depth;
|
|
239
|
+
jnt.off = Math.max(jnt.off, 0);
|
|
240
|
+
jnt.id = id;
|
|
241
|
+
return jnt;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// The rock surface at the ground point (x, z) for level of detail `lv`. Writes the position and what the shading needs to `o`:
|
|
245
|
+
// o.p (x, y, z), o.yy (the bedding height), o.bed (its tint 0..1), o.hard, o.cav (how far back in a crevice), o.m (cliff mask).
|
|
246
|
+
function cliffPoint(x, z, lv, o) {
|
|
247
|
+
const k = locate(x, z),
|
|
248
|
+
m = bilerp(mask, k, 1, 0),
|
|
249
|
+
bd = bilerp(band, k, 1, 0);
|
|
250
|
+
let nx = bilerp(nrm, k, 3, 0),
|
|
251
|
+
ny = bilerp(nrm, k, 3, 1),
|
|
252
|
+
nz = bilerp(nrm, k, 3, 2);
|
|
253
|
+
const nl = Math.hypot(nx, ny, nz) || 1;
|
|
254
|
+
nx /= nl;
|
|
255
|
+
ny /= nl;
|
|
256
|
+
nz /= nl;
|
|
257
|
+
const h = meshHeight(x, z),
|
|
258
|
+
g = levels[lv];
|
|
259
|
+
const yy = h + 0.045 * x - 0.022 * z + 18 * (fbm(x / 240, z / 240, 3, 533) - 0.5); // beds dip gently to the east and are folded a little
|
|
260
|
+
const b = bedAt(yy),
|
|
261
|
+
hard = clamp(BED.hard[b] + (bd - 0.5) * 0.3, 0, 1),
|
|
262
|
+
T = BED.thick[b],
|
|
263
|
+
u = 1 - (BED.top[b] - yy) / T;
|
|
264
|
+
// strata: a face then a ledge in each bed, at the angles a bed of this hardness keeps on a slope of angle th
|
|
265
|
+
const th = Math.acos(clamp(ny, 0.05, 1)),
|
|
266
|
+
sinT = Math.max(Math.sin(th), 0.3);
|
|
267
|
+
const face = lerp(th + 0.3, 1.62, hard * hard),
|
|
268
|
+
ledge = lerp(th - 0.15, 0.28, hard);
|
|
269
|
+
const tf = Math.tan(clamp(face - th, 0.05, 1.45)),
|
|
270
|
+
tl = Math.tan(clamp(th - ledge, 0.05, 1.3)),
|
|
271
|
+
f = tl / (tf + tl);
|
|
272
|
+
let A = f * (T / sinT) * tf;
|
|
273
|
+
const cap = lerp(0.3, 2.6, sstep(0.25, 0.8, hard)) * (0.75 + 0.5 * bd);
|
|
274
|
+
if (A > cap) A = cap;
|
|
275
|
+
// Use the actual triangle's rise here. A smoothed normal can miss a near-vertical
|
|
276
|
+
// patch: sampling alternating beds there gives one tooth per grid vertex.
|
|
277
|
+
const upper = cell.tx + cell.tz > 1;
|
|
278
|
+
const dhx = upper ? heights[k + stride + 1] - heights[k + stride] : heights[k + 1] - heights[k];
|
|
279
|
+
const dhz = upper ? heights[k + stride + 1] - heights[k + 1] : heights[k + stride] - heights[k];
|
|
280
|
+
const rise = Math.max((Math.abs(dhx) + Math.abs(dhz)) / spacing, 0.5);
|
|
281
|
+
const sampleRise = g * rise, resolved = sstep(4, 9, T / sampleRise);
|
|
282
|
+
const edgeFade = sstep(0, sampleRise * 1.5, T * Math.min(u, 1 - u));
|
|
283
|
+
const ra = u / f,
|
|
284
|
+
rb = (1 - u) / (1 - f),
|
|
285
|
+
tri = Math.max(0, (ra + rb - Math.sqrt((ra - rb) ** 2 + 0.06)) * 0.5); // the lip of each face worn round
|
|
286
|
+
const ds = A * tri * resolved;
|
|
287
|
+
// joints: large blocks through many beds, smaller ones that change from bed to bed, and the chips broken off their faces
|
|
288
|
+
joints(x, z, 7.5 + 3 * bd, 540, 1.0);
|
|
289
|
+
let dj = jnt.off;
|
|
290
|
+
const e1 = jnt.edge;
|
|
291
|
+
joints(x + b * 1.7, z - b * 2.3, 3.1, 560 + b, 0.5);
|
|
292
|
+
dj += jnt.off * (lv === 0 ? 1 : lv === 1 ? 0.5 : 0) * resolved * edgeFade;
|
|
293
|
+
const e2 = jnt.edge;
|
|
294
|
+
if (lv === 0) {
|
|
295
|
+
joints(x - b * 0.7, z + b * 1.3, 1.25, 580 + b, 0.2);
|
|
296
|
+
dj += jnt.off * resolved * edgeFade;
|
|
297
|
+
}
|
|
298
|
+
const groove = 0.6 * sstep(0.9, 0, e1) * (lv === 2 ? 0 : 1) + 0.3 * sstep(0.45, 0, e2) * (lv === 0 ? 1 : 0) * resolved * edgeFade;
|
|
299
|
+
dj = Math.max(dj - groove, 0);
|
|
300
|
+
// relief: broad buttresses and bays, and creased (not rounded) swells at a few metres
|
|
301
|
+
let dn = 1.0 * fbm(x / 31, z / 31, 3, 520) + 0.5 * vnoise((x + z) / 47, yy / 19, 521);
|
|
302
|
+
if (lv < 2) dn += 0.5 * Math.abs(vnoise((x + z) * 0.13, yy * 0.17, 522) + vnoise((x - z) * 0.13, yy * 0.17, 523) - 1);
|
|
303
|
+
const amp = sstep(0.3, 0.9, m) * (0.8 + 0.4 * bd),
|
|
304
|
+
base = lerp(-1.6, 0.3, sstep(0.12, 0.55, m));
|
|
305
|
+
// Ribs and gullies down the fall line: noise drawn out along the slope, sharp on the crests, for faces turned north or south and
|
|
306
|
+
// for faces turned east or west, mixed by which way this one faces
|
|
307
|
+
const wz = (nz * nz) / (nx * nx + nz * nz + 1e-6),
|
|
308
|
+
ridge = v => 1 - Math.abs(2 * v - 1);
|
|
309
|
+
let rib = lerp(ridge(vnoise(z / 11, x / 52, 541)), ridge(vnoise(x / 11, z / 52, 542)), wz) ** 1.6 * 2.4;
|
|
310
|
+
if (lv < 2) rib += lerp(ridge(vnoise(z / 4.2, x / 19, 543)), ridge(vnoise(x / 4.2, z / 19, 544)), wz) * 0.55;
|
|
311
|
+
// where the ground is near vertical the grid has few vertices up the face, so blocks and relief are kept low there (no flakes)
|
|
312
|
+
const sheer = lerp(1, 0.3, sstep(0.95, 1.22, th));
|
|
313
|
+
const off = base + amp * Math.min(ds + rib * lerp(0.5, 1, sstep(0.7, 1.1, th)) + (dj + dn) * sheer, 5);
|
|
314
|
+
o.p[0] = x + nx * off;
|
|
315
|
+
o.p[1] = h + ny * off;
|
|
316
|
+
o.p[2] = z + nz * off;
|
|
317
|
+
o.yy = yy;
|
|
318
|
+
o.bed = lerp(0.5, BED.tint[b], resolved * edgeFade);
|
|
319
|
+
o.hard = lerp(0.6, hard, resolved * edgeFade);
|
|
320
|
+
o.m = m;
|
|
321
|
+
o.h = h;
|
|
322
|
+
o.cav = clamp((1 - (A > 0 ? ds / A : 1)) ** 3 * sstep(0.25, 1.5, A) * 0.8 * resolved * edgeFade + groove * 0.9, 0, 1);
|
|
323
|
+
return o;
|
|
324
|
+
}
|
|
325
|
+
// The baked ground colour at (x, z), in linear light.
|
|
326
|
+
function groundColor(x, z, out) {
|
|
327
|
+
const k = locate(x, z);
|
|
328
|
+
for (let c = 0; c < 3; c++) out[c] = SRGB2LIN[Math.round(bilerp(colours, k, 3, c))];
|
|
329
|
+
return out;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// The plan as plain arrays (copies: a worker that planned keeps its own), and the page taking a plan a worker made as its own.
|
|
333
|
+
const cliffShapePlan = () => ({ mask: mask.slice(), nrm: nrm.slice(), band: band.slice() });
|
|
334
|
+
function takeCliffShape(plan, input) {
|
|
335
|
+
setGround(input);
|
|
336
|
+
({ mask, nrm, band } = plan); // the worker's copies, the page's own once they have crossed
|
|
337
|
+
}
|
|
338
|
+
return { levels, cliffMask, cliffPoint, cliffShapePlan, groundColor, planCliffShape, takeCliffShape };
|
|
339
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Geomorphable square tiles, using the same active cells at all three levels. Boxes
|
|
2
|
+
// carry raw attributes for a renderer and can be built on another thread or yielded between tiles.
|
|
3
|
+
export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundColor, tileSize = 32, boxSizes = [32, 128, 512] }) {
|
|
4
|
+
const half = size / 2, tilesSide = Math.ceil(size / tileSize);
|
|
5
|
+
const tiles = []; // the tiles with rock in them
|
|
6
|
+
|
|
7
|
+
// The coarse grid of the cliff mask, 2 m: a 2 m cell is rock if the mask is above zero at any corner. Every level meshes exactly these cells.
|
|
8
|
+
const MASK_SIDE = size / levels[2] + 1;
|
|
9
|
+
let region = null;
|
|
10
|
+
function planRegion() {
|
|
11
|
+
if (region) return; // planned already
|
|
12
|
+
const mn = new Float32Array(MASK_SIDE * MASK_SIDE);
|
|
13
|
+
region = new Uint8Array(MASK_SIDE * MASK_SIDE);
|
|
14
|
+
for (let b = 0; b < MASK_SIDE; b++)
|
|
15
|
+
for (let a = 0; a < MASK_SIDE; a++) mn[b * MASK_SIDE + a] = cliffMask(a * levels[2] - half, b * levels[2] - half);
|
|
16
|
+
for (let b = 0; b < MASK_SIDE - 1; b++)
|
|
17
|
+
for (let a = 0; a < MASK_SIDE - 1; a++) {
|
|
18
|
+
const k = b * MASK_SIDE + a;
|
|
19
|
+
if (Math.max(mn[k], mn[k + 1], mn[k + MASK_SIDE], mn[k + MASK_SIDE + 1]) > 0.004) region[k] = 1;
|
|
20
|
+
}
|
|
21
|
+
for (let tj = 0; tj < tilesSide; tj++)
|
|
22
|
+
for (let ti = 0; ti < tilesSide; ti++) {
|
|
23
|
+
const n = tileSize / levels[2];
|
|
24
|
+
let any = false;
|
|
25
|
+
for (let b = tj * n; b < (tj + 1) * n && b < MASK_SIDE - 1 && !any; b++)
|
|
26
|
+
for (let a = ti * n; a < (ti + 1) * n && a < MASK_SIDE - 1; a++)
|
|
27
|
+
if (region[b * MASK_SIDE + a]) {
|
|
28
|
+
any = true;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
if (any) tiles.push({ id: tj * tilesSide + ti, ti, tj, x0: ti * tileSize - half, z0: tj * tileSize - half, y0: 1e9, y1: -1e9, want: 2 });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const inRegion = (x, z) => {
|
|
35
|
+
const a = Math.floor((x + half) / levels[2]),
|
|
36
|
+
b = Math.floor((z + half) / levels[2]);
|
|
37
|
+
return a >= 0 && b >= 0 && a < MASK_SIDE - 1 && b < MASK_SIDE - 1 && region[b * MASK_SIDE + a] === 1;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// One level's node grid over a tile, with a ring of one node round it for the normals: positions for every node that a rock cell touches
|
|
41
|
+
// (and their ring), normals for the cell corners.
|
|
42
|
+
const pt = { p: [0, 0, 0] };
|
|
43
|
+
function tileGrid(t, lv, keepAttrs) {
|
|
44
|
+
const g = levels[lv],
|
|
45
|
+
n = tileSize / g,
|
|
46
|
+
w = n + 3,
|
|
47
|
+
used = new Uint8Array(w * w),
|
|
48
|
+
corner = new Uint8Array(w * w);
|
|
49
|
+
for (let b = 0; b < n; b++)
|
|
50
|
+
for (let a = 0; a < n; a++) {
|
|
51
|
+
if (!inRegion(t.x0 + (a + 0.5) * g, t.z0 + (b + 0.5) * g)) continue;
|
|
52
|
+
for (let j = 0; j < 4; j++) for (let i = 0; i < 4; i++) used[(b + j) * w + a + i] = 1;
|
|
53
|
+
for (let j = 1; j < 3; j++) for (let i = 1; i < 3; i++) corner[(b + j) * w + a + i] = 1;
|
|
54
|
+
}
|
|
55
|
+
const P = new Float32Array(w * w * 3),
|
|
56
|
+
N = new Float32Array(w * w * 3),
|
|
57
|
+
A = keepAttrs ? new Float32Array(w * w * 6) : null;
|
|
58
|
+
for (let b = 0; b < w; b++)
|
|
59
|
+
for (let a = 0; a < w; a++) {
|
|
60
|
+
const k = b * w + a;
|
|
61
|
+
if (!used[k]) continue;
|
|
62
|
+
cliffPoint(t.x0 + (a - 1) * g, t.z0 + (b - 1) * g, lv, pt);
|
|
63
|
+
P[k * 3] = pt.p[0];
|
|
64
|
+
P[k * 3 + 1] = pt.p[1];
|
|
65
|
+
P[k * 3 + 2] = pt.p[2];
|
|
66
|
+
if (A) {
|
|
67
|
+
A[k * 6] = pt.bed;
|
|
68
|
+
A[k * 6 + 1] = pt.hard;
|
|
69
|
+
A[k * 6 + 2] = pt.cav;
|
|
70
|
+
A[k * 6 + 3] = pt.m;
|
|
71
|
+
A[k * 6 + 4] = pt.yy;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (let b = 1; b < w - 1; b++)
|
|
75
|
+
for (let a = 1; a < w - 1; a++) {
|
|
76
|
+
const k = b * w + a;
|
|
77
|
+
if (!corner[k]) continue;
|
|
78
|
+
const ax = P[(k + 1) * 3] - P[(k - 1) * 3],
|
|
79
|
+
ay = P[(k + 1) * 3 + 1] - P[(k - 1) * 3 + 1],
|
|
80
|
+
az = P[(k + 1) * 3 + 2] - P[(k - 1) * 3 + 2];
|
|
81
|
+
const bx = P[(k + w) * 3] - P[(k - w) * 3],
|
|
82
|
+
by = P[(k + w) * 3 + 1] - P[(k - w) * 3 + 1],
|
|
83
|
+
bz = P[(k + w) * 3 + 2] - P[(k - w) * 3 + 2];
|
|
84
|
+
let nx = by * az - bz * ay,
|
|
85
|
+
ny = bz * ax - bx * az,
|
|
86
|
+
nz = bx * ay - by * ax;
|
|
87
|
+
const l = Math.hypot(nx, ny, nz) || 1;
|
|
88
|
+
nx /= l;
|
|
89
|
+
ny /= l;
|
|
90
|
+
nz /= l;
|
|
91
|
+
N[k * 3] = nx;
|
|
92
|
+
N[k * 3 + 1] = ny;
|
|
93
|
+
N[k * 3 + 2] = nz;
|
|
94
|
+
}
|
|
95
|
+
return { g, n, w, P, N, A, corner };
|
|
96
|
+
}
|
|
97
|
+
// A coarser grid's surface at a point of the tile, interpolated over its triangles as that level's mesh draws them.
|
|
98
|
+
function sample(c, lx, lz, arr, out) {
|
|
99
|
+
const fx = Math.min(lx / c.g, c.n - 1e-6),
|
|
100
|
+
fz = Math.min(lz / c.g, c.n - 1e-6),
|
|
101
|
+
i = Math.floor(fx),
|
|
102
|
+
j = Math.floor(fz),
|
|
103
|
+
tx = fx - i,
|
|
104
|
+
tz = fz - j;
|
|
105
|
+
const k = (j + 1) * c.w + i + 1,
|
|
106
|
+
a = k * 3,
|
|
107
|
+
b = (k + 1) * 3,
|
|
108
|
+
d = (k + c.w) * 3,
|
|
109
|
+
e = (k + c.w + 1) * 3;
|
|
110
|
+
for (let q = 0; q < 3; q++)
|
|
111
|
+
out[q] =
|
|
112
|
+
tx + tz <= 1
|
|
113
|
+
? arr[a + q] + (arr[b + q] - arr[a + q]) * tx + (arr[d + q] - arr[a + q]) * tz
|
|
114
|
+
: arr[e + q] + (arr[d + q] - arr[e + q]) * (1 - tx) + (arr[b + q] - arr[e + q]) * (1 - tz);
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// The arrays of one level of a box of tiles, as a generator that yields now and then so the work can be spread over frames; it returns
|
|
119
|
+
// { attributes: { name: [array, itemSize] }, index, bounds: [tile id, lowest y, highest y, ...] }.
|
|
120
|
+
function* boxArrays(lv, list) {
|
|
121
|
+
const pos = [],
|
|
122
|
+
nor = [],
|
|
123
|
+
m1 = [],
|
|
124
|
+
n1 = [],
|
|
125
|
+
m2 = [],
|
|
126
|
+
n2 = [],
|
|
127
|
+
col = [],
|
|
128
|
+
rock = [],
|
|
129
|
+
yy = [],
|
|
130
|
+
par = [],
|
|
131
|
+
idx = [],
|
|
132
|
+
s3 = [0, 0, 0],
|
|
133
|
+
c3 = [0, 0, 0],
|
|
134
|
+
bounds = [];
|
|
135
|
+
for (const t of list) {
|
|
136
|
+
const f = tileGrid(t, lv, true);
|
|
137
|
+
yield;
|
|
138
|
+
const c1 = lv < 2 ? tileGrid(t, lv + 1, false) : null,
|
|
139
|
+
c2 = lv < 1 ? tileGrid(t, 2, false) : null;
|
|
140
|
+
if (c1) yield;
|
|
141
|
+
const map = new Int32Array(f.w * f.w).fill(-1),
|
|
142
|
+
base = pos.length / 3;
|
|
143
|
+
let vi = base,
|
|
144
|
+
y0 = 1e9,
|
|
145
|
+
y1 = -1e9;
|
|
146
|
+
for (let b = 1; b < f.w - 1; b++)
|
|
147
|
+
for (let a = 1; a < f.w - 1; a++) {
|
|
148
|
+
const k = b * f.w + a;
|
|
149
|
+
if (!f.corner[k]) continue;
|
|
150
|
+
map[k] = vi++;
|
|
151
|
+
pos.push(f.P[k * 3], f.P[k * 3 + 1], f.P[k * 3 + 2]);
|
|
152
|
+
nor.push(f.N[k * 3], f.N[k * 3 + 1], f.N[k * 3 + 2]);
|
|
153
|
+
const lx = (a - 1) * f.g,
|
|
154
|
+
lz = (b - 1) * f.g,
|
|
155
|
+
x = t.x0 + lx,
|
|
156
|
+
z = t.z0 + lz;
|
|
157
|
+
if (c1) {
|
|
158
|
+
m1.push(...sample(c1, lx, lz, c1.P, s3));
|
|
159
|
+
const nn = sample(c1, lx, lz, c1.N, s3),
|
|
160
|
+
l = Math.hypot(...nn) || 1;
|
|
161
|
+
n1.push(nn[0] / l, nn[1] / l, nn[2] / l);
|
|
162
|
+
}
|
|
163
|
+
if (c2) {
|
|
164
|
+
m2.push(...sample(c2, lx, lz, c2.P, s3));
|
|
165
|
+
const nn = sample(c2, lx, lz, c2.N, s3),
|
|
166
|
+
l = Math.hypot(...nn) || 1;
|
|
167
|
+
n2.push(nn[0] / l, nn[1] / l, nn[2] / l);
|
|
168
|
+
}
|
|
169
|
+
col.push(...groundColor(x, z, c3));
|
|
170
|
+
rock.push(f.A[k * 6], f.A[k * 6 + 1], f.A[k * 6 + 2], f.A[k * 6 + 3]);
|
|
171
|
+
yy.push(f.A[k * 6 + 4]);
|
|
172
|
+
par.push(x, z, t.id);
|
|
173
|
+
y0 = Math.min(y0, f.P[k * 3 + 1]);
|
|
174
|
+
y1 = Math.max(y1, f.P[k * 3 + 1]);
|
|
175
|
+
}
|
|
176
|
+
bounds.push(t.id, y0, y1);
|
|
177
|
+
for (let b = 1; b < f.w - 2; b++)
|
|
178
|
+
for (let a = 1; a < f.w - 2; a++) {
|
|
179
|
+
if (!inRegion(t.x0 + (a - 0.5) * f.g, t.z0 + (b - 0.5) * f.g)) continue;
|
|
180
|
+
const i00 = map[b * f.w + a],
|
|
181
|
+
i10 = map[b * f.w + a + 1],
|
|
182
|
+
i01 = map[(b + 1) * f.w + a],
|
|
183
|
+
i11 = map[(b + 1) * f.w + a + 1];
|
|
184
|
+
idx.push(i00, i01, i10, i10, i01, i11);
|
|
185
|
+
}
|
|
186
|
+
yield;
|
|
187
|
+
}
|
|
188
|
+
const F = (a, n) => [new Float32Array(a), n],
|
|
189
|
+
attributes = { position: F(pos, 3), normal: F(nor, 3), color: F(col, 3), aRock: F(rock, 4), aYY: F(yy, 1), aParam: F(par, 3) };
|
|
190
|
+
if (lv < 2) Object.assign(attributes, { aM1: F(m1, 3), aN1: F(n1, 3) });
|
|
191
|
+
if (lv < 1) Object.assign(attributes, { aM2: F(m2, 3), aN2: F(n2, 3) });
|
|
192
|
+
return { attributes, index: pos.length / 3 > 65535 ? new Uint32Array(idx) : new Uint16Array(idx), bounds };
|
|
193
|
+
}
|
|
194
|
+
const boxKey = (t, lv) => Math.floor((t.x0 + half) / boxSizes[lv]) + ':' + Math.floor((t.z0 + half) / boxSizes[lv]);
|
|
195
|
+
function tilesIn(lv, key) {
|
|
196
|
+
return tiles.filter(t => boxKey(t, lv) === key);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// The arrays of the box `key` at level `lv`, all at once.
|
|
200
|
+
function boxNow(lv, key) {
|
|
201
|
+
const it = boxArrays(lv, tilesIn(lv, key));
|
|
202
|
+
for (;;) {
|
|
203
|
+
const step = it.next();
|
|
204
|
+
if (step.done) return step.value;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// The tiles with rock and the 2 m cells that are rock, as plain data (a copy of the cells), and the page taking them from a worker.
|
|
209
|
+
const regionPlan = () => ({ region: region.slice(), tiles });
|
|
210
|
+
function takeRegion(plan) {
|
|
211
|
+
region = plan.region;
|
|
212
|
+
if (plan.tiles !== tiles) { tiles.length = 0; tiles.push(...plan.tiles); }
|
|
213
|
+
}
|
|
214
|
+
return { boxArrays, boxKey, boxNow, planRegion, regionPlan, takeRegion, tileSize, tiles, tilesSide, tilesIn };
|
|
215
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { clamp, lerp, sstep } from '../math/scalar.js';
|
|
3
|
+
|
|
4
|
+
// A stream's ribbon of water along pts, width metres wide, its surface at surface(t) and its bed's heights in bed (for how
|
|
5
|
+
// fast it runs). It rises at a spring: a small pool, then a rill that widens into the stream over the first `grows` metres, so
|
|
6
|
+
// the ribbon begins as a point, never as a square end. With `banks` ({ ground(x, z), reach, grows }) the ribbon is laid `reach`
|
|
7
|
+
// metres wider than the stream on each side and knows how deep it lies over the ground, so its edge is where the bank rises
|
|
8
|
+
// out of it; without, it is a strip of the stream's own width with soft sides. `fadeAt(x, z)`, if given, is how much of the
|
|
9
|
+
// ribbon shows at a point of its line (0 to 1): where it runs on into still water drawn beside it (a lake's sheet), the two
|
|
10
|
+
// cross over, so it has no square end there either.
|
|
11
|
+
const DEEP = 8, // the depth the shader takes for water that knows no bed
|
|
12
|
+
FAR = 99, // how far from a spring's eye water is that has none
|
|
13
|
+
MURK = 7, // a stream's water hides its bed this many times sooner than the lake's
|
|
14
|
+
ACROSS = [-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1], // where a cross-section's vertices lie, as shares of its half width
|
|
15
|
+
SPRING = { grows: 3, rill: 0.2, pool: 0.1, eye: 0.43, size: 0.31, tip: 0.35, fade: 0.2 }; // lengths in stream widths, widths as shares of the half width
|
|
16
|
+
// The share of its half width a ribbon has `run` metres from its spring.
|
|
17
|
+
function headShare(run, width, grows) {
|
|
18
|
+
const rill = lerp(SPRING.rill, 1, sstep(0, grows, run)),
|
|
19
|
+
pool = SPRING.pool * Math.exp(-(((run - springEye(width)) / (SPRING.size * width)) ** 2));
|
|
20
|
+
return (rill + pool) * Math.sqrt(clamp(run / (SPRING.tip * width), 0, 1));
|
|
21
|
+
}
|
|
22
|
+
// Past the first ground that stands out of the water there is none, whatever lies beyond (the land may fall away again behind
|
|
23
|
+
// a bank): walks the cross-section that starts at vertex `first` from its middle to each side, and takes the water away after
|
|
24
|
+
// the first bank.
|
|
25
|
+
const MIDDLE = ACROSS.indexOf(0),
|
|
26
|
+
DRY = -1;
|
|
27
|
+
function shutBeyondBanks(bank, first) {
|
|
28
|
+
for (const step of [-1, 1]) {
|
|
29
|
+
let dry = false;
|
|
30
|
+
for (let k = MIDDLE + step; k >= 0 && k < ACROSS.length; k += step) {
|
|
31
|
+
const depth = (first + k) * 4;
|
|
32
|
+
if (dry) bank[depth] = Math.min(bank[depth], DRY);
|
|
33
|
+
else dry = bank[depth] < 0;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function streamGeometry(pts, width, surface, bed, banks = null, fadeAt = null) {
|
|
38
|
+
const n = pts.length,
|
|
39
|
+
across = ACROSS.length,
|
|
40
|
+
pos = new Float32Array(n * across * 3),
|
|
41
|
+
flow = new Float32Array(n * across * 3),
|
|
42
|
+
current = new Float32Array(n * across * 2),
|
|
43
|
+
bank = new Float32Array(n * across * 4),
|
|
44
|
+
idx = [],
|
|
45
|
+
reach = width * 0.5 + (banks ? banks.reach : 0),
|
|
46
|
+
grows = banks?.grows ?? SPRING.grows * width;
|
|
47
|
+
let run = 0;
|
|
48
|
+
for (let i = 0; i < n; i++) {
|
|
49
|
+
const a = pts[Math.max(0, i - 1)],
|
|
50
|
+
b = pts[Math.min(n - 1, i + 1)];
|
|
51
|
+
let dx = b[0] - a[0],
|
|
52
|
+
dz = b[1] - a[1];
|
|
53
|
+
const l = Math.hypot(dx, dz) || 1;
|
|
54
|
+
dx /= l;
|
|
55
|
+
dz /= l;
|
|
56
|
+
if (i > 0) run += Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]);
|
|
57
|
+
const y = surface(i / (n - 1)),
|
|
58
|
+
grade = sstep(0.015, 0.32, (bed[Math.max(i - 3, 0)] - bed[Math.min(i + 3, n - 1)]) / 9),
|
|
59
|
+
share = headShare(run, width, grows),
|
|
60
|
+
rising = sstep(0, SPRING.fade * width, run) * (fadeAt ? fadeAt(pts[i][0], pts[i][1]) : 1);
|
|
61
|
+
for (let k = 0; k < across; k++) {
|
|
62
|
+
const side = ACROSS[k],
|
|
63
|
+
// a strip with no banks to shape it wanders a little in width
|
|
64
|
+
wander = banks
|
|
65
|
+
? 1
|
|
66
|
+
: 0.89 + 0.09 * Math.sin(run * 0.23 + Math.sign(side) * 1.2) + 0.025 * Math.sin(run * 0.87 - Math.sign(side) * 1.4),
|
|
67
|
+
off = side * reach * share * wander,
|
|
68
|
+
x = pts[i][0] - dz * off,
|
|
69
|
+
z = pts[i][1] + dx * off,
|
|
70
|
+
v = i * across + k;
|
|
71
|
+
pos.set([x, y, z], v * 3);
|
|
72
|
+
flow.set([off / (width * 0.5), run, grade], v * 3);
|
|
73
|
+
current.set([dx, dz], v * 2);
|
|
74
|
+
bank.set(
|
|
75
|
+
[banks ? (y - banks.ground(x, z)) * MURK : DEEP, rising * sstep(1, 0.75, Math.abs(side)), run - springEye(width), off],
|
|
76
|
+
v * 4,
|
|
77
|
+
);
|
|
78
|
+
if (i < n - 1 && k < across - 1) idx.push(v, v + 1, v + across, v + 1, v + across + 1, v + across);
|
|
79
|
+
}
|
|
80
|
+
if (banks) shutBeyondBanks(bank, i * across);
|
|
81
|
+
}
|
|
82
|
+
const g = new THREE.BufferGeometry();
|
|
83
|
+
g.setAttribute('position', new THREE.BufferAttribute(pos, 3));
|
|
84
|
+
g.setAttribute('aFlow', new THREE.BufferAttribute(flow, 3));
|
|
85
|
+
g.setAttribute('aCurrent', new THREE.BufferAttribute(current, 2));
|
|
86
|
+
g.setAttribute('aBank', new THREE.BufferAttribute(bank, 4));
|
|
87
|
+
g.setIndex(idx);
|
|
88
|
+
return g;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// How far below its spring a stream of this width has the eye of its pool, in metres.
|
|
92
|
+
export const springEye = width => SPRING.eye * width;
|
|
93
|
+
|
|
94
|
+
export { DEEP, FAR };
|