@starcried/starlite-vrm 0.1.0-alpha.2 → 0.1.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/prepare.cjs CHANGED
@@ -30,147 +30,259 @@ var import_promises = require("node:fs/promises");
30
30
  var import_node_path = require("node:path");
31
31
 
32
32
  // src/inspect.ts
33
+ var VrmInspectionError = class extends Error {
34
+ code;
35
+ details;
36
+ constructor(code, message, details = {}) {
37
+ super(message);
38
+ this.name = "VrmInspectionError";
39
+ this.code = code;
40
+ this.details = details;
41
+ }
42
+ };
43
+ var requiredBones = ["hips", "spine", "head", ...["left", "right"].flatMap((side) => ["UpperLeg", "LowerLeg", "Foot", "UpperArm", "LowerArm", "Hand"].map((part) => side + part))];
44
+ var optionalBones = ["chest", "upperChest", "neck", "jaw", ...["left", "right"].flatMap((side) => ["Eye", "Shoulder", "Toes", ...["ThumbMetacarpal", "ThumbProximal", "ThumbIntermediate", "ThumbDistal", ...["Index", "Middle", "Ring", "Little"].flatMap((finger) => ["Proximal", "Intermediate", "Distal"].map((part) => finger + part))]].map((part) => side + part))];
33
45
  var MAX_MODEL_BYTES = 64 * 1024 * 1024;
34
46
  var MAX_ANIMATION_BYTES = 16 * 1024 * 1024;
35
47
  function parseGlb(bytes, kind) {
48
+ try {
49
+ return parseGlbContents(bytes, kind);
50
+ } catch (error) {
51
+ if (error instanceof VrmInspectionError) throw error;
52
+ throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF structure; re-export a self-contained VRM/VRMA file");
53
+ }
54
+ }
55
+ function parseGlbContents(bytes, kind) {
36
56
  const limit = kind === "vrm" ? MAX_MODEL_BYTES : MAX_ANIMATION_BYTES;
37
- if (bytes.byteLength < 20 || bytes.byteLength > limit) throw new Error("invalid GLB size");
57
+ if (bytes.byteLength < 20 || bytes.byteLength > limit) throw new VrmInspectionError("INVALID_GLB", `invalid GLB size; ${kind.toUpperCase()} must be a binary glTF 2.0 file no larger than ${limit / 1024 / 1024} MiB`);
38
58
  const view = new DataView(bytes);
39
- if (view.getUint32(0, true) !== 1179937895 || view.getUint32(4, true) !== 2 || view.getUint32(8, true) !== bytes.byteLength) throw new Error("invalid GLB header");
59
+ if (view.getUint32(0, true) !== 1179937895 || view.getUint32(4, true) !== 2 || view.getUint32(8, true) !== bytes.byteLength) throw new VrmInspectionError("INVALID_GLB", "invalid GLB header; export binary glTF 2.0 VRM/VRMA, not FBX or JSON glTF");
40
60
  let json;
41
61
  let binSize = 0;
42
62
  let binStart = 0;
43
63
  let chunkCount = 0;
44
64
  for (let offset = 12; offset < bytes.byteLength; ) {
45
- if (offset + 8 > bytes.byteLength) throw new Error("truncated GLB chunk");
65
+ if (offset + 8 > bytes.byteLength) throw new VrmInspectionError("INVALID_GLB", "truncated GLB chunk; download or export the complete file again");
46
66
  const size = view.getUint32(offset, true), type = view.getUint32(offset + 4, true);
47
67
  offset += 8;
48
- if (size % 4 || size > bytes.byteLength - offset) throw new Error("invalid GLB chunk length");
68
+ if (size % 4 || size > bytes.byteLength - offset) throw new VrmInspectionError("INVALID_GLB", "invalid GLB chunk length; download or export the complete file again");
49
69
  if (chunkCount === 0 && type === 1313821514) {
50
- json = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array(bytes, offset, size)));
70
+ try {
71
+ json = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(new Uint8Array(bytes, offset, size)));
72
+ } catch {
73
+ throw new VrmInspectionError("INVALID_GLB", "invalid GLB JSON; re-export the file with a VRM exporter");
74
+ }
51
75
  } else if (chunkCount === 1 && type === 5130562) {
52
76
  binSize = size;
53
77
  binStart = offset;
54
- } else throw new Error("unexpected GLB chunk");
78
+ } else throw new VrmInspectionError("INVALID_GLB", "unexpected GLB chunk; export a self-contained VRM/VRMA file");
55
79
  offset += size;
56
80
  chunkCount++;
57
81
  }
58
- if (!json || json.asset?.version !== "2.0") throw new Error("glTF 2.0 is required");
82
+ if (!json || typeof json !== "object" || Array.isArray(json) || json.asset?.version !== "2.0") throw new VrmInspectionError("UNSUPPORTED_FORMAT", "glTF 2.0 is required; use a VRM/VRMA exporter");
59
83
  const expected = kind === "vrm" ? json.extensions?.VRM ?? json.extensions?.VRMC_vrm : json.extensions?.VRMC_vrm_animation;
