|
8 | 8 |
|
9 | 9 | namespace FbxExporters
|
10 | 10 | {
|
| 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 | + |
11 | 71 | namespace Editor
|
12 | 72 | {
|
13 | 73 | public class ModelExporter : System.IDisposable
|
@@ -1076,8 +1136,9 @@ public void SetCurve(int i, AnimationCurve curve) {
|
1076 | 1136 | Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
|
1077 | 1137 | // Get the source pivot pre-rotation if any, so we can
|
1078 | 1138 | // 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(); |
1081 | 1142 | var fbxPreRotationInverse = new FbxQuaternion();
|
1082 | 1143 | fbxPreRotationInverse.ComposeSphericalXYZ(fbxPreRotationEuler);
|
1083 | 1144 | fbxPreRotationInverse.Inverse();
|
@@ -1129,32 +1190,33 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
|
1129 | 1190 | }
|
1130 | 1191 |
|
1131 | 1192 | public void Animate(Transform unityTransform, FbxNode fbxNode, FbxAnimLayer fbxAnimLayer, bool Verbose) {
|
| 1193 | + |
1132 | 1194 | /* 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); |
1136 | 1198 |
|
1137 | 1199 | /* set the keys */
|
1138 |
| - x.KeyModifyBegin(); |
1139 |
| - y.KeyModifyBegin(); |
1140 |
| - z.KeyModifyBegin(); |
| 1200 | + fbxAnimCurveX.KeyModifyBegin(); |
| 1201 | + fbxAnimCurveY.KeyModifyBegin(); |
| 1202 | + fbxAnimCurveZ.KeyModifyBegin(); |
1141 | 1203 |
|
1142 | 1204 | var keys = ComputeKeys(unityTransform.localRotation, fbxNode);
|
1143 | 1205 | for(int i = 0, n = keys.Length; i < n; ++i) {
|
1144 | 1206 | 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); |
1147 | 1209 |
|
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); |
1150 | 1212 |
|
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); |
1153 | 1215 | }
|
1154 | 1216 |
|
1155 |
| - z.KeyModifyEnd(); |
1156 |
| - y.KeyModifyEnd(); |
1157 |
| - x.KeyModifyEnd(); |
| 1217 | + fbxAnimCurveZ.KeyModifyEnd(); |
| 1218 | + fbxAnimCurveY.KeyModifyEnd(); |
| 1219 | + fbxAnimCurveX.KeyModifyEnd(); |
1158 | 1220 |
|
1159 | 1221 | if (Verbose) {
|
1160 | 1222 | Debug.Log("Exported rotation animation for " + fbxNode.GetName());
|
@@ -1212,6 +1274,11 @@ protected void ExportAnimationClip (AnimationClip unityAnimClip, GameObject unit
|
1212 | 1274 | AnimationCurve unityAnimCurve = AnimationUtility.GetEditorCurve (unityAnimClip, unityCurveBinding);
|
1213 | 1275 | if (unityAnimCurve == null) { continue; }
|
1214 | 1276 |
|
| 1277 | + if (Verbose) |
| 1278 | + { |
| 1279 | + Debug.Log (string.Format ("Exporting animation curve bound to {0} {1}", unityCurveBinding.propertyName, unityCurveBinding.path)); |
| 1280 | + } |
| 1281 | + |
1215 | 1282 | int index = QuaternionCurve.GetQuaternionIndex (unityCurveBinding.propertyName);
|
1216 | 1283 | if (index == -1)
|
1217 | 1284 | {
|
@@ -1240,7 +1307,7 @@ protected void ExportAnimationClip (AnimationClip unityAnimClip, GameObject unit
|
1240 | 1307 |
|
1241 | 1308 | FbxNode fbxNode;
|
1242 | 1309 | 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)); |
1244 | 1311 | continue;
|
1245 | 1312 | }
|
1246 | 1313 | quat.Animate (unityGo.transform, fbxNode, fbxAnimLayer, Verbose);
|
|
0 commit comments