@margoengine/rigging 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +969 -3
- package/dist/autorig.d.ts +50 -0
- package/dist/autorig.js +660 -0
- package/dist/bar-grip.d.ts +7 -0
- package/dist/checks.d.ts +50 -0
- package/dist/contact.d.ts +175 -0
- package/dist/contact.js +739 -0
- package/dist/correctives.d.ts +92 -0
- package/dist/correctives.js +162 -0
- package/dist/errors-D8rc8kzg.js +7 -0
- package/dist/face.d.ts +93 -0
- package/dist/face.js +144 -0
- package/dist/hands.d.ts +209 -0
- package/dist/hands.js +510 -0
- package/dist/heat.d.ts +38 -0
- package/dist/hinged-contact.d.ts +7 -0
- package/dist/hulls.d.ts +54 -0
- package/dist/hulls.js +134 -0
- package/dist/ik.d.ts +109 -0
- package/dist/ik.js +358 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +390 -0
- package/dist/joint-limits.d.ts +44 -0
- package/dist/joint-limits.js +74 -0
- package/dist/limb-collision.d.ts +99 -0
- package/dist/loading-CZaYr1N7.js +67 -0
- package/dist/mesh-CyvLHNp0.js +142 -0
- package/dist/mesh.d.ts +47 -0
- package/dist/mtoon-data.d.ts +118 -0
- package/dist/mtoon-legacy.d.ts +17 -0
- package/dist/mtoon-shader.d.ts +4 -0
- package/dist/mtoon.d.ts +84 -0
- package/dist/mtoon.js +990 -0
- package/dist/paint.d.ts +82 -0
- package/dist/pose-tracking.d.ts +117 -0
- package/dist/pose-tracking.js +488 -0
- package/dist/retarget.d.ts +71 -0
- package/dist/retarget.js +351 -0
- package/dist/rig-BawZ5OLb.js +127 -0
- package/dist/rig.d.ts +65 -0
- package/dist/shared/gltf.d.ts +257 -0
- package/dist/shared/layers.d.ts +16 -0
- package/dist/springs.d.ts +180 -0
- package/dist/springs.js +458 -0
- package/dist/strand-springs.d.ts +46 -0
- package/dist/strand-springs.js +168 -0
- package/dist/swim.d.ts +55 -0
- package/dist/swim.js +167 -0
- package/dist/transfer.d.ts +41 -0
- package/dist/transfer.js +136 -0
- package/dist/voxel-weights-48ok-Dax.js +255 -0
- package/dist/voxel-weights.d.ts +42 -0
- package/dist/voxel-weights.js +1 -0
- package/dist/voxels.d.ts +36 -0
- package/dist/vrm-constraints.d.ts +60 -0
- package/dist/vrm-constraints.js +261 -0
- package/dist/vrm-first-person.d.ts +115 -0
- package/dist/vrm-first-person.js +618 -0
- package/dist/vrm-presets-CU4V4yq6.js +47 -0
- package/dist/vrm-presets.d.ts +1 -0
- package/dist/vrm.d.ts +298 -0
- package/dist/vrm.js +574 -0
- package/dist/vrm0.d.ts +134 -0
- package/dist/vrm0.js +377 -0
- package/dist/vrma.d.ts +90 -0
- package/dist/vrma.js +315 -0
- package/dist/weight-map-D8cAONQQ.js +92 -0
- package/dist/weight-map.d.ts +58 -0
- package/package.json +175 -6
package/dist/vrm.js
ADDED
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { SpringBones as e, springAccess as t } from "./springs.js";
|
|
5
|
+
import { a as n, i as r, n as i, o as a, s as o, t as s } from "./vrm-presets-CU4V4yq6.js";
|
|
6
|
+
import { MargoError as c } from "@margoengine/math/errors";
|
|
7
|
+
import { axisAngleToQuaternion as l, multiplyQuaternions as u, normalizeQuaternion as d, rotateByQuaternion as f, splitTRS as p } from "@margoengine/math/quat";
|
|
8
|
+
//#region src/vrm.ts
|
|
9
|
+
var m = [
|
|
10
|
+
"hips",
|
|
11
|
+
"spine",
|
|
12
|
+
"head",
|
|
13
|
+
"leftUpperLeg",
|
|
14
|
+
"leftLowerLeg",
|
|
15
|
+
"leftFoot",
|
|
16
|
+
"rightUpperLeg",
|
|
17
|
+
"rightLowerLeg",
|
|
18
|
+
"rightFoot",
|
|
19
|
+
"leftUpperArm",
|
|
20
|
+
"leftLowerArm",
|
|
21
|
+
"leftHand",
|
|
22
|
+
"rightUpperArm",
|
|
23
|
+
"rightLowerArm",
|
|
24
|
+
"rightHand"
|
|
25
|
+
], h = {
|
|
26
|
+
overrideMouth: [
|
|
27
|
+
"aa",
|
|
28
|
+
"ih",
|
|
29
|
+
"ou",
|
|
30
|
+
"ee",
|
|
31
|
+
"oh"
|
|
32
|
+
],
|
|
33
|
+
overrideBlink: [
|
|
34
|
+
"blink",
|
|
35
|
+
"blinkLeft",
|
|
36
|
+
"blinkRight"
|
|
37
|
+
],
|
|
38
|
+
overrideLookAt: [
|
|
39
|
+
"lookUp",
|
|
40
|
+
"lookDown",
|
|
41
|
+
"lookLeft",
|
|
42
|
+
"lookRight"
|
|
43
|
+
]
|
|
44
|
+
}, g = [
|
|
45
|
+
["UpperLeg", "hips"],
|
|
46
|
+
["LowerLeg", ".UpperLeg"],
|
|
47
|
+
["Foot", ".LowerLeg"],
|
|
48
|
+
["Toes", ".Foot"],
|
|
49
|
+
["Shoulder", "upperChest chest spine"],
|
|
50
|
+
["UpperArm", ".Shoulder upperChest chest spine"],
|
|
51
|
+
["LowerArm", ".UpperArm"],
|
|
52
|
+
["Hand", ".LowerArm"],
|
|
53
|
+
["Eye", "head"]
|
|
54
|
+
];
|
|
55
|
+
function _() {
|
|
56
|
+
let e = /* @__PURE__ */ new Map([
|
|
57
|
+
["spine", ["hips"]],
|
|
58
|
+
["chest", ["spine"]],
|
|
59
|
+
["upperChest", ["chest"]],
|
|
60
|
+
["neck", [
|
|
61
|
+
"upperChest",
|
|
62
|
+
"chest",
|
|
63
|
+
"spine"
|
|
64
|
+
]],
|
|
65
|
+
["head", [
|
|
66
|
+
"neck",
|
|
67
|
+
"upperChest",
|
|
68
|
+
"chest",
|
|
69
|
+
"spine"
|
|
70
|
+
]],
|
|
71
|
+
["jaw", ["head"]]
|
|
72
|
+
]);
|
|
73
|
+
for (let t of ["left", "right"]) {
|
|
74
|
+
for (let [n, r] of g) e.set(t + n, r.split(" ").map((e) => e[0] === "." ? t + e.slice(1) : e));
|
|
75
|
+
let n = (n, r) => void e.set(t + n, r);
|
|
76
|
+
n("ThumbMetacarpal", [t + "Hand"]), n("ThumbProximal", [t + "ThumbMetacarpal"]), n("ThumbDistal", [t + "ThumbProximal"]);
|
|
77
|
+
for (let e of [
|
|
78
|
+
"Index",
|
|
79
|
+
"Middle",
|
|
80
|
+
"Ring",
|
|
81
|
+
"Little"
|
|
82
|
+
]) n(e + "Proximal", [t + "Hand"]), n(e + "Intermediate", [t + e + "Proximal"]), n(e + "Distal", [t + e + "Intermediate"]);
|
|
83
|
+
}
|
|
84
|
+
return e;
|
|
85
|
+
}
|
|
86
|
+
var v = (e) => !!e && typeof e == "object" && !Array.isArray(e);
|
|
87
|
+
function y(e) {
|
|
88
|
+
if (e instanceof Uint8Array) {
|
|
89
|
+
if (!o(e)) throw TypeError("A VRM file is a GLB; pass the bytes of a .vrm or .glb file");
|
|
90
|
+
return i(new TextDecoder().decode(a(e)[0].data));
|
|
91
|
+
}
|
|
92
|
+
if (!v(e)) throw TypeError("readVRM needs the bytes of a .vrm file or a parsed glTF document");
|
|
93
|
+
return e;
|
|
94
|
+
}
|
|
95
|
+
function b(e) {
|
|
96
|
+
let t = y(e).extensions ?? {}, n = t.VRMC_vrm;
|
|
97
|
+
if (!v(n)) throw v(t.VRM) ? new c("unsupported", "This is a VRM 0.x file; export it as VRM 1.0, or convert it, to read it here") : TypeError("This glTF has no VRMC_vrm extension; pass a VRM 1.0 file");
|
|
98
|
+
if (typeof n.specVersion != "string" || !/^1(\.|$)/.test(n.specVersion)) throw new c("unsupported", `VRM specVersion ${String(n.specVersion)} is not 1.x; export it as VRM 1.0`);
|
|
99
|
+
if (!v(n.meta) || !v(n.humanoid) || !v(n.humanoid.humanBones)) throw TypeError("VRMC_vrm needs meta and humanoid.humanBones; repair the file");
|
|
100
|
+
let r = structuredClone(n), i = t.VRMC_springBone;
|
|
101
|
+
return v(i) && (r.springBone = structuredClone(i)), r;
|
|
102
|
+
}
|
|
103
|
+
function x(e) {
|
|
104
|
+
return [...Object.entries(e.expressions?.preset ?? {}), ...Object.entries(e.expressions?.custom ?? {})];
|
|
105
|
+
}
|
|
106
|
+
function S(e, t) {
|
|
107
|
+
let n = [], r = t.nodes ?? [], i = Array(r.length).fill(-1);
|
|
108
|
+
r.forEach((e, t) => e.children?.forEach((e) => i[e] = t));
|
|
109
|
+
let a = (e) => Number.isInteger(e) && e >= 0 && e < r.length, o = (e, t) => {
|
|
110
|
+
for (let n = i[e]; n !== -1; n = i[n]) if (n === t) return !0;
|
|
111
|
+
return !1;
|
|
112
|
+
}, c = e.meta;
|
|
113
|
+
c?.name || n.push("meta.name is required"), (!Array.isArray(c?.authors) || !c.authors.length) && n.push("meta.authors needs at least one author"), c?.licenseUrl || n.push("meta.licenseUrl is required");
|
|
114
|
+
let l = e.humanoid?.humanBones ?? {}, u = _(), d = /* @__PURE__ */ new Map();
|
|
115
|
+
for (let e of m) l[e] || n.push(`humanoid bone ${e} is required`);
|
|
116
|
+
for (let [e, t] of Object.entries(l)) {
|
|
117
|
+
let i = t?.node;
|
|
118
|
+
if (e !== "hips" && !u.has(e) && n.push(`${e} is not a humanoid bone`), !a(i)) {
|
|
119
|
+
n.push(`humanoid bone ${e} names node ${String(i)}, which does not exist`);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
d.has(i) && n.push(`humanoid bones ${d.get(i)} and ${e} share node ${i}`), d.set(i, e);
|
|
123
|
+
let s = r[i].scale;
|
|
124
|
+
s && s.some((e) => !(e > 0)) && n.push(`humanoid bone ${e} needs a positive scale`);
|
|
125
|
+
let c = u.get(e)?.find((e) => l[e]);
|
|
126
|
+
c && a(l[c].node) && !o(i, l[c].node) && n.push(`humanoid bone ${e} must descend from ${c}`);
|
|
127
|
+
}
|
|
128
|
+
l.upperChest && !l.chest && n.push("humanoid bone upperChest needs chest");
|
|
129
|
+
for (let t of Object.keys(e.expressions?.custom ?? {})) s.has(t) && n.push(`custom expression ${t} has a preset's name`);
|
|
130
|
+
for (let [i, o] of x(e)) {
|
|
131
|
+
for (let e of o.morphTargetBinds ?? []) {
|
|
132
|
+
let o = a(e.node) ? t.meshes?.[r[e.node].mesh ?? -1] : void 0, s = o?.primitives?.[0]?.targets?.length ?? 0;
|
|
133
|
+
o ? e.index >= 0 && e.index < s || n.push(`expression ${i} binds morph target ${e.index} of node ${e.node}, which has ${s}`) : n.push(`expression ${i} binds node ${e.node}, which has no mesh`);
|
|
134
|
+
}
|
|
135
|
+
let e = t.materials?.length ?? 0;
|
|
136
|
+
for (let t of [...o.materialColorBinds ?? [], ...o.textureTransformBinds ?? []]) t.material >= 0 && t.material < e || n.push(`expression ${i} binds material ${t.material}, which does not exist`);
|
|
137
|
+
}
|
|
138
|
+
if (e.lookAt && e.lookAt.type !== "bone" && e.lookAt.type !== "expression" && n.push(`lookAt.type ${String(e.lookAt.type)} must be bone or expression`), e.lookAt) {
|
|
139
|
+
let t = e.lookAt.offsetFromHeadBone;
|
|
140
|
+
t !== void 0 && !(Array.isArray(t) && t.length === 3 && t.every(Number.isFinite)) && n.push("lookAt.offsetFromHeadBone must be three finite numbers");
|
|
141
|
+
for (let t of [
|
|
142
|
+
"HorizontalInner",
|
|
143
|
+
"HorizontalOuter",
|
|
144
|
+
"VerticalDown",
|
|
145
|
+
"VerticalUp"
|
|
146
|
+
]) {
|
|
147
|
+
let r = e.lookAt[`rangeMap${t}`];
|
|
148
|
+
r !== void 0 && (typeof r != "object" || !r ? n.push(`lookAt.rangeMap${t} must be an object`) : r.inputMaxValue !== void 0 && !(r.inputMaxValue >= 0 && r.inputMaxValue <= 180) ? n.push(`lookAt.rangeMap${t}.inputMaxValue must be from 0 to 180 degrees`) : r.outputScale !== void 0 && !Number.isFinite(r.outputScale) && n.push(`lookAt.rangeMap${t}.outputScale must be a finite number`));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (let t of e.firstPerson?.meshAnnotations ?? []) a(t.node) || n.push(`firstPerson names node ${t.node}, which does not exist`);
|
|
152
|
+
let f = e.springBone;
|
|
153
|
+
for (let e of f?.colliders ?? []) a(e.node) || n.push(`spring collider names node ${e.node}, which does not exist`);
|
|
154
|
+
return f?.springs?.forEach((e, t) => {
|
|
155
|
+
e.joints.forEach((r, i) => {
|
|
156
|
+
a(r.node) ? i && a(e.joints[i - 1].node) && !o(r.node, e.joints[i - 1].node) && n.push(`spring ${t} joint ${r.node} must descend from the joint before it`) : n.push(`spring ${t} joint names node ${r.node}, which does not exist`);
|
|
157
|
+
});
|
|
158
|
+
}), n;
|
|
159
|
+
}
|
|
160
|
+
function C(e, t) {
|
|
161
|
+
if (!(e instanceof Uint8Array) || !o(e)) throw TypeError("writeVRM needs the bytes of a GLB file");
|
|
162
|
+
let s = a(e), c = i(new TextDecoder().decode(s[0].data)), l = S(t, c);
|
|
163
|
+
if (l.length) throw RangeError(`This VRM is invalid; fix it: ${l.join("; ")}`);
|
|
164
|
+
let { springBone: u, ...d } = t, f = c.extensions ??= {}, p = new Set(c.extensionsUsed ?? []);
|
|
165
|
+
f.VRMC_vrm = d, p.add("VRMC_vrm"), u ? (f.VRMC_springBone = u, p.add("VRMC_springBone")) : (delete f.VRMC_springBone, p.delete("VRMC_springBone")), c.extensionsUsed = [...p];
|
|
166
|
+
let m = new TextEncoder().encode(JSON.stringify(c)), h = m.length + 3 & -4, g = s.slice(1), _ = 20 + h + g.reduce((e, t) => e + 8 + t.data.length, 0), v = new Uint8Array(_), y = new DataView(v.buffer);
|
|
167
|
+
y.setUint32(0, n, !0), y.setUint32(4, 2, !0), y.setUint32(8, _, !0), y.setUint32(12, h, !0), y.setUint32(16, r, !0), v.set(m, 20), v.fill(32, 20 + m.length, 20 + h);
|
|
168
|
+
let b = 20 + h;
|
|
169
|
+
for (let e of g) y.setUint32(b, e.data.length, !0), y.setUint32(b + 4, e.type, !0), v.set(e.data, b + 8), b += 8 + e.data.length;
|
|
170
|
+
return v;
|
|
171
|
+
}
|
|
172
|
+
function w(e) {
|
|
173
|
+
let { name: t, authors: n, bones: r, licenseUrl: i = "https://vrm.dev/licenses/1.0/", ...a } = e ?? {}, o = r instanceof Map ? [...r] : Object.entries(r ?? {}), s = Object.fromEntries(o.map(([e, t]) => [e, { node: t }]));
|
|
174
|
+
return {
|
|
175
|
+
specVersion: "1.0",
|
|
176
|
+
meta: {
|
|
177
|
+
name: t,
|
|
178
|
+
authors: n,
|
|
179
|
+
licenseUrl: i
|
|
180
|
+
},
|
|
181
|
+
humanoid: { humanBones: s },
|
|
182
|
+
...a
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function T(e) {
|
|
186
|
+
let t = e?.springBone;
|
|
187
|
+
if (!t?.springs?.length) return null;
|
|
188
|
+
let n = (t.colliders ?? []).map((e) => {
|
|
189
|
+
let { sphere: t, capsule: n } = e.shape ?? {}, r = n ?? t;
|
|
190
|
+
if (!r) throw TypeError("A VRM spring collider needs a sphere or a capsule shape; repair the file");
|
|
191
|
+
return {
|
|
192
|
+
node: e.node,
|
|
193
|
+
offset: r.offset ?? [
|
|
194
|
+
0,
|
|
195
|
+
0,
|
|
196
|
+
0
|
|
197
|
+
],
|
|
198
|
+
tail: n ? n.tail ?? [
|
|
199
|
+
0,
|
|
200
|
+
0,
|
|
201
|
+
0
|
|
202
|
+
] : null,
|
|
203
|
+
radius: r.radius || 1e-9
|
|
204
|
+
};
|
|
205
|
+
}), r = t.colliderGroups ?? [], i = [];
|
|
206
|
+
for (let e of t.springs) {
|
|
207
|
+
if (e.joints.length < 2) continue;
|
|
208
|
+
let t = /* @__PURE__ */ new Set();
|
|
209
|
+
for (let n of e.colliderGroups ?? []) for (let e of r[n]?.colliders ?? []) t.add(e);
|
|
210
|
+
i.push({
|
|
211
|
+
joints: e.joints.map((e) => e.node),
|
|
212
|
+
settings: e.joints.map((e) => ({
|
|
213
|
+
stiffness: e.stiffness ?? 1,
|
|
214
|
+
drag: e.dragForce ?? .5,
|
|
215
|
+
gravity: e.gravityPower ?? 0,
|
|
216
|
+
gravityDirection: e.gravityDir ?? [
|
|
217
|
+
0,
|
|
218
|
+
-1,
|
|
219
|
+
0
|
|
220
|
+
],
|
|
221
|
+
radius: e.hitRadius ?? 0
|
|
222
|
+
})),
|
|
223
|
+
center: e.center ?? null,
|
|
224
|
+
colliders: [...t]
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
chains: i,
|
|
229
|
+
colliders: n
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
var E = class {
|
|
233
|
+
constructor(e, t = {}) {
|
|
234
|
+
this.values = /* @__PURE__ */ new Map(), this.list = new Map(x(e)), this.names = [...this.list.keys()], this.kinds = [
|
|
235
|
+
["overrideMouth", /* @__PURE__ */ new Set([...h.overrideMouth, ...t.mouth ?? []])],
|
|
236
|
+
["overrideBlink", /* @__PURE__ */ new Set([...h.overrideBlink, ...t.blink ?? []])],
|
|
237
|
+
["overrideLookAt", /* @__PURE__ */ new Set([...h.overrideLookAt, ...t.lookAt ?? []])]
|
|
238
|
+
];
|
|
239
|
+
}
|
|
240
|
+
set(e, t) {
|
|
241
|
+
if (!Number.isFinite(t)) throw RangeError(`Expression ${e} value must be a finite number`);
|
|
242
|
+
if (!this.list.has(e) && !s.has(e)) throw RangeError(`Expression ${e} is not on this avatar; see expressions.names`);
|
|
243
|
+
return this.values.set(e, Math.min(1, Math.max(0, t))), this;
|
|
244
|
+
}
|
|
245
|
+
get(e) {
|
|
246
|
+
return this.values.get(e) ?? 0;
|
|
247
|
+
}
|
|
248
|
+
resolve() {
|
|
249
|
+
let e = /* @__PURE__ */ new Map();
|
|
250
|
+
for (let [t, n] of this.list) {
|
|
251
|
+
let r = this.get(t);
|
|
252
|
+
e.set(t, n.isBinary ? +(r > .5) : r);
|
|
253
|
+
}
|
|
254
|
+
for (let [t, n] of this.kinds) {
|
|
255
|
+
let r = !1, i = 0;
|
|
256
|
+
for (let [a, o] of this.list) {
|
|
257
|
+
let s = e.get(a), c = o[t];
|
|
258
|
+
s <= 0 || n.has(a) || c === void 0 || c === "none" || (c === "block" ? r = !0 : i += s);
|
|
259
|
+
}
|
|
260
|
+
let a = r ? 0 : 1 - Math.min(1, i);
|
|
261
|
+
if (a !== 1) for (let t of n) {
|
|
262
|
+
let n = this.list.get(t);
|
|
263
|
+
n && e.set(t, n.isBinary ? 0 : e.get(t) * a);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return e;
|
|
267
|
+
}
|
|
268
|
+
evaluate() {
|
|
269
|
+
let e = this.resolve(), t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
|
|
270
|
+
for (let [i, a] of this.list) {
|
|
271
|
+
let o = e.get(i);
|
|
272
|
+
for (let { node: e, index: n, weight: r } of a.morphTargetBinds ?? []) {
|
|
273
|
+
let i = `${e}:${n}`, a = t.get(i) ?? {
|
|
274
|
+
node: e,
|
|
275
|
+
index: n,
|
|
276
|
+
weight: 0
|
|
277
|
+
};
|
|
278
|
+
t.set(i, a), a.weight += r * o;
|
|
279
|
+
}
|
|
280
|
+
for (let { material: e, type: t, targetValue: r } of a.materialColorBinds ?? []) {
|
|
281
|
+
let i = `${e}:${t}`, a = n.get(i) ?? {
|
|
282
|
+
material: e,
|
|
283
|
+
type: t,
|
|
284
|
+
parts: []
|
|
285
|
+
};
|
|
286
|
+
n.set(i, a), o > 0 && a.parts.push({
|
|
287
|
+
value: o,
|
|
288
|
+
target: r
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
for (let { material: e, scale: t = [1, 1], offset: n = [0, 0] } of a.textureTransformBinds ?? []) {
|
|
292
|
+
let i = r.get(e) ?? {
|
|
293
|
+
material: e,
|
|
294
|
+
parts: []
|
|
295
|
+
};
|
|
296
|
+
r.set(e, i), o > 0 && i.parts.push({
|
|
297
|
+
value: o,
|
|
298
|
+
scale: t,
|
|
299
|
+
offset: n
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return {
|
|
304
|
+
morphs: [...t.values()],
|
|
305
|
+
colors: [...n.values()],
|
|
306
|
+
transforms: [...r.values()]
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
}, D = {
|
|
310
|
+
inputMaxValue: 90,
|
|
311
|
+
outputScale: 1
|
|
312
|
+
};
|
|
313
|
+
function O(e, t) {
|
|
314
|
+
let { inputMaxValue: n = D.inputMaxValue, outputScale: r = D.outputScale } = t ?? D, i = Math.abs(e);
|
|
315
|
+
return n > 0 ? Math.min(i, n) / n * r : i > 0 ? r : 0;
|
|
316
|
+
}
|
|
317
|
+
function k(e, t, n, r) {
|
|
318
|
+
let i = e.offsetFromHeadBone ?? [
|
|
319
|
+
0,
|
|
320
|
+
0,
|
|
321
|
+
0
|
|
322
|
+
], a = [
|
|
323
|
+
0,
|
|
324
|
+
1,
|
|
325
|
+
2
|
|
326
|
+
].map((e) => t[e] * i[0] + t[4 + e] * i[1] + t[8 + e] * i[2] + t[12 + e]), o = N(t), s = u(o, M(n)), c = f([
|
|
327
|
+
r[0] - a[0],
|
|
328
|
+
r[1] - a[1],
|
|
329
|
+
r[2] - a[2]
|
|
330
|
+
], M(s)), l = 180 / Math.PI;
|
|
331
|
+
return {
|
|
332
|
+
yaw: Math.atan2(c[0], c[2]) * l,
|
|
333
|
+
pitch: Math.atan2(-c[1], Math.hypot(c[0], c[2])) * l
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
function A(e, t) {
|
|
337
|
+
let { yaw: n, pitch: r } = t, i = r > 0 ? e.rangeMapVerticalDown : e.rangeMapVerticalUp, a = Math.sign(r) * O(r, i), o = Math.sign(n);
|
|
338
|
+
return {
|
|
339
|
+
left: {
|
|
340
|
+
yaw: o * O(n, n > 0 ? e.rangeMapHorizontalOuter : e.rangeMapHorizontalInner),
|
|
341
|
+
pitch: a
|
|
342
|
+
},
|
|
343
|
+
right: {
|
|
344
|
+
yaw: o * O(n, n > 0 ? e.rangeMapHorizontalInner : e.rangeMapHorizontalOuter),
|
|
345
|
+
pitch: a
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function j(e, t) {
|
|
350
|
+
let { yaw: n, pitch: r } = t;
|
|
351
|
+
return {
|
|
352
|
+
lookLeft: n > 0 ? O(n, e.rangeMapHorizontalOuter) : 0,
|
|
353
|
+
lookRight: n < 0 ? O(n, e.rangeMapHorizontalOuter) : 0,
|
|
354
|
+
lookDown: r > 0 ? O(r, e.rangeMapVerticalDown) : 0,
|
|
355
|
+
lookUp: r < 0 ? O(r, e.rangeMapVerticalUp) : 0
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
var M = (e) => [
|
|
359
|
+
-e[0],
|
|
360
|
+
-e[1],
|
|
361
|
+
-e[2],
|
|
362
|
+
e[3]
|
|
363
|
+
];
|
|
364
|
+
function N(e) {
|
|
365
|
+
let t = p(e, .001);
|
|
366
|
+
if (!t.ok) throw new c("state", "A VRM head matrix has no rotation; fix its scale");
|
|
367
|
+
return t.rotation;
|
|
368
|
+
}
|
|
369
|
+
function P(e) {
|
|
370
|
+
let t = e.nodes ?? [], n = t.map((e) => e.matrix ? N(e.matrix) : e.rotation ? Array.from(e.rotation) : [
|
|
371
|
+
0,
|
|
372
|
+
0,
|
|
373
|
+
0,
|
|
374
|
+
1
|
|
375
|
+
]), r = Array(t.length), i = (e, a) => {
|
|
376
|
+
r[e] = d(u(a, n[e]));
|
|
377
|
+
for (let n of t[e].children ?? []) i(n, r[e]);
|
|
378
|
+
}, a = new Set(t.flatMap((e) => e.children ?? []));
|
|
379
|
+
return t.forEach((e, t) => {
|
|
380
|
+
a.has(t) || i(t, [
|
|
381
|
+
0,
|
|
382
|
+
0,
|
|
383
|
+
0,
|
|
384
|
+
1
|
|
385
|
+
]);
|
|
386
|
+
}), {
|
|
387
|
+
local: n,
|
|
388
|
+
model: r
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
var F = 8;
|
|
392
|
+
function I(e, t, n, r) {
|
|
393
|
+
let i = Math.cos(n), a = Math.sin(n);
|
|
394
|
+
return [
|
|
395
|
+
i * t[0],
|
|
396
|
+
a * t[1],
|
|
397
|
+
e[0],
|
|
398
|
+
r,
|
|
399
|
+
-a * t[0],
|
|
400
|
+
i * t[1],
|
|
401
|
+
e[1],
|
|
402
|
+
0
|
|
403
|
+
];
|
|
404
|
+
}
|
|
405
|
+
function L(n, r, i = {}) {
|
|
406
|
+
let a = n.model, o = a.asset.doc, s = S(r, o);
|
|
407
|
+
if (s.length) throw RangeError(`This VRM does not fit the model; fix it: ${s.join("; ")}`);
|
|
408
|
+
let { animator: f } = i, p = new E(r, i), m = a.nodes, h = a.root.engine, g = h.store, _ = P(o), v = r.lookAt ?? null, y = r.humanoid.humanBones, b = y.head.node, C = [y.leftEye?.node, y.rightEye?.node], w = i.springs === !1 ? null : T(r), D = new Map(m.map((e, t) => [e.id, t])), O = m.map((e) => D.get(g.parents[e.id]) ?? -1), N = w ? new e(O, w) : null, L = (e, t) => {
|
|
409
|
+
f ? f.writeNodeRotation(m[e], t) : m[e].setQuaternion(t);
|
|
410
|
+
}, R = N ? t(a, L) : null, ee = !f && N ? N.bones() : [], z = [], B = (e) => [...new Set(a.primitives.filter((t) => t.definition.material === e).map((e) => e.material))], V = {
|
|
411
|
+
color: "base",
|
|
412
|
+
emissionColor: "emission"
|
|
413
|
+
}, H = /* @__PURE__ */ new Map(), U = /* @__PURE__ */ new Map();
|
|
414
|
+
function te(e) {
|
|
415
|
+
return B(e).map((e) => ({
|
|
416
|
+
live: e,
|
|
417
|
+
rows: (e.uvt ?? []).slice(),
|
|
418
|
+
maps: e.maps.map((e, t) => {
|
|
419
|
+
let n = e?.extensions?.KHR_texture_transform;
|
|
420
|
+
return {
|
|
421
|
+
slot: t,
|
|
422
|
+
used: !!e,
|
|
423
|
+
offset: n?.offset ?? [0, 0],
|
|
424
|
+
scale: n?.scale ?? [1, 1],
|
|
425
|
+
turn: n?.rotation ?? 0,
|
|
426
|
+
texCoord: e?.texCoord ?? 0
|
|
427
|
+
};
|
|
428
|
+
})
|
|
429
|
+
}));
|
|
430
|
+
}
|
|
431
|
+
for (let [e, t] of x(r)) {
|
|
432
|
+
for (let n of t.materialColorBinds ?? []) {
|
|
433
|
+
let t = V[n.type];
|
|
434
|
+
if (!t) {
|
|
435
|
+
z.push(`${e}: ${n.type}`);
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
let r = `${n.material}:${n.type}`;
|
|
439
|
+
if (H.has(r)) continue;
|
|
440
|
+
let i = B(n.material), a = o.materials?.[n.material] ?? {};
|
|
441
|
+
H.set(r, {
|
|
442
|
+
live: i,
|
|
443
|
+
value: i[0] ? i[0][t].slice() : [],
|
|
444
|
+
strength: t === "emission" ? a.extensions?.KHR_materials_emissive_strength?.emissiveStrength ?? 1 : 1
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
for (let e of t.textureTransformBinds ?? []) U.has(e.material) || U.set(e.material, te(e.material));
|
|
448
|
+
}
|
|
449
|
+
let W = /* @__PURE__ */ new Set(), G = {
|
|
450
|
+
target: null,
|
|
451
|
+
gaze: null,
|
|
452
|
+
input: null
|
|
453
|
+
}, K = () => {
|
|
454
|
+
if (!v) return;
|
|
455
|
+
let e = G.target;
|
|
456
|
+
G.input ? G.gaze = G.input : e ? G.gaze = k(v, m[b].matrix, _.model[b], e) : G.gaze = null;
|
|
457
|
+
let t = G.gaze ?? {
|
|
458
|
+
yaw: 0,
|
|
459
|
+
pitch: 0
|
|
460
|
+
};
|
|
461
|
+
if (v.type === "expression") {
|
|
462
|
+
let e = j(v, t);
|
|
463
|
+
for (let t of Object.keys(e)) p.set(t, e[t]);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
let n = A(v, t);
|
|
467
|
+
C.forEach((e, t) => {
|
|
468
|
+
if (e === void 0) return;
|
|
469
|
+
let { yaw: r, pitch: i } = t ? n.right : n.left, a = Math.PI / 180, o = u(l([
|
|
470
|
+
0,
|
|
471
|
+
1,
|
|
472
|
+
0
|
|
473
|
+
], r * a), l([
|
|
474
|
+
1,
|
|
475
|
+
0,
|
|
476
|
+
0
|
|
477
|
+
], i * a)), s = u(u(M(_.model[e]), o), _.model[e]);
|
|
478
|
+
L(e, d(u(_.local[e], s)));
|
|
479
|
+
});
|
|
480
|
+
}, q = () => {
|
|
481
|
+
let { morphs: e, colors: t, transforms: r } = p.evaluate();
|
|
482
|
+
for (let { node: t, index: r, weight: i } of e) n.setMorph(t, r, i);
|
|
483
|
+
for (let { material: e, type: n, parts: r } of t) {
|
|
484
|
+
let t = H.get(`${e}:${n}`);
|
|
485
|
+
if (!t) continue;
|
|
486
|
+
let i = V[n];
|
|
487
|
+
for (let e of t.live) t.value.forEach((n, a) => {
|
|
488
|
+
let o = n;
|
|
489
|
+
for (let { value: e, target: i } of r) o += (i[a] * t.strength - n) * e;
|
|
490
|
+
e[i][a] = o;
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
for (let { material: e, parts: t } of r) for (let { live: n, rows: r, maps: i } of U.get(e) ?? []) {
|
|
494
|
+
for (let e of i) {
|
|
495
|
+
if (!e.used) continue;
|
|
496
|
+
let r = [e.offset[0], e.offset[1]], i = [e.scale[0], e.scale[1]];
|
|
497
|
+
for (let n of t) for (let t = 0; t < 2; t++) r[t] += (n.offset[t] - e.offset[t]) * n.value, i[t] += (n.scale[t] - e.scale[t]) * n.value;
|
|
498
|
+
let a = I(r, i, e.turn, e.texCoord);
|
|
499
|
+
for (let t = 0; t < F; t++) n.uvt[e.slot * F + t] = a[t];
|
|
500
|
+
}
|
|
501
|
+
if (!r.length) throw new c("state", "This material has no texture transform rows; rebuild the model");
|
|
502
|
+
}
|
|
503
|
+
}, J = (e) => {
|
|
504
|
+
if (!f) {
|
|
505
|
+
for (let e of ee) m[e].setQuaternion(_.local[e]);
|
|
506
|
+
g.tick(!1);
|
|
507
|
+
}
|
|
508
|
+
for (let t of W) t(e);
|
|
509
|
+
K(), q(), N ? (g.tick(!1), N.step(e, R)) : g.tick(!1);
|
|
510
|
+
}, Y = `vrm:${m[b].id}`, X;
|
|
511
|
+
if (f) X = f.addSolver(J);
|
|
512
|
+
else {
|
|
513
|
+
let e = {coreRequirement:/*@__PURE__*/__margoCoreRequirement("0.1.0",import.meta.url),
|
|
514
|
+
name: Y,
|
|
515
|
+
setup() {},
|
|
516
|
+
onPreUpdate: J,
|
|
517
|
+
dispose: () => {
|
|
518
|
+
X && (X = () => {}, $());
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
h.use(e), X = () => {
|
|
522
|
+
let t = h.sceneOf(e);
|
|
523
|
+
t?.plugins.get(Y) === e && t.removePlugin(Y);
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
let Z = () => {
|
|
527
|
+
let e = X;
|
|
528
|
+
X = null, e?.(), W.clear(), Q?.(), Q = null;
|
|
529
|
+
}, Q = a.onDispose(Z), $ = () => {
|
|
530
|
+
if (X) {
|
|
531
|
+
Z();
|
|
532
|
+
for (let e of p.names) p.set(e, 0);
|
|
533
|
+
G.target = null, G.input = null, K(), q(), g.tick(!1);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
return {
|
|
537
|
+
avatar: r,
|
|
538
|
+
expressions: p,
|
|
539
|
+
springs: N,
|
|
540
|
+
unsupported: z,
|
|
541
|
+
addPoseSource(e) {
|
|
542
|
+
if (!X) throw new c("disposed", "VRM controller is disposed; create a new controller");
|
|
543
|
+
if (typeof e != "function") throw TypeError("VRM pose source must be a function");
|
|
544
|
+
return W.add(e), () => {
|
|
545
|
+
W.delete(e);
|
|
546
|
+
};
|
|
547
|
+
},
|
|
548
|
+
get lookAtTarget() {
|
|
549
|
+
return G.target;
|
|
550
|
+
},
|
|
551
|
+
set lookAtTarget(e) {
|
|
552
|
+
if (e !== null && !(Array.isArray(e) && e.length === 3 && e.every(Number.isFinite))) throw RangeError("lookAtTarget must be three finite numbers or null");
|
|
553
|
+
G.target = e;
|
|
554
|
+
},
|
|
555
|
+
get lookAtGaze() {
|
|
556
|
+
return G.input;
|
|
557
|
+
},
|
|
558
|
+
set lookAtGaze(e) {
|
|
559
|
+
if (e !== null && (!Number.isFinite(e.yaw) || !Number.isFinite(e.pitch))) throw RangeError("lookAtGaze needs finite yaw and pitch in degrees, or null");
|
|
560
|
+
G.input = e === null ? null : {
|
|
561
|
+
yaw: e.yaw,
|
|
562
|
+
pitch: e.pitch
|
|
563
|
+
};
|
|
564
|
+
},
|
|
565
|
+
get gaze() {
|
|
566
|
+
return G.gaze;
|
|
567
|
+
},
|
|
568
|
+
update: J,
|
|
569
|
+
dispose: $,
|
|
570
|
+
[Symbol.dispose]: $
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
//#endregion
|
|
574
|
+
export { E as VRMExpressions, m as VRM_REQUIRED_BONES, L as addVRM, S as checkVRM, w as createVRM, b as readVRM, A as vrmEyeAngles, k as vrmGaze, j as vrmLookWeights, T as vrmSprings, C as writeVRM };
|
package/dist/vrm0.d.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
4
|
+
import type { GLTFLoadOptions, PBRModel } from '@margoengine/gltf/pbr';
|
|
5
|
+
import { type VRMAvatar, type VRMDocument } from './vrm.js';
|
|
6
|
+
/** VRM 0.x uses object vectors in Unity's left-handed extension fields. */
|
|
7
|
+
export interface VRM0Vector {
|
|
8
|
+
x?: number;
|
|
9
|
+
y?: number;
|
|
10
|
+
z?: number;
|
|
11
|
+
}
|
|
12
|
+
/** Original permissions are retained verbatim; conversion never grants a new licence. */
|
|
13
|
+
export interface VRM0Meta {
|
|
14
|
+
title?: string;
|
|
15
|
+
version?: string;
|
|
16
|
+
author?: string;
|
|
17
|
+
contactInformation?: string;
|
|
18
|
+
reference?: string;
|
|
19
|
+
texture?: number;
|
|
20
|
+
allowedUserName?: string;
|
|
21
|
+
violentUssageName?: string;
|
|
22
|
+
sexualUssageName?: string;
|
|
23
|
+
commercialUssageName?: string;
|
|
24
|
+
otherPermissionUrl?: string;
|
|
25
|
+
licenseName?: string;
|
|
26
|
+
otherLicenseUrl?: string;
|
|
27
|
+
[key: string]: unknown;
|
|
28
|
+
}
|
|
29
|
+
/** The published VRM 0.x extension. Unknown properties survive in `legacy`. */
|
|
30
|
+
export interface VRM0Data {
|
|
31
|
+
specVersion?: string;
|
|
32
|
+
meta?: VRM0Meta;
|
|
33
|
+
humanoid?: {
|
|
34
|
+
humanBones?: {
|
|
35
|
+
bone: string;
|
|
36
|
+
node: number;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}[];
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
};
|
|
41
|
+
blendShapeMaster?: {
|
|
42
|
+
blendShapeGroups?: {
|
|
43
|
+
name?: string;
|
|
44
|
+
presetName?: string;
|
|
45
|
+
isBinary?: boolean;
|
|
46
|
+
binds?: {
|
|
47
|
+
mesh: number;
|
|
48
|
+
index: number;
|
|
49
|
+
weight: number;
|
|
50
|
+
}[];
|
|
51
|
+
materialValues?: {
|
|
52
|
+
materialName: string;
|
|
53
|
+
propertyName: string;
|
|
54
|
+
targetValue: number[];
|
|
55
|
+
}[];
|
|
56
|
+
}[];
|
|
57
|
+
};
|
|
58
|
+
firstPerson?: {
|
|
59
|
+
firstPersonBone?: number;
|
|
60
|
+
firstPersonBoneOffset?: VRM0Vector;
|
|
61
|
+
meshAnnotations?: {
|
|
62
|
+
mesh: number;
|
|
63
|
+
firstPersonFlag?: string;
|
|
64
|
+
}[];
|
|
65
|
+
lookAtTypeName?: string;
|
|
66
|
+
lookAtHorizontalInner?: VRM0DegreeMap;
|
|
67
|
+
lookAtHorizontalOuter?: VRM0DegreeMap;
|
|
68
|
+
lookAtVerticalUp?: VRM0DegreeMap;
|
|
69
|
+
lookAtVerticalDown?: VRM0DegreeMap;
|
|
70
|
+
};
|
|
71
|
+
secondaryAnimation?: {
|
|
72
|
+
colliderGroups?: {
|
|
73
|
+
node: number;
|
|
74
|
+
colliders?: {
|
|
75
|
+
offset?: VRM0Vector;
|
|
76
|
+
radius?: number;
|
|
77
|
+
}[];
|
|
78
|
+
}[];
|
|
79
|
+
boneGroups?: {
|
|
80
|
+
comment?: string;
|
|
81
|
+
bones?: number[];
|
|
82
|
+
center?: number;
|
|
83
|
+
colliderGroups?: number[];
|
|
84
|
+
stiffiness?: number;
|
|
85
|
+
gravityPower?: number;
|
|
86
|
+
gravityDir?: VRM0Vector;
|
|
87
|
+
dragForce?: number;
|
|
88
|
+
hitRadius?: number;
|
|
89
|
+
}[];
|
|
90
|
+
};
|
|
91
|
+
materialProperties?: VRM0Material[];
|
|
92
|
+
[key: string]: unknown;
|
|
93
|
+
}
|
|
94
|
+
/** Legacy gaze curve. VRM 1.0 retains the end points and uses a linear range. */
|
|
95
|
+
export interface VRM0DegreeMap {
|
|
96
|
+
xRange?: number;
|
|
97
|
+
yRange?: number;
|
|
98
|
+
curve?: number[];
|
|
99
|
+
}
|
|
100
|
+
/** Legacy material parameters, retained for the optional MToon renderer. */
|
|
101
|
+
export interface VRM0Material {
|
|
102
|
+
name?: string;
|
|
103
|
+
shader?: string;
|
|
104
|
+
renderQueue?: number;
|
|
105
|
+
floatProperties?: Record<string, number>;
|
|
106
|
+
vectorProperties?: Record<string, number[]>;
|
|
107
|
+
textureProperties?: Record<string, number>;
|
|
108
|
+
keywordMap?: Record<string, boolean>;
|
|
109
|
+
tagMap?: Record<string, string>;
|
|
110
|
+
}
|
|
111
|
+
/** A copy of the glTF with a 1.0 runtime avatar. No source objects or binary chunks are modified. */
|
|
112
|
+
export interface VRM0Conversion {
|
|
113
|
+
document: VRMDocument;
|
|
114
|
+
avatar: VRMAvatar;
|
|
115
|
+
legacy: VRM0Data;
|
|
116
|
+
/** Differences that have no exact 1.0 representation, including legacy licence and gaze curves. */
|
|
117
|
+
differences: string[];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Converts the 0.x extension to the existing 1.0 runtime model, normalizing forward to +Z.
|
|
121
|
+
* Retains original metadata/materials in `legacy` and `avatar.extras.vrm0`; does not relicense a model.
|
|
122
|
+
* Gaze curves become 1.0 linear ranges, as the Consortium's migration specifies.
|
|
123
|
+
*/
|
|
124
|
+
export declare function convertVRM0(source: Uint8Array | VRMDocument): VRM0Conversion;
|
|
125
|
+
/** Convert a binary .vrm to a GLB carrying the normalized runtime document; preserves all non-JSON chunks. */
|
|
126
|
+
export declare function convertVRM0Bytes(source: Uint8Array): Uint8Array;
|
|
127
|
+
/**
|
|
128
|
+
* Loads a VRM 0.x URL/bytes as a PBRModel with a normalized 1.0 avatar in `model.asset.doc`.
|
|
129
|
+
* Uses gltf/pbr's bounded fetch and decoder options; `signal` cancels downloads and image decoding.
|
|
130
|
+
* Geometry keeps the file's glTF material fallback until the optional MToon renderer is attached.
|
|
131
|
+
*/
|
|
132
|
+
export declare function loadVRM0(engine: EngineKernel, source: string | URL | Uint8Array, options?: GLTFLoadOptions): Promise<PBRModel>;
|
|
133
|
+
/** Read the converted runtime avatar for `addVRM`; use `convertVRM0` when the converted document is also needed. */
|
|
134
|
+
export declare const readVRM0: (source: Uint8Array | VRMDocument) => VRMAvatar;
|