60
- if (!expected) throw new Error(`missing ${kind} extension`);
61
- if (kind === "vrm" && json.extensions?.VRM && json.extensions.VRM.specVersion !== "0.0") throw new Error("unsupported VRM version");
62
- if (kind === "vrm" && json.extensions?.VRMC_vrm && json.extensions.VRMC_vrm.specVersion !== "1.0") throw new Error("unsupported VRM version");
63
- if (kind === "vrma" && expected.specVersion !== "1.0") throw new Error("VRMA 1.0 is required");
84
+ if (!expected) throw new VrmInspectionError("UNSUPPORTED_FORMAT", `missing ${kind} extension; export ${kind === "vrm" ? "VRM 0.0 or 1.0" : "VRMA 1.0"}, not a plain GLB`);
85
+ if (kind === "vrm" && json.extensions?.VRM && json.extensions.VRM.specVersion !== "0.0") throw new VrmInspectionError("UNSUPPORTED_VERSION", "unsupported VRM version; export VRM 0.0 or 1.0");
86
+ if (kind === "vrm" && json.extensions?.VRMC_vrm && json.extensions.VRMC_vrm.specVersion !== "1.0") throw new VrmInspectionError("UNSUPPORTED_VERSION", "unsupported VRM version; export VRM 0.0 or 1.0");
87
+ if (kind === "vrma" && expected.specVersion !== "1.0") throw new VrmInspectionError("UNSUPPORTED_VERSION", "VRMA 1.0 is required");
64
88
  const allowed = /* @__PURE__ */ new Set(["VRM", "VRMC_vrm", "VRMC_vrm_animation", "VRMC_materials_mtoon", "VRMC_springBone", "VRMC_node_constraint", "VRMC_materials_hdr_emissiveMultiplier", "KHR_materials_unlit", "KHR_texture_transform", "KHR_materials_emissive_strength"]);
65
- if ((json.extensionsRequired ?? []).some((name) => !allowed.has(name))) throw new Error("unsupported required glTF extension");
89
+ if ((json.extensionsRequired ?? []).some((name) => !allowed.has(name))) throw new VrmInspectionError("UNSUPPORTED_EXTENSION", "unsupported required glTF extension; re-export without geometry or texture compression extensions");
66
90
  const visit = (value, depth = 0) => {
67
- if (depth > 64) throw new Error("glTF nesting limit exceeded");
91
+ if (depth > 64) throw new VrmInspectionError("RESOURCE_LIMIT", "glTF nesting limit exceeded");
68
92
  if (!value || typeof value !== "object") return;
69
93
  for (const [key, child] of Object.entries(value)) {
70
- if (key === "uri") throw new Error("external glTF resources are not supported");
71
- if (typeof child === "number" && !Number.isFinite(child)) throw new Error("non-finite glTF value");
94
+ if (key === "uri") throw new VrmInspectionError("EXTERNAL_RESOURCE", "external glTF resources are not supported; embed all textures and buffers in one VRM/VRMA file");
95
+ if (typeof child === "number" && !Number.isFinite(child)) throw new VrmInspectionError("INVALID_STRUCTURE", "non-finite glTF value");
72
96
  visit(child, depth + 1);
73
97
  }
74
98
  };
75
99
  visit(json);
76
- if ((json.buffers?.length ?? 0) > 1 || (json.buffers?.[0]?.byteLength ?? 0) > binSize) throw new Error("invalid GLB buffer");
100
+ if ((json.buffers?.length ?? 0) > 1 || (json.buffers?.[0]?.byteLength ?? 0) > binSize) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid GLB buffer");
77
101
  for (const buffer of json.bufferViews ?? []) {
78
102
  const start = buffer.byteOffset ?? 0, length = buffer.byteLength;
79
- if (buffer.buffer !== 0 || !Number.isSafeInteger(start) || !Number.isSafeInteger(length) || start < 0 || length < 0 || start + length > (json.buffers?.[0]?.byteLength ?? 0)) throw new Error("invalid glTF buffer view");
103
+ if (buffer.buffer !== 0 || !Number.isSafeInteger(start) || !Number.isSafeInteger(length) || start < 0 || length < 0 || start + length > (json.buffers?.[0]?.byteLength ?? 0)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF buffer view");
80
104
  }
81
- if ((json.nodes?.length ?? 0) > 4096 || (json.accessors?.length ?? 0) > 32768) throw new Error("glTF resource count exceeded");
105
+ if ((json.nodes?.length ?? 0) > 4096 || (json.accessors?.length ?? 0) > 32768) throw new VrmInspectionError("RESOURCE_LIMIT", "glTF resource count exceeded");
82
106
  validateStructure(json, new DataView(bytes, binStart, binSize));
83
107
  return json;
84
108
  }
85
109
  function embeddedAnimationCatalog(json) {
86
110
  const animations = json.animations ?? [];
87
- if (!Array.isArray(animations) || animations.length > 64) throw new Error("at most 64 embedded animations are supported");
111
+ if (!Array.isArray(animations) || animations.length > 64) throw new VrmInspectionError("INVALID_ANIMATION", "at most 64 embedded animations are supported");
88
112
  const names = /* @__PURE__ */ new Set();
89
113
  return animations.map((animation, index) => {
90
114
  const name = animation.name ?? "Animation_" + (index + 1);
91
- if (typeof name !== "string" || !name.trim() || new TextEncoder().encode(name).length > 128 || /[\u0000-\u001f\u007f]/.test(name) || name === "StarLiteIdle" || names.has(name)) throw new Error("invalid or duplicate embedded animation name");
115
+ if (typeof name !== "string" || !name.trim() || new TextEncoder().encode(name).length > 128 || /[\u0000-\u001f\u007f]/.test(name) || name === "StarLiteIdle" || names.has(name)) throw new VrmInspectionError("INVALID_ANIMATION", "invalid or duplicate embedded animation name; use unique names of at most 128 UTF-8 bytes, excluding StarLiteIdle");
92
116
  names.add(name);
93
117
  let duration = 0;
94
118
  for (const sampler of animation.samplers ?? []) {
95
119
  const accessor = json.accessors?.[sampler.input];
96
- if (!accessor || accessor.type !== "SCALAR" || accessor.componentType !== 5126 || !Number.isFinite(accessor.max?.[0])) throw new Error("embedded animation has invalid animation times");
120
+ if (!accessor || accessor.type !== "SCALAR" || accessor.componentType !== 5126 || !Number.isFinite(accessor.max?.[0])) throw new VrmInspectionError("INVALID_ANIMATION", "embedded animation has invalid animation times");
97
121
  duration = Math.max(duration, accessor.max[0]);
98
122
  }
99
- if (!(duration > 0) || duration > 600) throw new Error("embedded animation duration must be between 0 and 600 seconds");
123
+ if (!(duration > 0) || duration > 600) throw new VrmInspectionError("INVALID_ANIMATION", "embedded animation duration must be between 0 and 600 seconds");
100
124
  return { name, durationMs: duration * 1e3 };
101
125
  });
102
126
  }
103
127
  function inspectVrm(bytes) {
104
128
  const json = parseGlb(bytes, "vrm");
105
129
  const v0 = json.extensions.VRM;
106
- const humanoid = (v0 ?? json.extensions.VRMC_vrm).humanoid?.humanBones;
107
- const bones = v0 ? Object.fromEntries((humanoid ?? []).map((b) => [b.bone, b])) : humanoid;
108
- const required = ["hips", "spine", "head", ...["left", "right"].flatMap((side) => ["UpperLeg", "LowerLeg", "Foot", "UpperArm", "LowerArm", "Hand"].map((part) => side + part))];
109
- const used = /* @__PURE__ */ new Set();
110
- for (const name2 of required) {
111
- const node = bones?.[name2]?.node;
112
- if (!Number.isSafeInteger(node) || !json.nodes?.[node] || used.has(node)) throw new Error("invalid required humanoid bone");
113
- used.add(node);
114
- }
130
+ const extension = v0 ?? json.extensions.VRMC_vrm;
131
+ if (Array.isArray(json.extensionsUsed) && !json.extensionsUsed.includes(v0 ? "VRM" : "VRMC_vrm")) throw new VrmInspectionError("UNSUPPORTED_FORMAT", "missing VRM extension declaration; re-export with the VRM exporter");
132
+ const bones = inspectHumanBones(json, extension.humanoid?.humanBones, Boolean(v0));
115
133
  const blinkNames = v0 ? ["blink", "blink_l", "blink_r"] : ["blink", "blinkLeft", "blinkRight"];
116
134
  const groups = v0?.blendShapeMaster?.blendShapeGroups;
117
- if (groups !== void 0 && !Array.isArray(groups)) throw new Error("invalid VRM blink expressions");
118
- const blinkPresets = v0 ? (groups ?? []).filter((item) => blinkNames.includes(item?.presetName)) : blinkNames.flatMap((name2) => {
119
- const expression = json.extensions.VRMC_vrm.expressions?.preset?.[name2];
120
- return expression === void 0 ? [] : [expression];
121
- });
122
- for (const expression of blinkPresets) {
123
- if (!expression || typeof expression !== "object" || Array.isArray(expression)) throw new Error("invalid VRM blink expression");
124
- const binds = v0 ? expression.binds : expression.morphTargetBinds;
125
- if (binds === void 0) continue;
126
- if (!Array.isArray(binds)) throw new Error("invalid VRM blink bindings");
127
- for (const bind of binds) {
128
- const mesh = v0 ? bind?.mesh : json.nodes?.[bind?.node]?.mesh;
129
- const primitives = json.meshes?.[mesh]?.primitives;
130
- if (!bind || !Number.isSafeInteger(mesh) || mesh < 0 || !v0 && (!Number.isSafeInteger(bind.node) || bind.node < 0) || !Number.isSafeInteger(bind.index) || bind.index < 0 || !Number.isFinite(bind.weight) || bind.weight < 0 || bind.weight > (v0 ? 100 : 1) || !Array.isArray(primitives) || !primitives.some((primitive) => primitive.targets?.[bind.index] !== void 0)) {
131
- throw new Error("invalid VRM blink morph binding");
132
- }
133
- }
135
+ if (groups !== void 0 && !Array.isArray(groups)) throw new VrmInspectionError("INVALID_BLINK", "invalid VRM blink expressions; re-export the blink presets");
136
+ const aliases = { a: "aa", e: "ee", i: "ih", o: "oh", u: "ou", joy: "happy", angry: "angry", sorrow: "sad", fun: "relaxed", neutral: "neutral", blink: "blink", blink_l: "blinkLeft", blink_r: "blinkRight", lookup: "lookUp", lookdown: "lookDown", lookleft: "lookLeft", lookright: "lookRight" };
137
+ const registeredName = (item) => aliases[item?.presetName] ?? item?.name;
138
+ const expression = (name2) => v0 ? (groups ?? []).find((item) => registeredName(item) === (aliases[name2] ?? name2)) : extension.expressions?.preset?.[name2];
139
+ for (const preset of blinkNames) {
140
+ const presets = v0 ? (groups ?? []).filter((item) => registeredName(item) === aliases[preset]) : expression(preset) === void 0 ? [] : [expression(preset)];
141
+ for (const item of presets) inspectMorphExpression(json, item, Boolean(v0), "INVALID_BLINK", preset);
134
142
  }
135
- const meta = (v0 ?? json.extensions.VRMC_vrm).meta ?? {};
143
+ const [combined, left, right] = blinkNames.map((preset) => inspectMorphExpression(json, expression(preset), Boolean(v0), "INVALID_BLINK", preset));
144
+ const meta = extension.meta ?? {};
136
145
  const name = v0 ? meta.title : meta.name;
137
146
  return {
138
147
  version: v0 ? "0" : "1",
139
- ...typeof name === "string" ? { name } : {},
148
+ ...typeof name === "string" ? { name: safeLabel(name) } : {},
140
149
  animations: embeddedAnimationCatalog(json),
141
- expressions: v0 ? (v0.blendShapeMaster?.blendShapeGroups ?? []).map((group) => group.name) : Object.keys({ ...json.extensions.VRMC_vrm.expressions?.preset, ...json.extensions.VRMC_vrm.expressions?.custom })
150
+ expressions: (v0 ? (groups ?? []).map((group) => group?.name) : Object.keys({ ...extension.expressions?.preset, ...extension.expressions?.custom })).filter((value) => typeof value === "string").map(safeLabel),
151
+ capabilities: {
152
+ humanoid: { requiredBones: [...requiredBones], optionalBones: optionalBones.filter((name2) => bones.has(name2)) },
153
+ blink: { automatic: Boolean(combined || left && right), combined: Boolean(combined), left: Boolean(left), right: Boolean(right) },
154
+ lookAt: inspectLookAt(json, extension, Boolean(v0), bones, expression)
155
+ },
156
+ metrics: inspectMetrics(json, bytes.byteLength)
157
+ };
158
+ }
159
+ function safeLabel(value) {
160
+ return value.replace(/[\u0000-\u001f\u007f]/g, "").slice(0, 128);
161
+ }
162
+ function inspectHumanBones(json, humanoid, v0) {
163
+ const known = /* @__PURE__ */ new Set([...requiredBones, ...optionalBones]), result = /* @__PURE__ */ new Map();
164
+ const invalid = [], duplicates = [], used = /* @__PURE__ */ new Map();
165
+ const entries = v0 ? Array.isArray(humanoid) ? humanoid.filter((item) => item && known.has(item.bone)).map((item) => [item.bone, item]) : [] : humanoid && typeof humanoid === "object" && !Array.isArray(humanoid) ? Object.entries(humanoid).filter(([name]) => known.has(name)) : [];
166
+ for (const [name, value] of entries) {
167
+ const node = value?.node;
168
+ if (result.has(name)) duplicates.push(name);
169
+ if (!Number.isSafeInteger(node) || node < 0 || !json.nodes?.[node]) {
170
+ invalid.push(name);
171
+ continue;
172
+ }
173
+ const previous = used.get(node);
174
+ if (previous && previous !== name) duplicates.push(previous, name);
175
+ used.set(node, name);
176
+ result.set(name, node);
177
+ }
178
+ const missing = requiredBones.filter((name) => !result.has(name) && !invalid.includes(name));
179
+ if (missing.length || invalid.length || duplicates.length) {
180
+ const details = { missingBones: missing, invalidBones: [...new Set(invalid)], duplicateBones: [...new Set(duplicates)] };
181
+ const summary = [missing.length ? `missing ${missing.join(", ")}` : "", invalid.length ? `invalid node for ${details.invalidBones.join(", ")}` : "", duplicates.length ? `duplicate mapping for ${details.duplicateBones.join(", ")}` : ""].filter(Boolean).join("; ");
182
+ throw new VrmInspectionError("HUMANOID_BONES", `invalid required humanoid bone: ${summary}; assign separate human bones in the VRM exporter and export again`, details);
183
+ }
184
+ return result;
185
+ }
186
+ function inspectMorphExpression(json, expression, v0, code, preset) {
187
+ if (expression === void 0) return false;
188
+ const label = code === "INVALID_BLINK" ? "blink" : "look-at";
189
+ if (!expression || typeof expression !== "object" || Array.isArray(expression)) throw new VrmInspectionError(code, `invalid VRM ${label} expression; re-export the ${preset} preset`, { preset });
190
+ const binds = v0 ? expression.binds : expression.morphTargetBinds;
191
+ if (binds === void 0) return false;
192
+ if (!Array.isArray(binds)) throw new VrmInspectionError(code, `invalid VRM ${label} bindings; re-export the ${preset} preset`, { preset });
193
+ let usable = false;
194
+ for (const bind of binds) {
195
+ const mesh = v0 ? bind?.mesh : json.nodes?.[bind?.node]?.mesh;
196
+ const primitives = json.meshes?.[mesh]?.primitives;
197
+ if (!bind || !Number.isSafeInteger(mesh) || mesh < 0 || !v0 && (!Number.isSafeInteger(bind.node) || bind.node < 0) || !Number.isSafeInteger(bind.index) || bind.index < 0 || !Number.isFinite(bind.weight) || bind.weight < 0 || bind.weight > (v0 ? 100 : 1) || !Array.isArray(primitives) || !primitives.length || !primitives.every((primitive) => primitive?.targets?.[bind.index] !== void 0)) {
198
+ throw new VrmInspectionError(code, `invalid VRM ${label} morph binding: ${preset}; check its mesh/node, morph target index and weight in the VRM exporter`, { preset });
199
+ }
200
+ const instantiated = !v0 || (json.nodes ?? []).some((node) => node?.mesh === mesh);
201
+ const morphs = primitives.every((primitive) => primitive.targets.some((target) => ["POSITION", "NORMAL", "COLOR_0"].some((semantic) => Number.isSafeInteger(target?.[semantic]) && json.accessors?.[target[semantic]])));
202
+ usable ||= bind.weight > 0 && instantiated && morphs;
203
+ }
204
+ return usable;
205
+ }
206
+ function inspectLookAt(json, extension, v0, bones, expression) {
207
+ const lookAt = v0 ? extension.firstPerson : extension.lookAt;
208
+ const empty = { mode: "none", head: true, eyes: false, horizontal: false, vertical: false };
209
+ if (lookAt === void 0) return empty;
210
+ if (!lookAt || typeof lookAt !== "object" || Array.isArray(lookAt)) throw new VrmInspectionError("INVALID_LOOK_AT", "invalid VRM look-at configuration; re-export eye tracking");
211
+ const expressionManager = v0 ? extension.blendShapeMaster : extension.expressions;
212
+ if (!expressionManager) return empty;
213
+ const type = v0 ? lookAt.lookAtTypeName : lookAt.type;
214
+ if (type !== void 0 && !(v0 ? ["Bone", "BlendShape"] : ["bone", "expression"]).includes(type)) throw new VrmInspectionError("INVALID_LOOK_AT", "invalid VRM look-at type; use bone or expression eye tracking in the VRM exporter");
215
+ const mode = type === (v0 ? "BlendShape" : "expression") ? "expression" : "bone";
216
+ const scale = (name) => {
217
+ const range = lookAt[name], value = range?.[v0 ? "yRange" : "outputScale"] ?? (mode === "expression" ? 1 : 10);
218
+ const input = range?.[v0 ? "xRange" : "inputMaxValue"] ?? 90;
219
+ if (range !== void 0 && (!range || typeof range !== "object" || Array.isArray(range)) || !Number.isFinite(value) || value < 0 || !Number.isFinite(input) || input < 0) throw new VrmInspectionError("INVALID_LOOK_AT", "invalid VRM look-at range; use finite, non-negative input and output ranges");
220
+ return value;
142
221
  };
222
+ const horizontalRange = scale(v0 ? "lookAtHorizontalOuter" : "rangeMapHorizontalOuter");
223
+ const innerRange = scale(v0 ? "lookAtHorizontalInner" : "rangeMapHorizontalInner");
224
+ const upRange = scale(v0 ? "lookAtVerticalUp" : "rangeMapVerticalUp"), downRange = scale(v0 ? "lookAtVerticalDown" : "rangeMapVerticalDown");
225
+ let horizontal, vertical;
226
+ if (mode === "bone") {
227
+ const eyes = bones.has("leftEye") || bones.has("rightEye");
228
+ horizontal = eyes && (horizontalRange > 0 || innerRange > 0);
229
+ vertical = eyes && (upRange > 0 || downRange > 0);
230
+ } else {
231
+ const usable = (name) => inspectMorphExpression(json, expression(name), v0, "INVALID_LOOK_AT", name);
232
+ const left = usable(v0 ? "lookleft" : "lookLeft"), right = usable(v0 ? "lookright" : "lookRight"), up = usable(v0 ? "lookup" : "lookUp"), down = usable(v0 ? "lookdown" : "lookDown");
233
+ horizontal = horizontalRange > 0 && left && right;
234
+ vertical = upRange > 0 && downRange > 0 && up && down;
235
+ }
236
+ return { mode, head: true, eyes: horizontal || vertical, horizontal, vertical };
237
+ }
238
+ function inspectMetrics(json, fileBytes) {
239
+ const primitives = (json.meshes ?? []).flatMap((mesh) => Array.isArray(mesh?.primitives) ? mesh.primitives : []);
240
+ let vertexCount = 0, triangleCount = 0, morphTargetCount = 0;
241
+ for (const primitive of primitives) {
242
+ const vertices = json.accessors?.[primitive?.attributes?.POSITION]?.count ?? 0;
243
+ const indices = primitive?.indices === void 0 ? vertices : json.accessors?.[primitive.indices]?.count ?? 0;
244
+ vertexCount += vertices;
245
+ if (primitive?.mode === void 0 || primitive.mode === 4) triangleCount += Math.floor(indices / 3);
246
+ else if (primitive.mode === 5 || primitive.mode === 6) triangleCount += Math.max(0, indices - 2);
247
+ morphTargetCount += Array.isArray(primitive?.targets) ? primitive.targets.length : 0;
248
+ }
249
+ const widths = { 5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4 }, components = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 };
250
+ const decodedAccessorBytes = (json.accessors ?? []).reduce((sum, accessor) => {
251
+ const width = widths[accessor.componentType], dimension = accessor.type === "MAT2" ? 2 : accessor.type === "MAT3" ? 3 : accessor.type === "MAT4" ? 4 : 0;
252
+ return sum + accessor.count * (dimension ? Math.ceil(dimension * width / 4) * 4 * dimension : width * components[accessor.type]);
253
+ }, 0);
254
+ return { fileBytes, nodeCount: json.nodes?.length ?? 0, meshCount: json.meshes?.length ?? 0, primitiveCount: primitives.length, materialCount: json.materials?.length ?? 0, textureCount: json.textures?.length ?? 0, imageCount: json.images?.length ?? 0, vertexCount, triangleCount, morphTargetCount, decodedAccessorBytes };
143
255
  }
144
256
  function inspectVrma(bytes) {
145
257
  const json = parseGlb(bytes, "vrma");
146
- if (json.animations?.length !== 1) throw new Error("each VRMA file must contain exactly one animation");
258
+ if (json.animations?.length !== 1) throw new VrmInspectionError("INVALID_STRUCTURE", "each VRMA file must contain exactly one animation");
147
259
  let duration = 0;
148
260
  for (const sampler of json.animations[0].samplers ?? []) {
149
261
  const accessor = json.accessors?.[sampler.input];
150
- if (!accessor || accessor.type !== "SCALAR" || accessor.componentType !== 5126 || !Number.isFinite(accessor.max?.[0])) throw new Error("VRMA has invalid animation times");
262
+ if (!accessor || accessor.type !== "SCALAR" || accessor.componentType !== 5126 || !Number.isFinite(accessor.max?.[0])) throw new VrmInspectionError("INVALID_STRUCTURE", "VRMA has invalid animation times");
151
263
  duration = Math.max(duration, accessor.max[0]);
152
264
  }
153
- if (!(duration > 0) || duration > 600) throw new Error("VRMA duration must be between 0 and 600 seconds");
265
+ if (!(duration > 0) || duration > 600) throw new VrmInspectionError("INVALID_STRUCTURE", "VRMA duration must be between 0 and 600 seconds");
154
266
  return { durationMs: duration * 1e3 };
155
267
  }
156
268
  function validateStructure(json, binary) {
157
- for (const key of ["nodes", "accessors", "bufferViews", "buffers", "meshes", "images", "animations", "skins", "scenes"]) {
158
- if (json[key] !== void 0 && !Array.isArray(json[key])) throw new Error(`invalid glTF ${key}`);
269
+ for (const key of ["nodes", "accessors", "bufferViews", "buffers", "meshes", "materials", "textures", "images", "animations", "skins", "scenes", "extensionsUsed", "extensionsRequired"]) {
270
+ if (json[key] !== void 0 && !Array.isArray(json[key])) throw new VrmInspectionError("INVALID_STRUCTURE", `invalid glTF ${key}`);
159
271
  }
160
272
  const views = json.bufferViews ?? [], nodes = json.nodes ?? [], accessors = json.accessors ?? [];
161
273
  const integer = (n, min, max) => Number.isSafeInteger(n) && n >= min && n <= max;
162
274
  const index = (n, array) => integer(n, 0, array.length - 1);
163
275
  const seen = /* @__PURE__ */ new Set(), active = /* @__PURE__ */ new Set(), parents = /* @__PURE__ */ new Set();
164
276
  const walk = (i, depth) => {
165
- if (!index(i, nodes) || active.has(i) || depth > 128) throw new Error("invalid glTF node hierarchy");
277
+ if (!index(i, nodes) || active.has(i) || depth > 128) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF node hierarchy");
166
278
  if (seen.has(i)) return;
167
279
  const node = nodes[i];
168
- if (!node || typeof node !== "object") throw new Error("invalid glTF node");
280
+ if (!node || typeof node !== "object") throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF node");
169
281
  seen.add(i);
170
282
  active.add(i);
171
- if (node.children !== void 0 && !Array.isArray(node.children)) throw new Error("invalid node children");
283
+ if (node.children !== void 0 && !Array.isArray(node.children)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid node children");
172
284
  for (const child of node.children ?? []) {
173
- if (parents.has(child)) throw new Error("node has multiple parents");
285
+ if (parents.has(child)) throw new VrmInspectionError("INVALID_STRUCTURE", "node has multiple parents");
174
286
  parents.add(child);
175
287
  walk(child, depth + 1);
176
288
  }
@@ -181,21 +293,21 @@ function validateStructure(json, binary) {
181
293
  const sizes = { 5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4 };
182
294
  let decoded = 0;
183
295
  const span = (viewIndex, offset, count, element, alignment, strided = false) => {
184
- if (!index(viewIndex, views)) throw new Error("invalid accessor buffer view");
296
+ if (!index(viewIndex, views)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid accessor buffer view");
185
297
  const v = views[viewIndex], stride = strided ? v.byteStride ?? element : element;
186
- if (!integer(offset, 0, v.byteLength) || offset % alignment || !integer(stride, element, 256) || stride % alignment || offset + (count - 1) * stride + element > v.byteLength) throw new Error("accessor exceeds buffer view");
298
+ if (!integer(offset, 0, v.byteLength) || offset % alignment || !integer(stride, element, 256) || stride % alignment || offset + (count - 1) * stride + element > v.byteLength) throw new VrmInspectionError("INVALID_STRUCTURE", "accessor exceeds buffer view");
187
299
  return (v.byteOffset ?? 0) + offset;
188
300
  };
189
301
  for (const a of accessors) {
190
302
  const width = sizes[a?.componentType], count = a?.count, componentsCount = components[a?.type];
191
- if (!width || !componentsCount || !integer(count, 1, 2e6)) throw new Error("invalid accessor allocation");
303
+ if (!width || !componentsCount || !integer(count, 1, 2e6)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid accessor allocation");
192
304
  const dimension = a.type === "MAT2" ? 2 : a.type === "MAT3" ? 3 : a.type === "MAT4" ? 4 : 0;
193
305
  const element = dimension ? Math.ceil(dimension * width / 4) * 4 * dimension : width * componentsCount;
194
306
  decoded += element * count;
195
- if (decoded > 256 * 1024 * 1024) throw new Error("decoded geometry exceeds 256 MiB");
307
+ if (decoded > 256 * 1024 * 1024) throw new VrmInspectionError("RESOURCE_LIMIT", "decoded geometry exceeds 256 MiB");
196
308
  const checkFloats = (at, n, stride) => {
197
309
  if (a.componentType !== 5126) return;
198
- for (let k = 0; k < n; k++) for (let c = 0; c < componentsCount; c++) if (!Number.isFinite(binary.getFloat32(at + k * stride + c * 4, true))) throw new Error("non-finite accessor value");
310
+ for (let k = 0; k < n; k++) for (let c = 0; c < componentsCount; c++) if (!Number.isFinite(binary.getFloat32(at + k * stride + c * 4, true))) throw new VrmInspectionError("INVALID_STRUCTURE", "non-finite accessor value");
199
311
  };
200
312
  if (a.bufferView !== void 0) {
201
313
  const at = span(a.bufferView, a.byteOffset ?? 0, count, element, width, true);
@@ -203,7 +315,7 @@ function validateStructure(json, binary) {
203
315
  }
204
316
  if (a.sparse) {
205
317
  const sparse = a.sparse, n = sparse.count, iw = sizes[sparse.indices?.componentType];
206
- if (!integer(n, 1, count) || ![5121, 5123, 5125].includes(sparse.indices?.componentType)) throw new Error("invalid sparse accessor");
318
+ if (!integer(n, 1, count) || ![5121, 5123, 5125].includes(sparse.indices?.componentType)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid sparse accessor");
207
319
  const at = span(sparse.indices.bufferView, sparse.indices.byteOffset ?? 0, n, iw, iw);
208
320
  const valuesAt = span(sparse.values?.bufferView, sparse.values?.byteOffset ?? 0, n, element, width);
209
321
  checkFloats(valuesAt, n, element);
@@ -211,29 +323,54 @@ function validateStructure(json, binary) {
211
323
  for (let k = 0; k < n; k++) {
212
324
  const offset = at + k * iw;
213
325
  const next = iw === 1 ? binary.getUint8(offset) : iw === 2 ? binary.getUint16(offset, true) : binary.getUint32(offset, true);
214
- if (next <= previous || next >= count) throw new Error("invalid sparse index");
326
+ if (next <= previous || next >= count) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid sparse index");
215
327
  previous = next;
216
328
  }
217
329
  }
218
330
  }
331
+ let primitiveCount = 0;
332
+ for (const mesh of json.meshes ?? []) {
333
+ if (!mesh || !Array.isArray(mesh.primitives) || !mesh.primitives.length) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF mesh primitives");
334
+ primitiveCount += mesh.primitives.length;
335
+ if (primitiveCount > 32768) throw new VrmInspectionError("RESOURCE_LIMIT", "glTF primitive count exceeded");
336
+ for (const primitive of mesh.primitives) {
337
+ const position = accessors[primitive?.attributes?.POSITION];
338
+ if (!primitive || !position || position.type !== "VEC3" || position.componentType !== 5126) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF primitive POSITION accessor");
339
+ if (primitive.indices !== void 0) {
340
+ const indices = accessors[primitive.indices];
341
+ if (!index(primitive.indices, accessors) || indices.type !== "SCALAR" || ![5121, 5123, 5125].includes(indices.componentType)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF primitive index accessor");
342
+ }
343
+ if (primitive.mode !== void 0 && !integer(primitive.mode, 0, 6)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF primitive mode");
344
+ if (primitive.targets !== void 0 && (!Array.isArray(primitive.targets) || primitive.targets.length > 1024)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF morph target list");
345
+ for (const target of primitive.targets ?? []) {
346
+ if (!target || typeof target !== "object" || Array.isArray(target)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF morph target");
347
+ for (const semantic of ["POSITION", "NORMAL", "COLOR_0"]) {
348
+ if (target[semantic] === void 0) continue;
349
+ const accessor = accessors[target[semantic]];
350
+ if (!index(target[semantic], accessors) || accessor.componentType !== 5126 || !(semantic === "COLOR_0" ? ["VEC3", "VEC4"] : ["VEC3"]).includes(accessor.type) || accessor.count !== position.count) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF morph target accessor");
351
+ }
352
+ }
353
+ }
354
+ }
355
+ for (const node of nodes) if (node.mesh !== void 0 && !index(node.mesh, json.meshes ?? [])) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid glTF node mesh reference");
219
356
  for (const animation of json.animations ?? []) {
220
- if (!Array.isArray(animation.samplers) || !Array.isArray(animation.channels)) throw new Error("invalid animation");
357
+ if (!Array.isArray(animation.samplers) || !Array.isArray(animation.channels)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid animation");
221
358
  for (const channel of animation.channels) {
222
- if (!index(channel.sampler, animation.samplers) || !index(channel.target?.node, nodes) || !["translation", "rotation", "scale", "weights"].includes(channel.target?.path)) throw new Error("invalid animation channel");
359
+ if (!index(channel.sampler, animation.samplers) || !index(channel.target?.node, nodes) || !["translation", "rotation", "scale", "weights"].includes(channel.target?.path)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid animation channel");
223
360
  }
224
361
  for (const sampler of animation.samplers) {
225
- if (!index(sampler.input, accessors) || !index(sampler.output, accessors)) throw new Error("invalid animation accessor");
362
+ if (!index(sampler.input, accessors) || !index(sampler.output, accessors)) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid animation accessor");
226
363
  const a = accessors[sampler.input];
227
- if (a.type !== "SCALAR" || a.componentType !== 5126 || a.sparse || a.bufferView === void 0) throw new Error("invalid animation timeline");
364
+ if (a.type !== "SCALAR" || a.componentType !== 5126 || a.sparse || a.bufferView === void 0) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid animation timeline");
228
365
  const start = span(a.bufferView, a.byteOffset ?? 0, a.count, 4, 4);
229
366
  let previous = -1;
230
367
  for (let i = 0; i < a.count; i++) {
231
368
  const time = binary.getFloat32(start + i * 4, true);
232
- if (!Number.isFinite(time) || time <= previous || time < 0 || time > 600) throw new Error("invalid animation times");
369
+ if (!Number.isFinite(time) || time <= previous || time < 0 || time > 600) throw new VrmInspectionError("INVALID_STRUCTURE", "invalid animation times");
233
370
  previous = time;
234
371
  }
235
- if (!Array.isArray(a.max) || Math.abs(a.max[0] - previous) > 1e-3) throw new Error("animation duration does not match timeline");
236
- if (sampler.interpolation !== void 0 && !["LINEAR", "STEP", "CUBICSPLINE"].includes(sampler.interpolation)) throw new Error("unsupported animation interpolation");
372
+ if (!Array.isArray(a.max) || Math.abs(a.max[0] - previous) > 1e-3) throw new VrmInspectionError("INVALID_STRUCTURE", "animation duration does not match timeline");
373
+ if (sampler.interpolation !== void 0 && !["LINEAR", "STEP", "CUBICSPLINE"].includes(sampler.interpolation)) throw new VrmInspectionError("INVALID_STRUCTURE", "unsupported animation interpolation");
237
374
  }
238
375
  }
239
376
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { DialogueSceneConfig, ModelController } from "@starcried/starlite-core";
1
+ import type { DialogueSceneConfig, ModelController, PerformanceConfig } from "@starcried/starlite-core";
2
2
  export type { ModelController, AnimationPlaybackResult, AnimationPlaybackOptions } from "@starcried/starlite-core";
3
3
  export interface VrmAnimationSource {
4
4
  name: string;
@@ -9,6 +9,22 @@ export interface BonePose {
9
9
  rotation: [number, number, number, number];
10
10
  position?: [number, number, number];
11
11
  }
12
+ /** Per-model calibration. Omitted values preserve the original pose and gaze. */
13
+ export interface ModelAdjustments {
14
+ idle?: {
15
+ armLiftDegrees?: number;
16
+ palmTwistDegrees?: number;
17
+ wristLiftDegrees?: number;
18
+ /** 0 opens mapped fingers; 1 closes them. Omit to keep the authored hands. */
19
+ handCurl?: number;
20
+ elbowBendDegrees?: number;
21
+ };
22
+ gaze?: {
23
+ headStrength?: number;
24
+ eyeStrength?: number;
25
+ smoothingMs?: number;
26
+ };
27
+ }
12
28
  /** Data only: imported profiles cannot execute scripts. All rotations use glTF coordinates. */
13
29
  export interface CharacterProfile {
14
30
  schemaVersion: 1;
@@ -34,6 +50,8 @@ export interface CharacterProfile {
34
50
  }
35
51
  export interface VrmWidgetConfig {
36
52
  backend?: "vrm";
53
+ adjustments?: ModelAdjustments;
54
+ performance?: PerformanceConfig;
37
55
  model: string;
38
56
  fallback: string;
39
57
  animations?: readonly VrmAnimationSource[];
@@ -140,6 +158,7 @@ export interface VrmDiagnostics {
140
158
  }[];
141
159
  }
142
160
  export interface VrmController extends ModelController {
161
+ setModelAdjustments(adjustments: ModelAdjustments): void;
143
162
  diagnostics(): VrmDiagnostics;
144
163
  captureFrame(): string;
145
164
  getExpressions(): readonly string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@starcried/starlite-vrm",
3
- "version": "0.1.0-alpha.2",
3
+ "version": "0.1.0-alpha.3",
4
4
  "type": "module",
5
5
  "license": "BSD-2-Clause",
6
6
  "files": [
@@ -40,7 +40,7 @@
40
40
  "dependencies": {
41
41
  "@pixiv/three-vrm": "3.5.5",
42
42
  "@pixiv/three-vrm-animation": "3.5.5",
43
- "@starcried/starlite-core": "0.1.0-alpha.2",
43
+ "@starcried/starlite-core": "0.1.0-alpha.3",
44
44
  "three": "0.186.0"
45
45
  },
46
46
  "devDependencies": {