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
@@ -1,3 +1,8 @@
1
+ /**
2
+ * Build static or skinned triangle geometry from coloured parts. Workers can produce attribute arrays for a renderer on the main thread.
3
+ * @module
4
+ */
5
+
1
6
  import * as THREE from 'three';
2
7
  import { markStill } from '../rendering/still-casters.js';
3
8
  import { box, TRS } from './primitives.js';
@@ -7,7 +12,7 @@ import { box, TRS } from './primitives.js';
7
12
  // (geometryOf), and the primitives it places
8
13
  // ════════════════════════════════════════════════════════════════════════
9
14
  const _n3 = new THREE.Matrix3(),
10
- _v = new THREE.Vector3(),
15
+ scratchVertex = new THREE.Vector3(),
11
16
  _m5 = new THREE.Matrix4();
12
17
  // Three.js calls this upload callback with the attribute as its receiver. Static
13
18
  // geometry can release its CPU copy once the GPU owns the buffer.
@@ -49,8 +54,8 @@ function sphereOf(points) {
49
54
  const dx = points[i] - cx,
50
55
  dy = points[i + 1] - cy,
51
56
  dz = points[i + 2] - cz,
52
- d = dx * dx + dy * dy + dz * dz;
53
- if (d > far) far = d;
57
+ distanceSquared = dx * dx + dy * dy + dz * dz;
58
+ if (distanceSquared > far) far = distanceSquared;
54
59
  }
55
60
  return [cx, cy, cz, Math.sqrt(far)];
56
61
  }
