Vertx » Unity – AnimationCurve.Lerp
MENU
+61430642190

Unity – AnimationCurve.Lerp

0
1000
1

If you’re looking for a lerp for AnimationCurve take mine!

During the development of some extremely specific animation work in Unity I had the need to write this code.

/*Thomas Ingram 2016*/
using UnityEngine;
using System.Collections;

public static class CurveExtensions {
	public static AnimationCurve Lerp (this AnimationCurve curve1, AnimationCurve curve2, float t){
		AnimationCurve curve1_ToLerp = new AnimationCurve(curve1.keys);
		AnimationCurve curve2_ToLerp = new AnimationCurve(curve2.keys);
		for(int i = 0; i<curve2.keys.Length; i++){
			bool found = false;
			for(int j = 0; j<curve1_ToLerp.length; j++){
				if(curve2.keys[i].time == curve1_ToLerp.keys[j].time){
					found = true;
				}
			}
			if(!found){
				curve1_ToLerp.AddKey(curve2.keys[i].time, curve1.Evaluate(curve2.keys[i].time));
			}
		}

		for(int i = 0; i<curve1.keys.Length; i++){
			bool found = false;
			for(int j = 0; j<curve2_ToLerp.length; j++){
				if(curve1.keys[i].time == curve2_ToLerp.keys[j].time){
					found = true;
				}
			}
			if(!found){
				curve2_ToLerp.AddKey(curve1.keys[i].time, curve2.Evaluate(curve1.keys[i].time));
			}
		}

		AnimationCurve newCurve = new AnimationCurve(curve1_ToLerp.keys);
		for(int i = 0; i<newCurve.keys.Length; i++){
			newCurve.MoveKey(i, curve1_ToLerp[i].Lerp(curve2_ToLerp[i], t));
		}
		return newCurve;
	}

	public static Keyframe Lerp (this Keyframe key1, Keyframe key2, float t){
		return new Keyframe(Mathf.Lerp(key1.time,key2.time,t),
			Mathf.Lerp(key1.value,key2.value,t),
			Mathf.Lerp(key1.inTangent,key2.inTangent,t),
			Mathf.Lerp(key1.outTangent,key2.outTangent,t));
	}
}

I also made AnimationClip.CopyTo and AnimationClip.Lerp but they’re probably even less useful!

public static AnimationClip Lerp (this AnimationClip clip1, AnimationClip clip2, float t, GameObject gO){
	#if UNITY_EDITOR
	AnimationClip clip = new AnimationClip();
	clip.legacy = true;
	
	UnityEditor.EditorCurveBinding[] bindings1 = UnityEditor.AnimationUtility.GetCurveBindings(clip1);
	for(int i = 0; i<bindings1.Length; i++){
		AnimationCurve curve = UnityEditor.AnimationUtility.GetEditorCurve(clip1, bindings1[i]).Lerp(UnityEditor.AnimationUtility.GetEditorCurve(clip2, bindings1[i]), t);
		clip.SetCurve("", bindings1[i].type, bindings1[i].propertyName, curve);
	}
	return clip;
	#else
	Debug.LogError("Cannot use AnimationClip outside of Editor");
	return null;
	#endif
}

public static void CopyTo (this AnimationClip from_, AnimationClip to_, GameObject gO){
	#if UNITY_EDITOR
	UnityEditor.EditorCurveBinding[] bindings = UnityEditor.AnimationUtility.GetCurveBindings(from_);
	for(int i = 0; i<bindings.Length; i++){
		AnimationCurve curve = UnityEditor.AnimationUtility.GetEditorCurve(from_, bindings[i]);
		to_.SetCurve("", bindings[i].type, bindings[i].propertyName, curve);
	}
	#else
	Debug.LogError("Cannot use AnimationClip outside of Editor");
	return null;
	#endif
}

Sorry, the comment form is closed at this time.