roamwright 0.1.0 → 0.2.0

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 (116) hide show
  1. package/CONTRIBUTING.md +39 -8
  2. package/README.md +53 -24
  3. package/examples/viaduct-camera.js +40 -0
  4. package/examples/viaduct-landscape.js +81 -0
  5. package/examples/viaduct.css +75 -0
  6. package/examples/viaduct.html +47 -0
  7. package/examples/viaduct.js +332 -0
  8. package/geometry/builder.js +11 -14
  9. package/geometry/creased-sheet.js +127 -0
  10. package/geometry/ear.js +97 -0
  11. package/geometry/erosion.js +106 -0
  12. package/geometry/face-grid.js +143 -0
  13. package/geometry/garment-tags.js +11 -0
  14. package/geometry/hair-gathers.js +174 -0
  15. package/geometry/hair-locks.js +156 -0
  16. package/geometry/hair-patch.js +42 -0
  17. package/geometry/hair-shell.js +186 -0
  18. package/geometry/hair-tags.js +6 -0
  19. package/geometry/head-profile.js +138 -0
  20. package/geometry/head-shape.js +499 -0
  21. package/geometry/heightfield.js +68 -0
  22. package/geometry/outline-grid.js +46 -0
  23. package/geometry/path-field.js +65 -0
  24. package/geometry/sagging-tube.js +58 -0
  25. package/geometry/sampled-surface.js +101 -0
  26. package/geometry/scatter-grid.js +32 -0
  27. package/geometry/sculpt.js +263 -0
  28. package/geometry/shore-grid.js +67 -0
  29. package/geometry/strata-field.js +339 -0
  30. package/geometry/strata-tiles.js +215 -0
  31. package/geometry/stream.js +94 -0
  32. package/geometry/terrain-chunks.js +300 -0
  33. package/geometry/terrain-extension.js +44 -0
  34. package/math/cell-edge.js +44 -0
  35. package/math/cloth-textures.js +83 -0
  36. package/math/cloud-textures.js +112 -0
  37. package/math/color.js +29 -0
  38. package/math/curve.js +3 -4
  39. package/math/mountain-ranges.js +463 -0
  40. package/math/particle-noise.js +48 -0
  41. package/math/periodic.js +20 -0
  42. package/math/rock-texture.js +56 -0
  43. package/math/surface-textures.js +241 -0
  44. package/motion/arm-reach.js +74 -0
  45. package/motion/biped-stride.js +276 -0
  46. package/motion/crowd-alarms.js +127 -0
  47. package/motion/crowd-routine.js +183 -0
  48. package/motion/daily-clock.js +59 -0
  49. package/motion/instance-sight.js +84 -0
  50. package/motion/quadruped.js +332 -0
  51. package/motion/weather.js +59 -0
  52. package/motion/wind.js +27 -0
  53. package/package.json +6 -4
  54. package/rendering/caster-motion.js +79 -0
  55. package/rendering/cell-lod.js +175 -0
  56. package/rendering/cloud-dome.js +371 -0
  57. package/rendering/cloud-field.js +83 -0
  58. package/rendering/compilation-schedule.js +34 -0
  59. package/rendering/contact-shadows.js +79 -0
  60. package/rendering/crowd-skinning.js +133 -0
  61. package/rendering/crust.js +28 -0
  62. package/rendering/daylight.js +50 -0
  63. package/rendering/far-shadow.js +186 -0
  64. package/rendering/flocks.js +323 -0
  65. package/rendering/garment-material.js +110 -0
  66. package/rendering/glow-lines.js +74 -0
  67. package/rendering/glow-shell.js +90 -0
  68. package/rendering/hair-strands.js +67 -0
  69. package/rendering/height-haze.js +129 -0
  70. package/rendering/height-probe.js +91 -0
  71. package/rendering/interior-mapping.js +19 -0
  72. package/rendering/light-budget.js +222 -0
  73. package/rendering/light-points.js +51 -0
  74. package/rendering/light-shafts.js +131 -0
  75. package/rendering/local-lights.js +28 -0
  76. package/rendering/low-light.js +13 -0
  77. package/rendering/material-lod.js +110 -0
  78. package/rendering/particle-emitters.js +223 -0
  79. package/rendering/particles.js +297 -0
  80. package/rendering/planar-reflection.js +175 -0
  81. package/rendering/post-shaders.js +21 -1
  82. package/rendering/post.js +8 -5
  83. package/rendering/rain-rings.js +22 -0
  84. package/rendering/road-surface.js +63 -0
  85. package/rendering/sea-shaders.js +230 -0
  86. package/rendering/shader-warmup.js +197 -0
  87. package/rendering/shadow-alignment.js +28 -0
  88. package/rendering/shadow-depth.js +129 -0
  89. package/rendering/sky-occlusion.js +187 -0
  90. package/rendering/sky.js +169 -0
  91. package/rendering/stage-lights.js +209 -0
  92. package/rendering/static-point-shadow.js +119 -0
  93. package/rendering/still-casters.js +6 -11
  94. package/rendering/strata-material.js +93 -0
  95. package/rendering/stream-flow.js +36 -0
  96. package/rendering/surface-light.js +22 -0
  97. package/rendering/surface-projection.js +69 -0
  98. package/rendering/talus.js +26 -0
  99. package/rendering/terrain-rock.js +48 -0
  100. package/rendering/texture-fills.js +68 -0
  101. package/rendering/viewport.js +9 -0
  102. package/rendering/wakes.js +99 -0
  103. package/rendering/water-material.js +288 -0
  104. package/rendering/world-matrices.js +4 -6
  105. package/runtime/device-quality.js +27 -0
  106. package/runtime/diagnostic-log.js +80 -0
  107. package/runtime/events.js +14 -0
  108. package/runtime/first-use.js +3 -1
  109. package/runtime/frame-yield.js +14 -0
  110. package/runtime/grid-cache.js +88 -0
  111. package/runtime/grid-rows.js +9 -0
  112. package/runtime/module-preload.js +15 -0
  113. package/runtime/quality-fit.js +5 -9
  114. package/runtime/search.js +69 -0
  115. package/runtime/spatial-audio.js +57 -0
  116. package/scripts/serve.py +26 -0
