3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Check JOINTS_0 indices against the skin joint count on GLB export (#2381)
Nguyen Xuan An7c997200d3c6
diff --git a/changes/2381.md b/changes/2381.mdnew file mode 100644index 00000000..debbdcc3--- /dev/null+++ b/changes/2381.md@@ -0,0 +1 @@+- Reject a glTF export skin joint index at or past the skin joint count, naming the part, vertex and joint count (#2381).diff --git a/changes/2381.size.md b/changes/2381.size.mdnew file mode 100644index 00000000..ee9e40ce--- /dev/null+++ b/changes/2381.size.md@@ -0,0 +1,2 @@+- entry:core: the joint index check names the part, vertex and joint count, so a GLB with skinning that points at missing joints fails at export instead of loading wrongly elsewhere.+- entry:core+lazy: the same joint index check, counted again in the lazy build.diff --git a/src/export.js b/src/export.jsindex 6d93169e..62a50b35 100644--- a/src/export.js+++ b/src/export.js@@ -142,7 +142,16 @@ function validateOptions(options) {if (options.nodes !== undefined && !Array.isArray(options.nodes)) throw new TypeError('GLB export nodes must be an array.');}+// JOINTS_0 points into skin.joints; weight 0 padding slots may hold any index.+function validateJointIndices({ name, geometry: { skinIndices, skinWeights }, skin }) {+ const count = skin?.joints?.length;+ skinIndices?.forEach((joint, slot) => {+ if (count && skinWeights[slot] > 0 && !(joint < count)) throw new RangeError(`GLB part ${name} vertex ${slot >> 2} has joint index ${joint}, but its skin has ${count} joints.`);+ });+}+function validateSkinnedPart(part) {+ validateJointIndices(part);validateSkinGeometry(part.geometry, part.skin);}diff --git a/tests/export-roundtrip.test.js b/tests/export-roundtrip.test.jsindex e37fd259..fa92bd2f 100644--- a/tests/export-roundtrip.test.js+++ b/tests/export-roundtrip.test.js@@ -216,3 +216,32 @@ test('a clip that targets a missing node names the fix', () => {const clip = { duration: 1, loop: false, tracks: [{ target: 'ghost', property: 'translation', keyframes: [{ time: 0, value: [0, 0, 0] }] }] };assert.throws(() => exportGlb([part], { clips: [clip] }), /ghost.*options\.nodes/);});++function skinnedRod(jointNames, jointMatrixCount) {+ const geometry = box();+ const vertexCount = geometry.positions.length / 3;+ geometry.skinIndices = new Uint16Array(vertexCount * 4);+ geometry.skinWeights = new Float32Array(vertexCount * 4);+ for (let vertex = 0; vertex < vertexCount; vertex++) geometry.skinWeights[vertex * 4] = 1;+ const identityMatrices = count => {+ const matrices = new Float32Array(count * 16);+ for (let joint = 0; joint < count; joint++) mat4.identity(matrices.subarray(joint * 16, joint * 16 + 16));+ return matrices;+ };+ const joints = jointNames.map((name, index) => ({ name, parent: index - 1 }));+ const skin = { jointMatrices: identityMatrices(jointMatrixCount), joints, inverseBindMatrices: identityMatrices(joints.length), jointWorldMatrices: identityMatrices(joints.length) };+ return { name: 'Rod', geometry, matrix: mat4.create(), material: { color: [1, 1, 1], metallic: 0, roughness: 1 }, skin };+}++test('export names the part, vertex and joint count when a weighted joint index is past the skin joints', () => {+ const part = skinnedRod(['root', 'tip'], 3);+ assert.ok(exportGlb([part]).byteLength > 0);+ part.geometry.skinIndices[4] = 2;+ assert.throws(() => exportGlb([part]), /GLB part Rod vertex 1 has joint index 2.*2 joints/);+});++test('export ignores a joint index on a zero weight slot of a padded influence', () => {+ const part = skinnedRod(['root', 'tip'], 2);+ part.geometry.skinIndices[1] = 1;+ assert.ok(exportGlb([part]).byteLength > 0);+});