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
|
@@ -13,32 +13,32 @@ export function creasedSheet(
|
|
|
13
13
|
{ wrap = false, inside = false, crease = -1, tint = null, slopeFrom = 0, slopeTo = 1 } = {},
|
|
14
14
|
) {
|
|
15
15
|
if (rows.length < 2 || rows[0].length < 2) return;
|
|
16
|
-
const
|
|
16
|
+
const rowSegments = rows.length - 1,
|
|
17
17
|
cols = rows[0].length,
|
|
18
|
-
|
|
19
|
-
faces = new Float64Array(
|
|
18
|
+
columnSegments = wrap ? cols : cols - 1,
|
|
19
|
+
faces = new Float64Array(rowSegments * columnSegments * 6), // each cell's two triangles' normals, as long as twice their areas
|
|
20
20
|
side = inside ? -1 : 1,
|
|
21
21
|
next = i => (wrap && i === cols - 1 ? 0 : i + 1);
|
|
22
|
-
const cross = (
|
|
23
|
-
const ux =
|
|
24
|
-
uy =
|
|
25
|
-
uz =
|
|
26
|
-
vx =
|
|
27
|
-
vy =
|
|
28
|
-
vz =
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
const cross = (output, pointA, pointB, pointC, at) => {
|
|
23
|
+
const ux = pointB[0] - pointA[0],
|
|
24
|
+
uy = pointB[1] - pointA[1],
|
|
25
|
+
uz = pointB[2] - pointA[2],
|
|
26
|
+
vx = pointC[0] - pointA[0],
|
|
27
|
+
vy = pointC[1] - pointA[1],
|
|
28
|
+
vz = pointC[2] - pointA[2];
|
|
29
|
+
output[at] = (uy * vz - uz * vy) * side;
|
|
30
|
+
output[at + 1] = (uz * vx - ux * vz) * side;
|
|
31
|
+
output[at + 2] = (ux * vy - uy * vx) * side;
|
|
32
32
|
};
|
|
33
|
-
for (let j = 0; j <
|
|
34
|
-
for (let i = 0; i <
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
at = (j *
|
|
40
|
-
cross(faces,
|
|
41
|
-
cross(faces,
|
|
33
|
+
for (let j = 0; j < rowSegments; j++)
|
|
34
|
+
for (let i = 0; i < columnSegments; i++) {
|
|
35
|
+
const topLeft = rows[j][i],
|
|
36
|
+
topRight = rows[j][next(i)],
|
|
37
|
+
bottomRight = rows[j + 1][next(i)],
|
|
38
|
+
bottomLeft = rows[j + 1][i],
|
|
39
|
+
at = (j * columnSegments + i) * 6;
|
|
40
|
+
cross(faces, topLeft, topRight, bottomRight, at);
|
|
41
|
+
cross(faces, topLeft, bottomRight, bottomLeft, at + 3);
|
|
42
42
|
}
|
|
43
43
|
const length = at => Math.hypot(faces[at], faces[at + 1], faces[at + 2]);
|
|
44
44
|
// The normal at point (j, i) for the triangle whose normal starts at `own` in faces: the triangles round the point that lie
|
|
@@ -48,22 +48,22 @@ export function creasedSheet(
|
|
|
48
48
|
const ol = length(own) || 1;
|
|
49
49
|
normal[0] = normal[1] = normal[2] = 0;
|
|
50
50
|
for (let qj = j - 1; qj <= j; qj++) {
|
|
51
|
-
if (qj < 0 || qj >=
|
|
51
|
+
if (qj < 0 || qj >= rowSegments) continue;
|
|
52
52
|
for (let s = -1; s <= 0; s++) {
|
|
53
53
|
let qi = i + s;
|
|
54
54
|
if (qi < 0) {
|
|
55
55
|
if (!wrap) continue;
|
|
56
56
|
qi += cols;
|
|
57
57
|
}
|
|
58
|
-
if (qi >=
|
|
59
|
-
//
|
|
58
|
+
if (qi >= columnSegments) continue;
|
|
59
|
+
// The top-right corner belongs only to the first triangle, the bottom-left only to the second.
|
|
60
60
|
const lower = qj === j,
|
|
61
61
|
left = qi === i,
|
|
62
62
|
isB = lower && !left,
|
|
63
63
|
isD = !lower && left;
|
|
64
64
|
for (let t = 0; t < 2; t++) {
|
|
65
65
|
if ((t === 0 && isD) || (t === 1 && isB)) continue;
|
|
66
|
-
const at = (qj *
|
|
66
|
+
const at = (qj * columnSegments + qi) * 6 + t * 3,
|
|
67
67
|
l = length(at);
|
|
68
68
|
if (l < 1e-12) continue;
|
|
69
69
|
if ((faces[at] * faces[own] + faces[at + 1] * faces[own + 1] + faces[at + 2] * faces[own + 2]) / (l * ol) < crease) continue;
|
|
@@ -82,14 +82,14 @@ export function creasedSheet(
|
|
|
82
82
|
return normal;
|
|
83
83
|
};
|
|
84
84
|
const corner = (j, i, own) => {
|
|
85
|
-
const
|
|
85
|
+
const point = rows[j][i],
|
|
86
86
|
n = pointNormal(j, i, own),
|
|
87
87
|
tone = tones[j][i],
|
|
88
88
|
lay = tint ? sstep(slopeFrom, slopeTo, n[1]) : 0;
|
|
89
89
|
builder.raw(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
point[0],
|
|
91
|
+
point[1],
|
|
92
|
+
point[2],
|
|
93
93
|
n[0],
|
|
94
94
|
n[1],
|
|
95
95
|
n[2],
|
|
@@ -98,11 +98,11 @@ export function creasedSheet(
|
|
|
98
98
|
tone[2] + (tint ? (tint[2] - tone[2]) * lay : 0),
|
|
99
99
|
);
|
|
100
100
|
};
|
|
101
|
-
for (let j = 0; j <
|
|
102
|
-
for (let i = 0; i <
|
|
103
|
-
const at = (j *
|
|
101
|
+
for (let j = 0; j < rowSegments; j++)
|
|
102
|
+
for (let i = 0; i < columnSegments; i++) {
|
|
103
|
+
const at = (j * columnSegments + i) * 6,
|
|
104
104
|
i1 = next(i);
|
|
105
|
-
//
|
|
105
|
+
// Both triangles share the top-left to bottom-right diagonal. Reverse their winding for an inside surface.
|
|
106
106
|
if (length(at) > 1e-12) {
|
|
107
107
|
corner(j, i, at);
|
|
108
108
|
if (inside) {
|
package/geometry/ear.js
CHANGED
|
@@ -6,66 +6,66 @@ import { clamp, lerp, sstep, TAU } from '../math/scalar.js';
|
|
|
6
6
|
const MM = 0.001,
|
|
7
7
|
ATTACH_REACH = 0.91, // the ear's foot sinks under the sampled skull to cover gaps in the rendered grid
|
|
8
8
|
ATTACH_SPAN = [0.63, 0.38]; // the foot is taller than it is deep, relative to the rim
|
|
9
|
-
const
|
|
9
|
+
const alongRim = new Vector3(), acrossRings = new Vector3(), surfaceNormal = new Vector3();
|
|
10
10
|
|
|
11
11
|
// Model a pinna around the sampled ear root. Height is the head's origin in the builder's frame, in metres.
|
|
12
|
-
function buildEar(body, head,
|
|
12
|
+
function buildEar(body, head, side, height, skin, bone) {
|
|
13
13
|
const RINGS = [0, 0.2, 0.4, 0.58, 0.76, 0.9, 1],
|
|
14
14
|
SPOKES = 14,
|
|
15
15
|
warm = clamp(skin[0] * 2.2, 0, 1),
|
|
16
16
|
root = new Vector3();
|
|
17
|
-
head.point(-8,
|
|
17
|
+
head.point(-8, side * 1.72, root, null);
|
|
18
18
|
const at = (i, j) => {
|
|
19
19
|
const r = RINGS[i],
|
|
20
|
-
|
|
21
|
-
up = Math.sin(
|
|
22
|
-
back = (15.5 * Math.cos(
|
|
20
|
+
angle = (j / SPOKES) * TAU,
|
|
21
|
+
up = Math.sin(angle),
|
|
22
|
+
back = (15.5 * Math.cos(angle) * (1 + 0.14 * up) + 1) * r - 2 * (1 - r),
|
|
23
23
|
high = 31 * up * r + 2 * (1 - r),
|
|
24
24
|
rim = sstep(-0.8, -0.45, up) + sstep(-0.45, -0.8, up) * 0.3,
|
|
25
25
|
relief =
|
|
26
26
|
3 * rim * sstep(0.72, 0.9, r) * (1 - 0.4 * sstep(0.92, 1, r)) +
|
|
27
|
-
2 * sstep(-0.9, -0.3, Math.cos(
|
|
27
|
+
2 * sstep(-0.9, -0.3, Math.cos(angle)) * sstep(-0.75, -0.2, up) * Math.exp(-((r - 0.5) * (r - 0.5)) / 0.012) -
|
|
28
28
|
7 * sstep(0.42, 0.1, r) +
|
|
29
|
-
3 * Math.exp(-((
|
|
29
|
+
3 * Math.exp(-((angle - Math.PI) * (angle - Math.PI)) / 0.16 - ((r - 0.8) * (r - 0.8)) / 0.04),
|
|
30
30
|
out = 1 + (back + 16) * 0.42 + relief;
|
|
31
31
|
return [
|
|
32
|
-
root.x +
|
|
32
|
+
root.x + side * out * MM,
|
|
33
33
|
root.y + height + high * MM,
|
|
34
34
|
root.z - (back + high * 0.18) * MM,
|
|
35
|
-
|
|
35
|
+
side,
|
|
36
36
|
0,
|
|
37
37
|
0,
|
|
38
38
|
dim(skin, (1 - 0.4 * sstep(0.45, 0.1, r)) * (1 + 0.08 * warm)),
|
|
39
39
|
];
|
|
40
40
|
};
|
|
41
|
-
const
|
|
42
|
-
for (let i = 0; i < RINGS.length; i++) for (let j = 0; j <= SPOKES; j++)
|
|
43
|
-
const
|
|
41
|
+
const ringPoints = [];
|
|
42
|
+
for (let i = 0; i < RINGS.length; i++) for (let j = 0; j <= SPOKES; j++) ringPoints.push(at(i, j % SPOKES));
|
|
43
|
+
const pointAt = (i, j) => ringPoints[i * (SPOKES + 1) + j];
|
|
44
44
|
// normals from the neighbours round and across the shell, kept pointing away from the head
|
|
45
45
|
for (let i = 0; i < RINGS.length; i++)
|
|
46
46
|
for (let j = 0; j <= SPOKES; j++) {
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (!(
|
|
55
|
-
if (
|
|
56
|
-
const v =
|
|
57
|
-
v[3] =
|
|
58
|
-
v[4] =
|
|
59
|
-
v[5] =
|
|
47
|
+
const nextSpoke = pointAt(i, (j + 1) % SPOKES),
|
|
48
|
+
previousSpoke = pointAt(i, (j + SPOKES - 1) % SPOKES),
|
|
49
|
+
outerRing = pointAt(Math.min(RINGS.length - 1, i + 1), j),
|
|
50
|
+
innerRing = pointAt(Math.max(0, i - 1), j);
|
|
51
|
+
alongRim.set(nextSpoke[0] - previousSpoke[0], nextSpoke[1] - previousSpoke[1], nextSpoke[2] - previousSpoke[2]);
|
|
52
|
+
acrossRings.set(outerRing[0] - innerRing[0], outerRing[1] - innerRing[1], outerRing[2] - innerRing[2]);
|
|
53
|
+
surfaceNormal.crossVectors(alongRim, acrossRings).normalize();
|
|
54
|
+
if (!(surfaceNormal.lengthSq() > 0.5)) surfaceNormal.set(side, 0, 0);
|
|
55
|
+
if (surfaceNormal.x * side < 0) surfaceNormal.negate();
|
|
56
|
+
const v = pointAt(i, j);
|
|
57
|
+
v[3] = surfaceNormal.x;
|
|
58
|
+
v[4] = surfaceNormal.y;
|
|
59
|
+
v[5] = surfaceNormal.z;
|
|
60
60
|
}
|
|
61
61
|
for (let i = 0; i < RINGS.length - 1; i++)
|
|
62
62
|
for (let j = 0; j < SPOKES; j++) {
|
|
63
|
-
if (i) triangle(body,
|
|
64
|
-
triangle(body,
|
|
63
|
+
if (i) triangle(body, pointAt(i, j), pointAt(i, j + 1), pointAt(i + 1, j + 1), null, bone);
|
|
64
|
+
triangle(body, pointAt(i, j), pointAt(i + 1, j + 1), pointAt(i + 1, j), null, bone);
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
// Turn the rim into the side of the skull. A single unbacked shell opens against the sky from behind.
|
|
68
|
-
const rim =
|
|
68
|
+
const rim = ringPoints.slice(-(SPOKES + 1), -1).map(point => point.slice());
|
|
69
69
|
const foot = rim.map(point => {
|
|
70
70
|
const y = lerp(root.y, point[1] - height, ATTACH_SPAN[0]),
|
|
71
71
|
z = lerp(root.z, point[2], ATTACH_SPAN[1]);
|
|
@@ -75,17 +75,17 @@ function buildEar(body, head, s, height, skin, bone) {
|
|
|
75
75
|
if (head.reach(x, y, z) < ATTACH_REACH) inside = x;
|
|
76
76
|
else outside = x;
|
|
77
77
|
}
|
|
78
|
-
return [
|
|
78
|
+
return [side * inside, height + y, z, 0, 0, 0, point[6]];
|
|
79
79
|
});
|
|
80
80
|
for (let j = 0; j < SPOKES; j++) {
|
|
81
81
|
const before = rim[(j + SPOKES - 1) % SPOKES], after = rim[(j + 1) % SPOKES],
|
|
82
82
|
edge = rim[j], base = foot[j];
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
edge[3] = base[3] =
|
|
87
|
-
edge[4] = base[4] =
|
|
88
|
-
edge[5] = base[5] =
|
|
83
|
+
alongRim.set(after[0] - before[0], after[1] - before[1], after[2] - before[2]);
|
|
84
|
+
acrossRings.set(base[0] - edge[0], base[1] - edge[1], base[2] - edge[2]);
|
|
85
|
+
surfaceNormal.crossVectors(acrossRings, alongRim).normalize().multiplyScalar(side);
|
|
86
|
+
edge[3] = base[3] = surfaceNormal.x;
|
|
87
|
+
edge[4] = base[4] = surfaceNormal.y;
|
|
88
|
+
edge[5] = base[5] = surfaceNormal.z;
|
|
89
89
|
}
|
|
90
90
|
for (let j = 0; j < SPOKES; j++) {
|
|
91
91
|
const next = (j + 1) % SPOKES;
|
package/geometry/erosion.js
CHANGED
|
@@ -1,25 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shape a square terrain heightfield with seeded hydraulic erosion while protecting selected vertices.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { lerp, sstep } from '../math/scalar.js';
|
|
2
7
|
|
|
3
8
|
// Weights in a circular erosion brush sum to one, including a fractional radius.
|
|
4
9
|
function erosionBrush(radius) {
|
|
5
10
|
const cells = [],
|
|
6
|
-
|
|
11
|
+
brushExtent = Math.ceil(radius);
|
|
7
12
|
let sum = 0;
|
|
8
|
-
for (let dj = -
|
|
9
|
-
for (let di = -
|
|
13
|
+
for (let dj = -brushExtent; dj <= brushExtent; dj++)
|
|
14
|
+
for (let di = -brushExtent; di <= brushExtent; di++) {
|
|
10
15
|
const w = Math.max(0, radius - Math.hypot(di, dj));
|
|
11
16
|
if (w > 0) {
|
|
12
17
|
cells.push(di, dj, w);
|
|
13
18
|
sum += w;
|
|
14
19
|
}
|
|
15
20
|
}
|
|
16
|
-
for (let
|
|
21
|
+
for (let weightOffset = 2; weightOffset < cells.length; weightOffset += 3) cells[weightOffset] /= sum;
|
|
17
22
|
return cells;
|
|
18
23
|
}
|
|
19
24
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Move sediment downhill by simulating droplets. The height buffer is modified
|
|
27
|
+
* in place. Reuse the original heights and reset the random generator to reproduce a bake.
|
|
28
|
+
* @param {Float32Array | Float64Array | number[]} heights - Row-major elevations, indexed by row × width + column.
|
|
29
|
+
* @param {number} width - Vertices per side of the square grid, at least four.
|
|
30
|
+
* @param {ArrayLike<number>} mask - One influence per vertex. Zero preserves the starting elevation, one allows full erosion.
|
|
31
|
+
* @param {() => number} random - Random stream in [0, 1), such as `mulberry32(seed)`.
|
|
32
|
+
* @param {object} options - Droplet simulation settings.
|
|
33
|
+
* @param {number} options.drops - Number of droplets to attempt.
|
|
34
|
+
* @param {number} options.lifetime - Maximum steps per droplet. Each step travels one grid cell.
|
|
35
|
+
* @param {number} options.inertia - Fraction of the previous direction retained in the next step.
|
|
36
|
+
* @param {number} options.capacityFactor - Scales the sediment load supported by downhill speed and water volume.
|
|
37
|
+
* @param {number} options.minCapacity - Lower bound on the supported sediment load.
|
|
38
|
+
* @param {number} options.erosionRate - Fraction of spare capacity to erode per step, capped by the local fall.
|
|
39
|
+
* @param {number} options.depositRate - Fraction of excess sediment deposited on a downhill step.
|
|
40
|
+
* @param {number} options.evaporation - Fraction of remaining water lost per step.
|
|
41
|
+
* @param {number} options.gravity - Converts the change in elevation into a change in squared speed.
|
|
42
|
+
* @param {number} options.radius - Positive erosion brush radius in grid cells.
|
|
43
|
+
* @param {number} options.maxChange - Maximum absolute deviation from each vertex’s initial elevation, in height units.
|
|
44
|
+
*/
|
|
45
|
+
export function erodeHeightfield(heights, width, mask, random, options) {
|
|
23
46
|
const { drops, lifetime, inertia, capacityFactor, minCapacity, erosionRate,
|
|
24
47
|
depositRate, evaporation, gravity, radius, maxChange } = options;
|
|
25
48
|
if (!Number.isInteger(width) || width < 4 || heights.length !== width * width || mask.length !== heights.length) {
|
|
@@ -27,29 +50,29 @@ export function erodeHeightfield(heights, width, mask, rng, options) {
|
|
|
27
50
|
}
|
|
28
51
|
if (!(radius > 0)) throw new RangeError('radius must be positive');
|
|
29
52
|
const last = width - 1, start = heights.slice(), brush = erosionBrush(radius);
|
|
30
|
-
const
|
|
53
|
+
const heightAndGradient = [0, 0, 0]; // height and its slope across and along at a point, worked out by heightGrad
|
|
31
54
|
// the height at (px, py) in cells, and its gradient, from the four cells round it
|
|
32
55
|
const heightGrad = (px, py) => {
|
|
33
56
|
const i = px | 0,
|
|
34
57
|
j = py | 0,
|
|
35
58
|
u = px - i,
|
|
36
59
|
v = py - j,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
60
|
+
cellIndex = j * width + i,
|
|
61
|
+
nearLeft = heights[cellIndex],
|
|
62
|
+
nearRight = heights[cellIndex + 1],
|
|
63
|
+
farLeft = heights[cellIndex + width],
|
|
64
|
+
farRight = heights[cellIndex + width + 1];
|
|
65
|
+
heightAndGradient[0] = nearLeft * (1 - u) * (1 - v) + nearRight * u * (1 - v) + farLeft * (1 - u) * v + farRight * u * v;
|
|
66
|
+
heightAndGradient[1] = (nearRight - nearLeft) * (1 - v) + (farRight - farLeft) * v;
|
|
67
|
+
heightAndGradient[2] = (farLeft - nearLeft) * (1 - u) + (farRight - nearRight) * u;
|
|
45
68
|
};
|
|
46
|
-
const change = (
|
|
47
|
-
const
|
|
48
|
-
heights[
|
|
69
|
+
const change = (cellIndex, amount) => {
|
|
70
|
+
const adjustedHeight = heights[cellIndex] + amount * mask[cellIndex];
|
|
71
|
+
heights[cellIndex] = Math.min(Math.max(adjustedHeight, start[cellIndex] - maxChange), start[cellIndex] + maxChange);
|
|
49
72
|
};
|
|
50
|
-
for (let
|
|
51
|
-
let px = 1 +
|
|
52
|
-
py = 1 +
|
|
73
|
+
for (let droplet = 0; droplet < drops; droplet++) {
|
|
74
|
+
let px = 1 + random() * (last - 2),
|
|
75
|
+
py = 1 + random() * (last - 2);
|
|
53
76
|
if (mask[(py | 0) * width + (px | 0)] <= 0) continue;
|
|
54
77
|
let dx = 0,
|
|
55
78
|
dy = 0,
|
|
@@ -61,39 +84,39 @@ export function erodeHeightfield(heights, width, mask, rng, options) {
|
|
|
61
84
|
j = py | 0,
|
|
62
85
|
u = px - i,
|
|
63
86
|
v = py - j,
|
|
64
|
-
|
|
87
|
+
cellIndex = j * width + i;
|
|
65
88
|
heightGrad(px, py);
|
|
66
|
-
const
|
|
67
|
-
dx = dx * inertia -
|
|
68
|
-
dy = dy * inertia -
|
|
69
|
-
const
|
|
70
|
-
if (
|
|
71
|
-
dx /=
|
|
72
|
-
dy /=
|
|
89
|
+
const previousHeight = heightAndGradient[0];
|
|
90
|
+
dx = dx * inertia - heightAndGradient[1] * (1 - inertia);
|
|
91
|
+
dy = dy * inertia - heightAndGradient[2] * (1 - inertia);
|
|
92
|
+
const directionLength = Math.hypot(dx, dy);
|
|
93
|
+
if (directionLength < 1e-6) break; // a hollow: the drop stands and soaks away
|
|
94
|
+
dx /= directionLength;
|
|
95
|
+
dy /= directionLength;
|
|
73
96
|
px += dx;
|
|
74
97
|
py += dy;
|
|
75
98
|
if (px < 1 || py < 1 || px >= last - 1 || py >= last - 1) break;
|
|
76
99
|
heightGrad(px, py);
|
|
77
|
-
const fall =
|
|
100
|
+
const fall = previousHeight - heightAndGradient[0], // positive downhill
|
|
78
101
|
capacity = Math.max(fall * speed * water * capacityFactor, minCapacity);
|
|
79
102
|
if (fall < 0 || carried > capacity) {
|
|
80
103
|
// uphill it fills the hollow it came out of; over its capacity it lays some down: both at the cell it left, by its corners
|
|
81
104
|
const laid = fall < 0 ? Math.min(-fall, carried) : (carried - capacity) * depositRate;
|
|
82
105
|
carried -= laid;
|
|
83
|
-
change(
|
|
84
|
-
change(
|
|
85
|
-
change(
|
|
86
|
-
change(
|
|
106
|
+
change(cellIndex, laid * (1 - u) * (1 - v));
|
|
107
|
+
change(cellIndex + 1, laid * u * (1 - v));
|
|
108
|
+
change(cellIndex + width, laid * (1 - u) * v);
|
|
109
|
+
change(cellIndex + width + 1, laid * u * v);
|
|
87
110
|
} else {
|
|
88
111
|
// under its capacity it wears the ground round the cell it left, never deeper than its own fall
|
|
89
112
|
const worn = Math.min((capacity - carried) * erosionRate, fall);
|
|
90
|
-
for (let
|
|
91
|
-
const bi = i + brush[
|
|
92
|
-
bj = j + brush[
|
|
113
|
+
for (let brushOffset = 0; brushOffset < brush.length; brushOffset += 3) {
|
|
114
|
+
const bi = i + brush[brushOffset],
|
|
115
|
+
bj = j + brush[brushOffset + 1];
|
|
93
116
|
if (bi < 0 || bj < 0 || bi > last || bj > last) continue;
|
|
94
|
-
const
|
|
95
|
-
take = worn * brush[
|
|
96
|
-
change(
|
|
117
|
+
const brushIndex = bj * width + bi,
|
|
118
|
+
take = worn * brush[brushOffset + 2] * mask[brushIndex];
|
|
119
|
+
change(brushIndex, -worn * brush[brushOffset + 2]);
|
|
97
120
|
carried += take;
|
|
98
121
|
}
|
|
99
122
|
}
|
|
@@ -102,5 +125,5 @@ export function erodeHeightfield(heights, width, mask, rng, options) {
|
|
|
102
125
|
}
|
|
103
126
|
}
|
|
104
127
|
// the worn ground is eased into the laid-out ground at the edge of the wild, so nothing steps where the mask fades
|
|
105
|
-
for (let
|
|
128
|
+
for (let cellIndex = 0; cellIndex < heights.length; cellIndex++) heights[cellIndex] = lerp(start[cellIndex], heights[cellIndex], sstep(0, 0.35, mask[cellIndex]));
|
|
106
129
|
}
|
package/geometry/face-grid.js
CHANGED
|
@@ -14,11 +14,11 @@ function angleAcross(across) {
|
|
|
14
14
|
let low = 0,
|
|
15
15
|
high = Math.PI / 2;
|
|
16
16
|
for (let i = 0; i < 30; i++) {
|
|
17
|
-
const
|
|
18
|
-
s = Math.sin(
|
|
19
|
-
c = Math.cos(
|
|
20
|
-
if (EYE_WIDTH * s * Math.pow(Math.pow(s, EYE_SQUARE) + Math.pow(c, EYE_SQUARE), -1 / EYE_SQUARE) < across) low =
|
|
21
|
-
else high =
|
|
17
|
+
const angle = (low + high) / 2,
|
|
18
|
+
s = Math.sin(angle),
|
|
19
|
+
c = Math.cos(angle);
|
|
20
|
+
if (EYE_WIDTH * s * Math.pow(Math.pow(s, EYE_SQUARE) + Math.pow(c, EYE_SQUARE), -1 / EYE_SQUARE) < across) low = angle;
|
|
21
|
+
else high = angle;
|
|
22
22
|
}
|
|
23
23
|
return (low + high) / 2;
|
|
24
24
|
}
|
|
@@ -32,40 +32,40 @@ function faceGrid(rows, across, behind, backRows, closed = true) {
|
|
|
32
32
|
...half
|
|
33
33
|
.slice(1)
|
|
34
34
|
.reverse()
|
|
35
|
-
.map(
|
|
35
|
+
.map(angle => -angle),
|
|
36
36
|
...half,
|
|
37
37
|
],
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
rowCount = rows.length,
|
|
39
|
+
columnCount = angles.length,
|
|
40
40
|
faceEdge = half[across.length - 1] + 1e-6,
|
|
41
|
-
whole = angles.map(
|
|
42
|
-
kept = rows.map((y, i) => (!backRows || i === 0 || i ===
|
|
43
|
-
used = new Uint8Array(
|
|
44
|
-
|
|
45
|
-
for (let j = 0; j <
|
|
46
|
-
for (let j = 0; j <
|
|
47
|
-
const left = (i,
|
|
48
|
-
right = i => i *
|
|
41
|
+
whole = angles.map(angle => Math.abs(angle) <= faceEdge), // the columns that carry every row
|
|
42
|
+
kept = rows.map((y, i) => (!backRows || i === 0 || i === rowCount - 1 || backRows.includes(y) ? i : -1)).filter(i => i >= 0),
|
|
43
|
+
used = new Uint8Array(rowCount * columnCount),
|
|
44
|
+
indices = [];
|
|
45
|
+
for (let j = 0; j < columnCount; j++) for (let i = 0; i < rowCount; i++) used[i * columnCount + j] = whole[j] || kept.includes(i) ? 1 : 0;
|
|
46
|
+
for (let j = 0; j < columnCount - 1; j++) {
|
|
47
|
+
const left = (i, column = j) => i * columnCount + column,
|
|
48
|
+
right = i => i * columnCount + j + 1;
|
|
49
49
|
if (whole[j] && whole[j + 1])
|
|
50
|
-
for (let i = 0; i <
|
|
50
|
+
for (let i = 0; i < rowCount - 1; i++) indices.push(left(i), right(i), right(i + 1), left(i), right(i + 1), left(i + 1));
|
|
51
51
|
else
|
|
52
52
|
for (let k = 0; k < kept.length - 1; k++) {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
mid = (
|
|
56
|
-
if (!whole[j] && !whole[j + 1])
|
|
53
|
+
const currentRow = kept[k],
|
|
54
|
+
nextRow = kept[k + 1],
|
|
55
|
+
mid = (currentRow + nextRow + 1) >> 1;
|
|
56
|
+
if (!whole[j] && !whole[j + 1]) indices.push(left(currentRow), right(currentRow), right(nextRow), left(currentRow), right(nextRow), left(nextRow));
|
|
57
57
|
else if (whole[j]) {
|
|
58
58
|
// every row on the left, the kept ones on the right: a fan from each end of the right-hand edge
|
|
59
|
-
for (let i =
|
|
60
|
-
|
|
59
|
+
for (let i = currentRow; i < nextRow; i++) indices.push(left(i), right(i < mid ? currentRow : nextRow), left(i + 1));
|
|
60
|
+
indices.push(left(mid), right(currentRow), right(nextRow));
|
|
61
61
|
} else {
|
|
62
|
-
for (let i =
|
|
63
|
-
|
|
62
|
+
for (let i = currentRow; i < nextRow; i++) indices.push(left(i < mid ? currentRow : nextRow), right(i), right(i + 1));
|
|
63
|
+
indices.push(left(currentRow), right(mid), left(nextRow));
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
if (closed) for (let j = 0; j <
|
|
68
|
-
return { rows, angles, nr, nc, used, idx };
|
|
67
|
+
if (closed) for (let j = 0; j < columnCount - 1; j++) indices.push((rowCount - 1) * columnCount + j, (rowCount - 1) * columnCount + j + 1, rowCount * columnCount, j + 1, j, rowCount * columnCount + 1);
|
|
68
|
+
return { rows, angles, nr: rowCount, nc: columnCount, used, idx: indices };
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
const _o = new Vector3(),
|
|
@@ -74,66 +74,66 @@ const _o = new Vector3(),
|
|
|
74
74
|
_ch = [0, 1, 0, 0, 0, 0, 0, 0];
|
|
75
75
|
|
|
76
76
|
// A head function sampled on a grid: positions, normals, the channels of each point, and the grid's triangles.
|
|
77
|
-
function faceSculpt(
|
|
78
|
-
const { rows, angles, nr, nc, used, idx } = grid,
|
|
79
|
-
n =
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
function faceSculpt(sampleFace, grid) {
|
|
78
|
+
const { rows, angles, nr: rowCount, nc: columnCount, used, idx: indices } = grid,
|
|
79
|
+
n = rowCount * columnCount + 2,
|
|
80
|
+
positions = new Float32Array(n * 3),
|
|
81
|
+
normals = new Float32Array(n * 3),
|
|
82
|
+
channels = new Float32Array(n * CHANNELS);
|
|
83
83
|
const put = (i, row, angle) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
for (let k = 0; k < CHANNELS; k++)
|
|
84
|
+
sampleFace.point(row, angle, _o, _ch);
|
|
85
|
+
positions[i * 3] = _o.x;
|
|
86
|
+
positions[i * 3 + 1] = _o.y;
|
|
87
|
+
positions[i * 3 + 2] = _o.z;
|
|
88
|
+
for (let k = 0; k < CHANNELS; k++) channels[i * CHANNELS + k] = _ch[k];
|
|
89
89
|
};
|
|
90
|
-
for (let i = 0; i <
|
|
91
|
-
put(
|
|
92
|
-
put(
|
|
90
|
+
for (let i = 0; i < rowCount; i++) for (let j = 0; j < columnCount; j++) if (used[i * columnCount + j]) put(i * columnCount + j, rows[i], angles[j]);
|
|
91
|
+
put(rowCount * columnCount, HALF, 0);
|
|
92
|
+
put(rowCount * columnCount + 1, -HALF, 0);
|
|
93
93
|
// each point's normal is the sum of its triangles', so a row pinched shut at the corner of an eye does no harm
|
|
94
|
-
for (let t = 0; t <
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
_a.set(
|
|
99
|
-
_b.set(
|
|
94
|
+
for (let t = 0; t < indices.length; t += 3) {
|
|
95
|
+
const firstOffset = indices[t] * 3,
|
|
96
|
+
secondOffset = indices[t + 1] * 3,
|
|
97
|
+
thirdOffset = indices[t + 2] * 3;
|
|
98
|
+
_a.set(positions[secondOffset] - positions[firstOffset], positions[secondOffset + 1] - positions[firstOffset + 1], positions[secondOffset + 2] - positions[firstOffset + 2]);
|
|
99
|
+
_b.set(positions[thirdOffset] - positions[firstOffset], positions[thirdOffset + 1] - positions[firstOffset + 1], positions[thirdOffset + 2] - positions[firstOffset + 2]);
|
|
100
100
|
_a.cross(_b);
|
|
101
|
-
for (const q of [
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
101
|
+
for (const q of [firstOffset, secondOffset, thirdOffset]) {
|
|
102
|
+
normals[q] += _a.x;
|
|
103
|
+
normals[q + 1] += _a.y;
|
|
104
|
+
normals[q + 2] += _a.z;
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
-
for (let i = 0; i <
|
|
107
|
+
for (let i = 0; i < rowCount; i++) {
|
|
108
108
|
// the first and last columns are one line down the back of the head
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
for (let k = 0; k < 3; k++)
|
|
109
|
+
const firstColumnOffset = i * columnCount * 3,
|
|
110
|
+
lastColumnOffset = (i * columnCount + columnCount - 1) * 3;
|
|
111
|
+
for (let k = 0; k < 3; k++) normals[firstColumnOffset + k] = normals[lastColumnOffset + k] = normals[firstColumnOffset + k] + normals[lastColumnOffset + k];
|
|
112
112
|
}
|
|
113
113
|
for (let i = 0; i < n; i++) {
|
|
114
114
|
const q = i * 3,
|
|
115
|
-
l = Math.hypot(
|
|
115
|
+
l = Math.hypot(normals[q], normals[q + 1], normals[q + 2]);
|
|
116
116
|
if (l > 0) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} else
|
|
117
|
+
normals[q] /= l;
|
|
118
|
+
normals[q + 1] /= l;
|
|
119
|
+
normals[q + 2] /= l;
|
|
120
|
+
} else normals[q + 1] = 1;
|
|
121
121
|
}
|
|
122
|
-
return { P, N, CH, idx };
|
|
122
|
+
return { P: positions, N: normals, CH: channels, idx: indices };
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
// The point of the face that lies at x across and y up from the head's middle (metres), as [x, y, z, and the normal there],
|
|
126
126
|
// raised off the skin by lift.
|
|
127
|
-
function faceAt(
|
|
127
|
+
function faceAt(sampleFace, x, y, lift = 0) {
|
|
128
128
|
let row = y * 1000,
|
|
129
129
|
angle = Math.asin(Math.max(-0.99, Math.min(0.99, (x * 1000) / EYE_WIDTH)));
|
|
130
130
|
for (let i = 0; i < 5; i++) {
|
|
131
|
-
|
|
131
|
+
sampleFace.point(row, angle, _o, null);
|
|
132
132
|
row += (y - _o.y) * 1000;
|
|
133
133
|
angle += ((x - _o.x) * 1000) / 70;
|
|
134
134
|
}
|
|
135
|
-
|
|
136
|
-
|
|
135
|
+
sampleFace.point(row + 0.6, angle, _a, null);
|
|
136
|
+
sampleFace.point(row, angle + 0.012, _b, null);
|
|
137
137
|
_a.sub(_o);
|
|
138
138
|
_b.sub(_o);
|
|
139
139
|
_b.cross(_a).normalize();
|
package/geometry/foliage.js
CHANGED
|
@@ -9,17 +9,17 @@ const _tint = [0, 0, 0],
|
|
|
9
9
|
function shapeParts() {
|
|
10
10
|
return { p: [], n: [], c: [], l: [] };
|
|
11
11
|
}
|
|
12
|
-
// Adds a geometry to parts, placed by matrix
|
|
13
|
-
function addShape(parts,
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
_n3.getNormalMatrix(
|
|
18
|
-
for (let i = 0; i <
|
|
19
|
-
tint(
|
|
20
|
-
_v.set(
|
|
12
|
+
// Adds a geometry to parts, placed by the transform matrix, each vertex coloured by tint(x, y, z, out) in its own space; leaf 1 for foliage.
|
|
13
|
+
function addShape(parts, geometry, transform, tint, leaf) {
|
|
14
|
+
const sourceGeometry = geometry.index ? geometry.toNonIndexed() : geometry,
|
|
15
|
+
positions = sourceGeometry.attributes.position.array,
|
|
16
|
+
normals = sourceGeometry.attributes.normal.array;
|
|
17
|
+
_n3.getNormalMatrix(transform);
|
|
18
|
+
for (let i = 0; i < positions.length; i += 3) {
|
|
19
|
+
tint(positions[i], positions[i + 1], positions[i + 2], _tint);
|
|
20
|
+
_v.set(positions[i], positions[i + 1], positions[i + 2]).applyMatrix4(transform);
|
|
21
21
|
parts.p.push(_v.x, _v.y, _v.z);
|
|
22
|
-
_v.set(
|
|
22
|
+
_v.set(normals[i], normals[i + 1], normals[i + 2])
|
|
23
23
|
.applyMatrix3(_n3)
|
|
24
24
|
.normalize();
|
|
25
25
|
parts.n.push(_v.x, _v.y, _v.z);
|