@@ -1445,12 +1445,55 @@ protected bool ExportLight (GameObject unityGo, FbxScene fbxScene, FbxNode fbxNo
1445
1445
}
1446
1446
1447
1447
/// <summary>
1448
- /// Export animation curve key frames with key tangents
1448
+ /// Return set of sample times to cover all keys on animation curves
1449
1449
/// </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 ,
1451
1494
UnityToMayaConvertSceneHelper convertSceneHelper )
1452
1495
{
1453
- // TODO: map key tangents mode between Unity and FBX
1496
+ // TODO: complete the mapping between key tangents modes Unity and FBX
1454
1497
Dictionary < AnimationUtility . TangentMode , List < FbxAnimCurveDef . ETangentMode > > MapUnityKeyTangentModeToFBX =
1455
1498
new Dictionary < AnimationUtility . TangentMode , List < FbxAnimCurveDef . ETangentMode > >
1456
1499
{
@@ -1504,32 +1547,14 @@ protected void ExportAnimationSamples (AnimationCurve uniAnimCurve, FbxAnimCurve
1504
1547
double sampleRate ,
1505
1548
UnityToMayaConvertSceneHelper convertSceneHelper )
1506
1549
{
1550
+
1507
1551
using ( new FbxAnimCurveModifyHelper ( new List < FbxAnimCurve > { fbxAnimCurve } ) )
1508
1552
{
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 ) )
1529
1554
{
1530
- float currSampleValue = uniAnimCurve . Evaluate ( ( float ) currSample ) ;
1555
+ float currSampleValue = uniAnimCurve . Evaluate ( ( float ) currSampleTime ) ;
1531
1556
1532
- var fbxTime = FbxTime . FromSecondDouble ( currSample ) ;
1557
+ var fbxTime = FbxTime . FromSecondDouble ( currSampleTime ) ;
1533
1558
1534
1559
int fbxKeyIndex = fbxAnimCurve . KeyAdd ( fbxTime ) ;
1535
1560
@@ -1608,7 +1633,7 @@ protected void ExportAnimationCurve (UnityEngine.Object uniObj,
1608
1633
}
1609
1634
else
1610
1635
{
1611
- ExportAnimationKeyFrames ( uniAnimCurve , fbxAnimCurve , convertSceneHelper ) ;
1636
+ ExportAnimationKeys ( uniAnimCurve , fbxAnimCurve , convertSceneHelper ) ;
1612
1637
}
1613
1638
}
1614
1639
@@ -1784,6 +1809,7 @@ public void Dispose()
1784
1809
/// from quaternion to euler. We use this class to help.
1785
1810
/// </summary>
1786
1811
class QuaternionCurve {
1812
+ public double sampleRate ;
1787
1813
public AnimationCurve x ;
1788
1814
public AnimationCurve y ;
1789
1815
public AnimationCurve z ;
@@ -1844,11 +1870,11 @@ Key [] ComputeKeys(UnityEngine.Quaternion restRotation, FbxNode node) {
1844
1870
var lclQuaternion = new FbxQuaternion ( restRotation . x , restRotation . y , restRotation . z , restRotation . w ) ;
1845
1871
1846
1872
// 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 ) ;
1852
1878
1853
1879
// Convert to the Key type.
1854
1880
var keys = new Key [ keyTimes . Count ] ;
@@ -1997,7 +2023,7 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
1997
2023
1998
2024
QuaternionCurve quat ;
1999
2025
if ( ! quaternions . TryGetValue ( uniGO , out quat ) ) {
2000
- quat = new QuaternionCurve ( ) ;
2026
+ quat = new QuaternionCurve { sampleRate = uniAnimClip . frameRate } ;
2001
2027
quaternions . Add ( uniGO , quat ) ;
2002
2028
}
2003
2029
quat . SetCurve ( index , uniAnimCurve ) ;
@@ -3027,7 +3053,7 @@ public void Dispose ()
3027
3053
{
3028
3054
}
3029
3055
3030
- public bool Verbose { private set { ; } get { return true ; } }
3056
+ public bool Verbose { private set { ; } get { return false ; } }
3031
3057
3032
3058
/// <summary>
3033
3059
/// manage the selection of a filename
0 commit comments