@@ -1127,6 +1127,30 @@ public static FbxVector4 ConvertQuaternionToXYZEuler (FbxQuaternion quat)
1127
1127
return new FbxVector4 ( vector4 . X , - vector4 . Y , - vector4 . Z , vector4 . W ) ;
1128
1128
}
1129
1129
1130
+ /// <summary>
1131
+ /// Euler to quaternion without axis conversion.
1132
+ /// </summary>
1133
+ /// <returns>a quaternion.</returns>
1134
+ /// <param name="euler">Euler.</param>
1135
+ public static FbxQuaternion EulerToQuaternion ( FbxVector4 euler )
1136
+ {
1137
+ FbxAMatrix m = new FbxAMatrix ( ) ;
1138
+ m . SetR ( euler ) ;
1139
+ return m . GetQ ( ) ;
1140
+ }
1141
+
1142
+ /// <summary>
1143
+ /// Quaternion to euler without axis conversion.
1144
+ /// </summary>
1145
+ /// <returns>a euler.</returns>
1146
+ /// <param name="quat">Quaternion.</param>
1147
+ public static FbxVector4 QuaternionToEuler ( FbxQuaternion quat )
1148
+ {
1149
+ FbxAMatrix m = new FbxAMatrix ( ) ;
1150
+ m . SetQ ( quat ) ;
1151
+ return m . GetR ( ) ;
1152
+ }
1153
+
1130
1154
// get a fbxNode's global default position.
1131
1155
protected bool ExportTransform ( UnityEngine . Transform unityTransform , FbxNode fbxNode , Vector3 newCenter , TransformExportType exportType )
1132
1156
{
@@ -1589,24 +1613,15 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
1589
1613
var fbxPreRotationEuler = node . GetRotationActive ( )
1590
1614
? node . GetPreRotation ( FbxNode . EPivotSet . eSourcePivot )
1591
1615
: new FbxVector4 ( ) ;
1592
- //Debug.LogWarning ("rotation active: " + node.GetRotationActive () + ", pre rotation: " + node.GetPreRotation(FbxNode.EPivotSet.eSourcePivot));
1593
- Debug . LogWarning ( "node name: " + node . GetName ( ) ) ;
1594
-
1595
- FbxQuaternion fbxPreRotationInverse ; // = new FbxQuaternion();
1596
-
1597
- FbxAMatrix m = new FbxAMatrix ( ) ;
1598
- m . SetR ( fbxPreRotationEuler ) ;
1599
- fbxPreRotationInverse = m . GetQ ( ) ;
1600
1616
1601
- var tempRestRotation = fbxPreRotationInverse ;
1602
-
1603
- //fbxPreRotationInverse.ComposeSphericalXYZ(fbxPreRotationEuler);
1617
+ // Get the inverse of the prerotation
1618
+ var fbxPreRotationInverse = ModelExporter . EulerToQuaternion ( fbxPreRotationEuler ) ;
1604
1619
fbxPreRotationInverse . Inverse ( ) ;
1605
1620
1606
1621
// If we're only animating along certain coords for some
1607
1622
// reason, we'll need to fill in the other coords with the
1608
1623
// rest-pose value.
1609
- var lclQuaternion = tempRestRotation ; // new FbxQuaternion(restRotation.x, restRotation.y, restRotation.z, restRotation.w);
1624
+ var lclQuaternion = new FbxQuaternion ( restRotation . x , restRotation . y , restRotation . z , restRotation . w ) ;
1610
1625
1611
1626
// Find when we have keys set.
1612
1627
var keyTimes = new HashSet < float > ( ) ;
@@ -1629,33 +1644,24 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
1629
1644
( z == null ) ? lclQuaternion [ 2 ] : z . Evaluate ( seconds ) ,
1630
1645
( w == null ) ? lclQuaternion [ 3 ] : w . Evaluate ( seconds ) ) ;
1631
1646
1647
+ // convert the final animation to righthanded coords
1648
+ var finalEuler = ModelExporter . ConvertQuaternionToXYZEuler ( fbxFinalAnimation ) ;
1649
+
1650
+ // convert it back to a quaternion for multiplication
1651
+ fbxFinalAnimation = ModelExporter . EulerToQuaternion ( finalEuler ) ;
1652
+
1632
1653
// Cancel out the pre-rotation. Order matters. FBX reads left-to-right.
1633
1654
// When we run animation we will apply:
1634
1655
// pre-rotation
1635
1656
// then pre-rotation inverse
1636
1657
// then animation.
1637
-
1638
- var result = ModelExporter . ConvertQuaternionToXYZEuler ( fbxFinalAnimation ) ;
1639
-
1640
- m = new FbxAMatrix ( ) ;
1641
- m . SetR ( result ) ;
1642
- fbxFinalAnimation = m . GetQ ( ) ;
1643
-
1644
- var fbxQuat = fbxPreRotationInverse * fbxFinalAnimation ;
1645
- //var unityQuat = new Quaternion ((float)fbxQuat.X, (float)fbxQuat.Y, (float)fbxQuat.Z, (float)fbxQuat.W);
1646
- //var unityEul = unityQuat.eulerAngles;
1647
-
1648
- m = new FbxAMatrix ( ) ;
1649
- m . SetQ ( fbxQuat ) ;
1650
- var unityEul = m . GetR ( ) ;
1658
+ var fbxFinalQuat = fbxPreRotationInverse * fbxFinalAnimation ;
1651
1659
1652
1660
// Store the key so we can sort them later.
1653
1661
Key key ;
1654
1662
key . time = FbxTime . FromSecondDouble ( seconds ) ;
1655
- key . euler = unityEul ; //new FbxVector4(unityEul.x, unityEul.y, unityEul.z); // ModelExporter.ConvertQuaternionToXYZEuler(fbxQuat) ;
1663
+ key . euler = ModelExporter . QuaternionToEuler ( fbxFinalQuat ) ; ;
1656
1664
keys [ i ++ ] = key ;
1657
-
1658
- Debug . Log ( "pre rotation: " + fbxPreRotationEuler + ", complete rotation: " + key . euler + ", unity rot: " + unityEul ) ;
1659
1665
}
1660
1666
1661
1667
// Sort the keys by time
0 commit comments