You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.8 KiB

  1. using UnityEngine;
  2. namespace Animmal.Animmals1
  3. {
  4. public sealed class Mathfx
  5. {
  6. //Ease in out
  7. public static float EasyInOut(float start, float end, float value)
  8. {
  9. return Mathf.Lerp(start, end, value * value * (3.0f - 2.0f * value));
  10. }
  11. public static Vector2 EasyInOut(Vector2 start, Vector2 end, float value)
  12. {
  13. return new Vector2(EasyInOut(start.x, end.x, value), EasyInOut(start.y, end.y, value));
  14. }
  15. public static Vector3 EasyInOut(Vector3 start, Vector3 end, float value)
  16. {
  17. return new Vector3(EasyInOut(start.x, end.x, value), EasyInOut(start.y, end.y, value), EasyInOut(start.z, end.z, value));
  18. }
  19. //Ease out
  20. public static float EasyOut(float start, float end, float value)
  21. {
  22. return Mathf.Lerp(start, end, Mathf.Sin(value * Mathf.PI * 0.5f));
  23. }
  24. public static Vector2 EasyOut(Vector2 start, Vector2 end, float value)
  25. {
  26. return new Vector2(Mathf.Lerp(start.x, end.x, Mathf.Sin(value * Mathf.PI * 0.5f)), Mathf.Lerp(start.y, end.y, Mathf.Sin(value * Mathf.PI * 0.5f)));
  27. }
  28. public static Vector3 EasyOut(Vector3 start, Vector3 end, float value)
  29. {
  30. return new Vector3(Mathf.Lerp(start.x, end.x, Mathf.Sin(value * Mathf.PI * 0.5f)), Mathf.Lerp(start.y, end.y, Mathf.Sin(value * Mathf.PI * 0.5f)), Mathf.Lerp(start.z, end.z, Mathf.Sin(value * Mathf.PI * 0.5f)));
  31. }
  32. //Ease in
  33. public static float EasyIn(float start, float end, float value)
  34. {
  35. return Mathf.Lerp(start, end, 1.0f - Mathf.Cos(value * Mathf.PI * 0.5f));
  36. }
  37. public static Vector2 EasyIn(Vector2 start, Vector2 end, float value)
  38. {
  39. return new Vector2(EasyIn(start.x, end.x, value), EasyIn(start.y, end.y, value));
  40. }
  41. public static Vector3 EasyIn(Vector3 start, Vector3 end, float value)
  42. {
  43. return new Vector3(EasyIn(start.x, end.x, value), EasyIn(start.y, end.y, value), EasyIn(start.z, end.z, value));
  44. }
  45. //Boing
  46. public static float Boing(float start, float end, float value)
  47. {
  48. value = Mathf.Clamp01(value);
  49. value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) * (1f + (1.2f * (1f - value)));
  50. return start + (end - start) * value;
  51. }
  52. public static Vector2 Boing(Vector2 start, Vector2 end, float value)
  53. {
  54. return new Vector2(Boing(start.x, end.x, value), Boing(start.y, end.y, value));
  55. }
  56. public static Vector3 Boing(Vector3 start, Vector3 end, float value)
  57. {
  58. return new Vector3(Boing(start.x, end.x, value), Boing(start.y, end.y, value), Boing(start.z, end.z, value));
  59. }
  60. //Like lerp with ease in ease out
  61. public static float SmoothStep(float x, float min, float max)
  62. {
  63. x = Mathf.Clamp(x, min, max);
  64. float v1 = (x - min) / (max - min);
  65. float v2 = (x - min) / (max - min);
  66. return -2 * v1 * v1 * v1 + 3 * v2 * v2;
  67. }
  68. public static Vector2 SmoothStep(Vector2 vec, float min, float max)
  69. {
  70. return new Vector2(SmoothStep(vec.x, min, max), SmoothStep(vec.y, min, max));
  71. }
  72. public static Vector3 SmoothStep(Vector3 vec, float min, float max)
  73. {
  74. return new Vector3(SmoothStep(vec.x, min, max), SmoothStep(vec.y, min, max), SmoothStep(vec.z, min, max));
  75. }
  76. public static float Lerp(float start, float end, float value)
  77. {
  78. return ((1.0f - value) * start) + (value * end);
  79. }
  80. public static Vector3 NearestPoint(Vector3 lineStart, Vector3 lineEnd, Vector3 point)
  81. {
  82. Vector3 lineDirection = Vector3.Normalize(lineEnd - lineStart);
  83. float closestPoint = Vector3.Dot((point - lineStart), lineDirection);
  84. return lineStart + (closestPoint * lineDirection);
  85. }
  86. public static Vector3 NearestPointStrict(Vector3 lineStart, Vector3 lineEnd, Vector3 point)
  87. {
  88. Vector3 fullDirection = lineEnd - lineStart;
  89. Vector3 lineDirection = Vector3.Normalize(fullDirection);
  90. float closestPoint = Vector3.Dot((point - lineStart), lineDirection);
  91. return lineStart + (Mathf.Clamp(closestPoint, 0.0f, Vector3.Magnitude(fullDirection)) * lineDirection);
  92. }
  93. //Bounce
  94. public static float Bounce(float x)
  95. {
  96. return Mathf.Abs(Mathf.Sin(6.28f * (x + 1f) * (x + 1f)) * (1f - x));
  97. }
  98. public static Vector2 Bounce(Vector2 vec)
  99. {
  100. return new Vector2(Bounce(vec.x), Bounce(vec.y));
  101. }
  102. public static Vector3 Bounce(Vector3 vec)
  103. {
  104. return new Vector3(Bounce(vec.x), Bounce(vec.y), Bounce(vec.z));
  105. }
  106. // test for value that is near specified float (due to floating point inprecision)
  107. // all thanks to Opless for this!
  108. public static bool Approx(float val, float about, float range)
  109. {
  110. return ((Mathf.Abs(val - about) < range));
  111. }
  112. // test if a Vector3 is close to another Vector3 (due to floating point inprecision)
  113. // compares the square of the distance to the square of the range as this
  114. // avoids calculating a square root which is much slower than squaring the range
  115. public static bool Approx(Vector3 val, Vector3 about, float range)
  116. {
  117. return ((val - about).sqrMagnitude < range * range);
  118. }
  119. /*
  120. * CLerp - Circular Lerp - is like lerp but handles the wraparound from 0 to 360.
  121. * This is useful when interpolating eulerAngles and the object
  122. * crosses the 0/360 boundary. The standard Lerp function causes the object
  123. * to rotate in the wrong direction and looks stupid. Clerp fixes that.
  124. */
  125. public static float Clerp(float start, float end, float value)
  126. {
  127. float min = 0.0f;
  128. float max = 360.0f;
  129. float half = Mathf.Abs((max - min) / 2.0f);//half the distance between min and max
  130. float retval = 0.0f;
  131. float diff = 0.0f;
  132. if ((end - start) < -half)
  133. {
  134. diff = ((max - start) + end) * value;
  135. retval = start + diff;
  136. }
  137. else if ((end - start) > half)
  138. {
  139. diff = -((max - end) + start) * value;
  140. retval = start + diff;
  141. }
  142. else retval = start + (end - start) * value;
  143. // Debug.Log("Start: " + start + " End: " + end + " Value: " + value + " Half: " + half + " Diff: " + diff + " Retval: " + retval);
  144. return retval;
  145. }
  146. }
  147. }