Helpers.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using UnityEngine;
  2. public class Helpers
  3. {
  4. public static void DrawPlane(Vector3 position, Vector3 normal)
  5. {
  6. Vector3 v3;
  7. if (normal.normalized != Vector3.forward)
  8. v3 = Vector3.Cross(normal, Vector3.forward).normalized * normal.magnitude;
  9. else
  10. v3 = Vector3.Cross(normal, Vector3.up).normalized * normal.magnitude;
  11. ;
  12. var corner0 = position + v3;
  13. var corner2 = position - v3;
  14. var q = Quaternion.AngleAxis(90.0f, normal);
  15. v3 = q * v3;
  16. var corner1 = position + v3;
  17. var corner3 = position - v3;
  18. Debug.DrawLine(corner0, corner2, Color.green);
  19. Debug.DrawLine(corner1, corner3, Color.green);
  20. Debug.DrawLine(corner0, corner1, Color.green);
  21. Debug.DrawLine(corner1, corner2, Color.green);
  22. Debug.DrawLine(corner2, corner3, Color.green);
  23. Debug.DrawLine(corner3, corner0, Color.green);
  24. Debug.DrawRay(position, normal, Color.red);
  25. }
  26. public static void DrawLine(Vector3 p1, Vector3 p2, float width)
  27. {
  28. int count = 1 + Mathf.CeilToInt(width); // how many lines are needed.
  29. if (count == 1)
  30. {
  31. Gizmos.DrawLine(p1, p2);
  32. }
  33. else
  34. {
  35. Camera c = Camera.current;
  36. if (c == null)
  37. {
  38. Debug.LogError("Camera.current is null");
  39. return;
  40. }
  41. var scp1 = c.WorldToScreenPoint(p1);
  42. var scp2 = c.WorldToScreenPoint(p2);
  43. Vector3 v1 = (scp2 - scp1).normalized; // line direction
  44. Vector3 n = Vector3.Cross(v1, Vector3.forward); // normal vector
  45. for (int i = 0; i < count; i++)
  46. {
  47. Vector3 o = 0.99f * n * width * ((float) i / (count - 1) - 0.5f);
  48. Vector3 origin = c.ScreenToWorldPoint(scp1 + o);
  49. Vector3 destiny = c.ScreenToWorldPoint(scp2 + o);
  50. Gizmos.DrawLine(origin, destiny);
  51. }
  52. }
  53. }
  54. public static float GetMinComponent(Vector3 vector)
  55. {
  56. if (vector.x <= vector.y && vector.x <= vector.z) return vector.x;
  57. if (vector.y <= vector.x && vector.y <= vector.z) return vector.y;
  58. return vector.z;
  59. }
  60. }