EaseUtility.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using Unity.Burst;
  2. using static Unity.Mathematics.math;
  3. namespace LitMotion
  4. {
  5. /// <summary>
  6. /// Utility class that provides calculation of easing functions.
  7. /// </summary>
  8. [BurstCompile]
  9. public static class EaseUtility
  10. {
  11. [BurstCompile]
  12. public static float Evaluate(float t, Ease ease)
  13. {
  14. return ease switch
  15. {
  16. Ease.InSine => InSine(t),
  17. Ease.OutSine => OutSine(t),
  18. Ease.InOutSine => InOutSine(t),
  19. Ease.InQuad => InQuad(t),
  20. Ease.OutQuad => OutQuad(t),
  21. Ease.InOutQuad => InOutQuad(t),
  22. Ease.InCubic => InCubic(t),
  23. Ease.OutCubic => OutCubic(t),
  24. Ease.InOutCubic => InOutCubic(t),
  25. Ease.InQuart => InQuart(t),
  26. Ease.OutQuart => OutQuart(t),
  27. Ease.InOutQuart => InOutQuart(t),
  28. Ease.InQuint => InQuint(t),
  29. Ease.OutQuint => OutQuint(t),
  30. Ease.InOutQuint => InOutQuint(t),
  31. Ease.InExpo => InExpo(t),
  32. Ease.OutExpo => OutExpo(t),
  33. Ease.InOutExpo => InOutExpo(t),
  34. Ease.InCirc => InCirc(t),
  35. Ease.OutCirc => OutCirc(t),
  36. Ease.InOutCirc => InOutCirc(t),
  37. Ease.InElastic => InElastic(t),
  38. Ease.OutElastic => OutElastic(t),
  39. Ease.InOutElastic => InOutElastic(t),
  40. Ease.InBack => InBack(t),
  41. Ease.OutBack => OutBack(t),
  42. Ease.InOutBack => InOutBack(t),
  43. Ease.InBounce => InBounce(t),
  44. Ease.OutBounce => OutBounce(t),
  45. Ease.InOutBounce => InOutBounce(t),
  46. _ => t,
  47. };
  48. }
  49. [BurstCompile]
  50. public static float Linear(float x) => x;
  51. [BurstCompile]
  52. public static float InSine(float x) => 1 - cos(x * PI / 2);
  53. [BurstCompile]
  54. public static float OutSine(float x) => sin(x * PI / 2);
  55. [BurstCompile]
  56. public static float InOutSine(float x) => -(cos(PI * x) - 1) / 2;
  57. [BurstCompile]
  58. public static float InQuad(float x) => x * x;
  59. [BurstCompile]
  60. public static float OutQuad(float x) => 1 - (1 - x) * (1 - x);
  61. [BurstCompile]
  62. public static float InOutQuad(float x) => x < 0.5f ? 2 * x * x : 1 - pow(-2 * x + 2, 2) / 2;
  63. [BurstCompile]
  64. public static float InCubic(float x) => x * x * x;
  65. [BurstCompile]
  66. public static float OutCubic(float x) => 1 - pow(1 - x, 3);
  67. [BurstCompile]
  68. public static float InOutCubic(float x) => x < 0.5f ? 4 * x * x * x : 1 - pow(-2 * x + 2, 3) / 2;
  69. [BurstCompile]
  70. public static float InQuart(float x) => x * x * x * x;
  71. [BurstCompile]
  72. public static float OutQuart(float x) => 1 - pow(1 - x, 4);
  73. [BurstCompile]
  74. public static float InOutQuart(float x) => x < 0.5 ? 8 * x * x * x * x : 1 - pow(-2 * x + 2, 4) / 2;
  75. [BurstCompile]
  76. public static float InQuint(float x) => x * x * x * x * x;
  77. [BurstCompile]
  78. public static float OutQuint(float x) => 1 - pow(1 - x, 5);
  79. [BurstCompile]
  80. public static float InOutQuint(float x) => x < 0.5f ? 16 * x * x * x * x * x : 1 - pow(-2 * x + 2, 5) / 2;
  81. [BurstCompile]
  82. public static float InExpo(float x) => x == 0 ? 0 : pow(2, 10 * x - 10);
  83. [BurstCompile]
  84. public static float OutExpo(float x) => x == 1 ? 1 : 1 - pow(2, -10 * x);
  85. [BurstCompile]
  86. public static float InOutExpo(float x)
  87. {
  88. return x == 0 ? 0 :
  89. x == 1 ? 1 :
  90. x < 0.5f ? pow(2, 20 * x - 10) / 2 :
  91. (2 - pow(2, -20 * x + 10)) / 2;
  92. }
  93. [BurstCompile]
  94. public static float InCirc(float x) => 1 - sqrt(1 - pow(x, 2));
  95. [BurstCompile]
  96. public static float OutCirc(float x) => sqrt(1 - pow(x - 1, 2));
  97. [BurstCompile]
  98. public static float InOutCirc(float x)
  99. {
  100. return x < 0.5 ?
  101. (1 - sqrt(1 - pow(2 * x, 2))) / 2 :
  102. (sqrt(1 - pow(-2 * x + 2, 2)) + 1) / 2;
  103. }
  104. [BurstCompile]
  105. public static float InBack(float x)
  106. {
  107. const float c1 = 1.70158f;
  108. const float c3 = c1 + 1;
  109. return c3 * x * x * x - c1 * x * x;
  110. }
  111. [BurstCompile]
  112. public static float OutBack(float x)
  113. {
  114. const float c1 = 1.70158f;
  115. const float c3 = c1 + 1;
  116. return 1 + c3 * pow(x - 1, 3) + c1 * pow(x - 1, 2);
  117. }
  118. [BurstCompile]
  119. public static float InOutBack(float x)
  120. {
  121. const float c1 = 1.70158f;
  122. const float c2 = c1 * 1.525f;
  123. return x < 0.5f
  124. ? pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2) / 2
  125. : (pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
  126. }
  127. [BurstCompile]
  128. public static float InElastic(float x)
  129. {
  130. const float c4 = 2 * PI / 3;
  131. return x == 0 ? 0 :
  132. x == 1 ? 1 :
  133. -pow(2, 10 * x - 10) * sin((x * 10 - 10.75f) * c4);
  134. }
  135. [BurstCompile]
  136. public static float OutElastic(float x)
  137. {
  138. const float c4 = 2 * PI / 3;
  139. return x == 0 ? 0 :
  140. x == 1 ? 1 :
  141. pow(2, -10 * x) * sin((x * 10 - 0.75f) * c4) + 1;
  142. }
  143. [BurstCompile]
  144. public static float InOutElastic(float x)
  145. {
  146. const float c5 = 2 * PI / 4.5f;
  147. return x == 0 ? 0 :
  148. x == 1 ? 1 :
  149. x < 0.5f ?
  150. -(pow(2, 20 * x - 10) * sin((20 * x - 11.125f) * c5)) / 2 :
  151. pow(2, -20 * x + 10) * sin((20 * x - 11.125f) * c5) / 2 + 1;
  152. }
  153. [BurstCompile]
  154. public static float InBounce(float x) => 1 - OutBounce(1 - x);
  155. [BurstCompile]
  156. public static float OutBounce(float x)
  157. {
  158. const float n1 = 7.5625f;
  159. const float d1 = 2.75f;
  160. float t = x;
  161. if (t < 1 / d1)
  162. {
  163. return n1 * t * t;
  164. }
  165. else if (t < 2 / d1)
  166. {
  167. return n1 * (t -= 1.5f / d1) * t + 0.75f;
  168. }
  169. else if (t < 2.5 / d1)
  170. {
  171. return n1 * (t -= 2.25f / d1) * t + 0.9375f;
  172. }
  173. else
  174. {
  175. return n1 * (t -= 2.625f / d1) * t + 0.984375f;
  176. }
  177. }
  178. [BurstCompile]
  179. public static float InOutBounce(float x)
  180. {
  181. return x < 0.5f ?
  182. (1 - OutBounce(1 - 2 * x)) / 2 :
  183. (1 + OutBounce(2 * x - 1)) / 2;
  184. }
  185. }
  186. }