three-mediapipe-rig 0.1.2 → 0.1.4

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/dist/meshcap.js CHANGED
@@ -1,316 +1,572 @@
1
- import { inflate as ht, deflate as ft } from "fflate";
2
- import { F as N, c as mt } from "./face-tracker-utils-xt9__vBF.js";
3
- import { MeshPhysicalNodeMaterial as pt, Vector3 as W } from "three/webgpu";
4
- import { instancedArray as B, texture as G, uniform as O, select as ut, attribute as gt, varying as Ut, vec3 as wt, float as yt } from "three/tsl";
5
- const J = 1296253264, _ = 1;
6
- async function Rt(m) {
7
- if (typeof m == "string") {
8
- const o = await (await fetch(m)).arrayBuffer();
9
- return K(o);
1
+ import { MeshPhysicalNodeMaterial as Lt, Vector3 as _, Matrix4 as Bt } from "three/webgpu";
2
+ import { deflate as St, inflate as Tt } from "fflate";
3
+ import { c as Vt, F as $ } from "./face-tracker-utils-xt9__vBF.js";
4
+ import { instancedArray as K, texture as mt, uniform as tt, attribute as zt, varying as Ft, vec3 as Et, float as Pt, select as Ot, positionLocal as jt } from "three/tsl";
5
+ import { TextureLoader as pt, Texture as Nt } from "three";
6
+ const wt = 1296253264, Wt = 3;
7
+ async function Dt(o) {
8
+ if (o instanceof ArrayBuffer)
9
+ return o;
10
+ if (o instanceof File || o instanceof Blob)
11
+ return o.arrayBuffer();
12
+ if (typeof o == "string")
13
+ return (await fetch(o)).arrayBuffer();
14
+ throw new Error(`Unsupported audio source type: ${typeof o}`);
15
+ }
16
+ async function Xt(o, s) {
17
+ const a = new AudioContext(), i = await Dt(o), n = await a.decodeAudioData(i);
18
+ for (const t of s) {
19
+ if (!t.audioSprite)
20
+ continue;
21
+ const g = Math.floor(t.audioSprite.start * n.sampleRate), m = Math.floor(t.duration * n.sampleRate), l = a.createBuffer(
22
+ n.numberOfChannels,
23
+ m,
24
+ n.sampleRate
25
+ );
26
+ for (let r = 0; r < n.numberOfChannels; r++) {
27
+ const e = n.getChannelData(r).subarray(g, g + m);
28
+ l.getChannelData(r).set(e);
29
+ }
30
+ const A = Ht(l), M = new Blob([A], { type: "audio/wav" }), x = URL.createObjectURL(M), f = new Audio(x);
31
+ t.audioSprite.domElement = f, console.log("Assigned audio to clip: ", t.name, " at ", t.audioSprite.start, " for ", t.duration, " seconds");
32
+ }
33
+ return a.close(), {
34
+ blob: new Blob([i], { type: "audio/wav" }),
35
+ sprites: s.map((t) => t.audioSprite ? [t.audioSprite.start, t.duration] : void 0)
36
+ };
37
+ }
38
+ function Ht(o) {
39
+ const s = o.numberOfChannels, a = o.sampleRate, i = o.length * s * 2, n = new ArrayBuffer(44 + i), t = new DataView(n), g = (l, A) => [...A].forEach((M, x) => t.setUint8(l + x, M.charCodeAt(0)));
40
+ g(0, "RIFF"), t.setUint32(4, 36 + i, !0), g(8, "WAVE"), g(12, "fmt "), t.setUint32(16, 16, !0), t.setUint16(20, 1, !0), t.setUint16(22, s, !0), t.setUint32(24, a, !0), t.setUint32(28, a * s * 2, !0), t.setUint16(32, s * 2, !0), t.setUint16(34, 16, !0), g(36, "data"), t.setUint32(40, i, !0);
41
+ let m = 44;
42
+ for (let l = 0; l < o.length; l++)
43
+ for (let A = 0; A < s; A++) {
44
+ const M = Math.max(-1, Math.min(1, o.getChannelData(A)[l]));
45
+ t.setInt16(m, M * 32767, !0), m += 2;
46
+ }
47
+ return n;
48
+ }
49
+ const ht = /* @__PURE__ */ new WeakMap();
50
+ function Yt(o, s) {
51
+ const a = ht.get(o) || new AudioContext();
52
+ ht.set(o, a);
53
+ const i = a.createGain();
54
+ i.connect(a.destination), i.gain.value = 1;
55
+ let n;
56
+ return {
57
+ stopCurrent() {
58
+ n && (n.stop(), n = void 0);
59
+ },
60
+ setVolume(t) {
61
+ i.gain.value = t;
62
+ },
63
+ /**
64
+ * @param clipIndex Index of the clip to play
65
+ * @returns The audio buffer source node that is playing the audio.
66
+ */
67
+ playSprite(t) {
68
+ this.stopCurrent();
69
+ const g = s[t];
70
+ if (!g.audioSprite) return;
71
+ const m = a.createBufferSource();
72
+ return m.buffer = o, m.connect(i), m.start(0, g.audioSprite.start, g.duration), n = m, m;
73
+ }
74
+ };
75
+ }
76
+ function qt(o, s, a, i, n) {
77
+ const t = !i;
78
+ i ?? (i = new Lt()), t && (a.material = i), a.geometry.hasAttribute("landmarkIndex") || Vt(a);
79
+ const g = [], m = [], l = [];
80
+ let A = 0, M = 0;
81
+ for (let y = 0; y < s.length; y++) {
82
+ const v = s[y];
83
+ g.push(A, M, v.frames.length, v.fps), A += v.frames.length, M += v.frames.length * $, m.push(v.aspectRatio), l.push(y == 0 ? 0 : l[y - 1] + s[y - 1].frames.length);
84
+ }
85
+ const x = K(new Float32Array(m), "float"), f = K(new Uint32Array(g), "uvec4"), r = new Float32Array(s.flatMap((y) => y.frames).reduce((y, v) => (y.push(v.frameUV.u, v.frameUV.v, v.frameUV.w, v.frameUV.h), y), [])), e = K(r, "vec4"), c = new Float32Array(s.flatMap((y) => y.frames.flatMap((v) => [v.cropUV.u, v.cropUV.v, v.cropUV.w, v.cropUV.h]))), d = K(c, "vec4"), u = new Float32Array(s.flatMap((y) => y.landmarks.flatMap((v) => v.flatMap((z) => [z.x, z.y, z.z])))), w = K(u, "vec3"), p = mt(o), h = tt(0), U = tt(0), I = tt(!0), b = x.element(h), C = K(new Uint32Array(l), "uint"), k = U, P = C.element(h).add(k), N = e.element(f.element(h).x.add(k)), L = f.element(h).y.add(k.mul($)), W = zt("landmarkIndex", "uint").toUint(), G = L.add(W), D = w.element(G), X = d.element(P), S = D.xy.sub(X.xy).div(X.zw), B = Ft(N.xy.add(S.mul(N.zw))), F = mt(p, B);
86
+ i.colorNode = F;
87
+ const E = w.element(L.add(234)).xy, T = w.element(L.add(93)).xy, yt = w.element(L.add(454)).xy, lt = w.element(L.add(323)).xy, ft = E.sub(T).div(2).add(T), vt = yt.sub(lt).div(2).add(lt).sub(ft).div(2).add(ft), R = a.geometry.attributes.position, et = 116, nt = 346, xt = new _().subVectors(
88
+ new _(R.getX(et), R.getY(et), R.getZ(et)),
89
+ new _(R.getX(nt), R.getY(nt), R.getZ(nt))
90
+ ).lengthSq(), ot = 209, at = 429, At = tt(new _().addVectors(
91
+ new _(R.getX(ot), R.getY(ot), R.getZ(ot)),
92
+ new _(R.getX(at), R.getY(at), R.getZ(at))
93
+ ).multiplyScalar(0.5)), Mt = tt(xt), bt = w.element(L.add(nt)).sub(w.element(L.add(et))).lengthSq(), Ct = Mt.div(bt).sqrt(), It = D.sub(vt).xzy.mul(Et(1, -1, Pt(1).div(b))).mul(Ct).add(At);
94
+ i.positionNode = Ot(W.toUint().lessThanEqual($), It, jt);
95
+ let J = 0, O = 0, H, Y, rt = s[0].frames, st = !1, q = n ? Yt(n, s) : void 0, dt;
96
+ const V = {
97
+ muted: !1,
98
+ clips: s,
99
+ atlasTexture: o,
100
+ material: i,
101
+ getLastKnownFaceTransform: () => dt,
102
+ goto(y, v = !0, z, Rt = !0) {
103
+ var ut;
104
+ let j = -1;
105
+ if (typeof y == "number")
106
+ j = y;
107
+ else if (j = s.findIndex((kt) => kt.name === y), j === -1)
108
+ throw new Error(`Clip ${y.toString()} not found`);
109
+ I.value = v, U.value = 0, h.value = j;
110
+ const Z = s[j];
111
+ H = Z, rt = Z.frames, J = Z.duration, O = 0, Y = z, st = !0, q && (q.stopCurrent(), Rt && !V.muted && Z.audioSprite && (q.playSprite(j), (ut = V.playClipAudioHook) == null || ut.call(V, j, Z.audioSprite.start, Z.duration)));
112
+ },
113
+ update(y) {
114
+ var v;
115
+ if (st) {
116
+ if (O += y, U.value = Zt(rt, O, U.value), dt = rt[U.value].transformMatrix, I.value)
117
+ O >= J && (O -= J, U.value = 0, V.muted || (q == null || q.playSprite(h.value), H != null && H.audioSprite && ((v = V.playClipAudioHook) == null || v.call(V, h.value, H.audioSprite.start, H.duration))), Y == null || Y(O));
118
+ else if (O >= J) {
119
+ const z = Y;
120
+ Y = void 0, z == null || z(J);
121
+ }
122
+ }
123
+ },
124
+ dispose() {
125
+ o.dispose(), i.dispose();
126
+ },
127
+ gotoAndLoop(y, v) {
128
+ this.goto(y, !0, v);
129
+ },
130
+ gotoAndPlay(y, v) {
131
+ this.goto(y, !1, v);
132
+ },
133
+ gotoAndStop(y, v = 0) {
134
+ this.goto(y, !1, void 0, !1), U.value = v, st = !1;
135
+ }
136
+ };
137
+ return V;
138
+ }
139
+ function Zt(o, s, a) {
140
+ for (; a < o.length - 1 && o[a + 1].startTime <= s; )
141
+ a++;
142
+ return a;
143
+ }
144
+ async function _t(o, s = !0) {
145
+ const a = await Ut(o, s);
146
+ return new Promise((i, n) => {
147
+ St(new Uint8Array(a), { level: 9 }, (t, g) => {
148
+ if (t) return n(t);
149
+ const m = new Blob([g], { type: "application/octet-stream" });
150
+ i(m);
151
+ });
152
+ });
153
+ }
154
+ async function Ut(o, s = !0) {
155
+ const a = new TextEncoder(), i = o.clips.length, n = o.clips.map((c) => a.encode(c.name)), t = 9, g = n.reduce((c, d, u) => {
156
+ const w = o.clips[u].frames.length, p = 1 + d.byteLength, h = 2, U = 1, I = 1, b = 1, C = 16;
157
+ let k = $ * 6;
158
+ return c + p + h + U + I + b + (C + k) * w;
159
+ }, 0), m = o.clips.reduce((c, d) => c + 2 + 2, 0), l = o.clips.reduce((c, d) => c + d.frames.length * 2, 0), A = o.clips.reduce((c, d) => c + d.frames.length * 64, 0), x = t + g + m + l + 3 & -4, f = new ArrayBuffer(x + A), r = new DataView(f);
160
+ let e = 0;
161
+ r.setUint32(e, wt), e += 4, r.setUint8(e, Wt), e += 1, r.setUint8(e, o.clips.length), e += 1, r.setUint16(e, o.atlasSize), e += 2, r.setUint8(e, o.padding), e += 1;
162
+ for (let c = 0; c < i; c++) {
163
+ r.setUint8(e, n[c].byteLength), e += 1, new Uint8Array(f, e, n[c].byteLength).set(n[c]), e += n[c].byteLength, r.setUint16(e, o.clips[c].frames.length), e += 2, r.setUint8(e, o.clips[c].fps), e += 1, r.setUint8(e, Math.round(o.clips[c].scale * 100)), e += 1, r.setUint8(e, Math.round(o.clips[c].aspectRatio * 100)), e += 1;
164
+ const d = o.clips[c];
165
+ for (let u = 0; u < d.frames.length; u++) {
166
+ const w = d.frames[u].cropUV, p = d.frames[u].frameUV, h = 1e4;
167
+ r.setUint16(e, Math.round(p.u * h)), e += 2, r.setUint16(e, Math.round(p.v * h)), e += 2, r.setUint16(e, Math.round(p.w * h)), e += 2, r.setUint16(e, Math.round(p.h * h)), e += 2, r.setUint16(e, Math.round(w.u * h)), e += 2, r.setUint16(e, Math.round(w.v * h)), e += 2, r.setUint16(e, Math.round(w.w * h)), e += 2, r.setUint16(e, Math.round(w.h * h)), e += 2;
168
+ const U = d.landmarks[u], I = u > 0 ? d.landmarks[u - 1] : null;
169
+ U.reduce((b, C) => Math.max(b, Math.abs(C.z)), 0);
170
+ for (let b = 0; b < $; b++) {
171
+ const C = Math.round(U[b].x * 1e3), k = Math.round(U[b].y * 1e3), P = Math.round(U[b].z * 1e3);
172
+ if (s)
173
+ if (I === null)
174
+ r.setUint16(e, C), e += 2, r.setUint16(e, k), e += 2, r.setInt16(e, P), e += 2;
175
+ else {
176
+ const N = Math.round(I[b].x * 1e3), L = Math.round(I[b].y * 1e3), W = Math.round(I[b].z * 1e3), G = C - N, D = k - L, X = P - W;
177
+ r.setInt16(e, G), e += 2, r.setInt16(e, D), e += 2, r.setInt16(e, X), e += 2;
178
+ }
179
+ else
180
+ r.setUint16(e, C), e += 2, r.setUint16(e, k), e += 2, r.setInt16(e, P), e += 2;
181
+ }
182
+ }
183
+ }
184
+ for (let c = 0; c < i; c++) {
185
+ const d = o.clips[c];
186
+ r.setUint16(e, Math.round(d.duration * 1e3)), e += 2, d.audioSprite ? r.setUint16(e, Math.round(d.audioSprite.start * 1e3)) : r.setUint16(e, 1), e += 2;
187
+ }
188
+ for (let c = 0; c < i; c++) {
189
+ const d = o.clips[c];
190
+ let u = 0;
191
+ for (const w of d.frames) {
192
+ const p = Math.floor((w.startTime - u) * 1e3);
193
+ r.setUint16(e, p), e += 2, u = w.startTime;
194
+ }
195
+ }
196
+ e = e + 3 & -4;
197
+ for (let c = 0; c < i; c++) {
198
+ const d = o.clips[c];
199
+ for (const u of d.frames)
200
+ u.transformMatrix ? u.transformMatrix.toArray(new Float32Array(f, e, 16)) : new Float32Array(f, e, 16).fill(0), e += 64;
201
+ }
202
+ return f;
203
+ }
204
+ async function ie(o) {
205
+ if (typeof o == "string") {
206
+ const a = await (await fetch(o)).arrayBuffer();
207
+ return it(a);
10
208
  } else
11
- return new Promise((u, o) => {
12
- const e = new FileReader();
13
- e.onload = (v) => {
14
- const x = v.target.result;
15
- u(K(x));
16
- }, e.onerror = (v) => {
17
- o(v);
18
- }, e.readAsArrayBuffer(m);
209
+ return new Promise((s, a) => {
210
+ const i = new FileReader();
211
+ i.onload = (n) => {
212
+ const t = n.target.result;
213
+ s(it(t));
214
+ }, i.onerror = (n) => {
215
+ a(n);
216
+ }, i.readAsArrayBuffer(o);
19
217
  });
20
218
  }
21
- async function K(m) {
22
- const u = await new Promise((a, r) => {
23
- ht(new Uint8Array(m), (p, y) => {
24
- p ? r(p) : a(y);
219
+ async function it(o, s = !0) {
220
+ let a = !1;
221
+ const i = s ? await new Promise((f, r) => {
222
+ Tt(new Uint8Array(o), (e, c) => {
223
+ e ? r(e) : f(c);
25
224
  });
26
- }), o = new DataView(u.buffer);
27
- let e = 0;
28
- const v = o.getUint32(e);
29
- if (e += 4, v !== J) throw new Error("Invalid file: not a MCAP file");
30
- const x = o.getUint8(e);
31
- if (e += 1, x !== _) throw new Error(`Unsupported version: ${x} != ${_}`);
32
- const h = o.getUint8(e);
33
- e += 1;
34
- const s = o.getUint16(e);
35
- e += 2;
36
- const t = o.getUint8(e);
37
- e += 1;
38
- const f = [];
39
- for (let a = 0; a < h; a++) {
40
- const r = o.getUint8(e);
41
- e += 1;
42
- const p = new Uint8Array(u.buffer, e, r), y = new TextDecoder().decode(p);
43
- e += r;
44
- const i = o.getUint8(e);
45
- e += 1;
46
- const U = o.getUint8(e);
47
- e += 1;
48
- const n = o.getUint8(e) / 100;
49
- e += 1;
50
- const c = o.getUint8(e) / 100;
51
- e += 1;
52
- const l = [], g = [], b = [];
53
- for (let M = 0; M < i; M++) {
54
- const k = o.getUint16(e) / 1e3;
55
- e += 2;
56
- const A = o.getUint16(e) / 1e3;
57
- e += 2;
58
- const R = o.getUint16(e) / 1e3;
59
- e += 2;
60
- const L = o.getUint16(e) / 1e3;
61
- e += 2, l.push({ u: k, v: A, w: R, h: L });
62
- const Y = o.getUint16(e) / 1e3;
63
- e += 2;
64
- const P = o.getUint16(e) / 1e3;
65
- e += 2;
66
- const q = o.getUint16(e) / 1e3;
67
- e += 2;
68
- const T = o.getUint16(e) / 1e3;
69
- e += 2, b.push({ u: Y, v: P, w: q, h: T });
70
- const C = [], z = M > 0 ? g[M - 1] : null;
71
- for (let V = 0; V < N; V++)
72
- if (z === null) {
73
- const F = o.getUint16(e) / 1e3;
74
- e += 2;
75
- const E = o.getUint16(e) / 1e3;
76
- e += 2;
77
- const X = o.getInt16(e) / 1e3;
78
- e += 2, C.push({ x: F, y: E, z: X });
225
+ }) : new Uint8Array(o), n = new DataView(i.buffer);
226
+ let t = 0;
227
+ const g = n.getUint32(t);
228
+ if (t += 4, g !== wt) throw new Error("Invalid file: not a MCAP file");
229
+ const m = n.getUint8(t);
230
+ t += 1;
231
+ const l = n.getUint8(t);
232
+ t += 1;
233
+ const A = n.getUint16(t);
234
+ t += 2;
235
+ const M = n.getUint8(t);
236
+ t += 1;
237
+ const x = [];
238
+ for (let f = 0; f < l; f++) {
239
+ const r = n.getUint8(t);
240
+ t += 1;
241
+ const e = new Uint8Array(i.buffer, t, r), c = new TextDecoder().decode(e);
242
+ t += r;
243
+ const d = m >= 3 ? n.getUint16(t) : n.getUint8(t);
244
+ t += m >= 3 ? 2 : 1;
245
+ const u = n.getUint8(t);
246
+ t += 1;
247
+ const w = n.getUint8(t) / 100;
248
+ t += 1;
249
+ const p = n.getUint8(t) / 100;
250
+ t += 1;
251
+ const h = [], U = [], I = [];
252
+ for (let b = 0; b < d; b++) {
253
+ const C = m < 3 ? 1e3 : 1e4, k = n.getUint16(t) / C;
254
+ t += 2;
255
+ const P = n.getUint16(t) / C;
256
+ t += 2;
257
+ const N = n.getUint16(t) / C;
258
+ t += 2;
259
+ const L = n.getUint16(t) / C;
260
+ t += 2, h.push({ u: k, v: P, w: N, h: L });
261
+ const W = n.getUint16(t) / C;
262
+ t += 2;
263
+ const G = n.getUint16(t) / C;
264
+ t += 2;
265
+ const D = n.getUint16(t) / C;
266
+ t += 2;
267
+ const X = n.getUint16(t) / C;
268
+ t += 2, I.push({ u: W, v: G, w: D, h: X });
269
+ const Q = [], S = b > 0 ? U[b - 1] : null;
270
+ for (let B = 0; B < $; B++)
271
+ if (S === null) {
272
+ const F = n.getUint16(t) / 1e3;
273
+ t += 2;
274
+ const E = n.getUint16(t) / 1e3;
275
+ t += 2;
276
+ const T = n.getInt16(t) / 1e3;
277
+ t += 2, Q.push({ x: F, y: E, z: T });
79
278
  } else {
80
- const F = o.getInt8(e) / 1e3;
81
- e += 1;
82
- const E = o.getInt8(e) / 1e3;
83
- e += 1;
84
- const X = o.getInt8(e) / 1e3;
85
- e += 1, C.push({
86
- x: z[V].x + F,
87
- y: z[V].y + E,
88
- z: z[V].z + X
89
- });
279
+ let F, E, T;
280
+ m < 3 ? (F = n.getInt8(t) / 1e3, t += 1, E = n.getInt8(t) / 1e3, t += 1, T = n.getInt8(t) / 1e3, t += 1, Q.push({
281
+ x: S[B].x + F,
282
+ y: S[B].y + E,
283
+ z: S[B].z + T
284
+ })) : (F = n.getInt16(t) / 1e3, t += 2, E = n.getInt16(t) / 1e3, t += 2, T = n.getInt16(t) / 1e3, t += 2, Q.push({
285
+ x: S[B].x + F,
286
+ y: S[B].y + E,
287
+ z: S[B].z + T
288
+ }));
90
289
  }
91
- g.push(C);
290
+ U.push(Q);
92
291
  }
93
- f.push({
94
- name: y,
95
- fps: U,
96
- scale: n,
97
- aspectRatio: c,
98
- frames: b.map((M, k) => ({
99
- cropUV: M,
100
- frameUV: l[k]
292
+ x.push({
293
+ name: c,
294
+ fps: u,
295
+ scale: w,
296
+ aspectRatio: p,
297
+ frames: I.map((b, C) => ({
298
+ cropUV: b,
299
+ frameUV: h[C],
300
+ startTime: C / u
301
+ //<-- default for versions sub 2...
101
302
  })),
102
- landmarks: g
303
+ landmarks: U,
304
+ duration: d / u
305
+ // default for versions sub 2...
103
306
  });
104
307
  }
308
+ if (m >= 2) {
309
+ for (let f = 0; f < x.length; f++) {
310
+ let r = n.getUint16(t);
311
+ t += 2, x[f].duration = r / 1e3;
312
+ let e = n.getUint16(t);
313
+ t += 2, e === 1 ? console.log("Clip " + f + " has no audio") : (e /= 1e3, x[f].audioSprite = { start: e }, a = !0, console.log("MCAP AUDIO CLIP;", e, "Duration: ", r / 1e3));
314
+ }
315
+ for (let f = 0; f < x.length; f++) {
316
+ const r = x[f];
317
+ let e = 0;
318
+ for (let c = 0; c < r.frames.length; c++) {
319
+ const d = m < 3 ? n.getUint8(t) : n.getUint16(t);
320
+ t += m < 3 ? 1 : 2;
321
+ const u = e + d / 1e3;
322
+ r.frames[c].startTime = u, e = u;
323
+ }
324
+ }
325
+ if (m >= 3) {
326
+ t = t + 3 & -4;
327
+ for (let f = 0; f < x.length; f++) {
328
+ const r = x[f];
329
+ for (let e = 0; e < r.frames.length; e++) {
330
+ const c = new Float32Array(n.buffer, t, 16);
331
+ t += 64, r.frames[e].transformMatrix = new Bt().fromArray(c);
332
+ }
333
+ }
334
+ }
335
+ }
105
336
  return {
106
- clips: f,
107
- version: x,
108
- atlasSize: s,
109
- atlasPadding: t,
110
- unpackClips: async (a) => {
111
- let r, p = !0;
112
- if (a instanceof HTMLCanvasElement)
113
- p = !1, r = a;
337
+ clips: x,
338
+ version: m,
339
+ atlasSize: A,
340
+ atlasPadding: M,
341
+ async unpackClips(f, r) {
342
+ let e, c = !0, d, u;
343
+ if (f instanceof HTMLCanvasElement)
344
+ c = !1, e = f, u = e.getContext("2d");
114
345
  else {
115
- let i = new Image();
116
- if (typeof a == "string")
117
- i.src = a;
118
- else if (a instanceof File || a instanceof Blob) {
119
- const n = URL.createObjectURL(a);
120
- i.src = n, await new Promise((c, l) => {
121
- i.onload = () => {
122
- URL.revokeObjectURL(n), c();
123
- }, i.onerror = l;
346
+ let p = new Image();
347
+ if (typeof f == "string")
348
+ p.src = f;
349
+ else if (f instanceof File || f instanceof Blob) {
350
+ const h = URL.createObjectURL(f);
351
+ p.src = h, await new Promise((U, I) => {
352
+ p.onload = () => {
353
+ URL.revokeObjectURL(h), U();
354
+ }, p.onerror = I;
124
355
  });
125
- } else a instanceof HTMLImageElement && (i = a);
126
- (!i.complete || !i.naturalWidth) && await new Promise((n, c) => {
127
- i.onload = () => n(), i.onerror = c;
128
- }), r = document.createElement("canvas"), r.width = i.width, r.height = i.height, r.getContext("2d").drawImage(i, 0, 0);
356
+ } else f instanceof HTMLImageElement && (p = f);
357
+ (!p.complete || !p.naturalWidth) && await new Promise((h, U) => {
358
+ p.onload = () => h(), p.onerror = U;
359
+ }), e = document.createElement("canvas"), e.width = p.width, e.height = p.height, u = e.getContext("2d"), u.drawImage(p, 0, 0);
129
360
  }
130
- const y = f.map((i) => ({
131
- ...i,
132
- frames: i.frames.map((U) => {
133
- const n = document.createElement("canvas");
134
- return n.width = U.frameUV.w * r.width, n.height = U.frameUV.h * r.height, n.getContext("2d").drawImage(
135
- r,
136
- U.frameUV.u * r.width,
137
- U.frameUV.v * r.height,
138
- U.frameUV.w * r.width,
139
- U.frameUV.h * r.height,
361
+ const w = x.map((p) => ({
362
+ ...p,
363
+ frames: p.frames.map((h) => {
364
+ const U = document.createElement("canvas");
365
+ return U.width = h.frameUV.w * e.width, U.height = h.frameUV.h * e.height, U.getContext("2d").drawImage(
366
+ e,
367
+ h.frameUV.u * e.width,
368
+ h.frameUV.v * e.height,
369
+ h.frameUV.w * e.width,
370
+ h.frameUV.h * e.height,
140
371
  0,
141
372
  0,
142
- n.width,
143
- n.height
373
+ U.width,
374
+ U.height
144
375
  ), {
145
- canvas: n,
146
- cropUV: U.cropUV
376
+ canvas: U,
377
+ cropUV: h.cropUV,
378
+ startTime: h.startTime
147
379
  };
148
380
  })
149
381
  }));
150
- return p && r.remove(), y;
382
+ return c && e.remove(), a && (r ? d = await Xt(r, w) : console.warn("This mcap file uses an audio atlas, but no audio atlas file was provided.")), {
383
+ clips: w,
384
+ audioAtlas: d
385
+ };
386
+ },
387
+ createMaterialHandlerOnMesh(f, r, e, c) {
388
+ return qt(r, x, f, e, c);
151
389
  }
152
390
  };
153
391
  }
154
- async function vt(m, u = !0) {
155
- const o = new TextEncoder(), e = m.clips.map((f) => o.encode(f.name)), v = 9, x = e.reduce((f, a, r) => {
156
- const p = m.clips[r].frames.length, y = 1 + a.byteLength, i = 1, U = 1, n = 1, c = 1, l = 16;
157
- let g = N * 6;
158
- return f + y + i + U + n + c + (u ? l + g + (l + N * 3) * (p - 1) : (l + g) * p);
159
- }, 0), h = new ArrayBuffer(v + x), s = new DataView(h);
160
- let t = 0;
161
- s.setUint32(t, J), t += 4, s.setUint8(t, _), t += 1, s.setUint8(t, m.clips.length), t += 1, s.setUint16(t, m.canvas.width), t += 2, s.setUint8(t, m.padding), t += 1;
162
- for (let f = 0; f < m.clips.length; f++) {
163
- s.setUint8(t, e[f].byteLength), t += 1, new Uint8Array(h, t, e[f].byteLength).set(e[f]), t += e[f].byteLength, s.setUint8(t, m.clips[f].frames.length), t += 1, s.setUint8(t, m.clips[f].fps), t += 1, s.setUint8(t, Math.round(m.clips[f].scale * 100)), t += 1, s.setUint8(t, Math.round(m.clips[f].aspectRatio * 100)), t += 1;
164
- const a = m.clips[f];
165
- for (let r = 0; r < a.frames.length; r++) {
166
- const p = a.frames[r].cropUV, y = a.frames[r].frameUV;
167
- s.setUint16(t, Math.round(y.u * 1e3)), t += 2, s.setUint16(t, Math.round(y.v * 1e3)), t += 2, s.setUint16(t, Math.round(y.w * 1e3)), t += 2, s.setUint16(t, Math.round(y.h * 1e3)), t += 2, s.setUint16(t, Math.round(p.u * 1e3)), t += 2, s.setUint16(t, Math.round(p.v * 1e3)), t += 2, s.setUint16(t, Math.round(p.w * 1e3)), t += 2, s.setUint16(t, Math.round(p.h * 1e3)), t += 2;
168
- const i = a.landmarks[r], U = r > 0 ? a.landmarks[r - 1] : null;
169
- i.reduce((n, c) => Math.max(n, Math.abs(c.z)), 0);
170
- for (let n = 0; n < N; n++) {
171
- const c = Math.round(i[n].x * 1e3), l = Math.round(i[n].y * 1e3), g = Math.round(i[n].z * 1e3);
172
- if (u)
173
- if (U === null)
174
- s.setUint16(t, c), t += 2, s.setUint16(t, l), t += 2, s.setInt16(t, g), t += 2;
175
- else {
176
- const b = Math.round(U[n].x * 1e3), M = Math.round(U[n].y * 1e3), k = Math.round(U[n].z * 1e3), A = c - b, R = l - M, L = g - k;
177
- console.log("Deltas: ", A), s.setInt8(t, A), t += 1, s.setInt8(t, R), t += 1, s.setInt8(t, L), t += 1;
178
- }
179
- else
180
- s.setUint16(t, c), t += 2, s.setUint16(t, l), t += 2, s.setInt16(t, g), t += 2;
181
- }
182
- }
183
- }
184
- return new Promise((f, a) => {
185
- ft(new Uint8Array(h), { level: 9 }, (r, p) => {
186
- if (r) return a(r);
187
- const y = new Blob([p], { type: "application/octet-stream" });
188
- f(y);
189
- });
190
- });
392
+ async function ce(o) {
393
+ const s = await Ut(o);
394
+ return it(s, !1);
191
395
  }
192
- function xt(m, u) {
193
- const o = [], e = [];
194
- let v = 0;
195
- const x = [...m].sort((h, s) => s.height - h.height);
196
- for (const h of x) {
197
- let s = !1;
198
- for (const t of o)
199
- if (h.width <= u - t.currentX && h.height <= t.height) {
200
- e.push({ id: h.id, x: t.currentX, y: t.y, width: h.width, height: h.height }), t.currentX += h.width, s = !0;
396
+ function Kt(o) {
397
+ const s = $t(o), a = [], i = [];
398
+ let n = 0;
399
+ const t = [...o].sort((l, A) => A.height - l.height);
400
+ for (const l of t) {
401
+ let A = !1;
402
+ for (const M of a)
403
+ if (l.width <= s - M.currentX && l.height <= M.height) {
404
+ i.push({
405
+ ...l,
406
+ x: M.currentX,
407
+ y: M.y
408
+ }), M.currentX += l.width, A = !0;
201
409
  break;
202
410
  }
203
- if (!s) {
204
- const t = {
205
- y: v,
206
- height: h.height,
207
- currentX: h.width
411
+ if (!A) {
412
+ const M = {
413
+ y: n,
414
+ height: l.height,
415
+ currentX: l.width
208
416
  };
209
- o.push(t), e.push({ id: h.id, x: 0, y: v, width: h.width, height: h.height }), v += h.height;
417
+ a.push(M), i.push({
418
+ ...l,
419
+ x: 0,
420
+ y: n
421
+ }), n += l.height;
210
422
  }
211
423
  }
212
- return e;
424
+ const g = a.length > 0 ? Math.max(...a.map((l) => l.currentX)) : 0, m = n;
425
+ return {
426
+ packed: i,
427
+ width: Math.max(g, s),
428
+ height: m
429
+ };
430
+ }
431
+ function $t(o) {
432
+ if (o.length === 0) return 1;
433
+ const s = o.reduce((n, t) => n + t.width * t.height, 0);
434
+ let a = Math.ceil(Math.sqrt(s));
435
+ const i = Math.max(...o.map((n) => n.width));
436
+ return a = Math.max(a, i), a = Gt(a), a;
213
437
  }
214
- const Mt = 20;
215
- function Lt(m, u, o = 0) {
216
- const e = m.flatMap((n) => n.frames.map((c) => c.canvas)), v = Array.from(e.entries()).map(([n, c]) => ({
217
- id: n,
438
+ function Gt(o) {
439
+ return Math.pow(2, Math.ceil(Math.log2(o)));
440
+ }
441
+ const Qt = 20;
442
+ function le(o, s, a = 0) {
443
+ const i = o.flatMap((d) => d.frames.map((u) => u.canvas)), n = Array.from(i.entries()).map(([d, u]) => ({
444
+ id: d,
218
445
  // index of the frame after all the frames from all the clips have been flattened
219
- width: c.width + o * 2,
220
- height: c.height + o * 2
221
- })), x = xt(v, u), h = Math.max(...x.map((n) => n.y + n.height)) + Mt, s = u, t = Math.min(1, u / s, u / h), f = Math.floor(s * t), a = Math.floor(h * t), r = document.createElement("canvas");
222
- r.width = f, r.height = a;
223
- const p = r.getContext("2d");
224
- p.fillStyle = "#000000", p.fillRect(0, 0, r.width, r.height);
225
- const y = [];
226
- for (const n of x) {
227
- const c = e[n.id], l = n.x + o, g = n.y + o, b = c.width, M = c.height;
228
- p.drawImage(c, l * t, g * t, b * t, M * t), y[n.id] = {
229
- u: l * t / f,
230
- v: g * t / a,
231
- w: b * t / f,
232
- h: M * t / a
446
+ width: u.width + a * 2,
447
+ height: u.height + a * 2
448
+ })), t = Kt(n), g = t.height, m = t.width, l = Math.min(1, s / m, s / g), A = Math.floor(m * l), M = Math.floor(g * l) + Qt, x = document.createElement("canvas");
449
+ x.width = A, x.height = M;
450
+ const f = x.getContext("2d");
451
+ f.fillStyle = "#000000", f.fillRect(0, 0, x.width, x.height);
452
+ const r = [];
453
+ for (const d of t.packed) {
454
+ const u = i[d.id], w = d.x + a, p = d.y + a, h = u.width, U = u.height;
455
+ f.drawImage(u, w * l, p * l, h * l, U * l), r[d.id] = {
456
+ u: w * l / A,
457
+ v: p * l / M,
458
+ w: h * l / A,
459
+ h: U * l / M
233
460
  };
234
461
  }
235
- const i = [];
236
- let U = 0;
237
- for (const n of m) {
238
- const c = {
239
- ...n,
462
+ const e = [];
463
+ let c = 0;
464
+ for (const d of o) {
465
+ const u = {
466
+ ...d,
240
467
  frames: []
241
468
  };
242
- for (const l of n.frames)
243
- c.frames.push({
244
- frameUV: y[U++],
245
- cropUV: l.cropUV
469
+ for (const w of d.frames)
470
+ u.frames.push({
471
+ frameUV: r[c++],
472
+ cropUV: w.cropUV,
473
+ startTime: w.startTime,
474
+ transformMatrix: w.transformMatrix
246
475
  });
247
- i.push(c);
476
+ e.push(u);
248
477
  }
249
- return p.font = `${Math.max(6, Math.floor(12 * t))}px monospace`, p.fillStyle = "#ff0000", p.fillText("Created with MeshCap : https://bandinopla.github.io/three-mediapipe-rig/?app=meshcap", 0, a - Math.max(2, 4 * t)), {
250
- canvas: r,
251
- clips: i,
252
- padding: o,
253
- async save(n) {
254
- const c = await vt(this);
255
- if (n) {
256
- const l = URL.createObjectURL(c), g = document.createElement("a");
257
- g.href = l, g.download = "atlas.mcap", g.click(), URL.revokeObjectURL(l), g.remove();
478
+ return f.font = `${Math.max(6, Math.floor(12))}px monospace`, f.fillStyle = "#ff0000", f.fillText("Created with MeshCap : https://bandinopla.github.io/three-mediapipe-rig/?editor=meshcap", 0, M - 4), {
479
+ canvas: x,
480
+ clips: e,
481
+ padding: a,
482
+ atlasSize: s,
483
+ async save(d) {
484
+ const u = await _t(this);
485
+ if (d) {
486
+ const w = URL.createObjectURL(u), p = document.createElement("a");
487
+ p.href = w, p.download = "atlas.mcap", p.click(), URL.revokeObjectURL(w), p.remove();
258
488
  }
259
- return c;
489
+ return u;
490
+ },
491
+ async saveImageAtlas(d, u) {
492
+ const w = document.createElement("a");
493
+ w.download = u ? "atlas.jpg" : "atlas.png";
494
+ let p = this.canvas;
495
+ if (d)
496
+ return await Jt(p, d, u);
497
+ w.href = p.toDataURL(u ? "image/jpeg" : "image/png", u ? 0.8 : 1), w.click();
260
498
  }
261
499
  };
262
500
  }
263
- function zt(m, u, o, e) {
264
- e ?? (e = new pt()), o.geometry.hasAttribute("landmarkIndex") || mt(o);
265
- const v = [], x = [], h = [];
266
- let s = 0, t = 0;
267
- for (let d = 0; d < u.length; d++) {
268
- const w = u[d];
269
- v.push(s, t, w.frames.length, w.fps), s += w.frames.length, t += w.frames.length * N, x.push(w.aspectRatio), h.push(d == 0 ? 0 : h[d - 1] + u[d - 1].frames.length);
501
+ function ct(o, s) {
502
+ const a = new Uint8Array(o);
503
+ let i = 0;
504
+ for (let n = 0; n < s.length; n++)
505
+ i = i + s.charCodeAt(n) & 255;
506
+ for (let n = 0; n < a.length; n++)
507
+ a[n] ^= i;
508
+ return a;
509
+ }
510
+ async function Jt(o, s, a) {
511
+ const n = await (await new Promise((A) => o.toBlob(A, a ? "image/jpeg" : "image/png", a ? 0.8 : 1))).arrayBuffer(), t = ct(n, s), g = new Blob([t], { type: "application/octet-stream" }), m = URL.createObjectURL(g), l = document.createElement("a");
512
+ l.href = m, l.download = "atlas.mcatlas", l.click(), URL.revokeObjectURL(m);
513
+ }
514
+ async function fe(o, s) {
515
+ if (typeof o == "string")
516
+ if (o.endsWith(".mcatlas")) {
517
+ const i = await (await fetch(o)).arrayBuffer();
518
+ return await gt(i, s);
519
+ } else
520
+ return new pt().loadAsync(o);
521
+ else {
522
+ if (o.name.endsWith(".mcatlas"))
523
+ return new Promise((i, n) => {
524
+ const t = new FileReader();
525
+ t.onload = (g) => {
526
+ const m = g.target.result;
527
+ i(gt(m, s));
528
+ }, t.onerror = (g) => {
529
+ n(g);
530
+ }, t.readAsArrayBuffer(o);
531
+ });
532
+ const a = URL.createObjectURL(o);
533
+ return await new Promise((i, n) => {
534
+ new pt().load(a, i, void 0, n);
535
+ });
270
536
  }
271
- const f = B(new Float32Array(x), "float"), a = B(new Uint32Array(v), "uvec4"), r = new Float32Array(u.flatMap((d) => d.frames).reduce((d, w) => (d.push(w.frameUV.u, w.frameUV.v, w.frameUV.w, w.frameUV.h), d), [])), p = B(r, "vec4"), y = new Float32Array(u.flatMap((d) => d.frames.flatMap((w) => [w.cropUV.u, w.cropUV.v, w.cropUV.w, w.cropUV.h]))), i = B(y, "vec4"), U = new Float32Array(u.flatMap((d) => d.landmarks.flatMap((w) => w.flatMap((I) => [I.x, I.y, I.z]))));
272
- u.reduce((d, w) => d + w.frames.length, 0);
273
- const n = B(U, "vec3"), c = G(m), l = O(0), g = O(0), b = O(!0), M = f.element(l), k = a.element(l).w, A = a.element(l).z, R = g.mul(k).floor().toUint(), L = ut(
274
- b.not().and(R.greaterThanEqual(A)),
275
- // no loop + past end
276
- A.sub(1),
277
- // clamp to last frame
278
- R.mod(A)
279
- // wrap around
280
- ), Y = B(new Uint32Array(h), "uint"), P = L, q = Y.element(l).add(L), T = p.element(a.element(l).x.add(P)), C = a.element(l).y.add(P.mul(N)), z = gt("landmarkIndex", "uint").toUint(), V = C.add(z), F = n.element(V), E = i.element(q), Q = F.xy.sub(E.xy).div(E.zw), tt = Ut(T.xy.add(Q.mul(T.zw))), et = G(c, tt);
281
- e.colorNode = et;
282
- const nt = n.element(C.add(234)).xy, $ = n.element(C.add(93)).xy, ot = n.element(C.add(454)).xy, Z = n.element(C.add(323)).xy, D = nt.sub($).div(2).add($), st = ot.sub(Z).div(2).add(Z).sub(D).div(2).add(D), S = o.geometry.attributes.position, j = 116, H = 346, rt = new W().subVectors(
283
- new W(S.getX(j), S.getY(j), S.getZ(j)),
284
- new W(S.getX(H), S.getY(H), S.getZ(H))
285
- ).lengthSq(), at = O(rt), ct = n.element(C.add(H)).sub(n.element(C.add(j))).lengthSq(), it = at.div(ct).sqrt().mul(2), lt = F.sub(st).xzy.mul(wt(1, -1, yt(1).div(M))).mul(it);
286
- return e.positionNode = lt, {
287
- clips: u,
288
- atlasTexture: m,
289
- material: e,
290
- goto(d, w = !0) {
291
- let I = -1;
292
- if (typeof d == "number")
293
- I = d;
294
- else if (I = u.findIndex((dt) => dt.name === d), I === -1)
295
- throw new Error(`Clip ${d.toString()} not found`);
296
- b.value = w, g.value = 0, l.value = I;
297
- },
298
- update(d) {
299
- g.value += d;
300
- },
301
- dispose() {
302
- m.dispose(), e.dispose();
303
- },
304
- gotoAndLoop(d) {
305
- this.goto(d, !0);
306
- },
307
- gotoAndPlay(d) {
308
- this.goto(d, !1);
309
- }
310
- };
537
+ }
538
+ async function gt(o, s) {
539
+ if (!s)
540
+ throw new Error("No passphrase provided");
541
+ const a = ct(o, s), i = new Blob([a], { type: "image/png" }), n = URL.createObjectURL(i), t = document.createElement("img");
542
+ await new Promise((m, l) => {
543
+ t.onload = () => m(), t.onerror = l, t.src = n;
544
+ });
545
+ const g = new Nt(t);
546
+ return g.needsUpdate = !0, g;
547
+ }
548
+ async function de(o, s) {
549
+ const a = document.createElement("canvas");
550
+ a.width = o.width, a.height = o.height, a.getContext("2d").drawImage(o, 0, 0);
551
+ const n = await new Promise((g, m) => {
552
+ a.toBlob((l) => {
553
+ if (!l) {
554
+ m(new Error("Failed to create blob"));
555
+ return;
556
+ }
557
+ g(l.arrayBuffer());
558
+ }, "image/jpeg", 0.75);
559
+ }), t = ct(n, s);
560
+ return new Blob([t], { type: "application/octet-stream" });
311
561
  }
312
562
  export {
313
- Lt as buildMeshCapAtlas,
314
- zt as createMeshCapMaterial,
315
- Rt as loadMeshCapFile
563
+ Ht as audioBufferToWav,
564
+ le as buildMeshCapAtlas,
565
+ Yt as createAudioAtlasPlayer,
566
+ qt as createMeshCapMaterial,
567
+ Xt as extractAudioSprites,
568
+ ie as loadMeshCapFile,
569
+ fe as loadMeshcapAtlas,
570
+ de as obfuscateImage,
571
+ ce as readAsMCapFile
316
572
  };