Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
fix(animation): ground-relative hip retargeting for origin-centered rigs
nirholas490bbfaf9680
diff --git a/data/changelog.json b/data/changelog.jsonindex 49b8da091..efbf53f83 100644--- a/data/changelog.json+++ b/data/changelog.json@@ -15972,6 +15972,15 @@"fix"],"link": "/agents"+ },+ {+ "date": "2026-07-25",+ "title": "Chibi and origin-centered avatars no longer float during animations",+ "summary": "Avatars with unusual proportions or models exported centered on the origin could leap a full body-height into the air the moment an animation played, leaving only their feet visible on agent profiles. The animation system now measures a rig's hip height from its own feet, so every avatar stays grounded no matter how it was exported.",+ "tags": [+ "fix"+ ],+ "link": "/agents"}]}\ No newline at end of filediff --git a/src/animation-manager.js b/src/animation-manager.jsindex e9fd1e87e..9f9a2a784 100644--- a/src/animation-manager.js+++ b/src/animation-manager.js@@ -14,6 +14,7 @@ import {canonicalRestMapFromObject,canonicalWorldRestMapFromObject,clipHipBaselineY,+ hipGroundLocalY,hipRestLocalHeight,hipsParentWorldQuat,morphTargetMapFromObject,@@ -214,6 +215,10 @@ export class AnimationManager {// this ÷ the clip's authored baseline — without it a 0.01-scaled rig's// hips collapse to the floor (metre-valued track × 0.01 parent ≈ 1cm).this._hipTargetLocalY = hipRestLocalHeight(model);+ // Where this rig's floor sits in the hips-parent's local frame (0 for a+ // ground-native export). The retarget adds it to the hip track so the+ // clip's ground lands on the model's actual ground.+ this._hipGroundLocalY = hipGroundLocalY(model);this._canonicalClipsSupported = _modelSupportsCanonicalClips(model);for (const [name, clip] of this.clips) {@@ -251,6 +256,7 @@ export class AnimationManager {hipsParentWorldQuat: this._hipsParentWorldQuat,morphTargets: this._morphTargets,hipScale,+ hipOffsetY: this._hipGroundLocalY || 0,});return out;}@@ -326,6 +332,7 @@ export class AnimationManager {this._hipsParentWorldQuat = null;this._morphTargets = null;this._hipTargetLocalY = 0;+ this._hipGroundLocalY = 0;this._canonicalClipsSupported = false;this.actions.clear();this.currentAction = null;diff --git a/src/animation-retarget.js b/src/animation-retarget.jsindex 127be71d3..8ccbe9ec6 100644--- a/src/animation-retarget.js+++ b/src/animation-retarget.js@@ -17,7 +17,7 @@// Pure module — three + the canonicalizer only — so it runs unchanged in the// browser (the /pose gallery) and in Node (the apply_animation MCP tool, vitest).-import { AnimationClip, Quaternion, Vector3 } from 'three';+import { AnimationClip, Box3, Quaternion, Vector3 } from 'three';import { canonicalizeBoneName } from './glb-canonicalize.js';import { CANONICAL_REST, CANONICAL_REST_WORLD } from './animation-canonical-rest.js';@@ -387,6 +387,18 @@ export function hipRestHeight(rig) {return _v.y;}+// Bounding-box bottom of the model, or NaN when it can't be computed. Guarded:+// three's pose-aware SkinnedMesh.computeBoundingBox walks skin attributes per+// vertex and throws on synthetic rigs whose geometry lacks them; a rig we can't+// measure just keeps the pre-floor behavior instead of failing the mount.+function safeFloorY(root) {+ try {+ return new Box3().setFromObject(root).min.y;+ } catch {+ return NaN;+ }+}+const _hipScalePos = new Vector3();const _hipScaleQuat = new Quaternion();const _hipScaleScale = new Vector3();@@ -419,13 +431,55 @@ export function hipRestLocalHeight(root) {if (!hips || !hips.parent) return 0;hips.updateWorldMatrix(true, false);hips.getWorldPosition(_hipScalePos);- const worldY = _hipScalePos.y;+ let worldY = _hipScalePos.y;+ // Measure above the model's own floor (its bounding-box bottom), not above+ // world y=0. Viewers recenter content around the origin (setContent+ // subtracts the bbox center) and origin-centered GLB exports put the floor+ // below zero; measured from world zero, both read as "hips at or below the+ // ground", which skipped scaling and let the metre-authored hip track fling+ // a small rig into the air. Clips author ground at 0, so hips-above-floor+ // is the like-for-like height. For a ground-native rig floorY is 0 and+ // nothing changes.+ const floorY = safeFloorY(root);+ if (Number.isFinite(floorY)) worldY -= floorY;hips.parent.matrixWorld.decompose(_hipScalePos, _hipScaleQuat, _hipScaleScale);const parentScale = (_hipScaleScale.x + _hipScaleScale.y + _hipScaleScale.z) / 3;if (!(parentScale > 1e-6) || !(worldY > 0) || !Number.isFinite(worldY)) return 0;return worldY / parentScale;}+/**+ * The model's floor (bounding-box bottom) expressed in the hips-parent's local+ * frame — the Y a hip-position track must be offset by so the clip's ground+ * (authored at 0) lands on this rig's actual ground. 0 for a ground-native rig+ * (floor at local 0); negative for an origin-centered export (floor below the+ * local origin). Returns 0 when it can't be determined, which leaves the+ * retarget exactly as before.+ *+ * @param {import('three').Object3D} root+ * @returns {number}+ */+export function hipGroundLocalY(root) {+ let hips = null;+ root.traverse((n) => {+ if (!hips && n.isBone && n.name && canonicalizeBoneName(n.name) === 'Hips') hips = n;+ });+ if (!hips) {+ root.traverse((n) => {+ if (!hips && n.name && canonicalizeBoneName(n.name) === 'Hips') hips = n;+ });+ }+ if (!hips || !hips.parent) return 0;+ hips.updateWorldMatrix(true, false);+ const floorY = safeFloorY(root);+ if (!Number.isFinite(floorY)) return 0;+ hips.getWorldPosition(_hipScalePos);+ // The floor point under the hips, mapped into the parent's local frame so+ // the offset composes with the track's local-position values.+ const local = hips.parent.worldToLocal(new Vector3(_hipScalePos.x, floorY, _hipScalePos.z));+ return Number.isFinite(local.y) ? local.y : 0;+}+// First Y value of a `Hips.position` track — the height the clip's root motion// was authored around. The retarget scales other-height rigs by target/source.export function clipHipBaselineY(clip) {@@ -444,11 +498,12 @@ export function clipHipBaselineY(clip) {** @param {AnimationClip} clip* @param {Map<string,string>} canonicalToNode- * @param {{ hipScale?: number, minCoverage?: number, morphTargets?: Map<string,string[]> }} [opts]+ * @param {{ hipScale?: number, hipOffsetY?: number, minCoverage?: number, morphTargets?: Map<string,string[]> }} [opts]* @returns {RetargetResult}*/export function retargetClip(clip, canonicalToNode, opts = {}) {const hipScale = Number.isFinite(opts.hipScale) && opts.hipScale > 0 ? opts.hipScale : 1;+ const hipOffsetY = Number.isFinite(opts.hipOffsetY) ? opts.hipOffsetY : 0;const minCoverage = opts.minCoverage ?? MIN_COVERAGE;const corrections = bindCorrections(opts.targetRest, opts.targetWorldRest);const hipsPosCorrection = hipPositionCorrection(opts.hipsParentWorldQuat, corrections);@@ -529,6 +584,13 @@ export function retargetClip(clip, canonicalToNode, opts = {}) {if (hipScale !== 1) {for (let i = 0; i < next.values.length; i++) next.values[i] *= hipScale;}+ // Land the clip's ground (authored at 0) on this rig's actual floor:+ // origin-centered exports keep their floor below the local origin, and+ // without this offset the scaled track still hovers the body a+ // floor-height above the mesh.+ if (hipOffsetY !== 0) {+ for (let i = 1; i < next.values.length; i += 3) next.values[i] += hipOffsetY;+ }}tracks.push(next);}diff --git a/tests/animation-retarget.test.js b/tests/animation-retarget.test.jsindex fc9ad4511..ee2295c44 100644--- a/tests/animation-retarget.test.js+++ b/tests/animation-retarget.test.js@@ -28,10 +28,14 @@ import {Quaternion,Vector3,Euler,+ Uint16BufferAttribute,+ Float32BufferAttribute,} from 'three';import {canonicalNodeMapFromObject,canonicalRestMapFromObject,+ hipGroundLocalY,+ hipRestLocalHeight,hipsParentWorldQuat,retargetClip,retargetClipToObject,@@ -676,3 +680,106 @@ describe('face (morph target) lanes', () => {expect(clip.tracks).toHaveLength(1);});});++describe('hipRestLocalHeight', () => {+ // A half-metre chibi rig with real geometry bounds: body spans floor..top,+ // hips partway up. Built like a GLB import: bones + a skinned mesh whose+ // geometry carries the body's vertical extent.+ function makeMeasuredRig({ floorY, topY, hipsY }) {+ const root = new Object3D();+ const hips = new Bone();+ hips.name = 'Hips';+ hips.position.y = hipsY;+ root.add(hips);+ const geo = new BufferGeometry();+ geo.setFromPoints([new Vector3(0, floorY, 0), new Vector3(0, topY, 0)]);+ // Real skin attributes: SkinnedMesh.computeBoundingBox (used by+ // Box3.setFromObject) walks them per vertex.+ geo.setAttribute(+ 'skinIndex',+ new Uint16BufferAttribute(new Uint16Array([0, 0, 0, 0, 0, 0, 0, 0]), 4),+ );+ geo.setAttribute(+ 'skinWeight',+ new Float32BufferAttribute(new Float32Array([1, 0, 0, 0, 1, 0, 0, 0]), 4),+ );+ const mesh = new SkinnedMesh(geo);+ root.add(mesh);+ // Update world matrices BEFORE bind so the inverse bind matrices capture+ // the true bind pose (as a GLB's authored IBMs would); binding at+ // identity would re-shift every vertex by the hips offset.+ root.updateMatrixWorld(true);+ mesh.bind(new Skeleton([hips]));+ return root;+ }++ it('measures a ground-native rig unchanged (floor at 0)', () => {+ const root = makeMeasuredRig({ floorY: 0, topY: 1, hipsY: 0.24 });+ expect(hipRestLocalHeight(root)).toBeCloseTo(0.24, 5);+ });++ it('measures above the model floor for an origin-centered rig', () => {+ // Same body exported origin-centered: floor at -0.5, hips world at -0.26.+ // Measured from world zero this reads negative and used to skip hip+ // scaling entirely, flinging the rig to the clip's metre-scale height.+ const root = makeMeasuredRig({ floorY: -0.5, topY: 0.5, hipsY: -0.26 });+ expect(hipRestLocalHeight(root)).toBeCloseTo(0.24, 5);+ });++ it('measures above the floor after a viewer recenters the content', () => {+ // Viewer.setContent subtracts the bbox center from the content root; the+ // hips height above the feet must survive that shift.+ const root = makeMeasuredRig({ floorY: 0, topY: 1, hipsY: 0.24 });+ root.position.y -= 0.5;+ root.updateMatrixWorld(true);+ expect(hipRestLocalHeight(root)).toBeCloseTo(0.24, 5);+ });+});++describe('hipGroundLocalY + hip track offset', () => {+ function measuredRig({ floorY, topY, hipsY }) {+ const root = new Object3D();+ const hips = new Bone();+ hips.name = 'Hips';+ hips.position.y = hipsY;+ root.add(hips);+ const geo = new BufferGeometry();+ geo.setFromPoints([new Vector3(0, floorY, 0), new Vector3(0, topY, 0)]);+ geo.setAttribute(+ 'skinIndex',+ new Uint16BufferAttribute(new Uint16Array([0, 0, 0, 0, 0, 0, 0, 0]), 4),+ );+ geo.setAttribute(+ 'skinWeight',+ new Float32BufferAttribute(new Float32Array([1, 0, 0, 0, 1, 0, 0, 0]), 4),+ );+ const mesh = new SkinnedMesh(geo);+ root.add(mesh);+ root.updateMatrixWorld(true);+ mesh.bind(new Skeleton([hips]));+ return root;+ }++ it('is 0 for a ground-native rig', () => {+ const root = measuredRig({ floorY: 0, topY: 1, hipsY: 0.24 });+ expect(hipGroundLocalY(root)).toBeCloseTo(0, 5);+ });++ it('reports the local floor of an origin-centered rig', () => {+ const root = measuredRig({ floorY: -0.5, topY: 0.5, hipsY: -0.26 });+ expect(hipGroundLocalY(root)).toBeCloseTo(-0.5, 5);+ });++ it('offsets the hip position track Y so the clip ground lands on the rig floor', () => {+ const clip = makeCanonicalClip(); // Hips.position Y keys: 1.0, 1.1+ const map = canonicalNodeMapFromObject(makeRig(CANONICAL_RIG));+ const r = retargetClip(clip, map, { hipScale: 0.24, hipOffsetY: -0.5 });+ const pos = r.clip.tracks.find((t) => t.name === 'Hips.position');+ // scaled then offset: 1.0 * 0.24 - 0.5, 1.1 * 0.24 - 0.5+ expect(pos.values[1]).toBeCloseTo(-0.26, 5);+ expect(pos.values[4]).toBeCloseTo(-0.236, 5);+ // X/Z stay un-offset.+ expect(pos.values[0]).toBeCloseTo(0, 5);+ expect(pos.values[2]).toBeCloseTo(0, 5);+ });+});