@yaks/voxel 0.0.0-stage → 0.2.36

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 (59) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +79 -2
  3. package/_dist/boxes.d.ts +80 -0
  4. package/_dist/boxes.d.ts.map +1 -0
  5. package/_dist/chartbook.d.ts +14 -0
  6. package/_dist/chartbook.d.ts.map +1 -0
  7. package/_dist/chartsheet.d.ts +8 -0
  8. package/_dist/chartsheet.d.ts.map +1 -0
  9. package/_dist/light.d.ts +18 -0
  10. package/_dist/light.d.ts.map +1 -0
  11. package/_dist/mesh.d.ts +120 -0
  12. package/_dist/mesh.d.ts.map +1 -0
  13. package/_dist/mod.d.ts +7 -0
  14. package/_dist/mod.d.ts.map +1 -0
  15. package/_dist/stream.d.ts +33 -0
  16. package/_dist/stream.d.ts.map +1 -0
  17. package/_dist/three/mod.d.ts +5 -0
  18. package/_dist/three/mod.d.ts.map +1 -0
  19. package/_dist/three/parts.d.ts +59 -0
  20. package/_dist/three/parts.d.ts.map +1 -0
  21. package/_dist/three/puppet.d.ts +93 -0
  22. package/_dist/three/puppet.d.ts.map +1 -0
  23. package/_dist/three/soft.d.ts +27 -0
  24. package/_dist/three/soft.d.ts.map +1 -0
  25. package/boxes.js +265 -0
  26. package/boxes.js.map +1 -0
  27. package/boxes.ts +266 -0
  28. package/chartbook.js +63 -0
  29. package/chartbook.js.map +1 -0
  30. package/chartbook.ts +88 -0
  31. package/chartsheet.js +31 -0
  32. package/chartsheet.js.map +1 -0
  33. package/chartsheet.ts +32 -0
  34. package/deno.json +1 -0
  35. package/light.js +45 -0
  36. package/light.js.map +1 -0
  37. package/light.ts +49 -0
  38. package/mesh.js +577 -0
  39. package/mesh.js.map +1 -0
  40. package/mesh.ts +624 -0
  41. package/mod.js +7 -0
  42. package/mod.js.map +1 -0
  43. package/mod.ts +7 -0
  44. package/package.json +45 -4
  45. package/stream.js +80 -0
  46. package/stream.js.map +1 -0
  47. package/stream.ts +86 -0
  48. package/three/mod.js +7 -0
  49. package/three/mod.js.map +1 -0
  50. package/three/mod.ts +7 -0
  51. package/three/parts.js +181 -0
  52. package/three/parts.js.map +1 -0
  53. package/three/parts.ts +185 -0
  54. package/three/puppet.js +311 -0
  55. package/three/puppet.js.map +1 -0
  56. package/three/puppet.ts +471 -0
  57. package/three/soft.js +338 -0
  58. package/three/soft.js.map +1 -0
  59. package/three/soft.ts +382 -0
