Hi,
I have a problem importing animations from Warcraft 3 models (I want to do a modding tool with Unity). I used as a reference the Mesh.bindposes documentation.
This is the model imported with the rig, each bone is a green dot.
Bindposes are calculated like this:
mesh.bindposes = bones.Values.Select(go => go.transform.worldToLocalMatrix * skeleton.transform.localToWorldMatrix).ToArray();
Then root bone and bones are set to the SkinnedMeshRenderer:
renderer.bones = bones.Values.ToArray().Select(go => go.transform).ToArray();
renderer.rootBone = skeleton.transform;
Skin weights are also imported properly.
And then the translations and rotations components of the animations are imported (Y and Z coordinates are swapped):
float time = node.Time - csequence.IntervalStart;
Vector3 position = node.Value.ToVector3();
Keyframe keyX = new Keyframe(time / frameRate, position.x);
curveX.AddKey(keyX);
Keyframe keyY = new Keyframe(time / frameRate, position.z);
curveY.AddKey(keyY);
Keyframe keyZ = new Keyframe(time / frameRate, position.y);
curveZ.AddKey(keyZ);
if( curveX.length > 0 )
clip.SetCurve(path, typeof(Transform), "localPosition.x", curveX);
if( curveY.length > 0 )
clip.SetCurve(path, typeof(Transform), "localPosition.y", curveY);
if( curveZ.length > 0 )
clip.SetCurve(path, typeof(Transform), "localPosition.z", curveZ);
float time = node.Time - csequence.IntervalStart;
Quaternion rotation = node.Value.ToQuaternion();
Keyframe keyX = new Keyframe(time / frameRate, rotation.x);
curveX.AddKey(keyX);
Keyframe keyY = new Keyframe(time / frameRate, rotation.z);
curveY.AddKey(keyY);
Keyframe keyZ = new Keyframe(time / frameRate, rotation.y);
curveZ.AddKey(keyZ);
Keyframe keyW = new Keyframe(time / frameRate, -rotation.w);
curveW.AddKey(keyW);
if( curveX.length > 0 )
clip.SetCurve(path, typeof(Transform), "localRotation.x", curveX);
if( curveY.length > 0 )
clip.SetCurve(path, typeof(Transform), "localRotation.y", curveY);
if( curveZ.length > 0 )
clip.SetCurve(path, typeof(Transform), "localRotation.z", curveZ);
if( curveW.length > 0 )
clip.SetCurve(path, typeof(Transform), "localRotation.w", curveW);
The animation result is the following:
The animations looks perfect but it is anchored to the skinned mesh renderer gameobject (0,0,0). It seems that the bindposes or pivot points are not applied when the animation is playing.
In the example, the Body bone pivot point is (0.6, 50, -0.5) and the local position at that animation keyframe is (0, -5, 0). Also the Rifle bone pivot point is (2.5, -0.5, 4) and the local position at that animation keyframe is (11, 37, -8), the Rifle is higher than the Body (both bones are parented to the Root gameobject).
Thank you for your time.