@margoengine/rigging 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1443 -2
  4. package/dist/anatomy-data-ansur2.d.ts +63 -0
  5. package/dist/anatomy-data-cdc.d.ts +16 -0
  6. package/dist/anatomy-data-deleva.d.ts +86 -0
  7. package/dist/anatomy-human.d.ts +67 -0
  8. package/dist/anatomy-human.js +2229 -0
  9. package/dist/anatomy.d.ts +638 -0
  10. package/dist/anatomy.js +1112 -0
  11. package/dist/autorig.d.ts +50 -0
  12. package/dist/autorig.js +663 -0
  13. package/dist/bar-grip.d.ts +7 -0
  14. package/dist/body-volumes-DTym0RVY.js +56 -0
  15. package/dist/body-volumes.d.ts +19 -0
  16. package/dist/checks.d.ts +50 -0
  17. package/dist/contact.d.ts +186 -0
  18. package/dist/contact.js +746 -0
  19. package/dist/correctives.d.ts +92 -0
  20. package/dist/correctives.js +162 -0
  21. package/dist/coupling.d.ts +57 -0
  22. package/dist/coupling.js +330 -0
  23. package/dist/errors-D8rc8kzg.js +7 -0
  24. package/dist/face.d.ts +93 -0
  25. package/dist/face.js +144 -0
  26. package/dist/hands.d.ts +209 -0
  27. package/dist/hands.js +510 -0
  28. package/dist/heat.d.ts +38 -0
  29. package/dist/hinged-contact.d.ts +7 -0
  30. package/dist/hulls.d.ts +54 -0
  31. package/dist/hulls.js +134 -0
  32. package/dist/ik.d.ts +162 -0
  33. package/dist/ik.js +561 -0
  34. package/dist/index.d.ts +6 -0
  35. package/dist/index.js +390 -0
  36. package/dist/joint-limits.d.ts +44 -0
  37. package/dist/joint-limits.js +74 -0
  38. package/dist/limb-collision.d.ts +99 -0
  39. package/dist/loading-CZaYr1N7.js +67 -0
  40. package/dist/mesh-CyvLHNp0.js +142 -0
  41. package/dist/mesh.d.ts +47 -0
  42. package/dist/mtoon-data.d.ts +118 -0
  43. package/dist/mtoon-legacy.d.ts +17 -0
  44. package/dist/mtoon-shader.d.ts +4 -0
  45. package/dist/mtoon.d.ts +84 -0
  46. package/dist/mtoon.js +990 -0
  47. package/dist/paint.d.ts +82 -0
  48. package/dist/pose-tracking.d.ts +117 -0
  49. package/dist/pose-tracking.js +488 -0
  50. package/dist/retarget.d.ts +78 -0
  51. package/dist/retarget.js +375 -0
  52. package/dist/rig-BawZ5OLb.js +127 -0
  53. package/dist/rig.d.ts +65 -0
  54. package/dist/shared/gltf.d.ts +257 -0
  55. package/dist/shared/layers.d.ts +16 -0
  56. package/dist/springs.d.ts +180 -0
  57. package/dist/springs.js +458 -0
  58. package/dist/strand-springs.d.ts +46 -0
  59. package/dist/strand-springs.js +168 -0
  60. package/dist/swim.d.ts +55 -0
  61. package/dist/swim.js +167 -0
  62. package/dist/transfer.d.ts +41 -0
  63. package/dist/transfer.js +136 -0
  64. package/dist/validate.d.ts +188 -0
  65. package/dist/validate.js +440 -0
  66. package/dist/voxel-weights-48ok-Dax.js +255 -0
  67. package/dist/voxel-weights.d.ts +42 -0
  68. package/dist/voxel-weights.js +1 -0
  69. package/dist/voxels.d.ts +36 -0
  70. package/dist/vrm-constraints.d.ts +60 -0
  71. package/dist/vrm-constraints.js +261 -0
  72. package/dist/vrm-first-person.d.ts +115 -0
  73. package/dist/vrm-first-person.js +618 -0
  74. package/dist/vrm-presets-CU4V4yq6.js +47 -0
  75. package/dist/vrm-presets.d.ts +1 -0
  76. package/dist/vrm.d.ts +303 -0
  77. package/dist/vrm.js +574 -0
  78. package/dist/vrm0.d.ts +134 -0
  79. package/dist/vrm0.js +377 -0
  80. package/dist/vrma.d.ts +90 -0
  81. package/dist/vrma.js +315 -0
  82. package/dist/weight-map-D8cAONQQ.js +92 -0
  83. package/dist/weight-map.d.ts +58 -0
  84. package/package.json +189 -4
