3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Count a skin's joints once per skin, bound pose copies by bytes (#2259)
Nguyen Xuan An96cb12099f6a
diff --git a/changes/2259.md b/changes/2259.mdnew file mode 100644index 00000000..4cf80abe--- /dev/null+++ b/changes/2259.md@@ -0,0 +1 @@+- A skin worn by many primitives now counts its joints once against `maxJoints`, so a 153-joint rig on 12 primitives loads through `scene_create` instead of being refused at 1860; the memory the pose copies hold is bounded by `maxDecodedBytes` (#2259).diff --git a/changes/2259.size.md b/changes/2259.size.mdnew file mode 100644index 00000000..7f054aa9--- /dev/null+++ b/changes/2259.size.md@@ -0,0 +1 @@+- goal:gltf-box: +10 bytes gzip (98702 to 98712) fixes the must bug #2259, where a normal rigged character was refused; the check moves from a joint count per primitive to a byte bound that is enforced the same way.diff --git a/docs/gltf.md b/docs/gltf.mdindex 5151fb02..f249c20d 100644--- a/docs/gltf.md+++ b/docs/gltf.md@@ -37,13 +37,13 @@ The optional `signal` aborts those fetches.| Option | Default | Limit || --- | ---: | --- || `maxBytes` | 268,435,456 | Total encoded input and fetched external resources. |-| `maxDecodedBytes` | 1,073,741,824 | Decoded accessor bytes, estimated from the accessor counts before anything is decoded. Left unset while `maxBytes` is set, it is four times `maxBytes`. |+| `maxDecodedBytes` | 1,073,741,824 | Decoded accessor bytes and skinned-primitive pose copies (448 bytes per joint, one copy per skinned primitive), estimated from the counts before anything is decoded. Left unset while `maxBytes` is set, it is four times `maxBytes`. || `maxVertices` | 8,000,000 | Declared and decoded geometry vertices. Indices have a separate ceiling of twelve times this value. || `maxParts` | 65,536 | Declared nodes and decoded mesh parts. || `maxResources` | 256 | Unique resource URIs. || `maxTexturePixels` | 16,777,216 | Combined texture image pixels, checked from the file headers before decoding. Over the budget, the largest images are halved together until they fit, with base color and emission maps kept at twice the size of the normal, roughness and occlusion maps, and `asset.scaledTextures` lists each one as `{ name, from, to }` (also summarised in `asset.warnings`). || `maxTextureImagePixels` | 67,108,864 | Hard ceiling for one image, checked before decoding. Larger images are rejected, never scaled. |-| `maxJoints` | 4,096 | Combined declared joints and joint palettes for instantiated meshes. |+| `maxJoints` | 4,096 | Joints declared by all skins; a skin worn by many primitives counts once. Pose copies count against `maxDecodedBytes`. || `maxInstances` | 65,536 | Instances of one `EXT_mesh_gpu_instancing` node. |Every numeric limit must be a positive safe integer.diff --git a/docs/skinning.md b/docs/skinning.mdindex 6ea4f44b..7a9f977e 100644--- a/docs/skinning.md+++ b/docs/skinning.md@@ -106,7 +106,8 @@ Texture dimensions must fit `MAX_TEXTURE_SIZE`.Both RGBA32F textures, including padding, contribute to the renderer's existing texture memory budget.Palette textures use 64 bytes per joint before padding. Influence textures use 32 bytes per vertex before padding.-The importer limits declared joints and the combined joint count of instantiated poses through `maxJoints`, which defaults to `4096`.+The importer limits the joints declared by each skin through `maxJoints`, which defaults to `4096`. A skin worn by many primitives counts once.+Each skinned primitive keeps its own pose copy, 448 bytes per joint, and those copies count against `maxDecodedBytes` with the decoded accessors, so many primitives on a large skin fail on bytes, not on joints.Existing byte, vertex, part, image, and resource limits remain active.Palette creation supports at most `65536` joints, subject to GPU texture capacity and memory budgets.diff --git a/src/gltf.js b/src/gltf.jsindex 82587da3..1e9c5c45 100644--- a/src/gltf.js+++ b/src/gltf.js@@ -11,7 +11,7 @@ import { readPunctualLights } from './gltf-lights.js';import { validateDocumentReferences } from './gltf-references.js';import { Matrix4 } from './gltf-nodes.js';import { textureDescriptorInfo } from './renderer.js';-import { createSkinPose, matrixDeterminant, updateSkinPose, validateSkinGeometry, validateSkinPose } from './skinning.js';+import { SKIN_POSE_BYTES_PER_JOINT, createSkinPose, matrixDeterminant, updateSkinPose, validateSkinGeometry, validateSkinPose } from './skinning.js';import { MAX_STORED_MORPH_TARGETS, createMorphPose, validateMorphGeometry, validateMorphPose, validateMorphTransform } from './morph.js';// The defaults fit one large exported scene (a 240 MB GLB of 8 million vertices with 8K textures) and still bound a hostile file.@@ -429,7 +429,7 @@ function validateDocument(document, limits, allowedExtensions) {}}overLimit(overruns, 'skin joints', jointCount, 'maxJoints', limits.maxJoints);- let poseJointCount = 0;+ let poseBytes = 0;let declaredParts = 0;for (const node of document.nodes ?? []) {const mesh = document.meshes?.[node.mesh];@@ -442,12 +442,11 @@ function validateDocument(document, limits, allowedExtensions) {for (const primitive of document.meshes[node.mesh].primitives ?? []) {if (isLinePrimitive(primitive)) continue;if (primitive.attributes?.JOINTS_0 === undefined || primitive.attributes?.WEIGHTS_0 === undefined) throw new Error('Skinned meshes require JOINTS_0 and WEIGHTS_0.');- poseJointCount += document.skins[node.skin].joints.length;+ poseBytes += document.skins[node.skin].joints.length * SKIN_POSE_BYTES_PER_JOINT;}}}overLimit(overruns, 'parts', declaredParts, 'maxParts', limits.maxParts);- overLimit(overruns, 'skin pose joints', poseJointCount, 'maxJoints', limits.maxJoints);validateHierarchy(document.nodes ?? []);validateAnimationChannels(document);for (const material of document.materials ?? []) {@@ -494,7 +493,8 @@ function validateDocument(document, limits, allowedExtensions) {const meshoptViewBytes = sumMeshoptViewBytes(document);overLimit(overruns, 'decoded meshopt view bytes', meshoptViewBytes, 'maxDecodedBytes', limits.maxDecodedBytes);// Meshopt views and accessors share one decoded budget, so a file cannot hold both at the full allowance.- let decodedBytes = morphBytes + meshoptViewBytes;+ // Each skinned primitive keeps its own pose copy, so the pose memory shares this budget. maxJoints counts a skin once above.+ let decodedBytes = morphBytes + meshoptViewBytes + poseBytes;const components = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 };for (const accessor of document.accessors ?? []) {if (!Number.isSafeInteger(accessor.count) || accessor.count < 0) throw new Error('Invalid glTF accessor count.');@@ -504,7 +504,7 @@ function validateDocument(document, limits, allowedExtensions) {if (!components[accessor.type]) throw new Error('Unsupported glTF accessor shape.');decodedBytes += accessor.count * components[accessor.type] * 4;}- const decodedLabel = meshoptViewBytes > 0 ? 'decoded accessor and meshopt view bytes' : 'decoded accessor bytes';+ const decodedLabel = meshoptViewBytes > 0 ? 'decoded accessor and meshopt view bytes' : poseBytes > 0 ? 'decoded accessor and skin pose bytes' : 'decoded accessor bytes';overLimit(overruns, decodedLabel, decodedBytes, 'maxDecodedBytes', limits.maxDecodedBytes);return { vertices, ignoredExtensions, overruns };}diff --git a/src/skinning.js b/src/skinning.jsindex 525959ed..20006746 100644--- a/src/skinning.js+++ b/src/skinning.js@@ -66,6 +66,10 @@ function validateMatrices(values, count, label) {}}+// CPU bytes one imported skinned primitive keeps per joint: seven 4 x 4 float matrices. They are the live and the pending+// pose (a palette and its workspace copy each), the inverse bind matrix, and the live and the pending joint world matrix.+export const SKIN_POSE_BYTES_PER_JOINT = 7 * 16 * 4;+/** Create a retained mesh-local joint palette. Shared pose identity shares a GPU palette. */export function createSkinPose(jointCount) {if (!Number.isSafeInteger(jointCount) || jointCount < 1 || jointCount > 65536) throw new Error('Joint count must be within one and 65536.');diff --git a/tests/gltf.test.js b/tests/gltf.test.jsindex 92b531f6..625b2e92 100644--- a/tests/gltf.test.js+++ b/tests/gltf.test.js@@ -5,6 +5,7 @@ import { Matrix4, Vector3 } from 'three';import { skinnedStripAsset } from './fixtures/skinned-strip.js';import { mat4 } from 'gl-matrix';import { exportGlb } from '../src/export.js';+import { AGENT_ASSET_LIMITS, agentAssetDecodeOptions } from '../src/agent-asset-limits.js';import { GLTF_CAPABILITIES, loadGltf, parseGltf } from '../src/gltf.js';function triangleAsset(edit = () => {}) {@@ -580,10 +581,49 @@ test('skin preflight guards joints, influence shapes, raw zero weights, indices];for (const [edit, expected] of cases) await assert.rejects(parseGltf(skinnedStripAsset(edit)), expected);await assert.rejects(parseGltf(skinnedStripAsset(), { maxJoints: 1 }), /maxJoints/);- await assert.rejects(parseGltf(skinnedStripAsset(document => {- document.nodes.push({ name: 'Second strip', mesh: 0, skin: 0 });- document.nodes[0].children.push(4);- }), { maxJoints: 2 }), /skin pose joints/);+});++/** One skin of `jointCount` joints (a chain under Root) worn by `meshNodeCount` mesh nodes of `primitiveCount` primitives each. */+function sharedSkinAsset({ jointCount, meshNodeCount, primitiveCount = 1 }) {+ return skinnedStripAsset((document, append) => {+ for (let joint = 2; joint < jointCount; joint++) {+ document.nodes.push({ name: `Extra joint ${joint}` });+ document.nodes[2].children.push(document.nodes.length - 1);+ document.skins[0].joints.push(document.nodes.length - 1);+ }+ const identities = new Float32Array(jointCount * 16);+ for (let joint = 0; joint < jointCount; joint++) identities.set([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1], joint * 16);+ document.skins[0].inverseBindMatrices = append(identities, 'MAT4');+ const primitive = document.meshes[0].primitives[0];+ for (let copy = 1; copy < primitiveCount; copy++) document.meshes[0].primitives.push({ ...primitive, attributes: { ...primitive.attributes } });+ for (let copy = 1; copy < meshNodeCount; copy++) {+ document.nodes.push({ name: `Worn mesh ${copy}`, mesh: 0, skin: 0 });+ document.nodes[0].children.push(document.nodes.length - 1);+ }+ });+}++test('one skin shared by many primitives counts its joints once against maxJoints (#2259)', async () => {+ const { maxJoints } = AGENT_ASSET_LIMITS;+ const jointCount = 153;+ assert.ok(jointCount * 12 > maxJoints, 'the fixture must exceed maxJoints when joints are counted per primitive');+ for (const shape of [{ meshNodeCount: 12 }, { meshNodeCount: 1, primitiveCount: 12 }]) {+ const asset = await parseGltf(sharedSkinAsset({ jointCount, ...shape }), agentAssetDecodeOptions());+ assert.equal(asset.parts.length, 12);+ assert.equal(asset.parts[0].skin.jointMatrices.length / 16, jointCount);+ asset.dispose();+ }+ await assert.rejects(parseGltf(sharedSkinAsset({ jointCount: 5, meshNodeCount: 1 }), { maxJoints: 4 }), /skin joints.*maxJoints/);+});++test('skinned primitives keep their pose copies inside maxDecodedBytes, and the error names the cause (#2259)', async () => {+ const limit = { maxDecodedBytes: 400_000 };+ const one = await parseGltf(sharedSkinAsset({ jointCount: 153, meshNodeCount: 1 }), limit);+ one.dispose();+ await assert.rejects(+ parseGltf(sharedSkinAsset({ jointCount: 153, meshNodeCount: 12 }), limit),+ /decoded accessor and skin pose bytes.*maxDecodedBytes/,+ );});test('negative skin weights are clamped to zero and renormalised, with one warning that counts them', async () => {