@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.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +969 -3
- package/dist/autorig.d.ts +50 -0
- package/dist/autorig.js +660 -0
- package/dist/bar-grip.d.ts +7 -0
- package/dist/checks.d.ts +50 -0
- package/dist/contact.d.ts +175 -0
- package/dist/contact.js +739 -0
- package/dist/correctives.d.ts +92 -0
- package/dist/correctives.js +162 -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 +109 -0
- package/dist/ik.js +358 -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 +71 -0
- package/dist/retarget.js +351 -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/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 +298 -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 +175 -6
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,109 @@
|
|
|
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
|
+
/** Settings shared by every FABRIK solve. */
|
|
7
|
+
export interface FABRIKOptions {
|
|
8
|
+
/** World point the chain bends toward (a knee or elbow hint); null or absent for none. */
|
|
9
|
+
pole?: Vec3 | null;
|
|
10
|
+
/** Most backward-and-forward iterations (1-1,024, default 32). */
|
|
11
|
+
iterations?: number;
|
|
12
|
+
/** Distance from the target that counts as reached, in the chain's units (default 1e-4). */
|
|
13
|
+
tolerance?: number;
|
|
14
|
+
}
|
|
15
|
+
/** What a FABRIK solve did. */
|
|
16
|
+
export interface FABRIKResult {
|
|
17
|
+
/** The end joint is within `tolerance` of the target. */
|
|
18
|
+
reached: boolean;
|
|
19
|
+
/** Distance from the end joint to the target after the solve. */
|
|
20
|
+
distance: number;
|
|
21
|
+
/** Iterations run. */
|
|
22
|
+
iterations: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Moves joint positions (`points`, xyz per joint, at least two joints, the first fixed) so the last reaches `target`,
|
|
26
|
+
* keeping every bone's length; FABRIK (Aristidou and Lasenby 2011) with an optional pole. Unreachable targets
|
|
27
|
+
* straighten the chain toward them.
|
|
28
|
+
* @throws RangeError for fewer than two joints, a zero-length bone or non-finite input.
|
|
29
|
+
*/
|
|
30
|
+
export declare function solveFABRIK(points: Float64Array | number[], target: Vec3, options?: FABRIKOptions): FABRIKResult;
|
|
31
|
+
/**
|
|
32
|
+
* The shortest-arc rotation (x, y, z, w) turning direction `from` to direction `to` (neither needs to be unit length).
|
|
33
|
+
* @throws RangeError for a zero direction.
|
|
34
|
+
*/
|
|
35
|
+
export declare function rotationBetween(from: ArrayLike<number>, to: ArrayLike<number>): [number, number, number, number];
|
|
36
|
+
/** One joint chain of a pose: what {@link solvePoseIK} turns. */
|
|
37
|
+
export interface IKChain extends FABRIKOptions {
|
|
38
|
+
/** Joint indices from the first to the last, each the child of the one before (2-256 joints). */
|
|
39
|
+
joints: readonly number[];
|
|
40
|
+
/** World (model-space) point the last joint reaches for. */
|
|
41
|
+
target: Vec3;
|
|
42
|
+
/** Blend from the current pose (0) to the solved chain (1); default 1. */
|
|
43
|
+
weight?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* A pose to solve: parents and local transforms, 10 numbers per bone (translation xyz, rotation xyzw, scale xyz),
|
|
47
|
+
* the layout of `@margoengine/animation`'s `retargetRig(...).rest` and {@link solveSkeletonIK}'s skeletons.
|
|
48
|
+
*/
|
|
49
|
+
export interface IKPose {
|
|
50
|
+
readonly parents: readonly number[];
|
|
51
|
+
readonly local: Float64Array;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Solves an IK chain on a pose without an engine: turns the chain's joints (all but the last) so the last reaches
|
|
55
|
+
* `target`, bending toward `pole`. Writes the new rotations into `pose.local`. Bones need positive scales. An uneven
|
|
56
|
+
* scale stretches the bones under it as they turn, so the last joint lands as close as that stretch allows.
|
|
57
|
+
* @throws RangeError for an invalid chain or pose.
|
|
58
|
+
*/
|
|
59
|
+
export declare function solvePoseIK(pose: IKPose, chain: IKChain): FABRIKResult;
|
|
60
|
+
/** A skeleton {@link solveSkeletonIK} can pose: `@margoengine/animation`'s `Skeleton` fits. */
|
|
61
|
+
export interface IKSkeleton {
|
|
62
|
+
readonly parents: readonly number[];
|
|
63
|
+
readonly names: readonly string[];
|
|
64
|
+
getPose(bone: number): {
|
|
65
|
+
translation: ArrayLike<number>;
|
|
66
|
+
rotation: ArrayLike<number>;
|
|
67
|
+
scale: ArrayLike<number>;
|
|
68
|
+
};
|
|
69
|
+
setPose(bone: number, pose: {
|
|
70
|
+
rotation: readonly [number, number, number, number];
|
|
71
|
+
}): unknown;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Solves an IK chain on a `Skeleton` (`@margoengine/animation`): reads its current pose, turns the chain's joints so
|
|
75
|
+
* the last reaches `target` (in skeleton space) bending toward `pole`, and sets their rotations. Joints may be bone
|
|
76
|
+
* indices or names.
|
|
77
|
+
*/
|
|
78
|
+
export declare function solveSkeletonIK(skeleton: IKSkeleton, chain: Omit<IKChain, 'joints'> & {
|
|
79
|
+
joints: readonly (number | string)[];
|
|
80
|
+
}): FABRIKResult;
|
|
81
|
+
/**
|
|
82
|
+
* An `ElbowEvaluator` for `@margoengine/kinematics-constraints`' `AvoidanceSolver`: the elbow of a two-bone arm of
|
|
83
|
+
* these lengths reaching from `root` to `target`, turned `poleAngle` radians about the root-target line from the side
|
|
84
|
+
* facing `pole` (a world point, or a function returning one each call). Unreachable targets put the elbow on the
|
|
85
|
+
* straight (or fully folded) arm.
|
|
86
|
+
*/
|
|
87
|
+
export declare function elbowEvaluator(upperLength: number, lowerLength: number, pole: Vec3 | (() => Vec3)): ElbowEvaluator;
|
|
88
|
+
/**
|
|
89
|
+
* A FABRIK IK chain on an Animator's skeleton ({@link addIKChain}). The Animator keeps the object and reads it every
|
|
90
|
+
* frame, so move `target` and `pole` by writing into them.
|
|
91
|
+
*/
|
|
92
|
+
export interface AnimatorIKChain extends FABRIKOptions {
|
|
93
|
+
/** glTF node indices from the first joint to the last (2-256), each the child of the one before. */
|
|
94
|
+
joints: readonly number[];
|
|
95
|
+
/** World point the last joint reaches for. */
|
|
96
|
+
target: Vec3;
|
|
97
|
+
/** Blend from the animated pose (0) to the solved chain (1); default 1. */
|
|
98
|
+
weight?: number;
|
|
99
|
+
/** Called after each frame's solve with its result. */
|
|
100
|
+
onSolve?: (result: FABRIKResult) => void;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Adds a FABRIK IK chain with an optional pole to `animator` (`@margoengine/animator`), solved every frame after the
|
|
104
|
+
* pose, additive layers and foot IK: the chain's joints turn so the last joint reaches `chain.target`, the bend facing
|
|
105
|
+
* `chain.pole`. The last joint keeps its animated local rotation. Joints and their ancestors need positive scales;
|
|
106
|
+
* rotations commit through the Animator's `writeRotation` when it has one. Returns a function that removes
|
|
107
|
+
* the chain. For joint limits, use `addCCDChain` from `@margoengine/animator`.
|
|
108
|
+
*/
|
|
109
|
+
export declare function addIKChain(animator: Animator, chain: AnimatorIKChain): () => void;
|
package/dist/ik.js
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import { r as e } from "./rig-BawZ5OLb.js";
|
|
2
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
3
|
+
import { multiplyQuaternions as n, normalizeQuaternion as r, rotateByQuaternion as i, rotationToQuaternion as a } from "@margoengine/math/quat";
|
|
4
|
+
//#region src/ik.ts
|
|
5
|
+
var o = (e = {}) => {
|
|
6
|
+
let t = e.iterations ?? 32, n = e.tolerance ?? 1e-4, r = e.pole ?? null;
|
|
7
|
+
if (!Number.isInteger(t) || t < 1 || t > 1024) throw RangeError("IK iterations must be an integer from 1 to 1,024");
|
|
8
|
+
if (!(n > 0 && n < Infinity)) throw RangeError("IK tolerance must be finite and above 0");
|
|
9
|
+
return r !== null && s(r, "IK pole"), {
|
|
10
|
+
iterations: t,
|
|
11
|
+
tolerance: n,
|
|
12
|
+
pole: r
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
function s(e, t) {
|
|
16
|
+
let n = e;
|
|
17
|
+
if (!n || n.length !== 3 || !Number.isFinite(n[0]) || !Number.isFinite(n[1]) || !Number.isFinite(n[2])) throw RangeError(`${t} must be three finite numbers`);
|
|
18
|
+
}
|
|
19
|
+
function c(e, t, n = {}) {
|
|
20
|
+
let r = e?.length / 3;
|
|
21
|
+
if (!Number.isInteger(r) || r < 2 || r > 256) throw RangeError("A FABRIK chain needs 2 to 256 joints, xyz each");
|
|
22
|
+
s(t, "IK target");
|
|
23
|
+
let { iterations: i, tolerance: a, pole: c } = o(n);
|
|
24
|
+
for (let t = 0; t < e.length; t++) if (!Number.isFinite(e[t])) throw RangeError(`IK joint coordinate ${t} is not finite; fix the pose`);
|
|
25
|
+
let u = new Float64Array(r - 1), d = 0;
|
|
26
|
+
for (let t = 0; t < r - 1; t++) {
|
|
27
|
+
if (u[t] = Math.hypot(e[t * 3 + 3] - e[t * 3], e[t * 3 + 4] - e[t * 3 + 1], e[t * 3 + 5] - e[t * 3 + 2]), !(u[t] > 0)) throw RangeError(`IK bone ${t} has zero length; separate its joints`);
|
|
28
|
+
d += u[t];
|
|
29
|
+
}
|
|
30
|
+
let f = e[0], p = e[1], m = e[2], h = t[0], g = t[1], _ = t[2], v = (r - 1) * 3, y = (t, n, r) => {
|
|
31
|
+
let i = e[n * 3] - e[t * 3], a = e[n * 3 + 1] - e[t * 3 + 1], o = e[n * 3 + 2] - e[t * 3 + 2], s = Math.hypot(i, a, o);
|
|
32
|
+
s > 1e-12 || (i = h - f, a = g - p, o = _ - m, s = Math.hypot(i, a, o), s > 1e-12 || (i = 0, a = 1, o = 0, s = 1)), e[n * 3] = e[t * 3] + i / s * r, e[n * 3 + 1] = e[t * 3 + 1] + a / s * r, e[n * 3 + 2] = e[t * 3 + 2] + o / s * r;
|
|
33
|
+
}, b = () => Math.hypot(e[v] - h, e[v + 1] - g, e[v + 2] - _);
|
|
34
|
+
if (Math.hypot(h - f, g - p, _ - m) >= d) {
|
|
35
|
+
let t = h - f, n = g - p, i = _ - m, o = Math.hypot(t, n, i) || 1, s = 0;
|
|
36
|
+
for (let a = 1; a < r; a++) s += u[a - 1], e[a * 3] = f + t / o * s, e[a * 3 + 1] = p + n / o * s, e[a * 3 + 2] = m + i / o * s;
|
|
37
|
+
let c = b();
|
|
38
|
+
return {
|
|
39
|
+
reached: c <= a,
|
|
40
|
+
distance: c,
|
|
41
|
+
iterations: 0
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
let x = 0;
|
|
45
|
+
for (let e = 1; e < r - 1; e++) u[e] > u[x] && (x = e);
|
|
46
|
+
let S = 2 * u[x] - d, C = Math.hypot(h - f, g - p, _ - m);
|
|
47
|
+
if (S > 0 && C < S) {
|
|
48
|
+
let t = h - f, n = g - p, i = _ - m, o = C;
|
|
49
|
+
o > 1e-12 || (t = e[v] - f, n = e[v + 1] - p, i = e[v + 2] - m, o = Math.hypot(t, n, i), o > 1e-12 || (t = 0, n = 1, i = 0, o = 1));
|
|
50
|
+
let s = 0;
|
|
51
|
+
for (let a = 1; a < r; a++) s += a - 1 === x ? u[a - 1] : -u[a - 1], e[a * 3] = f + t / o * s, e[a * 3 + 1] = p + n / o * s, e[a * 3 + 2] = m + i / o * s;
|
|
52
|
+
let c = b();
|
|
53
|
+
return {
|
|
54
|
+
reached: c <= a,
|
|
55
|
+
distance: c,
|
|
56
|
+
iterations: 0
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
let w = 0;
|
|
60
|
+
if (c && l(e, r, c), r > 2) {
|
|
61
|
+
let t = e[v] - f, n = e[v + 1] - p, i = e[v + 2] - m, a = Math.hypot(t, n, i), o = a > 0;
|
|
62
|
+
for (let s = 1; s < r - 1 && o; s++) {
|
|
63
|
+
let r = e[s * 3] - f, c = e[s * 3 + 1] - p, l = e[s * 3 + 2] - m;
|
|
64
|
+
o = Math.hypot(c * i - l * n, l * t - r * i, r * n - c * t) <= 1e-9 * a * a;
|
|
65
|
+
}
|
|
66
|
+
if (o) {
|
|
67
|
+
let i = Math.abs(t) <= Math.abs(n) ? [
|
|
68
|
+
1,
|
|
69
|
+
0,
|
|
70
|
+
0
|
|
71
|
+
] : [
|
|
72
|
+
0,
|
|
73
|
+
1,
|
|
74
|
+
0
|
|
75
|
+
];
|
|
76
|
+
l(e, r, [
|
|
77
|
+
f + i[0] * a,
|
|
78
|
+
p + i[1] * a,
|
|
79
|
+
m + i[2] * a
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (; w < i && b() > a; w++) {
|
|
84
|
+
e[v] = h, e[v + 1] = g, e[v + 2] = _;
|
|
85
|
+
for (let e = r - 2; e >= 0; e--) y(e + 1, e, u[e]);
|
|
86
|
+
e[0] = f, e[1] = p, e[2] = m;
|
|
87
|
+
for (let e = 1; e < r; e++) y(e - 1, e, u[e - 1]);
|
|
88
|
+
c && l(e, r, c);
|
|
89
|
+
}
|
|
90
|
+
let T = b();
|
|
91
|
+
return {
|
|
92
|
+
reached: T <= a,
|
|
93
|
+
distance: T,
|
|
94
|
+
iterations: w
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function l(e, t, n) {
|
|
98
|
+
for (let r = 1; r < t - 1; r++) {
|
|
99
|
+
let t = (r - 1) * 3, i = r * 3, a = (r + 1) * 3, o = e[a] - e[t], s = e[a + 1] - e[t + 1], c = e[a + 2] - e[t + 2], l = Math.hypot(o, s, c);
|
|
100
|
+
if (!(l > 0)) continue;
|
|
101
|
+
let u = o / l, d = s / l, f = c / l, p = e[i] - e[t], m = e[i + 1] - e[t + 1], h = e[i + 2] - e[t + 2], g = p * u + m * d + h * f, _ = n[0] - e[t], v = n[1] - e[t + 1], y = n[2] - e[t + 2], b = _ * u + v * d + y * f, x = [
|
|
102
|
+
p - u * g,
|
|
103
|
+
m - d * g,
|
|
104
|
+
h - f * g
|
|
105
|
+
], S = [
|
|
106
|
+
_ - u * b,
|
|
107
|
+
v - d * b,
|
|
108
|
+
y - f * b
|
|
109
|
+
], C = Math.hypot(x[0], x[1], x[2]), w = Math.hypot(S[0], S[1], S[2]);
|
|
110
|
+
if (!(w > 1e-12)) continue;
|
|
111
|
+
let T = Math.max(C, l * .001) / w;
|
|
112
|
+
e[i] = e[t] + u * g + S[0] * T, e[i + 1] = e[t + 1] + d * g + S[1] * T, e[i + 2] = e[t + 2] + f * g + S[2] * T;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
function u(e, t) {
|
|
116
|
+
let n = Math.hypot(e[0], e[1], e[2]), i = Math.hypot(t[0], t[1], t[2]);
|
|
117
|
+
if (!(n > 0) || !(i > 0)) throw RangeError("A rotation between directions needs two nonzero directions");
|
|
118
|
+
let a = e[0] / n, o = e[1] / n, s = e[2] / n, c = t[0] / i, l = t[1] / i, u = t[2] / i, d = a * c + o * l + s * u;
|
|
119
|
+
if (d < -1 + 1e-12) {
|
|
120
|
+
let e = +(Math.abs(a) < .9), t = 1 - e, n = o * 0 - s * t, r = s * e - a * 0, i = a * t - o * e, c = Math.hypot(n, r, i);
|
|
121
|
+
return [
|
|
122
|
+
n / c,
|
|
123
|
+
r / c,
|
|
124
|
+
i / c,
|
|
125
|
+
0
|
|
126
|
+
];
|
|
127
|
+
}
|
|
128
|
+
return r([
|
|
129
|
+
o * u - s * l,
|
|
130
|
+
s * c - a * u,
|
|
131
|
+
a * l - o * c,
|
|
132
|
+
1 + d
|
|
133
|
+
]);
|
|
134
|
+
}
|
|
135
|
+
function d(t, a) {
|
|
136
|
+
let { parents: s, local: l } = t ?? {}, u = s?.length;
|
|
137
|
+
if (!Array.isArray(s) || !(l instanceof Float64Array) || l.length !== u * 10) throw TypeError("solvePoseIK needs {parents, local} with 10 local numbers per bone");
|
|
138
|
+
s.forEach((e, t) => {
|
|
139
|
+
if (!Number.isInteger(e) || e < -1 || e >= u || e === t) throw RangeError(`IK pose bone ${t} parent ${e} is invalid; use -1 or another bone's index`);
|
|
140
|
+
}), e(s, "IK pose");
|
|
141
|
+
let d = f(a, u, (e, t) => s[e] === t), m = (e) => {
|
|
142
|
+
let t = [];
|
|
143
|
+
for (let n = e; n >= 0; n = s[n]) t.push(n);
|
|
144
|
+
let r = [
|
|
145
|
+
0,
|
|
146
|
+
0,
|
|
147
|
+
0
|
|
148
|
+
], a = [
|
|
149
|
+
0,
|
|
150
|
+
0,
|
|
151
|
+
0,
|
|
152
|
+
1
|
|
153
|
+
], o = [
|
|
154
|
+
[
|
|
155
|
+
1,
|
|
156
|
+
0,
|
|
157
|
+
0
|
|
158
|
+
],
|
|
159
|
+
[
|
|
160
|
+
0,
|
|
161
|
+
1,
|
|
162
|
+
0
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
0,
|
|
166
|
+
0,
|
|
167
|
+
1
|
|
168
|
+
]
|
|
169
|
+
], c = (e) => [
|
|
170
|
+
0,
|
|
171
|
+
1,
|
|
172
|
+
2
|
|
173
|
+
].map((t) => o[0][t] * e[0] + o[1][t] * e[1] + o[2][t] * e[2]);
|
|
174
|
+
for (let e = t.length - 1; e >= 0; e--) {
|
|
175
|
+
let s = t[e] * 10, u = [
|
|
176
|
+
l[s + 3],
|
|
177
|
+
l[s + 4],
|
|
178
|
+
l[s + 5],
|
|
179
|
+
l[s + 6]
|
|
180
|
+
];
|
|
181
|
+
if (!(l[s + 7] > 0 && l[s + 8] > 0 && l[s + 9] > 0)) throw RangeError(`IK bone ${t[e]} needs positive scales; remove its zero or mirrored scale`);
|
|
182
|
+
let d = c([
|
|
183
|
+
l[s],
|
|
184
|
+
l[s + 1],
|
|
185
|
+
l[s + 2]
|
|
186
|
+
]);
|
|
187
|
+
r = [
|
|
188
|
+
r[0] + d[0],
|
|
189
|
+
r[1] + d[1],
|
|
190
|
+
r[2] + d[2]
|
|
191
|
+
], a = n(a, u), o = [
|
|
192
|
+
0,
|
|
193
|
+
1,
|
|
194
|
+
2
|
|
195
|
+
].map((e) => c(i([
|
|
196
|
+
0,
|
|
197
|
+
1,
|
|
198
|
+
2
|
|
199
|
+
].map((t) => t === e ? l[s + 7 + e] : 0), u)));
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
position: r,
|
|
203
|
+
rotation: a
|
|
204
|
+
};
|
|
205
|
+
}, h = d.map(m), g = s[d[0]], _ = g >= 0 ? m(g).rotation : [
|
|
206
|
+
0,
|
|
207
|
+
0,
|
|
208
|
+
0,
|
|
209
|
+
1
|
|
210
|
+
], v = new Float64Array(d.length * 3);
|
|
211
|
+
h.forEach((e, t) => v.set(e.position, t * 3));
|
|
212
|
+
let y = v.slice(), b = c(v, a.target, a), x = p(y, v, h.map((e) => e.rotation), a.weight ?? 1);
|
|
213
|
+
for (let e = 0; e < d.length - 1; e++) {
|
|
214
|
+
let t = e === 0 ? _ : x[e - 1], i = d[e] * 10, a = r(n([
|
|
215
|
+
-t[0],
|
|
216
|
+
-t[1],
|
|
217
|
+
-t[2],
|
|
218
|
+
t[3]
|
|
219
|
+
], x[e]));
|
|
220
|
+
l.set(a, i + 3);
|
|
221
|
+
}
|
|
222
|
+
let S = m(d[d.length - 1]).position, C = a.target, w = Math.hypot(S[0] - C[0], S[1] - C[1], S[2] - C[2]);
|
|
223
|
+
return {
|
|
224
|
+
reached: w <= o(a).tolerance,
|
|
225
|
+
distance: w,
|
|
226
|
+
iterations: b.iterations
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function f(e, t, n) {
|
|
230
|
+
let r = e?.joints;
|
|
231
|
+
if (!Array.isArray(r) || r.length < 2 || r.length > 256) throw TypeError("An IK chain needs joints: 2 to 256 indices from the first joint to the last");
|
|
232
|
+
for (let e of r) if (!Number.isInteger(e) || e < 0 || e >= t) throw RangeError(`IK chain joint ${e} is not one of the ${t} bones; pass a valid index`);
|
|
233
|
+
for (let e = 1; e < r.length; e++) if (!n(r[e], r[e - 1])) throw RangeError(`IK chain joint ${r[e]} is not a child of ${r[e - 1]}; list a parent-child line`);
|
|
234
|
+
s(e.target, "IK target");
|
|
235
|
+
let i = e.weight ?? 1;
|
|
236
|
+
if (!(i >= 0 && i <= 1)) throw RangeError("IK weight must be from 0 to 1");
|
|
237
|
+
return r;
|
|
238
|
+
}
|
|
239
|
+
function p(e, t, i, a) {
|
|
240
|
+
let o = [];
|
|
241
|
+
for (let s = 0; s < i.length - 1; s++) {
|
|
242
|
+
let c = u([
|
|
243
|
+
0,
|
|
244
|
+
1,
|
|
245
|
+
2
|
|
246
|
+
].map((t) => e[s * 3 + 3 + t] - e[s * 3 + t]), [
|
|
247
|
+
0,
|
|
248
|
+
1,
|
|
249
|
+
2
|
|
250
|
+
].map((e) => t[s * 3 + 3 + e] - t[s * 3 + e]));
|
|
251
|
+
a < 1 && (c = m(c, a)), o.push(r(n(c, i[s])));
|
|
252
|
+
}
|
|
253
|
+
return o.push(i[i.length - 1]), o;
|
|
254
|
+
}
|
|
255
|
+
function m(e, t) {
|
|
256
|
+
let n = Math.min(1, Math.abs(e[3])), r = e[3] < 0 ? -1 : 1, i = 2 * Math.acos(n) * t, a = Math.sqrt(1 - n * n);
|
|
257
|
+
if (a < 1e-12) return [
|
|
258
|
+
0,
|
|
259
|
+
0,
|
|
260
|
+
0,
|
|
261
|
+
1
|
|
262
|
+
];
|
|
263
|
+
let o = Math.sin(i / 2) / a * r;
|
|
264
|
+
return [
|
|
265
|
+
e[0] * o,
|
|
266
|
+
e[1] * o,
|
|
267
|
+
e[2] * o,
|
|
268
|
+
Math.cos(i / 2)
|
|
269
|
+
];
|
|
270
|
+
}
|
|
271
|
+
var h = (e, t) => [
|
|
272
|
+
e[t],
|
|
273
|
+
e[t + 1],
|
|
274
|
+
e[t + 2],
|
|
275
|
+
e[t + 3]
|
|
276
|
+
];
|
|
277
|
+
function g(e, t) {
|
|
278
|
+
let { parents: n, names: r } = e ?? {};
|
|
279
|
+
if (!Array.isArray(n) || typeof e.getPose != "function" || typeof e.setPose != "function") throw TypeError("solveSkeletonIK needs a Skeleton from @margoengine/animation");
|
|
280
|
+
let i = new Float64Array(n.length * 10);
|
|
281
|
+
for (let t = 0; t < n.length; t++) {
|
|
282
|
+
let { translation: n, rotation: r, scale: a } = e.getPose(t);
|
|
283
|
+
i.set(n, t * 10), i.set(r, t * 10 + 3), i.set(a, t * 10 + 7);
|
|
284
|
+
}
|
|
285
|
+
let a = (t?.joints ?? []).map((e) => typeof e == "string" ? r.indexOf(e) : e), o = d({
|
|
286
|
+
parents: n,
|
|
287
|
+
local: i
|
|
288
|
+
}, {
|
|
289
|
+
...t,
|
|
290
|
+
joints: a
|
|
291
|
+
});
|
|
292
|
+
for (let t = 0; t < a.length - 1; t++) e.setPose(a[t], { rotation: h(i, a[t] * 10 + 3) });
|
|
293
|
+
return o;
|
|
294
|
+
}
|
|
295
|
+
function _(e, n, r) {
|
|
296
|
+
if (!(e > 0 && e < Infinity) || !(n > 0 && n < Infinity)) throw RangeError("Elbow evaluator bone lengths must be finite and above 0");
|
|
297
|
+
return typeof r != "function" && s(r, "Elbow pole"), (i, a, o, s) => {
|
|
298
|
+
let c = typeof r == "function" ? r() : r, l = a[0] - i[0], u = a[1] - i[1], d = a[2] - i[2], f = Math.hypot(l, u, d);
|
|
299
|
+
if (!(f > 0)) throw new t("state", "Elbow evaluator target is at the root; move the target");
|
|
300
|
+
let p = l / f, m = u / f, h = d / f, g = Math.min(Math.max(f, Math.abs(e - n)), e + n), _ = (g * g + e * e - n * n) / (2 * g), v = Math.sqrt(Math.max(0, e * e - _ * _)), y = c[0] - i[0], b = c[1] - i[1], x = c[2] - i[2], S = y * p + b * m + x * h;
|
|
301
|
+
y -= p * S, b -= m * S, x -= h * S;
|
|
302
|
+
let C = Math.hypot(y, b, x);
|
|
303
|
+
if (!(C > 1e-12)) {
|
|
304
|
+
let e = +(Math.abs(p) < .9), t = 1 - e;
|
|
305
|
+
y = -h * t, b = h * e, x = p * t - m * e, C = Math.hypot(y, b, x);
|
|
306
|
+
}
|
|
307
|
+
y /= C, b /= C, x /= C;
|
|
308
|
+
let w = Math.cos(o), T = Math.sin(o), E = m * x - h * b, D = h * y - p * x, O = p * b - m * y;
|
|
309
|
+
s[0] = i[0] + p * _ + (y * w + E * T) * v, s[1] = i[1] + m * _ + (b * w + D * T) * v, s[2] = i[2] + h * _ + (x * w + O * T) * v;
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function v(e, t) {
|
|
313
|
+
let i = e.animation.model.nodes, a = e.animation.model.root.engine, s = a.store, l = s.parents, u = f(t, i.length, (e, t) => l[i[e].id] === i[t].id);
|
|
314
|
+
if (o(t), t.onSolve !== void 0 && typeof t.onSolve != "function") throw TypeError("IK chain onSolve must be a function");
|
|
315
|
+
let d = u.map((e) => i[e]), m = new Float64Array(u.length * 3), h = new Float64Array(u.length * 3);
|
|
316
|
+
return e.addSolver(() => {
|
|
317
|
+
let i = t.weight ?? 1;
|
|
318
|
+
if (!(i >= 0 && i <= 1)) throw RangeError("IK weight must be from 0 to 1");
|
|
319
|
+
if (i === 0) return;
|
|
320
|
+
let o = d.map((e, t) => {
|
|
321
|
+
let n = e.matrix;
|
|
322
|
+
return m.set([
|
|
323
|
+
n[12],
|
|
324
|
+
n[13],
|
|
325
|
+
n[14]
|
|
326
|
+
], t * 3), y(n);
|
|
327
|
+
});
|
|
328
|
+
h.set(m);
|
|
329
|
+
let u = c(m, t.target, t), f = p(h, m, o, i), g = l[d[0].id], _ = g === -1 ? [
|
|
330
|
+
0,
|
|
331
|
+
0,
|
|
332
|
+
0,
|
|
333
|
+
1
|
|
334
|
+
] : y(a.meshes.get(g).matrix);
|
|
335
|
+
for (let t = 0; t < d.length - 1; t++) {
|
|
336
|
+
let i = t === 0 ? _ : f[t - 1];
|
|
337
|
+
e.writeNodeRotation(d[t], r(n([
|
|
338
|
+
-i[0],
|
|
339
|
+
-i[1],
|
|
340
|
+
-i[2],
|
|
341
|
+
i[3]
|
|
342
|
+
], f[t])));
|
|
343
|
+
}
|
|
344
|
+
s.tick(!1), t.onSolve?.(u);
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
function y(e) {
|
|
348
|
+
let t = Math.hypot(e[0], e[1], e[2]), n = Math.hypot(e[4], e[5], e[6]), i = Math.hypot(e[8], e[9], e[10]);
|
|
349
|
+
if (!(e[0] * (e[5] * e[10] - e[6] * e[9]) - e[4] * (e[1] * e[10] - e[2] * e[9]) + e[8] * (e[1] * e[6] - e[2] * e[5]) > 0)) throw RangeError("IK chain joints need positive scales; remove zero or mirrored joint scales");
|
|
350
|
+
return r(a(e, [
|
|
351
|
+
0,
|
|
352
|
+
0,
|
|
353
|
+
0,
|
|
354
|
+
0
|
|
355
|
+
], t, n, i));
|
|
356
|
+
}
|
|
357
|
+
//#endregion
|
|
358
|
+
export { v as addIKChain, _ as elbowEvaluator, u as rotationBetween, c as solveFABRIK, d as solvePoseIK, g as solveSkeletonIK };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { WeightMap, type VertexInfluences } from './weight-map.js';
|
|
2
|
+
export { boneHeatWeights, type BoneHeatOptions } from './heat.js';
|
|
3
|
+
export { WeightPainter, type BrushMode, type BrushFalloff, type BrushStroke, type WeightEdit, type WeightPainterOptions, } from './paint.js';
|
|
4
|
+
export { checkSkin, assertSkin, type SkinCheckInput, type SkinIssue, type SkinIssueCode, type SkinReport, } from './checks.js';
|
|
5
|
+
export { checkRig, rigJoints, type RigJoint, type RigPose } from './rig.js';
|
|
6
|
+
export type { WeightMesh } from './mesh.js';
|