Guest User

Untitled

a guest
Jul 30th, 2022
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.83 KB | None | 0 0
  1. private static class Easer
  2. {
  3.     private static float cos(float x) => Mathf.Cos(x) * Mathf.Rad2Deg;
  4.     private static float sin(float x) => Mathf.Sin(x) * Mathf.Rad2Deg;
  5.     private static float pow(float x, float y) => Mathf.Pow(x, y);
  6.     private static float sqrt(float x) => Mathf.Sqrt(x);
  7.     private const float PI = Mathf.PI;
  8.  
  9.     public static float GetValue(EasingType type, float x)
  10.     {
  11.         switch (type)
  12.         {
  13.             case EasingType.linear:
  14.                 {
  15.                     return x;
  16.                 }
  17.             case EasingType.easeInSine:
  18.                 {
  19.                     return 1 - cos((x * PI) / 2);
  20.                 }
  21.             case EasingType.easeOutSine:
  22.                 {
  23.                     return sin((x * PI) / 2);
  24.                 }
  25.             case EasingType.easeInOutSine:
  26.                 {
  27.                     return -(cos(PI * x) - 1) / 2;
  28.                 }
  29.             case EasingType.easeInQuad:
  30.                 {
  31.                     return x * x;
  32.                 }
  33.             case EasingType.easeOutQuad:
  34.                 {
  35.                     return 1 - (1 - x) * (1 - x);
  36.                 }
  37.             case EasingType.easeInOutQuad:
  38.                 {
  39.                     return x < 0.5 ? 2 * x * x : 1 - pow(-2 * x + 2, 2) / 2;
  40.                 }
  41.             case EasingType.easeInCubic:
  42.                 {
  43.                     return x * x * x;
  44.                 }
  45.             case EasingType.easeOutCubic:
  46.                 {
  47.                     return 1 - pow(1 - x, 3);
  48.                 }
  49.             case EasingType.easeInOutCubic:
  50.                 {
  51.                     return x < 0.5 ? 4 * x * x * x : 1 - pow(-2 * x + 2, 3) / 2;
  52.                 }
  53.             case EasingType.easeInQuart:
  54.                 {
  55.                     return x * x * x * x;
  56.                 }
  57.             case EasingType.easeOutQuart:
  58.                 {
  59.                     return 1 - pow(1 - x, 4);
  60.                 }
  61.             case EasingType.easeInOutQuart:
  62.                 {
  63.                     return x < 0.5 ? 8 * x * x * x * x : 1 - pow(-2 * x + 2, 4) / 2;
  64.                 }
  65.             case EasingType.easeInQuint:
  66.                 {
  67.                     return x * x * x * x * x;
  68.                 }
  69.             case EasingType.easeOutQuint:
  70.                 {
  71.                     return 1 - pow(1 - x, 5);
  72.                 }
  73.             case EasingType.easeInOutQuint:
  74.                 {
  75.                     return x < 0.5 ? 16 * x * x * x * x * x : 1 - pow(-2 * x + 2, 5) / 2;
  76.                 }
  77.             case EasingType.easeInExpo:
  78.                 {
  79.                     return x == 0 ? 0 : pow(2, 10 * x - 10);
  80.                 }
  81.             case EasingType.easeOutExpo:
  82.                 {
  83.                     return x == 1 ? 1 : 1 - pow(2, -10 * x);
  84.                 }
  85.             case EasingType.easeInOutExpo:
  86.                 {
  87.                     return x == 0
  88.                            ? 0
  89.                            : x == 1
  90.                            ? 1
  91.                            : x < 0.5 ? pow(2, 20 * x - 10) / 2
  92.                            : (2 - pow(2, -20 * x + 10)) / 2;
  93.                 }
  94.             case EasingType.easeInCirc:
  95.                 {
  96.                     return 1 - sqrt(1 - pow(x, 2));
  97.                 }
  98.             case EasingType.easeOutCirc:
  99.                 {
  100.                     return sqrt(1 - pow(x - 1, 2));
  101.                 }
  102.             case EasingType.easeInOutCirc:
  103.                 {
  104.                     return x < 0.5
  105.                            ? (1 - sqrt(1 - pow(2 * x, 2))) / 2
  106.                            : (sqrt(1 - pow(-2 * x + 2, 2)) + 1) / 2;
  107.                 }
  108.             case EasingType.easeInBack:
  109.                 {
  110.                     const float c1 = 1.70158f;
  111.                     const float c3 = c1 + 1;
  112.  
  113.                     return c3 * x * x * x - c1 * x * x;
  114.                 }
  115.             case EasingType.easeOutBack:
  116.                 {
  117.                     const float c1 = 1.70158f;
  118.                     const float c3 = c1 + 1;
  119.  
  120.                     return 1 + c3 * pow(x - 1, 3) + c1 * pow(x - 1, 2);
  121.                 }
  122.             case EasingType.easeInOutBack:
  123.                 {
  124.                     const float c1 = 1.70158f;
  125.                     const float c2 = c1 * 1.525f;
  126.  
  127.                     return x < 0.5
  128.                       ? (pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2
  129.                       : (pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
  130.                 }
  131.             case EasingType.easeInElastic:
  132.                 {
  133.                     const float c4 = (2 * PI) / 3;
  134.  
  135.                     return x == 0
  136.                       ? 0
  137.                       : x == 1
  138.                       ? 1
  139.                       : -pow(2, 10 * x - 10) * sin((x * 10 - 10.75f) * c4);
  140.                 }
  141.             case EasingType.easeOutElastic:
  142.                 {
  143.                     const float c4 = (2 * PI) / 3;
  144.  
  145.                     return x == 0
  146.                       ? 0
  147.                       : x == 1
  148.                       ? 1
  149.                       : pow(2, -10 * x) * sin((x * 10 - 0.75f) * c4) + 1;
  150.                 }
  151.             case EasingType.easeInOutElastic:
  152.                 {
  153.                     const float c5 = (2 * PI) / 4.5f;
  154.  
  155.                     return x == 0
  156.                       ? 0
  157.                       : x == 1
  158.                       ? 1
  159.                       : x < 0.5
  160.                       ? -(pow(2, 20 * x - 10) * sin((20 * x - 11.125f) * c5)) / 2
  161.                       : (pow(2, -20 * x + 10) * sin((20 * x - 11.125f) * c5)) / 2 + 1;
  162.                 }
  163.             case EasingType.easeInBounce:
  164.                 {
  165.                     return 1 - GetValue(EasingType.easeOutBounce, (1 - x));
  166.                 }
  167.             case EasingType.easeOutBounce:
  168.                 {
  169.                     const float n1 = 7.5625f;
  170.                     const float d1 = 2.75f;
  171.  
  172.                     if (x < 1 / d1)
  173.                     {
  174.                         return n1 * x * x;
  175.                     }
  176.                     else if (x < 2 / d1)
  177.                     {
  178.                         return n1 * (x -= 1.5f / d1) * x + 0.75f;
  179.                     }
  180.                     else if (x < 2.5 / d1)
  181.                     {
  182.                         return n1 * (x -= 2.25f / d1) * x + 0.9375f;
  183.                     }
  184.                     else
  185.                     {
  186.                         return n1 * (x -= 2.625f / d1) * x + 0.984375f;
  187.                     }
  188.                 }
  189.             case EasingType.easeInOutBounce:
  190.                 {
  191.                     return x < 0.5
  192.                            ? (1 - GetValue(EasingType.easeOutBounce, 1 - 2 * x)) / 2
  193.                            : (1 + GetValue(EasingType.easeOutBounce, 2 * x - 1)) / 2;
  194.                 }
  195.         }
  196.         return -1;
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment