@margoengine/input 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 +608 -2
- package/dist/body.d.ts +117 -0
- package/dist/body.js +446 -0
- package/dist/face.d.ts +136 -0
- package/dist/face.js +344 -0
- package/dist/gestures.d.ts +68 -0
- package/dist/haptics.d.ts +52 -0
- package/dist/haptics.js +59 -0
- package/dist/index.d.ts +205 -0
- package/dist/index.js +3 -0
- package/dist/interactions.d.ts +92 -0
- package/dist/interactions.js +93 -0
- package/dist/labels-DZOEJBkX.js +70 -0
- package/dist/labels.d.ts +8 -0
- package/dist/landmarks.d.ts +197 -0
- package/dist/landmarks.js +183 -0
- package/dist/layout.d.ts +62 -0
- package/dist/layout.js +76 -0
- package/dist/lock.d.ts +72 -0
- package/dist/lock.js +91 -0
- package/dist/motion.d.ts +97 -0
- package/dist/motion.js +98 -0
- package/dist/pen.d.ts +86 -0
- package/dist/pen.js +111 -0
- package/dist/players.d.ts +129 -0
- package/dist/players.js +161 -0
- package/dist/replay.d.ts +109 -0
- package/dist/replay.js +99 -0
- package/dist/src-eYqyrCA7.js +459 -0
- package/dist/vector.d.ts +44 -0
- package/dist/vector.js +66 -0
- package/dist/xr.d.ts +124 -0
- package/dist/xr.js +154 -0
- package/package.json +103 -4
package/dist/body.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { type LandmarkFrame, type LandmarkSource, type OneEuroOptions } from './landmarks.js';
|
|
2
|
+
/** The skeleton parts the driver uses; a `Skeleton` from `@margoengine/animation` fits. */
|
|
3
|
+
export interface SkeletonLike {
|
|
4
|
+
readonly names: readonly string[];
|
|
5
|
+
readonly parents: readonly number[];
|
|
6
|
+
getPose(bone: number): {
|
|
7
|
+
translation: ArrayLike<number>;
|
|
8
|
+
rotation: ArrayLike<number>;
|
|
9
|
+
};
|
|
10
|
+
setPose(bone: number, pose: {
|
|
11
|
+
rotation: [number, number, number, number];
|
|
12
|
+
}): unknown;
|
|
13
|
+
}
|
|
14
|
+
/** A landmark index, or several averaged (a midpoint). */
|
|
15
|
+
export type LandmarkRef = number | readonly number[];
|
|
16
|
+
/** One driven bone. Bone names are VRM humanoid names (`hips`, `leftUpperArm`, `leftIndexProximal`, ...). */
|
|
17
|
+
export interface BoneTarget {
|
|
18
|
+
/** The bone to rotate. */
|
|
19
|
+
bone: string;
|
|
20
|
+
/** Landmarks whose direction `from` -> `to` the bone follows. */
|
|
21
|
+
from: LandmarkRef;
|
|
22
|
+
to: LandmarkRef;
|
|
23
|
+
/** The bone whose rest position gives the rest direction; default the bone's own rest offset from its parent. */
|
|
24
|
+
tip?: string;
|
|
25
|
+
/** A second direction that fixes the twist about the first, with the two bones giving it at rest. */
|
|
26
|
+
side?: {
|
|
27
|
+
from: LandmarkRef;
|
|
28
|
+
to: LandmarkRef;
|
|
29
|
+
tipFrom: string;
|
|
30
|
+
tipTo: string;
|
|
31
|
+
};
|
|
32
|
+
/** Joint limit: the largest turn from the bone's rest rotation relative to its parent, in radians. Default pi (none). */
|
|
33
|
+
limit?: number;
|
|
34
|
+
}
|
|
35
|
+
/** Which landmarks drive the skeleton. */
|
|
36
|
+
export type BodyPart = 'pose' | 'hands';
|
|
37
|
+
/** The body bones driven by the 33 Pose Landmarker points. */
|
|
38
|
+
export declare const POSE_TARGETS: readonly BoneTarget[];
|
|
39
|
+
/** The hand and finger bones driven by the 21 Hand Landmarker points of one hand. */
|
|
40
|
+
export declare function handTargets(side: 'left' | 'right'): BoneTarget[];
|
|
41
|
+
/** {@link BodyDriver} options; every one is optional. */
|
|
42
|
+
export interface BodyDriverOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Humanoid bone name to skeleton bone: an index map (what `humanoidBones` in `@margoengine/rigging/retarget` returns)
|
|
45
|
+
* or name to name. Default: bones named with the humanoid names (case-insensitive).
|
|
46
|
+
*/
|
|
47
|
+
bones?: ReadonlyMap<string, number> | Readonly<Record<string, string>>;
|
|
48
|
+
/** Which landmarks drive bones. Default both. */
|
|
49
|
+
parts?: readonly BodyPart[];
|
|
50
|
+
/** Points below this visibility (when the detector gives it) leave their bone at its last rotation. Default 0.5. */
|
|
51
|
+
minVisibility?: number;
|
|
52
|
+
/** Tracks below this score are not trusted. Default 0.5. */
|
|
53
|
+
minScore?: number;
|
|
54
|
+
/** Seconds the last pose is held through lost tracking before the bones return to rest. Default 0.5. */
|
|
55
|
+
hold?: number;
|
|
56
|
+
/** One-euro filter on each landmark direction, or false for raw values. Default `{ minCutoff: 1.5, beta: 0.3 }`. */
|
|
57
|
+
smoothing?: OneEuroOptions | false;
|
|
58
|
+
/** Swap left and right, for a mirrored (selfie) picture. Default false. */
|
|
59
|
+
mirror?: boolean;
|
|
60
|
+
/** Frame width over height, used when only image points are given. Default 1. */
|
|
61
|
+
aspect?: number;
|
|
62
|
+
/** Largest swing per bone name in radians, over the targets' own limits. */
|
|
63
|
+
limits?: Readonly<Record<string, number>>;
|
|
64
|
+
}
|
|
65
|
+
/** The driver's latest result. */
|
|
66
|
+
export interface BodyState {
|
|
67
|
+
/** False once tracking has been lost for longer than `hold`. */
|
|
68
|
+
readonly tracked: boolean;
|
|
69
|
+
/** Bones written by the last update. */
|
|
70
|
+
readonly driven: number;
|
|
71
|
+
/** Frame time in ms. */
|
|
72
|
+
readonly time: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Drives a humanoid skeleton from pose and hand landmark frames: every mapped bone turns from its rest direction onto
|
|
76
|
+
* the landmark direction (a second direction fixes the twist where the points give one), within a swing limit,
|
|
77
|
+
* smoothed by the one-euro filter, holding the last good rotation for points the detector cannot see.
|
|
78
|
+
* Create it with the skeleton in its rest pose (T-pose or A-pose).
|
|
79
|
+
*/
|
|
80
|
+
export declare class BodyDriver implements Disposable {
|
|
81
|
+
readonly skeleton: SkeletonLike;
|
|
82
|
+
private readonly bound;
|
|
83
|
+
private readonly restWorld;
|
|
84
|
+
private readonly options;
|
|
85
|
+
private lastGood;
|
|
86
|
+
private current;
|
|
87
|
+
private disposed;
|
|
88
|
+
/** @throws RangeError for an option out of range; MargoError `state` when no target bone is in the skeleton. */
|
|
89
|
+
constructor(skeleton: SkeletonLike, options?: BodyDriverOptions);
|
|
90
|
+
/** Bone names the driver writes, in update order. */
|
|
91
|
+
get bones(): string[];
|
|
92
|
+
/** The latest result. */
|
|
93
|
+
get state(): BodyState;
|
|
94
|
+
private points;
|
|
95
|
+
private point;
|
|
96
|
+
private directions;
|
|
97
|
+
/**
|
|
98
|
+
* Take this frame as the rest pose: the person stands as the skeleton's rest pose (T-pose or A-pose). Bones it cannot
|
|
99
|
+
* see keep the skeleton's own rest directions. @throws MargoError `state` when the frame shows none of the bones.
|
|
100
|
+
*/
|
|
101
|
+
calibrate(frame: LandmarkFrame): this;
|
|
102
|
+
private check;
|
|
103
|
+
/**
|
|
104
|
+
* Pose the skeleton from one frame (its first pose and one hand per side). `time` (ms) defaults to the frame's.
|
|
105
|
+
* @throws MargoError `disposed` after dispose.
|
|
106
|
+
*/
|
|
107
|
+
update(frame: LandmarkFrame, time?: number): BodyState;
|
|
108
|
+
private isBelow;
|
|
109
|
+
/** Update from every frame of `source` until it ends or `signal` aborts. */
|
|
110
|
+
drive(source: LandmarkSource, { signal, onUpdate }?: {
|
|
111
|
+
signal?: AbortSignal;
|
|
112
|
+
onUpdate?: (state: BodyState) => void;
|
|
113
|
+
}): Promise<void>;
|
|
114
|
+
/** Release the driver; later calls throw. The skeleton keeps its last pose. */
|
|
115
|
+
dispose(): void;
|
|
116
|
+
[Symbol.dispose](): void;
|
|
117
|
+
}
|
package/dist/body.js
ADDED
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import { OneEuroFilter as e } from "./landmarks.js";
|
|
2
|
+
import { MargoError as t } from "@margoengine/math/errors";
|
|
3
|
+
import { Quaternion as n } from "@margoengine/math/quaternion";
|
|
4
|
+
//#region src/body.ts
|
|
5
|
+
var r = "; see https://margoengine.com/errors#body-driver", i = [23, 24], a = [11, 12], o = [
|
|
6
|
+
{
|
|
7
|
+
bone: "hips",
|
|
8
|
+
from: i,
|
|
9
|
+
to: a,
|
|
10
|
+
tip: "neck",
|
|
11
|
+
side: {
|
|
12
|
+
from: 24,
|
|
13
|
+
to: 23,
|
|
14
|
+
tipFrom: "rightUpperLeg",
|
|
15
|
+
tipTo: "leftUpperLeg"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
bone: "upperChest",
|
|
20
|
+
from: i,
|
|
21
|
+
to: a,
|
|
22
|
+
tip: "neck",
|
|
23
|
+
side: {
|
|
24
|
+
from: 12,
|
|
25
|
+
to: 11,
|
|
26
|
+
tipFrom: "rightUpperArm",
|
|
27
|
+
tipTo: "leftUpperArm"
|
|
28
|
+
},
|
|
29
|
+
limit: 1
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
bone: "neck",
|
|
33
|
+
from: a,
|
|
34
|
+
to: [7, 8],
|
|
35
|
+
tip: "head",
|
|
36
|
+
limit: 1
|
|
37
|
+
},
|
|
38
|
+
...["left", "right"].flatMap((e) => {
|
|
39
|
+
let t = e === "left" ? 0 : 1;
|
|
40
|
+
return [
|
|
41
|
+
{
|
|
42
|
+
bone: `${e}UpperArm`,
|
|
43
|
+
from: 11 + t,
|
|
44
|
+
to: 13 + t,
|
|
45
|
+
tip: `${e}LowerArm`
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
bone: `${e}LowerArm`,
|
|
49
|
+
from: 13 + t,
|
|
50
|
+
to: 15 + t,
|
|
51
|
+
tip: `${e}Hand`,
|
|
52
|
+
limit: 2.7
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
bone: `${e}UpperLeg`,
|
|
56
|
+
from: 23 + t,
|
|
57
|
+
to: 25 + t,
|
|
58
|
+
tip: `${e}LowerLeg`
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
bone: `${e}LowerLeg`,
|
|
62
|
+
from: 25 + t,
|
|
63
|
+
to: 27 + t,
|
|
64
|
+
tip: `${e}Foot`,
|
|
65
|
+
limit: 2.7
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
bone: `${e}Foot`,
|
|
69
|
+
from: 29 + t,
|
|
70
|
+
to: 31 + t,
|
|
71
|
+
tip: `${e}Toes`,
|
|
72
|
+
limit: 1.2
|
|
73
|
+
}
|
|
74
|
+
];
|
|
75
|
+
})
|
|
76
|
+
], s = [
|
|
77
|
+
"Index",
|
|
78
|
+
"Middle",
|
|
79
|
+
"Ring",
|
|
80
|
+
"Little"
|
|
81
|
+
];
|
|
82
|
+
function c(e) {
|
|
83
|
+
let t = e, n = [
|
|
84
|
+
{
|
|
85
|
+
bone: `${t}Hand`,
|
|
86
|
+
from: 0,
|
|
87
|
+
to: 9,
|
|
88
|
+
tip: `${t}MiddleProximal`,
|
|
89
|
+
side: {
|
|
90
|
+
from: 17,
|
|
91
|
+
to: 5,
|
|
92
|
+
tipFrom: `${t}LittleProximal`,
|
|
93
|
+
tipTo: `${t}IndexProximal`
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
bone: `${t}ThumbMetacarpal`,
|
|
98
|
+
from: 1,
|
|
99
|
+
to: 2,
|
|
100
|
+
tip: `${t}ThumbProximal`,
|
|
101
|
+
limit: 1.6
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
bone: `${t}ThumbProximal`,
|
|
105
|
+
from: 2,
|
|
106
|
+
to: 3,
|
|
107
|
+
tip: `${t}ThumbDistal`,
|
|
108
|
+
limit: 1.6
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
bone: `${t}ThumbDistal`,
|
|
112
|
+
from: 3,
|
|
113
|
+
to: 4,
|
|
114
|
+
limit: 1.6
|
|
115
|
+
}
|
|
116
|
+
];
|
|
117
|
+
return s.forEach((e, r) => {
|
|
118
|
+
let i = 5 + r * 4;
|
|
119
|
+
n.push({
|
|
120
|
+
bone: `${t}${e}Proximal`,
|
|
121
|
+
from: i,
|
|
122
|
+
to: i + 1,
|
|
123
|
+
tip: `${t}${e}Intermediate`,
|
|
124
|
+
limit: 1.75
|
|
125
|
+
}, {
|
|
126
|
+
bone: `${t}${e}Intermediate`,
|
|
127
|
+
from: i + 1,
|
|
128
|
+
to: i + 2,
|
|
129
|
+
tip: `${t}${e}Distal`,
|
|
130
|
+
limit: 1.9
|
|
131
|
+
}, {
|
|
132
|
+
bone: `${t}${e}Distal`,
|
|
133
|
+
from: i + 2,
|
|
134
|
+
to: i + 3,
|
|
135
|
+
limit: 1.6
|
|
136
|
+
});
|
|
137
|
+
}), n;
|
|
138
|
+
}
|
|
139
|
+
var l = (e, t) => [
|
|
140
|
+
e[0] - t[0],
|
|
141
|
+
e[1] - t[1],
|
|
142
|
+
e[2] - t[2]
|
|
143
|
+
], u = (e) => Math.hypot(e[0], e[1], e[2]), d = (e) => {
|
|
144
|
+
let t = u(e) || 1;
|
|
145
|
+
return [
|
|
146
|
+
e[0] / t,
|
|
147
|
+
e[1] / t,
|
|
148
|
+
e[2] / t
|
|
149
|
+
];
|
|
150
|
+
}, f = (e, t) => [
|
|
151
|
+
e[1] * t[2] - e[2] * t[1],
|
|
152
|
+
e[2] * t[0] - e[0] * t[2],
|
|
153
|
+
e[0] * t[1] - e[1] * t[0]
|
|
154
|
+
], p = (e) => [
|
|
155
|
+
e.x,
|
|
156
|
+
e.y,
|
|
157
|
+
e.z,
|
|
158
|
+
e.w
|
|
159
|
+
], m = (e) => new n(e[0], e[1], e[2], e[3]), h = (e, t) => p(m(e).multiply(m(t))), g = (e) => [
|
|
160
|
+
-e[0],
|
|
161
|
+
-e[1],
|
|
162
|
+
-e[2],
|
|
163
|
+
e[3]
|
|
164
|
+
], _ = (e, t) => {
|
|
165
|
+
let n = [
|
|
166
|
+
e[0],
|
|
167
|
+
e[1],
|
|
168
|
+
e[2]
|
|
169
|
+
], r = f(n, t).map((e) => 2 * e), i = f(n, r);
|
|
170
|
+
return [
|
|
171
|
+
t[0] + e[3] * r[0] + i[0],
|
|
172
|
+
t[1] + e[3] * r[1] + i[1],
|
|
173
|
+
t[2] + e[3] * r[2] + i[2]
|
|
174
|
+
];
|
|
175
|
+
}, v = (e, t) => {
|
|
176
|
+
let n = d(e), r = d(f(t, n));
|
|
177
|
+
return [
|
|
178
|
+
...f(n, r),
|
|
179
|
+
0,
|
|
180
|
+
...n,
|
|
181
|
+
0,
|
|
182
|
+
...r,
|
|
183
|
+
0,
|
|
184
|
+
0,
|
|
185
|
+
0,
|
|
186
|
+
0,
|
|
187
|
+
1
|
|
188
|
+
];
|
|
189
|
+
}, y = (e) => p(new n().setFromRotationMatrix({ elements: e })), b = (e, t, r, i) => !r || !i ? p(new n().setFromUnitVectors(x(d(e)), x(d(t)))) : h(y(v(t, i)), g(y(v(e, r)))), x = (e) => ({
|
|
190
|
+
x: e[0],
|
|
191
|
+
y: e[1],
|
|
192
|
+
z: e[2]
|
|
193
|
+
}), S = (e, t) => {
|
|
194
|
+
let n = e[3] < 0 ? -1 : 1, r = 2 * Math.acos(Math.min(1, Math.abs(e[3])));
|
|
195
|
+
if (r <= t) return e;
|
|
196
|
+
let i = Math.sin(r / 2) || 1, a = t / 2, o = Math.sin(a) / i * n;
|
|
197
|
+
return [
|
|
198
|
+
e[0] * o,
|
|
199
|
+
e[1] * o,
|
|
200
|
+
e[2] * o,
|
|
201
|
+
Math.cos(a)
|
|
202
|
+
];
|
|
203
|
+
}, C = (e, n) => {
|
|
204
|
+
if (!Array.isArray(n) || n.length !== e) throw new t("state", "BodyDriver skeleton parents must be an array as long as names" + r);
|
|
205
|
+
return n.forEach((n, i) => {
|
|
206
|
+
if (!Number.isInteger(n) || n < -1 || n >= e || n === i) throw new t("state", "BodyDriver skeleton parents must each be -1 or another bone index" + r);
|
|
207
|
+
}), n.map((i, a) => {
|
|
208
|
+
let o = 0;
|
|
209
|
+
for (let i = n[a]; i >= 0; i = n[i]) if (++o >= e) throw new t("state", "BodyDriver skeleton parents form a cycle; every chain must reach a root" + r);
|
|
210
|
+
return o;
|
|
211
|
+
});
|
|
212
|
+
}, w = Array.from({ length: 33 }, (e, t) => t === 0 ? 0 : t <= 6 ? t <= 3 ? t + 3 : t - 3 : t % 2 ? t + 1 : t - 1), T = class {
|
|
213
|
+
constructor(n, i = {}) {
|
|
214
|
+
this.skeleton = n, this.bound = [], this.restWorld = [], this.lastGood = -Infinity, this.current = {
|
|
215
|
+
tracked: !1,
|
|
216
|
+
driven: 0,
|
|
217
|
+
time: 0
|
|
218
|
+
}, this.disposed = !1;
|
|
219
|
+
let { parts: a = ["pose", "hands"], minVisibility: s = .5, minScore: f = .5, hold: p = .5, smoothing: m = {
|
|
220
|
+
minCutoff: 1.5,
|
|
221
|
+
beta: .3
|
|
222
|
+
}, mirror: g = !1, aspect: v = 1, bones: y, limits: b = {} } = i;
|
|
223
|
+
if (!(s >= 0 && s <= 1) || !(f >= 0 && f <= 1)) throw RangeError("Body minVisibility and minScore must be in 0..1" + r);
|
|
224
|
+
if (!(p >= 0)) throw RangeError("Body hold must be seconds >= 0" + r);
|
|
225
|
+
for (let e of Object.values(b)) if (!(e >= 0 && e <= Math.PI)) throw RangeError("Body limits must be radians in 0..pi" + r);
|
|
226
|
+
if (!(v > 0) || !Number.isFinite(v)) throw RangeError("Body aspect must be a finite width / height above 0" + r);
|
|
227
|
+
if (!n || !Array.isArray(n.names) || typeof n.setPose != "function") throw TypeError("BodyDriver needs a skeleton with names, parents, getPose and setPose" + r);
|
|
228
|
+
m && new e(m);
|
|
229
|
+
let x = C(n.names.length, n.parents);
|
|
230
|
+
this.options = {
|
|
231
|
+
parts: a,
|
|
232
|
+
minVisibility: s,
|
|
233
|
+
minScore: f,
|
|
234
|
+
hold: p,
|
|
235
|
+
smoothing: m,
|
|
236
|
+
mirror: g,
|
|
237
|
+
aspect: v
|
|
238
|
+
};
|
|
239
|
+
let S = new Map(n.names.map((e, t) => [e.toLowerCase(), t])), w = (e) => {
|
|
240
|
+
if (e === void 0) return -1;
|
|
241
|
+
if (y instanceof Map) return y.get(e) ?? -1;
|
|
242
|
+
let t = y?.[e] ?? e;
|
|
243
|
+
return S.get(t.toLowerCase()) ?? -1;
|
|
244
|
+
}, T = [], E = n.names.map((e, t) => t).sort((e, t) => x[e] - x[t]);
|
|
245
|
+
for (let e of E) {
|
|
246
|
+
let t = n.getPose(e), r = n.parents[e], i = [
|
|
247
|
+
t.translation[0],
|
|
248
|
+
t.translation[1],
|
|
249
|
+
t.translation[2]
|
|
250
|
+
], a = [
|
|
251
|
+
t.rotation[0],
|
|
252
|
+
t.rotation[1],
|
|
253
|
+
t.rotation[2],
|
|
254
|
+
t.rotation[3]
|
|
255
|
+
];
|
|
256
|
+
if (r < 0) {
|
|
257
|
+
this.restWorld[e] = a, T[e] = i;
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
let o = this.restWorld[r], s = T[r], c = _(o, i);
|
|
261
|
+
this.restWorld[e] = h(o, a), T[e] = [
|
|
262
|
+
s[0] + c[0],
|
|
263
|
+
s[1] + c[1],
|
|
264
|
+
s[2] + c[2]
|
|
265
|
+
];
|
|
266
|
+
}
|
|
267
|
+
let D = (t, r, i) => {
|
|
268
|
+
let a = w(t.bone);
|
|
269
|
+
if (a < 0) return;
|
|
270
|
+
let o = w(t.tip), s = n.parents[a], c = o >= 0 ? l(T[o], T[a]) : s >= 0 ? l(T[a], T[s]) : null;
|
|
271
|
+
if (!c || u(c) < 1e-9) return;
|
|
272
|
+
let f;
|
|
273
|
+
if (t.side) {
|
|
274
|
+
let e = w(t.side.tipFrom), n = w(t.side.tipTo);
|
|
275
|
+
if (e < 0 || n < 0) return;
|
|
276
|
+
f = l(T[n], T[e]);
|
|
277
|
+
}
|
|
278
|
+
let p = n.getPose(a).rotation;
|
|
279
|
+
this.bound.push({
|
|
280
|
+
target: t,
|
|
281
|
+
part: r,
|
|
282
|
+
hand: i,
|
|
283
|
+
index: a,
|
|
284
|
+
parent: s,
|
|
285
|
+
restDir: d(c),
|
|
286
|
+
restSide: f,
|
|
287
|
+
restWorld: this.restWorld[a],
|
|
288
|
+
restLocal: [
|
|
289
|
+
p[0],
|
|
290
|
+
p[1],
|
|
291
|
+
p[2],
|
|
292
|
+
p[3]
|
|
293
|
+
],
|
|
294
|
+
limit: b[t.bone] ?? t.limit ?? Math.PI,
|
|
295
|
+
filters: m ? Array.from({ length: 6 }, () => new e(m)) : []
|
|
296
|
+
});
|
|
297
|
+
};
|
|
298
|
+
if (a.includes("pose")) for (let e of o) D(e, "pose");
|
|
299
|
+
if (a.includes("hands")) for (let e of ["left", "right"]) for (let t of c(e)) D(t, "hands", e);
|
|
300
|
+
if (!this.bound.length) throw new t("state", "No humanoid bone of the targets is in the skeleton; pass a bones map" + r);
|
|
301
|
+
this.bound.sort((e, t) => x[e.index] - x[t.index]);
|
|
302
|
+
}
|
|
303
|
+
get bones() {
|
|
304
|
+
return this.bound.map((e) => this.skeleton.names[e.index]);
|
|
305
|
+
}
|
|
306
|
+
get state() {
|
|
307
|
+
return this.current;
|
|
308
|
+
}
|
|
309
|
+
points(e, t, n) {
|
|
310
|
+
let { minScore: r, mirror: i } = this.options, a = i ? n === "left" ? "right" : "left" : n, o = t === "pose" ? e.poses[0] : e.hands.find((e) => e.handedness === a);
|
|
311
|
+
if (!(!o || (o.score ?? 1) < r)) return o.worldLandmarks ? {
|
|
312
|
+
pts: o.worldLandmarks,
|
|
313
|
+
world: !0
|
|
314
|
+
} : {
|
|
315
|
+
pts: o.landmarks,
|
|
316
|
+
world: !1
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
point(e, t, n, r) {
|
|
320
|
+
let { mirror: i, aspect: a, minVisibility: o } = this.options, s = typeof t == "number" ? [t] : t, c = 0, l = 0, u = 0;
|
|
321
|
+
for (let t of s) {
|
|
322
|
+
let s = e[i && n ? w[t] : t];
|
|
323
|
+
if (!s || (s.visibility ?? 1) < o || (s.presence ?? 1) < o || !Number.isFinite(s.x) || !Number.isFinite(s.y) || !Number.isFinite(s.z)) return null;
|
|
324
|
+
let d = r ? 1 : a;
|
|
325
|
+
c += (i ? -s.x : s.x) * d, l -= s.y, u -= s.z * d;
|
|
326
|
+
}
|
|
327
|
+
return [
|
|
328
|
+
c / s.length,
|
|
329
|
+
l / s.length,
|
|
330
|
+
u / s.length
|
|
331
|
+
];
|
|
332
|
+
}
|
|
333
|
+
directions(e, t) {
|
|
334
|
+
let n = this.points(t, e.part, e.hand);
|
|
335
|
+
if (!n) return null;
|
|
336
|
+
let { pts: r, world: i } = n, a = e.part === "pose", o = e.target, s = this.point(r, o.from, a, i), c = this.point(r, o.to, a, i);
|
|
337
|
+
if (!s || !c || u(l(c, s)) < 1e-9) return null;
|
|
338
|
+
let d;
|
|
339
|
+
if (o.side) {
|
|
340
|
+
let e = this.point(r, o.side.from, a, i), t = this.point(r, o.side.to, a, i);
|
|
341
|
+
if (!e || !t) return null;
|
|
342
|
+
d = l(t, e);
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
dir: l(c, s),
|
|
346
|
+
side: d
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
calibrate(e) {
|
|
350
|
+
this.check();
|
|
351
|
+
let n = 0;
|
|
352
|
+
for (let t of this.bound) {
|
|
353
|
+
let r = this.directions(t, e);
|
|
354
|
+
t.calibrated = r ? {
|
|
355
|
+
dir: d(r.dir),
|
|
356
|
+
side: r.side && d(r.side)
|
|
357
|
+
} : void 0, r && n++;
|
|
358
|
+
for (let e of t.filters) e.reset();
|
|
359
|
+
}
|
|
360
|
+
if (!n) throw new t("state", "Calibrate on a frame where the body or hands are visible; pass a frame with the person in view" + r);
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
363
|
+
check() {
|
|
364
|
+
if (this.disposed) throw new t("disposed", "The body driver is disposed; create a new BodyDriver" + r);
|
|
365
|
+
}
|
|
366
|
+
update(e, t = e.time) {
|
|
367
|
+
this.check();
|
|
368
|
+
let { hold: n, smoothing: r } = this.options, i = t / 1e3, a = this.skeleton, o = /* @__PURE__ */ new Map(), s = (e) => {
|
|
369
|
+
if (e < 0) return [
|
|
370
|
+
0,
|
|
371
|
+
0,
|
|
372
|
+
0,
|
|
373
|
+
1
|
|
374
|
+
];
|
|
375
|
+
let t = o.get(e);
|
|
376
|
+
if (t) return t;
|
|
377
|
+
let n = a.getPose(e).rotation, r = h(s(a.parents[e]), [
|
|
378
|
+
n[0],
|
|
379
|
+
n[1],
|
|
380
|
+
n[2],
|
|
381
|
+
n[3]
|
|
382
|
+
]);
|
|
383
|
+
return o.set(e, r), r;
|
|
384
|
+
}, c = 0;
|
|
385
|
+
for (let t of this.bound) {
|
|
386
|
+
let n = this.directions(t, e);
|
|
387
|
+
if (!n) continue;
|
|
388
|
+
let { dir: l, side: u } = n;
|
|
389
|
+
if (r) {
|
|
390
|
+
let e = t.filters;
|
|
391
|
+
l = l.map((t, n) => e[n].filter(t, i)), u &&= u.map((t, n) => e[3 + n].filter(t, i));
|
|
392
|
+
}
|
|
393
|
+
let d = t.calibrated ?? {
|
|
394
|
+
dir: t.restDir,
|
|
395
|
+
side: t.restSide
|
|
396
|
+
}, f = b(d.dir, l, d.side ?? t.restSide, u), p = h(t.restLocal, S(h(g(t.restLocal), h(g(s(t.parent)), h(f, t.restWorld))), t.limit)), m = Math.hypot(...p);
|
|
397
|
+
if (Number.isFinite(m) && m !== 0) {
|
|
398
|
+
a.setPose(t.index, { rotation: p.map((e) => e / m) }), o.set(t.index, h(s(t.parent), p));
|
|
399
|
+
for (let [e] of o) e !== t.index && this.isBelow(e, t.index) && o.delete(e);
|
|
400
|
+
c++;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (c) this.lastGood = i;
|
|
404
|
+
else if (i - this.lastGood > n) {
|
|
405
|
+
for (let e of this.bound) a.setPose(e.index, { rotation: [...e.restLocal] });
|
|
406
|
+
return this.current = {
|
|
407
|
+
tracked: !1,
|
|
408
|
+
driven: 0,
|
|
409
|
+
time: t
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
return this.current = {
|
|
413
|
+
tracked: c > 0 || this.current.tracked,
|
|
414
|
+
driven: c,
|
|
415
|
+
time: t
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
isBelow(e, t) {
|
|
419
|
+
let n = this.skeleton.parents;
|
|
420
|
+
for (let r = n[e], i = 0; r >= 0 && i < n.length; r = n[r], i++) if (r === t) return !0;
|
|
421
|
+
return !1;
|
|
422
|
+
}
|
|
423
|
+
async drive(e, { signal: t, onUpdate: n } = {}) {
|
|
424
|
+
t?.throwIfAborted();
|
|
425
|
+
let r = () => e.dispose();
|
|
426
|
+
t?.addEventListener("abort", r, { once: !0 });
|
|
427
|
+
try {
|
|
428
|
+
for await (let t of e) {
|
|
429
|
+
if (this.disposed) break;
|
|
430
|
+
let e = this.update(t);
|
|
431
|
+
n?.(e);
|
|
432
|
+
}
|
|
433
|
+
} finally {
|
|
434
|
+
t?.removeEventListener("abort", r);
|
|
435
|
+
}
|
|
436
|
+
t?.throwIfAborted();
|
|
437
|
+
}
|
|
438
|
+
dispose() {
|
|
439
|
+
this.disposed = !0;
|
|
440
|
+
}
|
|
441
|
+
[Symbol.dispose]() {
|
|
442
|
+
this.dispose();
|
|
443
|
+
}
|
|
444
|
+
};
|
|
445
|
+
//#endregion
|
|
446
|
+
export { T as BodyDriver, o as POSE_TARGETS, c as handTargets };
|
package/dist/face.d.ts
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { type FaceTrack, type Landmark, type LandmarkFrame, type LandmarkSource, type OneEuroOptions } from './landmarks.js';
|
|
2
|
+
/** The 52 ARKit face blendshape names (Apple, ARFaceAnchor.BlendShapeLocation), the names face rigs search for. */
|
|
3
|
+
export declare const ARKIT_BLENDSHAPES: readonly ['browDownLeft', 'browDownRight', 'browInnerUp', 'browOuterUpLeft', 'browOuterUpRight', 'cheekPuff', 'cheekSquintLeft', 'cheekSquintRight', 'eyeBlinkLeft', 'eyeBlinkRight', 'eyeLookDownLeft', 'eyeLookDownRight', 'eyeLookInLeft', 'eyeLookInRight', 'eyeLookOutLeft', 'eyeLookOutRight', 'eyeLookUpLeft', 'eyeLookUpRight', 'eyeSquintLeft', 'eyeSquintRight', 'eyeWideLeft', 'eyeWideRight', 'jawForward', 'jawLeft', 'jawOpen', 'jawRight', 'mouthClose', 'mouthDimpleLeft', 'mouthDimpleRight', 'mouthFrownLeft', 'mouthFrownRight', 'mouthFunnel', 'mouthLeft', 'mouthLowerDownLeft', 'mouthLowerDownRight', 'mouthPressLeft', 'mouthPressRight', 'mouthPucker', 'mouthRight', 'mouthRollLower', 'mouthRollUpper', 'mouthShrugLower', 'mouthShrugUpper', 'mouthSmileLeft', 'mouthSmileRight', 'mouthStretchLeft', 'mouthStretchRight', 'mouthUpperUpLeft', 'mouthUpperUpRight', 'noseSneerLeft', 'noseSneerRight', 'tongueOut'];
|
|
4
|
+
/** One ARKit blendshape name. */
|
|
5
|
+
export type ARKitBlendshape = (typeof ARKIT_BLENDSHAPES)[number];
|
|
6
|
+
/** The shapes {@link faceWeightsFromLandmarks} measures from points alone; a detector's scores give all 52. */
|
|
7
|
+
export declare const LANDMARK_BLENDSHAPES: readonly ARKitBlendshape[];
|
|
8
|
+
/** A mesh primitive with named morph targets; a glTF model's primitives fit. */
|
|
9
|
+
export interface MorphPrimitive {
|
|
10
|
+
readonly targetNames: readonly string[];
|
|
11
|
+
readonly weights: {
|
|
12
|
+
[index: number]: number;
|
|
13
|
+
readonly length: number;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** Something holding morph primitives; a `PBRModel` from `@margoengine/gltf` fits. */
|
|
17
|
+
export interface MorphHost {
|
|
18
|
+
readonly primitives: readonly MorphPrimitive[];
|
|
19
|
+
}
|
|
20
|
+
/** The raw face measures, all scale-free. */
|
|
21
|
+
export interface FaceMeasures {
|
|
22
|
+
/** Eye aspect ratio (Soukupova and Cech 2016) per eye. */
|
|
23
|
+
eyeLeft: number;
|
|
24
|
+
eyeRight: number;
|
|
25
|
+
/** Inner lip gap over face height. */
|
|
26
|
+
mouthOpen: number;
|
|
27
|
+
/** Mouth corner distance over outer-eye distance. */
|
|
28
|
+
mouthWidth: number;
|
|
29
|
+
/** Corner height above the upper lip's middle over face height, per side (the jaw does not move it). */
|
|
30
|
+
cornerLeft: number;
|
|
31
|
+
cornerRight: number;
|
|
32
|
+
/** Brow height above the eye top over face height. */
|
|
33
|
+
browInnerLeft: number;
|
|
34
|
+
browInnerRight: number;
|
|
35
|
+
browOuterLeft: number;
|
|
36
|
+
browOuterRight: number;
|
|
37
|
+
}
|
|
38
|
+
/** Typical neutral measures, used until {@link FaceDriver.calibrate}. */
|
|
39
|
+
export declare const NEUTRAL_FACE: Readonly<FaceMeasures>;
|
|
40
|
+
/**
|
|
41
|
+
* Scale-free measures of a face mesh. `aspect` is the frame's width over height (normalised x and y have different
|
|
42
|
+
* units unless it is given). @throws RangeError for fewer than 468 points.
|
|
43
|
+
*/
|
|
44
|
+
export declare function measureFace(landmarks: readonly Landmark[], aspect?: number): FaceMeasures;
|
|
45
|
+
/**
|
|
46
|
+
* ARKit weights 0..1 for {@link LANDMARK_BLENDSHAPES} from a face mesh, against a neutral face (default
|
|
47
|
+
* {@link NEUTRAL_FACE}; pass a calibrated one for a real person). Each weight is the change of one measure from neutral
|
|
48
|
+
* over a typical full range (the eye closes at 35% of its open aspect ratio, the jaw opens 15% of the face height).
|
|
49
|
+
*/
|
|
50
|
+
export declare function faceWeightsFromLandmarks(landmarks: readonly Landmark[], neutral?: Readonly<FaceMeasures>, aspect?: number): Record<string, number>;
|
|
51
|
+
type Quat = [number, number, number, number];
|
|
52
|
+
/**
|
|
53
|
+
* Head rotation [x, y, z, w] of a face mesh, in a y-up frame where an unturned face looks along +z (toward the camera)
|
|
54
|
+
* with its left at +x: x from the right to the left outer eye corner, y from the chin to the forehead.
|
|
55
|
+
*/
|
|
56
|
+
export declare function headRotation(landmarks: readonly Landmark[], aspect?: number): Quat;
|
|
57
|
+
/** {@link FaceDriver} options; every one is optional. */
|
|
58
|
+
export interface FaceDriverOptions {
|
|
59
|
+
/** ARKit name to the model's morph target name, for rigs that name shapes their own way. Default the ARKit names. */
|
|
60
|
+
names?: Partial<Record<ARKitBlendshape, string>>;
|
|
61
|
+
/** Where weights come from: detector scores, landmark measures, or scores when present else landmarks. Default 'auto'. */
|
|
62
|
+
source?: 'auto' | 'blendshapes' | 'landmarks';
|
|
63
|
+
/** Faces below this detection score are not trusted and the last good value is held. Default 0.5. */
|
|
64
|
+
minScore?: number;
|
|
65
|
+
/** Seconds the last good value is held through lost tracking before easing back to neutral. Default 0.5. */
|
|
66
|
+
hold?: number;
|
|
67
|
+
/** One-euro filter on weights and head rotation, or false for raw values. Default `{ minCutoff: 1.5, beta: 0.5 }`. */
|
|
68
|
+
smoothing?: OneEuroOptions | false;
|
|
69
|
+
/** Swap left and right, for a mirrored (selfie) picture. Default false. */
|
|
70
|
+
mirror?: boolean;
|
|
71
|
+
/** Frame width over height, for landmark measures. Default 1. */
|
|
72
|
+
aspect?: number;
|
|
73
|
+
}
|
|
74
|
+
/** The driver's latest result. */
|
|
75
|
+
export interface FaceState {
|
|
76
|
+
/** ARKit name to weight 0..1 (detector scores give all 52, landmarks {@link LANDMARK_BLENDSHAPES}). */
|
|
77
|
+
readonly weights: Readonly<Record<string, number>>;
|
|
78
|
+
/** Head rotation [x, y, z, w] relative to the calibrated face (identity looks at the camera). */
|
|
79
|
+
readonly rotation: Readonly<Quat>;
|
|
80
|
+
/** False once tracking has been lost for longer than `hold`. */
|
|
81
|
+
readonly tracked: boolean;
|
|
82
|
+
/** Frame time in ms. */
|
|
83
|
+
readonly time: number;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Drives ARKit-named morph targets from face frames: detector blendshape scores, or landmark measures (eye aspect
|
|
87
|
+
* ratio, jaw opening, mouth width and corners, brow height), smoothed by the one-euro filter, gated on the detection
|
|
88
|
+
* score and calibrated on a neutral face; also gives the head rotation.
|
|
89
|
+
*/
|
|
90
|
+
export declare class FaceDriver implements Disposable {
|
|
91
|
+
private readonly options;
|
|
92
|
+
private readonly lookup;
|
|
93
|
+
private readonly maps;
|
|
94
|
+
private readonly filters;
|
|
95
|
+
private neutral;
|
|
96
|
+
private neutralScores;
|
|
97
|
+
private neutralRotation;
|
|
98
|
+
private lastGood;
|
|
99
|
+
private current;
|
|
100
|
+
private disposed;
|
|
101
|
+
/** @throws RangeError for an unknown source, a negative hold, a minScore outside 0..1 or an aspect not above 0. */
|
|
102
|
+
constructor(options?: FaceDriverOptions);
|
|
103
|
+
/** The latest result. */
|
|
104
|
+
get state(): FaceState;
|
|
105
|
+
private track;
|
|
106
|
+
private raw;
|
|
107
|
+
/**
|
|
108
|
+
* Take this face (or the frame's first face) as the neutral pose: relaxed, eyes open, looking at the camera.
|
|
109
|
+
* @throws MargoError `state` without a face.
|
|
110
|
+
*/
|
|
111
|
+
calibrate(input: LandmarkFrame | FaceTrack): this;
|
|
112
|
+
/**
|
|
113
|
+
* Read one frame (its first face) or one face. `time` (ms) defaults to the frame's. No face, or a score under
|
|
114
|
+
* `minScore`, holds the last good result for `hold` seconds and then eases to neutral.
|
|
115
|
+
* @throws MargoError `disposed` after dispose.
|
|
116
|
+
*/
|
|
117
|
+
update(input: LandmarkFrame | FaceTrack | null | undefined, time?: number): FaceState;
|
|
118
|
+
private smooth;
|
|
119
|
+
/**
|
|
120
|
+
* Write the latest weights to every morph target whose name (case-insensitive, through `names`) is an ARKit shape;
|
|
121
|
+
* targets of shapes this frame did not measure are left as they are.
|
|
122
|
+
*/
|
|
123
|
+
apply(target: MorphHost | readonly MorphHost[]): this;
|
|
124
|
+
/**
|
|
125
|
+
* Update from every frame of `source` and apply to `target` until the source ends or `signal` aborts.
|
|
126
|
+
* @param onUpdate Called after each frame is applied, for the head bone or a UI.
|
|
127
|
+
*/
|
|
128
|
+
drive(source: LandmarkSource, target: MorphHost | readonly MorphHost[], { signal, onUpdate }?: {
|
|
129
|
+
signal?: AbortSignal;
|
|
130
|
+
onUpdate?: (state: FaceState) => void;
|
|
131
|
+
}): Promise<void>;
|
|
132
|
+
/** Release the driver; later calls throw. */
|
|
133
|
+
dispose(): void;
|
|
134
|
+
[Symbol.dispose](): void;
|
|
135
|
+
}
|
|
136
|
+
export {};
|