• 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 /
avatar image
3
Question by grojguy · Feb 08, 2010 at 12:28 PM · bounds

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?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KingSharaman · Mar 10, 2012 at 12:54 AM

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.

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
avatar image
1

Answer by duck · Feb 08, 2010 at 02:34 PM

Have you tried calling yourMesh.RecalculateBounds?

Comment
Add comment · Show 1 · 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
avatar image grojguy · Feb 10, 2010 at 06:58 AM 0
Share

Thanks for the response Duck. Yes, intuition suggests that might do the trick, eh? But no, apparently that still only recalculates bounds in the mesh's local-space, not world-space.

avatar image
1

Answer by GXMark · Aug 01, 2013 at 02:01 PM

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;
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers

3d Text how do you autofit to a fixed space 0 Answers

How to get correct position after RecalculateBounds is called? 1 Answer

Issue with bounds.SetMinMax assigning incorrectly. 1 Answer

How to stop view from scrolling when edge of view collides with a game object? 0 Answers


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