4 namespace droid.Runtime.Utilities.GameObjects.BoundingBoxes.Experimental {
8 public static class BoundingBoxUtilities {
12 public const int _Num_Points_Per_Line = 2;
14 public const int _Num_Lines = 12;
15 public static Vector3 _Top_Front_Right =
new Vector3(1, 1, 1);
16 public static Vector3 _Top_Front_Left =
new Vector3(-1, 1, 1);
17 public static Vector3 _Bottom_Back_Right =
new Vector3(1, -1, -1);
18 public static Vector3 _Bottom_Back_Left =
new Vector3(-1, -1, -1);
19 public static Vector3 _Bottom_Front_Left =
new Vector3(-1, -1, 1);
20 public static Vector3 _Bottom_Front_Right =
new Vector3(1, -1, 1);
21 public static Vector3 _Top_Back_Right =
new Vector3(1, 1, -1);
22 public static Vector3 _Top_Back_Left =
new Vector3(-1, 1, -1);
34 public static Vector3[] GetCameraMinMaxPoints(
this Mesh mesh,
39 bool use_view_port =
false) {
40 var a = mesh.vertices;
44 foreach (var t1
in a) {
46 point = cam.WorldToViewportPoint(t.TransformPoint(t1));
48 point = cam.WorldToScreenPoint(t.TransformPoint(t1));
51 point.GetMinMax(ref min, ref max);
56 return new[] {min, max, size};
67 public static Vector3[] GetCameraMinMaxPoints(
this Mesh mesh,
70 bool use_view_port =
false) {
71 var a = mesh.vertices;
75 min = cam.WorldToViewportPoint(t.TransformPoint(a[0]));
77 min = cam.WorldToScreenPoint(t.TransformPoint(a[0]));
83 point.GetMinMax(ref min, ref max);
85 for (var i = 1; i < a.Length; i++) {
87 point = cam.WorldToViewportPoint(t.TransformPoint(a[i]));
89 point = cam.WorldToScreenPoint(t.TransformPoint(a[i]));
92 point.GetMinMax(ref min, ref max);
97 return new[] {min, max, size};
108 public static Rect GetCameraMinMaxRect(
this Mesh mesh,
112 bool use_viewport =
false) {
113 var cen = mesh.GetCameraMinMaxPoints(t, cam, use_viewport);
117 var r = Rect.MinMaxRect(min.x, min.y, max.x, max.y);
126 public static Rect GetMinMaxRect(Vector3 min, Vector3 max,
float margin = 0) {
127 var r = Rect.MinMaxRect(min.x, min.y, max.x, max.y);
143 public static Rect Normalise(
this Rect rect,
float width,
float height) {
144 if (width <
float.Epsilon || Math.Abs(height) <
float.Epsilon) {
150 width = rect.width / width,
152 height = rect.height / height
162 public static Bounds TransformBounds(
this Transform transform, Bounds local_bounds) {
163 var center = transform.TransformPoint(local_bounds.center);
166 var extents = local_bounds.extents;
167 var axis_x = transform.TransformVector(extents.x, 0, 0);
168 var axis_y = transform.TransformVector(0, extents.y, 0);
169 var axis_z = transform.TransformVector(0, 0, extents.z);
172 extents.x = Mathf.Abs(axis_x.x) + Mathf.Abs(axis_y.x) + Mathf.Abs(axis_z.x);
173 extents.y = Mathf.Abs(axis_x.y) + Mathf.Abs(axis_y.y) + Mathf.Abs(axis_z.y);
174 extents.z = Mathf.Abs(axis_x.z) + Mathf.Abs(axis_y.z) + Mathf.Abs(axis_z.z);
176 return new Bounds {center = center, extents = extents};