@@ -0,0 +1,618 @@
1
+ import { a as e, i as t, n, o as r, r as i, s as a } from "./vrm-presets-CU4V4yq6.js";
2
+ import { readVRM as o } from "./vrm.js";
3
+ import { t as s } from "./loading-CZaYr1N7.js";
4
+ import { MargoError as c } from "@margoengine/math/errors";
5
+ //#region ../shared/view-rect.js
6
+ function l(e, t) {
7
+ if (!Number.isInteger(e) || e < 0 || e > 4294967295) throw RangeError(`${t} ${e} is not a 32-bit layer mask; pass an integer from 0 to 0xffffffff`);
8
+ return e >>> 0;
9
+ }
10
+ //#endregion
11
+ //#region ../shared/layers.js
12
+ var u = Symbol.for("margoengine.layers");
13
+ function d() {
14
+ return globalThis[u] ??= {
15
+ masks: /* @__PURE__ */ new WeakMap(),
16
+ cameras: /* @__PURE__ */ new WeakMap(),
17
+ source: null,
18
+ used: !1,
19
+ maskOf(e) {
20
+ if (this.source) return this.source(e) >>> 0;
21
+ if (!this.used) return 1;
22
+ let t = e;
23
+ for (let e = 0; t && e < 64; e++) {
24
+ let e = this.masks.get(t);
25
+ if (e !== void 0) return e;
26
+ let n = t.engine.store.parents[t.id];
27
+ t = n >= 0 ? t.engine.meshes.get(n) : void 0;
28
+ }
29
+ return 1;
30
+ }
31
+ };
32
+ }
33
+ function f(e, t) {
34
+ let n = d();
35
+ n.masks.set(e, l(t, "Render layer mask")), n.used = !0, e.engine && (e.engine.collected = -1);
36
+ }
37
+ var p = 29, m = 30, h = {
38
+ 5120: 1,
39
+ 5121: 1,
40
+ 5122: 2,
41
+ 5123: 2,
42
+ 5125: 4,
43
+ 5126: 4
44
+ }, g = {
45
+ SCALAR: 1,
46
+ VEC2: 2,
47
+ VEC3: 3,
48
+ VEC4: 4
49
+ }, _ = {
50
+ 5120: 127,
51
+ 5121: 255,
52
+ 5122: 32767,
53
+ 5123: 65535
54
+ }, v = /* @__PURE__ */ new Set([
55
+ "auto",
56
+ "both",
57
+ "thirdPersonOnly",
58
+ "firstPersonOnly"
59
+ ]), y = "KHR_draco_mesh_compression", b = (e) => new c("failed", `VRM firstPerson split: ${e}; repair the file and check it with the glTF Validator`);
60
+ function x(e, t, n, r = e.normalized) {
61
+ let i = e.view ??= new DataView(e.bytes.buffer, e.bytes.byteOffset, e.bytes.byteLength), a = t * e.element + n * h[e.componentType], o;
62
+ switch (e.componentType) {
63
+ case 5120:
64
+ o = i.getInt8(a);
65
+ break;
66
+ case 5121:
67
+ o = i.getUint8(a);
68
+ break;
69
+ case 5122:
70
+ o = i.getInt16(a, !0);
71
+ break;
72
+ case 5123:
73
+ o = i.getUint16(a, !0);
74
+ break;
75
+ case 5125:
76
+ o = i.getUint32(a, !0);
77
+ break;
78
+ default: return i.getFloat32(a, !0);
79
+ }
80
+ return r ? Math.max(-1, o / _[e.componentType]) : o;
81
+ }
82
+ function S(e, t, n, r) {
83
+ let i = h[t], a = e.length / n, o = new Uint8Array(e.length * i), s = new DataView(o.buffer);
84
+ for (let n = 0; n < e.length; n++) {
85
+ let r = e[n], a = n * i;
86
+ t === 5126 ? s.setFloat32(a, r, !0) : t === 5120 ? s.setInt8(a, Math.round(r)) : t === 5121 ? s.setUint8(a, Math.round(r)) : t === 5122 ? s.setInt16(a, Math.round(r), !0) : t === 5123 ? s.setUint16(a, Math.round(r), !0) : s.setUint32(a, Math.round(r), !0);
87
+ }
88
+ return {
89
+ bytes: o,
90
+ element: i * n,
91
+ componentType: t,
92
+ type: r,
93
+ normalized: !1,
94
+ count: a
95
+ };
96
+ }
97
+ var C = class {
98
+ constructor(e, t) {
99
+ this.doc = e, this.bin = t, this.views = /* @__PURE__ */ new Map(), this.buffers = /* @__PURE__ */ new Map();
100
+ }
101
+ buffer(e) {
102
+ let t = this.buffers.get(e);
103
+ if (t) return t;
104
+ let n = this.doc.buffers?.[e];
105
+ if (!n) throw b(`buffer ${e} does not exist`);
106
+ if (n.uri === void 0) {
107
+ if (e !== 0 || !this.bin) throw b(`buffer ${e} has no data`);
108
+ t = this.bin;
109
+ } else {
110
+ let r = /^data:[^,]*;base64,(.*)$/s.exec(n.uri);
111
+ if (!r) throw new c("unsupported", `VRM firstPerson split: buffer ${e} is the external file ${n.uri}; pass a .vrm with every buffer embedded`);
112
+ t = Uint8Array.from(atob(r[1]), (e) => e.charCodeAt(0));
113
+ }
114
+ return this.buffers.set(e, t), t;
115
+ }
116
+ slice(e, t = 0, n) {
117
+ let r = this.buffer(e);
118
+ if (!(t >= 0 && n >= 0 && t + n <= r.length)) throw b(`a buffer view runs past buffer ${e}`);
119
+ return r.subarray(t, t + n);
120
+ }
121
+ view(e) {
122
+ let t = this.views.get(e);
123
+ if (t) return t;
124
+ let n = this.doc.bufferViews?.[e];
125
+ if (!n) throw b(`buffer view ${e} does not exist`);
126
+ let r = n.extensions?.EXT_meshopt_compression ?? n.extensions?.KHR_meshopt_compression;
127
+ if (r) {
128
+ if (t = this.meshopt(this.slice(r.buffer, r.byteOffset, r.byteLength), r.count, r.byteStride, r.mode, r.filter), t.length !== n.byteLength) throw b(`meshopt buffer view ${e} decodes to the wrong length`);
129
+ } else t = this.slice(n.buffer, n.byteOffset, n.byteLength);
130
+ return this.views.set(e, t), t;
131
+ }
132
+ column(e) {
133
+ let t = this.doc.accessors?.[e];
134
+ if (!t) throw b(`accessor ${e} does not exist`);
135
+ let n = h[t.componentType], r = g[t.type];
136
+ if (!n || !r || !Number.isSafeInteger(t.count) || t.count < 0) throw b(`accessor ${e} is not a vertex or index accessor`);
137
+ let i = n * r, a = t.count, o = new Uint8Array(a * i);
138
+ if (t.bufferView !== void 0) {
139
+ let n = this.view(t.bufferView), r = this.doc.bufferViews[t.bufferView].byteStride ?? i, s = t.byteOffset ?? 0;
140
+ if (a && s + (a - 1) * r + i > n.length) throw b(`accessor ${e} runs past its buffer view`);
141
+ for (let e = 0; e < a; e++) o.set(n.subarray(s + e * r, s + e * r + i), e * i);
142
+ }
143
+ let s = t.sparse;
144
+ if (s) {
145
+ let t = this.view(s.indices.bufferView), n = this.view(s.values.bufferView), r = h[s.indices.componentType], c = s.indices.byteOffset ?? 0, l = s.values.byteOffset ?? 0;
146
+ if (![
147
+ 5121,
148
+ 5123,
149
+ 5125
150
+ ].includes(s.indices.componentType) || c + s.count * r > t.length || l + s.count * i > n.length) throw b(`the sparse part of accessor ${e} runs past its buffer views`);
151
+ let u = {
152
+ bytes: t.subarray(c),
153
+ element: r,
154
+ componentType: s.indices.componentType,
155
+ type: "SCALAR",
156
+ normalized: !1,
157
+ count: s.count
158
+ };
159
+ for (let t = 0; t < s.count; t++) {
160
+ let r = x(u, t, 0);
161
+ if (r >= a) throw b(`sparse index ${r} of accessor ${e} is past its ${a} elements`);
162
+ o.set(n.subarray(l + t * i, l + (t + 1) * i), r * i);
163
+ }
164
+ }
165
+ return {
166
+ bytes: o,
167
+ element: i,
168
+ componentType: t.componentType,
169
+ type: t.type,
170
+ normalized: !!t.normalized,
171
+ count: a
172
+ };
173
+ }
174
+ };
175
+ function w(e, t) {
176
+ let n = e.length, r = [];
177
+ switch (t) {
178
+ case 0: return {
179
+ elements: Uint32Array.from(e),
180
+ per: 1
181
+ };
182
+ case 1: return {
183
+ elements: Uint32Array.from(e).subarray(0, n - n % 2),
184
+ per: 2
185
+ };
186
+ case 2:
187
+ case 3:
188
+ for (let t = 0; t + 1 < n; t++) r.push(e[t], e[t + 1]);
189
+ return t === 2 && n > 2 && r.push(e[n - 1], e[0]), {
190
+ elements: Uint32Array.from(r),
191
+ per: 2
192
+ };
193
+ case 4: return {
194
+ elements: Uint32Array.from(e).subarray(0, n - n % 3),
195
+ per: 3
196
+ };
197
+ case 5:
198
+ for (let t = 0; t + 2 < n; t++) r.push(e[t], e[t + 1 + t % 2], e[t + 2 - t % 2]);
199
+ return {
200
+ elements: Uint32Array.from(r),
201
+ per: 3
202
+ };
203
+ case 6:
204
+ for (let t = 0; t + 2 < n; t++) r.push(e[t + 1], e[t + 2], e[0]);
205
+ return {
206
+ elements: Uint32Array.from(r),
207
+ per: 3
208
+ };
209
+ }
210
+ throw b(`primitive mode ${t} is not 0 to 6`);
211
+ }
212
+ var T = class {
213
+ constructor(e, t) {
214
+ this.doc = e, this.length = t, this.chunks = [];
215
+ }
216
+ accessor(e, t, n, r) {
217
+ let i = t ? t.length : e.count, a = e.element, o = n ? a + 3 & -4 : a, s = new Uint8Array(i * o);
218
+ for (let n = 0; n < i; n++) {
219
+ let r = (t ? t[n] : n) * a;
220
+ s.set(e.bytes.subarray(r, r + a), n * o);
221
+ }
222
+ let c = this.doc.bufferViews ??= [];
223
+ c.push({
224
+ buffer: 0,
225
+ byteOffset: this.length,
226
+ byteLength: s.length,
227
+ ...o === a ? {} : { byteStride: o },
228
+ target: n ? 34962 : 34963
229
+ }), this.chunks.push(s), this.length += s.length + 3 & -4, s.length & 3 && this.chunks.push(new Uint8Array(4 - (s.length & 3)));
230
+ let l = {
231
+ bufferView: c.length - 1,
232
+ componentType: e.componentType,
233
+ count: i,
234
+ type: e.type,
235
+ ...e.normalized ? { normalized: !0 } : {}
236
+ };
237
+ if (r) {
238
+ let t = g[e.type], n = Array(t).fill(Infinity), r = Array(t).fill(-Infinity), a = {
239
+ ...e,
240
+ bytes: s,
241
+ element: o,
242
+ count: i,
243
+ view: void 0
244
+ };
245
+ for (let e = 0; e < i; e++) for (let i = 0; i < t; i++) {
246
+ let t = x(a, e, i, !1);
247
+ t < n[i] && (n[i] = t), t > r[i] && (r[i] = t);
248
+ }
249
+ i && Object.assign(l, {
250
+ min: n,
251
+ max: r
252
+ });
253
+ }
254
+ return (this.doc.accessors ??= []).push(l), this.doc.accessors.length - 1;
255
+ }
256
+ };
257
+ async function E(e, t, n, r, i) {
258
+ let a = {}, o = n.extensions?.[y], s, c = n.mode ?? 4;
259
+ if (o) {
260
+ let l = {};
261
+ for (let t of Object.keys(o.attributes)) {
262
+ let r = e.accessors?.[n.attributes[t]];
263
+ if (!r) throw b(`Draco attribute ${t} has no accessor`);
264
+ l[t] = r.componentType;
265
+ }
266
+ let u = await (await r()).decode(t.view(o.bufferView).slice(), {
267
+ attributeIds: o.attributes,
268
+ componentTypes: l,
269
+ signal: i
270
+ });
271
+ i?.throwIfAborted();
272
+ let d = {
273
+ POSITION: u.positions,
274
+ NORMAL: u.normals,
275
+ TEXCOORD_0: u.uvs,
276
+ COLOR_0: u.colors
277
+ };
278
+ for (let t of Object.keys(o.attributes)) {
279
+ let r = e.accessors[n.attributes[t]], i = g[r.type], o = d[t] ?? u.attributes?.[t]?.array;
280
+ if (!i || !o || o.length !== u.vertexCount * i) throw b(`Draco attribute ${t} does not match its accessor`);
281
+ let s = t === "COLOR_0" ? 5126 : r.componentType, c = S(o, s, i, r.type);
282
+ c.normalized = s !== 5126 && !!r.normalized, a[t] = c;
283
+ }
284
+ s = u.indices ?? Uint32Array.from({ length: u.vertexCount }, (e, t) => t), c = 4;
285
+ }
286
+ for (let [e, r] of Object.entries(n.attributes)) a[e] ??= t.column(r);
287
+ let l = a.POSITION?.count ?? Object.values(a)[0]?.count ?? 0;
288
+ for (let e of Object.values(a)) if (e.count !== l) throw b("the attributes of a primitive have different counts");
289
+ if (!s) {
290
+ if (n.indices === void 0) s = Uint32Array.from({ length: l }, (e, t) => t);
291
+ else {
292
+ let e = t.column(n.indices);
293
+ if (![
294
+ 5121,
295
+ 5123,
296
+ 5125
297
+ ].includes(e.componentType) || e.type !== "SCALAR") throw b("indices must be unsigned SCALAR integers");
298
+ s = Uint32Array.from({ length: e.count }, (t, n) => x(e, n, 0, !1));
299
+ }
300
+ }
301
+ for (let e = 0; e < s.length; e++) if (s[e] >= l) throw b(`index ${s[e]} is past ${l} vertices`);
302
+ return {
303
+ attributes: a,
304
+ targets: (n.targets ?? []).map((e) => {
305
+ let n = {};
306
+ for (let [r, i] of Object.entries(e)) if (n[r] = t.column(i), n[r].count !== l) throw b("a morph target has a different vertex count");
307
+ return n;
308
+ }),
309
+ ...w(s, c),
310
+ count: l
311
+ };
312
+ }
313
+ function D(e, t, n) {
314
+ let r = new Uint8Array(e.count);
315
+ for (let i = 0; e.attributes[`JOINTS_${i}`]; i++) {
316
+ let a = e.attributes[`JOINTS_${i}`], o = e.attributes[`WEIGHTS_${i}`];
317
+ if (!o) continue;
318
+ let s = g[a.type];
319
+ for (let i = 0; i < e.count; i++) for (let e = 0; e < s; e++) {
320
+ let s = x(a, i, e, !1);
321
+ if (s >= t.length) throw b(`joint ${s} is past the skin's ${t.length} joints`);
322
+ t[s] && x(o, i, e, !0) > n && (r[i] = 1);
323
+ }
324
+ }
325
+ return r;
326
+ }
327
+ function O(e, t, n, r, i) {
328
+ let a = new Int32Array(t.count).fill(-1), o = [], s = [], c = t.per;
329
+ for (let e = 0; e < n.length; e++) if (n[e] === r) for (let n = 0; n < c; n++) {
330
+ let r = t.elements[e * c + n];
331
+ a[r] < 0 && (a[r] = o.length, o.push(r)), s.push(a[r]);
332
+ }
333
+ let l = {};
334
+ for (let [e, n] of Object.entries(t.attributes)) l[e] = i.accessor(n, o, !0, e === "POSITION");
335
+ let u = S(s, o.length > 65535 ? 5125 : 5123, 1, "SCALAR"), { extensions: d, ...f } = e, p = {
336
+ ...structuredClone(f),
337
+ attributes: l,
338
+ indices: i.accessor(u, null, !1, !1),
339
+ mode: c === 3 ? 4 : +(c === 2)
340
+ };
341
+ if (d) {
342
+ let e = structuredClone(d);
343
+ delete e[y], Object.keys(e).length && (p.extensions = e);
344
+ }
345
+ return t.targets.length ? p.targets = t.targets.map((e) => Object.fromEntries(Object.entries(e).map(([e, t]) => [e, i.accessor(t, o, !0, e === "POSITION")]))) : delete p.targets, p;
346
+ }
347
+ var k = (e) => [...Object.values(e.attributes), ...(e.targets ?? []).flatMap((e) => Object.values(e))];
348
+ async function A(e, t = {}) {
349
+ let { threshold: i = 0, signal: s } = t;
350
+ if (s?.throwIfAborted(), !(e instanceof Uint8Array) || !a(e)) throw TypeError("splitVRMFirstPerson needs the bytes of a .vrm file");
351
+ if (!(i >= 0 && i < 1)) throw RangeError("threshold must be a weight from 0 up to 1");
352
+ let c = r(e), l = n(new TextDecoder().decode(c[0].data)), u = o(l), d = l.extensions.VRMC_vrm, f = l.nodes ?? [], p = u.humanoid.humanBones.head?.node, m = /* @__PURE__ */ new Map();
353
+ for (let e of d.firstPerson?.meshAnnotations ?? []) {
354
+ if (!f[e?.node]) throw b(`an annotation names node ${e?.node}, which does not exist`);
355
+ if (!v.has(e.type)) throw b(`node ${e.node} has the type ${String(e.type)}, not auto, both, thirdPersonOnly or firstPersonOnly`);
356
+ m.has(e.node) || m.set(e.node, e.type);
357
+ }
358
+ let h = f.flatMap((e, t) => e.mesh !== void 0 && (m.get(t) ?? "auto") === "auto" ? [t] : []);
359
+ if (!h.length) return e;
360
+ let g = new Uint8Array(f.length);
361
+ if (p !== void 0 && f[p]) {
362
+ let e = [p];
363
+ for (; e.length;) {
364
+ let t = e.pop();
365
+ if (!g[t]) {
366
+ g[t] = 1;
367
+ for (let n of f[t].children ?? []) f[n] && e.push(n);
368
+ }
369
+ }
370
+ }
371
+ let _ = c[1]?.type === 5130562 ? c[1].data : void 0, y = new C(l, _);
372
+ l.bufferViews?.some((e) => e.extensions?.EXT_meshopt_compression ?? e.extensions?.KHR_meshopt_compression) && (y.meshopt = (await import("@margoengine/decoders/meshopt")).meshoptDecodeGltfBuffer), s?.throwIfAborted();
373
+ let x = _?.length ?? 0, S = l.buffers ??= [];
374
+ if (!_ && (!S.length || S[0].uri !== void 0)) {
375
+ for (let e of l.bufferViews ?? []) {
376
+ e.buffer++;
377
+ let t = e.extensions?.EXT_meshopt_compression ?? e.extensions?.KHR_meshopt_compression;
378
+ t && t.buffer++;
379
+ }
380
+ S.unshift({ byteLength: 0 }), x = 0;
381
+ }
382
+ let w = new T(l, x + 3 & -4), A = /* @__PURE__ */ new Map();
383
+ for (let e of f) e.mesh !== void 0 && A.set(e.mesh, (A.get(e.mesh) ?? 0) + 1);
384
+ let M, N = t.draco, P = async () => {
385
+ if (!N) {
386
+ let { DracoDecoder: e } = await import("@margoengine/decoders");
387
+ s?.throwIfAborted();
388
+ let t = new e();
389
+ M = t, N = t;
390
+ }
391
+ return N;
392
+ }, F = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(), L = /* @__PURE__ */ new Map();
393
+ try {
394
+ for (let e of h) {
395
+ let t = f[e], n = t.mesh, r = l.meshes?.[n];
396
+ if (!r) throw b(`node ${e} names mesh ${n}, which does not exist`);
397
+ if (t.skin === void 0) {
398
+ m.set(e, g[e] ? "thirdPersonOnly" : "both");
399
+ continue;
400
+ }
401
+ let a = `${n} ${t.skin}`, o = L.get(a);
402
+ if (!o) {
403
+ let c = l.skins?.[t.skin]?.joints;
404
+ if (!c) throw b(`node ${e} names skin ${t.skin}, which does not exist`);
405
+ let u = Uint8Array.from(c, (e) => g[e] ?? 0), d = [];
406
+ for (let e of r.primitives) {
407
+ if (!e.attributes.JOINTS_0) {
408
+ d.push({
409
+ heads: 0,
410
+ total: 1
411
+ });
412
+ continue;
413
+ }
414
+ let t = await E(l, y, e, P, s), n = D(t, u, i), r = t.elements.length / t.per, a = new Uint8Array(r), o = 0;
415
+ for (let e = 0; e < r; e++) for (let r = 0; r < t.per; r++) if (n[t.elements[e * t.per + r]]) {
416
+ a[e] = 1, o++;
417
+ break;
418
+ }
419
+ d.push({
420
+ data: t,
421
+ flags: a,
422
+ heads: o,
423
+ total: r
424
+ });
425
+ }
426
+ let f = d.reduce((e, t) => e + t.heads, 0), p = d.reduce((e, t) => e + t.total, 0);
427
+ if (!f) o = { type: "both" };
428
+ else if (f === p && d.every((e) => e.heads === e.total)) o = { type: "thirdPersonOnly" };
429
+ else {
430
+ let e = (e) => new Set(r.primitives.flatMap((t, n) => d[n].heads === (e ? d[n].total : 0) ? k(t) : [])), t = e(0), i = [...e(1)].some((e) => t.has(e)), a = [], s = [];
431
+ r.primitives.forEach((e, t) => {
432
+ let n = d[t];
433
+ n.heads === 0 ? a.push(e) : n.heads === n.total && !i ? s.push(e) : (n.heads < n.total && a.push(O(e, n.data, n.flags, 0, w)), s.push(O(e, n.data, n.flags, 1, w)));
434
+ });
435
+ let { primitives: c, ...u } = r, f = l.meshes, p = {
436
+ ...structuredClone(u),
437
+ primitives: a
438
+ }, m = {
439
+ ...structuredClone(u),
440
+ ...r.name ? { name: `${r.name} head` } : {},
441
+ primitives: s
442
+ }, h = n;
443
+ A.get(n) === 1 ? f[n] = p : h = f.push(p) - 1;
444
+ let g = f.push(m) - 1;
445
+ I.set(n, [
446
+ ...I.get(n) ?? [],
447
+ ...h === n ? [] : [h],
448
+ g
449
+ ]), o = {
450
+ type: "split",
451
+ rest: h,
452
+ head: g
453
+ };
454
+ }
455
+ L.set(a, o);
456
+ }
457
+ if (o.type !== "split") {
458
+ m.set(e, o.type);
459
+ continue;
460
+ }
461
+ t.mesh = o.rest, m.set(e, "both");
462
+ let c = {
463
+ name: `${t.name ?? `node ${e}`} head`,
464
+ mesh: o.head,
465
+ skin: t.skin
466
+ };
467
+ t.weights && (c.weights = [...t.weights]);
468
+ let u = f.push(c) - 1;
469
+ (t.children ??= []).push(u), m.set(u, "thirdPersonOnly"), F.set(e, u);
470
+ }
471
+ } finally {
472
+ M?.dispose();
473
+ }
474
+ s?.throwIfAborted();
475
+ let R = d.expressions;
476
+ for (let e of [R?.preset, R?.custom]) for (let t of Object.values(e ?? {})) {
477
+ let e = t?.morphTargetBinds ?? [];
478
+ for (let t of [...e]) {
479
+ let n = F.get(t.node);
480
+ n !== void 0 && e.push({
481
+ ...t,
482
+ node: n
483
+ });
484
+ }
485
+ }
486
+ for (let e of l.animations ?? []) for (let t of [...e.channels ?? []]) {
487
+ let n = t.target, r = n.extensions?.KHR_animation_pointer?.pointer, i = r && /^\/(nodes|meshes)\/(\d+)\/weights$/.exec(r), a = (n) => void e.channels.push({
488
+ ...structuredClone(t),
489
+ target: n
490
+ });
491
+ if (n.path === "weights" && n.node !== void 0 && F.has(n.node)) a({
492
+ ...structuredClone(n),
493
+ node: F.get(n.node)
494
+ });
495
+ else if (i) {
496
+ let e = Number(i[2]), t = i[1] === "nodes" ? F.has(e) ? [F.get(e)] : [] : I.get(e) ?? [];
497
+ for (let e of t) a({
498
+ ...structuredClone(n),
499
+ extensions: {
500
+ ...structuredClone(n.extensions),
501
+ KHR_animation_pointer: { pointer: `/${i[1]}/${e}/weights` }
502
+ }
503
+ });
504
+ }
505
+ }
506
+ let z = (d.firstPerson?.meshAnnotations ?? []).filter((e) => f[e.node].mesh === void 0);
507
+ f.forEach((e, t) => {
508
+ e.mesh !== void 0 && z.push({
509
+ node: t,
510
+ type: m.get(t)
511
+ });
512
+ }), (d.firstPerson ??= {}).meshAnnotations = z;
513
+ let B = new Uint8Array(w.length);
514
+ _ && B.set(_);
515
+ let V = x + 3 & -4;
516
+ for (let e of w.chunks) B.set(e, V), V += e.length;
517
+ return S[0].byteLength = B.length, j(l, B, c.slice(_ ? 2 : 1));
518
+ }
519
+ function j(n, r, a) {
520
+ let o = new TextEncoder().encode(JSON.stringify(n)), s = o.length + 3 & -4, c = r.length + 3 & -4, l = 20 + s + 8 + c + a.reduce((e, t) => e + 8 + t.data.length, 0), u = new Uint8Array(l), d = new DataView(u.buffer);
521
+ d.setUint32(0, e, !0), d.setUint32(4, 2, !0), d.setUint32(8, l, !0), d.setUint32(12, s, !0), d.setUint32(16, t, !0), u.set(o, 20), u.fill(32, 20 + o.length, 20 + s);
522
+ let f = 20 + s;
523
+ for (let e of [{
524
+ type: i,
525
+ data: r
526
+ }, ...a]) {
527
+ let t = e.type === 5130562 ? c : e.data.length;
528
+ d.setUint32(f, t, !0), d.setUint32(f + 4, e.type, !0), u.set(e.data, f + 8), f += 8 + t;
529
+ }
530
+ return u;
531
+ }
532
+ async function M(e, t, n = {}) {
533
+ let { signal: r } = n;
534
+ r?.throwIfAborted();
535
+ let i = n.maxDownloadBytes ?? 67108864;
536
+ if (!Number.isSafeInteger(i) || i <= 0) throw RangeError("maxDownloadBytes must be a positive safe integer");
537
+ let a = t instanceof Uint8Array ? t : await s(await (n.fetch ?? globalThis.fetch)(t, { signal: r }), {
538
+ maxBytes: i,
539
+ signal: r,
540
+ label: "VRM"
541
+ });
542
+ if (a.byteLength > i) throw RangeError("The VRM is larger than maxDownloadBytes; raise it to load this file");
543
+ let { threshold: o, ...c } = n, l = await A(a, {
544
+ threshold: o,
545
+ signal: r,
546
+ draco: c.draco
547
+ }), { parseGLTFAsync: u } = await import("@margoengine/gltf/pbr");
548
+ return r?.throwIfAborted(), u(e, l, c);
549
+ }
550
+ var N = (e, t) => {
551
+ if (!Number.isInteger(e) || e < 0 || e > 31) throw RangeError(`${t} must be a layer from 0 to 31`);
552
+ return e;
553
+ };
554
+ function P(e, t = {}) {
555
+ let n = e.asset.doc, r = o(n), i = N(t.firstPersonOnlyLayer ?? 29, "firstPersonOnlyLayer"), a = N(t.thirdPersonOnlyLayer ?? 30, "thirdPersonOnlyLayer");
556
+ if (i === a) throw RangeError("firstPersonOnlyLayer and thirdPersonOnlyLayer must differ");
557
+ let s = n.nodes ?? [], l = /* @__PURE__ */ new Map();
558
+ for (let e of r.firstPerson?.meshAnnotations ?? []) l.has(e.node) || l.set(e.node, e.type);
559
+ let u = [];
560
+ s.forEach((e, t) => {
561
+ if (e.mesh === void 0) return;
562
+ let n = l.get(t) ?? "auto";
563
+ if (n === "auto") throw new c("state", `Node ${t} is a firstPerson auto mesh, which needs its head split before loading; load the file with loadVRM, or pass it through splitVRMFirstPerson`);
564
+ if (!v.has(n)) throw new c("failed", `Node ${t} has the firstPerson type ${n}; repair the file`);
565
+ u.push({
566
+ node: t,
567
+ type: n
568
+ });
569
+ });
570
+ let p = d(), m = 4294967295, h = (m & ~(1 << a)) >>> 0, g = (m & ~(1 << i)) >>> 0, _ = [], y = [];
571
+ for (let { node: t, type: n } of u) {
572
+ let r = e.nodes[t];
573
+ r && n !== "both" && (_.push([r, p.masks.get(r)]), f(r, 1 << (n === "thirdPersonOnly" ? a : i) >>> 0));
574
+ }
575
+ let b = e.root.engine, x = u.some((e) => e.type === "firstPersonOnly"), S = (e, t) => {
576
+ for (let n of e) y.push([n, p.cameras.get(n)]), p.cameras.set(n, t);
577
+ };
578
+ S(t.firstPersonCameras ?? [], h), x && S(t.thirdPersonCameras ?? [b.camera], g);
579
+ let C = r.humanoid.humanBones.head?.node, w = r.lookAt?.offsetFromHeadBone ?? [
580
+ 0,
581
+ 0,
582
+ 0
583
+ ], T = !1, E = {
584
+ firstPersonOnlyLayer: i,
585
+ thirdPersonOnlyLayer: a,
586
+ firstPersonLayers: h,
587
+ thirdPersonLayers: g,
588
+ meshAnnotations: u,
589
+ viewpoint(t = [
590
+ 0,
591
+ 0,
592
+ 0
593
+ ]) {
594
+ let n = C === void 0 ? void 0 : e.nodes[C];
595
+ if (!n) throw new c("failed", "The VRM has no head bone node; repair the file");
596
+ let r = n.matrix;
597
+ for (let e = 0; e < 3; e++) t[e] = r[e] * w[0] + r[4 + e] * w[1] + r[8 + e] * w[2] + r[12 + e];
598
+ return t;
599
+ },
600
+ dispose() {
601
+ if (!T) {
602
+ T = !0;
603
+ for (let [e, t] of _.reverse()) {
604
+ t === void 0 ? p.masks.delete(e) : p.masks.set(e, t);
605
+ let n = e.engine;
606
+ n && (n.collected = -1);
607
+ }
608
+ for (let [e, t] of y.reverse()) t === void 0 ? p.cameras.delete(e) : p.cameras.set(e, t);
609
+ }
610
+ },
611
+ [Symbol.dispose]() {
612
+ E.dispose();
613
+ }
614
+ };
615
+ return E;
616
+ }
617
+ //#endregion
618
+ export { p as FIRST_PERSON_ONLY_LAYER, m as THIRD_PERSON_ONLY_LAYER, P as addVRMFirstPerson, M as loadVRM, A as splitVRMFirstPerson };
@@ -0,0 +1,47 @@
1
+ import { i as e } from "./errors-D8rc8kzg.js";
2
+ //#region ../shared/glb.js
3
+ var t = 1179937895, n = 1313821514, r = 5130562, i = (t) => e(`GLB ${t}; pass a whole glTF 2.0 GLB file`, "failed"), a = (e) => e.length >= 4 && new DataView(e.buffer, e.byteOffset, 4).getUint32(0, !0) === 1179937895;
4
+ function o(e) {
5
+ if (e.length < 20) throw i(`needs at least 20 bytes for its 12-byte file header and 8-byte JSON chunk header; received ${e.length}`);
6
+ let t = new DataView(e.buffer, e.byteOffset, e.byteLength), n = t.getUint32(0, !0), r = t.getUint32(4, !0), a = t.getUint32(8, !0);
7
+ if (n !== 1179937895) throw i(`header magic 0x${n.toString(16)} is not glTF (0x46546c67)`);
8
+ if (r !== 2) throw i(`header version ${r} is unsupported; use version 2`);
9
+ if (a !== e.length) throw i(`header total length ${a} bytes differs from the received ${e.length} bytes`);
10
+ let o = [];
11
+ for (let n = 12; n < e.length;) {
12
+ if (n + 8 > e.length) throw i(`ends inside the 8-byte chunk header at byte ${n}`);
13
+ let r = t.getUint32(n, !0), a = t.getUint32(n + 4, !0), s = n + 8 + r;
14
+ if (r % 4) throw i(`chunk length ${r} at byte ${n} must have 4-byte alignment`);
15
+ if (s > e.length) throw i(`chunk length ${r} at byte ${n} extends past the ${e.length}-byte file`);
16
+ if (o.length === 0 != (a === 1313821514) || a === 5130562 && o.length !== 1) throw i("chunks must be JSON first, then at most one BIN second; export the file again");
17
+ o.push({
18
+ type: a,
19
+ data: e.subarray(n + 8, s)
20
+ }), n = s;
21
+ }
22
+ return o;
23
+ }
24
+ //#endregion
25
+ //#region ../shared/json.js
26
+ var s = /"(?:__proto__|constructor|prototype)"|\\u/, c = (e, t) => e === "__proto__" || e === "constructor" || e === "prototype" ? void 0 : t, l = (e) => s.test(e) ? JSON.parse(e, c) : JSON.parse(e), u = /* @__PURE__ */ new Set([
27
+ "happy",
28
+ "angry",
29
+ "sad",
30
+ "relaxed",
31
+ "surprised",
32
+ "aa",
33
+ "ih",
34
+ "ou",
35
+ "ee",
36
+ "oh",
37
+ "blink",
38
+ "blinkLeft",
39
+ "blinkRight",
40
+ "lookUp",
41
+ "lookDown",
42
+ "lookLeft",
43
+ "lookRight",
44
+ "neutral"
45
+ ]);
46
+ //#endregion
47
+ export { t as a, n as i, l as n, o, r, a as s, u as t };
@@ -0,0 +1 @@
1
+ export declare const PRESETS: ReadonlySet<string>;