@margoengine/rigging 0.0.0-stage → 0.1.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 (71) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +969 -3
  4. package/dist/autorig.d.ts +50 -0
  5. package/dist/autorig.js +660 -0
  6. package/dist/bar-grip.d.ts +7 -0
  7. package/dist/checks.d.ts +50 -0
  8. package/dist/contact.d.ts +175 -0
  9. package/dist/contact.js +739 -0
  10. package/dist/correctives.d.ts +92 -0
  11. package/dist/correctives.js +162 -0
  12. package/dist/errors-D8rc8kzg.js +7 -0
  13. package/dist/face.d.ts +93 -0
  14. package/dist/face.js +144 -0
  15. package/dist/hands.d.ts +209 -0
  16. package/dist/hands.js +510 -0
  17. package/dist/heat.d.ts +38 -0
  18. package/dist/hinged-contact.d.ts +7 -0
  19. package/dist/hulls.d.ts +54 -0
  20. package/dist/hulls.js +134 -0
  21. package/dist/ik.d.ts +109 -0
  22. package/dist/ik.js +358 -0
  23. package/dist/index.d.ts +6 -0
  24. package/dist/index.js +390 -0
  25. package/dist/joint-limits.d.ts +44 -0
  26. package/dist/joint-limits.js +74 -0
  27. package/dist/limb-collision.d.ts +99 -0
  28. package/dist/loading-CZaYr1N7.js +67 -0
  29. package/dist/mesh-CyvLHNp0.js +142 -0
  30. package/dist/mesh.d.ts +47 -0
  31. package/dist/mtoon-data.d.ts +118 -0
  32. package/dist/mtoon-legacy.d.ts +17 -0
  33. package/dist/mtoon-shader.d.ts +4 -0
  34. package/dist/mtoon.d.ts +84 -0
  35. package/dist/mtoon.js +990 -0
  36. package/dist/paint.d.ts +82 -0
  37. package/dist/pose-tracking.d.ts +117 -0
  38. package/dist/pose-tracking.js +488 -0
  39. package/dist/retarget.d.ts +71 -0
  40. package/dist/retarget.js +351 -0
  41. package/dist/rig-BawZ5OLb.js +127 -0
  42. package/dist/rig.d.ts +65 -0
  43. package/dist/shared/gltf.d.ts +257 -0
  44. package/dist/shared/layers.d.ts +16 -0
  45. package/dist/springs.d.ts +180 -0
  46. package/dist/springs.js +458 -0
  47. package/dist/strand-springs.d.ts +46 -0
  48. package/dist/strand-springs.js +168 -0
  49. package/dist/swim.d.ts +55 -0
  50. package/dist/swim.js +167 -0
  51. package/dist/transfer.d.ts +41 -0
  52. package/dist/transfer.js +136 -0
  53. package/dist/voxel-weights-48ok-Dax.js +255 -0
  54. package/dist/voxel-weights.d.ts +42 -0
  55. package/dist/voxel-weights.js +1 -0
  56. package/dist/voxels.d.ts +36 -0
  57. package/dist/vrm-constraints.d.ts +60 -0
  58. package/dist/vrm-constraints.js +261 -0
  59. package/dist/vrm-first-person.d.ts +115 -0
  60. package/dist/vrm-first-person.js +618 -0
  61. package/dist/vrm-presets-CU4V4yq6.js +47 -0
  62. package/dist/vrm-presets.d.ts +1 -0
  63. package/dist/vrm.d.ts +298 -0
  64. package/dist/vrm.js +574 -0
  65. package/dist/vrm0.d.ts +134 -0
  66. package/dist/vrm0.js +377 -0
  67. package/dist/vrma.d.ts +90 -0
  68. package/dist/vrma.js +315 -0
  69. package/dist/weight-map-D8cAONQQ.js +92 -0
  70. package/dist/weight-map.d.ts +58 -0
  71. package/package.json +175 -6
