Skip to content

Commit b626c1e

Browse files
committed
get new rotation from Matrix4x4 directly
- otherwise causes issues with negative rotations, or rotations of more than 180 - make sure that quaternion is valid when creating transform matrix
1 parent e664c55 commit b626c1e

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,23 +1819,24 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
18191819
// transfer root motion
18201820
var animSource = ExportOptions.AnimationSource;
18211821
var animDest = ExportOptions.AnimationDest;
1822+
if (animSource && animDest) {
1823+
// list of source to dest
1824+
var transformsInHierarchy = new List<Transform> ();
1825+
var curr = animDest;
1826+
while (curr != animSource) {
1827+
transformsInHierarchy.Add (curr);
1828+
curr = curr.parent;
1829+
}
1830+
transformsInHierarchy.Add (animSource);
1831+
transformsInHierarchy.Reverse ();
18221832

1823-
// list of source to dest
1824-
var transformsInHierarchy = new List<Transform> ();
1825-
var curr = animDest;
1826-
while (curr != animSource) {
1827-
transformsInHierarchy.Add (curr);
1828-
curr = curr.parent;
1829-
}
1830-
transformsInHierarchy.Add (animSource);
1831-
transformsInHierarchy.Reverse ();
1832-
1833-
while (transformsInHierarchy.Count >= 2) {
1834-
var source = transformsInHierarchy [0];
1835-
transformsInHierarchy.RemoveAt (0);
1836-
var dest = transformsInHierarchy [0];
1833+
while (transformsInHierarchy.Count >= 2) {
1834+
var source = transformsInHierarchy [0];
1835+
transformsInHierarchy.RemoveAt (0);
1836+
var dest = transformsInHierarchy [0];
18371837

1838-
TransferMotion (source, dest, uniAnimClip.frameRate, ref unityCurves);
1838+
TransferMotion (source, dest, uniAnimClip.frameRate, ref unityCurves);
1839+
}
18391840
}
18401841

18411842
foreach (var kvp in unityCurves) {
@@ -1845,7 +1846,7 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
18451846
var uniAnimCurve = uniCurve.uniAnimCurve;
18461847

18471848
// Do not create the curves if the component is a SkinnedMeshRenderer and if the option in FBX Export settings is toggled on.
1848-
if (removeAnimationsFromSkinnedMeshRenderer && (uniGO.GetComponent<SkinnedMeshRenderer> () != null || uniGO.GetComponentInChildren<SkinnedMeshRenderer> () != null)) {
1849+
if (!ExportOptions.AnimateSkinnedMesh && (uniGO.GetComponent<SkinnedMeshRenderer> () != null || uniGO.GetComponentInChildren<SkinnedMeshRenderer> () != null)) {
18491850
continue;
18501851
}
18511852

@@ -1934,24 +1935,28 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
19341935
var destLocalMatrix = GetTransformMatrix (currSampleTime, dest, destUnityCurves);
19351936

19361937
// child * parent
1937-
var mewLocalMatrix = sourceLocalMatrix * destLocalMatrix;
1938+
var newLocalMatrix = sourceLocalMatrix * destLocalMatrix;
19381939

19391940
// FBX is transposed relative to Unity: transpose as we convert.
19401941
FbxMatrix matrix = new FbxMatrix ();
1941-
matrix.SetColumn (0, new FbxVector4 (mewLocalMatrix.GetRow (0).x, mewLocalMatrix.GetRow (0).y, mewLocalMatrix.GetRow (0).z, mewLocalMatrix.GetRow (0).w));
1942-
matrix.SetColumn (1, new FbxVector4 (mewLocalMatrix.GetRow (1).x, mewLocalMatrix.GetRow (1).y, mewLocalMatrix.GetRow (1).z, mewLocalMatrix.GetRow (1).w));
1943-
matrix.SetColumn (2, new FbxVector4 (mewLocalMatrix.GetRow (2).x, mewLocalMatrix.GetRow (2).y, mewLocalMatrix.GetRow (2).z, mewLocalMatrix.GetRow (2).w));
1944-
matrix.SetColumn (3, new FbxVector4 (mewLocalMatrix.GetRow (3).x, mewLocalMatrix.GetRow (3).y, mewLocalMatrix.GetRow (3).z, mewLocalMatrix.GetRow (3).w));
1942+
matrix.SetColumn (0, new FbxVector4 (newLocalMatrix.GetRow (0).x, newLocalMatrix.GetRow (0).y, newLocalMatrix.GetRow (0).z, newLocalMatrix.GetRow (0).w));
1943+
matrix.SetColumn (1, new FbxVector4 (newLocalMatrix.GetRow (1).x, newLocalMatrix.GetRow (1).y, newLocalMatrix.GetRow (1).z, newLocalMatrix.GetRow (1).w));
1944+
matrix.SetColumn (2, new FbxVector4 (newLocalMatrix.GetRow (2).x, newLocalMatrix.GetRow (2).y, newLocalMatrix.GetRow (2).z, newLocalMatrix.GetRow (2).w));
1945+
matrix.SetColumn (3, new FbxVector4 (newLocalMatrix.GetRow (3).x, newLocalMatrix.GetRow (3).y, newLocalMatrix.GetRow (3).z, newLocalMatrix.GetRow (3).w));
19451946

19461947
// FBX wants translation, rotation (in euler angles) and scale.
19471948
// We assume there's no real shear, just rounding error.
19481949
FbxVector4 translation, rotation, shear, scale;
19491950
double sign;
19501951
matrix.GetElements (out translation, out rotation, out shear, out scale, out sign);
19511952

1953+
// get rotation directly from matrix, as otherwise causes issues
1954+
// with negative rotations.
1955+
var rot = newLocalMatrix.rotation.eulerAngles;
1956+
19521957
for (int k = 0; k < 3; k++) {
19531958
posKeyFrames [k][i] = new Keyframe(currSampleTime, (float)translation [k]);
1954-
rotKeyFrames [k][i] = new Keyframe(currSampleTime, (float)rotation [k]);
1959+
rotKeyFrames [k][i] = new Keyframe(currSampleTime, (float)rot [k]);
19551960
scaleKeyFrames [k][i] = new Keyframe(currSampleTime, (float)scale [k]);
19561961
}
19571962
i++;
@@ -1964,13 +1969,13 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
19641969
string scalePropName = "m_LocalScale.";
19651970
var xyz = new string[]{ "x", "y", "z" };
19661971
for (int j = 0; j < 3; j++) {
1967-
var posUniCurve = new UnityCurve ( posPropName + xyz[j], new AnimationCurve(posKeyFrames[j]) );
1972+
var posUniCurve = new UnityCurve ( posPropName + xyz[j], new AnimationCurve(posKeyFrames[j]));
19681973
newUnityCurves.Add (posUniCurve);
19691974

1970-
var rotUniCurve = new UnityCurve ( rotPropName + xyz[j], new AnimationCurve(rotKeyFrames[j]) );
1975+
var rotUniCurve = new UnityCurve ( rotPropName + xyz[j], new AnimationCurve(rotKeyFrames[j]));
19711976
newUnityCurves.Add (rotUniCurve);
19721977

1973-
var scaleUniCurve = new UnityCurve ( scalePropName + xyz[j], new AnimationCurve(scaleKeyFrames[j]) );
1978+
var scaleUniCurve = new UnityCurve ( scalePropName + xyz[j], new AnimationCurve(scaleKeyFrames[j]));
19741979
newUnityCurves.Add (scaleUniCurve);
19751980
}
19761981

@@ -1982,6 +1987,7 @@ private Matrix4x4 GetTransformMatrix(float currSampleTime, Transform orig, List<
19821987
var sourcePos = orig.localPosition;
19831988
var sourceRot = orig.localRotation;
19841989
var sourceScale = orig.localScale;
1990+
19851991
foreach (var uniCurve in unityCurves) {
19861992
float currSampleValue = uniCurve.uniAnimCurve.Evaluate((float)currSampleTime);
19871993
string propName = uniCurve.propertyName;
@@ -2009,6 +2015,7 @@ private Matrix4x4 GetTransformMatrix(float currSampleTime, Transform orig, List<
20092015
}
20102016
}
20112017

2018+
sourceRot = Quaternion.Euler(sourceRot.eulerAngles.x, sourceRot.eulerAngles.y, sourceRot.eulerAngles.z);
20122019
return Matrix4x4.TRS(sourcePos, sourceRot, sourceScale);
20132020
}
20142021

0 commit comments

Comments
 (0)