Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static class Easer
- {
- private static float cos(float x) => Mathf.Cos(x) * Mathf.Rad2Deg;
- private static float sin(float x) => Mathf.Sin(x) * Mathf.Rad2Deg;
- private static float pow(float x, float y) => Mathf.Pow(x, y);
- private static float sqrt(float x) => Mathf.Sqrt(x);
- private const float PI = Mathf.PI;
- public static float GetValue(EasingType type, float x)
- {
- switch (type)
- {
- case EasingType.linear:
- {
- return x;
- }
- case EasingType.easeInSine:
- {
- return 1 - cos((x * PI) / 2);
- }
- case EasingType.easeOutSine:
- {
- return sin((x * PI) / 2);
- }
- case EasingType.easeInOutSine:
- {
- return -(cos(PI * x) - 1) / 2;
- }
- case EasingType.easeInQuad:
- {
- return x * x;
- }
- case EasingType.easeOutQuad:
- {
- return 1 - (1 - x) * (1 - x);
- }
- case EasingType.easeInOutQuad:
- {
- return x < 0.5 ? 2 * x * x : 1 - pow(-2 * x + 2, 2) / 2;
- }
- case EasingType.easeInCubic:
- {
- return x * x * x;
- }
- case EasingType.easeOutCubic:
- {
- return 1 - pow(1 - x, 3);
- }
- case EasingType.easeInOutCubic:
- {
- return x < 0.5 ? 4 * x * x * x : 1 - pow(-2 * x + 2, 3) / 2;
- }
- case EasingType.easeInQuart:
- {
- return x * x * x * x;
- }
- case EasingType.easeOutQuart:
- {
- return 1 - pow(1 - x, 4);
- }
- case EasingType.easeInOutQuart:
- {
- return x < 0.5 ? 8 * x * x * x * x : 1 - pow(-2 * x + 2, 4) / 2;
- }
- case EasingType.easeInQuint:
- {
- return x * x * x * x * x;
- }
- case EasingType.easeOutQuint:
- {
- return 1 - pow(1 - x, 5);
- }
- case EasingType.easeInOutQuint:
- {
- return x < 0.5 ? 16 * x * x * x * x * x : 1 - pow(-2 * x + 2, 5) / 2;
- }
- case EasingType.easeInExpo:
- {
- return x == 0 ? 0 : pow(2, 10 * x - 10);
- }
- case EasingType.easeOutExpo:
- {
- return x == 1 ? 1 : 1 - pow(2, -10 * x);
- }
- case EasingType.easeInOutExpo:
- {
- return x == 0
- ? 0
- : x == 1
- ? 1
- : x < 0.5 ? pow(2, 20 * x - 10) / 2
- : (2 - pow(2, -20 * x + 10)) / 2;
- }
- case EasingType.easeInCirc:
- {
- return 1 - sqrt(1 - pow(x, 2));
- }
- case EasingType.easeOutCirc:
- {
- return sqrt(1 - pow(x - 1, 2));
- }
- case EasingType.easeInOutCirc:
- {
- return x < 0.5
- ? (1 - sqrt(1 - pow(2 * x, 2))) / 2
- : (sqrt(1 - pow(-2 * x + 2, 2)) + 1) / 2;
- }
- case EasingType.easeInBack:
- {
- const float c1 = 1.70158f;
- const float c3 = c1 + 1;
- return c3 * x * x * x - c1 * x * x;
- }
- case EasingType.easeOutBack:
- {
- const float c1 = 1.70158f;
- const float c3 = c1 + 1;
- return 1 + c3 * pow(x - 1, 3) + c1 * pow(x - 1, 2);
- }
- case EasingType.easeInOutBack:
- {
- const float c1 = 1.70158f;
- const float c2 = c1 * 1.525f;
- return x < 0.5
- ? (pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2
- : (pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
- }
- case EasingType.easeInElastic:
- {
- const float c4 = (2 * PI) / 3;
- return x == 0
- ? 0
- : x == 1
- ? 1
- : -pow(2, 10 * x - 10) * sin((x * 10 - 10.75f) * c4);
- }
- case EasingType.easeOutElastic:
- {
- const float c4 = (2 * PI) / 3;
- return x == 0
- ? 0
- : x == 1
- ? 1
- : pow(2, -10 * x) * sin((x * 10 - 0.75f) * c4) + 1;
- }
- case EasingType.easeInOutElastic:
- {
- const float c5 = (2 * PI) / 4.5f;
- return x == 0
- ? 0
- : x == 1
- ? 1
- : x < 0.5
- ? -(pow(2, 20 * x - 10) * sin((20 * x - 11.125f) * c5)) / 2
- : (pow(2, -20 * x + 10) * sin((20 * x - 11.125f) * c5)) / 2 + 1;
- }
- case EasingType.easeInBounce:
- {
- return 1 - GetValue(EasingType.easeOutBounce, (1 - x));
- }
- case EasingType.easeOutBounce:
- {
- 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;
- }
- }
- case EasingType.easeInOutBounce:
- {
- return x < 0.5
- ? (1 - GetValue(EasingType.easeOutBounce, 1 - 2 * x)) / 2
- : (1 + GetValue(EasingType.easeOutBounce, 2 * x - 1)) / 2;
- }
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment