本文主要是介绍Unity3D 31种缓动曲线,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
因为太多了,而且在我项目里是策略实现,这三十多种搞完真的吐一口老血
如果有类似需要,建议是用文本替换去直接用现成的,(又吐一口老血)
《曲线演示与函数》
曲线的接口
public interface ICurve{// 0 <= x <= 1public float Evaluate(float x);}
获取曲线的方式
public class BuiltInCurve{public static ICurve GetCurve(EaseCurve curveType){switch (curveType){case EaseCurve.Linear:return LinearCurve.Instance;case EaseCurve.InSine:return InSineCurve.Instance;case EaseCurve.OutSine:return OutSineCurve.Instance;case EaseCurve.InOutSine:return InOutSineCurve.Instance;case EaseCurve.InQuad:return InQuadCurve.Instance;case EaseCurve.OutQuad:return OutQuadCurve.Instance;case EaseCurve.InOutQuad:return InOutQuadCurve.Instance;case EaseCurve.InCubic:return InCubicCurve.Instance;case EaseCurve.OutCubic:return OutCubicCurve.Instance;case EaseCurve.InOutCubic:return InOutCubicCurve.Instance;case EaseCurve.InQuart:return InQuartCurve.Instance;case EaseCurve.OutQuart:return OutQuartCurve.Instance;case EaseCurve.InOutQuart:return InOutQuartCurve.Instance;case EaseCurve.InQuint:return InQuintCurve.Instance;case EaseCurve.OutQuint:return OutQuintCurve.Instance;case EaseCurve.InOutQuint:return InOutQuintCurve.Instance;case EaseCurve.InExpo:return InExpoCurve.Instance;case EaseCurve.OutExpo:return OutExpoCurve.Instance;case EaseCurve.InOutExpo:return InOutExpoCurve.Instance;case EaseCurve.InCirc:return InCircCurve.Instance;case EaseCurve.OutCirc:return OutCircCurve.Instance;case EaseCurve.InOutCirc:return InOutCircCurve.Instance;case EaseCurve.InBack:return InBackCurve.Instance;case EaseCurve.OutBack:return OutBackCurve.Instance;case EaseCurve.InOutBack:return InOutBackCurve.Instance;case EaseCurve.InBounce:return InBounceCurve.Instance;case EaseCurve.OutBounce:return OutBounceCurve.Instance;case EaseCurve.InOutBounce:return InOutBounceCurve.Instance;case EaseCurve.InElastic:return InElasticCurve.Instance;case EaseCurve.OutElastic:return OutElasticCurve.Instance;case EaseCurve.InOutElastic:return InOutElasticCurve.Instance;default:throw new ArgumentException("Invalid EaseCurve value", "curveType");}}}
曲线的枚举
namespace AirFramework
{public enum EaseCurve{Linear, // 线性缓动,匀速运动InSine, // 正弦加速缓动,开始缓慢,后来加速OutSine, // 正弦减速缓动,开始快速,后来减速InOutSine, // 正弦波缓动,先加速后减速InQuad, // 二次方加速缓动,开始缓慢,后来加速OutQuad, // 二次方减速缓动,开始快速,后来减速InOutQuad, // 二次方波缓动,先加速后减速InCubic, // 三次方加速缓动,开始缓慢,后来加速OutCubic, // 三次方减速缓动,开始快速,后来减速InOutCubic, // 三次方波缓动,先加速后减速InQuart, // 四次方加速缓动,开始缓慢,后来加速OutQuart, // 四次方减速缓动,开始快速,后来减速InOutQuart, // 四次方波缓动,先加速后减速InQuint, // 五次方加速缓动,开始缓慢,后来加速OutQuint, // 五次方减速缓动,开始快速,后来减速InOutQuint, // 五次方波缓动,先加速后减速InExpo, // 指数加速缓动,开始缓慢,后来加速OutExpo, // 指数减速缓动,开始快速,后来减速InOutExpo, // 指数波缓动,先加速后减速InCirc, // 圆形加速缓动,开始缓慢,后来加速OutCirc, // 圆形减速缓动,开始快速,后来减速InOutCirc, // 圆形波缓动,先加速后减速InOutBack, // 先正弦减速后正弦加速缓动,开始快速,后来减速,再加速InBack, // 回退加速(类似弹簧),开始缓慢,后来加速并回退一定距离,然后减速回弹OutBack, // 回退减速(类似弹簧),开始快速,后来减速并回退一定距离,然后加速回InBounce, // 弹跳加速缓动,开始缓慢,后来加速并反弹OutBounce, // 弹跳减速缓动,开始快速,后来减速并反弹InOutBounce, // 弹跳波缓动,先加速后减速并反弹InElastic, // 弹性加速缓动,开始缓慢,后来加速并弹性拉伸OutElastic, // 弹性减速缓动,开始快速,后来减速并弹性拉伸InOutElastic, // 弹性波缓动,先加速后减速并弹性拉伸}}
以下是曲线的实现
using System;namespace AirFramework
{#region Linearpublic class LinearCurve : Singleton<LinearCurve>, ICurve{public float Evaluate(float x){return x;}}#endregion#region Sinepublic class InSineCurve : Singleton<InSineCurve>, ICurve{public float Evaluate(float x){return -(float)Math.Cos((double)(x * 1.5707964f)) + 1f;}}//2public class OutSineCurve : Singleton<OutSineCurve>, ICurve{public float Evaluate(float x){return (float)Math.Sin((double)(x * 1.5707964f));}}//3public class InOutSineCurve : Singleton<InOutSineCurve>, ICurve{public float Evaluate(float x){return -0.5f * ((float)Math.Cos((double)(Math.PI * x)) - 1f);}}#endregion#region Quad//4public class InQuadCurve : Singleton<InQuadCurve>, ICurve{public float Evaluate(float x){return x * x;}}//5public class OutQuadCurve : Singleton<OutQuadCurve>, ICurve{public float Evaluate(float x){return 1f - (1f - x) * (1f - x);}}//6public class InOutQuadCurve : Singleton<InOutQuadCurve>, ICurve{public float Evaluate(float x){return x < 0.5f ? 2f * x * x : 1f - 2f * (1f - x) * (1f - x);}}#endregion#region Cubic//7public class InCubicCurve : Singleton<InCubicCurve>, ICurve{public float Evaluate(float x){return x * x * x;}}//8public class OutCubicCurve : Singleton<OutCubicCurve>, ICurve{public float Evaluate(float x){var t = x - 1f;return t * t * t + 1f;}}//9public class InOutCubicCurve : Singleton<InOutCubicCurve>, ICurve{public float Evaluate(float x){return x < 0.5f ? 4f * x * x * x : 1f + 4f * (x - 1f) * (x - 1f) * (x - 1f);}}#endregion#region Quart//10public class InQuartCurve : Singleton<InQuartCurve>, ICurve{public float Evaluate(float x){return x * x * x * x;}}//11public class OutQuartCurve : Singleton<OutQuartCurve>, ICurve{public float Evaluate(float x){var t = x - 1f;return 1f - t * t * t * t;}}//12public class InOutQuartCurve : Singleton<InOutQuartCurve>, ICurve{public float Evaluate(float x){return x < 0.5f ? 8f * x * x * x * x : 1f - 8f * (x - 1f) * (x - 1f) * (x - 1f) * (x - 1f);}}#endregion#region Quint//13public class InQuintCurve : Singleton<InQuintCurve>, ICurve{public float Evaluate(float x){return x * x * x * x * x;}}//14public class OutQuintCurve : Singleton<OutQuintCurve>, ICurve{public float Evaluate(float x){var t = x - 1f;return t * t * t * t * t + 1f;}}//15public class InOutQuintCurve : Singleton<InOutQuintCurve>, ICurve{public float Evaluate(float x){return x < 0.5f ? 16f * x * x * x * x * x : 1f + 16f * (x - 1f) * (x - 1f) * (x - 1f) * (x - 1f) * (x - 1f);}}#endregion#region Expo//16public class InExpoCurve : Singleton<InExpoCurve>, ICurve{public float Evaluate(float x){return x == 0f ? 0f : (float)Math.Pow(2f, 10f * (x - 1f));}}//17public class OutExpoCurve : Singleton<OutExpoCurve>, ICurve{public float Evaluate(float x){return x == 1f ? 1f : -(float)Math.Pow(2f, -10f * x) + 1f;}}//18public class InOutExpoCurve : Singleton<InOutExpoCurve>, ICurve{public float Evaluate(float x){if (x == 0f) return 0f;if (x == 1f) return 1f;return x < 0.5f ? 0.5f * (float)Math.Pow(2f, 10f * (2f * x - 1f)) : 0.5f * (-(float)Math.Pow(2f, -10f * (2f * x - 1f)) + 2f);}}#endregion#region Circpublic class InCircCurve : Singleton<InCircCurve>, ICurve{public float Evaluate(float x){return 1f - (float)Math.Sqrt(1f - x * x);}}//20public class OutCircCurve : Singleton<OutCircCurve>, ICurve{public float Evaluate(float x){return (float)Math.Sqrt(1f - (x - 1f) * (x - 1f));}}//21public class InOutCircCurve : Singleton<InOutCircCurve>, ICurve{public float Evaluate(float x){return x < 0.5f ? 0.5f * (1f - (float)Math.Sqrt(1f - 4f * x * x)) : 0.5f * ((float)Math.Sqrt(1f - 4f * (x - 1f) * (x - 1f)) + 1f);}}#endregion#region Back//22public class InBackCurve : Singleton<InBackCurve>, ICurve{private const float c1 = 1.70158f;private const float c2 = c1 * 1.525f;public float Evaluate(float x){return c2 * x * x * x - c1 * x * x;}}//23public class OutBackCurve : Singleton<OutBackCurve>, ICurve{private const float c1 = 1.70158f;private const float c2 = c1 * 1.525f;public float Evaluate(float x){return x * x * ((c1 + 1) * x + c1) + 1;}}//27public class InOutBackCurve : Singleton<InOutBackCurve>, ICurve{public float Evaluate(float x){const float c1 = 1.70158f;const float c2 = c1 * 1.525f;return (float)(x < 0.5f ? 2 * x * x * ((c2 + 1) * 2 * x - c2) : (float)(1 + Math.Pow(2 * x - 2, 3) + c2 * Math.Pow(2 * x - 2, 2)) / 2);}}#endregion#region Elastic//24public class InElasticCurve : Singleton<InElasticCurve>, ICurve{public float Evaluate(float x){const float c4 = (2 * (float)Math.PI) / 3;return (float)((x == 0) ? 0 : ((x == 1) ? 1 : -Math.Pow(2, 10 * x - 10) * Math.Sin((x * 10 - 10.75) * c4)));}}//25public class OutElasticCurve : Singleton<OutElasticCurve>, ICurve{public float Evaluate(float x){const float c4 = (2 * (float)Math.PI) / 3;return (float)((x == 0) ? 0 : ((x == 1) ? 1 : Math.Pow(2, -10 * x) * Math.Sin((x * 10 - 0.75) * c4) + 1));}}//26public class InOutElasticCurve : Singleton<InOutElasticCurve>, ICurve{public float Evaluate(float x){const float c5 = (2f * (float)Math.PI) / 4.5f;return (float)((x == 0) ? 0 : ((x == 1) ? 1 : (x < 0.5f ? -(Math.Pow(2, 20 * x - 10) * Math.Sin((20 * x - 11.125f) * c5)) / 2 : (Math.Pow(2, -20 * x + 10) * Math.Sin((20 * x - 11.125f) * c5)) / 2 + 1)));}}#endregion#region Bounce//28public class InBounceCurve : Singleton<InBounceCurve>, ICurve{public float Evaluate(float x){return 1 - OutBounceCurve.Instance.Evaluate(1 - x);}}//29public class OutBounceCurve : Singleton<OutBounceCurve>, ICurve{public float Evaluate(float x){const float n1 = 7.5625f;const float d1 = 2.75f;if (x < 1 / d1){return n1 * x * x;}else if (x < 2 / d1){return n1 * (x -= 1.5f / d1) * x + 0.75f;}else if (x < 2.5 / d1){return n1 * (x -= 2.25f / d1) * x + 0.9375f;}else{return n1 * (x -= 2.625f / d1) * x + 0.984375f;}}}//30public class InOutBounceCurve : Singleton<OutBounceCurve>, ICurve{public float Evaluate(float x){return x < 0.5f? (1 - Evaluate(1 - 2 * x)) / 2: (1 + Evaluate(2 * x - 1)) / 2;}}#endregion
}
这篇关于Unity3D 31种缓动曲线的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!