5 using System.Collections.Generic;
17 public float SnapToGravityThreshold = 0.0f;
20 public float MinArea = 1.0f;
22 public bool VisualizeSubPlanes =
false;
32 foreach (MeshFilter mesh
in GetComponentsInChildren<MeshFilter>())
41 planes = (VisualizeSubPlanes) ?
46 private static Color[] colors =
new Color[] { Color.blue, Color.cyan, Color.green, Color.magenta, Color.red, Color.white, Color.yellow };
47 private void OnDrawGizmos()
51 for (
int i = 0; i < planes.Length; ++i)
56 Vector3 normal = planes[i].
Plane.normal;
57 center -= planes[i].
Plane.GetDistanceToPoint(center) * normal;
59 Vector3[] corners =
new Vector3[4] {
60 center + rotation *
new Vector3(+extents.x, +extents.y, 0),
61 center + rotation *
new Vector3(-extents.x, +extents.y, 0),
62 center + rotation *
new Vector3(-extents.x, -extents.y, 0),
63 center + rotation *
new Vector3(+extents.x, -extents.y, 0)
66 Color color = colors[i % colors.Length];
69 Gizmos.DrawLine(corners[0], corners[1]);
70 Gizmos.DrawLine(corners[0], corners[2]);
71 Gizmos.DrawLine(corners[0], corners[3]);
72 Gizmos.DrawLine(corners[1], corners[2]);
73 Gizmos.DrawLine(corners[1], corners[3]);
74 Gizmos.DrawLine(corners[2], corners[3]);
75 Gizmos.DrawLine(center, center + normal * 0.4f);
82 private void OnDrawGizmosSelected()
86 Ray cameraForward =
new Ray(Camera.current.transform.position, Camera.current.transform.forward);
89 for (
int i = 0; i < planes.Length; ++i)
94 Vector3 normal = planes[i].
Plane.normal;
95 center -= planes[i].
Plane.GetDistanceToPoint(center) * normal;
97 Vector3[] corners =
new Vector3[4] {
98 center + rotation *
new Vector3(+extents.x, +extents.y, 0),
99 center + rotation *
new Vector3(-extents.x, +extents.y, 0),
100 center + rotation *
new Vector3(-extents.x, -extents.y, 0),
101 center + rotation *
new Vector3(+extents.x, -extents.y, 0)
104 Color color = colors[i % colors.Length];
108 UnityEditor.Handles.DrawLine(corners[0], corners[1]);
109 UnityEditor.Handles.DrawLine(corners[0], corners[2]);
110 UnityEditor.Handles.DrawLine(corners[0], corners[3]);
111 UnityEditor.Handles.DrawLine(corners[1], corners[2]);
112 UnityEditor.Handles.DrawLine(corners[1], corners[3]);
113 UnityEditor.Handles.DrawLine(corners[2], corners[3]);
114 #if UNITY_2017_3_OR_NEWER 115 UnityEditor.Handles.ArrowHandleCap(0, center, Quaternion.FromToRotation(Vector3.forward, normal), 0.4f, EventType.Ignore);
117 UnityEditor.Handles.ArrowHandleCap(0, center, Quaternion.FromToRotation(Vector3.forward, normal), 0.4f, EventType.ignore);
122 float planeHitDistance;
123 if (planes[i].Plane.Raycast(cameraForward, out planeHitDistance))
125 Vector3 hitPoint = Quaternion.Inverse(rotation) * (cameraForward.GetPoint(planeHitDistance) - center);
126 if (Mathf.Abs(hitPoint.x) <= extents.x && Mathf.Abs(hitPoint.y) <= extents.y)
129 UnityEditor.Handles.DrawSolidRectangleWithOutline(corners, color, Color.clear);
131 string text =
string.Format(
"Area: {0} Bounds: {1}\nPlane: N{2}, D({3})",
132 planes[i].Area.ToString(
"F1"),
133 ((Vector2)extents).ToString(
"F2"),
134 normal.ToString(
"F3"),
135 planes[i].
Plane.distance.ToString(
"F3"));
137 UnityEditor.Handles.Label(center, text, GUI.skin.textField);