const loader = new FBXLoader();
loader.load('/static/resources/models/pg_actorcore_embed.fbx', function (model) {
globalModel = model; // 将加载的模型赋值给全局变量
scene.add(model)
// 加载动画
loader.load('/static/resources/animations/blender_animation.fbx', function (anim) {
const clip = anim.animations[0];
//For ideal results, please comment the following code<
clip.tracks.forEach(track => {
const boneName = track.name.split('.')[0];
if (SMPL2ACTORCORE2[boneName]) {
track.name = track.name.replace(boneName, SMPL2ACTORCORE2[boneName]);
}
});
//>
let modelskinnedMesh;
model.traverse((object) => {
if (object.isSkinnedMesh) {
modelskinnedMesh = object;
}
});
const modelHelper = new THREE.SkeletonHelper(model);
let modelSkeleton = new THREE.Skeleton(modelHelper.bones);
modelHelper.skeleton = modelSkeleton
console.log(modelHelper)
scene.add(modelHelper)
let animbones = []
anim.traverse(function (object) {
if (object.isBone) {
//For ideal results, please comment the following code<
const newName = SMPL2ACTORCORE2[object.name];
if (newName) {
object.name = newName
}
//>
animbones.push(object);
}
});
const animHelper = new THREE.SkeletonHelper(anim);
let animSkeleton = new THREE.Skeleton(animbones);
animHelper.skeleton = animSkeleton;
console.log(animHelper)
scene.add(animHelper)
// 使用 SkeletonUtils.retargetClip 迁移动画
let retargetedClip = SkeletonUtils.retargetClip(modelHelper, animHelper, anim.animations[0], {
preserveMatrix: false,
preservePosition: false,
useFirstFramePosition: true,
preserveHipPosition: false,
useTargetMatrix: true,
});
// 创建动画操作并播放
mixer = new THREE.AnimationMixer(modelHelper);
const action = mixer.clipAction(retargetedClip);
// action.play();
model.rotation.x = -Math.PI / 2; // 使模型绕 X 轴旋转90度
});
});
Please refer to this link. Animation Retargeting – Wicked Engine
Thank you for your answer. I’ve seen the animation redirection documentation, it’s very good and I’m using SkeletonUtils.retargetClip in my code for redirection. Through the source code, we found that the ideas of the two are similar. The animation plays normally, but the skinning of the bone nodes is distorted. Is it related to the bone pointing problem in the first picture?
Check for the following issues:
-
Check Bone Orientations: Ensure that the bone orientations in both the source and target rigs are as similar as possible. Use tools or scripts to match bone orientations between rigs before migrating animations.
-
Standardize Rigging: Maintain a consistent rigging process across all models to minimize variations in bone orientations. Standardized rigging practices can help reduce the likelihood of skin distortion during animation migration.
-
Weight Painting: Adjust weight painting on the mesh to account for differences in bone orientations. Fine-tuning the weights of vertices can help distribute deformation more evenly across the mesh, reducing distortion.
Yes, in Blender, I tried to correct the second set of bones in Figure 1. When I changed the orientation of the bones to connect them, their skin would be stretched and twisted.
This is sometimes a really hard thing to solve after the fact. You may want to focus your efforts on importing the model with the correct settings first, rather than trying to correct it later.
If you attempt to manually change the bone orientation, that doesn’t correct the internal bind pose of the model, and you may end up having to re-bind the skeleton to the mesh before it will work correctly.
You want to look for import/export settings involving “Y-Up” or “Y-forward” and experiment with those settings either when importing into blender, or exporting from blender to gltf etc.
For FBX import, look at the “Armature” section of the import settings and pay special attention to the “primary bone axis” and “secondary bone axis”
For gltf export, pay attention to the Transform->Y-Up checkbox and experiment with checking/unchecking that box.