//Create default curve for the dynamic bias private AnimationCurve defineCurve() { AnimationCurve temp = new AnimationCurve(); temp.AddKey(-1, -1); temp.AddKey(-0.55f, -0.95f); temp.AddKey(-0.001f, -0.5f); temp.AddKey(0.001f, 0.5f); temp.AddKey(0.55f, 0.95f); temp.AddKey(1, 1); temp.SmoothTangents(2, 1); temp.SmoothTangents(3, -1); return temp; }
public static void SetLinear(ref AnimationCurve curve) { for (int i = 0; i < curve.length; i++) { Keyframe boxed = curve.keys[i]; // getting around the fact that Keyframe is a struct by pre-boxing boxed.tangentMode = GetNewTangentKeyMode(boxed.tangentMode, TangentDirection.Left, TangentMode.Linear); boxed.tangentMode = GetNewTangentKeyMode(boxed.tangentMode, TangentDirection.Right, TangentMode.Linear); curve.MoveKey(i, (Keyframe)boxed); curve.SmoothTangents(i, 0f); } }
public static AnimationCurve CreateCurve( float[] values, float[] times, bool smooth = true) { D.Assert( values != null && times != null, "Param == null" ); D.Assert( values.Length == times.Length, "Size not same" ); if ( values.Length != times.Length ){ D.Log("values.Length: {0} times.Length: {1}", values.Length, times.Length); } AnimationCurve result; Keyframe[] ks = new Keyframe[values.Length]; for( int i = 0; i < values.Length; i ++ ) { ks[ i ] = new Keyframe( times[ i ], values[ i ] ); } result = new AnimationCurve( ks ); if ( smooth){ for( int i = 0; i < result.length; i++ ) { result.SmoothTangents( i, 0 ); } } return result; }
static public int SmoothTangents(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.AnimationCurve self = (UnityEngine.AnimationCurve)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Single a2; checkType(l, 3, out a2); self.SmoothTangents(a1, a2); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
public static AnimationCurve CreateCurve( Keyframe[] keyFrames){ AnimationCurve result; result = new AnimationCurve( keyFrames ); for( int i = 0; i < result.length; i++ ) { result.SmoothTangents( i, 0 ); } return result; }
static public int SmoothTangents(IntPtr l) { try { UnityEngine.AnimationCurve self = (UnityEngine.AnimationCurve)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Single a2; checkType(l, 3, out a2); self.SmoothTangents(a1, a2); return(0); } catch (Exception e) { return(error(l, e)); } }
static int SmoothTangents(IntPtr L) { try { ToLua.CheckArgsCount(L, 3); UnityEngine.AnimationCurve obj = (UnityEngine.AnimationCurve)ToLua.CheckObject(L, 1, typeof(UnityEngine.AnimationCurve)); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); float arg1 = (float)LuaDLL.luaL_checknumber(L, 3); obj.SmoothTangents(arg0, arg1); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
// UnityEditor.CurveUtility.cs (c) Unity Technologies public static void UpdateTangentsFromMode(AnimationCurve curve, int index) { if (index < 0 || index >= curve.length) return; Keyframe key = curve [index]; if (KeyframeUtil.GetKeyTangentMode (key, 0) == TangentMode.Linear && index >= 1) { key.inTangent = CalculateLinearTangent (curve, index, index - 1); curve.MoveKey (index, key); } if (KeyframeUtil.GetKeyTangentMode (key, 1) == TangentMode.Linear && index + 1 < curve.length) { key.outTangent = CalculateLinearTangent (curve, index, index + 1); curve.MoveKey (index, key); } if (KeyframeUtil.GetKeyTangentMode (key, 0) != TangentMode.Smooth && KeyframeUtil.GetKeyTangentMode (key, 1) != TangentMode.Smooth) return; curve.SmoothTangents (index, 0.0f); }
/// <summary> /// Remove a key from an AnimationCurve. /// </summary> /// <param name="curve">The existing AnimationCurve.</param> /// <param name="index">The index of the Key to be removed.</param> public static void RemoveKey(AnimationCurve curve, int index) { curve.RemoveKey(index); // Update left neighbour. if (index > 0) { // Update tangent data based on tangent mode. int tangentMode = curve[index-1].tangentMode; if (IsAuto(tangentMode)) { curve.SmoothTangents(index - 1, 0); } if (IsBroken(tangentMode)) { if (IsRightLinear(tangentMode)) { SetKeyRightLinear(curve, index - 1); } } } // Update right neighbour. if (index < curve.length) { // Update tangent data based on tangent mode. int tangentMode = curve[index].tangentMode; if (IsAuto(tangentMode)) { curve.SmoothTangents(index, 0); } if (IsBroken(tangentMode)) { if (IsLeftLinear(tangentMode)) { SetKeyLeftLinear(curve, index); } } } }
/// <summary> /// Move/Change an existing key in an AnimationCurve. /// Maintains TangentMode and updates neighbours. /// </summary> /// <param name="curve">The existing AnimationCurve.</param> /// <param name="index">The index of the current Keyframe.</param> /// <param name="keyframe">The new Keyframe data.</param> /// <returns>The index of the Keyframe.</returns> public static int MoveKey(AnimationCurve curve, int index, Keyframe keyframe) { // Save the tangent mode. Keyframe old = curve[index]; keyframe.tangentMode = old.tangentMode; int newIndex = curve.MoveKey(index, keyframe); // Respect the tangentMode and update as necessary. if (IsAuto(keyframe.tangentMode)) { curve.SmoothTangents(newIndex, 0); } else if (IsBroken(keyframe.tangentMode)) { if (IsLeftLinear(keyframe.tangentMode)) { SetKeyLeftLinear(curve, newIndex); } if (IsRightLinear(keyframe.tangentMode)) { SetKeyRightLinear(curve, newIndex); } } // update the left neighbour if (newIndex > 0) { // Update tangent data based on tangent mode. int tangentMode = curve[newIndex - 1].tangentMode; if (IsAuto(tangentMode)) { curve.SmoothTangents(newIndex - 1, 0); } if (IsBroken(tangentMode)) { if (IsRightLinear(tangentMode)) { SetKeyRightLinear(curve, newIndex - 1); } } } // update the right neighbour if (newIndex < curve.length - 1) { // Update tangent data based on tangent mode. int tangentMode = curve[newIndex + 1].tangentMode; if (IsAuto(tangentMode)) { curve.SmoothTangents(newIndex + 1, 0); } if (IsBroken(tangentMode)) { if (IsLeftLinear(tangentMode)) { SetKeyLeftLinear(curve, newIndex + 1); } } } return newIndex; }
/// <summary> /// Add a new key to an AnimationCurve. /// Ensures the integrity of other key's tangent modes. /// </summary> /// <param name="curve">The existing AnimationCurve.</param> /// <param name="keyframe">The new keyframe</param> /// <returns>The index of the newly added key.</returns> public static int AddKey(AnimationCurve curve, Keyframe keyframe) { if (curve.length == 0) { return curve.AddKey(keyframe); } else if (curve.length == 1) { // Save the existing keyframe data. (Unity changes the tangent info). Keyframe temp = curve[0]; int newIndex = curve.AddKey(keyframe); if(newIndex == -1) { return 0; } else if(newIndex == 0) { curve.MoveKey(1, temp); } else { curve.MoveKey(0, temp); } return newIndex; } else { Keyframe left = new Keyframe(); Keyframe right = new Keyframe(); for (int i = 0; i < curve.length - 1; i++) { Keyframe l = curve[i]; Keyframe r = curve[i + 1]; if (l.time < keyframe.time && keyframe.time < r.time) { left = l; right = r; } } int index = curve.AddKey(keyframe); // Handle left neighbour. if (index > 0) { // Restore the saved data. curve.MoveKey(index - 1, left); // Update tangent data based on tangent mode. int tangentMode = curve[index - 1].tangentMode; if (IsAuto(tangentMode)) { curve.SmoothTangents(index - 1, 0); } if (IsBroken(tangentMode)) { if (IsRightLinear(tangentMode)) { SetKeyRightLinear(curve, index - 1); } } } // Handle the Right neighbour. if (index < curve.length - 1) { // Restore the saved data. curve.MoveKey(index + 1, right); // Update tangent data based on tangent mode. int tangentMode = curve[index + 1].tangentMode; if (IsAuto(tangentMode)) { curve.SmoothTangents(index + 1, 0); } if (IsBroken(tangentMode)) { if (IsLeftLinear(tangentMode)) { SetKeyLeftLinear(curve, index + 1); } } } return index; } }
public static AnimationCurve GenerateRootTZMotion (AnimationCurve lFtTZCurve, AnimationCurve rFtTZCurve, int lFtIn, int lFtOut, int rFtIn, int rFtOut) { int curveLength = lFtTZCurve.keys.Length; List<Keyframe> footKeys = new List<Keyframe> (); int minIndex = lFtIn; AREA area = AREA.RIGHT_FOOT_LEAVE; if (minIndex > lFtOut) { minIndex = lFtOut; area = AREA.LEFT_FOOT_ENTER; } if (minIndex > rFtIn) { minIndex = rFtIn; area = AREA.LEFT_FOOT_LEAVE; } if (minIndex > rFtOut) { minIndex = rFtOut; area = AREA.RIGHT_FOOT_ENTER; } Debug.Log ("start area: " + area); for (int i=0; i<curveLength; i++) { if (i == lFtIn) { area = AREA.LEFT_FOOT_ENTER; } else if (i == lFtOut) { area = AREA.LEFT_FOOT_LEAVE; } else if (i == rFtIn) { area = AREA.RIGHT_FOOT_ENTER; } else if (i == rFtOut) { area = AREA.RIGHT_FOOT_LEAVE; } Debug.Log ("index: " + i + " current area: " + area); int preIndex; switch (area) { case AREA.LEFT_FOOT_ENTER: preIndex = (i - 1 + curveLength) % curveLength; footKeys.Add (new Keyframe (lFtTZCurve.keys [i].time, lFtTZCurve.keys [preIndex].value - lFtTZCurve.keys [i].value)); break; case AREA.RIGHT_FOOT_ENTER: preIndex = (i - 1 + curveLength) % curveLength; footKeys.Add (new Keyframe (rFtTZCurve.keys [i].time, rFtTZCurve.keys [preIndex].value - rFtTZCurve.keys [i].value)); break; case AREA.LEFT_FOOT_LEAVE: preIndex = (lFtOut - 1 + curveLength) % curveLength; footKeys.Add (new Keyframe (lFtTZCurve.keys [i].time, lFtTZCurve.keys [preIndex].value - lFtTZCurve.keys [lFtOut].value)); break; case AREA.RIGHT_FOOT_LEAVE: preIndex = (rFtOut - 1 + curveLength) % curveLength; footKeys.Add (new Keyframe (rFtTZCurve.keys [i].time, rFtTZCurve.keys [preIndex].value - rFtTZCurve.keys [rFtOut].value)); break; } } AnimationCurve rootCurve = new AnimationCurve (); float value = 0; for (int i=0; i<curveLength; i++) { value += Mathf.Abs (footKeys [i].value); rootCurve.AddKey (new Keyframe (footKeys [i].time, value)); rootCurve.SmoothTangents (i, 0); } return rootCurve; }
private static void UpdateTangentsFromMode(AnimationCurve curve, int index) { if (index < 0 || index >= curve.length) { return; } Keyframe key = curve[index]; if (CurveUtility.GetKeyTangentMode(key, 0) == TangentMode.Linear && index >= 1) { key.inTangent = CurveUtility.CalculateLinearTangent(curve, index, index - 1); curve.MoveKey(index, key); } if (CurveUtility.GetKeyTangentMode(key, 1) == TangentMode.Linear && index + 1 < curve.length) { key.outTangent = CurveUtility.CalculateLinearTangent(curve, index, index + 1); curve.MoveKey(index, key); } if (CurveUtility.GetKeyTangentMode(key, 0) == TangentMode.Smooth || CurveUtility.GetKeyTangentMode(key, 1) == TangentMode.Smooth) { curve.SmoothTangents(index, 0f); } }
private static void UpdateTangentsFromMode(AnimationCurve curve, int index) { if ((index >= 0) && (index < curve.length)) { Keyframe key = curve[index]; if ((GetKeyTangentMode(key, 0) == TangentMode.Linear) && (index >= 1)) { key.inTangent = CalculateLinearTangent(curve, index, index - 1); curve.MoveKey(index, key); } if ((GetKeyTangentMode(key, 1) == TangentMode.Linear) && ((index + 1) < curve.length)) { key.outTangent = CalculateLinearTangent(curve, index, index + 1); curve.MoveKey(index, key); } if ((GetKeyTangentMode(key, 0) == TangentMode.Smooth) || (GetKeyTangentMode(key, 1) == TangentMode.Smooth)) { curve.SmoothTangents(index, 0f); } } }
private static void Internal_UpdateTangents(AnimationCurve curve, int index) { if ((index >= 0) && (index < curve.length)) { Keyframe key = curve[index]; if ((GetKeyLeftTangentMode(key) == TangentMode.Linear) && (index >= 1)) { key.inTangent = Internal_CalculateLinearTangent(curve, index, index - 1); curve.MoveKey(index, key); } if ((GetKeyRightTangentMode(key) == TangentMode.Linear) && ((index + 1) < curve.length)) { key.outTangent = Internal_CalculateLinearTangent(curve, index, index + 1); curve.MoveKey(index, key); } if ((GetKeyLeftTangentMode(key) == TangentMode.ClampedAuto) || (GetKeyRightTangentMode(key) == TangentMode.ClampedAuto)) { Internal_CalculateAutoTangent(curve, index); } if ((GetKeyLeftTangentMode(key) == TangentMode.Auto) || (GetKeyRightTangentMode(key) == TangentMode.Auto)) { curve.SmoothTangents(index, 0f); } if (((GetKeyLeftTangentMode(key) == TangentMode.Free) && (GetKeyRightTangentMode(key) == TangentMode.Free)) && !GetKeyBroken(key)) { key.outTangent = key.inTangent; curve.MoveKey(index, key); } if (GetKeyLeftTangentMode(key) == TangentMode.Constant) { key.inTangent = float.PositiveInfinity; curve.MoveKey(index, key); } if (GetKeyRightTangentMode(key) == TangentMode.Constant) { key.outTangent = float.PositiveInfinity; curve.MoveKey(index, key); } } }
AnimationCurve ScaleThenOffset(AnimationCurve curve, float length, float scale, float offset) { List<Keyframe> frames = new List<Keyframe>(); foreach (Keyframe key in curve.keys) { Keyframe newKey = key; newKey.time = key.time * scale + offset; newKey.inTangent = key.inTangent / scale; newKey.outTangent = key.outTangent / scale; frames.Add(newKey); } if (frames.Count != 0 && frames[0].time != 0) { Keyframe f = frames[0]; f.outTangent = 0; f.time = 0; frames.Insert(0, f); } if (frames.Count != 0 && frames[frames.Count - 1].time != length) { Keyframe f = frames[frames.Count - 1]; f.time = length; frames.Add(f); } if (frames.Count < 2) { Debug.LogError("keycount <2"); } AnimationCurve result = new AnimationCurve(frames.ToArray()); result.SmoothTangents(0, 0); result.SmoothTangents(result.keys.Length - 1, 0); return result; }
void UpdateTangentsFromMode(AnimationCurve curve, int index) { if (index < 0 || index >= curve.length) return; Keyframe key = curve[index]; // Adjust linear tangent if (GetKeyTangentMode(key, 0) == TangentMode.Linear && index >= 1) { key.inTangent = CalculateLinearTangent(curve[index], curve[index-1]); } if (GetKeyTangentMode(key, 1) == TangentMode.Linear && index+1 < curve.length) { key.outTangent = CalculateLinearTangent(curve[index], curve[index+1]); } float smoothWeight = 0.0F; // if (index > 0 && index < curve.length - 1) // { // smoothWeight = Mathf.InverseLerp(curve[index - 1].time, curve[index + 1].time, curve[index].time) * 2 - 1; // } if (GetKeyTangentMode(key, 0) == TangentMode.Smooth) { curve.SmoothTangents(index, smoothWeight); key.inTangent = curve[index].inTangent; } if (GetKeyTangentMode(key, 1) == TangentMode.Smooth) { curve.SmoothTangents(index, smoothWeight); key.outTangent = curve[index].outTangent; } curve.MoveKey(index, key); }
static AnimationCurve GenerateCurve(EasingFunction easingFunction, int resolution) { var curve = new AnimationCurve(); for (var i = 0; i < resolution; ++i) { var time = i / (resolution - 1f); var value = (float)easingFunction(time, 0.0, 1.0, 1.0); var key = new Keyframe(time, value); curve.AddKey(key); } for (var i = 0; i < resolution; ++i) { curve.SmoothTangents(i, 0f); } return curve; }