@wildshard/kit 0.0.0-stage → 0.4.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 (129) hide show
  1. package/GUIDE.md +221 -0
  2. package/content/abilities/bite.json +24 -0
  3. package/content/abilities/charge.json +25 -0
  4. package/content/abilities/claw.json +24 -0
  5. package/content/abilities/slam.json +24 -0
  6. package/content/abilities/swing.json +25 -0
  7. package/content/animated/bear/pbr/black-blaze.glb +0 -0
  8. package/content/animated/bear/pbr/black-blaze.json +1 -0
  9. package/content/animated/bear/pbr/black-old.glb +0 -0
  10. package/content/animated/bear/pbr/black-old.json +1 -0
  11. package/content/animated/bear/pbr/black.glb +0 -0
  12. package/content/animated/bear/pbr/black.json +1 -0
  13. package/content/animated/bear/pbr/brown-old.glb +0 -0
  14. package/content/animated/bear/pbr/brown-old.json +1 -0
  15. package/content/animated/bear/pbr/brown.glb +0 -0
  16. package/content/animated/bear/pbr/brown.json +1 -0
  17. package/content/animated/bear/toon/black-blaze.glb +0 -0
  18. package/content/animated/bear/toon/black-blaze.json +1 -0
  19. package/content/animated/bear/toon/black-old.glb +0 -0
  20. package/content/animated/bear/toon/black-old.json +1 -0
  21. package/content/animated/bear/toon/black.glb +0 -0
  22. package/content/animated/bear/toon/black.json +1 -0
  23. package/content/animated/bear/toon/brown-old.glb +0 -0
  24. package/content/animated/bear/toon/brown-old.json +1 -0
  25. package/content/animated/bear/toon/brown.glb +0 -0
  26. package/content/animated/bear/toon/brown.json +1 -0
  27. package/content/animated/boar/pbr/big.glb +0 -0
  28. package/content/animated/boar/pbr/big.json +1 -0
  29. package/content/animated/boar/pbr/black.glb +0 -0
  30. package/content/animated/boar/pbr/black.json +1 -0
  31. package/content/animated/boar/pbr/boar.glb +0 -0
  32. package/content/animated/boar/pbr/boar.json +1 -0
  33. package/content/animated/boar/pbr/ironhide.glb +0 -0
  34. package/content/animated/boar/pbr/ironhide.json +1 -0
  35. package/content/animated/boar/pbr/scarback.glb +0 -0
  36. package/content/animated/boar/pbr/scarback.json +1 -0
  37. package/content/animated/boar/pbr/sow.glb +0 -0
  38. package/content/animated/boar/pbr/sow.json +1 -0
  39. package/content/animated/boar/toon/big.glb +0 -0
  40. package/content/animated/boar/toon/big.json +1 -0
  41. package/content/animated/boar/toon/black.glb +0 -0
  42. package/content/animated/boar/toon/black.json +1 -0
  43. package/content/animated/boar/toon/boar.glb +0 -0
  44. package/content/animated/boar/toon/boar.json +1 -0
  45. package/content/animated/boar/toon/ironhide.glb +0 -0
  46. package/content/animated/boar/toon/ironhide.json +1 -0
  47. package/content/animated/boar/toon/scarback.glb +0 -0
  48. package/content/animated/boar/toon/scarback.json +1 -0
  49. package/content/animated/boar/toon/sow.glb +0 -0
  50. package/content/animated/boar/toon/sow.json +1 -0
  51. package/content/animated/catalogue.json +1 -0
  52. package/content/bodies/biped.json +5 -0
  53. package/content/bodies/quadruped.json +5 -0
  54. package/content/clips/quadruped.json +1 -0
  55. package/content/creatures/bear/look.json +199 -0
  56. package/content/creatures/bear/pbr/black-blaze.glb +0 -0
  57. package/content/creatures/bear/pbr/black-old.glb +0 -0
  58. package/content/creatures/bear/pbr/black.glb +0 -0
  59. package/content/creatures/bear/pbr/brown-old.glb +0 -0
  60. package/content/creatures/bear/pbr/brown.glb +0 -0
  61. package/content/creatures/bear/toon/black-blaze.glb +0 -0
  62. package/content/creatures/bear/toon/black-old.glb +0 -0
  63. package/content/creatures/bear/toon/black.glb +0 -0
  64. package/content/creatures/bear/toon/brown-old.glb +0 -0
  65. package/content/creatures/bear/toon/brown.glb +0 -0
  66. package/content/creatures/bear.json +257 -0
  67. package/content/creatures/boar/look.json +184 -0
  68. package/content/creatures/boar/pbr/big.glb +0 -0
  69. package/content/creatures/boar/pbr/black.glb +0 -0
  70. package/content/creatures/boar/pbr/boar.glb +0 -0
  71. package/content/creatures/boar/pbr/ironhide.glb +0 -0
  72. package/content/creatures/boar/pbr/scarback.glb +0 -0
  73. package/content/creatures/boar/pbr/sow.glb +0 -0
  74. package/content/creatures/boar/toon/big.glb +0 -0
  75. package/content/creatures/boar/toon/black.glb +0 -0
  76. package/content/creatures/boar/toon/boar.glb +0 -0
  77. package/content/creatures/boar/toon/ironhide.glb +0 -0
  78. package/content/creatures/boar/toon/scarback.glb +0 -0
  79. package/content/creatures/boar/toon/sow.glb +0 -0
  80. package/content/creatures/boar.json +275 -0
  81. package/content/creatures/bodies.json +4754 -0
  82. package/content/harvest-items.json +98 -0
  83. package/content/harvest.json +31 -0
  84. package/content/journal-icons/antlers.png +0 -0
  85. package/content/journal-icons/bear.png +0 -0
  86. package/content/journal-icons/boar.png +0 -0
  87. package/content/journal-icons/deer.png +0 -0
  88. package/content/journal-icons/elk.png +0 -0
  89. package/content/journal-icons/ghost.png +0 -0
  90. package/content/journal-icons/ironhide.png +0 -0
  91. package/content/journal-icons.json +9 -0
  92. package/content/shared/pickupCatalogue.json +1008 -0
  93. package/content/shared/starterBow.json +21 -0
  94. package/content/species/bear.json +144 -0
  95. package/content/species/boar.json +147 -0
  96. package/content/sword-debris.json +7 -0
  97. package/content/temperaments.json +519 -0
  98. package/content/tuning/bear.json +43 -0
  99. package/content/tuning/boar.json +32 -0
  100. package/content/tuning/deer.json +32 -0
  101. package/content/tuning/pursue.json +12 -0
  102. package/package.json +63 -3
  103. package/src/abilities.ts +85 -0
  104. package/src/body.ts +25 -0
  105. package/src/clips.ts +139 -0
  106. package/src/creatures.ts +76 -0
  107. package/src/geometry/arms.ts +725 -0
  108. package/src/geometry/bear.ts +521 -0
  109. package/src/geometry/boar.ts +604 -0
  110. package/src/geometry/bowProfile.ts +34 -0
  111. package/src/geometry/export.ts +101 -0
  112. package/src/geometry/meleeProfile.ts +261 -0
  113. package/src/geometry/nativeInteractionFacts.ts +7 -0
  114. package/src/geometry/nativeLooks.ts +54 -0
  115. package/src/geometry/riderArms.ts +215 -0
  116. package/src/geometry/sharedSpecs.ts +26 -0
  117. package/src/geometry/species.ts +768 -0
  118. package/src/geometry/specimenCatalogue.ts +80 -0
  119. package/src/geometry/starterCombat.ts +194 -0
  120. package/src/geometry/starterGlyphs.ts +12 -0
  121. package/src/geometry/starterMeleeProfile.ts +134 -0
  122. package/src/geometry/starterMoves.ts +172 -0
  123. package/src/index.ts +26 -0
  124. package/src/legacyCombat.ts +25 -0
  125. package/src/legacyHarvest.ts +104 -0
  126. package/src/legacyJournalIcons.ts +10 -0
  127. package/src/legacySpecies.ts +127 -0
  128. package/src/temperament.ts +97 -0
  129. package/README.md +0 -3
@@ -0,0 +1,725 @@
1
+ import * as THREE from 'three'
2
+ import { mergeGeometries } from 'three/examples/jsm/utils/BufferGeometryUtils.js'
3
+
4
+ const lin = (hex: number): THREE.Color =>
5
+ new THREE.Color(hex).convertSRGBToLinear().multiplyScalar(1)
6
+
7
+ /** Build-time geometry for first-person arms.
8
+ * Numeric colours for a glove, cuff and sleeve builder.
9
+ * Hex colours use the authored colour conversion; triples are linear RGB. */
10
+ export type ArmPaletteKey =
11
+ | 'leather'
12
+ | 'leatherLight'
13
+ | 'leatherDark'
14
+ | 'leatherEdge'
15
+ | 'thread'
16
+ | 'jade'
17
+ | 'jadeDark'
18
+ | 'fleece'
19
+ | 'fleeceShade'
20
+ | 'fleeceDeep'
21
+ | 'wool'
22
+ | 'woolShade'
23
+ | 'red'
24
+ | 'redDeep'
25
+ | 'redLine'
26
+ | 'gold'
27
+ export type ArmStyle = Readonly<
28
+ Record<ArmPaletteKey, number | readonly [number, number, number]>
29
+ >
30
+ const armColour = (value: ArmStyle[ArmPaletteKey]): THREE.Color =>
31
+ typeof value === 'number' ? lin(value) : new THREE.Color(...value)
32
+ /** Build independent colours; one bake never mutates another bake's palette. */
33
+ export function armPalette(
34
+ style: ArmStyle,
35
+ ): Record<ArmPaletteKey, THREE.Color> {
36
+ return {
37
+ leather: armColour(style.leather),
38
+ leatherLight: armColour(style.leatherLight),
39
+ leatherDark: armColour(style.leatherDark),
40
+ leatherEdge: armColour(style.leatherEdge),
41
+ thread: armColour(style.thread),
42
+ jade: armColour(style.jade),
43
+ jadeDark: armColour(style.jadeDark),
44
+ fleece: armColour(style.fleece),
45
+ fleeceShade: armColour(style.fleeceShade),
46
+ fleeceDeep: armColour(style.fleeceDeep),
47
+ wool: armColour(style.wool),
48
+ woolShade: armColour(style.woolShade),
49
+ red: armColour(style.red),
50
+ redDeep: armColour(style.redDeep),
51
+ redLine: armColour(style.redLine),
52
+ gold: armColour(style.gold),
53
+ }
54
+ }
55
+
56
+ // ───────────────────────────── the loft ─────────────────────────────
57
+
58
+ type Paint = (v: number, a: number, out: THREE.Color) => THREE.Color
59
+ interface LoftOpts {
60
+ /** superellipse exponent of the section: 2 = ellipse, higher = squarer (a hand's back) */
61
+ squareness?: number
62
+ capStart?: boolean
63
+ capEnd?: boolean
64
+ }
65
+ const _t = new THREE.Vector3(),
66
+ _u = new THREE.Vector3(),
67
+ _s = new THREE.Vector3(),
68
+ _c = new THREE.Color()
69
+ /**
70
+ * Sweep a section along centre points. `ups[i]` orients the section's "thickness" axis (the rest is its "width" axis
71
+ * = tangent × up). `w[i]` / `h[i]` are the half width / half thickness. Indexed, a duplicated seam (so `a` paints
72
+ * round), smooth normals.
73
+ */
74
+ function loft(
75
+ centers: THREE.Vector3[],
76
+ ups: THREE.Vector3[],
77
+ w: number[],
78
+ h: number[],
79
+ radial: number,
80
+ paint: Paint,
81
+ opts: LoftOpts = {},
82
+ ): THREE.BufferGeometry {
83
+ const n = centers.length,
84
+ e = opts.squareness ?? 2
85
+ const pos: number[] = [],
86
+ col: number[] = [],
87
+ idx: number[] = []
88
+ for (let i = 0; i < n; i++) {
89
+ const c = centers[i] ?? _t,
90
+ prev = centers[Math.max(0, i - 1)] ?? c,
91
+ next = centers[Math.min(n - 1, i + 1)] ?? c
92
+ _t.subVectors(next, prev).normalize()
93
+ _u.copy(ups[i] ?? ups[0] ?? _u)
94
+ .addScaledVector(_t, -(ups[i] ?? _u).dot(_t))
95
+ .normalize()
96
+ _s.crossVectors(_t, _u)
97
+ const wi = w[i] ?? 0,
98
+ hi = h[i] ?? 0
99
+ for (let k = 0; k <= radial; k++) {
100
+ const th = (k / radial) * Math.PI * 2,
101
+ ct = Math.cos(th),
102
+ st = Math.sin(th)
103
+ const cx = Math.sign(ct) * Math.abs(ct) ** (2 / e),
104
+ sy = Math.sign(st) * Math.abs(st) ** (2 / e)
105
+ pos.push(
106
+ c.x + _s.x * cx * wi + _u.x * sy * hi,
107
+ c.y + _s.y * cx * wi + _u.y * sy * hi,
108
+ c.z + _s.z * cx * wi + _u.z * sy * hi,
109
+ )
110
+ paint(n > 1 ? i / (n - 1) : 0, k / radial, _c)
111
+ col.push(_c.r, _c.g, _c.b)
112
+ }
113
+ }
114
+ const row = radial + 1
115
+ for (let i = 0; i < n - 1; i++)
116
+ for (let k = 0; k < radial; k++) {
117
+ const a = i * row + k,
118
+ b = a + 1,
119
+ d = a + row,
120
+ f = d + 1
121
+ idx.push(a, d, b, b, d, f)
122
+ }
123
+ const cap = (i: number, flip: boolean) => {
124
+ const c = centers[i]
125
+ if (c === undefined) return
126
+ const ci = pos.length / 3
127
+ pos.push(c.x, c.y, c.z)
128
+ paint(n > 1 ? i / (n - 1) : 0, 0, _c)
129
+ col.push(_c.r, _c.g, _c.b)
130
+ for (let k = 0; k < radial; k++) {
131
+ const a = i * row + k
132
+ if (flip) idx.push(ci, a + 1, a)
133
+ else idx.push(ci, a, a + 1)
134
+ }
135
+ }
136
+ if (opts.capStart === true) cap(0, false)
137
+ if (opts.capEnd === true) cap(n - 1, true)
138
+ const g = new THREE.BufferGeometry()
139
+ g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3))
140
+ g.setAttribute('color', new THREE.Float32BufferAttribute(col, 3))
141
+ g.setIndex(idx)
142
+ g.computeVertexNormals()
143
+ return g
144
+ }
145
+
146
+ /** a smooth ellipsoid painted one colour, darker underneath */
147
+ function ellipsoid(
148
+ center: THREE.Vector3,
149
+ r: THREE.Vector3,
150
+ col: THREE.Color,
151
+ q?: THREE.Quaternion,
152
+ seg = 10,
153
+ ): THREE.BufferGeometry {
154
+ const g = new THREE.SphereGeometry(1, seg, Math.max(4, Math.round(seg * 0.7)))
155
+ g.deleteAttribute('uv')
156
+ g.scale(r.x, r.y, r.z)
157
+ if (q) g.applyQuaternion(q)
158
+ g.translate(center.x, center.y, center.z)
159
+ const p = g.getAttribute('position'),
160
+ c = new Float32Array(p.count * 3)
161
+ for (let i = 0; i < p.count; i++) {
162
+ const k =
163
+ 0.82 +
164
+ 0.18 *
165
+ Math.min(1, Math.max(0, ((p.getY(i) - center.y) / r.y) * 0.5 + 0.5))
166
+ c[i * 3] = col.r * k
167
+ c[i * 3 + 1] = col.g * k
168
+ c[i * 3 + 2] = col.b * k
169
+ }
170
+ g.setAttribute('color', new THREE.BufferAttribute(c, 3))
171
+ return g
172
+ }
173
+
174
+ function mergeAll(parts: THREE.BufferGeometry[]): THREE.BufferGeometry {
175
+ for (const p of parts)
176
+ for (const k of Object.keys(p.attributes))
177
+ if (k !== 'position' && k !== 'normal' && k !== 'color')
178
+ p.deleteAttribute(k)
179
+ const g = mergeGeometries(parts, false)
180
+ g.computeBoundingSphere()
181
+ return g
182
+ }
183
+
184
+ /** flip X (a left hand from a right one): scale, re-wind, and re-derive normals */
185
+ function mirrorX(g: THREE.BufferGeometry): THREE.BufferGeometry {
186
+ g.scale(-1, 1, 1)
187
+ const idx = g.getIndex()
188
+ if (idx) {
189
+ const a = idx.array
190
+ for (let i = 0; i < a.length; i += 3) {
191
+ const t = a[i + 1] ?? 0
192
+ a[i + 1] = a[i + 2] ?? 0
193
+ a[i + 2] = t
194
+ }
195
+ idx.needsUpdate = true
196
+ }
197
+ g.computeVertexNormals()
198
+ return g
199
+ }
200
+
201
+ /** hash noise (build-time only) */
202
+ function hash(x: number, y: number): number {
203
+ const s = Math.sin(x * 127.1 + y * 311.7) * 43758.5453
204
+ return s - Math.floor(s)
205
+ }
206
+ function vnoise(x: number, y: number): number {
207
+ const xi = Math.floor(x),
208
+ yi = Math.floor(y),
209
+ xf = x - xi,
210
+ yf = y - yi
211
+ const u = xf * xf * (3 - 2 * xf),
212
+ v = yf * yf * (3 - 2 * yf)
213
+ const a = hash(xi, yi),
214
+ b = hash(xi + 1, yi),
215
+ c = hash(xi, yi + 1),
216
+ d = hash(xi + 1, yi + 1)
217
+ return a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v
218
+ }
219
+
220
+ // ───────────────────────────── the gloved fist ─────────────────────────────
221
+
222
+ export interface FistOpts {
223
+ /** radius of what the fist closes on (a bow grip ~0.02, a sabre grip 0.016, a bowstring 0.009 — a tight fist) */
224
+ R: number
225
+ mirror?: boolean
226
+ thumbRing?: boolean
227
+ /** finger-stack scale along Y (1 = 8.5 cm, a gloved man's hand) */
228
+ span?: number
229
+ /** turn about the grip (+Y), radians: aims the forearm (canonical +Z) */
230
+ yaw?: number
231
+ /** how far the fingers and the thumb close round the grip (1 = a full fist; less = an open cradle on a thick stock:
232
+ * a support hand under a thick fore-end). The knuckles stay put; only the curl shortens. */
233
+ curl?: number
234
+ /** the thumb's own curl (default: `curl`) */
235
+ thumbCurl?: number
236
+ }
237
+ /** `hook`: the point on the grip axis inside the thumb's curl — where a bowstring sits in a thumb draw */
238
+ export interface Fist {
239
+ geometry: THREE.BufferGeometry
240
+ wrist: THREE.Vector3
241
+ wristDir: THREE.Vector3
242
+ hook: THREE.Vector3
243
+ }
244
+
245
+ /** a point in grip-cylinder coordinates: angle φ (0 = +X, negative toward −Z = the front), radius ρ, height y */
246
+ const cyl = (phiDeg: number, rho: number, y: number) => {
247
+ const p = THREE.MathUtils.degToRad(phiDeg)
248
+ return new THREE.Vector3(Math.cos(p) * rho, y, Math.sin(p) * rho)
249
+ }
250
+
251
+ /** one finger (or the thumb) as a lofted tube through joint points, with knuckle bulges, a rounded tip and a stitched back */
252
+ function finger(
253
+ palette: ReturnType<typeof armPalette>,
254
+ joints: THREE.Vector3[],
255
+ r: number,
256
+ bulgeAt: number[],
257
+ leather: THREE.Color,
258
+ dashes: THREE.BufferGeometry[],
259
+ stitchFrom: number,
260
+ stitchTo: number,
261
+ ): THREE.BufferGeometry {
262
+ const curve = new THREE.CatmullRomCurve3(joints, false, 'centripetal')
263
+ const N = 26
264
+ const centers: THREE.Vector3[] = [],
265
+ ups: THREE.Vector3[] = [],
266
+ w: number[] = [],
267
+ h: number[] = []
268
+ for (let i = 0; i <= N; i++) {
269
+ const t = i / N,
270
+ p = curve.getPoint(t)
271
+ centers.push(p)
272
+ ups.push(new THREE.Vector3(p.x, 0, p.z).normalize()) // "thickness" axis = out from the grip axis: the finger's back
273
+ let k = 1 - 0.14 * t
274
+ for (const b of bulgeAt) k += 0.1 * Math.exp(-(((t - b) / 0.06) ** 2))
275
+ if (t > 0.86) k *= Math.sqrt(Math.max(0.02, 1 - ((t - 0.86) / 0.14) ** 2)) // rounded tip
276
+ w.push(r * k * 1.06)
277
+ h.push(r * k * 0.92)
278
+ }
279
+ const g = loft(
280
+ centers,
281
+ ups,
282
+ w,
283
+ h,
284
+ 12,
285
+ (v, a, out) => {
286
+ const back = Math.sin(a * Math.PI * 2) // +1 on the back, −1 facing the grip
287
+ out.copy(leather).lerp(palette.leatherLight, Math.max(0, back) * 0.35)
288
+ for (const b of bulgeAt)
289
+ out.lerp(
290
+ palette.leatherLight,
291
+ 0.35 * Math.exp(-(((v - b) / 0.05) ** 2)) * Math.max(0, back),
292
+ )
293
+ if (back < -0.2) out.lerp(palette.leatherDark, 0.3 * -back) // the crease against the grip
294
+ const side = Math.abs(Math.cos(a * Math.PI * 2))
295
+ out.multiplyScalar(1 - 0.2 * side * side) // dark between the fingers
296
+ return out
297
+ },
298
+ { capEnd: true },
299
+ )
300
+ // stitching: a dashed seam of pale thread down the finger's back
301
+ for (let t = stitchFrom; t <= stitchTo; t += 0.07) {
302
+ const p = curve.getPoint(t),
303
+ tan = curve.getTangent(t),
304
+ out = new THREE.Vector3(p.x, 0, p.z).normalize()
305
+ const c = p.clone().addScaledVector(out, r * 0.93)
306
+ dashes.push(
307
+ ellipsoid(
308
+ c,
309
+ new THREE.Vector3(0.0009, 0.0009, 0.0026),
310
+ palette.thread,
311
+ new THREE.Quaternion().setFromUnitVectors(
312
+ new THREE.Vector3(0, 0, 1),
313
+ tan,
314
+ ),
315
+ 5,
316
+ ),
317
+ )
318
+ }
319
+ return g
320
+ }
321
+
322
+ export function gloveFist(style: ArmStyle, opts: FistOpts): Fist {
323
+ const R = opts.R,
324
+ sp = (opts.span ?? 1) * 1.12
325
+ const parts: THREE.BufferGeometry[] = [],
326
+ dashes: THREE.BufferGeometry[] = []
327
+ const L = armPalette(style)
328
+ // ── the back of the hand + palm: a squarish loft from the wrist (+Z) to the knuckle row (front-right of the grip) ──
329
+ const wrist = new THREE.Vector3(R + 0.015, -0.002 * sp, 0.072)
330
+ const knuckle = cyl(-44, R + 0.019, 0.002 * sp)
331
+ const body: THREE.Vector3[] = [],
332
+ ups: THREE.Vector3[] = [],
333
+ bw: number[] = [],
334
+ bh: number[] = []
335
+ const NB = 12
336
+ for (let i = 0; i <= NB; i++) {
337
+ const t = i / NB
338
+ const p = new THREE.Vector3().lerpVectors(wrist, knuckle, t)
339
+ p.x += Math.sin(t * Math.PI) * 0.006 // the back of the hand arches
340
+ body.push(p)
341
+ const out = new THREE.Vector3(p.x, 0, p.z).normalize()
342
+ ups.push(
343
+ new THREE.Vector3()
344
+ .lerpVectors(new THREE.Vector3(1, 0, 0), out, t)
345
+ .normalize(),
346
+ )
347
+ bw.push(
348
+ (0.03 + 0.013 * Math.min(1, t * 1.4)) *
349
+ sp *
350
+ (t > 0.9 ? 1 - (t - 0.9) * 2.5 : 1),
351
+ )
352
+ bh.push(0.019 - 0.004 * t)
353
+ }
354
+ parts.push(
355
+ loft(
356
+ body,
357
+ ups,
358
+ bw,
359
+ bh,
360
+ 20,
361
+ (v, a, out) => {
362
+ const back = Math.sin(a * Math.PI * 2)
363
+ out
364
+ .copy(L.leather)
365
+ .lerp(L.leatherLight, Math.max(0, back) * 0.3 * (0.6 + 0.4 * v))
366
+ if (back < -0.3) out.lerp(L.leatherDark, 0.22) // the palm, in the grip's shadow
367
+ return out
368
+ },
369
+ { squareness: 2.6, capEnd: true },
370
+ ),
371
+ )
372
+ // back-of-hand stitching: the glove's three "points" running from the finger valleys toward the wrist
373
+ for (const yk of [0.019, 0, -0.019])
374
+ for (let t = 0.35; t <= 0.9; t += 0.09) {
375
+ const p = new THREE.Vector3().lerpVectors(wrist, knuckle, t)
376
+ p.x += Math.sin(t * Math.PI) * 0.006
377
+ const out = new THREE.Vector3(p.x, 0, p.z).normalize()
378
+ const c = p.clone().addScaledVector(out, 0.0172 - 0.004 * t)
379
+ c.y = yk * sp * (0.7 + 0.3 * t)
380
+ const dir = new THREE.Vector3().subVectors(knuckle, wrist).normalize()
381
+ dashes.push(
382
+ ellipsoid(
383
+ c,
384
+ new THREE.Vector3(0.0009, 0.0009, 0.0028),
385
+ L.thread,
386
+ new THREE.Quaternion().setFromUnitVectors(
387
+ new THREE.Vector3(0, 0, 1),
388
+ dir,
389
+ ),
390
+ 5,
391
+ ),
392
+ )
393
+ }
394
+ // ── four fingers curled round the grip: index on top (the thumb side), pinky at the bottom ──
395
+ const fingers: [number, number, number][] = [
396
+ [0.029, 0.0112, 0],
397
+ [0.0095, 0.0116, 4],
398
+ [-0.0095, 0.011, 2],
399
+ [-0.028, 0.0098, -8],
400
+ ] // y, radius, tip angle trim
401
+ const curl = opts.curl ?? 1,
402
+ fc = (deg: number): number => -18 + (deg + 18) * curl,
403
+ tcurl = opts.thumbCurl ?? curl,
404
+ tc = (deg: number): number => 28 + (deg - 28) * tcurl
405
+ for (const [fy, fr, trim] of fingers) {
406
+ const y = fy * sp
407
+ const j = [
408
+ cyl(-18, R + 0.02, y),
409
+ cyl(fc(-48), R + 0.022, y),
410
+ cyl(fc(-92), R + fr + 0.003, y),
411
+ cyl(fc(-138), R + fr + 0.001, y * 0.98),
412
+ cyl(fc(-182), R + fr, y * 0.96),
413
+ cyl(fc(-222 - trim), R + fr * 0.95, y * 0.95),
414
+ ]
415
+ parts.push(
416
+ finger(L, j, fr, [0.2, 0.5, 0.76], L.leather, dashes, 0.18, 0.62),
417
+ )
418
+ // the knuckle bump on the back of the hand
419
+ const kn = cyl(-47, R + 0.029, y)
420
+ parts.push(
421
+ ellipsoid(
422
+ kn,
423
+ new THREE.Vector3(0.0105, 0.0085, 0.0085).multiplyScalar(fr / 0.01),
424
+ L.leatherLight,
425
+ new THREE.Quaternion().setFromAxisAngle(
426
+ new THREE.Vector3(0, 1, 0),
427
+ THREE.MathUtils.degToRad(47),
428
+ ),
429
+ 8,
430
+ ),
431
+ )
432
+ }
433
+ // ── the thumb: from the heel of the palm, round the back of the grip, its tip over the index finger's middle joint ──
434
+ const ty = 0.043 * sp
435
+ const thumbJ = [
436
+ new THREE.Vector3(R + 0.012, 0.022 * sp, 0.05),
437
+ cyl(28, R + 0.02, 0.034 * sp),
438
+ cyl(tc(78), R + 0.013, ty),
439
+ cyl(tc(128), R + 0.011, ty + 0.002),
440
+ cyl(tc(176), R + 0.011, ty),
441
+ cyl(tc(204), R + 0.01, ty - 0.002),
442
+ ]
443
+ parts.push(
444
+ finger(L, thumbJ, 0.013, [0.35, 0.66], L.leather, dashes, 0.25, 0.7),
445
+ )
446
+ if (opts.thumbRing === true) {
447
+ const c = new THREE.CatmullRomCurve3(thumbJ, false, 'centripetal')
448
+ const p = c.getPoint(0.5),
449
+ tan = c.getTangent(0.5)
450
+ const ring = new THREE.TorusGeometry(0.0138, 0.0026, 8, 20)
451
+ ring.deleteAttribute('uv')
452
+ ring.applyQuaternion(
453
+ new THREE.Quaternion().setFromUnitVectors(
454
+ new THREE.Vector3(0, 0, 1),
455
+ tan,
456
+ ),
457
+ )
458
+ ring.translate(p.x, p.y, p.z)
459
+ const pa = ring.getAttribute('position'),
460
+ cc = new Float32Array(pa.count * 3)
461
+ for (let i = 0; i < pa.count; i++) {
462
+ const k = 0.5 + 0.5 * Math.sin(pa.getX(i) * 900 + pa.getY(i) * 700)
463
+ const col = L.jade.clone().lerp(L.jadeDark, k * 0.5)
464
+ cc[i * 3] = col.r
465
+ cc[i * 3 + 1] = col.g
466
+ cc[i * 3 + 2] = col.b
467
+ }
468
+ ring.setAttribute('color', new THREE.BufferAttribute(cc, 3))
469
+ parts.push(ring)
470
+ }
471
+ let g = mergeAll([...parts, ...dashes])
472
+ const wristDir = new THREE.Vector3(0.12, 0, 1).normalize()
473
+ const w = wrist.clone()
474
+ if (opts.mirror === true) {
475
+ g = mirrorX(g)
476
+ w.x = -w.x
477
+ wristDir.x = -wristDir.x
478
+ }
479
+ if (opts.yaw !== undefined && opts.yaw !== 0) {
480
+ const q = new THREE.Quaternion().setFromAxisAngle(
481
+ new THREE.Vector3(0, 1, 0),
482
+ opts.yaw,
483
+ )
484
+ g.applyQuaternion(q)
485
+ w.applyQuaternion(q)
486
+ wristDir.applyQuaternion(q)
487
+ }
488
+ g.computeBoundingSphere()
489
+ return { geometry: g, wrist: w, wristDir, hook: new THREE.Vector3(0, ty, 0) }
490
+ }
491
+
492
+ // ───────────────────────────── the arm piece: gauntlet → fleece cuff → sleeve ─────────────────────────────
493
+
494
+ const GAUNT_END = 0.07,
495
+ FLEECE_END = 0.13
496
+
497
+ /** ram's-horn scroll (0 = wool, 1 = red appliqué) in one tile: `a` across 0..1 (one wave), `b` along the band 0..1.
498
+ * A continuous wave; at each crest / trough a horn curls back into itself; a small diamond sits in each bay. */
499
+ function qoshqar(a: number, b: number): number {
500
+ const ax = 0.55 // the tile is wider round the arm than along it
501
+ const stroke = (d: number, wdt: number) =>
502
+ 1 - THREE.MathUtils.smoothstep(d, wdt * 0.55, wdt)
503
+ const wave = 0.5 + 0.26 * Math.sin(a * Math.PI * 2)
504
+ const slope = 0.26 * Math.PI * 2 * Math.cos(a * Math.PI * 2) * ax
505
+ let m = stroke(Math.abs(b - wave) / Math.sqrt(1 + slope * slope), 0.085)
506
+ for (const [cx, cy, dir] of [
507
+ [0.25, 0.76, -1],
508
+ [0.75, 0.24, 1],
509
+ ] as const) {
510
+ // the horn: an open ring hanging off the crest toward the next bay, and a dot at its heart
511
+ const hx = cx + dir * -0.1,
512
+ hy = cy + dir * 0.02
513
+ const dx = (a - hx) * ax,
514
+ dy = b - hy,
515
+ r = Math.hypot(dx, dy)
516
+ const open = dir * dx < 0.01 || dir * dy < 0.0
517
+ if (open) m = Math.max(m, stroke(Math.abs(r - 0.1), 0.06))
518
+ m = Math.max(m, stroke(r, 0.045))
519
+ }
520
+ // diamonds in the bays
521
+ for (const [cx, cy] of [
522
+ [0.75, 0.8],
523
+ [0.25, 0.2],
524
+ ] as const)
525
+ m = Math.max(
526
+ m,
527
+ stroke(Math.abs(a - cx) * ax * 1.4 + Math.abs(b - cy), 0.09),
528
+ )
529
+ // border rules
530
+ if (b < 0.06 || b > 0.94) m = 1
531
+ return Math.min(1, m)
532
+ }
533
+
534
+ /**
535
+ * The arm from the wrist, along +Y: a flared leather gauntlet with a rolled, stitched edge (0 … 7 cm), a thick fleece
536
+ * cuff (… 13 cm), then the cream wool sleeve — soft folds, red ornament bands with a little felt relief, the band
537
+ * borders in deep red — running `len` m, well out of frame.
538
+ */
539
+ export function riderArm(
540
+ style: ArmStyle,
541
+ len = 0.9,
542
+ seed = 1,
543
+ ): THREE.BufferGeometry {
544
+ const steps = riderArmSteps(style, len, seed)
545
+ for (;;) {
546
+ const r = steps.next()
547
+ if (r.done === true) return r.value
548
+ }
549
+ }
550
+
551
+ /** The rings a `riderArmSteps` stretch builds before it yields. */
552
+ const ARM_RINGS_PER_STEP = 24
553
+ /**
554
+ * `riderArm` as steps (SF67): a yield every `ARM_RINGS_PER_STEP` rings, before the normals and before the stitched edge's
555
+ * merge, so a build running between drawn frames can let one through. The same arithmetic in the same order as the one-go
556
+ * build: the bytes match it exactly. Each iterator retains its own style colours.
557
+ */
558
+ export function* riderArmSteps(
559
+ style: ArmStyle,
560
+ len = 0.9,
561
+ seed = 1,
562
+ ): Generator<void, THREE.BufferGeometry> {
563
+ const L = armPalette(style)
564
+ const RAD = 96
565
+ const ys: number[] = []
566
+ const bands: [number, number][] = [
567
+ [0.16, 0.28],
568
+ [0.44, 0.56],
569
+ ]
570
+ const inBand = (y: number) =>
571
+ bands.some(([a, b]) => y > a - 0.02 && y < b + 0.02)
572
+ for (let y = 0; y < len;) {
573
+ ys.push(y)
574
+ y +=
575
+ y < GAUNT_END
576
+ ? 0.005
577
+ : y < FLEECE_END + 0.02
578
+ ? 0.003
579
+ : inBand(y)
580
+ ? 0.003
581
+ : 0.012
582
+ }
583
+ ys.push(len)
584
+ const pos: number[] = [],
585
+ col: number[] = [],
586
+ idx: number[] = []
587
+ const c = new THREE.Color()
588
+ let rings = 0
589
+ for (const y of ys) {
590
+ if (rings > 0 && rings % ARM_RINGS_PER_STEP === 0) yield
591
+ rings++
592
+ for (let k = 0; k <= RAD; k++) {
593
+ const a = k / RAD,
594
+ ph = a * Math.PI * 2
595
+ let r: number
596
+ if (y < GAUNT_END) {
597
+ // gauntlet: flares, a rolled edge at the end
598
+ const t = y / GAUNT_END
599
+ r =
600
+ 0.033 + 0.012 * t * t + 0.0022 * Math.exp(-(((t - 0.93) / 0.05) ** 2))
601
+ c.copy(L.leather).lerp(L.leatherLight, 0.25 + 0.2 * Math.sin(ph + 0.4))
602
+ if (t > 0.86) c.lerp(L.leatherEdge, 0.6)
603
+ c.multiplyScalar(0.9 + 0.1 * vnoise(ph * 4, y * 90 + seed))
604
+ if (t < 0.12) c.lerp(L.leatherDark, 0.5 * (1 - t / 0.12))
605
+ } else if (y < FLEECE_END) {
606
+ // fleece: a fat roll of tufts
607
+ const t = (y - GAUNT_END) / (FLEECE_END - GAUNT_END)
608
+ const tuft =
609
+ vnoise(ph * 7 + seed, y * 170) * 0.7 +
610
+ vnoise(ph * 17, y * 380 + seed) * 0.3
611
+ r =
612
+ 0.046 +
613
+ 0.012 * Math.sin(t * Math.PI) ** 0.7 +
614
+ 0.006 * (tuft - 0.5) * Math.sin(t * Math.PI)
615
+ c.copy(L.fleece)
616
+ .lerp(L.fleeceShade, 0.65 * (1 - tuft))
617
+ .lerp(L.fleeceDeep, 0.5 * (1 - Math.sin(t * Math.PI)) ** 2)
618
+ } else {
619
+ // wool sleeve
620
+ const s = y - FLEECE_END
621
+ const fold =
622
+ Math.sin(ph * 3 + y * 7 + seed) * 0.6 +
623
+ Math.sin(ph * 5 - y * 11 + seed * 2) * 0.4
624
+ const bunch = Math.sin(y * 70) * Math.exp(-s * 9) // fabric bunched against the cuff
625
+ r =
626
+ 0.047 + Math.min(1, s / 0.35) * 0.014 + 0.0032 * fold + 0.0022 * bunch
627
+ c.copy(L.wool)
628
+ const band = bands.find(([a0, b0]) => y > a0 - 0.012 && y < b0 + 0.012)
629
+ if (band !== undefined) {
630
+ const [a0, b0] = band
631
+ const b = (y - a0) / (b0 - a0)
632
+ if (b < 0 || b > 1) {
633
+ c.copy(L.redDeep)
634
+ r += 0.0007
635
+ } // the border lines
636
+ else if (b < 0.1 || b > 0.9) c.copy(L.wool)
637
+ else {
638
+ const m = qoshqar((a * 6) % 1, (b - 0.1) / 0.8)
639
+ c.lerpColors(L.wool, L.red, m)
640
+ r += 0.0009 * m // felt appliqué, a hair proud
641
+ }
642
+ } else if (Math.abs(y - 0.33) < 0.006 || Math.abs(y - 0.35) < 0.004)
643
+ c.copy(L.red) // pin stripes between the bands
644
+ c.lerp(L.woolShade, 0.35 * Math.max(0, -fold) + 0.4 * Math.exp(-s * 40)) // fold valleys, the fleece's shadow
645
+ c.multiplyScalar(0.94 + 0.08 * vnoise(ph * 30, y * 200 + seed)) // wool grain
646
+ }
647
+ pos.push(Math.cos(ph) * r, y, Math.sin(ph) * r)
648
+ col.push(c.r, c.g, c.b)
649
+ }
650
+ }
651
+ const row = RAD + 1
652
+ for (let i = 0; i < ys.length - 1; i++)
653
+ for (let k = 0; k < RAD; k++) {
654
+ const a = i * row + k,
655
+ b = a + 1,
656
+ d = a + row,
657
+ e = d + 1
658
+ idx.push(a, d, b, b, d, e)
659
+ }
660
+ const g = new THREE.BufferGeometry()
661
+ g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3))
662
+ g.setAttribute('color', new THREE.Float32BufferAttribute(col, 3))
663
+ g.setIndex(idx)
664
+ yield
665
+ g.computeVertexNormals()
666
+ yield
667
+ // the gauntlet's stitched edge: a ring of pale dashes just inside the roll
668
+ const dashes: THREE.BufferGeometry[] = [g]
669
+ const ey = GAUNT_END * 0.8,
670
+ er = 0.033 + 0.012 * 0.64 + 0.0006
671
+ for (let k = 0; k < 40; k++) {
672
+ const ph = (k / 40) * Math.PI * 2
673
+ dashes.push(
674
+ ellipsoid(
675
+ new THREE.Vector3(Math.cos(ph) * er, ey, Math.sin(ph) * er),
676
+ new THREE.Vector3(0.0026, 0.0009, 0.0009),
677
+ L.thread,
678
+ new THREE.Quaternion().setFromAxisAngle(
679
+ new THREE.Vector3(0, 1, 0),
680
+ -ph + Math.PI / 2,
681
+ ),
682
+ 5,
683
+ ),
684
+ )
685
+ }
686
+ const out = mergeAll(dashes)
687
+ out.boundingSphere = new THREE.Sphere(new THREE.Vector3(0, len / 2, 0), len)
688
+ return out
689
+ }
690
+
691
+ const _y = new THREE.Vector3(0, 1, 0)
692
+ /** pose an arm mesh (built along +Y from the wrist) at `wrist`, heading along `dir` (same space as the mesh's parent) */
693
+ export function forearm(
694
+ style: ArmStyle,
695
+ dir: THREE.Vector3,
696
+ len = 0.6,
697
+ gripR = 0.017,
698
+ opts: {
699
+ mirror?: boolean
700
+ fistLen?: number
701
+ part?: 'fist' | 'arm' | 'both'
702
+ } = {},
703
+ ): THREE.BufferGeometry {
704
+ const d = dir.clone().normalize(),
705
+ part = opts.part ?? 'both'
706
+ // canonical forearm heads along +Z (+ a little +X): turn the hand about Y so it heads along d's XZ projection
707
+ const baseYaw = Math.atan2(opts.mirror === true ? -0.12 : 0.12, 1)
708
+ const yaw = Math.hypot(d.x, d.z) > 0.15 ? Math.atan2(d.x, d.z) - baseYaw : 0
709
+ const fist = gloveFist(style, {
710
+ R: gripR,
711
+ mirror: opts.mirror ?? false,
712
+ span: (opts.fistLen ?? 0.085) / 0.085,
713
+ yaw,
714
+ })
715
+ const parts: THREE.BufferGeometry[] = []
716
+ if (part !== 'arm') parts.push(fist.geometry)
717
+ if (part !== 'fist') {
718
+ const arm = riderArm(style, len, opts.mirror === true ? 2 : 1)
719
+ const start = fist.wrist.clone().addScaledVector(d, -0.018) // the gauntlet laps over the back of the hand
720
+ arm.applyQuaternion(new THREE.Quaternion().setFromUnitVectors(_y, d))
721
+ arm.translate(start.x, start.y, start.z)
722
+ parts.push(arm)
723
+ }
724
+ return parts.length === 1 && parts[0] ? parts[0] : mergeAll(parts)
725
+ }