3 namespace droid.Runtime.Utilities.GameObjects.BoundingBoxes.Experimental.Unused {
4 public static class BoundingBoxUtilities {
11 public static void DrawBoxFromCenter(Vector3 p,
float r, Color c) {
14 Debug.DrawLine(
new Vector3(-r + p.x, -r + p.y, -r + p.z),
new Vector3(r + p.x, -r + p.y, -r + p.z), c);
15 Debug.DrawLine(
new Vector3(-r + p.x, -r + p.y, -r + p.z),
new Vector3(-r + p.x, -r + p.y, r + p.z), c);
16 Debug.DrawLine(
new Vector3(r + p.x, -r + p.y, r + p.z),
new Vector3(-r + p.x, -r + p.y, r + p.z), c);
17 Debug.DrawLine(
new Vector3(r + p.x, -r + p.y, r + p.z),
new Vector3(r + p.x, -r + p.y, -r + p.z), c);
20 Debug.DrawLine(
new Vector3(-r + p.x, r + p.y, -r + p.z),
new Vector3(r + p.x, r + p.y, -r + p.z), c);
21 Debug.DrawLine(
new Vector3(-r + p.x, r + p.y, -r + p.z),
new Vector3(-r + p.x, r + p.y, r + p.z), c);
22 Debug.DrawLine(
new Vector3(r + p.x, r + p.y, r + p.z),
new Vector3(-r + p.x, r + p.y, r + p.z), c);
23 Debug.DrawLine(
new Vector3(r + p.x, r + p.y, r + p.z),
new Vector3(r + p.x, r + p.y, -r + p.z), c);
26 Debug.DrawLine(
new Vector3(-r + p.x, -r + p.y, -r + p.z),
new Vector3(-r + p.x, r + p.y, -r + p.z), c);
27 Debug.DrawLine(
new Vector3(-r + p.x, -r + p.y, r + p.z),
new Vector3(-r + p.x, r + p.y, r + p.z), c);
28 Debug.DrawLine(
new Vector3(r + p.x, -r + p.y, -r + p.z),
new Vector3(r + p.x, r + p.y, -r + p.z), c);
29 Debug.DrawLine(
new Vector3(r + p.x, -r + p.y, r + p.z),
new Vector3(r + p.x, r + p.y, r + p.z), c);
40 public static void DrawRect(
float x_size,
float y_size,
float z_size, Vector3 pos, Color color) {
46 Debug.DrawLine(
new Vector3(-x + pos.x, -y + pos.y, -z + pos.z),
47 new Vector3(-x + pos.x, y + pos.y, -z + pos.z),
49 Debug.DrawLine(
new Vector3(x + pos.x, -y + pos.y, -z + pos.z),
50 new Vector3(x + pos.x, y + pos.y, -z + pos.z),
52 Debug.DrawLine(
new Vector3(-x + pos.x, -y + pos.y, z + pos.z),
53 new Vector3(-x + pos.x, y + pos.y, z + pos.z),
55 Debug.DrawLine(
new Vector3(x + pos.x, -y + pos.y, z + pos.z),
56 new Vector3(x + pos.x, y + pos.y, z + pos.z),
60 Debug.DrawLine(
new Vector3(-x + pos.x, y + pos.y, -z + pos.z),
61 new Vector3(x + pos.x, y + pos.y, -z + pos.z),
63 Debug.DrawLine(
new Vector3(-x + pos.x, y + pos.y, z + pos.z),
64 new Vector3(x + pos.x, y + pos.y, z + pos.z),
66 Debug.DrawLine(
new Vector3(-x + pos.x, y + pos.y, -z + pos.z),
67 new Vector3(-x + pos.x, y + pos.y, z + pos.z),
69 Debug.DrawLine(
new Vector3(x + pos.x, y + pos.y, -z + pos.z),
70 new Vector3(x + pos.x, y + pos.y, z + pos.z),
74 Debug.DrawLine(
new Vector3(-x + pos.x, -y + pos.y, -z + pos.z),
75 new Vector3(x + pos.x, -y + pos.y, -z + pos.z),
77 Debug.DrawLine(
new Vector3(-x + pos.x, -y + pos.y, z + pos.z),
78 new Vector3(x + pos.x, -y + pos.y, z + pos.z),
80 Debug.DrawLine(
new Vector3(-x + pos.x, -y + pos.y, -z + pos.z),
81 new Vector3(-x + pos.x, -y + pos.y, z + pos.z),
83 Debug.DrawLine(
new Vector3(x + pos.x, -y + pos.y, -z + pos.z),
84 new Vector3(x + pos.x, -y + pos.y, z + pos.z),
94 public static bool DidTransformsChange(
95 Transform[] old_transforms,
96 Transform[] newly_acquired_transforms) {
97 if (old_transforms.Length != newly_acquired_transforms.Length) {
102 foreach (var old
in old_transforms) {
103 if (old.position != newly_acquired_transforms[i].position
104 || old.rotation != newly_acquired_transforms[i].rotation) {
119 public static Bounds GetTotalMeshFilterBounds(Transform object_transform) {
120 var mesh_filter = object_transform.GetComponent<MeshFilter>();
122 var result = mesh_filter != null ? mesh_filter.mesh.bounds :
new Bounds();
124 foreach (Transform transform
in object_transform) {
125 var bounds = GetTotalMeshFilterBounds(transform);
126 result.Encapsulate(bounds.min);
127 result.Encapsulate(bounds.max);
141 var scaled_min = result.min;
142 var local_scale = object_transform.localScale;
143 scaled_min.Scale(local_scale);
144 result.min = scaled_min;
145 var scaled_max = result.max;
146 scaled_max.Scale(local_scale);
147 result.max = scaled_max;
155 public static Bounds GetTotalColliderBounds(Transform object_transform) {
156 var mesh_filter = object_transform.GetComponent<Collider>();
158 var result = mesh_filter != null ? mesh_filter.bounds :
new Bounds();
160 foreach (Transform transform
in object_transform) {
161 var bounds = GetTotalColliderBounds(transform);
162 result.Encapsulate(bounds.min);
163 result.Encapsulate(bounds.max);
166 var scaled_min = result.min;
167 var local_scale = object_transform.localScale;
168 scaled_min.Scale(local_scale);
169 result.min = scaled_min;
170 var scaled_max = result.max;
171 scaled_max.Scale(local_scale);
172 result.max = scaled_max;
180 public static Bounds GetMaxBounds(GameObject g) {
181 var b =
new Bounds(g.transform.position, Vector3.zero);
182 foreach (var r
in g.GetComponentsInChildren<Renderer>()) {
183 b.Encapsulate(r.bounds);
196 public static Rect GetBoundsScreenRectEncapsulationSlow(
this Bounds bounds,
199 var rect =
new Rect();
201 var points =
new Vector3[8];
202 var screen_pos =
new Vector3[8];
205 points[0] =
new Vector3(b.min.x, b.min.y, b.min.z);
206 points[1] =
new Vector3(b.max.x, b.min.y, b.min.z);
207 points[2] =
new Vector3(b.max.x, b.max.y, b.min.z);
208 points[3] =
new Vector3(b.min.x, b.max.y, b.min.z);
209 points[4] =
new Vector3(b.min.x, b.min.y, b.max.z);
210 points[5] =
new Vector3(b.max.x, b.min.y, b.max.z);
211 points[6] =
new Vector3(b.max.x, b.max.y, b.max.z);
212 points[7] =
new Vector3(b.min.x, b.max.y, b.max.z);
214 var screen_bounds =
new Bounds();
215 for (var i = 0; i < 8; i++) {
216 screen_pos[i] = cam.WorldToScreenPoint(points[i]);
219 screen_bounds =
new Bounds(screen_pos[0], Vector3.zero);
222 screen_bounds.Encapsulate(screen_pos[i]);
227 rect.xMin = screen_bounds.min.x;
228 rect.yMin = screen_bounds.min.y;
229 rect.xMax = screen_bounds.max.x;
230 rect.yMax = screen_bounds.max.y;
288 public static void CalculateLimits(Camera a_cam,
291 out
float a_max_height) {
293 var angle = a_cam.fieldOfView * Mathf.Deg2Rad * 0.5f;
296 var tan = Vector2.one * Mathf.Tan(angle);
297 tan.x *= a_cam.aspect;
301 var dim = a_area.extents;
302 var center = a_area.center -
new Vector3(0, a_area.extents.y, 0);
305 var max_dist =
new Vector2(dim.x / tan.x, dim.z / tan.y);
308 var dist = a_cam.transform.position.y - center.y;
311 dim.x *= 1f - dist / max_dist.x;
312 dim.z *= 1f - dist / max_dist.y;
315 a_max_height = center.y + Mathf.Min(max_dist.x, max_dist.y);
318 a_limits =
new Rect(center.x - dim.x, center.z - dim.z, dim.x * 2, dim.z * 2);
321 public static bool CullToFrustum(Mesh mesh, Camera cam, Transform trans,
bool update_position) {
322 var fov = cam.fieldOfView;
323 cam.fieldOfView = fov * 0.97f;
324 var planes = GeometryUtility.CalculateFrustumPlanes(cam);
325 cam.fieldOfView = fov;
326 if (GeometryUtility.TestPlanesAABB(planes, mesh.bounds)) {
330 Debug.Log(
"Culling :" + trans.name);
342 public static Rect GetMinMaxRect(
this Bounds bounds, Transform t, Camera cam,
float margin = 0) {
343 var cen = bounds.center;
344 var ext = bounds.extents;
346 var x_min = cen.x - ext.x;
347 var y_min = cen.y - ext.y;
348 var z_min = cen.z - ext.z;
349 var x_max = cen.x + ext.x;
350 var y_max = cen.y + ext.y;
351 var z_max = cen.z + ext.z;
353 Vector2 min = cam.WorldToScreenPoint(
new Vector3(x_min, y_min, z_min));
357 point.GetMinMax(ref min, ref max);
359 point = cam.WorldToScreenPoint(
new Vector3(x_max, y_max, z_max));
360 point.GetMinMax(ref min, ref max);
362 point = cam.WorldToScreenPoint(
new Vector3(x_max, y_min, z_min));
363 point.GetMinMax(ref min, ref max);
365 point = cam.WorldToScreenPoint(
new Vector3(x_min, y_max, z_min));
366 point.GetMinMax(ref min, ref max);
368 point = cam.WorldToScreenPoint(
new Vector3(x_min, y_min, z_max));
369 point.GetMinMax(ref min, ref max);
371 point = cam.WorldToScreenPoint(
new Vector3(x_max, y_min, z_max));
372 point.GetMinMax(ref min, ref max);
374 point = cam.WorldToScreenPoint(
new Vector3(x_max, y_max, z_min));
375 point.GetMinMax(ref min, ref max);
377 point = cam.WorldToScreenPoint(
new Vector3(x_min, y_max, z_max));
378 point.GetMinMax(ref min, ref max);
380 var r = Rect.MinMaxRect(min.x, min.y, max.x, max.y);
394 public static float MaxDim(
this Vector3 vec) {
return Mathf.Max(vec.x, vec.y, vec.z); }
401 public static float MinDim(
this Vector3 vec) {
return Mathf.Min(vec.x, vec.y, vec.z); }
410 public static Vector3[] GetMinMaxPoints(
this MeshCollider mesh, Transform t, Camera cam) {
411 return mesh.sharedMesh.GetCameraMinMaxPoints(t, cam);