EaseMath.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. namespace Urho
  3. {
  4. internal static class EaseMath
  5. {
  6. internal static float BackIn(float time)
  7. {
  8. const float overshoot = 1.70158f;
  9. return time * time * ((overshoot + 1) * time - overshoot);
  10. }
  11. internal static float BackOut(float time)
  12. {
  13. const float overshoot = 1.70158f;
  14. time = time - 1;
  15. return time * time * ((overshoot + 1) * time + overshoot) + 1;
  16. }
  17. internal static float BackInOut(float time)
  18. {
  19. const float overshoot = 1.70158f * 1.525f;
  20. time = time * 2;
  21. if (time < 1)
  22. {
  23. return (time * time * ((overshoot + 1) * time - overshoot)) / 2;
  24. }
  25. else
  26. {
  27. time = time - 2;
  28. return (time * time * ((overshoot + 1) * time + overshoot)) / 2 + 1;
  29. }
  30. }
  31. internal static float BounceOut(float time)
  32. {
  33. if (time < 1 / 2.75)
  34. {
  35. return 7.5625f * time * time;
  36. }
  37. else if (time < 2 / 2.75)
  38. {
  39. time -= 1.5f / 2.75f;
  40. return 7.5625f * time * time + 0.75f;
  41. }
  42. else if (time < 2.5 / 2.75)
  43. {
  44. time -= 2.25f / 2.75f;
  45. return 7.5625f * time * time + 0.9375f;
  46. }
  47. time -= 2.625f / 2.75f;
  48. return 7.5625f * time * time + 0.984375f;
  49. }
  50. internal static float BounceIn(float time)
  51. {
  52. return 1f - BounceOut(1f - time);
  53. }
  54. internal static float BounceInOut(float time)
  55. {
  56. if (time < 0.5f)
  57. {
  58. time = time * 2;
  59. return (1 - BounceOut(1 - time)) * 0.5f;
  60. }
  61. return BounceOut(time * 2 - 1) * 0.5f + 0.5f;
  62. }
  63. internal static float SineOut(float time)
  64. {
  65. return (float) Math.Sin(time * MathHelper.PiOver2);
  66. }
  67. internal static float SineIn(float time)
  68. {
  69. return -1f * (float)Math.Cos(time * MathHelper.PiOver2) + 1f;
  70. }
  71. internal static float SineInOut(float time)
  72. {
  73. return -0.5f * ((float)Math.Cos((float)Math.PI * time) - 1f);
  74. }
  75. internal static float ExponentialOut(float time)
  76. {
  77. return time == 1f ? 1f : (-(float)Math.Pow(2f, -10f * time / 1f) + 1f);
  78. }
  79. internal static float ExponentialIn(float time)
  80. {
  81. return time == 0f ? 0f : (float)Math.Pow(2f, 10f * (time / 1f - 1f)) - 1f * 0.001f;
  82. }
  83. internal static float ExponentialInOut(float time)
  84. {
  85. time /= 0.5f;
  86. if (time < 1)
  87. {
  88. return 0.5f * (float)Math.Pow(2f, 10f * (time - 1f));
  89. }
  90. else
  91. {
  92. return 0.5f * (-(float)Math.Pow(2f, -10f * (time - 1f)) + 2f);
  93. }
  94. }
  95. internal static float ElasticIn(float time, float period)
  96. {
  97. if (time == 0 || time == 1)
  98. {
  99. return time;
  100. }
  101. else
  102. {
  103. float s = period / 4;
  104. time = time - 1;
  105. return -(float)(Math.Pow(2, 10 * time) * Math.Sin((time - s) * MathHelper.Pi * 2.0f / period));
  106. }
  107. }
  108. internal static float ElasticOut(float time, float period)
  109. {
  110. if (time == 0 || time == 1)
  111. {
  112. return time;
  113. }
  114. else
  115. {
  116. float s = period / 4;
  117. return (float)(Math.Pow(2, -10 * time) * Math.Sin((time - s) * MathHelper.Pi * 2f / period) + 1);
  118. }
  119. }
  120. internal static float ElasticInOut(float time, float period)
  121. {
  122. if (time == 0 || time == 1)
  123. {
  124. return time;
  125. }
  126. else
  127. {
  128. time = time * 2;
  129. if (period == 0)
  130. {
  131. period = 0.3f * 1.5f;
  132. }
  133. float s = period / 4;
  134. time = time - 1;
  135. if (time < 0)
  136. {
  137. return (float)(-0.5f * Math.Pow(2, 10 * time) * Math.Sin((time - s) * MathHelper.TwoPi / period));
  138. }
  139. else
  140. {
  141. return (float)(Math.Pow(2, -10 * time) * Math.Sin((time - s) * MathHelper.TwoPi / period) * 0.5f + 1);
  142. }
  143. }
  144. }
  145. }
  146. }