Vectors.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using UnityEngine;
  4. namespace FirstGearGames.Utilities.Maths
  5. {
  6. public static class Vectors
  7. {
  8. /// <summary>
  9. /// Vector3.zero.
  10. /// </summary>
  11. private static readonly Vector3 VECTOR3_ZERO = new Vector3(0.0f, 0.0f, 0.0f);
  12. /// <summary>
  13. /// Float epislon.
  14. /// </summary>
  15. private const float FLOAT_EPSILON = 0.00001f;
  16. /// <summary>
  17. /// Float square epislon.
  18. /// </summary>
  19. private const float FLOAT_SQR_EPSILON = 1e-15f;
  20. #region Vector3.
  21. /// <summary>
  22. /// Calculates the linear parameter t that produces the interpolant value within the range [a, b].
  23. /// </summary>
  24. /// <param name="a"></param>
  25. /// <param name="b"></param>
  26. /// <param name="value"></param>
  27. /// <returns></returns>
  28. public static float InverseLerp(Vector3 a, Vector3 b, Vector3 value)
  29. {
  30. Vector3 ab = b - a;
  31. Vector3 av = value - a;
  32. return Mathf.Clamp01(Vector3.Dot(av, ab) / Vector3.Dot(ab, ab));
  33. }
  34. /// <summary>
  35. /// Returns if the target Vector3 is within variance of the source Vector3.
  36. /// </summary>
  37. /// <param name="a">Source vector.</param>
  38. /// <param name="b">Target vector.</param>
  39. /// <param name="tolerance">How close the target vector must be to be considered close.</param>
  40. /// <returns></returns>
  41. public static bool Near(this Vector3 a, Vector3 b, float tolerance = 0.01f)
  42. {
  43. return (Vector3.Distance(a, b) <= tolerance);
  44. }
  45. /// <summary>
  46. /// Returns if any values within a Vector3 are NaN.
  47. /// </summary>
  48. /// <param name="source"></param>
  49. /// <returns></returns>
  50. public static bool IsNan(this Vector3 source)
  51. {
  52. return (float.IsNaN(source.x) || float.IsNaN(source.y) || float.IsNaN(source.z));
  53. }
  54. /// <summary>
  55. /// Lerp between three Vector3 values.
  56. /// </summary>
  57. /// <param name="a"></param>
  58. /// <param name="b"></param>
  59. /// <param name="c"></param>
  60. /// <param name="percent"></param>
  61. /// <returns></returns>
  62. public static Vector3 Lerp3(Vector3 a, Vector3 b, Vector3 c, float percent)
  63. {
  64. Vector3 r0 = Vector3.Lerp(a, b, percent);
  65. Vector3 r1 = Vector3.Lerp(b, c, percent);
  66. return Vector3.Lerp(r0, r1, percent);
  67. }
  68. /// <summary>
  69. /// Lerp between three Vector3 values.
  70. /// </summary>
  71. /// <param name="vectors"></param>
  72. /// <param name="percent"></param>
  73. /// <returns></returns>
  74. public static Vector3 Lerp3(Vector3[] vectors, float percent)
  75. {
  76. if (vectors.Length < 3)
  77. {
  78. Debug.LogWarning("Vectors -> Lerp3 -> Vectors length must be 3.");
  79. return Vector3.zero;
  80. }
  81. return Lerp3(vectors[0], vectors[1], vectors[2], percent);
  82. }
  83. /// <summary>
  84. /// Multiplies a Vector3 by another.
  85. /// </summary>
  86. /// <param name="src"></param>
  87. /// <param name="multiplier"></param>
  88. /// <returns></returns>
  89. public static Vector3 Multiply(this Vector3 src, Vector3 multiplier)
  90. {
  91. return new Vector3(src.x * multiplier.x, src.y * multiplier.y, src.z * multiplier.z);
  92. }
  93. #region Fast.
  94. /* Fast checks are property of:
  95. * Copyright (c) 2020 Maxim Munnig Schmidt
  96. *
  97. * Permission is hereby granted, free of charge, to any person obtaining a copy
  98. * of this software and associated documentation files (the "Software"), to deal
  99. * in the Software without restriction, including without limitation the rights
  100. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  101. * copies of the Software, and to permit persons to whom the Software is
  102. * furnished to do so, subject to the following conditions:
  103. *
  104. * The above copyright notice and this permission notice shall be included in all
  105. * copies or substantial portions of the Software.
  106. *
  107. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  108. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  109. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  110. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  111. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  112. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  113. * SOFTWARE.
  114. */
  115. /// <summary>
  116. /// Fast Distance.
  117. /// </summary>
  118. /// <param name="a"></param>
  119. /// <param name="b"></param>
  120. /// <returns></returns>
  121. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  122. public static float FastDistance(Vector3 a, Vector3 b)
  123. {
  124. var distx = a.x - b.x;
  125. var disty = a.y - b.y;
  126. var distz = a.z - b.z;
  127. return (float)Math.Sqrt(distx * distx + disty * disty + distz * distz);
  128. }
  129. /// <summary>
  130. /// Fast SqrMagnitude.
  131. /// </summary>
  132. /// <param name="vector"></param>
  133. /// <returns></returns>
  134. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  135. public static float FastSqrMagnitude(Vector3 vector)
  136. {
  137. return vector.x * vector.x + vector.y * vector.y + vector.z * vector.z;
  138. }
  139. /// <summary>
  140. /// Fast Normalize.
  141. /// </summary>
  142. /// <param name="value"></param>
  143. /// <returns></returns>
  144. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  145. public static Vector3 FastNormalize(Vector3 value)
  146. {
  147. float mag = (float)Math.Sqrt(value.x * value.x + value.y * value.y + value.z * value.z); //Magnitude(value);
  148. if (mag > FLOAT_EPSILON)
  149. {
  150. Vector3 result;
  151. result.x = value.x / mag;
  152. result.y = value.y / mag;
  153. result.z = value.z / mag;
  154. return result;// value / mag;
  155. }
  156. else
  157. return VECTOR3_ZERO;
  158. }
  159. #endregion
  160. #endregion
  161. #region Vector2.
  162. /// <summary>
  163. /// Lerp between three Vector2 values.
  164. /// </summary>
  165. /// <param name="a"></param>
  166. /// <param name="b"></param>
  167. /// <param name="c"></param>
  168. /// <param name="percent"></param>
  169. /// <returns></returns>
  170. public static Vector2 Lerp3(Vector2 a, Vector2 b, Vector2 c, float percent)
  171. {
  172. Vector2 r0 = Vector2.Lerp(a, b, percent);
  173. Vector2 r1 = Vector2.Lerp(b, c, percent);
  174. return Vector2.Lerp(r0, r1, percent);
  175. }
  176. /// <summary>
  177. /// Lerp between three Vector2 values.
  178. /// </summary>
  179. /// <param name="vectors"></param>
  180. /// <param name="percent"></param>
  181. /// <returns></returns>
  182. public static Vector2 Lerp2(Vector2[] vectors, float percent)
  183. {
  184. if (vectors.Length < 3)
  185. {
  186. Debug.LogWarning("Vectors -> Lerp3 -> Vectors length must be 3.");
  187. return Vector2.zero;
  188. }
  189. return Lerp3(vectors[0], vectors[1], vectors[2], percent);
  190. }
  191. /// <summary>
  192. /// Multiplies a Vector2 by another.
  193. /// </summary>
  194. /// <param name="src"></param>
  195. /// <param name="multiplier"></param>
  196. /// <returns></returns>
  197. public static Vector2 Multiply(this Vector2 src, Vector2 multiplier)
  198. {
  199. return new Vector2(src.x * multiplier.x, src.y * multiplier.y);
  200. }
  201. #endregion
  202. }
  203. }