@@ -671,47 +671,49 @@ public static bool NearlyEqual(float a, float b, float epsilon)
671
671
public static void setSteppedInterval ( AnimationCurve curve , int i , int nextI ) {
672
672
673
673
if ( curve . keys [ i ] . value == curve . keys [ nextI ] . value ) {
674
- setLinearInterval ( curve , i , nextI ) ;
675
674
return ;
676
675
}
677
676
678
677
object thisKeyframeBoxed = curve [ i ] ;
679
678
object nextKeyframeBoxed = curve [ nextI ] ;
680
679
681
- KeyframeUtil . SetKeyBroken ( thisKeyframeBoxed , true ) ;
682
- KeyframeUtil . SetKeyBroken ( nextKeyframeBoxed , true ) ;
680
+ if ( ! KeyframeUtil . isKeyBroken ( thisKeyframeBoxed ) )
681
+ KeyframeUtil . SetKeyBroken ( thisKeyframeBoxed , true ) ;
682
+ if ( ! KeyframeUtil . isKeyBroken ( nextKeyframeBoxed ) )
683
+ KeyframeUtil . SetKeyBroken ( nextKeyframeBoxed , true ) ;
683
684
684
685
KeyframeUtil . SetKeyTangentMode ( thisKeyframeBoxed , 1 , TangentMode . Stepped ) ;
685
686
KeyframeUtil . SetKeyTangentMode ( nextKeyframeBoxed , 0 , TangentMode . Stepped ) ;
686
687
687
688
Keyframe thisKeyframe = ( Keyframe ) thisKeyframeBoxed ;
688
689
Keyframe nextKeyframe = ( Keyframe ) nextKeyframeBoxed ;
689
690
thisKeyframe . outTangent = float . PositiveInfinity ;
691
+ nextKeyframe . inTangent = float . PositiveInfinity ;
690
692
curve . MoveKey ( i , thisKeyframe ) ;
691
693
curve . MoveKey ( nextI , nextKeyframe ) ;
692
694
}
693
695
694
696
695
697
public static void setLinearInterval ( AnimationCurve curve , int i , int nextI ) {
696
- object thisKeyframeBoxed = curve [ i ] ;
697
- object nextKeyframeBoxed = curve [ nextI ] ;
698
- KeyframeUtil . SetKeyBroken ( thisKeyframeBoxed , true ) ;
699
- KeyframeUtil . SetKeyBroken ( nextKeyframeBoxed , true ) ;
698
+ Keyframe thisKeyframe = curve [ i ] ;
699
+ Keyframe nextKeyframe = curve [ nextI ] ;
700
+ thisKeyframe . outTangent = CurveExtension . CalculateLinearTangent ( curve , i , nextI ) ;
701
+ nextKeyframe . inTangent = CurveExtension . CalculateLinearTangent ( curve , nextI , i ) ;
700
702
701
- KeyframeUtil . SetKeyTangentMode ( thisKeyframeBoxed , 1 , TangentMode . Linear ) ;
702
- KeyframeUtil . SetKeyTangentMode ( nextKeyframeBoxed , 0 , TangentMode . Linear ) ;
703
+ KeyframeUtil . SetKeyBroken ( ( object ) thisKeyframe , true ) ;
704
+ KeyframeUtil . SetKeyBroken ( ( object ) nextKeyframe , true ) ;
705
+
706
+ KeyframeUtil . SetKeyTangentMode ( ( object ) thisKeyframe , 1 , TangentMode . Linear ) ;
707
+ KeyframeUtil . SetKeyTangentMode ( ( object ) nextKeyframe , 0 , TangentMode . Linear ) ;
703
708
704
- Keyframe thisKeyframe = ( Keyframe ) thisKeyframeBoxed ;
705
- Keyframe nextKeyframe = ( Keyframe ) nextKeyframeBoxed ;
706
709
707
- thisKeyframe . outTangent = CurveExtension . CalculateLinearTangent ( curve , i , nextI ) ;
708
- nextKeyframe . inTangent = CurveExtension . CalculateLinearTangent ( curve , nextI , i ) ;
709
710
curve . MoveKey ( i , thisKeyframe ) ;
710
711
curve . MoveKey ( nextI , nextKeyframe ) ;
711
712
}
712
713
713
714
714
- public static void addBoneAnimationToClip ( AnimationClip clip , Dictionary < string , SpineBoneAnimation > bonesAnimation , SpineData spineData , Dictionary < string , GameObject > boneGOByName , float ratio ) {
715
+ public static void addBoneAnimationToClip ( AnimationClip clip , Dictionary < string , SpineBoneAnimation > bonesAnimation ,
716
+ SpineData spineData , Dictionary < string , GameObject > boneGOByName , float ratio ) {
715
717
foreach ( KeyValuePair < string , SpineBoneAnimation > kvp in bonesAnimation ) {
716
718
string boneName = kvp . Key ;
717
719
GameObject boneGO = boneGOByName [ boneName ] ;
@@ -731,15 +733,16 @@ public static void addBoneAnimationToClip(AnimationClip clip, Dictionary<string,
731
733
732
734
setTangents ( curveX , curveData ) ;
733
735
setTangents ( curveY , curveData ) ;
734
- AnimationUtility . SetEditorCurve ( clip , EditorCurveBinding . FloatCurve ( bonePath , typeof ( Transform ) , "m_LocalPosition.x" ) , curveX ) ;
735
- AnimationUtility . SetEditorCurve ( clip , EditorCurveBinding . FloatCurve ( bonePath , typeof ( Transform ) , "m_LocalPosition.y" ) , curveY ) ;
736
+ AnimationUtility . SetEditorCurve ( clip , EditorCurveBinding . FloatCurve ( bonePath , typeof ( Transform ) , "m_LocalPosition.x" ) , curveX ) ;
737
+ AnimationUtility . SetEditorCurve ( clip , EditorCurveBinding . FloatCurve ( bonePath , typeof ( Transform ) , "m_LocalPosition.y" ) , curveY ) ;
738
+
739
+
736
740
}
737
741
738
742
if ( boneAnimation . rotate != null && boneAnimation . rotate . Count > 0 ) {
739
- AnimationCurve rotationX = new AnimationCurve ( ) ; // next 4 is quaternion values
740
- AnimationCurve rotationY = new AnimationCurve ( ) ;
741
- AnimationCurve rotationZ = new AnimationCurve ( ) ;
742
- AnimationCurve rotationW = new AnimationCurve ( ) ;
743
+ AnimationCurve localRotationZ = new AnimationCurve ( ) ;
744
+ AnimationCurve localRotationZero = new AnimationCurve ( ) ;
745
+
743
746
JsonData [ ] curveData = new JsonData [ boneAnimation . rotate . Count ] ;
744
747
for ( int i = 0 ; i < boneAnimation . rotate . Count ; i ++ ) {
745
748
float origAngle = ( float ) boneAnimation . rotate [ i ] . angle ;
@@ -750,28 +753,23 @@ public static void addBoneAnimationToClip(AnimationClip clip, Dictionary<string,
750
753
751
754
float newZ = boneGO . transform . localRotation . eulerAngles . z + origAngle ;
752
755
753
-
754
756
Quaternion angle = Quaternion . Euler ( 0 , 0 , newZ ) ;
755
757
float time = ( float ) boneAnimation . rotate [ i ] . time ;
756
758
757
759
curveData [ i ] = boneAnimation . rotate [ i ] . curve ;
758
760
759
- rotationX . AddKey ( new Keyframe ( time , angle . x ) ) ;
760
- rotationY . AddKey ( new Keyframe ( time , angle . y ) ) ;
761
- rotationZ . AddKey ( new Keyframe ( time , angle . z ) ) ;
762
- rotationW . AddKey ( new Keyframe ( time , angle . w ) ) ;
761
+ localRotationZ . AddKey ( new Keyframe ( time , newZ ) ) ;
762
+ localRotationZero . AddKey ( new Keyframe ( time , 0 ) ) ;
763
763
}
764
764
765
+ fixAngles ( localRotationZ , curveData ) ;
766
+ setTangents ( localRotationZ , curveData ) ;
767
+ setTangents ( localRotationZero , curveData ) ;
765
768
766
- setTangents ( rotationX , curveData ) ;
767
- setTangents ( rotationY , curveData ) ;
768
- setTangents ( rotationZ , curveData ) ;
769
- setTangents ( rotationW , curveData ) ;
770
-
771
- clip . SetCurve ( bonePath , typeof ( Transform ) , "localRotation.x" , rotationX ) ;
772
- clip . SetCurve ( bonePath , typeof ( Transform ) , "localRotation.y" , rotationY ) ;
773
- clip . SetCurve ( bonePath , typeof ( Transform ) , "localRotation.z" , rotationZ ) ;
774
- clip . SetCurve ( bonePath , typeof ( Transform ) , "localRotation.w" , rotationW ) ;
769
+ AnimationUtility . SetEditorCurve ( clip , EditorCurveBinding . FloatCurve ( bonePath , typeof ( Transform ) , "localEulerAnglesBaked.x" ) , localRotationZero ) ;
770
+ AnimationUtility . SetEditorCurve ( clip , EditorCurveBinding . FloatCurve ( bonePath , typeof ( Transform ) , "localEulerAnglesBaked.y" ) , new AnimationCurve ( localRotationZero . keys ) ) ;
771
+ AnimationUtility . SetEditorCurve ( clip , EditorCurveBinding . FloatCurve ( bonePath , typeof ( Transform ) , "localEulerAnglesBaked.z" ) , localRotationZ ) ;
772
+ //AnimationUtility.SetEditorCurve(clip,bonePath,typeof(Transform),"localEulerAngles.z",localRotationZ);
775
773
}
776
774
777
775
if ( boneAnimation . scale != null && boneAnimation . scale . Count > 0 ) {
@@ -797,6 +795,32 @@ public static void addBoneAnimationToClip(AnimationClip clip, Dictionary<string,
797
795
}
798
796
799
797
798
+ static void fixAngles ( AnimationCurve curve , JsonData [ ] curveData ) {
799
+ if ( curve . keys . Length < 3 )
800
+ return ;
801
+ int fullTurn = 0 ;
802
+ bool forward = true ;
803
+ float currValue , previousValue , diff ;
804
+ for ( int previousI = 0 , i = 1 ; i < curve . keys . Length ; previousI = i ++ ) {
805
+ if ( curveData [ previousI ] != null && curveData [ previousI ] . IsString && ( ( string ) curveData [ previousI ] ) . Equals ( "stepped" ) )
806
+ continue ;
807
+
808
+ currValue = curve . keys [ i ] . value ;
809
+ previousValue = curve . keys [ previousI ] . value ;
810
+
811
+ while ( ( currValue - previousValue ) > 180 ) {
812
+ currValue -= 360 ;
813
+ }
814
+
815
+ while ( ( currValue - previousValue ) < - 180 ) {
816
+ currValue += 360 ;
817
+ }
818
+ if ( curve . keys [ i ] . value != currValue ) {
819
+ curve . MoveKey ( i , new Keyframe ( curve . keys [ i ] . time , currValue ) ) ;
820
+ }
821
+ }
822
+ }
823
+
800
824
801
825
public static AnimationClip AddClipToAnimatorComponent ( GameObject animatedObject , AnimationClip newClip )
802
826
{
0 commit comments