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,3 +1,8 @@
1
+ /**
2
+ * Render a heightfield in chunks and change each chunk’s mesh resolution as the camera moves.
3
+ * @module
4
+ */
5
+
1
6
  import * as THREE from 'three';
2
7
  import { SRGB2LIN } from '../math/color.js';
3
8
  import { clamp } from '../math/scalar.js';
@@ -20,59 +25,59 @@ function buildChunkGeometry(land, ci, cj, lod) {
20
25
  const step = 1 << lod,
21
26
  n = cells / step,
22
27
  m = n + 3;
23
- const pos = new Float32Array(m * m * 3),
24
- nor = new Float32Array(m * m * 3),
25
- col = new Float32Array(m * m * 3),
28
+ const positions = new Float32Array(m * m * 3),
29
+ normals = new Float32Array(m * m * 3),
30
+ chunkColors = new Float32Array(m * m * 3),
26
31
  uv = new Float32Array(m * m * 2);
27
32
  let p = 0;
28
- for (let b = 0; b < m; b++) {
29
- const jj = clamp(b - 1, 0, n),
30
- sb = b === 0 || b === m - 1;
31
- for (let a = 0; a < m; a++, p++) {
32
- const ii = clamp(a - 1, 0, n),
33
- skirt = sb || a === 0 || a === m - 1;
33
+ for (let row = 0; row < m; row++) {
34
+ const jj = clamp(row - 1, 0, n),
35
+ sb = row === 0 || row === m - 1;
36
+ for (let column = 0; column < m; column++, p++) {
37
+ const ii = clamp(column - 1, 0, n),
38
+ skirt = sb || column === 0 || column === m - 1;
34
39
  const gi = ci * cells + ii * step,
35
40
  gj = cj * cells + jj * step,
36
41
  k = gj * stride + gi;
37
42
  const x = x0 + gi * spacing,
38
43
  z = z0 + gj * spacing;
39
- pos[p * 3] = x;
40
- pos[p * 3 + 1] = heights[k] - (skirt ? 5 + 5 * lod : 0) - (sunkAt ? sunkAt(k) : 0);
41
- pos[p * 3 + 2] = z;
44
+ positions[p * 3] = x;
45
+ positions[p * 3 + 1] = heights[k] - (skirt ? 5 + 5 * lod : 0) - (sunkAt ? sunkAt(k) : 0);
46
+ positions[p * 3 + 2] = z;
42
47
  normalOf(land, k, _normal);
43
- nor[p * 3] = _normal[0];
44
- nor[p * 3 + 1] = _normal[1];
45
- nor[p * 3 + 2] = _normal[2];
46
- col[p * 3] = SRGB2LIN[colors[k * 3]];
47
- col[p * 3 + 1] = SRGB2LIN[colors[k * 3 + 1]];
48
- col[p * 3 + 2] = SRGB2LIN[colors[k * 3 + 2]];
48
+ normals[p * 3] = _normal[0];
49
+ normals[p * 3 + 1] = _normal[1];
50
+ normals[p * 3 + 2] = _normal[2];
51
+ chunkColors[p * 3] = SRGB2LIN[colors[k * 3]];
52
+ chunkColors[p * 3 + 1] = SRGB2LIN[colors[k * 3 + 1]];
53
+ chunkColors[p * 3 + 2] = SRGB2LIN[colors[k * 3 + 2]];
49
54
  uv[p * 2] = x / 9;
50
55
  uv[p * 2 + 1] = z / 9;
51
56
  }
52
57
  }
53
- return chunkGeometry(pos, nor, col, uv, m, bedY);
58
+ return chunkGeometry(positions, normals, chunkColors, uv, m, bedY);
54
59
  }
55
60
  // A chunk's geometry from its m x m points: two triangles to a cell, the deep lake bed's (below bedY) in a group of their own.
56
- function chunkGeometry(pos, nor, col, uv, m, bedY) {
61
+ function chunkGeometry(positions, normals, chunkColors, uv, m, bedY) {
57
62
  const Index = m * m > 65536 ? Uint32Array : Uint16Array;
58
- const idx = new Index((m - 1) * (m - 1) * 6);
63
+ const indices = new Index((m - 1) * (m - 1) * 6);
59
64
  let q = 0,
60
- e = idx.length; // land from the front, deep lake bed (group 1, the plain bed material) from the back
61
- const deep = (i, j, k) => pos[i * 3 + 1] < bedY && pos[j * 3 + 1] < bedY && pos[k * 3 + 1] < bedY;
65
+ e = indices.length; // land from the front, deep lake bed (group 1, the plain bed material) from the back
66
+ const deep = (i, j, k) => positions[i * 3 + 1] < bedY && positions[j * 3 + 1] < bedY && positions[k * 3 + 1] < bedY;
62
67
  const tri = (i, j, k) => {
63
68
  if (deep(i, j, k)) {
64
- idx[--e] = k;
65
- idx[--e] = j;
66
- idx[--e] = i;
69
+ indices[--e] = k;
70
+ indices[--e] = j;
71
+ indices[--e] = i;
67
72
  } else {
68
- idx[q++] = i;
69
- idx[q++] = j;
70
- idx[q++] = k;
73
+ indices[q++] = i;
74
+ indices[q++] = j;
75
+ indices[q++] = k;
71
76
  }
72
77
  };
73
- for (let b = 0; b < m - 1; b++)
74
- for (let a = 0; a < m - 1; a++) {
75
- const i00 = b * m + a,
78
+ for (let row = 0; row < m - 1; row++)
79
+ for (let column = 0; column < m - 1; column++) {
80
+ const i00 = row * m + column,
76
81
  i10 = i00 + 1,
77
82
  i01 = i00 + m,
78
83
  i11 = i01 + 1;
@@ -80,15 +85,15 @@ function chunkGeometry(pos, nor, col, uv, m, bedY) {
80
85
  tri(i10, i01, i11);
81
86
  }
82
87
  const g = new THREE.BufferGeometry();
83
- g.setAttribute('position', new THREE.BufferAttribute(pos, 3));
84
- g.setAttribute('normal', new THREE.BufferAttribute(nor, 3));
85
- g.setAttribute('color', new THREE.BufferAttribute(col, 3));
88
+ g.setAttribute('position', new THREE.BufferAttribute(positions, 3));
89
+ g.setAttribute('normal', new THREE.BufferAttribute(normals, 3));
90
+ g.setAttribute('color', new THREE.BufferAttribute(chunkColors, 3));
86
91
  g.setAttribute('uv', new THREE.BufferAttribute(uv, 2));
87
- g.setIndex(new THREE.BufferAttribute(idx, 1));
92
+ g.setIndex(new THREE.BufferAttribute(indices, 1));
88
93
  if (bedY !== undefined) {
89
94
  // a chunk with no deep bed (or nothing but bed) has one group: an empty one would still be a draw call
90
95
  if (q) g.addGroup(0, q, 0);
91
- if (q < idx.length) g.addGroup(q, idx.length - q, 1);
96
+ if (q < indices.length) g.addGroup(q, indices.length - q, 1);
92
97
  }
93
98
  g.computeBoundingSphere();
94
99
  g.computeBoundingBox();
@@ -110,10 +115,10 @@ function buildFineGeometry(land, ci, cj) {
110
115
  rel = new Float32Array(r * r),
111
116
  weights = [0, 0, 0, 0],
112
117
  corner = [0, 0, 0, 0];
113
- const place = (a, b) => {
114
- // the grid cell and the place in it of fine point (a, b) of this chunk, clamped to the land
115
- const fa = Math.min(Math.max(ci * cells * FINE + a, 0), last * FINE),
116
- fb = Math.min(Math.max(cj * cells * FINE + b, 0), last * FINE),
118
+ const place = (column, row) => {
119
+ // the grid cell and the place in it of fine point (column, row) of this chunk, clamped to the land
120
+ const fa = Math.min(Math.max(ci * cells * FINE + column, 0), last * FINE),
121
+ fb = Math.min(Math.max(cj * cells * FINE + row, 0), last * FINE),
117
122
  i = Math.min((fa / FINE) | 0, last - 1),
118
123
  j = Math.min((fb / FINE) | 0, last - 1),
119
124
  u = fa / FINE - i,
@@ -134,32 +139,32 @@ function buildFineGeometry(land, ci, cj) {
134
139
  (1 - u) * (heights[k + stride] - heights[k + stride + 1]) +
135
140
  (1 - v) * (heights[k + 1] - heights[k + stride + 1]);
136
141
  };
137
- for (let b = 0; b < r; b++)
138
- for (let a = 0; a < r; a++) {
139
- const h = place(a - 1, b - 1),
140
- x = x0 + (ci * cells * FINE + a - 1) * step,
141
- z = z0 + (cj * cells * FINE + b - 1) * step;
142
- base[b * r + a] = h;
143
- rel[b * r + a] = relief(x, z, h);
142
+ for (let row = 0; row < r; row++)
143
+ for (let column = 0; column < r; column++) {
144
+ const h = place(column - 1, row - 1),
145
+ x = x0 + (ci * cells * FINE + column - 1) * step,
146
+ z = z0 + (cj * cells * FINE + row - 1) * step;
147
+ base[row * r + column] = h;
148
+ rel[row * r + column] = relief(x, z, h);
144
149
  }
145
- const pos = new Float32Array(m * m * 3),
146
- nor = new Float32Array(m * m * 3),
147
- col = new Float32Array(m * m * 3),
150
+ const positions = new Float32Array(m * m * 3),
151
+ normals = new Float32Array(m * m * 3),
152
+ chunkColors = new Float32Array(m * m * 3),
148
153
  uv = new Float32Array(m * m * 2);
149
154
  let p = 0;
150
- for (let b = 0; b < m; b++) {
151
- const jj = clamp(b - 1, 0, n),
152
- sb = b === 0 || b === m - 1;
153
- for (let a = 0; a < m; a++, p++) {
154
- const ii = clamp(a - 1, 0, n),
155
- skirt = sb || a === 0 || a === m - 1,
155
+ for (let row = 0; row < m; row++) {
156
+ const jj = clamp(row - 1, 0, n),
157
+ sb = row === 0 || row === m - 1;
158
+ for (let column = 0; column < m; column++, p++) {
159
+ const ii = clamp(column - 1, 0, n),
160
+ skirt = sb || column === 0 || column === m - 1,
156
161
  q = (jj + 1) * r + ii + 1,
157
162
  x = x0 + (ci * cells * FINE + ii) * step,
158
163
  z = z0 + (cj * cells * FINE + jj) * step;
159
164
  place(ii, jj);
160
- pos[p * 3] = x;
161
- pos[p * 3 + 1] = base[q] + rel[q] - (skirt ? 5 : 0);
162
- pos[p * 3 + 2] = z;
165
+ positions[p * 3] = x;
166
+ positions[p * 3 + 1] = base[q] + rel[q] - (skirt ? 5 : 0);
167
+ positions[p * 3 + 2] = z;
163
168
  // the grid's normal between its points, as the slope of a height field, plus the relief's slope
164
169
  let nx = 0,
165
170
  ny = 0,
@@ -179,17 +184,17 @@ function buildFineGeometry(land, ci, cj) {
179
184
  const gx = -nx / ny + (rel[q + 1] - rel[q - 1]) / (2 * step),
180
185
  gz = -nz / ny + (rel[q + r] - rel[q - r]) / (2 * step),
181
186
  len = Math.hypot(gx, 1, gz);
182
- nor[p * 3] = -gx / len;
183
- nor[p * 3 + 1] = 1 / len;
184
- nor[p * 3 + 2] = -gz / len;
185
- col[p * 3] = cr;
186
- col[p * 3 + 1] = cg;
187
- col[p * 3 + 2] = cb;
187
+ normals[p * 3] = -gx / len;
188
+ normals[p * 3 + 1] = 1 / len;
189
+ normals[p * 3 + 2] = -gz / len;
190
+ chunkColors[p * 3] = cr;
191
+ chunkColors[p * 3 + 1] = cg;
192
+ chunkColors[p * 3 + 2] = cb;
188
193
  uv[p * 2] = x / 9;
189
194
  uv[p * 2 + 1] = z / 9;
190
195
  }
191
196
  }
192
- return chunkGeometry(pos, nor, col, uv, m, bedY);
197
+ return chunkGeometry(positions, normals, chunkColors, uv, m, bedY);
193
198
  }
194
199
  // Builds the ground at its coarsest or, with initialPosition {x, y, z}, at the levels seen from that position.
195
200
  // The returned updater follows camera.position as the viewer moves. `materials` is the pair
@@ -246,8 +251,8 @@ export function terrainChunks(parent, land, materials, {
246
251
  if (!initialPosition) show(chunk, 3);
247
252
  chunks.push(chunk);
248
253
  }
249
- const boxOf = c => [c.cx - width / 2, c.cz - width / 2, c.cx + width / 2, c.cz + width / 2],
250
- overlaps = (a, b) => a[0] < b[2] && b[0] < a[2] && a[1] < b[3] && b[1] < a[3],
254
+ const boxOf = chunk => [chunk.cx - width / 2, chunk.cz - width / 2, chunk.cx + width / 2, chunk.cz + width / 2],
255
+ overlaps = (firstBounds, secondBounds) => firstBounds[0] < secondBounds[2] && secondBounds[0] < firstBounds[2] && firstBounds[1] < secondBounds[3] && secondBounds[1] < firstBounds[3],
251
256
  nearBox = (box, range, position) =>
252
257
  Math.hypot(
253
258
  Math.max(box[0] - position.x, 0, position.x - box[2]),
@@ -256,14 +261,14 @@ export function terrainChunks(parent, land, materials, {
256
261
  range * lodScale();
257
262
  // the chunks under ground held at a level, found when it is first seen (the list may fill later)
258
263
  const holdUnder = h => {
259
- h.chunks = chunks.filter(c => overlaps(boxOf(c), h.box));
264
+ h.chunks = chunks.filter(chunk => overlaps(boxOf(chunk), h.box));
260
265
  h.fine = false;
261
- for (const c of h.chunks) (c.holds ??= []).push(h);
266
+ for (const chunk of h.chunks) (chunk.holds ??= []).push(h);
262
267
  };
263
268
  // the level a chunk wants, `want` by its distance, or finer where ground over it is held within range
264
- const heldLevel = (c, want, position) => {
265
- if (c.holds)
266
- for (let i = 0; i < c.holds.length; i++) if (nearBox(c.holds[i].box, c.holds[i].range, position)) want = Math.min(want, c.holds[i].level);
269
+ const heldLevel = (chunk, want, position) => {
270
+ if (chunk.holds)
271
+ for (let i = 0; i < chunk.holds.length; i++) if (nearBox(chunk.holds[i].box, chunk.holds[i].range, position)) want = Math.min(want, chunk.holds[i].level);
267
272
  return want;
268
273
  };
269
274
  // whether every chunk under held ground stands at its level or finer
@@ -279,21 +284,21 @@ export function terrainChunks(parent, land, materials, {
279
284
  let built = false;
280
285
  const count = force ? chunks.length : checksPerFrame;
281
286
  for (let n = 0; n < count; n++) {
282
- const c = chunks[chunkCursor];
287
+ const chunk = chunks[chunkCursor];
283
288
  chunkCursor = (chunkCursor + 1) % chunks.length;
284
- const dx = Math.max(Math.abs(position.x - c.cx) - width * 0.5, 0),
285
- dz = Math.max(Math.abs(position.z - c.cz) - width * 0.5, 0);
286
- const d = Math.hypot(dx, dz, Math.max(0, position.y - altitudeStart) * altitudeWeight);
289
+ const dx = Math.max(Math.abs(position.x - chunk.cx) - width * 0.5, 0),
290
+ dz = Math.max(Math.abs(position.z - chunk.cz) - width * 0.5, 0);
291
+ const viewDistance = Math.hypot(dx, dz, Math.max(0, position.y - altitudeStart) * altitudeWeight);
287
292
  const fine = low && Math.hypot(dx, dz) < fineRange;
288
293
  const want = fine
289
294
  ? -1
290
295
  : heldLevel(
291
- c,
292
- d < distances[0] * lodScale() ? 0 : d < distances[1] * lodScale() ? 1 : d < distances[2] * lodScale() ? 2 : 3,
296
+ chunk,
297
+ viewDistance < distances[0] * lodScale() ? 0 : viewDistance < distances[1] * lodScale() ? 1 : viewDistance < distances[2] * lodScale() ? 2 : 3,
293
298
  position,
294
299
  );
295
- if (want === c.lod || (built && !force)) continue;
296
- show(c, want);
300
+ if (want === chunk.lod || (built && !force)) continue;
301
+ show(chunk, want);
297
302
  built = true;
298
303
  }
299
304
  for (let i = 0; i < held.length; i++) held[i].fine = nearBox(held[i].box, held[i].range, position) && standsAt(held[i]);
@@ -15,22 +15,22 @@ export function terrainExtension({ centerX = 0, centerZ = 0, size, segments, rea
15
15
  const boundary = new Uint32Array(perimeter);
16
16
  for (let ring = 0; ring <= rows; ring++) {
17
17
  const radius = half * growth ** ring;
18
- for (let t = 0; t < perimeter; t++) {
19
- const side = Math.floor(t / segments);
20
- const along = t % segments;
18
+ for (let perimeterIndex = 0; perimeterIndex < perimeter; perimeterIndex++) {
19
+ const side = Math.floor(perimeterIndex / segments);
20
+ const along = perimeterIndex % segments;
21
21
  let col = along, row = 0;
22
22
  switch (side) {
23
23
  case 1: col = segments; row = along; break;
24
24
  case 2: col = segments - along; row = segments; break;
25
25
  case 3: col = 0; row = segments - along; break;
26
26
  }
27
- if (ring === 0) boundary[t] = row * (segments + 1) + col;
28
- const vertex = ring * perimeter + t;
27
+ if (ring === 0) boundary[perimeterIndex] = row * (segments + 1) + col;
28
+ const vertex = ring * perimeter + perimeterIndex;
29
29
  const axis = -radius + 2 * radius * along / segments;
30
30
  positions[3 * vertex] = centerX + (side === 0 ? axis : side === 1 ? radius : side === 2 ? -axis : -radius);
31
31
  positions[3 * vertex + 2] = centerZ + (side === 0 ? -radius : side === 1 ? axis : side === 2 ? radius : -axis);
32
32
  if (ring === rows) continue;
33
- const next = ring * perimeter + (t + 1) % perimeter;
33
+ const next = ring * perimeter + (perimeterIndex + 1) % perimeter;
34
34
  const offset = 6 * vertex;
35
35
  indices[offset] = vertex;
36
36
  indices[offset + 1] = next;
package/index.js CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Core entry points for building geometry, rendering a world, moving through it and dispatching worker jobs. Import these names directly from `roamwright`.
3
+ * @module
4
+ */
5
+
1
6
  export { createRenderContext } from './rendering/context.js';
2
7
  export { createNoise } from './math/noise.js';
3
8
  export { mulberry32 } from './math/random.js';
package/math/cell-edge.js CHANGED
@@ -1,8 +1,8 @@
1
- // Distance (in texture units) from (u, v) to the nearest edge of a tileable pattern of `per` x `per` jittered cells; `site`, when
1
+ // Distance (in texture units) from (u, v) to the nearest edge of a tileable pattern of `period` jittered cells per side; `site`, when
2
2
  // given, is told which cell (u, v) lies in, wrapped to the tile.
3
- export function cellEdge(hash2, u, v, per, seed, site) {
4
- const gx = u * per,
5
- gz = v * per,
3
+ export function cellEdge(hash2, u, v, period, seed, site) {
4
+ const gx = u * period,
5
+ gz = v * period,
6
6
  ix = Math.floor(gx),
7
7
  iz = Math.floor(gz);
8
8
  let d1 = 9,
@@ -11,16 +11,16 @@ export function cellEdge(hash2, u, v, per, seed, site) {
11
11
  az = 0,
12
12
  bx = 0,
13
13
  bz = 0;
14
- for (let b = -1; b <= 1; b++)
15
- for (let a = -1; a <= 1; a++) {
16
- const cx = ix + a,
17
- cz = iz + b,
18
- wx = ((cx % per) + per) % per,
19
- wz = ((cz % per) + per) % per;
14
+ for (let offsetZ = -1; offsetZ <= 1; offsetZ++)
15
+ for (let offsetX = -1; offsetX <= 1; offsetX++) {
16
+ const cx = ix + offsetX,
17
+ cz = iz + offsetZ,
18
+ wx = ((cx % period) + period) % period,
19
+ wz = ((cz % period) + period) % period;
20
20
  const sx = cx + 0.15 + 0.7 * hash2(wx, wz, seed),
21
21
  sz = cz + 0.15 + 0.7 * hash2(wx, wz, seed + 1),
22
- d = (sx - gx) ** 2 + (sz - gz) ** 2;
23
- if (d < d1) {
22
+ distanceSquared = (sx - gx) ** 2 + (sz - gz) ** 2;
23
+ if (distanceSquared < d1) {
24
24
  if (site) {
25
25
  site[0] = wx;
26
26
  site[1] = wz;
@@ -28,11 +28,11 @@ export function cellEdge(hash2, u, v, per, seed, site) {
28
28
  d2 = d1;
29
29
  bx = ax;
30
30
  bz = az;
31
- d1 = d;
31
+ d1 = distanceSquared;
32
32
  ax = sx;
33
33
  az = sz;
34
- } else if (d < d2) {
35
- d2 = d;
34
+ } else if (distanceSquared < d2) {
35
+ d2 = distanceSquared;
36
36
  bx = sx;
37
37
  bz = sz;
38
38
  }
@@ -40,5 +40,5 @@ export function cellEdge(hash2, u, v, per, seed, site) {
40
40
  const ex = bx - ax,
41
41
  ez = bz - az,
42
42
  l = Math.hypot(ex, ez) || 1;
43
- return (((ax + bx) * 0.5 - gx) * ex + ((az + bz) * 0.5 - gz) * ez) / l / per;
43
+ return (((ax + bx) * 0.5 - gx) * ex + ((az + bz) * 0.5 - gz) * ez) / l / period;
44
44
  }
@@ -0,0 +1,6 @@
1
+ // Keep common cells as small numeric keys. A row outside the packed range uses
2
+ // a separate key so two distant places can never share a collider bucket.
3
+ export function cellKey(column, row) {
4
+ if (row >= -2048 && row < 2048 && Math.abs(column) < 2 ** 39) return (column + 2048) * 4096 + row + 2048;
5
+ return column + ',' + row;
6
+ }
@@ -0,0 +1,19 @@
1
+ // Largest radii that leave a fraction of each centre-to-centre distance clear.
2
+ // Positions are in the x-z plane. Calculate once when laying out a group.
3
+ // A lone centre has unlimited clearance. Coincident centres have none.
4
+ export function circleClearances(centres, gapFraction = 0) {
5
+ if (!(gapFraction >= 0 && gapFraction < 1)) throw new RangeError('gapFraction must be in [0, 1)');
6
+ const nearestSq = new Float64Array(centres.length).fill(Infinity);
7
+ for (let i = 0; i < centres.length; i++) {
8
+ const centre = centres[i];
9
+ for (let j = i + 1; j < centres.length; j++) {
10
+ const neighbour = centres[j];
11
+ const distanceSq = (centre.x - neighbour.x) ** 2 + (centre.z - neighbour.z) ** 2;
12
+ nearestSq[i] = Math.min(nearestSq[i], distanceSq);
13
+ nearestSq[j] = Math.min(nearestSq[j], distanceSq);
14
+ }
15
+ }
16
+ const share = (1 - gapFraction) / 2;
17
+ for (let i = 0; i < nearestSq.length; i++) nearestSq[i] = Math.sqrt(nearestSq[i]) * share;
18
+ return nearestSq;
19
+ }
@@ -9,12 +9,12 @@ const FOLD_SLOPES = [110, 30];
9
9
  const PIPES = { n: 46, half: [0.009, 0.024], wander: [0.005, 0.016], tall: [0.45, 1], lasts: [0.07, 0.2], foot: 2.3, flank: 0.5 },
10
10
  CRUMPLE = { per: 6, octaves: 2, seed: 83, full: 1.6 }, // the crumple's noise: cells a tile, and how far from its middle value it is at its full height
11
11
  STEP = 1 / 512; // how far apart a field is read to find its slope (tiles)
12
- const around = d => d - Math.round(d), // the nearest way round a tile
12
+ const around = offset => offset - Math.round(offset), // the nearest way round a tile
13
13
  byte = slope => clamp(127.5 + 127.5 * slope, 0, 255);
14
14
  // The long folds' fill: a pixel's bytes from its place in the tile.
15
15
  function garmentFolds(foldSeed) {
16
16
  const rand = mulberry32(foldSeed),
17
- between = ([a, b]) => a + (b - a) * rand(),
17
+ between = ([minimum, maximum]) => minimum + (maximum - minimum) * rand(),
18
18
  pipes = [];
19
19
  for (let k = 0; k < PIPES.n; k++)
20
20
  pipes.push({
@@ -65,10 +65,10 @@ const cloth = (threads, hash2, tfbm) => (u, v, o) => {
65
65
  thread = Math.sin(Math.PI * along), // round across the thread on top, darker where it dips under
66
66
  tone = over ? hash2(i, 0, 69) : hash2(j, 1, 69), // one thread spun a little thicker or paler than the next
67
67
  wear = tfbm(u, v, 4, 4, 70);
68
- const c = clamp(158 + wear * 56 + (thread - 0.64) * 20 + (tone - 0.5) * 12, 0, 255);
69
- o[0] = c;
70
- o[1] = c * 0.99;
71
- o[2] = c * 0.975;
68
+ const brightness = clamp(158 + wear * 56 + (thread - 0.64) * 20 + (tone - 0.5) * 12, 0, 255);
69
+ o[0] = brightness;
70
+ o[1] = brightness * 0.99;
71
+ o[2] = brightness * 0.975;
72
72
  };
73
73
 
74
74
  function createClothTextures({ seed = 0, foldSeed = seed, threads = 64 } = {}) {
@@ -17,26 +17,26 @@ export function createCloudTextures({
17
17
  ix = Math.floor(gx),
18
18
  iz = Math.floor(gz);
19
19
  let near = 9;
20
- for (let b = -1; b <= 1; b++)
21
- for (let a = -1; a <= 1; a++) {
22
- const cx = ix + a,
23
- cz = iz + b,
20
+ for (let offsetZ = -1; offsetZ <= 1; offsetZ++)
21
+ for (let offsetX = -1; offsetX <= 1; offsetX++) {
22
+ const cx = ix + offsetX,
23
+ cz = iz + offsetZ,
24
24
  wx = ((cx % per) + per) % per,
25
25
  wz = ((cz % per) + per) % per,
26
- d = (cx + 0.15 + 0.7 * hash2(wx, wz, seed) - gx) ** 2 + (cz + 0.15 + 0.7 * hash2(wx, wz, seed + 1) - gz) ** 2;
27
- if (d < near) near = d;
26
+ distanceSquared = (cx + 0.15 + 0.7 * hash2(wx, wz, seed) - gx) ** 2 + (cz + 0.15 + 0.7 * hash2(wx, wz, seed + 1) - gz) ** 2;
27
+ if (distanceSquared < near) near = distanceSquared;
28
28
  }
29
29
  return Math.max(0, 1 - near * 2.2);
30
30
  }
31
31
  // Rounded lumps with creases between them, as a cloud's top is made of: domes at three sizes, from `per` to a sheet.
32
32
  function lumps(u, v, per, seed) {
33
33
  let s = 0,
34
- a = 0.5,
34
+ amplitude = 0.5,
35
35
  n = 0;
36
36
  for (let i = 0; i < 3; i++) {
37
- s += a * domes(u, v, per, seed + i * 13);
38
- n += a;
39
- a *= 0.5;
37
+ s += amplitude * domes(u, v, per, seed + i * 13);
38
+ n += amplitude;
39
+ amplitude *= 0.5;
40
40
  per *= 2;
41
41
  }
42
42
  return s / n;
@@ -44,11 +44,11 @@ export function createCloudTextures({
44
44
  // Streaks four times as long as they are wide, at three scales.
45
45
  function wisps(u, v) {
46
46
  let s = 0,
47
- a = 0.5,
47
+ amplitude = 0.5,
48
48
  n = 0;
49
- for (let i = 0, per = 3; i < 3; i++, per *= 2, a *= 0.5) {
50
- s += a * tnoise(u * per, v * per * 4, per, 1259 + i * 13, per * 4);
51
- n += a;
49
+ for (let i = 0, per = 3; i < 3; i++, per *= 2, amplitude *= 0.5) {
50
+ s += amplitude * tnoise(u * per, v * per * 4, per, 1259 + i * 13, per * 4);
51
+ n += amplitude;
52
52
  }
53
53
  return s / n;
54
54
  }
@@ -88,12 +88,12 @@ export function createCloudTextures({
88
88
  iy = Math.floor(y),
89
89
  iz = Math.floor(z);
90
90
  let near = 9;
91
- for (let c = -1; c <= 1; c++)
92
- for (let b = -1; b <= 1; b++)
93
- for (let a = -1; a <= 1; a++) {
94
- const k = ((wrap(iz + c) * per + wrap(iy + b)) * per + wrap(ix + a)) * 3,
95
- d = (ix + a + middles[k] - x) ** 2 + (iy + b + middles[k + 1] - y) ** 2 + (iz + c + middles[k + 2] - z) ** 2;
96
- if (d < near) near = d;
91
+ for (let offsetZ = -1; offsetZ <= 1; offsetZ++)
92
+ for (let offsetY = -1; offsetY <= 1; offsetY++)
93
+ for (let offsetX = -1; offsetX <= 1; offsetX++) {
94
+ const k = ((wrap(iz + offsetZ) * per + wrap(iy + offsetY)) * per + wrap(ix + offsetX)) * 3,
95
+ distanceSquared = (ix + offsetX + middles[k] - x) ** 2 + (iy + offsetY + middles[k + 1] - y) ** 2 + (iz + offsetZ + middles[k + 2] - z) ** 2;
96
+ if (distanceSquared < near) near = distanceSquared;
97
97
  }
98
98
  return Math.max(0, 1 - near * 1.7);
99
99
  };
package/math/color.js CHANGED
@@ -1,29 +1,29 @@
1
1
  import { clamp } from './scalar.js';
2
2
 
3
- const dim = (c, k) => [c[0] * k, c[1] * k, c[2] * k];
3
+ const dim = (color, scale) => [color[0] * scale, color[1] * scale, color[2] * scale];
4
4
 
5
5
  const SRGB2LIN = new Float32Array(256);
6
6
  for (let i = 0; i < 256; i++) {
7
- const c = i / 255;
8
- SRGB2LIN[i] = c < 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
7
+ const channel = i / 255;
8
+ SRGB2LIN[i] = channel < 0.04045 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4);
9
9
  }
10
10
  // Three.js ACES filmic fit (see licenses/three.txt and licenses/baking-lab.txt).
11
11
  // Mutates a linear RGB color so fog applied after tone mapping can match the sky.
12
- function acesToColor(c, exposure) {
13
- const r = (c.r * exposure) / 0.6,
14
- g = (c.g * exposure) / 0.6,
15
- b = (c.b * exposure) / 0.6;
16
- let r2 = 0.59719 * r + 0.35458 * g + 0.04823 * b,
17
- g2 = 0.076 * r + 0.90834 * g + 0.01566 * b,
18
- b2 = 0.0284 * r + 0.13383 * g + 0.83777 * b;
12
+ function acesToColor(color, exposure) {
13
+ const red = (color.r * exposure) / 0.6,
14
+ green = (color.g * exposure) / 0.6,
15
+ blue = (color.b * exposure) / 0.6;
16
+ let r2 = 0.59719 * red + 0.35458 * green + 0.04823 * blue,
17
+ g2 = 0.076 * red + 0.90834 * green + 0.01566 * blue,
18
+ b2 = 0.0284 * red + 0.13383 * green + 0.83777 * blue;
19
19
  const f = v => (v * (v + 0.0245786) - 0.000090537) / (v * (0.983729 * v + 0.432951) + 0.238081);
20
20
  r2 = f(r2);
21
21
  g2 = f(g2);
22
22
  b2 = f(b2);
23
- c.r = clamp(1.60475 * r2 - 0.53108 * g2 - 0.07367 * b2, 0, 1);
24
- c.g = clamp(-0.10208 * r2 + 1.10813 * g2 - 0.00605 * b2, 0, 1);
25
- c.b = clamp(-0.00327 * r2 - 0.07276 * g2 + 1.07602 * b2, 0, 1);
26
- return c;
23
+ color.r = clamp(1.60475 * r2 - 0.53108 * g2 - 0.07367 * b2, 0, 1);
24
+ color.g = clamp(-0.10208 * r2 + 1.10813 * g2 - 0.00605 * b2, 0, 1);
25
+ color.b = clamp(-0.00327 * r2 - 0.07276 * g2 + 1.07602 * b2, 0, 1);
26
+ return color;
27
27
  }
28
28
 
29
29
  export { acesToColor, dim, SRGB2LIN };
package/math/curve.js CHANGED
@@ -17,12 +17,12 @@ function cubic(x0, x1, x2, x3, dt0, dt1, dt2, t) {
17
17
  tt = t * t;
18
18
  return x1 + t1 * t + c2 * tt + c3 * (tt * t);
19
19
  }
20
- const knot = (a, b) => Math.pow((a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]), 0.25);
20
+ const knot = (startPoint, endPoint) => Math.pow((startPoint[0] - endPoint[0]) * (startPoint[0] - endPoint[0]) + (startPoint[1] - endPoint[1]) * (startPoint[1] - endPoint[1]), 0.25);
21
21
  const _before = [0, 0],
22
22
  _after = [0, 0];
23
- // The point of the curve through pts at t in 0..1, where t runs evenly from point to point.
24
- function curvePoint(pts, t) {
25
- const count = pts.length,
23
+ // The point of the curve through the control points at t in 0..1, where t runs evenly from point to point.
24
+ function curvePoint(controlPoints, t) {
25
+ const count = controlPoints.length,
26
26
  at = (count - 1) * t;
27
27
  let i = Math.floor(at),
28
28
  weight = at - i;
@@ -31,17 +31,17 @@ function curvePoint(pts, t) {
31
31
  weight = 1;
32
32
  }
33
33
  // past either end the line is carried on by its last step
34
- const first = pts[0],
35
- last = pts[count - 1];
36
- _after[0] = last[0] - pts[count - 2][0] + last[0];
37
- _after[1] = last[1] - pts[count - 2][1] + last[1];
38
- _before[0] = first[0] - pts[1][0] + first[0];
39
- _before[1] = first[1] - pts[1][1] + first[1];
34
+ const first = controlPoints[0],
35
+ last = controlPoints[count - 1];
36
+ _after[0] = last[0] - controlPoints[count - 2][0] + last[0];
37
+ _after[1] = last[1] - controlPoints[count - 2][1] + last[1];
38
+ _before[0] = first[0] - controlPoints[1][0] + first[0];
39
+ _before[1] = first[1] - controlPoints[1][1] + first[1];
40
40
  const atEnd = i + 2 >= count,
41
- p0 = i > 0 ? pts[i - 1] : atEnd ? _after : _before,
42
- p1 = pts[i],
43
- p2 = pts[i + 1],
44
- p3 = atEnd ? _after : pts[i + 2];
41
+ p0 = i > 0 ? controlPoints[i - 1] : atEnd ? _after : _before,
42
+ p1 = controlPoints[i],
43
+ p2 = controlPoints[i + 1],
44
+ p3 = atEnd ? _after : controlPoints[i + 2];
45
45
  let dt0 = knot(p0, p1),
46
46
  dt1 = knot(p1, p2),
47
47
  dt2 = knot(p2, p3);
@@ -51,12 +51,12 @@ function curvePoint(pts, t) {
51
51
  return [cubic(p0[0], p1[0], p2[0], p3[0], dt0, dt1, dt2, weight), cubic(p0[1], p1[1], p2[1], p3[1], dt0, dt1, dt2, weight)];
52
52
  }
53
53
  // The curve's length from its start to each of ARC_STEPS + 1 points along it.
54
- function arcLengths(pts) {
54
+ function arcLengths(controlPoints) {
55
55
  const lengths = [0];
56
- let last = curvePoint(pts, 0),
56
+ let last = curvePoint(controlPoints, 0),
57
57
  sum = 0;
58
- for (let p = 1; p <= ARC_STEPS; p++) {
59
- const point = curvePoint(pts, p / ARC_STEPS);
58
+ for (let stepIndex = 1; stepIndex <= ARC_STEPS; stepIndex++) {
59
+ const point = curvePoint(controlPoints, stepIndex / ARC_STEPS);
60
60
  sum += Math.sqrt((point[0] - last[0]) * (point[0] - last[0]) + (point[1] - last[1]) * (point[1] - last[1]));
61
61
  lengths.push(sum);
62
62
  last = point;
@@ -81,12 +81,12 @@ function shareToT(lengths, u) {
81
81
  if (lengths[high] === target) return high / ARC_STEPS;
82
82
  return (high + (target - lengths[high]) / (lengths[high + 1] - lengths[high])) / ARC_STEPS;
83
83
  }
84
- // The line through pts as a smooth curve of evenly spaced points, about `spacing` apart.
85
- function spacedCurve(pts, spacing) {
86
- const lengths = arcLengths(pts),
84
+ // The line through the control points as a smooth curve of evenly spaced points, about `spacing` apart.
85
+ function spacedCurve(controlPoints, spacing) {
86
+ const lengths = arcLengths(controlPoints),
87
87
  steps = Math.max(2, Math.round(lengths[ARC_STEPS] / spacing)),
88
88
  points = [];
89
- for (let d = 0; d <= steps; d++) points.push(curvePoint(pts, shareToT(lengths, d / steps)));
89
+ for (let stepIndex = 0; stepIndex <= steps; stepIndex++) points.push(curvePoint(controlPoints, shareToT(lengths, stepIndex / steps)));
90
90
  return points;
91
91
  }
92
92