using System.Linq; using UnityEngine; namespace ProGrids { public static class pg_Util { private const float EPSILON = 0.0001f; public static Color ColorWithString(string value) { string valid = "01234567890.,"; value = new string(value.Where((char c) => valid.Contains(c)).ToArray()); string[] array = value.Split(','); if (array.Length < 4) { return new Color(1f, 0f, 1f, 1f); } return new Color(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]), float.Parse(array[3])); } private static Vector3 VectorToMask(Vector3 vec) { return new Vector3((!(Mathf.Abs(vec.x) > Mathf.Epsilon)) ? 0f : 1f, (!(Mathf.Abs(vec.y) > Mathf.Epsilon)) ? 0f : 1f, (!(Mathf.Abs(vec.z) > Mathf.Epsilon)) ? 0f : 1f); } private static Axis MaskToAxis(Vector3 vec) { Axis axis = Axis.None; if (Mathf.Abs(vec.x) > 0f) { axis |= Axis.X; } if (Mathf.Abs(vec.y) > 0f) { axis |= Axis.Y; } if (Mathf.Abs(vec.z) > 0f) { axis |= Axis.Z; } return axis; } private static Axis BestAxis(Vector3 vec) { float num = Mathf.Abs(vec.x); float num2 = Mathf.Abs(vec.y); float num3 = Mathf.Abs(vec.z); return (num > num2 && num > num3) ? Axis.X : ((!(num2 > num) || !(num2 > num3)) ? Axis.Z : Axis.Y); } public static Axis CalcDragAxis(Vector3 movement, Camera cam) { Vector3 vector = VectorToMask(movement); if (vector.x + vector.y + vector.z == 2f) { return MaskToAxis(Vector3.one - vector); } switch (MaskToAxis(vector)) { case Axis.X: if (Mathf.Abs(Vector3.Dot(cam.transform.forward, Vector3.up)) < Mathf.Abs(Vector3.Dot(cam.transform.forward, Vector3.forward))) { return Axis.Z; } return Axis.Y; case Axis.Y: if (Mathf.Abs(Vector3.Dot(cam.transform.forward, Vector3.right)) < Mathf.Abs(Vector3.Dot(cam.transform.forward, Vector3.forward))) { return Axis.Z; } return Axis.X; case Axis.Z: if (Mathf.Abs(Vector3.Dot(cam.transform.forward, Vector3.right)) < Mathf.Abs(Vector3.Dot(cam.transform.forward, Vector3.up))) { return Axis.Y; } return Axis.X; default: return Axis.None; } } public static float ValueFromMask(Vector3 val, Vector3 mask) { if (Mathf.Abs(mask.x) > 0.0001f) { return val.x; } if (Mathf.Abs(mask.y) > 0.0001f) { return val.y; } return val.z; } public static Vector3 SnapValue(Vector3 val, float snapValue) { float x = val.x; float y = val.y; float z = val.z; return new Vector3(Snap(x, snapValue), Snap(y, snapValue), Snap(z, snapValue)); } public static Vector3 SnapValue(Vector3 val, Vector3 mask, float snapValue) { float x = val.x; float y = val.y; float z = val.z; return new Vector3((!(Mathf.Abs(mask.x) < 0.0001f)) ? Snap(x, snapValue) : x, (!(Mathf.Abs(mask.y) < 0.0001f)) ? Snap(y, snapValue) : y, (!(Mathf.Abs(mask.z) < 0.0001f)) ? Snap(z, snapValue) : z); } public static Vector3 SnapToCeil(Vector3 val, Vector3 mask, float snapValue) { float x = val.x; float y = val.y; float z = val.z; return new Vector3((!(Mathf.Abs(mask.x) < 0.0001f)) ? SnapToCeil(x, snapValue) : x, (!(Mathf.Abs(mask.y) < 0.0001f)) ? SnapToCeil(y, snapValue) : y, (!(Mathf.Abs(mask.z) < 0.0001f)) ? SnapToCeil(z, snapValue) : z); } public static Vector3 SnapToFloor(Vector3 val, float snapValue) { float x = val.x; float y = val.y; float z = val.z; return new Vector3(SnapToFloor(x, snapValue), SnapToFloor(y, snapValue), SnapToFloor(z, snapValue)); } public static Vector3 SnapToFloor(Vector3 val, Vector3 mask, float snapValue) { float x = val.x; float y = val.y; float z = val.z; return new Vector3((!(Mathf.Abs(mask.x) < 0.0001f)) ? SnapToFloor(x, snapValue) : x, (!(Mathf.Abs(mask.y) < 0.0001f)) ? SnapToFloor(y, snapValue) : y, (!(Mathf.Abs(mask.z) < 0.0001f)) ? SnapToFloor(z, snapValue) : z); } public static float Snap(float val, float round) { return round * Mathf.Round(val / round); } public static float SnapToFloor(float val, float snapValue) { return snapValue * Mathf.Floor(val / snapValue); } public static float SnapToCeil(float val, float snapValue) { return snapValue * Mathf.Ceil(val / snapValue); } public static Vector3 CeilFloor(Vector3 v) { v.x = ((!(v.x < 0f)) ? 1 : (-1)); v.y = ((!(v.y < 0f)) ? 1 : (-1)); v.z = ((!(v.z < 0f)) ? 1 : (-1)); return v; } } }