@margoengine/rigging 0.0.0-stage → 1.0.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 (84) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1443 -2
  4. package/dist/anatomy-data-ansur2.d.ts +63 -0
  5. package/dist/anatomy-data-cdc.d.ts +16 -0
  6. package/dist/anatomy-data-deleva.d.ts +86 -0
  7. package/dist/anatomy-human.d.ts +67 -0
  8. package/dist/anatomy-human.js +2229 -0
  9. package/dist/anatomy.d.ts +638 -0
  10. package/dist/anatomy.js +1112 -0
  11. package/dist/autorig.d.ts +50 -0
  12. package/dist/autorig.js +663 -0
  13. package/dist/bar-grip.d.ts +7 -0
  14. package/dist/body-volumes-DTym0RVY.js +56 -0
  15. package/dist/body-volumes.d.ts +19 -0
  16. package/dist/checks.d.ts +50 -0
  17. package/dist/contact.d.ts +186 -0
  18. package/dist/contact.js +746 -0
  19. package/dist/correctives.d.ts +92 -0
  20. package/dist/correctives.js +162 -0
  21. package/dist/coupling.d.ts +57 -0
  22. package/dist/coupling.js +330 -0
  23. package/dist/errors-D8rc8kzg.js +7 -0
  24. package/dist/face.d.ts +93 -0
  25. package/dist/face.js +144 -0
  26. package/dist/hands.d.ts +209 -0
  27. package/dist/hands.js +510 -0
  28. package/dist/heat.d.ts +38 -0
  29. package/dist/hinged-contact.d.ts +7 -0
  30. package/dist/hulls.d.ts +54 -0
  31. package/dist/hulls.js +134 -0
  32. package/dist/ik.d.ts +162 -0
  33. package/dist/ik.js +561 -0
  34. package/dist/index.d.ts +6 -0
  35. package/dist/index.js +390 -0
  36. package/dist/joint-limits.d.ts +44 -0
  37. package/dist/joint-limits.js +74 -0
  38. package/dist/limb-collision.d.ts +99 -0
  39. package/dist/loading-CZaYr1N7.js +67 -0
  40. package/dist/mesh-CyvLHNp0.js +142 -0
  41. package/dist/mesh.d.ts +47 -0
  42. package/dist/mtoon-data.d.ts +118 -0
  43. package/dist/mtoon-legacy.d.ts +17 -0
  44. package/dist/mtoon-shader.d.ts +4 -0
  45. package/dist/mtoon.d.ts +84 -0
  46. package/dist/mtoon.js +990 -0
  47. package/dist/paint.d.ts +82 -0
  48. package/dist/pose-tracking.d.ts +117 -0
  49. package/dist/pose-tracking.js +488 -0
  50. package/dist/retarget.d.ts +78 -0
  51. package/dist/retarget.js +375 -0
  52. package/dist/rig-BawZ5OLb.js +127 -0
  53. package/dist/rig.d.ts +65 -0
  54. package/dist/shared/gltf.d.ts +257 -0
  55. package/dist/shared/layers.d.ts +16 -0
  56. package/dist/springs.d.ts +180 -0
  57. package/dist/springs.js +458 -0
  58. package/dist/strand-springs.d.ts +46 -0
  59. package/dist/strand-springs.js +168 -0
  60. package/dist/swim.d.ts +55 -0
  61. package/dist/swim.js +167 -0
  62. package/dist/transfer.d.ts +41 -0
  63. package/dist/transfer.js +136 -0
  64. package/dist/validate.d.ts +188 -0
  65. package/dist/validate.js +440 -0
  66. package/dist/voxel-weights-48ok-Dax.js +255 -0
  67. package/dist/voxel-weights.d.ts +42 -0
  68. package/dist/voxel-weights.js +1 -0
  69. package/dist/voxels.d.ts +36 -0
  70. package/dist/vrm-constraints.d.ts +60 -0
  71. package/dist/vrm-constraints.js +261 -0
  72. package/dist/vrm-first-person.d.ts +115 -0
  73. package/dist/vrm-first-person.js +618 -0
  74. package/dist/vrm-presets-CU4V4yq6.js +47 -0
  75. package/dist/vrm-presets.d.ts +1 -0
  76. package/dist/vrm.d.ts +303 -0
  77. package/dist/vrm.js +574 -0
  78. package/dist/vrm0.d.ts +134 -0
  79. package/dist/vrm0.js +377 -0
  80. package/dist/vrma.d.ts +90 -0
  81. package/dist/vrma.js +315 -0
  82. package/dist/weight-map-D8cAONQQ.js +92 -0
  83. package/dist/weight-map.d.ts +58 -0
  84. package/package.json +189 -4
