using Codice.Client.BaseCommands.BranchExplorer; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using XericLibrary.Runtime.Type; namespace XericLibrary.Runtime.MacroLibrary { /// /// 平滑宏 /// public static partial class MacroSmooth { #region y=f(x) public static double Linear(double k) { return k; } public static double QuadraticIn(double k) { return k * k; } public static double QuadraticOut(double k) { return k * (2 - k); } public static double QuadraticInOut(double k) { if((k *= 2) < 1) { return 0.5 * k * k; } return -0.5 * (--k * (k - 2) - 1); } public static double CubicIn(double k) { return k * k * k; } public static double CubicOut(double k) { return --k * k * k + 1; } public static double CubicInOut(double k) { if((k *= 2) < 1) { return 0.5 * k * k * k; } return 0.5 * ((k -= 2) * k * k + 2); } public static double QuarticIn(double k) { return k * k * k * k; } public static double QuarticOut(double k) { return 1 - --k * k * k * k; } public static double QuarticInOut(double k) { if((k *= 2) < 1) { return 0.5 * k * k * k * k; } return -0.5 * ((k -= 2) * k * k * k - 2); } public static double QuinticIn(double k) { return k * k * k * k * k; } public static double QuinticOut(double k) { return --k * k * k * k * k + 1; } public static double QuinticInOut(double k) { if((k *= 2) < 1) { return 0.5 * k * k * k * k * k; } return 0.5 * ((k -= 2) * k * k * k * k + 2); } public static double SinusoidalIn(double k) { return 1 - Math.Cos((k * Math.PI) / 2); } public static double SinusoidalOut(double k) { return Math.Sin((k * Math.PI) / 2); } public static double SinusoidalInOut(double k) { return 0.5 * (1 - Math.Cos(Math.PI * k)); } public static double ExponentialIn(double k) { return k == 0 ? 0 : Math.Pow(1024, k - 1); } public static double ExponentialOut(double k) { return k == 1 ? 1 : 1 - Math.Pow(2, -10 * k); } public static double ExponentialInOut(double k) { if(k == 0) { return 0; } if(k == 1) { return 1; } if((k *= 2) < 1) { return 0.5 * Math.Pow(1024, k - 1); } return 0.5 * (-Math.Pow(2, -10 * (k - 1)) + 2); } public static double CircularIn(double k) { return 1 - Math.Sqrt(1 - k * k); } public static double CircularOut(double k) { return Math.Sqrt(1 - --k * k); } public static double CircularInOut(double k) { if((k *= 2) < 1) { return -0.5 * (Math.Sqrt(1 - k * k) - 1); } return 0.5 * (Math.Sqrt(1 - (k -= 2) * k) + 1); } public static double ElasticIn(double k) { double s; var a = 0.1; var p = 0.4; if(k == 0) { return 0; } if(k == 1) { return 1; } if(a != 0 || a < 1) { a = 1; s = p / 4; } else { s = (p * Math.Asin(1 / a)) / (2 * Math.PI); } return -( a * Math.Pow(2, 10 * (k -= 1)) * Math.Sin(((k - s) * (2 * Math.PI)) / p) ); } public static double ElasticOut(double k) { double s; var a = 0.1; var p = 0.4; if(k == 0) { return 0; } if(k == 1) { return 1; } if(a != 0 || a < 1) { a = 1; s = p / 4; } else { s = (p * Math.Asin(1 / a)) / (2 * Math.PI); } return ( a * Math.Pow(2, -10 * k) * Math.Sin(((k - s) * (2 * Math.PI)) / p) + 1 ); } public static double ElasticInOut(double k) { double s; var a = 0.1; var p = 0.4; if(k == 0) { return 0; } if(k == 1) { return 1; } if(a != 0 || a < 1) { a = 1; s = p / 4; } else { s = (p * Math.Asin(1 / a)) / (2 * Math.PI); } if((k *= 2) < 1) { return ( -0.5 * (a * Math.Pow(2, 10 * (k -= 1)) * Math.Sin(((k - s) * (2 * Math.PI)) / p)) ); } return ( a * Math.Pow(2, -10 * (k -= 1)) * Math.Sin(((k - s) * (2 * Math.PI)) / p) * 0.5 + 1 ); } // 在某一动画开始沿指示的路径进行动画处理前稍稍收回该动画的移动 public static double BackIn(double k) { var s = 1.70158; return k * k * ((s + 1) * k - s); } public static double BackOut(double k) { var s = 1.70158; return --k * k * ((s + 1) * k + s) + 1; } public static double BackInOut(double k) { var s = 1.70158 * 1.525; if((k *= 2) < 1) { return 0.5 * (k * k * ((s + 1) * k - s)); } return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2); } public static double BounceIn(double k) { return 1 - BounceOut(1 - k); } public static double BounceOut(double k) { if(k < 1 / 2.75) { return 7.5625 * k * k; } else if(k < 2 / 2.75) { return 7.5625 * (k -= 1.5 / 2.75) * k + 0.75; } else if(k < 2.5 / 2.75) { return 7.5625 * (k -= 2.25 / 2.75) * k + 0.9375; } else { return 7.5625 * (k -= 2.625 / 2.75) * k + 0.984375; } } public static double BounceInOut(double k) { if(k < 0.5) { return BounceIn(k * 2) * 0.5; } return BounceIn(k * 2 - 1) * 0.5 + 0.5; } #endregion #region 多函数一站式解决 /// /// 切换缓动模式 /// /// /// /// public static double SwitchEasing(this double k, EasingMode mode) { switch(mode) { default: case EasingMode.Linear: return Linear(k); case EasingMode.QuadraticIn: return QuadraticIn(k); case EasingMode.QuadraticOut: return QuadraticOut(k); case EasingMode.QuadraticInOut: return QuadraticInOut(k); case EasingMode.CubicIn: return CubicIn(k); case EasingMode.CubicOut: return CubicOut(k); case EasingMode.CubicInOut: return CubicInOut(k); case EasingMode.QuarticIn: return QuarticIn(k); case EasingMode.QuarticOut: return QuarticOut(k); case EasingMode.QuarticInOut: return QuarticInOut(k); case EasingMode.QuinticIn: return QuinticIn(k); case EasingMode.QuinticOut: return QuinticOut(k); case EasingMode.QuinticInOut: return QuinticInOut(k); case EasingMode.SinusoidalIn: return SinusoidalIn(k); case EasingMode.SinusoidalOut: return SinusoidalOut(k); case EasingMode.SinusoidalInOut: return SinusoidalInOut(k); case EasingMode.ExponentialIn: return ExponentialIn(k); case EasingMode.ExponentialOut: return ExponentialOut(k); case EasingMode.ExponentialInOut: return ExponentialInOut(k); case EasingMode.CircularIn: return CircularIn(k); case EasingMode.CircularOut: return CircularOut(k); case EasingMode.CircularInOut: return CircularInOut(k); case EasingMode.ElasticIn: return ElasticIn(k); case EasingMode.ElasticOut: return ElasticOut(k); case EasingMode.ElasticInOut: return ElasticInOut(k); case EasingMode.BackIn: return BackIn(k); case EasingMode.BounceIn: return BounceIn(k); case EasingMode.BounceOut: return BounceOut(k); case EasingMode.BounceInOut: return BounceInOut(k); } } #endregion } }