@@ -0,0 +1,739 @@
1
+ import { r as e, s as t } from "./rig-BawZ5OLb.js";
2
+ import { FINGERS as n, grip as r, handRotations as i, measureHands as a, poseTable as o, relaxed as s } from "./hands.js";
3
+ import { axisAngleToQuaternion as c, conjugateQuaternion as l, interpolateQuaternions as u, multiplyQuaternions as d, normalizeQuaternion as f, rotateByQuaternion as p, rotationToQuaternion as m } from "@margoengine/math/quat";
4
+ import { CCDSolver as h } from "@margoengine/animator/ccd";
5
+ import { add as g, cross as _, dot as v, length as y, normalize as b, scale as x, sub as S } from "@margoengine/math/vec3";
6
+ //#region src/bar-grip.ts
7
+ function C(e, t, r, i, a, o, s, m) {
8
+ let C = t.world(t.hand), w = b(_(r, i));
9
+ if (y(w) < .99) return;
10
+ let T = _(i, w), E = (e, t) => p(S(D(e).position, D(t).position), l(D(t).rotation)), D = (e) => t.world(e), O = t.fingers.index?.bones[0];
11
+ if (O === void 0) return;
12
+ let k = v(p(S(D(O).position, C.position), l(C.rotation)), w), A = o / 2, j = g(a, x(i, A));
13
+ for (let r of n) {
14
+ let n = t.fingers[r];
15
+ if (!n || n.curl.some((e) => y(e) < .9)) continue;
16
+ let [a, o, _] = n.bones;
17
+ if (e.parents[a] !== t.hand || e.parents[o] !== a || e.parents[_] !== o) continue;
18
+ let C = r === "thumb", O = e.parents.findIndex((e) => e === _), M = O < 0 ? p(S(D(_).position, D(o).position), l(D(_).rotation)) : E(O, _), N = x(b(M), n.lengths[2]), P = [
19
+ E(a, t.hand),
20
+ E(o, a),
21
+ E(_, o)
22
+ ];
23
+ if (y(N) < 1e-6 || P.slice(1).some((e) => y(e) < 1e-6)) continue;
24
+ let F = new h(P, N), I = n.lengths[1] * (C ? .23 : .28), L = /* @__PURE__ */ new Float64Array(12), R = /* @__PURE__ */ new Float64Array(12);
25
+ for (let [e, r] of n.bones.entries()) {
26
+ let n = f(t.rest.get(r) ?? [
27
+ 0,
28
+ 0,
29
+ 0,
30
+ 1
31
+ ]);
32
+ F.rest.set(n, e * 4), L.set(m.get(r) ?? n, e * 4), R.set(s.get(r) ?? n, e * 4);
33
+ }
34
+ F.setLimit(0, C ? {
35
+ type: "cone",
36
+ axis: b(P[1]),
37
+ angle: 1.12,
38
+ twist: [-.58, .58]
39
+ } : {
40
+ type: "hinge",
41
+ axis: n.curl[0],
42
+ min: -.08,
43
+ max: 1.56
44
+ });
45
+ let z = {
46
+ type: "hinge",
47
+ axis: n.curl[1],
48
+ min: -.15,
49
+ max: C ? 1.16 : 1.97
50
+ }, B = {
51
+ type: "hinge",
52
+ axis: n.curl[2],
53
+ min: -.15,
54
+ max: 1.37
55
+ };
56
+ F.setLimit(1, z), F.setLimit(2, B), F.tolerance = Math.max(5e-5, I * .018), F.iterations = 28;
57
+ let V = (e) => {
58
+ let t = S(F.positions.subarray(e, e + 3), j);
59
+ return [v(t, T), v(t, i)];
60
+ }, H = () => {
61
+ let e = 0;
62
+ for (let t = 0; t < 3; t++) {
63
+ let n = V(t * 3), r = V((t + 1) * 3), i = r[0] - n[0], a = r[1] - n[1], o = i * i + a * a, s = o > 0 ? Math.max(0, Math.min(1, -(n[0] * i + n[1] * a) / o)) : 0;
64
+ e = Math.max(e, A + I - Math.hypot(n[0] + i * s, n[1] + a * s));
65
+ }
66
+ return e;
67
+ }, U = v(P[0], w), W = C ? k * .7 + U * .3 : U, G = x(w, W - v(j, w)), K = g(j, G), q = (e, t, n, r = 0) => {
68
+ let a = g(x(T, Math.sin(e) * (C ? -1 : 1)), x(i, -Math.cos(e)));
69
+ return F.rotations.set(n), F.solve(g(g(K, x(w, r)), x(a, A + I + t))), F.rotations.slice();
70
+ }, J = (e, t) => {
71
+ let r = 0;
72
+ for (let n of [
73
+ 0,
74
+ .16,
75
+ .32,
76
+ .49,
77
+ .66,
78
+ .83,
79
+ 1
80
+ ]) {
81
+ for (let r of [
82
+ 0,
83
+ 4,
84
+ 8
85
+ ]) F.rotations.set(u(e.subarray(r, r + 4), t.subarray(r, r + 4), n, .9995), r);
86
+ F.update(), r = Math.max(r, H());
87
+ }
88
+ let i = V(9), a = 0;
89
+ if (C) for (let r of [1, 2]) {
90
+ let i = r * 4, o = d(l(e.subarray(i, i + 4)), t.subarray(i, i + 4)), s = 2 * Math.atan2(v(o.slice(0, 3), n.curl[r]), o[3]);
91
+ a += I * 16 * Math.max(0, .16 - Math.atan2(Math.sin(s), Math.cos(s)));
92
+ }
93
+ return Math.abs(Math.hypot(...i) - A - I) + 16 * r + a + Math.max(0, C ? i[0] : -i[0]);
94
+ }, Y = L.slice(), X = R, Z = J(R, L);
95
+ for (let e of C ? [
96
+ .4,
97
+ .68,
98
+ .95,
99
+ 1.22,
100
+ 1.48
101
+ ] : [
102
+ .74,
103
+ 1.06,
104
+ 1.42,
105
+ 1.78,
106
+ 2.14,
107
+ 2.52,
108
+ 2.88
109
+ ]) {
110
+ let t = q(e, 0, L), n = J(R, t);
111
+ if (n + 1e-6 < Z && (Z = n, Y = t, X = R), n > .003) for (let n of [
112
+ 0,
113
+ -.35,
114
+ .35
115
+ ]) for (let r of [
116
+ .007,
117
+ .013,
118
+ .022
119
+ ]) {
120
+ let i = q(e + n, r, t), a = J(i, t) + .001;
121
+ a + 1e-6 < Z && (Z = a, Y = t, X = i);
122
+ }
123
+ }
124
+ if (C) for (let e of [
125
+ .4,
126
+ .68,
127
+ .95,
128
+ 1.22,
129
+ 1.48,
130
+ 1.78,
131
+ 2.1,
132
+ 2.44
133
+ ]) for (let t of [
134
+ .17,
135
+ .32,
136
+ .51,
137
+ .73
138
+ ]) for (let n of [
139
+ 0,
140
+ -.02,
141
+ .02,
142
+ -.038,
143
+ .038
144
+ ]) {
145
+ for (let [e, n, r] of [[
146
+ 1,
147
+ z,
148
+ t
149
+ ], [
150
+ 2,
151
+ B,
152
+ t * .75
153
+ ]]) {
154
+ let t = e * 4;
155
+ L.set(d(F.rest.subarray(t, t + 4), c(n.axis, r)), t), F.setLimit(e, {
156
+ ...n,
157
+ min: r,
158
+ max: r + .14
159
+ });
160
+ }
161
+ let r = q(e, 0, L, n);
162
+ F.setLimit(1, z), F.setLimit(2, B);
163
+ for (let t of [
164
+ R,
165
+ X,
166
+ q(e, .013, r, n)
167
+ ]) {
168
+ let e = J(t, r);
169
+ e + 1e-6 < Z && (Z = e, Y = r, X = t);
170
+ }
171
+ }
172
+ for (let [e, t] of n.bones.entries()) m.set(t, Array.from(Y.subarray(e * 4, e * 4 + 4))), s.set(t, Array.from(X.subarray(e * 4, e * 4 + 4)));
173
+ }
174
+ }
175
+ //#endregion
176
+ //#region src/hinged-contact.ts
177
+ function w(e, t) {
178
+ return m([
179
+ ...e,
180
+ ..._(t, e),
181
+ ...t
182
+ ]);
183
+ }
184
+ function T(e, t, n, r, i) {
185
+ let a = e.joints - 1, o = a - 1, s = a - 2, u = (t) => [
186
+ e.positions[t * 3],
187
+ e.positions[t * 3 + 1],
188
+ e.positions[t * 3 + 2]
189
+ ], f = (t) => e.worldRotations.slice(t * 4, t * 4 + 4), m = (t) => e.rest.subarray(t * 4, t * 4 + 4), h = (t) => b(e.offsets.subarray(t * 3, t * 3 + 3)), C = h(o), T = h(a), E = b(i.axis), D = p(E, m(o)), O = p(T, m(o));
190
+ if (Math.abs(v(D, C)) > 1e-5 || Math.abs(v(D, O)) > 1e-5 || i.twist && v(b(i.twist.axis), T) < .99999) throw RangeError("Hinged contact needs a bend axis perpendicular to both links and forearm roll along the last link");
191
+ let k = Math.atan2(v(D, _(C, O)), v(C, O)), A = Math.max(1e-5, k + (i.min ?? -Math.PI)), j = Math.min(Math.PI - 1e-5, k + (i.max ?? Math.PI));
192
+ if (j < A) throw RangeError("Hinged contact needs a flexion range that intersects the positive bend branch");
193
+ e.projectRotations(), e.update();
194
+ let M = u(s), N = u(o), P = u(a), F = y(S(N, M)), I = y(S(P, N)), L = S(t, M), R = y(L), z = R < 1e-8 ? b(S(P, M)) : x(L, 1 / R);
195
+ if (F < 1e-8 || I < 1e-8) throw RangeError("Hinged contact needs two links with nonzero lengths");
196
+ let B = b(S(r, M)), V = _(B, z);
197
+ if (y(V) < 1e-6) throw RangeError("Choose a hinged contact pole off the shoulder-to-target line to define a bend plane");
198
+ let H = (e) => Math.sqrt(Math.max(0, F * F + I * I + 2 * F * I * Math.cos(e))), U = Math.max(H(j), Math.min(H(A), R)), W = b(V), G = (F * F - I * I + U * U) / (2 * U), K = Math.sqrt(Math.max(0, F * F - G * G)), q = b(g(x(z, G), x(_(z, W), K))), J = (U * U - F * F - I * I) / (2 * F * I), Y = Math.acos(Math.max(-1, Math.min(1, J))), X = d(w(q, W), l(w(C, D))), Z = s === 0 ? e.parentRotation : f(s - 1), Q = d(m(o), c(E, Y - k)), ee = d(X, Q), $ = 0;
199
+ if (i.twist) {
200
+ let e = d(l(ee), d(n, l(m(a)))), t = 2 * Math.atan2(v(e.slice(0, 3), T), e[3]), r = Math.atan2(Math.sin(t), Math.cos(t));
201
+ $ = Math.max(i.twist.range[0], Math.min(i.twist.range[1], r));
202
+ }
203
+ let te = d(Q, c(T, $)), ne = d(X, te);
204
+ e.rotations.set(d(l(Z), X), s * 4), e.rotations.set(te, o * 4), e.rotations.set(d(l(ne), n), a * 4), e.projectRotations(), e.update();
205
+ }
206
+ //#endregion
207
+ //#region src/limb-collision.ts
208
+ var E = [
209
+ 0,
210
+ 0,
211
+ 0
212
+ ], D = (e, t) => [
213
+ e[t],
214
+ e[t + 1],
215
+ e[t + 2]
216
+ ];
217
+ function O(e) {
218
+ return e?.length === 3 && Number.isFinite(e[0]) && Number.isFinite(e[1]) && Number.isFinite(e[2]);
219
+ }
220
+ function k(e, t, n) {
221
+ let r = v(e, t) + y(e) * n;
222
+ if (!Number.isFinite(r)) throw RangeError("Give the direction limit finite vectors at scene scale");
223
+ return r;
224
+ }
225
+ function A(e, t, n, r) {
226
+ let i = D(e, n), a = D(t, n), o = D(e, n + 3), s = D(t, n + 3);
227
+ if (Math.abs(y(o) - 1) > 1e-9 || Math.abs(y(s) - 1) > 1e-9) throw RangeError("Direction samples need unit body axes");
228
+ let c = b(o), l = b(s), u = _(c, l), d = y(u), f = v(c, l), p = Math.atan2(d, f), m = _(b(u), c), h = S(a, i), g = (1 + r) * y(h) + p * Math.max(y(i), y(a));
229
+ if (!Number.isFinite(g)) throw RangeError("Give the direction sweep finite motion at scene scale");
230
+ return {
231
+ start: i,
232
+ delta: h,
233
+ axis: c,
234
+ tangent: m,
235
+ arc: p,
236
+ allowance: r,
237
+ speed: g,
238
+ opposed: d <= 1e-12 && f < 0
239
+ };
240
+ }
241
+ function j(e, t) {
242
+ let n = e.arc * t;
243
+ return k(g(e.start, x(e.delta, t)), g(x(e.axis, Math.cos(n)), x(e.tangent, Math.sin(n))), e.allowance);
244
+ }
245
+ function M(e, t, n) {
246
+ return y(S(e, x(t, Math.max(0, Math.min(n, v(e, t))))));
247
+ }
248
+ function N(e, t, n, r) {
249
+ let i = S(t, e), a = S(r, n), o = S(n, e), s = b(i), c = b(a), l = y(i), u = y(a), d = Math.min(M(x(o, -1), c, u), M(S(i, o), c, u), M(o, s, l), M(S(r, e), s, l)), f = _(s, c), p = v(f, f);
250
+ if (p > 0) {
251
+ let e = v(_(o, c), f) / p, t = v(_(o, s), f) / p;
252
+ e >= 0 && e <= l && t >= 0 && t <= u && (d = Math.min(d, Math.abs(v(o, f)) / Math.sqrt(p)));
253
+ }
254
+ return d;
255
+ }
256
+ function P(e, t, n, r) {
257
+ let { position: i, linear: a } = e, o = t * 9;
258
+ return [
259
+ a[o] * n[0] + a[o + 3] * n[1] + a[o + 6] * n[2] + (r ? i[t * 3] : 0),
260
+ a[o + 1] * n[0] + a[o + 4] * n[1] + a[o + 7] * n[2] + (r ? i[t * 3 + 1] : 0),
261
+ a[o + 2] * n[0] + a[o + 5] * n[1] + a[o + 8] * n[2] + (r ? i[t * 3 + 2] : 0)
262
+ ];
263
+ }
264
+ var F = class {
265
+ constructor(t, n, r, i = []) {
266
+ if (this.axes = [], this.posed = !1, this.clearance = (e) => {
267
+ this.packLimb(e, this.candidate);
268
+ let t = this.measure(this.body, this.candidate);
269
+ return this.limits.forEach(({ start: n, end: r, maxBackwards: i }, a) => {
270
+ let o = S(D(e, r * 3), D(e, n * 3));
271
+ t = Math.min(t, k(o, this.axes[a], Math.sin(i)));
272
+ }), t;
273
+ }, !t.length || t.length > 65536 || t.some((e) => !Number.isInteger(e) || e < -1 || e >= t.length)) throw RangeError("Limb collision needs 1–65,536 joints with valid parent indices or -1");
274
+ let a = (e) => Number.isInteger(e) && e >= 0 && e < t.length, o = (e) => Number.isInteger(e) && e >= 0 && e < 65536, s = (e) => {
275
+ let t = e.offset ?? E;
276
+ if (!a(e.joint) || !O(t)) throw RangeError("Body collision landmarks need a valid bone and a finite local XYZ offset");
277
+ return {
278
+ joint: e.joint,
279
+ offset: [...t]
280
+ };
281
+ }, c = (e) => {
282
+ if (!(Number.isFinite(e) && e >= 0)) throw RangeError("Collision radii must be finite and nonnegative");
283
+ return e;
284
+ };
285
+ if (!n.length || !r.length) throw RangeError("Pass at least one body volume and one limb link to check");
286
+ this.parents = [...t], this.order = e(this.parents, "Limb collision"), this.volumes = n.map(({ start: e, end: t, radius: n }) => ({
287
+ start: s(e),
288
+ end: t ? s(t) : void 0,
289
+ radius: c(n)
290
+ })), this.links = r.map(({ start: e, end: t, radius: n, range: r = [0, 1] }) => {
291
+ if (!(o(e) && o(t) && r.length === 2 && r[0] >= 0 && r[1] <= 1 && r[0] <= r[1])) throw RangeError("Limb links need valid joint slots and a range of fractions with 0 ≤ start ≤ end ≤ 1");
292
+ return {
293
+ start: e,
294
+ end: t,
295
+ radius: c(n),
296
+ range: [...r]
297
+ };
298
+ }), this.limits = i.map(({ start: e, end: t, reference: n, forward: r, maxBackwards: i = .75 }) => {
299
+ if (!(o(e) && o(t) && e !== t && a(n) && O(r) && y(r) > 0 && i >= 0 && i <= Math.PI / 2)) throw RangeError("Direction limits need distinct valid slots, a reference bone, a forward axis and an angle from 0 to π/2");
300
+ return {
301
+ start: e,
302
+ end: t,
303
+ reference: n,
304
+ forward: b(r),
305
+ maxBackwards: i
306
+ };
307
+ }), this.slots = [...this.links, ...this.limits].reduce((e, { start: t, end: n }) => Math.max(e, t, n), 0) + 1, this.body = new Float64Array(this.volumes.length * 6), this.candidate = new Float64Array(this.links.length * 6);
308
+ }
309
+ setBodyPose(e) {
310
+ if (e.length !== this.parents.length * 10) throw RangeError("A collision pose needs ten local transform components per joint");
311
+ for (let t = 0; t < e.length; t++) if (!Number.isFinite(e[t])) throw RangeError("Give the collision rig finite translation, quaternion and scale values");
312
+ let n = t(this.parents, e, this.order), r = new Float64Array(this.body.length), i = (e) => P(n, e.joint, e.offset ?? E, !0);
313
+ this.volumes.forEach(({ start: e, end: t }, n) => {
314
+ let a = i(e), o = t ? i(t) : a;
315
+ if (!O(a) || !O(o)) throw RangeError("Collision body landmarks overflowed; use finite transforms at scene scale");
316
+ r.set(a, n * 6), r.set(o, n * 6 + 3);
317
+ });
318
+ let a = this.limits.map(({ reference: e, forward: t }) => {
319
+ let r = b(P(n, e, t, !1));
320
+ if (y(r) < .9) throw RangeError("A collision direction needs a non-degenerate reference frame");
321
+ return r;
322
+ });
323
+ this.body.set(r), this.axes = a, this.posed = !0;
324
+ }
325
+ snapshot(e) {
326
+ this.packLimb(e, this.candidate);
327
+ let t = {
328
+ body: this.body.slice(),
329
+ limb: this.candidate.slice()
330
+ };
331
+ if (!this.limits.length) return t;
332
+ let n = new Float64Array(this.limits.length * 6);
333
+ return this.limits.forEach(({ start: t, end: r }, i) => {
334
+ let a = S(D(e, r * 3), D(e, t * 3));
335
+ if (!O(a)) throw RangeError("Give the direction sample finite joint displacements at scene scale");
336
+ n.set(a, i * 6), n.set(this.axes[i], i * 6 + 3);
337
+ }), {
338
+ ...t,
339
+ directions: n
340
+ };
341
+ }
342
+ sweep(e, t, { tolerance: n = 1e-4, maxSteps: r = 128 } = {}) {
343
+ if (!(n > 0 && Number.isFinite(n) && Number.isInteger(r) && r >= 1 && r <= 4096)) throw RangeError("Swept limb collision needs positive tolerance and an integer step budget from 1 to 4096");
344
+ for (let n of [e, t]) if (n.body.length !== this.body.length || n.limb.length !== this.candidate.length || !n.body.every(Number.isFinite) || !n.limb.every(Number.isFinite)) throw RangeError("Sweep samples must come from the same proxy layout and contain finite positions");
345
+ let i = e.directions, a = t.directions;
346
+ if (!!i != !!a) throw RangeError("Pass direction data in both sweep snapshots, or two legacy snapshots");
347
+ for (let e of [i, a]) if (e && (e.length !== this.limits.length * 6 || !e.every(Number.isFinite))) throw RangeError("Sweep direction samples must match the rig layout and contain finite vectors");
348
+ let o = i && a ? this.limits.map(({ maxBackwards: e }, t) => A(i, a, t * 6, Math.sin(e))) : [], s = o.some((e) => e.opposed), c = e.body.slice(), l = e.limb.slice(), u = t.body.map((t, n) => t - e.body[n]), d = t.limb.map((t, n) => t - e.limb[n]), f = D(u, 0), p = (e) => {
349
+ let t = 0;
350
+ for (let n = 0; n < e.length; n += 3) t = Math.max(t, y(S(D(e, n), f)));
351
+ return t;
352
+ }, m = p(u) + p(d);
353
+ if (!Number.isFinite(m)) throw RangeError("Give the collision sweep finite motion at scene scale");
354
+ let h = 0, g = Infinity;
355
+ for (let t = 0; t < r; t++) {
356
+ let t = this.measure(c, l), r = t, i = m > 0 ? (t - n * .5) / m : Infinity;
357
+ for (let e of o) {
358
+ let t = j(e, h);
359
+ r = Math.min(r, t), e.speed > 0 && (i = Math.min(i, (t - n * .5) / e.speed));
360
+ }
361
+ if (g = Math.min(g, r), r <= n) return {
362
+ clear: !1,
363
+ time: h,
364
+ clearance: g,
365
+ exhausted: !1
366
+ };
367
+ if (s) break;
368
+ if (h === 1 || i === Infinity) return {
369
+ clear: !0,
370
+ time: 1,
371
+ clearance: g,
372
+ exhausted: !1
373
+ };
374
+ let a = Math.min(1, h + i);
375
+ if (a <= h) break;
376
+ h = a;
377
+ for (let t = 0; t < c.length; t++) c[t] = e.body[t] + u[t] * h;
378
+ for (let t = 0; t < l.length; t++) l[t] = e.limb[t] + d[t] * h;
379
+ }
380
+ return {
381
+ clear: !1,
382
+ time: h,
383
+ clearance: g,
384
+ exhausted: !0
385
+ };
386
+ }
387
+ packLimb(e, t) {
388
+ if (!this.posed) throw RangeError("Call setBodyPose before checking a limb");
389
+ if (e.length < this.slots * 3) throw RangeError("Pass packed XYZ positions for every configured collision joint slot");
390
+ for (let t = 0; t < this.slots * 3; t++) if (!Number.isFinite(e[t])) throw RangeError("Limb collision needs finite candidate points");
391
+ this.links.forEach(({ start: n, end: r, range: i }, a) => {
392
+ let o = n * 3, s = r * 3, c = a * 6;
393
+ for (let n = 0; n < 3; n++) {
394
+ let r = e[s + n] - e[o + n];
395
+ t[c + n] = e[o + n] + r * i[0], t[c + 3 + n] = e[o + n] + r * i[1];
396
+ }
397
+ });
398
+ }
399
+ measure(e, t) {
400
+ let n = Infinity;
401
+ return this.links.forEach(({ radius: r }, i) => {
402
+ let a = D(t, i * 6), o = D(t, i * 6 + 3);
403
+ this.volumes.forEach((t, i) => {
404
+ let s = N(a, o, D(e, i * 6), D(e, i * 6 + 3)) - r - t.radius;
405
+ if (!Number.isFinite(s)) throw RangeError("Collision distance overflowed; use finite positions at scene scale");
406
+ n = Math.min(n, s);
407
+ });
408
+ }), n;
409
+ }
410
+ };
411
+ //#endregion
412
+ //#region src/contact.ts
413
+ function I(e, t) {
414
+ if (!(e?.length === 4 && e.every(Number.isFinite) && Math.hypot(...e) > 0)) throw RangeError(`Contact IK ${t} needs a finite, nonzero quaternion`);
415
+ return f(e);
416
+ }
417
+ function L(n, r) {
418
+ let { parents: i, local: a } = n, { joints: o, point: s, anchor: c, rotation: l, pole: d, limits: f = [], clearance: m } = r, { weight: _ = 1, iterations: v = 64, tolerance: y = 1e-4, angleTolerance: b = .02 } = r;
419
+ if (!Array.isArray(i) || !(a instanceof Float64Array) || a.length !== i.length * 10) throw TypeError("Contact IK needs a pose with parents and 10 local values per joint");
420
+ if (!Array.isArray(o) || o.length < 2 || o.length > 256) throw TypeError("Contact IK needs a chain of 2 to 256 joints");
421
+ if (!o.every((e, t) => Number.isInteger(e) && e >= 0 && e < i.length && (t === 0 || i[e] === o[t - 1]))) throw RangeError("Contact IK joints must form a parent-child chain");
422
+ if (i.some((e) => !Number.isInteger(e) || e < -1 || e >= i.length)) throw RangeError("Contact IK parents must be valid joint indices or -1");
423
+ if (!(s?.length === 3 && c?.length === 3 && [...s, ...c].every(Number.isFinite))) throw RangeError("Contact IK point and anchor need three finite coordinates each");
424
+ if (!(_ >= 0 && _ <= 1 && y > 0 && Number.isFinite(y) && b > 0 && b <= Math.PI && Number.isInteger(v) && v >= 1 && v <= 1024)) throw RangeError("Contact IK weight, tolerance or iteration count is out of range; use the supported ranges");
425
+ let C = e(i, "Contact IK"), w = o.at(-1), E = w * 10, D = I(l, "rotation"), O = i[o[0]], k = [
426
+ c[0] * a[E + 7],
427
+ c[1] * a[E + 8],
428
+ c[2] * a[E + 9]
429
+ ], A = 1, j = 1;
430
+ for (let e = i[w]; e >= 0; e = i[e]) {
431
+ let t = e * 10, n = a[t + 7];
432
+ if (!(n > 0 && Number.isFinite(n) && a[t + 8] === n && a[t + 9] === n)) throw RangeError("Contact IK needs uniform positive ancestor scales; apply scale to the mesh before rigging");
433
+ A *= n;
434
+ }
435
+ for (let e = O; e >= 0; e = i[e]) j *= a[e * 10 + 7];
436
+ if (!(Number.isFinite(A) && A > 0 && k.every(Number.isFinite) && a.subarray(E + 7, E + 10).every((e) => e > 0 && Number.isFinite(e)))) throw RangeError("Contact IK needs finite positive scales");
437
+ let M = x(k, A), N = t(i, a, C), P = N.rotation.subarray(w * 4, w * 4 + 4), F = g(N.position.subarray(w * 3, w * 3 + 3), p(M, P)), L = () => B(F, P, s, D, y, b), V = () => R(m, o.flatMap((e) => Array.from(N.position.subarray(e * 3, e * 3 + 3))), o.flatMap((e) => Array.from(N.rotation.subarray(e * 4, e * 4 + 4))));
438
+ if (_ === 0) {
439
+ let e = L();
440
+ if (!m) return e;
441
+ let t = V();
442
+ return {
443
+ ...e,
444
+ locked: e.locked && t >= 0,
445
+ blocked: t < 0,
446
+ clearance: t
447
+ };
448
+ }
449
+ let H = new h(o.map((e) => {
450
+ let t = e * 10;
451
+ return [
452
+ a[t],
453
+ a[t + 1],
454
+ a[t + 2]
455
+ ];
456
+ }));
457
+ H.iterations = v, H.tolerance = y, H.angleTolerance = b, O >= 0 && (H.parentPosition.set(N.position.subarray(O * 3, O * 3 + 3)), H.parentRotation.set(N.rotation.subarray(O * 4, O * 4 + 4)), H.parentScale[0] = j), o.forEach((e, t) => {
458
+ let n = e * 10;
459
+ H.rotations.set(a.subarray(n + 3, n + 7), t * 4), e !== w && (H.scales[t] = a[n + 7]);
460
+ });
461
+ let U = /* @__PURE__ */ new Set();
462
+ for (let { joint: e, neutral: t, limit: n } of f) {
463
+ let r = o.indexOf(e);
464
+ if (r < 0 || U.has(e)) throw RangeError("Contact IK limits must name distinct joints in the chain");
465
+ U.add(e), H.rest.set(I(t, "neutral rotation"), r * 4), H.setLimit(r, n);
466
+ }
467
+ let W = u(P, D, _, .9995), G = g(F, x(S(s, F), _)), K = S(G, p(M, W)), q;
468
+ if (r.reach === "hinged") {
469
+ let e = f.find(({ joint: e }) => e === o[o.length - 2])?.limit;
470
+ if (o.length < 3 || !d || e?.type !== "hinge") throw RangeError("Hinged contact requires three joints or more, an explicit pole and a penultimate hinge limit");
471
+ if (T(H, K, W, d, e), m && (q = R(m, H.positions.subarray(0, H.joints * 3), H.worldRotations), q < 0)) return {
472
+ ...L(),
473
+ locked: !1,
474
+ blocked: !0,
475
+ clearance: V()
476
+ };
477
+ } else if (m) {
478
+ let e = o[o.length - 2] * 3;
479
+ if (q = z(H, K, W, d ?? [
480
+ N.position[e],
481
+ N.position[e + 1],
482
+ N.position[e + 2]
483
+ ], m), q === void 0) return {
484
+ ...L(),
485
+ locked: !1,
486
+ blocked: !0,
487
+ clearance: V()
488
+ };
489
+ } else H.solve(K, W, d);
490
+ let J = o.length - 1, Y = H.worldRotations.subarray(J * 4, J * 4 + 4), X = g(H.positions.subarray(J * 3, J * 3 + 3), p(M, Y));
491
+ o.forEach((e, t) => a.set(H.rotations.subarray(t * 4, t * 4 + 4), e * 10 + 3));
492
+ let Z = B(X, Y, s, D, y, b);
493
+ return q === void 0 ? Z : {
494
+ ...Z,
495
+ blocked: !1,
496
+ clearance: q
497
+ };
498
+ }
499
+ function R(e, t, n) {
500
+ let r = e(t, n);
501
+ if (!Number.isFinite(r) && r !== Infinity) throw RangeError("Contact clearance must return a signed distance, or positive infinity when no obstacle applies");
502
+ return r;
503
+ }
504
+ function z(e, t, n, r, i) {
505
+ let a = e.positions.subarray(0, e.joints * 3), o = e.solve(t, n, r), s = R(i, a, e.worldRotations);
506
+ if (s >= 0 && o.reached) return s;
507
+ let u = Math.max(0, e.joints - 3), f = a.slice(u * 3, u * 3 + 3), m = b(S(t, f));
508
+ if (y(m) < .9) return s >= 0 ? s : void 0;
509
+ let h = e.rotations.slice(), v = (e.joints - 1) * 4, x = u * 4, C = h.subarray(x, x + 4), w = u === 0 ? e.parentRotation : e.worldRotations.slice(x - 4, x), T = l(w), E = e.worldRotations.slice(v - 4, v), D = e.worldRotations.slice(v, v + 4), O = S(r, f);
510
+ y(_(m, O)) < 1e-6 && (O = _(m, Math.abs(m[1]) < .9 ? [
511
+ 0,
512
+ 1,
513
+ 0
514
+ ] : [
515
+ 1,
516
+ 0,
517
+ 0
518
+ ]));
519
+ let k = (t, n) => t / e.tolerance + n / e.angleTolerance, A = s >= 0 ? {
520
+ distance: k(o.distance, o.angle),
521
+ gap: s,
522
+ pose: h
523
+ } : void 0;
524
+ for (let r of [
525
+ .32,
526
+ -.32,
527
+ .65,
528
+ -.65,
529
+ 1.05,
530
+ -1.05,
531
+ 1.6,
532
+ -1.6,
533
+ 2.25,
534
+ -2.25,
535
+ Math.PI
536
+ ]) {
537
+ let o = c(m, r);
538
+ e.rotations.set(h);
539
+ let s = d(T, d(o, w));
540
+ e.rotations.set(d(s, C), x), e.rotations.set(d(l(d(o, E)), D), v);
541
+ let u = e.solve(t, n, g(f, p(O, o))), _ = R(i, a, e.worldRotations);
542
+ if (_ < 0) continue;
543
+ if (u.reached) return _;
544
+ let y = k(u.distance, u.angle);
545
+ (!A || y < A.distance) && (A = {
546
+ distance: y,
547
+ gap: _,
548
+ pose: e.rotations.slice()
549
+ });
550
+ }
551
+ if (A) return e.rotations.set(A.pose), e.update(), A.gap;
552
+ }
553
+ function B(e, t, n, r, i, a) {
554
+ let o = y(S(e, n)), s = t[0] * r[0] + t[1] * r[1] + t[2] * r[2] + t[3] * r[3], c = 2 * Math.acos(Math.min(1, Math.abs(s)));
555
+ return {
556
+ locked: o <= i && c <= a,
557
+ distance: o,
558
+ angle: c,
559
+ point: e
560
+ };
561
+ }
562
+ function V(e, t) {
563
+ let n = b(t), r = b(S(e, x(n, v(e, n))));
564
+ if (y(n) < .9 || y(r) < .9 || y(_(b(e), n)) < 1e-5) throw RangeError("Palm orientation needs a nonzero normal and a finger direction across that normal");
565
+ return f(m([
566
+ ...r,
567
+ ...n,
568
+ ..._(r, n)
569
+ ]));
570
+ }
571
+ function H(e, t) {
572
+ if (e?.length !== 3 || !e.every(Number.isFinite)) throw RangeError(`${t} needs three finite coordinates`);
573
+ }
574
+ function U(e, t) {
575
+ let n = t * 10 + 3;
576
+ return I([
577
+ e.rest[n],
578
+ e.rest[n + 1],
579
+ e.rest[n + 2],
580
+ e.rest[n + 3]
581
+ ], "local rest rotation");
582
+ }
583
+ function W(e, t, n, r) {
584
+ let { joint: i, flexDirection: a, min: o = -.035, max: s = 2.55, roll: c = [-1.25, 1.25] } = r, u = n.indexOf(i);
585
+ if (u <= 0 || u >= n.length - 1) throw RangeError("Elbow calibration needs an intermediate joint of the arm chain");
586
+ H(a, "Elbow flexion direction");
587
+ let d = t.world(i), f = b(S(t.world(t.hand).position, d.position)), m = _(f, b(a)), h = l(d.rotation);
588
+ if (y(m) < 1e-5) throw RangeError("Elbow flexion needs a direction across the forearm");
589
+ return {
590
+ joint: i,
591
+ neutral: U(e, i),
592
+ limit: {
593
+ type: "hinge",
594
+ axis: p(b(m), h),
595
+ min: o,
596
+ max: s,
597
+ twist: c === !1 ? void 0 : {
598
+ axis: p(f, h),
599
+ range: c
600
+ }
601
+ }
602
+ };
603
+ }
604
+ var G = class {
605
+ constructor(n, r) {
606
+ this.rig = n;
607
+ let { side: c, preset: u, bones: d, joints: f, limits: m = [], anchor: h, elbow: _ } = r;
608
+ if (c !== "left" && c !== "right") throw RangeError("Choose left or right for the palm calibration");
609
+ if (n.parents.some((e) => !Number.isInteger(e) || e < -1 || e >= n.parents.length)) throw RangeError("Palm calibration needs parents with valid joint indices or −1");
610
+ let C = e(n.parents, "Palm calibration"), w = a(n, u, d)[c], T = w?.fingers.middle?.bones[0];
611
+ if (!w || T === void 0) throw RangeError("Palm calibration needs an index, middle and outer finger mapped on the requested hand");
612
+ if (!Array.isArray(f) || f.length < 2 || f.length > 256 || f.at(-1) !== w.hand || !f.every((e, t) => Number.isInteger(e) && e >= 0 && e < n.parents.length && (!t || n.parents[e] === f[t - 1]))) throw RangeError("Palm calibration needs a consecutive 2–256 joint arm ending at the mapped wrist");
613
+ let E = w.world(w.hand), D = l(I(E.rotation, "wrist rest rotation")), O = p(S(w.world(T).position, E.position), D), k = p(w.palm, D), A = x(k, y(O) * .085);
614
+ this.inversePalm = l(V(O, k));
615
+ let j = t(n.parents, n.rest, C).linear, M = w.hand * 9, N = [
616
+ 0,
617
+ 1,
618
+ 2
619
+ ].map((e) => {
620
+ let t = M + e * 3;
621
+ return Math.hypot(j[t], j[t + 1], j[t + 2]);
622
+ }), P = y(O), F = b(O), L = w.fingers.thumb, R = L ? v(p(S(w.world(L.bones[0]).position, E.position), D), F) : 0, z = Math.min(P * .88, Math.max(P * .56, R + .9 * (L?.lengths[1] ?? 0))), B = g(x(F, z), A).map((e, t) => e / N[t]);
623
+ this.anchor = h ? [...h] : B, H(this.anchor, "Palm anchor"), this.gripForward = b(O), this.gripNormal = b(k), this.gripAnchor = this.anchor.map((e, t) => e * N[t]);
624
+ let { flexion: G = 1.12, extension: K = 1, deviation: q = .46 } = r.wrist || {};
625
+ if (![
626
+ G,
627
+ K,
628
+ q
629
+ ].every((e) => e >= 0 && e <= Math.PI / 2)) throw RangeError("Palm wrist ranges need angles from zero to π/2 radians");
630
+ this.wrist = r.wrist === !1 ? null : {
631
+ flexion: G,
632
+ extension: K,
633
+ deviation: q
634
+ }, this.hand = w, this.arm = [...f];
635
+ let J = [...m];
636
+ if (_ && !J.some(({ joint: e }) => e === _.joint) && J.push(W(n, w, f, _)), J.some(({ joint: e }) => e === w.hand) || J.push({
637
+ joint: w.hand,
638
+ neutral: U(n, w.hand),
639
+ limit: {
640
+ type: "cone",
641
+ axis: b(O),
642
+ angle: 1.15,
643
+ twist: [-.22, .22]
644
+ }
645
+ }), this.limits = J, this.tolerance = r.tolerance ?? .003, this.angleTolerance = r.angleTolerance ?? Math.PI / 36, !(this.tolerance > 0 && Number.isFinite(this.tolerance) && this.angleTolerance > 0 && this.angleTolerance <= Math.PI)) throw RangeError("Palm tolerances need a finite positive distance and an angle between zero and π");
646
+ this.open = i(w, o(s(), w));
647
+ for (let [e, t] of w.rest) this.open.has(e) || this.open.set(e, [...t]);
648
+ this.joints = [...this.arm, ...this.open.keys()];
649
+ }
650
+ solve(e, t) {
651
+ let { point: n, normal: a, towardsFingers: s, diameter: c, close: l = 1, weight: p = 1, elbowPole: m, clearance: h } = t;
652
+ if (H(a, "Surface normal"), H(s, "Direction toward fingers"), m && H(m, "Elbow pole"), !(l >= 0 && l <= 1 && p >= 0 && p <= 1)) throw RangeError("Hand closure and reach weight must be from zero to one");
653
+ if (this.gripPose?.diameter !== c) {
654
+ let e = i(this.hand, o(r(c), this.hand)), t = new Map(this.open);
655
+ C(this.rig, this.hand, this.gripForward, this.gripNormal, this.gripAnchor, c, t, e), this.gripPose = {
656
+ diameter: c,
657
+ approach: t,
658
+ closed: e
659
+ };
660
+ }
661
+ let g = f(d(V(s, x(a, -1)), this.inversePalm)), _ = this.wrist, v = _ ? (e, t) => Math.min(this.wristGap(e, t, _), h?.(e, t) ?? Infinity) : h, y = L({
662
+ parents: this.rig.parents,
663
+ local: e
664
+ }, {
665
+ joints: this.arm,
666
+ point: n,
667
+ anchor: this.anchor,
668
+ rotation: g,
669
+ weight: p,
670
+ pole: m,
671
+ reach: t.reach,
672
+ limits: this.limits,
673
+ clearance: v,
674
+ tolerance: this.tolerance * .05,
675
+ angleTolerance: this.angleTolerance * .05
676
+ }), b = y.blocked ? Infinity : Math.max(y.distance / this.tolerance, y.angle / this.angleTolerance), S = Math.max(0, 1 - b), w = p === 0 ? 0 : l * S * S * (3 - 2 * S);
677
+ if (p > 0) for (let [t, n] of this.gripPose.approach) {
678
+ let r = u(n, this.gripPose.closed.get(t) ?? n, w, .9995);
679
+ e.set(r, t * 10 + 3);
680
+ }
681
+ return {
682
+ ...y,
683
+ locked: b <= 1,
684
+ gripWeight: w
685
+ };
686
+ }
687
+ wristGap(e, t, n) {
688
+ let r = this.arm.length - 1, i = r * 3, a = r * 4, o = S([
689
+ e[i],
690
+ e[i + 1],
691
+ e[i + 2]
692
+ ], [
693
+ e[i - 3],
694
+ e[i - 2],
695
+ e[i - 1]
696
+ ]), s = y(o);
697
+ if (s < 1e-6) return Infinity;
698
+ let c = [
699
+ -t[a],
700
+ -t[a + 1],
701
+ -t[a + 2],
702
+ t[a + 3]
703
+ ], l = p(o, c), u = b(_(this.gripForward, this.gripNormal)), d = _(this.gripNormal, u), f = v(l, d), m = v(l, this.gripNormal), h = v(l, u), g = -Math.atan2(m, f), x = Math.atan2(Math.abs(h), Math.hypot(f, m));
704
+ return s * Math.min(n.flexion - g, n.extension + g, n.deviation - x);
705
+ }
706
+ };
707
+ function K(e, t, n, r) {
708
+ let i = e.animation.model, a = i.nodes, o = i.root.engine.store, s = new Map(a.map((e, t) => [e.id, t])), c = a.map((e) => s.get(o.parents[e.id]) ?? -1), l = new Float64Array(a.length * 10);
709
+ a.forEach((t, n) => {
710
+ l.set(e.restPosition(n), n * 10), l.set(e.restRotation(n), n * 10 + 3), l.set(e.restScale(n), n * 10 + 7);
711
+ });
712
+ let u = new G({
713
+ names: a.map((e) => e.name ?? ""),
714
+ parents: c,
715
+ rest: l
716
+ }, t), d = new Float64Array(l.length), f = [
717
+ 0,
718
+ 0,
719
+ 0,
720
+ 1
721
+ ];
722
+ return e.addSolver(() => {
723
+ let t = n();
724
+ if (!t) return;
725
+ let i = o.transforms;
726
+ a.forEach(({ id: e }, t) => {
727
+ let n = t * 10;
728
+ d[n] = i.x[e], d[n + 1] = i.y[e], d[n + 2] = i.z[e], d[n + 3] = i.qx[e], d[n + 4] = i.qy[e], d[n + 5] = i.qz[e], d[n + 6] = i.qw[e], d[n + 7] = i.sx[e], d[n + 8] = i.sy[e], d[n + 9] = i.sz[e];
729
+ });
730
+ let s = u.solve(d, t);
731
+ for (let t of u.joints) {
732
+ for (let e = 0; e < 4; e++) f[e] = d[t * 10 + 3 + e];
733
+ e.writeNodeRotation(a[t], f);
734
+ }
735
+ o.tick(!1), r?.(s);
736
+ });
737
+ }
738
+ //#endregion
739
+ export { F as LimbCollisionRig, G as PalmContactRig, K as addPalmContact, L as solveContactIK };