123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- using System;
- using UnityEngine;
- public static class SRMath
- {
- public static float Ease(float from, float to, float t, SRMath.EaseType type)
- {
- switch (type)
- {
- case SRMath.EaseType.Linear:
- return SRMath.TweenFunctions.Linear(t, from, to, 1f);
- case SRMath.EaseType.QuadEaseOut:
- return SRMath.TweenFunctions.QuadEaseOut(t, from, to, 1f);
- case SRMath.EaseType.QuadEaseIn:
- return SRMath.TweenFunctions.QuadEaseIn(t, from, to, 1f);
- case SRMath.EaseType.QuadEaseInOut:
- return SRMath.TweenFunctions.QuadEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.QuadEaseOutIn:
- return SRMath.TweenFunctions.QuadEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.ExpoEaseOut:
- return SRMath.TweenFunctions.ExpoEaseOut(t, from, to, 1f);
- case SRMath.EaseType.ExpoEaseIn:
- return SRMath.TweenFunctions.ExpoEaseIn(t, from, to, 1f);
- case SRMath.EaseType.ExpoEaseInOut:
- return SRMath.TweenFunctions.ExpoEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.ExpoEaseOutIn:
- return SRMath.TweenFunctions.ExpoEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.CubicEaseOut:
- return SRMath.TweenFunctions.CubicEaseOut(t, from, to, 1f);
- case SRMath.EaseType.CubicEaseIn:
- return SRMath.TweenFunctions.CubicEaseIn(t, from, to, 1f);
- case SRMath.EaseType.CubicEaseInOut:
- return SRMath.TweenFunctions.CubicEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.CubicEaseOutIn:
- return SRMath.TweenFunctions.CubicEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.QuartEaseOut:
- return SRMath.TweenFunctions.QuartEaseOut(t, from, to, 1f);
- case SRMath.EaseType.QuartEaseIn:
- return SRMath.TweenFunctions.QuartEaseIn(t, from, to, 1f);
- case SRMath.EaseType.QuartEaseInOut:
- return SRMath.TweenFunctions.QuartEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.QuartEaseOutIn:
- return SRMath.TweenFunctions.QuartEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.QuintEaseOut:
- return SRMath.TweenFunctions.QuintEaseOut(t, from, to, 1f);
- case SRMath.EaseType.QuintEaseIn:
- return SRMath.TweenFunctions.QuintEaseIn(t, from, to, 1f);
- case SRMath.EaseType.QuintEaseInOut:
- return SRMath.TweenFunctions.QuintEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.QuintEaseOutIn:
- return SRMath.TweenFunctions.QuintEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.CircEaseOut:
- return SRMath.TweenFunctions.CircEaseOut(t, from, to, 1f);
- case SRMath.EaseType.CircEaseIn:
- return SRMath.TweenFunctions.CircEaseIn(t, from, to, 1f);
- case SRMath.EaseType.CircEaseInOut:
- return SRMath.TweenFunctions.CircEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.CircEaseOutIn:
- return SRMath.TweenFunctions.CircEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.SineEaseOut:
- return SRMath.TweenFunctions.SineEaseOut(t, from, to, 1f);
- case SRMath.EaseType.SineEaseIn:
- return SRMath.TweenFunctions.SineEaseIn(t, from, to, 1f);
- case SRMath.EaseType.SineEaseInOut:
- return SRMath.TweenFunctions.SineEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.SineEaseOutIn:
- return SRMath.TweenFunctions.SineEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.ElasticEaseOut:
- return SRMath.TweenFunctions.ElasticEaseOut(t, from, to, 1f);
- case SRMath.EaseType.ElasticEaseIn:
- return SRMath.TweenFunctions.ElasticEaseIn(t, from, to, 1f);
- case SRMath.EaseType.ElasticEaseInOut:
- return SRMath.TweenFunctions.ElasticEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.ElasticEaseOutIn:
- return SRMath.TweenFunctions.ElasticEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.BounceEaseOut:
- return SRMath.TweenFunctions.BounceEaseOut(t, from, to, 1f);
- case SRMath.EaseType.BounceEaseIn:
- return SRMath.TweenFunctions.BounceEaseIn(t, from, to, 1f);
- case SRMath.EaseType.BounceEaseInOut:
- return SRMath.TweenFunctions.BounceEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.BounceEaseOutIn:
- return SRMath.TweenFunctions.BounceEaseOutIn(t, from, to, 1f);
- case SRMath.EaseType.BackEaseOut:
- return SRMath.TweenFunctions.BackEaseOut(t, from, to, 1f);
- case SRMath.EaseType.BackEaseIn:
- return SRMath.TweenFunctions.BackEaseIn(t, from, to, 1f);
- case SRMath.EaseType.BackEaseInOut:
- return SRMath.TweenFunctions.BackEaseInOut(t, from, to, 1f);
- case SRMath.EaseType.BackEaseOutIn:
- return SRMath.TweenFunctions.BackEaseOutIn(t, from, to, 1f);
- default:
- throw new ArgumentOutOfRangeException("type");
- }
- }
- public static float SpringLerp(float strength, float deltaTime)
- {
- int num = Mathf.RoundToInt(deltaTime * 1000f);
- float t = 0.001f * strength;
- float num2 = 0f;
- float b = 1f;
- for (int i = 0; i < num; i++)
- {
- num2 = Mathf.Lerp(num2, b, t);
- }
- return num2;
- }
- public static float SpringLerp(float from, float to, float strength, float deltaTime)
- {
- return Mathf.Lerp(from, to, SRMath.SpringLerp(strength, deltaTime));
- }
- public static Vector3 SpringLerp(Vector3 from, Vector3 to, float strength, float deltaTime)
- {
- return Vector3.Lerp(from, to, SRMath.SpringLerp(strength, deltaTime));
- }
- public static Quaternion SpringLerp(Quaternion from, Quaternion to, float strength, float deltaTime)
- {
- return Quaternion.Slerp(from, to, SRMath.SpringLerp(strength, deltaTime));
- }
- public static float SmoothClamp(float value, float min, float max, float scrollMax, SRMath.EaseType easeType = SRMath.EaseType.ExpoEaseOut)
- {
- if (value < min)
- {
- return value;
- }
- float num = Mathf.Clamp01((value - min) / (scrollMax - min));
- UnityEngine.Debug.Log(num);
- return Mathf.Clamp(min + Mathf.Lerp(value - min, max, SRMath.Ease(0f, 1f, num, easeType)), 0f, max);
- }
- public static float LerpUnclamped(float from, float to, float t)
- {
- return (1f - t) * from + t * to;
- }
- public static Vector3 LerpUnclamped(Vector3 from, Vector3 to, float t)
- {
- return new Vector3(SRMath.LerpUnclamped(from.x, to.x, t), SRMath.LerpUnclamped(from.y, to.y, t), SRMath.LerpUnclamped(from.z, to.z, t));
- }
- public static float FacingNormalized(Vector3 dir1, Vector3 dir2)
- {
- dir1.Normalize();
- dir2.Normalize();
- return Mathf.InverseLerp(-1f, 1f, Vector3.Dot(dir1, dir2));
- }
- public static float WrapAngle(float angle)
- {
- if (angle <= -180f)
- {
- angle += 360f;
- }
- else if (angle > 180f)
- {
- angle -= 360f;
- }
- return angle;
- }
- public static float NearestAngle(float to, float angle1, float angle2)
- {
- if (Mathf.Abs(Mathf.DeltaAngle(to, angle1)) > Mathf.Abs(Mathf.DeltaAngle(to, angle2)))
- {
- return angle2;
- }
- return angle1;
- }
- public static int Wrap(int max, int value)
- {
- if (max < 0)
- {
- throw new ArgumentOutOfRangeException("max", "max must be greater than 0");
- }
- while (value < 0)
- {
- value += max;
- }
- while (value >= max)
- {
- value -= max;
- }
- return value;
- }
- public static float Wrap(float max, float value)
- {
- while (value < 0f)
- {
- value += max;
- }
- while (value >= max)
- {
- value -= max;
- }
- return value;
- }
- public static float Average(float v1, float v2)
- {
- return (v1 + v2) * 0.5f;
- }
- public static float Angle(Vector2 direction)
- {
- float num = Vector3.Angle(Vector3.up, direction);
- if (Vector3.Cross(direction, Vector3.up).z > 0f)
- {
- num *= -1f;
- }
- return num;
- }
- private static class TweenFunctions
- {
- public static float Linear(float t, float b, float c, float d)
- {
- return c * t / d + b;
- }
- public static float ExpoEaseOut(float t, float b, float c, float d)
- {
- return (t != d) ? (c * (-Mathf.Pow(2f, -10f * t / d) + 1f) + b) : (b + c);
- }
- public static float ExpoEaseIn(float t, float b, float c, float d)
- {
- return (t != 0f) ? (c * Mathf.Pow(2f, 10f * (t / d - 1f)) + b) : b;
- }
- public static float ExpoEaseInOut(float t, float b, float c, float d)
- {
- if (t == 0f)
- {
- return b;
- }
- if (t == d)
- {
- return b + c;
- }
- if ((t /= d / 2f) < 1f)
- {
- return c / 2f * Mathf.Pow(2f, 10f * (t - 1f)) + b;
- }
- return c / 2f * (-Mathf.Pow(2f, -10f * (t -= 1f)) + 2f) + b;
- }
- public static float ExpoEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.ExpoEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.ExpoEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float CircEaseOut(float t, float b, float c, float d)
- {
- return c * Mathf.Sqrt(1f - (t = t / d - 1f) * t) + b;
- }
- public static float CircEaseIn(float t, float b, float c, float d)
- {
- return -c * (Mathf.Sqrt(1f - (t /= d) * t) - 1f) + b;
- }
- public static float CircEaseInOut(float t, float b, float c, float d)
- {
- if ((t /= d / 2f) < 1f)
- {
- return -c / 2f * (Mathf.Sqrt(1f - t * t) - 1f) + b;
- }
- return c / 2f * (Mathf.Sqrt(1f - (t -= 2f) * t) + 1f) + b;
- }
- public static float CircEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.CircEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.CircEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float QuadEaseOut(float t, float b, float c, float d)
- {
- return -c * (t /= d) * (t - 2f) + b;
- }
- public static float QuadEaseIn(float t, float b, float c, float d)
- {
- return c * (t /= d) * t + b;
- }
- public static float QuadEaseInOut(float t, float b, float c, float d)
- {
- if ((t /= d / 2f) < 1f)
- {
- return c / 2f * t * t + b;
- }
- return -c / 2f * ((t -= 1f) * (t - 2f) - 1f) + b;
- }
- public static float QuadEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.QuadEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.QuadEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float SineEaseOut(float t, float b, float c, float d)
- {
- return c * Mathf.Sin(t / d * 1.57079637f) + b;
- }
- public static float SineEaseIn(float t, float b, float c, float d)
- {
- return -c * Mathf.Cos(t / d * 1.57079637f) + c + b;
- }
- public static float SineEaseInOut(float t, float b, float c, float d)
- {
- if ((t /= d / 2f) < 1f)
- {
- return c / 2f * Mathf.Sin(3.14159274f * t / 2f) + b;
- }
- return -c / 2f * (Mathf.Cos(3.14159274f * (t -= 1f) / 2f) - 2f) + b;
- }
- public static float SineEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.SineEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.SineEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float CubicEaseOut(float t, float b, float c, float d)
- {
- return c * ((t = t / d - 1f) * t * t + 1f) + b;
- }
- public static float CubicEaseIn(float t, float b, float c, float d)
- {
- return c * (t /= d) * t * t + b;
- }
- public static float CubicEaseInOut(float t, float b, float c, float d)
- {
- if ((t /= d / 2f) < 1f)
- {
- return c / 2f * t * t * t + b;
- }
- return c / 2f * ((t -= 2f) * t * t + 2f) + b;
- }
- public static float CubicEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.CubicEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.CubicEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float QuartEaseOut(float t, float b, float c, float d)
- {
- return -c * ((t = t / d - 1f) * t * t * t - 1f) + b;
- }
- public static float QuartEaseIn(float t, float b, float c, float d)
- {
- return c * (t /= d) * t * t * t + b;
- }
- public static float QuartEaseInOut(float t, float b, float c, float d)
- {
- if ((t /= d / 2f) < 1f)
- {
- return c / 2f * t * t * t * t + b;
- }
- return -c / 2f * ((t -= 2f) * t * t * t - 2f) + b;
- }
- public static float QuartEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.QuartEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.QuartEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float QuintEaseOut(float t, float b, float c, float d)
- {
- return c * ((t = t / d - 1f) * t * t * t * t + 1f) + b;
- }
- public static float QuintEaseIn(float t, float b, float c, float d)
- {
- return c * (t /= d) * t * t * t * t + b;
- }
- public static float QuintEaseInOut(float t, float b, float c, float d)
- {
- if ((t /= d / 2f) < 1f)
- {
- return c / 2f * t * t * t * t * t + b;
- }
- return c / 2f * ((t -= 2f) * t * t * t * t + 2f) + b;
- }
- public static float QuintEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.QuintEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.QuintEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float ElasticEaseOut(float t, float b, float c, float d)
- {
- if ((t /= d) == 1f)
- {
- return b + c;
- }
- float num = d * 0.3f;
- float num2 = num / 4f;
- return c * Mathf.Pow(2f, -10f * t) * Mathf.Sin((t * d - num2) * 6.28318548f / num) + c + b;
- }
- public static float ElasticEaseIn(float t, float b, float c, float d)
- {
- if ((t /= d) == 1f)
- {
- return b + c;
- }
- float num = d * 0.3f;
- float num2 = num / 4f;
- return -(c * Mathf.Pow(2f, 10f * (t -= 1f)) * Mathf.Sin((t * d - num2) * 6.28318548f / num)) + b;
- }
- public static float ElasticEaseInOut(float t, float b, float c, float d)
- {
- if ((t /= d / 2f) == 2f)
- {
- return b + c;
- }
- float num = d * 0.450000018f;
- float num2 = num / 4f;
- if (t < 1f)
- {
- return -0.5f * (c * Mathf.Pow(2f, 10f * (t -= 1f)) * Mathf.Sin((t * d - num2) * 6.28318548f / num)) + b;
- }
- return c * Mathf.Pow(2f, -10f * (t -= 1f)) * Mathf.Sin((t * d - num2) * 6.28318548f / num) * 0.5f + c + b;
- }
- public static float ElasticEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.ElasticEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.ElasticEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float BounceEaseOut(float t, float b, float c, float d)
- {
- if ((t /= d) < 0.363636374f)
- {
- return c * (7.5625f * t * t) + b;
- }
- if ((double)t < 0.72727272727272729)
- {
- return c * (7.5625f * (t -= 0.545454562f) * t + 0.75f) + b;
- }
- if ((double)t < 0.90909090909090906)
- {
- return c * (7.5625f * (t -= 0.8181818f) * t + 0.9375f) + b;
- }
- return c * (7.5625f * (t -= 0.954545438f) * t + 0.984375f) + b;
- }
- public static float BounceEaseIn(float t, float b, float c, float d)
- {
- return c - SRMath.TweenFunctions.BounceEaseOut(d - t, 0f, c, d) + b;
- }
- public static float BounceEaseInOut(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.BounceEaseIn(t * 2f, 0f, c, d) * 0.5f + b;
- }
- return SRMath.TweenFunctions.BounceEaseOut(t * 2f - d, 0f, c, d) * 0.5f + c * 0.5f + b;
- }
- public static float BounceEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.BounceEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.BounceEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- public static float BackEaseOut(float t, float b, float c, float d)
- {
- return c * ((t = t / d - 1f) * t * (2.70158f * t + 1.70158f) + 1f) + b;
- }
- public static float BackEaseIn(float t, float b, float c, float d)
- {
- return c * (t /= d) * t * (2.70158f * t - 1.70158f) + b;
- }
- public static float BackEaseInOut(float t, float b, float c, float d)
- {
- float num = 1.70158f;
- if ((t /= d / 2f) < 1f)
- {
- return c / 2f * (t * t * (((num *= 1.525f) + 1f) * t - num)) + b;
- }
- return c / 2f * ((t -= 2f) * t * (((num *= 1.525f) + 1f) * t + num) + 2f) + b;
- }
- public static float BackEaseOutIn(float t, float b, float c, float d)
- {
- if (t < d / 2f)
- {
- return SRMath.TweenFunctions.BackEaseOut(t * 2f, b, c / 2f, d);
- }
- return SRMath.TweenFunctions.BackEaseIn(t * 2f - d, b + c / 2f, c / 2f, d);
- }
- }
- public enum EaseType
- {
- Linear,
- QuadEaseOut,
- QuadEaseIn,
- QuadEaseInOut,
- QuadEaseOutIn,
- ExpoEaseOut,
- ExpoEaseIn,
- ExpoEaseInOut,
- ExpoEaseOutIn,
- CubicEaseOut,
- CubicEaseIn,
- CubicEaseInOut,
- CubicEaseOutIn,
- QuartEaseOut,
- QuartEaseIn,
- QuartEaseInOut,
- QuartEaseOutIn,
- QuintEaseOut,
- QuintEaseIn,
- QuintEaseInOut,
- QuintEaseOutIn,
- CircEaseOut,
- CircEaseIn,
- CircEaseInOut,
- CircEaseOutIn,
- SineEaseOut,
- SineEaseIn,
- SineEaseInOut,
- SineEaseOutIn,
- ElasticEaseOut,
- ElasticEaseIn,
- ElasticEaseInOut,
- ElasticEaseOutIn,
- BounceEaseOut,
- BounceEaseIn,
- BounceEaseInOut,
- BounceEaseOutIn,
- BackEaseOut,
- BackEaseIn,
- BackEaseInOut,
- BackEaseOutIn
- }
- }
|