using System; using System.Collections.Generic; using System.Text.RegularExpressions; using UnityEngine; namespace kt.Utility { public static class MathUtility { public static float VolumeToDecibel(float volume) { return 20f * Mathf.Log10(Mathf.Clamp(volume, 0.0001f, 1f)); } public static float DecibelToVolume(float db) { float num = Mathf.Pow(10f, Mathf.Clamp(db, -80f, 0f) / 20f); return (num > 0.0001f) ? num : 0f; } public static float MillisecondToSecond(int millisecond) { return (float)millisecond / 1000f; } public static int SecondToMillisecond(float second) { return Mathf.FloorToInt(second * 1000f); } public static int RoundToDigits(int num, int maxDigits) { int[] array = new int[] { 0, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999 }; return Mathf.Clamp(num, 0, array[(array.Length >= maxDigits) ? 0 : maxDigits]); } public static bool Approximately(Vector2 objA, Vector2 objB) { return Mathf.Approximately(objA.x, objB.x) && Mathf.Approximately(objA.y, objB.y); } public static bool Approximately(Vector3 objA, Vector3 objB) { return Mathf.Approximately(objA.x, objB.x) && Mathf.Approximately(objA.y, objB.y) && Mathf.Approximately(objA.z, objB.z); } public static float Normalize(float val, float valmin, float valmax, float min = 0f, float max = 1f) { return (val - valmin) / (valmax - valmin) * (max - min) + min; } public static double Normalize(double val, double valmin, double valmax, double min = 0.0, double max = 1.0) { return (val - valmin) / (valmax - valmin) * (max - min) + min; } public static float Sin01(float a, float b, bool is_reverse = false, bool is_repeat = false) { return MathUtility.Sin01(a / b, is_reverse, is_repeat); } public static float Sin01(float t, bool is_reverse = false, bool is_repeat = false) { if (!is_repeat) { t = Mathf.Clamp01(t); } if (!is_reverse) { return Mathf.Abs(Mathf.Sin(t * 90f * 0.017453292f)); } return 1f - Mathf.Abs(Mathf.Sin(t * 90f * 0.017453292f)); } public static Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 c, float t) { Vector3 a = Vector3.Lerp(p0, c, t); Vector3 b = Vector3.Lerp(c, p1, t); return Vector3.Lerp(a, b, t); } public static Vector3 Hermite(Vector3 p0, Vector3 p1, Vector3 v0, Vector3 v1, float t) { Vector3 a = 2f * p0 + -2f * p1 + v0 + v1; Vector3 a2 = -3f * p0 + 3f * p1 + -2f * v0 - v1; float num = t * t; float d = num * t; return a * d + a2 * num + v0 * t + p0; } public static Vector3 Catmul(List point_list, float t) { if (point_list.Count == 0) { return Vector3.zero; } if (point_list.Count == 1) { return point_list[0]; } float num = (float)(point_list.Count - 1) * t; int num2 = Mathf.FloorToInt(num); float t2 = num - (float)num2; if (num2 >= point_list.Count - 1) { num2 = point_list.Count - 2; t2 = 1f; } Vector3 p = point_list[num2]; Vector3 p2 = point_list[num2 + 1]; Vector3 v = Vector3.zero; if (num2 > 0) { v = 0.5f * (point_list[num2 + 1] - point_list[num2 - 1]); } else { v = point_list[num2 + 1] - point_list[num2]; } Vector3 v2 = Vector3.zero; if (num2 < point_list.Count - 2) { v2 = 0.5f * (point_list[num2 + 2] - point_list[num2]); } else { v2 = point_list[num2 + 1] - point_list[num2]; } return MathUtility.Hermite(p, p2, v, v2, t2); } public static Vector3 Vec3Parse(string str, char split_ch = ',') { str = Regex.Replace(str, "[()]", string.Empty); str = Regex.Replace(str, "[ \u3000]", string.Empty); string[] array = str.Split(new char[] { split_ch }); Vector3 zero = Vector3.zero; zero.x = float.Parse(array[0]); zero.y = float.Parse(array[1]); zero.z = float.Parse(array[2]); return zero; } public static float AngleClamp360(float angle) { if (Mathf.Abs(angle) >= 360f) { if (angle > 0f) { angle -= (float)(Mathf.FloorToInt(angle / 360f) * 360); } else { angle += (float)(Mathf.FloorToInt(angle / 360f) * 360); } } else if (angle < 0f) { angle += 360f; } return angle; } public static Vector3 AngleClamp360(Vector3 angle) { angle.x = MathUtility.AngleClamp360(angle.x); angle.y = MathUtility.AngleClamp360(angle.y); angle.z = MathUtility.AngleClamp360(angle.z); return angle; } public static float AngleClamp180(float angle) { if (Mathf.Abs(angle) > 180f) { if (angle > 0f) { angle -= (float)(Mathf.FloorToInt(angle / 180f) * 360); } else { angle += (float)(Mathf.FloorToInt(angle / 180f) * 360); } } return angle; } public static Vector3 AngleClamp180(Vector3 angle) { angle.x = MathUtility.AngleClamp180(angle.x); angle.y = MathUtility.AngleClamp180(angle.y); angle.z = MathUtility.AngleClamp180(angle.z); return angle; } public static float GetValue(this Vector3 vector, MathUtility.VectorType type) { return vector[(int)type]; } public static Vector3 GetPrimitiveVector(MathUtility.VectorType type) { if (type == MathUtility.VectorType.X) { return Vector3.right; } if (type != MathUtility.VectorType.Y) { return Vector3.forward; } return Vector3.up; } public static int RatioToPercentage(float ratio, bool is_clamp = false) { int num = Mathf.FloorToInt(ratio * 100f); if (is_clamp) { num = Mathf.Clamp(num, 0, 100); } return num; } public static float PercentageToRatio(int percentage, bool is_clamp = false) { float num = (float)percentage / 100f; if (is_clamp) { num = Mathf.Clamp01(num); } return num; } public static float FloorDecimal(float value, int decimal_point) { float num = 1f; for (int i = 0; i < decimal_point; i++) { num *= 10f; } return Mathf.Floor(value * num) / num; } public static Vector3 FloorVec3Decimal(Vector3 value, int decimal_point) { value.x = MathUtility.FloorDecimal(value.x, decimal_point); value.y = MathUtility.FloorDecimal(value.y, decimal_point); value.z = MathUtility.FloorDecimal(value.z, decimal_point); return value; } public static bool IsVector3NaN(Vector3 vector) { return float.IsNaN(vector.x) || float.IsNaN(vector.y) || float.IsNaN(vector.z); } public enum VectorType { X, Y, Z } } }