From 90fbee6294a9dfa36746b0a6e421bc8f80a2eba6 Mon Sep 17 00:00:00 2001 From: SimonDarksideJ Date: Wed, 18 Oct 2023 22:39:29 +0100 Subject: [PATCH] Added a new Calculate Bounds extension and updated documentation for all --- Runtime/Extensions/BoundsExtensions.cs | 147 +++++++++++++++---------- 1 file changed, 88 insertions(+), 59 deletions(-) diff --git a/Runtime/Extensions/BoundsExtensions.cs b/Runtime/Extensions/BoundsExtensions.cs index 9f8f6fe..bfa0ddc 100644 --- a/Runtime/Extensions/BoundsExtensions.cs +++ b/Runtime/Extensions/BoundsExtensions.cs @@ -67,10 +67,11 @@ public static class BoundsExtensions #region Public Static Functions /// - /// Returns an instance of the 'Bounds' class which is invalid. An invalid 'Bounds' instance - /// is one which has its size vector set to 'float.MaxValue' for all 3 components. The center - /// of an invalid bounds instance is the zero vector. + /// Returns an instance of the class which is invalid. + /// An invalid instance is one which has its size vector set to 'float.MaxValue' for all 3 components. + /// The center of an invalid bounds instance is the zero vector. /// + /// Returns an instance of the class which is invalid. public static Bounds GetInvalidBoundsInstance() { return new Bounds(Vector3.zero, GetInvalidBoundsSize()); @@ -80,6 +81,7 @@ public static Bounds GetInvalidBoundsInstance() /// Checks if the specified bounds instance is valid. A valid 'Bounds' instance is /// one whose size vector does not have all 3 components set to 'float.MaxValue'. /// + /// The input bounding volume. public static bool IsValid(this Bounds bounds) { return bounds.size != GetInvalidBoundsSize(); @@ -88,9 +90,9 @@ public static bool IsValid(this Bounds bounds) /// /// Gets all the corner points of the bounds in world space. /// - /// - /// - /// + /// The input bounding volume. + /// A relative transform point. + /// A reference array of 8 points to populate. public static void GetCornerPositionsWorldSpace(this Bounds bounds, Transform transform, ref Vector3[] positions) { // Calculate the local points to transform. @@ -125,8 +127,8 @@ public static void GetCornerPositionsWorldSpace(this Bounds bounds, Transform tr /// /// Gets all the corner points of the bounds in local space. /// - /// - /// + /// The input bounding volume. + /// A reference array of 8 points to populate. public static void GetCornerPositionsLocalSpace(this Bounds bounds, ref Vector3[] positions) { // Allocate the array if needed. @@ -153,8 +155,8 @@ public static void GetCornerPositionsLocalSpace(this Bounds bounds, ref Vector3[ /// /// Gets all the corner points of the bounds. /// - /// - /// + /// The input bounding volume. + /// A reference array of 8 points to populate. /// /// is world space bounding volume. /// is local space bounding volume. @@ -188,6 +190,12 @@ public static void GetCornerPositions(this Bounds bounds, ref Vector3[] position positions[RTB] = new Vector3(rightEdge, topEdge, backEdge); } + /// + /// Gets all the face points of the bounds. + /// + /// The input bounding volume. + /// A relative transform point. + /// A reference array of 8 points to populate. public static void GetFacePositions(this Bounds bounds, Transform transform, ref Vector3[] positions) { const int numPoints = 6; @@ -211,9 +219,9 @@ public static void GetFacePositions(this Bounds bounds, Transform transform, ref /// /// Gets all the corner points and mid points from Bounds /// - /// - /// - /// + /// The input bounding volume. + /// A relative transform point. + /// A reference array of 8 points to populate. public static void GetCornerAndMidPointPositions(this Bounds bounds, Transform transform, ref Vector3[] positions) { // Calculate the local points to transform. @@ -262,10 +270,10 @@ public static void GetCornerAndMidPointPositions(this Bounds bounds, Transform t /// /// Gets all the corner points and mid points from Bounds, ignoring the z axis /// - /// - /// - /// - /// + /// The input bounding volume. + /// A relative transform point. + /// A reference array of 8 points to populate. + /// The to flatten the points against. public static void GetCornerAndMidPointPositions2D(this Bounds bounds, Transform transform, ref Vector3[] positions, CardinalAxis flattenAxis) { // Calculate the local points to transform. @@ -334,9 +342,9 @@ public static void GetCornerAndMidPointPositions2D(this Bounds bounds, Transform /// Method to get bounding box points using Collider method. /// /// gameObject that boundingBox bounds. - /// array reference that gets filled with points - /// layerMask to simplify search - /// The colliders to use for calculating the bounds of this gameObject + /// array reference that gets filled with points. + /// layerMask to simplify search. + /// The colliders to use for calculating the bounds of this gameObject. public static void GetColliderBoundsPoints(GameObject target, ref List boundsPoints, LayerMask ignoreLayers, Collider[] colliders = null) { if (colliders == null) @@ -400,10 +408,10 @@ public static void GetColliderBoundsPoints(GameObject target, ref List /// /// GetRenderBoundsPoints gets bounding box points using Render method. /// - /// gameObject that bounding box bounds - /// array reference that gets filled with points - /// layerMask to simplify search - /// The renderers to use for calculating the bounds of this gameObject + /// gameObject that bounding box bounds. + /// array reference that gets filled with points. + /// layerMask to simplify search. + /// The renderers to use for calculating the bounds of this gameObject. public static void GetRenderBoundsPoints(GameObject target, ref List boundsPoints, LayerMask ignoreLayers, Renderer[] renderers = null) { if (renderers == null) @@ -430,10 +438,10 @@ public static void GetRenderBoundsPoints(GameObject target, ref List bo /// /// GetMeshFilterBoundsPoints - gets bounding box points using MeshFilter method. /// - /// gameObject that bounding box bounds - /// array reference that gets filled with points - /// layerMask to simplify search - /// The mesh filters to use for calculating the bounds of this gameObject + /// gameObject that bounding box bounds. + /// array reference that gets filled with points. + /// layerMask to simplify search. + /// The mesh filters to use for calculating the bounds of this gameObject. public static void GetMeshFilterBoundsPoints(GameObject target, ref List boundsPoints, LayerMask ignoreLayers, MeshFilter[] meshFilters = null) { if (meshFilters == null) @@ -465,25 +473,15 @@ public static void GetMeshFilterBoundsPoints(GameObject target, ref List - /// Transforms 'bounds' using the specified transform matrix. + /// Transforms using the specified transform matrix. /// /// - /// Transforming a 'Bounds' instance means that the function will construct a new 'Bounds' - /// instance which has its center translated using the translation information stored in - /// the specified matrix and its size adjusted to account for rotation and scale. The size - /// of the new 'Bounds' instance will be calculated in such a way that it will contain the - /// old 'Bounds'. + /// Transforming a instance means that the function will construct a new instance which has its center translated using the translation information stored in the specified matrix and its size adjusted to account for rotation and scale. + /// The size of the new instance will be calculated in such a way that it will contain the old . /// - /// - /// The 'Bounds' instance which must be transformed. - /// - /// - /// The specified 'Bounds' instance will be transformed using this transform matrix. The function - /// assumes that the matrix doesn't contain any projection or skew transformation. - /// - /// - /// The transformed 'Bounds' instance. - /// + /// The input bounding volume. + /// The specified instance will be transformed using this transform matrix. The function assumes that the matrix does not contain any projection or skew transformation. + /// The transformed instance. public static Bounds Transform(this Bounds bounds, Matrix4x4 transformMatrix) { // We will need access to the right, up and look vector which are encoded inside the transform matrix @@ -519,11 +517,9 @@ public static Bounds Transform(this Bounds bounds, Matrix4x4 transformMatrix) /// /// Returns the screen space corner points of the specified 'Bounds' instance. /// - /// - /// - /// The camera used for rendering to the screen. This is needed to perform the - /// transformation to screen space. - /// + /// The input bounding volume. + /// The camera used for rendering to the screen. This is needed to perform the transformation to screen space. + /// An array of screen points for the camera corners. public static Vector2[] GetScreenSpaceCornerPoints(this Bounds bounds, Camera camera) { var aabbCenter = bounds.center; @@ -544,9 +540,13 @@ public static Vector2[] GetScreenSpaceCornerPoints(this Bounds bounds, Camera ca }; } + /// /// Returns the rectangle which encloses the specifies 'Bounds' instance in screen space. /// + /// The input bounding volume. + /// The camera volume to calculate bounds from. + /// Returns a for the encapsulated screen public static Rect GetScreenRectangle(this Bounds bounds, Camera camera) { // Retrieve the bounds' corner points in screen space @@ -568,8 +568,8 @@ public static Rect GetScreenRectangle(this Bounds bounds, Camera camera) /// /// Returns the volume of the bounds. /// - /// - /// + /// The input bounding volume. + /// A representation of the volume. public static float Volume(this Bounds bounds) { return bounds.size.x * bounds.size.y * bounds.size.z; @@ -578,9 +578,9 @@ public static float Volume(this Bounds bounds) /// /// Returns bounds that contain both this bounds and the bounds passed in. /// - /// - /// - /// + /// The original bounding area. + /// The additional bounded area to expand the original bounds with. + /// Returns the collective volume. public static Bounds ExpandToContain(this Bounds originalBounds, Bounds otherBounds) { var tmpBounds = originalBounds; @@ -591,8 +591,8 @@ public static Bounds ExpandToContain(this Bounds originalBounds, Bounds otherBou /// /// Checks to see if bounds contains the other bounds completely. /// - /// - /// + /// The input bounding volume. + /// The testing bounding volume for contact. /// public static bool ContainsBounds(this Bounds bounds, Bounds otherBounds) { @@ -602,9 +602,9 @@ public static bool ContainsBounds(this Bounds bounds, Bounds otherBounds) /// /// Checks to see whether point is closer to bounds or otherBounds /// - /// - /// - /// + /// The input bounding volume. + /// The point to test against. + /// The comparative bounds volume to compare against. /// public static bool CloserToPoint(this Bounds bounds, Vector3 point, Bounds otherBounds) { @@ -622,6 +622,35 @@ public static bool CloserToPoint(this Bounds bounds, Vector3 point, Bounds other return (distToClosestPoint1.magnitude <= distToClosestPoint2.magnitude); } + /// + /// Returns the bounds of the model and all its contained meshes. + /// + /// The input bounding volume. + /// The target transform to inspect and calculate the bounds from. + /// Should the mesh query also include inactive components/objects. + /// Returns a object with the updated bounds (used to apply to the Size and Center points of a bounding volume) + public static Bounds CalculateBoundsForModel(this Bounds bounds, Transform transform, bool includeInactive = false) + { + var renderers = transform.GetComponentsInChildren(includeInactive); + + if (renderers.Length > 0) + { + bounds = renderers[0].bounds; + + for (int i = 1; i < renderers.Length; i++) + { + bounds.Encapsulate(renderers[i].bounds); + } + } + + foreach (Transform child in transform) + { + bounds.CalculateBoundsForModel(child); + } + + return bounds; + } + #endregion #region Private Static Functions