@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +1443 -2
- package/dist/anatomy-data-ansur2.d.ts +63 -0
- package/dist/anatomy-data-cdc.d.ts +16 -0
- package/dist/anatomy-data-deleva.d.ts +86 -0
- package/dist/anatomy-human.d.ts +67 -0
- package/dist/anatomy-human.js +2229 -0
- package/dist/anatomy.d.ts +638 -0
- package/dist/anatomy.js +1112 -0
- package/dist/autorig.d.ts +50 -0
- package/dist/autorig.js +663 -0
- package/dist/bar-grip.d.ts +7 -0
- package/dist/body-volumes-DTym0RVY.js +56 -0
- package/dist/body-volumes.d.ts +19 -0
- package/dist/checks.d.ts +50 -0
- package/dist/contact.d.ts +186 -0
- package/dist/contact.js +746 -0
- package/dist/correctives.d.ts +92 -0
- package/dist/correctives.js +162 -0
- package/dist/coupling.d.ts +57 -0
- package/dist/coupling.js +330 -0
- package/dist/errors-D8rc8kzg.js +7 -0
- package/dist/face.d.ts +93 -0
- package/dist/face.js +144 -0
- package/dist/hands.d.ts +209 -0
- package/dist/hands.js +510 -0
- package/dist/heat.d.ts +38 -0
- package/dist/hinged-contact.d.ts +7 -0
- package/dist/hulls.d.ts +54 -0
- package/dist/hulls.js +134 -0
- package/dist/ik.d.ts +162 -0
- package/dist/ik.js +561 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +390 -0
- package/dist/joint-limits.d.ts +44 -0
- package/dist/joint-limits.js +74 -0
- package/dist/limb-collision.d.ts +99 -0
- package/dist/loading-CZaYr1N7.js +67 -0
- package/dist/mesh-CyvLHNp0.js +142 -0
- package/dist/mesh.d.ts +47 -0
- package/dist/mtoon-data.d.ts +118 -0
- package/dist/mtoon-legacy.d.ts +17 -0
- package/dist/mtoon-shader.d.ts +4 -0
- package/dist/mtoon.d.ts +84 -0
- package/dist/mtoon.js +990 -0
- package/dist/paint.d.ts +82 -0
- package/dist/pose-tracking.d.ts +117 -0
- package/dist/pose-tracking.js +488 -0
- package/dist/retarget.d.ts +78 -0
- package/dist/retarget.js +375 -0
- package/dist/rig-BawZ5OLb.js +127 -0
- package/dist/rig.d.ts +65 -0
- package/dist/shared/gltf.d.ts +257 -0
- package/dist/shared/layers.d.ts +16 -0
- package/dist/springs.d.ts +180 -0
- package/dist/springs.js +458 -0
- package/dist/strand-springs.d.ts +46 -0
- package/dist/strand-springs.js +168 -0
- package/dist/swim.d.ts +55 -0
- package/dist/swim.js +167 -0
- package/dist/transfer.d.ts +41 -0
- package/dist/transfer.js +136 -0
- package/dist/validate.d.ts +188 -0
- package/dist/validate.js +440 -0
- package/dist/voxel-weights-48ok-Dax.js +255 -0
- package/dist/voxel-weights.d.ts +42 -0
- package/dist/voxel-weights.js +1 -0
- package/dist/voxels.d.ts +36 -0
- package/dist/vrm-constraints.d.ts +60 -0
- package/dist/vrm-constraints.js +261 -0
- package/dist/vrm-first-person.d.ts +115 -0
- package/dist/vrm-first-person.js +618 -0
- package/dist/vrm-presets-CU4V4yq6.js +47 -0
- package/dist/vrm-presets.d.ts +1 -0
- package/dist/vrm.d.ts +303 -0
- package/dist/vrm.js +574 -0
- package/dist/vrm0.d.ts +134 -0
- package/dist/vrm0.js +377 -0
- package/dist/vrma.d.ts +90 -0
- package/dist/vrma.js +315 -0
- package/dist/weight-map-D8cAONQQ.js +92 -0
- package/dist/weight-map.d.ts +58 -0
- package/package.json +189 -4
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
2
|
+
import type { Animator } from '@margoengine/animator';
|
|
3
|
+
/** A morph target of a model: glTF node with the mesh, and target index. */
|
|
4
|
+
export interface ShapeTarget {
|
|
5
|
+
/** glTF node index of the mesh. */
|
|
6
|
+
node: number;
|
|
7
|
+
/** Morph target index. */
|
|
8
|
+
target: number;
|
|
9
|
+
}
|
|
10
|
+
/** A shape driven by one joint's rotation toward one pose: elbows and knees. */
|
|
11
|
+
export interface CorrectiveShape extends ShapeTarget {
|
|
12
|
+
/** The driving bone (glTF node index with {@link addCorrectives}). */
|
|
13
|
+
joint: number;
|
|
14
|
+
/** The joint's local rotation at which the shape is full, quaternion xyzw. */
|
|
15
|
+
pose: readonly number[];
|
|
16
|
+
/** The joint's local rotation at which the shape is off. Default its rest rotation, or identity with no rig. */
|
|
17
|
+
rest?: readonly number[];
|
|
18
|
+
/**
|
|
19
|
+
* How far from the line from `rest` to `pose` the joint can go, radians, before the shape fades out. Default none:
|
|
20
|
+
* only the bend along that line counts, so a hinge's twist does nothing.
|
|
21
|
+
*/
|
|
22
|
+
cone?: number | null;
|
|
23
|
+
/** Reading at which the shape starts, 0 to below 1 (default 0): 0.5 keeps it off for the first half of the bend. */
|
|
24
|
+
start?: number;
|
|
25
|
+
/** `smooth` eases in and out, `linear` follows the angle (default). */
|
|
26
|
+
curve?: 'linear' | 'smooth';
|
|
27
|
+
/** Weight at full bend, 0 to 1 (default 1). */
|
|
28
|
+
amount?: number;
|
|
29
|
+
}
|
|
30
|
+
/** A pose of a {@link PoseSpace}. */
|
|
31
|
+
export interface PoseSample {
|
|
32
|
+
/** The joint's local rotation, quaternion xyzw. */
|
|
33
|
+
rotation: readonly number[];
|
|
34
|
+
/** Weight of each of the space's `shapes`, by position, at this pose. */
|
|
35
|
+
weights: readonly number[];
|
|
36
|
+
}
|
|
37
|
+
/** Several shapes driven together by a joint that swings every way: shoulders and hips. */
|
|
38
|
+
export interface PoseSpace {
|
|
39
|
+
/** The driving bone. */
|
|
40
|
+
joint: number;
|
|
41
|
+
/** The shapes the samples mix. */
|
|
42
|
+
shapes: readonly ShapeTarget[];
|
|
43
|
+
/** Sculpted poses, 1 to 64. Include the rest pose with zero weights, or the shapes hold outside the samples. */
|
|
44
|
+
samples: readonly PoseSample[];
|
|
45
|
+
/** Gaussian width, radians. Default the mean angle from each sample to its nearest neighbour. */
|
|
46
|
+
width?: number;
|
|
47
|
+
}
|
|
48
|
+
/** What {@link Correctives} drives. */
|
|
49
|
+
export interface CorrectivesOptions {
|
|
50
|
+
shapes?: readonly CorrectiveShape[];
|
|
51
|
+
spaces?: readonly PoseSpace[];
|
|
52
|
+
}
|
|
53
|
+
/** One morph weight. */
|
|
54
|
+
export interface ShapeWeight extends ShapeTarget {
|
|
55
|
+
weight: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Shape weights from joint rotations, as plain data: no engine or GPU. {@link addCorrectives} runs it on a model.
|
|
59
|
+
* Shapes are read every call, so settings change live; pose spaces are solved once, here.
|
|
60
|
+
*/
|
|
61
|
+
export declare class Correctives {
|
|
62
|
+
readonly options: CorrectivesOptions;
|
|
63
|
+
private readonly spaces;
|
|
64
|
+
/**
|
|
65
|
+
* @param options what to drive.
|
|
66
|
+
* @throws RangeError for bad poses, samples or settings.
|
|
67
|
+
*/
|
|
68
|
+
constructor(options: CorrectivesOptions);
|
|
69
|
+
private check;
|
|
70
|
+
/** How far `rotation` has gone from `shape.rest` to `shape.pose`, with the shape's settings: the shape's weight. */
|
|
71
|
+
private reading;
|
|
72
|
+
/**
|
|
73
|
+
* Every driven shape's weight. Shapes on one target add up, then clamp to 0 to 1.
|
|
74
|
+
* @param rotationOf the current local rotation of a joint.
|
|
75
|
+
* @param restOf a joint's rest rotation, for shapes with no `rest`. Default identity.
|
|
76
|
+
*/
|
|
77
|
+
evaluate(rotationOf: (joint: number) => readonly number[], restOf?: (joint: number) => readonly number[]): ShapeWeight[];
|
|
78
|
+
}
|
|
79
|
+
/** A running {@link addCorrectives}. */
|
|
80
|
+
export interface CorrectivesHandle {
|
|
81
|
+
readonly correctives: Correctives;
|
|
82
|
+
/** Stops driving and sets the shapes to 0. */
|
|
83
|
+
dispose(): void;
|
|
84
|
+
[Symbol.dispose](): void;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Drives corrective shapes on `animator` (`@margoengine/animator`), every frame after the pose and the solvers added
|
|
88
|
+
* before it, so IK and foot placement count. Joints and mesh nodes are glTF node indices of the animator's model. A
|
|
89
|
+
* driven morph target belongs to this: a clip's track on the same target is overwritten. Returns the handle.
|
|
90
|
+
* @throws RangeError for bad settings or a target the model lacks.
|
|
91
|
+
*/
|
|
92
|
+
export declare function addCorrectives(animator: Animator, options: CorrectivesOptions): CorrectivesHandle;
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { multiplyQuaternions as e, normalizeQuaternion as t } from "@margoengine/math/quat";
|
|
2
|
+
//#region src/correctives.ts
|
|
3
|
+
var n = [
|
|
4
|
+
0,
|
|
5
|
+
0,
|
|
6
|
+
0,
|
|
7
|
+
1
|
|
8
|
+
], r = (e) => [
|
|
9
|
+
-e[0],
|
|
10
|
+
-e[1],
|
|
11
|
+
-e[2],
|
|
12
|
+
e[3]
|
|
13
|
+
];
|
|
14
|
+
function i(e, t) {
|
|
15
|
+
let n = e;
|
|
16
|
+
if (!n || n.length !== 4) throw RangeError(`${t} must be a quaternion: four numbers, x y z w`);
|
|
17
|
+
for (let e = 0; e < 4; e++) if (!Number.isFinite(n[e])) throw RangeError(`${t} must be finite`);
|
|
18
|
+
if (!(Math.hypot(n[0], n[1], n[2], n[3]) > 0)) throw RangeError(`${t} must not be all zeros`);
|
|
19
|
+
}
|
|
20
|
+
function a(e) {
|
|
21
|
+
let t = e[3] < 0 ? -1 : 1, n = Math.hypot(e[0], e[1], e[2]), r = 2 * Math.atan2(n, e[3] * t);
|
|
22
|
+
if (n < 1e-12) return [
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
0
|
|
26
|
+
];
|
|
27
|
+
let i = t * r / n;
|
|
28
|
+
return [
|
|
29
|
+
e[0] * i,
|
|
30
|
+
e[1] * i,
|
|
31
|
+
e[2] * i
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
var o = (e, t) => 2 * Math.acos(Math.min(1, Math.abs(e[0] * t[0] + e[1] * t[1] + e[2] * t[2] + e[3] * t[3])));
|
|
35
|
+
function s(e, t, n, r) {
|
|
36
|
+
for (let i = 0; i < n; i++) {
|
|
37
|
+
let a = i;
|
|
38
|
+
for (let t = i + 1; t < n; t++) Math.abs(e[t * n + i]) > Math.abs(e[a * n + i]) && (a = t);
|
|
39
|
+
if (Math.abs(e[a * n + i]) < 1e-12) throw RangeError("Pose samples repeat; give each sample a different rotation");
|
|
40
|
+
if (a !== i) {
|
|
41
|
+
for (let t = 0; t < n; t++) [e[i * n + t], e[a * n + t]] = [e[a * n + t], e[i * n + t]];
|
|
42
|
+
for (let e = 0; e < r; e++) [t[i * r + e], t[a * r + e]] = [t[a * r + e], t[i * r + e]];
|
|
43
|
+
}
|
|
44
|
+
for (let a = 0; a < n; a++) {
|
|
45
|
+
if (a === i) continue;
|
|
46
|
+
let o = e[a * n + i] / e[i * n + i];
|
|
47
|
+
if (o !== 0) {
|
|
48
|
+
for (let t = i; t < n; t++) e[a * n + t] -= o * e[i * n + t];
|
|
49
|
+
for (let e = 0; e < r; e++) t[a * r + e] -= o * t[i * r + e];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (let i = 0; i < n; i++) for (let a = 0; a < r; a++) t[i * r + a] /= e[i * n + i];
|
|
54
|
+
}
|
|
55
|
+
function c(e) {
|
|
56
|
+
let { samples: n, shapes: r } = e, a = n?.length, c = r?.length;
|
|
57
|
+
if (!Array.isArray(n) || a < 1 || a > 64) throw RangeError("A pose space needs 1 to 64 samples");
|
|
58
|
+
if (!Array.isArray(r) || !c) throw RangeError("A pose space needs at least one shape");
|
|
59
|
+
let l = n.map((e, n) => {
|
|
60
|
+
if (i(e?.rotation, `Pose sample ${n} rotation`), e.weights?.length !== c || !e.weights.every(Number.isFinite)) throw RangeError(`Pose sample ${n} needs one finite weight per shape`);
|
|
61
|
+
return t([...e.rotation]);
|
|
62
|
+
}), u = e.width;
|
|
63
|
+
if (u === void 0) {
|
|
64
|
+
let e = 0;
|
|
65
|
+
l.forEach((t, n) => {
|
|
66
|
+
let r = Infinity;
|
|
67
|
+
l.forEach((e, i) => {
|
|
68
|
+
n !== i && (r = Math.min(r, o(t, e)));
|
|
69
|
+
}), e += r === Infinity ? 1 : r;
|
|
70
|
+
}), u = e / a;
|
|
71
|
+
}
|
|
72
|
+
if (!(u > 0 && u < Infinity)) throw RangeError("Pose space width must be above 0; separate the samples");
|
|
73
|
+
let d = new Float64Array(a * a), f = new Float64Array(a * c);
|
|
74
|
+
for (let e = 0; e < a; e++) {
|
|
75
|
+
for (let t = 0; t < a; t++) d[e * a + t] = Math.exp(-((o(l[e], l[t]) / u) ** 2));
|
|
76
|
+
for (let t = 0; t < c; t++) f[e * c + t] = n[e].weights[t];
|
|
77
|
+
}
|
|
78
|
+
return s(d, f, a, c), {
|
|
79
|
+
spec: e,
|
|
80
|
+
rotations: l,
|
|
81
|
+
width: u,
|
|
82
|
+
coefficients: f
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
var l = (e) => e < 0 ? 0 : e > 1 ? 1 : e, u = class {
|
|
86
|
+
constructor(e) {
|
|
87
|
+
this.options = e, this.spaces = (e?.spaces ?? []).map(c), this.check();
|
|
88
|
+
}
|
|
89
|
+
check() {
|
|
90
|
+
for (let e of this.options?.shapes ?? []) {
|
|
91
|
+
i(e?.pose, "Corrective pose"), e.rest !== void 0 && i(e.rest, "Corrective rest");
|
|
92
|
+
let { cone: t = null, start: n = 0, curve: r = "linear", amount: a = 1 } = e;
|
|
93
|
+
if (t !== null && !(t > 0 && t < Infinity)) throw RangeError("Corrective cone must be above 0");
|
|
94
|
+
if (!(n >= 0 && n < 1)) throw RangeError("Corrective start must be from 0 to below 1");
|
|
95
|
+
if (r !== "linear" && r !== "smooth") throw RangeError("Corrective curve must be 'linear' or 'smooth'");
|
|
96
|
+
if (!(a >= 0 && a <= 1)) throw RangeError("Corrective amount must be from 0 to 1");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
reading(n, i, o) {
|
|
100
|
+
let s = r(n.rest ?? o), c = a(t(e(s, n.pose))), u = a(t(e(s, i))), d = c[0] ** 2 + c[1] ** 2 + c[2] ** 2;
|
|
101
|
+
if (!(d > 0)) throw RangeError("A corrective pose must differ from its rest; turn the joint");
|
|
102
|
+
let f = (u[0] * c[0] + u[1] * c[1] + u[2] * c[2]) / d, { start: p = 0, curve: m = "linear", amount: h = 1, cone: g = null } = n, _ = l((f - p) / (1 - p));
|
|
103
|
+
if (m === "smooth" && (_ = _ * _ * (3 - 2 * _)), g !== null) {
|
|
104
|
+
let e = l(1 - Math.hypot(u[0] - c[0] * f, u[1] - c[1] * f, u[2] - c[2] * f) / g);
|
|
105
|
+
_ *= e * e * (3 - 2 * e);
|
|
106
|
+
}
|
|
107
|
+
return _ * h;
|
|
108
|
+
}
|
|
109
|
+
evaluate(e, r = () => n) {
|
|
110
|
+
this.check();
|
|
111
|
+
let i = /* @__PURE__ */ new Map(), a = (e, t) => {
|
|
112
|
+
let n = `${e.node}:${e.target}`, r = i.get(n) ?? {
|
|
113
|
+
node: e.node,
|
|
114
|
+
target: e.target,
|
|
115
|
+
weight: 0
|
|
116
|
+
};
|
|
117
|
+
i.set(n, r), r.weight += t;
|
|
118
|
+
};
|
|
119
|
+
for (let t of this.options.shapes ?? []) a(t, this.reading(t, e(t.joint), r(t.joint)));
|
|
120
|
+
for (let { spec: n, rotations: r, width: i, coefficients: s } of this.spaces) {
|
|
121
|
+
let c = t([...e(n.joint)]), l = n.shapes.length, u = new Float64Array(l);
|
|
122
|
+
r.forEach((e, t) => {
|
|
123
|
+
let n = Math.exp(-((o(c, e) / i) ** 2));
|
|
124
|
+
for (let e = 0; e < l; e++) u[e] += n * s[t * l + e];
|
|
125
|
+
}), n.shapes.forEach((e, t) => a(e, u[t]));
|
|
126
|
+
}
|
|
127
|
+
for (let e of i.values()) e.weight = l(e.weight);
|
|
128
|
+
return [...i.values()];
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
function d(e, t) {
|
|
132
|
+
let n = new u(t), r = e.animation, i = r.model.nodes, a = r.model.root.engine.store.transforms, o = (e) => {
|
|
133
|
+
let t = i[e];
|
|
134
|
+
if (!t) throw RangeError(`Corrective joint ${e} is not a node of this model; pass a glTF node index`);
|
|
135
|
+
let n = t.id;
|
|
136
|
+
return [
|
|
137
|
+
a.qx[n],
|
|
138
|
+
a.qy[n],
|
|
139
|
+
a.qz[n],
|
|
140
|
+
a.qw[n]
|
|
141
|
+
];
|
|
142
|
+
}, s = (t) => e.restRotation(t), c = n.evaluate(o, s);
|
|
143
|
+
for (let { node: e, target: t } of c) r.setMorph(e, t, 0);
|
|
144
|
+
let l = e.addSolver(() => {
|
|
145
|
+
c = n.evaluate(o, s);
|
|
146
|
+
for (let { node: e, target: t, weight: n } of c) r.setMorph(e, t, n);
|
|
147
|
+
}), d = () => {
|
|
148
|
+
l?.(), l = null, f?.(), f = null;
|
|
149
|
+
}, f = r.model.onDispose(d), p = () => {
|
|
150
|
+
if (l) {
|
|
151
|
+
d();
|
|
152
|
+
for (let { node: e, target: t } of c) r.setMorph(e, t, 0);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
correctives: n,
|
|
157
|
+
dispose: p,
|
|
158
|
+
[Symbol.dispose]: p
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
//#endregion
|
|
162
|
+
export { u as Correctives, d as addCorrectives };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
2
|
+
import type { Animator } from '@margoengine/animator';
|
|
3
|
+
import { type AnatomicalRig } from './anatomy.js';
|
|
4
|
+
/** Which rules {@link couplePose} applies. Each defaults to true when the species has its coupling. */
|
|
5
|
+
export interface CouplingOptions {
|
|
6
|
+
/** The shoulder blade and collarbone follow arm elevation (`shoulder-girdle`). */
|
|
7
|
+
readonly girdle?: boolean;
|
|
8
|
+
/** Twist bones carry their share of a limb segment's twist (`limb-twist`). */
|
|
9
|
+
readonly twist?: boolean;
|
|
10
|
+
/** The last joint of a finger follows the middle one (`finger-curl`). */
|
|
11
|
+
readonly fingers?: boolean;
|
|
12
|
+
/** A bend held by one level of the trunk or neck is shared by every level (`spine-share`). */
|
|
13
|
+
readonly spine?: boolean;
|
|
14
|
+
/** The tibia turns inward as a straight knee bends and outward as it locks (`screw-home`). */
|
|
15
|
+
readonly knee?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** What {@link couplePose} drove: one entry per follower. */
|
|
18
|
+
export interface CouplingIssue {
|
|
19
|
+
/** The coupling's id in the species table. */
|
|
20
|
+
readonly rule: string;
|
|
21
|
+
/** The joint or bone that followed. */
|
|
22
|
+
readonly follower: string;
|
|
23
|
+
/** The joint that drove it. */
|
|
24
|
+
readonly driver: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Moves the joints a real body moves together with another, in place: the shoulder blade and collarbone with arm
|
|
28
|
+
* elevation (the arm keeps its direction against the thorax), twist bones with their limb's twist, the last finger
|
|
29
|
+
* joint with the middle one, the levels of the trunk and neck with a bend one level holds (the top of the run keeps
|
|
30
|
+
* its turn), and the tibia with knee extension. A follower the pose moves itself is left as it is, so the rest pose and
|
|
31
|
+
* a clip that animates the follower are unchanged. Run it before `clampPose`, which then holds the result in range.
|
|
32
|
+
* @example
|
|
33
|
+
* const pose = poseFromAngles(body, { l_shoulder: { abduction: 170 } });
|
|
34
|
+
* couplePose(body, pose); // the left collarbone rises and draws back, the shoulder blade turns up
|
|
35
|
+
* @param pose A local pose, 10 numbers per bone, edited in place.
|
|
36
|
+
* @param issues An array that receives one entry per follower driven.
|
|
37
|
+
* @returns How many followers were driven.
|
|
38
|
+
* @throws TypeError for a pose that is not a Float64Array of the right length.
|
|
39
|
+
*/
|
|
40
|
+
export declare function couplePose(body: AnatomicalRig, pose: Float64Array, options?: CouplingOptions, issues?: CouplingIssue[]): number;
|
|
41
|
+
/**
|
|
42
|
+
* The twist bones {@link couplePose} drives, with the joint each follows and its share of that joint's twist (1 at the
|
|
43
|
+
* far end of the segment, negative for a bone that undoes the segment's own twist toward its root).
|
|
44
|
+
*/
|
|
45
|
+
export declare function twistBones(body: AnatomicalRig): {
|
|
46
|
+
bone: string;
|
|
47
|
+
driver: string;
|
|
48
|
+
share: number;
|
|
49
|
+
}[];
|
|
50
|
+
/**
|
|
51
|
+
* Couples an Animator's pose every frame (see {@link couplePose}), after the clips and the solvers added before it.
|
|
52
|
+
* The body must be fitted to the Animator's own model, bone for bone. Returns the remover.
|
|
53
|
+
* @example
|
|
54
|
+
* const stop = addCoupling(animator, body);
|
|
55
|
+
* @throws RangeError when the body and the Animator's model differ in bone count.
|
|
56
|
+
*/
|
|
57
|
+
export declare function addCoupling(animator: Animator, body: AnatomicalRig, options?: CouplingOptions): () => void;
|
package/dist/coupling.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { r as e, s as t } from "./rig-BawZ5OLb.js";
|
|
2
|
+
import { poseFromAngles as n, readJointAngles as r } from "./anatomy.js";
|
|
3
|
+
//#region src/coupling.ts
|
|
4
|
+
var i = (e, t) => [
|
|
5
|
+
e[3] * t[0] + e[0] * t[3] + e[1] * t[2] - e[2] * t[1],
|
|
6
|
+
e[3] * t[1] - e[0] * t[2] + e[1] * t[3] + e[2] * t[0],
|
|
7
|
+
e[3] * t[2] + e[0] * t[1] - e[1] * t[0] + e[2] * t[3],
|
|
8
|
+
e[3] * t[3] - e[0] * t[0] - e[1] * t[1] - e[2] * t[2]
|
|
9
|
+
], a = (e) => [
|
|
10
|
+
-e[0],
|
|
11
|
+
-e[1],
|
|
12
|
+
-e[2],
|
|
13
|
+
e[3]
|
|
14
|
+
], o = (e, t) => {
|
|
15
|
+
let n = i(i(e, [
|
|
16
|
+
t[0],
|
|
17
|
+
t[1],
|
|
18
|
+
t[2],
|
|
19
|
+
0
|
|
20
|
+
]), a(e));
|
|
21
|
+
return [
|
|
22
|
+
n[0],
|
|
23
|
+
n[1],
|
|
24
|
+
n[2]
|
|
25
|
+
];
|
|
26
|
+
}, s = (e, t) => {
|
|
27
|
+
let n = Math.sin(t / 2);
|
|
28
|
+
return [
|
|
29
|
+
e[0] * n,
|
|
30
|
+
e[1] * n,
|
|
31
|
+
e[2] * n,
|
|
32
|
+
Math.cos(t / 2)
|
|
33
|
+
];
|
|
34
|
+
}, c = (e) => {
|
|
35
|
+
let t = Math.hypot(e[0], e[1], e[2]);
|
|
36
|
+
return t > 1e-12 ? [
|
|
37
|
+
e[0] / t,
|
|
38
|
+
e[1] / t,
|
|
39
|
+
e[2] / t
|
|
40
|
+
] : null;
|
|
41
|
+
}, l = (e, t) => {
|
|
42
|
+
let n = e[0] * t[0] + e[1] * t[1] + e[2] * t[2], r = 2 * Math.atan2(n, e[3]);
|
|
43
|
+
return r > Math.PI && (r -= 2 * Math.PI), r < -Math.PI && (r += 2 * Math.PI), r;
|
|
44
|
+
}, u = (e, t) => [
|
|
45
|
+
e[t * 10 + 3],
|
|
46
|
+
e[t * 10 + 4],
|
|
47
|
+
e[t * 10 + 5],
|
|
48
|
+
e[t * 10 + 6]
|
|
49
|
+
], d = (e, t, n) => {
|
|
50
|
+
let r = n * 10 + 3, i = e[r] * t[r] + e[r + 1] * t[r + 1] + e[r + 2] * t[r + 2] + e[r + 3] * t[r + 3];
|
|
51
|
+
return Math.abs(i) > 1 - 1e-9;
|
|
52
|
+
};
|
|
53
|
+
function f(e, t, n) {
|
|
54
|
+
if (t <= e[0][0]) return e[0][n];
|
|
55
|
+
for (let r = 1; r < e.length; r++) if (t <= e[r][0]) {
|
|
56
|
+
let i = e[r - 1], a = e[r];
|
|
57
|
+
return i[n] + (t - i[0]) / (a[0] - i[0]) * (a[n] - i[n]);
|
|
58
|
+
}
|
|
59
|
+
return e.at(-1)[n];
|
|
60
|
+
}
|
|
61
|
+
var p = (e, t, n) => Math.min(1, Math.max(0, (e - t) / (n - t))), m = /* @__PURE__ */ new WeakMap();
|
|
62
|
+
function h(e, t) {
|
|
63
|
+
let n = e.species.joints.find((e) => e.id === t), r = [];
|
|
64
|
+
for (let i of [t, n?.mirror]) {
|
|
65
|
+
if (!i) continue;
|
|
66
|
+
let t = e.index.get(i);
|
|
67
|
+
t !== void 0 && e.joints[t].id === i && e.joints[t].limited && r.push(t);
|
|
68
|
+
}
|
|
69
|
+
return r;
|
|
70
|
+
}
|
|
71
|
+
var g = (e, t) => e.species.joints.find((e) => e.id === t)?.mirror ?? t;
|
|
72
|
+
function _(n) {
|
|
73
|
+
let r = m.get(n);
|
|
74
|
+
if (r) return r;
|
|
75
|
+
let s = new Map(n.species.couplings.map((e) => [e.id, e])), l = n.joints, u = n.names.length, d = e(n.parents, "Rig"), f = t(n.parents, n.rest, d), p = (e) => [
|
|
76
|
+
f.position[e * 3],
|
|
77
|
+
f.position[e * 3 + 1],
|
|
78
|
+
f.position[e * 3 + 2]
|
|
79
|
+
], _ = (e) => [
|
|
80
|
+
f.rotation[e * 4],
|
|
81
|
+
f.rotation[e * 4 + 1],
|
|
82
|
+
f.rotation[e * 4 + 2],
|
|
83
|
+
f.rotation[e * 4 + 3]
|
|
84
|
+
], v = [], y = s.get("shoulder-girdle");
|
|
85
|
+
if (y && y.joints.length >= 2) for (let e of h(n, y.joints[0])) {
|
|
86
|
+
let t = l[e].id === y.joints[0] ? (e) => e : (e) => g(n, e), r = n.index.get(t(y.joints[1])), i = y.joints[2] === void 0 ? void 0 : n.index.get(t(y.joints[2]));
|
|
87
|
+
if (r === void 0 || l[r].id !== t(y.joints[1]) || !l[r].limited) continue;
|
|
88
|
+
let a = l[r].spec.rhythm?.curve;
|
|
89
|
+
a && v.push({
|
|
90
|
+
shoulder: e,
|
|
91
|
+
clavicle: r,
|
|
92
|
+
scapula: i !== void 0 && l[i].id === t(y.joints[2]) && l[i].limited ? i : -1,
|
|
93
|
+
curve: a,
|
|
94
|
+
values: y.values
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
let b = [], x = s.get("limb-twist"), S = new Set(l.map((e) => e.bone));
|
|
98
|
+
if (x) for (let e of x.joints) for (let t of h(n, e)) {
|
|
99
|
+
let e = l[t], r = e.bone, s = l.find((e) => e.reference === r && e.limited && n.parents[e.bone] >= 0);
|
|
100
|
+
if (!s) continue;
|
|
101
|
+
let d = p(r), f = [
|
|
102
|
+
p(s.bone)[0] - d[0],
|
|
103
|
+
p(s.bone)[1] - d[1],
|
|
104
|
+
p(s.bone)[2] - d[2]
|
|
105
|
+
], m = f[0] * f[0] + f[1] * f[1] + f[2] * f[2], h = c(f);
|
|
106
|
+
if (!h || !(m > 0)) continue;
|
|
107
|
+
let g = !!s.spec.dofs?.[2].spill;
|
|
108
|
+
for (let t = 0; t < u; t++) {
|
|
109
|
+
if (S.has(t)) continue;
|
|
110
|
+
let c = n.parents[t];
|
|
111
|
+
for (; c >= 0 && c !== r && !S.has(c);) c = n.parents[c];
|
|
112
|
+
if (c !== r) continue;
|
|
113
|
+
let l = [
|
|
114
|
+
p(t)[0] - d[0],
|
|
115
|
+
p(t)[1] - d[1],
|
|
116
|
+
p(t)[2] - d[2]
|
|
117
|
+
], u = (l[0] * f[0] + l[1] * f[1] + l[2] * f[2]) / m, v = [
|
|
118
|
+
l[0] - f[0] * u,
|
|
119
|
+
l[1] - f[1] * u,
|
|
120
|
+
l[2] - f[2] * u
|
|
121
|
+
];
|
|
122
|
+
if (!(u > .02 && u < .98) || v[0] * v[0] + v[1] * v[1] + v[2] * v[2] > .0225 * m) continue;
|
|
123
|
+
let y = g, C = y ? s.bone : r, w = y ? r : n.parents[r];
|
|
124
|
+
if (w < 0) continue;
|
|
125
|
+
let T = i(a(_(w)), _(C)), E = o(a(_(C)), h), D = o(a(_(t)), h);
|
|
126
|
+
b.push({
|
|
127
|
+
bone: t,
|
|
128
|
+
name: n.names[t],
|
|
129
|
+
driver: y ? s.id : e.id,
|
|
130
|
+
turned: C,
|
|
131
|
+
against: w,
|
|
132
|
+
rest: T,
|
|
133
|
+
axis: E,
|
|
134
|
+
own: D,
|
|
135
|
+
share: (y ? u : u - 1) * (x.values.spread ?? 1)
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
let C = [], w = s.get("finger-curl");
|
|
140
|
+
if (w) for (let e = 0; e + 1 < w.joints.length; e += 2) for (let t of h(n, w.joints[e])) {
|
|
141
|
+
let r = l[t].id === w.joints[e] ? w.joints[e + 1] : g(n, w.joints[e + 1]), i = n.index.get(r);
|
|
142
|
+
i !== void 0 && l[i].id === r && l[i].limited && C.push([
|
|
143
|
+
t,
|
|
144
|
+
i,
|
|
145
|
+
w.values.ratio ?? 2 / 3
|
|
146
|
+
]);
|
|
147
|
+
}
|
|
148
|
+
let T = [], E = s.get("screw-home");
|
|
149
|
+
if (E) for (let e of E.joints) for (let t of h(n, e)) T.push({
|
|
150
|
+
joint: t,
|
|
151
|
+
rotation: E.values.rotation ?? 0,
|
|
152
|
+
over: E.values.over ?? 20
|
|
153
|
+
});
|
|
154
|
+
let D = {
|
|
155
|
+
girdles: v,
|
|
156
|
+
twists: b,
|
|
157
|
+
fingers: C,
|
|
158
|
+
knees: T,
|
|
159
|
+
spines: s.has("spine-share") ? n.regions.filter((e) => e.joints.length >= 2).map((e) => e.joints) : [],
|
|
160
|
+
hooks: s,
|
|
161
|
+
angles: new Float64Array(l.length * 3),
|
|
162
|
+
base: new Float64Array(l.length * 3)
|
|
163
|
+
};
|
|
164
|
+
return m.set(n, D), D;
|
|
165
|
+
}
|
|
166
|
+
var v = (e, t, n, r = -1) => {
|
|
167
|
+
let i = d(t, e.rest, n), a = d(t, e.neutral, n);
|
|
168
|
+
return i && a ? r >= 0 && d(t, e.rest, r) ? "rest" : "neutral" : i ? "rest" : a ? "neutral" : null;
|
|
169
|
+
}, y = (e, t) => t === "rest" ? [
|
|
170
|
+
e.rest.flexion,
|
|
171
|
+
e.rest.abduction,
|
|
172
|
+
e.rest.rotation
|
|
173
|
+
] : [
|
|
174
|
+
0,
|
|
175
|
+
0,
|
|
176
|
+
0
|
|
177
|
+
];
|
|
178
|
+
function b(o, c, m = {}, h) {
|
|
179
|
+
if (!(c instanceof Float64Array) || c.length !== o.names.length * 10) throw TypeError("couplePose needs a local pose as a Float64Array with 10 numbers per bone");
|
|
180
|
+
let g = _(o), b = o.joints, x = r(o, c, g.angles), S = 0, C = (e, t, n) => {
|
|
181
|
+
S++, h?.push({
|
|
182
|
+
rule: e,
|
|
183
|
+
follower: t,
|
|
184
|
+
driver: n
|
|
185
|
+
});
|
|
186
|
+
}, w = {}, T = (e, t) => w[b[e].id] = {
|
|
187
|
+
flexion: t[0],
|
|
188
|
+
abduction: t[1],
|
|
189
|
+
rotation: t[2]
|
|
190
|
+
};
|
|
191
|
+
if (m.girdle !== !1) for (let e of g.girdles) {
|
|
192
|
+
let t = b[e.clavicle], n = v(o, c, t.bone, b[e.shoulder].bone);
|
|
193
|
+
if (!n) continue;
|
|
194
|
+
let r = ((e, t) => Math.hypot(t[e * 3], t[e * 3 + 1]))(e.shoulder, x), i = b[e.shoulder], a = n === "rest" ? Math.hypot(i.rest.flexion, i.rest.abduction) : 0;
|
|
195
|
+
if (Math.abs(r - a) < 1e-6) continue;
|
|
196
|
+
let s = e.values, l = (e) => (s.clavicleRoll ?? 0) * p(e, s.clavicleRollFrom ?? 90, s.clavicleRollTo ?? 180), u = y(t, n);
|
|
197
|
+
if (T(e.clavicle, [
|
|
198
|
+
u[0] + f(e.curve, r, 1) - f(e.curve, a, 1),
|
|
199
|
+
u[1] - (f(e.curve, r, 2) - f(e.curve, a, 2)),
|
|
200
|
+
u[2] + l(r) - l(a)
|
|
201
|
+
]), C("shoulder-girdle", t.id, i.id), e.scapula >= 0) {
|
|
202
|
+
let t = b[e.scapula], n = v(o, c, t.bone, b[e.shoulder].bone);
|
|
203
|
+
if (n) {
|
|
204
|
+
let o = (e) => p(e, s.scapulaFrom ?? 30, s.scapulaTo ?? 120), c = o(r) - o(a), l = y(t, n);
|
|
205
|
+
T(e.scapula, [
|
|
206
|
+
l[0] + (s.scapulaUpward ?? 0) * c,
|
|
207
|
+
l[1] + (s.scapulaInternal ?? 0) * c,
|
|
208
|
+
l[2] + (s.scapulaTilt ?? 0) * c
|
|
209
|
+
]), C("shoulder-girdle", t.id, i.id);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
T(e.shoulder, [
|
|
213
|
+
x[e.shoulder * 3],
|
|
214
|
+
x[e.shoulder * 3 + 1],
|
|
215
|
+
x[e.shoulder * 3 + 2]
|
|
216
|
+
]);
|
|
217
|
+
}
|
|
218
|
+
if (m.fingers !== !1) for (let [e, t, n] of g.fingers) {
|
|
219
|
+
let r = b[t], i = v(o, c, r.bone, b[e].bone);
|
|
220
|
+
if (!i) continue;
|
|
221
|
+
let a = b[e], s = y(a, i)[0], l = x[e * 3] - s;
|
|
222
|
+
if (Math.abs(l) < 1e-6) continue;
|
|
223
|
+
let u = y(r, i);
|
|
224
|
+
T(t, [
|
|
225
|
+
u[0] + n * l,
|
|
226
|
+
u[1],
|
|
227
|
+
u[2]
|
|
228
|
+
]), C("finger-curl", r.id, a.id);
|
|
229
|
+
}
|
|
230
|
+
if (m.knee !== !1) for (let { joint: e, rotation: t, over: n } of g.knees) {
|
|
231
|
+
let r = b[e], i = [
|
|
232
|
+
r.rest.flexion,
|
|
233
|
+
r.rest.abduction,
|
|
234
|
+
r.rest.rotation
|
|
235
|
+
], a = x[e * 3 + 2], o = Math.abs(a - i[2]) < .001 ? "rest" : Math.abs(a) < .001 ? "neutral" : null;
|
|
236
|
+
if (!o) continue;
|
|
237
|
+
let s = o === "rest" ? i : [
|
|
238
|
+
0,
|
|
239
|
+
0,
|
|
240
|
+
0
|
|
241
|
+
], c = t * (p(x[e * 3], 0, n) - p(s[0], 0, n));
|
|
242
|
+
Math.abs(c) < 1e-6 || (T(e, [
|
|
243
|
+
x[e * 3],
|
|
244
|
+
x[e * 3 + 1],
|
|
245
|
+
s[2] + c
|
|
246
|
+
]), C("screw-home", r.id, r.id));
|
|
247
|
+
}
|
|
248
|
+
let E = Object.keys(w).length ? n(o, w, c) : c;
|
|
249
|
+
if (m.spine !== !1) for (let s of g.spines) {
|
|
250
|
+
let c = s.filter((e) => !v(o, E, b[e].bone));
|
|
251
|
+
if (c.length !== 1) continue;
|
|
252
|
+
let l = s.map((e) => v(o, E, b[e].bone) ?? "rest"), u = r(o, E, g.base), d = [
|
|
253
|
+
0,
|
|
254
|
+
1,
|
|
255
|
+
2
|
|
256
|
+
].map((e) => s.reduce((t, n) => t + b[n].normal[e * 2 + 1] - b[n].normal[e * 2], 0)), f = [
|
|
257
|
+
0,
|
|
258
|
+
1,
|
|
259
|
+
2
|
|
260
|
+
].map((e) => s.reduce((t, n, r) => t + u[n * 3 + e] - y(b[n], l[r])[e], 0));
|
|
261
|
+
if (Math.hypot(...f) < 1e-6) continue;
|
|
262
|
+
let p = e(o.parents, "Rig"), m = b[s.at(-1)], h = t(o.parents, E, p).rotation.slice(m.bone * 4, m.bone * 4 + 4), _ = {};
|
|
263
|
+
s.forEach((e, t) => {
|
|
264
|
+
if (e === s.at(-1)) return;
|
|
265
|
+
let n = y(b[e], l[t]), r = (t) => d[t] > 0 ? (b[e].normal[t * 2 + 1] - b[e].normal[t * 2]) / d[t] : 0;
|
|
266
|
+
_[b[e].id] = {
|
|
267
|
+
flexion: n[0] + r(0) * f[0],
|
|
268
|
+
abduction: n[1] + r(1) * f[1],
|
|
269
|
+
rotation: n[2] + r(2) * f[2]
|
|
270
|
+
}, C("spine-share", b[e].id, b[c[0]].id);
|
|
271
|
+
}), E = n(o, _, E);
|
|
272
|
+
let x = o.parents[m.bone], S = x < 0 ? [
|
|
273
|
+
0,
|
|
274
|
+
0,
|
|
275
|
+
0,
|
|
276
|
+
1
|
|
277
|
+
] : t(o.parents, E, p).rotation.slice(x * 4, x * 4 + 4);
|
|
278
|
+
E.set(i(a(S), h), m.bone * 10 + 3), m !== b[c[0]] && C("spine-share", m.id, b[c[0]].id);
|
|
279
|
+
}
|
|
280
|
+
if (m.twist !== !1 && g.twists.length) {
|
|
281
|
+
let n = t(o.parents, E, e(o.parents, "Rig")).rotation, r = (e) => [
|
|
282
|
+
n[e * 4],
|
|
283
|
+
n[e * 4 + 1],
|
|
284
|
+
n[e * 4 + 2],
|
|
285
|
+
n[e * 4 + 3]
|
|
286
|
+
];
|
|
287
|
+
for (let e of g.twists) {
|
|
288
|
+
if (!d(E, o.rest, e.bone)) continue;
|
|
289
|
+
let t = l(i(a(e.rest), i(a(r(e.against)), r(e.turned))), e.axis) * e.share;
|
|
290
|
+
Math.abs(t) < 1e-7 || (E.set(i(u(E, e.bone), s(e.own, t)), e.bone * 10 + 3), C("limb-twist", e.name, e.driver));
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return E !== c && c.set(E), S;
|
|
294
|
+
}
|
|
295
|
+
function x(e) {
|
|
296
|
+
return _(e).twists.map((e) => ({
|
|
297
|
+
bone: e.name,
|
|
298
|
+
driver: e.driver,
|
|
299
|
+
share: e.share
|
|
300
|
+
}));
|
|
301
|
+
}
|
|
302
|
+
function S(e, t, n = {}) {
|
|
303
|
+
_(t);
|
|
304
|
+
let r = e.animation.model.nodes, i = r.length;
|
|
305
|
+
if (i !== t.names.length) throw RangeError(`addCoupling: the body has ${t.names.length} bones and the Animator's model ${i}; fit the body to that model`);
|
|
306
|
+
let a = e.animation.model.root.engine.store, o = a.transforms, s = t.rest.slice(), c = new Float64Array(i * 4), l = [
|
|
307
|
+
0,
|
|
308
|
+
0,
|
|
309
|
+
0,
|
|
310
|
+
1
|
|
311
|
+
];
|
|
312
|
+
return e.addSolver(() => {
|
|
313
|
+
for (let e = 0; e < i; e++) {
|
|
314
|
+
let { id: t } = r[e], n = e * 10 + 3;
|
|
315
|
+
c[e * 4] = s[n] = o.qx[t], c[e * 4 + 1] = s[n + 1] = o.qy[t], c[e * 4 + 2] = s[n + 2] = o.qz[t], c[e * 4 + 3] = s[n + 3] = o.qw[t];
|
|
316
|
+
}
|
|
317
|
+
if (!b(t, s, n)) return;
|
|
318
|
+
let u = !1;
|
|
319
|
+
for (let t = 0; t < i; t++) {
|
|
320
|
+
let n = t * 10 + 3;
|
|
321
|
+
if (s[n] !== c[t * 4] || s[n + 1] !== c[t * 4 + 1] || s[n + 2] !== c[t * 4 + 2] || s[n + 3] !== c[t * 4 + 3]) {
|
|
322
|
+
for (let e = 0; e < 4; e++) l[e] = s[n + e];
|
|
323
|
+
e.writeNodeRotation(r[t], l), u = !0;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
u && a.tick(!1);
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
export { S as addCoupling, b as couplePose, x as twistBones };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region ../shared/errors.js
|
|
2
|
+
var e = (e, t) => Object.assign(Error(t), {
|
|
3
|
+
name: "MargoError",
|
|
4
|
+
code: e
|
|
5
|
+
}), t = (e, t) => `${e}; see https://margoengine.com/errors#${t}`, n = (n, r, i = n) => e(n, t(r, i)), r = (e, n) => TypeError(t(e, n)), i = (e, n) => RangeError(t(e, n));
|
|
6
|
+
//#endregion
|
|
7
|
+
export { r as i, e as n, i as r, n as t };
|