MathUtility.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. namespace kt.Utility
  6. {
  7. public static class MathUtility
  8. {
  9. public static float VolumeToDecibel(float volume)
  10. {
  11. return 20f * Mathf.Log10(Mathf.Clamp(volume, 0.0001f, 1f));
  12. }
  13. public static float DecibelToVolume(float db)
  14. {
  15. float num = Mathf.Pow(10f, Mathf.Clamp(db, -80f, 0f) / 20f);
  16. return (num > 0.0001f) ? num : 0f;
  17. }
  18. public static float MillisecondToSecond(int millisecond)
  19. {
  20. return (float)millisecond / 1000f;
  21. }
  22. public static int SecondToMillisecond(float second)
  23. {
  24. return Mathf.FloorToInt(second * 1000f);
  25. }
  26. public static int RoundToDigits(int num, int maxDigits)
  27. {
  28. int[] array = new int[]
  29. {
  30. 0,
  31. 9,
  32. 99,
  33. 999,
  34. 9999,
  35. 99999,
  36. 999999,
  37. 9999999,
  38. 99999999,
  39. 999999999
  40. };
  41. return Mathf.Clamp(num, 0, array[(array.Length >= maxDigits) ? 0 : maxDigits]);
  42. }
  43. public static bool Approximately(Vector2 objA, Vector2 objB)
  44. {
  45. return Mathf.Approximately(objA.x, objB.x) && Mathf.Approximately(objA.y, objB.y);
  46. }
  47. public static bool Approximately(Vector3 objA, Vector3 objB)
  48. {
  49. return Mathf.Approximately(objA.x, objB.x) && Mathf.Approximately(objA.y, objB.y) && Mathf.Approximately(objA.z, objB.z);
  50. }
  51. public static float Normalize(float val, float valmin, float valmax, float min = 0f, float max = 1f)
  52. {
  53. return (val - valmin) / (valmax - valmin) * (max - min) + min;
  54. }
  55. public static double Normalize(double val, double valmin, double valmax, double min = 0.0, double max = 1.0)
  56. {
  57. return (val - valmin) / (valmax - valmin) * (max - min) + min;
  58. }
  59. public static float Sin01(float a, float b, bool is_reverse = false, bool is_repeat = false)
  60. {
  61. return MathUtility.Sin01(a / b, is_reverse, is_repeat);
  62. }
  63. public static float Sin01(float t, bool is_reverse = false, bool is_repeat = false)
  64. {
  65. if (!is_repeat)
  66. {
  67. t = Mathf.Clamp01(t);
  68. }
  69. if (!is_reverse)
  70. {
  71. return Mathf.Abs(Mathf.Sin(t * 90f * 0.017453292f));
  72. }
  73. return 1f - Mathf.Abs(Mathf.Sin(t * 90f * 0.017453292f));
  74. }
  75. public static Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 c, float t)
  76. {
  77. Vector3 a = Vector3.Lerp(p0, c, t);
  78. Vector3 b = Vector3.Lerp(c, p1, t);
  79. return Vector3.Lerp(a, b, t);
  80. }
  81. public static Vector3 Hermite(Vector3 p0, Vector3 p1, Vector3 v0, Vector3 v1, float t)
  82. {
  83. Vector3 a = 2f * p0 + -2f * p1 + v0 + v1;
  84. Vector3 a2 = -3f * p0 + 3f * p1 + -2f * v0 - v1;
  85. float num = t * t;
  86. float d = num * t;
  87. return a * d + a2 * num + v0 * t + p0;
  88. }
  89. public static Vector3 Catmul(List<Vector3> point_list, float t)
  90. {
  91. if (point_list.Count == 0)
  92. {
  93. return Vector3.zero;
  94. }
  95. if (point_list.Count == 1)
  96. {
  97. return point_list[0];
  98. }
  99. float num = (float)(point_list.Count - 1) * t;
  100. int num2 = Mathf.FloorToInt(num);
  101. float t2 = num - (float)num2;
  102. if (num2 >= point_list.Count - 1)
  103. {
  104. num2 = point_list.Count - 2;
  105. t2 = 1f;
  106. }
  107. Vector3 p = point_list[num2];
  108. Vector3 p2 = point_list[num2 + 1];
  109. Vector3 v = Vector3.zero;
  110. if (num2 > 0)
  111. {
  112. v = 0.5f * (point_list[num2 + 1] - point_list[num2 - 1]);
  113. }
  114. else
  115. {
  116. v = point_list[num2 + 1] - point_list[num2];
  117. }
  118. Vector3 v2 = Vector3.zero;
  119. if (num2 < point_list.Count - 2)
  120. {
  121. v2 = 0.5f * (point_list[num2 + 2] - point_list[num2]);
  122. }
  123. else
  124. {
  125. v2 = point_list[num2 + 1] - point_list[num2];
  126. }
  127. return MathUtility.Hermite(p, p2, v, v2, t2);
  128. }
  129. public static Vector3 Vec3Parse(string str, char split_ch = ',')
  130. {
  131. str = Regex.Replace(str, "[()]", string.Empty);
  132. str = Regex.Replace(str, "[ \u3000]", string.Empty);
  133. string[] array = str.Split(new char[]
  134. {
  135. split_ch
  136. });
  137. Vector3 zero = Vector3.zero;
  138. zero.x = float.Parse(array[0]);
  139. zero.y = float.Parse(array[1]);
  140. zero.z = float.Parse(array[2]);
  141. return zero;
  142. }
  143. public static float AngleClamp360(float angle)
  144. {
  145. if (Mathf.Abs(angle) >= 360f)
  146. {
  147. if (angle > 0f)
  148. {
  149. angle -= (float)(Mathf.FloorToInt(angle / 360f) * 360);
  150. }
  151. else
  152. {
  153. angle += (float)(Mathf.FloorToInt(angle / 360f) * 360);
  154. }
  155. }
  156. else if (angle < 0f)
  157. {
  158. angle += 360f;
  159. }
  160. return angle;
  161. }
  162. public static Vector3 AngleClamp360(Vector3 angle)
  163. {
  164. angle.x = MathUtility.AngleClamp360(angle.x);
  165. angle.y = MathUtility.AngleClamp360(angle.y);
  166. angle.z = MathUtility.AngleClamp360(angle.z);
  167. return angle;
  168. }
  169. public static float AngleClamp180(float angle)
  170. {
  171. if (Mathf.Abs(angle) > 180f)
  172. {
  173. if (angle > 0f)
  174. {
  175. angle -= (float)(Mathf.FloorToInt(angle / 180f) * 360);
  176. }
  177. else
  178. {
  179. angle += (float)(Mathf.FloorToInt(angle / 180f) * 360);
  180. }
  181. }
  182. return angle;
  183. }
  184. public static Vector3 AngleClamp180(Vector3 angle)
  185. {
  186. angle.x = MathUtility.AngleClamp180(angle.x);
  187. angle.y = MathUtility.AngleClamp180(angle.y);
  188. angle.z = MathUtility.AngleClamp180(angle.z);
  189. return angle;
  190. }
  191. public static float GetValue(this Vector3 vector, MathUtility.VectorType type)
  192. {
  193. return vector[(int)type];
  194. }
  195. public static Vector3 GetPrimitiveVector(MathUtility.VectorType type)
  196. {
  197. if (type == MathUtility.VectorType.X)
  198. {
  199. return Vector3.right;
  200. }
  201. if (type != MathUtility.VectorType.Y)
  202. {
  203. return Vector3.forward;
  204. }
  205. return Vector3.up;
  206. }
  207. public static int RatioToPercentage(float ratio, bool is_clamp = false)
  208. {
  209. int num = Mathf.FloorToInt(ratio * 100f);
  210. if (is_clamp)
  211. {
  212. num = Mathf.Clamp(num, 0, 100);
  213. }
  214. return num;
  215. }
  216. public static float PercentageToRatio(int percentage, bool is_clamp = false)
  217. {
  218. float num = (float)percentage / 100f;
  219. if (is_clamp)
  220. {
  221. num = Mathf.Clamp01(num);
  222. }
  223. return num;
  224. }
  225. public static float FloorDecimal(float value, int decimal_point)
  226. {
  227. float num = 1f;
  228. for (int i = 0; i < decimal_point; i++)
  229. {
  230. num *= 10f;
  231. }
  232. return Mathf.Floor(value * num) / num;
  233. }
  234. public static Vector3 FloorVec3Decimal(Vector3 value, int decimal_point)
  235. {
  236. value.x = MathUtility.FloorDecimal(value.x, decimal_point);
  237. value.y = MathUtility.FloorDecimal(value.y, decimal_point);
  238. value.z = MathUtility.FloorDecimal(value.z, decimal_point);
  239. return value;
  240. }
  241. public static bool IsVector3NaN(Vector3 vector)
  242. {
  243. return float.IsNaN(vector.x) || float.IsNaN(vector.y) || float.IsNaN(vector.z);
  244. }
  245. public enum VectorType
  246. {
  247. X,
  248. Y,
  249. Z
  250. }
  251. }
  252. }