Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Fredex · Aug 29, 2016 at 09:20 AM · meshboundsmeshrendererpercentage

Trouble utilizing MeshRenderer bounds in a way that is independent of object rotation / orientation.

I am attempting to use MeshRender.Bounds to identify a number of points on my Plane object. My end goal is to RayCast from a 'grid' of points on that plane outwards to identify whether that point is being covered by a specific object. Using each of these points, I want to calculate a rough percentage of how much of the plane has been covered by the specific objects. I had this system working smoothly for a plane that faced the camera, but soon realized that the same setup on an identical plane rotated in another direction showed many points on the grid being off.

My grid scheme made a couple of assumptions, the two main ones being that MeshRenderer.bounds.min would always yield the bottom left corner point of the mesh, and that MeshRenderer.bounds.max would always yield the top-right point of the mesh. Each intermittent grid point was based on this assumption ( which worked correctly for the camera-facing plane).

I decided to do a little test using bounds.min / bounds.max on identical objects(planes) facing different directions - with the following results: alt text Note here that bounds.min is being drawn in white, center = red, and max = blue. The bounds.center point(red) on each object seems to be as expected, though the min and max results seem to vary pretty greatly.

Here is a snippet of how I am showing these rays:

     Bounds bounds = GetComponent<MeshRenderer>().bounds;

     Vector3 botLeft = bounds.min;//bottom left
     Debug.DrawLine (botLeft, botLeft + transform.up * 15, Color.white);

     Vector3 center = bounds.center;//center
     Debug.DrawLine (center, center + transform.up * 15, Color.red);

     Vector3 topRight = bounds.max;//top Right
     Debug.DrawLine (topRight, topRight + transform.up * 15, Color.blue);

I have also tried some alternative methods of obtaining the bottom left/ top right points of the mesh, such as botLeft = bounds.center - bounds.extent; // topRight = bounds.center + bounds.extent; or using bounds.size/2f in place of extent. Each with very similar results.

I have also tried converting these points from local to world space using transform.TransformPoint - but these results are much further off.

Anyway my question is basically, is there a way for me to manipulate MeshRender.bounds.min / max to yield the bottom left/ top right corners of the mesh regardless of it's object's rotation. Any suggestions would be greatly appreciated.

p.s I do have an alternate solution to this problem which involves using many, manually placed child objects which will determine the position to raycast from, or using a number of child box colliders to detect collisions with the specified objects such as : alt text but this seems a little excessive and overall less expandable than what I want to achieve.

boundsresult1.png (207.4 kB)
collideralternative.png (199.5 kB)
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
0

Answer by samizzo · Jun 20, 2018 at 03:39 AM

Note that the bounds are axis-aligned bounding boxes (AABBs), which means they are aligned to the world space coordinate system. That is, they aren't aligned to your object's orientation; they aren't oriented bounding boxes (OBBs).

This means that as you rotate your object, the AABB will grow and shrink in size. Are you taking this into account? It looks like you aren't, because you seem to be assuming that they are OBBs judging from this line:

 Debug.DrawLine (botLeft, botLeft + transform.up * 15, Color.white);


To draw the AABB for the object, you can do this:

 private void OnDrawGizmos()
 {
     Gizmos.DrawWireCube(m_bounds.center, m_bounds.size);
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Bounds of dynamic mesh not updating 1 Answer

Why zero height mesh gets black independent of shader 1 Answer

How to change one child object to another with the same armature? 0 Answers

Mesh.RecalculateBounds 1 Answer

Rendering invisible objects 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