three-mediapipe-rig 0.1.3 → 0.1.41

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