package/dist/hands.js ADDED
@@ -0,0 +1,510 @@
1
+ import { detectSkeleton as e, humanoidBones as t } from "./retarget.js";
2
+ import { axisAngleToQuaternion as n, conjugateQuaternion as r, interpolateQuaternions as i, multiplyQuaternions as a, normalizeQuaternion as o, rotateByQuaternion as s } from "@margoengine/math/quat";
3
+ //#region src/hands.ts
4
+ var c = [
5
+ "thumb",
6
+ "index",
7
+ "middle",
8
+ "ring",
9
+ "little"
10
+ ], l = (e, t, n) => [
11
+ e,
12
+ t,
13
+ n
14
+ ], u = {
15
+ relaxed: {
16
+ flex: {
17
+ index: l(14, 26, 17),
18
+ middle: l(18, 32, 21),
19
+ ring: l(22, 37, 25),
20
+ little: l(26, 42, 28)
21
+ },
22
+ spread: {
23
+ index: 3,
24
+ ring: 3,
25
+ little: 7
26
+ }
27
+ },
28
+ fist: {
29
+ flex: {
30
+ thumb: l(30, 45, 35),
31
+ index: l(88, 100, 67),
32
+ middle: l(90, 102, 68),
33
+ ring: l(92, 104, 69),
34
+ little: l(94, 106, 71)
35
+ },
36
+ spread: {
37
+ index: -2,
38
+ ring: -2,
39
+ little: -3
40
+ }
41
+ },
42
+ flat: {
43
+ flex: {
44
+ thumb: l(0, 0, 0),
45
+ index: l(0, 0, 0),
46
+ middle: l(0, 0, 0),
47
+ ring: l(0, 0, 0),
48
+ little: l(0, 0, 0)
49
+ },
50
+ spread: {
51
+ index: 0,
52
+ ring: 0,
53
+ little: 0
54
+ }
55
+ },
56
+ point: {
57
+ flex: {
58
+ thumb: l(25, 35, 30),
59
+ index: l(4, 5, 3),
60
+ middle: l(80, 100, 67),
61
+ ring: l(85, 103, 69),
62
+ little: l(90, 105, 70)
63
+ },
64
+ spread: {
65
+ index: 2,
66
+ ring: -2,
67
+ little: -3
68
+ }
69
+ },
70
+ pinch: {
71
+ flex: {
72
+ thumb: l(30, 25, 20),
73
+ index: l(40, 50, 33),
74
+ middle: l(32, 46, 31),
75
+ ring: l(36, 50, 33),
76
+ little: l(40, 54, 36)
77
+ },
78
+ spread: {
79
+ index: 0,
80
+ ring: 2,
81
+ little: 5
82
+ }
83
+ }
84
+ }, d = () => ({ name: "relaxed" }), f = () => ({ name: "fist" }), p = () => ({ name: "flat" }), m = () => ({ name: "point" }), h = () => ({ name: "pinch" });
85
+ function g(e = .035) {
86
+ if (!(e >= .005 && e <= .12)) throw RangeError("grip diameter must be from 0.005 to 0.12 metres");
87
+ return {
88
+ name: "grip",
89
+ diameter: e
90
+ };
91
+ }
92
+ function _(e) {
93
+ return v(e), {
94
+ name: "table",
95
+ table: e
96
+ };
97
+ }
98
+ function v(e) {
99
+ if (!e || typeof e != "object" || !e.flex || typeof e.flex != "object") throw TypeError("A hand pose table needs flex: {index: [base, middle, tip], ...} in degrees");
100
+ for (let [t, n] of Object.entries(e.flex)) {
101
+ if (!c.includes(t)) throw RangeError(`Unknown finger ${t}; use ${c.join(", ")}`);
102
+ if (!Array.isArray(n) || n.length !== 3 || !n.every((e) => Number.isFinite(e) && e >= -30 && e <= 130)) throw RangeError(`Finger ${t} flex must be three angles from -30 to 130 degrees`);
103
+ }
104
+ for (let [t, n] of Object.entries(e.spread ?? {})) if (!c.includes(t) || !(Number.isFinite(n) && Math.abs(n) <= 40)) throw RangeError(`Finger ${t} spread must be from -40 to 40 degrees`);
105
+ }
106
+ var y = [
107
+ 95,
108
+ 110,
109
+ 80
110
+ ], b = (e, t, n) => {
111
+ let r = n[0] - t[0], i = n[1] - t[1], a = Math.max(0, Math.min(1, ((e[0] - t[0]) * r + (e[1] - t[1]) * i) / (r * r + i * i || 1)));
112
+ return Math.hypot(t[0] + r * a - e[0], t[1] + i * a - e[1]);
113
+ }, x = (e, t) => {
114
+ let n = [[0, 0]], r = 0;
115
+ for (let i = 0; i < 3; i++) {
116
+ r -= t[i] * Math.PI / 180;
117
+ let [a, o] = n[i];
118
+ n.push([a + e[i] * Math.cos(r), o + e[i] * Math.sin(r)]);
119
+ }
120
+ return n;
121
+ };
122
+ function S(e, t, n = {}) {
123
+ if (!Array.isArray(e) || e.length !== 3 || !e.every((e) => e > 0 && e < 1)) throw RangeError("gripFinger needs three phalanx lengths in metres, each above 0 and below 1");
124
+ if (!(t > 0 && t < 1)) throw RangeError("gripFinger diameter must be above 0 and below 1 metre");
125
+ let r = t / 2, i = n.thickness ?? .18 * e[0], a = n.palm ?? .25 * e[0], o = r + i, s = r + a, c = .1, l = (t) => {
126
+ let n = [t, -s], r = [
127
+ 0,
128
+ 0,
129
+ 0
130
+ ], i = (t) => {
131
+ let i = x(e, r);
132
+ for (let e = t; e < 3; e++) if (b(n, i[e], i[e + 1]) <= o) return e;
133
+ return -1;
134
+ };
135
+ for (; r[0] < y[0] && i(0) < 0;) r[0] = Math.min(y[0], r[0] + c);
136
+ let a = i(1);
137
+ for (; a < 0 && r[1] < y[1];) r[1] = Math.min(y[1], r[1] + c), r[2] = Math.min(y[2], r[1] * 2 / 3), a = i(1);
138
+ for (; a < 2 && r[2] < y[2] && (r[2] = Math.min(y[2], r[2] + c), i(2) !== 2););
139
+ let l = x(e, r), u = 0;
140
+ for (let e = 0; e < 3; e++) u = Math.max(u, o - b(n, l[e], l[e + 1]));
141
+ let d = Math.hypot(l[3][0] - n[0], l[3][1] - n[1]) - o;
142
+ return {
143
+ flex: [
144
+ r[0],
145
+ r[1],
146
+ r[2]
147
+ ],
148
+ along: t,
149
+ depth: s,
150
+ points: l,
151
+ tipGap: d,
152
+ depthIn: u
153
+ };
154
+ }, u, d;
155
+ for (let t = -r; t <= e[0] && !u; t += 25e-5) {
156
+ let e = l(t);
157
+ Math.abs(e.tipGap) <= 5e-4 && (u = e), (!d || Math.abs(e.tipGap) < Math.abs(d.tipGap)) && (d = e);
158
+ }
159
+ return u ?? d;
160
+ }
161
+ var C = (e, t) => [
162
+ e[0] - t[0],
163
+ e[1] - t[1],
164
+ e[2] - t[2]
165
+ ], w = (e, t) => e[0] * t[0] + e[1] * t[1] + e[2] * t[2], T = (e, t) => [
166
+ e[1] * t[2] - e[2] * t[1],
167
+ e[2] * t[0] - e[0] * t[2],
168
+ e[0] * t[1] - e[1] * t[0]
169
+ ], E = (e) => {
170
+ let t = Math.hypot(e[0], e[1], e[2]);
171
+ return t > 0 ? [
172
+ e[0] / t,
173
+ e[1] / t,
174
+ e[2] / t
175
+ ] : [
176
+ 0,
177
+ 0,
178
+ 0
179
+ ];
180
+ }, D = (e, t) => [
181
+ e[0] * t,
182
+ e[1] * t,
183
+ e[2] * t
184
+ ], O = {
185
+ thumb: [
186
+ "ThumbMetacarpal",
187
+ "ThumbProximal",
188
+ "ThumbDistal"
189
+ ],
190
+ index: [
191
+ "IndexProximal",
192
+ "IndexIntermediate",
193
+ "IndexDistal"
194
+ ],
195
+ middle: [
196
+ "MiddleProximal",
197
+ "MiddleIntermediate",
198
+ "MiddleDistal"
199
+ ],
200
+ ring: [
201
+ "RingProximal",
202
+ "RingIntermediate",
203
+ "RingDistal"
204
+ ],
205
+ little: [
206
+ "LittleProximal",
207
+ "LittleIntermediate",
208
+ "LittleDistal"
209
+ ]
210
+ };
211
+ function k(n, i, l) {
212
+ let { names: u, parents: d, rest: f } = n ?? {};
213
+ if (!Array.isArray(u) || !Array.isArray(d) || d.length !== u.length || !f || f.length !== u.length * 10) throw TypeError("measureHands needs {names, parents, rest} with 10 rest numbers per bone");
214
+ let p = l ? void 0 : i ?? e(u)?.preset;
215
+ if (!l && !p) throw RangeError("measureHands found no skeleton preset for these bone names; pass one or a bone map");
216
+ let m = l ?? t(u, p), h = [], g = (e) => {
217
+ let t = h[e];
218
+ if (t) return t;
219
+ let n = e * 10, r = [
220
+ f[n],
221
+ f[n + 1],
222
+ f[n + 2]
223
+ ], i = [
224
+ f[n + 3],
225
+ f[n + 4],
226
+ f[n + 5],
227
+ f[n + 6]
228
+ ], c = [
229
+ f[n + 7],
230
+ f[n + 8],
231
+ f[n + 9]
232
+ ], l = d[e], u;
233
+ if (l < 0) u = {
234
+ position: r,
235
+ rotation: i,
236
+ scale: c
237
+ };
238
+ else {
239
+ let e = g(l);
240
+ u = {
241
+ position: [...e.position.map((t, n) => t + s([
242
+ r[0] * e.scale[0],
243
+ r[1] * e.scale[1],
244
+ r[2] * e.scale[2]
245
+ ], e.rotation)[n])],
246
+ rotation: o(a(e.rotation, i)),
247
+ scale: [
248
+ e.scale[0] * c[0],
249
+ e.scale[1] * c[1],
250
+ e.scale[2] * c[2]
251
+ ]
252
+ };
253
+ }
254
+ return h[e] = u, u;
255
+ }, _ = {};
256
+ for (let e of ["left", "right"]) {
257
+ let t = (t) => m.get(`${e}${t}`), n = t("Hand"), i = t("IndexProximal"), a = t("MiddleProximal"), o = t("LittleProximal") ?? t("RingProximal");
258
+ if (n === void 0 || i === void 0 || a === void 0 || o === void 0) continue;
259
+ let l = (e) => g(e).position, p = C(l(a), l(n)), h = C(l(i), l(o)), v = E(e === "left" ? T(p, h) : T(h, p)), y = {}, b = /* @__PURE__ */ new Map();
260
+ for (let e of c) {
261
+ let n = O[e].map((e) => t(e));
262
+ if (n.some((e) => e === void 0)) continue;
263
+ let [i, o, c] = n, p = u.findIndex((e, t) => d[t] === c), m = Math.hypot(...C(l(o), l(i))), h = Math.hypot(...C(l(c), l(o))), _ = p >= 0 ? Math.hypot(...C(l(p), l(c))) : .75 * h, x = [
264
+ l(o),
265
+ l(c),
266
+ p >= 0 ? l(p) : null
267
+ ], S = [
268
+ i,
269
+ o,
270
+ c
271
+ ].map((e, t) => {
272
+ let n = x[t] ? C(x[t], l(e)) : C(l(c), l(o));
273
+ return n = E(C(n, D(v, w(n, v)))), E(s(T(n, v), r(g(e).rotation)));
274
+ }), k = null;
275
+ if (e !== "middle" && e !== "thumb") {
276
+ let e = E(C(l(o), l(i))), t = C(l(i), l(a)), n = E(C(C(t, D(e, w(t, e))), D(v, w(t, v))));
277
+ k = E(s(T(e, n), r(g(i).rotation)));
278
+ }
279
+ for (let e of [
280
+ i,
281
+ o,
282
+ c
283
+ ]) b.set(e, [
284
+ f[e * 10 + 3],
285
+ f[e * 10 + 4],
286
+ f[e * 10 + 5],
287
+ f[e * 10 + 6]
288
+ ]);
289
+ y[e] = {
290
+ bones: [
291
+ i,
292
+ o,
293
+ c
294
+ ],
295
+ lengths: [
296
+ m,
297
+ h,
298
+ _
299
+ ],
300
+ curl: S,
301
+ spread: k
302
+ };
303
+ }
304
+ _[e] = {
305
+ side: e,
306
+ hand: n,
307
+ fingers: y,
308
+ palm: v,
309
+ rest: b,
310
+ world: (e) => g(e)
311
+ };
312
+ }
313
+ return _;
314
+ }
315
+ function A(e, t) {
316
+ if (!e || typeof e != "object") throw TypeError("Pass a hand pose: relaxed(), fist(), grip(0.03) ...");
317
+ if (e.name === "table") return e.table;
318
+ if (e.name !== "grip") {
319
+ let t = u[e.name];
320
+ if (!t) throw RangeError(`Unknown hand pose ${String(e.name)}; pass relaxed(), fist(), grip(0.03) or another pose of this entry`);
321
+ return t;
322
+ }
323
+ let n = { thumb: l(35, 40, 30) };
324
+ for (let r of [
325
+ "index",
326
+ "middle",
327
+ "ring",
328
+ "little"
329
+ ]) {
330
+ let i = t.fingers[r];
331
+ i && (n[r] = S(i.lengths, e.diameter).flex);
332
+ }
333
+ return {
334
+ flex: n,
335
+ spread: {
336
+ index: 0,
337
+ ring: 0,
338
+ little: 0
339
+ }
340
+ };
341
+ }
342
+ function j(e, t) {
343
+ let r = /* @__PURE__ */ new Map(), i = Math.PI / 180;
344
+ for (let s of c) {
345
+ let c = e.fingers[s], l = t.flex[s];
346
+ c && l && c.bones.forEach((u, d) => {
347
+ let f = e.rest.get(u).slice(), p = t.spread?.[s] ?? 0;
348
+ d === 0 && c.spread && p && (f = a(f, n(c.spread, p * i))), r.set(u, o(a(f, n(c.curl[d], l[d] * i))));
349
+ });
350
+ }
351
+ return r;
352
+ }
353
+ function M(e, t) {
354
+ let n = Math.abs(e[0] * t[0] + e[1] * t[1] + e[2] * t[2] + e[3] * t[3]);
355
+ return 2 * Math.acos(Math.min(1, n)) * 180 / Math.PI;
356
+ }
357
+ function N(e, t) {
358
+ let n = 0, r = 0;
359
+ for (let i of c) {
360
+ let a = t.fingers[i];
361
+ if (a) for (let i of a.bones) {
362
+ r++;
363
+ let a = e.tracks.find((e) => e.node === i && e.path === "rotation");
364
+ if (!a || a.values.length < 4) continue;
365
+ let o = t.rest.get(i), s = 0, c = a.values.length >> 2;
366
+ for (let e = 0; e < c; e++) s += M([
367
+ a.values[e * 4],
368
+ a.values[e * 4 + 1],
369
+ a.values[e * 4 + 2],
370
+ a.values[e * 4 + 3]
371
+ ], o);
372
+ n += s / c;
373
+ }
374
+ }
375
+ return r ? n / r : 0;
376
+ }
377
+ var P = () => ({
378
+ thumb: 0,
379
+ index: 0,
380
+ middle: 0,
381
+ ring: 0,
382
+ little: 0
383
+ }), F = (e) => e <= 0 ? 0 : e >= 1 ? 1 : e * e * (3 - 2 * e), I = (e, t, n) => {
384
+ let r = {}, i = {};
385
+ for (let a of c) {
386
+ let o = e.flex[a], s = t.flex[a];
387
+ o && s ? r[a] = [
388
+ o[0] + (s[0] - o[0]) * n,
389
+ o[1] + (s[1] - o[1]) * n,
390
+ o[2] + (s[2] - o[2]) * n
391
+ ] : (o || s) && (r[a] = o ?? s), i[a] = (e.spread?.[a] ?? 0) + ((t.spread?.[a] ?? 0) - (e.spread?.[a] ?? 0)) * n;
392
+ }
393
+ return {
394
+ flex: r,
395
+ spread: i
396
+ };
397
+ }, L = (e) => {
398
+ if (!e || !(e.time >= 0 && e.time < Infinity)) throw RangeError("A hand key needs a time of 0 or more seconds");
399
+ if (e.blend !== void 0 && !(e.blend >= 0 && e.blend < Infinity)) throw RangeError("A hand key blend must be 0 or more seconds");
400
+ if (e.weight !== void 0 && !(e.weight >= 0 && e.weight <= 1)) throw RangeError("A hand key weight must be from 0 to 1");
401
+ if (e.pose !== null && (!e.pose || typeof e.pose != "object")) throw TypeError("A hand key pose must be a hand pose or null");
402
+ };
403
+ function R(t, n = {}) {
404
+ let r = n.threshold ?? 15;
405
+ if (!(r >= 0 && r <= 90)) throw RangeError("addHands threshold must be from 0 to 90 degrees");
406
+ let a = t.animation.model, o = a.nodes, s = a.root.engine.store, l = new Map(o.map((e, t) => [e.id, t])), u = o.map((e) => l.get(s.parents[e.id]) ?? -1), f = new Float64Array(o.length * 10);
407
+ o.forEach((e, n) => {
408
+ f.set(t.restPosition(n), n * 10), f.set(t.restRotation(n), n * 10 + 3), f.set(t.restScale(n), n * 10 + 7);
409
+ });
410
+ let p = o.map((e) => e.name ?? ""), m = n.bones ? void 0 : n.preset ?? e(p)?.preset, h = n.bones || m ? k({
411
+ names: p,
412
+ parents: u,
413
+ rest: f
414
+ }, m, n.bones) : {}, g = n.auto ?? !0, _ = n.rest ?? d(), v = t.animation.clips, y = t.options.states, b = {
415
+ left: [],
416
+ right: []
417
+ };
418
+ for (let e of ["left", "right"]) {
419
+ let t = h[e];
420
+ if (t && g) for (let n of v) N(n, t) < r && b[e].push(n.name);
421
+ }
422
+ let x = (e, t) => {
423
+ let n = y[e];
424
+ return n === void 0 ? 0 : +!!(typeof n == "string" ? [n] : n.samples.map((e) => e.clip)).every((e) => b[t].includes(e));
425
+ }, S = /* @__PURE__ */ new Map(), C = (e, t) => {
426
+ let n = e.side + JSON.stringify(t), r = S.get(n);
427
+ return r || (S.size > 64 && S.clear(), S.set(n, r = {
428
+ table: t,
429
+ rotations: j(e, t)
430
+ })), r.rotations;
431
+ }, w = 0, T = (e, t, n) => {
432
+ let r = {
433
+ table: null,
434
+ weights: P()
435
+ };
436
+ for (let i of e.keys) {
437
+ if (i.time > n) break;
438
+ let e = i.pose ? A(i.pose, t) : r.table, a = P(), o = i.blend ?? .25, s = o > 0 ? F((n - i.time) / o) : 1;
439
+ if (i.pose && e) for (let t of c) a[t] = e.flex[t] ? i.weight ?? 1 : 0;
440
+ let l = r;
441
+ for (let e of c) a[e] = l.weights[e] + (a[e] - l.weights[e]) * s;
442
+ r = {
443
+ table: l.table && e ? I(l.table, e, s) : e ?? l.table,
444
+ weights: a
445
+ };
446
+ }
447
+ return r;
448
+ }, E = (e) => {
449
+ let t = {
450
+ hand: h[e],
451
+ keys: [],
452
+ set(e, { blend: n = .25, weight: r = 1 } = {}) {
453
+ let i = {
454
+ time: w,
455
+ pose: e,
456
+ blend: n,
457
+ weight: r
458
+ };
459
+ return L(i), t.keys = t.keys.filter((e) => e.time <= w), t.keys.push(i), t;
460
+ }
461
+ }, r = n[e];
462
+ return r && t.keys.push({
463
+ time: 0,
464
+ pose: r,
465
+ blend: 0
466
+ }), t;
467
+ }, D = E("left"), O = E("right"), M = t.addLateStep((e, n) => {
468
+ w += n;
469
+ let r = t.transitionInfo;
470
+ for (let n of [D, O]) {
471
+ let a = n.hand;
472
+ if (!a) continue;
473
+ for (let e of n.keys) L(e);
474
+ n.keys.sort((e, t) => e.time - t.time);
475
+ let o = g ? x(t.state, a.side) : 0;
476
+ if (g && r && r.duration > 0) {
477
+ let e = Math.min(1, r.elapsed / r.duration);
478
+ o = x(r.from, a.side) * (1 - e) + o * e;
479
+ }
480
+ let s = T(n, a, w), l = c.some((e) => s.weights[e] > 0);
481
+ if (o <= 0 && !l) continue;
482
+ let u = o > 0 ? C(a, A(_, a)) : void 0, d = l && s.table ? C(a, s.table) : void 0;
483
+ for (let t of c) {
484
+ let n = a.fingers[t];
485
+ if (n) for (let r of n.bones) {
486
+ let n = e.rotation[r];
487
+ u?.has(r) && (n = i(n, u.get(r), o, .9995)), d?.has(r) && s.weights[t] > 0 && (n = i(n, d.get(r), s.weights[t], .9995)), e.rotation[r] = n;
488
+ }
489
+ }
490
+ }
491
+ }), R = () => {
492
+ M?.(), M = null;
493
+ };
494
+ return t.onDispose(R), {
495
+ left: D,
496
+ right: O,
497
+ get time() {
498
+ return w;
499
+ },
500
+ set time(e) {
501
+ if (!(e >= 0 && e < Infinity)) throw RangeError("Hands time must be a finite number of seconds, 0 or more");
502
+ w = e;
503
+ },
504
+ flat: b,
505
+ dispose: R,
506
+ [Symbol.dispose]: R
507
+ };
508
+ }
509
+ //#endregion
510
+ export { c as FINGERS, R as addHands, N as fingerTurn, f as fist, p as flat, g as grip, S as gripFinger, _ as handPose, j as handRotations, k as measureHands, h as pinch, m as point, A as poseTable, d as relaxed, M as turnFromRest };
package/dist/heat.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { type RigJoint } from './rig.js';
2
+ import { type WeightMesh } from './mesh.js';
3
+ import { WeightMap } from './weight-map.js';
4
+ /** Options for {@link boneHeatWeights}. */
5
+ export interface BoneHeatOptions {
6
+ /**
7
+ * Joints that move no vertices (indices or names), such as a root or armature joint at the feet. Default none.
8
+ */
9
+ readonly skip?: readonly (number | string)[];
10
+ /**
11
+ * Whether heat reaches a vertex only from bones it can see (the segment to the bone's closest point stays inside the
12
+ * mesh). `true` (default) builds a BVH with `@margoengine/bvh`, loaded on first use; `false` uses the nearest bone
13
+ * whatever lies between (faster, for convex parts and voxel blocks); or pass your own test, true when the segment
14
+ * from `from` to `to` crosses no surface.
15
+ */
16
+ readonly visibility?: boolean | ((from: readonly number[], to: readonly number[]) => boolean);
17
+ /** Heat constant c (default 1): larger keeps each vertex closer to its nearest bone, smaller blends wider. */
18
+ readonly heat?: number;
19
+ /** Welding tolerance as a fraction of the mesh's size (default 1e-6: coincident vertices). */
20
+ readonly weld?: number;
21
+ /** Largest weight correction (Jacobi-scaled residual) that ends a bone's solve (default 1e-6). */
22
+ readonly tolerance?: number;
23
+ /** Most conjugate-gradient iterations per bone (default 2,000). */
24
+ readonly maxIterations?: number;
25
+ /** Weights below this are dropped before renormalising (default 1e-4). */
26
+ readonly threshold?: number;
27
+ /** Cancels the solve between iterations; the promise rejects with the signal's reason. */
28
+ readonly signal?: AbortSignal;
29
+ }
30
+ /**
31
+ * Smooth automatic skin weights for `mesh` from the joints of a rest skeleton, by bone heat diffusion (Baran and
32
+ * Popovic 2007). Joint positions and vertices share one space. Each joint moves the vertices around its bone
33
+ * segments (from the joint to each child; a point at a leaf); weights fall off smoothly across joints, sum to 1 per
34
+ * vertex and are 0 or more. Resolves to a {@link WeightMap} over the input vertices (duplicates at one position get
35
+ * the same weights); `toInfluences()` gives JOINTS_0 / WEIGHTS_0.
36
+ * @throws TypeError or RangeError for invalid input (thrown synchronously from the returned promise's start).
37
+ */
38
+ export declare function boneHeatWeights(mesh: WeightMesh, joints: readonly RigJoint[], options?: BoneHeatOptions): Promise<WeightMap>;
@@ -0,0 +1,7 @@
1
+ // @ts-ignore optional peer: its types apply when it is installed
2
+ import type { CCDHingeLimit, CCDSolver } from '@margoengine/animator/ccd';
3
+ import type { Vec3 } from '@margoengine/math';
4
+ type Quat = readonly [number, number, number, number];
5
+ /** Solves the last two links in the selected bend plane, then distributes the requested turn. */
6
+ export declare function poseHingedContact(solver: CCDSolver, target: Vec3, goal: Quat, pole: Vec3, limit: CCDHingeLimit): void;
7
+ export {};
@@ -0,0 +1,54 @@
1
+ import { WeightMap, type VertexInfluences } from './weight-map.js';
2
+ /** A skinned mesh in its bind pose: vertex positions, and the skin weights that bind each vertex to joints. */
3
+ export interface JointHullModel {
4
+ /** Bind-pose vertex positions, xyz per vertex, in the space the inverse bind matrices start from (mesh space). */
5
+ readonly positions: ArrayLike<number>;
6
+ /** Per-vertex influences (glTF JOINTS_0 / WEIGHTS_0, or wider) or a dense {@link WeightMap}; one row per vertex. */
7
+ readonly skin: VertexInfluences | WeightMap;
8
+ }
9
+ /** Settings for {@link jointHulls}. */
10
+ export interface JointHullOptions {
11
+ /**
12
+ * A vertex counts toward a joint when that joint's weight on it is at least this (above 0, at most 1). The
13
+ * default 0.5 gives each vertex to the joint that carries most of it, so the hulls of neighbouring joints meet
14
+ * instead of overlapping; lower it to let blended vertices (an elbow) reach into both joints.
15
+ */
16
+ minWeight?: number;
17
+ /** Most corners a hull keeps (8 to 1,024, default 64). A larger hull keeps the corners that stick out farthest. */
18
+ maxPoints?: number;
19
+ /**
20
+ * Inverse bind matrices, 16 numbers per joint, column-major (glTF's `inverseBindMatrices`). When given, hull
21
+ * corners are returned in each joint's own space (mesh space to joint space), so the hull moves with the joint by
22
+ * its world matrix. Without it they stay in the space of `positions`.
23
+ */
24
+ inverseBindMatrices?: ArrayLike<number>;
25
+ }
26
+ /** One joint's hull. */
27
+ export interface JointHull {
28
+ /** Index of the joint in the skin. */
29
+ readonly joint: number;
30
+ /** Vertices that counted toward this joint. */
31
+ readonly vertexCount: number;
32
+ /** The hull's corners, xyz each (at most `maxPoints`). */
33
+ readonly points: Float32Array;
34
+ }
35
+ /**
36
+ * The convex hull of the vertices each joint dominates, from a skinned mesh in its bind pose. Joints that no vertex
37
+ * dominates, or whose vertices do not span a volume (fewer than four, or all on a line or plane), have no hull and
38
+ * are left out. Hulls come back in joint order. Run it once at load; it is not meant for a frame loop.
39
+ *
40
+ * ```ts
41
+ * import {jointHulls} from 'margoengine/rigging/hulls';
42
+ * // A 1 m column skinned to two joints: the lower box (y 0 to 0.4) to joint 0, the upper (0.6 to 1) to joint 1.
43
+ * // Each joint needs vertices that span a volume, so each box has its own eight corners.
44
+ * const positions = [0, 0.4, 0.6, 1].flatMap((y) => [-0.5, 0.5].flatMap((x) => [-0.5, 0.5].flatMap((z) => [x, y, z])));
45
+ * const joints = new Uint16Array(16).map((_, v) => (v < 8 ? 0 : 1));
46
+ * const hulls = jointHulls({positions, skin: {joints, weights: new Float32Array(16).fill(1), influences: 1}});
47
+ * hulls.length; // 2 (hulls[1].joint === 1)
48
+ * ```
49
+ *
50
+ * @throws TypeError when `model` has no positions or skin.
51
+ * @throws RangeError for positions that are not xyz triples or are not finite, a skin whose vertex count differs from
52
+ * the positions, a joint index outside `inverseBindMatrices`, a weight that is not finite, or an option out of range.
53
+ */
54
+ export declare function jointHulls(model: JointHullModel, options?: JointHullOptions): JointHull[];