Skip to content

Commit e20d866

Browse files
committed
add custom extensions. add tests for quaternions. add tests for gimbal conditions
1 parent 6d520ca commit e20d866

File tree

2 files changed

+220
-63
lines changed

2 files changed

+220
-63
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,66 @@
88

99
namespace FbxExporters
1010
{
11+
namespace CustomExtensions
12+
{
13+
//Extension methods must be defined in a static class
14+
public static class QuaternionExtension
15+
{
16+
// This is an extension method for the Quaternion class
17+
// The first parameter takes the "this" modifier
18+
// and specifies the type for which the method is defined.
19+
public static float Get (this Quaternion q, int id)
20+
{
21+
switch (id) {
22+
case 0: return q.x;
23+
case 1: return q.y;
24+
case 2: return q.z;
25+
case 3: return q.w;
26+
default: throw new System.IndexOutOfRangeException ();
27+
}
28+
}
29+
}
30+
31+
//Extension methods must be defined in a static class
32+
public static class Vector3Extension
33+
{
34+
// This is an extension method for the Vector3 class
35+
// The first parameter takes the "this" modifier
36+
// and specifies the type for which the method is defined.
37+
public static float Get (this Vector3 v, int id)
38+
{
39+
switch (id) {
40+
case 0: return v.x;
41+
case 1: return v.y;
42+
case 2: return v.z;
43+
default: throw new System.IndexOutOfRangeException ();
44+
}
45+
}
46+
}
47+
//Extension methods must be defined in a static class
48+
public static class AnimationCurveExtension
49+
{
50+
// This is an extension method for the AnimationCurve class
51+
// The first parameter takes the "this" modifier
52+
// and specifies the type for which the method is defined.
53+
public static void Dump (this AnimationCurve animCurve, string message, float [] keyTimesExpected = null, float [] keyValuesExpected = null)
54+
{
55+
int idx = 0;
56+
foreach (var key in animCurve.keys) {
57+
if (keyTimesExpected != null && keyValuesExpected != null) {
58+
Debug.Log (string.Format ("{5} keys[{0}] {1}({3}) {2} ({4})",
59+
idx, key.time, key.value,
60+
keyTimesExpected [idx], keyValuesExpected [idx],
61+
message));
62+
} else {
63+
Debug.Log (string.Format ("{3} keys[{0}] {1} {2}", idx, key.time, key.value, message));
64+
}
65+
idx++;
66+
}
67+
}
68+
}
69+
}
70+
1171
namespace Editor
1272
{
1373
public class ModelExporter : System.IDisposable
@@ -1076,8 +1136,9 @@ public void SetCurve(int i, AnimationCurve curve) {
10761136
Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
10771137
// Get the source pivot pre-rotation if any, so we can
10781138
// remove it from the animation we get from Unity.
1079-
var fbxPreRotationEuler = node.GetRotationActive() ? node.GetPreRotation(FbxNode.EPivotSet.eSourcePivot)
1080-
: new FbxVector4();
1139+
var fbxPreRotationEuler = node.GetRotationActive()
1140+
? node.GetPreRotation(FbxNode.EPivotSet.eSourcePivot)
1141+
: new FbxVector4();
10811142
var fbxPreRotationInverse = new FbxQuaternion();
10821143
fbxPreRotationInverse.ComposeSphericalXYZ(fbxPreRotationEuler);
10831144
fbxPreRotationInverse.Inverse();
@@ -1129,32 +1190,33 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
11291190
}
11301191

11311192
public void Animate(Transform unityTransform, FbxNode fbxNode, FbxAnimLayer fbxAnimLayer, bool Verbose) {
1193+
11321194
/* Find or create the three curves. */
1133-
var x = fbxNode.LclRotation.GetCurve(fbxAnimLayer, Globals.FBXSDK_CURVENODE_COMPONENT_X, true);
1134-
var y = fbxNode.LclRotation.GetCurve(fbxAnimLayer, Globals.FBXSDK_CURVENODE_COMPONENT_Y, true);
1135-
var z = fbxNode.LclRotation.GetCurve(fbxAnimLayer, Globals.FBXSDK_CURVENODE_COMPONENT_Z, true);
1195+
var fbxAnimCurveX = fbxNode.LclRotation.GetCurve(fbxAnimLayer, Globals.FBXSDK_CURVENODE_COMPONENT_X, true);
1196+
var fbxAnimCurveY = fbxNode.LclRotation.GetCurve(fbxAnimLayer, Globals.FBXSDK_CURVENODE_COMPONENT_Y, true);
1197+
var fbxAnimCurveZ = fbxNode.LclRotation.GetCurve(fbxAnimLayer, Globals.FBXSDK_CURVENODE_COMPONENT_Z, true);
11361198

11371199
/* set the keys */
1138-
x.KeyModifyBegin();
1139-
y.KeyModifyBegin();
1140-
z.KeyModifyBegin();
1200+
fbxAnimCurveX.KeyModifyBegin();
1201+
fbxAnimCurveY.KeyModifyBegin();
1202+
fbxAnimCurveZ.KeyModifyBegin();
11411203

11421204
var keys = ComputeKeys(unityTransform.localRotation, fbxNode);
11431205
for(int i = 0, n = keys.Length; i < n; ++i) {
11441206
var key = keys[i];
1145-
x.KeyAdd(key.time);
1146-
x.KeySet(i, key.time, (float)key.euler.X);
1207+
fbxAnimCurveX.KeyAdd(key.time);
1208+
fbxAnimCurveX.KeySet(i, key.time, (float)key.euler.X);
11471209

1148-
y.KeyAdd(key.time);
1149-
y.KeySet(i, key.time, (float)key.euler.Y);
1210+
fbxAnimCurveY.KeyAdd(key.time);
1211+
fbxAnimCurveY.KeySet(i, key.time, (float)key.euler.Y);
11501212

1151-
z.KeyAdd(key.time);
1152-
z.KeySet(i, key.time, (float)key.euler.Z);
1213+
fbxAnimCurveZ.KeyAdd(key.time);
1214+
fbxAnimCurveZ.KeySet(i, key.time, (float)key.euler.Z);
11531215
}
11541216

1155-
z.KeyModifyEnd();
1156-
y.KeyModifyEnd();
1157-
x.KeyModifyEnd();
1217+
fbxAnimCurveZ.KeyModifyEnd();
1218+
fbxAnimCurveY.KeyModifyEnd();
1219+
fbxAnimCurveX.KeyModifyEnd();
11581220

11591221
if (Verbose) {
11601222
Debug.Log("Exported rotation animation for " + fbxNode.GetName());
@@ -1212,6 +1274,11 @@ protected void ExportAnimationClip (AnimationClip unityAnimClip, GameObject unit
12121274
AnimationCurve unityAnimCurve = AnimationUtility.GetEditorCurve (unityAnimClip, unityCurveBinding);
12131275
if (unityAnimCurve == null) { continue; }
12141276

1277+
if (Verbose)
1278+
{
1279+
Debug.Log (string.Format ("Exporting animation curve bound to {0} {1}", unityCurveBinding.propertyName, unityCurveBinding.path));
1280+
}
1281+
12151282
int index = QuaternionCurve.GetQuaternionIndex (unityCurveBinding.propertyName);
12161283
if (index == -1)
12171284
{
@@ -1240,7 +1307,7 @@ protected void ExportAnimationClip (AnimationClip unityAnimClip, GameObject unit
12401307

12411308
FbxNode fbxNode;
12421309
if (!MapUnityObjectToFbxNode.TryGetValue (unityGo, out fbxNode)) {
1243-
Debug.LogError (string.Format ("no fbxnode found for '0'", unityGo.name));
1310+
Debug.LogError (string.Format ("no FbxNode found for {0}", unityGo.name));
12441311
continue;
12451312
}
12461313
quat.Animate (unityGo.transform, fbxNode, fbxAnimLayer, Verbose);

0 commit comments

Comments
 (0)