Skip to content

Commit a65b897

Browse files
committed
polish
1 parent 4904af7 commit a65b897

File tree

2 files changed

+73
-35
lines changed

2 files changed

+73
-35
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,12 +1445,55 @@ protected bool ExportLight (GameObject unityGo, FbxScene fbxScene, FbxNode fbxNo
14451445
}
14461446

14471447
/// <summary>
1448-
/// Export animation curve key frames with key tangents
1448+
/// Return set of sample times to cover all keys on animation curves
14491449
/// </summary>
1450-
protected void ExportAnimationKeyFrames (AnimationCurve uniAnimCurve, FbxAnimCurve fbxAnimCurve,
1450+
public static HashSet<float> GetSampleTimes(AnimationCurve[] animCurves, double sampleRate)
1451+
{
1452+
var keyTimes = new HashSet<float>();
1453+
double fs = 1.0/sampleRate;
1454+
1455+
double currSample = double.MaxValue, firstTime = double.MaxValue, lastTime = double.MinValue;
1456+
1457+
foreach (var ac in animCurves)
1458+
{
1459+
if (ac==null || ac.length<=0) continue;
1460+
1461+
firstTime = System.Math.Min(firstTime, ac[0].time);
1462+
lastTime = System.Math.Max(lastTime, ac[ac.length-1].time);
1463+
}
1464+
1465+
for (currSample = firstTime; currSample < lastTime; currSample += fs)
1466+
{
1467+
keyTimes.Add((float)currSample);
1468+
}
1469+
1470+
return keyTimes;
1471+
}
1472+
1473+
/// <summary>
1474+
/// Return set of all keys times on animation curves
1475+
/// </summary>
1476+
public static HashSet<float> GetKeyTimes(AnimationCurve[] animCurves)
1477+
{
1478+
var keyTimes = new HashSet<float>();
1479+
1480+
foreach (var ac in animCurves)
1481+
{
1482+
if (ac!=null) foreach(var key in ac.keys) { keyTimes.Add(key.time); }
1483+
}
1484+
1485+
return keyTimes;
1486+
}
1487+
1488+
/// <summary>
1489+
/// Export animation curve key frames with key tangents
1490+
/// NOTE : This is a work in progress (WIP). We only export the key time and value on
1491+
/// a Cubic curve using the default tangents.
1492+
/// </summary>
1493+
protected void ExportAnimationKeys (AnimationCurve uniAnimCurve, FbxAnimCurve fbxAnimCurve,
14511494
UnityToMayaConvertSceneHelper convertSceneHelper)
14521495
{
1453-
// TODO: map key tangents mode between Unity and FBX
1496+
// TODO: complete the mapping between key tangents modes Unity and FBX
14541497
Dictionary<AnimationUtility.TangentMode, List<FbxAnimCurveDef.ETangentMode>> MapUnityKeyTangentModeToFBX =
14551498
new Dictionary<AnimationUtility.TangentMode, List<FbxAnimCurveDef.ETangentMode>>
14561499
{
@@ -1504,32 +1547,14 @@ protected void ExportAnimationSamples (AnimationCurve uniAnimCurve, FbxAnimCurve
15041547
double sampleRate,
15051548
UnityToMayaConvertSceneHelper convertSceneHelper)
15061549
{
1550+
15071551
using (new FbxAnimCurveModifyHelper(new List<FbxAnimCurve>{fbxAnimCurve}))
15081552
{
1509-
double fs = 1.0/sampleRate;
1510-
int numKeys = uniAnimCurve.length;
1511-
1512-
int numSamples = 0;
1513-
int sampleIndex = 0;
1514-
double currSample = double.MaxValue, firstTime = double.MaxValue, lastTime = double.MaxValue;
1515-
1516-
if (numKeys>0)
1517-
{
1518-
firstTime = uniAnimCurve[0].time;
1519-
lastTime = uniAnimCurve[uniAnimCurve.length-1].time;
1520-
1521-
numSamples = (int)((float)((lastTime-firstTime) * sampleRate));
1522-
Debug.Log(string.Format("Exporting Animation Samples : firstTime={0}, lastTime={1} frameRate={2} numSamples={3}",
1523-
firstTime, lastTime, sampleRate, numSamples));
1524-
1525-
currSample = firstTime;
1526-
}
1527-
1528-
for (sampleIndex = 0, currSample = firstTime; currSample < lastTime; ++sampleIndex, currSample += fs)
1553+
foreach (var currSampleTime in GetSampleTimes(new AnimationCurve[]{uniAnimCurve}, sampleRate))
15291554
{
1530-
float currSampleValue = uniAnimCurve.Evaluate((float)currSample);
1555+
float currSampleValue = uniAnimCurve.Evaluate((float)currSampleTime);
15311556

1532-
var fbxTime = FbxTime.FromSecondDouble (currSample);
1557+
var fbxTime = FbxTime.FromSecondDouble (currSampleTime);
15331558

15341559
int fbxKeyIndex = fbxAnimCurve.KeyAdd (fbxTime);
15351560

@@ -1608,7 +1633,7 @@ protected void ExportAnimationCurve (UnityEngine.Object uniObj,
16081633
}
16091634
else
16101635
{
1611-
ExportAnimationKeyFrames(uniAnimCurve, fbxAnimCurve, convertSceneHelper);
1636+
ExportAnimationKeys(uniAnimCurve, fbxAnimCurve, convertSceneHelper);
16121637
}
16131638
}
16141639

@@ -1784,6 +1809,7 @@ public void Dispose()
17841809
/// from quaternion to euler. We use this class to help.
17851810
/// </summary>
17861811
class QuaternionCurve {
1812+
public double sampleRate;
17871813
public AnimationCurve x;
17881814
public AnimationCurve y;
17891815
public AnimationCurve z;
@@ -1844,11 +1870,11 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
18441870
var lclQuaternion = new FbxQuaternion(restRotation.x, restRotation.y, restRotation.z, restRotation.w);
18451871

18461872
// Find when we have keys set.
1847-
var keyTimes = new HashSet<float>();
1848-
if (x != null) { foreach(var key in x.keys) { keyTimes.Add(key.time); } }
1849-
if (y != null) { foreach(var key in y.keys) { keyTimes.Add(key.time); } }
1850-
if (z != null) { foreach(var key in z.keys) { keyTimes.Add(key.time); } }
1851-
if (w != null) { foreach(var key in w.keys) { keyTimes.Add(key.time); } }
1873+
var animCurves = new AnimationCurve[]{x,y,z,w};
1874+
var keyTimes =
1875+
(FbxExporters.Editor.ModelExporter.ExportSettings.BakeAnimation)
1876+
? ModelExporter.GetSampleTimes(animCurves, sampleRate)
1877+
: ModelExporter.GetKeyTimes(animCurves);
18521878

18531879
// Convert to the Key type.
18541880
var keys = new Key[keyTimes.Count];
@@ -1997,7 +2023,7 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
19972023

19982024
QuaternionCurve quat;
19992025
if (!quaternions.TryGetValue (uniGO, out quat)) {
2000-
quat = new QuaternionCurve ();
2026+
quat = new QuaternionCurve {sampleRate = uniAnimClip.frameRate};
20012027
quaternions.Add (uniGO, quat);
20022028
}
20032029
quat.SetCurve (index, uniAnimCurve);
@@ -3027,7 +3053,7 @@ public void Dispose ()
30273053
{
30283054
}
30293055

3030-
public bool Verbose { private set {;} get { return true; } }
3056+
public bool Verbose { private set {;} get { return false; } }
30313057

30323058
/// <summary>
30333059
/// manage the selection of a filename

Assets/FbxExporters/Editor/UnitTests/FbxAnimationTest.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,14 @@ public int CompareKeyValue(Keyframe a, Keyframe b)
273273
bool result = true;
274274

275275
result &= a.time.Equals(b.time);
276+
#if DEBUG_UNITTEST
276277
Debug.Log(string.Format("{2} a.time: {0}, b.time: {1}", a.time, b.time,result));
278+
#endif
277279

278280
result &= (Mathf.Abs(a.value - b.value) <= Epsilon);
281+
#if DEBUG_UNITTEST
279282
Debug.Log(string.Format("{2} a.value: {0}, b.value: {1}", a.value, b.value,result));
283+
#endif
280284

281285
return result ? 0 : 1;
282286
}
@@ -294,15 +298,17 @@ public int CompareKeyTangents(Keyframe a, Keyframe b)
294298
bool result = true;
295299

296300
result &= a.time.Equals(b.time);
301+
#if DEBUG_UNITTEST
297302
Debug.Log(string.Format("{2} a.time: {0}, b.time: {1}", a.time, b.time,result));
298-
303+
#endif
299304
// TODO : use AnimationUtility.GetLeftTangentMode
300305
// requires reference to AnimationCurve and keyindex
301306
result &= (a.tangentMode == b.tangentMode);
307+
#if DEBUG_UNITTEST
302308
Debug.Log(string.Format("{2} a.tangentMode={0} b.tangentMode={1}",
303309
((AnimationUtility.TangentMode)a.tangentMode).ToString(),
304310
((AnimationUtility.TangentMode)b.tangentMode).ToString(),result));
305-
311+
#endif
306312
return result ? 0 : 1;
307313
}
308314

@@ -712,11 +718,15 @@ public int KeyTangentsAnimTest (float [] keyTimesInSeconds, Vector3 [] keyPosVal
712718
[Test, TestCaseSource (typeof (AnimationTestDataClass), "ComponentTestCases")]
713719
public int ComponentAnimTest (System.Type componentType)
714720
{
721+
#if DEBUG_UNITTEST
715722
Debug.Log (string.Format ("ComponentAnimTest {0}", componentType.ToString()));
723+
#endif
716724

717725
if (!ModelExporter.MapsToFbxObject.ContainsKey(componentType))
718726
{
727+
#if DEBUG_UNITTEST
719728
Debug.Log (string.Format ("skipping {0}; fbx export not supported", componentType.ToString()));
729+
#endif
720730
return 1;
721731
}
722732

@@ -729,7 +739,9 @@ public int ComponentAnimTest (System.Type componentType)
729739

730740
if (propertyNames.Length == 0)
731741
{
742+
#if DEBUG_UNITTEST
732743
Debug.Log (string.Format ("skipping {0}; no animatable Single properties found", componentType.ToString()));
744+
#endif
733745
return 1;
734746
}
735747

0 commit comments

Comments
 (0)