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/shore-grid.js
CHANGED
|
@@ -1,66 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Measure signed distance to a land outline directly or bake distances for repeated shoreline queries.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
|
|
1
6
|
import { clamp, lerp } from '../math/scalar.js';
|
|
2
7
|
|
|
3
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Prepare edges once for repeated {@link outlineDistance} queries. The last point connects to the first.
|
|
10
|
+
* @param {[number, number][]} outline - Ordered [x, z] vertices of a closed polygon in metres.
|
|
11
|
+
*/
|
|
4
12
|
export const outlineEdges = outline =>
|
|
5
13
|
outline.map(([x, z], i) => {
|
|
6
|
-
const [
|
|
7
|
-
dx =
|
|
8
|
-
dz =
|
|
9
|
-
return { x, z, dx, dz, ez, inv: 1 / (dx * dx + dz * dz || 1) };
|
|
14
|
+
const [endX, endZ] = outline[(i + 1) % outline.length],
|
|
15
|
+
dx = endX - x,
|
|
16
|
+
dz = endZ - z;
|
|
17
|
+
return { x, z, dx, dz, ez: endZ, inv: 1 / (dx * dx + dz * dz || 1) };
|
|
10
18
|
});
|
|
11
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Measure distance to the closest point on a polygon edge. The sign is positive
|
|
21
|
+
* inland, zero on the boundary and negative outside, for either winding order.
|
|
22
|
+
* @param {ReturnType<typeof outlineEdges>} edges - Precomputed edges of one land outline.
|
|
23
|
+
* @param {number} x - World X in metres.
|
|
24
|
+
* @param {number} z - World Z in metres.
|
|
25
|
+
*/
|
|
12
26
|
export function outlineDistance(edges, x, z) {
|
|
13
27
|
let inside = false,
|
|
14
28
|
best = Infinity;
|
|
15
|
-
for (const
|
|
16
|
-
const px = x -
|
|
17
|
-
pz = z -
|
|
18
|
-
if (
|
|
19
|
-
const
|
|
20
|
-
best = Math.min(best, (px -
|
|
29
|
+
for (const edge of edges) {
|
|
30
|
+
const px = x - edge.x,
|
|
31
|
+
pz = z - edge.z;
|
|
32
|
+
if (edge.z > z !== edge.ez > z && px < (pz * edge.dx) / edge.dz) inside = !inside;
|
|
33
|
+
const edgeFraction = clamp((px * edge.dx + pz * edge.dz) * edge.inv, 0, 1);
|
|
34
|
+
best = Math.min(best, (px - edge.dx * edgeFraction) ** 2 + (pz - edge.dz * edgeFraction) ** 2);
|
|
21
35
|
}
|
|
22
36
|
return Math.sqrt(best) * (inside ? 1 : -1);
|
|
23
37
|
}
|
|
24
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Cache shoreline distances for frequent terrain or material queries. Allocate a
|
|
41
|
+
* square grid, bake one or more land outlines, then sample it by world position.
|
|
42
|
+
* Baking keeps the largest signed distance across all outlines at each grid point.
|
|
43
|
+
* Queries interpolate four neighbouring samples and clamp to the grid near its edge.
|
|
44
|
+
* @example
|
|
45
|
+
* ```js
|
|
46
|
+
* import { createShoreGrid } from 'roamwright/geometry/shore-grid.js';
|
|
47
|
+
*
|
|
48
|
+
* const shore = createShoreGrid();
|
|
49
|
+
* shore.layOutShore(-8, -8, 1, 17);
|
|
50
|
+
* shore.bakeShore([[[-4, -4], [4, -4], [4, 4], [-4, 4]]]);
|
|
51
|
+
* const inland = shore.shoreDistance(0, 0); // 4 metres
|
|
52
|
+
* const offshore = shore.shoreDistance(6, 0); // -2 metres
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
25
55
|
export function createShoreGrid() {
|
|
26
|
-
let grid = null, // the
|
|
27
|
-
|
|
56
|
+
let grid = null, // the distance samples, with pointsPerSide along each edge
|
|
57
|
+
pointsPerSide = 0,
|
|
28
58
|
x0 = 0,
|
|
29
59
|
z0 = 0,
|
|
30
60
|
step = 0; // metres from one point of the grid to the next
|
|
31
61
|
|
|
32
|
-
|
|
62
|
+
/**
|
|
63
|
+
* Replace any existing grid with zeroed storage.
|
|
64
|
+
* @param {number} cornerX - Smallest sample X in metres.
|
|
65
|
+
* @param {number} cornerZ - Smallest sample Z in metres.
|
|
66
|
+
* @param {number} spacing - Positive distance between samples in metres.
|
|
67
|
+
* @param {number} points - Samples along each side, at least two.
|
|
68
|
+
*/
|
|
33
69
|
function layOutShore(cornerX, cornerZ, spacing, points) {
|
|
34
70
|
x0 = cornerX;
|
|
35
71
|
z0 = cornerZ;
|
|
36
72
|
step = spacing;
|
|
37
|
-
|
|
38
|
-
grid = new Float32Array(
|
|
73
|
+
pointsPerSide = points;
|
|
74
|
+
grid = new Float32Array(pointsPerSide * pointsPerSide);
|
|
39
75
|
}
|
|
40
|
-
|
|
76
|
+
/**
|
|
77
|
+
* Restore a matching cached grid by writing into this buffer after layout.
|
|
78
|
+
* @returns {Float32Array | null} The live row-major distances, or null before layout.
|
|
79
|
+
*/
|
|
41
80
|
const shoreGrid = () => grid;
|
|
42
|
-
|
|
81
|
+
/** @param {[number, number][][]} outlines - Land polygons to rasterize into the allocated grid. */
|
|
43
82
|
function bakeShore(outlines) {
|
|
44
83
|
const edges = outlines.map(outlineEdges);
|
|
45
|
-
for (let
|
|
46
|
-
for (let i = 0; i <
|
|
84
|
+
for (let row = 0; row < pointsPerSide; row++)
|
|
85
|
+
for (let i = 0; i < pointsPerSide; i++) {
|
|
47
86
|
const x = x0 + i * step,
|
|
48
|
-
z = z0 +
|
|
49
|
-
let
|
|
50
|
-
for (const outline of edges)
|
|
51
|
-
grid[
|
|
87
|
+
z = z0 + row * step;
|
|
88
|
+
let landDistance = -Infinity;
|
|
89
|
+
for (const outline of edges) landDistance = Math.max(landDistance, outlineDistance(outline, x, z));
|
|
90
|
+
grid[row * pointsPerSide + i] = landDistance;
|
|
52
91
|
}
|
|
53
92
|
}
|
|
54
|
-
|
|
93
|
+
/**
|
|
94
|
+
* @param {number} x - World X in metres.
|
|
95
|
+
* @param {number} z - World Z in metres.
|
|
96
|
+
* @returns {number} Interpolated signed distance in metres, positive inland.
|
|
97
|
+
*/
|
|
55
98
|
function shoreDistance(x, z) {
|
|
56
|
-
const u = clamp((x - x0) / step, 0,
|
|
57
|
-
v = clamp((z - z0) / step, 0,
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return lerp(lerp(grid[
|
|
99
|
+
const u = clamp((x - x0) / step, 0, pointsPerSide - 1.001),
|
|
100
|
+
v = clamp((z - z0) / step, 0, pointsPerSide - 1.001),
|
|
101
|
+
column = Math.floor(u),
|
|
102
|
+
row = Math.floor(v),
|
|
103
|
+
columnFraction = u - column,
|
|
104
|
+
rowFraction = v - row,
|
|
105
|
+
gridIndex = row * pointsPerSide + column;
|
|
106
|
+
return lerp(lerp(grid[gridIndex], grid[gridIndex + 1], columnFraction), lerp(grid[gridIndex + pointsPerSide], grid[gridIndex + pointsPerSide + 1], columnFraction), rowFraction);
|
|
64
107
|
}
|
|
65
108
|
|
|
66
109
|
return { bakeShore, layOutShore, shoreDistance, shoreGrid };
|
package/geometry/strata-field.js
CHANGED
|
@@ -14,41 +14,41 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
14
14
|
colours = input.colours;
|
|
15
15
|
}
|
|
16
16
|
let mask = null, // 0..1, how much of a cliff stands here
|
|
17
|
-
|
|
17
|
+
smoothedNormals = null, // the terrain normal smoothed over about 12 m, the direction the rock is pushed out
|
|
18
18
|
band = null; // 0..1, the character of the band of cliffs a cell belongs to (relief, colour)
|
|
19
19
|
function makeGrids() {
|
|
20
20
|
mask ??= new Float32Array(stride * stride);
|
|
21
|
-
|
|
21
|
+
smoothedNormals ??= new Float32Array(stride * stride * 3);
|
|
22
22
|
band ??= new Float32Array(stride * stride);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
// The beds, bottom to top: packages of massive hard beds, of thin soft ones, and of middling ones.
|
|
26
|
-
const
|
|
26
|
+
const bedProfile = { y0: bedRange[0], step: 0.1, top: [], thick: [], hard: [], tint: [], index: null };
|
|
27
27
|
(() => {
|
|
28
|
-
const
|
|
29
|
-
let y =
|
|
28
|
+
const random = mulberry32(seed * 7 + 311);
|
|
29
|
+
let y = bedProfile.y0;
|
|
30
30
|
while (y < bedRange[1]) {
|
|
31
|
-
const kind =
|
|
32
|
-
n = kind < 0.36 ? 1 + ((
|
|
31
|
+
const kind = random(),
|
|
32
|
+
n = kind < 0.36 ? 1 + ((random() * 2) | 0) : kind < 0.66 ? 3 + ((random() * 4) | 0) : 2 + ((random() * 3) | 0);
|
|
33
33
|
for (let i = 0; i < n; i++) {
|
|
34
|
-
const t = kind < 0.36 ? lerp(3.5, 8,
|
|
35
|
-
const hard = kind < 0.36 ? lerp(0.78, 1,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
34
|
+
const t = kind < 0.36 ? lerp(3.5, 8, random()) : kind < 0.66 ? lerp(0.9, 2.2, random()) : lerp(2.2, 4.5, random());
|
|
35
|
+
const hard = kind < 0.36 ? lerp(0.78, 1, random()) : kind < 0.66 ? lerp(0.12, 0.5, random()) : lerp(0.5, 0.88, random());
|
|
36
|
+
bedProfile.top.push(y + t);
|
|
37
|
+
bedProfile.thick.push(t);
|
|
38
|
+
bedProfile.hard.push(hard);
|
|
39
|
+
bedProfile.tint.push(random());
|
|
40
40
|
y += t;
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
|
|
44
|
-
for (let i = 0,
|
|
45
|
-
const yy =
|
|
46
|
-
while (
|
|
47
|
-
|
|
43
|
+
bedProfile.index = new Int16Array(Math.ceil((y - bedProfile.y0) / bedProfile.step));
|
|
44
|
+
for (let i = 0, bedIndex = 0; i < bedProfile.index.length; i++) {
|
|
45
|
+
const yy = bedProfile.y0 + (i + 0.5) * bedProfile.step;
|
|
46
|
+
while (bedProfile.top[bedIndex] < yy) bedIndex++;
|
|
47
|
+
bedProfile.index[i] = bedIndex;
|
|
48
48
|
}
|
|
49
49
|
})();
|
|
50
50
|
function bedAt(yy) {
|
|
51
|
-
return
|
|
51
|
+
return bedProfile.index[clamp(((yy - bedProfile.y0) / bedProfile.step) | 0, 0, bedProfile.index.length - 1)];
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
function planCliffShape(input) {
|
|
@@ -86,10 +86,10 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
86
86
|
connected.push(k);
|
|
87
87
|
for (let dj = -1; dj <= 1; dj++)
|
|
88
88
|
for (let di = -1; di <= 1; di++) {
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
kk =
|
|
92
|
-
if (
|
|
89
|
+
const column = i + di,
|
|
90
|
+
row = j + dj,
|
|
91
|
+
kk = row * stride + column;
|
|
92
|
+
if (column < 0 || row < 0 || column > cells || row > cells || id[kk] >= 0 || mask[kk] < 0.5) continue;
|
|
93
93
|
id[kk] = bands;
|
|
94
94
|
stack.push(kk);
|
|
95
95
|
}
|
|
@@ -121,32 +121,32 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
// the smoothed normal
|
|
124
|
-
for (let
|
|
125
|
-
for (let k = 0; k < raw.length; k++) raw[k] = normals[k * 3 +
|
|
124
|
+
for (let channel = 0; channel < 3; channel++) {
|
|
125
|
+
for (let k = 0; k < raw.length; k++) raw[k] = normals[k * 3 + channel];
|
|
126
126
|
blur(raw, tmp, 1);
|
|
127
127
|
blur(raw, tmp, 1);
|
|
128
128
|
blur(raw, tmp, 1);
|
|
129
|
-
for (let k = 0; k < raw.length; k++)
|
|
129
|
+
for (let k = 0; k < raw.length; k++) smoothedNormals[k * 3 + channel] = raw[k];
|
|
130
130
|
}
|
|
131
131
|
for (let k = 0; k < raw.length; k++) {
|
|
132
|
-
const l = Math.hypot(
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
132
|
+
const l = Math.hypot(smoothedNormals[k * 3], smoothedNormals[k * 3 + 1], smoothedNormals[k * 3 + 2]) || 1;
|
|
133
|
+
smoothedNormals[k * 3] /= l;
|
|
134
|
+
smoothedNormals[k * 3 + 1] /= l;
|
|
135
|
+
smoothedNormals[k * 3 + 2] /= l;
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
|
-
function blur(
|
|
139
|
-
// separable box blur of radius
|
|
138
|
+
function blur(values, scratch, radius) {
|
|
139
|
+
// separable box blur of the specified radius in cells, in place
|
|
140
140
|
for (let pass = 0; pass < 2; pass++) {
|
|
141
|
-
const src = pass ?
|
|
142
|
-
dst = pass ?
|
|
141
|
+
const src = pass ? scratch : values,
|
|
142
|
+
dst = pass ? values : scratch,
|
|
143
143
|
sx = pass ? stride : 1,
|
|
144
144
|
sy = pass ? 1 : stride;
|
|
145
145
|
for (let j = 0; j <= cells; j++)
|
|
146
146
|
for (let i = 0; i <= cells; i++) {
|
|
147
147
|
let s = 0,
|
|
148
148
|
n = 0;
|
|
149
|
-
for (let d = -
|
|
149
|
+
for (let d = -radius; d <= radius; d++) {
|
|
150
150
|
const ii = i + d;
|
|
151
151
|
if (ii < 0 || ii > cells) continue;
|
|
152
152
|
s += src[j * sy + ii * sx];
|
|
@@ -170,22 +170,22 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
170
170
|
cell.tz = fz - cell.j;
|
|
171
171
|
return cell.j * stride + cell.i;
|
|
172
172
|
}
|
|
173
|
-
function bilerp(
|
|
173
|
+
function bilerp(grid, cellIndex, components, channel) {
|
|
174
174
|
const { tx, tz } = cell,
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return
|
|
175
|
+
nearLeft = grid[cellIndex * components + channel],
|
|
176
|
+
nearRight = grid[(cellIndex + 1) * components + channel],
|
|
177
|
+
farLeft = grid[(cellIndex + stride) * components + channel],
|
|
178
|
+
farRight = grid[(cellIndex + stride + 1) * components + channel];
|
|
179
|
+
return nearLeft + (nearRight - nearLeft) * tx + (farLeft - nearLeft) * tz + (nearLeft - nearRight - farLeft + farRight) * tx * tz;
|
|
180
180
|
}
|
|
181
181
|
function meshHeight(x, z) {
|
|
182
182
|
const k = locate(x, z),
|
|
183
183
|
{ tx, tz } = cell,
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
return tx + tz <= 1 ?
|
|
184
|
+
nearLeft = heights[k],
|
|
185
|
+
nearRight = heights[k + 1],
|
|
186
|
+
farLeft = heights[k + stride],
|
|
187
|
+
farRight = heights[k + stride + 1];
|
|
188
|
+
return tx + tz <= 1 ? nearLeft + (nearRight - nearLeft) * tx + (farLeft - nearLeft) * tz : farRight + (farLeft - farRight) * (1 - tx) + (nearRight - farRight) * (1 - tz);
|
|
189
189
|
}
|
|
190
190
|
function cliffMask(x, z) {
|
|
191
191
|
return mask ? bilerp(mask, locate(x, z), 1, 0) : 0;
|
|
@@ -206,23 +206,23 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
206
206
|
bx = 0,
|
|
207
207
|
bz = 0,
|
|
208
208
|
id = 0;
|
|
209
|
-
for (let
|
|
210
|
-
for (let
|
|
211
|
-
const cx = ix +
|
|
212
|
-
cz = iz +
|
|
209
|
+
for (let rowOffset = -1; rowOffset <= 1; rowOffset++)
|
|
210
|
+
for (let columnOffset = -1; columnOffset <= 1; columnOffset++) {
|
|
211
|
+
const cx = ix + columnOffset,
|
|
212
|
+
cz = iz + rowOffset,
|
|
213
213
|
sx = cx + 0.1 + 0.8 * hash2(cx, cz, seed),
|
|
214
214
|
sz = cz + 0.1 + 0.8 * hash2(cx, cz, seed + 1);
|
|
215
|
-
const
|
|
216
|
-
if (
|
|
215
|
+
const distanceSquared = (sx - gx) ** 2 + (sz - gz) ** 2;
|
|
216
|
+
if (distanceSquared < d1) {
|
|
217
217
|
d2 = d1;
|
|
218
218
|
bx = ax;
|
|
219
219
|
bz = az;
|
|
220
|
-
d1 =
|
|
220
|
+
d1 = distanceSquared;
|
|
221
221
|
ax = sx;
|
|
222
222
|
az = sz;
|
|
223
223
|
id = cx * 7919 + cz;
|
|
224
|
-
} else if (
|
|
225
|
-
d2 =
|
|
224
|
+
} else if (distanceSquared < d2) {
|
|
225
|
+
d2 = distanceSquared;
|
|
226
226
|
bx = sx;
|
|
227
227
|
bz = sz;
|
|
228
228
|
}
|
|
@@ -241,26 +241,26 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
241
241
|
return jnt;
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
-
// The rock surface at the ground point (x, z) for level of detail `
|
|
245
|
-
//
|
|
246
|
-
function cliffPoint(x, z,
|
|
244
|
+
// The rock surface at the ground point (x, z) for level of detail `levelIndex`. Writes the position and what the shading needs to `output`:
|
|
245
|
+
// output.p (x, y, z), output.yy (the bedding height), output.bed (its tint 0..1), output.hard, output.cav (how far back in a crevice), output.m (cliff mask).
|
|
246
|
+
function cliffPoint(x, z, levelIndex, output) {
|
|
247
247
|
const k = locate(x, z),
|
|
248
|
-
|
|
248
|
+
maskValue = bilerp(mask, k, 1, 0),
|
|
249
249
|
bd = bilerp(band, k, 1, 0);
|
|
250
|
-
let nx = bilerp(
|
|
251
|
-
ny = bilerp(
|
|
252
|
-
nz = bilerp(
|
|
250
|
+
let nx = bilerp(smoothedNormals, k, 3, 0),
|
|
251
|
+
ny = bilerp(smoothedNormals, k, 3, 1),
|
|
252
|
+
nz = bilerp(smoothedNormals, k, 3, 2);
|
|
253
253
|
const nl = Math.hypot(nx, ny, nz) || 1;
|
|
254
254
|
nx /= nl;
|
|
255
255
|
ny /= nl;
|
|
256
256
|
nz /= nl;
|
|
257
257
|
const h = meshHeight(x, z),
|
|
258
|
-
|
|
258
|
+
gridSpacing = levels[levelIndex];
|
|
259
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
|
|
261
|
-
hard = clamp(
|
|
262
|
-
|
|
263
|
-
u = 1 - (
|
|
260
|
+
const bed = bedAt(yy),
|
|
261
|
+
hard = clamp(bedProfile.hard[bed] + (bd - 0.5) * 0.3, 0, 1),
|
|
262
|
+
bedThickness = bedProfile.thick[bed],
|
|
263
|
+
u = 1 - (bedProfile.top[bed] - yy) / bedThickness;
|
|
264
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
265
|
const th = Math.acos(clamp(ny, 0.05, 1)),
|
|
266
266
|
sinT = Math.max(Math.sin(th), 0.3);
|
|
@@ -269,7 +269,7 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
269
269
|
const tf = Math.tan(clamp(face - th, 0.05, 1.45)),
|
|
270
270
|
tl = Math.tan(clamp(th - ledge, 0.05, 1.3)),
|
|
271
271
|
f = tl / (tf + tl);
|
|
272
|
-
let A = f * (
|
|
272
|
+
let A = f * (bedThickness / sinT) * tf;
|
|
273
273
|
const cap = lerp(0.3, 2.6, sstep(0.25, 0.8, hard)) * (0.75 + 0.5 * bd);
|
|
274
274
|
if (A > cap) A = cap;
|
|
275
275
|
// Use the actual triangle's rise here. A smoothed normal can miss a near-vertical
|
|
@@ -278,8 +278,8 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
278
278
|
const dhx = upper ? heights[k + stride + 1] - heights[k + stride] : heights[k + 1] - heights[k];
|
|
279
279
|
const dhz = upper ? heights[k + stride + 1] - heights[k + 1] : heights[k + stride] - heights[k];
|
|
280
280
|
const rise = Math.max((Math.abs(dhx) + Math.abs(dhz)) / spacing, 0.5);
|
|
281
|
-
const sampleRise =
|
|
282
|
-
const edgeFade = sstep(0, sampleRise * 1.5,
|
|
281
|
+
const sampleRise = gridSpacing * rise, resolved = sstep(4, 9, bedThickness / sampleRise);
|
|
282
|
+
const edgeFade = sstep(0, sampleRise * 1.5, bedThickness * Math.min(u, 1 - u));
|
|
283
283
|
const ra = u / f,
|
|
284
284
|
rb = (1 - u) / (1 - f),
|
|
285
285
|
tri = Math.max(0, (ra + rb - Math.sqrt((ra - rb) ** 2 + 0.06)) * 0.5); // the lip of each face worn round
|
|
@@ -288,52 +288,52 @@ export function createStrataField({ seed = 0, cells, spacing, levels = [0.5, 1,
|
|
|
288
288
|
joints(x, z, 7.5 + 3 * bd, 540, 1.0);
|
|
289
289
|
let dj = jnt.off;
|
|
290
290
|
const e1 = jnt.edge;
|
|
291
|
-
joints(x +
|
|
292
|
-
dj += jnt.off * (
|
|
291
|
+
joints(x + bed * 1.7, z - bed * 2.3, 3.1, 560 + bed, 0.5);
|
|
292
|
+
dj += jnt.off * (levelIndex === 0 ? 1 : levelIndex === 1 ? 0.5 : 0) * resolved * edgeFade;
|
|
293
293
|
const e2 = jnt.edge;
|
|
294
|
-
if (
|
|
295
|
-
joints(x -
|
|
294
|
+
if (levelIndex === 0) {
|
|
295
|
+
joints(x - bed * 0.7, z + bed * 1.3, 1.25, 580 + bed, 0.2);
|
|
296
296
|
dj += jnt.off * resolved * edgeFade;
|
|
297
297
|
}
|
|
298
|
-
const groove = 0.6 * sstep(0.9, 0, e1) * (
|
|
298
|
+
const groove = 0.6 * sstep(0.9, 0, e1) * (levelIndex === 2 ? 0 : 1) + 0.3 * sstep(0.45, 0, e2) * (levelIndex === 0 ? 1 : 0) * resolved * edgeFade;
|
|
299
299
|
dj = Math.max(dj - groove, 0);
|
|
300
300
|
// relief: broad buttresses and bays, and creased (not rounded) swells at a few metres
|
|
301
301
|
let dn = 1.0 * fbm(x / 31, z / 31, 3, 520) + 0.5 * vnoise((x + z) / 47, yy / 19, 521);
|
|
302
|
-
if (
|
|
303
|
-
const amp = sstep(0.3, 0.9,
|
|
304
|
-
base = lerp(-1.6, 0.3, sstep(0.12, 0.55,
|
|
302
|
+
if (levelIndex < 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, maskValue) * (0.8 + 0.4 * bd),
|
|
304
|
+
base = lerp(-1.6, 0.3, sstep(0.12, 0.55, maskValue));
|
|
305
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
306
|
// for faces turned east or west, mixed by which way this one faces
|
|
307
307
|
const wz = (nz * nz) / (nx * nx + nz * nz + 1e-6),
|
|
308
308
|
ridge = v => 1 - Math.abs(2 * v - 1);
|
|
309
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 (
|
|
310
|
+
if (levelIndex < 2) rib += lerp(ridge(vnoise(z / 4.2, x / 19, 543)), ridge(vnoise(x / 4.2, z / 19, 544)), wz) * 0.55;
|
|
311
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
312
|
const sheer = lerp(1, 0.3, sstep(0.95, 1.22, th));
|
|
313
313
|
const off = base + amp * Math.min(ds + rib * lerp(0.5, 1, sstep(0.7, 1.1, th)) + (dj + dn) * sheer, 5);
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
return
|
|
314
|
+
output.p[0] = x + nx * off;
|
|
315
|
+
output.p[1] = h + ny * off;
|
|
316
|
+
output.p[2] = z + nz * off;
|
|
317
|
+
output.yy = yy;
|
|
318
|
+
output.bed = lerp(0.5, bedProfile.tint[bed], resolved * edgeFade);
|
|
319
|
+
output.hard = lerp(0.6, hard, resolved * edgeFade);
|
|
320
|
+
output.m = maskValue;
|
|
321
|
+
output.h = h;
|
|
322
|
+
output.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 output;
|
|
324
324
|
}
|
|
325
325
|
// The baked ground colour at (x, z), in linear light.
|
|
326
326
|
function groundColor(x, z, out) {
|
|
327
327
|
const k = locate(x, z);
|
|
328
|
-
for (let
|
|
328
|
+
for (let channel = 0; channel < 3; channel++) out[channel] = SRGB2LIN[Math.round(bilerp(colours, k, 3, channel))];
|
|
329
329
|
return out;
|
|
330
330
|
}
|
|
331
331
|
|
|
332
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:
|
|
333
|
+
const cliffShapePlan = () => ({ mask: mask.slice(), nrm: smoothedNormals.slice(), band: band.slice() });
|
|
334
334
|
function takeCliffShape(plan, input) {
|
|
335
335
|
setGround(input);
|
|
336
|
-
({ mask, nrm, band } = plan); // the worker's copies, the page's own once they have crossed
|
|
336
|
+
({ mask, nrm: smoothedNormals, band } = plan); // the worker's copies, the page's own once they have crossed
|
|
337
337
|
}
|
|
338
338
|
return { levels, cliffMask, cliffPoint, cliffShapePlan, groundColor, planCliffShape, takeCliffShape };
|
|
339
339
|
}
|