@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
package/dist/swim.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { RigPose } from './rig.js';
|
|
2
|
+
/** The supplied swimming profiles, including a fin-driven ray. */
|
|
3
|
+
export type SwimSpecies = 'reef-fish' | 'reef-shark' | 'bottlenose-dolphin' | 'humpback-whale' | 'bluespotted-ray';
|
|
4
|
+
/** Scale and joint placement for a swimming skeleton. */
|
|
5
|
+
export interface SwimRigOptions {
|
|
6
|
+
/** Head to caudal-fin or fluke hinge in metres. */
|
|
7
|
+
readonly length: number;
|
|
8
|
+
/** Number of articulated spine segments. The tail hinge is one extra joint. Default 24. */
|
|
9
|
+
readonly segments?: number;
|
|
10
|
+
/** Optional nonuniform hinge positions as fractions of length, starting at 0 and ending at 1. */
|
|
11
|
+
readonly stations?: readonly number[];
|
|
12
|
+
/** Paired pectoral hinges, in head-relative metres. The closest spine station owns each hinge. */
|
|
13
|
+
readonly pectorals?: readonly [left: SwimFin, right: SwimFin];
|
|
14
|
+
}
|
|
15
|
+
/** Hinge locations and rotation range for a pectoral fin. */
|
|
16
|
+
export interface SwimFin {
|
|
17
|
+
/** Proximal hinge in head-relative metres. */
|
|
18
|
+
readonly position: readonly [number, number, number];
|
|
19
|
+
/** Available fin roll in radians. Default 0.22. */
|
|
20
|
+
readonly roll?: number;
|
|
21
|
+
/** Optional distal hinge in head-relative metres, for a travelling wave along a flexible fin. */
|
|
22
|
+
readonly distal?: readonly [number, number, number];
|
|
23
|
+
}
|
|
24
|
+
/** A sampled swimmer's topology and local bind pose. */
|
|
25
|
+
export interface SwimRig extends RigPose {
|
|
26
|
+
/** Profile used when sampling the chain. */
|
|
27
|
+
readonly species: SwimSpecies;
|
|
28
|
+
/** Head to caudal hinge, in metres. */
|
|
29
|
+
readonly length: number;
|
|
30
|
+
/** Fractional spine locations in head-to-tail order. */
|
|
31
|
+
readonly stations: readonly number[];
|
|
32
|
+
/** Optional calibrated paired fins. */
|
|
33
|
+
readonly pectorals?: readonly [left: SwimFin, right: SwimFin];
|
|
34
|
+
/** Local bind translations, quaternion rotations and scales, ten floats per joint. */
|
|
35
|
+
readonly rest: Float32Array;
|
|
36
|
+
}
|
|
37
|
+
/** Animated stroke controls at one sample of the swimmer's path. */
|
|
38
|
+
export interface SwimStroke {
|
|
39
|
+
/** Accumulated tail beats, including the fractional beat. Drive this continuously across speed changes. */
|
|
40
|
+
readonly phase: number;
|
|
41
|
+
/** Stroke amplitude. Fade to zero to coast. Default 1, range 0–1. */
|
|
42
|
+
readonly effort?: number;
|
|
43
|
+
/** Lateral body bend, left negative and right positive. Default 0, range −1 to 1. */
|
|
44
|
+
readonly turn?: number;
|
|
45
|
+
}
|
|
46
|
+
/** Create a Y-up, +Z-forward chain. The head is at the origin and the tail extends along −Z. */
|
|
47
|
+
export declare function createSwimRig(species: SwimSpecies, options: SwimRigOptions): SwimRig;
|
|
48
|
+
/** Estimate steady cruising cadence in beats/second using profile Strouhal number and peak-to-peak excursion. */
|
|
49
|
+
export declare function swimCadence(rig: SwimRig, speed: number, effort?: number): number;
|
|
50
|
+
/**
|
|
51
|
+
* Evaluate local translation/rotation/scale frames. The output may be reused and must have the
|
|
52
|
+
* same size as rig.rest. Each span follows an analytic body-wave tangent at its midpoint, keeping
|
|
53
|
+
* the underlying segment length constant. The last joint feathers the caudal fin or flukes.
|
|
54
|
+
*/
|
|
55
|
+
export declare function sampleSwimPose(rig: SwimRig, stroke: SwimStroke, out?: Float32Array<ArrayBuffer>): Float32Array;
|
package/dist/swim.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
//#region src/swim.ts
|
|
2
|
+
var e = {
|
|
3
|
+
"reef-fish": {
|
|
4
|
+
vertical: !1,
|
|
5
|
+
onset: .24,
|
|
6
|
+
envelope: 2,
|
|
7
|
+
wavelength: .94,
|
|
8
|
+
excursion: .095,
|
|
9
|
+
feather: .25,
|
|
10
|
+
strouhal: .29,
|
|
11
|
+
turn: .52
|
|
12
|
+
},
|
|
13
|
+
"reef-shark": {
|
|
14
|
+
vertical: !1,
|
|
15
|
+
onset: .16,
|
|
16
|
+
envelope: 1.6,
|
|
17
|
+
wavelength: 1.04,
|
|
18
|
+
excursion: .078,
|
|
19
|
+
feather: .16,
|
|
20
|
+
strouhal: .29,
|
|
21
|
+
turn: .48
|
|
22
|
+
},
|
|
23
|
+
"bottlenose-dolphin": {
|
|
24
|
+
vertical: !0,
|
|
25
|
+
onset: .46,
|
|
26
|
+
envelope: 2.1,
|
|
27
|
+
wavelength: 1.27,
|
|
28
|
+
excursion: .088,
|
|
29
|
+
feather: .33,
|
|
30
|
+
strouhal: .27,
|
|
31
|
+
turn: .49
|
|
32
|
+
},
|
|
33
|
+
"humpback-whale": {
|
|
34
|
+
vertical: !0,
|
|
35
|
+
onset: .48,
|
|
36
|
+
envelope: 2.45,
|
|
37
|
+
wavelength: 1.52,
|
|
38
|
+
excursion: .085,
|
|
39
|
+
feather: .4,
|
|
40
|
+
strouhal: .25,
|
|
41
|
+
turn: .38
|
|
42
|
+
},
|
|
43
|
+
"bluespotted-ray": {
|
|
44
|
+
vertical: !1,
|
|
45
|
+
onset: .78,
|
|
46
|
+
envelope: 2.6,
|
|
47
|
+
wavelength: 1.41,
|
|
48
|
+
excursion: .012,
|
|
49
|
+
feather: .04,
|
|
50
|
+
strouhal: .25,
|
|
51
|
+
turn: .16
|
|
52
|
+
}
|
|
53
|
+
}, t = 2 * Math.PI;
|
|
54
|
+
function n(t, n) {
|
|
55
|
+
if (!Object.hasOwn(e, t)) throw RangeError("Unknown swimming species profile; pass a supplied SwimSpecies");
|
|
56
|
+
let { length: i, segments: a = 24 } = n;
|
|
57
|
+
if (!Number.isFinite(i) || i < .001 || i > 1e3) throw RangeError("Swimmer length must be in the range 0.001–1000 metres");
|
|
58
|
+
if (!Number.isInteger(a) || a < 4 || a >= 65536) throw RangeError("A swimmer needs 4 to 65,535 spine segments");
|
|
59
|
+
let o = n.stations ? Array.from(n.stations) : Array.from({ length: a + 1 }, (e, t) => t / a);
|
|
60
|
+
if (o.length < 5 || o.length > 65536 || o[0] !== 0 || o.at(-1) !== 1 || o.some((e, t) => !Number.isFinite(e) || t > 0 && e - o[t - 1] < 1e-8)) throw RangeError("Swimmer stations need 5 to 65,536 increasing fractions from 0 to 1");
|
|
61
|
+
let s = (n.pectorals ?? (t === "bluespotted-ray" ? r(i) : void 0))?.map(({ position: e, roll: t = .22, distal: n }) => {
|
|
62
|
+
let r = (e) => e.length === 3 && e.every(Number.isFinite) && e[2] <= 0 && e[2] >= -i;
|
|
63
|
+
if (!r(e) || n && !r(n) || !Number.isFinite(t) || t < 0 || t > Math.PI / 2) throw RangeError("Pectoral hinge needs a finite XYZ position along the body and roll from 0 to π/2");
|
|
64
|
+
return {
|
|
65
|
+
position: [...e],
|
|
66
|
+
roll: t,
|
|
67
|
+
...n && { distal: [...n] }
|
|
68
|
+
};
|
|
69
|
+
}), c = s?.reduce((e, t) => e + (t.distal ? 2 : 1), 0) ?? 0;
|
|
70
|
+
if (s && s.length !== 2 || o.length + c > 65536) throw RangeError("Pectorals need a left/right pair with available joint indices");
|
|
71
|
+
let l = new Float32Array((o.length + c) * 10), u = o.length - 1;
|
|
72
|
+
for (let e = 0; e <= u; e++) {
|
|
73
|
+
let t = e > 0 ? (o[e] - o[e - 1]) * i : 0;
|
|
74
|
+
l.set([
|
|
75
|
+
0,
|
|
76
|
+
0,
|
|
77
|
+
-t,
|
|
78
|
+
0,
|
|
79
|
+
0,
|
|
80
|
+
0,
|
|
81
|
+
1,
|
|
82
|
+
1,
|
|
83
|
+
1,
|
|
84
|
+
1
|
|
85
|
+
], e * 10);
|
|
86
|
+
}
|
|
87
|
+
let d = o.map((e, t) => t === 0 ? "head" : t === u ? "caudal-fin" : `spine-${t}`), f = o.map((e, t) => t - 1);
|
|
88
|
+
return s?.forEach(({ position: e }, t) => {
|
|
89
|
+
let [n, r, a] = e, s = -a / i, c = o.reduce((e, t, n) => Math.abs(s - t) < Math.abs(s - o[e]) ? n : e, 0);
|
|
90
|
+
d.push(t === 0 ? "pectoral-L" : "pectoral-R"), f.push(c), l.set([
|
|
91
|
+
n,
|
|
92
|
+
r,
|
|
93
|
+
a + o[c] * i,
|
|
94
|
+
0,
|
|
95
|
+
0,
|
|
96
|
+
0,
|
|
97
|
+
1,
|
|
98
|
+
1,
|
|
99
|
+
1,
|
|
100
|
+
1
|
|
101
|
+
], (o.length + t) * 10);
|
|
102
|
+
}), s?.forEach(({ position: e, distal: t }, n) => {
|
|
103
|
+
if (!t) return;
|
|
104
|
+
let r = d.length * 10;
|
|
105
|
+
d.push(n === 0 ? "pectoral-tip-L" : "pectoral-tip-R"), f.push(o.length + n), l.set([
|
|
106
|
+
...t.map((t, n) => t - e[n]),
|
|
107
|
+
0,
|
|
108
|
+
0,
|
|
109
|
+
0,
|
|
110
|
+
1,
|
|
111
|
+
1,
|
|
112
|
+
1,
|
|
113
|
+
1
|
|
114
|
+
], r);
|
|
115
|
+
}), {
|
|
116
|
+
species: t,
|
|
117
|
+
length: i,
|
|
118
|
+
stations: o,
|
|
119
|
+
names: d,
|
|
120
|
+
parents: f,
|
|
121
|
+
rest: l,
|
|
122
|
+
...s && { pectorals: s }
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function r(e) {
|
|
126
|
+
let t = (t) => ({
|
|
127
|
+
position: [
|
|
128
|
+
t * e * .054,
|
|
129
|
+
e * .022,
|
|
130
|
+
-e * .05
|
|
131
|
+
],
|
|
132
|
+
distal: [
|
|
133
|
+
t * e * .285,
|
|
134
|
+
e * .015,
|
|
135
|
+
-e * .21
|
|
136
|
+
],
|
|
137
|
+
roll: .68
|
|
138
|
+
});
|
|
139
|
+
return [t(1), t(-1)];
|
|
140
|
+
}
|
|
141
|
+
function i(t, n, r = 1) {
|
|
142
|
+
if (!Number.isFinite(n) || n < 0 || !Number.isFinite(r) || r <= 0 || r > 1) throw RangeError("Cruising speed must be nonnegative and effort greater than 0, up to 1");
|
|
143
|
+
let i = e[t.species], a = t.species === "bluespotted-ray" ? .13 : i.excursion, o = i.strouhal * n / (2 * a * t.length * r);
|
|
144
|
+
if (!Number.isFinite(o)) throw RangeError("Cruising cadence exceeds the finite range; increase effort or lower speed");
|
|
145
|
+
return o;
|
|
146
|
+
}
|
|
147
|
+
function a(n, r, i = new Float32Array(n.rest.length)) {
|
|
148
|
+
let { phase: a, effort: o = 1, turn: s = 0 } = r;
|
|
149
|
+
if (!Number.isFinite(a) || !Number.isFinite(o) || o < 0 || o > 1 || !Number.isFinite(s) || Math.abs(s) > 1) throw RangeError("A swim stroke needs a finite phase, effort from 0 to 1 and turn from −1 to 1");
|
|
150
|
+
if (i.length !== n.rest.length || i.buffer === n.rest.buffer) throw RangeError("The swim output must be separate from the matching rest pose");
|
|
151
|
+
if (i.set(n.rest), o === 0 && s === 0) return i;
|
|
152
|
+
let { vertical: c, onset: l, envelope: u, wavelength: d, excursion: f, feather: p, turn: m } = e[n.species], h = t / d, g = a % 1 * t, _ = n.stations.length - 1, v = 0, y = 0, b = 0, x = 1;
|
|
153
|
+
for (let e = 0; e <= _; e++) {
|
|
154
|
+
let t = e === _ ? 1 : (n.stations[e] + n.stations[e + 1]) * .5, r = Math.max(0, (t - l) / (1 - l)), a = h * t - g, d = r ** u, S = u * r ** (u - 1) / (1 - l), C = o * f * (S * Math.sin(a) + h * d * Math.cos(a)), w = e === _ ? o * p * Math.cos(a - Math.PI / 4) : 0, T = s * m * t * t + (c ? 0 : -Math.atan(C) + w), E = c ? Math.atan(C) + w : 0, D = T * .5, O = E * .5, k = Math.cos(D) * Math.sin(O), A = Math.sin(D) * Math.cos(O), j = -Math.sin(D) * Math.sin(O), M = Math.cos(D) * Math.cos(O), N = e * 10 + 3;
|
|
155
|
+
i[N] = x * k - v * M - y * j + b * A, i[N + 1] = x * A + v * j - y * M - b * k, i[N + 2] = x * j - v * A + y * k - b * M, i[N + 3] = x * M + v * k + y * A + b * j, v = k, y = A, b = j, x = M;
|
|
156
|
+
}
|
|
157
|
+
let S = _ + 3;
|
|
158
|
+
return n.pectorals?.forEach(({ roll: e = .22, distal: t }, r) => {
|
|
159
|
+
let a = r === 0 ? 1 : -1, l = n.species === "bluespotted-ray", u = g + a * .16 - .67, d = (a * (o * (l ? .85 : c ? .41 : .19) * Math.sin(u)) + s * .52) * e * .5, f = (_ + 1 + r) * 10;
|
|
160
|
+
if (i[f + 5] = Math.sin(d), i[f + 6] = Math.cos(d), t) {
|
|
161
|
+
let t = (a * o * Math.sin(u - .78) + s * .18) * e * .5 - d;
|
|
162
|
+
i[S * 10 + 5] = Math.sin(t), i[S * 10 + 6] = Math.cos(t), S++;
|
|
163
|
+
}
|
|
164
|
+
}), i;
|
|
165
|
+
}
|
|
166
|
+
//#endregion
|
|
167
|
+
export { n as createSwimRig, a as sampleSwimPose, i as swimCadence };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type WeightMesh } from './mesh.js';
|
|
2
|
+
import { WeightMap, type VertexInfluences } from './weight-map.js';
|
|
3
|
+
/** A skinned mesh to copy from. */
|
|
4
|
+
export interface WeightSource extends WeightMesh {
|
|
5
|
+
/** One row per vertex: a {@link WeightMap}, or JOINTS_0 / WEIGHTS_0 style influences. */
|
|
6
|
+
readonly weights: WeightMap | VertexInfluences;
|
|
7
|
+
}
|
|
8
|
+
/** Options for {@link transferWeights}. */
|
|
9
|
+
export interface TransferOptions {
|
|
10
|
+
/** Bones in the result, for influences that do not use the last ones. Default the source's. */
|
|
11
|
+
boneCount?: number;
|
|
12
|
+
/** Farthest a match may be, as a fraction of the source's size (default 0.05). */
|
|
13
|
+
maxDistance?: number;
|
|
14
|
+
/** Largest angle between the two surfaces at a match, degrees, 0 to 90 (default 60); either facing counts. */
|
|
15
|
+
maxAngle?: number;
|
|
16
|
+
/** Most conjugate-gradient steps of the fill per bone (default 1,000); it stops once the residual is 1e-10 of the start. */
|
|
17
|
+
iterations?: number;
|
|
18
|
+
/** Weights below this are dropped before renormalising (default 1e-4). */
|
|
19
|
+
threshold?: number;
|
|
20
|
+
/** Welding tolerance of the target as a fraction of its size (default 1e-6: coincident vertices). */
|
|
21
|
+
weld?: number;
|
|
22
|
+
/** Cancels the transfer; the promise rejects with the signal's reason. */
|
|
23
|
+
signal?: AbortSignal;
|
|
24
|
+
}
|
|
25
|
+
/** {@link transferWeights}'s result. */
|
|
26
|
+
export interface TransferResult {
|
|
27
|
+
/** Weights over the target's vertices. */
|
|
28
|
+
readonly weights: WeightMap;
|
|
29
|
+
/** Target points that matched the source. */
|
|
30
|
+
readonly matched: number;
|
|
31
|
+
/** Target points filled from their neighbours. */
|
|
32
|
+
readonly filled: number;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Copies skin weights from `source` to `target` by proximity. Both meshes share one space. Vertices at one position get
|
|
36
|
+
* the same weights. Resolves to a {@link WeightMap} over the target's vertices, with how many points matched and how
|
|
37
|
+
* many were filled in. A part of the target with no match at all, such as a hat far from the body, takes the closest
|
|
38
|
+
* weights as they are.
|
|
39
|
+
* @throws TypeError or RangeError for invalid input.
|
|
40
|
+
*/
|
|
41
|
+
export declare function transferWeights(source: WeightSource, target: WeightMesh, options?: TransferOptions): Promise<TransferResult>;
|
package/dist/transfer.js
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { t as e } from "./weight-map-D8cAONQQ.js";
|
|
2
|
+
import { i as t, r as n, t as r } from "./mesh-CyvLHNp0.js";
|
|
3
|
+
import { MargoError as i } from "@margoengine/math/errors";
|
|
4
|
+
//#region src/transfer.ts
|
|
5
|
+
var a = 30;
|
|
6
|
+
function o(e) {
|
|
7
|
+
let t = e.positions, n = e.triangles, r = new Float64Array(t.length);
|
|
8
|
+
for (let e = 0; e < n.length; e += 3) {
|
|
9
|
+
let i = n[e] * 3, a = n[e + 1] * 3, o = n[e + 2] * 3, s = t[a] - t[i], c = t[a + 1] - t[i + 1], l = t[a + 2] - t[i + 2], u = t[o] - t[i], d = t[o + 1] - t[i + 1], f = t[o + 2] - t[i + 2], p = c * f - l * d, m = l * u - s * f, h = s * d - c * u;
|
|
10
|
+
for (let e of [
|
|
11
|
+
i,
|
|
12
|
+
a,
|
|
13
|
+
o
|
|
14
|
+
]) r[e] += p, r[e + 1] += m, r[e + 2] += h;
|
|
15
|
+
}
|
|
16
|
+
for (let e = 0; e < r.length; e += 3) {
|
|
17
|
+
let t = Math.hypot(r[e], r[e + 1], r[e + 2]);
|
|
18
|
+
t > 0 && (r[e] /= t, r[e + 1] /= t, r[e + 2] /= t);
|
|
19
|
+
}
|
|
20
|
+
return r;
|
|
21
|
+
}
|
|
22
|
+
async function s(s, c, l = {}) {
|
|
23
|
+
let { maxDistance: u = .05, maxAngle: d = 60, iterations: f = 1e3, threshold: p = 1e-4, signal: m } = l;
|
|
24
|
+
if (!(u > 0 && u < Infinity)) throw RangeError("Transfer maxDistance must be above 0");
|
|
25
|
+
if (!(d >= 0 && d <= 90)) throw RangeError("Transfer maxAngle must be from 0 to 90 degrees");
|
|
26
|
+
if (!Number.isInteger(f) || f < 0) throw RangeError("Transfer iterations must be 0 or more");
|
|
27
|
+
if (!(p >= 0 && p < .5)) throw RangeError("Transfer threshold must be from 0 to below 0.5");
|
|
28
|
+
m?.throwIfAborted();
|
|
29
|
+
let h = s?.weights instanceof e ? s.weights : e.fromInfluences(s?.weights, l.boneCount), g = l.boneCount ?? h.boneCount, _ = s?.positions, v = _?.length / 3;
|
|
30
|
+
if (g < h.boneCount) throw RangeError(`Transfer boneCount ${g} is under the source's ${h.boneCount}; raise it`);
|
|
31
|
+
if (h.vertexCount !== v) throw RangeError(`Transfer source has ${v} vertices and ${h.vertexCount} rows; give one row each`);
|
|
32
|
+
let y = t({
|
|
33
|
+
positions: _,
|
|
34
|
+
indices: s.indices
|
|
35
|
+
}, 0), b = t(c, l.weld);
|
|
36
|
+
if (!y.triangles.length) throw RangeError("Transfer source needs triangles; pass a mesh with faces");
|
|
37
|
+
let x;
|
|
38
|
+
try {
|
|
39
|
+
x = await import("@margoengine/bvh");
|
|
40
|
+
} catch (e) {
|
|
41
|
+
throw new i("unsupported", `Weight transfer needs @margoengine/bvh (${String(e)}); install it`);
|
|
42
|
+
}
|
|
43
|
+
m?.throwIfAborted();
|
|
44
|
+
let S = new Int32Array(y.positions.length / 3).fill(-1);
|
|
45
|
+
for (let e = v - 1; e >= 0; e--) S[y.point[e]] = e;
|
|
46
|
+
let C = x.MeshBVH.build({
|
|
47
|
+
positions: y.positions,
|
|
48
|
+
indices: y.triangles
|
|
49
|
+
}), w = b.positions.length / 3, T = new Float32Array(w * g), E = new Uint8Array(w), D = o(b), O = u * y.size, k = Math.cos(d * Math.PI / 180) - 1e-9, A = y.positions, j = y.triangles, M = h.boneCount, N = h.values, P = [
|
|
50
|
+
0,
|
|
51
|
+
0,
|
|
52
|
+
0
|
|
53
|
+
], F = performance.now();
|
|
54
|
+
for (let e = 0; e < w; e++) {
|
|
55
|
+
P[0] = b.positions[e * 3], P[1] = b.positions[e * 3 + 1], P[2] = b.positions[e * 3 + 2];
|
|
56
|
+
let t = C.closestPointToPoint(P), n = j[t.faceIndex * 3], r = j[t.faceIndex * 3 + 1], i = j[t.faceIndex * 3 + 2], [o, s, c] = t.barycoord, l = S[n] * M, u = S[r] * M, d = S[i] * M;
|
|
57
|
+
for (let t = 0; t < M; t++) T[e * g + t] = N[l + t] * o + N[u + t] * s + N[d + t] * c;
|
|
58
|
+
let f = A[r * 3] - A[n * 3], p = A[r * 3 + 1] - A[n * 3 + 1], h = A[r * 3 + 2] - A[n * 3 + 2], _ = A[i * 3] - A[n * 3], v = A[i * 3 + 1] - A[n * 3 + 1], y = A[i * 3 + 2] - A[n * 3 + 2], x = p * y - h * v, w = h * _ - f * y, I = f * v - p * _, L = Math.hypot(x, w, I) || 1, R = Math.abs(x * D[e * 3] + w * D[e * 3 + 1] + I * D[e * 3 + 2]) / L, z = D[e * 3] || D[e * 3 + 1] || D[e * 3 + 2];
|
|
59
|
+
t.distance <= O && (!z || R >= k) && (E[e] = 1), performance.now() - F > a && (await new Promise((e) => setTimeout(e)), F = performance.now(), m?.throwIfAborted());
|
|
60
|
+
}
|
|
61
|
+
let I = r(b), L = /* @__PURE__ */ new Set();
|
|
62
|
+
for (let e = 0; e < w; e++) E[e] && L.add(I[e]);
|
|
63
|
+
for (let e = 0; e < w; e++) L.has(I[e]) || (E[e] = 1);
|
|
64
|
+
let { offsets: R, columns: z } = n(b), B = [];
|
|
65
|
+
for (let e = 0; e < w; e++) E[e] || B.push(e);
|
|
66
|
+
let V = new Float32Array(z.length), H = b.positions;
|
|
67
|
+
for (let e of B) for (let t = R[e]; t < R[e + 1]; t++) {
|
|
68
|
+
let n = z[t];
|
|
69
|
+
V[t] = 1 / Math.max(1e-12, Math.hypot(H[e * 3] - H[n * 3], H[e * 3 + 1] - H[n * 3 + 1], H[e * 3 + 2] - H[n * 3 + 2]));
|
|
70
|
+
}
|
|
71
|
+
let U = new Int32Array(w).fill(-1);
|
|
72
|
+
B.forEach((e, t) => U[e] = t);
|
|
73
|
+
let W = B.length, G = new Float64Array(W), K = new Float64Array(W), q = new Float64Array(W), J = new Float64Array(W), Y = new Float64Array(W), X = new Float64Array(W), Z = new Float64Array(W);
|
|
74
|
+
B.forEach((e, t) => {
|
|
75
|
+
for (let n = R[e]; n < R[e + 1]; n++) G[t] += V[n];
|
|
76
|
+
});
|
|
77
|
+
let Q = (e, t) => {
|
|
78
|
+
for (let n = 0; n < W; n++) {
|
|
79
|
+
let r = B[n], i = G[n] * e[n];
|
|
80
|
+
for (let t = R[r]; t < R[r + 1]; t++) {
|
|
81
|
+
let n = U[z[t]];
|
|
82
|
+
n >= 0 && (i -= V[t] * e[n]);
|
|
83
|
+
}
|
|
84
|
+
t[n] = i;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
for (let e = 0; e < g && W; e++) {
|
|
88
|
+
let t = 0;
|
|
89
|
+
for (let n = 0; n < W; n++) {
|
|
90
|
+
let r = B[n], i = 0;
|
|
91
|
+
for (let t = R[r]; t < R[r + 1]; t++) U[z[t]] < 0 && (i += V[t] * T[z[t] * g + e]);
|
|
92
|
+
Z[n] = i, t += i * i, K[n] = T[r * g + e];
|
|
93
|
+
}
|
|
94
|
+
if (!(t > 0)) {
|
|
95
|
+
for (let t of B) T[t * g + e] = 0;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
Q(K, X);
|
|
99
|
+
let n = 0;
|
|
100
|
+
for (let e = 0; e < W; e++) q[e] = Z[e] - X[e], J[e] = q[e] / G[e], Y[e] = J[e], n += q[e] * J[e];
|
|
101
|
+
let r = 1e-20 * t;
|
|
102
|
+
for (let e = 0; e < f; e++) {
|
|
103
|
+
let e = 0;
|
|
104
|
+
for (let t = 0; t < W; t++) e += q[t] * q[t];
|
|
105
|
+
if (e <= r) break;
|
|
106
|
+
Q(Y, X);
|
|
107
|
+
let t = 0;
|
|
108
|
+
for (let e = 0; e < W; e++) t += Y[e] * X[e];
|
|
109
|
+
if (!(t > 0)) break;
|
|
110
|
+
let i = n / t, o = 0;
|
|
111
|
+
for (let e = 0; e < W; e++) K[e] += i * Y[e], q[e] -= i * X[e], J[e] = q[e] / G[e], o += q[e] * J[e];
|
|
112
|
+
let s = o / n;
|
|
113
|
+
n = o;
|
|
114
|
+
for (let e = 0; e < W; e++) Y[e] = J[e] + s * Y[e];
|
|
115
|
+
performance.now() - F > a && (await new Promise((e) => setTimeout(e)), F = performance.now(), m?.throwIfAborted());
|
|
116
|
+
}
|
|
117
|
+
for (let t = 0; t < W; t++) T[B[t] * g + e] = Math.min(1, Math.max(0, K[t]));
|
|
118
|
+
}
|
|
119
|
+
let $ = new e(b.vertexCount, g);
|
|
120
|
+
for (let e = 0; e < w; e++) {
|
|
121
|
+
let t = 0;
|
|
122
|
+
for (let n = 0; n < g; n++) {
|
|
123
|
+
let r = e * g + n;
|
|
124
|
+
T[r] < p && (T[r] = 0), t += T[r];
|
|
125
|
+
}
|
|
126
|
+
if (t > 0) for (let n = 0; n < g; n++) T[e * g + n] /= t;
|
|
127
|
+
}
|
|
128
|
+
for (let e = 0; e < b.vertexCount; e++) $.values.set(T.subarray(b.point[e] * g, (b.point[e] + 1) * g), e * g);
|
|
129
|
+
return {
|
|
130
|
+
weights: $,
|
|
131
|
+
matched: w - B.length,
|
|
132
|
+
filled: B.length
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
//#endregion
|
|
136
|
+
export { s as transferWeights };
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { type AnatomicalRig, type Tier, type Vec3 } from './anatomy.js';
|
|
2
|
+
/** What an issue is. */
|
|
3
|
+
export type PoseIssueCode =
|
|
4
|
+
/** A swing past the joint's range. */
|
|
5
|
+
'out-of-range'
|
|
6
|
+
/** A hinge (elbow, knee, finger) bent past straight the wrong way. */
|
|
7
|
+
| 'hinge-reversed'
|
|
8
|
+
/** A hinge bent sideways, across its one axis. */
|
|
9
|
+
| 'hinge-sideways'
|
|
10
|
+
/** A twist past the joint's range. */
|
|
11
|
+
| 'twist-out-of-range'
|
|
12
|
+
/** A bone longer or shorter than at rest, or scaled. */
|
|
13
|
+
| 'bone-stretch'
|
|
14
|
+
/** A joint, or a point of the skin, below the ground. */
|
|
15
|
+
| 'ground-penetration'
|
|
16
|
+
/** A joint that should bear weight held off the ground. */
|
|
17
|
+
| 'floating'
|
|
18
|
+
/** Two limb volumes through each other. */
|
|
19
|
+
| 'self-penetration'
|
|
20
|
+
/** A planted foot moving along the ground. */
|
|
21
|
+
| 'foot-slide'
|
|
22
|
+
/** A joint turning faster than any body turns, or jumping in one frame. */
|
|
23
|
+
| 'pop'
|
|
24
|
+
/** A body measure outside what people measure. */
|
|
25
|
+
| 'proportion'
|
|
26
|
+
/** The left and right of a measure differing. */
|
|
27
|
+
| 'asymmetry';
|
|
28
|
+
/** `error`: no body does this. `warning`: inside the stops but outside the clinical range or what looks right. */
|
|
29
|
+
export type PoseSeverity = 'error' | 'warning';
|
|
30
|
+
/** One thing wrong with a pose. */
|
|
31
|
+
export interface PoseIssue {
|
|
32
|
+
/** What it is. */
|
|
33
|
+
readonly code: PoseIssueCode;
|
|
34
|
+
/** How bad. */
|
|
35
|
+
readonly severity: PoseSeverity;
|
|
36
|
+
/** The table joint, region or measure, or two volumes as `a + b`. */
|
|
37
|
+
readonly joint: string;
|
|
38
|
+
/** The rig bone's name, or null. */
|
|
39
|
+
readonly bone: string | null;
|
|
40
|
+
/** The clinical motion for an angle (such as `hyperextension`), or null. */
|
|
41
|
+
readonly motion: string | null;
|
|
42
|
+
/** What was measured, in `unit`. */
|
|
43
|
+
readonly value: number;
|
|
44
|
+
/** The limit it was measured against, in `unit`. */
|
|
45
|
+
readonly limit: number;
|
|
46
|
+
/** How far past the limit, in `unit`, never negative. */
|
|
47
|
+
readonly excess: number;
|
|
48
|
+
/** `deg`, `deg/s`, `stature` (a share of the body's height), `stature/s` or `ratio`. */
|
|
49
|
+
readonly unit: 'deg' | 'deg/s' | 'stature' | 'stature/s' | 'ratio';
|
|
50
|
+
/** The range tier, for an angle. */
|
|
51
|
+
readonly tier: Tier | null;
|
|
52
|
+
/** The key of the limit's first source in the species table, or null. */
|
|
53
|
+
readonly source: string | null;
|
|
54
|
+
/** The clip time in seconds, for a clip; null for one pose. */
|
|
55
|
+
readonly time: number | null;
|
|
56
|
+
/** The frame index, for a clip; null for one pose. */
|
|
57
|
+
readonly frame: number | null;
|
|
58
|
+
/** The issue in words. */
|
|
59
|
+
readonly message: string;
|
|
60
|
+
/** Where to read what it means and how to fix it. */
|
|
61
|
+
readonly help: string;
|
|
62
|
+
}
|
|
63
|
+
/** Options of {@link checkPose}. */
|
|
64
|
+
export interface PoseCheckOptions {
|
|
65
|
+
/** `normal` checks the clinical range only, `structural` the stops only, `both` (default) each with its severity. */
|
|
66
|
+
readonly tier?: Tier | 'both';
|
|
67
|
+
/** Degrees by which an angle may pass its limit before it counts, for noise. Default 0.5. */
|
|
68
|
+
readonly tolerance?: number;
|
|
69
|
+
/**
|
|
70
|
+
* Height of the ground along the body's vertical (`body.frame.vertical`, the world's up), in the rig's space, or null
|
|
71
|
+
* for no ground tests. Default null.
|
|
72
|
+
*/
|
|
73
|
+
readonly ground?: number | null;
|
|
74
|
+
/** Skin points in the rig's space (x, y, z, ...), tested against the ground as well as the joints. */
|
|
75
|
+
readonly points?: ArrayLike<number>;
|
|
76
|
+
/** Table joints that bear weight in this pose: each must be within `float` of the ground. Default none. */
|
|
77
|
+
readonly supports?: readonly string[];
|
|
78
|
+
/** Shares of stature a support may sit above the ground before it floats. Default 0.06. */
|
|
79
|
+
readonly float?: number;
|
|
80
|
+
/** Shares of stature below the ground a joint or point may sink before it counts. Default 0.002. */
|
|
81
|
+
readonly sink?: number;
|
|
82
|
+
/** Test limbs through limbs with the table's volumes. Default true. */
|
|
83
|
+
readonly volumes?: boolean;
|
|
84
|
+
/** Share of the two radii a pass must reach into before it counts as one limb through another. Default 0.7. */
|
|
85
|
+
readonly shrink?: number;
|
|
86
|
+
/** Share by which a bone's length or scale may differ from rest. Default 0.01. */
|
|
87
|
+
readonly stretch?: number;
|
|
88
|
+
}
|
|
89
|
+
/** Options of {@link checkClip}. */
|
|
90
|
+
export interface ClipCheckOptions extends PoseCheckOptions {
|
|
91
|
+
/** Frames per second the clip is sampled at. Default 30. */
|
|
92
|
+
readonly fps?: number;
|
|
93
|
+
/** Seconds, needed when `sample` is a function. */
|
|
94
|
+
readonly duration?: number;
|
|
95
|
+
/** Time of the first frame. Default 0. */
|
|
96
|
+
readonly start?: number;
|
|
97
|
+
/** How the body travels through the world (rig units per second), for a clip played in place. Default none. */
|
|
98
|
+
readonly velocity?: Vec3;
|
|
99
|
+
/** Degrees per second no joint reaches. Default 9,000. */
|
|
100
|
+
readonly maxSpeed?: number;
|
|
101
|
+
/** Shares of stature above its lowest a foot joint may be and still be planted. Default 0.02. */
|
|
102
|
+
readonly contact?: number;
|
|
103
|
+
/** Shares of stature per second a planted foot may move along the ground. Default 0.1. */
|
|
104
|
+
readonly slide?: number;
|
|
105
|
+
}
|
|
106
|
+
/** One issue over a clip: how often it happens, when, and its worst frame. */
|
|
107
|
+
export interface ClipIssueSummary {
|
|
108
|
+
readonly code: PoseIssueCode;
|
|
109
|
+
readonly severity: PoseSeverity;
|
|
110
|
+
readonly joint: string;
|
|
111
|
+
readonly motion: string | null;
|
|
112
|
+
/** Frames it happens in. */
|
|
113
|
+
readonly frames: number;
|
|
114
|
+
/** Time of the first and last of them. */
|
|
115
|
+
readonly first: number;
|
|
116
|
+
readonly last: number;
|
|
117
|
+
/** The frame where it is furthest past its limit. */
|
|
118
|
+
readonly worst: PoseIssue;
|
|
119
|
+
}
|
|
120
|
+
/** What {@link checkPose} returns. */
|
|
121
|
+
export interface PoseReport {
|
|
122
|
+
/** True when no issue is an error. */
|
|
123
|
+
readonly passed: boolean;
|
|
124
|
+
readonly errors: number;
|
|
125
|
+
readonly warnings: number;
|
|
126
|
+
/** Every issue, worst first within each code. */
|
|
127
|
+
readonly issues: readonly PoseIssue[];
|
|
128
|
+
}
|
|
129
|
+
/** What {@link checkClip} returns. */
|
|
130
|
+
export interface ClipReport extends PoseReport {
|
|
131
|
+
/** Frames checked. */
|
|
132
|
+
readonly frames: number;
|
|
133
|
+
readonly fps: number;
|
|
134
|
+
/** One row per joint, motion and code, errors first, then by how often. */
|
|
135
|
+
readonly summary: readonly ClipIssueSummary[];
|
|
136
|
+
}
|
|
137
|
+
/** Options of {@link checkProportions}. */
|
|
138
|
+
export interface ProportionCheckOptions {
|
|
139
|
+
/** The body's real standing height in rig units, when known (from its skin). Default: the fitted body's stature. */
|
|
140
|
+
readonly stature?: number;
|
|
141
|
+
/** Share by which a left and a right measure may differ. Default 0.05. */
|
|
142
|
+
readonly asymmetry?: number;
|
|
143
|
+
}
|
|
144
|
+
/** The README section that explains each issue code. */
|
|
145
|
+
export declare const POSE_HELP = "https://margoengine.com/llms/packages/rigging.md#pose-checks-margoengineriggingvalidate";
|
|
146
|
+
/**
|
|
147
|
+
* Checks one pose of a fitted body: each joint against its clinical range (warnings) and its stops (errors), with the
|
|
148
|
+
* hinge faults named apart (`hinge-reversed`, `hinge-sideways`), bones stretched from rest, joints or skin points below
|
|
149
|
+
* `ground`, weight-bearing `supports` off it, and limbs through limbs. Nothing is changed.
|
|
150
|
+
* @example
|
|
151
|
+
* const report = checkPose(body, pose, { ground: 0 });
|
|
152
|
+
* for (const issue of report.issues) console.log(issue.severity, issue.message);
|
|
153
|
+
* @param pose A local pose, 10 numbers per bone (translation, rotation xyzw, scale).
|
|
154
|
+
* @throws TypeError for a body not made by `fitAnatomy` or a pose of the wrong length.
|
|
155
|
+
*/
|
|
156
|
+
export declare function checkPose(body: AnatomicalRig, pose: ArrayLike<number>, options?: PoseCheckOptions): PoseReport;
|
|
157
|
+
/**
|
|
158
|
+
* Checks every frame of a clip, as {@link checkPose} does, and across frames: joints that turn faster than any body or
|
|
159
|
+
* jump in one frame (`pop`), and planted feet that slide (`foot-slide`). `sample` is either the frames (local poses,
|
|
160
|
+
* one buffer may be reused) or a function that writes the pose at a time into `out`. The summary has one row per joint,
|
|
161
|
+
* motion and code with how many frames it lasts and its worst frame: the report for a capture or a retargeted take.
|
|
162
|
+
* @example
|
|
163
|
+
* const clip = checkClip(body, (time, out) => out.set(sampleMyClip(time)), { duration: 2.4, fps: 30, ground: 0 });
|
|
164
|
+
* console.log(formatPoseReport(clip));
|
|
165
|
+
* @throws TypeError for a pose of the wrong length or a function sampler without a duration.
|
|
166
|
+
*/
|
|
167
|
+
export declare function checkClip(body: AnatomicalRig, sample: Iterable<ArrayLike<number>> | ((time: number, out: Float64Array) => void), options?: ClipCheckOptions): ClipReport;
|
|
168
|
+
/**
|
|
169
|
+
* Checks a fitted body's build on its neutral pose (standing, arms down) against the species' measured proportions
|
|
170
|
+
* that the table says how to read from joint centres (heights of the shoulder, elbow, wrist and knee, the length of
|
|
171
|
+
* each long bone, the trunk), and the left side against the right. An arm too long or a knee too high is an error.
|
|
172
|
+
* @example
|
|
173
|
+
* for (const issue of checkProportions(body)) console.log(issue.message);
|
|
174
|
+
* @throws TypeError for a body not made by `fitAnatomy`.
|
|
175
|
+
*/
|
|
176
|
+
export declare function checkProportions(body: AnatomicalRig, options?: ProportionCheckOptions): PoseIssue[];
|
|
177
|
+
/** A report as lines of text: a header, then the clip summary or each issue. */
|
|
178
|
+
export declare function formatPoseReport(report: PoseReport | ClipReport): string;
|
|
179
|
+
/**
|
|
180
|
+
* {@link checkPose} that throws when any issue is an error, for a test.
|
|
181
|
+
* @throws MargoError `failed` with the report as `error.report`.
|
|
182
|
+
*/
|
|
183
|
+
export declare function assertPose(body: AnatomicalRig, pose: ArrayLike<number>, options?: PoseCheckOptions): PoseReport;
|
|
184
|
+
/**
|
|
185
|
+
* {@link checkClip} that throws when any issue is an error, for a test of a capture or a retargeted take.
|
|
186
|
+
* @throws MargoError `failed` with the report as `error.report`.
|
|
187
|
+
*/
|
|
188
|
+
export declare function assertClip(body: AnatomicalRig, sample: Iterable<ArrayLike<number>> | ((time: number, out: Float64Array) => void), options?: ClipCheckOptions): ClipReport;
|