Skip to content

Commit b2dce2c

Browse files
committed
clean up implementation
1 parent 22ecd5e commit b2dce2c

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,30 @@ public static FbxVector4 ConvertQuaternionToXYZEuler (FbxQuaternion quat)
11271127
return new FbxVector4 (vector4.X, -vector4.Y, -vector4.Z, vector4.W);
11281128
}
11291129

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+
11301154
// get a fbxNode's global default position.
11311155
protected bool ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbxNode, Vector3 newCenter, TransformExportType exportType)
11321156
{
@@ -1589,24 +1613,15 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
15891613
var fbxPreRotationEuler = node.GetRotationActive()
15901614
? node.GetPreRotation(FbxNode.EPivotSet.eSourcePivot)
15911615
: 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();
16001616

1601-
var tempRestRotation = fbxPreRotationInverse;
1602-
1603-
//fbxPreRotationInverse.ComposeSphericalXYZ(fbxPreRotationEuler);
1617+
// Get the inverse of the prerotation
1618+
var fbxPreRotationInverse = ModelExporter.EulerToQuaternion (fbxPreRotationEuler);
16041619
fbxPreRotationInverse.Inverse();
16051620

16061621
// If we're only animating along certain coords for some
16071622
// reason, we'll need to fill in the other coords with the
16081623
// 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);
16101625

16111626
// Find when we have keys set.
16121627
var keyTimes = new HashSet<float>();
@@ -1629,33 +1644,24 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
16291644
(z == null) ? lclQuaternion[2] : z.Evaluate(seconds),
16301645
(w == null) ? lclQuaternion[3] : w.Evaluate(seconds));
16311646

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+
16321653
// Cancel out the pre-rotation. Order matters. FBX reads left-to-right.
16331654
// When we run animation we will apply:
16341655
// pre-rotation
16351656
// then pre-rotation inverse
16361657
// 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;
16511659

16521660
// Store the key so we can sort them later.
16531661
Key key;
16541662
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);;
16561664
keys[i++] = key;
1657-
1658-
Debug.Log ("pre rotation: " + fbxPreRotationEuler + ", complete rotation: " + key.euler + ", unity rot: " + unityEul);
16591665
}
16601666

16611667
// Sort the keys by time

0 commit comments

Comments
 (0)