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
@@ -22,7 +22,7 @@ const MM = 0.001,
22
22
  SCAR_SHADE = 0.14, // how much a millimetre of a scar's seam darkens the skin in it (a crease holds shadow)
23
23
  WOUND_DEEP = 4.5, // millimetres a fresh wound's torn flesh lies below the skin round it,
24
24
  WOUND_SWELL = 2.2, // and how far the bruised skin at its edges swells;
25
- HEALED_DEEP = 2.4, // once healed (H.healed), how far the scar lies sunk where the flesh was torn, and how high its edges and
25
+ HEALED_DEEP = 2.4, // once healed (faceFeatures.healed), how far the scar lies sunk where the flesh was torn, and how high its edges and
26
26
  HEALED_RIDGE = 0.9, // the cords of scar tissue across it stand
27
27
  // Where a wound has torn the nose away, the opening into the skull's nose left in its place: a pear-shaped hollow down the
28
28
  // middle of the face (from the top of its rows to its foot, millimetres up the face), its half width at the foot, how deep it
@@ -35,19 +35,19 @@ const MM = 0.001,
35
35
  // worked out: a head asks for these some hundred thousand times.
36
36
  const FAR = 3.2;
37
37
  function bump(v, at, wide) {
38
- const d = (v - at) / wide;
39
- return d > FAR || d < -FAR ? 0 : Math.exp(-d * d);
38
+ const scaledOffset = (v - at) / wide;
39
+ return scaledOffset > FAR || scaledOffset < -FAR ? 0 : Math.exp(-scaledOffset * scaledOffset);
40
40
  }
41
41
  function blob(x, y, ax, ay, wx, wy) {
42
42
  const dx = (x - ax) / wx,
43
43
  dy = (y - ay) / wy,
44
- d = dx * dx + dy * dy;
45
- return d > FAR * FAR ? 0 : Math.exp(-d);
44
+ distanceSquared = dx * dx + dy * dy;
45
+ return distanceSquared > FAR * FAR ? 0 : Math.exp(-distanceSquared);
46
46
  }
47
47
  // The larger of two heights, rounded where they meet.
