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.
Files changed (139) hide show
  1. package/CONTRIBUTING.md +3 -1
  2. package/README.md +11 -34
  3. package/geometry/bedding.js +141 -0
  4. package/geometry/builder-cells.js +42 -0
  5. package/geometry/builder-frame.js +47 -0
  6. package/geometry/builder.js +189 -177
  7. package/geometry/cell-mask.js +25 -0
  8. package/geometry/creased-sheet.js +34 -34
  9. package/geometry/ear.js +36 -36
  10. package/geometry/erosion.js +65 -42
  11. package/geometry/face-grid.js +65 -65
  12. package/geometry/foliage.js +10 -10
  13. package/geometry/hair-gathers.js +41 -41
  14. package/geometry/hair-locks.js +41 -41
  15. package/geometry/hair-patch.js +18 -18
  16. package/geometry/hair-shell.js +31 -31
  17. package/geometry/head-profile.js +15 -15
  18. package/geometry/head-shape.js +77 -77
  19. package/geometry/heightfield.js +27 -18
  20. package/geometry/index-parts.js +2 -2
  21. package/geometry/instance-visibility.js +12 -12
  22. package/geometry/instances.js +15 -15
  23. package/geometry/outline-grid.js +7 -7
  24. package/geometry/part-ranges.js +3 -3
  25. package/geometry/path-field.js +63 -33
  26. package/geometry/placement.js +52 -52
  27. package/geometry/point-buckets.js +50 -0
  28. package/geometry/primitives.js +63 -47
  29. package/geometry/sagging-tube.js +27 -27
  30. package/geometry/sampled-surface.js +109 -77
  31. package/geometry/scatter-grid.js +46 -6
  32. package/geometry/sculpt.js +93 -93
  33. package/geometry/shore-grid.js +77 -34
  34. package/geometry/strata-field.js +92 -92
  35. package/geometry/strata-tiles.js +91 -91
  36. package/geometry/stream.js +70 -48
  37. package/geometry/terrain-chunks.js +87 -82
  38. package/geometry/terrain-extension.js +6 -6
  39. package/index.js +5 -0
  40. package/math/cell-edge.js +16 -16
  41. package/math/cell-key.js +6 -0
  42. package/math/circle-clearances.js +19 -0
  43. package/math/cloth-textures.js +6 -6
  44. package/math/cloud-textures.js +20 -20
  45. package/math/color.js +14 -14
  46. package/math/curve.js +22 -22
  47. package/math/mountain-ranges.js +216 -165
  48. package/math/noise.js +101 -49
  49. package/math/particle-noise.js +28 -28
  50. package/math/periodic.js +5 -5
  51. package/math/random.js +24 -15
  52. package/math/rectangles.js +52 -0
  53. package/math/rock-texture.js +7 -7
  54. package/math/scalar.js +5 -5
  55. package/math/surface-textures.js +54 -54
  56. package/motion/arm-reach.js +24 -24
  57. package/motion/ballistic-arc.js +11 -0
  58. package/motion/biped-cycle.js +44 -0
  59. package/motion/biped-leg.js +37 -0
  60. package/motion/biped-stride.js +98 -122
  61. package/motion/contact-transition.js +33 -0
  62. package/motion/crowd-alarms.js +39 -39
  63. package/motion/crowd-routine.js +94 -65
  64. package/motion/daily-clock.js +9 -9
  65. package/motion/deck-surface.js +38 -0
  66. package/motion/ground-lookahead.js +5 -5
  67. package/motion/instance-sight.js +8 -8
  68. package/motion/player.js +36 -28
  69. package/motion/quadruped.js +155 -155
  70. package/motion/spaces.js +32 -32
  71. package/motion/weather.js +73 -24
  72. package/motion/wind.js +32 -13
  73. package/package.json +7 -3
  74. package/rendering/caster-motion.js +5 -5
  75. package/rendering/cell-lod.js +138 -82
  76. package/rendering/cloud-dome.js +28 -28
  77. package/rendering/cloud-field.js +2 -2
  78. package/rendering/compilation-schedule.js +68 -11
  79. package/rendering/contact-shadows.js +2 -2
  80. package/rendering/context.js +12 -0
  81. package/rendering/crowd-skinning.js +13 -13
  82. package/rendering/crust.js +18 -12
  83. package/rendering/daylight.js +3 -3
  84. package/rendering/detail-range.js +28 -0
  85. package/rendering/dispose-meshes.js +18 -0
  86. package/rendering/dof.js +9 -9
  87. package/rendering/eye-adapt.js +14 -14
  88. package/rendering/far-shadow.js +8 -8
  89. package/rendering/flocks.js +78 -78
  90. package/rendering/garment-material.js +17 -17
  91. package/rendering/glow-lines.js +13 -13
  92. package/rendering/hair-strands.js +20 -20
  93. package/rendering/height-probe.js +149 -19
  94. package/rendering/label-layout.js +46 -0
  95. package/rendering/label-range.js +38 -0
  96. package/rendering/light-budget.js +52 -52
  97. package/rendering/light-shafts.js +23 -23
  98. package/rendering/local-lights.js +15 -4
  99. package/rendering/material-lod.js +49 -49
  100. package/rendering/particle-emitters.js +90 -90
  101. package/rendering/particles.js +78 -67
  102. package/rendering/piece-batch.js +17 -6
  103. package/rendering/planar-reflection.js +38 -38
  104. package/rendering/post-shaders.js +17 -13
  105. package/rendering/post.js +71 -71
  106. package/rendering/rain-streaks.js +67 -0
  107. package/rendering/road-surface.js +11 -11
  108. package/rendering/sea-shaders.js +11 -11
  109. package/rendering/shader-warmup.js +29 -16
  110. package/rendering/shadow-alignment.js +4 -4
  111. package/rendering/shadow-depth.js +7 -7
  112. package/rendering/sky-occlusion.js +16 -16
  113. package/rendering/sky.js +16 -6
  114. package/rendering/stage-lights.js +6 -6
  115. package/rendering/static-point-shadow.js +3 -3
  116. package/rendering/still-casters.js +1 -1
  117. package/rendering/strata-material.js +10 -10
  118. package/rendering/stream-flow.js +10 -10
  119. package/rendering/taa.js +26 -26
  120. package/rendering/talus.js +15 -15
  121. package/rendering/terrain-rock.js +7 -7
  122. package/rendering/texture-fills.js +8 -8
  123. package/rendering/textures.js +28 -28
  124. package/rendering/wakes.js +24 -24
  125. package/rendering/water-material.js +36 -23
  126. package/rendering/world-matrices.js +38 -38
  127. package/runtime/diagnostic-log.js +2 -2
  128. package/runtime/first-use.js +33 -28
  129. package/runtime/frame-pacing.js +33 -0
  130. package/runtime/gpu-timing.js +7 -7
  131. package/runtime/grid-cache.js +1 -1
  132. package/runtime/grid-rows.js +44 -5
  133. package/runtime/hand-over.js +41 -23
  134. package/runtime/pool.js +58 -16
  135. package/runtime/quality-fit.js +1 -1
  136. package/runtime/search.js +22 -22
  137. package/runtime/spatial-audio.js +13 -13
  138. package/runtime/withdrawable-task.js +35 -0
  139. package/runtime/worker.js +7 -0