@@ -59,38 +64,38 @@ function sphereOf(points) {
59
64
  // `sphere`: its bounding sphere as [x, y, z, radius], where it was worked out already, so the points are
60
65
  // not read again.
61
66
  function geometryOf(parts, sphere = null) {
62
- const g = new THREE.BufferGeometry(),
67
+ const geometry = new THREE.BufferGeometry(),
63
68
  vertices = parts.position.length / 3;
64
69
  for (const name in ATTRIBUTES) {
65
70
  const array = parts[name],
66
71
  size = vertices ? array?.length / vertices : ATTRIBUTES[name];
67
- if (array) g.setAttribute(name, new THREE.BufferAttribute(array, size, name !== 'skinIndex' && !(array instanceof Float32Array)));
72
+ if (array) geometry.setAttribute(name, new THREE.BufferAttribute(array, size, name !== 'skinIndex' && !(array instanceof Float32Array)));
68
73
  }
69
- if (parts.index) g.setIndex(new THREE.BufferAttribute(parts.index, 1).onUpload(releaseArray));
74
+ if (parts.index) geometry.setIndex(new THREE.BufferAttribute(parts.index, 1).onUpload(releaseArray));
70
75
  const points = parts.position;
71
76
  if (!sphere && points instanceof Float32Array && points.length) sphere = sphereOf(points);
72
- if (sphere) g.boundingSphere = new THREE.Sphere(new THREE.Vector3(sphere[0], sphere[1], sphere[2]), sphere[3]);
73
- else g.computeBoundingSphere();
77
+ if (sphere) geometry.boundingSphere = new THREE.Sphere(new THREE.Vector3(sphere[0], sphere[1], sphere[2]), sphere[3]);
78
+ else geometry.computeBoundingSphere();
74
79
  // Skinned meshes compute bounds on the CPU. Keep their positions, joint indices
75
80
  // and weights available, including when several meshes share the geometry.
76
- for (const name in g.attributes) if (!(parts.skinIndex && SKIN_BOUNDS.includes(name))) g.attributes[name].onUpload(releaseArray);
77
- return g;
81
+ for (const name in geometry.attributes) if (!(parts.skinIndex && SKIN_BOUNDS.includes(name))) geometry.attributes[name].onUpload(releaseArray);
82
+ return geometry;
78
83
  }
79
84
  // A mesh of a builder's arrays (GB.parts), as GB.mesh makes one, in `parent`
80
85
  function meshOf(parts, material, parent, shadow = true) {
81
- const m = stillMesh(geometryOf(parts), material, shadow);
82
- parent.add(m);
83
- return m;
86
+ const mesh = stillMesh(geometryOf(parts), material, shadow);
87
+ parent.add(mesh);
88
+ return mesh;
84
89
  }
85
90
  // A static mesh with its local transform fixed and optional shadow casting.
86
91
  function stillMesh(geometry, material, shadow) {
87
- const m = new THREE.Mesh(geometry, material);
88
- m.castShadow = shadow;
89
- m.receiveShadow = true;
90
- m.matrixAutoUpdate = false;
91
- m.updateMatrix();
92
- if (shadow) markStill(m);
93
- return m;
92
+ const mesh = new THREE.Mesh(geometry, material);
93
+ mesh.castShadow = shadow;
94
+ mesh.receiveShadow = true;
95
+ mesh.matrixAutoUpdate = false;
96
+ mesh.updateMatrix();
97
+ if (shadow) markStill(mesh);
98
+ return mesh;
94
99
  }
95
100
  // Share GPU buffers and bounds while giving each instanced mesh a geometry identity
96
101
  // of its own. Three.js can then reuse the vertex bindings for each mesh.
@@ -114,22 +119,26 @@ function withRoom(array, room, used) {
114
119
  return roomier;
115
120
  }
116
121
  // A normal's or a colour's number as the byte a compact geometry stores (the typed array it goes into wraps what is out of range).
117
- const normalByte = n => Math.round(n * 127),
118
- colourByte = c => Math.min(255, Math.round(c * 255)),
122
+ const normalByte = component => Math.round(component * 127),
123
+ colourByte = component => Math.min(255, Math.round(component * 255)),
119
124
  OPAQUE = 255; // a padded colour's fourth byte: an alpha of one
120
125
  // The first `count` bytes of three to a vertex, four to a vertex: the three and then `fourth`
121
126
  function padded(bytes, count, fourth) {
122
127
  const out = new bytes.constructor((count / 3) * 4);
123
- for (let i = 0, o = 0; i < count; i += 3, o += 4) {
124
- out[o] = bytes[i];
125
- out[o + 1] = bytes[i + 1];
126
- out[o + 2] = bytes[i + 2];
127
- out[o + 3] = fourth;
128
+ for (let i = 0, writeOffset = 0; i < count; i += 3, writeOffset += 4) {
129
+ out[writeOffset] = bytes[i];
130
+ out[writeOffset + 1] = bytes[i + 1];
131
+ out[writeOffset + 2] = bytes[i + 2];
132
+ out[writeOffset + 3] = fourth;
128
133
  }
129
134
  return out;
130
135
  }
136
+ /**
137
+ * Accumulate coloured primitives and triangles in one set of vertex buffers.
138
+ * Import this class as `GeometryBuilder` from the package root. Use `parts`
139
+ * to transfer the result from a worker, or `mesh` to add it directly to a scene.
140
+ */
131
141
  class GB {
132
- // merges many coloured parts into one static geometry
133
142
  constructor() {
134
143
  // Positions, normals and colours fill typed arrays that grow, three numbers to a vertex: four bytes a number where a plain
135
144
  // array of numbers takes eight. Normals and colours are kept twice, as floats and as the bytes a compact geometry stores
@@ -177,48 +186,48 @@ class GB {
177
186
  this.pb.push(this.count);
178
187
  this.bounds.push(1e9, -1e9, 1e9, -1e9, 1e9, -1e9);
179
188
  }
180
- add(geo, col, m) {
181
- const g = geo.index ? geo.userData.ni || (geo.userData.ni = geo.toNonIndexed()) : geo;
182
- const pa = g.attributes.position.array,
183
- na = g.attributes.normal.array,
184
- numbers = pa.length,
189
+ add(geometry, color, transform) {
190
+ const sourceGeometry = geometry.index ? geometry.userData.ni || (geometry.userData.ni = geometry.toNonIndexed()) : geometry;
191
+ const positionArray = sourceGeometry.attributes.position.array,
192
+ normalArray = sourceGeometry.attributes.normal.array,
193
+ numbers = positionArray.length,
185
194
  parts = !!this.pb;
186
195
  if (parts) {
187
196
  this.startPart();
188
197
  this._ra = false;
189
198
  }
190
- if (this.M) m = _m5.multiplyMatrices(this.M, m);
191
- _n3.getNormalMatrix(m);
199
+ if (this.M) transform = _m5.multiplyMatrices(this.M, transform);
200
+ _n3.getNormalMatrix(transform);
192
201
  this.reserve(numbers);
193
202
  // Three.js's Vector3.applyMatrix4, applyMatrix3 and normalize, written out with the matrices' numbers read once for the
194
203
  // part: the same sums in the same order, so every vertex is where Three.js would put it
195
- const [e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15] = m.elements,
204
+ const [e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15] = transform.elements,
196
205
  [f0, f1, f2, f3, f4, f5, f6, f7, f8] = _n3.elements,
197
206
  flat = e3 === 0 && e7 === 0 && e11 === 0 && e15 === 1, // no perspective in the matrix: the division Three.js does is by one
198
207
  { ox, oy, oz, positions, normals, colours, normalBytes, colourBytes } = this,
199
- [r, gr, b] = col,
200
- rByte = colourByte(r),
201
- grByte = colourByte(gr),
202
- bByte = colourByte(b);
203
- if (!(Math.min(r, gr, b) >= 0 && Math.max(r, gr, b) <= 1)) this.wide = true;
204
- let k = this.count,
208
+ [red, green, blue] = color,
209
+ rByte = colourByte(red),
210
+ grByte = colourByte(green),
211
+ bByte = colourByte(blue);
212
+ if (!(Math.min(red, green, blue) >= 0 && Math.max(red, green, blue) <= 1)) this.wide = true;
213
+ let writeOffset = this.count,
205
214
  x0 = 1e9,
206
215
  x1 = -1e9,
207
216
  y0 = 1e9,
208
217
  y1 = -1e9,
209
218
  z0 = 1e9,
210
219
  z1 = -1e9;
211
- for (let i = 0; i < numbers; i += 3, k += 3) {
212
- let x = pa[i],
213
- y = pa[i + 1],
214
- z = pa[i + 2];
220
+ for (let i = 0; i < numbers; i += 3, writeOffset += 3) {
221
+ let x = positionArray[i],
222
+ y = positionArray[i + 1],
223
+ z = positionArray[i + 2];
215
224
  const w = flat ? 1 : 1 / (e3 * x + e7 * y + e11 * z + e15),
216
225
  px = (e0 * x + e4 * y + e8 * z + e12) * w + ox,
217
226
  py = (e1 * x + e5 * y + e9 * z + e13) * w + oy,
218
227
  pz = (e2 * x + e6 * y + e10 * z + e14) * w + oz;
219
- positions[k] = px;
220
- positions[k + 1] = py;
221
- positions[k + 2] = pz;
228
+ positions[writeOffset] = px;
229
+ positions[writeOffset + 1] = py;
230
+ positions[writeOffset + 2] = pz;
222
231
  if (parts) {
223
232
  if (px < x0) x0 = px;
224
233
  if (px > x1) x1 = px;
@@ -227,9 +236,9 @@ class GB {
227
236
  if (pz < z0) z0 = pz;
228
237
  if (pz > z1) z1 = pz;
229
238
  }
230
- x = na[i];
231
- y = na[i + 1];
232
- z = na[i + 2];
239
+ x = normalArray[i];
240
+ y = normalArray[i + 1];
241
+ z = normalArray[i + 2];
233
242
  let nx = f0 * x + f3 * y + f6 * z,
234
243
  ny = f1 * x + f4 * y + f7 * z,
235
244
  nz = f2 * x + f5 * y + f8 * z;
@@ -237,24 +246,24 @@ class GB {
237
246
  nx *= unit;
238
247
  ny *= unit;
239
248
  nz *= unit;
240
- normals[k] = nx;
241
- normals[k + 1] = ny;
242
- normals[k + 2] = nz;
243
- normalBytes[k] = normalByte(nx);
244
- normalBytes[k + 1] = normalByte(ny);
245
- normalBytes[k + 2] = normalByte(nz);
246
- colours[k] = r;
247
- colours[k + 1] = gr;
248
- colours[k + 2] = b;
249
- colourBytes[k] = rByte;
250
- colourBytes[k + 1] = grByte;
251
- colourBytes[k + 2] = bByte;
249
+ normals[writeOffset] = nx;
250
+ normals[writeOffset + 1] = ny;
251
+ normals[writeOffset + 2] = nz;
252
+ normalBytes[writeOffset] = normalByte(nx);
253
+ normalBytes[writeOffset + 1] = normalByte(ny);
254
+ normalBytes[writeOffset + 2] = normalByte(nz);
255
+ colours[writeOffset] = red;
256
+ colours[writeOffset + 1] = green;
257
+ colours[writeOffset + 2] = blue;
258
+ colourBytes[writeOffset] = rByte;
259
+ colourBytes[writeOffset + 1] = grByte;
260
+ colourBytes[writeOffset + 2] = bByte;
252
261
  }
253
- this.count = k;
262
+ this.count = writeOffset;
254
263
  if (parts) this.bounds.splice(-6, 6, x0, x1, y0, y1, z0, z1);
255
264
  const vertices = numbers / 3;
256
265
  if (this.tg) {
257
- const tag = this.tags?.get(col) ?? this.tv; // a builder may say what each of its colours is made of (a person's body: its metal, its leather)
266
+ const tag = this.tags?.get(color) ?? this.tv; // a builder may say what each of its colours is made of (a person's body: its metal, its leather)
258
267
  for (let i = 0; i < vertices; i++) this.tg.push(tag);
259
268
  }
260
269
  if (this.sk)
@@ -267,20 +276,21 @@ class GB {
267
276
  }
268
277
  return this;
269
278
  }
270
- box(x, y, z, sx, sy, sz, col, ry = 0, rx = 0, rz = 0) {
271
- return this.add(box, col, TRS(x, y, z, sx, sy, sz, ry, rx, rz));
279
+ /** Append a box with its base centred at (x, y, z). Dimensions are in world units and rotations are in radians. */
280
+ box(x, y, z, sx, sy, sz, color, ry = 0, rx = 0, rz = 0) {
281
+ return this.add(box, color, TRS(x, y, z, sx, sy, sz, ry, rx, rz));
272
282
  }
273
- raw(x, y, z, nx, ny, nz, r, g, b, b1 = 0, b2 = 0, w = 1) {
283
+ raw(x, y, z, nx, ny, nz, red, green, blue, primaryBone = 0, secondaryBone = 0, w = 1) {
274
284
  // one vertex, optionally weighted between two bones
275
285
  if (this.M) {
276
- _v.set(x, y, z).applyMatrix4(this.M);
277
- x = _v.x;
278
- y = _v.y;
279
- z = _v.z;
280
- _v.set(nx, ny, nz).transformDirection(this.M);
281
- nx = _v.x;
282
- ny = _v.y;
283
- nz = _v.z;
286
+ scratchVertex.set(x, y, z).applyMatrix4(this.M);
287
+ x = scratchVertex.x;
288
+ y = scratchVertex.y;
289
+ z = scratchVertex.z;
290
+ scratchVertex.set(nx, ny, nz).transformDirection(this.M);
291
+ nx = scratchVertex.x;
292
+ ny = scratchVertex.y;
293
+ nz = scratchVertex.z;
284
294
  }
285
295
  if (this.pb) {
286
296
  // vertices put in one after another are one part
@@ -298,30 +308,30 @@ class GB {
298
308
  if (z > box[at + 5]) box[at + 5] = z;
299
309
  }
300
310
  this.reserve(3);
301
- if (!(Math.min(r, g, b) >= 0 && Math.max(r, g, b) <= 1 && Math.max(Math.abs(nx), Math.abs(ny), Math.abs(nz)) <= 1)) this.wide = true;
302
- const k = this.count,
311
+ if (!(Math.min(red, green, blue) >= 0 && Math.max(red, green, blue) <= 1 && Math.max(Math.abs(nx), Math.abs(ny), Math.abs(nz)) <= 1)) this.wide = true;
312
+ const writeOffset = this.count,
303
313
  { positions, normals, colours, normalBytes, colourBytes } = this;
304
- positions[k] = x;
305
- positions[k + 1] = y;
306
- positions[k + 2] = z;
307
- normals[k] = nx;
308
- normals[k + 1] = ny;
309
- normals[k + 2] = nz;
310
- normalBytes[k] = normalByte(nx);
311
- normalBytes[k + 1] = normalByte(ny);
312
- normalBytes[k + 2] = normalByte(nz);
313
- colours[k] = r;
314
- colours[k + 1] = g;
315
- colours[k + 2] = b;
316
- colourBytes[k] = colourByte(r);
317
- colourBytes[k + 1] = colourByte(g);
318
- colourBytes[k + 2] = colourByte(b);
319
- this.count = k + 3;
314
+ positions[writeOffset] = x;
315
+ positions[writeOffset + 1] = y;
316
+ positions[writeOffset + 2] = z;
317
+ normals[writeOffset] = nx;
318
+ normals[writeOffset + 1] = ny;
319
+ normals[writeOffset + 2] = nz;
320
+ normalBytes[writeOffset] = normalByte(nx);
321
+ normalBytes[writeOffset + 1] = normalByte(ny);
322
+ normalBytes[writeOffset + 2] = normalByte(nz);
323
+ colours[writeOffset] = red;
324
+ colours[writeOffset + 1] = green;
325
+ colours[writeOffset + 2] = blue;
326
+ colourBytes[writeOffset] = colourByte(red);
327
+ colourBytes[writeOffset + 1] = colourByte(green);
328
+ colourBytes[writeOffset + 2] = colourByte(blue);
329
+ this.count = writeOffset + 3;
320
330
  if (this.tg) this.tg.push(this.tv);
321
331
  if (this.sk) {
322
- this.sk.push(b1);
332
+ this.sk.push(primaryBone);
323
333
  if (this.sk2) {
324
- this.sk2.push(b2);
334
+ this.sk2.push(secondaryBone);
325
335
  this.skw.push(w);
326
336
  }
327
337
  }
@@ -372,10 +382,10 @@ class GB {
372
382
  const skip = lo - part.first,
373
383
  limit = hi - lo;
374
384
  for (let i = 0; i < part.corners.length; i += 3) {
375
- const a = part.corners[i] - skip,
376
- b = part.corners[i + 1] - skip,
377
- c = part.corners[i + 2] - skip;
378
- if (a >= 0 && a < limit && b >= 0 && b < limit && c >= 0 && c < limit) corners.push(a, b, c);
385
+ const firstCorner = part.corners[i] - skip,
386
+ secondCorner = part.corners[i + 1] - skip,
387
+ thirdCorner = part.corners[i + 2] - skip;
388
+ if (firstCorner >= 0 && firstCorner < limit && secondCorner >= 0 && secondCorner < limit && thirdCorner >= 0 && thirdCorner < limit) corners.push(firstCorner, secondCorner, thirdCorner);
379
389
  }
380
390
  }
381
391
  this.triangles(first + lo - start, corners, first + hi - start);
@@ -387,83 +397,83 @@ class GB {
387
397
  // A sculptMesh, tinted between two colours by its first channel. cut(dir) leaves out what it is true for; the triangles its edge
388
398
  // crosses are clipped where the edge runs (found by halving along each crossed side), so a hairline, beard or hood ends in a
389
399
  // smooth line instead of the sphere's triangles.
390
- sculpt(m, col, col2, cut, k = 1) {
391
- const { P, N, A, B, D } = m,
392
- bn = this.bone,
400
+ sculpt(sculptedMesh, color, secondColor, cut, scale = 1) {
401
+ const { P: positions, N: normals, A: tintWeights, B: shades, D: directions } = sculptedMesh,
402
+ boneIndex = this.bone,
393
403
  ox = this.ox,
394
404
  oy = this.oy,
395
405
  oz = this.oz;
396
- const emit = v => {
397
- const a = v[9],
398
- sh = v[10];
406
+ const emit = vertex => {
407
+ const tintWeight = vertex[9],
408
+ shade = vertex[10];
399
409
  this.raw(
400
- v[0] * k + ox,
401
- v[1] * k + oy,
402
- v[2] * k + oz,
403
- v[3],
404
- v[4],
405
- v[5],
406
- (col[0] + (col2[0] - col[0]) * a) * sh,
407
- (col[1] + (col2[1] - col[1]) * a) * sh,
408
- (col[2] + (col2[2] - col[2]) * a) * sh,
409
- bn,
410
- bn,
410
+ vertex[0] * scale + ox,
411
+ vertex[1] * scale + oy,
412
+ vertex[2] * scale + oz,
413
+ vertex[3],
414
+ vertex[4],
415
+ vertex[5],
416
+ (color[0] + (secondColor[0] - color[0]) * tintWeight) * shade,
417
+ (color[1] + (secondColor[1] - color[1]) * tintWeight) * shade,
418
+ (color[2] + (secondColor[2] - color[2]) * tintWeight) * shade,
419
+ boneIndex,
420
+ boneIndex,
411
421
  1,
412
422
  );
413
423
  };
414
424
  const vert = i => [
415
- P[i * 3],
416
- P[i * 3 + 1],
417
- P[i * 3 + 2],
418
- N[i * 3],
419
- N[i * 3 + 1],
420
- N[i * 3 + 2],
421
- D[i * 3],
422
- D[i * 3 + 1],
423
- D[i * 3 + 2],
424
- A[i],
425
- B[i],
425
+ positions[i * 3],
426
+ positions[i * 3 + 1],
427
+ positions[i * 3 + 2],
428
+ normals[i * 3],
429
+ normals[i * 3 + 1],
430
+ normals[i * 3 + 2],
431
+ directions[i * 3],
432
+ directions[i * 3 + 1],
433
+ directions[i * 3 + 2],
434
+ tintWeights[i],
435
+ shades[i],
426
436
  ];
427
- const mix = (u, v, t) => {
428
- const r = u.map((x, j) => x + (v[j] - x) * t),
429
- l = Math.hypot(r[6], r[7], r[8]) || 1,
430
- ln = Math.hypot(r[3], r[4], r[5]) || 1;
431
- r[6] /= l;
432
- r[7] /= l;
433
- r[8] /= l;
434
- r[3] /= ln;
435
- r[4] /= ln;
436
- r[5] /= ln;
437
- return r;
437
+ const mix = (startVertex, endVertex, fraction) => {
438
+ const interpolated = startVertex.map((x, j) => x + (endVertex[j] - x) * fraction),
439
+ directionLength = Math.hypot(interpolated[6], interpolated[7], interpolated[8]) || 1,
440
+ normalLength = Math.hypot(interpolated[3], interpolated[4], interpolated[5]) || 1;
441
+ interpolated[6] /= directionLength;
442
+ interpolated[7] /= directionLength;
443
+ interpolated[8] /= directionLength;
444
+ interpolated[3] /= normalLength;
445
+ interpolated[4] /= normalLength;
446
+ interpolated[5] /= normalLength;
447
+ return interpolated;
438
448
  };
439
- const out = v => !!cut(v[6], v[7], v[8]);
440
- for (let t = 0; t < m.n; t += 3) {
441
- const q = t * 3,
442
- o0 = cut ? !!cut(D[q], D[q + 1], D[q + 2]) : false,
443
- o1 = cut ? !!cut(D[q + 3], D[q + 4], D[q + 5]) : false,
444
- o2 = cut ? !!cut(D[q + 6], D[q + 7], D[q + 8]) : false;
449
+ const out = vertex => !!cut(vertex[6], vertex[7], vertex[8]);
450
+ for (let triangleStart = 0; triangleStart < sculptedMesh.n; triangleStart += 3) {
451
+ const directionOffset = triangleStart * 3,
452
+ o0 = cut ? !!cut(directions[directionOffset], directions[directionOffset + 1], directions[directionOffset + 2]) : false,
453
+ o1 = cut ? !!cut(directions[directionOffset + 3], directions[directionOffset + 4], directions[directionOffset + 5]) : false,
454
+ o2 = cut ? !!cut(directions[directionOffset + 6], directions[directionOffset + 7], directions[directionOffset + 8]) : false;
445
455
  if (o0 && o1 && o2) continue;
446
456
  if (!o0 && !o1 && !o2) {
447
- for (let i = t; i < t + 3; i++) emit(vert(i));
457
+ for (let i = triangleStart; i < triangleStart + 3; i++) emit(vert(i));
448
458
  continue;
449
459
  }
450
- const tri = [vert(t), vert(t + 1), vert(t + 2)],
451
- o = [o0, o1, o2],
460
+ const tri = [vert(triangleStart), vert(triangleStart + 1), vert(triangleStart + 2)],
461
+ outsideFlags = [o0, o1, o2],
452
462
  poly = [];
453
463
  for (let e = 0; e < 3; e++) {
454
- const a = tri[e],
455
- b = tri[(e + 1) % 3],
456
- oa = o[e];
457
- if (!oa) poly.push(a);
458
- if (oa !== o[(e + 1) % 3]) {
464
+ const edgeStart = tri[e],
465
+ edgeEnd = tri[(e + 1) % 3],
466
+ startOutside = outsideFlags[e];
467
+ if (!startOutside) poly.push(edgeStart);
468
+ if (startOutside !== outsideFlags[(e + 1) % 3]) {
459
469
  let lo = 0,
460
470
  hi = 1;
461
- for (let s = 0; s < 7; s++) {
471
+ for (let iteration = 0; iteration < 7; iteration++) {
462
472
  const mid = (lo + hi) / 2;
463
- if (out(mix(a, b, mid)) === oa) lo = mid;
473
+ if (out(mix(edgeStart, edgeEnd, mid)) === startOutside) lo = mid;
464
474
  else hi = mid;
465
475
  }
466
- poly.push(mix(a, b, oa ? hi : lo));
476
+ poly.push(mix(edgeStart, edgeEnd, startOutside ? hi : lo));
467
477
  }
468
478
  }
469
479
  for (let i = 1; i < poly.length - 1; i++) {
@@ -473,11 +483,12 @@ class GB {
473
483
  }
474
484
  }
475
485
  }
476
- // What has been filled, as the arrays a geometry is made of (geometryOf), each of its own size: the room to grow is not kept.
477
- // A worker hands these to the page, which has the geometry made of them. `compact`: byte normals and colours, a third of the
478
- // memory, for the big generated blocks. `pad` (with compact): four bytes to a normal and a colour, the fourth a normal's 0
479
- // and a colour's opaque alpha, as the graphics card keeps them: ANGLE on Metal copies a vertex buffer whose stride is not a
480
- // multiple of four bytes into one that is and keeps both, so a three-byte normal or colour took seven bytes of the card.
486
+ /**
487
+ * Copy the filled attributes into right-sized typed arrays for `geometryOf`.
488
+ * The arrays can be transferred to another thread. The builder retains its own storage.
489
+ * @param {boolean} [compact] - Store normals and colours as normalized bytes.
490
+ * @param {boolean} [pad] - Pad compact normals and colours to four bytes per vertex for aligned GPU uploads.
491
+ */
481
492
  parts(compact, pad = false) {
482
493
  if (this.spent) throw new Error('GB: built again after its parts were let go (release)');
483
494
  const filled = array => array.slice(0, this.count),
@@ -491,23 +502,23 @@ class GB {
491
502
  if (this.tg) parts.aTag = new Float32Array(this.tg);
492
503
  if (this.sk) {
493
504
  // rigid skinning: every vertex follows exactly one bone
494
- const n = this.sk.length,
495
- si = (parts.skinIndex = new Uint8Array(n * 4)),
496
- sw = (parts.skinWeight = new Uint8Array(n * 4));
497
- for (let i = 0; i < n; i++) {
505
+ const vertexCount = this.sk.length,
506
+ skinIndices = (parts.skinIndex = new Uint8Array(vertexCount * 4)),
507
+ skinWeights = (parts.skinWeight = new Uint8Array(vertexCount * 4));
508
+ for (let i = 0; i < vertexCount; i++) {
498
509
  const w = this.sk2 ? Math.round(this.skw[i] * 255) : 255;
499
- si[i * 4] = this.sk[i];
500
- sw[i * 4] = w;
510
+ skinIndices[i * 4] = this.sk[i];
511
+ skinWeights[i * 4] = w;
501
512
  if (this.sk2) {
502
- si[i * 4 + 1] = this.sk2[i];
503
- sw[i * 4 + 1] = 255 - w;
513
+ skinIndices[i * 4 + 1] = this.sk2[i];
514
+ skinWeights[i * 4 + 1] = 255 - w;
504
515
  }
505
516
  }
506
517
  if (this.sk3)
507
518
  for (const [i, third] of this.sk3) {
508
- si[i * 4 + 2] = third[0];
509
- sw[i * 4 + 2] = Math.min(255 - sw[i * 4], Math.round(third[1] * 255));
510
- sw[i * 4 + 1] = 255 - sw[i * 4] - sw[i * 4 + 2];
519
+ skinIndices[i * 4 + 2] = third[0];
520
+ skinWeights[i * 4 + 2] = Math.min(255 - skinWeights[i * 4], Math.round(third[1] * 255));
521
+ skinWeights[i * 4 + 1] = 255 - skinWeights[i * 4] - skinWeights[i * 4 + 2];
511
522
  }
512
523
  }
513
524
  return parts;
@@ -532,11 +543,12 @@ class GB {
532
543
  if (this.skw) this.skw = [];
533
544
  this.spent = true;
534
545
  }
535
- mesh(mat, parent, shadow = true) {
536
- const m = stillMesh(this.build(), mat, shadow);
546
+ /** Build a static mesh, release the builder’s backing arrays and attach the mesh to the optional parent. */
547
+ mesh(material, parent, shadow = true) {
548
+ const mesh = stillMesh(this.build(), material, shadow);
537
549
  this.release();
538
- if (parent) parent.add(m);
539
- return m;
550
+ if (parent) parent.add(mesh);
551
+ return mesh;
540
552
  }
541
553
  }
542
554
 
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Pack a grid into one bit per cell. The predicate receives the row-major index
3
+ * `row * width + column`. Transfer the returned `bits.buffer` with the mask when
4
+ * handing it to a worker.
5
+ * @param {number} width - Positive integer column count.
6
+ * @param {number} height - Positive integer row count.
7
+ * @param {(index: number) => boolean} includesIndex - Selects cells to mark.
8
+ */
9
+ export function packCellMask(width, height, includesIndex) {
10
+ if (!(Number.isSafeInteger(width) && width > 0 && Number.isSafeInteger(height) && height > 0))
11
+ throw new RangeError('Cell mask dimensions must be positive integers');
12
+ const length = width * height;
13
+ const bits = new Uint8Array(Math.ceil(length / 8));
14
+ for (let cellIndex = 0; cellIndex < length; cellIndex++) {
15
+ if (includesIndex(cellIndex)) bits[Math.floor(cellIndex / 8)] |= 1 << (cellIndex & 7);
16
+ }
17
+ return { width, height, bits };
18
+ }
19
+
20
+ /** Read membership at cell coordinates, rounding down within a cell. Coordinates outside the grid return false. */
21
+ export function cellIsSet(mask, x, y) {
22
+ if (!(x >= 0 && x < mask.width && y >= 0 && y < mask.height)) return false;
23
+ const cellIndex = Math.floor(y) * mask.width + Math.floor(x);
24
+ return (mask.bits[Math.floor(cellIndex / 8)] & (1 << (cellIndex & 7))) !== 0;
25
+ }