Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

Improve importing FBX retargeting for bettering resetting to rest pose

Scott Petrovicd196aea4fa2e
diff --git a/src/retarget/RetargetUtils.ts b/src/retarget/RetargetUtils.ts
index 132ec83..636c279 100644
--- a/src/retarget/RetargetUtils.ts
+++ b/src/retarget/RetargetUtils.ts
@@ -1,4 +1,4 @@
-import { type Scene, Group, Skeleton, type SkinnedMesh, type Bone, type Object3D } from 'three'
+import { type Scene, Group, Matrix4, Skeleton, type SkinnedMesh, type Bone, type Object3D } from 'three'
import { ModalDialog } from '../lib/ModalDialog.ts'
export interface TrackNameParts {
@@ -42,14 +42,43 @@ export class RetargetUtils {
}
/**
- * Resets all SkinnedMeshes in the group to their rest pose
+ * Resets all SkinnedMeshes in the group to their rest pose.
+ *
+ * This is a parent-aware version of Skeleton.pose(). The native pose() writes
+ * the bind-time WORLD matrix into root bone locals without accounting for the
+ * armature node's own transform, so the armature scale/rotation gets applied a
+ * second time on the next updateMatrixWorld (e.g. an armature with scale 0.01
+ * makes the model render at 1/100th the size the bounding box reports).
*/
static reset_skinned_mesh_to_rest_pose (skinned_meshes_group: Scene): void {
+ // ensure non-bone parent (armature) world matrices are current
+ skinned_meshes_group.updateMatrixWorld(true)
+
+ const parent_inverse = new Matrix4()
+
skinned_meshes_group.traverse((child) => {
if (child.type === 'SkinnedMesh') {
const skinned_mesh = child as SkinnedMesh
const skeleton: Skeleton = skinned_mesh.skeleton
- skeleton.pose()
+
+ // first pass: recover the bind-time world matrices
+ skeleton.bones.forEach((bone, index) => {
+ bone.matrixWorld.copy(skeleton.boneInverses[index]).invert()
+ })
+
+ // second pass: convert bind world matrices into bone-local matrices,
+ // dividing out whatever transform the parent (bone or armature) has
+ skeleton.bones.forEach((bone) => {
+ if (bone.parent !== null) {
+ parent_inverse.copy(bone.parent.matrixWorld).invert()
+ bone.matrix.copy(parent_inverse).multiply(bone.matrixWorld)
+ } else {
+ bone.matrix.copy(bone.matrixWorld)
+ }
+
+ bone.matrix.decompose(bone.position, bone.quaternion, bone.scale)
+ })
+
skinned_mesh.updateMatrixWorld(true)
}
})
diff --git a/src/retarget/steps/StepLoadTargetModel.ts b/src/retarget/steps/StepLoadTargetModel.ts
index 033983d..b4dacb1 100644
--- a/src/retarget/steps/StepLoadTargetModel.ts
+++ b/src/retarget/steps/StepLoadTargetModel.ts
@@ -128,14 +128,6 @@ export class StepLoadTargetModel extends EventTarget {
}
}
- private fix_fbx_mixamo_bones (): void {
- // TODO: The FBX files for Mixamo do something weird with the bone hieararchy in three.js.
- // The root bone is is the "hips" bone, and the first child of the hips bone is also named "hips".
- // This is messing up stuff like the skeleton helper and probably the animations by assigning keyframes in the wrong way.
- // The root hips bone is not in the "bones list" with the skeleton data, so I am not sure why that is there. I might have to look into the FBX loader
- // class to see what is going on.
- }
-
/**
* Some FBX files contain multiple disconnected bone hierarchies (multiple
* root bones). Mesh2Motion only supports a single hierarchy, so the user is
@@ -179,7 +171,8 @@ export class StepLoadTargetModel extends EventTarget {
// if the largest dimension is over 20, scale the entire scene down to be
const target_height: number = 1.5 // in meters
- if (largest_dimension > 20) {
+
+ if (largest_dimension > 20 || largest_dimension < 0.1) {
// calculate scale factor
const scale_factor = target_height / largest_dimension
console.log('scaling model down because of ', largest_dimension)