roamwright 0.2.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 (140) hide show
  1. package/CONTRIBUTING.md +3 -1
  2. package/README.md +19 -21
  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 +47 -0
  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 +60 -0
  21. package/geometry/instance-visibility.js +41 -0
  22. package/geometry/instances.js +35 -12
  23. package/geometry/outline-grid.js +7 -7
  24. package/geometry/part-ranges.js +18 -0
  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 +101 -92
  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 +29 -0
  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 +8 -4
  74. package/rendering/caster-motion.js +5 -5
  75. package/rendering/cell-lod.js +147 -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 +91 -26
  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-budget.js +49 -0
  130. package/runtime/frame-pacing.js +33 -0
  131. package/runtime/gpu-timing.js +13 -10
  132. package/runtime/grid-cache.js +1 -1
  133. package/runtime/grid-rows.js +44 -5
  134. package/runtime/hand-over.js +41 -23
  135. package/runtime/pool.js +58 -16
  136. package/runtime/quality-fit.js +1 -1
  137. package/runtime/search.js +22 -22
  138. package/runtime/spatial-audio.js +13 -13
  139. package/runtime/withdrawable-task.js +35 -0
  140. package/runtime/worker.js +7 -0
package/math/noise.js CHANGED
@@ -1,8 +1,34 @@
1
+ /**
2
+ * Seeded two-dimensional noise for terrain heights and seamless textures.
3
+ * @module
4
+ */
5
+
1
6
  import { createHash } from './random.js';
2
7
 
