roamwright 0.4.0 → 0.20.1
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 +3 -1
- package/README.md +11 -34
- package/geometry/bedding.js +141 -0
- package/geometry/builder-cells.js +42 -0
- package/geometry/builder-frame.js +47 -0
- package/geometry/builder.js +189 -177
- package/geometry/cell-mask.js +25 -0
- package/geometry/creased-sheet.js +34 -34
- package/geometry/ear.js +36 -36
- package/geometry/erosion.js +65 -42
- package/geometry/face-grid.js +65 -65
- package/geometry/foliage.js +10 -10
- package/geometry/hair-gathers.js +41 -41
- package/geometry/hair-locks.js +41 -41
- package/geometry/hair-patch.js +18 -18
- package/geometry/hair-shell.js +31 -31
- package/geometry/head-profile.js +15 -15
- package/geometry/head-shape.js +77 -77
- package/geometry/heightfield.js +27 -18
- package/geometry/index-parts.js +2 -2
- package/geometry/instance-visibility.js +12 -12
- package/geometry/instances.js +15 -15
- package/geometry/outline-grid.js +7 -7
- package/geometry/part-ranges.js +3 -3
- package/geometry/path-field.js +63 -33
- package/geometry/placement.js +52 -52
- package/geometry/point-buckets.js +50 -0
- package/geometry/primitives.js +63 -47
- package/geometry/sagging-tube.js +27 -27
- package/geometry/sampled-surface.js +109 -77
- package/geometry/scatter-grid.js +46 -6
- package/geometry/sculpt.js +93 -93
- package/geometry/shore-grid.js +77 -34
- package/geometry/strata-field.js +92 -92
- package/geometry/strata-tiles.js +91 -91
- package/geometry/stream.js +70 -48
- package/geometry/terrain-chunks.js +87 -82
- package/geometry/terrain-extension.js +6 -6
- package/index.js +5 -0
- package/math/cell-edge.js +16 -16
- package/math/cell-key.js +6 -0
- package/math/circle-clearances.js +19 -0
- package/math/cloth-textures.js +6 -6
- package/math/cloud-textures.js +20 -20
- package/math/color.js +14 -14
- package/math/curve.js +22 -22
- package/math/mountain-ranges.js +216 -165
- package/math/noise.js +101 -49
- package/math/particle-noise.js +28 -28
- package/math/periodic.js +5 -5
- package/math/random.js +24 -15
- package/math/rectangles.js +52 -0
- package/math/rock-texture.js +7 -7
- package/math/scalar.js +5 -5
- package/math/surface-textures.js +54 -54
- package/motion/arm-reach.js +24 -24
- package/motion/ballistic-arc.js +11 -0
- package/motion/biped-cycle.js +44 -0
- package/motion/biped-leg.js +37 -0
- package/motion/biped-stride.js +98 -122
- package/motion/contact-transition.js +33 -0
- package/motion/crowd-alarms.js +39 -39
- package/motion/crowd-routine.js +94 -65
- package/motion/daily-clock.js +9 -9
- package/motion/deck-surface.js +38 -0
- package/motion/ground-lookahead.js +5 -5
- package/motion/instance-sight.js +8 -8
- package/motion/player.js +36 -28
- package/motion/quadruped.js +155 -155
- package/motion/spaces.js +32 -32
- package/motion/weather.js +73 -24
- package/motion/wind.js +32 -13
- package/package.json +7 -3
- package/rendering/caster-motion.js +5 -5
- package/rendering/cell-lod.js +138 -82
- package/rendering/cloud-dome.js +28 -28
- package/rendering/cloud-field.js +2 -2
- package/rendering/compilation-schedule.js +68 -11
- package/rendering/contact-shadows.js +2 -2
- package/rendering/context.js +12 -0
- package/rendering/crowd-skinning.js +13 -13
- package/rendering/crust.js +18 -12
- package/rendering/daylight.js +3 -3
- package/rendering/detail-range.js +28 -0
- package/rendering/dispose-meshes.js +18 -0
- package/rendering/dof.js +9 -9
- package/rendering/eye-adapt.js +14 -14
- package/rendering/far-shadow.js +8 -8
- package/rendering/flocks.js +78 -78
- package/rendering/garment-material.js +17 -17
- package/rendering/glow-lines.js +13 -13
- package/rendering/hair-strands.js +20 -20
- package/rendering/height-probe.js +149 -19
- package/rendering/label-layout.js +46 -0
- package/rendering/label-range.js +38 -0
- package/rendering/light-budget.js +52 -52
- package/rendering/light-shafts.js +23 -23
- package/rendering/local-lights.js +15 -4
- package/rendering/material-lod.js +49 -49
- package/rendering/particle-emitters.js +90 -90
- package/rendering/particles.js +78 -67
- package/rendering/piece-batch.js +17 -6
- package/rendering/planar-reflection.js +38 -38
- package/rendering/post-shaders.js +17 -13
- package/rendering/post.js +71 -71
- package/rendering/rain-streaks.js +67 -0
- package/rendering/road-surface.js +11 -11
- package/rendering/sea-shaders.js +11 -11
- package/rendering/shader-warmup.js +29 -16
- package/rendering/shadow-alignment.js +4 -4
- package/rendering/shadow-depth.js +7 -7
- package/rendering/sky-occlusion.js +16 -16
- package/rendering/sky.js +16 -6
- package/rendering/stage-lights.js +6 -6
- package/rendering/static-point-shadow.js +3 -3
- package/rendering/still-casters.js +1 -1
- package/rendering/strata-material.js +10 -10
- package/rendering/stream-flow.js +10 -10
- package/rendering/taa.js +26 -26
- package/rendering/talus.js +15 -15
- package/rendering/terrain-rock.js +7 -7
- package/rendering/texture-fills.js +8 -8
- package/rendering/textures.js +28 -28
- package/rendering/wakes.js +24 -24
- package/rendering/water-material.js +36 -23
- package/rendering/world-matrices.js +38 -38
- package/runtime/diagnostic-log.js +2 -2
- package/runtime/first-use.js +33 -28
- package/runtime/frame-pacing.js +33 -0
- package/runtime/gpu-timing.js +7 -7
- package/runtime/grid-cache.js +1 -1
- package/runtime/grid-rows.js +44 -5
- package/runtime/hand-over.js +41 -23
- package/runtime/pool.js +58 -16
- package/runtime/quality-fit.js +1 -1
- package/runtime/search.js +22 -22
- package/runtime/spatial-audio.js +13 -13
- package/runtime/withdrawable-task.js +35 -0
- package/runtime/worker.js +7 -0
package/geometry/strata-tiles.js
CHANGED
|
@@ -11,20 +11,20 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
|
|
|
11
11
|
if (region) return; // planned already
|
|
12
12
|
const mn = new Float32Array(MASK_SIDE * MASK_SIDE);
|
|
13
13
|
region = new Uint8Array(MASK_SIDE * MASK_SIDE);
|
|
14
|
-
for (let
|
|
15
|
-
for (let
|
|
16
|
-
for (let
|
|
17
|
-
for (let
|
|
18
|
-
const k =
|
|
14
|
+
for (let row = 0; row < MASK_SIDE; row++)
|
|
15
|
+
for (let column = 0; column < MASK_SIDE; column++) mn[row * MASK_SIDE + column] = cliffMask(column * levels[2] - half, row * levels[2] - half);
|
|
16
|
+
for (let row = 0; row < MASK_SIDE - 1; row++)
|
|
17
|
+
for (let column = 0; column < MASK_SIDE - 1; column++) {
|
|
18
|
+
const k = row * MASK_SIDE + column;
|
|
19
19
|
if (Math.max(mn[k], mn[k + 1], mn[k + MASK_SIDE], mn[k + MASK_SIDE + 1]) > 0.004) region[k] = 1;
|
|
20
20
|
}
|
|
21
21
|
for (let tj = 0; tj < tilesSide; tj++)
|
|
22
22
|
for (let ti = 0; ti < tilesSide; ti++) {
|
|
23
23
|
const n = tileSize / levels[2];
|
|
24
24
|
let any = false;
|
|
25
|
-
for (let
|
|
26
|
-
for (let
|
|
27
|
-
if (region[
|
|
25
|
+
for (let row = tj * n; row < (tj + 1) * n && row < MASK_SIDE - 1 && !any; row++)
|
|
26
|
+
for (let column = ti * n; column < (ti + 1) * n && column < MASK_SIDE - 1; column++)
|
|
27
|
+
if (region[row * MASK_SIDE + column]) {
|
|
28
28
|
any = true;
|
|
29
29
|
break;
|
|
30
30
|
}
|
|
@@ -32,55 +32,55 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
const inRegion = (x, z) => {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
return
|
|
35
|
+
const column = Math.floor((x + half) / levels[2]),
|
|
36
|
+
row = Math.floor((z + half) / levels[2]);
|
|
37
|
+
return column >= 0 && row >= 0 && column < MASK_SIDE - 1 && row < MASK_SIDE - 1 && region[row * MASK_SIDE + column] === 1;
|
|
38
38
|
};
|
|
39
39
|
|
|
40
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
41
|
// (and their ring), normals for the cell corners.
|
|
42
42
|
const pt = { p: [0, 0, 0] };
|
|
43
|
-
function tileGrid(t,
|
|
44
|
-
const g = levels[
|
|
43
|
+
function tileGrid(t, levelIndex, keepAttrs) {
|
|
44
|
+
const g = levels[levelIndex],
|
|
45
45
|
n = tileSize / g,
|
|
46
46
|
w = n + 3,
|
|
47
47
|
used = new Uint8Array(w * w),
|
|
48
48
|
corner = new Uint8Array(w * w);
|
|
49
|
-
for (let
|
|
50
|
-
for (let
|
|
51
|
-
if (!inRegion(t.x0 + (
|
|
52
|
-
for (let j = 0; j < 4; j++) for (let i = 0; i < 4; i++) used[(
|
|
53
|
-
for (let j = 1; j < 3; j++) for (let i = 1; i < 3; i++) corner[(
|
|
49
|
+
for (let row = 0; row < n; row++)
|
|
50
|
+
for (let column = 0; column < n; column++) {
|
|
51
|
+
if (!inRegion(t.x0 + (column + 0.5) * g, t.z0 + (row + 0.5) * g)) continue;
|
|
52
|
+
for (let j = 0; j < 4; j++) for (let i = 0; i < 4; i++) used[(row + j) * w + column + i] = 1;
|
|
53
|
+
for (let j = 1; j < 3; j++) for (let i = 1; i < 3; i++) corner[(row + j) * w + column + i] = 1;
|
|
54
54
|
}
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
for (let
|
|
59
|
-
for (let
|
|
60
|
-
const k =
|
|
55
|
+
const positions = new Float32Array(w * w * 3),
|
|
56
|
+
normals = new Float32Array(w * w * 3),
|
|
57
|
+
attributes = keepAttrs ? new Float32Array(w * w * 6) : null;
|
|
58
|
+
for (let row = 0; row < w; row++)
|
|
59
|
+
for (let column = 0; column < w; column++) {
|
|
60
|
+
const k = row * w + column;
|
|
61
61
|
if (!used[k]) continue;
|
|
62
|
-
cliffPoint(t.x0 + (
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
62
|
+
cliffPoint(t.x0 + (column - 1) * g, t.z0 + (row - 1) * g, levelIndex, pt);
|
|
63
|
+
positions[k * 3] = pt.p[0];
|
|
64
|
+
positions[k * 3 + 1] = pt.p[1];
|
|
65
|
+
positions[k * 3 + 2] = pt.p[2];
|
|
66
|
+
if (attributes) {
|
|
67
|
+
attributes[k * 6] = pt.bed;
|
|
68
|
+
attributes[k * 6 + 1] = pt.hard;
|
|
69
|
+
attributes[k * 6 + 2] = pt.cav;
|
|
70
|
+
attributes[k * 6 + 3] = pt.m;
|
|
71
|
+
attributes[k * 6 + 4] = pt.yy;
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
-
for (let
|
|
75
|
-
for (let
|
|
76
|
-
const k =
|
|
74
|
+
for (let row = 1; row < w - 1; row++)
|
|
75
|
+
for (let column = 1; column < w - 1; column++) {
|
|
76
|
+
const k = row * w + column;
|
|
77
77
|
if (!corner[k]) continue;
|
|
78
|
-
const ax =
|
|
79
|
-
ay =
|
|
80
|
-
az =
|
|
81
|
-
const bx =
|
|
82
|
-
by =
|
|
83
|
-
bz =
|
|
78
|
+
const ax = positions[(k + 1) * 3] - positions[(k - 1) * 3],
|
|
79
|
+
ay = positions[(k + 1) * 3 + 1] - positions[(k - 1) * 3 + 1],
|
|
80
|
+
az = positions[(k + 1) * 3 + 2] - positions[(k - 1) * 3 + 2];
|
|
81
|
+
const bx = positions[(k + w) * 3] - positions[(k - w) * 3],
|
|
82
|
+
by = positions[(k + w) * 3 + 1] - positions[(k - w) * 3 + 1],
|
|
83
|
+
bz = positions[(k + w) * 3 + 2] - positions[(k - w) * 3 + 2];
|
|
84
84
|
let nx = by * az - bz * ay,
|
|
85
85
|
ny = bz * ax - bx * az,
|
|
86
86
|
nz = bx * ay - by * ax;
|
|
@@ -88,38 +88,38 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
|
|
|
88
88
|
nx /= l;
|
|
89
89
|
ny /= l;
|
|
90
90
|
nz /= l;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
normals[k * 3] = nx;
|
|
92
|
+
normals[k * 3 + 1] = ny;
|
|
93
|
+
normals[k * 3 + 2] = nz;
|
|
94
94
|
}
|
|
95
|
-
return { g, n, w, P, N, A, corner };
|
|
95
|
+
return { g, n, w, P: positions, N: normals, A: attributes, corner };
|
|
96
96
|
}
|
|
97
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(
|
|
99
|
-
const fx = Math.min(lx /
|
|
100
|
-
fz = Math.min(lz /
|
|
98
|
+
function sample(tile, lx, lz, arr, out) {
|
|
99
|
+
const fx = Math.min(lx / tile.g, tile.n - 1e-6),
|
|
100
|
+
fz = Math.min(lz / tile.g, tile.n - 1e-6),
|
|
101
101
|
i = Math.floor(fx),
|
|
102
102
|
j = Math.floor(fz),
|
|
103
103
|
tx = fx - i,
|
|
104
104
|
tz = fz - j;
|
|
105
|
-
const k = (j + 1) *
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
e = (k +
|
|
105
|
+
const k = (j + 1) * tile.w + i + 1,
|
|
106
|
+
nearLeft = k * 3,
|
|
107
|
+
nearRight = (k + 1) * 3,
|
|
108
|
+
farLeft = (k + tile.w) * 3,
|
|
109
|
+
e = (k + tile.w + 1) * 3;
|
|
110
110
|
for (let q = 0; q < 3; q++)
|
|
111
111
|
out[q] =
|
|
112
112
|
tx + tz <= 1
|
|
113
|
-
? arr[
|
|
114
|
-
: arr[e + q] + (arr[
|
|
113
|
+
? arr[nearLeft + q] + (arr[nearRight + q] - arr[nearLeft + q]) * tx + (arr[farLeft + q] - arr[nearLeft + q]) * tz
|
|
114
|
+
: arr[e + q] + (arr[farLeft + q] - arr[e + q]) * (1 - tx) + (arr[nearRight + q] - arr[e + q]) * (1 - tz);
|
|
115
115
|
return out;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
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
119
|
// { attributes: { name: [array, itemSize] }, index, bounds: [tile id, lowest y, highest y, ...] }.
|
|
120
|
-
function* boxArrays(
|
|
121
|
-
const
|
|
122
|
-
|
|
120
|
+
function* boxArrays(levelIndex, list) {
|
|
121
|
+
const positions = [],
|
|
122
|
+
normals = [],
|
|
123
123
|
m1 = [],
|
|
124
124
|
n1 = [],
|
|
125
125
|
m2 = [],
|
|
@@ -128,30 +128,30 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
|
|
|
128
128
|
rock = [],
|
|
129
129
|
yy = [],
|
|
130
130
|
par = [],
|
|
131
|
-
|
|
131
|
+
indices = [],
|
|
132
132
|
s3 = [0, 0, 0],
|
|
133
133
|
c3 = [0, 0, 0],
|
|
134
134
|
bounds = [];
|
|
135
135
|
for (const t of list) {
|
|
136
|
-
const f = tileGrid(t,
|
|
136
|
+
const f = tileGrid(t, levelIndex, true);
|
|
137
137
|
yield;
|
|
138
|
-
const c1 =
|
|
139
|
-
c2 =
|
|
138
|
+
const c1 = levelIndex < 2 ? tileGrid(t, levelIndex + 1, false) : null,
|
|
139
|
+
c2 = levelIndex < 1 ? tileGrid(t, 2, false) : null;
|
|
140
140
|
if (c1) yield;
|
|
141
141
|
const map = new Int32Array(f.w * f.w).fill(-1),
|
|
142
|
-
base =
|
|
142
|
+
base = positions.length / 3;
|
|
143
143
|
let vi = base,
|
|
144
144
|
y0 = 1e9,
|
|
145
145
|
y1 = -1e9;
|
|
146
|
-
for (let
|
|
147
|
-
for (let
|
|
148
|
-
const k =
|
|
146
|
+
for (let row = 1; row < f.w - 1; row++)
|
|
147
|
+
for (let column = 1; column < f.w - 1; column++) {
|
|
148
|
+
const k = row * f.w + column;
|
|
149
149
|
if (!f.corner[k]) continue;
|
|
150
150
|
map[k] = vi++;
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const lx = (
|
|
154
|
-
lz = (
|
|
151
|
+
positions.push(f.P[k * 3], f.P[k * 3 + 1], f.P[k * 3 + 2]);
|
|
152
|
+
normals.push(f.N[k * 3], f.N[k * 3 + 1], f.N[k * 3 + 2]);
|
|
153
|
+
const lx = (column - 1) * f.g,
|
|
154
|
+
lz = (row - 1) * f.g,
|
|
155
155
|
x = t.x0 + lx,
|
|
156
156
|
z = t.z0 + lz;
|
|
157
157
|
if (c1) {
|
|
@@ -174,31 +174,31 @@ export function createStrataTiles({ size, levels, cliffMask, cliffPoint, groundC
|
|
|
174
174
|
y1 = Math.max(y1, f.P[k * 3 + 1]);
|
|
175
175
|
}
|
|
176
176
|
bounds.push(t.id, y0, y1);
|
|
177
|
-
for (let
|
|
178
|
-
for (let
|
|
179
|
-
if (!inRegion(t.x0 + (
|
|
180
|
-
const i00 = map[
|
|
181
|
-
i10 = map[
|
|
182
|
-
i01 = map[(
|
|
183
|
-
i11 = map[(
|
|
184
|
-
|
|
177
|
+
for (let row = 1; row < f.w - 2; row++)
|
|
178
|
+
for (let column = 1; column < f.w - 2; column++) {
|
|
179
|
+
if (!inRegion(t.x0 + (column - 0.5) * f.g, t.z0 + (row - 0.5) * f.g)) continue;
|
|
180
|
+
const i00 = map[row * f.w + column],
|
|
181
|
+
i10 = map[row * f.w + column + 1],
|
|
182
|
+
i01 = map[(row + 1) * f.w + column],
|
|
183
|
+
i11 = map[(row + 1) * f.w + column + 1];
|
|
184
|
+
indices.push(i00, i01, i10, i10, i01, i11);
|
|
185
185
|
}
|
|
186
186
|
yield;
|
|
187
187
|
}
|
|
188
|
-
const
|
|
189
|
-
attributes = { position:
|
|
190
|
-
if (
|
|
191
|
-
if (
|
|
192
|
-
return { attributes, index:
|
|
188
|
+
const packAttribute = (values, n) => [new Float32Array(values), n],
|
|
189
|
+
attributes = { position: packAttribute(positions, 3), normal: packAttribute(normals, 3), color: packAttribute(col, 3), aRock: packAttribute(rock, 4), aYY: packAttribute(yy, 1), aParam: packAttribute(par, 3) };
|
|
190
|
+
if (levelIndex < 2) Object.assign(attributes, { aM1: packAttribute(m1, 3), aN1: packAttribute(n1, 3) });
|
|
191
|
+
if (levelIndex < 1) Object.assign(attributes, { aM2: packAttribute(m2, 3), aN2: packAttribute(n2, 3) });
|
|
192
|
+
return { attributes, index: positions.length / 3 > 65535 ? new Uint32Array(indices) : new Uint16Array(indices), bounds };
|
|
193
193
|
}
|
|
194
|
-
const boxKey = (t,
|
|
195
|
-
function tilesIn(
|
|
196
|
-
return tiles.filter(t => boxKey(t,
|
|
194
|
+
const boxKey = (t, levelIndex) => Math.floor((t.x0 + half) / boxSizes[levelIndex]) + ':' + Math.floor((t.z0 + half) / boxSizes[levelIndex]);
|
|
195
|
+
function tilesIn(levelIndex, key) {
|
|
196
|
+
return tiles.filter(t => boxKey(t, levelIndex) === key);
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
// The arrays of the box `key` at level `
|
|
200
|
-
function boxNow(
|
|
201
|
-
const it = boxArrays(
|
|
199
|
+
// The arrays of the box `key` at level `levelIndex`, all at once.
|
|
200
|
+
function boxNow(levelIndex, key) {
|
|
201
|
+
const it = boxArrays(levelIndex, tilesIn(levelIndex, key));
|
|
202
202
|
for (;;) {
|
|
203
203
|
const step = it.next();
|
|
204
204
|
if (step.done) return step.value;
|
package/geometry/stream.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build river ribbons from a centreline, a water level and a sampled bed.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import * as THREE from 'three';
|
|
2
7
|
import { clamp, lerp, sstep } from '../math/scalar.js';
|
|
3
8
|
|
|
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
9
|
const DEEP = 8, // the depth the shader takes for water that knows no bed
|
|
12
10
|
FAR = 99, // how far from a spring's eye water is that has none
|
|
13
11
|
MURK = 7, // a stream's water hides its bed this many times sooner than the lake's
|
|
@@ -27,65 +25,89 @@ const MIDDLE = ACROSS.indexOf(0),
|
|
|
27
25
|
function shutBeyondBanks(bank, first) {
|
|
28
26
|
for (const step of [-1, 1]) {
|
|
29
27
|
let dry = false;
|
|
30
|
-
for (let
|
|
31
|
-
const depth = (first +
|
|
28
|
+
for (let crossIndex = MIDDLE + step; crossIndex >= 0 && crossIndex < ACROSS.length; crossIndex += step) {
|
|
29
|
+
const depth = (first + crossIndex) * 4;
|
|
32
30
|
if (dry) bank[depth] = Math.min(bank[depth], DRY);
|
|
33
31
|
else dry = bank[depth] < 0;
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
34
|
}
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Build an indexed ribbon from spring to mouth. The ribbon starts at a point
|
|
37
|
+
* and widens downstream. When terrain is supplied, the water shader clips the
|
|
38
|
+
* ribbon at the first raised bank on each side of a cross-section.
|
|
39
|
+
* @param {[number, number][]} points - At least two downstream-ordered [x, z] points in metres.
|
|
40
|
+
* @param {number} width - Full stream width in metres once the channel has widened.
|
|
41
|
+
* @param {(t: number) => number} surface - Water elevation in metres at `t = i / (pts.length - 1)`.
|
|
42
|
+
* Samples are spaced by point index. Unevenly spaced points get equal increments in t.
|
|
43
|
+
* @param {ArrayLike<number>} bed - One bed elevation per point. The local drop drives the flow shader.
|
|
44
|
+
* @param {{ground: (x: number, z: number) => number, reach: number, grows?: number} | null} [banks] -
|
|
45
|
+
* Terrain elevation sampler, extra reach on each bank in metres and optional downstream widening distance.
|
|
46
|
+
* @param {((x: number, z: number) => number) | null} [fadeAt] - Visible share (0–1) along the centreline,
|
|
47
|
+
* useful where the stream meets a lake.
|
|
48
|
+
* @returns An owned BufferGeometry with position, flow, current and bank attributes for the stream material.
|
|
49
|
+
* @example
|
|
50
|
+
* ```js
|
|
51
|
+
* import { streamGeometry } from 'roamwright/geometry/stream.js';
|
|
52
|
+
*
|
|
53
|
+
* const channel = [[0, 0], [1, 8], [3, 16]];
|
|
54
|
+
* const water = streamGeometry(channel, 4, t => 3 - t, [2.4, 1.8, 1.2]);
|
|
55
|
+
* // Pass water to a mesh using the stream material, then dispose it on removal.
|
|
56
|
+
* water.dispose();
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function streamGeometry(points, width, surface, bed, banks = null, fadeAt = null) {
|
|
60
|
+
const pointCount = points.length,
|
|
39
61
|
across = ACROSS.length,
|
|
40
|
-
|
|
41
|
-
flow = new Float32Array(
|
|
42
|
-
current = new Float32Array(
|
|
43
|
-
bank = new Float32Array(
|
|
44
|
-
|
|
62
|
+
positions = new Float32Array(pointCount * across * 3),
|
|
63
|
+
flow = new Float32Array(pointCount * across * 3),
|
|
64
|
+
current = new Float32Array(pointCount * across * 2),
|
|
65
|
+
bank = new Float32Array(pointCount * across * 4),
|
|
66
|
+
indices = [],
|
|
45
67
|
reach = width * 0.5 + (banks ? banks.reach : 0),
|
|
46
68
|
grows = banks?.grows ?? SPRING.grows * width;
|
|
47
69
|
let run = 0;
|
|
48
|
-
for (let
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
let dx =
|
|
52
|
-
dz =
|
|
53
|
-
const
|
|
54
|
-
dx /=
|
|
55
|
-
dz /=
|
|
56
|
-
if (
|
|
57
|
-
const y = surface(
|
|
58
|
-
grade = sstep(0.015, 0.32, (bed[Math.max(
|
|
70
|
+
for (let pointIndex = 0; pointIndex < pointCount; pointIndex++) {
|
|
71
|
+
const previousPoint = points[Math.max(0, pointIndex - 1)],
|
|
72
|
+
nextPoint = points[Math.min(pointCount - 1, pointIndex + 1)];
|
|
73
|
+
let dx = nextPoint[0] - previousPoint[0],
|
|
74
|
+
dz = nextPoint[1] - previousPoint[1];
|
|
75
|
+
const tangentLength = Math.hypot(dx, dz) || 1;
|
|
76
|
+
dx /= tangentLength;
|
|
77
|
+
dz /= tangentLength;
|
|
78
|
+
if (pointIndex > 0) run += Math.hypot(points[pointIndex][0] - points[pointIndex - 1][0], points[pointIndex][1] - points[pointIndex - 1][1]);
|
|
79
|
+
const y = surface(pointIndex / (pointCount - 1)),
|
|
80
|
+
grade = sstep(0.015, 0.32, (bed[Math.max(pointIndex - 3, 0)] - bed[Math.min(pointIndex + 3, pointCount - 1)]) / 9),
|
|
59
81
|
share = headShare(run, width, grows),
|
|
60
|
-
rising = sstep(0, SPRING.fade * width, run) * (fadeAt ? fadeAt(
|
|
61
|
-
for (let
|
|
62
|
-
const side = ACROSS[
|
|
82
|
+
rising = sstep(0, SPRING.fade * width, run) * (fadeAt ? fadeAt(points[pointIndex][0], points[pointIndex][1]) : 1);
|
|
83
|
+
for (let crossIndex = 0; crossIndex < across; crossIndex++) {
|
|
84
|
+
const side = ACROSS[crossIndex],
|
|
63
85
|
// a strip with no banks to shape it wanders a little in width
|
|
64
86
|
wander = banks
|
|
65
87
|
? 1
|
|
66
88
|
: 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
|
-
|
|
68
|
-
x =
|
|
69
|
-
z =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
flow.set([
|
|
73
|
-
current.set([dx, dz],
|
|
89
|
+
lateralOffset = side * reach * share * wander,
|
|
90
|
+
x = points[pointIndex][0] - dz * lateralOffset,
|
|
91
|
+
z = points[pointIndex][1] + dx * lateralOffset,
|
|
92
|
+
vertexIndex = pointIndex * across + crossIndex;
|
|
93
|
+
positions.set([x, y, z], vertexIndex * 3);
|
|
94
|
+
flow.set([lateralOffset / (width * 0.5), run, grade], vertexIndex * 3);
|
|
95
|
+
current.set([dx, dz], vertexIndex * 2);
|
|
74
96
|
bank.set(
|
|
75
|
-
[banks ? (y - banks.ground(x, z)) * MURK : DEEP, rising * sstep(1, 0.75, Math.abs(side)), run - springEye(width),
|
|
76
|
-
|
|
97
|
+
[banks ? (y - banks.ground(x, z)) * MURK : DEEP, rising * sstep(1, 0.75, Math.abs(side)), run - springEye(width), lateralOffset],
|
|
98
|
+
vertexIndex * 4,
|
|
77
99
|
);
|
|
78
|
-
if (
|
|
100
|
+
if (pointIndex < pointCount - 1 && crossIndex < across - 1) indices.push(vertexIndex, vertexIndex + 1, vertexIndex + across, vertexIndex + 1, vertexIndex + across + 1, vertexIndex + across);
|
|
79
101
|
}
|
|
80
|
-
if (banks) shutBeyondBanks(bank,
|
|
102
|
+
if (banks) shutBeyondBanks(bank, pointIndex * across);
|
|
81
103
|
}
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
return
|
|
104
|
+
const geometry = new THREE.BufferGeometry();
|
|
105
|
+
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
|
|
106
|
+
geometry.setAttribute('aFlow', new THREE.BufferAttribute(flow, 3));
|
|
107
|
+
geometry.setAttribute('aCurrent', new THREE.BufferAttribute(current, 2));
|
|
108
|
+
geometry.setAttribute('aBank', new THREE.BufferAttribute(bank, 4));
|
|
109
|
+
geometry.setIndex(indices);
|
|
110
|
+
return geometry;
|
|
89
111
|
}
|
|
90
112
|
|
|
91
113
|
// How far below its spring a stream of this width has the eye of its pool, in metres.
|