@@ -0,0 +1,323 @@
1
+ import * as THREE from 'three';
2
+ import { TAU, sstep } from '../math/scalar.js';
3
+ import { placeBankedAt, placement } from '../geometry/placement.js';
4
+
5
+ /**
6
+ * Flocks of birds: one instanced mesh a flock, the wings beaten in the vertex shader. A bird beats while it climbs or holds its
7
+ * height and glides on held wings as it comes down, and banks into its turn as far as its speed round it asks.
8
+ *
9
+ * A bird's look gives its wing's outline and, for a bird of more than one colour, the colours of its parts (all in the bird's own
10
+ * units, nose to +z, about 2.1 long and 3.2 across the wings at size 1):
11
+ * - `arm`, `hand`: rows across the wing from the leading edge to the trailing one, [x, y, z of the front, x, y, z of the back],
12
+ * the arm out to the wrist (x = WRIST) and the hand out to the tip;
13
+ * - `fingers`: the outer primaries' tips, notched apart [x, z where one leaves the wing, how far its tip lies out and forward];
14
+ * - `tail`: [how far back its middle reaches, how wide its end is];
15
+ * - `beat`: wingbeats a second; `gloss`: the plumage's roughness, if not the flock's;
16
+ * - `colours` (linear) { body, head, arm, hand, tip }, the tip beyond x = `tips`; `under` { arm, hand }: the under face's colours;
17
+ * - `soar`: a bird that rides the air on held wings and seldom beats them; `silent`: one that does not call.
18
+ */
19
+ export const SHOULDER = 0.16; // how far out the wings are hinged
20
+ export const WRIST = 0.8; // and the wrists
21
+ const SPAN = 3.2; // a bird across the wings at size 1
22
+
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 = [],
27
+ K = [],
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);
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;
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);
40
+ }
41
+ };
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 = [
44
+ [1.08, 0, 0, 0.02],
45
+ [0.86, 0.06, 0.06, 0.04],
46
+ [0.68, 0.1, 0.1, 0.05],
47
+ [0.4, 0.17, 0.15, 0.01],
48
+ [0.05, 0.18, 0.15, 0],
49
+ [-0.35, 0.12, 0.1, 0.01],
50
+ [-0.6, 0.05, 0.04, 0.02],
51
+ ],
52
+ AROUND = 6,
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]];
56
+ };
57
+ for (let i = 0; i < R.length - 1; i++)
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);
63
+ const part = i < 2 ? 'head' : 'body';
64
+ tri(a, c, d, 0, part);
65
+ if (i > 0) tri(a, d, b, 0, part);
66
+ }
67
+ // the tail, a fan a little wider at its end, its middle feathers longest
68
+ const [back, wide] = L.tail;
69
+ tri([-0.05, 0.02, -0.5], [0.05, 0.02, -0.5], [wide, 0.02, -back + 0.08]);
70
+ tri([-0.05, 0.02, -0.5], [wide, 0.02, -back + 0.08], [0, 0.02, -back]);
71
+ tri([-0.05, 0.02, -0.5], [0, 0.02, -back], [-wide, 0.02, -back + 0.08]);
72
+ for (const s of [-1, 1]) {
73
+ const X = (x, y, z) => [s * x, y, z];
74
+ for (const [rows, w] of [
75
+ [L.arm, 1],
76
+ [L.hand, 2],
77
+ ])
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]);
85
+ tri(la, ta, tb, w, w === 1 ? 'arm' : 'hand');
86
+ tri(la, tb, lb, w, w === 1 ? 'arm' : 'hand');
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');
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;
97
+ }
98
+
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
+ export function plainBirdGeometry() {
101
+ const g = new THREE.BufferGeometry();
102
+ const P = [
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
+ -1.5, 0, 0.05, -0.2, 0, 0.35,
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;
111
+ }
112
+
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
114
+ // on the upstroke; the body rises and falls against the wings; a gliding bird holds its wings in a shallow V.
115
+ const BEAT = /* glsl */ `
116
+ float beat = aFlap.y, ph = uTime * uBeat + aFlap.x, up = max(cos(ph), 0.0) * beat;
117
+ if (aW > 0.5) {
118
+ float side = sign(transformed.x), ax = abs(transformed.x);
119
+ float a1 = beat * (0.12 + 0.75 * sin(ph)) + (1.0 - beat) * 0.1;
120
+ float a2 = a1 + beat * 0.5 * sin(ph - 0.9) - (1.0 - beat) * 0.06;
121
+ float r1 = min(ax, ${WRIST.toFixed(2)}) - ${SHOULDER.toFixed(2)}, r2 = max(ax - ${WRIST.toFixed(2)}, 0.0) * (1.0 - 0.35 * up);
122
+ vec2 xy = vec2(${SHOULDER.toFixed(2)}, 0.0) + vec2(cos(a1), sin(a1)) * r1 + vec2(cos(a2), sin(a2)) * r2;
123
+ transformed.x = side * xy.x;
124
+ transformed.y += xy.y;
125
+ transformed.z -= 0.3 * up * r2;
126
+ }
127
+ transformed.y -= beat * 0.06 * sin(ph);`;
128
+ // A wing's under face in its own colour: vUnder carries that colour and whether the triangle's front face is its upper one (1),
129
+ // its lower one (-1), or it is one colour on both faces (0).
130
+ const UNDER_HEAD = /* glsl */ ` attribute vec4 aUnder; varying vec4 vUnder;`,
131
+ UNDER = /* glsl */ `
132
+ if (vUnder.w != 0.0 && gl_FrontFacing != (vUnder.w > 0.0)) diffuseColor.rgb = diffuse * vUnder.rgb;`;
133
+
134
+ /** A plumage's colour (hex, sRGB) lifted to the linear luminance `least` if darker, its hue kept: black feathers still shine. */
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;
139
+ }
140
+
141
+ /**
142
+ * A flock of `count` birds.
143
+ *
144
+ * @param {object} options
145
+ * @param {number} options.count
146
+ * @param {number} options.color Its plumage (hex), for a bird of one colour.
147
+ * @param {number} options.size Its birds' size (1: 3.2 across the wings).
148
+ * @param {object} [options.look] The bird's look (above); none for a plain flock (plainBirdGeometry), which flaps without banking.
149
+ * @param {{ value: number }} options.time The clock uniform, in seconds.
150
+ * @param {THREE.Camera} options.lens The camera birds keep clear of.
151
+ * @param {() => number} options.random Seeded random numbers for the birds' places.
152
+ * @param {number} [options.roughness=0.7] Its plumage's roughness unless the look says.
153
+ * @param {number} [options.least=0.035] The least linear luminance of its plumage.
154
+ * @param {number} [options.lensClear=5] Wingspans a bird keeps from the lens: nearer, one bird would fill the picture.
155
+ * @param {object} [options.calls] Now and then one bird calls: { sound(x, y, z, loudness), heard: metres within which it is heard
156
+ * (80), gap: [least, most] seconds between calls (4, 12) }.
157
+ * @param {string} [options.key] Told apart from other flock materials (`customProgramCacheKey`).
158
+ */
159
+ export function createFlock({
160
+ count: n,
161
+ color,
162
+ size,
163
+ look,
164
+ time,
165
+ lens,
166
+ random,
167
+ roughness = 0.7,
168
+ least = 0.035,
169
+ lensClear = 5,
170
+ calls,
171
+ key = look ? 'bird' : 'plain',
172
+ }) {
173
+ const bird = !!look,
174
+ g = bird ? birdGeometry(look) : plainBirdGeometry(),
175
+ flap = bird ? new THREE.InstancedBufferAttribute(new Float32Array(n * 2), 2) : null;
176
+ if (flap) g.setAttribute('aFlap', flap.setUsage(THREE.DynamicDrawUsage));
177
+ const coloured = bird && !!look.colours,
178
+ under = bird && !!look.under,
179
+ mat = new THREE.MeshStandardMaterial({
180
+ color: coloured ? 0xffffff : bird ? plumage(color, least) : color,
181
+ vertexColors: coloured,
182
+ roughness: (bird && look.gloss) || roughness,
183
+ side: THREE.DoubleSide,
184
+ flatShading: true,
185
+ });
186
+ mat.onBeforeCompile = sh => {
187
+ sh.uniforms.uTime = time;
188
+ if (bird) sh.uniforms.uBeat = { value: look.beat * TAU };
189
+ sh.vertexShader = sh.vertexShader
190
+ .replace(
191
+ '#include <common>',
192
+ '#include <common>\nattribute float aW; uniform float uTime;' +
193
+ (bird ? ' attribute vec2 aFlap; uniform float uBeat;' : '') +
194
+ (under ? UNDER_HEAD : ''),
195
+ )
196
+ .replace(
197
+ '#include <begin_vertex>',
198
+ bird
199
+ ? '#include <begin_vertex>' + BEAT + (under ? '\n vUnder = aUnder;' : '')
200
+ : `/* glsl */
201
+ #include <begin_vertex>
202
+ #ifdef USE_INSTANCING
203
+ float ph = instanceMatrix[3].x * 0.31 + instanceMatrix[3].z * 0.17;
204
+ #else
205
+ float ph = 0.0;
206
+ #endif
207
+ transformed.y += aW * sin(uTime * 5.5 + ph) * 0.75;`,
208
+ );
209
+ if (under)
210
+ sh.fragmentShader = sh.fragmentShader
211
+ .replace('#include <common>', '#include <common>\nvarying vec4 vUnder;')
212
+ .replace('#include <color_fragment>', '#include <color_fragment>' + UNDER);
213
+ };
214
+ mat.customProgramCacheKey = () => 'flock:' + key + (coloured ? ':coloured' : '') + (under ? ':under' : '');
215
+ const mesh = new THREE.InstancedMesh(g, mat, n);
216
+ mesh.frustumCulled = false;
217
+ const birds = [];
218
+ for (let i = 0; i < n; i++)
219
+ birds.push({
220
+ r: 0.55 + random() * 0.9,
221
+ h: random() * 2 - 1,
222
+ sp: (0.05 + random() * 0.08) * (random() < 0.5 ? 1 : -1),
223
+ ph: random() * TAU,
224
+ a: 0, // where it is round its circle, radians
225
+ wob: random() * TAU,
226
+ beat: 1, // how hard it beats now, eased towards what its climb or fall asks
227
+ });
228
+ return flockFlight({ mesh, birds, flap, size, lens, bird, soar: bird && look.soar, silent: !bird || look.silent, calls, lensClear });
229
+ }
230
+
231
+ const GRAVITY = 9.8, // metres a second a second
232
+ MAX_BANK = 0.7, // radians
233
+ BEAT_EASE = 1.5, // how quickly a bird goes from beating to gliding and back, a share a second
234
+ BOB = 9, // metres a bird rises and falls above and below its own height in the flock, at most
235
+ BOB_SHARE = 3, // and at most this many times the flock's height, so that a flock gathered low stays off the ground
236
+ BOB_RATE = 0.3, // radians a second of that rise and fall
237
+ SOAR_BEAT = 0.12; // how hard a soaring bird beats at most
238
+
239
+ function flockFlight({ mesh, birds, flap, size, lens, bird, soar, silent, calls, lensClear }) {
240
+ const n = birds.length,
241
+ place = placement(),
242
+ clear = lensClear * SPAN * size, // metres
243
+ heard = calls?.heard ?? 80,
244
+ gap = calls?.gap ?? [4, 12];
245
+ let last = null,
246
+ nextCall = 0;
247
+ place.s = size;
248
+ // A bird nearer the lens than `clear` metres is pushed out along the line from it to that distance.
249
+ function keepFromLens(p) {
250
+ const at = lens.position,
251
+ dx = p.x - at.x,
252
+ dy = p.y - at.y,
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);
257
+ p.x = at.x + dx * k;
258
+ p.y = at.y + dy * k;
259
+ p.z = at.z + dz * k;
260
+ }
261
+ // now and then one bird of a flock being drawn calls from where it flies, if the listener is near enough to hear it
262
+ function call(t, loud) {
263
+ if (!calls || silent || t < nextCall) return;
264
+ nextCall = t + gap[0] + Math.random() * (gap[1] - gap[0]);
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];
270
+ if (Math.hypot(x - lens.position.x, y - lens.position.y, z - lens.position.z) > heard) return;
271
+ calls.sound(x, y, z, loud);
272
+ }
273
+ return {
274
+ mesh,
275
+ birds,
276
+ distantGeometry: mesh.geometry,
277
+ // the most metres a bird flies above or below the flock's middle when update is given this height
278
+ reach: height => height + Math.min(BOB, BOB_SHARE * height),
279
+ // Each bird rounds the centre on a circle of its own, rising and falling slowly; t is the flock's clock in seconds. airspeed, in
280
+ // metres a second, flies every bird at about that pace however wide the flock wheels; without it each bird keeps a turn of its
281
+ // own, so that a wider circle is flown faster.
282
+ update(cx, cy, cz, radius, height, t, airspeed = 0) {
283
+ const first = last === null,
284
+ dt = first ? 0 : Math.min(0.1, Math.max(0, t - last)),
285
+ bob = Math.min(BOB, BOB_SHARE * height);
286
+ last = t;
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;
299
+ place.ry = yaw;
300
+ if (!bird) {
301
+ place.rx = 0;
302
+ place.rz = dir * 0.28; // banked slightly into the turn
303
+ placeBankedAt(mesh.instanceMatrix.array, i * 16, place);
304
+ continue;
305
+ }
306
+ const speed = Math.abs(turn) * r,
307
+ climb = bob * BOB_RATE * Math.cos(t * BOB_RATE + b.wob), // metres a second up
308
+ bank = Math.min(MAX_BANK, Math.atan((speed * Math.abs(turn)) / GRAVITY)) * dir,
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);
311
+ keepFromLens(place);
312
+ place.rx = -pitch;
313
+ place.rz = bank;
314
+ placeBankedAt(mesh.instanceMatrix.array, i * 16, place);
315
+ flap.array[i * 2] = b.ph * 3;
316
+ flap.array[i * 2 + 1] = b.beat;
317
+ }
318
+ mesh.instanceMatrix.needsUpdate = true;
319
+ if (flap) flap.needsUpdate = true;
320
+ if (!first) call(t, Math.min(1.4, 0.6 + size * 0.5));
321
+ },
322
+ };
323
+ }
@@ -0,0 +1,110 @@
1
+ import { CREASED, PLAIN } from '../geometry/garment-tags.js';
2
+ import { FOLD_SLOPES } from '../math/cloth-textures.js';
3
+
4
+ const FOLDS = {
5
+ hang: [1, 0.3], // the long folds' texture tiles round the body (once: no fold comes twice) and for each metre up it
6
+ crumple: [5, 4.2], // the crumple's, and how far it leans round the body for each metre up it
7
+ lean: 1.3,
8
+ deep: [0.014, 0.009], // how far each stands out at its fullest (metres)
9
+ smooth: 0.28, // how much of the long folds cloth that lies smooth has, of what cloth hanging in folds has
10
+ shade: [0.55, 0.45], // how much darker the foot of a long fold is, and a crease of the crumple
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
+ slope: FOLD_SLOPES, // the steepest slope each field's byte holds (of its height, for each tile)
13
+ };
14
+ const f = n => n.toFixed(4);
15
+ // What each kind of garment is to the eye (KIND order): how much of the
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
+ // the crumple it takes, how rough it is (a plain weave keeps the material's own: 0) and how much a metal.
18
+ const KINDS = [
19
+ [0, 1, 1, 1, 0, 0], // wool: the weave as it is, soft and matte
20
+ [0.55, 1.14, 1, 1.35, 0.84, 0], // linen: a finer, paler weave, which crumples more
21
+ [0.82, 1.04, 1.05, 0.6, 0.6, 0], // silk: its fine weave barely seen, and a soft sheen that moves with the light over its folds
22
+ [0.92, 0.96, 0.22, 0.75, 0.56, 0], // leather: smooth, creased where it bends, with a low sheen
23
+ [1, 0.87, 0, 0, 0.48, 0.48], // mail: small links over a dark backing
24
+ [1, 1.12, 0, 0, 0.34, 0.6], // plate: smooth and bright, no folds
25
+ ],
26
+ WEAVE_TONE = 0.76, // the tone of the plain weave (the cloth texture's, with its gain), which a smoothed kind keeps
27
+ LEATHER = { grain: 0.14, worn: 0.18 },
28
+ PLATE_WEAR = { face: 0.22, edge: 0.38 },
29
+ MAIL = { around: 120, up: 210, fade: [0.18, 0.44] }, // links to the circumference and rows per metre
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])})`;
32
+ // What the fragment shader declares: the textures; the folds' slopes across the screen (metres of height a pixel), which
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
+ const FOLD_PARS = `/* glsl */
35
+ uniform sampler2D uFolds, uCrumple;
36
+ vec2 gFold = vec2(0.0);
37
+ float gWorn = 0.0;
38
+ float mailLink(vec2 uv, float row, float aa) {
39
+ float x = fract(uv.x + 0.5 * mod(row, 2.0)) - 0.5;
40
+ float y = uv.y - row - 0.5;
41
+ float radius = length(vec2(x + y * (0.24 - 0.48 * mod(row, 2.0)), y * 0.74));
42
+ return 1.0 - smoothstep(0.052, 0.052 + aa, abs(radius - 0.42));
43
+ }`;
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
+ // the folds' slopes, and their shade.
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))));
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); }
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;
52
+ // how far round the body, and up it, a pixel to the right and a pixel up are
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;
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)};
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));
63
+ // Plate's tag can mark an exposed edge. The existing crease field breaks up its polish.
64
+ float plate = step(4.5, kind);
65
+ float edge = plate * smoothstep(0.65, 0.96, crease) * mix(0.6, 1.0, crumple.b);
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));
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);
72
+ if (detail > 0.0) {
73
+ vec2 uv = vec2(turn * ${f(MAIL.around)}, vL.y * ${f(MAIL.up)});
74
+ float row = floor(uv.y), aa = max(0.02, pixel * 0.5);
75
+ // Alternating rows lean and stagger so adjacent links meet on the half-row.
76
+ float link = max(mailLink(uv, row, aa), mailLink(uv, row + (fract(uv.y) < 0.5 ? -1.0 : 1.0), aa));
77
+ diffuseColor.rgb *= 1.0 + detail * (link - 0.4) * 0.75;
78
+ gH += detail * (link - 0.4) * 0.10;
79
+ gWorn = detail * link * 0.32;
80
+ }
81
+ }
82
+ } }`;
83
+ // After the material's own roughness and metalness are known: the garment's.
84
+ const GARMENT_SHEEN = `/* glsl */
85
+ { float worn = -vSk, kind = floor(worn + 0.001), rough = ${kindOf(4)};
86
+ if (worn > 0.05) { if (rough > 0.0) roughnessFactor = rough - 0.16 * gWorn; metalnessFactor = ${kindOf(5)}; } }`;
87
+ // After the surface's normal is known: turned down the folds' slopes.
88
+ const FOLD_RELIEF = `/* glsl */
89
+ { vec3 fgx = dFdx(-vViewPosition), fgy = dFdy(-vViewPosition), f1 = cross(fgy, normal), f2 = cross(normal, fgx);
90
+ float fdet = dot(fgx, f1);
91
+ vec3 fn = abs(fdet) * normal - sign(fdet) * (gFold.x * f1 + gFold.y * f2); float fl = dot(fn, fn);
92
+ if (fl > 1e-16) normal = fn * inversesqrt(fl); }`;
93
+
94
+ // GLSL stages for a standard material. The host supplies its object-space position vL,
95
+ // garment tag vSk, sampled weave tx, and scalar relief gH.
96
+ function garmentShader({ scale = 1, threads = 64, smoothness = 'vSk', textureSize = 256 } = {}) {
97
+ const sampleArea = (textureSize * textureSize).toFixed(1);
98
+ const weaveScale = typeof scale === 'number' ? scale.toFixed(4) : scale;
99
+ return {
100
+ declarations: FOLD_PARS,
101
+ color: `/* glsl */
102
+ { float sk = clamp(${smoothness}, 0.0, 1.0); diffuseColor.rgb = mix(diffuseColor.rgb, vec3(1.14 + 0.12 * tx.g), sk); gH = mix(gH, 0.5, sk); }
103
+ gH = mix(gH, 0.5, smoothstep(0.15, 0.45, length(fwidth(vViewPosition)) * ${weaveScale} * ${threads.toFixed(1)}));
104
+ ${GARMENT_FOLDS.replaceAll('65536.0', sampleArea)}`,
105
+ normal: FOLD_RELIEF,
106
+ roughness: `roughnessFactor = mix(mix(roughnessFactor, 0.56, clamp(${smoothness}, 0.0, 1.0)), 0.1, clamp(${smoothness} - 1.0, 0.0, 1.0));`,
107
+ metalness: GARMENT_SHEEN,
108
+ };
109
+ }
110
+ export { garmentShader };
@@ -0,0 +1,74 @@
1
+ import * as THREE from 'three';
2
+
3
+ const PULL = [0.25, 0.004]; // how far each end is drawn towards the viewer: metres, and a share of its distance
4
+
5
+ const VERTEX = /* glsl */ `
6
+ attribute vec3 from;
7
+ attribute vec3 to;
8
+ uniform vec2 screen;
9
+ uniform float width;
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;
16
+ }
17
+ void main() {
18
+ vec4 a = seen(from), b = seen(to);
19
+ across = position.y;
20
+ if (a.w <= 0.0 || b.w <= 0.0) {
21
+ gl_Position = vec4(0.0, 0.0, 2.0, 1.0); // an end behind the viewer: the segment is not drawn
22
+ return;
23
+ }
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)),
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;
32
+ }`;
33
+
34
+ const FRAGMENT = /* glsl */ `
35
+ uniform vec3 color;
36
+ uniform float opacity;
37
+ varying float across;
38
+ void main() {
39
+ gl_FragColor = vec4(color * opacity * exp(-4.0 * across * across), 1.0);
40
+ }`;
41
+
42
+ // A mesh of glowing lines along `segments`, a flat list [ax, ay, az, bx, by, bz, ...] in its parent's frame. Its material's
43
+ // uniforms are set by its owner: screen (the picture's size in pixels), width (pixels from the line's middle to where its
44
+ // light has gone), color and opacity.
45
+ function glowLines(segments, color) {
46
+ const count = segments.length / 6,
47
+ quad = new THREE.InstancedBufferGeometry(),
48
+ ends = new Float32Array(segments);
49
+ quad.setAttribute('position', new THREE.BufferAttribute(new Float32Array([0, -1, 0, 1, -1, 0, 1, 1, 0, 0, 1, 0]), 3));
50
+ quad.setIndex([0, 1, 2, 0, 2, 3]);
51
+ const shared = new THREE.InstancedInterleavedBuffer(ends, 6, 1);
52
+ quad.setAttribute('from', new THREE.InterleavedBufferAttribute(shared, 3, 0));
53
+ quad.setAttribute('to', new THREE.InterleavedBufferAttribute(shared, 3, 3));
54
+ quad.instanceCount = count;
55
+ const material = new THREE.ShaderMaterial({
56
+ vertexShader: VERTEX,
57
+ fragmentShader: FRAGMENT,
58
+ uniforms: {
59
+ screen: { value: new THREE.Vector2(1, 1) },
60
+ width: { value: 3 },
61
+ color: { value: new THREE.Color(color) },
62
+ opacity: { value: 1 },
63
+ },
64
+ transparent: true,
65
+ depthWrite: false,
66
+ blending: THREE.AdditiveBlending,
67
+ toneMapped: false,
68
+ });
69
+ const mesh = new THREE.Mesh(quad, material);
70
+ mesh.frustumCulled = false; // its quads are placed by the shader; there are few of them
71
+ return mesh;
72
+ }
73
+
74
+ export { glowLines };
@@ -0,0 +1,90 @@
1
+ import * as THREE from 'three';
2
+
3
+ const LEAST_HEIGHT = 0.6, // metres: a line nearer the foot than this hangs no sheet (a ring at the foot itself)
4
+ PULL = 0.06; // metres each sheet is drawn towards the viewer, so the wall it lies on never hides it
5
+
6
+ const VERTEX = /* glsl */ `
7
+ attribute vec3 side;
8
+ varying vec3 vSide, vTo;
9
+ varying float vUp;
10
+ #include <fog_pars_vertex>
11
+ void main() {
12
+ vec4 w = modelMatrix * vec4(position, 1.0);
13
+ vUp = uv.y;
14
+ vSide = normalize(mat3(modelMatrix) * side);
15
+ vTo = cameraPosition - w.xyz;
16
+ vec4 mvPosition = viewMatrix * w;
17
+ mvPosition.xyz *= 1.0 - ${PULL.toFixed(2)} / max(length(mvPosition.xyz), 1.0);
18
+ gl_Position = projectionMatrix * mvPosition;
19
+ #include <fog_vertex>
20
+ }`;
21
+
22
+ const FRAGMENT = /* glsl */ `
23
+ uniform vec3 color;
24
+ uniform float opacity, wash, rim;
25
+ varying vec3 vSide, vTo;
26
+ varying float vUp;
27
+ #include <fog_pars_fragment>
28
+ void main() {
29
+ float facing = abs(dot(vSide, normalize(vTo))), edge = 1.0 - facing;
30
+ float light = (wash + rim * edge * edge * edge) * smoothstep(0.0, 0.35, vUp) * (0.8 + 0.2 * vUp);
31
+ gl_FragColor = vec4(color * opacity * light, 1.0);
32
+ #include <fog_fragment>
33
+ }`;
34
+
35
+ // A mesh of glowing sheets hung from `segments` (a flat list [ax, ay, az, bx, by, bz, ...] in its parent's frame) down to
36
+ // footAt(x, z), the height of the building's foot under each end. Its material's uniforms are set by its owner: color, opacity, and how much of it is wash and how much rim.
37
+ function glowShell(segments, footAt, color) {
38
+ const position = [],
39
+ side = [],
40
+ uv = [];
41
+ for (let i = 0; i < segments.length; i += 6) {
42
+ const [ax, ay, az, bx, by, bz] = segments.slice(i, i + 6),
43
+ len = Math.hypot(bx - ax, bz - az),
44
+ fa = footAt(ax, az),
45
+ fb = footAt(bx, bz);
46
+ if (Math.min(ay - fa, by - fb) < LEAST_HEIGHT || len < 0.01) continue;
47
+ const nx = -(bz - az) / len,
48
+ nz = (bx - ax) / len,
49
+ // two triangles: the top edge along the line, the bottom at the foot
50
+ corners = [
51
+ [ax, ay, az, 1],
52
+ [bx, by, bz, 1],
53
+ [bx, fb, bz, 0],
54
+ [ax, ay, az, 1],
55
+ [bx, fb, bz, 0],
56
+ [ax, fa, az, 0],
57
+ ];
58
+ for (const [x, y, z, up] of corners) {
59
+ position.push(x, y, z);
60
+ side.push(nx, 0, nz);
61
+ uv.push(0, up);
62
+ }
63
+ }
64
+ if (!position.length) return null;
65
+ const geometry = new THREE.BufferGeometry();
66
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(position, 3));
67
+ geometry.setAttribute('side', new THREE.Float32BufferAttribute(side, 3));
68
+ geometry.setAttribute('uv', new THREE.Float32BufferAttribute(uv, 2));
69
+ const material = new THREE.ShaderMaterial({
70
+ vertexShader: VERTEX,
71
+ fragmentShader: FRAGMENT,
72
+ uniforms: {
73
+ ...THREE.UniformsUtils.clone(THREE.UniformsLib.fog),
74
+ color: { value: new THREE.Color(color) },
75
+ opacity: { value: 1 },
76
+ wash: { value: 0.1 },
77
+ rim: { value: 0.6 },
78
+ },
79
+ defines: { FOG_FADES: '' }, // Applications can use this define to attenuate additive fog.
80
+ fog: true,
81
+ transparent: true,
82
+ depthWrite: false,
83
+ side: THREE.DoubleSide,
84
+ blending: THREE.AdditiveBlending,
85
+ toneMapped: false,
86
+ });
87
+ return new THREE.Mesh(geometry, material);
88
+ }
89
+
90
+ export { glowShell };
@@ -0,0 +1,67 @@
1
+ import { HAIR_NEAR, HAIR_FAR, STRANDS } from '../geometry/hair-tags.js';
2
+
3
+ const f = n => n.toFixed(4);
4
+ const LOOK = {
5
+ fade: [0.22, 0.6], // clumps to a pixel from which the strands fade, and are gone
6
+ stand: [0.1, 0.3], // and from which the clumps no longer stand out of the surface
7
+ fine: 3, // finer strands to a clump
8
+ even: 0.945, // the tone a far body's hair has, the cloth's weave being part of it there: a near body's has no weave
9
+ tone: 0.34, // how much lighter and darker the strands are than one another
10
+ relief: 0.5, // how far they stand out of the surface (of the weave's own relief)
11
+ along: [95, 140, 88], // a strand's tone wanders along its length: its phase by where it is (a metre)
12
+ sheen: [8, 20], // metres from the eye over which the strands' own light fades
13
+ rough: 0.5, // how rough hair is to the light
14
+ lie: 0.5, // how far the light's band is drawn out across the strands (0: a round spot)
15
+ shift: 0.55, // how far the band shifts from one strand to the next
16
+ };
17
+ // A point's own tag as every other part of the material reads it: near hair's is far hair's.
18
+ const HAIR_PARS = `
19
+ float gHair = 0.0, gSheen = 0.0, gClump = 0.5; vec2 gAcross = vec2(0.0);
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})`;
22
+ // In the surface's colour, once the tag (vSk) and the point as built (vL) are known: the strands' tone and relief.
23
+ const HAIR_STRANDS = `/* glsl */
24
+ { vec2 slope = vec2(dFdx(vSk), dFdy(vSk)) * ${f(STRANDS.clumps)};
25
+ if (vSk > ${f(HAIR_NEAR - 0.5)}) {
26
+ float thick = length(slope);
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)});
31
+ gH = 0.5;
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);
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);
41
+ // the fine strands are a tone only; the clumps also stand out of the surface and shift the light, while they are
42
+ // wide enough on the screen for their slopes to be read from one pixel to the next
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);
47
+ }
48
+ } }`;
49
+ // After the material's own roughness is known.
50
+ const HAIR_SHEEN = `/* glsl */
51
+ if (vSk > ${f(HAIR_NEAR - 0.5)}) roughnessFactor = mix(roughnessFactor, ${f(LOOK.rough)}, gSheen);`;
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
+ // the light on a head of hair lies in a band across the strands.
54
+ const HAIR_LIE = `/* glsl */
55
+ if (gSheen > 0.0 && dot(gAcross, gAcross) > 1e-12) {
56
+ vec3 hgx = dFdx(-vViewPosition), hgy = dFdy(-vViewPosition), h1 = cross(hgy, normal), h2 = cross(normal, hgx);
57
+ vec3 climb = sign(dot(hgx, h1)) * (gAcross.x * h1 + gAcross.y * h2), strand = cross(normal, climb);
58
+ float sl = dot(strand, strand);
59
+ if (sl > 1e-20) {
60
+ strand = normalize(strand * inversesqrt(sl) + normal * (${f(LOOK.shift)} * gHair * (gClump - 0.5)));
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);
63
+ normal = lie * inversesqrt(max(dot(lie, lie), 1e-8));
64
+ }
65
+ }`;
66
+
67
+ export { HAIR_LIE, HAIR_PARS, HAIR_SHEEN, HAIR_STRANDS, smoothOf };