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,100 +1,132 @@
1
- // A sheet sampled on a grid with smooth normals: fn(u, v) -> [x, y, z]. Sagging canvas, flags, earth banks and rubble mounds are all made with it.
2
- // col is one colour, or col(u, v) for each cell of the grid; with o.blend, col(u, v) is read at each point of the grid instead and
3
- // blended across the cells (ground whose colour changes smoothly, without a patchwork of cells).
1
+ /**
2
+ * Tessellate a parametric sheet into triangles with smooth normals for a geometry builder.
3
+ * @module
4
+ */
5
+
4
6
  const buffers = [];
5
- // Callbacks may themselves sample another surface. Only inactive buffers are reused.
6
- export function sampleSurface(gb, nu, nv, fn, col, o = {}) {
7
+ /**
8
+ * Append two triangles per grid cell. On an X/Z sheet where u increases along +X
9
+ * and v along +Z, the front faces point upward. A second side duplicates the
10
+ * triangles with reversed winding and normals.
11
+ * @param {Pick<import('./builder.js').GB, 'raw'>} builder - Destination for the generated vertices.
12
+ * @param {number} columns - Positive integer cell count along u.
13
+ * @param {number} rows - Positive integer cell count along v.
14
+ * @param {(u: number, v: number) => ArrayLike<number>} pointAt - Map coordinates in [0, 1] to [x, y, z].
15
+ * @param {ArrayLike<number> | ((u: number, v: number) => ArrayLike<number>)} surfaceColor -
16
+ * Linear RGB colour or a function sampled at cell centres, or at vertices when blending.
17
+ * @param {{blend?: boolean, flip?: boolean, back?: boolean | ArrayLike<number>}} [options] -
18
+ * Blend vertex colours, flip the front winding, or add a back side. Set back to true
19
+ * to reuse front colours, or supply a separate linear RGB colour.
20
+ * @example
21
+ * ```js
22
+ * import { GeometryBuilder } from 'roamwright';
23
+ * import { sampleSurface } from 'roamwright/geometry/sampled-surface.js';
24
+ *
25
+ * const ground = new GeometryBuilder();
26
+ * sampleSurface(ground, 8, 8, (u, v) => [16 * u, 2 * Math.sin(u * Math.PI), 16 * v], [0.3, 0.5, 0.2]);
27
+ * const parts = ground.parts(); // 128 triangles
28
+ * ```
29
+ */
30
+ export function sampleSurface(builder, columns, rows, pointAt, surfaceColor, options = {}) {
7
31
  const scratch = buffers.pop() ?? { positions: [], normals: [], colors: [] };
8
32
  try {
9
- writeSurface(gb, nu, nv, fn, col, o, scratch);
33
+ writeSurface(builder, columns, rows, pointAt, surfaceColor, options, scratch);
10
34
  } finally {
11
35
  buffers.push(scratch);
12
36
  }
13
37
  }
14
38
 
15
- function writeSurface(gb, nu, nv, fn, col, o, scratch) {
16
- const w1 = nu + 1,
17
- P_ = scratch.positions,
18
- N_ = scratch.normals,
19
- cf = typeof col === 'function',
20
- back = o.back,
21
- fl = o.flip ? -1 : 1,
22
- blend = cf && o.blend,
23
- C_ = scratch.colors;
24
- for (let j = 0; j <= nv; j++)
25
- for (let i = 0; i <= nu; i++) {
26
- const p = fn(i / nu, j / nv),
27
- q = (j * w1 + i) * 3;
28
- P_[q] = p[0];
29
- P_[q + 1] = p[1];
30
- P_[q + 2] = p[2];
39
+ function writeSurface(builder, columns, rows, pointAt, surfaceColor, options, scratch) {
40
+ const rowStride = columns + 1,
41
+ positions = scratch.positions,
42
+ normals = scratch.normals,
43
+ hasColorSampler = typeof surfaceColor === 'function',
44
+ back = options.back,
45
+ normalSign = options.flip ? -1 : 1,
46
+ blend = hasColorSampler && options.blend,
47
+ colors = scratch.colors;
48
+ for (let row = 0; row <= rows; row++)
49
+ for (let column = 0; column <= columns; column++) {
50
+ const point = pointAt(column / columns, row / rows),
51
+ componentOffset = (row * rowStride + column) * 3;
52
+ positions[componentOffset] = point[0];
53
+ positions[componentOffset + 1] = point[1];
54
+ positions[componentOffset + 2] = point[2];
31
55
  if (blend) {
32
- const c = col(i / nu, j / nv);
33
- C_[q] = c[0];
34
- C_[q + 1] = c[1];
35
- C_[q + 2] = c[2];
56
+ const vertexColor = surfaceColor(column / columns, row / rows);
57
+ colors[componentOffset] = vertexColor[0];
58
+ colors[componentOffset + 1] = vertexColor[1];
59
+ colors[componentOffset + 2] = vertexColor[2];
36
60
  }
37
61
  }
38
- for (let j = 0; j <= nv; j++)
39
- for (let i = 0; i <= nu; i++) {
40
- const a = (j * w1 + Math.max(0, i - 1)) * 3,
41
- b = (j * w1 + Math.min(nu, i + 1)) * 3,
42
- c = (Math.max(0, j - 1) * w1 + i) * 3,
43
- d = (Math.min(nv, j + 1) * w1 + i) * 3,
44
- q = (j * w1 + i) * 3;
45
- const tx = P_[b] - P_[a],
46
- ty = P_[b + 1] - P_[a + 1],
47
- tz = P_[b + 2] - P_[a + 2],
48
- sx = P_[d] - P_[c],
49
- sy = P_[d + 1] - P_[c + 1],
50
- sz = P_[d + 2] - P_[c + 2];
62
+ for (let row = 0; row <= rows; row++)
63
+ for (let column = 0; column <= columns; column++) {
64
+ const leftOffset = (row * rowStride + Math.max(0, column - 1)) * 3,
65
+ rightOffset = (row * rowStride + Math.min(columns, column + 1)) * 3,
66
+ previousRowOffset = (Math.max(0, row - 1) * rowStride + column) * 3,
67
+ nextRowOffset = (Math.min(rows, row + 1) * rowStride + column) * 3,
68
+ componentOffset = (row * rowStride + column) * 3;
69
+ const tx = positions[rightOffset] - positions[leftOffset],
70
+ ty = positions[rightOffset + 1] - positions[leftOffset + 1],
71
+ tz = positions[rightOffset + 2] - positions[leftOffset + 2],
72
+ sx = positions[nextRowOffset] - positions[previousRowOffset],
73
+ sy = positions[nextRowOffset + 1] - positions[previousRowOffset + 1],
74
+ sz = positions[nextRowOffset + 2] - positions[previousRowOffset + 2];
51
75
  const nx = sy * tz - sz * ty,
52
76
  ny = sz * tx - sx * tz,
53
77
  nz = sx * ty - sy * tx,
54
- l = (Math.hypot(nx, ny, nz) || 1) * fl;
55
- N_[q] = nx / l;
56
- N_[q + 1] = ny / l;
57
- N_[q + 2] = nz / l;
78
+ normalDivisor = (Math.hypot(nx, ny, nz) || 1) * normalSign;
79
+ normals[componentOffset] = nx / normalDivisor;
80
+ normals[componentOffset + 1] = ny / normalDivisor;
81
+ normals[componentOffset + 2] = nz / normalDivisor;
58
82
  }
59
- const V = (i, j, s, cc) => {
60
- const q = (j * w1 + i) * 3;
61
- if (cc) gb.raw(P_[q], P_[q + 1], P_[q + 2], N_[q] * s, N_[q + 1] * s, N_[q + 2] * s, cc[0], cc[1], cc[2]);
62
- else gb.raw(P_[q], P_[q + 1], P_[q + 2], N_[q] * s, N_[q + 1] * s, N_[q + 2] * s, C_[q], C_[q + 1], C_[q + 2]);
83
+ const writeVertex = (column, row, sideSign, frontColor) => {
84
+ const componentOffset = (row * rowStride + column) * 3;
85
+ if (frontColor) builder.raw(
86
+ positions[componentOffset], positions[componentOffset + 1], positions[componentOffset + 2],
87
+ normals[componentOffset] * sideSign, normals[componentOffset + 1] * sideSign, normals[componentOffset + 2] * sideSign,
88
+ frontColor[0], frontColor[1], frontColor[2],
89
+ );
90
+ else builder.raw(
91
+ positions[componentOffset], positions[componentOffset + 1], positions[componentOffset + 2],
92
+ normals[componentOffset] * sideSign, normals[componentOffset + 1] * sideSign, normals[componentOffset + 2] * sideSign,
93
+ colors[componentOffset], colors[componentOffset + 1], colors[componentOffset + 2],
94
+ );
63
95
  };
64
- for (let j = 0; j < nv; j++)
65
- for (let i = 0; i < nu; i++) {
66
- const cc = blend ? null : cf ? col((i + 0.5) / nu, (j + 0.5) / nv) : col,
67
- bc = back === true ? cc : back;
68
- if (fl > 0) {
69
- V(i, j, 1, cc);
70
- V(i, j + 1, 1, cc);
71
- V(i + 1, j + 1, 1, cc);
72
- V(i, j, 1, cc);
73
- V(i + 1, j + 1, 1, cc);
74
- V(i + 1, j, 1, cc);
96
+ for (let row = 0; row < rows; row++)
97
+ for (let column = 0; column < columns; column++) {
98
+ const frontColor = blend ? null : hasColorSampler ? surfaceColor((column + 0.5) / columns, (row + 0.5) / rows) : surfaceColor,
99
+ backColor = back === true ? frontColor : back;
100
+ if (normalSign > 0) {
101
+ writeVertex(column, row, 1, frontColor);
102
+ writeVertex(column, row + 1, 1, frontColor);
103
+ writeVertex(column + 1, row + 1, 1, frontColor);
104
+ writeVertex(column, row, 1, frontColor);
105
+ writeVertex(column + 1, row + 1, 1, frontColor);
106
+ writeVertex(column + 1, row, 1, frontColor);
75
107
  } else {
76
- V(i, j, 1, cc);
77
- V(i + 1, j + 1, 1, cc);
78
- V(i, j + 1, 1, cc);
79
- V(i, j, 1, cc);
80
- V(i + 1, j, 1, cc);
81
- V(i + 1, j + 1, 1, cc);
108
+ writeVertex(column, row, 1, frontColor);
109
+ writeVertex(column + 1, row + 1, 1, frontColor);
110
+ writeVertex(column, row + 1, 1, frontColor);
111
+ writeVertex(column, row, 1, frontColor);
112
+ writeVertex(column + 1, row, 1, frontColor);
113
+ writeVertex(column + 1, row + 1, 1, frontColor);
82
114
  }
83
115
  if (back) {
84
- if (fl > 0) {
85
- V(i, j, -1, bc);
86
- V(i + 1, j + 1, -1, bc);
87
- V(i, j + 1, -1, bc);
88
- V(i, j, -1, bc);
89
- V(i + 1, j, -1, bc);
90
- V(i + 1, j + 1, -1, bc);
116
+ if (normalSign > 0) {
117
+ writeVertex(column, row, -1, backColor);
118
+ writeVertex(column + 1, row + 1, -1, backColor);
119
+ writeVertex(column, row + 1, -1, backColor);
120
+ writeVertex(column, row, -1, backColor);
121
+ writeVertex(column + 1, row, -1, backColor);
122
+ writeVertex(column + 1, row + 1, -1, backColor);
91
123
  } else {
92
- V(i, j, -1, bc);
93
- V(i, j + 1, -1, bc);
94
- V(i + 1, j + 1, -1, bc);
95
- V(i, j, -1, bc);
96
- V(i + 1, j + 1, -1, bc);
97
- V(i + 1, j, -1, bc);
124
+ writeVertex(column, row, -1, backColor);
125
+ writeVertex(column, row + 1, -1, backColor);
126
+ writeVertex(column + 1, row + 1, -1, backColor);
127
+ writeVertex(column, row, -1, backColor);
128
+ writeVertex(column + 1, row + 1, -1, backColor);
129
+ writeVertex(column + 1, row, -1, backColor);
98
130
  }
99
131
  }
100
132
  }
@@ -1,4 +1,44 @@
1
- // Deterministic cell samples around a moving observer, from fine inner cover to coarse outer cover.
1
+ /**
2
+ * Place repeatable patches of scenery around a moving observer.
3
+ * @module
4
+ */
5
+
6
+ /**
7
+ * Sample one candidate per world cell around the observer. Each level scans a square.
8
+ * The fill callback decides which candidates to keep. A rejected candidate leaves its
9
+ * output index available to the next one. Ring bounds are available to `finish` for
10
+ * fading or clipping overlap between levels.
11
+ * @param {object} options - Sampling levels and callbacks.
12
+ * @param {{cell: number, half: number, refill: number}[]} options.rings - Levels from fine to coarse.
13
+ * Cell width and refill distance are in world units. At full density each side spans `2 * half` cells.
14
+ * @param {(col: number, row: number, seed: number) => number} options.hash - Repeatable offset in [0, 1).
15
+ * @param {[number, number]} options.seeds - Separate hash seeds for the X and Z offsets.
16
+ * @param {(level: number, index: number, x: number, z: number, h1: number, h2: number, col: number, row: number, cell: number) => boolean} options.fill -
17
+ * Write the candidate at the supplied index and return true to count it.
18
+ * @param {(level: number) => void} [options.start] - Called before a level refills so its old output can be cleared.
19
+ * @param {(grids: {cell: number, count: number, inner: number, outer: number}[]) => void} [options.finish] -
20
+ * Called once after any levels refill. Counts and bounds describe the current levels and are reused on later updates.
21
+ * @returns {(x: number, z: number, density?: number) => boolean} Update using world X/Z. Density scales each
22
+ * level’s half-width from zero to full size. Returns true when at least one level refills.
23
+ * @example
24
+ * ```js
25
+ * import { createHash } from 'roamwright/math/random.js';
26
+ * import { createScatterGrid } from 'roamwright/geometry/scatter-grid.js';
27
+ *
28
+ * const trees = [];
29
+ * const update = createScatterGrid({
30
+ * rings: [{ cell: 8, half: 2, refill: 4 }],
31
+ * hash: createHash(42), seeds: [1, 2],
32
+ * start() { trees.length = 0; },
33
+ * fill(level, index, x, z) {
34
+ * trees[index] = [x, z];
35
+ * return true;
36
+ * },
37
+ * });
38
+ * update(0, 0); // 16 tree positions
39
+ * update(1, 0); // false, still inside the refill distance
40
+ * ```
41
+ */
2
42
  export function createScatterGrid({ rings, hash, seeds, fill, start, finish }) {
3
43
  const grids = rings.map(ring => ({ ...ring, x: Infinity, z: Infinity, halfUsed: -1, count: 0, inner: 0, outer: 0 }));
4
44
  const update = (x, z, density = 1) => {
@@ -11,16 +51,16 @@ export function createScatterGrid({ rings, hash, seeds, fill, start, finish }) {
11
51
  grid.outer = Math.max(inner, half * grid.cell - grid.refill);
12
52
  inner = grid.outer;
13
53
  if (grid.halfUsed === half && Math.abs(x - grid.x) + Math.abs(z - grid.z) < grid.refill) continue;
14
- const ix = Math.floor(x / grid.cell), iz = Math.floor(z / grid.cell);
54
+ const centreColumn = Math.floor(x / grid.cell), centreRow = Math.floor(z / grid.cell);
15
55
  grid.x = x;
16
56
  grid.z = z;
17
57
  grid.halfUsed = half;
18
58
  grid.count = 0;
19
59
  start?.(level);
20
- for (let row = iz - half; row < iz + half; row++) {
21
- for (let col = ix - half; col < ix + half; col++) {
22
- const h1 = hash(col, row, seeds[0]), h2 = hash(col, row, seeds[1]);
23
- if (fill(level, grid.count, (col + h1) * grid.cell, (row + h2) * grid.cell, h1, h2, col, row, grid.cell)) grid.count++;
60
+ for (let row = centreRow - half; row < centreRow + half; row++) {
61
+ for (let col = centreColumn - half; col < centreColumn + half; col++) {
62
+ const offsetX = hash(col, row, seeds[0]), offsetZ = hash(col, row, seeds[1]);
63
+ if (fill(level, grid.count, (col + offsetX) * grid.cell, (row + offsetZ) * grid.cell, offsetX, offsetZ, col, row, grid.cell)) grid.count++;
24
64
  }
25
65
  }
26
66
  changed = true;
@@ -13,8 +13,8 @@ export function createSculptor({ vertexTag } = {}) {
13
13
  // Level 1 halves detail. Level 2 is for the smallest screen-space silhouettes.
14
14
  let LOD_LO = 0;
15
15
  // rings: [y, rx, rz, cx, cz, col, bone1, bone2, weight1]; a ring marked `fine` is left out of the far and light versions.
16
- // o.fold(v, a) scales the radius (pleats); o.lay(p, a, ring) moves a ring's point p = [x, y, z] at angle a from where the level
17
- // ring puts it (a hem that dips, a ring that slopes, a muscle's swell); o.tint(p, a, ring) is how bright the ring's colour is
16
+ // options.fold(v, angle) scales the radius (pleats); options.lay(point, angle, ring) moves a ring's point [x, y, z] at that angle from where the level
17
+ // ring puts it (a hem that dips, a ring that slopes, a muscle's swell); options.tint(point, angle, ring) is how bright the ring's colour is
18
18
  // there (a crease's shade, at most 1); o.sk(y, a, ring) gives a point's bones and weight in place of its ring's; o.a0/o.a1
19
19
  // give an open sheet; o.joined puts each point in once and joins the triangles through them by number (a sixth of the points,
20
20
  // for a builder whose geometry is drawn by its numbers: a person's body). o.relief(y, a, ring) is what stands out of the surface
@@ -24,52 +24,52 @@ export function createSculptor({ vertexTag } = {}) {
24
24
  const RELIEF_STEP = [0.002, 0.02], // how far apart the relief is read to find its slope: metres of height, radians round
25
25
  _lq = [],
26
26
  _lm = [];
27
- function loft(gb, ox, oy, oz, rings, seg, o = {}) {
27
+ function loft(builder, ox, oy, oz, rings, segments, options = {}) {
28
28
  if (LOD_LO) {
29
- seg = LOD_LO > 1 ? Math.max(5, Math.round(seg * 0.38)) : Math.max(5, (seg + 1) >> 1);
29
+ segments = LOD_LO > 1 ? Math.max(5, Math.round(segments * 0.38)) : Math.max(5, (segments + 1) >> 1);
30
30
  rings = rings.filter(r => !r.fine);
31
31
  }
32
- const nr = rings.length,
33
- a0 = o.a0 ?? 0,
34
- a1 = o.a1 ?? TAU,
32
+ const ringCount = rings.length,
33
+ a0 = options.a0 ?? 0,
34
+ a1 = options.a1 ?? TAU,
35
35
  closed = a1 - a0 > TAU - 1e-4,
36
- ns = closed ? seg : seg + 1,
36
+ ns = closed ? segments : segments + 1,
37
37
  P_ = _lp,
38
38
  N_ = _ln,
39
39
  T_ = _lt,
40
- { fold, lay, tint } = o,
41
- up = rings[nr - 1][0] > rings[0][0];
42
- for (let i = 0; i < nr; i++) {
40
+ { fold, lay, tint } = options,
41
+ up = rings[ringCount - 1][0] > rings[0][0];
42
+ for (let i = 0; i < ringCount; i++) {
43
43
  const r = rings[i];
44
44
  for (let j = 0; j < ns; j++) {
45
- const a = a0 + ((a1 - a0) * j) / seg,
46
- k = fold ? fold(i / (nr - 1), a) : 1,
45
+ const angle = a0 + ((a1 - a0) * j) / segments,
46
+ k = fold ? fold(i / (ringCount - 1), angle) : 1,
47
47
  q = (i * ns + j) * 3;
48
- _pt[0] = r[3] + Math.cos(a) * r[1] * k;
48
+ _pt[0] = r[3] + Math.cos(angle) * r[1] * k;
49
49
  _pt[1] = r[0];
50
- _pt[2] = r[4] + Math.sin(a) * r[2] * k;
51
- if (lay) lay(_pt, a, r);
52
- if (tint) T_[i * ns + j] = tint(_pt, a, r);
50
+ _pt[2] = r[4] + Math.sin(angle) * r[2] * k;
51
+ if (lay) lay(_pt, angle, r);
52
+ if (tint) T_[i * ns + j] = tint(_pt, angle, r);
53
53
  P_[q] = ox + _pt[0];
54
54
  P_[q + 1] = oy + _pt[1];
55
55
  P_[q + 2] = oz + _pt[2];
56
56
  }
57
57
  }
58
- const idx = (i, j) => (i * ns + (closed ? (j + ns) % ns : clamp(j, 0, ns - 1))) * 3;
59
- for (let i = 0; i < nr; i++)
58
+ const vertexOffset = (i, j) => (i * ns + (closed ? (j + ns) % ns : clamp(j, 0, ns - 1))) * 3;
59
+ for (let i = 0; i < ringCount; i++)
60
60
  for (let j = 0; j < ns; j++) {
61
- const A = idx(i, j - 1),
62
- B_ = idx(i, j + 1),
63
- C_ = idx(Math.max(0, i - 1), j),
64
- D = idx(Math.min(nr - 1, i + 1), j),
61
+ const previousSideOffset = vertexOffset(i, j - 1),
62
+ B_ = vertexOffset(i, j + 1),
63
+ C_ = vertexOffset(Math.max(0, i - 1), j),
64
+ nextRingOffset = vertexOffset(Math.min(ringCount - 1, i + 1), j),
65
65
  q = (i * ns + j) * 3,
66
66
  r = rings[i];
67
- const tx = P_[B_] - P_[A],
68
- ty = P_[B_ + 1] - P_[A + 1],
69
- tz = P_[B_ + 2] - P_[A + 2],
70
- ux = P_[D] - P_[C_],
71
- uy = P_[D + 1] - P_[C_ + 1],
72
- uz = P_[D + 2] - P_[C_ + 2];
67
+ const tx = P_[B_] - P_[previousSideOffset],
68
+ ty = P_[B_ + 1] - P_[previousSideOffset + 1],
69
+ tz = P_[B_ + 2] - P_[previousSideOffset + 2],
70
+ ux = P_[nextRingOffset] - P_[C_],
71
+ uy = P_[nextRingOffset + 1] - P_[C_ + 1],
72
+ uz = P_[nextRingOffset + 2] - P_[C_ + 2];
73
73
  const nx = ty * uz - tz * uy,
74
74
  ny = tz * ux - tx * uz,
75
75
  nz = tx * uy - ty * ux,
@@ -79,47 +79,47 @@ export function createSculptor({ vertexTag } = {}) {
79
79
  N_[q + 1] = (ny / l) * s;
80
80
  N_[q + 2] = (nz / l) * s;
81
81
  }
82
- if (o.relief) raise(rings, ns, closed, a0, (a1 - a0) / seg, idx, o.relief);
83
- const tag = gb.tv,
84
- sk = o.sk,
82
+ if (options.relief) raise(rings, ns, closed, a0, (a1 - a0) / segments, vertexOffset, options.relief);
83
+ const tag = builder.tv,
84
+ sk = options.sk,
85
85
  V = (i, j) => {
86
- const q = idx(i, j),
86
+ const q = vertexOffset(i, j),
87
87
  r = rings[i],
88
- c = r[5],
88
+ ringColor = r[5],
89
89
  t = tint ? T_[q / 3] : 1,
90
- a = a0 + ((a1 - a0) * j) / seg;
91
- if (vertexTag) gb.tv = vertexTag(gb, r, a, o, tag);
92
- const w = sk ? sk(r[0], a, r) : null;
93
- gb.raw(
90
+ angle = a0 + ((a1 - a0) * j) / segments;
91
+ if (vertexTag) builder.tv = vertexTag(builder, r, angle, options, tag);
92
+ const w = sk ? sk(r[0], angle, r) : null;
93
+ builder.raw(
94
94
  P_[q],
95
95
  P_[q + 1],
96
96
  P_[q + 2],
97
97
  N_[q],
98
98
  N_[q + 1],
99
99
  N_[q + 2],
100
- c[0] * t,
101
- c[1] * t,
102
- c[2] * t,
100
+ ringColor[0] * t,
101
+ ringColor[1] * t,
102
+ ringColor[2] * t,
103
103
  w ? w[0] : r[6] || 0,
104
104
  w ? w[1] : (r[7] ?? (r[6] || 0)),
105
105
  w ? w[2] : (r[8] ?? 1),
106
106
  );
107
107
  };
108
- if (o.joined) {
108
+ if (options.joined) {
109
109
  // each point once, and the triangles through them by number
110
- const first = gb.count / 3,
110
+ const first = builder.count / 3,
111
111
  corners = [],
112
112
  at = (i, j) => i * ns + (closed ? j % ns : j);
113
- for (let i = 0; i < nr; i++) for (let j = 0; j < ns; j++) V(i, j);
114
- for (let i = 0; i < nr - 1; i++)
115
- for (let j = 0; j < seg; j++) {
113
+ for (let i = 0; i < ringCount; i++) for (let j = 0; j < ns; j++) V(i, j);
114
+ for (let i = 0; i < ringCount - 1; i++)
115
+ for (let j = 0; j < segments; j++) {
116
116
  if (up) corners.push(at(i, j), at(i + 1, j), at(i + 1, j + 1), at(i, j), at(i + 1, j + 1), at(i, j + 1));
117
117
  else corners.push(at(i, j), at(i + 1, j + 1), at(i + 1, j), at(i, j), at(i, j + 1), at(i + 1, j + 1));
118
118
  }
119
- gb.triangles(first, corners);
119
+ builder.triangles(first, corners);
120
120
  } else
121
- for (let i = 0; i < nr - 1; i++)
122
- for (let j = 0; j < seg; j++) {
121
+ for (let i = 0; i < ringCount - 1; i++)
122
+ for (let j = 0; j < segments; j++) {
123
123
  if (up) {
124
124
  V(i, j);
125
125
  V(i + 1, j);
@@ -136,38 +136,38 @@ export function createSculptor({ vertexTag } = {}) {
136
136
  V(i + 1, j + 1);
137
137
  }
138
138
  }
139
- if (vertexTag) gb.tv = tag;
139
+ if (vertexTag) builder.tv = tag;
140
140
  }
141
141
  // Moves a loft's points (_lp, with their normals _ln) out by its relief and turns each normal down the relief's slopes: along the
142
142
  // ring (the slope for each radian, over the metres a radian is there) and from ring to ring (the slope for each metre of
143
143
  // height, over the metres of surface a metre of height is there).
144
- function raise(rings, ns, closed, a0, step, idx, relief) {
145
- const nr = rings.length,
144
+ function raise(rings, ns, closed, a0, step, vertexOffset, relief) {
145
+ const ringCount = rings.length,
146
146
  P_ = _lp,
147
147
  N_ = _ln,
148
148
  [dy, da] = RELIEF_STEP;
149
- for (let i = 0; i < nr; i++) {
149
+ for (let i = 0; i < ringCount; i++) {
150
150
  const r = rings[i],
151
151
  y = r[0],
152
152
  low = Math.max(0, i - 1),
153
- high = Math.min(nr - 1, i + 1),
153
+ high = Math.min(ringCount - 1, i + 1),
154
154
  rise = rings[high][0] - rings[low][0];
155
155
  for (let j = 0; j < ns; j++) {
156
- const a = a0 + step * j,
156
+ const angle = a0 + step * j,
157
157
  q = (i * ns + j) * 3,
158
- A = idx(i, j - 1),
159
- B_ = idx(i, j + 1),
160
- C_ = idx(low, j),
161
- D = idx(high, j),
162
- out = relief(y, a, r),
163
- round = (relief(y, a + da, r) - relief(y, a - da, r)) / (2 * da),
164
- up = (relief(y + dy, a, r) - relief(y - dy, a, r)) / (2 * dy),
165
- tx = P_[B_] - P_[A],
166
- ty = P_[B_ + 1] - P_[A + 1],
167
- tz = P_[B_ + 2] - P_[A + 2],
168
- ux = P_[D] - P_[C_],
169
- uy = P_[D + 1] - P_[C_ + 1],
170
- uz = P_[D + 2] - P_[C_ + 2],
158
+ previousSideOffset = vertexOffset(i, j - 1),
159
+ B_ = vertexOffset(i, j + 1),
160
+ C_ = vertexOffset(low, j),
161
+ nextRingOffset = vertexOffset(high, j),
162
+ out = relief(y, angle, r),
163
+ round = (relief(y, angle + da, r) - relief(y, angle - da, r)) / (2 * da),
164
+ up = (relief(y + dy, angle, r) - relief(y - dy, angle, r)) / (2 * dy),
165
+ tx = P_[B_] - P_[previousSideOffset],
166
+ ty = P_[B_ + 1] - P_[previousSideOffset + 1],
167
+ tz = P_[B_ + 2] - P_[previousSideOffset + 2],
168
+ ux = P_[nextRingOffset] - P_[C_],
169
+ uy = P_[nextRingOffset + 1] - P_[C_ + 1],
170
+ uz = P_[nextRingOffset + 2] - P_[C_ + 2],
171
171
  turn = (closed ? 2 : Math.min(ns - 1, j + 1) - Math.max(0, j - 1)) * step, // radians between the two neighbours (one step at an open sheet's edge)
172
172
  kt = (round * turn) / (tx * tx + ty * ty + tz * tz || 1),
173
173
  ku = (up * rise) / (ux * ux + uy * uy + uz * uz || 1),
@@ -183,14 +183,14 @@ export function createSculptor({ vertexTag } = {}) {
183
183
  _lm[q + 2] = nz / l;
184
184
  }
185
185
  }
186
- for (let k = nr * ns * 3 - 1; k >= 0; k--) {
186
+ for (let k = ringCount * ns * 3 - 1; k >= 0; k--) {
187
187
  P_[k] = _lq[k];
188
188
  N_[k] = _lm[k];
189
189
  }
190
190
  }
191
191
  const meshes = new Map();
192
- // A sphere pushed into shape by fn(dir, outPos, channels). Cached by key; channels are a colour mix and a brightness.
193
- function sculptMesh(key, detail, fn, warp = 0) {
192
+ // A sphere pushed into shape by samplePoint(direction, outPos, channels). Cached by key; channels are a colour mix and a brightness.
193
+ function sculptMesh(key, detail, samplePoint, warp = 0) {
194
194
  if (LOD_LO > 1) {
195
195
  key += ':light';
196
196
  detail = Math.max(2, (detail + 1) >> 2);
@@ -203,10 +203,10 @@ export function createSculptor({ vertexTag } = {}) {
203
203
  n = pa.length / 3;
204
204
  const P_ = new Float32Array(n * 3),
205
205
  N_ = new Float32Array(n * 3),
206
- A = new Float32Array(n),
206
+ tintWeights = new Float32Array(n),
207
207
  Bc = new Float32Array(n),
208
- D = new Float32Array(n * 3);
209
- const d = new THREE.Vector3(),
208
+ directions = new Float32Array(n * 3);
209
+ const direction = new THREE.Vector3(),
210
210
  t1 = new THREE.Vector3(),
211
211
  t2 = new THREE.Vector3(),
212
212
  p0 = new THREE.Vector3(),
@@ -216,31 +216,31 @@ export function createSculptor({ vertexTag } = {}) {
216
216
  c2 = [0, 1],
217
217
  e = 0.02;
218
218
  for (let i = 0; i < n; i++) {
219
- d.set(pa[i * 3], pa[i * 3 + 1], pa[i * 3 + 2] + warp).normalize();
220
- D[i * 3] = d.x;
221
- D[i * 3 + 1] = d.y;
222
- D[i * 3 + 2] = d.z;
223
- fn(d, p0, ch);
224
- t1.set(d.z, 0, -d.x);
219
+ direction.set(pa[i * 3], pa[i * 3 + 1], pa[i * 3 + 2] + warp).normalize();
220
+ directions[i * 3] = direction.x;
221
+ directions[i * 3 + 1] = direction.y;
222
+ directions[i * 3 + 2] = direction.z;
223
+ samplePoint(direction, p0, ch);
224
+ t1.set(direction.z, 0, -direction.x);
225
225
  if (t1.lengthSq() < 1e-4) t1.set(1, 0, 0);
226
226
  t1.normalize();
227
- t2.crossVectors(d, t1);
228
- fn(t1.multiplyScalar(e).add(d).normalize(), p1, c2);
229
- fn(t2.multiplyScalar(e).add(d).normalize(), p2, c2);
227
+ t2.crossVectors(direction, t1);
228
+ samplePoint(t1.multiplyScalar(e).add(direction).normalize(), p1, c2);
229
+ samplePoint(t2.multiplyScalar(e).add(direction).normalize(), p2, c2);
230
230
  p1.sub(p0);
231
231
  p2.sub(p0);
232
232
  t1.crossVectors(p1, p2).normalize();
233
- if (t1.dot(d) < 0) t1.negate();
233
+ if (t1.dot(direction) < 0) t1.negate();
234
234
  P_[i * 3] = p0.x;
235
235
  P_[i * 3 + 1] = p0.y;
236
236
  P_[i * 3 + 2] = p0.z;
237
237
  N_[i * 3] = t1.x;
238
238
  N_[i * 3 + 1] = t1.y;
239
239
  N_[i * 3 + 2] = t1.z;
240
- A[i] = ch[0];
240
+ tintWeights[i] = ch[0];
241
241
  Bc[i] = ch[1];
242
242
  }
243
- const mesh = { P: P_, N: N_, A, B: Bc, D, n };
243
+ const mesh = { P: P_, N: N_, A: tintWeights, B: Bc, D: directions, n };
244
244
  meshes.set(key, mesh);
245
245
  return mesh;
246
246
  }
@@ -251,13 +251,13 @@ const _e1 = new THREE.Vector3(),
251
251
  _e2 = new THREE.Vector3();
252
252
  // One triangle of points [x, y, z, nx, ny, nz, own colour, own weight], wound so that it faces the way its normals do. A point
253
253
  // without a colour or a weight of its own takes col and weight.
254
- export function triangle(gb, p, q, r, col, bone, other = bone, weight = 1) {
255
- _e1.set(q[0] - p[0], q[1] - p[1], q[2] - p[2]);
256
- _e2.set(r[0] - p[0], r[1] - p[1], r[2] - p[2]);
254
+ export function triangle(builder, firstPoint, secondPoint, thirdPoint, col, bone, other = bone, weight = 1) {
255
+ _e1.set(secondPoint[0] - firstPoint[0], secondPoint[1] - firstPoint[1], secondPoint[2] - firstPoint[2]);
256
+ _e2.set(thirdPoint[0] - firstPoint[0], thirdPoint[1] - firstPoint[1], thirdPoint[2] - firstPoint[2]);
257
257
  _e1.cross(_e2);
258
- const flip = _e1.x * (p[3] + q[3] + r[3]) + _e1.y * (p[4] + q[4] + r[4]) + _e1.z * (p[5] + q[5] + r[5]) < 0;
259
- for (const v of flip ? [p, r, q] : [p, q, r]) {
260
- const c = v[6] || col;
261
- gb.raw(v[0], v[1], v[2], v[3], v[4], v[5], c[0], c[1], c[2], bone, other, v[7] ?? weight);
258
+ const flip = _e1.x * (firstPoint[3] + secondPoint[3] + thirdPoint[3]) + _e1.y * (firstPoint[4] + secondPoint[4] + thirdPoint[4]) + _e1.z * (firstPoint[5] + secondPoint[5] + thirdPoint[5]) < 0;
259
+ for (const v of flip ? [firstPoint, thirdPoint, secondPoint] : [firstPoint, secondPoint, thirdPoint]) {
260
+ const vertexColor = v[6] || col;
261
+ builder.raw(v[0], v[1], v[2], v[3], v[4], v[5], vertexColor[0], vertexColor[1], vertexColor[2], bone, other, v[7] ?? weight);
262
262
  }
263
263
  }