@@ -0,0 +1,471 @@
1
+ // Data-driven box rigs: jointed parts, their animation roles, and a puppet
2
+ // posed from values each frame. The caller owns world state and game rules.
3
+ // @ts-types="npm:@types/three@^0.186.0"
4
+ import * as THREE from 'three'
5
+ import type { Box } from '../boxes.ts'
6
+ import type { Vec } from '../mesh.ts'
7
+ import { knit, lunge, partOf } from './parts.js'
8
+ import { flash, soft } from './soft.js'
9
+
10
+ /** How the whole moves over the ground: striding on its legs, bounding,
11
+ * hanging in the air on beating wings, sliding on its belly, or floating. */
12
+ export type Gait = 'walk' | 'hop' | 'hover' | 'slide' | 'drift'
13
+
14
+ /** How it strikes: its body lunges and its head tosses (lunge), its head
15
+ * stabs down (peck), its arms come down overhead (smash), its head rears and
16
+ * drives forward with its body following (thrust), it swoops from the air
17
+ * (dive), it springs (leap), or it swells with light (flare). */
18
+ export type Bite =
19
+ | 'lunge'
20
+ | 'peck'
21
+ | 'smash'
22
+ | 'thrust'
23
+ | 'dive'
24
+ | 'leap'
25
+ | 'flare'
26
+
27
+ /** How it falls: over onto its side, onto its back with its legs in the air,
28
+ * back flat out as a person falls, or squashed into the ground. */
29
+ export type Fall = 'topple' | 'flip' | 'back' | 'flatten'
30
+
31
+ type None = Record<string, never>
32
+
33
+ /** A part's role as the figure moves.
34
+ * - `leg`: strides as it walks, a leg of `phase` 0.5 half a step after one
35
+ * of 0; kicks back (0) or reaches forward (0.5) as it hops; sweeps as it
36
+ * slides. A leg hung from a leg bends at the knee.
37
+ * - `arm`: swings as it walks and comes down in a smash, with whatever hangs
38
+ * from it. An arm hung from an arm bends at the elbow.
39
+ * - `wing`: beats `beat` times a second as it hovers; on the ground it rests
40
+ * as posed and opens as it pecks.
41
+ * - `tail`: wags; one curled up over the back (a sting) strikes as it bites.
42
+ * - `head`: looks about, dips now and then, and strikes as its bite says.
43
+ * - `jaw`: claws or mandibles, which open wide as it strikes.
44
+ * - `spin`: turns over and over, and swells as it flares; what hangs from it
45
+ * wheels about with it.
46
+ * - `link`: the `n`th length of a serpent's body behind its head, from 1,
47
+ * swaying on a wave that runs down it. */
48
+ export type Move =
49
+ | { leg: { phase: number } }
50
+ | { arm: { phase: number } }
51
+ | { wing: { beat: number } }
52
+ | { tail: None }
53
+ | { head: None }
54
+ | { jaw: None }
55
+ | { spin: None }
56
+ | { link: { n: number } }
57
+
58
+ /** A part: soft boxes turning about `pivot`, in the space of the part it
59
+ * hangs from (`parent`, by name) or of the figure, turned `turn` radians
60
+ * (y, then x, then z) at rest, its colour speckled in voxels of edge `cell`. */
61
+ export type Part = {
62
+ name: string
63
+ parent?: string
64
+ pivot: Vec
65
+ turn?: Vec
66
+ cell?: number
67
+ boxes: Box[]
68
+ move?: Move
69
+ }
70
+
71
+ export type Rig = {
72
+ parts: Part[]
73
+ height: number
74
+ gait: Gait
75
+ bite: Bite
76
+ fall: Fall
77
+ speckle?: number
78
+ glow?: number
79
+ }
80
+
81
+ /** What a figure is doing this frame. */
82
+ export type Act = {
83
+ /** metres a second over the ground */
84
+ speed: number
85
+ /** off the ground */
86
+ air: boolean
87
+ /** how far through a blow or a bite, 0 to 1, or -1 for none */
88
+ swing: number
89
+ /** how freshly struck, 1 just now to 0 */
90
+ hurt: number
91
+ /** how far through a roll, 0 to 1, or -1 for none */
92
+ roll: number
93
+ /** how far the head is turned from the body to look at someone, in
94
+ * radians */
95
+ look?: number
96
+ down: boolean
97
+ /** seconds, for idling */
98
+ t: number
99
+ }
100
+
101
+ /** A rig made up to be drawn and moved, told what it is doing every frame. */
102
+ export type Puppet<A extends Act = Act> = {
103
+ root: THREE.Group
104
+ material: THREE.Material
105
+ /** where a name plate sits, above the root */
106
+ height: number
107
+ animate: (a: A, dt: number) => void
108
+ }
109
+
110
+ let TAU = Math.PI * 2
111
+
112
+ type Role = keyof { [K in Move as keyof K]: 0 }
113
+
114
+ // A part as the animator holds it: its bone, its rest pose, its role and
115
+ // what goes with it, which side of the middle it is on, whether it hangs
116
+ // from a part of its own role (a shin from a thigh, a forearm from an arm),
117
+ // and how a tail lies: trailing behind, hanging below, or curled up over the
118
+ // back.
119
+ type Held = {
120
+ bone: THREE.Bone
121
+ pivot: THREE.Vector3
122
+ turn: THREE.Euler
123
+ role?: Role
124
+ phase: number
125
+ beat: number
126
+ n: number
127
+ side: number
128
+ below: boolean
129
+ lies: 'trails' | 'hangs' | 'curls'
130
+ /** for an arm, whether something hangs from it that has no role: a club */
131
+ holds: boolean
132
+ }
133
+
134
+ let roleOf = (m?: Move): Role | undefined =>
135
+ m ? Object.keys(m)[0] as Role : undefined
136
+
137
+ let param = (m: Move | undefined, k: string) =>
138
+ m ? (Object.values(m)[0] as Record<string, number>)[k] ?? 0 : 0
139
+
140
+ // The corners of a box.
141
+ let corners = (b: THREE.Box3) =>
142
+ Array.from({ length: 8 }, (_, k) =>
143
+ new THREE.Vector3(
144
+ k & 1 ? b.max.x : b.min.x,
145
+ k & 2 ? b.max.y : b.min.y,
146
+ k & 4 ? b.max.z : b.min.z,
147
+ ))
148
+
149
+ // Where the boxes of `parts` lie as the figure stands, in its space.
150
+ let bounds = (parts: { bone: THREE.Bone; boxes: Box[] }[]) => {
151
+ let b = new THREE.Box3()
152
+ for (let { bone, boxes } of parts) {
153
+ for (let [lo, size] of boxes) {
154
+ let one = new THREE.Box3().setFromArray([
155
+ ...lo,
156
+ ...lo.map((x, i) => x + size[i]),
157
+ ])
158
+ for (let c of corners(one)) {
159
+ b.expandByPoint(c.applyMatrix4(bone.matrixWorld))
160
+ }
161
+ }
162
+ }
163
+ return b
164
+ }
165
+
166
+ // How each fall turns and squashes the body.
167
+ let FALLS: Record<Fall, (body: THREE.Object3D) => void> = {
168
+ topple: (b) => b.rotation.set(0, 0, Math.PI / 2),
169
+ flip: (b) => b.rotation.set(0, 0, Math.PI),
170
+ back: (b) => b.rotation.set(-Math.PI / 2 + 0.1, 0, 0),
171
+ flatten: (b) => b.scale.set(1.4, 0.2, 1.4),
172
+ }
173
+
174
+ // How high a body fallen about `at` stands for its lowest point to touch the
175
+ // ground, from where its boxes lie standing (`box`).
176
+ let lift = (fall: Fall, box: THREE.Box3, at: THREE.Vector3) => {
177
+ let o = new THREE.Object3D()
178
+ FALLS[fall](o)
179
+ o.updateMatrix()
180
+ return -Math.min(
181
+ ...corners(box).map((c) => c.sub(at).applyMatrix4(o.matrix).y),
182
+ )
183
+ }
184
+
185
+ /** A figure made up to be drawn and moved: its parts knitted into one
186
+ * skinned mesh, posed every frame from what it is doing.
187
+ */
188
+ export let puppet = (f: Rig): Puppet => {
189
+ let m = soft({ speckle: f.speckle ?? 0.1 })
190
+ if (f.glow != undefined && m instanceof THREE.MeshLambertMaterial) {
191
+ m.emissive.setHex(f.glow)
192
+ m.emissiveIntensity = 0.7
193
+ }
194
+ // The body turns about `at`: the ground under it, or the middle of one in
195
+ // the air. What it holds is set back by as much, so at rest it is where
196
+ // its parts say.
197
+ let root = new THREE.Group()
198
+ let body = new THREE.Group()
199
+ let inner = new THREE.Group()
200
+ root.add(body)
201
+ body.add(inner)
202
+ let bones = new Map<string, THREE.Bone>()
203
+ let made = f.parts.map((p) => {
204
+ let bone = partOf(p.boxes, p.pivot, p.cell)
205
+ bone.name = p.name
206
+ bone.rotation.order = 'YXZ'
207
+ if (p.turn) bone.rotation.set(...p.turn)
208
+ ;(bones.get(p.parent ?? '') ?? inner).add(bone)
209
+ bones.set(p.name, bone)
210
+ return { bone, boxes: p.boxes }
211
+ })
212
+ knit(root, m)
213
+ root.updateMatrixWorld(true)
214
+ let box = bounds(made)
215
+ let grounded = f.gait != 'hover' && f.gait != 'drift'
216
+ let at = grounded
217
+ ? new THREE.Vector3()
218
+ : box.getCenter(new THREE.Vector3()).setX(0)
219
+ inner.position.copy(at).negate()
220
+ // Which parts hang below which, for an arm that holds something.
221
+ let above = (name?: string): string[] => {
222
+ let p = f.parts.find((q) => q.name == name)
223
+ return p ? [p.name, ...above(p.parent)] : []
224
+ }
225
+ let held = new Set(
226
+ f.parts.filter((p) => !p.move).flatMap((p) => above(p.parent)),
227
+ )
228
+ let v = new THREE.Vector3()
229
+ let parts: Held[] = f.parts.map((p, i) => {
230
+ let bone = made[i].bone, role = roleOf(p.move)
231
+ let c = new THREE.Vector3()
232
+ for (let [[x, y, z], [w, h, d]] of p.boxes) {
233
+ c.add(v.set(x + w / 2, y + h / 2, z + d / 2))
234
+ }
235
+ return {
236
+ bone,
237
+ pivot: bone.position.clone(),
238
+ turn: bone.rotation.clone(),
239
+ role,
240
+ phase: param(p.move, 'phase'),
241
+ beat: param(p.move, 'beat'),
242
+ n: param(p.move, 'n'),
243
+ side: Math.sign(bone.getWorldPosition(v).x) || 1,
244
+ below: !!p.parent &&
245
+ roleOf(f.parts.find((q) => q.name == p.parent)?.move) == role,
246
+ lies: Math.abs(c.y) <= Math.abs(c.z)
247
+ ? 'trails'
248
+ : c.y < 0
249
+ ? 'hangs'
250
+ : 'curls',
251
+ holds: held.has(p.name),
252
+ }
253
+ })
254
+ let of = (r: Role) => parts.filter((h) => h.role == r)
255
+ let legs = of('leg'), arms = of('arm'), wings = of('wing'), tails = of('tail')
256
+ let heads = of('head'), jaws = of('jaw'), spins = of('spin')
257
+ let links = of('link')
258
+ let holding = arms.some((r) => r.holds)
259
+ // Its bulk, in metres: what its sways, bobs and lunges are measured by.
260
+ let size = box.getSize(new THREE.Vector3())
261
+ let bulk = Math.cbrt(size.x * size.y * size.z)
262
+ // How high its hips stand, which sets how quickly it steps.
263
+ let hips = legs.filter((l) => !l.below)
264
+ let hip = hips.length
265
+ ? hips.reduce((s, l) => s + l.bone.getWorldPosition(v).y, 0) / hips.length
266
+ : 0.42
267
+ // How high its lowest point hangs in the air, and how high it lies fallen:
268
+ // on its body, the parts with no role, while its limbs give way.
269
+ let air = Math.max(0, box.min.y)
270
+ let trunk = made.filter((_, i) => !f.parts[i].move)
271
+ let low = lift(f.fall, trunk.length ? bounds(trunk) : box, at)
272
+ let phase = Math.random() * 6, seed = Math.random() * 6
273
+
274
+ // How it goes over the ground at its speed, `dt` seconds on.
275
+ let GAITS: Record<Gait, (a: Act, dt: number) => void> = {
276
+ walk: (a, dt) => {
277
+ phase += dt * (1.5 + 2.6 * a.speed) *
278
+ Math.sqrt(0.42 / Math.max(hip, 0.05))
279
+ let amp = Math.min(1, a.speed / 3.5) * 0.7
280
+ for (let l of legs) {
281
+ let w = phase + TAU * l.phase
282
+ l.bone.rotation.x += l.below
283
+ ? Math.max(0, -Math.cos(w)) * amp * 1.5
284
+ : Math.sin(w) * amp
285
+ }
286
+ for (let r of arms) {
287
+ r.bone.rotation.x += r.below
288
+ ? -amp * 0.3
289
+ : Math.sin(phase + TAU * r.phase) * amp * 0.8
290
+ }
291
+ for (let h of heads) {
292
+ h.bone.position.z += Math.sin(phase * 2) * 0.03 * amp * bulk
293
+ }
294
+ body.position.y += Math.abs(Math.cos(phase)) * 0.045 * amp * bulk
295
+ body.rotation.z = Math.sin(phase) * 0.04 * amp
296
+ },
297
+ hop: (a, dt) => {
298
+ let moving = a.speed > 0.2
299
+ phase += dt * (moving ? 6 + a.speed : 2)
300
+ let hop = moving ? Math.max(0, Math.sin(phase)) : 0
301
+ body.position.y += hop * 0.5 * bulk
302
+ for (let l of legs) {
303
+ l.bone.rotation.x += hop * 0.75 * Math.cos(TAU * l.phase)
304
+ }
305
+ if (legs.length) {
306
+ body.rotation.x = hop > 0 ? -Math.cos(phase) * 0.25 : 0
307
+ return
308
+ }
309
+ // With no legs to spring on, it squashes and stretches.
310
+ let k = moving
311
+ ? 1 - Math.max(0, -Math.sin(phase)) * 0.25 + hop * 0.12
312
+ : 1 + Math.sin(phase) * 0.04
313
+ body.scale.set(1 / Math.sqrt(k), k, 1 / Math.sqrt(k))
314
+ },
315
+ hover: (a) => {
316
+ body.position.x += Math.sin(a.t * 1.7 + seed) * 0.08 * bulk
317
+ body.position.y += Math.sin(a.t * 2.4 + seed) * 0.1 * bulk
318
+ body.rotation.x = Math.min(1, a.speed / 3) * 0.22
319
+ for (let w of wings) {
320
+ w.bone.rotation.z += w.side * 0.75 *
321
+ Math.sin(a.t * TAU * w.beat + seed)
322
+ }
323
+ },
324
+ slide: (a, dt) => {
325
+ let move = Math.min(1, a.speed / 2.2)
326
+ phase += dt * (1.3 + a.speed * 3.1)
327
+ let s = Math.sin(phase), c = Math.cos(phase)
328
+ // A body with no lengths to carry the wave rolls as it goes.
329
+ if (!links.length) {
330
+ body.position.y += move * (0.02 + 0.03 * c * c) * bulk
331
+ body.position.z += s * 0.03 * move * bulk
332
+ body.rotation.set(c * 0.08 * move, s * 0.08 * move, 0)
333
+ }
334
+ for (let l of legs) {
335
+ l.bone.rotation.y += l.side * 0.3 * move *
336
+ Math.sin(phase + TAU * l.phase)
337
+ }
338
+ // A wave runs down its body, wider as it hurries, and its head sways
339
+ // ahead of it.
340
+ let sway = 0.05 + Math.min(1, a.speed / 2.5) * 0.12
341
+ let wide = sway * bulk * 1.4
342
+ for (let l of links) {
343
+ let i = l.n - 1, w = phase - i * 0.9
344
+ l.bone.position.x += Math.sin(w) * wide * (1 + i * 0.15)
345
+ l.bone.rotation.y += Math.cos(w) * sway * 1.6
346
+ }
347
+ for (let h of heads) {
348
+ if (links.length) {
349
+ h.bone.position.x += Math.sin(phase + 0.9) * wide * 0.6
350
+ } else h.bone.rotation.x += c * 0.1 * move
351
+ }
352
+ },
353
+ drift: (a, dt) => {
354
+ phase += dt * (1.5 + a.speed * 0.8)
355
+ body.position.y += Math.sin(a.t * 1.8 + seed) * 0.12 * bulk
356
+ },
357
+ }
358
+
359
+ // How it strikes, `swing` of the way through.
360
+ let BITES: Record<Bite, (swing: number) => void> = {
361
+ lunge: (swing) => {
362
+ let up = lunge(swing)
363
+ body.position.z += up * 0.24 * bulk
364
+ body.rotation.x -= up * 0.1
365
+ for (let h of heads) h.bone.rotation.x -= up * 0.6
366
+ },
367
+ peck: (swing) => {
368
+ let up = lunge(swing)
369
+ for (let h of heads) {
370
+ h.bone.position.z += up * 0.18 * bulk
371
+ h.bone.rotation.x += up * 0.7
372
+ }
373
+ for (let w of wings) {
374
+ let r = w.bone.rotation
375
+ r.set(
376
+ r.x * (1 - up),
377
+ r.y * (1 - up),
378
+ r.z * (1 - up) + w.side * up * 0.5,
379
+ )
380
+ }
381
+ },
382
+ smash: (swing) => {
383
+ // What is held comes down overhead; with nothing held, both fists.
384
+ let up = lunge(swing, 0.45)
385
+ for (let r of arms) {
386
+ let x = r.below ? -up * 1.1 : 0.4 - up * 3
387
+ if (holding && !r.holds) x = r.below ? 0 : -0.3
388
+ r.bone.rotation.x = r.turn.x + x
389
+ }
390
+ body.rotation.x -= up * 0.2
391
+ },
392
+ thrust: (swing) => {
393
+ let up = lunge(swing, 0.5)
394
+ for (let h of heads) {
395
+ h.bone.position.y += up * 0.45 * bulk
396
+ h.bone.position.z += up * 0.5 * bulk
397
+ h.bone.rotation.x += up * 0.8
398
+ }
399
+ for (let l of links) {
400
+ l.bone.position.z += up * 0.5 * bulk * Math.max(0, 1 - (l.n - 1) / 3)
401
+ }
402
+ },
403
+ dive: (swing) => {
404
+ let up = lunge(swing)
405
+ body.position.y -= up * 0.7 * air
406
+ body.position.z += up * 0.45 * bulk
407
+ body.rotation.x += up * 0.5
408
+ },
409
+ leap: (swing) => {
410
+ let up = Math.sin(swing * Math.PI)
411
+ body.position.y = Math.max(body.position.y, at.y + up * 0.25 * bulk)
412
+ body.position.z += up * 0.55 * bulk
413
+ if (legs.length) body.rotation.x = -up * 0.3
414
+ else body.scale.y *= 1 - up * 0.2
415
+ },
416
+ flare: (swing) => {
417
+ let up = lunge(swing)
418
+ body.position.z += up * 0.6 * bulk
419
+ for (let s of spins) s.bone.scale.multiplyScalar(1 + up * 0.35)
420
+ },
421
+ }
422
+
423
+ return {
424
+ root,
425
+ material: m,
426
+ height: f.height,
427
+ animate: (a, dt) => {
428
+ for (let h of parts) {
429
+ h.bone.position.copy(h.pivot)
430
+ h.bone.rotation.copy(h.turn)
431
+ h.bone.scale.setScalar(1)
432
+ }
433
+ body.position.copy(at)
434
+ body.rotation.set(0, 0, 0)
435
+ body.scale.setScalar(1)
436
+ flash(m, a.hurt * 0.8)
437
+ if (a.down) {
438
+ FALLS[f.fall](body)
439
+ body.position.y = low
440
+ return
441
+ }
442
+ GAITS[f.gait](a, dt)
443
+ // Still, it looks about, and on the ground dips its head now and then.
444
+ let still = 1 - Math.min(1, a.speed / 3.5)
445
+ let dip = grounded ? Math.max(0, Math.sin(a.t * 0.9 + seed) - 0.8) * 5 : 0
446
+ for (let h of heads) {
447
+ h.bone.rotation.y += Math.sin(a.t * 0.6 + seed) * 0.2 * still
448
+ h.bone.rotation.x += (Math.sin(a.t * 1.3 + seed) * 0.05 + dip * 0.4) *
449
+ still
450
+ }
451
+ for (let t of tails) {
452
+ let wag = Math.sin(a.t * 2.4 + seed) * 0.25
453
+ if (t.lies == 'hangs') t.bone.rotation.z += wag
454
+ else t.bone.rotation.y += wag
455
+ }
456
+ for (let s of spins) {
457
+ s.bone.rotation.x += a.t * 0.7
458
+ s.bone.rotation.y += a.t * 1.1
459
+ s.bone.scale.setScalar(1 + Math.sin(a.t * 5) * 0.04)
460
+ }
461
+ if (a.swing < 0) return
462
+ BITES[f.bite](a.swing)
463
+ let up = lunge(a.swing)
464
+ for (let t of tails) if (t.lies == 'curls') t.bone.rotation.x += up * 1.1
465
+ for (let j of jaws) {
466
+ j.bone.rotation.x -= up * 0.6
467
+ j.bone.rotation.y += j.side * up * 0.5
468
+ }
469
+ },
470
+ }
471
+ }