three-mediapipe-rig 0.1.2 → 0.1.3

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