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
@@ -21,26 +21,26 @@ export const WRIST = 0.8; // and the wrists
21
21
  const SPAN = 3.2; // a bird across the wings at size 1
22
22
 
23
23
  /** A bird's geometry from its look: position, aW (0 body, 1 arm, 2 hand), and for a coloured look, color and aUnder. */
24
- export function birdGeometry(L) {
25
- const P = [],
26
- W = [],
24
+ export function birdGeometry(birdStyle) {
25
+ const positions = [],
26
+ wingWeights = [],
27
27
  K = [],
28
28
  U = [], // the under face's colour and which way the triangle's front face looks (1 up, -1 down, 0 one colour on both faces)
29
- col = L.colours,
30
- tri = (a, b, c, w = 0, part = 'body') => {
31
- P.push(...a, ...b, ...c);
32
- W.push(w, w, w);
29
+ col = birdStyle.colours,
30
+ tri = (firstPoint, secondPoint, thirdPoint, w = 0, part = 'body') => {
31
+ positions.push(...firstPoint, ...secondPoint, ...thirdPoint);
32
+ wingWeights.push(w, w, w);
33
33
  if (!col) return;
34
- const under = L.under?.[part],
35
- up = Math.sign((b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]));
36
- for (const p of [a, b, c]) {
37
- const tip = part !== 'body' && part !== 'head' && Math.abs(p[0]) > L.tips;
34
+ const under = birdStyle.under?.[part],
35
+ up = Math.sign((secondPoint[2] - firstPoint[2]) * (thirdPoint[0] - firstPoint[0]) - (secondPoint[0] - firstPoint[0]) * (thirdPoint[2] - firstPoint[2]));
36
+ for (const p of [firstPoint, secondPoint, thirdPoint]) {
37
+ const tip = part !== 'body' && part !== 'head' && Math.abs(p[0]) > birdStyle.tips;
38
38
  K.push(...(tip ? col.tip : col[part]));
39
- if (L.under) U.push(...(under && !tip ? under : tip ? col.tip : col[part]), under && !tip ? up : 0);
39
+ if (birdStyle.under) U.push(...(under && !tip ? under : tip ? col.tip : col[part]), under && !tip ? up : 0);
40
40
  }
41
41
  };
42
42
  // the body: rings along z [z, half width, half height, centre y], from the beak's tip to the root of the tail
43
- const R = [
43
+ const bodyRings = [
44
44
  [1.08, 0, 0, 0.02],
45
45
  [0.86, 0.06, 0.06, 0.04],
46
46
  [0.68, 0.1, 0.1, 0.05],
@@ -51,63 +51,63 @@ export function birdGeometry(L) {
51
51
  ],
52
52
  AROUND = 6,
53
53
  at = (r, j) => {
54
- const a = (j / AROUND) * TAU;
55
- return [Math.cos(a) * r[1], r[3] + Math.sin(a) * r[2], r[0]];
54
+ const angle = (j / AROUND) * TAU;
55
+ return [Math.cos(angle) * r[1], r[3] + Math.sin(angle) * r[2], r[0]];
56
56
  };
57
- for (let i = 0; i < R.length - 1; i++)
57
+ for (let i = 0; i < bodyRings.length - 1; i++)
58
58
  for (let j = 0; j < AROUND; j++) {
59
- const a = at(R[i], j),
60
- b = at(R[i], j + 1),
61
- c = at(R[i + 1], j),
62
- d = at(R[i + 1], j + 1);
59
+ const ringStart = at(bodyRings[i], j),
60
+ ringEnd = at(bodyRings[i], j + 1),
61
+ nextRingStart = at(bodyRings[i + 1], j),
62
+ nextRingEnd = at(bodyRings[i + 1], j + 1);
63
63
  const part = i < 2 ? 'head' : 'body';
64
- tri(a, c, d, 0, part);
65
- if (i > 0) tri(a, d, b, 0, part);
64
+ tri(ringStart, nextRingStart, nextRingEnd, 0, part);
65
+ if (i > 0) tri(ringStart, nextRingEnd, ringEnd, 0, part);
66
66
  }
67
67
  // the tail, a fan a little wider at its end, its middle feathers longest
68
- const [back, wide] = L.tail;
68
+ const [back, wide] = birdStyle.tail;
69
69
  tri([-0.05, 0.02, -0.5], [0.05, 0.02, -0.5], [wide, 0.02, -back + 0.08]);
70
70
  tri([-0.05, 0.02, -0.5], [wide, 0.02, -back + 0.08], [0, 0.02, -back]);
71
71
  tri([-0.05, 0.02, -0.5], [0, 0.02, -back], [-wide, 0.02, -back + 0.08]);
72
72
  for (const s of [-1, 1]) {
73
73
  const X = (x, y, z) => [s * x, y, z];
74
74
  for (const [rows, w] of [
75
- [L.arm, 1],
76
- [L.hand, 2],
75
+ [birdStyle.arm, 1],
76
+ [birdStyle.hand, 2],
77
77
  ])
78
78
  for (let i = 0; i < rows.length - 1; i++) {
79
- const a = rows[i],
80
- b = rows[i + 1],
81
- la = X(a[0], a[1], a[2]),
82
- ta = X(a[3], a[4], a[5]),
83
- lb = X(b[0], b[1], b[2]),
84
- tb = X(b[3], b[4], b[5]);
79
+ const currentRow = rows[i],
80
+ nextRow = rows[i + 1],
81
+ la = X(currentRow[0], currentRow[1], currentRow[2]),
82
+ ta = X(currentRow[3], currentRow[4], currentRow[5]),
83
+ lb = X(nextRow[0], nextRow[1], nextRow[2]),
84
+ tb = X(nextRow[3], nextRow[4], nextRow[5]);
85
85
  tri(la, ta, tb, w, w === 1 ? 'arm' : 'hand');
86
86
  tri(la, tb, lb, w, w === 1 ? 'arm' : 'hand');
87
87
  }
88
- for (const [x, z, dx, dz] of L.fingers) tri(X(x - 0.07, 0.01, z + 0.05), X(x + 0.05, 0.01, z + 0.04), X(x + dx, 0, z + dz), 2, 'tip');
88
+ for (const [x, z, dx, dz] of birdStyle.fingers) tri(X(x - 0.07, 0.01, z + 0.05), X(x + 0.05, 0.01, z + 0.04), X(x + dx, 0, z + dz), 2, 'tip');
89
89
  }
90
- const g = new THREE.BufferGeometry();
91
- g.setAttribute('position', new THREE.Float32BufferAttribute(P, 3));
92
- g.setAttribute('aW', new THREE.Float32BufferAttribute(W, 1));
93
- if (col) g.setAttribute('color', new THREE.Float32BufferAttribute(K, 3));
94
- if (U.length) g.setAttribute('aUnder', new THREE.Float32BufferAttribute(U, 4));
95
- g.computeVertexNormals();
96
- return g;
90
+ const geometry = new THREE.BufferGeometry();
91
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
92
+ geometry.setAttribute('aW', new THREE.Float32BufferAttribute(wingWeights, 1));
93
+ if (col) geometry.setAttribute('color', new THREE.Float32BufferAttribute(K, 3));
94
+ if (U.length) geometry.setAttribute('aUnder', new THREE.Float32BufferAttribute(U, 4));
95
+ geometry.computeVertexNormals();
96
+ return geometry;
97
97
  }
98
98
 
99
99
  /** A plain bird for far or stylised flocks: a body diamond and two wings, aW marking how far out along the wing a vertex sits. */
100
100
  export function plainBirdGeometry() {
101
- const g = new THREE.BufferGeometry();
102
- const P = [
101
+ const geometry = new THREE.BufferGeometry();
102
+ const positions = [
103
103
  0, 0, 0.9, 0.22, 0, 0, -0.22, 0, 0, 0.22, 0, 0, 0, 0, -1.0, -0.22, 0, 0, 0.2, 0, 0.35, 1.5, 0, 0.05, 0.2, 0, -0.35, -0.2, 0, -0.35,
104
104
  -1.5, 0, 0.05, -0.2, 0, 0.35,
105
105
  ];
106
- const W = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0];
107
- g.setAttribute('position', new THREE.Float32BufferAttribute(P, 3));
108
- g.setAttribute('aW', new THREE.Float32BufferAttribute(W, 1));
109
- g.computeVertexNormals();
110
- return g;
106
+ const wingWeights = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0];
107
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
108
+ geometry.setAttribute('aW', new THREE.Float32BufferAttribute(wingWeights, 1));
109
+ geometry.computeVertexNormals();
110
+ return geometry;
111
111
  }
112
112
 
113
113
  // The beat in the vertex shader: the arm about the shoulder, the hand about the wrist a little behind it, drawn in and swept back
@@ -133,9 +133,9 @@ const UNDER_HEAD = /* glsl */ ` attribute vec4 aUnder; varying vec4 vUnder;`,
133
133
 
134
134
  /** A plumage's colour (hex, sRGB) lifted to the linear luminance `least` if darker, its hue kept: black feathers still shine. */
135
135
  export function plumage(hex, least = 0.035) {
136
- const c = new THREE.Color(hex),
137
- lum = 0.2126 * c.r + 0.7152 * c.g + 0.0722 * c.b;
138
- return lum < least ? c.multiplyScalar(least / Math.max(lum, 1e-4)) : c;
136
+ const color = new THREE.Color(hex),
137
+ lum = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
138
+ return lum < least ? color.multiplyScalar(least / Math.max(lum, 1e-4)) : color;
139
139
  }
140
140
 
141
141
  /**
@@ -171,19 +171,19 @@ export function createFlock({
171
171
  key = look ? 'bird' : 'plain',
172
172
  }) {
173
173
  const bird = !!look,
174
- g = bird ? birdGeometry(look) : plainBirdGeometry(),
174
+ geometry = bird ? birdGeometry(look) : plainBirdGeometry(),
175
175
  flap = bird ? new THREE.InstancedBufferAttribute(new Float32Array(n * 2), 2) : null;
176
- if (flap) g.setAttribute('aFlap', flap.setUsage(THREE.DynamicDrawUsage));
176
+ if (flap) geometry.setAttribute('aFlap', flap.setUsage(THREE.DynamicDrawUsage));
177
177
  const coloured = bird && !!look.colours,
178
178
  under = bird && !!look.under,
179
- mat = new THREE.MeshStandardMaterial({
179
+ material = new THREE.MeshStandardMaterial({
180
180
  color: coloured ? 0xffffff : bird ? plumage(color, least) : color,
181
181
  vertexColors: coloured,
182
182
  roughness: (bird && look.gloss) || roughness,
183
183
  side: THREE.DoubleSide,
184
184
  flatShading: true,
185
185
  });
186
- mat.onBeforeCompile = sh => {
186
+ material.onBeforeCompile = sh => {
187
187
  sh.uniforms.uTime = time;
188
188
  if (bird) sh.uniforms.uBeat = { value: look.beat * TAU };
189
189
  sh.vertexShader = sh.vertexShader
@@ -211,8 +211,8 @@ export function createFlock({
211
211
  .replace('#include <common>', '#include <common>\nvarying vec4 vUnder;')
212
212
  .replace('#include <color_fragment>', '#include <color_fragment>' + UNDER);
213
213
  };
214
- mat.customProgramCacheKey = () => 'flock:' + key + (coloured ? ':coloured' : '') + (under ? ':under' : '');
215
- const mesh = new THREE.InstancedMesh(g, mat, n);
214
+ material.customProgramCacheKey = () => 'flock:' + key + (coloured ? ':coloured' : '') + (under ? ':under' : '');
215
+ const mesh = new THREE.InstancedMesh(geometry, material, n);
216
216
  mesh.frustumCulled = false;
217
217
  const birds = [];
218
218
  for (let i = 0; i < n; i++)
@@ -251,9 +251,9 @@ function flockFlight({ mesh, birds, flap, size, lens, bird, soar, silent, calls,
251
251
  dx = p.x - at.x,
252
252
  dy = p.y - at.y,
253
253
  dz = p.z - at.z,
254
- d = Math.hypot(dx, dy, dz);
255
- if (d >= clear) return;
256
- const k = clear / Math.max(d, 1e-3);
254
+ distance = Math.hypot(dx, dy, dz);
255
+ if (distance >= clear) return;
256
+ const k = clear / Math.max(distance, 1e-3);
257
257
  p.x = at.x + dx * k;
258
258
  p.y = at.y + dy * k;
259
259
  p.z = at.z + dz * k;
@@ -263,10 +263,10 @@ function flockFlight({ mesh, birds, flap, size, lens, bird, soar, silent, calls,
263
263
  if (!calls || silent || t < nextCall) return;
264
264
  nextCall = t + gap[0] + Math.random() * (gap[1] - gap[0]);
265
265
  const e = mesh.instanceMatrix.array,
266
- o = Math.floor(Math.random() * n) * 16,
267
- x = e[o + 12],
268
- y = e[o + 13],
269
- z = e[o + 14];
266
+ matrixOffset = Math.floor(Math.random() * n) * 16,
267
+ x = e[matrixOffset + 12],
268
+ y = e[matrixOffset + 13],
269
+ z = e[matrixOffset + 14];
270
270
  if (Math.hypot(x - lens.position.x, y - lens.position.y, z - lens.position.z) > heard) return;
271
271
  calls.sound(x, y, z, loud);
272
272
  }
@@ -281,21 +281,21 @@ function flockFlight({ mesh, birds, flap, size, lens, bird, soar, silent, calls,
281
281
  // own, so that a wider circle is flown faster.
282
282
  update(cx, cy, cz, radius, height, t, airspeed = 0) {
283
283
  const first = last === null,
284
- dt = first ? 0 : Math.min(0.1, Math.max(0, t - last)),
284
+ deltaSeconds = first ? 0 : Math.min(0.1, Math.max(0, t - last)),
285
285
  bob = Math.min(BOB, BOB_SHARE * height);
286
286
  last = t;
287
287
  for (let i = 0; i < n; i++) {
288
- const b = birds[i],
289
- r = radius * b.r,
290
- dir = b.sp > 0 ? 1 : -1,
291
- pace = 0.8 + (Math.abs(b.sp) - 0.05) * 5, // 0.8 to 1.2: some birds fly a little faster than others
292
- turn = airspeed > 0 ? (dir * airspeed * pace) / Math.max(r, 1) : b.sp * 6; // radians a second round the circle
293
- b.a = first ? b.ph + t * turn : b.a + turn * dt;
294
- const a = b.a,
295
- yaw = -a + (dir > 0 ? 0 : Math.PI);
296
- place.x = cx + Math.cos(a) * r;
297
- place.z = cz + Math.sin(a) * r;
298
- place.y = cy + b.h * height + Math.sin(t * BOB_RATE + b.wob) * bob;
288
+ const flockMember = birds[i],
289
+ r = radius * flockMember.r,
290
+ dir = flockMember.sp > 0 ? 1 : -1,
291
+ pace = 0.8 + (Math.abs(flockMember.sp) - 0.05) * 5, // 0.8 to 1.2: some birds fly a little faster than others
292
+ turn = airspeed > 0 ? (dir * airspeed * pace) / Math.max(r, 1) : flockMember.sp * 6; // radians a second round the circle
293
+ flockMember.a = first ? flockMember.ph + t * turn : flockMember.a + turn * deltaSeconds;
294
+ const orbitAngle = flockMember.a,
295
+ yaw = -orbitAngle + (dir > 0 ? 0 : Math.PI);
296
+ place.x = cx + Math.cos(orbitAngle) * r;
297
+ place.z = cz + Math.sin(orbitAngle) * r;
298
+ place.y = cy + flockMember.h * height + Math.sin(t * BOB_RATE + flockMember.wob) * bob;
299
299
  place.ry = yaw;
300
300
  if (!bird) {
301
301
  place.rx = 0;
@@ -304,16 +304,16 @@ function flockFlight({ mesh, birds, flap, size, lens, bird, soar, silent, calls,
304
304
  continue;
305
305
  }
306
306
  const speed = Math.abs(turn) * r,
307
- climb = bob * BOB_RATE * Math.cos(t * BOB_RATE + b.wob), // metres a second up
307
+ climb = bob * BOB_RATE * Math.cos(t * BOB_RATE + flockMember.wob), // metres a second up
308
308
  bank = Math.min(MAX_BANK, Math.atan((speed * Math.abs(turn)) / GRAVITY)) * dir,
309
309
  pitch = Math.atan2(climb, Math.max(speed, 1));
310
- b.beat += (sstep(-0.6, 0.2, climb) * (soar ? SOAR_BEAT : 1) - b.beat) * Math.min(1, dt * BEAT_EASE);
310
+ flockMember.beat += (sstep(-0.6, 0.2, climb) * (soar ? SOAR_BEAT : 1) - flockMember.beat) * Math.min(1, deltaSeconds * BEAT_EASE);
311
311
  keepFromLens(place);
312
312
  place.rx = -pitch;
313
313
  place.rz = bank;
314
314
  placeBankedAt(mesh.instanceMatrix.array, i * 16, place);
315
- flap.array[i * 2] = b.ph * 3;
316
- flap.array[i * 2 + 1] = b.beat;
315
+ flap.array[i * 2] = flockMember.ph * 3;
316
+ flap.array[i * 2 + 1] = flockMember.beat;
317
317
  }
318
318
  mesh.instanceMatrix.needsUpdate = true;
319
319
  if (flap) flap.needsUpdate = true;
@@ -11,7 +11,7 @@ const FOLDS = {
11
11
  fade: [0.008, 0.022], // the size of a pixel (metres) from which the folds fade and are gone: a fold a pixel or two wide would crawl
12
12
  slope: FOLD_SLOPES, // the steepest slope each field's byte holds (of its height, for each tile)
13
13
  };
14
- const f = n => n.toFixed(4);
14
+ const shaderFloat = value => value.toFixed(4);
15
15
  // What each kind of garment is to the eye (KIND order): how much of the
16
16
  // cloth texture's weave is smoothed away and the tone it is left at (of a plain weave's), how much of the long folds and of
17
17
  // the crumple it takes, how rough it is (a plain weave keeps the material's own: 0) and how much a metal.
@@ -28,7 +28,7 @@ const KINDS = [
28
28
  PLATE_WEAR = { face: 0.22, edge: 0.38 },
29
29
  MAIL = { around: 120, up: 210, fade: [0.18, 0.44] }, // links to the circumference and rows per metre
30
30
  kindOf = column =>
31
- `(kind < 0.5 ? ${f(KINDS[0][column])} : kind < 1.5 ? ${f(KINDS[1][column])} : kind < 2.5 ? ${f(KINDS[2][column])} : kind < 3.5 ? ${f(KINDS[3][column])} : kind < 4.5 ? ${f(KINDS[4][column])} : ${f(KINDS[5][column])})`;
31
+ `(kind < 0.5 ? ${shaderFloat(KINDS[0][column])} : kind < 1.5 ? ${shaderFloat(KINDS[1][column])} : kind < 2.5 ? ${shaderFloat(KINDS[2][column])} : kind < 3.5 ? ${shaderFloat(KINDS[3][column])} : kind < 4.5 ? ${shaderFloat(KINDS[4][column])} : ${shaderFloat(KINDS[5][column])})`;
32
32
  // What the fragment shader declares: the textures; the folds' slopes across the screen (metres of height a pixel), which
33
33
  // GARMENT_FOLDS works out and FOLD_RELIEF turns the normal by; and how worn smooth a spot of leather is, for its sheen.
34
34
  const FOLD_PARS = `/* glsl */
@@ -44,33 +44,33 @@ float mailLink(vec2 uv, float row, float aa) {
44
44
  // In the surface's colour (after vSk, the tag, and vL, the point as built, are known): the garment's own weave or smoothness,
45
45
  // the folds' slopes, and their shade.
46
46
  const GARMENT_FOLDS = `/* glsl */
47
- { float worn = -vSk, kind = floor(worn + 0.001), show = step(0.05, worn) * (1.0 - smoothstep(${f(FOLDS.fade[0])}, ${f(FOLDS.fade[1])}, length(fwidth(vViewPosition))));
47
+ { float worn = -vSk, kind = floor(worn + 0.001), show = step(0.05, worn) * (1.0 - smoothstep(${shaderFloat(FOLDS.fade[0])}, ${shaderFloat(FOLDS.fade[1])}, length(fwidth(vViewPosition))));
48
48
  vec3 bx = dFdx(vL), by = dFdy(vL);
49
- if (worn > 0.05) { float even = ${kindOf(0)}; diffuseColor.rgb = mix(diffuseColor.rgb, vec3(${f(WEAVE_TONE)}), even) * ${kindOf(1)}; gH = mix(gH, 0.5, even); }
49
+ if (worn > 0.05) { float even = ${kindOf(0)}; diffuseColor.rgb = mix(diffuseColor.rgb, vec3(${shaderFloat(WEAVE_TONE)}), even) * ${kindOf(1)}; gH = mix(gH, 0.5, even); }
50
50
  if (show > 0.0) {
51
- float crease = clamp((worn - kind - ${f(PLAIN)}) / ${f(CREASED - PLAIN)}, 0.0, 1.0), turn = atan(vL.x, vL.z) * 0.15915494;
51
+ float crease = clamp((worn - kind - ${shaderFloat(PLAIN)}) / ${shaderFloat(CREASED - PLAIN)}, 0.0, 1.0), turn = atan(vL.x, vL.z) * 0.15915494;
52
52
  // how far round the body, and up it, a pixel to the right and a pixel up are
53
53
  vec2 round = vec2(vL.z * bx.x - vL.x * bx.z, vL.z * by.x - vL.x * by.z) / (6.2832 * max(dot(vL.xz, vL.xz), 0.0036)), up = vec2(bx.y, by.y);
54
- vec2 hu = round * ${f(FOLDS.hang[0])}, cu = round * ${f(FOLDS.crumple[0])} + up * ${f(FOLDS.lean)}, cv = up * ${f(FOLDS.crumple[1])};
55
- vec2 hang = textureLod(uFolds, vec2(turn * ${f(FOLDS.hang[0])}, vL.y * ${f(FOLDS.hang[1])}), 0.5 * log2(max(dot(hu, hu), dot(up, up) * ${f(FOLDS.hang[1] ** 2)}) * 65536.0)).rg;
56
- vec3 crumple = textureLod(uCrumple, vec2(turn * ${f(FOLDS.crumple[0])} + vL.y * ${f(FOLDS.lean)}, vL.y * ${f(FOLDS.crumple[1])}), 0.5 * log2(max(dot(cu, cu), dot(cv, cv)) * 65536.0)).rgb;
54
+ vec2 hu = round * ${shaderFloat(FOLDS.hang[0])}, cu = round * ${shaderFloat(FOLDS.crumple[0])} + up * ${shaderFloat(FOLDS.lean)}, cv = up * ${shaderFloat(FOLDS.crumple[1])};
55
+ vec2 hang = textureLod(uFolds, vec2(turn * ${shaderFloat(FOLDS.hang[0])}, vL.y * ${shaderFloat(FOLDS.hang[1])}), 0.5 * log2(max(dot(hu, hu), dot(up, up) * ${shaderFloat(FOLDS.hang[1] ** 2)}) * 65536.0)).rg;
56
+ vec3 crumple = textureLod(uCrumple, vec2(turn * ${shaderFloat(FOLDS.crumple[0])} + vL.y * ${shaderFloat(FOLDS.lean)}, vL.y * ${shaderFloat(FOLDS.crumple[1])}), 0.5 * log2(max(dot(cu, cu), dot(cv, cv)) * 65536.0)).rgb;
57
57
  // half gathered, the cloth hangs in its long folds; past that it is crumpled as well. Leather is creased a little wherever it is
58
- float leather = 1.0 - step(0.5, abs(kind - 3.0)), fall = show * mix(${f(FOLDS.smooth)}, 1.0, min(1.0, crease * 2.0)) * ${kindOf(2)};
58
+ float leather = 1.0 - step(0.5, abs(kind - 3.0)), fall = show * mix(${shaderFloat(FOLDS.smooth)}, 1.0, min(1.0, crease * 2.0)) * ${kindOf(2)};
59
59
  float crush = max(crease * 2.0 - 1.0, 0.0); crush = show * max(crush * crush, 0.15 * leather) * ${kindOf(3)};
60
- gFold = fall * ${f(FOLDS.deep[0] * FOLDS.slope[0])} * (hang.r * 2.0 - 1.0) * hu
61
- + crush * ${f(FOLDS.deep[1] * FOLDS.slope[1])} * ((crumple.r * 2.0 - 1.0) * cu + (crumple.g * 2.0 - 1.0) * cv);
62
- diffuseColor.rgb *= (1.0 - ${f(FOLDS.shade[0])} * fall * (1.0 - hang.g)) * (1.0 - ${f(FOLDS.shade[1])} * crush * (1.0 - crumple.b));
60
+ gFold = fall * ${shaderFloat(FOLDS.deep[0] * FOLDS.slope[0])} * (hang.r * 2.0 - 1.0) * hu
61
+ + crush * ${shaderFloat(FOLDS.deep[1] * FOLDS.slope[1])} * ((crumple.r * 2.0 - 1.0) * cu + (crumple.g * 2.0 - 1.0) * cv);
62
+ diffuseColor.rgb *= (1.0 - ${shaderFloat(FOLDS.shade[0])} * fall * (1.0 - hang.g)) * (1.0 - ${shaderFloat(FOLDS.shade[1])} * crush * (1.0 - crumple.b));
63
63
  // Plate's tag can mark an exposed edge. The existing crease field breaks up its polish.
64
64
  float plate = step(4.5, kind);
65
65
  float edge = plate * smoothstep(0.65, 0.96, crease) * mix(0.6, 1.0, crumple.b);
66
66
  gWorn = show * max(max(leather, plate) * crumple.b, edge);
67
- diffuseColor.rgb *= 1.0 + show * (${f(PLATE_WEAR.edge)} * edge - plate * ${f(PLATE_WEAR.face)} * (1.0 - crumple.b) * (1.0 - edge));
68
- diffuseColor.rgb *= 1.0 + leather * show * (${f(LEATHER.worn)} * crumple.b * crumple.b - ${f(LEATHER.grain)} * (1.0 - crumple.b));
67
+ diffuseColor.rgb *= 1.0 + show * (${shaderFloat(PLATE_WEAR.edge)} * edge - plate * ${shaderFloat(PLATE_WEAR.face)} * (1.0 - crumple.b) * (1.0 - edge));
68
+ diffuseColor.rgb *= 1.0 + leather * show * (${shaderFloat(LEATHER.worn)} * crumple.b * crumple.b - ${shaderFloat(LEATHER.grain)} * (1.0 - crumple.b));
69
69
  if (kind == 4.0) {
70
- float pixel = max(length(round) * ${f(MAIL.around)}, length(up) * ${f(MAIL.up)});
71
- float detail = 1.0 - smoothstep(${f(MAIL.fade[0])}, ${f(MAIL.fade[1])}, pixel);
70
+ float pixel = max(length(round) * ${shaderFloat(MAIL.around)}, length(up) * ${shaderFloat(MAIL.up)});
71
+ float detail = 1.0 - smoothstep(${shaderFloat(MAIL.fade[0])}, ${shaderFloat(MAIL.fade[1])}, pixel);
72
72
  if (detail > 0.0) {
73
- vec2 uv = vec2(turn * ${f(MAIL.around)}, vL.y * ${f(MAIL.up)});
73
+ vec2 uv = vec2(turn * ${shaderFloat(MAIL.around)}, vL.y * ${shaderFloat(MAIL.up)});
74
74
  float row = floor(uv.y), aa = max(0.02, pixel * 0.5);
75
75
  // Alternating rows lean and stagger so adjacent links meet on the half-row.
76
76
  float link = max(mailLink(uv, row, aa), mailLink(uv, row + (fract(uv.y) < 0.5 ? -1.0 : 1.0), aa));
@@ -8,27 +8,27 @@ attribute vec3 to;
8
8
  uniform vec2 screen;
9
9
  uniform float width;
10
10
  varying float across;
11
- vec4 seen(vec3 p) {
12
- vec4 v = modelViewMatrix * vec4(p, 1.0);
13
- float far = length(v.xyz);
14
- v.xyz -= v.xyz / far * (${PULL[0].toFixed(3)} + ${PULL[1].toFixed(4)} * far);
15
- return projectionMatrix * v;
11
+ vec4 seen(vec3 localPoint) {
12
+ vec4 viewPoint = modelViewMatrix * vec4(localPoint, 1.0);
13
+ float viewDistance = length(viewPoint.xyz);
14
+ viewPoint.xyz -= viewPoint.xyz / viewDistance * (${PULL[0].toFixed(3)} + ${PULL[1].toFixed(4)} * viewDistance);
15
+ return projectionMatrix * viewPoint;
16
16
  }
17
17
  void main() {
18
- vec4 a = seen(from), b = seen(to);
18
+ vec4 clipStart = seen(from), clipEnd = seen(to);
19
19
  across = position.y;
20
- if (a.w <= 0.0 || b.w <= 0.0) {
20
+ if (clipStart.w <= 0.0 || clipEnd.w <= 0.0) {
21
21
  gl_Position = vec4(0.0, 0.0, 2.0, 1.0); // an end behind the viewer: the segment is not drawn
22
22
  return;
23
23
  }
24
24
  vec2 halfScreen = 0.5 * screen,
25
- sa = a.xy / a.w * halfScreen,
26
- sb = b.xy / b.w * halfScreen,
27
- along = normalize(sb - sa + vec2(1e-5, 0.0)),
25
+ screenStart = clipStart.xy / clipStart.w * halfScreen,
26
+ screenEnd = clipEnd.xy / clipEnd.w * halfScreen,
27
+ along = normalize(screenEnd - screenStart + vec2(1e-5, 0.0)),
28
28
  side = vec2(-along.y, along.x);
29
- vec4 p = mix(a, b, position.x);
30
- p.xy += (side * position.y + along * (position.x * 2.0 - 1.0)) * width / halfScreen * p.w;
31
- gl_Position = p;
29
+ vec4 clipPoint = mix(clipStart, clipEnd, position.x);
30
+ clipPoint.xy += (side * position.y + along * (position.x * 2.0 - 1.0)) * width / halfScreen * clipPoint.w;
31
+ gl_Position = clipPoint;
32
32
  }`;
33
33
 
34
34
  const FRAGMENT = /* glsl */ `
@@ -1,6 +1,6 @@
1
1
  import { HAIR_NEAR, HAIR_FAR, STRANDS } from '../geometry/hair-tags.js';
2
2
 
3
- const f = n => n.toFixed(4);
3
+ const shaderFloat = value => value.toFixed(4);
4
4
  const LOOK = {
5
5
  fade: [0.22, 0.6], // clumps to a pixel from which the strands fade, and are gone
6
6
  stand: [0.1, 0.3], // and from which the clumps no longer stand out of the surface
@@ -18,37 +18,37 @@ const LOOK = {
18
18
  const HAIR_PARS = `
19
19
  float gHair = 0.0, gSheen = 0.0, gClump = 0.5; vec2 gAcross = vec2(0.0);
20
20
  float strandOf(float cell, float period) { return fract(sin(mod(cell, period) * 91.7 + 3.1) * 43758.5453); }`;
21
- const smoothOf = tag => `(${tag} > ${f(HAIR_NEAR - 0.5)} ? ${f(HAIR_FAR)} : ${tag})`;
21
+ const smoothOf = tag => `(${tag} > ${shaderFloat(HAIR_NEAR - 0.5)} ? ${shaderFloat(HAIR_FAR)} : ${tag})`;
22
22
  // In the surface's colour, once the tag (vSk) and the point as built (vL) are known: the strands' tone and relief.
23
23
  const HAIR_STRANDS = `/* glsl */
24
- { vec2 slope = vec2(dFdx(vSk), dFdy(vSk)) * ${f(STRANDS.clumps)};
25
- if (vSk > ${f(HAIR_NEAR - 0.5)}) {
24
+ { vec2 slope = vec2(dFdx(vSk), dFdy(vSk)) * ${shaderFloat(STRANDS.clumps)};
25
+ if (vSk > ${shaderFloat(HAIR_NEAR - 0.5)}) {
26
26
  float thick = length(slope);
27
27
  gAcross = slope;
28
- gSheen = 1.0 - smoothstep(${f(LOOK.sheen[0])}, ${f(LOOK.sheen[1])}, length(vViewPosition));
29
- gHair = 1.0 - smoothstep(${f(LOOK.fade[0])}, ${f(LOOK.fade[1])}, thick);
30
- diffuseColor.rgb = vec3(${f(LOOK.even)});
28
+ gSheen = 1.0 - smoothstep(${shaderFloat(LOOK.sheen[0])}, ${shaderFloat(LOOK.sheen[1])}, length(vViewPosition));
29
+ gHair = 1.0 - smoothstep(${shaderFloat(LOOK.fade[0])}, ${shaderFloat(LOOK.fade[1])}, thick);
30
+ diffuseColor.rgb = vec3(${shaderFloat(LOOK.even)});
31
31
  gH = 0.5;
32
32
  if (gHair > 0.0) {
33
- float across = fract(vSk) * ${f(STRANDS.clumps)}, cell = floor(across), t = across - cell;
34
- float wander = dot(vL, vec3(${LOOK.along.map(f).join(', ')})), ease = t * t * (3.0 - 2.0 * t);
33
+ float across = fract(vSk) * ${shaderFloat(STRANDS.clumps)}, cell = floor(across), t = across - cell;
34
+ float wander = dot(vL, vec3(${LOOK.along.map(shaderFloat).join(', ')})), ease = t * t * (3.0 - 2.0 * t);
35
35
  // a clump's tone, and its slow change along its length, each eased into the next clump's
36
- float clump = mix(strandOf(cell, ${f(STRANDS.clumps * STRANDS.period)}) + 0.22 * sin(wander + cell * 2.4),
37
- strandOf(cell + 1.0, ${f(STRANDS.clumps * STRANDS.period)}) + 0.22 * sin(wander + cell * 2.4 + 2.4), ease);
38
- float thin = across * ${f(LOOK.fine)}, cell2 = floor(thin), t2 = thin - cell2;
39
- float fine = mix(strandOf(cell2 + 7.0, ${f(STRANDS.clumps * STRANDS.period * LOOK.fine)}), strandOf(cell2 + 8.0, ${f(STRANDS.clumps * STRANDS.period * LOOK.fine)}), t2 * t2 * (3.0 - 2.0 * t2));
40
- float few = 1.0 - smoothstep(${f(LOOK.fade[0] / LOOK.fine)}, ${f(LOOK.fade[1] / LOOK.fine)}, thick);
36
+ float clump = mix(strandOf(cell, ${shaderFloat(STRANDS.clumps * STRANDS.period)}) + 0.22 * sin(wander + cell * 2.4),
37
+ strandOf(cell + 1.0, ${shaderFloat(STRANDS.clumps * STRANDS.period)}) + 0.22 * sin(wander + cell * 2.4 + 2.4), ease);
38
+ float thin = across * ${shaderFloat(LOOK.fine)}, cell2 = floor(thin), t2 = thin - cell2;
39
+ float fine = mix(strandOf(cell2 + 7.0, ${shaderFloat(STRANDS.clumps * STRANDS.period * LOOK.fine)}), strandOf(cell2 + 8.0, ${shaderFloat(STRANDS.clumps * STRANDS.period * LOOK.fine)}), t2 * t2 * (3.0 - 2.0 * t2));
40
+ float few = 1.0 - smoothstep(${shaderFloat(LOOK.fade[0] / LOOK.fine)}, ${shaderFloat(LOOK.fade[1] / LOOK.fine)}, thick);
41
41
  // the fine strands are a tone only; the clumps also stand out of the surface and shift the light, while they are
42
42
  // wide enough on the screen for their slopes to be read from one pixel to the next
43
43
  gClump = clamp(clump, 0.0, 1.0);
44
- diffuseColor.rgb *= 1.0 + gHair * ${f(LOOK.tone)} * (mix(gClump, 0.55 * gClump + 0.45 * fine, few) - 0.5);
45
- gHair *= 1.0 - smoothstep(${f(LOOK.stand[0])}, ${f(LOOK.stand[1])}, thick);
46
- gH += gHair * ${f(LOOK.relief)} * (gClump - 0.5);
44
+ diffuseColor.rgb *= 1.0 + gHair * ${shaderFloat(LOOK.tone)} * (mix(gClump, 0.55 * gClump + 0.45 * fine, few) - 0.5);
45
+ gHair *= 1.0 - smoothstep(${shaderFloat(LOOK.stand[0])}, ${shaderFloat(LOOK.stand[1])}, thick);
46
+ gH += gHair * ${shaderFloat(LOOK.relief)} * (gClump - 0.5);
47
47
  }
48
48
  } }`;
49
49
  // After the material's own roughness is known.
50
50
  const HAIR_SHEEN = `/* glsl */
51
- if (vSk > ${f(HAIR_NEAR - 0.5)}) roughnessFactor = mix(roughnessFactor, ${f(LOOK.rough)}, gSheen);`;
51
+ if (vSk > ${shaderFloat(HAIR_NEAR - 0.5)}) roughnessFactor = mix(roughnessFactor, ${shaderFloat(LOOK.rough)}, gSheen);`;
52
52
  // After the surface's normal is known: turned toward the normal a strand shows the eye, which is square to the strand, so that
53
53
  // the light on a head of hair lies in a band across the strands.
54
54
  const HAIR_LIE = `/* glsl */
@@ -57,9 +57,9 @@ const HAIR_LIE = `/* glsl */
57
57
  vec3 climb = sign(dot(hgx, h1)) * (gAcross.x * h1 + gAcross.y * h2), strand = cross(normal, climb);
58
58
  float sl = dot(strand, strand);
59
59
  if (sl > 1e-20) {
60
- strand = normalize(strand * inversesqrt(sl) + normal * (${f(LOOK.shift)} * gHair * (gClump - 0.5)));
60
+ strand = normalize(strand * inversesqrt(sl) + normal * (${shaderFloat(LOOK.shift)} * gHair * (gClump - 0.5)));
61
61
  vec3 toEye = normalize(vViewPosition), shown = toEye - strand * dot(toEye, strand);
62
- vec3 lie = mix(normal, shown * inversesqrt(max(dot(shown, shown), 1e-8)), ${f(LOOK.lie)} * gSheen);
62
+ vec3 lie = mix(normal, shown * inversesqrt(max(dot(shown, shown), 1e-8)), ${shaderFloat(LOOK.lie)} * gSheen);
63
63
  normal = lie * inversesqrt(max(dot(lie, lie), 1e-8));
64
64
  }
65
65
  }`;