namespace UnityEngine.Polybrush { internal static class BoundsUtility { internal struct SphereBounds { internal Vector3 position; internal float radius; /// /// Create a sphere bound based on a position and a radius. /// /// Position /// Radius internal SphereBounds(Vector3 p, float r) { position = p; radius = r; } /// /// Return true if the other sphere is in the radius of this sphere bound. /// /// /// internal bool Intersects(SphereBounds other) { return Vector3.Distance(position, other.position) < (radius + other.radius); } } /// /// Create a spherical bounds inside of a Renderer bounds. /// It'll take every meshes found in the hierarchy into account. /// /// GameObject having Renderer component(s). /// Sphere bounds instance in which sphere information will be store. /// True if bounds data has been set. internal static bool GetSphereBounds(GameObject go, out SphereBounds bounds) { Bounds entireObjectBounds = GetHierarchyBounds(go); bounds = default(SphereBounds); if (entireObjectBounds.size == Vector3.zero) return false; bounds.position = entireObjectBounds.center; bounds.radius = Mathf.Max(entireObjectBounds.extents.x, entireObjectBounds.extents.z); return true; } /// /// Create bounds based on meshes found in the given GameObject hierarchy. /// /// Root object. /// New bounds around the given GameObject. /// If GameObject has no Renderer, bounds will have a size of zero. internal static Bounds GetHierarchyBounds(GameObject parent) { Renderer[] renderers = parent.GetComponentsInChildren(); Bounds bounds = default(Bounds); if (renderers.Length == 0) return bounds; for (int i = 0; i < renderers.Length; ++i) { Bounds it = renderers[i].bounds; if (i == 0) { // World position bounds.center = it.center; } bounds.Encapsulate(it.max); bounds.Encapsulate(it.min); } return bounds; } } }