Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by HeywoodFloyd · Sep 08, 2010 at 02:53 PM · meshmultipletreeboundingbox

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Wiki

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.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges