Skip to content

Commit cbed08e

Browse files
committed
#11 rotation full round fix
1 parent c1bb050 commit cbed08e

File tree

1 file changed

+58
-34
lines changed

1 file changed

+58
-34
lines changed

Assets/UnitySpineImporter/Scripts/Editor/Util/SpineUtil.cs

+58-34
Original file line numberDiff line numberDiff line change
@@ -671,47 +671,49 @@ public static bool NearlyEqual(float a, float b, float epsilon)
671671
public static void setSteppedInterval(AnimationCurve curve, int i, int nextI){
672672

673673
if (curve.keys[i].value == curve.keys[nextI].value){
674-
setLinearInterval(curve, i, nextI);
675674
return;
676675
}
677676

678677
object thisKeyframeBoxed = curve[i];
679678
object nextKeyframeBoxed = curve[nextI];
680679

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);
683684

684685
KeyframeUtil.SetKeyTangentMode(thisKeyframeBoxed, 1, TangentMode.Stepped);
685686
KeyframeUtil.SetKeyTangentMode(nextKeyframeBoxed, 0, TangentMode.Stepped);
686687

687688
Keyframe thisKeyframe = (Keyframe)thisKeyframeBoxed;
688689
Keyframe nextKeyframe = (Keyframe)nextKeyframeBoxed;
689690
thisKeyframe.outTangent = float.PositiveInfinity;
691+
nextKeyframe.inTangent = float.PositiveInfinity;
690692
curve.MoveKey(i, thisKeyframe);
691693
curve.MoveKey(nextI, nextKeyframe);
692694
}
693695

694696

695697
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);
700702

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);
703708

704-
Keyframe thisKeyframe = (Keyframe)thisKeyframeBoxed;
705-
Keyframe nextKeyframe = (Keyframe)nextKeyframeBoxed;
706709

707-
thisKeyframe.outTangent = CurveExtension.CalculateLinearTangent(curve, i, nextI);
708-
nextKeyframe.inTangent = CurveExtension.CalculateLinearTangent(curve, nextI, i);
709710
curve.MoveKey(i, thisKeyframe);
710711
curve.MoveKey(nextI, nextKeyframe);
711712
}
712713

713714

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){
715717
foreach(KeyValuePair<string,SpineBoneAnimation> kvp in bonesAnimation){
716718
string boneName = kvp.Key;
717719
GameObject boneGO = boneGOByName[boneName];
@@ -731,15 +733,16 @@ public static void addBoneAnimationToClip(AnimationClip clip, Dictionary<string,
731733

732734
setTangents(curveX, curveData);
733735
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+
736740
}
737741

738742
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+
743746
JsonData[] curveData = new JsonData[boneAnimation.rotate.Count];
744747
for (int i = 0; i < boneAnimation.rotate.Count; i++) {
745748
float origAngle = (float)boneAnimation.rotate[i].angle;
@@ -750,28 +753,23 @@ public static void addBoneAnimationToClip(AnimationClip clip, Dictionary<string,
750753

751754
float newZ = boneGO.transform.localRotation.eulerAngles.z + origAngle;
752755

753-
754756
Quaternion angle = Quaternion.Euler(0,0,newZ);
755757
float time = (float)boneAnimation.rotate[i].time;
756758

757759
curveData[i] = boneAnimation.rotate[i].curve;
758760

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));
763763
}
764764

765+
fixAngles (localRotationZ , curveData);
766+
setTangents(localRotationZ , curveData);
767+
setTangents(localRotationZero, curveData);
765768

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);
775773
}
776774

777775
if (boneAnimation.scale != null && boneAnimation.scale.Count > 0){
@@ -797,6 +795,32 @@ public static void addBoneAnimationToClip(AnimationClip clip, Dictionary<string,
797795
}
798796

799797

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+
800824

801825
public static AnimationClip AddClipToAnimatorComponent(GameObject animatedObject, AnimationClip newClip)
802826
{

0 commit comments

Comments
 (0)