@@ -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
- // The edges of a closed outline, as the distance to it is worked out from them.
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 [ex, ez] = outline[(i + 1) % outline.length],
7
- dx = ex - x,
8
- dz = ez - z;
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
- // The signed distance from (x, z) to an outline's edges: positive inside it.
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 e of edges) {
16
- const px = x - e.x,
17
- pz = z - e.z;
18
- if (e.z > z !== e.ez > z && px < (pz * e.dx) / e.dz) inside = !inside;
19
- const t = clamp((px * e.dx + pz * e.dz) * e.inv, 0, 1);
20
- best = Math.min(best, (px - e.dx * t) ** 2 + (pz - e.dz * t) ** 2);
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 distances, n to a side
27
- n = 0,
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
- // Lays the grid out empty over the square from (x0, z0): n points a side, `step` metres apart.
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
- n = points;
38
- grid = new Float32Array(n * n);
73
+ pointsPerSide = points;
74
+ grid = new Float32Array(pointsPerSide * pointsPerSide);
39
75
  }
40
- // The grid's distances: what a bake fills and a later visit reads back (core/kept.js).
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
- // Fills the grid from the land's outlines, each a closed line of [x, z] points.
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 j = 0; j < n; j++)
46
- for (let i = 0; i < n; 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 + j * step;
49
- let d = -Infinity;
50
- for (const outline of edges) d = Math.max(d, outlineDistance(outline, x, z));
51
- grid[j * n + i] = d;
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
- // The signed distance from (x, z) to the shore, in metres: positive inland.
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, n - 1.001),
57
- v = clamp((z - z0) / step, 0, n - 1.001),
58
- i = Math.floor(u),
59
- j = Math.floor(v),
60
- a = u - i,
61
- b = v - j,
62
- k = j * n + i;
63
- return lerp(lerp(grid[k], grid[k + 1], a), lerp(grid[k + n], grid[k + n + 1], a), b);
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 };
@@ -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
- nrm = null, // the terrain normal smoothed over about 12 m, the direction the rock is pushed out
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
- nrm ??= new Float32Array(stride * stride * 3);
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 BED = { y0: bedRange[0], step: 0.1, top: [], thick: [], hard: [], tint: [], index: null };
26
+ const bedProfile = { y0: bedRange[0], step: 0.1, top: [], thick: [], hard: [], tint: [], index: null };
27
27
  (() => {
28
- const R = mulberry32(seed * 7 + 311);
29
- let y = BED.y0;
28
+ const random = mulberry32(seed * 7 + 311);
29
+ let y = bedProfile.y0;
30
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);
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, 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());
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
- 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;
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 BED.index[clamp(((yy - BED.y0) / BED.step) | 0, 0, BED.index.length - 1)];
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 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;
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 c = 0; c < 3; c++) {
125
- for (let k = 0; k < raw.length; k++) raw[k] = normals[k * 3 + c];
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++) nrm[k * 3 + c] = raw[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(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;
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(a, t, r) {
139
- // separable box blur of radius r cells, in place
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 ? t : a,
142
- dst = pass ? a : t,
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 = -r; d <= r; 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(g, k, n, c) {
173
+ function bilerp(grid, cellIndex, components, channel) {
174
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;
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
- 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);
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 b = -1; b <= 1; b++)
210
- for (let a = -1; a <= 1; a++) {
211
- const cx = ix + a,
212
- cz = iz + b,
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 d = (sx - gx) ** 2 + (sz - gz) ** 2;
216
- if (d < d1) {
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 = d;
220
+ d1 = distanceSquared;
221
221
  ax = sx;
222
222
  az = sz;
223
223
  id = cx * 7919 + cz;
224
- } else if (d < d2) {
225
- d2 = d;
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 `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) {
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
- m = bilerp(mask, k, 1, 0),
248
+ maskValue = bilerp(mask, k, 1, 0),
249
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);
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
- g = levels[lv];
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 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;
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 * (T / sinT) * tf;
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 = g * rise, resolved = sstep(4, 9, T / sampleRise);
282
- const edgeFade = sstep(0, sampleRise * 1.5, T * Math.min(u, 1 - u));
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 + 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;
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 (lv === 0) {
295
- joints(x - b * 0.7, z + b * 1.3, 1.25, 580 + b, 0.2);
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) * (lv === 2 ? 0 : 1) + 0.3 * sstep(0.45, 0, e2) * (lv === 0 ? 1 : 0) * resolved * edgeFade;
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 (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));
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 (lv < 2) rib += lerp(ridge(vnoise(z / 4.2, x / 19, 543)), ridge(vnoise(x / 4.2, z / 19, 544)), wz) * 0.55;
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
- 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;
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 c = 0; c < 3; c++) out[c] = SRGB2LIN[Math.round(bilerp(colours, k, 3, c))];
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: nrm.slice(), band: band.slice() });
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
  }