- Home /
Object Oriented Bounding Boxes for Multiple Meshes
I'd like to get an object-oriented bounding box (OOBB), rather than an axis oriented bounding box (AABB) for a hierarchy of nodes rather than just a single node. Based on stuff I saw in UnityAnswers and on the forums, I came up with this C# function:
public static Bounds getMeshBounds(GameObject root) { //Get all the mesh filters in the tree. MeshFilter[] filters = root.GetComponentsInChildren<MeshFilter>();
//Construct an empty bounds object w/o an extant.
Bounds bounds = new Bounds(Vector3.zero, Vector3.zero);
bool firstTime = true;
//For each mesh filter...
foreach(MeshFilter mf in filters)
{
//Pull its bounds into the overall bounds. Bounds are given in
//the local space of the mesh, but we want them in world space,
//so, tranform the max and min points by the xform of the object
//containing the mesh.
Vector3 maxWorld = mf.transform.TransformPoint(mf.mesh.bounds.max);
Vector3 minWorld = mf.transform.TransformPoint(mf.mesh.bounds.min);
//If no bounds have been set yet...
if(firstTime)
{
firstTime = false;
//Set the bounding box to encompass the current mesh, bounds,
//but in world coordinates.
bounds = new Bounds((maxWorld + minWorld ) / 2, //center
maxWorld - minWorld ); //extent
}
else //We've started a bounding box. Make sure it ecapsulates
{ //the current mesh extrema.
bounds.Encapsulate(maxWorld);
bounds.Encapsulate(minWorld);
}
}
//Return the bounds just computed.
return bounds;
}
It works when the objects are moved or scaled, but not when they're rotated. When object are rotated, the bounding box not only doesn't rotate with the objects, but in fact no longer bounds the objects. I can't figure out why this is.
Please let me know if there's a better way to do this, and/or what my bone-headed error is in the above code that keeps the computed bounding boxes from rotating with the objects.
Answer by Jesse Anders · Sep 08, 2010 at 10:04 PM
That won't work, for at least a couple of reasons.
First of all, an AABB class ('Bounds' in this case) can't be used to represent a general oriented box. Since you're returning an AABB from your function, you're always going to get an AABB; an AABB can't 'become' an OBB just by virtue of how it's constructed. So, if the intent is to return an oriented box, that's the first thing you'll need to address. (I don't know that the Unity API includes an oriented box class; if it doesn't, you may have to create one yourself.)
Second, transforming the min and max vectors as in your example won't work. The min and max vectors of an AABB have a specific meaning; they 'encode' the shape of the AABB in a specific way, and rotating them will (in general) produce an AABB that is either incorrect or invalid.
If you want to return an AABB that fully contains the OBBs of all the child nodes, you'll need to rotate each child AABB to yield an OBB, build an AABB from that OBB, and then add the AABB to the root AABB. If you want to return an OBB that fully contains the OBBs of all the child nodes, that's a more complicated problem.
It might also be worth asking whether or not the greater accuracy offered by OBBs is needed. (I don't know what problem you're trying to solve here, but it may be that a simpler solution using spheres or AABBs would be sufficient, and maybe even preferable.)
Your answer
Follow this Question
Related Questions
Scripting large amounts of tree instances 0 Answers
Anti-aliasing issue after upgrade to Unity 5.0 0 Answers
Placed Trees in Terrain are white planes? 1 Answer
How do I get a SpeedTree into Unity? 3 Answers
Using my own trees 1 Answer