@@ -915,9 +915,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
915915 foreach ( KeyValuePair < string , PropertyCurve > c in curves )
916916 {
917917 var prop = c . Value ;
918- if ( BakePropertyAnimation ( prop , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values ) )
918+ if ( BakePropertyAnimation ( prop , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values , out var interpolationType ) )
919919 {
920- AddAnimationData ( prop . target , prop . propertyName , animation , times , values ) ;
920+ AddAnimationData ( prop . target , prop . propertyName , animation , times , values , interpolationType ) ;
921921 sampledAnimationData = true ;
922922 }
923923 }
@@ -932,9 +932,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
932932 {
933933 var trp2 = new PropertyCurve ( targetTr , "translation" ) { propertyType = typeof ( Vector3 ) } ;
934934 trp2 . curve . AddRange ( curve . translationCurves ) ;
935- if ( BakePropertyAnimation ( trp2 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values2 ) )
935+ if ( BakePropertyAnimation ( trp2 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values2 , out var interpolationType ) )
936936 {
937- AddAnimationData ( targetTr , trp2 . propertyName , animation , times , values2 ) ;
937+ AddAnimationData ( targetTr , trp2 . propertyName , animation , times , values2 , interpolationType ) ;
938938 sampledAnimationData = true ;
939939 }
940940 }
@@ -943,9 +943,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
943943 {
944944 var trp3 = new PropertyCurve ( targetTr , "rotation" ) { propertyType = typeof ( Quaternion ) } ;
945945 trp3 . curve . AddRange ( curve . rotationCurves . Where ( x => x != null ) ) ;
946- if ( BakePropertyAnimation ( trp3 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values3 ) )
946+ if ( BakePropertyAnimation ( trp3 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values3 , out var interpolationType ) )
947947 {
948- AddAnimationData ( targetTr , trp3 . propertyName , animation , times , values3 ) ;
948+ AddAnimationData ( targetTr , trp3 . propertyName , animation , times , values3 , interpolationType ) ;
949949 sampledAnimationData = true ;
950950 }
951951
@@ -955,9 +955,9 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
955955 {
956956 var trp4 = new PropertyCurve ( targetTr , "scale" ) { propertyType = typeof ( Vector3 ) } ;
957957 trp4 . curve . AddRange ( curve . scaleCurves ) ;
958- if ( BakePropertyAnimation ( trp4 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values4 ) )
958+ if ( BakePropertyAnimation ( trp4 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values4 , out var interpolationType ) )
959959 {
960- AddAnimationData ( targetTr , trp4 . propertyName , animation , times , values4 ) ;
960+ AddAnimationData ( targetTr , trp4 . propertyName , animation , times , values4 , interpolationType ) ;
961961 sampledAnimationData = true ;
962962 }
963963 }
@@ -966,10 +966,10 @@ private void ConvertClipToGLTFAnimation(AnimationClip clip, Transform transform,
966966 {
967967 var trp5 = new PropertyCurve ( targetTr , "weights" ) { propertyType = typeof ( float ) } ;
968968 trp5 . curve . AddRange ( curve . weightCurves . Values ) ;
969- if ( BakePropertyAnimation ( trp5 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values5 ) )
969+ if ( BakePropertyAnimation ( trp5 , clip . length , AnimationBakingFramerate , speedMultiplier , out times , out var values5 , out var interpolationType ) )
970970 {
971971 var targetComponent = targetTr . GetComponent < SkinnedMeshRenderer > ( ) ;
972- AddAnimationData ( targetComponent , trp5 . propertyName , animation , times , values5 ) ;
972+ AddAnimationData ( targetComponent , trp5 . propertyName , animation , times , values5 , interpolationType ) ;
973973 sampledAnimationData = true ;
974974 }
975975 }
@@ -1398,12 +1398,13 @@ private AnimationCurve CreateConstantCurve(float value, float endTime)
13981398 return curve ;
13991399 }
14001400
1401- private bool BakePropertyAnimation ( PropertyCurve prop , float length , float bakingFramerate , float speedMultiplier , out float [ ] times , out object [ ] values )
1401+ private bool BakePropertyAnimation ( PropertyCurve prop , float length , float bakingFramerate , float speedMultiplier , out float [ ] times , out object [ ] values , out InterpolationType interpolationType )
14021402 {
14031403 var isReverse = speedMultiplier < 0 ;
14041404 speedMultiplier = Mathf . Abs ( speedMultiplier ) ;
14051405 times = null ;
14061406 values = null ;
1407+ interpolationType = InterpolationType . LINEAR ;
14071408
14081409 prop . SortCurves ( ) ;
14091410 if ( ! prop . Validate ( ) ) return false ;
@@ -1418,6 +1419,20 @@ private bool BakePropertyAnimation(PropertyCurve prop, float length, float bakin
14181419 var keyframes = prop . curve . Select ( x => x . keys ) . ToArray ( ) ;
14191420 var keyframeIndex = new int [ curveCount ] ;
14201421
1422+ // Check if all samples are constant
1423+ var allConstant = true ;
1424+ for ( int i = 0 ; i < keyframes . Length ; i ++ )
1425+ {
1426+ var kf = keyframes [ i ] ;
1427+ for ( var k = 0 ; k < kf . Length ; k ++ )
1428+ allConstant |= float . IsInfinity ( kf [ k ] . inTangent ) ;
1429+
1430+ if ( ! allConstant ) break ;
1431+ }
1432+
1433+ if ( allConstant )
1434+ interpolationType = InterpolationType . STEP ;
1435+
14211436 // Assuming all the curves exist now
14221437 for ( var i = 0 ; i < nbSamples ; ++ i )
14231438 {
@@ -1429,8 +1444,11 @@ private bool BakePropertyAnimation(PropertyCurve prop, float length, float bakin
14291444 keyframeIndex [ k ] ++ ;
14301445
14311446 var isConstant = false ;
1432- for ( var k = 0 ; k < curveCount ; k ++ )
1433- isConstant |= float . IsInfinity ( keyframes [ k ] [ keyframeIndex [ k ] ] . inTangent ) ;
1447+ if ( interpolationType != InterpolationType . STEP )
1448+ {
1449+ for ( var k = 0 ; k < curveCount ; k ++ )
1450+ isConstant |= float . IsInfinity ( keyframes [ k ] [ keyframeIndex [ k ] ] . inTangent ) ;
1451+ }
14341452
14351453 if ( isConstant && _times . Count > 0 )
14361454 {
0 commit comments