8
+ /**
9
+ * Create a family of noise samplers that share a world seed. Use `fbm` for
10
+ * layered terrain, `ridged` for crests and `tfbm` for seamless textures.
11
+ * For a terrain feature spanning roughly 100 metres, divide world X/Z by 100
12
+ * before sampling the first octave. For a texture, pass unit UVs to `tfbm`;
13
+ * the opposite edges of the unit tile match.
14
+ * @example
15
+ * ```js
16
+ * import { createNoise } from 'roamwright';
17
+ *
18
+ * const { fbm, tfbm } = createNoise(42);
19
+ * const groundAt = (x, z) => 30 * fbm(x / 100, z / 100, 4, 7);
20
+ * const grainAt = (u, v) => tfbm(u, v, 8, 3, 11);
21
+ * ```
22
+ * @param worldSeed - Integer offset shared by the samplers. Each sample also accepts its own seed.
23
+ */
3
24
  export function createNoise(worldSeed = 0) {
4
25
  const hash2 = createHash(worldSeed);
5
- // Value noise in 0..1, smooth between the integer grid points.
26
+ /**
27
+ * Smooth value noise in [0, 1] between integer grid points.
28
+ * @param {number} x - Noise-grid X.
29
+ * @param {number} z - Noise-grid Z.
30
+ * @param {number} seed - Integer sample seed.
31
+ */
6
32
  function vnoise(x, z, seed) {
7
33
  const ix = Math.floor(x),
8
34
  iz = Math.floor(z),
@@ -10,71 +36,97 @@ export function createNoise(worldSeed = 0) {
10
36
  fz = z - iz;
11
37
  const ux = fx * fx * (3 - 2 * fx),
12
38
  uz = fz * fz * (3 - 2 * fz);
13
- const a = hash2(ix, iz, seed),
14
- b = hash2(ix + 1, iz, seed),
15
- c = hash2(ix, iz + 1, seed),
16
- d = hash2(ix + 1, iz + 1, seed);
17
- return a + (b - a) * ux + (c - a) * uz + (a - b - c + d) * ux * uz;
39
+ const nearLeft = hash2(ix, iz, seed),
40
+ nearRight = hash2(ix + 1, iz, seed),
41
+ farLeft = hash2(ix, iz + 1, seed),
42
+ farRight = hash2(ix + 1, iz + 1, seed);
43
+ return nearLeft + (nearRight - nearLeft) * ux + (farLeft - nearLeft) * uz + (nearLeft - nearRight - farLeft + farRight) * ux * uz;
18
44
  }
19
- // Fractal noise: oct octaves of value noise, each twice as fine and half as strong.
20
- function fbm(x, z, oct, seed) {
21
- let s = 0,
22
- a = 0.5,
23
- n = 0;
24
- for (let i = 0; i < oct; i++) {
25
- s += a * vnoise(x, z, seed + i * 17);
26
- n += a;
27
- a *= 0.5;
45
+ /**
46
+ * Average layers of value noise, halving the weight at each finer scale.
47
+ * @param {number} x - First-octave X.
48
+ * @param {number} z - First-octave Z.
49
+ * @param {number} octaves - Positive integer octave count.
50
+ * @param {number} seed - Integer sample seed.
51
+ */
52
+ function fbm(x, z, octaves, seed) {
53
+ let weightedSum = 0,
54
+ amplitude = 0.5,
55
+ totalWeight = 0;
56
+ for (let octave = 0; octave < octaves; octave++) {
57
+ weightedSum += amplitude * vnoise(x, z, seed + octave * 17);
58
+ totalWeight += amplitude;
59
+ amplitude *= 0.5;
28
60
  x = x * 2.03 + 11.7;
29
61
  z = z * 2.03 - 5.3;
30
62
  }
31
- return s / n;
63
+ return weightedSum / totalWeight;
32
64
  }
33
- // Ridged fractal noise: sharp crests where the noise crosses its middle, for ridges and veins.
34
- function ridged(x, z, oct, seed) {
35
- let s = 0,
36
- a = 0.5,
37
- n = 0;
38
- for (let i = 0; i < oct; i++) {
39
- const v = 1 - Math.abs(vnoise(x, z, seed + i * 31) * 2 - 1);
40
- s += a * v * v;
41
- n += a;
42
- a *= 0.5;
65
+ /**
66
+ * Layer sharp crests where the underlying value noise crosses its midpoint.
67
+ * @param {number} x - First-octave X.
68
+ * @param {number} z - First-octave Z.
69
+ * @param {number} octaves - Positive integer octave count.
70
+ * @param {number} seed - Integer sample seed.
71
+ */
72
+ function ridged(x, z, octaves, seed) {
73
+ let weightedSum = 0,
74
+ amplitude = 0.5,
75
+ totalWeight = 0;
76
+ for (let octave = 0; octave < octaves; octave++) {
77
+ const ridgeValue = 1 - Math.abs(vnoise(x, z, seed + octave * 31) * 2 - 1);
78
+ weightedSum += amplitude * ridgeValue * ridgeValue;
79
+ totalWeight += amplitude;
80
+ amplitude *= 0.5;
43
81
  x = x * 2.1 + 3.1;
44
82
  z = z * 2.1 + 8.9;
45
83
  }
46
- return s / n;
84
+ return weightedSum / totalWeight;
47
85
  }
48
- // Tileable noise for textures: it repeats every `per` cells (every `perY` along y, where the cells are not square).
49
- function tnoise(x, y, per, seed, perY = per) {
86
+ /**
87
+ * Sample smooth noise that repeats after a whole number of lattice cells.
88
+ * @param {number} x - Horizontal lattice coordinate.
89
+ * @param {number} y - Vertical lattice coordinate.
90
+ * @param {number} period - Positive integer period along X.
91
+ * @param {number} seed - Integer sample seed.
92
+ * @param {number} [verticalPeriod] - Vertical period in cells. Defaults to per.
93
+ */
94
+ function tnoise(x, y, period, seed, verticalPeriod = period) {
50
95
  const ix = Math.floor(x),
51
96
  iy = Math.floor(y),
52
97
  fx = x - ix,
53
98
  fy = y - iy;
54
99
  const ux = fx * fx * (3 - 2 * fx),
55
100
  uy = fy * fy * (3 - 2 * fy);
56
- const x0 = ((ix % per) + per) % per,
57
- x1 = (x0 + 1) % per,
58
- y0 = ((iy % perY) + perY) % perY,
59
- y1 = (y0 + 1) % perY;
60
- const a = hash2(x0, y0, seed),
61
- b = hash2(x1, y0, seed),
62
- c = hash2(x0, y1, seed),
63
- d = hash2(x1, y1, seed);
64
- return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy;
101
+ const x0 = ((ix % period) + period) % period,
102
+ x1 = (x0 + 1) % period,
103
+ y0 = ((iy % verticalPeriod) + verticalPeriod) % verticalPeriod,
104
+ y1 = (y0 + 1) % verticalPeriod;
105
+ const nearLeft = hash2(x0, y0, seed),
106
+ nearRight = hash2(x1, y0, seed),
107
+ farLeft = hash2(x0, y1, seed),
108
+ farRight = hash2(x1, y1, seed);
109
+ return nearLeft + (nearRight - nearLeft) * ux + (farLeft - nearLeft) * uy + (nearLeft - nearRight - farLeft + farRight) * ux * uy;
65
110
  }
66
- // Fractal noise that tiles with period per, for seamless textures.
67
- function tfbm(u, v, per, oct, seed) {
68
- let s = 0,
69
- a = 0.5,
70
- n = 0;
71
- for (let i = 0; i < oct; i++) {
72
- s += a * tnoise(u * per, v * per, per, seed + i * 13);
73
- n += a;
74
- a *= 0.5;
75
- per *= 2;
111
+ /**
112
+ * Layer tileable noise in UV space. Adding one to u or v repeats the same sample.
113
+ * @param {number} u - Horizontal texture coordinate.
114
+ * @param {number} v - Vertical texture coordinate.
115
+ * @param {number} period - Positive integer first-octave cell count per tile.
116
+ * @param {number} octaves - Positive integer octave count.
117
+ * @param {number} seed - Integer sample seed.
118
+ */
119
+ function tfbm(u, v, period, octaves, seed) {
120
+ let weightedSum = 0,
121
+ amplitude = 0.5,
122
+ totalWeight = 0;
123
+ for (let octave = 0; octave < octaves; octave++) {
124
+ weightedSum += amplitude * tnoise(u * period, v * period, period, seed + octave * 13);
125
+ totalWeight += amplitude;
126
+ amplitude *= 0.5;
127
+ period *= 2;
76
128
  }
77
- return s / n;
129
+ return weightedSum / totalWeight;
78
130
  }
79
131
  return { fbm, ridged, tfbm, tnoise, vnoise };
80
132
  }
@@ -4,45 +4,45 @@ import { createNoise } from './noise.js';
4
4
  // The effects' noise sheet, shared by every sprite. R: billowing lumps with creases between them; G, B: the
5
5
  // slope of R, so a sprite can be shaded as if it had relief; A: a softer cloud that frays edges and drives the flames. R's slope
6
6
  // needs its neighbours, so the sheet is worked out whole first, in its own rows (lowest first), as whole bytes.
7
- export function particleNoise({ size: N = 256, ridgeSeed = 311, cloudSeed = 733, noise = createNoise() } = {}) {
7
+ export function particleNoise({ size: size = 256, ridgeSeed = 311, cloudSeed = 733, noise = createNoise() } = {}) {
8
8
  const { tfbm, tnoise } = noise;
9
- const H = new Float32Array(N * N),
10
- d = new Uint8Array(N * N * 4);
11
- for (let y = 0; y < N; y++)
12
- for (let x = 0; x < N; x++) {
13
- const u = (x + 0.5) / N,
14
- v = (y + 0.5) / N;
9
+ const heightSamples = new Float32Array(size * size),
10
+ pixels = new Uint8Array(size * size * 4);
11
+ for (let y = 0; y < size; y++)
12
+ for (let x = 0; x < size; x++) {
13
+ const u = (x + 0.5) / size,
14
+ v = (y + 0.5) / size;
15
15
  let h = 0,
16
- a = 0.5,
16
+ amplitude = 0.5,
17
17
  n = 0,
18
18
  per = 3;
19
19
  for (let o = 0; o < 4; o++) {
20
- h += a * Math.abs(tnoise(u * per, v * per, per, ridgeSeed + o * 17) * 2 - 1);
21
- n += a;
22
- a *= 0.5;
20
+ h += amplitude * Math.abs(tnoise(u * per, v * per, per, ridgeSeed + o * 17) * 2 - 1);
21
+ n += amplitude;
22
+ amplitude *= 0.5;
23
23
  per *= 2;
24
24
  }
25
- H[y * N + x] = clamp((h / n) * 1.9, 0, 1);
25
+ heightSamples[y * size + x] = clamp((h / n) * 1.9, 0, 1);
26
26
  }
27
- for (let y = 0; y < N; y++)
28
- for (let x = 0; x < N; x++) {
29
- const i = y * N + x,
27
+ for (let y = 0; y < size; y++)
28
+ for (let x = 0; x < size; x++) {
29
+ const i = y * size + x,
30
30
  k = i * 4,
31
- xm = (x + N - 1) % N,
32
- xp = (x + 1) % N,
33
- ym = (y + N - 1) % N,
34
- yp = (y + 1) % N;
35
- d[k] = H[i] * 255;
36
- d[k + 1] = clamp(0.5 + (H[y * N + xp] - H[y * N + xm]) * 5, 0, 1) * 255;
37
- d[k + 2] = clamp(0.5 + (H[yp * N + x] - H[ym * N + x]) * 5, 0, 1) * 255;
38
- d[k + 3] = clamp((tfbm((x + 0.5) / N, (y + 0.5) / N, 5, 4, cloudSeed) - 0.5) * 1.7 + 0.5, 0, 1) * 255;
31
+ xm = (x + size - 1) % size,
32
+ xp = (x + 1) % size,
33
+ ym = (y + size - 1) % size,
34
+ yp = (y + 1) % size;
35
+ pixels[k] = heightSamples[i] * 255;
36
+ pixels[k + 1] = clamp(0.5 + (heightSamples[y * size + xp] - heightSamples[y * size + xm]) * 5, 0, 1) * 255;
37
+ pixels[k + 2] = clamp(0.5 + (heightSamples[yp * size + x] - heightSamples[ym * size + x]) * 5, 0, 1) * 255;
38
+ pixels[k + 3] = clamp((tfbm((x + 0.5) / size, (y + 0.5) / size, 5, 4, cloudSeed) - 0.5) * 1.7 + 0.5, 0, 1) * 255;
39
39
  }
40
40
  // fillPixels writes the row of v from the top down; the sheet's row y is read as the card's row y
41
41
  return (u, v, out) => {
42
- const k = ((N - 1 - Math.floor(v * N)) * N + Math.floor(u * N)) * 4;
43
- out[0] = d[k];
44
- out[1] = d[k + 1];
45
- out[2] = d[k + 2];
46
- out[3] = d[k + 3];
42
+ const k = ((size - 1 - Math.floor(v * size)) * size + Math.floor(u * size)) * 4;
43
+ out[0] = pixels[k];
44
+ out[1] = pixels[k + 1];
45
+ out[2] = pixels[k + 2];
46
+ out[3] = pixels[k + 3];
47
47
  };
48
48
  }
package/math/periodic.js CHANGED
@@ -5,16 +5,16 @@
5
5
  * within that step.
6
6
  */
7
7
  export function cycleDelta(from, to, period = 1) {
8
- const d = to - from;
9
- return d - Math.round(d / period) * period;
8
+ const delta = to - from;
9
+ return delta - Math.round(delta / period) * period;
10
10
  }
11
11
 
12
12
  // Advance a normalized phase towards its target by at most step. Snap exactly on arrival.
13
13
  export function approachPhase(from, to, step, onArrival) {
14
- const d = cycleDelta(from, to);
15
- if (Math.abs(d) <= step) {
14
+ const delta = cycleDelta(from, to);
15
+ if (Math.abs(delta) <= step) {
16
16
  onArrival?.();
17
17
  return to;
18
18
  }
19
- return (from + Math.sign(d) * step + 1) % 1;
19
+ return (from + Math.sign(delta) * step + 1) % 1;
20
20
  }
package/math/random.js CHANGED
@@ -1,27 +1,36 @@
1
- // A small seeded random generator: the same seed gives the same world every time. Its `state()` is where the stream stands:
2
- // mulberry32(state) goes on from there, as a stream handed to a worker of the pool does.
3
- function mulberry32(a) {
1
+ /**
2
+ * Repeatable random streams and coordinate hashes for procedural placement.
3
+ * @module
4
+ */
5
+
6
+ /**
7
+ * Create a repeatable stream of numbers in [0, 1). Save `next.state()` and pass
8
+ * it to a new generator to resume at the next draw, including in a worker.
9
+ * @param {number} state - Integer seed or a previously saved generator state.
10
+ */
11
+ function mulberry32(state) {
4
12
  const next = function () {
5
- a |= 0;
6
- a = (a + 0x6d2b79f5) | 0;
7
- let t = Math.imul(a ^ (a >>> 15), 1 | a);
8
- t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
9
- return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
13
+ state |= 0;
14
+ state = (state + 0x6d2b79f5) | 0;
15
+ let mixedState = Math.imul(state ^ (state >>> 15), 1 | state);
16
+ mixedState = (mixedState + Math.imul(mixedState ^ (mixedState >>> 7), 61 | mixedState)) ^ mixedState;
17
+ return ((mixedState ^ (mixedState >>> 14)) >>> 0) / 4294967296;
10
18
  };
11
- next.state = () => a;
19
+ next.state = () => state;
12
20
  return next;
13
21
  }
14
- // A random item of a, drawn with the generator R.
15
- const pickR = (R, a) => a[(R() * a.length) | 0];
22
+ /** Pick one item using the supplied random generator. */
23
+ const pickR = (random, items) => items[(random() * items.length) | 0];
16
24
  export { mulberry32, pickR };
17
25
 
26
+ /** Return a repeatable hash in [0, 1) for integer X/Z grid coordinates and a per-sample seed. */
18
27
  export function createHash(worldSeed = 0) {
19
28
  // A random number in 0..1 fixed by two integers and a seed.
20
29
  function hash2(ix, iz, seed) {
21
- let h = (Math.imul(ix, 374761393) + Math.imul(iz, 668265263) + Math.imul(seed + worldSeed, 1442695041)) | 0;
22
- h = Math.imul(h ^ (h >>> 13), 1274126177);
23
- h ^= h >>> 16;
24
- return (h >>> 0) / 4294967296;
30
+ let hash = (Math.imul(ix, 374761393) + Math.imul(iz, 668265263) + Math.imul(seed + worldSeed, 1442695041)) | 0;
31
+ hash = Math.imul(hash ^ (hash >>> 13), 1274126177);
32
+ hash ^= hash >>> 16;
33
+ return (hash >>> 0) / 4294967296;
25
34
  }
26
35
  return hash2;
27
36
  }
@@ -0,0 +1,52 @@
1
+ /** Distance in world units from a point to a yawed rectangle, zero inside.
2
+ * Supply the centre, half width and depth, yaw in radians, and the query point. */
3
+ export function distanceToRectangle(centerX, centerZ, halfWidth, halfDepth, rotation, pointX, pointZ) {
4
+ const dx = pointX - centerX,
5
+ dz = pointZ - centerZ,
6
+ cosRotation = Math.cos(rotation),
7
+ sinRotation = Math.sin(rotation);
8
+ return Math.hypot(Math.max(Math.abs(dx * cosRotation - dz * sinRotation) - halfWidth, 0), Math.max(Math.abs(dx * sinRotation + dz * cosRotation) - halfDepth, 0));
9
+ }
10
+
11
+ /** Four x-z perimeter corners, using full dimensions and a yaw in radians. */
12
+ export function rectangleCorners(centerX, centerZ, width, depth, rotation) {
13
+ const cosRotation = Math.cos(rotation),
14
+ sinRotation = Math.sin(rotation),
15
+ o = [];
16
+ for (const [u, v] of [
17
+ [1, 1],
18
+ [1, -1],
19
+ [-1, -1],
20
+ [-1, 1],
21
+ ])
22
+ o.push([centerX + (u * width * cosRotation + v * depth * sinRotation) / 2, centerZ + (-u * width * sinRotation + v * depth * cosRotation) / 2]);
23
+ return o;
24
+ }
25
+
26
+ // Strict overlap of two nondegenerate rectangles given four perimeter corners each. Touching edges have no area in common.
27
+ export function rectanglesOverlap(firstCorners, secondCorners) {
28
+ for (const [P1, P2] of [
29
+ [firstCorners, secondCorners],
30
+ [secondCorners, firstCorners],
31
+ ])
32
+ for (let i = 0; i < 2; i++) {
33
+ const ax = P1[i + 1][0] - P1[i][0],
34
+ az = P1[i + 1][1] - P1[i][1];
35
+ let a0 = Infinity,
36
+ a1 = -Infinity,
37
+ b0 = Infinity,
38
+ b1 = -Infinity;
39
+ for (const p of P1) {
40
+ const t = p[0] * ax + p[1] * az;
41
+ a0 = Math.min(a0, t);
42
+ a1 = Math.max(a1, t);
43
+ }
44
+ for (const p of P2) {
45
+ const t = p[0] * ax + p[1] * az;
46
+ b0 = Math.min(b0, t);
47
+ b1 = Math.max(b1, t);
48
+ }
49
+ if (a1 <= b0 || b1 <= a0) return false;
50
+ }
51
+ return true;
52
+ }
@@ -18,12 +18,12 @@ export function createRockTexture(seed = 0, { fractures = 'cells' } = {}) {
18
18
  const { tfbm } = createNoise(seed);
19
19
  const jointed = fractures === 'joints';
20
20
  if (!jointed && fractures !== 'cells') throw new TypeError('Unknown rock fracture pattern: ' + fractures);
21
- return (u, v, o) => {
22
- const n = tfbm(u, v, 6, 6, 81),
21
+ return (u, v, output) => {
22
+ const grainNoise = tfbm(u, v, 6, 6, 81),
23
23
  w = tfbm(u, v, 4, 3, 82) - 0.5;
24
- o[0] = clamp(255 * (n * 1.25 - 0.12), 0, 255);
24
+ output[0] = clamp(255 * (grainNoise * 1.25 - 0.12), 0, 255);
25
25
  if (jointed) {
26
- const warp = w * 0.22 + (n - 0.5) * 0.08;
26
+ const warp = w * 0.22 + (grainNoise - 0.5) * 0.08;
27
27
  // Independently spaced vertical joints stop in competent beds; the oblique set only opens in patches.
28
28
  const trace = u * 7 + 0.17 * Math.sin(v * Math.PI * 2) + warp;
29
29
  const joint = (Math.round(trace) % 7 + 7) % 7;
@@ -38,7 +38,7 @@ export function createRockTexture(seed = 0, { fractures = 'cells' } = {}) {
38
38
  const seam = fractureLine(bedCoordinate, 6, hash2, 85, 0.065);
39
39
  const open = tfbm(u, v, 4, 2, 86);
40
40
  const crossOpen = tfbm(u, v, 3, 2, 87);
41
- o[1] = 255 * Math.max(
41
+ output[1] = 255 * Math.max(
42
42
  steep * opening * sstep(0.37, 0.61, open),
43
43
  cross * sstep(0.53, 0.76, crossOpen) * 0.64,
44
44
  seam * sstep(0.36, 0.72, crossOpen) * 0.60,
@@ -46,11 +46,11 @@ export function createRockTexture(seed = 0, { fractures = 'cells' } = {}) {
46
46
  } else {
47
47
  const e1 = cellEdge(hash2, u + w * 0.05, v - w * 0.03, 5, 83);
48
48
  const e2 = cellEdge(hash2, u - w * 0.03, v + w * 0.05, 13, 84);
49
- o[1] = 255 * Math.max(
49
+ output[1] = 255 * Math.max(
50
50
  sstep(0.014, 0.003, e1) * sstep(0.55, 0.66, tfbm(u, v, 5, 2, 86)),
51
51
  0.5 * sstep(0.009, 0.002, e2) * sstep(0.66, 0.74, tfbm(u, v, 7, 2, 87)),
52
52
  );
53
53
  }
54
- o[2] = 255 * tfbm(u, v, 4, 5, 85);
54
+ output[2] = 255 * tfbm(u, v, 4, 5, 85);
55
55
  };
56
56
  }
package/math/scalar.js CHANGED
@@ -1,8 +1,8 @@
1
- const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
2
- const lerp = (a, b, t) => a + (b - a) * t;
3
- // Smoothstep: 0 at a, 1 at b, eased between (b may be less than a, to fall instead of rise).
4
- const sstep = (a, b, x) => {
5
- const t = clamp((x - a) / (b - a), 0, 1);
1
+ const clamp = (value, minimum, maximum) => (value < minimum ? minimum : value > maximum ? maximum : value);
2
+ const lerp = (start, end, fraction) => start + (end - start) * fraction;
3
+ /** Ease from 0 at start to 1 at end. Reverse the endpoints for a falling transition. */
4
+ const sstep = (start, end, x) => {
5
+ const t = clamp((x - start) / (end - start), 0, 1);
6
6
  return t * t * (3 - 2 * t);
7
7
  };
8
8
  const TAU = Math.PI * 2;
@@ -10,28 +10,28 @@ import { clamp, sstep } from './scalar.js';
10
10
  export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
11
11
  // Coursed ashlar: courses of uneven height, blocks of uneven length, thin joints, and each block its own tone.
12
12
  function ashlar({ seed = 611, courses = 10 } = {}) {
13
- const R = mulberry32(seed),
13
+ const random = mulberry32(seed),
14
14
  N = courses,
15
15
  rows = [];
16
16
  let acc = 0;
17
17
  for (let i = 0; i < N; i++) {
18
- const h = 0.75 + R() * 0.6;
18
+ const h = 0.75 + random() * 0.6;
19
19
  rows.push({ y0: acc, h });
20
20
  acc += h;
21
21
  }
22
22
  for (const r of rows) {
23
23
  r.y0 /= acc;
24
24
  r.h /= acc;
25
- const n = 3 + ((R() * 3) | 0),
25
+ const n = 3 + ((random() * 3) | 0),
26
26
  cuts = [];
27
- let a = R();
27
+ let cutPosition = random();
28
28
  for (let k = 0; k < n; k++) {
29
- cuts.push(a % 1);
30
- a += (0.7 + R() * 0.6) / n;
29
+ cuts.push(cutPosition % 1);
30
+ cutPosition += (0.7 + random() * 0.6) / n;
31
31
  }
32
32
  r.cuts = cuts.sort((p, q) => p - q);
33
- r.tone = cuts.map(() => 0.86 + R() * 0.26);
34
- r.warm = cuts.map(() => R());
33
+ r.tone = cuts.map(() => 0.86 + random() * 0.26);
34
+ r.warm = cuts.map(() => random());
35
35
  }
36
36
  return (u, v, o) => {
37
37
  let r = rows[N - 1];
@@ -40,29 +40,29 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
40
40
  r = rows[i];
41
41
  break;
42
42
  }
43
- const c_ = r.cuts,
44
- m = c_.length;
43
+ const cuts = r.cuts,
44
+ m = cuts.length;
45
45
  let k = m - 1;
46
46
  for (let i = 0; i < m; i++)
47
- if (u < c_[i]) {
47
+ if (u < cuts[i]) {
48
48
  k = (i + m - 1) % m;
49
49
  break;
50
50
  }
51
- const a = c_[k],
52
- b = c_[(k + 1) % m],
53
- w = (b - a + 1) % 1 || 1,
54
- bu = ((u - a + 1) % 1) / w,
51
+ const startCut = cuts[k],
52
+ endCut = cuts[(k + 1) % m],
53
+ w = (endCut - startCut + 1) % 1 || 1,
54
+ bu = ((u - startCut + 1) % 1) / w,
55
55
  bv = (v - r.y0) / r.h;
56
56
  const dj = Math.min(Math.min(bu, 1 - bu) * w, Math.min(bv, 1 - bv) * r.h); // distance to the nearest joint, in tile units
57
57
  const joint = sstep(0.0, 0.0055, dj),
58
58
  edge = sstep(0.0, 0.028, dj),
59
59
  n = tfbm(u, v, 8, 4, 62),
60
60
  pit = sstep(0.62, 0.8, tfbm(u, v, 32, 2, 65));
61
- const c = (158 + n * 62) * r.tone[k] * (0.72 + 0.28 * joint) * (0.93 + 0.07 * edge) * (1 - 0.12 * pit),
61
+ const brightness = (158 + n * 62) * r.tone[k] * (0.72 + 0.28 * joint) * (0.93 + 0.07 * edge) * (1 - 0.12 * pit),
62
62
  wm = r.warm[k];
63
- o[0] = c * (1 + 0.03 * wm);
64
- o[1] = c * 0.985;
65
- o[2] = c * (0.95 - 0.04 * wm);
63
+ o[0] = brightness * (1 + 0.03 * wm);
64
+ o[1] = brightness * 0.985;
65
+ o[2] = brightness * (0.95 - 0.04 * wm);
66
66
  };
67
67
  }
68
68
 
@@ -88,11 +88,11 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
88
88
  const tone = 0.8 + 0.35 * id,
89
89
  stone = (150 + 60 * grain) * tone * (0.72 + 0.28 * Math.sqrt(rim)) * (1 - 0.12 * pit),
90
90
  mortar = 84 + 26 * tfbm(u, v, 14, 3, 100) + 14 * tfbm(u, v, 60, 2, 103), // lying back, flat and sandy
91
- c = mortar + (stone - mortar) * inStone,
91
+ brightness = mortar + (stone - mortar) * inStone,
92
92
  warm = (hash2(big[0] + 7, big[1], 101) - 0.5) * 0.08 * inStone; // ironstone, limestone and grey field stone
93
- o[0] = clamp(c * (1 + warm), 0, 255);
94
- o[1] = clamp(c * 0.985, 0, 255);
95
- o[2] = clamp(c * (0.95 - warm), 0, 255);
93
+ o[0] = clamp(brightness * (1 + warm), 0, 255);
94
+ o[1] = clamp(brightness * 0.985, 0, 255);
95
+ o[2] = clamp(brightness * (0.95 - warm), 0, 255);
96
96
  };
97
97
  }
98
98
 
@@ -100,10 +100,10 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
100
100
  const plaster = () => (u, v, o) => {
101
101
  const n = tfbm(u, v, 5, 5, 63),
102
102
  streak = tfbm(u * 3, v * 0.4, 4, 3, 64);
103
- const c = clamp(170 + n * 60 + streak * 24 - 20 * sstep(0.9, 1.0, (v * 6) % 1), 0, 255);
104
- o[0] = c;
105
- o[1] = c * 0.99;
106
- o[2] = c * 0.97;
103
+ const brightness = clamp(170 + n * 60 + streak * 24 - 20 * sstep(0.9, 1.0, (v * 6) % 1), 0, 255);
104
+ o[0] = brightness;
105
+ o[1] = brightness * 0.99;
106
+ o[2] = brightness * 0.97;
107
107
  };
108
108
 
109
109
  // Roof tiles in rows that break joint: each tile its own shade, its lower lip lit, a gap between neighbours.
@@ -116,20 +116,20 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
116
116
  const shade = 0.72 + 0.28 * hash2(Math.floor(u * cols + (r % 2) * 0.5) % cols, r, 65);
117
117
  const lip = 0.66 + 0.34 * sstep(0.0, 0.5, tv),
118
118
  gap = 0.75 + 0.25 * sstep(0, 0.12, tu) * sstep(1, 0.88, tu);
119
- const c = (150 + tfbm(u, v, 8, 3, 66) * 70) * shade * lip * gap;
120
- o[0] = c;
121
- o[1] = c * 0.98;
122
- o[2] = c * 0.96;
119
+ const brightness = (150 + tfbm(u, v, 8, 3, 66) * 70) * shade * lip * gap;
120
+ o[0] = brightness;
121
+ o[1] = brightness * 0.98;
122
+ o[2] = brightness * 0.96;
123
123
  };
124
124
 
125
125
  // Bare rock: mottled grey with dark cracks.
126
126
  const rock = () => (u, v, o) => {
127
127
  const n = tfbm(u, v, 4, 6, 67),
128
128
  crack = Math.abs(tfbm(u, v, 6, 4, 68) - 0.5) * 2;
129
- const c = clamp((130 + n * 110) * (0.55 + 0.45 * sstep(0.0, 0.12, crack)), 0, 255);
130
- o[0] = c;
131
- o[1] = c;
132
- o[2] = c * 0.98;
129
+ const brightness = clamp((130 + n * 110) * (0.55 + 0.45 * sstep(0.0, 0.12, crack)), 0, 255);
130
+ o[0] = brightness;
131
+ o[1] = brightness;
132
+ o[2] = brightness * 0.98;
133
133
  };
134
134
 
135
135
  // Planks side by side: grain along each, a dark edge between them, each plank its own tone.
@@ -139,18 +139,18 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
139
139
  const plank = Math.floor(u * count),
140
140
  g = tfbm(u * 0.6 + plank * 0.13, v, 4, 5, 69),
141
141
  edge = sstep(0, 0.06, (u * count) % 1);
142
- const c = (130 + g * 90) * (0.82 + 0.18 * hash2(plank, 0, 70)) * (0.6 + 0.4 * edge);
143
- o[0] = c * 1.05;
144
- o[1] = c * 0.95;
145
- o[2] = c * 0.8;
142
+ const brightness = (130 + g * 90) * (0.82 + 0.18 * hash2(plank, 0, 70)) * (0.6 + 0.4 * edge);
143
+ o[0] = brightness * 1.05;
144
+ o[1] = brightness * 0.95;
145
+ o[2] = brightness * 0.8;
146
146
  };
147
147
 
148
148
  // Leaf clusters, as one grey value: bright clumps, dark gaps.
149
149
  const foliage = () => (u, v, o) => {
150
- const a = tfbm(u, v, 10, 3, 71),
151
- b = tnoise(u * 34, v * 34, 34, 72),
152
- c = tnoise(u * 90, v * 90, 90, 73);
153
- const n = clamp(sstep(0.34, 0.62, a) * 0.62 + b * 0.3 + c * 0.16, 0, 1) * 255;
150
+ const coarseNoise = tfbm(u, v, 10, 3, 71),
151
+ mediumNoise = tnoise(u * 34, v * 34, 34, 72),
152
+ fineNoise = tnoise(u * 90, v * 90, 90, 73);
153
+ const n = clamp(sstep(0.34, 0.62, coarseNoise) * 0.62 + mediumNoise * 0.3 + fineNoise * 0.16, 0, 1) * 255;
154
154
  o[0] = o[1] = o[2] = n;
155
155
  };
156
156
 
@@ -161,10 +161,10 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
161
161
  (u, v, o) => {
162
162
  const n = tfbm(u, v, 6, 6, 3),
163
163
  g = tnoise(u * 160, v * 160, 160, 9);
164
- const c = clamp(168 + n * 72 + g * 30, 0, 255);
165
- o[0] = c;
166
- o[1] = c * 0.99;
167
- o[2] = c * 0.96;
164
+ const brightness = clamp(168 + n * 72 + g * 30, 0, 255);
165
+ o[0] = brightness;
166
+ o[1] = brightness * 0.99;
167
+ o[2] = brightness * 0.96;
168
168
  if (alpha) o[3] = 255 * alpha(u, v);
169
169
  };
170
170
 
@@ -178,9 +178,9 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
178
178
 
179
179
  // Sun-dried brick in mud mortar: courses that break joint, each brick its own tone, flecked with straw and worn at its edges.
180
180
  function brick({ seed = 733, courses: COURSES = 13, perCourse: BRICKS = 4 } = {}) {
181
- const R = mulberry32(seed),
182
- tone = Array.from({ length: COURSES * BRICKS * 2 }, () => 0.84 + R() * 0.3),
183
- shift = Array.from({ length: COURSES }, () => R() * 0.18);
181
+ const random = mulberry32(seed),
182
+ tone = Array.from({ length: COURSES * BRICKS * 2 }, () => 0.84 + random() * 0.3),
183
+ shift = Array.from({ length: COURSES }, () => random() * 0.18);
184
184
  return (u, v, o) => {
185
185
  const row = Math.min(COURSES - 1, Math.floor(v * COURSES)),
186
186
  bv = v * COURSES - row,
@@ -193,10 +193,10 @@ export function createSurfaceTextures({ noise: { tfbm, tnoise }, hash2 }) {
193
193
  joint = sstep(0.06, 0.2, edge),
194
194
  grain = tfbm(u, v, 24, 4, 742),
195
195
  straw = sstep(0.8, 0.86, tfbm(u * 3, v * 0.6, 48, 2, 743));
196
- const c = clamp((128 + grain * 70) * (0.74 + 0.26 * joint) * (joint > 0.5 ? tone[k] : 0.92) * (1 + straw * 0.12), 0, 255);
197
- o[0] = c * 1.02;
198
- o[1] = c * 0.97;
199
- o[2] = c * 0.9;
196
+ const brightness = clamp((128 + grain * 70) * (0.74 + 0.26 * joint) * (joint > 0.5 ? tone[k] : 0.92) * (1 + straw * 0.12), 0, 255);
197
+ o[0] = brightness * 1.02;
198
+ o[1] = brightness * 0.97;
199
+ o[2] = brightness * 0.9;
200
200
  };
201
201
  }
202
202