/// <summary> /// Given a list of points convert to animation curve using supplied conversionProperties. /// </summary> /// <param name="animCurve">AnimationCurve that contains converted results.</param> /// <param name="conversionProperties">Struct that defines various curve conversion properties.</param> /// <param name="inPts">List of 2D Points on curve.</param> /// <param name="debug">When true will log various debug statements.</param> public static void ConvertToAnimCurve(AnimationCurve animCurve, ConversionProperties conversionProperties, List <Vector2> inPts, bool debug = false) { Keyframe keyFrame; Keyframe[] keyFrames = new Keyframe[inPts.Count + 1]; float[] keyAngles = new float[inPts.Count + 1]; for (int i = 0; i < inPts.Count; i++) { keyFrame = new Keyframe(); if (i > 0 && i < inPts.Count - 1) { // keyAngles[ i ] = Mathf.Atan2(inPts[ i ].y - inPts[ i-1 ].y, inPts[ i ].x - inPts[ i-1 ].x) * 180f / Mathf.PI; Vector2 left = new Vector2(inPts[i].y - inPts[i - 1].y, inPts[i].x - inPts[i - 1].x); //.normalized; Vector2 right = new Vector2(inPts[i + 1].y - inPts[i].y, inPts[i + 1].x - inPts[i - 1].x); //.normalized; keyAngles[i] = Vector2.Angle(left, right); if (keyAngles[i] > conversionProperties.m_SmoothTangentMaxAngle) { keyFrame.inTangent = Mathf.Atan2(inPts[i].y - inPts[i - 1].y, inPts[i].x - inPts[i - 1].x); keyFrame.outTangent = Mathf.Atan2(inPts[i + 1].y - inPts[i].y, inPts[i + 1].x - inPts[i].x); } } keyFrame.value = inPts[i].y; keyFrame.time = inPts[i].x; keyFrames[i] = keyFrame; } // Last Frame keyFrame = new Keyframe(); keyFrame.value = inPts[inPts.Count - 1].y; keyFrame.time = inPts[inPts.Count - 1].x; keyFrames[inPts.Count] = keyFrame; animCurve.keys = keyFrames; for (int i = 0; i < keyFrames.Length; i++) { RuntimeAnimationUtility.TangentMode tangent = (keyAngles[i] > conversionProperties.m_SmoothTangentMaxAngle) ? RuntimeAnimationUtility.TangentMode.Linear : conversionProperties.m_TangentMode; RuntimeAnimationUtility.SetKeyBroken(animCurve, i, (keyAngles[i] > conversionProperties.m_SmoothTangentMaxAngle)); RuntimeAnimationUtility.SetKeyLeftTangentMode(animCurve, i, tangent); RuntimeAnimationUtility.SetKeyRightTangentMode(animCurve, i, tangent); } if (debug) { AnimationCurveToString(animCurve, keyAngles); } }
public static void ConvertToAnimCurve(AnimationCurve animCurve, ConversionProperties conversionProperties, CubicBezier[] curves, bool debug = false) { Keyframe keyFrame; Keyframe[] keyFrames = new Keyframe[curves.Length + 1]; float curveLength = 1f / curves.Length; float curveTime = 0; for (int i = 0; i < curves.Length; i++) { keyFrame = new Keyframe(); keyFrame.value = curves[i].p0.y; keyFrame.time = curveTime; curveTime += curveLength; keyFrames[i] = keyFrame; } keyFrame = new Keyframe(); keyFrame.value = curves[curves.Length - 1].p3.y; keyFrame.time = curveTime; curveTime += curveLength; keyFrames[curves.Length] = keyFrame; animCurve.keys = keyFrames; for (int i = 0; i < keyFrames.Length; i++) { RuntimeAnimationUtility.SetKeyLeftTangentMode(animCurve, i, conversionProperties.m_TangentMode); RuntimeAnimationUtility.SetKeyRightTangentMode(animCurve, i, conversionProperties.m_TangentMode); } if (debug) { AnimationCurveToString(animCurve); } }