# Re-calculate AABB after rotating a mesh?

This is a little tricky to properly explain, so bear with me. Is there no built-in function to recalculate a world-space AABB bounding-box on a mesh?

For example, say I import a cube which has rotation of 45 deg on one or more axis. Of course Unity gives bounds larger than the actual cube volume, due to the rotation.

OK, so now let's say I 'correct' the cube's rotation in Inspector so it is now AA (aligned to world axis). Does renderer.bounds give me the new (smaller) bounds that are equal to the cube volume?

Apparently NO, it gives me LARGER bounds that encapsulate the original rotated bounding box. It seems renderer.bounds simply translates the (now rotated 45 degrees) mesh.bounds bounding box into world-space, INCLUDING the local rotated bounding box. Isn't that somewhat less-than-useful in terms of accuracy? It works the same for the Box Collider.

What I'd like to do is find the "OBB" (Oriented Bounding Box) / "MVBB" (Minimum Volume Bounding Box). I thought I would iteratively rotate the object and then re-calculate the bounds, until I found the smallest bounding volume.

But the best I can think of now is to manually re-calculate the world-space bounding box by iterating through each mesh vertex and TransformPoint(vertex) to world-space while cumulatively Encapsulate() into a new Bounds.

Is there no built-in function to do this....? Obviously Unity does it on original import of the mesh, right?

Have you tried calling yourMesh.RecalculateBounds?

Use RecalculateBounds() after you assign the mesh to a MeshFilter component. So for example in C#:

GetComponent().mesh = aMeshObject;
aMeshObject.RecalculateBounds();

This way it will recalculate renderer bounds.

When you rotate or scale an object the renderer bounds is not correct anymore

To recalculate this properly all you have to do is ensure that the objects mesh has its bounds recalculated but crucially done with the localRotation reset as follows :-

Example code

``````//Calculates the renderer bounds of the gameobject with its children
public static Bounds CalculateRendererBounds(GameObject obj)
{
Quaternion prevRot = obj.transform.localRotation;

obj.transform.localRotation = Quaternion.identity;

Bounds bounds = new Bounds(obj.transform.position, Vector3.one);

Renderer[] children = obj.GetComponentsInChildren<Renderer>();
foreach(Renderer child in children)
if (child != null)
bounds.Encapsulate(child.bounds);

obj.transform.localRotation = prevRot;

return bounds;
}
``````