- Home /
Getting actual AABB of an object
I'm trying to get the actual AABB of an object in the editor.
The mesh.bounds are an AABB for the object in it's default transform. (Magenta)
If you transform the mesh.bounds using the objects transform matrix, (red) you get the results of the object.renderbounds (yellow)
What I want is an AABB that extents to the min and max of the actual mesh (green)
Right now I loop through every vertex in the mesh. I transform it by the objects transform matrix. Then check for minimum and maximum values. Is there a better way to do this?
I only need it in the editor, but I'm assuming it's not very efficient looping through mesh verts like that.
Pseudocode:
for each vertex in mesh
point = vertex.position * object.worldMatrix
if (point > max)
max = point
if (point < min)
min = point
If you compute vertex positions for each corner of the red bounding box, you could just enumerate those to deter$$anonymous$$e the render bounds? This way there are only 8 vertices to consider rather than each mesh vertex.
I ran across this answer by @bunny83 a couple of weeks ago that suggested that to produce the bounds you specify, the way is to loop through the vertices:
http://answers.unity3d.com/questions/297453/finding-locations-of-vertices-on-a-mesh.html.
Given that procedural meshes that modify the mesh every frame run in real time, I doubt you will have performance issues with checking the vertices.
BTW: Great picture.
Answer by Zebibto · Aug 16, 2013 at 04:17 AM
@robertbu Thanks for the link. That's pretty much what I'm doing at the moment. Was just wondering if there is something built-in that might be faster/more optimized. I'll just keep using what I have then.
@numberkruncher Yes, but you don't have to compute a bounding box from the transformed mesh bounds. Each object already has renderbounds built-in (object.renderer.bounds) that you can use. I used that for illustration purposes since I didn't know at first why the render bounds were bigger than the actual object.
Thanks for the replies. I'll consider my question answered then.
Could you please tell me how did you take the transformed mesh bounds? I am interested in finding the same but I struggle...Thanks!
Your question isn't clear. Renderer.bounds gives you the transformed mesh bounds in worldspace. But as you can see in the image above, the transformed mesh bounds might not be the optimal axis aligned bounding box as it's usually larger. If you want an exact axis aligned bounding box there's no way around transfor$$anonymous$$g every vertex yourself and calculating the bounding box the way as i showed in the answer robertbu linked above.
Btw: You just woke a three years old question. If you have a similar question that isn't answered by the information provided here, ask your own seperate question ins$$anonymous$$d of commenting on old questions. Feel free to post a link to this question in your question as reference, but make sure you describe your problem more detailed.
Your answer
Follow this Question
Related Questions
Bounds Finding Box 1 Answer
Generate Bounding Box, In Game 1 Answer
Getting Bounding box from a collider with children 1 Answer
Mesh.RecalculateBounds 1 Answer