mesh-avatar 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -21,157 +21,157 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
21
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
  SOFTWARE.
23
23
  */
24
- const te = (n, e, t) => {
24
+ const ne = (n, e, t) => {
25
25
  (typeof n != "number" || !Number.isFinite(n)) && t.push(`${e}: expected a finite number`);
26
- }, Re = (n, e, t) => {
27
- te(n, e, t), typeof n == "number" && n <= 0 && t.push(`${e}: must be positive`);
28
- }, xe = (n, e, t) => {
29
- Re(n, e, t), typeof n == "number" && !Number.isInteger(n) && t.push(`${e}: expected an integer`);
30
- }, Fe = (n, e, t) => {
31
- te(n, e, t), typeof n == "number" && !Number.isInteger(n) && t.push(`${e}: expected an integer`);
32
- }, Ft = (n, e, t) => {
26
+ }, Le = (n, e, t) => {
27
+ ne(n, e, t), typeof n == "number" && n <= 0 && t.push(`${e}: must be positive`);
28
+ }, we = (n, e, t) => {
29
+ Le(n, e, t), typeof n == "number" && !Number.isInteger(n) && t.push(`${e}: expected an integer`);
30
+ }, Ie = (n, e, t) => {
31
+ ne(n, e, t), typeof n == "number" && !Number.isInteger(n) && t.push(`${e}: expected an integer`);
32
+ }, jt = (n, e, t) => {
33
33
  (typeof n != "string" || !n.trim()) && t.push(`${e}: expected a non-empty string`);
34
- }, de = (n, e = 0, t = 1 / 0) => (r, a, o) => {
34
+ }, ge = (n, e = 0, t = 1 / 0) => (r, a, s) => {
35
35
  if (!Array.isArray(r)) {
36
- o.push(`${a}: expected an array`);
36
+ s.push(`${a}: expected an array`);
37
37
  return;
38
38
  }
39
- (r.length < e || r.length > t) && o.push(`${a}: expected ${e}..${t} items`), r.forEach((i, f) => n(i, `${a}[${f}]`, o));
40
- }, ge = de(te, 2, 2), he = (n, e, t) => {
41
- ge(n, e, t), Array.isArray(n) && n.length === 2 && n[0] >= n[1] && t.push(`${e}: start must be less than end`);
42
- }, se = (n, e = []) => (t, r, a) => {
39
+ (r.length < e || r.length > t) && s.push(`${a}: expected ${e}..${t} items`), r.forEach((l, f) => n(l, `${a}[${f}]`, s));
40
+ }, ye = ge(ne, 2, 2), ue = (n, e, t) => {
41
+ ye(n, e, t), Array.isArray(n) && n.length === 2 && n[0] >= n[1] && t.push(`${e}: start must be less than end`);
42
+ }, oe = (n, e = []) => (t, r, a) => {
43
43
  if (!t || typeof t != "object" || Array.isArray(t)) {
44
44
  a.push(`${r}: expected an object`);
45
45
  return;
46
46
  }
47
- const o = t;
48
- for (const [i, f] of Object.entries(n))
49
- o[i] === void 0 && e.includes(i) || f(o[i], `${r}.${i}`, a);
50
- }, je = { cx: te, cy: te, rx: Re, ry: Re }, ye = se(je), He = de(ge, 3), Bt = de(ge, 2), Qe = (n, e, t) => {
51
- te(n, e, t), typeof n == "number" && (n < 0 || n > 1) && t.push(`${e}: must be between 0 and 1`);
52
- }, vn = (n) => se({
47
+ const s = t;
48
+ for (const [l, f] of Object.entries(n))
49
+ s[l] === void 0 && e.includes(l) || f(s[l], `${r}.${l}`, a);
50
+ }, Ce = { cx: ne, cy: ne, rx: Le, ry: Le }, ve = oe(Ce), We = ge(ye, 3), It = ge(ye, 2), nt = (n, e, t) => {
51
+ ne(n, e, t), typeof n == "number" && (n < 0 || n > 1) && t.push(`${e}: must be between 0 and 1`);
52
+ }, bn = (n) => oe({
53
53
  version: (e, t, r) => {
54
54
  e !== 1 && r.push(`${t}: only version 1 is supported`);
55
55
  },
56
- image: se({ width: xe, height: xe }),
57
- head: se({
58
- ...je,
59
- shiftX: te,
60
- shiftY: te,
61
- pivotX: te,
62
- pivotY: te,
63
- maxRoll: te,
64
- weightBand: he,
65
- turnBand: he
56
+ image: oe({ width: we, height: we }),
57
+ head: oe({
58
+ ...Ce,
59
+ shiftX: ne,
60
+ shiftY: ne,
61
+ pivotX: ne,
62
+ pivotY: ne,
63
+ maxRoll: ne,
64
+ weightBand: ue,
65
+ turnBand: ue
66
66
  }),
67
- body: se({
68
- pivotX: te,
69
- pivotY: te,
70
- maxRoll: te,
71
- breathBand: he,
72
- rollBand: he,
73
- chest: ye,
74
- shoulders: de(ye)
67
+ body: oe({
68
+ pivotX: ne,
69
+ pivotY: ne,
70
+ maxRoll: ne,
71
+ breathBand: ue,
72
+ rollBand: ue,
73
+ chest: ve,
74
+ shoulders: ge(ve)
75
75
  }),
76
- face: se({
77
- nose: ye,
78
- mouth: ye,
79
- eyeA: ye,
80
- eyeB: ye,
81
- earL: ye,
82
- earR: ye,
83
- brow: se({ ...je, band: he }),
84
- jaw: se({ ...je, band: he })
76
+ face: oe({
77
+ nose: ve,
78
+ mouth: ve,
79
+ eyeA: ve,
80
+ eyeB: ve,
81
+ earL: ve,
82
+ earR: ve,
83
+ brow: oe({ ...Ce, band: ue }),
84
+ jaw: oe({ ...Ce, band: ue })
85
85
  }),
86
- buns: se({ bunL: ye, bunR: ye }, ["bunL", "bunR"]),
87
- eyes: de(se({
88
- opening: He,
89
- roi: He,
90
- x0: te,
91
- x1: te,
92
- top: de(te, 24, 24),
93
- bot: de(te, 24, 24)
86
+ buns: oe({ bunL: ve, bunR: ve }, ["bunL", "bunR"]),
87
+ eyes: ge(oe({
88
+ opening: We,
89
+ roi: We,
90
+ x0: ne,
91
+ x1: ne,
92
+ top: ge(ne, 24, 24),
93
+ bot: ge(ne, 24, 24)
94
94
  }, n ? ["x0", "x1", "top", "bot"] : []), 2, 2),
95
- mouth: se({
96
- cx: te,
97
- cy: te,
98
- angle: te,
99
- halfLen: Re,
100
- bow: te,
101
- area: se({ ...je, angle: te })
95
+ mouth: oe({
96
+ cx: ne,
97
+ cy: ne,
98
+ angle: ne,
99
+ halfLen: Le,
100
+ bow: ne,
101
+ area: oe({ ...Ce, angle: ne })
102
102
  }),
103
- cheeks: de(ge, 2, 2),
104
- strands: de(se({ name: Ft, nodes: Bt, sigma: Re, k: Re, max: Re })),
105
- accessories: de(se({
106
- name: Ft,
107
- pivot: ge,
108
- tip: ge,
109
- split: Qe,
110
- box: de(Fe, 4, 4),
111
- color: se({ redness: Qe, minRed: te })
103
+ cheeks: ge(ye, 2, 2),
104
+ strands: ge(oe({ name: jt, nodes: It, sigma: Le, k: Le, max: Le })),
105
+ accessories: ge(oe({
106
+ name: jt,
107
+ pivot: ye,
108
+ tip: ye,
109
+ split: nt,
110
+ box: ge(Ie, 4, 4),
111
+ color: oe({ redness: nt, minRed: ne })
112
112
  })),
113
- hand: se({
114
- outline: He,
115
- jaw: Bt,
116
- jawRange: he,
117
- background: He,
118
- elbow: ge,
119
- wrist: ge,
120
- knuckle: ge,
121
- contact: ge,
122
- forearmShare: Qe,
123
- armBand: he,
124
- wristBand: he,
125
- handBand: he,
126
- fingerXBand: he,
127
- fingerYBand: he,
128
- pinBand: he
113
+ hand: oe({
114
+ outline: We,
115
+ jaw: It,
116
+ jawRange: ue,
117
+ background: We,
118
+ elbow: ye,
119
+ wrist: ye,
120
+ knuckle: ye,
121
+ contact: ye,
122
+ forearmShare: nt,
123
+ armBand: ue,
124
+ wristBand: ue,
125
+ handBand: ue,
126
+ fingerXBand: ue,
127
+ fingerYBand: ue,
128
+ pinBand: ue
129
129
  }),
130
- mesh: se({
131
- baseCell: xe,
132
- fine: se({
133
- x0: Fe,
134
- x1: Fe,
135
- y0: Fe,
136
- y1: Fe,
137
- cell: xe
130
+ mesh: oe({
131
+ baseCell: we,
132
+ fine: oe({
133
+ x0: Ie,
134
+ x1: Ie,
135
+ y0: Ie,
136
+ y1: Ie,
137
+ cell: we
138
138
  }),
139
- handCell: xe,
140
- tasselCell: xe,
141
- eyeBallCell: xe,
142
- eyeCell: xe,
143
- spriteCell: xe
139
+ handCell: we,
140
+ tasselCell: we,
141
+ eyeBallCell: we,
142
+ eyeCell: we,
143
+ spriteCell: we
144
144
  }),
145
- view: se({ padTop: te, padSide: te, gazeCenter: ge }, ["gazeCenter"])
145
+ view: oe({ padTop: ne, padSide: ne, gazeCenter: ye }, ["gazeCenter"])
146
146
  }, ["buns", "strands", "accessories", "hand"]);
147
- function Kt(n, e = {}) {
147
+ function en(n, e = {}) {
148
148
  const t = [];
149
- if (vn(e.draft ?? !1)(n, "rig", t), t.length) return t;
149
+ if (bn(e.draft ?? !1)(n, "rig", t), t.length) return t;
150
150
  const r = n;
151
- r.eyes.forEach((a, o) => {
152
- const i = ["x0", "x1", "top", "bot"];
153
- if (!(e.draft && i.every((f) => a[f] === void 0))) {
154
- if (i.some((f) => a[f] === void 0)) {
155
- t.push(`rig.eyes[${o}]: supply all x0/x1/top/bot fields or omit all in a draft`);
151
+ r.eyes.forEach((a, s) => {
152
+ const l = ["x0", "x1", "top", "bot"];
153
+ if (!(e.draft && l.every((f) => a[f] === void 0))) {
154
+ if (l.some((f) => a[f] === void 0)) {
155
+ t.push(`rig.eyes[${s}]: supply all x0/x1/top/bot fields or omit all in a draft`);
156
156
  return;
157
157
  }
158
- a.x0 >= a.x1 && t.push(`rig.eyes[${o}].x1: must exceed x0`), a.top.some((f, m) => f > a.bot[m]) && t.push(`rig.eyes[${o}].top: must not exceed bottom curve`);
158
+ a.x0 >= a.x1 && t.push(`rig.eyes[${s}].x1: must exceed x0`), a.top.some((f, y) => f > a.bot[y]) && t.push(`rig.eyes[${s}].top: must not exceed bottom curve`);
159
159
  }
160
160
  }), (r.mesh.fine.x0 >= r.mesh.fine.x1 || r.mesh.fine.y0 >= r.mesh.fine.y1) && t.push("rig.mesh.fine: rectangle must have positive area"), (r.view.padTop <= -1 || r.view.padTop >= 1 || r.view.padSide <= -0.5) && t.push("rig.view: margins must leave a positive viewport");
161
- for (const [a, o] of (r.strands ?? []).entries())
162
- o.nodes.some((i, f) => f > 0 && i[0] === o.nodes[f - 1][0] && i[1] === o.nodes[f - 1][1]) && t.push(`rig.strands[${a}].nodes: consecutive nodes must differ`);
163
- for (const [a, o] of (r.accessories ?? []).entries())
164
- (o.box[0] < 0 || o.box[1] < 0 || o.box[2] > r.image.width || o.box[3] > r.image.height) && t.push(`rig.accessories[${a}].box: rectangle must stay inside the image`), (o.split <= 0 || o.split >= 1) && t.push(`rig.accessories[${a}].split: must be strictly between 0 and 1`), o.pivot.every((i, f) => i === o.tip[f]) && t.push(`rig.accessories[${a}].tip: must differ from pivot`), (o.box[0] >= o.box[2] || o.box[1] >= o.box[3]) && t.push(`rig.accessories[${a}].box: rectangle must have positive area`);
161
+ for (const [a, s] of (r.strands ?? []).entries())
162
+ s.nodes.some((l, f) => f > 0 && l[0] === s.nodes[f - 1][0] && l[1] === s.nodes[f - 1][1]) && t.push(`rig.strands[${a}].nodes: consecutive nodes must differ`);
163
+ for (const [a, s] of (r.accessories ?? []).entries())
164
+ (s.box[0] < 0 || s.box[1] < 0 || s.box[2] > r.image.width || s.box[3] > r.image.height) && t.push(`rig.accessories[${a}].box: rectangle must stay inside the image`), (s.split <= 0 || s.split >= 1) && t.push(`rig.accessories[${a}].split: must be strictly between 0 and 1`), s.pivot.every((l, f) => l === s.tip[f]) && t.push(`rig.accessories[${a}].tip: must differ from pivot`), (s.box[0] >= s.box[2] || s.box[1] >= s.box[3]) && t.push(`rig.accessories[${a}].box: rectangle must have positive area`);
165
165
  return t;
166
166
  }
167
- function xt(n) {
168
- const e = Kt(n);
167
+ function Et(n) {
168
+ const e = en(n);
169
169
  if (e.length) throw new Error(`Invalid rig:
170
170
  ${e.join(`
171
171
  `)}`);
172
172
  return structuredClone(n);
173
173
  }
174
- const Ze = [
174
+ const Ge = [
175
175
  { id: "angleX", label: "Face angle X", min: -30, max: 30, def: 0, group: "Head and body" },
176
176
  { id: "angleY", label: "Face angle Y", min: -30, max: 30, def: 0, group: "Head and body" },
177
177
  { id: "angleZ", label: "Face roll", min: -30, max: 30, def: 0, group: "Head and body" },
@@ -192,253 +192,253 @@ const Ze = [
192
192
  { id: "handAngle", label: "Wrist angle", min: -10, max: 10, def: 0, group: "Hand" },
193
193
  { id: "fingerTap", label: "Finger tap", min: 0, max: 1, def: 0, group: "Hand" }
194
194
  ];
195
- function bn(n) {
196
- const e = { w: n.image.width, h: n.image.height }, t = (v, A, E) => E ? Math.exp(-(((v - E.cx) / E.rx) ** 2 + ((A - E.cy) / E.ry) ** 2)) : 0, r = 24, a = n.eyes;
197
- function o(v, A) {
198
- const E = Math.min(1, Math.max(0, A)) * (r - 1), k = Math.min(Math.floor(E), r - 2), N = E - k;
199
- return v[k] + (v[k + 1] - v[k]) * N;
200
- }
201
- function i(v, A, E, k) {
202
- A = Math.min(1, Math.max(0, A));
203
- const N = o(v.top, A), z = o(v.bot, A), F = 1 - E, X = 4 * A * (1 - A), M = o(v.bot, 0.5) - o(v.top, 0.5), L = v.top[0] + (v.top[r - 1] - v.top[0]) * A - 0.3 * M * X, W = Math.max(N, z + 1 + (L - z - 1) * k);
204
- return [(W - N) * Math.max(F, -0.07), F > 0 ? (W - z) * F * k : 0];
205
- }
206
- const f = [[-2, 1], [-1, 2], [0, 3], [1, 2], [2, 1]], m = 5;
207
- function g(v, A, E, k) {
208
- let N = 0, z = 0;
209
- for (const [L, W] of f) {
210
- const [Q, ne] = i(v, (A + L * m - v.x0) / (v.x1 - v.x0), E, k);
211
- N += Q * W / 9, z += ne * W / 9;
212
- }
213
- const F = Math.min(1, Math.max(0, (A - v.x0) / (v.x1 - v.x0))), X = o(v.top, F), M = o(v.bot, F);
214
- return { top: X, bot: M, b: 1 - E, lid: X + N, bc: M + z };
215
- }
216
- function p(v, A, E, k, N, z) {
217
- const F = g(v, E, N, z);
218
- return A === "lash" ? F.lid - (F.top - k) * (1 - 0.3 * Math.min(1, Math.max(0, F.b))) : A === "low" ? k + (F.bc - F.bot) : A === "crease" ? k + (F.lid - F.top) * 0.22 : k;
219
- }
220
- function l(v, A, E) {
221
- const k = Math.min(1, Math.max(0, 1 - A));
222
- return v === "low" ? 1 - Math.min(1, k * E * 1.2) : v === "crease" ? 1 - 0.45 * k : 1;
223
- }
224
- const h = n.mouth, S = n.cheeks, y = n.head, u = n.body, b = (v, A, E) => {
225
- const k = Math.min(1, Math.max(0, (E - v) / (A - v)));
226
- return k * k * (3 - 2 * k);
195
+ function xn(n) {
196
+ const e = { w: n.image.width, h: n.image.height }, t = (x, M, T) => T ? Math.exp(-(((x - T.cx) / T.rx) ** 2 + ((M - T.cy) / T.ry) ** 2)) : 0, r = 24, a = n.eyes;
197
+ function s(x, M) {
198
+ const T = Math.min(1, Math.max(0, M)) * (r - 1), _ = Math.min(Math.floor(T), r - 2), Y = T - _;
199
+ return x[_] + (x[_ + 1] - x[_]) * Y;
200
+ }
201
+ function l(x, M, T, _) {
202
+ M = Math.min(1, Math.max(0, M));
203
+ const Y = s(x.top, M), X = s(x.bot, M), F = 1 - T, C = 4 * M * (1 - M), Z = s(x.bot, 0.5) - s(x.top, 0.5), E = x.top[0] + (x.top[r - 1] - x.top[0]) * M - 0.3 * Z * C, R = Math.max(Y, X + 1 + (E - X - 1) * _);
204
+ return [(R - Y) * Math.max(F, -0.07), F > 0 ? (R - X) * F * _ : 0];
205
+ }
206
+ const f = [[-2, 1], [-1, 2], [0, 3], [1, 2], [2, 1]], y = 5;
207
+ function d(x, M, T, _) {
208
+ let Y = 0, X = 0;
209
+ for (const [E, R] of f) {
210
+ const [J, te] = l(x, (M + E * y - x.x0) / (x.x1 - x.x0), T, _);
211
+ Y += J * R / 9, X += te * R / 9;
212
+ }
213
+ const F = Math.min(1, Math.max(0, (M - x.x0) / (x.x1 - x.x0))), C = s(x.top, F), Z = s(x.bot, F);
214
+ return { top: C, bot: Z, b: 1 - T, lid: C + Y, bc: Z + X };
215
+ }
216
+ function m(x, M, T, _, Y, X) {
217
+ const F = d(x, T, Y, X);
218
+ return M === "lash" ? F.lid - (F.top - _) * (1 - 0.3 * Math.min(1, Math.max(0, F.b))) : M === "low" ? _ + (F.bc - F.bot) : M === "crease" ? _ + (F.lid - F.top) * 0.22 : _;
219
+ }
220
+ function c(x, M, T) {
221
+ const _ = Math.min(1, Math.max(0, 1 - M));
222
+ return x === "low" ? 1 - Math.min(1, _ * T * 1.2) : x === "crease" ? 1 - 0.45 * _ : 1;
223
+ }
224
+ const p = n.mouth, b = n.cheeks, v = n.head, h = n.body, u = (x, M, T) => {
225
+ const _ = Math.min(1, Math.max(0, (T - x) / (M - x)));
226
+ return _ * _ * (3 - 2 * _);
227
227
  };
228
- function x(v, A) {
229
- return 1 - b(...y.weightBand, A);
230
- }
231
- function s(v, A) {
232
- return 1 - b(...y.turnBand, A);
233
- }
234
- const c = n.strands ?? [];
235
- function T(v, A, E) {
236
- let k = 1 / 0, N = 0;
237
- const z = E.length - 1;
238
- for (let F = 0; F < z; F++) {
239
- const [X, M] = E[F], [L, W] = E[F + 1], Q = L - X, ne = W - M, ee = Math.min(1, Math.max(0, ((v - X) * Q + (A - M) * ne) / (Q * Q + ne * ne))), ce = Math.hypot(v - X - Q * ee, A - M - ne * ee);
240
- ce < k && (k = ce, N = (F + ee) / z);
241
- }
242
- return [k, N];
243
- }
244
- function d(v, A, E) {
245
- if (E < 0.02) return [];
246
- const k = [];
247
- let N = 0;
248
- c.forEach((F, X) => {
249
- const [M, L] = T(v, A, F.nodes), W = Math.exp(-((M / F.sigma) ** 2));
250
- W < 0.02 || (k.push([X, W, L]), N += W);
228
+ function w(x, M) {
229
+ return 1 - u(...v.weightBand, M);
230
+ }
231
+ function o(x, M) {
232
+ return 1 - u(...v.turnBand, M);
233
+ }
234
+ const i = n.strands ?? [];
235
+ function S(x, M, T) {
236
+ let _ = 1 / 0, Y = 0;
237
+ const X = T.length - 1;
238
+ for (let F = 0; F < X; F++) {
239
+ const [C, Z] = T[F], [E, R] = T[F + 1], J = E - C, te = R - Z, ee = Math.min(1, Math.max(0, ((x - C) * J + (M - Z) * te) / (J * J + te * te))), se = Math.hypot(x - C - J * ee, M - Z - te * ee);
240
+ se < _ && (_ = se, Y = (F + ee) / X);
241
+ }
242
+ return [_, Y];
243
+ }
244
+ function g(x, M, T) {
245
+ if (T < 0.02) return [];
246
+ const _ = [];
247
+ let Y = 0;
248
+ i.forEach((F, C) => {
249
+ const [Z, E] = S(x, M, F.nodes), R = Math.exp(-((Z / F.sigma) ** 2));
250
+ R < 0.02 || (_.push([C, R, E]), Y += R);
251
251
  });
252
- const z = Math.max(1, N);
253
- return k.map(([F, X, M]) => [F, E * X / z * b(0.04, 0.55, M), M]);
252
+ const X = Math.max(1, Y);
253
+ return _.map(([F, C, Z]) => [F, T * C / X * u(0.04, 0.55, Z), Z]);
254
254
  }
255
- function w(v, A, E = 1) {
255
+ function A(x, M, T = 1) {
256
256
  return {
257
- head: x(v, A),
258
- turn: s(v, A),
259
- strands: d(v, A, E),
260
- bunL: t(v, A, n.buns?.bunL),
261
- bunR: t(v, A, n.buns?.bunR),
262
- brow: t(v, A, n.face.brow) * (1 - b(...n.face.brow.band, A)),
257
+ head: w(x, M),
258
+ turn: o(x, M),
259
+ strands: g(x, M, T),
260
+ bunL: t(x, M, n.buns?.bunL),
261
+ bunR: t(x, M, n.buns?.bunR),
262
+ brow: t(x, M, n.face.brow) * (1 - u(...n.face.brow.band, M)),
263
263
  // chin below the mouth line drops a little when the mouth opens
264
- jaw: t(v, A, n.face.jaw) * b(...n.face.jaw.band, A),
264
+ jaw: t(x, M, n.face.jaw) * u(...n.face.jaw.band, M),
265
265
  // per-feature depth for head turns (nose sticks out most, ears sit at the back)
266
- nose: t(v, A, n.face.nose),
267
- mouth: t(v, A, n.face.mouth),
268
- eyeA: t(v, A, n.face.eyeA),
269
- eyeB: t(v, A, n.face.eyeB),
270
- earR: t(v, A, n.face.earR),
271
- earL: t(v, A, n.face.earL),
272
- chest: t(v, A, u.chest),
273
- breath: 1 - b(...u.breathBand, A),
274
- shoulder: u.shoulders.reduce((k, N) => k + t(v, A, N), 0)
266
+ nose: t(x, M, n.face.nose),
267
+ mouth: t(x, M, n.face.mouth),
268
+ eyeA: t(x, M, n.face.eyeA),
269
+ eyeB: t(x, M, n.face.eyeB),
270
+ earR: t(x, M, n.face.earR),
271
+ earL: t(x, M, n.face.earL),
272
+ chest: t(x, M, h.chest),
273
+ breath: 1 - u(...h.breathBand, M),
274
+ shoulder: h.shoulders.reduce((_, Y) => _ + t(x, M, Y), 0)
275
275
  };
276
276
  }
277
- function B(v, A, E, k) {
278
- const N = (v - y.cx) / y.rx, z = (A - y.cy) / y.ry, F = 1 - N * N - z * z;
277
+ function L(x, M, T, _) {
278
+ const Y = (x - v.cx) / v.rx, X = (M - v.cy) / v.ry, F = 1 - Y * Y - X * X;
279
279
  if (F <= 0) return [0, 0];
280
- const X = b(0, 0.84, F);
281
- return [E * y.shiftX * X, -k * y.shiftY * X];
282
- }
283
- function Y(v, A, E, k) {
284
- const N = Math.cos(k), z = Math.sin(k), F = v[0] - A, X = v[1] - E;
285
- v[0] = A + F * N - X * z, v[1] = E + F * z + X * N;
286
- }
287
- function I(v, A, E, k = A) {
288
- const N = B(v[0], v[1], E.angleX / 30, E.angleY / 30);
289
- v[0] += N[0] * k, v[1] += N[1] * k, !(A <= 0) && Y(v, y.pivotX, y.pivotY, -E.angleZ / 30 * y.maxRoll * A);
290
- }
291
- function _(v, A, E, k = 0, N = 1, z = 0) {
292
- const F = E.breath;
293
- v[1] -= F * (5 * N + 4 * z), v[0] += (v[0] - u.chest.cx) * 0.012 * F * k, v[0] += E.bodyAngleX / 10 * (7 + 9 * k), Y(v, u.pivotX, u.pivotY, -E.bodyAngleZ / 10 * u.maxRoll * (1 - b(...u.rollBand, A)));
294
- }
295
- function U(v, A, E, k, N, z) {
296
- const F = z;
297
- F[0] = v, F[1] = A;
298
- const X = N.gain;
299
- for (const [W, Q, ne] of E.strands) {
300
- const ee = N.strands?.[W];
280
+ const C = u(0, 0.84, F);
281
+ return [T * v.shiftX * C, -_ * v.shiftY * C];
282
+ }
283
+ function B(x, M, T, _) {
284
+ const Y = Math.cos(_), X = Math.sin(_), F = x[0] - M, C = x[1] - T;
285
+ x[0] = M + F * Y - C * X, x[1] = T + F * X + C * Y;
286
+ }
287
+ function j(x, M, T, _ = M) {
288
+ const Y = L(x[0], x[1], T.angleX / 30, T.angleY / 30);
289
+ x[0] += Y[0] * _, x[1] += Y[1] * _, !(M <= 0) && B(x, v.pivotX, v.pivotY, -T.angleZ / 30 * v.maxRoll * M);
290
+ }
291
+ function k(x, M, T, _ = 0, Y = 1, X = 0) {
292
+ const F = T.breath;
293
+ x[1] -= F * (5 * Y + 4 * X), x[0] += (x[0] - h.chest.cx) * 0.012 * F * _, x[0] += T.bodyAngleX / 10 * (7 + 9 * _), B(x, h.pivotX, h.pivotY, -T.bodyAngleZ / 10 * h.maxRoll * (1 - u(...h.rollBand, M)));
294
+ }
295
+ function U(x, M, T, _, Y, X) {
296
+ const F = X;
297
+ F[0] = x, F[1] = M;
298
+ const C = Y.gain;
299
+ for (const [R, J, te] of T.strands) {
300
+ const ee = Y.strands?.[R];
301
301
  if (!ee) continue;
302
- const ce = ne * 3, oe = Math.min(2, Math.floor(ce)), _e = ce - oe;
303
- F[0] += (ee[oe][0] + (ee[oe + 1][0] - ee[oe][0]) * _e) * Q * X, F[1] += (ee[oe][1] + (ee[oe + 1][1] - ee[oe][1]) * _e) * Q * X;
302
+ const se = te * 3, le = Math.min(2, Math.floor(se)), ce = se - le;
303
+ F[0] += (ee[le][0] + (ee[le + 1][0] - ee[le][0]) * ce) * J * C, F[1] += (ee[le][1] + (ee[le + 1][1] - ee[le][1]) * ce) * J * C;
304
304
  }
305
- if (F[0] += (N.bunL[0] * E.bunL + N.bunR[0] * E.bunR) * X, F[1] += (N.bunL[1] * E.bunL + N.bunR[1] * E.bunR) * X, F[1] -= k.browY * 7 * E.brow, E.brow > 0.01) {
306
- const W = k.browAngle * 0.12 * E.brow;
307
- F[1] += (v - n.face.brow.cx) * Math.sin(W);
305
+ if (F[0] += (Y.bunL[0] * T.bunL + Y.bunR[0] * T.bunR) * C, F[1] += (Y.bunL[1] * T.bunL + Y.bunR[1] * T.bunR) * C, F[1] -= _.browY * 7 * T.brow, T.brow > 0.01) {
306
+ const R = _.browAngle * 0.12 * T.brow;
307
+ F[1] += (x - n.face.brow.cx) * Math.sin(R);
308
308
  }
309
- F[1] += k.mouthOpen * 3.5 * E.jaw;
310
- const M = k.angleX / 30, L = k.angleY / 30;
311
- return F[0] += M * (8 * E.nose + 5 * E.mouth) + (v - n.face.eyeA.cx) * 0.12 * M * E.eyeA - (v - n.face.eyeB.cx) * 0.12 * M * E.eyeB - M * 9 * E.earR + M * 4 * E.earL - M * 10 * (E.bunL + E.bunR), F[1] -= L * (6 * E.nose + 3 * E.mouth), F[1] += (A - n.face.eyeA.cy) * -0.06 * Math.abs(L) * E.eyeA + (A - n.face.eyeB.cy) * -0.06 * Math.abs(L) * E.eyeB, I(F, E.head, k, E.turn), _(F, A, k, E.chest, E.breath, E.shoulder), F;
309
+ F[1] += _.mouthOpen * 3.5 * T.jaw;
310
+ const Z = _.angleX / 30, E = _.angleY / 30;
311
+ return F[0] += Z * (8 * T.nose + 5 * T.mouth) + (x - n.face.eyeA.cx) * 0.12 * Z * T.eyeA - (x - n.face.eyeB.cx) * 0.12 * Z * T.eyeB - Z * 9 * T.earR + Z * 4 * T.earL - Z * 10 * (T.bunL + T.bunR), F[1] -= E * (6 * T.nose + 3 * T.mouth), F[1] += (M - n.face.eyeA.cy) * -0.06 * Math.abs(E) * T.eyeA + (M - n.face.eyeB.cy) * -0.06 * Math.abs(E) * T.eyeB, j(F, T.head, _, T.turn), k(F, M, _, T.chest, T.breath, T.shoulder), F;
312
312
  }
313
- const O = n.hand, C = O?.elbow ?? [0, 0], $ = O?.wrist ?? [0, 0], V = O?.knuckle ?? [0, 0], j = O?.contact ?? [0, 0], D = O?.forearmShare ?? 0;
314
- function H(v, A) {
313
+ const O = n.hand, $ = O?.elbow ?? [0, 0], D = O?.wrist ?? [0, 0], q = O?.knuckle ?? [0, 0], N = O?.contact ?? [0, 0], P = O?.forearmShare ?? 0;
314
+ function H(x, M) {
315
315
  return O ? {
316
- arm: 1 - b(...O.armBand, A),
317
- wrist: 1 - b(...O.wristBand, A),
318
- hand: 1 - b(...O.handBand, A),
319
- finger: b(...O.fingerXBand, v) * (1 - b(...O.fingerYBand, A)),
320
- pinBottom: b(...O.pinBand, A)
316
+ arm: 1 - u(...O.armBand, M),
317
+ wrist: 1 - u(...O.wristBand, M),
318
+ hand: 1 - u(...O.handBand, M),
319
+ finger: u(...O.fingerXBand, x) * (1 - u(...O.fingerYBand, M)),
320
+ pinBottom: u(...O.pinBand, M)
321
321
  } : { arm: 0, wrist: 0, hand: 0, finger: 0, pinBottom: 0 };
322
322
  }
323
- const R = (v, A, E, k) => {
324
- const N = v[0] - A[0], z = v[1] - A[1];
325
- return [A[0] + N * E - z * k, A[1] + N * k + z * E];
326
- }, Z = (v) => Math.atan2(v[1], v[0]);
327
- function P(v) {
328
- const A = [...j];
329
- I(A, x(...j), v, s(...j)), _(A, j[1], v, 0, 1, 0);
330
- const E = [...C];
331
- _(E, C[1], v, 0, 0, 0);
332
- const N = (Z([A[0] - E[0], A[1] - E[1]]) - Z([j[0] - C[0], j[1] - C[1]])) * D, z = Math.cos(N), F = Math.sin(N), X = (ne) => {
333
- const ee = R(ne, C, z, F);
334
- return [ee[0] + E[0] - C[0], ee[1] + E[1] - C[1]];
335
- }, M = X($), L = X(j), W = Z([A[0] - M[0], A[1] - M[1]]) - Z([L[0] - M[0], L[1] - M[1]]), Q = Math.min(1.03, Math.max(0.97, Math.hypot(A[0] - M[0], A[1] - M[1]) / Math.hypot(L[0] - M[0], L[1] - M[1])));
336
- return { tr: X, w: M, b: W, k: Q };
337
- }
338
- function G(v, A, E, k, N, z) {
339
- let F = [v, A];
340
- F = R(F, V, Math.cos(k.fingerTap * 0.07 * E.finger), Math.sin(k.fingerTap * 0.07 * E.finger));
341
- const X = -k.handAngle / 10 * 0.045 * E.wrist, M = -k.armAngle / 10 * 0.015 * E.arm;
342
- F = R(F, $, Math.cos(X), Math.sin(X)), F = R(F, C, Math.cos(M), Math.sin(M)), F = N.tr(F);
343
- const L = N.b * E.hand, W = 1 + (N.k - 1) * E.hand;
344
- return F = R(F, N.w, Math.cos(L) * W, Math.sin(L) * W), F[1] += (A - F[1]) * E.pinBottom, z[0] = F[0], z[1] = F[1], z;
345
- }
346
- const J = n.accessories ?? [];
347
- function q(v, A, E, k) {
348
- const N = w(v.pivot[0], v.pivot[1]);
349
- return U(v.pivot[0], v.pivot[1], N, A, E, k);
350
- }
351
- return { IMG: e, EYES: a, MOUTH: h, CHEEKS: S, STRANDS: c, TASSELS: J, headWeight: x, turnWeight: s, baseWeights: w, deformBase: U, applyHead: I, applyBody: _, handWeights: H, handFrame: P, deformHand: G, eyePartY: p, eyePartAlpha: l, tasselAnchor: q };
323
+ const G = (x, M, T, _) => {
324
+ const Y = x[0] - M[0], X = x[1] - M[1];
325
+ return [M[0] + Y * T - X * _, M[1] + Y * _ + X * T];
326
+ }, z = (x) => Math.atan2(x[1], x[0]);
327
+ function W(x) {
328
+ const M = [...N];
329
+ j(M, w(...N), x, o(...N)), k(M, N[1], x, 0, 1, 0);
330
+ const T = [...$];
331
+ k(T, $[1], x, 0, 0, 0);
332
+ const Y = (z([M[0] - T[0], M[1] - T[1]]) - z([N[0] - $[0], N[1] - $[1]])) * P, X = Math.cos(Y), F = Math.sin(Y), C = (te) => {
333
+ const ee = G(te, $, X, F);
334
+ return [ee[0] + T[0] - $[0], ee[1] + T[1] - $[1]];
335
+ }, Z = C(D), E = C(N), R = z([M[0] - Z[0], M[1] - Z[1]]) - z([E[0] - Z[0], E[1] - Z[1]]), J = Math.min(1.03, Math.max(0.97, Math.hypot(M[0] - Z[0], M[1] - Z[1]) / Math.hypot(E[0] - Z[0], E[1] - Z[1])));
336
+ return { tr: C, w: Z, b: R, k: J };
337
+ }
338
+ function I(x, M, T, _, Y, X) {
339
+ let F = [x, M];
340
+ F = G(F, q, Math.cos(_.fingerTap * 0.07 * T.finger), Math.sin(_.fingerTap * 0.07 * T.finger));
341
+ const C = -_.handAngle / 10 * 0.045 * T.wrist, Z = -_.armAngle / 10 * 0.015 * T.arm;
342
+ F = G(F, D, Math.cos(C), Math.sin(C)), F = G(F, $, Math.cos(Z), Math.sin(Z)), F = Y.tr(F);
343
+ const E = Y.b * T.hand, R = 1 + (Y.k - 1) * T.hand;
344
+ return F = G(F, Y.w, Math.cos(E) * R, Math.sin(E) * R), F[1] += (M - F[1]) * T.pinBottom, X[0] = F[0], X[1] = F[1], X;
345
+ }
346
+ const V = n.accessories ?? [];
347
+ function K(x, M, T, _) {
348
+ const Y = A(x.pivot[0], x.pivot[1]);
349
+ return U(x.pivot[0], x.pivot[1], Y, M, T, _);
350
+ }
351
+ return { IMG: e, EYES: a, MOUTH: p, CHEEKS: b, STRANDS: i, TASSELS: V, headWeight: w, turnWeight: o, baseWeights: A, deformBase: U, applyHead: j, applyBody: k, handWeights: H, handFrame: W, deformHand: I, eyePartY: m, eyePartAlpha: c, tasselAnchor: K };
352
352
  }
353
- function xn(n, e, t, r = !1) {
354
- const a = r ? Math.max(e, t) * 2 : 1, o = new Float32Array(n.length), i = Math.SQRT2;
355
- for (let f = 0; f < t; f++) for (let m = 0; m < e; m++) {
356
- const g = f * e + m;
357
- n[g] <= 3 || (o[g] = Math.min(
358
- m ? o[g - 1] + 1 : a,
359
- f ? o[g - e] + 1 : a,
360
- m && f ? o[g - e - 1] + i : a,
361
- f && m + 1 < e ? o[g - e + 1] + i : a
353
+ function wn(n, e, t, r = !1) {
354
+ const a = r ? Math.max(e, t) * 2 : 1, s = new Float32Array(n.length), l = Math.SQRT2;
355
+ for (let f = 0; f < t; f++) for (let y = 0; y < e; y++) {
356
+ const d = f * e + y;
357
+ n[d] <= 3 || (s[d] = Math.min(
358
+ y ? s[d - 1] + 1 : a,
359
+ f ? s[d - e] + 1 : a,
360
+ y && f ? s[d - e - 1] + l : a,
361
+ f && y + 1 < e ? s[d - e + 1] + l : a
362
362
  ));
363
363
  }
364
- for (let f = t - 1; f >= 0; f--) for (let m = e - 1; m >= 0; m--) {
365
- const g = f * e + m;
366
- !n[g] || n[g] <= 3 || (o[g] = Math.min(
367
- o[g],
368
- m + 1 < e ? o[g + 1] + 1 : a,
369
- f + 1 < t ? o[g + e] + 1 : a,
370
- m && f + 1 < t ? o[g + e - 1] + i : a,
371
- m + 1 < e && f + 1 < t ? o[g + e + 1] + i : a
364
+ for (let f = t - 1; f >= 0; f--) for (let y = e - 1; y >= 0; y--) {
365
+ const d = f * e + y;
366
+ !n[d] || n[d] <= 3 || (s[d] = Math.min(
367
+ s[d],
368
+ y + 1 < e ? s[d + 1] + 1 : a,
369
+ f + 1 < t ? s[d + e] + 1 : a,
370
+ y && f + 1 < t ? s[d + e - 1] + l : a,
371
+ y + 1 < e && f + 1 < t ? s[d + e + 1] + l : a
372
372
  ));
373
373
  }
374
- return o;
374
+ return s;
375
375
  }
376
- function et(n, e, t) {
376
+ function rt(n, e, t) {
377
377
  return Math.exp(-(((n - t.cx) / t.rx) ** 2 + ((e - t.cy) / t.ry) ** 2));
378
378
  }
379
- function wn(n, e, t) {
379
+ function An(n, e, t) {
380
380
  const r = ((n - t.head.cx) / t.head.rx) ** 2 + ((e - t.head.cy) / t.head.ry) ** 2;
381
- let a = t.head.rx * 0.8 * Math.sqrt(Math.max(0, 1 - r)) + t.body.chest.rx * 0.23 * et(n, e, t.body.chest);
382
- a += t.face.nose.rx * 0.2 * et(n, e, t.face.nose);
383
- for (const [o, i] of t.cheeks) a += t.head.rx * 0.012 * et(n, e, { cx: o, cy: i, rx: t.head.rx * 0.3, ry: t.head.ry * 0.16 });
381
+ let a = t.head.rx * 0.8 * Math.sqrt(Math.max(0, 1 - r)) + t.body.chest.rx * 0.23 * rt(n, e, t.body.chest);
382
+ a += t.face.nose.rx * 0.2 * rt(n, e, t.face.nose);
383
+ for (const [s, l] of t.cheeks) a += t.head.rx * 0.012 * rt(n, e, { cx: s, cy: l, rx: t.head.rx * 0.3, ry: t.head.ry * 0.16 });
384
384
  return a;
385
385
  }
386
- function Ve(n, e, t, r) {
387
- const a = new Float32Array(n.length), o = new Float32Array(n.length), i = (f, m, g, p, l, h) => {
388
- for (let S = 0; S < p; S++) {
389
- const y = S * h;
390
- let u = 0, b = 0;
391
- for (let x = 0; x < Math.min(r, g - 1) + 1; x++)
392
- u += f[y + x * l], b++;
393
- for (let x = 0; x < g; x++) {
394
- m[y + x * l] = u / b;
395
- const s = x + r + 1, c = x - r;
396
- s < g && (u += f[y + s * l], b++), c >= 0 && (u -= f[y + c * l], b--);
386
+ function Ke(n, e, t, r) {
387
+ const a = new Float32Array(n.length), s = new Float32Array(n.length), l = (f, y, d, m, c, p) => {
388
+ for (let b = 0; b < m; b++) {
389
+ const v = b * p;
390
+ let h = 0, u = 0;
391
+ for (let w = 0; w < Math.min(r, d - 1) + 1; w++)
392
+ h += f[v + w * c], u++;
393
+ for (let w = 0; w < d; w++) {
394
+ y[v + w * c] = h / u;
395
+ const o = w + r + 1, i = w - r;
396
+ o < d && (h += f[v + o * c], u++), i >= 0 && (h -= f[v + i * c], u--);
397
397
  }
398
398
  }
399
399
  };
400
- return i(n, a, e, t, 1, e), i(a, o, t, e, e, 1), o;
400
+ return l(n, a, e, t, 1, e), l(a, s, t, e, e, 1), s;
401
401
  }
402
- function An(n, e, t, r) {
403
- const a = Ve(n, t, r, 1), o = Ve(n, t, r, 6), i = new Float32Array(n.length);
404
- for (let g = 0; g < i.length; g++) i[g] = e[g] > 128 ? a[g] - o[g] : 0;
405
- const f = new Float32Array(n.length * 2), m = (g, p) => i[Math.max(0, Math.min(r - 1, p)) * t + Math.max(0, Math.min(t - 1, g))];
406
- for (let g = 0; g < r; g++) for (let p = 0; p < t; p++) {
407
- const l = g * t + p;
408
- e[l] <= 128 || (f[2 * l] = Math.max(-1, Math.min(1, -(m(p + 1, g) - m(p - 1, g)) * 4)), f[2 * l + 1] = Math.max(-1, Math.min(1, -(m(p, g + 1) - m(p, g - 1)) * 4)));
402
+ function Mn(n, e, t, r) {
403
+ const a = Ke(n, t, r, 1), s = Ke(n, t, r, 6), l = new Float32Array(n.length);
404
+ for (let d = 0; d < l.length; d++) l[d] = e[d] > 128 ? a[d] - s[d] : 0;
405
+ const f = new Float32Array(n.length * 2), y = (d, m) => l[Math.max(0, Math.min(r - 1, m)) * t + Math.max(0, Math.min(t - 1, d))];
406
+ for (let d = 0; d < r; d++) for (let m = 0; m < t; m++) {
407
+ const c = d * t + m;
408
+ e[c] <= 128 || (f[2 * c] = Math.max(-1, Math.min(1, -(y(m + 1, d) - y(m - 1, d)) * 4)), f[2 * c + 1] = Math.max(-1, Math.min(1, -(y(m, d + 1) - y(m, d - 1)) * 4)));
409
409
  }
410
410
  return f;
411
411
  }
412
- function Mn(n, e, t, r, a, o = !0, i) {
413
- const f = !!a && r[0] === 0 && r[1] === 0 && r[2] === a.image.width && r[3] === a.image.height, m = xn(n, e, t, f), g = new Float32Array(n.length), p = r[2] / e, l = r[3] / t, h = Math.min(p, l), S = Math.min(r[2], r[3]) * 0.08;
414
- for (let s = 0; s < t; s++) for (let c = 0; c < e; c++) {
415
- const T = s * e + c;
416
- g[T] = (o ? Math.sqrt(m[T] * h * S) * 0.55 : 0) + (a ? wn(r[0] + (c + 0.5) * p, r[1] + (s + 0.5) * l, a) : 0);
417
- }
418
- const y = Math.max(1, Math.round(Math.min(e, t) * 0.02));
419
- g.set(Ve(Ve(g, e, t, y), e, t, y));
420
- const u = i ? An(i, n, e, t) : null, b = new Uint8Array(e * t * 4), x = (s, c) => g[Math.max(0, Math.min(t - 1, c)) * e + Math.max(0, Math.min(e - 1, s))];
421
- for (let s = 0; s < t; s++) for (let c = 0; c < e; c++) {
422
- const T = -(x(c + 2, s) - x(c - 2, s)) / (4 * p), d = -(x(c, s + 2) - x(c, s - 2)) / (4 * l), w = Math.hypot(T, d, 1), B = (s * e + c) * 4, Y = s * e + c;
423
- b[B] = Math.round((T / w * 0.5 + 0.5) * 255), b[B + 1] = Math.round((d / w * 0.5 + 0.5) * 255), b[B + 2] = Math.round(((u?.[2 * Y] ?? 0) * 0.5 + 0.5) * 255), b[B + 3] = Math.round(((u?.[2 * Y + 1] ?? 0) * 0.5 + 0.5) * 255);
424
- }
425
- return b;
412
+ function En(n, e, t, r, a, s = !0, l) {
413
+ const f = !!a && r[0] === 0 && r[1] === 0 && r[2] === a.image.width && r[3] === a.image.height, y = wn(n, e, t, f), d = new Float32Array(n.length), m = r[2] / e, c = r[3] / t, p = Math.min(m, c), b = Math.min(r[2], r[3]) * 0.08;
414
+ for (let o = 0; o < t; o++) for (let i = 0; i < e; i++) {
415
+ const S = o * e + i;
416
+ d[S] = (s ? Math.sqrt(y[S] * p * b) * 0.55 : 0) + (a ? An(r[0] + (i + 0.5) * m, r[1] + (o + 0.5) * c, a) : 0);
417
+ }
418
+ const v = Math.max(1, Math.round(Math.min(e, t) * 0.02));
419
+ d.set(Ke(Ke(d, e, t, v), e, t, v));
420
+ const h = l ? Mn(l, n, e, t) : null, u = new Uint8Array(e * t * 4), w = (o, i) => d[Math.max(0, Math.min(t - 1, i)) * e + Math.max(0, Math.min(e - 1, o))];
421
+ for (let o = 0; o < t; o++) for (let i = 0; i < e; i++) {
422
+ const S = -(w(i + 2, o) - w(i - 2, o)) / (4 * m), g = -(w(i, o + 2) - w(i, o - 2)) / (4 * c), A = Math.hypot(S, g, 1), L = (o * e + i) * 4, B = o * e + i;
423
+ u[L] = Math.round((S / A * 0.5 + 0.5) * 255), u[L + 1] = Math.round((g / A * 0.5 + 0.5) * 255), u[L + 2] = Math.round(((h?.[2 * B] ?? 0) * 0.5 + 0.5) * 255), u[L + 3] = Math.round(((h?.[2 * B + 1] ?? 0) * 0.5 + 0.5) * 255);
424
+ }
425
+ return u;
426
426
  }
427
- const Ut = /* @__PURE__ */ new WeakMap();
428
- function En(n, e, t, r) {
429
- const a = JSON.stringify([e, t.head, t.body.chest, t.face.nose, t.cheeks, r]), o = Ut.get(n);
430
- if (o?.key === a) return { ...o, computed: !1, ms: 0 };
431
- const i = performance.now(), f = Math.min(1, 512 / Math.max(n.width, n.height)), m = Math.max(1, Math.round(n.width * f)), g = Math.max(1, Math.round(n.height * f)), p = document.createElement("canvas");
432
- p.width = m, p.height = g;
433
- const l = p.getContext("2d", { willReadFrequently: !0 });
434
- l.drawImage(n, 0, 0, m, g);
435
- const h = l.getImageData(0, 0, m, g).data, S = new Uint8Array(m * g), y = new Float32Array(m * g);
436
- for (let b = 0; b < S.length; b++)
437
- S[b] = h[4 * b + 3], y[b] = (0.2126 * h[4 * b] + 0.7152 * h[4 * b + 1] + 0.0722 * h[4 * b + 2]) / 255;
438
- const u = { key: a, data: Mn(S, m, g, e, t, r, r ? y : void 0), width: m, height: g };
439
- return Ut.set(n, u), { ...u, computed: !0, ms: performance.now() - i };
427
+ const Nt = /* @__PURE__ */ new WeakMap();
428
+ function Sn(n, e, t, r) {
429
+ const a = JSON.stringify([e, t.head, t.body.chest, t.face.nose, t.cheeks, r]), s = Nt.get(n);
430
+ if (s?.key === a) return { ...s, computed: !1, ms: 0 };
431
+ const l = performance.now(), f = Math.min(1, 512 / Math.max(n.width, n.height)), y = Math.max(1, Math.round(n.width * f)), d = Math.max(1, Math.round(n.height * f)), m = document.createElement("canvas");
432
+ m.width = y, m.height = d;
433
+ const c = m.getContext("2d", { willReadFrequently: !0 });
434
+ c.drawImage(n, 0, 0, y, d);
435
+ const p = c.getImageData(0, 0, y, d).data, b = new Uint8Array(y * d), v = new Float32Array(y * d);
436
+ for (let u = 0; u < b.length; u++)
437
+ b[u] = p[4 * u + 3], v[u] = (0.2126 * p[4 * u] + 0.7152 * p[4 * u + 1] + 0.0722 * p[4 * u + 2]) / 255;
438
+ const h = { key: a, data: En(b, y, d, e, t, r, r ? v : void 0), width: y, height: d };
439
+ return Nt.set(n, h), { ...h, computed: !0, ms: performance.now() - l };
440
440
  }
441
- const Le = Object.freeze({
441
+ const Ue = Object.freeze({
442
442
  enabled: !1,
443
443
  x: 0.2,
444
444
  y: 0.2,
@@ -455,7 +455,7 @@ const Le = Object.freeze({
455
455
  specular: 0.12,
456
456
  rim: 0.3,
457
457
  detail: 0.4
458
- }), Yt = {
458
+ }), Xt = {
459
459
  x: [0, 1],
460
460
  y: [0, 1],
461
461
  z: [0.1, 2],
@@ -469,41 +469,41 @@ const Le = Object.freeze({
469
469
  specular: [0, 1],
470
470
  rim: [0, 1],
471
471
  detail: [0, 1]
472
- }, Sn = /* @__PURE__ */ new Set(["color", "ambientColor"]);
473
- function Tn(n) {
472
+ }, Tn = /* @__PURE__ */ new Set(["color", "ambientColor"]);
473
+ function On(n) {
474
474
  if (!n || typeof n != "object" || Array.isArray(n)) return null;
475
475
  const e = n;
476
- if (Object.keys(e).length !== Object.keys(Le).length || Object.keys(e).some((r) => !(r in Le)) || ![!0, !1].includes(e.enabled) || ![!0, !1].includes(e.shadow) || !["soft", "cel"].includes(e.mode)) return null;
477
- const t = { ...Le, enabled: e.enabled, shadow: e.shadow, mode: e.mode };
478
- for (const r of Object.keys(Yt)) {
479
- const a = e[r], [o, i] = Yt[r];
480
- if (typeof a != "number" || !Number.isFinite(a) || Sn.has(r) && !Number.isInteger(a)) return null;
481
- t[r] = Math.max(o, Math.min(i, a));
476
+ if (Object.keys(e).length !== Object.keys(Ue).length || Object.keys(e).some((r) => !(r in Ue)) || ![!0, !1].includes(e.enabled) || ![!0, !1].includes(e.shadow) || !["soft", "cel"].includes(e.mode)) return null;
477
+ const t = { ...Ue, enabled: e.enabled, shadow: e.shadow, mode: e.mode };
478
+ for (const r of Object.keys(Xt)) {
479
+ const a = e[r], [s, l] = Xt[r];
480
+ if (typeof a != "number" || !Number.isFinite(a) || Tn.has(r) && !Number.isInteger(a)) return null;
481
+ t[r] = Math.max(s, Math.min(l, a));
482
482
  }
483
483
  return t;
484
484
  }
485
- function kr(n) {
485
+ function Ur(n) {
486
486
  return `#${n.toString(16).padStart(6, "0")}`;
487
487
  }
488
- const We = -0.1, On = 0.9, Rn = 1, Ln = 1.06;
488
+ const Ve = -0.1, Rn = 0.9, Ln = 1, _n = 1.06;
489
489
  function kn(n, e) {
490
490
  return [(0.5 - n) * 0.12, (0.5 - e) * 0.12];
491
491
  }
492
- function _n(n, e, t, r) {
493
- const a = n * Math.PI / 180, o = e * Math.PI / 180, i = -t / 30 * r, f = Math.cos(a), m = Math.sin(a), g = Math.cos(o), p = Math.sin(o), l = Math.cos(i), h = Math.sin(i);
492
+ function Fn(n, e, t, r) {
493
+ const a = n * Math.PI / 180, s = e * Math.PI / 180, l = -t / 30 * r, f = Math.cos(a), y = Math.sin(a), d = Math.cos(s), m = Math.sin(s), c = Math.cos(l), p = Math.sin(l);
494
494
  return [
495
- l * f,
496
- h * f,
497
- -m,
498
- l * m * p - h * g,
499
- h * m * p + l * g,
500
- f * p,
501
- l * m * g + h * p,
502
- h * m * g - l * p,
503
- f * g
495
+ c * f,
496
+ p * f,
497
+ -y,
498
+ c * y * m - p * d,
499
+ p * y * m + c * d,
500
+ f * m,
501
+ c * y * d + p * m,
502
+ p * y * d - c * m,
503
+ f * d
504
504
  ];
505
505
  }
506
- class Fn {
506
+ class Bn {
507
507
  constructor(e, t) {
508
508
  this.gl = e, this.program = t(e, `#version 300 es
509
509
  out vec2 vUv;
@@ -521,19 +521,19 @@ class Fn {
521
521
  }`), this.vao = e.createVertexArray(), this.targets = [];
522
522
  }
523
523
  target(e, t) {
524
- const r = this.gl, a = r.createTexture(), o = r.createFramebuffer();
525
- if (r.bindTexture(r.TEXTURE_2D, a), r.texImage2D(r.TEXTURE_2D, 0, r.RGBA, e, t, 0, r.RGBA, r.UNSIGNED_BYTE, null), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MIN_FILTER, r.LINEAR), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MAG_FILTER, r.LINEAR), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_S, r.CLAMP_TO_EDGE), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_T, r.CLAMP_TO_EDGE), r.bindFramebuffer(r.FRAMEBUFFER, o), r.framebufferTexture2D(r.FRAMEBUFFER, r.COLOR_ATTACHMENT0, r.TEXTURE_2D, a, 0), r.checkFramebufferStatus(r.FRAMEBUFFER) !== r.FRAMEBUFFER_COMPLETE) throw new Error("Shadow framebuffer unavailable");
526
- return { texture: a, framebuffer: o };
524
+ const r = this.gl, a = r.createTexture(), s = r.createFramebuffer();
525
+ if (r.bindTexture(r.TEXTURE_2D, a), r.texImage2D(r.TEXTURE_2D, 0, r.RGBA, e, t, 0, r.RGBA, r.UNSIGNED_BYTE, null), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MIN_FILTER, r.LINEAR), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_MAG_FILTER, r.LINEAR), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_S, r.CLAMP_TO_EDGE), r.texParameteri(r.TEXTURE_2D, r.TEXTURE_WRAP_T, r.CLAMP_TO_EDGE), r.bindFramebuffer(r.FRAMEBUFFER, s), r.framebufferTexture2D(r.FRAMEBUFFER, r.COLOR_ATTACHMENT0, r.TEXTURE_2D, a, 0), r.checkFramebufferStatus(r.FRAMEBUFFER) !== r.FRAMEBUFFER_COMPLETE) throw new Error("Shadow framebuffer unavailable");
526
+ return { texture: a, framebuffer: s };
527
527
  }
528
528
  begin(e, t) {
529
529
  const r = this.gl;
530
530
  (this.width !== e || this.height !== t) && (this.clearTargets(), this.width = e, this.height = t, this.targets = [this.target(e, t), this.target(Math.ceil(e / 2), Math.ceil(t / 2))]), r.bindFramebuffer(r.FRAMEBUFFER, this.targets[0].framebuffer), r.viewport(0, 0, e, t), r.clearColor(0, 0, 0, 0), r.clear(r.COLOR_BUFFER_BIT);
531
531
  }
532
532
  composite(e) {
533
- const t = this.gl, { prog: r, u: a } = this.program, o = this.width, i = this.height;
534
- t.useProgram(r), t.bindVertexArray(this.vao), t.activeTexture(t.TEXTURE0), t.uniform1i(a.uTex, 0), t.disable(t.BLEND), t.bindFramebuffer(t.FRAMEBUFFER, this.targets[1].framebuffer), t.viewport(0, 0, Math.ceil(o / 2), Math.ceil(i / 2)), t.bindTexture(t.TEXTURE_2D, this.targets[0].texture), t.uniform1i(a.uPass, 0), t.uniform2f(a.uShift, 0, 0), t.uniform2f(a.uStep, 3 / o, 0), t.drawArrays(t.TRIANGLES, 0, 3), t.bindFramebuffer(t.FRAMEBUFFER, null), t.viewport(0, 0, o, i), t.clearColor(0, 0, 0, 0), t.clear(t.COLOR_BUFFER_BIT), t.enable(t.BLEND), t.bindTexture(t.TEXTURE_2D, this.targets[1].texture);
533
+ const t = this.gl, { prog: r, u: a } = this.program, s = this.width, l = this.height;
534
+ t.useProgram(r), t.bindVertexArray(this.vao), t.activeTexture(t.TEXTURE0), t.uniform1i(a.uTex, 0), t.disable(t.BLEND), t.bindFramebuffer(t.FRAMEBUFFER, this.targets[1].framebuffer), t.viewport(0, 0, Math.ceil(s / 2), Math.ceil(l / 2)), t.bindTexture(t.TEXTURE_2D, this.targets[0].texture), t.uniform1i(a.uPass, 0), t.uniform2f(a.uShift, 0, 0), t.uniform2f(a.uStep, 3 / s, 0), t.drawArrays(t.TRIANGLES, 0, 3), t.bindFramebuffer(t.FRAMEBUFFER, null), t.viewport(0, 0, s, l), t.clearColor(0, 0, 0, 0), t.clear(t.COLOR_BUFFER_BIT), t.enable(t.BLEND), t.bindTexture(t.TEXTURE_2D, this.targets[1].texture);
535
535
  const f = kn(e.x, e.y);
536
- t.uniform1i(a.uPass, 1), t.uniform2f(a.uShift, f[0], -f[1]), t.uniform2f(a.uStep, 0, 3 / i), t.drawArrays(t.TRIANGLES, 0, 3), t.bindTexture(t.TEXTURE_2D, this.targets[0].texture), t.uniform1i(a.uPass, 2), t.drawArrays(t.TRIANGLES, 0, 3), t.bindVertexArray(null);
536
+ t.uniform1i(a.uPass, 1), t.uniform2f(a.uShift, f[0], -f[1]), t.uniform2f(a.uStep, 0, 3 / l), t.drawArrays(t.TRIANGLES, 0, 3), t.bindTexture(t.TEXTURE_2D, this.targets[0].texture), t.uniform1i(a.uPass, 2), t.drawArrays(t.TRIANGLES, 0, 3), t.bindVertexArray(null);
537
537
  }
538
538
  clearTargets() {
539
539
  for (const e of this.targets)
@@ -544,8 +544,8 @@ class Fn {
544
544
  this.clearTargets(), this.gl.deleteProgram(this.program.prog), this.gl.deleteVertexArray(this.vao);
545
545
  }
546
546
  }
547
- function Bn(n, e) {
548
- const { IMG: t, EYES: r, MOUTH: a, CHEEKS: o } = n, i = `#version 300 es
547
+ function Un(n, e) {
548
+ const { IMG: t, EYES: r, MOUTH: a, CHEEKS: s } = n, l = `#version 300 es
549
549
  in vec2 aPos; in vec2 aUv;
550
550
  uniform vec2 uScale; uniform vec2 uOffset;
551
551
  out vec2 vUv;
@@ -657,7 +657,7 @@ function Bn(n, e) {
657
657
  c = texAt(p);
658
658
  }
659
659
  outColor = c * uAlpha;
660
- }`, m = i.replace("out vec2 vUv;", "in float aHeadWeight; out float vHeadWeight; out vec2 vScreen; out vec2 vUv;").replace("vUv = aUv;", "vHeadWeight = aHeadWeight; vScreen = vec2((aPos.x * uScale.x + uOffset.x + 1.0) * 0.5, (1.0 - aPos.y * uScale.y - uOffset.y) * 0.5); vUv = aUv;"), g = f.replace("uniform sampler2D uTex;", `uniform sampler2D uTex;
660
+ }`, y = l.replace("out vec2 vUv;", "in float aHeadWeight; out float vHeadWeight; out vec2 vScreen; out vec2 vUv;").replace("vUv = aUv;", "vHeadWeight = aHeadWeight; vScreen = vec2((aPos.x * uScale.x + uOffset.x + 1.0) * 0.5, (1.0 - aPos.y * uScale.y - uOffset.y) * 0.5); vUv = aUv;"), d = f.replace("uniform sampler2D uTex;", `uniform sampler2D uTex;
661
661
  uniform sampler2D uNormal; in float vHeadWeight; in vec2 vScreen;
662
662
  uniform mat3 uHeadRotation; uniform vec3 uLight; uniform vec3 uLightColor; uniform vec3 uAmbientColor;
663
663
  uniform vec4 uLighting; uniform vec4 uSurface; uniform float uAspect; uniform int uCel;`).replace("outColor = c * uAlpha;", `
@@ -665,7 +665,7 @@ function Bn(n, e) {
665
665
  vec4 encoded = texture(uNormal, vUv);
666
666
  vec2 base = encoded.rg * 2.0 - 1.0;
667
667
  vec3 n = vec3(base, sqrt(max(0.0, 1.0 - dot(base, base))));
668
- n = normalize(n + vec3((encoded.ba * 2.0 - 1.0) * uSurface.w * ${On.toFixed(3)}, 0.0));
668
+ n = normalize(n + vec3((encoded.ba * 2.0 - 1.0) * uSurface.w * ${Rn.toFixed(3)}, 0.0));
669
669
  n = normalize(mix(n, uHeadRotation * n, vHeadWeight));
670
670
  vec2 toLight = (uLight.xy - vScreen) * vec2(uAspect, 1.0);
671
671
  vec3 light = normalize(vec3(toLight, uLight.z));
@@ -673,21 +673,21 @@ function Bn(n, e) {
673
673
  float diffuse;
674
674
  if (uCel == 1) {
675
675
  float w = 0.01 + uSurface.x * 0.08;
676
- diffuse = 0.2 + 0.45 * smoothstep(${We.toFixed(3)} - w, ${We.toFixed(3)} + w, ndl) + 0.35 * smoothstep(0.55 - w, 0.55 + w, ndl);
676
+ diffuse = 0.2 + 0.45 * smoothstep(${Ve.toFixed(3)} - w, ${Ve.toFixed(3)} + w, ndl) + 0.35 * smoothstep(0.55 - w, 0.55 + w, ndl);
677
677
  } else {
678
678
  float s = 0.05 + uSurface.x * 0.5;
679
- diffuse = smoothstep(${We.toFixed(3)} - s, ${We.toFixed(3)} + s, ndl) * (0.35 + 0.65 * max(0.0, ndl));
679
+ diffuse = smoothstep(${Ve.toFixed(3)} - s, ${Ve.toFixed(3)} + s, ndl) * (0.35 + 0.65 * max(0.0, ndl));
680
680
  }
681
681
  float reach = dot(toLight, toLight) / (uLighting.w * uLighting.w);
682
682
  float attenuation = 1.0 / (1.0 + reach);
683
683
  vec3 radiance = uLightColor * uLighting.y * attenuation;
684
684
  // A light close to the surface may brighten a little, but must not bleach painted colours.
685
- vec3 irradiance = min(uAmbientColor * uLighting.z + radiance * diffuse, vec3(${Ln.toFixed(3)}));
685
+ vec3 irradiance = min(uAmbientColor * uLighting.z + radiance * diffuse, vec3(${_n.toFixed(3)}));
686
686
  // Painted shadows are deeper versions of the base colour, not grey: multiply by the colour
687
687
  // itself as the surface turns away from the light.
688
688
  vec3 albedo = c.rgb / max(c.a, 1e-4);
689
689
  float dark = 1.0 - clamp(dot(irradiance, vec3(0.3333)), 0.0, 1.0);
690
- vec3 shaded = c.rgb * mix(vec3(1.0), albedo, dark * ${Rn.toFixed(3)}) * irradiance;
690
+ vec3 shaded = c.rgb * mix(vec3(1.0), albedo, dark * ${Ln.toFixed(3)}) * irradiance;
691
691
  float specular = pow(max(dot(n, normalize(light + vec3(0.0, 0.0, 1.0))), 0.0), 60.0) * uSurface.y;
692
692
  if (uCel == 1) specular = smoothstep(0.3, 0.35, specular) * uSurface.y;
693
693
  vec2 side = length(n.xy) > 0.001 ? normalize(n.xy) : vec2(0.0);
@@ -695,352 +695,398 @@ function Bn(n, e) {
695
695
  shaded += radiance * (specular * max(0.0, ndl) + rim) * c.a;
696
696
  // Strength blends between the original painting and the relit result; alpha is unchanged.
697
697
  c.rgb = clamp(mix(c.rgb, shaded, uLighting.x), vec3(0.0), vec3(c.a));
698
- outColor = c * uAlpha;`), p = `#version 300 es
698
+ outColor = c * uAlpha;`), m = `#version 300 es
699
699
  in vec2 aPos; uniform vec2 uScale; uniform vec2 uOffset;
700
- void main() { gl_Position = vec4(aPos * uScale + uOffset, 0.0, 1.0); gl_PointSize = 7.0; }`, l = `#version 300 es
700
+ void main() { gl_Position = vec4(aPos * uScale + uOffset, 0.0, 1.0); gl_PointSize = 7.0; }`, c = `#version 300 es
701
701
  precision mediump float; uniform vec4 uColor; out vec4 o; void main() { o = uColor; }`;
702
- function h(b, x, s) {
703
- const c = b.createProgram();
704
- for (const [w, B] of [[b.VERTEX_SHADER, x], [b.FRAGMENT_SHADER, s]]) {
705
- const Y = b.createShader(w);
706
- if (b.shaderSource(Y, B), b.compileShader(Y), !b.getShaderParameter(Y, b.COMPILE_STATUS)) throw new Error(b.getShaderInfoLog(Y));
707
- b.attachShader(c, Y), b.deleteShader(Y);
708
- }
709
- if (b.bindAttribLocation(c, 0, "aPos"), b.bindAttribLocation(c, 1, "aUv"), b.bindAttribLocation(c, 2, "aHeadWeight"), b.linkProgram(c), !b.getProgramParameter(c, b.LINK_STATUS)) throw new Error(b.getProgramInfoLog(c));
710
- const T = {}, d = b.getProgramParameter(c, b.ACTIVE_UNIFORMS);
711
- for (let w = 0; w < d; w++) {
712
- const B = b.getActiveUniform(c, w).name.replace("[0]", "");
713
- T[B] = b.getUniformLocation(c, B);
714
- }
715
- return { prog: c, u: T };
716
- }
717
- function S(b, x, s, c) {
718
- const T = [b];
719
- let d = b;
720
- for (; d < b + x - 0.5; ) {
721
- const w = c && d >= c.from && d < c.to ? c.cell : s;
722
- d = Math.min(b + x, d + w), T.push(d);
723
- }
724
- return T;
725
- }
726
- function y(b, x, s, c, T = null) {
727
- const [d, w, B, Y] = b, I = S(d, B, x, T && { from: T.x0, to: T.x1, cell: T.cell }), _ = S(w, Y, x, T && { from: T.y0, to: T.y1, cell: T.cell }), U = I.length - 1, O = _.length - 1, C = (U + 1) * (O + 1), $ = new Float32Array(C * 2), V = new Float32Array(C * 2);
728
- for (let Z = 0; Z <= O; Z++) for (let P = 0; P <= U; P++) {
729
- const G = Z * (U + 1) + P;
730
- $[G * 2] = I[P], $[G * 2 + 1] = _[Z], V[G * 2] = (I[P] - d) / B, V[G * 2 + 1] = (_[Z] - w) / Y;
731
- }
732
- const j = [], D = [], H = s.length / c, R = c === 1 ? 0 : 1;
733
- for (let Z = 0; Z < O; Z++) for (let P = 0; P < U; P++) {
734
- const G = Math.floor(I[P] - d) * R - 2, J = Math.ceil(I[P + 1] - d) * R + 2, q = Math.floor(_[Z] - w) * R - 2, v = Math.ceil(_[Z + 1] - w) * R + 2;
735
- let A = !1;
736
- for (let F = Math.max(0, q); F < Math.min(H, v) && !A; F += 1)
737
- for (let X = Math.max(0, G); X < Math.min(c, J); X += 1)
738
- if (s[F * c + X] > 3) {
739
- A = !0;
702
+ function p(u, w, o) {
703
+ const i = u.createProgram();
704
+ for (const [A, L] of [[u.VERTEX_SHADER, w], [u.FRAGMENT_SHADER, o]]) {
705
+ const B = u.createShader(A);
706
+ if (u.shaderSource(B, L), u.compileShader(B), !u.getShaderParameter(B, u.COMPILE_STATUS)) throw new Error(u.getShaderInfoLog(B));
707
+ u.attachShader(i, B), u.deleteShader(B);
708
+ }
709
+ if (u.bindAttribLocation(i, 0, "aPos"), u.bindAttribLocation(i, 1, "aUv"), u.bindAttribLocation(i, 2, "aHeadWeight"), u.linkProgram(i), !u.getProgramParameter(i, u.LINK_STATUS)) throw new Error(u.getProgramInfoLog(i));
710
+ const S = {}, g = u.getProgramParameter(i, u.ACTIVE_UNIFORMS);
711
+ for (let A = 0; A < g; A++) {
712
+ const L = u.getActiveUniform(i, A).name.replace("[0]", "");
713
+ S[L] = u.getUniformLocation(i, L);
714
+ }
715
+ return { prog: i, u: S };
716
+ }
717
+ function b(u, w, o, i) {
718
+ const S = [u];
719
+ let g = u;
720
+ for (; g < u + w - 0.5; ) {
721
+ const A = i && g >= i.from && g < i.to ? i.cell : o;
722
+ g = Math.min(u + w, g + A), S.push(g);
723
+ }
724
+ return S;
725
+ }
726
+ function v(u, w, o, i, S = null) {
727
+ const [g, A, L, B] = u, j = b(g, L, w, S && { from: S.x0, to: S.x1, cell: S.cell }), k = b(A, B, w, S && { from: S.y0, to: S.y1, cell: S.cell }), U = j.length - 1, O = k.length - 1, $ = (U + 1) * (O + 1), D = new Float32Array($ * 2), q = new Float32Array($ * 2);
728
+ for (let z = 0; z <= O; z++) for (let W = 0; W <= U; W++) {
729
+ const I = z * (U + 1) + W;
730
+ D[I * 2] = j[W], D[I * 2 + 1] = k[z], q[I * 2] = (j[W] - g) / L, q[I * 2 + 1] = (k[z] - A) / B;
731
+ }
732
+ const N = [], P = [], H = o.length / i, G = i === 1 ? 0 : 1;
733
+ for (let z = 0; z < O; z++) for (let W = 0; W < U; W++) {
734
+ const I = Math.floor(j[W] - g) * G - 2, V = Math.ceil(j[W + 1] - g) * G + 2, K = Math.floor(k[z] - A) * G - 2, x = Math.ceil(k[z + 1] - A) * G + 2;
735
+ let M = !1;
736
+ for (let F = Math.max(0, K); F < Math.min(H, x) && !M; F += 1)
737
+ for (let C = Math.max(0, I); C < Math.min(i, V); C += 1)
738
+ if (o[F * i + C] > 3) {
739
+ M = !0;
740
740
  break;
741
741
  }
742
- if (!A) continue;
743
- const E = Z * (U + 1) + P, k = E + 1, N = E + U + 1, z = N + 1;
744
- j.push(E, k, N, k, z, N), D.push(E, k, E, N, k, N), P === U - 1 && D.push(k, z), Z === O - 1 && D.push(N, z);
742
+ if (!M) continue;
743
+ const T = z * (U + 1) + W, _ = T + 1, Y = T + U + 1, X = Y + 1;
744
+ N.push(T, _, Y, _, X, Y), P.push(T, _, T, Y, _, Y), W === U - 1 && P.push(_, X), z === O - 1 && P.push(Y, X);
745
745
  }
746
- return { rect: b, cols: U, rows: O, rest: $, uv: V, pos: new Float32Array($), tris: new Uint32Array(j), lines: new Uint32Array(D) };
746
+ return { rect: u, cols: U, rows: O, rest: D, uv: q, pos: new Float32Array(D), tris: new Uint32Array(N), lines: new Uint32Array(P) };
747
747
  }
748
- class u {
748
+ class h {
749
749
  // padTop / padSide: margin around the image, as a share of its height / width. A negative
750
750
  // padTop pushes the top of the image above the canvas (hides a cut-off top edge).
751
- constructor(x, { padTop: s = 0, padSide: c = 0, fit: T = "contain" } = {}) {
752
- this.pad = { top: s, side: c }, this.fit = T;
753
- const d = x.getContext("webgl2", { premultipliedAlpha: !0, antialias: !0, alpha: !0, preserveDrawingBuffer: !0 });
754
- if (!d) throw new Error("WebGL2 is not available");
755
- this.gl = d, this.canvas = x, this.main = h(d, i, f), this.line = h(d, p, l), this.layers = [], this.lighting = Le, this.lightingStats = { normalMs: 0, computedLayers: 0, cachedLayers: 0 }, d.enable(d.BLEND), d.blendFunc(d.ONE, d.ONE_MINUS_SRC_ALPHA);
756
- }
757
- texture(x) {
758
- const s = this.gl, c = s.createTexture();
759
- return s.bindTexture(s.TEXTURE_2D, c), s.pixelStorei(s.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !0), s.texImage2D(s.TEXTURE_2D, 0, s.RGBA, s.RGBA, s.UNSIGNED_BYTE, x), s.generateMipmap(s.TEXTURE_2D), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_MIN_FILTER, s.LINEAR_MIPMAP_LINEAR), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_MAG_FILTER, s.LINEAR), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_WRAP_S, s.CLAMP_TO_EDGE), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_WRAP_T, s.CLAMP_TO_EDGE), c;
760
- }
761
- addLayer(x, s, c, T = {}) {
762
- const d = this.gl, w = d.createVertexArray();
763
- d.bindVertexArray(w);
764
- const B = d.createBuffer();
765
- d.bindBuffer(d.ARRAY_BUFFER, B), d.bufferData(d.ARRAY_BUFFER, c.pos, d.DYNAMIC_DRAW);
766
- for (const C of [this.main, this.line]) {
767
- const $ = d.getAttribLocation(C.prog, "aPos");
768
- d.enableVertexAttribArray($), d.vertexAttribPointer($, 2, d.FLOAT, !1, 0, 0);
751
+ constructor(w, { padTop: o = 0, padSide: i = 0, fit: S = "contain" } = {}) {
752
+ this.pad = { top: o, side: i }, this.fit = S;
753
+ const g = w.getContext("webgl2", { premultipliedAlpha: !0, antialias: !0, alpha: !0, preserveDrawingBuffer: !0 });
754
+ if (!g) throw new Error("WebGL2 is not available");
755
+ this.gl = g, this.canvas = w, this.main = p(g, l, f), this.line = p(g, m, c), this.layers = [], this.lighting = Ue, this.lightingStats = { normalMs: 0, computedLayers: 0, cachedLayers: 0 }, g.enable(g.BLEND), g.blendFunc(g.ONE, g.ONE_MINUS_SRC_ALPHA);
756
+ }
757
+ texture(w) {
758
+ const o = this.gl, i = o.createTexture();
759
+ return o.bindTexture(o.TEXTURE_2D, i), o.pixelStorei(o.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !0), o.texImage2D(o.TEXTURE_2D, 0, o.RGBA, o.RGBA, o.UNSIGNED_BYTE, w), o.generateMipmap(o.TEXTURE_2D), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MIN_FILTER, o.LINEAR_MIPMAP_LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MAG_FILTER, o.LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_S, o.CLAMP_TO_EDGE), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_T, o.CLAMP_TO_EDGE), i;
760
+ }
761
+ addLayer(w, o, i, S = {}) {
762
+ const g = this.gl, A = g.createVertexArray();
763
+ g.bindVertexArray(A);
764
+ const L = g.createBuffer();
765
+ g.bindBuffer(g.ARRAY_BUFFER, L), g.bufferData(g.ARRAY_BUFFER, i.pos, g.DYNAMIC_DRAW);
766
+ for (const $ of [this.main, this.line]) {
767
+ const D = g.getAttribLocation($.prog, "aPos");
768
+ g.enableVertexAttribArray(D), g.vertexAttribPointer(D, 2, g.FLOAT, !1, 0, 0);
769
769
  }
770
- const Y = d.createBuffer();
771
- d.bindBuffer(d.ARRAY_BUFFER, Y), d.bufferData(d.ARRAY_BUFFER, c.uv, d.STATIC_DRAW);
772
- const I = d.getAttribLocation(this.main.prog, "aUv");
773
- d.enableVertexAttribArray(I), d.vertexAttribPointer(I, 2, d.FLOAT, !1, 0, 0);
774
- const _ = d.createBuffer();
775
- d.bindBuffer(d.ELEMENT_ARRAY_BUFFER, _), d.bufferData(d.ELEMENT_ARRAY_BUFFER, c.tris, d.STATIC_DRAW), d.bindVertexArray(null);
776
- const U = d.createBuffer();
777
- d.bindBuffer(d.ELEMENT_ARRAY_BUFFER, U), d.bufferData(d.ELEMENT_ARRAY_BUFFER, c.lines, d.STATIC_DRAW);
778
- const O = { name: x, image: s, mesh: c, tex: this.texture(s), vao: w, posBuf: B, uvBuf: Y, triBuf: _, lineBuf: U, visible: !0, face: !!T.face, eyeBall: T.eyeBall, color: T.color || [1, 1, 1, 1] };
770
+ const B = g.createBuffer();
771
+ g.bindBuffer(g.ARRAY_BUFFER, B), g.bufferData(g.ARRAY_BUFFER, i.uv, g.STATIC_DRAW);
772
+ const j = g.getAttribLocation(this.main.prog, "aUv");
773
+ g.enableVertexAttribArray(j), g.vertexAttribPointer(j, 2, g.FLOAT, !1, 0, 0);
774
+ const k = g.createBuffer();
775
+ g.bindBuffer(g.ELEMENT_ARRAY_BUFFER, k), g.bufferData(g.ELEMENT_ARRAY_BUFFER, i.tris, g.STATIC_DRAW), g.bindVertexArray(null);
776
+ const U = g.createBuffer();
777
+ g.bindBuffer(g.ELEMENT_ARRAY_BUFFER, U), g.bufferData(g.ELEMENT_ARRAY_BUFFER, i.lines, g.STATIC_DRAW);
778
+ const O = { name: w, image: o, mesh: i, tex: this.texture(o), vao: A, posBuf: L, uvBuf: B, triBuf: k, lineBuf: U, visible: !0, face: !!S.face, eyeBall: S.eyeBall, color: S.color || [1, 1, 1, 1] };
779
779
  return this.layers.push(O), O;
780
780
  }
781
781
  destroy() {
782
- const x = this.gl;
783
- for (const s of this.layers) {
784
- x.deleteTexture(s.tex), s.normal && x.deleteTexture(s.normal), s.headBuf && x.deleteBuffer(s.headBuf), x.deleteVertexArray(s.vao);
785
- for (const c of [s.posBuf, s.uvBuf, s.triBuf, s.lineBuf]) x.deleteBuffer(c);
782
+ const w = this.gl;
783
+ for (const o of this.layers) {
784
+ w.deleteTexture(o.tex), o.normal && w.deleteTexture(o.normal), o.headBuf && w.deleteBuffer(o.headBuf), w.deleteVertexArray(o.vao);
785
+ for (const i of [o.posBuf, o.uvBuf, o.triBuf, o.lineBuf]) w.deleteBuffer(i);
786
786
  }
787
- x.deleteProgram(this.main.prog), x.deleteProgram(this.line.prog), this.lit && x.deleteProgram(this.lit.prog), this.shadow?.destroy(), this.layers.length = 0;
788
- }
789
- setLighting(x) {
790
- if (this.lighting = Tn({ ...Le, ...x }) ?? Le, !this.lighting.enabled) return;
791
- const s = this.gl;
792
- this.lit || (this.lit = h(s, m, g));
793
- for (const c of this.layers) {
794
- if (c.normal || c.overlay) continue;
795
- const T = En(c.image, c.mesh.rect, e, !/^(eye|mouth)/.test(c.name));
796
- this.lightingStats.normalMs += T.ms, this.lightingStats[T.computed ? "computedLayers" : "cachedLayers"]++, c.normal = s.createTexture(), s.activeTexture(s.TEXTURE0), s.bindTexture(s.TEXTURE_2D, c.normal), s.pixelStorei(s.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !1), s.texImage2D(s.TEXTURE_2D, 0, s.RGBA, T.width, T.height, 0, s.RGBA, s.UNSIGNED_BYTE, T.data), s.generateMipmap(s.TEXTURE_2D), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_MIN_FILTER, s.LINEAR_MIPMAP_LINEAR), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_MAG_FILTER, s.LINEAR), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_WRAP_S, s.CLAMP_TO_EDGE), s.texParameteri(s.TEXTURE_2D, s.TEXTURE_WRAP_T, s.CLAMP_TO_EDGE);
797
- const d = new Float32Array(c.mesh.rest.length / 2);
798
- for (let w = 0; w < d.length; w++) d[w] = n.headWeight(c.mesh.rest[w * 2], c.mesh.rest[w * 2 + 1]);
799
- c.headBuf = s.createBuffer(), s.bindVertexArray(c.vao), s.bindBuffer(s.ARRAY_BUFFER, c.headBuf), s.bufferData(s.ARRAY_BUFFER, d, s.STATIC_DRAW), s.enableVertexAttribArray(2), s.vertexAttribPointer(2, 1, s.FLOAT, !1, 0, 0);
787
+ w.deleteProgram(this.main.prog), w.deleteProgram(this.line.prog), this.lit && w.deleteProgram(this.lit.prog), this.shadow?.destroy(), this.layers.length = 0;
788
+ }
789
+ setLighting(w) {
790
+ if (this.lighting = On({ ...Ue, ...w }) ?? Ue, !this.lighting.enabled) return;
791
+ const o = this.gl;
792
+ this.lit || (this.lit = p(o, y, d));
793
+ for (const i of this.layers) {
794
+ if (i.normal || i.overlay) continue;
795
+ const S = Sn(i.image, i.mesh.rect, e, !/^(eye|mouth)/.test(i.name));
796
+ this.lightingStats.normalMs += S.ms, this.lightingStats[S.computed ? "computedLayers" : "cachedLayers"]++, i.normal = o.createTexture(), o.activeTexture(o.TEXTURE0), o.bindTexture(o.TEXTURE_2D, i.normal), o.pixelStorei(o.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !1), o.texImage2D(o.TEXTURE_2D, 0, o.RGBA, S.width, S.height, 0, o.RGBA, o.UNSIGNED_BYTE, S.data), o.generateMipmap(o.TEXTURE_2D), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MIN_FILTER, o.LINEAR_MIPMAP_LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MAG_FILTER, o.LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_S, o.CLAMP_TO_EDGE), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_T, o.CLAMP_TO_EDGE);
797
+ const g = new Float32Array(i.mesh.rest.length / 2);
798
+ for (let A = 0; A < g.length; A++) g[A] = n.headWeight(i.mesh.rest[A * 2], i.mesh.rest[A * 2 + 1]);
799
+ i.headBuf = o.createBuffer(), o.bindVertexArray(i.vao), o.bindBuffer(o.ARRAY_BUFFER, i.headBuf), o.bufferData(o.ARRAY_BUFFER, g, o.STATIC_DRAW), o.enableVertexAttribArray(2), o.vertexAttribPointer(2, 1, o.FLOAT, !1, 0, 0);
800
800
  }
801
- s.bindVertexArray(null), this.lighting.shadow && !this.shadow && (this.shadow = new Fn(s, h));
801
+ o.bindVertexArray(null), this.lighting.shadow && !this.shadow && (this.shadow = new Bn(o, p));
802
802
  }
803
803
  resize() {
804
- const x = this.canvas, s = Math.min(window.devicePixelRatio || 1, 2), c = Math.round(x.clientWidth * s), T = Math.round(x.clientHeight * s);
805
- (x.width !== c || x.height !== T) && (x.width = c, x.height = T);
806
- const d = (this.fit === "cover" ? Math.max : Math.min)(c / (t.w * (1 + 2 * this.pad.side)), T / (t.h * (1 + this.pad.top))), w = (c - t.w * d) / 2, B = (T - t.h * d) / (this.fit === "cover" ? 2 : 1);
807
- this.scale = [2 * d / c, -2 * d / T], this.offset = [-1 + 2 * w / c, 1 - 2 * B / T], this.pxScale = d, this.pxOff = [w, B];
808
- }
809
- draw(x) {
810
- const s = this.gl;
811
- this.resize(), s.viewport(0, 0, this.canvas.width, this.canvas.height), s.clearColor(0, 0, 0, 0), s.clear(s.COLOR_BUFFER_BIT);
812
- const c = this.lighting.enabled, T = c && this.lighting.shadow;
813
- T && this.shadow.begin(this.canvas.width, this.canvas.height);
814
- const d = c ? this.lit : this.main;
815
- if (this.current = d, s.useProgram(d.prog), c) {
816
- const w = this.lighting;
817
- s.uniform1i(d.u.uNormal, 1), s.uniform3f(d.u.uLight, w.x, w.y, w.z), s.uniform3f(d.u.uLightColor, (w.color >> 16 & 255) / 255, (w.color >> 8 & 255) / 255, (w.color & 255) / 255), s.uniform3f(d.u.uAmbientColor, (w.ambientColor >> 16 & 255) / 255, (w.ambientColor >> 8 & 255) / 255, (w.ambientColor & 255) / 255), s.uniform4f(d.u.uLighting, w.strength, w.intensity, w.ambient, w.reach), s.uniform4f(d.u.uSurface, w.softness, w.specular, w.rim, w.detail), s.uniform1f(d.u.uAspect, this.canvas.width / this.canvas.height), s.uniform1i(d.u.uCel, w.mode === "cel" ? 1 : 0), s.uniformMatrix3fv(d.u.uHeadRotation, !1, _n(x.angleX ?? 0, x.angleY ?? 0, x.angleZ ?? 0, e.head.maxRoll));
804
+ const w = this.canvas, o = Math.min(window.devicePixelRatio || 1, 2), i = Math.round(w.clientWidth * o), S = Math.round(w.clientHeight * o);
805
+ (w.width !== i || w.height !== S) && (w.width = i, w.height = S);
806
+ const g = (this.fit === "cover" ? Math.max : Math.min)(i / (t.w * (1 + 2 * this.pad.side)), S / (t.h * (1 + this.pad.top))), A = (i - t.w * g) / 2, L = (S - t.h * g) / (this.fit === "cover" ? 2 : 1);
807
+ this.scale = [2 * g / i, -2 * g / S], this.offset = [-1 + 2 * A / i, 1 - 2 * L / S], this.pxScale = g, this.pxOff = [A, L];
808
+ }
809
+ draw(w) {
810
+ const o = this.gl;
811
+ this.resize(), o.viewport(0, 0, this.canvas.width, this.canvas.height), o.clearColor(0, 0, 0, 0), o.clear(o.COLOR_BUFFER_BIT);
812
+ const i = this.lighting.enabled, S = i && this.lighting.shadow;
813
+ S && this.shadow.begin(this.canvas.width, this.canvas.height);
814
+ const g = i ? this.lit : this.main;
815
+ if (this.current = g, o.useProgram(g.prog), i) {
816
+ const A = this.lighting;
817
+ o.uniform1i(g.u.uNormal, 1), o.uniform3f(g.u.uLight, A.x, A.y, A.z), o.uniform3f(g.u.uLightColor, (A.color >> 16 & 255) / 255, (A.color >> 8 & 255) / 255, (A.color & 255) / 255), o.uniform3f(g.u.uAmbientColor, (A.ambientColor >> 16 & 255) / 255, (A.ambientColor >> 8 & 255) / 255, (A.ambientColor & 255) / 255), o.uniform4f(g.u.uLighting, A.strength, A.intensity, A.ambient, A.reach), o.uniform4f(g.u.uSurface, A.softness, A.specular, A.rim, A.detail), o.uniform1f(g.u.uAspect, this.canvas.width / this.canvas.height), o.uniform1i(g.u.uCel, A.mode === "cel" ? 1 : 0), o.uniformMatrix3fv(g.u.uHeadRotation, !1, Fn(w.angleX ?? 0, w.angleY ?? 0, w.angleZ ?? 0, e.head.maxRoll));
818
818
  }
819
- s.uniform2fv(d.u.uScale, this.scale), s.uniform2fv(d.u.uOffset, this.offset), s.uniform1i(d.u.uTex, 0), s.uniform4fv(d.u.uEyeBox, r.flatMap((w) => [w.x0, w.x1, 0, 0])), s.uniform1fv(d.u.uEyeTop, r.flatMap((w) => w.top)), s.uniform1fv(d.u.uEyeBot, r.flatMap((w) => w.bot)), s.uniform4fv(d.u.uEyeState, x.eyes), s.uniform4f(d.u.uMouth, a.cx, a.cy, a.angle, a.halfLen), s.uniform4f(d.u.uMouthState, x.mouthOpen, x.mouthForm, a.bow, 0), s.uniform4f(d.u.uCheeks, o[0][0], o[0][1], o[1][0], o[1][1]), s.uniform1f(d.u.uCheek, x.cheek), s.activeTexture(s.TEXTURE0);
820
- for (const w of this.layers)
821
- !w.visible || w.overlay || this.drawLayer(w, w.alpha ?? 1);
822
- if (this.original && x.originalAlpha > 0 && this.drawLayer(this.original, x.originalAlpha), T && this.shadow.composite(this.lighting), x.showMesh) {
823
- const w = this.line;
824
- s.useProgram(w.prog), s.uniform2fv(w.u.uScale, this.scale), s.uniform2fv(w.u.uOffset, this.offset);
825
- for (const B of this.layers)
826
- !B.visible || B.overlay || (s.bindVertexArray(B.vao), s.bindBuffer(s.ELEMENT_ARRAY_BUFFER, B.lineBuf), s.uniform4fv(w.u.uColor, B.color), s.drawElements(s.LINES, B.mesh.lines.length, s.UNSIGNED_INT, 0), s.bindBuffer(s.ELEMENT_ARRAY_BUFFER, B.triBuf));
827
- x.joints?.length && this.drawPoints(x.joints), s.bindVertexArray(null);
819
+ o.uniform2fv(g.u.uScale, this.scale), o.uniform2fv(g.u.uOffset, this.offset), o.uniform1i(g.u.uTex, 0), o.uniform4fv(g.u.uEyeBox, r.flatMap((A) => [A.x0, A.x1, 0, 0])), o.uniform1fv(g.u.uEyeTop, r.flatMap((A) => A.top)), o.uniform1fv(g.u.uEyeBot, r.flatMap((A) => A.bot)), o.uniform4fv(g.u.uEyeState, w.eyes), o.uniform4f(g.u.uMouth, a.cx, a.cy, a.angle, a.halfLen), o.uniform4f(g.u.uMouthState, w.mouthOpen, w.mouthForm, a.bow, 0), o.uniform4f(g.u.uCheeks, s[0][0], s[0][1], s[1][0], s[1][1]), o.uniform1f(g.u.uCheek, w.cheek), o.activeTexture(o.TEXTURE0);
820
+ for (const A of this.layers)
821
+ !A.visible || A.overlay || this.drawLayer(A, A.alpha ?? 1);
822
+ if (this.original && w.originalAlpha > 0 && this.drawLayer(this.original, w.originalAlpha), S && this.shadow.composite(this.lighting), w.showMesh) {
823
+ const A = this.line;
824
+ o.useProgram(A.prog), o.uniform2fv(A.u.uScale, this.scale), o.uniform2fv(A.u.uOffset, this.offset);
825
+ for (const L of this.layers)
826
+ !L.visible || L.overlay || (o.bindVertexArray(L.vao), o.bindBuffer(o.ELEMENT_ARRAY_BUFFER, L.lineBuf), o.uniform4fv(A.u.uColor, L.color), o.drawElements(o.LINES, L.mesh.lines.length, o.UNSIGNED_INT, 0), o.bindBuffer(o.ELEMENT_ARRAY_BUFFER, L.triBuf));
827
+ w.joints?.length && this.drawPoints(w.joints), o.bindVertexArray(null);
828
828
  }
829
829
  }
830
- drawLayer(x, s) {
831
- const c = this.gl, T = this.current;
832
- c.useProgram(T.prog), c.bindVertexArray(x.vao), c.bindBuffer(c.ARRAY_BUFFER, x.posBuf), c.bufferSubData(c.ARRAY_BUFFER, 0, x.mesh.pos), this.lighting.enabled && (c.activeTexture(c.TEXTURE1), c.bindTexture(c.TEXTURE_2D, x.normal), c.activeTexture(c.TEXTURE0)), c.bindTexture(c.TEXTURE_2D, x.tex), c.uniform4fv(T.u.uRect, x.mesh.rect), c.uniform1i(T.u.uFace, x.eyeBall !== void 0 ? 2 + x.eyeBall : x.face ? 1 : 0), c.uniform1f(T.u.uAlpha, s), c.drawElements(c.TRIANGLES, x.mesh.tris.length, c.UNSIGNED_INT, 0);
830
+ drawLayer(w, o) {
831
+ const i = this.gl, S = this.current;
832
+ i.useProgram(S.prog), i.bindVertexArray(w.vao), i.bindBuffer(i.ARRAY_BUFFER, w.posBuf), i.bufferSubData(i.ARRAY_BUFFER, 0, w.mesh.pos), this.lighting.enabled && (i.activeTexture(i.TEXTURE1), i.bindTexture(i.TEXTURE_2D, w.normal), i.activeTexture(i.TEXTURE0)), i.bindTexture(i.TEXTURE_2D, w.tex), i.uniform4fv(S.u.uRect, w.mesh.rect), i.uniform1i(S.u.uFace, w.eyeBall !== void 0 ? 2 + w.eyeBall : w.face ? 1 : 0), i.uniform1f(S.u.uAlpha, o), i.drawElements(i.TRIANGLES, w.mesh.tris.length, i.UNSIGNED_INT, 0);
833
833
  }
834
- drawPoints(x) {
835
- const s = this.gl, c = this.line;
834
+ drawPoints(w) {
835
+ const o = this.gl, i = this.line;
836
836
  if (!this.pointBuf) {
837
- this.pointBuf = s.createBuffer(), this.pointVao = s.createVertexArray(), s.bindVertexArray(this.pointVao), s.bindBuffer(s.ARRAY_BUFFER, this.pointBuf);
838
- const T = s.getAttribLocation(c.prog, "aPos");
839
- s.enableVertexAttribArray(T), s.vertexAttribPointer(T, 2, s.FLOAT, !1, 0, 0);
837
+ this.pointBuf = o.createBuffer(), this.pointVao = o.createVertexArray(), o.bindVertexArray(this.pointVao), o.bindBuffer(o.ARRAY_BUFFER, this.pointBuf);
838
+ const S = o.getAttribLocation(i.prog, "aPos");
839
+ o.enableVertexAttribArray(S), o.vertexAttribPointer(S, 2, o.FLOAT, !1, 0, 0);
840
840
  }
841
- s.bindVertexArray(this.pointVao), s.bindBuffer(s.ARRAY_BUFFER, this.pointBuf), s.bufferData(s.ARRAY_BUFFER, new Float32Array(x.flat()), s.DYNAMIC_DRAW), s.uniform4f(c.u.uColor, 1, 0.85, 0.2, 1), s.drawArrays(s.POINTS, 0, x.length);
841
+ o.bindVertexArray(this.pointVao), o.bindBuffer(o.ARRAY_BUFFER, this.pointBuf), o.bufferData(o.ARRAY_BUFFER, new Float32Array(w.flat()), o.DYNAMIC_DRAW), o.uniform4f(i.u.uColor, 1, 0.85, 0.2, 1), o.drawArrays(o.POINTS, 0, w.length);
842
842
  }
843
843
  // canvas CSS px -> source image px
844
- toImage(x, s) {
844
+ toImage(w, o) {
845
845
  if (!this.pxOff) return null;
846
- const c = this.canvas.width / this.canvas.clientWidth;
847
- return [(x * c - this.pxOff[0]) / this.pxScale, (s * c - this.pxOff[1]) / this.pxScale];
846
+ const i = this.canvas.width / this.canvas.clientWidth;
847
+ return [(w * i - this.pxOff[0]) / this.pxScale, (o * i - this.pxOff[1]) / this.pxScale];
848
848
  }
849
849
  }
850
- return { Renderer: u, buildGrid: y };
850
+ return { Renderer: h, buildGrid: v };
851
851
  }
852
- function Un(n, e) {
853
- const { applyHead: t, applyBody: r, TASSELS: a, tasselAnchor: o } = n, i = (e.strands ?? []).map((s) => {
854
- if (s.nodes.length === 4) return s;
855
- const c = Array.from({ length: 4 }, (T, d) => {
856
- const w = d / 3 * (s.nodes.length - 1), B = Math.min(s.nodes.length - 2, Math.floor(w)), Y = w - B;
857
- return [0, 1].map((I) => s.nodes[B][I] + (s.nodes[B + 1][I] - s.nodes[B][I]) * Y);
852
+ function Yn(n, e) {
853
+ const { applyHead: t, applyBody: r, TASSELS: a, tasselAnchor: s } = n, l = (e.strands ?? []).map((o) => {
854
+ if (o.nodes.length === 4) return o;
855
+ const i = Array.from({ length: 4 }, (S, g) => {
856
+ const A = g / 3 * (o.nodes.length - 1), L = Math.min(o.nodes.length - 2, Math.floor(A)), B = A - L;
857
+ return [0, 1].map((j) => o.nodes[L][j] + (o.nodes[L + 1][j] - o.nodes[L][j]) * B);
858
858
  });
859
- return { ...s, nodes: c };
860
- }), f = [0, 300, 200, 140], m = 0.9, g = 7, p = { stiff: 1.7, damp: 1.6, couple: 0.85, reach: 0.9, bind: 4 }, l = Math.min(6, i.filter((s) => s.name.startsWith("bang")).length), h = { freq: 0.7, zeta: 0.22, hang: 0.85, inertia: 0.32, wind: 0.03, max: 0.55, smooth: 6 }, S = (s, c) => [s[0] * Math.cos(c) - s[1] * Math.sin(c), s[0] * Math.sin(c) + s[1] * Math.cos(c)], y = (s, c) => {
861
- const T = Math.hypot(s[0], s[1]);
862
- if (T < 1e-6) return s;
863
- const d = c * Math.tanh(T / c) / T;
864
- return [s[0] * d, s[1] * d];
865
- }, u = Object.fromEntries(Object.entries(e.buns ?? {}).map(([s, c]) => [s, { at: [c.cx, c.cy], k: 260, c: 11, hang: 3, max: 4 }])), b = (s, c) => {
866
- const T = Math.hypot(s[0], s[1]);
867
- T > c && (s[0] *= c / T, s[1] *= c / T);
859
+ return { ...o, nodes: i };
860
+ }), f = [0, 300, 200, 140], y = 0.9, d = 7, m = { stiff: 1.7, damp: 1.6, couple: 0.85, reach: 0.9, bind: 4 }, c = Math.min(6, l.filter((o) => o.name.startsWith("bang")).length), p = { freq: 0.7, zeta: 0.22, hang: 0.85, inertia: 0.32, wind: 0.03, max: 0.55, smooth: 6 }, b = (o, i) => [o[0] * Math.cos(i) - o[1] * Math.sin(i), o[0] * Math.sin(i) + o[1] * Math.cos(i)], v = (o, i) => {
861
+ const S = Math.hypot(o[0], o[1]);
862
+ if (S < 1e-6) return o;
863
+ const g = i * Math.tanh(S / i) / S;
864
+ return [o[0] * g, o[1] * g];
865
+ }, h = Object.fromEntries(Object.entries(e.buns ?? {}).map(([o, i]) => [o, { at: [i.cx, i.cy], k: 260, c: 11, hang: 3, max: 4 }])), u = (o, i) => {
866
+ const S = Math.hypot(o[0], o[1]);
867
+ S > i && (o[0] *= i / S, o[1] *= i / S);
868
868
  };
869
- class x {
869
+ class w {
870
870
  constructor() {
871
871
  this.gain = 1, this.out = { gain: 1, bunL: [0, 0], bunR: [0, 0] }, this.hair = {};
872
- for (const c of Object.keys(u))
873
- this.hair[c] = { p: null, v: [0, 0] }, this.out[c] = [0, 0];
874
- this.strands = i.map(() => ({ p: null, v: [[0, 0], [0, 0], [0, 0], [0, 0]] })), this.out.strands = i.map(() => [[0, 0], [0, 0], [0, 0], [0, 0]]), this.chains = a.map((c) => {
875
- const T = c.tip[0] - c.pivot[0], d = c.tip[1] - c.pivot[1], w = Math.hypot(T, d), B = [T / w, d / w];
876
- return { t: c, dir: B, L1: w * c.split, L2: w * (1 - c.split), pts: null, prev: null, phi: [0, 0], anchor: [0, 0] };
872
+ for (const i of Object.keys(h))
873
+ this.hair[i] = { p: null, v: [0, 0] }, this.out[i] = [0, 0];
874
+ this.strands = l.map(() => ({ p: null, v: [[0, 0], [0, 0], [0, 0], [0, 0]] })), this.out.strands = l.map(() => [[0, 0], [0, 0], [0, 0], [0, 0]]), this.chains = a.map((i) => {
875
+ const S = i.tip[0] - i.pivot[0], g = i.tip[1] - i.pivot[1], A = Math.hypot(S, g), L = [S / A, g / A];
876
+ return { t: i, dir: L, L1: A * i.split, L2: A * (1 - i.split), pts: null, prev: null, phi: [0, 0], anchor: [0, 0] };
877
877
  }), this.time = 0;
878
878
  }
879
- hairAnchor(c, T) {
880
- const d = [c[0], c[1]];
881
- return t(d, 1, T), r(d, c[1], T), d;
882
- }
883
- step(c, T) {
884
- const d = Math.max(1, Math.ceil(T / 0.004166666666666667)), w = Math.min(T, 0.05) / d, B = -c.angleZ / 30 * e.head.maxRoll - c.bodyAngleZ / 10 * e.body.maxRoll, Y = this.gain;
885
- i.forEach((_, U) => {
886
- const O = this.strands[U], C = this.out.strands[U], $ = _.nodes.map((j) => this.hairAnchor(j, c));
887
- O.p || (O.p = $.map((j) => [...j]));
888
- const V = U * 1.7;
889
- for (let j = 0; j < d; j++) {
890
- const D = this.time + j * w;
879
+ hairAnchor(i, S) {
880
+ const g = [i[0], i[1]];
881
+ return t(g, 1, S), r(g, i[1], S), g;
882
+ }
883
+ step(i, S) {
884
+ const g = Math.max(1, Math.ceil(S / 0.004166666666666667)), A = Math.min(S, 0.05) / g, L = -i.angleZ / 30 * e.head.maxRoll - i.bodyAngleZ / 10 * e.body.maxRoll, B = this.gain;
885
+ l.forEach((k, U) => {
886
+ const O = this.strands[U], $ = this.out.strands[U], D = k.nodes.map((N) => this.hairAnchor(N, i));
887
+ O.p || (O.p = D.map((N) => [...N]));
888
+ const q = U * 1.7;
889
+ for (let N = 0; N < g; N++) {
890
+ const P = this.time + N * A;
891
891
  for (let H = 1; H < 4; H++) {
892
- const R = Math.hypot(_.nodes[H][0] - _.nodes[0][0], _.nodes[H][1] - _.nodes[0][1]), Z = (Math.sin(D * 1.3) * 0.5 + Math.sin(D * 3.1) * 0.3 + Math.sin(D * 4.7 + V) * 0.2) * _.max * 0.1 * H / 3, P = [O.p[H - 1][0] - $[H - 1][0], O.p[H - 1][1] - $[H - 1][1]], G = $[H][0] + P[0] * m * p.couple - Math.sin(B) * R * 0.12 + Z, J = $[H][1] + P[1] * m * p.couple, q = f[H] * _.k * p.stiff / Math.max(0.35, Y), v = g * p.damp * Math.sqrt(p.stiff) / Math.max(0.5, Math.sqrt(Y)), A = O.v[H], E = O.p[H];
893
- A[0] += (q * (G - E[0]) - v * A[0]) * w, A[1] += (q * (J - E[1]) - v * A[1]) * w, E[0] += A[0] * w, E[1] += A[1] * w;
892
+ const G = Math.hypot(k.nodes[H][0] - k.nodes[0][0], k.nodes[H][1] - k.nodes[0][1]), z = (Math.sin(P * 1.3) * 0.5 + Math.sin(P * 3.1) * 0.3 + Math.sin(P * 4.7 + q) * 0.2) * k.max * 0.1 * H / 3, W = [O.p[H - 1][0] - D[H - 1][0], O.p[H - 1][1] - D[H - 1][1]], I = D[H][0] + W[0] * y * m.couple - Math.sin(L) * G * 0.12 + z, V = D[H][1] + W[1] * y * m.couple, K = f[H] * k.k * m.stiff / Math.max(0.35, B), x = d * m.damp * Math.sqrt(m.stiff) / Math.max(0.5, Math.sqrt(B)), M = O.v[H], T = O.p[H];
893
+ M[0] += (K * (I - T[0]) - x * M[0]) * A, M[1] += (K * (V - T[1]) - x * M[1]) * A, T[0] += M[0] * A, T[1] += M[1] * A;
894
894
  }
895
- O.p[0] = [...$[0]];
895
+ O.p[0] = [...D[0]];
896
896
  }
897
- for (let j = 0; j < 4; j++) {
898
- const D = [O.p[j][0] - $[j][0], O.p[j][1] - $[j][1]];
899
- C[j] = y(D, Math.max(0.5, _.max * p.reach * Math.max(1, Y) * j / 3));
897
+ for (let N = 0; N < 4; N++) {
898
+ const P = [O.p[N][0] - D[N][0], O.p[N][1] - D[N][1]];
899
+ $[N] = v(P, Math.max(0.5, k.max * m.reach * Math.max(1, B) * N / 3));
900
900
  }
901
901
  });
902
- for (const [_, U] of Object.entries(u)) {
903
- const O = this.hair[_], C = this.hairAnchor(U.at, c), $ = Math.sin(this.time * 1.3 + U.at[0] * 0.01) * 0.6 + Math.sin(this.time * 2.9 + U.at[1] * 0.02) * 0.4, V = C[0] - Math.sin(B) * U.hang + $ * U.max * 0.12, j = C[1];
904
- O.p || (O.p = [V, j]);
905
- const D = U.k / Math.max(0.35, Y), H = U.c / Math.max(0.5, Math.sqrt(Y));
906
- for (let Z = 0; Z < d; Z++)
907
- O.v[0] += (D * (V - O.p[0]) - H * O.v[0]) * w, O.v[1] += (D * (j - O.p[1]) - H * O.v[1]) * w, O.p[0] += O.v[0] * w, O.p[1] += O.v[1] * w;
908
- const R = [O.p[0] - C[0], O.p[1] - C[1]];
909
- b(R, U.max * Math.max(1, Y)), this.out[_] = R;
902
+ for (const [k, U] of Object.entries(h)) {
903
+ const O = this.hair[k], $ = this.hairAnchor(U.at, i), D = Math.sin(this.time * 1.3 + U.at[0] * 0.01) * 0.6 + Math.sin(this.time * 2.9 + U.at[1] * 0.02) * 0.4, q = $[0] - Math.sin(L) * U.hang + D * U.max * 0.12, N = $[1];
904
+ O.p || (O.p = [q, N]);
905
+ const P = U.k / Math.max(0.35, B), H = U.c / Math.max(0.5, Math.sqrt(B));
906
+ for (let z = 0; z < g; z++)
907
+ O.v[0] += (P * (q - O.p[0]) - H * O.v[0]) * A, O.v[1] += (P * (N - O.p[1]) - H * O.v[1]) * A, O.p[0] += O.v[0] * A, O.p[1] += O.v[1] * A;
908
+ const G = [O.p[0] - $[0], O.p[1] - $[1]];
909
+ u(G, U.max * Math.max(1, B)), this.out[k] = G;
910
910
  }
911
911
  this.out.gain = 1;
912
- const I = this.out.strands;
913
- for (let _ = 0; _ < p.bind; _++) for (let U = 1; U < 4; U++) {
914
- const O = I.slice(0, l).map((C) => [...C[U]]);
915
- for (let C = 0; C < l; C++) {
916
- const $ = O[Math.max(0, C - 1)], V = O[Math.min(l - 1, C + 1)];
917
- for (const j of [0, 1]) I[C][U][j] = O[C][j] * 0.5 + ($[j] + V[j]) * 0.25;
912
+ const j = this.out.strands;
913
+ for (let k = 0; k < m.bind; k++) for (let U = 1; U < 4; U++) {
914
+ const O = j.slice(0, c).map(($) => [...$[U]]);
915
+ for (let $ = 0; $ < c; $++) {
916
+ const D = O[Math.max(0, $ - 1)], q = O[Math.min(c - 1, $ + 1)];
917
+ for (const N of [0, 1]) j[$][U][N] = O[$][N] * 0.5 + (D[N] + q[N]) * 0.25;
918
918
  }
919
919
  }
920
- for (const _ of this.chains) {
921
- const U = o(_.t, c, this.out, [0, 0]);
922
- _.st || (_.st = { th: [0, 0], w: [0, 0], pos: [...U], vel: [0, 0], acc: [0, 0] });
923
- const O = _.st, C = h, $ = [(U[0] - O.pos[0]) / Math.max(T, 1e-3), (U[1] - O.pos[1]) / Math.max(T, 1e-3)], V = 1 - Math.exp(-T * C.smooth), j = [($[0] - O.vel[0]) / Math.max(T, 1e-3), ($[1] - O.vel[1]) / Math.max(T, 1e-3)];
924
- O.acc = [O.acc[0] + (j[0] - O.acc[0]) * V, O.acc[1] + (j[1] - O.acc[1]) * V], O.vel = [O.vel[0] + ($[0] - O.vel[0]) * V, O.vel[1] + ($[1] - O.vel[1]) * V], O.pos = [...U];
925
- const D = [-_.dir[1], _.dir[0]], H = -(O.acc[0] * D[0] + O.acc[1] * D[1]) / (_.L1 + _.L2) * C.inertia, R = (Math.sin(this.time * 0.9 + _.t.pivot[0]) * 0.6 + Math.sin(this.time * 0.37 + _.t.pivot[1]) * 0.4) * C.wind, Z = -B * C.hang + R, P = 2 * Math.PI * C.freq / Math.sqrt(Math.max(0.4, Y)), G = P * 1.35;
926
- for (let A = 0; A < d; A++) {
927
- const E = -P * P * (O.th[0] - Z) - 2 * C.zeta * P * O.w[0] + H;
928
- O.w[0] += E * w, O.th[0] += O.w[0] * w;
929
- const k = -G * G * (O.th[1] - O.th[0]) - 2 * C.zeta * G * O.w[1] + H * 0.4 + E * 0.5;
930
- O.w[1] += k * w, O.th[1] += O.w[1] * w;
920
+ for (const k of this.chains) {
921
+ const U = s(k.t, i, this.out, [0, 0]);
922
+ k.st || (k.st = { th: [0, 0], w: [0, 0], pos: [...U], vel: [0, 0], acc: [0, 0] });
923
+ const O = k.st, $ = p, D = [(U[0] - O.pos[0]) / Math.max(S, 1e-3), (U[1] - O.pos[1]) / Math.max(S, 1e-3)], q = 1 - Math.exp(-S * $.smooth), N = [(D[0] - O.vel[0]) / Math.max(S, 1e-3), (D[1] - O.vel[1]) / Math.max(S, 1e-3)];
924
+ O.acc = [O.acc[0] + (N[0] - O.acc[0]) * q, O.acc[1] + (N[1] - O.acc[1]) * q], O.vel = [O.vel[0] + (D[0] - O.vel[0]) * q, O.vel[1] + (D[1] - O.vel[1]) * q], O.pos = [...U];
925
+ const P = [-k.dir[1], k.dir[0]], H = -(O.acc[0] * P[0] + O.acc[1] * P[1]) / (k.L1 + k.L2) * $.inertia, G = (Math.sin(this.time * 0.9 + k.t.pivot[0]) * 0.6 + Math.sin(this.time * 0.37 + k.t.pivot[1]) * 0.4) * $.wind, z = -L * $.hang + G, W = 2 * Math.PI * $.freq / Math.sqrt(Math.max(0.4, B)), I = W * 1.35;
926
+ for (let M = 0; M < g; M++) {
927
+ const T = -W * W * (O.th[0] - z) - 2 * $.zeta * W * O.w[0] + H;
928
+ O.w[0] += T * A, O.th[0] += O.w[0] * A;
929
+ const _ = -I * I * (O.th[1] - O.th[0]) - 2 * $.zeta * I * O.w[1] + H * 0.4 + T * 0.5;
930
+ O.w[1] += _ * A, O.th[1] += O.w[1] * A;
931
931
  }
932
- O.th[0] = Math.max(-0.55, Math.min(C.max, O.th[0])), O.th[1] = Math.max(-0.55 * 1.4, Math.min(C.max * 1.4, O.th[1])), _.phi = [O.th[0], O.th[1]];
933
- const J = S(_.dir, O.th[0]), q = S(_.dir, O.th[1]), v = [U[0] + J[0] * _.L1, U[1] + J[1] * _.L1];
934
- _.pts = [[...U], v, [v[0] + q[0] * _.L2, v[1] + q[1] * _.L2]], _.anchor = U;
932
+ O.th[0] = Math.max(-0.55, Math.min($.max, O.th[0])), O.th[1] = Math.max(-0.55 * 1.4, Math.min($.max * 1.4, O.th[1])), k.phi = [O.th[0], O.th[1]];
933
+ const V = b(k.dir, O.th[0]), K = b(k.dir, O.th[1]), x = [U[0] + V[0] * k.L1, U[1] + V[1] * k.L1];
934
+ k.pts = [[...U], x, [x[0] + K[0] * k.L2, x[1] + K[1] * k.L2]], k.anchor = U;
935
935
  }
936
- return this.time += T, this.out;
936
+ return this.time += S, this.out;
937
937
  }
938
938
  }
939
- return { Physics: x };
939
+ return { Physics: w };
940
940
  }
941
- function Yn(n, e) {
942
- const { baseWeights: t, deformBase: r, MOUTH: a } = n, o = (d, w, B) => {
943
- const Y = Math.min(1, Math.max(0, (B - d) / (w - d)));
944
- return Y * Y * (3 - 2 * Y);
945
- }, i = e.mouth.area, f = {
941
+ function jn(n, e) {
942
+ const { baseWeights: t, deformBase: r, MOUTH: a } = n, s = (g, A, L) => {
943
+ const B = Math.min(1, Math.max(0, (L - g) / (A - g)));
944
+ return B * B * (3 - 2 * B);
945
+ }, l = e.mouth.area, f = {
946
946
  a: { sprite: "mouth_a", ref: 0.9, width: 1 },
947
947
  aHalf: { sprite: "mouth_a_half", ref: 0.55, width: 1 },
948
948
  i: { sprite: "mouth_i", ref: 0.55, width: 1 },
949
949
  e: { sprite: "mouth_a_half", ref: 0.65, width: 1.06 },
950
950
  o: { sprite: "mouth_o", ref: 0.75, width: 1 },
951
951
  u: { sprite: "mouth_o", ref: 0.75, width: 0.78 }
952
- }, m = 0.16, g = ["angry", "sad"];
953
- function p(d, w) {
954
- const Y = (U, O, C) => U >= O - (w ? 0.07 : 0) && U < C + (w ? 0.07 : 0), I = [["i", -9, -0.55], ["e", -0.55, -0.2], ["a", -0.2, 0.35], ["o", 0.35, 0.75], ["u", 0.75, 9]], _ = w && I.find((U) => U[0] === w || w === "aHalf" && U[0] === "a");
955
- return _ && Y(d, _[1], _[2]) ? _[0] : I.find((U) => d >= U[1] && d < U[2])[0];
956
- }
957
- const l = 0.75, h = 0.3, S = 0.06, y = ["sad", "surprise"];
958
- function u(d, w, B = null) {
959
- return B && d >= h ? B : d >= l ? null : d < h ? w >= 0.5 ? "eyes_smile" : "eyes_closed" : "eyes_half";
960
- }
961
- function b(d, w, B = null) {
962
- if (d < m) return null;
963
- const Y = p(w, B);
964
- return Y === "a" ? d >= 0.6 ? "a" : "aHalf" : Y;
965
- }
966
- function x(d) {
967
- return a.cy + (d - a.cx) * Math.tan(a.angle);
968
- }
969
- function s() {
970
- return i.cx;
971
- }
972
- function c(d, w) {
973
- const B = Math.cos(-i.angle), Y = Math.sin(-i.angle), I = d - i.cx, _ = w - i.cy, U = (I * B - _ * Y) / i.rx, O = (I * Y + _ * B) / i.ry;
974
- return 1 - o(0.55, 0.95, Math.hypot(U, O));
975
- }
976
- function T(d, w, B, Y, I) {
977
- const _ = {}, U = (R) => R.startsWith("eyes_closed") || R.startsWith("eyes_smile"), O = (R) => g.some((Z) => R === `mouth_${Z}`), C = (R) => U(R) ? 1 : R.startsWith("eyes_") ? 0 : O(R) ? 2 : 3, $ = Object.entries(w.layers).sort((R, Z) => C(R[0]) - C(Z[0]));
978
- for (const [R, Z] of $) {
979
- const P = Y(Z, e.mesh.spriteCell, I(B[R]), B[R].width), G = [], J = [];
980
- for (let v = 0; v < P.rest.length / 2; v++) {
981
- const A = P.rest[v * 2], E = P.rest[v * 2 + 1];
982
- G.push(t(A, E, 0)), J.push(R.startsWith("mouth") ? c(A, E) : 0);
952
+ }, y = 0.16, d = ["angry", "sad"];
953
+ function m(g, A) {
954
+ const B = (U, O, $) => U >= O - (A ? 0.07 : 0) && U < $ + (A ? 0.07 : 0), j = [["i", -9, -0.55], ["e", -0.55, -0.2], ["a", -0.2, 0.35], ["o", 0.35, 0.75], ["u", 0.75, 9]], k = A && j.find((U) => U[0] === A || A === "aHalf" && U[0] === "a");
955
+ return k && B(g, k[1], k[2]) ? k[0] : j.find((U) => g >= U[1] && g < U[2])[0];
956
+ }
957
+ const c = 0.75, p = 0.3, b = 0.06, v = ["sad", "surprise"];
958
+ function h(g, A, L = null) {
959
+ return L && g >= p ? L : g >= c ? null : g < p ? A >= 0.5 ? "eyes_smile" : "eyes_closed" : "eyes_half";
960
+ }
961
+ function u(g, A, L = null) {
962
+ if (g < y) return null;
963
+ const B = m(A, L);
964
+ return B === "a" ? g >= 0.6 ? "a" : "aHalf" : B;
965
+ }
966
+ function w(g) {
967
+ return a.cy + (g - a.cx) * Math.tan(a.angle);
968
+ }
969
+ function o() {
970
+ return l.cx;
971
+ }
972
+ function i(g, A) {
973
+ const L = Math.cos(-l.angle), B = Math.sin(-l.angle), j = g - l.cx, k = A - l.cy, U = (j * L - k * B) / l.rx, O = (j * B + k * L) / l.ry;
974
+ return 1 - s(0.55, 0.95, Math.hypot(U, O));
975
+ }
976
+ function S(g, A, L, B, j) {
977
+ const k = {}, U = (I) => I.startsWith("eyes_closed") || I.startsWith("eyes_smile"), O = (I) => d.some((V) => I === `mouth_${V}`), $ = (I) => U(I) ? 1 : I.startsWith("eyes_") ? 0 : O(I) ? 2 : 3, D = Object.entries(A.layers).sort((I, V) => $(I[0]) - $(V[0]));
978
+ for (const [I, V] of D) {
979
+ const K = B(V, e.mesh.spriteCell, j(L[I]), L[I].width), x = [], M = [];
980
+ for (let _ = 0; _ < K.rest.length / 2; _++) {
981
+ const Y = K.rest[_ * 2], X = K.rest[_ * 2 + 1];
982
+ x.push(t(Y, X, 0)), M.push(I.startsWith("mouth") ? i(Y, X) : 0);
983
983
  }
984
- const q = d.addLayer(R, B[R], P, { color: [0.4, 1, 0.8, 0.6] });
985
- q.visible = !1, _[R] = { mesh: P, W: G, inner: J, layer: q };
986
- }
987
- const V = [0, 0];
988
- let j = null;
989
- const D = [0, 1].map(() => ({ cur: "open", prev: "open", t: 1 }));
990
- function H(R, Z, P, G, J = 1) {
991
- const q = R.mesh.rest, v = R.mesh.pos;
992
- for (let A = 0; A < R.W.length; A++) {
993
- let E = q[A * 2], k = q[A * 2 + 1];
994
- const N = R.inner[A];
995
- if (N > 0) {
996
- const z = x(E);
997
- k = z + (k - z) * (1 + (G - 1) * N), E = s() + (E - s()) * (1 + (J - 1) * N);
984
+ const T = g.addLayer(I, L[I], K, { color: [0.4, 1, 0.8, 0.6] });
985
+ T.visible = !1, k[I] = { mesh: K, W: x, inner: M, layer: T };
986
+ }
987
+ const q = [0, 1].map((I) => !!(k[`eyes_closed_${I}`] && k[`eyes_half_${I}`])), N = Object.values(f).some((I) => k[I.sprite]), P = (I, V) => (I === "eyes_smile" && !k[`eyes_smile_${V}`] && (I = "eyes_closed"), I === "open" || k[`${I}_${V}`] ? I : "open"), H = [0, 0];
988
+ let G = null;
989
+ const z = [0, 1].map(() => ({ cur: "open", prev: "open", t: 1 }));
990
+ function W(I, V, K, x, M = 1) {
991
+ const T = I.mesh.rest, _ = I.mesh.pos;
992
+ for (let Y = 0; Y < I.W.length; Y++) {
993
+ let X = T[Y * 2], F = T[Y * 2 + 1];
994
+ const C = I.inner[Y];
995
+ if (C > 0) {
996
+ const Z = w(X);
997
+ F = Z + (F - Z) * (1 + (x - 1) * C), X = o() + (X - o()) * (1 + (M - 1) * C);
998
998
  }
999
- r(E, k, R.W[A], Z, P, V), v[A * 2] = V[0], v[A * 2 + 1] = V[1];
999
+ r(X, F, I.W[Y], V, K, H), _[Y * 2] = H[0], _[Y * 2 + 1] = H[1];
1000
1000
  }
1001
1001
  }
1002
1002
  return {
1003
1003
  /**
1004
1004
  * Show / hide and deform the sprites for this frame.
1005
- * Returns which eyes are covered by a sprite, so the layered eye parts can be hidden.
1005
+ * Returns which eyes are covered by a sprite, so the layered eye parts can be hidden, and
1006
+ * whether a drawing shows the mouth, so the shader mouth can be left out.
1006
1007
  * `mood` (angry / sad / surprise) picks drawn expression eyes (sad, surprise) and closed
1007
1008
  * mouths (angry, sad) when the avatar has them.
1008
1009
  */
1009
- update(R, Z, P = 1 / 60, G = null) {
1010
- for (const N of Object.values(_)) N.layer.visible = !1;
1011
- const J = [!1, !1];
1010
+ update(I, V, K = 1 / 60, x = null) {
1011
+ for (const C of Object.values(k)) C.layer.visible = !1;
1012
+ const M = [!1, !1];
1012
1013
  [
1013
- [R.eyeROpen, R.eyeSmile],
1014
- [R.eyeLOpen, R.eyeSmileL ?? R.eyeSmile]
1015
- ].forEach(([N, z], F) => {
1016
- const X = D[F], M = y.includes(G) && _[`eyes_${G}_${F}`] ? `eyes_${G}` : null, L = u(N, z, M) ?? "open";
1017
- L !== X.cur && (X.prev = X.cur, X.cur = L, X.t = 0), X.t = P > 0 ? Math.min(1, X.t + P / S) : 1;
1018
- const W = o(0, 1, X.t), Q = (ee, ce) => {
1019
- if (ee === "open" || ce <= 1e-3) return;
1020
- const oe = _[`${ee}_${F}`];
1021
- oe && (oe.layer.visible = !0, oe.layer.alpha = ce, H(oe, R, Z, 1));
1022
- }, ne = (ee) => ee === "open" ? -1 : U(ee) ? 1 : 0;
1023
- X.t >= 1 ? Q(X.cur, 1) : ne(X.cur) > ne(X.prev) ? (Q(X.prev, 1), Q(X.cur, W)) : (Q(X.cur, 1), Q(X.prev, 1 - W)), J[F] = X.cur !== "open" && W >= 0.999;
1014
+ [I.eyeROpen, I.eyeSmile],
1015
+ [I.eyeLOpen, I.eyeSmileL ?? I.eyeSmile]
1016
+ ].forEach(([C, Z], E) => {
1017
+ const R = z[E], J = v.includes(x) && k[`eyes_${x}_${E}`] ? `eyes_${x}` : null, te = P(h(C, Z, J) ?? "open", E);
1018
+ te !== R.cur && (R.prev = R.cur, R.cur = te, R.t = 0), R.t = K > 0 ? Math.min(1, R.t + K / b) : 1;
1019
+ const ee = s(0, 1, R.t), se = (ce, je) => {
1020
+ if (ce === "open" || je <= 1e-3) return;
1021
+ const _e = k[`${ce}_${E}`];
1022
+ _e && (_e.layer.visible = !0, _e.layer.alpha = je, W(_e, I, V, 1));
1023
+ }, le = (ce) => ce === "open" ? -1 : U(ce) ? 1 : 0;
1024
+ R.t >= 1 ? se(R.cur, 1) : le(R.cur) > le(R.prev) ? (se(R.prev, 1), se(R.cur, ee)) : (se(R.cur, 1), se(R.prev, 1 - ee)), M[E] = R.cur !== "open" && ee >= 0.999;
1024
1025
  });
1025
- const v = b(R.mouthOpen, R.mouthForm, j);
1026
- j = v;
1027
- const A = v && f[v], E = A && _[A.sprite], k = g.includes(G) && _[`mouth_${G}`];
1028
- if (k && (!E || R.mouthOpen < m + 0.04) && (k.layer.visible = !0, k.layer.alpha = 1, H(k, R, Z, 1)), E) {
1029
- E.layer.visible = !0, E.layer.alpha = o(m, m + 0.04, R.mouthOpen);
1030
- const N = Math.min(1.1, Math.max(0.5, R.mouthOpen / A.ref));
1031
- H(E, R, Z, N, A.width);
1026
+ const _ = u(I.mouthOpen, I.mouthForm, G);
1027
+ G = _;
1028
+ const Y = _ && f[_], X = Y && k[Y.sprite], F = d.includes(x) && k[`mouth_${x}`];
1029
+ if (F && (!X || I.mouthOpen < y + 0.04) && (F.layer.visible = !0, F.layer.alpha = 1, W(F, I, V, 1)), X) {
1030
+ X.layer.visible = !0, X.layer.alpha = s(y, y + 0.04, I.mouthOpen);
1031
+ const C = Math.min(1.1, Math.max(0.5, I.mouthOpen / Y.ref));
1032
+ W(X, I, V, C, Y.width);
1032
1033
  }
1033
- return { eyes: J, mouth: v };
1034
- }
1034
+ return { eyes: M, mouth: _, mouthDrawn: N && (!Y || !!X) };
1035
+ },
1036
+ /** Whether each eye has the closed and half-closed drawings, so the layered eye stays open. */
1037
+ eyesDrawn: q
1035
1038
  };
1036
1039
  }
1037
- return { createSprites: T, mouthShape: b, eyeSprite: u };
1040
+ return { createSprites: S, mouthShape: u, eyeSprite: h };
1038
1041
  }
1039
- const It = { angry: "angry", angryTalk: "angry", sad: "sad", sadTalk: "sad", surprise: "surprise", surprisedTalk: "surprise" };
1042
+ const $t = { angry: "angry", angryTalk: "angry", sad: "sad", sadTalk: "sad", surprise: "surprise", surprisedTalk: "surprise" };
1040
1043
  function In(n, e) {
1041
- return n && n !== "neutral" ? It[n] ?? null : (e && It[e]) ?? null;
1044
+ return n && n !== "neutral" ? $t[n] ?? null : (e && $t[e]) ?? null;
1045
+ }
1046
+ function Nn(n, e, t, r = 6) {
1047
+ let a = e, s = t, l = -1, f = -1;
1048
+ for (let m = 0; m < t; m++) {
1049
+ const c = m * e;
1050
+ for (let p = 0; p < e; p++)
1051
+ n[c + p] > 3 && (p < a && (a = p), p > l && (l = p), m < s && (s = m), m > f && (f = m));
1052
+ }
1053
+ if (l < 0) return null;
1054
+ const y = Math.max(0, a - r), d = Math.max(0, s - r);
1055
+ return [y, d, Math.min(e, l + 1 + r) - y, Math.min(t, f + 1 + r) - d];
1042
1056
  }
1043
- function wt(n) {
1057
+ function Xn(n, e, t, r, a, s, l = 16, f, y, d) {
1058
+ const m = /* @__PURE__ */ new Map();
1059
+ for (const c of e.overlays) {
1060
+ const p = t[c.name];
1061
+ if (f !== void 0 && p.width !== f || y !== void 0 && p.height !== y)
1062
+ throw new Error(`overlay ${c.name}: image size does not match base.png`);
1063
+ const b = a(p), v = Nn(b, p.width, p.height);
1064
+ if (!v) continue;
1065
+ const [h, u, w, o] = v, i = new Uint8Array(w * o);
1066
+ for (let L = 0; L < o; L++) i.set(b.subarray((u + L) * p.width + h, (u + L) * p.width + h + w), L * w);
1067
+ const S = r(v, l, i, w), g = [];
1068
+ for (let L = 0; L < S.rest.length; L += 2) {
1069
+ const B = s(S.rest[L], S.rest[L + 1], 0);
1070
+ g.push({ ...B, brow: 0, jaw: 0 });
1071
+ }
1072
+ const A = n.addLayer(`overlay_${c.name}`, d(p, v), S);
1073
+ A.visible = !1, m.set(c.name, { mesh: S, weights: g, layer: A });
1074
+ }
1075
+ return {
1076
+ /** Shows the named overlays and hides the rest. Unknown names are ignored. */
1077
+ set(c) {
1078
+ const p = new Set(c);
1079
+ for (const [b, v] of m) v.layer.visible = p.has(b);
1080
+ },
1081
+ update(c, p, b, v) {
1082
+ for (const { mesh: h, weights: u, layer: w } of m.values())
1083
+ if (w.visible)
1084
+ for (let o = 0; o < u.length; o++)
1085
+ b(h.rest[o * 2], h.rest[o * 2 + 1], u[o], c, p, v), h.pos[o * 2] = v[0], h.pos[o * 2 + 1] = v[1];
1086
+ }
1087
+ };
1088
+ }
1089
+ function St(n) {
1044
1090
  if (!n || typeof n != "object" || Array.isArray(n)) throw new TypeError("lipSync: expected an object");
1045
1091
  const e = n, t = {};
1046
1092
  for (const r of ["granularity", "openness", "closeSpeed"]) {
@@ -1058,32 +1104,32 @@ function wt(n) {
1058
1104
  }
1059
1105
  return t;
1060
1106
  }
1061
- function Nt(n = {}, e = {
1107
+ function zt(n = {}, e = {
1062
1108
  granularity: 0.5,
1063
1109
  openness: 0.5,
1064
1110
  closeSpeed: 0.5
1065
1111
  }) {
1066
- const t = wt(n);
1112
+ const t = St(n);
1067
1113
  return {
1068
1114
  granularity: t.granularity ?? e.granularity,
1069
1115
  openness: t.openness ?? e.openness,
1070
1116
  closeSpeed: t.closeSpeed ?? e.closeSpeed
1071
1117
  };
1072
1118
  }
1073
- const Me = (n, e, t, r) => n === 0.5 ? t : n < 0.5 ? e + (t - e) * n * 2 : t + (r - t) * (n - 0.5) * 2;
1074
- function jt(n) {
1119
+ const Ee = (n, e, t, r) => n === 0.5 ? t : n < 0.5 ? e + (t - e) * n * 2 : t + (r - t) * (n - 0.5) * 2;
1120
+ function Ct(n) {
1075
1121
  return {
1076
- arm: Me(n.granularity, 0.2, 0.6, 0.9),
1077
- trigger: Me(n.granularity, 0.65, 0.8, 0.95),
1078
- relax: Me(n.granularity, 1.5, 5, 12),
1079
- range: Me(n.granularity, 0.7, 0.3, 0.05),
1080
- risePower: Me(n.granularity, 0.05, 0.3, 0.8),
1081
- openness: Me(n.openness, 0.55, 1, 1.25),
1082
- audioRelease: Me(n.closeSpeed, 6, 32, 45),
1083
- kanaRelease: Me(n.closeSpeed, 6, 22, 45)
1122
+ arm: Ee(n.granularity, 0.2, 0.6, 0.9),
1123
+ trigger: Ee(n.granularity, 0.65, 0.8, 0.95),
1124
+ relax: Ee(n.granularity, 1.5, 5, 12),
1125
+ range: Ee(n.granularity, 0.7, 0.3, 0.05),
1126
+ risePower: Ee(n.granularity, 0.05, 0.3, 0.8),
1127
+ openness: Ee(n.openness, 0.55, 1, 1.25),
1128
+ audioRelease: Ee(n.closeSpeed, 6, 32, 45),
1129
+ kanaRelease: Ee(n.closeSpeed, 6, 22, 45)
1084
1130
  };
1085
1131
  }
1086
- const Nn = /* @__PURE__ */ new Set([
1132
+ const $n = /* @__PURE__ */ new Set([
1087
1133
  "angleX",
1088
1134
  "angleY",
1089
1135
  "angleZ",
@@ -1093,7 +1139,7 @@ const Nn = /* @__PURE__ */ new Set([
1093
1139
  "handAngle",
1094
1140
  "gazeX",
1095
1141
  "gazeY"
1096
- ]), At = {
1142
+ ]), Tt = {
1097
1143
  nod: {
1098
1144
  label: "Nod",
1099
1145
  dur: 1.7,
@@ -1241,7 +1287,7 @@ const Nn = /* @__PURE__ */ new Set([
1241
1287
  blush: [[0, 0], [1.6, 0.3], [2.6, 0]]
1242
1288
  }
1243
1289
  }
1244
- }, Mt = {
1290
+ }, Ot = {
1245
1291
  lookAround: {
1246
1292
  label: "Look around",
1247
1293
  dur: 5.2,
@@ -1481,16 +1527,16 @@ const Nn = /* @__PURE__ */ new Set([
1481
1527
  }
1482
1528
  }
1483
1529
  };
1484
- function jn(n, e) {
1530
+ function zn(n, e) {
1485
1531
  if (e <= n[0][0]) return n[0][1];
1486
1532
  const t = n.length - 1;
1487
1533
  if (e >= n[t][0]) return n[t][1];
1488
1534
  let r = 0;
1489
1535
  for (; e > n[r + 1][0]; ) r++;
1490
- const [a, o] = n[r], [i, f] = n[r + 1], m = (u) => u <= 0 || u >= t ? 0 : (n[u + 1][1] - n[u - 1][1]) / (n[u + 1][0] - n[u - 1][0]), g = i - a, p = (e - a) / g, l = m(r) * g, h = m(r + 1) * g, S = p * p, y = S * p;
1491
- return (2 * y - 3 * S + 1) * o + (y - 2 * S + p) * l + (-2 * y + 3 * S) * f + (y - S) * h;
1536
+ const [a, s] = n[r], [l, f] = n[r + 1], y = (h) => h <= 0 || h >= t ? 0 : (n[h + 1][1] - n[h - 1][1]) / (n[h + 1][0] - n[h - 1][0]), d = l - a, m = (e - a) / d, c = y(r) * d, p = y(r + 1) * d, b = m * m, v = b * m;
1537
+ return (2 * v - 3 * b + 1) * s + (v - 2 * b + m) * c + (-2 * v + 3 * b) * f + (v - b) * p;
1492
1538
  }
1493
- const Ce = {
1539
+ const Pe = {
1494
1540
  a: { open: 0.9, form: 0, label: "あ" },
1495
1541
  i: { open: 0.5, form: -0.85, label: "い" },
1496
1542
  u: { open: 0.4, form: 0.9, label: "う" },
@@ -1498,21 +1544,21 @@ const Ce = {
1498
1544
  o: { open: 0.75, form: 0.6, label: "お" },
1499
1545
  n: { open: 0, form: null, label: "ん" }
1500
1546
  // closes with the previous shape
1501
- }, Xn = {
1547
+ }, Cn = {
1502
1548
  a: "あかさたなはまやらわがざだばぱぁ",
1503
1549
  i: "いきしちにひみりぎじぢびぴぃ",
1504
1550
  u: "うくすつぬふむゆるぐずづぶぷぅゔ",
1505
1551
  e: "えけせてねへめれげぜでべぺぇ",
1506
1552
  o: "おこそとのほもよろをごぞどぼぽぉ"
1507
- }, Qt = {};
1508
- for (const [n, e] of Object.entries(Xn)) for (const t of e) Qt[t] = n;
1509
- const Cn = /* @__PURE__ */ new Set([..."まみむめもばびぶべぼぱぴぷぺぽ"]), zn = /* @__PURE__ */ new Set([..."あいうえおぁぃぅぇぉ"]), Xt = { ゃ: "a", ゅ: "u", ょ: "o" }, Pn = /* @__PURE__ */ new Set([..."、。,.,.!?!?  …"]), $n = (n) => n.replace(/[ァ-ヶ]/g, (e) => String.fromCharCode(e.charCodeAt(0) - 96));
1510
- function Dn(n, e = 0.14) {
1553
+ }, tn = {};
1554
+ for (const [n, e] of Object.entries(Cn)) for (const t of e) tn[t] = n;
1555
+ const Pn = /* @__PURE__ */ new Set([..."まみむめもばびぶべぼぱぴぷぺぽ"]), Dn = /* @__PURE__ */ new Set([..."あいうえおぁぃぅぇぉ"]), Pt = { ゃ: "a", ゅ: "u", ょ: "o" }, Hn = /* @__PURE__ */ new Set([..."、。,.,.!?!?  …"]), Zn = (n) => n.replace(/[ァ-ヶ]/g, (e) => String.fromCharCode(e.charCodeAt(0) - 96));
1556
+ function Wn(n, e = 0.14) {
1511
1557
  const t = [];
1512
- for (const r of $n(n)) {
1558
+ for (const r of Zn(n)) {
1513
1559
  const a = t[t.length - 1];
1514
- if (Xt[r] && a) {
1515
- a.vowel = Xt[r];
1560
+ if (Pt[r] && a) {
1561
+ a.vowel = Pt[r];
1516
1562
  continue;
1517
1563
  }
1518
1564
  if (r === "ー" && a) {
@@ -1527,41 +1573,41 @@ function Dn(n, e = 0.14) {
1527
1573
  t.push({ vowel: "n", dur: e, onset: null });
1528
1574
  continue;
1529
1575
  }
1530
- if (Pn.has(r)) {
1576
+ if (Hn.has(r)) {
1531
1577
  t.push({ vowel: "n", dur: e * 2.2, onset: null });
1532
1578
  continue;
1533
1579
  }
1534
- const o = Qt[r];
1535
- o && t.push({ vowel: o, dur: e, onset: Cn.has(r) ? "lips" : zn.has(r) ? null : "consonant" });
1580
+ const s = tn[r];
1581
+ s && t.push({ vowel: s, dur: e, onset: Pn.has(r) ? "lips" : Dn.has(r) ? null : "consonant" });
1536
1582
  }
1537
1583
  return t;
1538
1584
  }
1539
- function Hn(n, e, t = 0.5) {
1540
- if (t !== 0.5) return Zn(n, e, t);
1585
+ function Gn(n, e, t = 0.5) {
1586
+ if (t !== 0.5) return Vn(n, e, t);
1541
1587
  let r = 0;
1542
1588
  for (const a of n) {
1543
1589
  if (e < r + a.dur) {
1544
- const o = (e - r) / a.dur, i = Ce[a.vowel], f = a.onset === "lips" ? 0.05 : a.onset === "consonant" ? 0.45 : 1, m = o < 0.35 ? f + (1 - f) * (o / 0.35) : 1;
1545
- return { open: i.open * m, form: i.form, vowel: a.vowel };
1590
+ const s = (e - r) / a.dur, l = Pe[a.vowel], f = a.onset === "lips" ? 0.05 : a.onset === "consonant" ? 0.45 : 1, y = s < 0.35 ? f + (1 - f) * (s / 0.35) : 1;
1591
+ return { open: l.open * y, form: l.form, vowel: a.vowel };
1546
1592
  }
1547
1593
  r += a.dur;
1548
1594
  }
1549
1595
  return null;
1550
1596
  }
1551
- function Zn(n, e, t) {
1597
+ function Vn(n, e, t) {
1552
1598
  const r = t < 0.5 ? (0.5 - t) * 0.72 : 0;
1553
- let a = 0, o = 0, i = null;
1599
+ let a = 0, s = 0, l = null;
1554
1600
  for (const f of n) {
1555
- let m = f.vowel;
1556
- if (f.vowel === "n" ? r && f.dur <= 0.2 && i ? m = i : (o = 0, i = null) : ((!i || o >= r) && (i = f.vowel, o = 0), o += f.dur, r && (m = i)), e < a + f.dur) {
1557
- const g = Ce[m], p = f.onset === "lips" ? 0.05 : f.onset === "consonant" ? 0.45 : 1, l = t < 0.5 ? 1 - (1 - p) * t * 2 : p * (2 - t * 2), h = Math.min(f.dur * 0.35, 0.05), S = (e - a) / h;
1558
- return { open: g.open * (S < 1 ? l + (1 - l) * S : 1), form: g.form, vowel: m };
1601
+ let y = f.vowel;
1602
+ if (f.vowel === "n" ? r && f.dur <= 0.2 && l ? y = l : (s = 0, l = null) : ((!l || s >= r) && (l = f.vowel, s = 0), s += f.dur, r && (y = l)), e < a + f.dur) {
1603
+ const d = Pe[y], m = f.onset === "lips" ? 0.05 : f.onset === "consonant" ? 0.45 : 1, c = t < 0.5 ? 1 - (1 - m) * t * 2 : m * (2 - t * 2), p = Math.min(f.dur * 0.35, 0.05), b = (e - a) / p;
1604
+ return { open: d.open * (b < 1 ? c + (1 - c) * b : 1), form: d.form, vowel: y };
1559
1605
  }
1560
1606
  a += f.dur;
1561
1607
  }
1562
1608
  return null;
1563
1609
  }
1564
- const tt = { ...At, ...Mt }, Ct = { amp: 1, stiff: 1.8 }, Wn = /^(angle|bodyAngle|armAngle|handAngle)/, Gn = /* @__PURE__ */ new Set([
1610
+ const at = { ...Tt, ...Ot }, Dt = { amp: 1, stiff: 1.8 }, qn = /^(angle|bodyAngle|armAngle|handAngle)/, Jn = /* @__PURE__ */ new Set([
1565
1611
  "eyeOpen",
1566
1612
  "eyeOpenL",
1567
1613
  "eyeSmileL",
@@ -1571,18 +1617,18 @@ const tt = { ...At, ...Mt }, Ct = { amp: 1, stiff: 1.8 }, Wn = /^(angle|bodyAngl
1571
1617
  "blush",
1572
1618
  "browY",
1573
1619
  "browAngle"
1574
- ]), Ge = (n, e, t) => {
1620
+ ]), qe = (n, e, t) => {
1575
1621
  const r = Math.min(1, Math.max(0, (t - n) / (e - n)));
1576
1622
  return r * r * (3 - 2 * r);
1577
1623
  };
1578
- function zt(n, e) {
1579
- const t = Math.floor(n), r = n - t, a = (i) => {
1580
- const f = Math.sin((i + e * 57.3) * 127.1) * 43758.5453;
1624
+ function Ht(n, e) {
1625
+ const t = Math.floor(n), r = n - t, a = (l) => {
1626
+ const f = Math.sin((l + e * 57.3) * 127.1) * 43758.5453;
1581
1627
  return f - Math.floor(f);
1582
- }, o = r * r * (3 - 2 * r);
1583
- return (a(t) * (1 - o) + a(t + 1) * o) * 2 - 1;
1628
+ }, s = r * r * (3 - 2 * r);
1629
+ return (a(t) * (1 - s) + a(t + 1) * s) * 2 - 1;
1584
1630
  }
1585
- const we = (n, e) => zt(n, e) * 0.7 + zt(n * 2.3, e + 9) * 0.3, me = (n, e, t) => Math.max(e, Math.min(t, n)), en = {
1631
+ const Ae = (n, e) => Ht(n, e) * 0.7 + Ht(n * 2.3, e + 9) * 0.3, pe = (n, e, t) => Math.max(e, Math.min(t, n)), nn = {
1586
1632
  normal: { label: "Neutral", p: {} },
1587
1633
  smile: { label: "Smile", p: { eyeOpen: 0, eyeSmile: 1, blush: 0.35, browY: 0.2 } },
1588
1634
  shy: { label: "Blush", p: { eyeOpen: 0.78, eyeSmile: 0.5, blush: 1, browAngle: -0.5, browY: -0.1, glance: [0.7, 0.35] } },
@@ -1596,14 +1642,14 @@ const we = (n, e) => zt(n, e) * 0.7 + zt(n * 2.3, e + 9) * 0.3, me = (n, e, t) =
1596
1642
  angryTalk: { label: "angry", p: { eyeOpen: 0.72, browAngle: 0.85, browY: -0.55, mouthForm: 0.2 } },
1597
1643
  surprisedTalk: { label: "surprised", p: { eyeOpen: 1.18, browY: 0.9, mouthForm: 0.9 } },
1598
1644
  relaxedTalk: { label: "relaxed", p: { eyeOpen: 0.82, eyeSmile: 0.35, blush: 0.15, browY: 0.1 } }
1599
- }, Pt = {
1645
+ }, Zt = {
1600
1646
  happy: ["happyTalk", "nod"],
1601
1647
  sad: ["sadTalk", "sigh"],
1602
1648
  angry: ["angryTalk", "no"],
1603
1649
  surprised: ["surprisedTalk", "surprise"],
1604
1650
  relaxed: ["relaxedTalk", "sway"],
1605
1651
  neutral: ["normal", null]
1606
- }, Vn = {
1652
+ }, Kn = {
1607
1653
  gazeX: [260, 0.9],
1608
1654
  gazeY: [260, 0.9],
1609
1655
  angleX: [55, 0.72],
@@ -1614,41 +1660,41 @@ const we = (n, e) => zt(n, e) * 0.7 + zt(n * 2.3, e + 9) * 0.3, me = (n, e, t) =
1614
1660
  armAngle: [20, 0.85],
1615
1661
  handAngle: [30, 0.8]
1616
1662
  };
1617
- class qn {
1663
+ class Qn {
1618
1664
  /** @param {number[]} gazeCenter */
1619
1665
  constructor(e) {
1620
- this.mode = "auto", this.P = Object.fromEntries(Ze.map((t) => [t.id, t.def])), this.manual = { ...this.P }, this.cur = { ...this.P }, this.vel = Object.fromEntries(Ze.map((t) => [t.id, 0])), this.expr = "normal", this.exprMix = {}, this.t = 0, this.blink = { t: 0, start: 1.5, double: !1 }, this.gaze = { x: 0, y: 0, next: 0 }, this.tap = { next: 4, until: -1 }, this.pointer = null, this.faceCenter = [...e], this.lipOpen = null, this.lipForm = 0, this.speaking = !1, this.voice = 0, this.mouth = 0, this.vowel = "a", this.vowelForm = 0, this.lipSync = Nt(), this.lipMotion = jt(this.lipSync), this.syllableArmed = !0, this.voicePeak = 0, this.voiceTrough = 0, this.kana = null, this.lipHold = null, this.emotionUntil = 0, this.talkGain = 1, this.play = null, this.autoMotion = !0, this.nextAuto = 14, this.autoIdle = !0, this.nextIdle = 3, this.lastAuto = null, this.onMotion = null;
1666
+ this.mode = "auto", this.P = Object.fromEntries(Ge.map((t) => [t.id, t.def])), this.manual = { ...this.P }, this.cur = { ...this.P }, this.vel = Object.fromEntries(Ge.map((t) => [t.id, 0])), this.expr = "normal", this.exprMix = {}, this.t = 0, this.blink = { t: 0, start: 1.5, double: !1 }, this.gaze = { x: 0, y: 0, next: 0 }, this.tap = { next: 4, until: -1 }, this.pointer = null, this.faceCenter = [...e], this.lipOpen = null, this.lipForm = 0, this.speaking = !1, this.voice = 0, this.mouth = 0, this.vowel = "a", this.vowelForm = 0, this.lipSync = zt(), this.lipMotion = Ct(this.lipSync), this.syllableArmed = !0, this.voicePeak = 0, this.voiceTrough = 0, this.kana = null, this.lipHold = null, this.emotionUntil = 0, this.talkGain = 1, this.play = null, this.autoMotion = !0, this.nextAuto = 14, this.autoIdle = !0, this.nextIdle = 3, this.lastAuto = null, this.onMotion = null;
1621
1667
  }
1622
1668
  setExpression(e) {
1623
1669
  this.expr = e;
1624
1670
  }
1625
1671
  hasMotion(e) {
1626
- return e in tt;
1672
+ return e in at;
1627
1673
  }
1628
1674
  // ---- speech (driven from outside, e.g. by the TTS audio analyser) ----
1629
1675
  setVoiceLevel(e) {
1630
1676
  this.voice = Math.min(1, Math.max(0, Number(e) || 0));
1631
1677
  }
1632
1678
  setLipSync(e) {
1633
- this.lipSync = Nt(e, this.lipSync), this.lipMotion = jt(this.lipSync);
1679
+ this.lipSync = zt(e, this.lipSync), this.lipMotion = Ct(this.lipSync);
1634
1680
  }
1635
1681
  setSpeaking(e) {
1636
1682
  e !== this.speaking && (this.speaking = !!e, e || (this.emotionUntil = this.t + 1.2, this.voice = 0));
1637
1683
  }
1638
1684
  /** `playMotion: false` changes only the face (a caller that picks motions itself). */
1639
1685
  setEmotion(e, { playMotion: t = !0 } = {}) {
1640
- const r = Pt[e] ?? Pt.neutral;
1686
+ const r = Zt[e] ?? Zt.neutral;
1641
1687
  this.expr = r[0], this.emotionUntil = 1 / 0, r[1] && t && this.playMotion(r[1]);
1642
1688
  }
1643
1689
  /** Mouth shapes from kana text, without audio (preview / tuning). */
1644
1690
  /** @param {string} text @param {{ speed?: number, loop?: boolean }} [options] */
1645
1691
  speakKana(e, { speed: t, loop: r = !1 } = {}) {
1646
1692
  this.lipHold = null;
1647
- const a = Dn(e, t === void 0 ? 0.14 : 1 / me(Number(t) || 7, 4, 12));
1648
- this.kana = a.length ? { moras: a, t: 0, loop: r, duration: a.reduce((o, i) => o + i.dur, 0) } : null;
1693
+ const a = Wn(e, t === void 0 ? 0.14 : 1 / pe(Number(t) || 7, 4, 12));
1694
+ this.kana = a.length ? { moras: a, t: 0, loop: r, duration: a.reduce((s, l) => s + l.dur, 0) } : null;
1649
1695
  }
1650
1696
  holdMouth(e) {
1651
- this.kana = null, this.lipHold = Object.hasOwn(Ce, e) ? e : null;
1697
+ this.kana = null, this.lipHold = Object.hasOwn(Pe, e) ? e : null;
1652
1698
  }
1653
1699
  stopLipSync() {
1654
1700
  this.kana = null, this.lipHold = null, this.lipOpen = null, this.mouth = 0;
@@ -1664,11 +1710,11 @@ class qn {
1664
1710
  // the dip before each mora even when a loud voice never gets quiet.
1665
1711
  speechMouth(e) {
1666
1712
  if (this.kana || this.lipHold) {
1667
- let h;
1668
- if (this.lipHold ? h = Ce[this.lipHold] : (this.kana.t += e, this.kana.loop && (this.kana.t %= this.kana.duration), h = Hn(this.kana.moras, this.kana.t, this.lipSync.granularity)), !h)
1713
+ let p;
1714
+ if (this.lipHold ? p = Pe[this.lipHold] : (this.kana.t += e, this.kana.loop && (this.kana.t %= this.kana.duration), p = Gn(this.kana.moras, this.kana.t, this.lipSync.granularity)), !p)
1669
1715
  return this.kana = null, this.mouth > 0.01 ? [this.mouth *= this.lipSync.closeSpeed === 0.5 ? 0.6 : Math.exp(-e * this.lipMotion.kanaRelease), this.vowelForm] : null;
1670
- const S = this.lipMotion.openness === 1 ? h.open : Math.min(1, h.open * this.lipMotion.openness), y = S > this.mouth ? 1 - Math.exp(-e * 30) : 1 - Math.exp(-e * this.lipMotion.kanaRelease);
1671
- return this.mouth += (S - this.mouth) * y, h.form !== null && (this.vowelForm = h.form), [this.mouth, this.vowelForm];
1716
+ const b = this.lipMotion.openness === 1 ? p.open : Math.min(1, p.open * this.lipMotion.openness), v = b > this.mouth ? 1 - Math.exp(-e * 30) : 1 - Math.exp(-e * this.lipMotion.kanaRelease);
1717
+ return this.mouth += (b - this.mouth) * v, p.form !== null && (this.vowelForm = p.form), [this.mouth, this.vowelForm];
1672
1718
  }
1673
1719
  const t = this.speaking ? this.voice : 0, r = 1 - Math.exp(-e * this.lipMotion.relax);
1674
1720
  this.voicePeak = t > this.voicePeak ? t : this.voicePeak + (t - this.voicePeak) * r, this.voiceTrough = t < this.voiceTrough ? t : this.voiceTrough + (t - this.voiceTrough) * r;
@@ -1676,18 +1722,18 @@ class qn {
1676
1722
  if (t < a * this.lipMotion.arm) this.syllableArmed = !0;
1677
1723
  else if (this.syllableArmed && t > a * this.lipMotion.trigger && t > 0.1) {
1678
1724
  this.syllableArmed = !1;
1679
- const h = Math.random();
1680
- this.vowel = h < 0.4 ? "a" : h < 0.58 ? "o" : h < 0.74 ? "e" : h < 0.88 ? "i" : "u";
1725
+ const p = Math.random();
1726
+ this.vowel = p < 0.4 ? "a" : p < 0.58 ? "o" : p < 0.74 ? "e" : p < 0.88 ? "i" : "u";
1681
1727
  }
1682
- const o = this.voicePeak - this.voiceTrough, i = o < a * this.lipMotion.range ? 1 : Math.max(0, (t - this.voiceTrough) / o), f = Ce[this.vowel ?? "a"], m = Math.min(1, Math.pow(t, 0.7) * 1.15) * Math.pow(i, this.lipMotion.risePower), g = this.speaking ? Math.min(0.95, m) * (0.45 + 0.55 * f.open / 0.9) : 0, p = this.lipMotion.openness === 1 ? g : Math.min(1, g * this.lipMotion.openness), l = p > this.mouth ? 1 - Math.exp(-e * 40) : 1 - Math.exp(-e * this.lipMotion.audioRelease);
1683
- return this.mouth += (p - this.mouth) * l, this.speaking && (this.vowelForm = f.form), this.speaking || this.mouth > 0.01 ? [this.mouth, this.vowelForm] : null;
1728
+ const s = this.voicePeak - this.voiceTrough, l = s < a * this.lipMotion.range ? 1 : Math.max(0, (t - this.voiceTrough) / s), f = Pe[this.vowel ?? "a"], y = Math.min(1, Math.pow(t, 0.7) * 1.15) * Math.pow(l, this.lipMotion.risePower), d = this.speaking ? Math.min(0.95, y) * (0.45 + 0.55 * f.open / 0.9) : 0, m = this.lipMotion.openness === 1 ? d : Math.min(1, d * this.lipMotion.openness), c = m > this.mouth ? 1 - Math.exp(-e * 40) : 1 - Math.exp(-e * this.lipMotion.audioRelease);
1729
+ return this.mouth += (m - this.mouth) * c, this.speaking && (this.vowelForm = f.form), this.speaking || this.mouth > 0.01 ? [this.mouth, this.vowelForm] : null;
1684
1730
  }
1685
1731
  // Breathing: inhale ~40% of the cycle, a slower exhale, a short pause at the bottom,
1686
1732
  // and a period that drifts between ~3.2 and 4.6 s so it never looks metronomic.
1687
1733
  breathValue(e) {
1688
1734
  this.breathPhase = (this.breathPhase ?? 0) + e / (this.breathPeriod ?? 3.8), this.breathPhase >= 1 && (this.breathPhase -= 1, this.breathPeriod = 3.2 + Math.random() * 1.4);
1689
1735
  const t = this.breathPhase;
1690
- return t < 0.4 ? Ge(0, 0.4, t) : t < 0.88 ? 1 - Ge(0.4, 0.88, t) : 0;
1736
+ return t < 0.4 ? qe(0, 0.4, t) : t < 0.88 ? 1 - qe(0.4, 0.88, t) : 0;
1691
1737
  }
1692
1738
  // pick one at random, never the one that just played
1693
1739
  playRandom(e) {
@@ -1706,81 +1752,81 @@ class qn {
1706
1752
  update(e) {
1707
1753
  this.t += e;
1708
1754
  const t = this.t, r = {};
1709
- for (const y of Ze) r[y.id] = y.def;
1755
+ for (const v of Ge) r[v.id] = v.def;
1710
1756
  if (this.mode === "manual")
1711
1757
  Object.assign(r, this.manual);
1712
1758
  else if (this.mode === "auto") {
1713
- if (r.angleX = we(t * 0.18, 1) * 10 + this.gaze.x * 11, r.angleY = we(t * 0.15, 2) * 6 - 1 + this.gaze.y * 7, r.angleZ = we(t * 0.12, 3) * 10, r.bodyAngleX = we(t * 0.1, 4) * 5, r.bodyAngleZ = we(t * 0.09, 5) * 4, r.armAngle = we(t * 0.14, 6) * 5, r.handAngle = we(t * 0.21, 7) * 5, t > this.gaze.next) {
1714
- const y = Math.random();
1715
- this.gaze.x = y < 0.45 ? 0 : (Math.random() * 2 - 1) * 0.8, this.gaze.y = y < 0.45 ? 0 : (Math.random() * 2 - 1) * 0.5, this.gaze.next = t + 0.8 + Math.random() * 2.5;
1759
+ if (r.angleX = Ae(t * 0.18, 1) * 10 + this.gaze.x * 11, r.angleY = Ae(t * 0.15, 2) * 6 - 1 + this.gaze.y * 7, r.angleZ = Ae(t * 0.12, 3) * 10, r.bodyAngleX = Ae(t * 0.1, 4) * 5, r.bodyAngleZ = Ae(t * 0.09, 5) * 4, r.armAngle = Ae(t * 0.14, 6) * 5, r.handAngle = Ae(t * 0.21, 7) * 5, t > this.gaze.next) {
1760
+ const v = Math.random();
1761
+ this.gaze.x = v < 0.45 ? 0 : (Math.random() * 2 - 1) * 0.8, this.gaze.y = v < 0.45 ? 0 : (Math.random() * 2 - 1) * 0.5, this.gaze.next = t + 0.8 + Math.random() * 2.5;
1716
1762
  }
1717
1763
  if (r.gazeX = this.gaze.x + r.angleX / 60, r.gazeY = this.gaze.y + r.angleY / 60, t > this.tap.next && (this.tap.until = t + 1.1, this.tap.next = t + 5 + Math.random() * 6), this.speaking) {
1718
1764
  this.nextIdle = Math.max(this.nextIdle, t + 2.5), this.nextAuto = Math.max(this.nextAuto, t + 6);
1719
- const y = this.talkGain;
1720
- this.talkNod = (this.talkNod ?? 0) + (this.voice * 7 * y - (this.talkNod ?? 0)) * Math.min(1, e * 6), r.angleY -= this.talkNod, r.angleX += we(t * 0.6, 11) * 5 * y, r.angleZ += we(t * 0.5, 12) * 4 * y;
1765
+ const v = this.talkGain;
1766
+ this.talkNod = (this.talkNod ?? 0) + (this.voice * 7 * v - (this.talkNod ?? 0)) * Math.min(1, e * 6), r.angleY -= this.talkNod, r.angleX += Ae(t * 0.6, 11) * 5 * v, r.angleZ += Ae(t * 0.5, 12) * 4 * v;
1721
1767
  }
1722
- !this.speaking && t > this.emotionUntil && (this.expr = "normal", this.emotionUntil = 1 / 0), this.speaking || (!this.play && this.autoMotion && t > this.nextAuto ? (this.playRandom(Object.keys(At).filter((y) => y !== "surprise")), this.nextAuto = t + 15 + Math.random() * 10, this.nextIdle = Math.max(this.nextIdle, t + 5)) : !this.play && this.autoIdle && t > this.nextIdle && (this.playRandom(Object.keys(Mt)), this.nextIdle = t + 5 + Math.random() * 5));
1768
+ !this.speaking && t > this.emotionUntil && (this.expr = "normal", this.emotionUntil = 1 / 0), this.speaking || (!this.play && this.autoMotion && t > this.nextAuto ? (this.playRandom(Object.keys(Tt).filter((v) => v !== "surprise")), this.nextAuto = t + 15 + Math.random() * 10, this.nextIdle = Math.max(this.nextIdle, t + 5)) : !this.play && this.autoIdle && t > this.nextIdle && (this.playRandom(Object.keys(Ot)), this.nextIdle = t + 5 + Math.random() * 5));
1723
1769
  } else if (this.mode === "mouse" && this.pointer) {
1724
- const y = (this.pointer[0] - this.faceCenter[0]) / 500, u = (this.pointer[1] - this.faceCenter[1]) / 500;
1725
- r.angleX = me(y * 30, -30, 30), r.angleY = me(-u * 30, -30, 30), r.angleZ = me(-y * u * 25, -12, 12), r.bodyAngleX = me(y * 10, -10, 10) * 0.6, r.gazeX = me(y * 1.4, -1, 1), r.gazeY = me(-u * 1.4, -1, 1), r.handAngle = me(y * 4, -5, 5);
1770
+ const v = (this.pointer[0] - this.faceCenter[0]) / 500, h = (this.pointer[1] - this.faceCenter[1]) / 500;
1771
+ r.angleX = pe(v * 30, -30, 30), r.angleY = pe(-h * 30, -30, 30), r.angleZ = pe(-v * h * 25, -12, 12), r.bodyAngleX = pe(v * 10, -10, 10) * 0.6, r.gazeX = pe(v * 1.4, -1, 1), r.gazeY = pe(-h * 1.4, -1, 1), r.handAngle = pe(v * 4, -5, 5);
1726
1772
  }
1727
1773
  this.mode !== "manual" && (r.breath = this.breathValue(e), t < this.tap.until && (r.fingerTap = Math.max(0, Math.sin((this.tap.until - t) * Math.PI * 3.6)) ** 2));
1728
- let a = null, o = 0;
1774
+ let a = null, s = 0;
1729
1775
  if (this.play) {
1730
- const y = tt[this.play.id];
1731
- if (this.play.t += e, this.play.t >= y.dur)
1776
+ const v = at[this.play.id];
1777
+ if (this.play.t += e, this.play.t >= v.dur)
1732
1778
  this.play = null, this.onMotion?.(null);
1733
1779
  else {
1734
1780
  a = {};
1735
- for (const [u, b] of Object.entries(y.tracks))
1736
- a[u] = jn(b, this.play.t) * (y.idle && Wn.test(u) ? Ct.amp : 1);
1737
- o = Ge(0, 0.15, this.play.t) * (1 - Ge(y.dur - 0.25, y.dur, this.play.t));
1738
- for (const [u, b] of Object.entries(a))
1739
- Nn.has(u) ? r[u] += b : u === "breath" ? r[u] += (b - r[u]) * o : Gn.has(u) || (r[u] = Math.max(r[u], Math.max(0, b)));
1781
+ for (const [h, u] of Object.entries(v.tracks))
1782
+ a[h] = zn(u, this.play.t) * (v.idle && qn.test(h) ? Dt.amp : 1);
1783
+ s = qe(0, 0.15, this.play.t) * (1 - qe(v.dur - 0.25, v.dur, this.play.t));
1784
+ for (const [h, u] of Object.entries(a))
1785
+ $n.has(h) ? r[h] += u : h === "breath" ? r[h] += (u - r[h]) * s : Jn.has(h) || (r[h] = Math.max(r[h], Math.max(0, u)));
1740
1786
  }
1741
1787
  }
1742
1788
  this.mode !== "manual" && (r.angleY += r.breath * 1.6);
1743
- const i = en[this.expr].p, f = ["eyeOpen", "eyeOpenL", "eyeSmileL", "eyeSmile", "mouthOpen", "mouthForm", "blush", "browY", "browAngle", "gx", "gy"], m = {
1744
- eyeOpen: i.eyeOpen ?? 1,
1745
- eyeOpenL: i.eyeOpenL ?? i.eyeOpen ?? 1,
1746
- eyeSmileL: i.eyeSmileL ?? i.eyeSmile ?? 0,
1747
- eyeSmile: i.eyeSmile ?? 0,
1748
- mouthOpen: i.mouthOpen ?? 0,
1749
- mouthForm: i.mouthForm ?? 0,
1750
- blush: i.blush ?? 0,
1751
- browY: i.browY ?? 0,
1752
- browAngle: i.browAngle ?? 0,
1753
- gx: i.glance?.[0] ?? 0,
1754
- gy: i.glance?.[1] ?? 0
1755
- }, g = 1 - Math.exp(-e * 9);
1756
- for (const y of f)
1757
- this.exprMix[y] === void 0 && (this.exprMix[y] = m[y]), this.exprMix[y] += (m[y] - this.exprMix[y]) * g;
1758
- const p = this.exprMix, l = {};
1759
- for (const y of Ze) {
1760
- const u = y.id;
1789
+ const l = nn[this.expr].p, f = ["eyeOpen", "eyeOpenL", "eyeSmileL", "eyeSmile", "mouthOpen", "mouthForm", "blush", "browY", "browAngle", "gx", "gy"], y = {
1790
+ eyeOpen: l.eyeOpen ?? 1,
1791
+ eyeOpenL: l.eyeOpenL ?? l.eyeOpen ?? 1,
1792
+ eyeSmileL: l.eyeSmileL ?? l.eyeSmile ?? 0,
1793
+ eyeSmile: l.eyeSmile ?? 0,
1794
+ mouthOpen: l.mouthOpen ?? 0,
1795
+ mouthForm: l.mouthForm ?? 0,
1796
+ blush: l.blush ?? 0,
1797
+ browY: l.browY ?? 0,
1798
+ browAngle: l.browAngle ?? 0,
1799
+ gx: l.glance?.[0] ?? 0,
1800
+ gy: l.glance?.[1] ?? 0
1801
+ }, d = 1 - Math.exp(-e * 9);
1802
+ for (const v of f)
1803
+ this.exprMix[v] === void 0 && (this.exprMix[v] = y[v]), this.exprMix[v] += (y[v] - this.exprMix[v]) * d;
1804
+ const m = this.exprMix, c = {};
1805
+ for (const v of Ge) {
1806
+ const h = v.id;
1761
1807
  if (this.mode === "manual" && !a) {
1762
- l[u] = r[u], this.cur[u] = r[u], this.vel[u] = 0;
1808
+ c[h] = r[h], this.cur[h] = r[h], this.vel[h] = 0;
1763
1809
  continue;
1764
1810
  }
1765
- let [b, x] = Vn[u] ?? [120, 0.95];
1766
- a && /^angle|^body/.test(u) && (tt[this.play?.id ?? ""]?.idle ? (b *= Ct.stiff, x = 0.8) : /^angle/.test(u) && (b *= 4.5, x = 0.75));
1767
- const s = 2 * Math.sqrt(b) * x;
1768
- this.vel[u] += (b * (r[u] - this.cur[u]) - s * this.vel[u]) * e, this.cur[u] += this.vel[u] * e, l[u] = this.cur[u];
1769
- }
1770
- let h = 1;
1771
- if (this.mode !== "manual" ? (h = this.blinkValue(e), l.eyeROpen = p.eyeOpen * h, l.eyeLOpen = p.eyeOpenL * h, l.eyeSmile = p.eyeSmile, l.eyeSmileL = p.eyeSmileL, l.mouthOpen = p.mouthOpen, l.mouthForm = p.mouthForm, l.blush = p.blush, l.browY = p.browY + (l.angleY > 0 ? l.angleY / 60 : 0) - (1 - h) * 0.18, l.browAngle = p.browAngle, l.gazeX = me(l.gazeX + p.gx, -1, 1), l.gazeY = me(l.gazeY + p.gy, -1, 1)) : l.eyeSmileL = l.eyeSmile, a) {
1772
- const y = (b, x) => {
1773
- l[b] += (x - l[b]) * o;
1774
- }, u = (b) => Math.min(1.3, Math.max(0, b));
1775
- "eyeOpen" in a && (y("eyeROpen", u(a.eyeOpen) * h), "eyeOpenL" in a || y("eyeLOpen", u(a.eyeOpen) * h)), "eyeOpenL" in a && y("eyeLOpen", u(a.eyeOpenL) * h), "eyeSmile" in a && (y("eyeSmile", a.eyeSmile), "eyeSmileL" in a || y("eyeSmileL", a.eyeSmile)), "eyeSmileL" in a && y("eyeSmileL", a.eyeSmileL);
1776
- for (const b of ["mouthOpen", "mouthForm", "blush", "browY", "browAngle"])
1777
- b in a && y(b, b === "mouthOpen" || b === "blush" ? Math.max(0, a[b]) : a[b]);
1778
- }
1779
- const S = this.speechMouth(e);
1780
- return S ? (this.lipOpen = S[0], this.lipForm = S[1]) : this.lipOpen !== null && !this.speaking && !this.kana && (this.lipOpen = null), this.lipOpen !== null && (l.mouthOpen = Math.max(l.mouthOpen * 0.3, this.lipOpen), l.mouthForm = me(this.lipForm, -1, 1)), this.P = l, l;
1811
+ let [u, w] = Kn[h] ?? [120, 0.95];
1812
+ a && /^angle|^body/.test(h) && (at[this.play?.id ?? ""]?.idle ? (u *= Dt.stiff, w = 0.8) : /^angle/.test(h) && (u *= 4.5, w = 0.75));
1813
+ const o = 2 * Math.sqrt(u) * w;
1814
+ this.vel[h] += (u * (r[h] - this.cur[h]) - o * this.vel[h]) * e, this.cur[h] += this.vel[h] * e, c[h] = this.cur[h];
1815
+ }
1816
+ let p = 1;
1817
+ if (this.mode !== "manual" ? (p = this.blinkValue(e), c.eyeROpen = m.eyeOpen * p, c.eyeLOpen = m.eyeOpenL * p, c.eyeSmile = m.eyeSmile, c.eyeSmileL = m.eyeSmileL, c.mouthOpen = m.mouthOpen, c.mouthForm = m.mouthForm, c.blush = m.blush, c.browY = m.browY + (c.angleY > 0 ? c.angleY / 60 : 0) - (1 - p) * 0.18, c.browAngle = m.browAngle, c.gazeX = pe(c.gazeX + m.gx, -1, 1), c.gazeY = pe(c.gazeY + m.gy, -1, 1)) : c.eyeSmileL = c.eyeSmile, a) {
1818
+ const v = (u, w) => {
1819
+ c[u] += (w - c[u]) * s;
1820
+ }, h = (u) => Math.min(1.3, Math.max(0, u));
1821
+ "eyeOpen" in a && (v("eyeROpen", h(a.eyeOpen) * p), "eyeOpenL" in a || v("eyeLOpen", h(a.eyeOpen) * p)), "eyeOpenL" in a && v("eyeLOpen", h(a.eyeOpenL) * p), "eyeSmile" in a && (v("eyeSmile", a.eyeSmile), "eyeSmileL" in a || v("eyeSmileL", a.eyeSmile)), "eyeSmileL" in a && v("eyeSmileL", a.eyeSmileL);
1822
+ for (const u of ["mouthOpen", "mouthForm", "blush", "browY", "browAngle"])
1823
+ u in a && v(u, u === "mouthOpen" || u === "blush" ? Math.max(0, a[u]) : a[u]);
1824
+ }
1825
+ const b = this.speechMouth(e);
1826
+ return b ? (this.lipOpen = b[0], this.lipForm = b[1]) : this.lipOpen !== null && !this.speaking && !this.kana && (this.lipOpen = null), this.lipOpen !== null && (c.mouthOpen = Math.max(c.mouthOpen * 0.3, this.lipOpen), c.mouthForm = pe(this.lipForm, -1, 1)), this.P = c, c;
1781
1827
  }
1782
1828
  }
1783
- const Et = {
1829
+ const Rt = {
1784
1830
  neutral: "normal",
1785
1831
  smile: "smile",
1786
1832
  shy: "shy",
@@ -1789,9 +1835,9 @@ const Et = {
1789
1835
  angry: "angryTalk",
1790
1836
  sad: "sadTalk",
1791
1837
  wink: "wink"
1792
- }, Jn = { sad: { pitch: -6, gy: -0.45 } }, Be = (n, e, t) => Math.max(e, Math.min(t, n)), Ue = Object.freeze({ eyeOpen: 1, eyeOpenL: 1, eyeSmile: 0, eyeSmileL: 0, blush: 0, browY: 0, browAngle: 0, mouthOpen: 0, gx: 0, gy: 0, pitch: 0 });
1793
- function Kn(n) {
1794
- const e = en[Et[n]]?.p ?? {}, t = Jn[n] ?? {};
1838
+ }, er = { sad: { pitch: -6, gy: -0.45 } }, Ne = (n, e, t) => Math.max(e, Math.min(t, n)), Xe = Object.freeze({ eyeOpen: 1, eyeOpenL: 1, eyeSmile: 0, eyeSmileL: 0, blush: 0, browY: 0, browAngle: 0, mouthOpen: 0, gx: 0, gy: 0, pitch: 0 });
1839
+ function tr(n) {
1840
+ const e = nn[Rt[n]]?.p ?? {}, t = er[n] ?? {};
1795
1841
  return {
1796
1842
  eyeOpen: e.eyeOpen ?? 1,
1797
1843
  eyeOpenL: e.eyeOpenL ?? e.eyeOpen ?? 1,
@@ -1806,277 +1852,293 @@ function Kn(n) {
1806
1852
  pitch: t.pitch ?? 0
1807
1853
  };
1808
1854
  }
1809
- function Qn(n, e) {
1855
+ function nr(n, e) {
1810
1856
  const t = { ...n };
1811
- return t.eyeROpen = (n.eyeROpen ?? 1) * e.eyeOpen, t.eyeLOpen = (n.eyeLOpen ?? 1) * e.eyeOpenL, t.eyeSmile = Math.max(n.eyeSmile ?? 0, e.eyeSmile), t.eyeSmileL = Math.max(n.eyeSmileL ?? n.eyeSmile ?? 0, e.eyeSmileL), t.blush = Math.max(n.blush ?? 0, e.blush), t.browY = Be((n.browY ?? 0) + e.browY, -1, 1), t.browAngle = Be((n.browAngle ?? 0) + e.browAngle, -1, 1), t.mouthOpen = Math.max(n.mouthOpen ?? 0, e.mouthOpen), t.gazeX = Be((n.gazeX ?? 0) + e.gx, -1, 1), t.gazeY = Be((n.gazeY ?? 0) + e.gy, -1, 1), t.angleY = Be((n.angleY ?? 0) + e.pitch, -30, 30), t;
1857
+ return t.eyeROpen = (n.eyeROpen ?? 1) * e.eyeOpen, t.eyeLOpen = (n.eyeLOpen ?? 1) * e.eyeOpenL, t.eyeSmile = Math.max(n.eyeSmile ?? 0, e.eyeSmile), t.eyeSmileL = Math.max(n.eyeSmileL ?? n.eyeSmile ?? 0, e.eyeSmileL), t.blush = Math.max(n.blush ?? 0, e.blush), t.browY = Ne((n.browY ?? 0) + e.browY, -1, 1), t.browAngle = Ne((n.browAngle ?? 0) + e.browAngle, -1, 1), t.mouthOpen = Math.max(n.mouthOpen ?? 0, e.mouthOpen), t.gazeX = Ne((n.gazeX ?? 0) + e.gx, -1, 1), t.gazeY = Ne((n.gazeY ?? 0) + e.gy, -1, 1), t.angleY = Ne((n.angleY ?? 0) + e.pitch, -30, 30), t;
1812
1858
  }
1813
- class er {
1859
+ class rr {
1814
1860
  /** @type {keyof typeof OVERLAY_EXPRESSIONS} */
1815
1861
  name = "neutral";
1816
- mix = { ...Ue };
1862
+ mix = { ...Xe };
1817
1863
  /** @param {string} name */
1818
1864
  set(e) {
1819
- this.name = Object.hasOwn(Et, e) ? (
1865
+ this.name = Object.hasOwn(Rt, e) ? (
1820
1866
  /** @type {keyof typeof OVERLAY_EXPRESSIONS} */
1821
1867
  e
1822
1868
  ) : "neutral";
1823
1869
  }
1824
1870
  /** True while an expression is shown or still fading out. */
1825
1871
  get active() {
1826
- return this.name !== "neutral" || Object.keys(Ue).some((e) => Math.abs(this.mix[e] - Ue[e]) > 1e-3);
1872
+ return this.name !== "neutral" || Object.keys(Xe).some((e) => Math.abs(this.mix[e] - Xe[e]) > 1e-3);
1827
1873
  }
1828
1874
  apply(e, t) {
1829
1875
  if (!this.active)
1830
- return this.mix = { ...Ue }, e;
1831
- const r = Kn(this.name), a = 1 - Math.exp(-Math.max(0, t) * 12);
1832
- for (const o of Object.keys(Ue)) this.mix[o] += (r[o] - this.mix[o]) * a;
1833
- return Qn(e, this.mix);
1876
+ return this.mix = { ...Xe }, e;
1877
+ const r = tr(this.name), a = 1 - Math.exp(-Math.max(0, t) * 12);
1878
+ for (const s of Object.keys(Xe)) this.mix[s] += (r[s] - this.mix[s]) * a;
1879
+ return nr(e, this.mix);
1834
1880
  }
1835
1881
  }
1836
- const $t = ["ball", "low", "crease", "lash"], nt = /* @__PURE__ */ new Map(), rt = /* @__PURE__ */ new Map();
1837
- async function Dt(n) {
1838
- return rt.has(n) || rt.set(n, fetch(n).then((e) => {
1882
+ const Wt = ["ball", "low", "crease", "lash"], ot = /* @__PURE__ */ new Map(), st = /* @__PURE__ */ new Map();
1883
+ async function it(n) {
1884
+ return st.has(n) || st.set(n, fetch(n).then((e) => {
1839
1885
  if (!e.ok) throw new Error(`failed to load ${n}`);
1840
1886
  return e.json();
1841
- })), rt.get(n);
1887
+ })), st.get(n);
1842
1888
  }
1843
- function Ht(n) {
1844
- if (nt.has(n)) return nt.get(n);
1889
+ function lt(n) {
1890
+ if (ot.has(n)) return ot.get(n);
1845
1891
  const e = new Promise((t, r) => {
1846
1892
  const a = new Image();
1847
1893
  a.onload = () => t(a), a.onerror = () => r(new Error(`failed to load ${n}`)), a.src = n;
1848
1894
  });
1849
- return nt.set(n, e), e;
1895
+ return ot.set(n, e), e;
1850
1896
  }
1851
- function tn(n, e) {
1897
+ function rn(n, e) {
1852
1898
  const t = document.createElement("canvas");
1853
1899
  t.width = n.width, t.height = n.height;
1854
1900
  const r = t.getContext("2d");
1855
1901
  r.drawImage(n, 0, 0);
1856
- const a = r.getImageData(0, 0, t.width, t.height).data, o = new Uint8Array(t.width * t.height);
1857
- for (let i = 0; i < o.length; i++) o[i] = a[i * 4 + e];
1858
- return o;
1902
+ const a = r.getImageData(0, 0, t.width, t.height).data, s = new Uint8Array(t.width * t.height);
1903
+ for (let l = 0; l < s.length; l++) s[l] = a[l * 4 + e];
1904
+ return s;
1859
1905
  }
1860
- const Ye = (n) => tn(n, 3);
1861
- function tr(n, e) {
1862
- const [t, r] = n.t.pivot, a = n.dir, o = n.pts, i = (S, y, u) => {
1863
- const b = Math.min(1, Math.max(0, (u - S) / (y - S)));
1864
- return b * b * (3 - 2 * b);
1865
- }, f = Math.cos(n.phi[0]), m = Math.sin(n.phi[0]), g = Math.cos(n.phi[1]), p = Math.sin(n.phi[1]), l = e.rest, h = e.pos;
1866
- for (let S = 0; S < l.length; S += 2) {
1867
- const y = l[S] - t, u = l[S + 1] - r, b = y * a[0] + u * a[1], x = o[0][0] + y * f - u * m, s = o[0][1] + y * m + u * f, c = y - a[0] * n.L1, T = u - a[1] * n.L1, d = o[1][0] + c * g - T * p, w = o[1][1] + c * p + T * g, B = i(n.L1 * 0.55, n.L1 * 1.35, b);
1868
- h[S] = x + (d - x) * B, h[S + 1] = s + (w - s) * B;
1906
+ const Fe = (n) => rn(n, 3);
1907
+ function ar(n, [e, t, r, a]) {
1908
+ const s = document.createElement("canvas");
1909
+ return s.width = r, s.height = a, s.getContext("2d").drawImage(n, e, t, r, a, 0, 0, r, a), s;
1910
+ }
1911
+ function or(n, e) {
1912
+ const [t, r] = n.t.pivot, a = n.dir, s = n.pts, l = (b, v, h) => {
1913
+ const u = Math.min(1, Math.max(0, (h - b) / (v - b)));
1914
+ return u * u * (3 - 2 * u);
1915
+ }, f = Math.cos(n.phi[0]), y = Math.sin(n.phi[0]), d = Math.cos(n.phi[1]), m = Math.sin(n.phi[1]), c = e.rest, p = e.pos;
1916
+ for (let b = 0; b < c.length; b += 2) {
1917
+ const v = c[b] - t, h = c[b + 1] - r, u = v * a[0] + h * a[1], w = s[0][0] + v * f - h * y, o = s[0][1] + v * y + h * f, i = v - a[0] * n.L1, S = h - a[1] * n.L1, g = s[1][0] + i * d - S * m, A = s[1][1] + i * m + S * d, L = l(n.L1 * 0.55, n.L1 * 1.35, u);
1918
+ p[b] = w + (g - w) * L, p[b + 1] = o + (A - o) * L;
1869
1919
  }
1870
1920
  }
1871
- async function nr(n, e) {
1921
+ async function sr(n, e) {
1872
1922
  if (!e.assets && e.assetsBase === void 0)
1873
1923
  throw new Error("createMeshAvatar requires assets or assetsBase.");
1874
- const t = e.rig, r = bn(t), {
1924
+ const t = e.rig, r = xn(t), {
1875
1925
  IMG: a,
1876
- EYES: o,
1877
- baseWeights: i,
1926
+ EYES: s,
1927
+ baseWeights: l,
1878
1928
  deformBase: f,
1879
- eyePartY: m,
1880
- eyePartAlpha: g,
1881
- handWeights: p,
1882
- handFrame: l,
1883
- deformHand: h,
1884
- TASSELS: S
1885
- } = r, { Renderer: y, buildGrid: u } = Bn(r, t), { Physics: b } = Un(r, t), { createSprites: x } = Yn(r, t), s = (e.assetsBase ?? "").replace(/\/?$/, "/"), c = (M, L) => {
1929
+ eyePartY: y,
1930
+ eyePartAlpha: d,
1931
+ handWeights: m,
1932
+ handFrame: c,
1933
+ deformHand: p,
1934
+ TASSELS: b
1935
+ } = r, { Renderer: v, buildGrid: h } = Un(r, t), { Physics: u } = Yn(r, t), { createSprites: w } = jn(r, t), o = (e.assetsBase ?? "").replace(/\/?$/, "/"), i = (E, R) => {
1886
1936
  if (e.assets) {
1887
- if (!e.assets[M]) throw new Error(`Missing project asset: ${M}`);
1888
- return e.assets[M];
1889
- }
1890
- return `${s}${M}${L === void 0 ? "" : `?b=${L}`}`;
1891
- }, T = await Dt(c("layers.json")), d = ["base", ...t.hand ? ["hand"] : [], ...S.map((M) => M.name), ...[0, 1].flatMap((M) => $t.map((L) => `eye${M}_${L}`)), "hairmask"], w = Object.fromEntries(await Promise.all(d.map(async (M) => [M, await Ht(c(`${M}.png`, T.build))]))), B = tn(w.hairmask, 0), Y = (M, L) => B[Math.min(a.h - 1, Math.max(0, Math.round(L))) * a.w + Math.min(a.w - 1, Math.max(0, Math.round(M)))] / 255, I = new y(n, { padTop: e.padTop ?? t.view.padTop, padSide: e.padSide ?? t.view.padSide, fit: e.fit }), _ = { base: [0, 0, a.w, a.h], ...T.layers }, U = u(_.base, t.mesh.baseCell, Ye(w.base), w.base.width, t.mesh.fine), O = [];
1892
- for (let M = 0; M < U.rest.length / 2; M++) {
1893
- const L = U.rest[M * 2], W = U.rest[M * 2 + 1];
1894
- O.push(i(L, W, Y(L, W)));
1895
- }
1896
- I.addLayer("base", w.base, U, { face: !0 });
1897
- const C = [];
1898
- o.forEach((M, L) => {
1899
- for (const W of $t) {
1900
- const Q = `eye${L}_${W}`, ne = u(_[Q], W === "ball" ? t.mesh.eyeBallCell : t.mesh.eyeCell, Ye(w[Q]), w[Q].width), ee = [];
1901
- for (let oe = 0; oe < ne.rest.length / 2; oe++) ee.push(i(ne.rest[oe * 2], ne.rest[oe * 2 + 1], 0));
1902
- const ce = I.addLayer(Q, w[Q], ne, { eyeBall: W === "ball" ? L : void 0 });
1903
- C.push({ e: M, eye: L, part: W, mesh: ne, W: ee, layer: ce });
1937
+ if (!e.assets[E]) throw new Error(`Missing project asset: ${E}`);
1938
+ return e.assets[E];
1939
+ }
1940
+ return `${o}${E}${R === void 0 ? "" : `?b=${R}`}`;
1941
+ }, S = await it(i("layers.json")), g = ["base", ...t.hand ? ["hand"] : [], ...b.map((E) => E.name), ...[0, 1].flatMap((E) => Wt.map((R) => `eye${E}_${R}`)), "hairmask"], A = Object.fromEntries(await Promise.all(g.map(async (E) => [E, await lt(i(`${E}.png`, S.build))]))), L = rn(A.hairmask, 0), B = (E, R) => L[Math.min(a.h - 1, Math.max(0, Math.round(R))) * a.w + Math.min(a.w - 1, Math.max(0, Math.round(E)))] / 255, j = new v(n, { padTop: e.padTop ?? t.view.padTop, padSide: e.padSide ?? t.view.padSide, fit: e.fit }), k = { base: [0, 0, a.w, a.h], ...S.layers }, U = h(k.base, t.mesh.baseCell, Fe(A.base), A.base.width, t.mesh.fine), O = [];
1942
+ for (let E = 0; E < U.rest.length / 2; E++) {
1943
+ const R = U.rest[E * 2], J = U.rest[E * 2 + 1];
1944
+ O.push(l(R, J, B(R, J)));
1945
+ }
1946
+ j.addLayer("base", A.base, U, { face: !0 });
1947
+ const $ = [];
1948
+ s.forEach((E, R) => {
1949
+ for (const J of Wt) {
1950
+ const te = `eye${R}_${J}`, ee = h(k[te], J === "ball" ? t.mesh.eyeBallCell : t.mesh.eyeCell, Fe(A[te]), A[te].width), se = [];
1951
+ for (let ce = 0; ce < ee.rest.length / 2; ce++) se.push(l(ee.rest[ce * 2], ee.rest[ce * 2 + 1], 0));
1952
+ const le = j.addLayer(te, A[te], ee, { eyeBall: J === "ball" ? R : void 0 });
1953
+ $.push({ e: E, eye: R, part: J, mesh: ee, W: se, layer: le });
1904
1954
  }
1905
1955
  });
1906
- let $ = null;
1956
+ let D = null;
1907
1957
  try {
1908
1958
  {
1909
- const M = await Dt(c("sprites/sprites.json")), L = Object.fromEntries(await Promise.all(Object.keys(M.layers).map(async (W) => [W, await Ht(c(`sprites/${W}.png`, M.build))])));
1910
- $ = x(I, M, L, u, Ye);
1911
- }
1912
- } catch (M) {
1913
- console.warn("eye / mouth sprites not loaded:", M);
1914
- }
1915
- const V = S.map((M) => {
1916
- const L = u(_[M.name], t.mesh.tasselCell, Ye(w[M.name]), w[M.name].width);
1917
- return I.addLayer(M.name, w[M.name], L);
1918
- }), j = t.hand ? u(_.hand, t.mesh.handCell, Ye(w.hand), w.hand.width) : { rest: [], pos: [] }, D = [];
1919
- for (let M = 0; M < j.rest.length / 2; M++) D.push(p(j.rest[M * 2], j.rest[M * 2 + 1]));
1920
- t.hand && I.addLayer("hand", w.hand, j);
1921
- const H = new b(), R = new qn(t.view.gazeCenter ?? [t.head.cx, t.head.cy]);
1922
- e.lipSync !== void 0 && R.setLipSync(e.lipSync);
1923
- const Z = /* @__PURE__ */ new Set();
1924
- R.onMotion = (M) => {
1925
- for (const L of Z) L(M);
1959
+ const E = await it(i("sprites/sprites.json")), R = Object.fromEntries(await Promise.all(Object.keys(E.layers).map(async (J) => [J, await lt(i(`sprites/${J}.png`, E.build))])));
1960
+ D = w(j, E, R, h, Fe);
1961
+ }
1962
+ } catch (E) {
1963
+ console.warn("eye / mouth sprites not loaded:", E);
1964
+ }
1965
+ let q = null;
1966
+ try {
1967
+ const E = await it(i("overlays/overlays.json")), R = Object.fromEntries(await Promise.all(E.overlays.map(async (J) => [J.name, await lt(i(`overlays/${J.file}`))])));
1968
+ q = Xn(j, E, R, h, Fe, l, t.mesh.spriteCell, a.w, a.h, ar);
1969
+ } catch (E) {
1970
+ e.assets?.["overlays/overlays.json"] && console.warn("overlay layers not loaded:", E);
1971
+ }
1972
+ const N = b.map((E) => {
1973
+ const R = h(k[E.name], t.mesh.tasselCell, Fe(A[E.name]), A[E.name].width);
1974
+ return j.addLayer(E.name, A[E.name], R);
1975
+ }), P = t.hand ? h(k.hand, t.mesh.handCell, Fe(A.hand), A.hand.width) : { rest: [], pos: [] }, H = [];
1976
+ for (let E = 0; E < P.rest.length / 2; E++) H.push(m(P.rest[E * 2], P.rest[E * 2 + 1]));
1977
+ t.hand && j.addLayer("hand", A.hand, P);
1978
+ const G = new u(), z = new Qn(t.view.gazeCenter ?? [t.head.cx, t.head.cy]);
1979
+ e.lipSync !== void 0 && z.setLipSync(e.lipSync);
1980
+ const W = /* @__PURE__ */ new Set();
1981
+ z.onMotion = (E) => {
1982
+ for (const R of W) R(E);
1926
1983
  };
1927
- let P = {}, G = 1, J = {};
1928
- const q = new er(), v = [0, 0];
1929
- function A(M) {
1930
- const L = { ...R.update(M) };
1931
- for (const [Q, ne] of Object.entries(P)) L[Q] = G === 1 ? ne : (L[Q] ?? 0) + (ne - (L[Q] ?? 0)) * G;
1932
- R.lipOpen !== null && (L.mouthOpen = R.P.mouthOpen, (!e.preserveMouthForm || P.mouthForm === void 0) && (L.mouthForm = R.P.mouthForm));
1933
- const W = q.apply(L, M);
1934
- return J = W, W;
1935
- }
1936
- function E(M) {
1937
- const L = A(M), W = H.step(L, M), Q = U.pos, ne = U.rest;
1938
- for (let K = 0; K < O.length; K++)
1939
- f(ne[K * 2], ne[K * 2 + 1], O[K], L, W, v), Q[K * 2] = v[0], Q[K * 2 + 1] = v[1];
1940
- const ee = (K) => $ ? 1 : K;
1941
- for (const K of C) {
1942
- const De = ee(K.eye === 0 ? L.eyeROpen : L.eyeLOpen), Rt = K.eye === 0 ? L.eyeSmile : L.eyeSmileL ?? L.eyeSmile;
1943
- K.layer.alpha = g(K.part, De, Rt);
1944
- const Lt = K.mesh.rest, kt = K.mesh.pos;
1945
- for (let Te = 0; Te < K.W.length; Te++) {
1946
- const _t = Lt[Te * 2], yn = Lt[Te * 2 + 1];
1947
- f(_t, m(K.e, K.part, _t, yn, De, Rt), K.W[Te], L, W, v), kt[Te * 2] = v[0], kt[Te * 2 + 1] = v[1];
1984
+ let I = {}, V = 1, K = {};
1985
+ const x = new rr(), M = [0, 0];
1986
+ function T(E) {
1987
+ const R = { ...z.update(E) };
1988
+ for (const [te, ee] of Object.entries(I)) R[te] = V === 1 ? ee : (R[te] ?? 0) + (ee - (R[te] ?? 0)) * V;
1989
+ z.lipOpen !== null && (R.mouthOpen = z.P.mouthOpen, (!e.preserveMouthForm || I.mouthForm === void 0) && (R.mouthForm = z.P.mouthForm));
1990
+ const J = x.apply(R, E);
1991
+ return K = J, J;
1992
+ }
1993
+ function _(E) {
1994
+ const R = T(E), J = G.step(R, E), te = U.pos, ee = U.rest;
1995
+ for (let Q = 0; Q < O.length; Q++)
1996
+ f(ee[Q * 2], ee[Q * 2 + 1], O[Q], R, J, M), te[Q * 2] = M[0], te[Q * 2 + 1] = M[1];
1997
+ const se = (Q, ke) => D?.eyesDrawn[ke] ? 1 : Q;
1998
+ for (const Q of $) {
1999
+ const ke = se(Q.eye === 0 ? R.eyeROpen : R.eyeLOpen, Q.eye), Ft = Q.eye === 0 ? R.eyeSmile : R.eyeSmileL ?? R.eyeSmile;
2000
+ Q.layer.alpha = d(Q.part, ke, Ft);
2001
+ const Bt = Q.mesh.rest, Ut = Q.mesh.pos;
2002
+ for (let Oe = 0; Oe < Q.W.length; Oe++) {
2003
+ const Yt = Bt[Oe * 2], vn = Bt[Oe * 2 + 1];
2004
+ f(Yt, y(Q.e, Q.part, Yt, vn, ke, Ft), Q.W[Oe], R, J, M), Ut[Oe * 2] = M[0], Ut[Oe * 2 + 1] = M[1];
1948
2005
  }
1949
2006
  }
1950
- const ce = $?.update(L, W, M, In(q.name, R.mode === "manual" ? null : R.expr));
1951
- if (ce) for (const K of C) ce.eyes[K.eye] && (K.layer.alpha = 0);
1952
- const oe = j.pos, _e = j.rest, gn = l(L);
1953
- for (let K = 0; K < D.length; K++)
1954
- h(_e[K * 2], _e[K * 2 + 1], D[K], L, gn, v), oe[K * 2] = v[0], oe[K * 2 + 1] = v[1];
1955
- H.chains.forEach((K, De) => tr(K, V[De].mesh));
1956
- const $e = 7;
1957
- I.draw({
1958
- angleX: L.angleX,
1959
- angleY: L.angleY,
1960
- angleZ: L.angleZ,
2007
+ const le = D?.update(R, J, E, In(x.name, z.mode === "manual" ? null : z.expr));
2008
+ if (le) for (const Q of $) le.eyes[Q.eye] && (Q.layer.alpha = 0);
2009
+ q?.update(R, J, f, M);
2010
+ const ce = P.pos, je = P.rest, _e = c(R);
2011
+ for (let Q = 0; Q < H.length; Q++)
2012
+ p(je[Q * 2], je[Q * 2 + 1], H[Q], R, _e, M), ce[Q * 2] = M[0], ce[Q * 2 + 1] = M[1];
2013
+ G.chains.forEach((Q, ke) => or(Q, N[ke].mesh));
2014
+ const Ze = 7;
2015
+ j.draw({
2016
+ angleX: R.angleX,
2017
+ angleY: R.angleY,
2018
+ angleZ: R.angleZ,
1961
2019
  eyes: [
1962
- ee(L.eyeROpen),
1963
- L.eyeSmile,
1964
- L.gazeX * $e,
1965
- -L.gazeY * $e * 0.6,
1966
- ee(L.eyeLOpen),
1967
- L.eyeSmileL ?? L.eyeSmile,
1968
- L.gazeX * $e * 0.85,
1969
- -L.gazeY * $e * 0.6
2020
+ se(R.eyeROpen, 0),
2021
+ R.eyeSmile,
2022
+ R.gazeX * Ze,
2023
+ -R.gazeY * Ze * 0.6,
2024
+ se(R.eyeLOpen, 1),
2025
+ R.eyeSmileL ?? R.eyeSmile,
2026
+ R.gazeX * Ze * 0.85,
2027
+ -R.gazeY * Ze * 0.6
1970
2028
  ],
1971
2029
  // with sprites the drawn mouths replace the shader-painted one
1972
- mouthOpen: $ ? 0 : L.mouthOpen,
1973
- mouthForm: L.mouthForm,
1974
- cheek: L.blush,
2030
+ mouthOpen: le?.mouthDrawn ? 0 : R.mouthOpen,
2031
+ mouthForm: R.mouthForm,
2032
+ cheek: R.blush,
1975
2033
  showMesh: !1,
1976
2034
  originalAlpha: 0,
1977
2035
  joints: []
1978
2036
  });
1979
2037
  }
1980
- let k = 0, N = performance.now(), z = !1;
1981
- const F = (M) => {
1982
- const L = Math.min(0.05, (M - N) / 1e3);
1983
- N = M, E(L), k = requestAnimationFrame(F);
2038
+ let Y = 0, X = performance.now(), F = !1;
2039
+ const C = (E) => {
2040
+ const R = Math.min(0.05, (E - X) / 1e3);
2041
+ X = E, _(R), Y = requestAnimationFrame(C);
1984
2042
  };
1985
- return e.manual || (k = requestAnimationFrame(F)), {
2043
+ return e.manual || (Y = requestAnimationFrame(C)), {
1986
2044
  motions: [
1987
- ...Object.entries(At).map(([M, L]) => ({ id: M, label: L.label, idle: !1 })),
1988
- ...Object.entries(Mt).map(([M, L]) => ({ id: M, label: L.label, idle: !0 }))
2045
+ ...Object.entries(Tt).map(([E, R]) => ({ id: E, label: R.label, idle: !1 })),
2046
+ ...Object.entries(Ot).map(([E, R]) => ({ id: E, label: R.label, idle: !0 }))
1989
2047
  ],
1990
- setParameters(M, L = 1) {
1991
- P = { ...M }, G = Math.min(1, Math.max(0, Number(L) || 0));
2048
+ setParameters(E, R = 1) {
2049
+ I = { ...E }, V = Math.min(1, Math.max(0, Number(R) || 0));
1992
2050
  },
1993
2051
  getParameters() {
1994
- return { ...J };
2052
+ return { ...K };
1995
2053
  },
1996
2054
  /** 0..1 loudness of the voice being played (e.g. normalised RMS). */
1997
- setVoiceLevel(M) {
1998
- R.setVoiceLevel(M);
2055
+ setVoiceLevel(E) {
2056
+ z.setVoiceLevel(E);
1999
2057
  },
2000
2058
  /** Partial mouth-movement settings; omitted fields retain their current values. */
2001
- setLipSync(M) {
2002
- R.setLipSync(M);
2059
+ setLipSync(E) {
2060
+ z.setLipSync(E);
2003
2061
  },
2004
2062
  /** true while TTS audio is playing: idle motions pause and the head nods along. */
2005
- setSpeaking(M) {
2006
- R.setSpeaking(M);
2063
+ setSpeaking(E) {
2064
+ z.setSpeaking(E);
2007
2065
  },
2008
2066
  /** AITuber OnAir emotion tag: happy / sad / angry / surprised / relaxed / neutral (or null). */
2009
- setEmotion(M, L) {
2010
- R.setEmotion(M, L);
2067
+ setEmotion(E, R) {
2068
+ z.setEmotion(E, R);
2011
2069
  },
2012
2070
  /** Live expression layered over tracking (see OVERLAY_EXPRESSIONS); 'neutral' clears it. */
2013
- setExpression(M) {
2014
- q.set(M);
2071
+ setExpression(E) {
2072
+ x.set(E);
2073
+ },
2074
+ /** Show the named overlay layers (built/overlays/overlays.json) and hide the others. */
2075
+ setOverlays(E) {
2076
+ q?.set(E);
2015
2077
  },
2016
2078
  getExpression() {
2017
- return { name: q.name, active: q.active };
2079
+ return { name: x.name, active: x.active };
2018
2080
  },
2019
2081
  /** How much the head moves with the voice while speaking (1 = default, calmer below). */
2020
- setTalkGain(M) {
2021
- R.talkGain = Math.max(0, Number(M) || 0);
2082
+ setTalkGain(E) {
2083
+ z.talkGain = Math.max(0, Number(E) || 0);
2022
2084
  },
2023
2085
  /** Play a motion or idle motion by id (see `motions`). */
2024
- play(M) {
2025
- R.hasMotion(M) && R.playMotion(M);
2086
+ play(E) {
2087
+ z.hasMotion(E) && z.playMotion(E);
2026
2088
  },
2027
2089
  /** Move the mouth through the vowels of kana text (no audio; for previews). */
2028
- speakKana(M, L) {
2029
- R.speakKana(String(M ?? ""), L);
2090
+ speakKana(E, R) {
2091
+ z.speakKana(String(E ?? ""), R);
2030
2092
  },
2031
- holdMouth(M) {
2032
- R.holdMouth(M);
2093
+ holdMouth(E) {
2094
+ z.holdMouth(E);
2033
2095
  },
2034
2096
  stopLipSync() {
2035
- R.stopLipSync();
2097
+ z.stopLipSync();
2036
2098
  },
2037
2099
  getLipSyncState() {
2038
- return R.getLipSyncState();
2100
+ return z.getLipSyncState();
2039
2101
  },
2040
- setAutoIdle(M) {
2041
- R.autoIdle = !!M;
2102
+ setAutoIdle(E) {
2103
+ z.autoIdle = !!E;
2042
2104
  },
2043
- setLighting(M) {
2044
- I.setLighting(M);
2105
+ setLighting(E) {
2106
+ j.setLighting(E);
2045
2107
  },
2046
2108
  getLightingStats() {
2047
- return { ...I.lightingStats };
2109
+ return { ...j.lightingStats };
2048
2110
  },
2049
- setAutoMotion(M) {
2050
- R.autoMotion = !!M;
2111
+ setAutoMotion(E) {
2112
+ z.autoMotion = !!E;
2051
2113
  },
2052
2114
  /** Hair / tassel sway multiplier (1 = default). */
2053
- setSwayGain(M) {
2054
- H.gain = M;
2115
+ setSwayGain(E) {
2116
+ G.gain = E;
2055
2117
  },
2056
2118
  /** Called with the motion id when a motion starts and with null when it ends. */
2057
- onMotion(M) {
2058
- return Z.add(M), () => Z.delete(M);
2119
+ onMotion(E) {
2120
+ return W.add(E), () => W.delete(E);
2059
2121
  },
2060
2122
  /** Advance the simulation by `sec` and draw (for tests / hidden tabs). */
2061
- advance(M, L = 60) {
2062
- if (!z)
2063
- if (M === 0) E(0);
2064
- else for (let W = 0; W < Math.round(M * L); W++) E(1 / L);
2123
+ advance(E, R = 60) {
2124
+ if (!F)
2125
+ if (E === 0) _(0);
2126
+ else for (let J = 0; J < Math.round(E * R); J++) _(1 / R);
2065
2127
  },
2066
2128
  /** Update motion and lip sync without drawing a hidden preview. */
2067
- advanceParameters(M) {
2068
- z || A(M);
2129
+ advanceParameters(E) {
2130
+ F || T(E);
2069
2131
  },
2070
2132
  destroy() {
2071
- z || (z = !0, cancelAnimationFrame(k), Z.clear(), I.destroy());
2133
+ F || (F = !0, cancelAnimationFrame(Y), W.clear(), j.destroy());
2072
2134
  }
2073
2135
  };
2074
2136
  }
2075
- const _r = Object.keys(Et);
2076
- async function rr(n, e) {
2077
- return nr(n, { ...e, rig: xt(e.rig) });
2137
+ const Yr = Object.keys(Rt);
2138
+ async function ir(n, e) {
2139
+ return sr(n, { ...e, rig: Et(e.rig) });
2078
2140
  }
2079
- function ke(n) {
2141
+ function Ye(n) {
2080
2142
  if (!n || typeof n != "object" || Array.isArray(n))
2081
2143
  throw new Error("avatar.json: expected an object");
2082
2144
  const e = n, t = [];
@@ -2090,7 +2152,7 @@ function ke(n) {
2090
2152
  }
2091
2153
  if (e.lipSync !== void 0)
2092
2154
  try {
2093
- r.lipSync = wt(e.lipSync);
2155
+ r.lipSync = St(e.lipSync);
2094
2156
  } catch (a) {
2095
2157
  t.push(`avatar.json.lipSync: ${a instanceof Error ? a.message : String(a)}`);
2096
2158
  }
@@ -2098,111 +2160,126 @@ function ke(n) {
2098
2160
  `));
2099
2161
  return r;
2100
2162
  }
2101
- function nn(n) {
2102
- return ke({ format: "mesh-avatar", version: 1, name: n });
2163
+ function an(n) {
2164
+ return Ye({ format: "mesh-avatar", version: 1, name: n });
2103
2165
  }
2104
- const at = (n) => !!n && typeof n == "object" && !Array.isArray(n), Oe = (n, e) => Array.isArray(n) && n.length === e && n.every((t) => typeof t == "number" && Number.isFinite(t)), ot = (n, e) => Object.hasOwn(n, e);
2105
- function rn(n) {
2166
+ const Je = (n) => !!n && typeof n == "object" && !Array.isArray(n), Re = (n, e) => Array.isArray(n) && n.length === e && n.every((t) => typeof t == "number" && Number.isFinite(t)), ct = (n, e) => Object.hasOwn(n, e);
2167
+ function on(n) {
2106
2168
  const e = [];
2107
- function t(h, S = !0) {
2108
- if (!ot(n, h)) {
2109
- S && e.push(`${h}: missing file`);
2169
+ function t(b, v = !0) {
2170
+ if (!ct(n, b)) {
2171
+ v && e.push(`${b}: missing file`);
2110
2172
  return;
2111
2173
  }
2112
- const y = n[h];
2113
- if (!(y instanceof Uint8Array)) {
2114
- e.push(`${h}: JSON bytes are required`);
2174
+ const h = n[b];
2175
+ if (!(h instanceof Uint8Array)) {
2176
+ e.push(`${b}: JSON bytes are required`);
2115
2177
  return;
2116
2178
  }
2117
2179
  let u;
2118
2180
  try {
2119
- u = JSON.parse(new TextDecoder("utf-8", { fatal: !0 }).decode(y));
2181
+ u = JSON.parse(new TextDecoder("utf-8", { fatal: !0 }).decode(h));
2120
2182
  } catch {
2121
- e.push(`${h}: invalid UTF-8 JSON`);
2183
+ e.push(`${b}: invalid UTF-8 JSON`);
2122
2184
  return;
2123
2185
  }
2124
- if (!at(u)) {
2125
- e.push(`${h}: expected an object`);
2186
+ if (!Je(u)) {
2187
+ e.push(`${b}: expected an object`);
2126
2188
  return;
2127
2189
  }
2128
2190
  return u;
2129
2191
  }
2130
- function r(h, S, y = !0) {
2131
- return h.version === 1 || y && h.version === void 0 ? !0 : (e.push(`${S}.version: unsupported version ${JSON.stringify(h.version) ?? "missing"} (expected 1)`), !1);
2192
+ function r(b, v, h = !0) {
2193
+ return b.version === 1 || h && b.version === void 0 ? !0 : (e.push(`${v}.version: unsupported version ${JSON.stringify(b.version) ?? "missing"} (expected 1)`), !1);
2132
2194
  }
2133
- function a(h) {
2134
- if (!ot(n, h)) {
2135
- e.push(`${h}: missing file`);
2195
+ function a(b) {
2196
+ if (!ct(n, b)) {
2197
+ e.push(`${b}: missing file`);
2136
2198
  return;
2137
2199
  }
2138
- const S = n[h], y = S instanceof Uint8Array ? S.byteLength : S?.size;
2139
- (!Number.isSafeInteger(y) || y <= 0) && e.push(`${h}: expected a non-empty file`);
2200
+ const v = n[b], h = v instanceof Uint8Array ? v.byteLength : v?.size;
2201
+ (!Number.isSafeInteger(h) || h <= 0) && e.push(`${b}: expected a non-empty file`);
2140
2202
  }
2141
- function o(h, S, y, u) {
2142
- if (!at(h)) {
2143
- e.push(`${S}.layers: expected a layer rectangle map`);
2203
+ function s(b, v, h, u) {
2204
+ if (!Je(b)) {
2205
+ e.push(`${v}.layers: expected a layer rectangle map`);
2144
2206
  return;
2145
2207
  }
2146
- for (const [b, x] of Object.entries(h)) {
2147
- const s = `${S}.layers.${b}`;
2148
- if (!/^[A-Za-z0-9_-]+$/.test(b)) {
2149
- e.push(`${s}: expected a safe layer name (letters, digits, hyphens or underscores)`);
2208
+ for (const [w, o] of Object.entries(b)) {
2209
+ const i = `${v}.layers.${w}`;
2210
+ if (!/^[A-Za-z0-9_-]+$/.test(w)) {
2211
+ e.push(`${i}: expected a safe layer name (letters, digits, hyphens or underscores)`);
2150
2212
  continue;
2151
2213
  }
2152
- !Oe(x, 4) || !x.every(Number.isInteger) || x[0] < 0 || x[1] < 0 || x[2] <= 0 || x[3] <= 0 ? e.push(`${s}: expected [x, y, width, height] with non-negative integer origins and positive integer dimensions`) : u && (x[0] + x[2] > u[0] || x[1] + x[3] > u[1]) && e.push(`${s}: rectangle must stay inside the image`), a(`${y}/${b}.png`);
2214
+ !Re(o, 4) || !o.every(Number.isInteger) || o[0] < 0 || o[1] < 0 || o[2] <= 0 || o[3] <= 0 ? e.push(`${i}: expected [x, y, width, height] with non-negative integer origins and positive integer dimensions`) : u && (o[0] + o[2] > u[0] || o[1] + o[3] > u[1]) && e.push(`${i}: rectangle must stay inside the image`), a(`${h}/${w}.png`);
2153
2215
  }
2154
- return h;
2216
+ return b;
2155
2217
  }
2156
- function i(h, S) {
2157
- (typeof h.build != "string" || !h.build.trim()) && e.push(`${S}.build: expected a non-empty string`);
2218
+ function l(b, v) {
2219
+ (typeof b.build != "string" || !b.build.trim()) && e.push(`${v}.build: expected a non-empty string`);
2158
2220
  }
2159
2221
  const f = t("avatar.json", !1);
2160
2222
  if (f)
2161
2223
  try {
2162
- ke(f);
2163
- } catch (h) {
2164
- e.push(h.message);
2224
+ Ye(f);
2225
+ } catch (b) {
2226
+ e.push(b.message);
2165
2227
  }
2166
- const m = t("rig.json");
2167
- let g;
2168
- if (m && r(m, "rig.json", !1)) {
2169
- const h = Kt(m);
2170
- e.push(...h.map((S) => `rig.json: ${S}`)), h.length || (g = m);
2228
+ const y = t("rig.json");
2229
+ let d;
2230
+ if (y && r(y, "rig.json", !1)) {
2231
+ const b = en(y);
2232
+ e.push(...b.map((v) => `rig.json: ${v}`)), b.length || (d = y);
2171
2233
  }
2172
2234
  a("built/base.png"), a("built/hairmask.png");
2173
- const p = t("built/layers.json");
2174
- if (p && r(p, "built/layers.json")) {
2175
- i(p, "built/layers.json");
2176
- let h;
2177
- !Oe(p.size, 2) || !p.size.every((y) => Number.isInteger(y) && y > 0) ? e.push("built/layers.json.size: expected [width, height] with positive integer dimensions") : (h = p.size, g && (h[0] !== g.image.width || h[1] !== g.image.height) && e.push("built/layers.json.size: must match rig.json.image"));
2178
- const S = o(p.layers, "built/layers.json", "built", h);
2179
- if (g && S) {
2180
- const y = [
2181
- ...g.eyes.flatMap((u, b) => ["ball", "low", "crease", "lash"].map((x) => `eye${b}_${x}`)),
2182
- ...g.hand ? ["hand"] : [],
2183
- ...(g.accessories ?? []).map((u) => u.name)
2235
+ const m = t("built/layers.json");
2236
+ if (m && r(m, "built/layers.json")) {
2237
+ l(m, "built/layers.json");
2238
+ let b;
2239
+ !Re(m.size, 2) || !m.size.every((h) => Number.isInteger(h) && h > 0) ? e.push("built/layers.json.size: expected [width, height] with positive integer dimensions") : (b = m.size, d && (b[0] !== d.image.width || b[1] !== d.image.height) && e.push("built/layers.json.size: must match rig.json.image"));
2240
+ const v = s(m.layers, "built/layers.json", "built", b);
2241
+ if (d && v) {
2242
+ const h = [
2243
+ ...d.eyes.flatMap((u, w) => ["ball", "low", "crease", "lash"].map((o) => `eye${w}_${o}`)),
2244
+ ...d.hand ? ["hand"] : [],
2245
+ ...(d.accessories ?? []).map((u) => u.name)
2184
2246
  ];
2185
- for (const u of y) ot(S, u) || e.push(`built/layers.json.layers.${u}: missing required layer`);
2247
+ for (const u of h) ct(v, u) || e.push(`built/layers.json.layers.${u}: missing required layer`);
2186
2248
  }
2187
- !Array.isArray(p.eyes) || p.eyes.length !== 2 ? e.push("built/layers.json.eyes: expected two eye curves") : p.eyes.forEach((y, u) => {
2188
- const b = `built/layers.json.eyes[${u}]`;
2189
- if (!at(y)) {
2190
- e.push(`${b}: expected an object`);
2249
+ !Array.isArray(m.eyes) || m.eyes.length !== 2 ? e.push("built/layers.json.eyes: expected two eye curves") : m.eyes.forEach((h, u) => {
2250
+ const w = `built/layers.json.eyes[${u}]`;
2251
+ if (!Je(h)) {
2252
+ e.push(`${w}: expected an object`);
2191
2253
  return;
2192
2254
  }
2193
- Oe([y.x0, y.x1], 2) && y.x0 < y.x1 || e.push(`${b}: expected finite x0 < x1`);
2194
- for (const s of ["top", "bot"])
2195
- Oe(y[s], 24) || e.push(`${b}.${s}: expected 24 finite numbers`);
2196
- if (Oe(y.top, 24) && Oe(y.bot, 24) && y.top.some((s, c) => s > y.bot[c]) && e.push(`${b}.top: must not exceed bottom curve`), g) for (const s of ["x0", "x1", "top", "bot"]) {
2197
- const c = y[s], T = g.eyes[u][s];
2198
- (Array.isArray(T) ? Oe(c, T.length) && c.every((w, B) => w === T[B]) : c === T) || e.push(`${b}.${s}: must match rig.json.eyes[${u}].${s}`);
2255
+ Re([h.x0, h.x1], 2) && h.x0 < h.x1 || e.push(`${w}: expected finite x0 < x1`);
2256
+ for (const i of ["top", "bot"])
2257
+ Re(h[i], 24) || e.push(`${w}.${i}: expected 24 finite numbers`);
2258
+ if (Re(h.top, 24) && Re(h.bot, 24) && h.top.some((i, S) => i > h.bot[S]) && e.push(`${w}.top: must not exceed bottom curve`), d) for (const i of ["x0", "x1", "top", "bot"]) {
2259
+ const S = h[i], g = d.eyes[u][i];
2260
+ (Array.isArray(g) ? Re(S, g.length) && S.every((L, B) => L === g[B]) : S === g) || e.push(`${w}.${i}: must match rig.json.eyes[${u}].${i}`);
2199
2261
  }
2200
2262
  });
2201
2263
  }
2202
- const l = t("built/sprites/sprites.json", !1);
2203
- return l && r(l, "built/sprites/sprites.json") && (i(l, "built/sprites/sprites.json"), o(l.layers, "built/sprites/sprites.json", "built/sprites", g ? [g.image.width, g.image.height] : void 0)), e;
2264
+ const c = t("built/sprites/sprites.json", !1);
2265
+ c && r(c, "built/sprites/sprites.json") && (l(c, "built/sprites/sprites.json"), s(c.layers, "built/sprites/sprites.json", "built/sprites", d ? [d.image.width, d.image.height] : void 0));
2266
+ const p = t("built/overlays/overlays.json", !1);
2267
+ if (p && r(p, "built/overlays/overlays.json"))
2268
+ if (!Array.isArray(p.overlays)) e.push("built/overlays/overlays.json.overlays: expected an array");
2269
+ else {
2270
+ const b = /* @__PURE__ */ new Set();
2271
+ for (const [v, h] of p.overlays.entries()) {
2272
+ const u = `built/overlays/overlays.json.overlays[${v}]`;
2273
+ if (!Je(h) || typeof h.name != "string" || !/^[A-Za-z0-9_-]+$/.test(h.name) || typeof h.file != "string" || !/^[A-Za-z0-9_-]+\.png$/.test(h.file)) {
2274
+ e.push(`${u}: expected { name, file } with safe names`);
2275
+ continue;
2276
+ }
2277
+ b.has(h.name) && e.push(`${u}.name: duplicate overlay name`), b.add(h.name), a(`built/overlays/${h.file}`);
2278
+ }
2279
+ }
2280
+ return e;
2204
2281
  }
2205
- var ae = Uint8Array, ue = Uint16Array, St = Int32Array, Je = new ae([
2282
+ var ae = Uint8Array, fe = Uint16Array, Lt = Int32Array, et = new ae([
2206
2283
  0,
2207
2284
  0,
2208
2285
  0,
@@ -2237,7 +2314,7 @@ var ae = Uint8Array, ue = Uint16Array, St = Int32Array, Je = new ae([
2237
2314
  0,
2238
2315
  /* impossible */
2239
2316
  0
2240
- ]), Ke = new ae([
2317
+ ]), tt = new ae([
2241
2318
  0,
2242
2319
  0,
2243
2320
  0,
@@ -2271,65 +2348,65 @@ var ae = Uint8Array, ue = Uint16Array, St = Int32Array, Je = new ae([
2271
2348
  /* unused */
2272
2349
  0,
2273
2350
  0
2274
- ]), ut = new ae([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]), an = function(n, e) {
2275
- for (var t = new ue(31), r = 0; r < 31; ++r)
2351
+ ]), dt = new ae([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]), sn = function(n, e) {
2352
+ for (var t = new fe(31), r = 0; r < 31; ++r)
2276
2353
  t[r] = e += 1 << n[r - 1];
2277
- for (var a = new St(t[30]), r = 1; r < 30; ++r)
2278
- for (var o = t[r]; o < t[r + 1]; ++o)
2279
- a[o] = o - t[r] << 5 | r;
2354
+ for (var a = new Lt(t[30]), r = 1; r < 30; ++r)
2355
+ for (var s = t[r]; s < t[r + 1]; ++s)
2356
+ a[s] = s - t[r] << 5 | r;
2280
2357
  return { b: t, r: a };
2281
- }, on = an(Je, 2), sn = on.b, ft = on.r;
2282
- sn[28] = 258, ft[258] = 28;
2283
- var ln = an(Ke, 0), ar = ln.b, Zt = ln.r, mt = new ue(32768);
2358
+ }, ln = sn(et, 2), cn = ln.b, gt = ln.r;
2359
+ cn[28] = 258, gt[258] = 28;
2360
+ var hn = sn(tt, 0), lr = hn.b, Gt = hn.r, yt = new fe(32768);
2284
2361
  for (var re = 0; re < 32768; ++re) {
2285
- var Ee = (re & 43690) >> 1 | (re & 21845) << 1;
2286
- Ee = (Ee & 52428) >> 2 | (Ee & 13107) << 2, Ee = (Ee & 61680) >> 4 | (Ee & 3855) << 4, mt[re] = ((Ee & 65280) >> 8 | (Ee & 255) << 8) >> 1;
2362
+ var Se = (re & 43690) >> 1 | (re & 21845) << 1;
2363
+ Se = (Se & 52428) >> 2 | (Se & 13107) << 2, Se = (Se & 61680) >> 4 | (Se & 3855) << 4, yt[re] = ((Se & 65280) >> 8 | (Se & 255) << 8) >> 1;
2287
2364
  }
2288
- var be = (function(n, e, t) {
2289
- for (var r = n.length, a = 0, o = new ue(e); a < r; ++a)
2290
- n[a] && ++o[n[a] - 1];
2291
- var i = new ue(e);
2365
+ var xe = (function(n, e, t) {
2366
+ for (var r = n.length, a = 0, s = new fe(e); a < r; ++a)
2367
+ n[a] && ++s[n[a] - 1];
2368
+ var l = new fe(e);
2292
2369
  for (a = 1; a < e; ++a)
2293
- i[a] = i[a - 1] + o[a - 1] << 1;
2370
+ l[a] = l[a - 1] + s[a - 1] << 1;
2294
2371
  var f;
2295
2372
  if (t) {
2296
- f = new ue(1 << e);
2297
- var m = 15 - e;
2373
+ f = new fe(1 << e);
2374
+ var y = 15 - e;
2298
2375
  for (a = 0; a < r; ++a)
2299
2376
  if (n[a])
2300
- for (var g = a << 4 | n[a], p = e - n[a], l = i[n[a] - 1]++ << p, h = l | (1 << p) - 1; l <= h; ++l)
2301
- f[mt[l] >> m] = g;
2377
+ for (var d = a << 4 | n[a], m = e - n[a], c = l[n[a] - 1]++ << m, p = c | (1 << m) - 1; c <= p; ++c)
2378
+ f[yt[c] >> y] = d;
2302
2379
  } else
2303
- for (f = new ue(r), a = 0; a < r; ++a)
2304
- n[a] && (f[a] = mt[i[n[a] - 1]++] >> 15 - n[a]);
2380
+ for (f = new fe(r), a = 0; a < r; ++a)
2381
+ n[a] && (f[a] = yt[l[n[a] - 1]++] >> 15 - n[a]);
2305
2382
  return f;
2306
- }), Se = new ae(288);
2383
+ }), Te = new ae(288);
2307
2384
  for (var re = 0; re < 144; ++re)
2308
- Se[re] = 8;
2385
+ Te[re] = 8;
2309
2386
  for (var re = 144; re < 256; ++re)
2310
- Se[re] = 9;
2387
+ Te[re] = 9;
2311
2388
  for (var re = 256; re < 280; ++re)
2312
- Se[re] = 7;
2389
+ Te[re] = 7;
2313
2390
  for (var re = 280; re < 288; ++re)
2314
- Se[re] = 8;
2315
- var ze = new ae(32);
2391
+ Te[re] = 8;
2392
+ var De = new ae(32);
2316
2393
  for (var re = 0; re < 32; ++re)
2317
- ze[re] = 5;
2318
- var or = /* @__PURE__ */ be(Se, 9, 0), sr = /* @__PURE__ */ be(Se, 9, 1), ir = /* @__PURE__ */ be(ze, 5, 0), lr = /* @__PURE__ */ be(ze, 5, 1), st = function(n) {
2394
+ De[re] = 5;
2395
+ var cr = /* @__PURE__ */ xe(Te, 9, 0), hr = /* @__PURE__ */ xe(Te, 9, 1), ur = /* @__PURE__ */ xe(De, 5, 0), fr = /* @__PURE__ */ xe(De, 5, 1), ht = function(n) {
2319
2396
  for (var e = n[0], t = 1; t < n.length; ++t)
2320
2397
  n[t] > e && (e = n[t]);
2321
2398
  return e;
2322
- }, pe = function(n, e, t) {
2399
+ }, de = function(n, e, t) {
2323
2400
  var r = e / 8 | 0;
2324
2401
  return (n[r] | n[r + 1] << 8) >> (e & 7) & t;
2325
- }, it = function(n, e) {
2402
+ }, ut = function(n, e) {
2326
2403
  var t = e / 8 | 0;
2327
2404
  return (n[t] | n[t + 1] << 8 | n[t + 2] << 16) >> (e & 7);
2328
- }, Tt = function(n) {
2405
+ }, _t = function(n) {
2329
2406
  return (n + 7) / 8 | 0;
2330
- }, Pe = function(n, e, t) {
2407
+ }, He = function(n, e, t) {
2331
2408
  return (e == null || e < 0) && (e = 0), (t == null || t > n.length) && (t = n.length), new ae(n.subarray(e, t));
2332
- }, cr = [
2409
+ }, mr = [
2333
2410
  "unexpected EOF",
2334
2411
  "invalid block type",
2335
2412
  "invalid length/literal",
@@ -2346,649 +2423,653 @@ var or = /* @__PURE__ */ be(Se, 9, 0), sr = /* @__PURE__ */ be(Se, 9, 1), ir = /
2346
2423
  "stream finishing",
2347
2424
  "invalid zip data"
2348
2425
  // determined by unknown compression method
2349
- ], le = function(n, e, t) {
2350
- var r = new Error(e || cr[n]);
2351
- if (r.code = n, Error.captureStackTrace && Error.captureStackTrace(r, le), !t)
2426
+ ], he = function(n, e, t) {
2427
+ var r = new Error(e || mr[n]);
2428
+ if (r.code = n, Error.captureStackTrace && Error.captureStackTrace(r, he), !t)
2352
2429
  throw r;
2353
2430
  return r;
2354
- }, hr = function(n, e, t, r) {
2355
- var a = n.length, o = r ? r.length : 0;
2431
+ }, pr = function(n, e, t, r) {
2432
+ var a = n.length, s = r ? r.length : 0;
2356
2433
  if (!a || e.f && !e.l)
2357
2434
  return t || new ae(0);
2358
- var i = !t, f = i || e.i != 2, m = e.i;
2359
- i && (t = new ae(a * 3));
2360
- var g = function(F) {
2361
- var X = t.length;
2362
- if (F > X) {
2363
- var M = new ae(Math.max(X * 2, F));
2364
- M.set(t), t = M;
2365
- }
2366
- }, p = e.f || 0, l = e.p || 0, h = e.b || 0, S = e.l, y = e.d, u = e.m, b = e.n, x = a * 8;
2435
+ var l = !t, f = l || e.i != 2, y = e.i;
2436
+ l && (t = new ae(a * 3));
2437
+ var d = function(F) {
2438
+ var C = t.length;
2439
+ if (F > C) {
2440
+ var Z = new ae(Math.max(C * 2, F));
2441
+ Z.set(t), t = Z;
2442
+ }
2443
+ }, m = e.f || 0, c = e.p || 0, p = e.b || 0, b = e.l, v = e.d, h = e.m, u = e.n, w = a * 8;
2367
2444
  do {
2368
- if (!S) {
2369
- p = pe(n, l, 1);
2370
- var s = pe(n, l + 1, 3);
2371
- if (l += 3, s)
2372
- if (s == 1)
2373
- S = sr, y = lr, u = 9, b = 5;
2374
- else if (s == 2) {
2375
- var w = pe(n, l, 31) + 257, B = pe(n, l + 10, 15) + 4, Y = w + pe(n, l + 5, 31) + 1;
2376
- l += 14;
2377
- for (var I = new ae(Y), _ = new ae(19), U = 0; U < B; ++U)
2378
- _[ut[U]] = pe(n, l + U * 3, 7);
2379
- l += B * 3;
2380
- for (var O = st(_), C = (1 << O) - 1, $ = be(_, O, 1), U = 0; U < Y; ) {
2381
- var V = $[pe(n, l, C)];
2382
- l += V & 15;
2383
- var c = V >> 4;
2384
- if (c < 16)
2385
- I[U++] = c;
2445
+ if (!b) {
2446
+ m = de(n, c, 1);
2447
+ var o = de(n, c + 1, 3);
2448
+ if (c += 3, o)
2449
+ if (o == 1)
2450
+ b = hr, v = fr, h = 9, u = 5;
2451
+ else if (o == 2) {
2452
+ var A = de(n, c, 31) + 257, L = de(n, c + 10, 15) + 4, B = A + de(n, c + 5, 31) + 1;
2453
+ c += 14;
2454
+ for (var j = new ae(B), k = new ae(19), U = 0; U < L; ++U)
2455
+ k[dt[U]] = de(n, c + U * 3, 7);
2456
+ c += L * 3;
2457
+ for (var O = ht(k), $ = (1 << O) - 1, D = xe(k, O, 1), U = 0; U < B; ) {
2458
+ var q = D[de(n, c, $)];
2459
+ c += q & 15;
2460
+ var i = q >> 4;
2461
+ if (i < 16)
2462
+ j[U++] = i;
2386
2463
  else {
2387
- var j = 0, D = 0;
2388
- for (c == 16 ? (D = 3 + pe(n, l, 3), l += 2, j = I[U - 1]) : c == 17 ? (D = 3 + pe(n, l, 7), l += 3) : c == 18 && (D = 11 + pe(n, l, 127), l += 7); D--; )
2389
- I[U++] = j;
2464
+ var N = 0, P = 0;
2465
+ for (i == 16 ? (P = 3 + de(n, c, 3), c += 2, N = j[U - 1]) : i == 17 ? (P = 3 + de(n, c, 7), c += 3) : i == 18 && (P = 11 + de(n, c, 127), c += 7); P--; )
2466
+ j[U++] = N;
2390
2467
  }
2391
2468
  }
2392
- var H = I.subarray(0, w), R = I.subarray(w);
2393
- u = st(H), b = st(R), S = be(H, u, 1), y = be(R, b, 1);
2469
+ var H = j.subarray(0, A), G = j.subarray(A);
2470
+ h = ht(H), u = ht(G), b = xe(H, h, 1), v = xe(G, u, 1);
2394
2471
  } else
2395
- le(1);
2472
+ he(1);
2396
2473
  else {
2397
- var c = Tt(l) + 4, T = n[c - 4] | n[c - 3] << 8, d = c + T;
2398
- if (d > a) {
2399
- m && le(0);
2474
+ var i = _t(c) + 4, S = n[i - 4] | n[i - 3] << 8, g = i + S;
2475
+ if (g > a) {
2476
+ y && he(0);
2400
2477
  break;
2401
2478
  }
2402
- f && g(h + T), t.set(n.subarray(c, d), h), e.b = h += T, e.p = l = d * 8, e.f = p;
2479
+ f && d(p + S), t.set(n.subarray(i, g), p), e.b = p += S, e.p = c = g * 8, e.f = m;
2403
2480
  continue;
2404
2481
  }
2405
- if (l > x) {
2406
- m && le(0);
2482
+ if (c > w) {
2483
+ y && he(0);
2407
2484
  break;
2408
2485
  }
2409
2486
  }
2410
- f && g(h + 131072);
2411
- for (var Z = (1 << u) - 1, P = (1 << b) - 1, G = l; ; G = l) {
2412
- var j = S[it(n, l) & Z], J = j >> 4;
2413
- if (l += j & 15, l > x) {
2414
- m && le(0);
2487
+ f && d(p + 131072);
2488
+ for (var z = (1 << h) - 1, W = (1 << u) - 1, I = c; ; I = c) {
2489
+ var N = b[ut(n, c) & z], V = N >> 4;
2490
+ if (c += N & 15, c > w) {
2491
+ y && he(0);
2415
2492
  break;
2416
2493
  }
2417
- if (j || le(2), J < 256)
2418
- t[h++] = J;
2419
- else if (J == 256) {
2420
- G = l, S = null;
2494
+ if (N || he(2), V < 256)
2495
+ t[p++] = V;
2496
+ else if (V == 256) {
2497
+ I = c, b = null;
2421
2498
  break;
2422
2499
  } else {
2423
- var q = J - 254;
2424
- if (J > 264) {
2425
- var U = J - 257, v = Je[U];
2426
- q = pe(n, l, (1 << v) - 1) + sn[U], l += v;
2500
+ var K = V - 254;
2501
+ if (V > 264) {
2502
+ var U = V - 257, x = et[U];
2503
+ K = de(n, c, (1 << x) - 1) + cn[U], c += x;
2427
2504
  }
2428
- var A = y[it(n, l) & P], E = A >> 4;
2429
- A || le(3), l += A & 15;
2430
- var R = ar[E];
2431
- if (E > 3) {
2432
- var v = Ke[E];
2433
- R += it(n, l) & (1 << v) - 1, l += v;
2505
+ var M = v[ut(n, c) & W], T = M >> 4;
2506
+ M || he(3), c += M & 15;
2507
+ var G = lr[T];
2508
+ if (T > 3) {
2509
+ var x = tt[T];
2510
+ G += ut(n, c) & (1 << x) - 1, c += x;
2434
2511
  }
2435
- if (l > x) {
2436
- m && le(0);
2512
+ if (c > w) {
2513
+ y && he(0);
2437
2514
  break;
2438
2515
  }
2439
- f && g(h + 131072);
2440
- var k = h + q;
2441
- if (h < R) {
2442
- var N = o - R, z = Math.min(R, k);
2443
- for (N + h < 0 && le(3); h < z; ++h)
2444
- t[h] = r[N + h];
2516
+ f && d(p + 131072);
2517
+ var _ = p + K;
2518
+ if (p < G) {
2519
+ var Y = s - G, X = Math.min(G, _);
2520
+ for (Y + p < 0 && he(3); p < X; ++p)
2521
+ t[p] = r[Y + p];
2445
2522
  }
2446
- for (; h < k; ++h)
2447
- t[h] = t[h - R];
2523
+ for (; p < _; ++p)
2524
+ t[p] = t[p - G];
2448
2525
  }
2449
2526
  }
2450
- e.l = S, e.p = G, e.b = h, e.f = p, S && (p = 1, e.m = u, e.d = y, e.n = b);
2451
- } while (!p);
2452
- return h != t.length && i ? Pe(t, 0, h) : t.subarray(0, h);
2453
- }, Ae = function(n, e, t) {
2527
+ e.l = b, e.p = I, e.b = p, e.f = m, b && (m = 1, e.m = h, e.d = v, e.n = u);
2528
+ } while (!m);
2529
+ return p != t.length && l ? He(t, 0, p) : t.subarray(0, p);
2530
+ }, Me = function(n, e, t) {
2454
2531
  t <<= e & 7;
2455
2532
  var r = e / 8 | 0;
2456
2533
  n[r] |= t, n[r + 1] |= t >> 8;
2457
- }, Ie = function(n, e, t) {
2534
+ }, $e = function(n, e, t) {
2458
2535
  t <<= e & 7;
2459
2536
  var r = e / 8 | 0;
2460
2537
  n[r] |= t, n[r + 1] |= t >> 8, n[r + 2] |= t >> 16;
2461
- }, lt = function(n, e) {
2538
+ }, ft = function(n, e) {
2462
2539
  for (var t = [], r = 0; r < n.length; ++r)
2463
2540
  n[r] && t.push({ s: r, f: n[r] });
2464
- var a = t.length, o = t.slice();
2541
+ var a = t.length, s = t.slice();
2465
2542
  if (!a)
2466
- return { t: hn, l: 0 };
2543
+ return { t: fn, l: 0 };
2467
2544
  if (a == 1) {
2468
- var i = new ae(t[0].s + 1);
2469
- return i[t[0].s] = 1, { t: i, l: 1 };
2545
+ var l = new ae(t[0].s + 1);
2546
+ return l[t[0].s] = 1, { t: l, l: 1 };
2470
2547
  }
2471
- t.sort(function(d, w) {
2472
- return d.f - w.f;
2548
+ t.sort(function(g, A) {
2549
+ return g.f - A.f;
2473
2550
  }), t.push({ s: -1, f: 25001 });
2474
- var f = t[0], m = t[1], g = 0, p = 1, l = 2;
2475
- for (t[0] = { s: -1, f: f.f + m.f, l: f, r: m }; p != a - 1; )
2476
- f = t[t[g].f < t[l].f ? g++ : l++], m = t[g != p && t[g].f < t[l].f ? g++ : l++], t[p++] = { s: -1, f: f.f + m.f, l: f, r: m };
2477
- for (var h = o[0].s, r = 1; r < a; ++r)
2478
- o[r].s > h && (h = o[r].s);
2479
- var S = new ue(h + 1), y = pt(t[p - 1], S, 0);
2480
- if (y > e) {
2481
- var r = 0, u = 0, b = y - e, x = 1 << b;
2482
- for (o.sort(function(w, B) {
2483
- return S[B.s] - S[w.s] || w.f - B.f;
2551
+ var f = t[0], y = t[1], d = 0, m = 1, c = 2;
2552
+ for (t[0] = { s: -1, f: f.f + y.f, l: f, r: y }; m != a - 1; )
2553
+ f = t[t[d].f < t[c].f ? d++ : c++], y = t[d != m && t[d].f < t[c].f ? d++ : c++], t[m++] = { s: -1, f: f.f + y.f, l: f, r: y };
2554
+ for (var p = s[0].s, r = 1; r < a; ++r)
2555
+ s[r].s > p && (p = s[r].s);
2556
+ var b = new fe(p + 1), v = vt(t[m - 1], b, 0);
2557
+ if (v > e) {
2558
+ var r = 0, h = 0, u = v - e, w = 1 << u;
2559
+ for (s.sort(function(A, L) {
2560
+ return b[L.s] - b[A.s] || A.f - L.f;
2484
2561
  }); r < a; ++r) {
2485
- var s = o[r].s;
2486
- if (S[s] > e)
2487
- u += x - (1 << y - S[s]), S[s] = e;
2562
+ var o = s[r].s;
2563
+ if (b[o] > e)
2564
+ h += w - (1 << v - b[o]), b[o] = e;
2488
2565
  else
2489
2566
  break;
2490
2567
  }
2491
- for (u >>= b; u > 0; ) {
2492
- var c = o[r].s;
2493
- S[c] < e ? u -= 1 << e - S[c]++ - 1 : ++r;
2568
+ for (h >>= u; h > 0; ) {
2569
+ var i = s[r].s;
2570
+ b[i] < e ? h -= 1 << e - b[i]++ - 1 : ++r;
2494
2571
  }
2495
- for (; r >= 0 && u; --r) {
2496
- var T = o[r].s;
2497
- S[T] == e && (--S[T], ++u);
2572
+ for (; r >= 0 && h; --r) {
2573
+ var S = s[r].s;
2574
+ b[S] == e && (--b[S], ++h);
2498
2575
  }
2499
- y = e;
2576
+ v = e;
2500
2577
  }
2501
- return { t: new ae(S), l: y };
2502
- }, pt = function(n, e, t) {
2503
- return n.s == -1 ? Math.max(pt(n.l, e, t + 1), pt(n.r, e, t + 1)) : e[n.s] = t;
2504
- }, Wt = function(n) {
2578
+ return { t: new ae(b), l: v };
2579
+ }, vt = function(n, e, t) {
2580
+ return n.s == -1 ? Math.max(vt(n.l, e, t + 1), vt(n.r, e, t + 1)) : e[n.s] = t;
2581
+ }, Vt = function(n) {
2505
2582
  for (var e = n.length; e && !n[--e]; )
2506
2583
  ;
2507
- for (var t = new ue(++e), r = 0, a = n[0], o = 1, i = function(m) {
2508
- t[r++] = m;
2584
+ for (var t = new fe(++e), r = 0, a = n[0], s = 1, l = function(y) {
2585
+ t[r++] = y;
2509
2586
  }, f = 1; f <= e; ++f)
2510
2587
  if (n[f] == a && f != e)
2511
- ++o;
2588
+ ++s;
2512
2589
  else {
2513
- if (!a && o > 2) {
2514
- for (; o > 138; o -= 138)
2515
- i(32754);
2516
- o > 2 && (i(o > 10 ? o - 11 << 5 | 28690 : o - 3 << 5 | 12305), o = 0);
2517
- } else if (o > 3) {
2518
- for (i(a), --o; o > 6; o -= 6)
2519
- i(8304);
2520
- o > 2 && (i(o - 3 << 5 | 8208), o = 0);
2590
+ if (!a && s > 2) {
2591
+ for (; s > 138; s -= 138)
2592
+ l(32754);
2593
+ s > 2 && (l(s > 10 ? s - 11 << 5 | 28690 : s - 3 << 5 | 12305), s = 0);
2594
+ } else if (s > 3) {
2595
+ for (l(a), --s; s > 6; s -= 6)
2596
+ l(8304);
2597
+ s > 2 && (l(s - 3 << 5 | 8208), s = 0);
2521
2598
  }
2522
- for (; o--; )
2523
- i(a);
2524
- o = 1, a = n[f];
2599
+ for (; s--; )
2600
+ l(a);
2601
+ s = 1, a = n[f];
2525
2602
  }
2526
2603
  return { c: t.subarray(0, r), n: e };
2527
- }, Ne = function(n, e) {
2604
+ }, ze = function(n, e) {
2528
2605
  for (var t = 0, r = 0; r < e.length; ++r)
2529
2606
  t += n[r] * e[r];
2530
2607
  return t;
2531
- }, cn = function(n, e, t) {
2532
- var r = t.length, a = Tt(e + 2);
2608
+ }, un = function(n, e, t) {
2609
+ var r = t.length, a = _t(e + 2);
2533
2610
  n[a] = r & 255, n[a + 1] = r >> 8, n[a + 2] = n[a] ^ 255, n[a + 3] = n[a + 1] ^ 255;
2534
- for (var o = 0; o < r; ++o)
2535
- n[a + o + 4] = t[o];
2611
+ for (var s = 0; s < r; ++s)
2612
+ n[a + s + 4] = t[s];
2536
2613
  return (a + 4 + r) * 8;
2537
- }, Gt = function(n, e, t, r, a, o, i, f, m, g, p) {
2538
- Ae(e, p++, t), ++a[256];
2539
- for (var l = lt(a, 15), h = l.t, S = l.l, y = lt(o, 15), u = y.t, b = y.l, x = Wt(h), s = x.c, c = x.n, T = Wt(u), d = T.c, w = T.n, B = new ue(19), Y = 0; Y < s.length; ++Y)
2540
- ++B[s[Y] & 31];
2541
- for (var Y = 0; Y < d.length; ++Y)
2542
- ++B[d[Y] & 31];
2543
- for (var I = lt(B, 7), _ = I.t, U = I.l, O = 19; O > 4 && !_[ut[O - 1]]; --O)
2614
+ }, qt = function(n, e, t, r, a, s, l, f, y, d, m) {
2615
+ Me(e, m++, t), ++a[256];
2616
+ for (var c = ft(a, 15), p = c.t, b = c.l, v = ft(s, 15), h = v.t, u = v.l, w = Vt(p), o = w.c, i = w.n, S = Vt(h), g = S.c, A = S.n, L = new fe(19), B = 0; B < o.length; ++B)
2617
+ ++L[o[B] & 31];
2618
+ for (var B = 0; B < g.length; ++B)
2619
+ ++L[g[B] & 31];
2620
+ for (var j = ft(L, 7), k = j.t, U = j.l, O = 19; O > 4 && !k[dt[O - 1]]; --O)
2544
2621
  ;
2545
- var C = g + 5 << 3, $ = Ne(a, Se) + Ne(o, ze) + i, V = Ne(a, h) + Ne(o, u) + i + 14 + 3 * O + Ne(B, _) + 2 * B[16] + 3 * B[17] + 7 * B[18];
2546
- if (m >= 0 && C <= $ && C <= V)
2547
- return cn(e, p, n.subarray(m, m + g));
2548
- var j, D, H, R;
2549
- if (Ae(e, p, 1 + (V < $)), p += 2, V < $) {
2550
- j = be(h, S, 0), D = h, H = be(u, b, 0), R = u;
2551
- var Z = be(_, U, 0);
2552
- Ae(e, p, c - 257), Ae(e, p + 5, w - 1), Ae(e, p + 10, O - 4), p += 14;
2553
- for (var Y = 0; Y < O; ++Y)
2554
- Ae(e, p + 3 * Y, _[ut[Y]]);
2555
- p += 3 * O;
2556
- for (var P = [s, d], G = 0; G < 2; ++G)
2557
- for (var J = P[G], Y = 0; Y < J.length; ++Y) {
2558
- var q = J[Y] & 31;
2559
- Ae(e, p, Z[q]), p += _[q], q > 15 && (Ae(e, p, J[Y] >> 5 & 127), p += J[Y] >> 12);
2622
+ var $ = d + 5 << 3, D = ze(a, Te) + ze(s, De) + l, q = ze(a, p) + ze(s, h) + l + 14 + 3 * O + ze(L, k) + 2 * L[16] + 3 * L[17] + 7 * L[18];
2623
+ if (y >= 0 && $ <= D && $ <= q)
2624
+ return un(e, m, n.subarray(y, y + d));
2625
+ var N, P, H, G;
2626
+ if (Me(e, m, 1 + (q < D)), m += 2, q < D) {
2627
+ N = xe(p, b, 0), P = p, H = xe(h, u, 0), G = h;
2628
+ var z = xe(k, U, 0);
2629
+ Me(e, m, i - 257), Me(e, m + 5, A - 1), Me(e, m + 10, O - 4), m += 14;
2630
+ for (var B = 0; B < O; ++B)
2631
+ Me(e, m + 3 * B, k[dt[B]]);
2632
+ m += 3 * O;
2633
+ for (var W = [o, g], I = 0; I < 2; ++I)
2634
+ for (var V = W[I], B = 0; B < V.length; ++B) {
2635
+ var K = V[B] & 31;
2636
+ Me(e, m, z[K]), m += k[K], K > 15 && (Me(e, m, V[B] >> 5 & 127), m += V[B] >> 12);
2560
2637
  }
2561
2638
  } else
2562
- j = or, D = Se, H = ir, R = ze;
2563
- for (var Y = 0; Y < f; ++Y) {
2564
- var v = r[Y];
2565
- if (v > 255) {
2566
- var q = v >> 18 & 31;
2567
- Ie(e, p, j[q + 257]), p += D[q + 257], q > 7 && (Ae(e, p, v >> 23 & 31), p += Je[q]);
2568
- var A = v & 31;
2569
- Ie(e, p, H[A]), p += R[A], A > 3 && (Ie(e, p, v >> 5 & 8191), p += Ke[A]);
2639
+ N = cr, P = Te, H = ur, G = De;
2640
+ for (var B = 0; B < f; ++B) {
2641
+ var x = r[B];
2642
+ if (x > 255) {
2643
+ var K = x >> 18 & 31;
2644
+ $e(e, m, N[K + 257]), m += P[K + 257], K > 7 && (Me(e, m, x >> 23 & 31), m += et[K]);
2645
+ var M = x & 31;
2646
+ $e(e, m, H[M]), m += G[M], M > 3 && ($e(e, m, x >> 5 & 8191), m += tt[M]);
2570
2647
  } else
2571
- Ie(e, p, j[v]), p += D[v];
2648
+ $e(e, m, N[x]), m += P[x];
2572
2649
  }
2573
- return Ie(e, p, j[256]), p + D[256];
2574
- }, ur = /* @__PURE__ */ new St([65540, 131080, 131088, 131104, 262176, 1048704, 1048832, 2114560, 2117632]), hn = /* @__PURE__ */ new ae(0), fr = function(n, e, t, r, a, o) {
2575
- var i = o.z || n.length, f = new ae(r + i + 5 * (1 + Math.ceil(i / 7e3)) + a), m = f.subarray(r, f.length - a), g = o.l, p = (o.r || 0) & 7;
2650
+ return $e(e, m, N[256]), m + P[256];
2651
+ }, dr = /* @__PURE__ */ new Lt([65540, 131080, 131088, 131104, 262176, 1048704, 1048832, 2114560, 2117632]), fn = /* @__PURE__ */ new ae(0), gr = function(n, e, t, r, a, s) {
2652
+ var l = s.z || n.length, f = new ae(r + l + 5 * (1 + Math.ceil(l / 7e3)) + a), y = f.subarray(r, f.length - a), d = s.l, m = (s.r || 0) & 7;
2576
2653
  if (e) {
2577
- p && (m[0] = o.r >> 3);
2578
- for (var l = ur[e - 1], h = l >> 13, S = l & 8191, y = (1 << t) - 1, u = o.p || new ue(32768), b = o.h || new ue(y + 1), x = Math.ceil(t / 3), s = 2 * x, c = function(L) {
2579
- return (n[L] ^ n[L + 1] << x ^ n[L + 2] << s) & y;
2580
- }, T = new St(25e3), d = new ue(288), w = new ue(32), B = 0, Y = 0, I = o.i || 0, _ = 0, U = o.w || 0, O = 0; I + 2 < i; ++I) {
2581
- var C = c(I), $ = I & 32767, V = b[C];
2582
- if (u[$] = V, b[C] = $, U <= I) {
2583
- var j = i - I;
2584
- if ((B > 7e3 || _ > 24576) && (j > 423 || !g)) {
2585
- p = Gt(n, m, 0, T, d, w, Y, _, O, I - O, p), _ = B = Y = 0, O = I;
2586
- for (var D = 0; D < 286; ++D)
2587
- d[D] = 0;
2588
- for (var D = 0; D < 30; ++D)
2589
- w[D] = 0;
2654
+ m && (y[0] = s.r >> 3);
2655
+ for (var c = dr[e - 1], p = c >> 13, b = c & 8191, v = (1 << t) - 1, h = s.p || new fe(32768), u = s.h || new fe(v + 1), w = Math.ceil(t / 3), o = 2 * w, i = function(E) {
2656
+ return (n[E] ^ n[E + 1] << w ^ n[E + 2] << o) & v;
2657
+ }, S = new Lt(25e3), g = new fe(288), A = new fe(32), L = 0, B = 0, j = s.i || 0, k = 0, U = s.w || 0, O = 0; j + 2 < l; ++j) {
2658
+ var $ = i(j), D = j & 32767, q = u[$];
2659
+ if (h[D] = q, u[$] = D, U <= j) {
2660
+ var N = l - j;
2661
+ if ((L > 7e3 || k > 24576) && (N > 423 || !d)) {
2662
+ m = qt(n, y, 0, S, g, A, B, k, O, j - O, m), k = L = B = 0, O = j;
2663
+ for (var P = 0; P < 286; ++P)
2664
+ g[P] = 0;
2665
+ for (var P = 0; P < 30; ++P)
2666
+ A[P] = 0;
2590
2667
  }
2591
- var H = 2, R = 0, Z = S, P = $ - V & 32767;
2592
- if (j > 2 && C == c(I - P))
2593
- for (var G = Math.min(h, j) - 1, J = Math.min(32767, I), q = Math.min(258, j); P <= J && --Z && $ != V; ) {
2594
- if (n[I + H] == n[I + H - P]) {
2595
- for (var v = 0; v < q && n[I + v] == n[I + v - P]; ++v)
2668
+ var H = 2, G = 0, z = b, W = D - q & 32767;
2669
+ if (N > 2 && $ == i(j - W))
2670
+ for (var I = Math.min(p, N) - 1, V = Math.min(32767, j), K = Math.min(258, N); W <= V && --z && D != q; ) {
2671
+ if (n[j + H] == n[j + H - W]) {
2672
+ for (var x = 0; x < K && n[j + x] == n[j + x - W]; ++x)
2596
2673
  ;
2597
- if (v > H) {
2598
- if (H = v, R = P, v > G)
2674
+ if (x > H) {
2675
+ if (H = x, G = W, x > I)
2599
2676
  break;
2600
- for (var A = Math.min(P, v - 2), E = 0, D = 0; D < A; ++D) {
2601
- var k = I - P + D & 32767, N = u[k], z = k - N & 32767;
2602
- z > E && (E = z, V = k);
2677
+ for (var M = Math.min(W, x - 2), T = 0, P = 0; P < M; ++P) {
2678
+ var _ = j - W + P & 32767, Y = h[_], X = _ - Y & 32767;
2679
+ X > T && (T = X, q = _);
2603
2680
  }
2604
2681
  }
2605
2682
  }
2606
- $ = V, V = u[$], P += $ - V & 32767;
2683
+ D = q, q = h[D], W += D - q & 32767;
2607
2684
  }
2608
- if (R) {
2609
- T[_++] = 268435456 | ft[H] << 18 | Zt[R];
2610
- var F = ft[H] & 31, X = Zt[R] & 31;
2611
- Y += Je[F] + Ke[X], ++d[257 + F], ++w[X], U = I + H, ++B;
2685
+ if (G) {
2686
+ S[k++] = 268435456 | gt[H] << 18 | Gt[G];
2687
+ var F = gt[H] & 31, C = Gt[G] & 31;
2688
+ B += et[F] + tt[C], ++g[257 + F], ++A[C], U = j + H, ++L;
2612
2689
  } else
2613
- T[_++] = n[I], ++d[n[I]];
2690
+ S[k++] = n[j], ++g[n[j]];
2614
2691
  }
2615
2692
  }
2616
- for (I = Math.max(I, U); I < i; ++I)
2617
- T[_++] = n[I], ++d[n[I]];
2618
- p = Gt(n, m, g, T, d, w, Y, _, O, I - O, p), g || (o.r = p & 7 | m[p / 8 | 0] << 3, p -= 7, o.h = b, o.p = u, o.i = I, o.w = U);
2693
+ for (j = Math.max(j, U); j < l; ++j)
2694
+ S[k++] = n[j], ++g[n[j]];
2695
+ m = qt(n, y, d, S, g, A, B, k, O, j - O, m), d || (s.r = m & 7 | y[m / 8 | 0] << 3, m -= 7, s.h = u, s.p = h, s.i = j, s.w = U);
2619
2696
  } else {
2620
- for (var I = o.w || 0; I < i + g; I += 65535) {
2621
- var M = I + 65535;
2622
- M >= i && (m[p / 8 | 0] = g, M = i), p = cn(m, p + 1, n.subarray(I, M));
2697
+ for (var j = s.w || 0; j < l + d; j += 65535) {
2698
+ var Z = j + 65535;
2699
+ Z >= l && (y[m / 8 | 0] = d, Z = l), m = un(y, m + 1, n.subarray(j, Z));
2623
2700
  }
2624
- o.i = i;
2701
+ s.i = l;
2625
2702
  }
2626
- return Pe(f, 0, r + Tt(p) + a);
2627
- }, mr = /* @__PURE__ */ (function() {
2703
+ return He(f, 0, r + _t(m) + a);
2704
+ }, yr = /* @__PURE__ */ (function() {
2628
2705
  for (var n = new Int32Array(256), e = 0; e < 256; ++e) {
2629
2706
  for (var t = e, r = 9; --r; )
2630
2707
  t = (t & 1 && -306674912) ^ t >>> 1;
2631
2708
  n[e] = t;
2632
2709
  }
2633
2710
  return n;
2634
- })(), pr = function() {
2711
+ })(), vr = function() {
2635
2712
  var n = -1;
2636
2713
  return {
2637
2714
  p: function(e) {
2638
2715
  for (var t = n, r = 0; r < e.length; ++r)
2639
- t = mr[t & 255 ^ e[r]] ^ t >>> 8;
2716
+ t = yr[t & 255 ^ e[r]] ^ t >>> 8;
2640
2717
  n = t;
2641
2718
  },
2642
2719
  d: function() {
2643
2720
  return ~n;
2644
2721
  }
2645
2722
  };
2646
- }, dr = function(n, e, t, r, a) {
2723
+ }, br = function(n, e, t, r, a) {
2647
2724
  if (!a && (a = { l: 1 }, e.dictionary)) {
2648
- var o = e.dictionary.subarray(-32768), i = new ae(o.length + n.length);
2649
- i.set(o), i.set(n, o.length), n = i, a.w = o.length;
2725
+ var s = e.dictionary.subarray(-32768), l = new ae(s.length + n.length);
2726
+ l.set(s), l.set(n, s.length), n = l, a.w = s.length;
2650
2727
  }
2651
- return fr(n, e.level == null ? 6 : e.level, e.mem == null ? a.l ? Math.ceil(Math.max(8, Math.min(13, Math.log(n.length))) * 1.5) : 20 : 12 + e.mem, t, r, a);
2652
- }, un = function(n, e) {
2728
+ return gr(n, e.level == null ? 6 : e.level, e.mem == null ? a.l ? Math.ceil(Math.max(8, Math.min(13, Math.log(n.length))) * 1.5) : 20 : 12 + e.mem, t, r, a);
2729
+ }, mn = function(n, e) {
2653
2730
  var t = {};
2654
2731
  for (var r in n)
2655
2732
  t[r] = n[r];
2656
2733
  for (var r in e)
2657
2734
  t[r] = e[r];
2658
2735
  return t;
2659
- }, ve = function(n, e) {
2736
+ }, be = function(n, e) {
2660
2737
  return n[e] | n[e + 1] << 8;
2661
- }, fe = function(n, e) {
2738
+ }, me = function(n, e) {
2662
2739
  return (n[e] | n[e + 1] << 8 | n[e + 2] << 16 | n[e + 3] << 24) >>> 0;
2663
- }, ct = function(n, e) {
2664
- return fe(n, e) + fe(n, e + 4) * 4294967296;
2740
+ }, mt = function(n, e) {
2741
+ return me(n, e) + me(n, e + 4) * 4294967296;
2665
2742
  }, ie = function(n, e, t) {
2666
2743
  for (; t; ++e)
2667
2744
  n[e] = t, t >>>= 8;
2668
2745
  };
2669
- function gr(n, e) {
2670
- return dr(n, e || {}, 0, 0);
2746
+ function xr(n, e) {
2747
+ return br(n, e || {}, 0, 0);
2671
2748
  }
2672
- function yr(n, e) {
2673
- return hr(n, { i: 2 }, e && e.out, e && e.dictionary);
2749
+ function wr(n, e) {
2750
+ return pr(n, { i: 2 }, e && e.out, e && e.dictionary);
2674
2751
  }
2675
- var fn = function(n, e, t, r) {
2752
+ var pn = function(n, e, t, r) {
2676
2753
  for (var a in n) {
2677
- var o = n[a], i = e + a, f = r;
2678
- Array.isArray(o) && (f = un(r, o[1]), o = o[0]), ArrayBuffer.isView(o) ? t[i] = [o, f] : (t[i += "/"] = [new ae(0), f], fn(o, i, t, r));
2754
+ var s = n[a], l = e + a, f = r;
2755
+ Array.isArray(s) && (f = mn(r, s[1]), s = s[0]), ArrayBuffer.isView(s) ? t[l] = [s, f] : (t[l += "/"] = [new ae(0), f], pn(s, l, t, r));
2679
2756
  }
2680
- }, Vt = typeof TextEncoder < "u" && /* @__PURE__ */ new TextEncoder(), dt = typeof TextDecoder < "u" && /* @__PURE__ */ new TextDecoder(), vr = 0;
2757
+ }, Jt = typeof TextEncoder < "u" && /* @__PURE__ */ new TextEncoder(), bt = typeof TextDecoder < "u" && /* @__PURE__ */ new TextDecoder(), Ar = 0;
2681
2758
  try {
2682
- dt.decode(hn, { stream: !0 }), vr = 1;
2759
+ bt.decode(fn, { stream: !0 }), Ar = 1;
2683
2760
  } catch {
2684
2761
  }
2685
- var br = function(n) {
2762
+ var Mr = function(n) {
2686
2763
  for (var e = "", t = 0; ; ) {
2687
2764
  var r = n[t++], a = (r > 127) + (r > 223) + (r > 239);
2688
2765
  if (t + a > n.length)
2689
- return { s: e, r: Pe(n, t - 1) };
2766
+ return { s: e, r: He(n, t - 1) };
2690
2767
  a ? a == 3 ? (r = ((r & 15) << 18 | (n[t++] & 63) << 12 | (n[t++] & 63) << 6 | n[t++] & 63) - 65536, e += String.fromCharCode(55296 | r >> 10, 56320 | r & 1023)) : a & 1 ? e += String.fromCharCode((r & 31) << 6 | n[t++] & 63) : e += String.fromCharCode((r & 15) << 12 | (n[t++] & 63) << 6 | n[t++] & 63) : e += String.fromCharCode(r);
2691
2768
  }
2692
2769
  };
2693
- function qt(n, e) {
2770
+ function Kt(n, e) {
2694
2771
  var t;
2695
- if (Vt)
2696
- return Vt.encode(n);
2697
- for (var r = n.length, a = new ae(n.length + (n.length >> 1)), o = 0, i = function(g) {
2698
- a[o++] = g;
2772
+ if (Jt)
2773
+ return Jt.encode(n);
2774
+ for (var r = n.length, a = new ae(n.length + (n.length >> 1)), s = 0, l = function(d) {
2775
+ a[s++] = d;
2699
2776
  }, t = 0; t < r; ++t) {
2700
- if (o + 5 > a.length) {
2701
- var f = new ae(o + 8 + (r - t << 1));
2777
+ if (s + 5 > a.length) {
2778
+ var f = new ae(s + 8 + (r - t << 1));
2702
2779
  f.set(a), a = f;
2703
2780
  }
2704
- var m = n.charCodeAt(t);
2705
- m < 128 || e ? i(m) : m < 2048 ? (i(192 | m >> 6), i(128 | m & 63)) : m > 55295 && m < 57344 ? (m = 65536 + (m & 1047552) | n.charCodeAt(++t) & 1023, i(240 | m >> 18), i(128 | m >> 12 & 63), i(128 | m >> 6 & 63), i(128 | m & 63)) : (i(224 | m >> 12), i(128 | m >> 6 & 63), i(128 | m & 63));
2781
+ var y = n.charCodeAt(t);
2782
+ y < 128 || e ? l(y) : y < 2048 ? (l(192 | y >> 6), l(128 | y & 63)) : y > 55295 && y < 57344 ? (y = 65536 + (y & 1047552) | n.charCodeAt(++t) & 1023, l(240 | y >> 18), l(128 | y >> 12 & 63), l(128 | y >> 6 & 63), l(128 | y & 63)) : (l(224 | y >> 12), l(128 | y >> 6 & 63), l(128 | y & 63));
2706
2783
  }
2707
- return Pe(a, 0, o);
2784
+ return He(a, 0, s);
2708
2785
  }
2709
- function xr(n, e) {
2786
+ function Er(n, e) {
2710
2787
  if (e) {
2711
2788
  for (var t = "", r = 0; r < n.length; r += 16384)
2712
2789
  t += String.fromCharCode.apply(null, n.subarray(r, r + 16384));
2713
2790
  return t;
2714
2791
  } else {
2715
- if (dt)
2716
- return dt.decode(n);
2717
- var a = br(n), o = a.s, t = a.r;
2718
- return t.length && le(8), o;
2792
+ if (bt)
2793
+ return bt.decode(n);
2794
+ var a = Mr(n), s = a.s, t = a.r;
2795
+ return t.length && he(8), s;
2719
2796
  }
2720
2797
  }
2721
- var wr = function(n, e) {
2722
- return e + 30 + ve(n, e + 26) + ve(n, e + 28);
2723
- }, Ar = function(n, e, t) {
2724
- var r = ve(n, e + 28), a = ve(n, e + 30), o = xr(n.subarray(e + 46, e + 46 + r), !(ve(n, e + 8) & 2048)), i = e + 46 + r, f = Mr(n, i, a, t, fe(n, e + 20), fe(n, e + 24), fe(n, e + 42)), m = f[0], g = f[1], p = f[2];
2725
- return [ve(n, e + 10), m, g, o, i + a + ve(n, e + 32), p];
2726
- }, Mr = function(n, e, t, r, a, o, i) {
2727
- var f = a == 4294967295, m = o == 4294967295, g = i == 4294967295, p = e + t, l = f + m + g;
2728
- if (r && l) {
2729
- for (; e + 4 < p; e += 4 + ve(n, e + 2))
2730
- if (ve(n, e) == 1)
2798
+ var Sr = function(n, e) {
2799
+ return e + 30 + be(n, e + 26) + be(n, e + 28);
2800
+ }, Tr = function(n, e, t) {
2801
+ var r = be(n, e + 28), a = be(n, e + 30), s = Er(n.subarray(e + 46, e + 46 + r), !(be(n, e + 8) & 2048)), l = e + 46 + r, f = Or(n, l, a, t, me(n, e + 20), me(n, e + 24), me(n, e + 42)), y = f[0], d = f[1], m = f[2];
2802
+ return [be(n, e + 10), y, d, s, l + a + be(n, e + 32), m];
2803
+ }, Or = function(n, e, t, r, a, s, l) {
2804
+ var f = a == 4294967295, y = s == 4294967295, d = l == 4294967295, m = e + t, c = f + y + d;
2805
+ if (r && c) {
2806
+ for (; e + 4 < m; e += 4 + be(n, e + 2))
2807
+ if (be(n, e) == 1)
2731
2808
  return [
2732
- f ? ct(n, e + 4 + 8 * m) : a,
2733
- m ? ct(n, e + 4) : o,
2734
- g ? ct(n, e + 4 + 8 * (m + f)) : i,
2809
+ f ? mt(n, e + 4 + 8 * y) : a,
2810
+ y ? mt(n, e + 4) : s,
2811
+ d ? mt(n, e + 4 + 8 * (y + f)) : l,
2735
2812
  1
2736
2813
  ];
2737
- r < 2 && le(13);
2814
+ r < 2 && he(13);
2738
2815
  }
2739
- return [a, o, i, 0];
2740
- }, gt = function(n) {
2816
+ return [a, s, l, 0];
2817
+ }, xt = function(n) {
2741
2818
  var e = 0;
2742
2819
  if (n)
2743
2820
  for (var t in n) {
2744
2821
  var r = n[t].length;
2745
- r > 65535 && le(9), e += r + 4;
2822
+ r > 65535 && he(9), e += r + 4;
2746
2823
  }
2747
2824
  return e;
2748
- }, Jt = function(n, e, t, r, a, o, i, f) {
2749
- var m = r.length, g = t.extra, p = f && f.length, l = gt(g);
2750
- ie(n, e, i != null ? 33639248 : 67324752), e += 4, i != null && (n[e++] = 20, n[e++] = t.os), n[e] = 20, e += 2, n[e++] = t.flag << 1 | (o < 0 && 8), n[e++] = a && 8, n[e++] = t.compression & 255, n[e++] = t.compression >> 8;
2751
- var h = new Date(t.mtime == null ? Date.now() : t.mtime), S = h.getFullYear() - 1980;
2752
- if ((S < 0 || S > 119) && le(10), ie(n, e, S << 25 | h.getMonth() + 1 << 21 | h.getDate() << 16 | h.getHours() << 11 | h.getMinutes() << 5 | h.getSeconds() >> 1), e += 4, o != -1 && (ie(n, e, t.crc), ie(n, e + 4, o < 0 ? -o - 2 : o), ie(n, e + 8, t.size)), ie(n, e + 12, m), ie(n, e + 14, l), e += 16, i != null && (ie(n, e, p), ie(n, e + 6, t.attrs), ie(n, e + 10, i), e += 14), n.set(r, e), e += m, l)
2753
- for (var y in g) {
2754
- var u = g[y], b = u.length;
2755
- ie(n, e, +y), ie(n, e + 2, b), n.set(u, e + 4), e += 4 + b;
2756
- }
2757
- return p && (n.set(f, e), e += p), e;
2758
- }, Er = function(n, e, t, r, a) {
2825
+ }, Qt = function(n, e, t, r, a, s, l, f) {
2826
+ var y = r.length, d = t.extra, m = f && f.length, c = xt(d);
2827
+ ie(n, e, l != null ? 33639248 : 67324752), e += 4, l != null && (n[e++] = 20, n[e++] = t.os), n[e] = 20, e += 2, n[e++] = t.flag << 1 | (s < 0 && 8), n[e++] = a && 8, n[e++] = t.compression & 255, n[e++] = t.compression >> 8;
2828
+ var p = new Date(t.mtime == null ? Date.now() : t.mtime), b = p.getFullYear() - 1980;
2829
+ if ((b < 0 || b > 119) && he(10), ie(n, e, b << 25 | p.getMonth() + 1 << 21 | p.getDate() << 16 | p.getHours() << 11 | p.getMinutes() << 5 | p.getSeconds() >> 1), e += 4, s != -1 && (ie(n, e, t.crc), ie(n, e + 4, s < 0 ? -s - 2 : s), ie(n, e + 8, t.size)), ie(n, e + 12, y), ie(n, e + 14, c), e += 16, l != null && (ie(n, e, m), ie(n, e + 6, t.attrs), ie(n, e + 10, l), e += 14), n.set(r, e), e += y, c)
2830
+ for (var v in d) {
2831
+ var h = d[v], u = h.length;
2832
+ ie(n, e, +v), ie(n, e + 2, u), n.set(h, e + 4), e += 4 + u;
2833
+ }
2834
+ return m && (n.set(f, e), e += m), e;
2835
+ }, Rr = function(n, e, t, r, a) {
2759
2836
  ie(n, e, 101010256), ie(n, e + 8, t), ie(n, e + 10, t), ie(n, e + 12, r), ie(n, e + 16, a);
2760
2837
  };
2761
- function Sr(n, e) {
2838
+ function Lr(n, e) {
2762
2839
  e || (e = {});
2763
2840
  var t = {}, r = [];
2764
- fn(n, "", t, e);
2765
- var a = 0, o = 0;
2766
- for (var i in t) {
2767
- var f = t[i], m = f[0], g = f[1], p = g.level == 0 ? 0 : 8, l = qt(i), h = l.length, S = g.comment, y = S && qt(S), u = y && y.length, b = gt(g.extra);
2768
- h > 65535 && le(11);
2769
- var x = p ? gr(m, g) : m, s = x.length, c = pr();
2770
- c.p(m), r.push(un(g, {
2771
- size: m.length,
2772
- crc: c.d(),
2773
- c: x,
2774
- f: l,
2775
- m: y,
2776
- u: h != i.length || y && S.length != u,
2841
+ pn(n, "", t, e);
2842
+ var a = 0, s = 0;
2843
+ for (var l in t) {
2844
+ var f = t[l], y = f[0], d = f[1], m = d.level == 0 ? 0 : 8, c = Kt(l), p = c.length, b = d.comment, v = b && Kt(b), h = v && v.length, u = xt(d.extra);
2845
+ p > 65535 && he(11);
2846
+ var w = m ? xr(y, d) : y, o = w.length, i = vr();
2847
+ i.p(y), r.push(mn(d, {
2848
+ size: y.length,
2849
+ crc: i.d(),
2850
+ c: w,
2851
+ f: c,
2852
+ m: v,
2853
+ u: p != l.length || v && b.length != h,
2777
2854
  o: a,
2778
- compression: p
2779
- })), a += 30 + h + b + s, o += 76 + 2 * (h + b) + (u || 0) + s;
2855
+ compression: m
2856
+ })), a += 30 + p + u + o, s += 76 + 2 * (p + u) + (h || 0) + o;
2780
2857
  }
2781
- for (var T = new ae(o + 22), d = a, w = o - a, B = 0; B < r.length; ++B) {
2782
- var l = r[B];
2783
- Jt(T, l.o, l, l.f, l.u, l.c.length);
2784
- var Y = 30 + l.f.length + gt(l.extra);
2785
- T.set(l.c, l.o + Y), Jt(T, a, l, l.f, l.u, l.c.length, l.o, l.m), a += 16 + Y + (l.m ? l.m.length : 0);
2858
+ for (var S = new ae(s + 22), g = a, A = s - a, L = 0; L < r.length; ++L) {
2859
+ var c = r[L];
2860
+ Qt(S, c.o, c, c.f, c.u, c.c.length);
2861
+ var B = 30 + c.f.length + xt(c.extra);
2862
+ S.set(c.c, c.o + B), Qt(S, a, c, c.f, c.u, c.c.length, c.o, c.m), a += 16 + B + (c.m ? c.m.length : 0);
2786
2863
  }
2787
- return Er(T, a, r.length, w, d), T;
2864
+ return Rr(S, a, r.length, A, g), S;
2788
2865
  }
2789
- function Tr(n, e) {
2790
- for (var t = {}, r = n.length - 22; fe(n, r) != 101010256; --r)
2791
- (!r || n.length - r > 65558) && le(13);
2792
- var a = ve(n, r + 8);
2866
+ function _r(n, e) {
2867
+ for (var t = {}, r = n.length - 22; me(n, r) != 101010256; --r)
2868
+ (!r || n.length - r > 65558) && he(13);
2869
+ var a = be(n, r + 8);
2793
2870
  if (!a)
2794
2871
  return {};
2795
- var o = fe(n, r + 16), i = fe(n, r - 20) == 117853008;
2796
- if (i) {
2797
- var f = fe(n, r - 12);
2798
- i = fe(n, f) == 101075792, i && (a = fe(n, f + 32), o = fe(n, f + 48));
2799
- }
2800
- for (var m = e && e.filter, g = 0; g < a; ++g) {
2801
- var p = Ar(n, o, i), l = p[0], h = p[1], S = p[2], y = p[3], u = p[4], b = p[5], x = wr(n, b);
2802
- o = u, (!m || m({
2803
- name: y,
2804
- size: h,
2805
- originalSize: S,
2806
- compression: l
2807
- })) && (l ? l == 8 ? t[y] = yr(n.subarray(x, x + h), { out: new ae(S) }) : le(14, "unknown compression type " + l) : t[y] = Pe(n, x, x + h));
2872
+ var s = me(n, r + 16), l = me(n, r - 20) == 117853008;
2873
+ if (l) {
2874
+ var f = me(n, r - 12);
2875
+ l = me(n, f) == 101075792, l && (a = me(n, f + 32), s = me(n, f + 48));
2876
+ }
2877
+ for (var y = e && e.filter, d = 0; d < a; ++d) {
2878
+ var m = Tr(n, s, l), c = m[0], p = m[1], b = m[2], v = m[3], h = m[4], u = m[5], w = Sr(n, u);
2879
+ s = h, (!y || y({
2880
+ name: v,
2881
+ size: p,
2882
+ originalSize: b,
2883
+ compression: c
2884
+ })) && (c ? c == 8 ? t[v] = wr(n.subarray(w, w + p), { out: new ae(b) }) : he(14, "unknown compression type " + c) : t[v] = He(n, w, w + p));
2808
2885
  }
2809
2886
  return t;
2810
2887
  }
2811
- const qe = 200, yt = 64 * 1024 * 1024, vt = new TextDecoder("utf-8", { fatal: !0 });
2812
- function Ot(n) {
2888
+ const Qe = 200, wt = 64 * 1024 * 1024, At = new TextDecoder("utf-8", { fatal: !0 });
2889
+ function kt(n) {
2813
2890
  if (!n || n.startsWith("/") || /^[A-Za-z]:/.test(n) || n.includes("\\") || n.includes("\0") || n.split("/").includes(".."))
2814
2891
  throw new Error(`ZIP entry ${JSON.stringify(n)}: path must be relative, without .. or backslashes`);
2815
2892
  }
2816
- function bt(n) {
2817
- const e = new DataView(n.buffer, n.byteOffset, n.byteLength), t = (h = "(archive)") => {
2818
- throw new Error(`ZIP entry ${JSON.stringify(h)}: invalid or truncated ZIP`);
2819
- }, r = (h) => h >= 0 && h + 2 <= n.length ? e.getUint16(h, !0) : t(), a = (h) => h >= 0 && h + 4 <= n.length ? e.getUint32(h, !0) : t();
2820
- let o = n.length - 22;
2821
- for (; o >= Math.max(0, n.length - 65557) && !(a(o) === 101010256 && o + 22 + r(o + 20) === n.length); o--)
2893
+ function Mt(n) {
2894
+ const e = new DataView(n.buffer, n.byteOffset, n.byteLength), t = (p = "(archive)") => {
2895
+ throw new Error(`ZIP entry ${JSON.stringify(p)}: invalid or truncated ZIP`);
2896
+ }, r = (p) => p >= 0 && p + 2 <= n.length ? e.getUint16(p, !0) : t(), a = (p) => p >= 0 && p + 4 <= n.length ? e.getUint32(p, !0) : t();
2897
+ let s = n.length - 22;
2898
+ for (; s >= Math.max(0, n.length - 65557) && !(a(s) === 101010256 && s + 22 + r(s + 20) === n.length); s--)
2822
2899
  ;
2823
- (o < 0 || o < n.length - 65557) && t(), (r(o + 4) || r(o + 6) || r(o + 8) !== r(o + 10)) && t();
2824
- const i = r(o + 10), f = a(o + 16), m = f + a(o + 12);
2825
- m !== o && t();
2826
- const g = [], p = /* @__PURE__ */ new Set();
2827
- let l = f;
2828
- for (let h = 0; h < i; h++) {
2829
- (l + 46 > m || a(l) !== 33639248) && t();
2830
- const S = r(l + 28), y = l + 46 + S + r(l + 30) + r(l + 32);
2831
- y > m && t();
2832
- let u;
2900
+ (s < 0 || s < n.length - 65557) && t(), (r(s + 4) || r(s + 6) || r(s + 8) !== r(s + 10)) && t();
2901
+ const l = r(s + 10), f = a(s + 16), y = f + a(s + 12);
2902
+ y !== s && t();
2903
+ const d = [], m = /* @__PURE__ */ new Set();
2904
+ let c = f;
2905
+ for (let p = 0; p < l; p++) {
2906
+ (c + 46 > y || a(c) !== 33639248) && t();
2907
+ const b = r(c + 28), v = c + 46 + b + r(c + 30) + r(c + 32);
2908
+ v > y && t();
2909
+ let h;
2833
2910
  try {
2834
- u = vt.decode(n.subarray(l + 46, l + 46 + S));
2911
+ h = At.decode(n.subarray(c + 46, c + 46 + b));
2835
2912
  } catch {
2836
2913
  throw new Error("ZIP entry (invalid UTF-8 name): invalid filename");
2837
2914
  }
2838
- if (Ot(u), h >= qe) throw new Error(`ZIP entry ${JSON.stringify(u)}: more than ${qe} entries`);
2839
- if (p.has(u)) throw new Error(`ZIP entry ${JSON.stringify(u)}: duplicate entry`);
2840
- p.add(u);
2841
- const b = a(l + 24), x = a(l + 20), s = a(l + 42);
2842
- if (b > yt || x > yt) throw new Error(`ZIP entry ${JSON.stringify(u)}: exceeds 64 MB`);
2843
- if (r(l + 10) !== 0) throw new Error(`ZIP entry ${JSON.stringify(u)}: expected stored compression (method 0)`);
2844
- if (r(l + 8) & 1) throw new Error(`ZIP entry ${JSON.stringify(u)}: encrypted entries are not supported`);
2845
- (x !== b || s + 30 > f || a(s) !== 67324752) && t(u);
2846
- const c = s + 30 + r(s + 26) + r(s + 28);
2847
- (c + b > f || r(s + 8) !== 0 || r(s + 6) !== r(l + 8)) && t(u);
2848
- let T;
2915
+ if (kt(h), p >= Qe) throw new Error(`ZIP entry ${JSON.stringify(h)}: more than ${Qe} entries`);
2916
+ if (m.has(h)) throw new Error(`ZIP entry ${JSON.stringify(h)}: duplicate entry`);
2917
+ m.add(h);
2918
+ const u = a(c + 24), w = a(c + 20), o = a(c + 42);
2919
+ if (u > wt || w > wt) throw new Error(`ZIP entry ${JSON.stringify(h)}: exceeds 64 MB`);
2920
+ if (r(c + 10) !== 0) throw new Error(`ZIP entry ${JSON.stringify(h)}: expected stored compression (method 0)`);
2921
+ if (r(c + 8) & 1) throw new Error(`ZIP entry ${JSON.stringify(h)}: encrypted entries are not supported`);
2922
+ (w !== u || o + 30 > f || a(o) !== 67324752) && t(h);
2923
+ const i = o + 30 + r(o + 26) + r(o + 28);
2924
+ (i + u > f || r(o + 8) !== 0 || r(o + 6) !== r(c + 8)) && t(h);
2925
+ let S;
2849
2926
  try {
2850
- T = vt.decode(n.subarray(s + 30, s + 30 + r(s + 26)));
2927
+ S = At.decode(n.subarray(o + 30, o + 30 + r(o + 26)));
2851
2928
  } catch {
2852
- t(u);
2929
+ t(h);
2853
2930
  }
2854
- T !== u && t(u), g.push({ name: u, offset: s, start: c, size: b, central: l }), l = y;
2931
+ S !== h && t(h), d.push({ name: h, offset: o, start: i, size: u, central: c }), c = v;
2855
2932
  }
2856
- return l !== m && t(), { files: g };
2933
+ return c !== y && t(), { files: d };
2857
2934
  }
2858
- function Or(n) {
2859
- const e = n instanceof Uint8Array ? n : new Uint8Array(n), t = bt(e), r = Tr(e, { filter: (o) => o.name !== "__proto__" }), a = /* @__PURE__ */ Object.create(null);
2860
- for (const o of t.files) {
2861
- const i = o.name === "__proto__" ? e.slice(o.start, o.start + o.size) : r[o.name];
2862
- if (!(i instanceof Uint8Array) || i.length !== o.size) throw new Error(`ZIP entry ${JSON.stringify(o.name)}: invalid size`);
2863
- a[o.name] = i;
2935
+ function kr(n) {
2936
+ const e = n instanceof Uint8Array ? n : new Uint8Array(n), t = Mt(e), r = _r(e, { filter: (s) => s.name !== "__proto__" }), a = /* @__PURE__ */ Object.create(null);
2937
+ for (const s of t.files) {
2938
+ const l = s.name === "__proto__" ? e.slice(s.start, s.start + s.size) : r[s.name];
2939
+ if (!(l instanceof Uint8Array) || l.length !== s.size) throw new Error(`ZIP entry ${JSON.stringify(s.name)}: invalid size`);
2940
+ a[s.name] = l;
2864
2941
  }
2865
2942
  return a;
2866
2943
  }
2867
- function Fr(n, e = {}) {
2868
- const t = Object.hasOwn(n, "avatar.json") ? n["avatar.json"] : void 0, r = t ? ke(JSON.parse(vt.decode(t))) : nn("Avatar"), a = t && !e.manifest ? t : new TextEncoder().encode(JSON.stringify(ke({ ...r, ...e.manifest }), null, 2) + `
2869
- `), o = /* @__PURE__ */ Object.create(null);
2870
- o["avatar.json"] = a;
2871
- for (const [h, S] of Object.entries(n)) h !== "avatar.json" && (o[h] = S);
2872
- for (const [h, [S, y]] of Object.entries(o).entries()) {
2873
- if (Ot(S), h >= qe) throw new Error(`ZIP entry ${JSON.stringify(S)}: more than ${qe} entries`);
2874
- if (y.byteLength > yt) throw new Error(`ZIP entry ${JSON.stringify(S)}: exceeds 64 MB`);
2875
- }
2876
- const i = rn(o);
2877
- if (i.length) throw new Error(i.join(`
2944
+ function jr(n, e = {}) {
2945
+ const t = Object.hasOwn(n, "avatar.json") ? n["avatar.json"] : void 0, r = t ? Ye(JSON.parse(At.decode(t))) : an("Avatar"), a = t && !e.manifest ? t : new TextEncoder().encode(JSON.stringify(Ye({ ...r, ...e.manifest }), null, 2) + `
2946
+ `), s = /* @__PURE__ */ Object.create(null);
2947
+ s["avatar.json"] = a;
2948
+ for (const [p, b] of Object.entries(n)) p !== "avatar.json" && (s[p] = b);
2949
+ for (const [p, [b, v]] of Object.entries(s).entries()) {
2950
+ if (kt(b), p >= Qe) throw new Error(`ZIP entry ${JSON.stringify(b)}: more than ${Qe} entries`);
2951
+ if (v.byteLength > wt) throw new Error(`ZIP entry ${JSON.stringify(b)}: exceeds 64 MB`);
2952
+ }
2953
+ const l = on(s);
2954
+ if (l.length) throw new Error(l.join(`
2878
2955
  `));
2879
2956
  let f = "proto0000";
2880
- for (let h = 0; Object.hasOwn(o, f); h++) f = `proto${String(h + 1).padStart(4, "0")}`;
2881
- const m = Object.fromEntries(Object.entries(o).map(([h, S]) => [h === "__proto__" ? f : h, S])), g = Sr(m, { level: 0 });
2882
- if (Object.hasOwn(o, "__proto__")) {
2883
- const h = bt(g).files.find((y) => y.name === f), S = new TextEncoder().encode("__proto__");
2884
- g.set(S, h.offset + 30), g.set(S, h.central + 46);
2885
- }
2886
- const p = bt(g), l = p.files.find((h) => h.name === "avatar.json");
2887
- if (l.offset !== 0) {
2888
- const h = l.start + l.size - l.offset, S = new Uint8Array(g.length);
2889
- S.set(g.subarray(l.offset, l.offset + h)), S.set(g.subarray(0, l.offset), h), S.set(g.subarray(l.offset + h), l.offset + h);
2890
- const y = new DataView(S.buffer);
2891
- for (const u of p.files) y.setUint32(u.central + 42, u === l ? 0 : u.offset < l.offset ? u.offset + h : u.offset, !0);
2892
- return S;
2893
- }
2894
- return g;
2957
+ for (let p = 0; Object.hasOwn(s, f); p++) f = `proto${String(p + 1).padStart(4, "0")}`;
2958
+ const y = Object.fromEntries(Object.entries(s).map(([p, b]) => [p === "__proto__" ? f : p, b])), d = Lr(y, { level: 0 });
2959
+ if (Object.hasOwn(s, "__proto__")) {
2960
+ const p = Mt(d).files.find((v) => v.name === f), b = new TextEncoder().encode("__proto__");
2961
+ d.set(b, p.offset + 30), d.set(b, p.central + 46);
2962
+ }
2963
+ const m = Mt(d), c = m.files.find((p) => p.name === "avatar.json");
2964
+ if (c.offset !== 0) {
2965
+ const p = c.start + c.size - c.offset, b = new Uint8Array(d.length);
2966
+ b.set(d.subarray(c.offset, c.offset + p)), b.set(d.subarray(0, c.offset), p), b.set(d.subarray(c.offset + p), c.offset + p);
2967
+ const v = new DataView(b.buffer);
2968
+ for (const h of m.files) v.setUint32(h.central + 42, h === c ? 0 : h.offset < c.offset ? h.offset + p : h.offset, !0);
2969
+ return b;
2970
+ }
2971
+ return d;
2895
2972
  }
2896
- const Rr = (n) => JSON.parse(new TextDecoder("utf-8", { fatal: !0 }).decode(n));
2897
- function Xe(n, e) {
2973
+ const Fr = (n) => JSON.parse(new TextDecoder("utf-8", { fatal: !0 }).decode(n));
2974
+ function Be(n, e) {
2898
2975
  try {
2899
- return Rr(n);
2976
+ return Fr(n);
2900
2977
  } catch {
2901
2978
  throw new Error(`${e}: invalid UTF-8 JSON`);
2902
2979
  }
2903
2980
  }
2904
- function mn(n, e) {
2981
+ function dn(n, e, t) {
2905
2982
  return [
2906
2983
  "layers.json",
2907
2984
  "base.png",
2908
2985
  "hairmask.png",
2909
- ...Object.keys(n?.layers ?? {}).map((t) => `${t}.png`),
2910
- ...e ? ["sprites/sprites.json", ...Object.keys(e.layers ?? {}).map((t) => `sprites/${t}.png`)] : []
2986
+ ...Object.keys(n?.layers ?? {}).map((r) => `${r}.png`),
2987
+ ...e ? ["sprites/sprites.json", ...Object.keys(e.layers ?? {}).map((r) => `sprites/${r}.png`)] : [],
2988
+ // malformed entries are left to validateAvatarFiles, which reports them
2989
+ ...t ? ["overlays/overlays.json", ...(Array.isArray(t.overlays) ? t.overlays : []).flatMap((r) => typeof r?.file == "string" ? [`overlays/${r.file}`] : [])] : []
2911
2990
  ];
2912
2991
  }
2913
- function pn(n) {
2914
- const e = rn(n);
2992
+ function gn(n) {
2993
+ const e = on(n);
2915
2994
  if (e.length) throw new Error(e.join(`
2916
2995
  `));
2917
2996
  }
2918
- function dn(n) {
2997
+ function yn(n) {
2919
2998
  return !!n && typeof n == "object" && "manifest" in n && "rig" in n && "assets" in n && "release" in n;
2920
2999
  }
2921
- function ht(n) {
2922
- const e = Or(n);
3000
+ function pt(n) {
3001
+ const e = kr(n);
2923
3002
  if (!Object.hasOwn(e, "avatar.json")) throw new Error("avatar.json: missing file in .mavatar archive");
2924
- pn(e);
2925
- const t = ke(Xe(e["avatar.json"], "avatar.json")), r = xt(Xe(e["rig.json"], "rig.json")), a = mn(
2926
- Xe(e["built/layers.json"], "built/layers.json"),
2927
- e["built/sprites/sprites.json"] ? Xe(e["built/sprites/sprites.json"], "built/sprites/sprites.json") : void 0
2928
- ), o = {}, i = [], f = () => {
2929
- i.splice(0).forEach((m) => URL.revokeObjectURL(m));
3003
+ gn(e);
3004
+ const t = Ye(Be(e["avatar.json"], "avatar.json")), r = Et(Be(e["rig.json"], "rig.json")), a = e["built/overlays/overlays.json"] ? Be(e["built/overlays/overlays.json"], "built/overlays/overlays.json") : void 0, s = dn(
3005
+ Be(e["built/layers.json"], "built/layers.json"),
3006
+ e["built/sprites/sprites.json"] ? Be(e["built/sprites/sprites.json"], "built/sprites/sprites.json") : void 0,
3007
+ a
3008
+ ), l = {}, f = [], y = () => {
3009
+ f.splice(0).forEach((d) => URL.revokeObjectURL(d));
2930
3010
  };
2931
3011
  try {
2932
- for (const m of new Set(a)) {
2933
- const g = URL.createObjectURL(new Blob([new Uint8Array(e[`built/${m}`])], { type: m.endsWith(".json") ? "application/json" : "image/png" }));
2934
- i.push(g), o[m] = g;
3012
+ for (const d of new Set(s)) {
3013
+ const m = URL.createObjectURL(new Blob([new Uint8Array(e[`built/${d}`])], { type: d.endsWith(".json") ? "application/json" : "image/png" }));
3014
+ f.push(m), l[d] = m;
2935
3015
  }
2936
- } catch (m) {
2937
- throw f(), m;
3016
+ } catch (d) {
3017
+ throw y(), d;
2938
3018
  }
2939
- return { manifest: t, rig: r, assets: o, release: f };
3019
+ return { manifest: t, rig: r, assets: l, release: y };
2940
3020
  }
2941
- async function Lr(n, e = {}) {
2942
- if (dn(n)) return n;
2943
- if (n instanceof Blob) return ht(await n.arrayBuffer());
2944
- if (n instanceof ArrayBuffer || n instanceof Uint8Array) return ht(n);
3021
+ async function Br(n, e = {}) {
3022
+ if (yn(n)) return n;
3023
+ if (n instanceof Blob) return pt(await n.arrayBuffer());
3024
+ if (n instanceof ArrayBuffer || n instanceof Uint8Array) return pt(n);
2945
3025
  const t = new URL(n, typeof location > "u" ? void 0 : location.href);
2946
3026
  if (/\.mavatar$/i.test(t.pathname)) {
2947
3027
  const u = await fetch(t);
2948
3028
  if (!u.ok) throw new Error(`.mavatar: could not load (${u.status})`);
2949
- return ht(await u.arrayBuffer());
3029
+ return pt(await u.arrayBuffer());
2950
3030
  }
2951
3031
  t.pathname.endsWith("/") || (t.pathname += "/");
2952
3032
  const r = (u) => {
2953
- const b = new URL(t);
2954
- return b.pathname += u.split("/").map(encodeURIComponent).join("/"), e.version !== void 0 && b.searchParams.set("v", String(e.version)), b.href;
3033
+ const w = new URL(t);
3034
+ return w.pathname += u.split("/").map(encodeURIComponent).join("/"), e.version !== void 0 && w.searchParams.set("v", String(e.version)), w.href;
2955
3035
  }, a = {};
2956
- async function o(u, b = !1) {
2957
- const x = await fetch(r(u));
2958
- if (b && (x.status === 404 || x.ok && x.headers.get("content-type")?.includes("text/html"))) return;
2959
- if (!x.ok) throw new Error(`${u}: could not load (${x.status})`);
2960
- const s = new Uint8Array(await x.arrayBuffer());
2961
- return a[u] = s, Xe(s, u);
2962
- }
2963
- const i = e.rigFile ?? "rig.json";
2964
- Ot(i);
2965
- const [f, m, g, p] = await Promise.all([
2966
- o(i),
2967
- o("built/layers.json"),
2968
- o("avatar.json", !0),
2969
- o("built/sprites/sprites.json", !0)
3036
+ async function s(u, w = !1) {
3037
+ const o = await fetch(r(u));
3038
+ if (w && (o.status === 404 || o.ok && o.headers.get("content-type")?.includes("text/html"))) return;
3039
+ if (!o.ok) throw new Error(`${u}: could not load (${o.status})`);
3040
+ const i = new Uint8Array(await o.arrayBuffer());
3041
+ return a[u] = i, Be(i, u);
3042
+ }
3043
+ const l = e.rigFile ?? "rig.json";
3044
+ kt(l);
3045
+ const [f, y, d, m, c] = await Promise.all([
3046
+ s(l),
3047
+ s("built/layers.json"),
3048
+ s("avatar.json", !0),
3049
+ s("built/sprites/sprites.json", !0),
3050
+ s("built/overlays/overlays.json", !0)
2970
3051
  ]);
2971
- Array.isArray(f?.eyes) && Array.isArray(m?.eyes) && (f.eyes = f.eyes.map((u, b) => {
3052
+ Array.isArray(f?.eyes) && Array.isArray(y?.eyes) && (f.eyes = f.eyes.map((u, w) => {
2972
3053
  if (!u || typeof u != "object") return u;
2973
- const x = { ...u };
2974
- for (const s of ["x0", "x1", "top", "bot"]) x[s] === void 0 && (x[s] = m.eyes[b]?.[s]);
2975
- return x;
3054
+ const o = { ...u };
3055
+ for (const i of ["x0", "x1", "top", "bot"]) o[i] === void 0 && (o[i] = y.eyes[w]?.[i]);
3056
+ return o;
2976
3057
  })), a["rig.json"] = new TextEncoder().encode(JSON.stringify(f));
2977
- const l = mn(m, p);
2978
- for (const u of l) u.endsWith(".png") && (a[`built/${u}`] = { size: 1 });
2979
- pn(a);
2980
- const h = xt(f), S = Object.fromEntries(l.map((u) => [u, r(`built/${u}`)])), y = decodeURIComponent(t.pathname.split("/").filter(Boolean).at(-1) ?? "Avatar");
2981
- return { manifest: g === void 0 ? nn(y) : ke(g), rig: h, assets: S, release() {
3058
+ const p = dn(y, m, c);
3059
+ for (const u of p) u.endsWith(".png") && (a[`built/${u}`] = { size: 1 });
3060
+ gn(a);
3061
+ const b = Et(f), v = Object.fromEntries(p.map((u) => [u, r(`built/${u}`)])), h = decodeURIComponent(t.pathname.split("/").filter(Boolean).at(-1) ?? "Avatar");
3062
+ return { manifest: d === void 0 ? an(h) : Ye(d), rig: b, assets: v, release() {
2982
3063
  } };
2983
3064
  }
2984
- async function Br(n, e, t = {}) {
2985
- const r = !dn(e), a = await Lr(e);
3065
+ async function Ir(n, e, t = {}) {
3066
+ const r = !yn(e), a = await Br(e);
2986
3067
  try {
2987
- const o = t.lipSync === void 0 ? a.manifest.lipSync : { ...a.manifest.lipSync, ...wt(t.lipSync) }, i = await rr(n, { ...t, ...o === void 0 ? {} : { lipSync: o }, rig: a.rig, assets: a.assets }), f = i.destroy.bind(i);
2988
- let m = !1;
2989
- return Object.assign(i, { package: a, destroy() {
2990
- if (!m) {
2991
- m = !0;
3068
+ const s = t.lipSync === void 0 ? a.manifest.lipSync : { ...a.manifest.lipSync, ...St(t.lipSync) }, l = await ir(n, { ...t, ...s === void 0 ? {} : { lipSync: s }, rig: a.rig, assets: a.assets }), f = l.destroy.bind(l);
3069
+ let y = !1;
3070
+ return Object.assign(l, { package: a, destroy() {
3071
+ if (!y) {
3072
+ y = !0;
2992
3073
  try {
2993
3074
  f();
2994
3075
  } finally {
@@ -2996,23 +3077,23 @@ async function Br(n, e, t = {}) {
2996
3077
  }
2997
3078
  }
2998
3079
  } });
2999
- } catch (o) {
3000
- throw r && a.release(), o;
3080
+ } catch (s) {
3081
+ throw r && a.release(), s;
3001
3082
  }
3002
3083
  }
3003
3084
  export {
3004
- Le as DEFAULT_LIGHTING,
3005
- _r as LIVE_EXPRESSIONS,
3006
- Ze as PARAMS,
3007
- kr as colorHex,
3008
- rr as createMeshAvatar,
3009
- Br as loadMeshAvatar,
3010
- Lr as openAvatar,
3011
- Fr as packAvatar,
3012
- Tn as parseLighting,
3013
- ke as parseManifest,
3014
- xt as parseRig,
3015
- Or as unpackAvatar,
3016
- rn as validateAvatarFiles,
3017
- Kt as validateRig
3085
+ Ue as DEFAULT_LIGHTING,
3086
+ Yr as LIVE_EXPRESSIONS,
3087
+ Ge as PARAMS,
3088
+ Ur as colorHex,
3089
+ ir as createMeshAvatar,
3090
+ Ir as loadMeshAvatar,
3091
+ Br as openAvatar,
3092
+ jr as packAvatar,
3093
+ On as parseLighting,
3094
+ Ye as parseManifest,
3095
+ Et as parseRig,
3096
+ kr as unpackAvatar,
3097
+ on as validateAvatarFiles,
3098
+ en as validateRig
3018
3099
  };