@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/hulls.js ADDED
@@ -0,0 +1,134 @@
1
+ import { t as e } from "./weight-map-D8cAONQQ.js";
2
+ import { convexGeometry as t } from "@margoengine/geometry/extras";
3
+ //#region src/hulls.ts
4
+ var n = Math.PI * (3 - Math.sqrt(5));
5
+ function r(e, t) {
6
+ let r = e.length / 3, i = /* @__PURE__ */ new Set(), a = (t, n, a) => {
7
+ let o = 0, s = -Infinity;
8
+ for (let i = 0; i < r; i++) {
9
+ let r = e[i * 3] * t + e[i * 3 + 1] * n + e[i * 3 + 2] * a;
10
+ r > s && (s = r, o = i);
11
+ }
12
+ i.add(o);
13
+ };
14
+ for (let [e, t, n] of [
15
+ [
16
+ 1,
17
+ 0,
18
+ 0
19
+ ],
20
+ [
21
+ -1,
22
+ 0,
23
+ 0
24
+ ],
25
+ [
26
+ 0,
27
+ 1,
28
+ 0
29
+ ],
30
+ [
31
+ 0,
32
+ -1,
33
+ 0
34
+ ],
35
+ [
36
+ 0,
37
+ 0,
38
+ 1
39
+ ],
40
+ [
41
+ 0,
42
+ 0,
43
+ -1
44
+ ]
45
+ ]) a(e, t, n);
46
+ let o = t - 6;
47
+ for (let e = 0; e < o; e++) {
48
+ let t = 1 - 2 * (e + .5) / o, r = Math.sqrt(1 - t * t), i = e * n;
49
+ a(r * Math.cos(i), t, r * Math.sin(i));
50
+ }
51
+ let s = i.size >= 4 ? [...i] : Array.from({ length: t }, (e, t) => t), c = new Float32Array(s.length * 3);
52
+ return s.forEach((t, n) => c.set(e.subarray(t * 3, t * 3 + 3), n * 3)), c;
53
+ }
54
+ function i(n, i = {}) {
55
+ let a = n?.positions, o = n?.skin;
56
+ if (!a || typeof a.length != "number" || !o) throw TypeError("jointHulls needs a model {positions, skin}: bind-pose xyz positions and skin weights");
57
+ let { minWeight: s = .5, maxPoints: c = 64, inverseBindMatrices: l } = i;
58
+ if (!(s > 0 && s <= 1)) throw RangeError("jointHulls minWeight must be above 0 and at most 1");
59
+ if (!Number.isInteger(c) || c < 8 || c > 1024) throw RangeError("jointHulls maxPoints must be an integer from 8 to 1024");
60
+ if (a.length % 3 || a.length < 12) throw RangeError("jointHulls positions need xyz per vertex, at least four vertices");
61
+ let u = a.length / 3;
62
+ for (let e = 0; e < a.length; e++) if (!Number.isFinite(a[e])) throw RangeError(`jointHulls position ${e} is not finite; repair the mesh`);
63
+ let d = 65536;
64
+ if (l !== void 0) {
65
+ if (!l || l.length % 16 || l.length < 16) throw RangeError("jointHulls inverseBindMatrices need 16 numbers per joint");
66
+ for (let e = 0; e < l.length; e++) if (!Number.isFinite(l[e])) throw RangeError(`jointHulls inverseBindMatrices[${e}] is not finite; repair the skin`);
67
+ d = l.length / 16;
68
+ }
69
+ let f = /* @__PURE__ */ new Map(), p = (e, t) => {
70
+ if (e >= d) throw RangeError(`jointHulls joint ${e} has no inverse bind matrix; pass 16 numbers per joint`);
71
+ let n = f.get(e);
72
+ n || f.set(e, n = []), n.push(t);
73
+ };
74
+ if (o instanceof e) {
75
+ if (o.vertexCount !== u) throw RangeError(`jointHulls skin has ${o.vertexCount} vertices but positions have ${u}; pass one skin row per vertex`);
76
+ let e = o.boneCount;
77
+ for (let t = 0; t < u; t++) for (let n = 0; n < e; n++) {
78
+ let r = o.values[t * e + n];
79
+ if (!Number.isFinite(r)) throw RangeError(`jointHulls weight of vertex ${t} on bone ${n} is not finite; repaint or normalize the skin`);
80
+ r >= s && p(n, t);
81
+ }
82
+ } else {
83
+ let { joints: e, weights: t } = o, n = o.influences ?? 4;
84
+ if (!e || !t || !Number.isInteger(n) || n < 1 || e.length !== t.length) throw TypeError("jointHulls skin needs joints and weights arrays of one length, `influences` per vertex");
85
+ if (e.length !== u * n) throw RangeError(`jointHulls skin has ${e.length / n} vertices but positions have ${u}; check influences`);
86
+ for (let r = 0; r < u; r++) for (let i = 0; i < n; i++) {
87
+ let a = e[r * n + i], o = t[r * n + i];
88
+ if (!Number.isFinite(o)) throw RangeError(`jointHulls weight of vertex ${r} slot ${i} is not finite; repaint or normalize the skin`);
89
+ if (o <= 0) continue;
90
+ let c = o, l = !0;
91
+ for (let o = 0; o < n; o++) o !== i && e[r * n + o] === a && t[r * n + o] > 0 && (o < i && (l = !1), c += t[r * n + o]);
92
+ l && c >= s && p(a, r);
93
+ }
94
+ }
95
+ let m = [];
96
+ for (let e of [...f.keys()].sort((e, t) => e - t)) {
97
+ let n = f.get(e);
98
+ if (n.length < 4) continue;
99
+ let i = new Float64Array(n.length * 3);
100
+ n.forEach((e, t) => i.set([
101
+ a[e * 3],
102
+ a[e * 3 + 1],
103
+ a[e * 3 + 2]
104
+ ], t * 3));
105
+ let o;
106
+ try {
107
+ o = t(i).positions;
108
+ } catch (e) {
109
+ if (e instanceof RangeError && e.message.startsWith("convexGeometry")) continue;
110
+ throw e;
111
+ }
112
+ let s = /* @__PURE__ */ new Set(), u = [];
113
+ for (let e = 0; e < o.length; e += 3) {
114
+ let t = `${o[e]},${o[e + 1]},${o[e + 2]}`;
115
+ s.has(t) || (s.add(t), u.push(o[e], o[e + 1], o[e + 2]));
116
+ }
117
+ let d = Float32Array.from(u);
118
+ if (d.length / 3 > c && (d = r(d, c)), l) {
119
+ let t = l, n = e * 16;
120
+ for (let e = 0; e < d.length; e += 3) {
121
+ let r = d[e], i = d[e + 1], a = d[e + 2];
122
+ d[e] = t[n] * r + t[n + 4] * i + t[n + 8] * a + t[n + 12], d[e + 1] = t[n + 1] * r + t[n + 5] * i + t[n + 9] * a + t[n + 13], d[e + 2] = t[n + 2] * r + t[n + 6] * i + t[n + 10] * a + t[n + 14];
123
+ }
124
+ }
125
+ m.push({
126
+ joint: e,
127
+ vertexCount: n.length,
128
+ points: d
129
+ });
130
+ }
131
+ return m;
132
+ }
133
+ //#endregion
134
+ export { i as jointHulls };
package/dist/ik.d.ts ADDED
@@ -0,0 +1,162 @@
1
+ import type { Vec3 } from '@margoengine/math';
2
+ // @ts-ignore optional peer: its types apply when it is installed
3
+ import type { Animator } from '@margoengine/animator';
4
+ // @ts-ignore optional peer: its types apply when it is installed
5
+ import type { ElbowEvaluator } from '@margoengine/kinematics-constraints';
6
+ // @ts-ignore optional peer: its types apply when it is installed
7
+ import { type Anatomy } from '@margoengine/animator/ik';
8
+ // @ts-ignore optional peer: its types apply when it is installed
9
+ export type { Anatomy } from '@margoengine/animator/ik';
10
+ /** Settings shared by every FABRIK solve. */
11
+ export interface FABRIKOptions {
12
+ /** World point the chain bends toward (a knee or elbow hint); null or absent for none. */
13
+ pole?: Vec3 | null;
14
+ /** Most backward-and-forward iterations (1-1,024, default 32). */
15
+ iterations?: number;
16
+ /** Distance from the target that counts as reached, in the chain's units (default 1e-4). */
17
+ tolerance?: number;
18
+ }
19
+ /** What a FABRIK solve did. */
20
+ export interface FABRIKResult {
21
+ /** The end joint is within `tolerance` of the target. */
22
+ reached: boolean;
23
+ /** Distance from the end joint to the target after the solve. */
24
+ distance: number;
25
+ /** Iterations run. */
26
+ iterations: number;
27
+ /** On a fitted body: a joint stopped at its range and the end is short of the target. */
28
+ held?: boolean;
29
+ }
30
+ /**
31
+ * Moves joint positions (`points`, xyz per joint, at least two joints, the first fixed) so the last reaches `target`,
32
+ * keeping every bone's length; FABRIK (Aristidou and Lasenby 2011) with an optional pole. Unreachable targets
33
+ * straighten the chain toward them.
34
+ * @throws RangeError for fewer than two joints, a zero-length bone or non-finite input.
35
+ */
36
+ export declare function solveFABRIK(points: Float64Array | number[], target: Vec3, options?: FABRIKOptions): FABRIKResult;
37
+ /**
38
+ * The shortest-arc rotation (x, y, z, w) turning direction `from` to direction `to` (neither needs to be unit length).
39
+ * @throws RangeError for a zero direction.
40
+ */
41
+ export declare function rotationBetween(from: ArrayLike<number>, to: ArrayLike<number>): [number, number, number, number];
42
+ /** One joint chain of a pose: what {@link solvePoseIK} turns. */
43
+ export interface IKChain extends FABRIKOptions {
44
+ /** Joint indices from the first to the last, each the child of the one before (2-256 joints). */
45
+ joints: readonly number[];
46
+ /** World (model-space) point the last joint reaches for. */
47
+ target: Vec3;
48
+ /** Blend from the current pose (0) to the solved chain (1); default 1. */
49
+ weight?: number;
50
+ /**
51
+ * Keep each bent inner joint (an elbow or knee bent more than 2 degrees in the pose being solved) on its own side:
52
+ * when the pole would fold it the other way, it goes to the nearest allowed side instead, square to its bend
53
+ * (default true). A straight inner joint follows the pole as before. False lets the pole fold any joint either way.
54
+ */
55
+ keepBend?: boolean;
56
+ /**
57
+ * The fitted body whose joint ranges the chain obeys (an `AnatomicalRig` from `fitAnatomy`). Default: the body
58
+ * `setAnatomy` gave the pose, skeleton or Animator, else none.
59
+ */
60
+ body?: Anatomy | null;
61
+ /** False solves as if there were no body. Default true: a fitted body's limits are on. */
62
+ limits?: boolean;
63
+ /**
64
+ * On a fitted arm or leg: the share of the limb's length over which the reach eases in before full stretch, so the
65
+ * elbow or knee does not snap straight (0-0.5, default 0: the end meets every target in reach exactly).
66
+ */
67
+ soft?: number;
68
+ /**
69
+ * On a fitted arm (a shoulder, an elbow and a wrist): `natural` (default) turns the forearm to its anatomical zero
70
+ * and leaves the hand at the body's neutral, a relaxed hand whatever the pose held; `keep` keeps the pose's forearm
71
+ * twist and hand.
72
+ */
73
+ hand?: 'natural' | 'keep';
74
+ }
75
+ /**
76
+ * A pose to solve: parents and local transforms, 10 numbers per bone (translation xyz, rotation xyzw, scale xyz),
77
+ * the layout of `@margoengine/animation`'s `retargetRig(...).rest` and {@link solveSkeletonIK}'s skeletons.
78
+ */
79
+ export interface IKPose {
80
+ readonly parents: readonly number[];
81
+ readonly local: Float64Array;
82
+ /** The fitted body of these bones; its limits are on unless the chain says `limits: false`. */
83
+ readonly body?: Anatomy | null;
84
+ }
85
+ /**
86
+ * Solves an IK chain on a pose without an engine: turns the chain's joints (all but the last) so the last reaches
87
+ * `target`, bending toward `pole`. Writes the new rotations into `pose.local`. Bones need positive scales. An uneven
88
+ * scale stretches the bones under it as they turn, so the last joint lands as close as that stretch allows.
89
+ *
90
+ * When the pose has a fitted body (`chain.body`, `pose.body` or `setAnatomy(pose, body)`) with limits on, the chain
91
+ * obeys it: an elbow or knee bends only the anatomical way and stops at its range, even from a straight limb; the pole
92
+ * only picks the limb's swivel inside the shoulder's or hip's range; every other joint of the chain stays inside its
93
+ * range. A target the ranges do not allow is approached as closely as they allow, and `held` is true.
94
+ * `limits: false` on the chain solves as if there were no body.
95
+ * @throws RangeError for an invalid chain or pose.
96
+ */
97
+ export declare function solvePoseIK(pose: IKPose, chain: IKChain): FABRIKResult;
98
+ /** A skeleton {@link solveSkeletonIK} can pose: `@margoengine/animation`'s `Skeleton` fits. */
99
+ export interface IKSkeleton {
100
+ readonly parents: readonly number[];
101
+ readonly names: readonly string[];
102
+ getPose(bone: number): {
103
+ translation: ArrayLike<number>;
104
+ rotation: ArrayLike<number>;
105
+ scale: ArrayLike<number>;
106
+ };
107
+ setPose(bone: number, pose: {
108
+ rotation: readonly [number, number, number, number];
109
+ }): unknown;
110
+ }
111
+ /**
112
+ * Solves an IK chain on a `Skeleton` (`@margoengine/animation`): reads its current pose, turns the chain's joints so
113
+ * the last reaches `target` (in skeleton space) bending toward `pole`, and sets their rotations. Joints may be bone
114
+ * indices or names. A body given by `chain.body` or `setAnatomy(skeleton, body)` is obeyed as in {@link solvePoseIK}.
115
+ */
116
+ export declare function solveSkeletonIK(skeleton: IKSkeleton, chain: Omit<IKChain, 'joints'> & {
117
+ joints: readonly (number | string)[];
118
+ }): FABRIKResult;
119
+ /**
120
+ * An `ElbowEvaluator` for `@margoengine/kinematics-constraints`' `AvoidanceSolver`: the elbow of a two-bone arm of
121
+ * these lengths reaching from `root` to `target`, turned `poleAngle` radians about the root-target line from the side
122
+ * facing `pole` (a world point, or a function returning one each call). Unreachable targets put the elbow on the
123
+ * straight (or fully folded) arm.
124
+ */
125
+ export declare function elbowEvaluator(upperLength: number, lowerLength: number, pole: Vec3 | (() => Vec3)): ElbowEvaluator;
126
+ /**
127
+ * A FABRIK IK chain on an Animator's skeleton ({@link addIKChain}). The Animator keeps the object and reads it every
128
+ * frame, so move `target` and `pole` by writing into them.
129
+ */
130
+ export interface AnimatorIKChain extends FABRIKOptions {
131
+ /** glTF node indices from the first joint to the last (2-256), each the child of the one before. */
132
+ joints: readonly number[];
133
+ /** World point the last joint reaches for. */
134
+ target: Vec3;
135
+ /** Blend from the animated pose (0) to the solved chain (1); default 1. */
136
+ weight?: number;
137
+ /** Keep each bent elbow or knee on the side the animation bends it to, as {@link IKChain.keepBend}; default true. */
138
+ keepBend?: boolean;
139
+ /** The fitted body the chain obeys, as {@link IKChain.body}. Default: the body `setAnatomy` gave the Animator. */
140
+ body?: Anatomy | null;
141
+ /** False solves as if there were no body, as {@link IKChain.limits}. */
142
+ limits?: boolean;
143
+ /** Eases a fitted limb's reach in before full stretch, as {@link IKChain.soft}. */
144
+ soft?: number;
145
+ /** A fitted arm's forearm and hand, as {@link IKChain.hand}; default `natural`. */
146
+ hand?: 'natural' | 'keep';
147
+ /** Called after each frame's solve with its result. */
148
+ onSolve?: (result: FABRIKResult) => void;
149
+ }
150
+ /**
151
+ * Adds a FABRIK IK chain with an optional pole to `animator` (`@margoengine/animator`), solved every frame after the
152
+ * pose, additive layers and foot IK: the chain's joints turn so the last joint reaches `chain.target`, the bend facing
153
+ * `chain.pole`. The last joint keeps its animated local rotation. Joints and their ancestors need positive scales;
154
+ * rotations commit through the Animator's `writeRotation` when it has one. Returns a function that removes
155
+ * the chain.
156
+ *
157
+ * When the Animator has a fitted body (`chain.body`, or `setAnatomy(animator, body)`) with limits on, the chain obeys
158
+ * it as {@link solvePoseIK} does: elbows and knees bend the anatomical way inside their range, the pole only picks the
159
+ * swivel inside the shoulder's or hip's range, and every joint of the chain stays in range. `limits: false` on the
160
+ * chain turns that off.
161
+ */
162
+ export declare function addIKChain(animator: Animator, chain: AnimatorIKChain): () => void;