Helpers.cs 2.0 KB

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