@@ -1819,23 +1819,24 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
1819
1819
// transfer root motion
1820
1820
var animSource = ExportOptions . AnimationSource ;
1821
1821
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 ( ) ;
1822
1832
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 ] ;
1837
1837
1838
- TransferMotion ( source , dest , uniAnimClip . frameRate , ref unityCurves ) ;
1838
+ TransferMotion ( source , dest , uniAnimClip . frameRate , ref unityCurves ) ;
1839
+ }
1839
1840
}
1840
1841
1841
1842
foreach ( var kvp in unityCurves ) {
@@ -1845,7 +1846,7 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
1845
1846
var uniAnimCurve = uniCurve . uniAnimCurve ;
1846
1847
1847
1848
// 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 ) ) {
1849
1850
continue ;
1850
1851
}
1851
1852
@@ -1934,24 +1935,28 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
1934
1935
var destLocalMatrix = GetTransformMatrix ( currSampleTime , dest , destUnityCurves ) ;
1935
1936
1936
1937
// child * parent
1937
- var mewLocalMatrix = sourceLocalMatrix * destLocalMatrix ;
1938
+ var newLocalMatrix = sourceLocalMatrix * destLocalMatrix ;
1938
1939
1939
1940
// FBX is transposed relative to Unity: transpose as we convert.
1940
1941
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 ) ) ;
1945
1946
1946
1947
// FBX wants translation, rotation (in euler angles) and scale.
1947
1948
// We assume there's no real shear, just rounding error.
1948
1949
FbxVector4 translation , rotation , shear , scale ;
1949
1950
double sign ;
1950
1951
matrix . GetElements ( out translation , out rotation , out shear , out scale , out sign ) ;
1951
1952
1953
+ // get rotation directly from matrix, as otherwise causes issues
1954
+ // with negative rotations.
1955
+ var rot = newLocalMatrix . rotation . eulerAngles ;
1956
+
1952
1957
for ( int k = 0 ; k < 3 ; k ++ ) {
1953
1958
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 ] ) ;
1955
1960
scaleKeyFrames [ k ] [ i ] = new Keyframe ( currSampleTime , ( float ) scale [ k ] ) ;
1956
1961
}
1957
1962
i ++ ;
@@ -1964,13 +1969,13 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
1964
1969
string scalePropName = "m_LocalScale." ;
1965
1970
var xyz = new string [ ] { "x" , "y" , "z" } ;
1966
1971
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 ] ) ) ;
1968
1973
newUnityCurves . Add ( posUniCurve ) ;
1969
1974
1970
- var rotUniCurve = new UnityCurve ( rotPropName + xyz [ j ] , new AnimationCurve ( rotKeyFrames [ j ] ) ) ;
1975
+ var rotUniCurve = new UnityCurve ( rotPropName + xyz [ j ] , new AnimationCurve ( rotKeyFrames [ j ] ) ) ;
1971
1976
newUnityCurves . Add ( rotUniCurve ) ;
1972
1977
1973
- var scaleUniCurve = new UnityCurve ( scalePropName + xyz [ j ] , new AnimationCurve ( scaleKeyFrames [ j ] ) ) ;
1978
+ var scaleUniCurve = new UnityCurve ( scalePropName + xyz [ j ] , new AnimationCurve ( scaleKeyFrames [ j ] ) ) ;
1974
1979
newUnityCurves . Add ( scaleUniCurve ) ;
1975
1980
}
1976
1981
@@ -1982,6 +1987,7 @@ private Matrix4x4 GetTransformMatrix(float currSampleTime, Transform orig, List<
1982
1987
var sourcePos = orig . localPosition ;
1983
1988
var sourceRot = orig . localRotation ;
1984
1989
var sourceScale = orig . localScale ;
1990
+
1985
1991
foreach ( var uniCurve in unityCurves ) {
1986
1992
float currSampleValue = uniCurve . uniAnimCurve . Evaluate ( ( float ) currSampleTime ) ;
1987
1993
string propName = uniCurve . propertyName ;
@@ -2009,6 +2015,7 @@ private Matrix4x4 GetTransformMatrix(float currSampleTime, Transform orig, List<
2009
2015
}
2010
2016
}
2011
2017
2018
+ sourceRot = Quaternion . Euler ( sourceRot . eulerAngles . x , sourceRot . eulerAngles . y , sourceRot . eulerAngles . z ) ;
2012
2019
return Matrix4x4 . TRS ( sourcePos , sourceRot , sourceScale ) ;
2013
2020
}
2014
2021
0 commit comments