3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Frame and ground skinned assets by their drawn bounds (#1976)
Nguyen Xuan An88de014012e7
diff --git a/changes/1976.md b/changes/1976.mdnew file mode 100644index 00000000..13a4ff58--- /dev/null+++ b/changes/1976.md@@ -0,0 +1 @@+- Auto-framed camera and the preview ground of a skinned asset now use the drawn (skinned) bounds instead of the bind-pose bounds (#1976).diff --git a/src/agent-asset.js b/src/agent-asset.jsindex e14cb862..26990899 100644--- a/src/agent-asset.js+++ b/src/agent-asset.js@@ -4,7 +4,7 @@ import { createAgentScene, REVISION_HISTORY_LIMIT } from './agent-scene.js';import { environmentPatchWithSetup } from './agent-look.js';import { assetDiagnostics } from './agent-diagnostics.js';import { fitPerspectiveDistance } from './camera.js';-import { validateSkinGeometry, validateSkinPose } from './skinning.js';+import { skinnedWorldBounds, validateSkinGeometry, validateSkinPose } from './skinning.js';import { validateMorphGeometry, validateMorphTransform } from './morph.js';import * as agentMotion from './agent-motion.js';import { compileClipStrip } from './clip-strip.js';@@ -186,10 +186,20 @@ function localBounds(geometry) {return { min: minimum, max: maximum };}-function framingBounds(geometryInfos, matrices) {+// Unskinned parts use their geometry bounds corners; a skinned part is measured as drawn, through its joint palette.+function framingBounds(geometryInfos, matrices, skinnedParts = []) {const minimum = [Infinity, Infinity, Infinity];const maximum = [-Infinity, -Infinity, -Infinity];for (let index = 0; index < geometryInfos.length; index++) {+ const skinned = skinnedParts[index];+ if (skinned) {+ const drawn = skinnedWorldBounds(skinned.geometry, skinned.skin, matrices[index]);+ for (let axis = 0; axis < 3; axis++) {+ minimum[axis] = Math.min(minimum[axis], drawn.minimum[axis]);+ maximum[axis] = Math.max(maximum[axis], drawn.maximum[axis]);+ }+ continue;+ }const bounds = geometryInfos[index].bounds;const matrix = matrices[index];for (let corner = 0; corner < 8; corner++) {@@ -771,7 +781,11 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {return world;});if (autoFrame) {- candidate.camera = framedCamera(framingBounds(records.map(record => geometryInfoOf(record, stagedEdits)), stagedMatrices));+ candidate.camera = framedCamera(framingBounds(+ records.map(record => geometryInfoOf(record, stagedEdits)),+ stagedMatrices,+ records.map((record, index) => transaction.parts[index].skin ? { geometry: geometryOf(record, stagedEdits), skin: transaction.parts[index].skin } : null),+ ));candidate = normalize(candidate);stagedSampler = agentMotion.compileAgentMotion(candidate.motion, motionRecipe(candidate));sampled = targetTime === null ? null : stagedSampler?.sample(targetTime);diff --git a/src/agent-ground.js b/src/agent-ground.jsindex de995e40..1fc07b90 100644--- a/src/agent-ground.js+++ b/src/agent-ground.js@@ -3,6 +3,7 @@// recipe never holds it, and it is left out when a part of the recipe already forms a floor or `environment.ground`// is false. Callers give it to the renderer as the `ground` option (renderer.js draws and shadows it like any part).import { mat4 } from 'gl-matrix';+import { skinnedWorldBounds } from './skinning.js';const DISC_SEGMENTS = 48;// A part is a floor when it spans this share of the scene's width and depth, is thin, and is the lowest geometry.@@ -14,20 +15,9 @@ const DISC_REACH = 6;const MINIMUM_RADIUS = 2;export const FLOOR_COLOR = [0.22, 0.22, 0.21];-/** World-space axis-aligned bounds of one part, from its positions and matrix. */+/** World-space axis-aligned bounds of one part, from its positions, its skin pose when it has one, and its matrix. */function partBounds(part) {- const { positions } = part.geometry;- const matrix = part.matrix;- const minimum = [Infinity, Infinity, Infinity];- const maximum = [-Infinity, -Infinity, -Infinity];- for (let offset = 0; offset < positions.length; offset += 3) {- for (let axis = 0; axis < 3; axis++) {- const value = matrix[axis] * positions[offset] + matrix[4 + axis] * positions[offset + 1] + matrix[8 + axis] * positions[offset + 2] + matrix[12 + axis];- minimum[axis] = Math.min(minimum[axis], value);- maximum[axis] = Math.max(maximum[axis], value);- }- }- return { minimum, maximum };+ return skinnedWorldBounds(part.geometry, part.skin, part.matrix);}function sceneBounds(boundsList) {diff --git a/src/skinning.js b/src/skinning.jsindex f9c0d02e..525959ed 100644--- a/src/skinning.js+++ b/src/skinning.js@@ -312,3 +312,38 @@ export function reflectedSkinDiagnostics(partLocations) {}registerDeformationSupport({ validateSkinGeometry, validateSkinPose, reflectedSkinDiagnostics, skinTextureLayout, packSkinInfluences });++/**+ * Bounds of a mesh as it is drawn: each vertex blended by its joint weights through the pose's palette, then moved+ * by `worldMatrix`. Bind-pose positions alone miss where the skin puts the mesh. A mesh with no skin uses the matrix alone.+ * Returns `{ minimum, maximum }`.+ */+export function skinnedWorldBounds(geometry, pose, worldMatrix) {+ const minimum = [Infinity, Infinity, Infinity];+ const maximum = [-Infinity, -Infinity, -Infinity];+ const { positions, skinIndices, skinWeights } = geometry;+ const palette = pose?.jointMatrices;+ const skinned = Boolean(palette && skinIndices && skinWeights);+ const blended = mat4.create();+ for (let vertex = 0; vertex < positions.length / 3; vertex++) {+ const offset = vertex * 3;+ let matrix = worldMatrix;+ if (skinned) {+ blended.fill(0);+ for (let slot = 0; slot < 4; slot++) {+ const weight = skinWeights[vertex * 4 + slot];+ if (weight === 0) continue;+ const jointOffset = skinIndices[vertex * 4 + slot] * 16;+ for (let element = 0; element < 16; element++) blended[element] += weight * palette[jointOffset + element];+ }+ matrix = mat4.multiply(blended, worldMatrix, blended);+ }+ for (let axis = 0; axis < 3; axis++) {+ const value = matrix[axis] * positions[offset] + matrix[4 + axis] * positions[offset + 1] ++ matrix[8 + axis] * positions[offset + 2] + matrix[12 + axis];+ minimum[axis] = Math.min(minimum[axis], value);+ maximum[axis] = Math.max(maximum[axis], value);+ }+ }+ return { minimum, maximum };+}diff --git a/tests/agent-asset.test.js b/tests/agent-asset.test.jsindex 13ba8503..59a23a32 100644--- a/tests/agent-asset.test.js+++ b/tests/agent-asset.test.js@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';import { parseGltf } from '../src/gltf.js';import { createAgentAssetScene } from '../src/agent-asset.js';import { skinnedStripAsset } from './fixtures/skinned-strip.js';+import { agentGroundPlacement } from '../src/agent-ground.js';const recipe = () => ({ format: 'crucible.asset', schemaVersion: 1, assetId: 'ab'.repeat(32) });@@ -230,3 +231,15 @@ test('decoded instances share original geometry and material wrappers', async ()assert.equal(scene.parts[1].material.roughness, 0.2);asset.dispose();});++test('auto-framed camera and preview ground follow the skinned bounds, not the bind pose', async () => {+ const asset = await fixture(document => {+ document.nodes[2].translation = [0, 10, 0];+ });+ const scene = createAgentAssetScene(asset, recipe());+ const [, targetHeight] = scene.camera.target;+ assert.ok(targetHeight > 12 && targetHeight < 14, `camera target height ${targetHeight} should lie inside the drawn 12 to 14 metres`);+ const placement = agentGroundPlacement(scene.parts, scene.environment);+ assert.ok(Math.abs(placement.height - 12) < 1e-4, `ground height ${placement.height} should sit at the drawn minimum 12`);+ asset.dispose();+});