48
- function smoothMax(a, b, k) {
49
- const h = clamp(0.5 + (a - b) / (2 * k), 0, 1);
50
- return lerp(b, a, h) + k * h * (1 - h);
48
+ function smoothMax(firstValue, secondValue, blendWidth) {
49
+ const h = clamp(0.5 + (firstValue - secondValue) / (2 * blendWidth), 0, 1);
50
+ return lerp(secondValue, firstValue, h) + blendWidth * h * (1 - h);
51
51
  }
52
52
  // How far a point lies from the line a-b, to which side of it (positive to the outside of the face), and how far along.
53
53
  function fromLine(x, y, ax, ay, bx, by, out) {
@@ -62,27 +62,27 @@ function fromLine(x, y, ax, ay, bx, by, out) {
62
62
  const _slice = { x: 0, z: 0 },
63
63
  _line = [0, 0];
64
64
 
65
- // The surface of a head of shape H, as a function of a direction from its middle: fn(dir, outPoint, channels). Channels: lips,
65
+ // The surface of a head with the given features, as a function of a direction from its middle: sampleHead(direction, outPoint, channels). Channels: lips,
66
66
  // shade, warmth (cheeks, nose), where a beard grows on cheeks and jaw, brow, where a moustache grows, the chin's tuft, a scar. A fine head has open lids and a mouth that can open (its grid lays rows
67
67
  // along their edges); a plain one, for the far mesh and for coarse sculpts, has them closed and painted.
68
68
  function headFn(features = {}, fine = false) {
69
- const H = { jaw: 0.5, lips: 1, cheek: 1, mouthW: 1, brow: 1, socket: 1, hollow: 0, chin: 1, nose: 1, bridge: 0, ...features };
70
- const age = H.age || 0,
71
- fat = H.fat || 0,
72
- dead = H.dead || 0,
73
- jawWide = (H.jaw - 0.5) * 0.17,
74
- chinWide = ((H.chinW ?? 1) - 1) * 0.5,
75
- slope = H.slope ?? 1,
76
- lips = H.lips,
77
- noseLong = H.noseL ?? 1,
78
- noseWide = H.noseW ?? 1;
69
+ const faceFeatures = { jaw: 0.5, lips: 1, cheek: 1, mouthW: 1, brow: 1, socket: 1, hollow: 0, chin: 1, nose: 1, bridge: 0, ...features };
70
+ const age = faceFeatures.age || 0,
71
+ fat = faceFeatures.fat || 0,
72
+ dead = faceFeatures.dead || 0,
73
+ jawWide = (faceFeatures.jaw - 0.5) * 0.17,
74
+ chinWide = ((faceFeatures.chinW ?? 1) - 1) * 0.5,
75
+ slope = faceFeatures.slope ?? 1,
76
+ lips = faceFeatures.lips,
77
+ noseLong = faceFeatures.noseL ?? 1,
78
+ noseWide = faceFeatures.noseW ?? 1;
79
79
  const width = sampled(
80
80
  y =>
81
81
  WIDTH(y) *
82
82
  (1 +
83
83
  jawWide * bump(y, -84, 26) +
84
84
  chinWide * sstep(-88, -108, y) +
85
- (H.cheek - 1) * 0.05 * bump(y, -18, 16) +
85
+ (faceFeatures.cheek - 1) * 0.05 * bump(y, -18, 16) +
86
86
  fat * 0.1 * bump(y, -62, 36)),
87
87
  -HALF,
88
88
  HALF,
@@ -100,7 +100,7 @@ function headFn(features = {}, fine = false) {
100
100
  // the outline of the bare skull and jaw at a height, at an angle round from the front
101
101
  // (a grid asks for one height many times running, so the slice last asked for is kept)
102
102
  const kept = { y: NaN, square: 2, wide: 0, fore: 0, aft: 0, mid: 0 };
103
- const slice = (y, angle, o) => {
103
+ const slice = (y, angle, output) => {
104
104
  if (y !== kept.y) {
105
105
  const k = closing(y);
106
106
  kept.y = y;
@@ -112,24 +112,24 @@ function headFn(features = {}, fine = false) {
112
112
  }
113
113
  const n = kept.square,
114
114
  s = Math.sin(angle),
115
- c = Math.cos(angle),
116
- q = Math.pow(Math.pow(Math.abs(s), n) + Math.pow(Math.abs(c), n), -1 / n);
117
- o.x = kept.wide * s * q;
118
- o.z = kept.mid + (c > 0 ? kept.fore : kept.aft) * c * q;
115
+ cosAngle = Math.cos(angle),
116
+ q = Math.pow(Math.pow(Math.abs(s), n) + Math.pow(Math.abs(cosAngle), n), -1 / n);
117
+ output.x = kept.wide * s * q;
118
+ output.z = kept.mid + (cosAngle > 0 ? kept.fore : kept.aft) * cosAngle * q;
119
119
  };
120
120
 
121
121
  // the eyes: where each ball sits, and the edges of its lids from the inner corner (0) to the outer (1)
122
- const eyeX = 31.5 * (H.eyeGap ?? 1),
122
+ const eyeX = 31.5 * (faceFeatures.eyeGap ?? 1),
123
123
  eyeY = -1,
124
124
  ballR = 12.2,
125
125
  lidR = ballR + 1.3,
126
126
  innerX = eyeX - 14.5,
127
127
  outerX = eyeX + 14,
128
- one = !!H.oneEye, // the two eyes healed over, and one in the middle of the brow
129
- opening = one ? 0 : (H.eye ?? 1),
130
- tilt = H.tilt ?? 0;
128
+ one = !!faceFeatures.oneEye, // the two eyes healed over, and one in the middle of the brow
129
+ opening = one ? 0 : (faceFeatures.eye ?? 1),
130
+ tilt = faceFeatures.tilt ?? 0;
131
131
  slice(eyeY, Math.asin(eyeX / width(eyeY)) * 0.93, _slice);
132
- const eyeZ = _slice.z - 14.4 - (H.deep ?? 0);
132
+ const eyeZ = _slice.z - 14.4 - (faceFeatures.deep ?? 0);
133
133
  slice(ONE_EYE.y, 0, _slice);
134
134
  const oneZ = _slice.z + ONE_EYE.out - ONE_EYE.r, // where the middle of the one eye's ball is
135
135
  oneLid = ONE_EYE.r + 1.3;
@@ -137,16 +137,16 @@ function headFn(features = {}, fine = false) {
137
137
  upperLid = t => corner(t) + 5.4 * opening * Math.pow(4 * Math.pow(t, 0.85) * (1 - Math.pow(t, 0.85)), 0.72),
138
138
  lowerLid = t => corner(t) - 3.9 * opening * Math.pow(4 * Math.pow(t, 1.15) * (1 - Math.pow(t, 1.15)), 0.8);
139
139
  // a row of the grid is bent so that the two lid rows run along the lids' edges; away from the eye it lies level
140
- const lidEdges = (ax, o) => {
140
+ const lidEdges = (ax, output) => {
141
141
  const t = (ax - innerX) / (outerX - innerX);
142
142
  if (t > 0 && t < 1) {
143
- o[0] = upperLid(t);
144
- o[1] = lowerLid(t);
143
+ output[0] = upperLid(t);
144
+ output[1] = lowerLid(t);
145
145
  } else {
146
146
  const c = corner(clamp(t, 0, 1)),
147
147
  e = sstep(0, 7, t < 0 ? innerX - ax : ax - outerX);
148
- o[0] = c + (LID_TOP - c) * e;
149
- o[1] = c + (LID_LOW - c) * e;
148
+ output[0] = c + (LID_TOP - c) * e;
149
+ output[1] = c + (LID_LOW - c) * e;
150
150
  }
151
151
  return t;
152
152
  };
@@ -158,11 +158,11 @@ function headFn(features = {}, fine = false) {
158
158
  : low - ((LID_LOW - row) * (low - LID_BELOW)) / (LID_LOW - LID_BELOW);
159
159
 
160
160
  // An old scar at a point of the face (millimetres; x to the person's left), as how far the skin is sunk there and how pale it
161
- // is: H.scar is the seam's two ends, its width (millimetres) and how far its middle bows down from the straight, like a smile.
161
+ // is: faceFeatures.scar is the seam's two ends, its width (millimetres) and how far its middle bows down from the straight, like a smile.
162
162
  // A wider one is deeper and paler.
163
163
  const _scar = [0, 0],
164
164
  scarAt = (x, y) => {
165
- const [ax0, ay0, bx, by, wide = SCAR_WIDE, bow = 0] = H.scar,
165
+ const [ax0, ay0, bx, by, wide = SCAR_WIDE, bow = 0] = faceFeatures.scar,
166
166
  k = Math.sqrt(wide / SCAR_WIDE);
167
167
  fromLine(x, y, ax0, ay0, bx, by, _line);
168
168
  const t = _line[1],
@@ -172,19 +172,19 @@ function headFn(features = {}, fine = false) {
172
172
  _scar[1] = k * ends * bump(d, 0, wide);
173
173
  return _scar;
174
174
  };
175
- // A fresh wound (H.wound: as a scar, its two ends, width and bow): a broad band of torn flesh, sunk and ragged, the skin
175
+ // A fresh wound (faceFeatures.wound: as a scar, its two ends, width and bow): a broad band of torn flesh, sunk and ragged, the skin
176
176
  // swollen along its edges; as how deep it lies at a point and how much of it there is (0 to 1). Healed, the band has closed
177
177
  // into a scar: shallower, the swelling gone, its edges and cords of scar tissue across it standing proud.
178
178
  const _wound = [0, 0],
179
179
  woundAt = (x, y) => {
180
- const [ax0, ay0, bx, by, wide, bow = 0] = H.wound;
180
+ const [ax0, ay0, bx, by, wide, bow = 0] = faceFeatures.wound;
181
181
  fromLine(x, y, ax0, ay0, bx, by, _line);
182
182
  const t = _line[1],
183
183
  ragged = 0.22 * wide * (Math.sin(t * 23 + x * 0.31) * 0.6 + Math.sin(t * 41 - y * 0.27) * 0.4),
184
184
  d = Math.abs(_line[0] + bow * 4 * t * (1 - t)) - ragged,
185
185
  ends = sstep(-0.06, 0.08, t) * sstep(1.06, 0.92, t),
186
186
  torn = ends * sstep(wide, wide * 0.7, d);
187
- _wound[0] = H.healed
187
+ _wound[0] = faceFeatures.healed
188
188
  ? torn * HEALED_DEEP * (0.75 + 0.25 * Math.sin(x * 0.9) * Math.sin(y * 1.1)) -
189
189
  HEALED_RIDGE * (ends * bump(d, wide * 0.85, wide * 0.18) + torn * Math.max(0, Math.sin(t * 29 + x * 0.2)) ** 3)
190
190
  : torn * WOUND_DEEP * (0.8 + 0.2 * Math.sin(x * 0.9) * Math.sin(y * 1.1)) - ends * WOUND_SWELL * bump(d, wide * 1.15, wide * 0.4);
@@ -192,33 +192,33 @@ function headFn(features = {}, fine = false) {
192
192
  return _wound;
193
193
  };
194
194
  // an eye the wound crosses is burst and swollen shut
195
- const woundShut = H.wound ? [woundAt(-eyeX, eyeY)[1], woundAt(eyeX, eyeY)[1]] : null;
195
+ const woundShut = faceFeatures.wound ? [woundAt(-eyeX, eyeY)[1], woundAt(eyeX, eyeY)[1]] : null;
196
196
  // a wound across the middle of the nose takes all of it below the bone, tip and wings, not only the band it crosses
197
- const noseLost = H.wound ? Math.max(woundAt(0, -12)[1], woundAt(0, -22)[1], woundAt(0, -32)[1]) : 0;
198
- const mouthX = 24.5 * H.mouthW,
197
+ const noseLost = faceFeatures.wound ? Math.max(woundAt(0, -12)[1], woundAt(0, -22)[1], woundAt(0, -32)[1]) : 0;
198
+ const mouthX = 24.5 * faceFeatures.mouthW,
199
199
  browLine = ax => 19 + 5 * Math.sin(Math.PI * clamp(ax / 56, 0, 1)) - 2.5 * sstep(40, 60, ax),
200
- foldDeep = (0.55 + 1.5 * age + fat) * (H.fold ?? 1),
200
+ foldDeep = (0.55 + 1.5 * age + fat) * (faceFeatures.fold ?? 1),
201
201
  alaX = 11.8 * noseWide;
202
202
  // the soft forms of the face without the nose, lids and lips: brow ridge, sockets, cheekbones, the muzzle of the mouth, chin
203
203
  const soft = (ax, y) => {
204
204
  const line = browLine(ax),
205
- ridge = (1 + 2.8 * bump(ax, 17, 14)) * H.brow * sstep(64, 42, ax) * (y > line ? bump(y, line, 8) : bump(y, line, 4.2)),
206
- socket = H.socket * (4 * blob(ax, y, 30, 5, 17, 11) + 2.6 * blob(ax, y, 17.5, 1, 7, 9));
205
+ ridge = (1 + 2.8 * bump(ax, 17, 14)) * faceFeatures.brow * sstep(64, 42, ax) * (y > line ? bump(y, line, 8) : bump(y, line, 4.2)),
206
+ socket = faceFeatures.socket * (4 * blob(ax, y, 30, 5, 17, 11) + 2.6 * blob(ax, y, 17.5, 1, 7, 9));
207
207
  return (
208
208
  ridge -
209
209
  socket +
210
- 2.6 * H.cheek * blob(ax, y, 46, -16, 14, 11) -
210
+ 2.6 * faceFeatures.cheek * blob(ax, y, 46, -16, 14, 11) -
211
211
  (0.5 + 1.2 * age) * blob(ax, y, 26, -15, 9, 5) -
212
- (3 * H.hollow + 2 * age) * blob(ax, y, 43, -55, 13, 16) +
212
+ (3 * faceFeatures.hollow + 2 * age) * blob(ax, y, 43, -55, 13, 16) +
213
213
  3.4 * blob(ax, y, 0, -63, 26, 19) +
214
214
  3 * fat * blob(ax, y, 38, -52, 18, 22) +
215
215
  2.2 * (age + fat) * blob(ax, y, 36, -91, 10, 9) -
216
216
  3 * blob(ax, y, 0, -83.5, 17, 4.6) +
217
- 3.6 * H.chin * blob(ax, y, 0, -100, 15 * (H.chinW ?? 1), 10)
217
+ 3.6 * faceFeatures.chin * blob(ax, y, 0, -100, 15 * (faceFeatures.chinW ?? 1), 10)
218
218
  );
219
219
  };
220
220
 
221
- const point = (row, angle, o, ch) => {
221
+ const point = (row, angle, output, ch) => {
222
222
  slice(row, angle, _slice);
223
223
  const side = _slice.x < 0 ? -1 : 1,
224
224
  cosA = Math.cos(angle),
@@ -249,7 +249,7 @@ function headFn(features = {}, fine = false) {
249
249
  if (face > 0) {
250
250
  out = soft(ax, y);
251
251
  shade -=
252
- 0.15 * clamp((H.socket * blob(ax, y, 30, 5, 17, 11)) / 1.2, 0, 1) +
252
+ 0.15 * clamp((faceFeatures.socket * blob(ax, y, 30, 5, 17, 11)) / 1.2, 0, 1) +
253
253
  0.1 * bump(y, browLine(ax) - 8, 4.5) * sstep(50, 36, ax) * sstep(6, 14, ax);
254
254
  if (age > 0) {
255
255
  // the lines of age: furrows across the brow (an aged head's grid has rows for them), the frown between the brows,
@@ -267,18 +267,18 @@ function headFn(features = {}, fine = false) {
267
267
  out -= age * (0.5 * furrow + 0.7 * frown + 0.3 * fan);
268
268
  shade -= age * (0.17 * furrow + 0.16 * frown + 0.12 * fan);
269
269
  }
270
- if (H.scar) {
270
+ if (faceFeatures.scar) {
271
271
  // an old wound the skin has closed over: a pale band with a sunken seam down its middle, puckered a little either side
272
272
  const [deep, pale] = scarAt(side * ax, y);
273
273
  scar = pale;
274
274
  out -= deep;
275
275
  shade -= SCAR_SHADE * Math.max(0, deep);
276
276
  }
277
- if (H.wound) {
277
+ if (faceFeatures.wound) {
278
278
  const [deep, torn] = woundAt(side * ax, y);
279
279
  raw = torn;
280
280
  out -= deep;
281
- shade -= (H.healed ? 0.12 : 0.3) * torn * (0.6 + 0.4 * Math.sin(ax * 0.7 + y * 0.9)); // the torn flesh holds shadow in its pits
281
+ shade -= (faceFeatures.healed ? 0.12 : 0.3) * torn * (0.6 + 0.4 * Math.sin(ax * 0.7 + y * 0.9)); // the torn flesh holds shadow in its pits
282
282
  }
283
283
  // the lids over the ball of the eye, with the fold above the upper one and, with age, the bag under the lower
284
284
  const ex = ax - eyeX,
@@ -327,13 +327,13 @@ function headFn(features = {}, fine = false) {
327
327
  // the nose: a ridge down the middle of the face to a rounded tip, wings beside it and the nostrils beneath
328
328
  const noseY = 12 + (y - 12) / noseLong;
329
329
  if (noseY < 17 && noseY > -52) {
330
- const reach = NOSE_OUT(noseY) * (1 + (H.nose - 1) * sstep(8, -6, noseY)) + 2.2 * H.bridge * bump(noseY, -8, 11),
330
+ const reach = NOSE_OUT(noseY) * (1 + (faceFeatures.nose - 1) * sstep(8, -6, noseY)) + 2.2 * faceFeatures.bridge * bump(noseY, -8, 11),
331
331
  t = ax / (NOSE_WIDE(noseY) * (1 + (noseWide - 1) * sstep(-5, -35, noseY))),
332
332
  tip = bump(noseY, -36, 7);
333
333
  const torn = 1 - Math.max(0.85 * raw, noseLost * sstep(-6, -16, noseY)); // what a wound has torn away of the nose
334
334
  if (t < 1) out += reach * lerp(1 - sstep(0.22, 1, t), Math.pow(1 - t * t, 0.75), 0.75 * tip) * torn;
335
335
  const wr = ((ax - alaX) / (6.4 * noseWide)) ** 2 + ((noseY + 41.5) / 6.4) ** 2;
336
- if (wr < 1) out = smoothMax(out, soft(ax, y) + 7.2 * H.nose * Math.pow(1 - wr, 0.7) * torn, 1.2);
336
+ if (wr < 1) out = smoothMax(out, soft(ax, y) + 7.2 * faceFeatures.nose * Math.pow(1 - wr, 0.7) * torn, 1.2);
337
337
  const nostril = ((ax - 6.6 * noseWide) / (4.2 * noseWide)) ** 2 + ((noseY + 45.4) / 2.2) ** 2;
338
338
  if (nostril < 1.6) {
339
339
  out -= (fine ? 2.6 : 0.6) * sstep(1.6, 0.4, nostril);
@@ -345,8 +345,8 @@ function headFn(features = {}, fine = false) {
345
345
  const { top, foot, wide } = NOSE_HOLE,
346
346
  half = wide * (0.35 + 0.65 * sstep(top, foot + 6, noseY)) * noseWide,
347
347
  hole = noseLost * sstep(top + 2, top - 4, noseY) * sstep(foot - 2, foot + 3, noseY) * sstep(1, 0.55, ax / half);
348
- out -= hole * (H.healed ? NOSE_HOLE.healedDeep : NOSE_HOLE.deep);
349
- shade *= 1 - hole * (H.healed ? NOSE_HOLE.healedDark : NOSE_HOLE.dark);
348
+ out -= hole * (faceFeatures.healed ? NOSE_HOLE.healedDeep : NOSE_HOLE.deep);
349
+ shade *= 1 - hole * (faceFeatures.healed ? NOSE_HOLE.healedDark : NOSE_HOLE.dark);
350
350
  raw = Math.max(raw, hole); // coloured as the wound round it is, raw or scarred
351
351
  }
352
352
  warm += 0.5 * tip * bump(ax, 0, 13);
@@ -418,17 +418,17 @@ function headFn(features = {}, fine = false) {
418
418
  flank =
419
419
  1 +
420
420
  (-2.8 * (1 + 0.8 * age) * bump(y, 22, 17) * bump(turn, 1.05, 0.33) +
421
- 1.6 * H.cheek * bump(y, -14, 8) * bump(turn, 1.2, 0.35) +
422
- (0.6 + 1.2 * H.jaw) * bump(y, -62, 18) * bump(turn, 1.25, 0.3)) /
421
+ 1.6 * faceFeatures.cheek * bump(y, -14, 8) * bump(turn, 1.2, 0.35) +
422
+ (0.6 + 1.2 * faceFeatures.jaw) * bump(y, -62, 18) * bump(turn, 1.25, 0.3)) /
423
423
  75;
424
424
  if (y < -84) {
425
425
  shade -= 0.2 * sstep(0.25, -0.35, cosA) * sstep(-84, -98, y); // under the jaw
426
426
  if (!dead) beard = Math.max(beard, sstep(-84, -93, y));
427
427
  }
428
428
  const mid = MIDDLE(y);
429
- o.x = side * ax * flank * MM;
430
- o.y = y * MM;
431
- o.z = (mid + (_slice.z - mid) * flank + out) * MM;
429
+ output.x = side * ax * flank * MM;
430
+ output.y = y * MM;
431
+ output.z = (mid + (_slice.z - mid) * flank + out) * MM;
432
432
  if (ch) {
433
433
  ch[0] = lip;
434
434
  ch[1] = Math.max(0.04, shade);
@@ -441,16 +441,16 @@ function headFn(features = {}, fine = false) {
441
441
  }
442
442
  };
443
443
 
444
- const fn = (d, o, ch) => point(d.y * HALF, Math.atan2(d.x, d.z), o, ch);
445
- fn.point = point; // by a row of the grid (millimetres up from the middle) and an angle round from the front
444
+ const sampleHead = (d, output, ch) => point(d.y * HALF, Math.atan2(d.x, d.z), output, ch);
445
+ sampleHead.point = point; // by a row of the grid (millimetres up from the middle) and an angle round from the front
446
446
  // the bare skull at a direction, for what sits over it (hair, hoods, helmets)
447
- fn.skull = (d, o) => {
447
+ sampleHead.skull = (d, output) => {
448
448
  const y = d.y * HALF;
449
449
  slice(y, Math.atan2(d.x, d.z), _slice);
450
- o.set(_slice.x * MM, y * MM, _slice.z * MM);
450
+ output.set(_slice.x * MM, y * MM, _slice.z * MM);
451
451
  };
452
452
  // how far out from the bare skull a point lies (metres from the head's middle): 1 on its surface, less within
453
- fn.reach = (px, py, pz) => {
453
+ sampleHead.reach = (px, py, pz) => {
454
454
  const up = py / MM,
455
455
  y = clamp(up, -HALF, HALF),
456
456
  n = SQUARE(y),
@@ -461,10 +461,10 @@ function headFn(features = {}, fine = false) {
461
461
  return across;
462
462
  };
463
463
  // the direction at which the bare skull lies under a point (metres from the head's middle), on the line from the middle
464
- // through it: what fn.skull turns into a point again. Hair that lies over the skull finds its place by it.
465
- fn.under = (px, py, pz, d) => {
464
+ // through it: what sampleHead.skull turns into a point again. Hair that lies over the skull finds its place by it.
465
+ sampleHead.under = (px, py, pz, d) => {
466
466
  for (let i = 0; i < UNDER_STEPS; i++) {
467
- const k = fn.reach(px, py, pz);
467
+ const k = sampleHead.reach(px, py, pz);
468
468
  px /= k;
469
469
  py /= k;
470
470
  pz /= k;
@@ -477,7 +477,7 @@ function headFn(features = {}, fine = false) {
477
477
  };
478
478
  const upAt = t => Math.asin(clamp((upperLid(t) - eyeY) / lidR, -0.95, 0.95)),
479
479
  lowAt = t => Math.asin(clamp((lowerLid(t) - eyeY) / lidR, -0.95, 0.95));
480
- fn.eye = {
480
+ sampleHead.eye = {
481
481
  x: eyeX * MM,
482
482
  y: eyeY * MM,
483
483
  z: eyeZ * MM,
@@ -489,11 +489,11 @@ function headFn(features = {}, fine = false) {
489
489
  t > 0 && t < 1 ? clamp((upAt(t) - lowAt(t) + 0.06 * Math.sqrt(Math.sin(Math.PI * t))) / (upAt(0.45) - lowAt(0.45) + 0.06), 0, 1) : 0, // the share of it at each point along the lid
490
490
  upper: t => upperLid(t) * MM,
491
491
  };
492
- fn.mouth = { x: mouthX * MM, y: SEAM * MM };
493
- fn.one = one ? { y: ONE_EYE.y * MM, z: oneZ * MM, r: ONE_EYE.r * MM, wide: ONE_EYE.wide * MM } : null; // the one eye's ball, if so
494
- fn.browLine = x => browLine(x / MM) * MM;
495
- fn.scar = (x, y) => (H.scar ? scarAt(x / MM, y / MM)[1] : 0); // how much of a scar there is at a point (metres)
496
- return fn;
492
+ sampleHead.mouth = { x: mouthX * MM, y: SEAM * MM };
493
+ sampleHead.one = one ? { y: ONE_EYE.y * MM, z: oneZ * MM, r: ONE_EYE.r * MM, wide: ONE_EYE.wide * MM } : null; // the one eye's ball, if so
494
+ sampleHead.browLine = x => browLine(x / MM) * MM;
495
+ sampleHead.scar = (x, y) => (faceFeatures.scar ? scarAt(x / MM, y / MM)[1] : 0); // how much of a scar there is at a point (metres)
496
+ return sampleHead;
497
497
  }
498
498
 
499
499
  export { FURROWS, LID_ABOVE, LID_BELOW, LID_LOW, LID_TOP, SEAM, headFn };
@@ -1,5 +1,14 @@
1
- // Sample the triangles of a regular, axis-aligned X/Z PlaneGeometry in geometry coordinates.
2
- // The first vertex is the minimum X and Z; the index follows Three.js PlaneGeometry's diagonal.
1
+ /**
2
+ * Sample and refine regular terrain grids in local X/Z coordinates. Heights follow the triangles used to render the surface.
3
+ * @module
4
+ */
5
+
6
+ /**
7
+ * Interpolate height on the same triangle that the renderer draws.
8
+ * @param {import('three').PlaneGeometry} geometry - Regular X/Z grid whose first vertex has the smallest X and Z.
9
+ * @param {(x: number, z: number) => number} [outside] - Fallback height beyond the grid.
10
+ * @returns {(x: number, z: number) => (number | undefined)} A sampler in local geometry coordinates. Outside the grid it returns the fallback height, or undefined.
11
+ */
3
12
  export function heightfieldSampler(geometry, outside) {
4
13
  const { widthSegments: columns, heightSegments: rows } = geometry.parameters;
5
14
  const positions = geometry.getAttribute('position');
@@ -9,21 +18,21 @@ export function heightfieldSampler(geometry, outside) {
9
18
  const depth = positions.getZ(rows * (columns + 1)) - z0;
10
19
  if (!(width > 0 && depth > 0)) throw new RangeError('Expected a regular heightfield in the X/Z plane');
11
20
  return (x, z) => {
12
- const fx = (x - x0) / width * columns;
13
- const fz = (z - z0) / depth * rows;
14
- if (fx < 0 || fz < 0 || fx > columns || fz > rows) return outside?.(x, z);
15
- const col = Math.min(Math.floor(fx), columns - 1);
16
- const row = Math.min(Math.floor(fz), rows - 1);
17
- const u = fx - col, v = fz - row;
18
- const k = row * (columns + 1) + col;
19
- const b = positions.getY(k + columns + 1);
20
- const d = positions.getY(k + 1);
21
- if (u + v <= 1) {
22
- const a = positions.getY(k);
23
- return a + (d - a) * u + (b - a) * v;
21
+ const gridX = (x - x0) / width * columns;
22
+ const gridZ = (z - z0) / depth * rows;
23
+ if (gridX < 0 || gridZ < 0 || gridX > columns || gridZ > rows) return outside?.(x, z);
24
+ const col = Math.min(Math.floor(gridX), columns - 1);
25
+ const row = Math.min(Math.floor(gridZ), rows - 1);
26
+ const columnFraction = gridX - col, rowFraction = gridZ - row;
27
+ const cornerIndex = row * (columns + 1) + col;
28
+ const farLeft = positions.getY(cornerIndex + columns + 1);
29
+ const nearRight = positions.getY(cornerIndex + 1);
30
+ if (columnFraction + rowFraction <= 1) {
31
+ const nearLeft = positions.getY(cornerIndex);
32
+ return nearLeft + (nearRight - nearLeft) * columnFraction + (farLeft - nearLeft) * rowFraction;
24
33
  }
25
- const c = positions.getY(k + columns + 2);
26
- return c + (b - c) * (1 - u) + (d - c) * (1 - v);
34
+ const farRight = positions.getY(cornerIndex + columns + 2);
35
+ return farRight + (farLeft - farRight) * (1 - columnFraction) + (nearRight - farRight) * (1 - rowFraction);
27
36
  };
28
37
  }
29
38
 
@@ -59,8 +68,8 @@ export function refineHeightfield(base, { x, z, cells, subdivisions, height, cre
59
68
  });
60
69
  const sample = heightfieldSampler(patch, coarse);
61
70
  // Keep the base vertex and index layout for other disjoint patches and external attributes.
62
- for (let r = row; r < row + cells; r++) {
63
- const start = (r * columns + col) * 6;
71
+ for (let patchRow = row; patchRow < row + cells; patchRow++) {
72
+ const start = (patchRow * columns + col) * 6;
64
73
  base.index.array.fill(0, start, start + cells * 6);
65
74
  }
66
75
  base.index.needsUpdate = true;
@@ -51,10 +51,10 @@ export function indexParts(parts) {
51
51
  return packed;
52
52
  }
53
53
 
54
- function sameVertex(attributes, a, b) {
54
+ function sameVertex(attributes, firstVertex, secondVertex) {
55
55
  for (let i = 0; i < attributes.length; i++) {
56
56
  const { source, stride } = attributes[i];
57
- for (let j = 0; j < stride; j++) if (source[a * stride + j] !== source[b * stride + j]) return false;
57
+ for (let j = 0; j < stride; j++) if (source[firstVertex * stride + j] !== source[secondVertex * stride + j]) return false;
58
58
  }
59
59
  return true;
60
60
  }
@@ -2,33 +2,33 @@
2
2
  // visible records occupy the front of the arrays. The original full bounds stay valid.
3
3
  export function createInstanceVisibility(mesh) {
4
4
  const slots = Uint32Array.from({ length: mesh.count }, (_, i) => i);
5
- const ids = slots.slice();
5
+ const logicalIds = slots.slice();
6
6
  const attributes = [mesh.instanceMatrix];
7
7
  if (mesh.instanceColor) attributes.push(mesh.instanceColor);
8
8
  if (!mesh.boundingSphere) mesh.computeBoundingSphere();
9
9
  return function show(id, visible) {
10
- if (!Number.isInteger(id) || id < 0 || id >= ids.length) throw new RangeError('Unknown instance id');
10
+ if (!Number.isInteger(id) || id < 0 || id >= logicalIds.length) throw new RangeError('Unknown instance id');
11
11
  const from = slots[id];
12
12
  if (from < mesh.count === visible) return;
13
13
  const to = visible ? mesh.count++ : --mesh.count;
14
14
  if (from === to) return;
15
- const other = ids[to];
16
- ids[from] = other;
15
+ const other = logicalIds[to];
16
+ logicalIds[from] = other;
17
17
  slots[other] = from;
18
- ids[to] = id;
18
+ logicalIds[to] = id;
19
19
  slots[id] = to;
20
20
  for (const attribute of attributes) {
21
21
  const { array, itemSize } = attribute;
22
- const a = from * itemSize,
23
- b = to * itemSize;
22
+ const sourceOffset = from * itemSize,
23
+ destinationOffset = to * itemSize;
24
24
  for (let i = 0; i < itemSize; i++) {
25
- const value = array[a + i];
26
- array[a + i] = array[b + i];
27
- array[b + i] = value;
25
+ const value = array[sourceOffset + i];
26
+ array[sourceOffset + i] = array[destinationOffset + i];
27
+ array[destinationOffset + i] = value;
28
28
  }
29
29
  // Include the hidden slot: it can become visible by growing count without another swap.
30
- const start = Math.min(a, b),
31
- end = Math.max(a, b) + itemSize;
30
+ const start = Math.min(sourceOffset, destinationOffset),
31
+ end = Math.max(sourceOffset, destinationOffset) + itemSize;
32
32
  const range = attribute.updateRanges[0];
33
33
  if (range) {
34
34
  const limit = Math.max(end, range.start + range.count);
@@ -7,14 +7,14 @@ import { markStill } from '../rendering/still-casters.js';
7
7
  // (instanceArrays, putInstance), and the instanced meshes the page draws from them (instancedMesh, instancedTwin).
8
8
  // ════════════════════════════════════════════════════════════════════════
9
9
 
10
- // The arrays of `n` copies: a matrix and a linear colour to each, filled by putInstance.
11
- const instanceArrays = n => ({ matrices: new Float32Array(n * 16), colours: new Float32Array(n * 3) });
12
- // Copy `i` of `arrays`: placed by `matrix` (TRS), tinted (r, g, b), as InstancedMesh.setMatrixAt and setColorAt would write them.
13
- function putInstance(arrays, i, matrix, r, g, b) {
14
- matrix.toArray(arrays.matrices, i * 16);
15
- arrays.colours[i * 3] = r;
16
- arrays.colours[i * 3 + 1] = g;
17
- arrays.colours[i * 3 + 2] = b;
10
+ // Allocate space for one matrix and one linear RGB colour per instance.
11
+ const instanceArrays = instanceCount => ({ matrices: new Float32Array(instanceCount * 16), colours: new Float32Array(instanceCount * 3) });
12
+ // Write the transform and linear RGB colour at instanceIndex in Three.js buffer order.
13
+ function putInstance(arrays, instanceIndex, matrix, red, green, blue) {
14
+ matrix.toArray(arrays.matrices, instanceIndex * 16);
15
+ arrays.colours[instanceIndex * 3] = red;
16
+ arrays.colours[instanceIndex * 3 + 1] = green;
17
+ arrays.colours[instanceIndex * 3 + 2] = blue;
18
18
  }
19
19
  // An instanced mesh of `geometry` drawn at the copies of `arrays`. It is made empty and then given them, so that it does not
20
20
  // first write a matrix of its own for every copy only to have it replaced.
@@ -46,15 +46,15 @@ class Copies {
46
46
  this.matrices = [];
47
47
  this.colours = [];
48
48
  }
49
- add(shape, col, m) {
49
+ add(shape, color, transform) {
50
50
  if (shape !== this.shape) throw new Error('Copies hold one shape');
51
- const e = m.elements;
52
- this.matrices.push(...e);
51
+ const elements = transform.elements;
52
+ this.matrices.push(...elements);
53
53
  const at = this.matrices.length - 4; // the translation, moved to the builder's origin
54
54
  this.matrices[at] += this.ox;
55
55
  this.matrices[at + 1] += this.oy;
56
56
  this.matrices[at + 2] += this.oz;
57
- this.colours.push(col[0], col[1], col[2]);
57
+ this.colours.push(color[0], color[1], color[2]);
58
58
  }
59
59
  // The instanced mesh of the copies under `parent`, standing still, casting a shadow unless told not to; the lists are let go.
60
60
  mesh(material, parent, shadow = true) {
@@ -82,9 +82,9 @@ export function partitionInstances(arrays, cellSize) {
82
82
  const { matrices, colours } = arrays;
83
83
  const cells = new Map();
84
84
  for (let i = 0; i < matrices.length / 16; i++) {
85
- const k = Math.floor(matrices[i*16+12] / cellSize) + ',' + Math.floor(matrices[i*16+14] / cellSize);
86
- let ids = cells.get(k);
87
- if (!ids) cells.set(k, ids = []);
85
+ const cellKey = Math.floor(matrices[i*16+12] / cellSize) + ',' + Math.floor(matrices[i*16+14] / cellSize);
86
+ let ids = cells.get(cellKey);
87
+ if (!ids) cells.set(cellKey, ids = []);
88
88
  ids.push(i);
89
89
  }
90
90
  if (cells.size < 2) return matrices.length ? [arrays] : [];
@@ -29,13 +29,13 @@ function polyGrid(poly, cell, pad) {
29
29
  // The signed distance anywhere, read from a grid of polyGrid's; beyond the box, the distance to the box.
30
30
  function gridField({ x0, z0, x1, z1, nx, nz, cell, pad, g }) {
31
31
  return (x, z) => {
32
- const fx = (x - x0) / cell,
33
- fz = (z - z0) / cell;
34
- if (fx < 0 || fz < 0 || fx > nx - 1 || fz > nz - 1) return pad + Math.hypot(Math.max(x0 - x, 0, x - x1), Math.max(z0 - z, 0, z - z1));
35
- const i = Math.min(nx - 2, fx | 0),
36
- j = Math.min(nz - 2, fz | 0),
37
- u = fx - i,
38
- v = fz - j,
32
+ const gridX = (x - x0) / cell,
33
+ gridZ = (z - z0) / cell;
34
+ if (gridX < 0 || gridZ < 0 || gridX > nx - 1 || gridZ > nz - 1) return pad + Math.hypot(Math.max(x0 - x, 0, x - x1), Math.max(z0 - z, 0, z - z1));
35
+ const i = Math.min(nx - 2, gridX | 0),
36
+ j = Math.min(nz - 2, gridZ | 0),
37
+ u = gridX - i,
38
+ v = gridZ - j,
39
39
  k = j * nx + i;
40
40
  return (g[k] * (1 - u) + g[k + 1] * u) * (1 - v) + (g[k + nx] * (1 - u) + g[k + nx + 1] * u) * v;
41
41
  };
@@ -8,10 +8,10 @@ export function joinParts(pieces) {
8
8
  for (const name in sample) {
9
9
  const size = vertices && sample[name].length / (sample.position.length / 3),
10
10
  array = (joined[name] = new sample[name].constructor(vertices * size));
11
- let at = 0;
11
+ let writeOffset = 0;
12
12
  for (const [parts, from, to] of pieces) {
13
- array.set(parts[name].subarray((from / 3) * size, (to / 3) * size), at);
14
- at += ((to - from) / 3) * size;
13
+ array.set(parts[name].subarray((from / 3) * size, (to / 3) * size), writeOffset);
14
+ writeOffset += ((to - from) / 3) * size;
15
15
  }
16
16
  }
17
17
  return joined;