@margoengine/rigging 0.1.0 → 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 -201
- package/NOTICE +4 -4
- package/README.md +1444 -969
- 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.js +137 -134
- package/dist/body-volumes-DTym0RVY.js +56 -0
- package/dist/body-volumes.d.ts +19 -0
- package/dist/contact.d.ts +12 -1
- package/dist/contact.js +197 -190
- package/dist/coupling.d.ts +57 -0
- package/dist/coupling.js +330 -0
- package/dist/face.js +2 -2
- package/dist/ik.d.ts +55 -2
- package/dist/ik.js +369 -166
- package/dist/mtoon.js +4 -4
- package/dist/pose-tracking.js +2 -2
- package/dist/retarget.d.ts +9 -2
- package/dist/retarget.js +53 -29
- package/dist/shared/gltf.d.ts +257 -257
- package/dist/shared/layers.d.ts +16 -16
- package/dist/validate.d.ts +188 -0
- package/dist/validate.js +440 -0
- package/dist/vrm-constraints.js +3 -3
- package/dist/vrm.d.ts +8 -3
- package/dist/vrm.js +2 -2
- package/dist/vrma.js +2 -2
- package/package.json +191 -175
|
@@ -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 };
|
package/dist/face.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @margo/core-built
|
|
1
|
+
// @margo/core-built 1.0.0
|
|
2
2
|
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
3
|
|
|
4
4
|
import { MargoError as e } from "@margoengine/math/errors";
|
|
@@ -108,7 +108,7 @@ function f(r, i = {}) {
|
|
|
108
108
|
let w = `face:${s.root.id}`, T;
|
|
109
109
|
if (f) T = f.addSolver(C);
|
|
110
110
|
else {
|
|
111
|
-
let e = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("
|
|
111
|
+
let e = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("1.0.0",import.meta.url),
|
|
112
112
|
name: w,
|
|
113
113
|
setup() {},
|
|
114
114
|
onPreUpdate: C,
|
package/dist/ik.d.ts
CHANGED
|
@@ -3,6 +3,10 @@ import type { Vec3 } from '@margoengine/math';
|
|
|
3
3
|
import type { Animator } from '@margoengine/animator';
|
|
4
4
|
// @ts-ignore optional peer: its types apply when it is installed
|
|
5
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';
|
|
6
10
|
/** Settings shared by every FABRIK solve. */
|
|
7
11
|
export interface FABRIKOptions {
|
|
8
12
|
/** World point the chain bends toward (a knee or elbow hint); null or absent for none. */
|
|
@@ -20,6 +24,8 @@ export interface FABRIKResult {
|
|
|
20
24
|
distance: number;
|
|
21
25
|
/** Iterations run. */
|
|
22
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;
|
|
23
29
|
}
|
|
24
30
|
/**
|
|
25
31
|
* Moves joint positions (`points`, xyz per joint, at least two joints, the first fixed) so the last reaches `target`,
|
|
@@ -41,6 +47,30 @@ export interface IKChain extends FABRIKOptions {
|
|
|
41
47
|
target: Vec3;
|
|
42
48
|
/** Blend from the current pose (0) to the solved chain (1); default 1. */
|
|
43
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';
|
|
44
74
|
}
|
|
45
75
|
/**
|
|
46
76
|
* A pose to solve: parents and local transforms, 10 numbers per bone (translation xyz, rotation xyzw, scale xyz),
|
|
@@ -49,11 +79,19 @@ export interface IKChain extends FABRIKOptions {
|
|
|
49
79
|
export interface IKPose {
|
|
50
80
|
readonly parents: readonly number[];
|
|
51
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;
|
|
52
84
|
}
|
|
53
85
|
/**
|
|
54
86
|
* Solves an IK chain on a pose without an engine: turns the chain's joints (all but the last) so the last reaches
|
|
55
87
|
* `target`, bending toward `pole`. Writes the new rotations into `pose.local`. Bones need positive scales. An uneven
|
|
56
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.
|
|
57
95
|
* @throws RangeError for an invalid chain or pose.
|
|
58
96
|
*/
|
|
59
97
|
export declare function solvePoseIK(pose: IKPose, chain: IKChain): FABRIKResult;
|
|
@@ -73,7 +111,7 @@ export interface IKSkeleton {
|
|
|
73
111
|
/**
|
|
74
112
|
* Solves an IK chain on a `Skeleton` (`@margoengine/animation`): reads its current pose, turns the chain's joints so
|
|
75
113
|
* the last reaches `target` (in skeleton space) bending toward `pole`, and sets their rotations. Joints may be bone
|
|
76
|
-
* indices or names.
|
|
114
|
+
* indices or names. A body given by `chain.body` or `setAnatomy(skeleton, body)` is obeyed as in {@link solvePoseIK}.
|
|
77
115
|
*/
|
|
78
116
|
export declare function solveSkeletonIK(skeleton: IKSkeleton, chain: Omit<IKChain, 'joints'> & {
|
|
79
117
|
joints: readonly (number | string)[];
|
|
@@ -96,6 +134,16 @@ export interface AnimatorIKChain extends FABRIKOptions {
|
|
|
96
134
|
target: Vec3;
|
|
97
135
|
/** Blend from the animated pose (0) to the solved chain (1); default 1. */
|
|
98
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';
|
|
99
147
|
/** Called after each frame's solve with its result. */
|
|
100
148
|
onSolve?: (result: FABRIKResult) => void;
|
|
101
149
|
}
|
|
@@ -104,6 +152,11 @@ export interface AnimatorIKChain extends FABRIKOptions {
|
|
|
104
152
|
* pose, additive layers and foot IK: the chain's joints turn so the last joint reaches `chain.target`, the bend facing
|
|
105
153
|
* `chain.pole`. The last joint keeps its animated local rotation. Joints and their ancestors need positive scales;
|
|
106
154
|
* rotations commit through the Animator's `writeRotation` when it has one. Returns a function that removes
|
|
107
|
-
* the chain.
|
|
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.
|
|
108
161
|
*/
|
|
109
162
|
export declare function addIKChain(animator: Animator, chain: AnimatorIKChain): () => void;
|