• 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
1
Question by Lethil · Mar 28, 2012 at 08:48 PM · guiboundsworldtoscreenpointrectangle

How can I consistently get the rectangle bounds around a mesh?

I've been trying to get the bounds around a mesh so that I may draw a rectangle around it in the UI, however I'm having the worst time finding a way to do so.

I was using bounds.min and bounds.max and translating those points using WorldToScreenPoint(), but unfortunately, it merely gives me the min point and max point of the mesh in one plane. When you rotate, it looks like this:

I'd like to get it to look like this:

Could anyone point me in the right direction on this one?

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

2 Replies

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

Answer by Lethil · Mar 29, 2012 at 01:52 AM

I figured it out. Renderer.bounds was half the story.

The other half was not using min and max. Instead, I had to generate my own 2 coordinates based on extents. Here's the code that explains it all:

 _Center = transform.InverseTransformPoint(SelectedInfo.renderer.bounds.center);
 if (_Center.z > 1)
 {
     _UpperLeft = camera.WorldToScreenPoint(transform.TransformPoint(_Center + new Vector3(-SelectedInfo.renderer.bounds.extents.x * 1.4F, SelectedInfo.renderer.bounds.extents.y * 1.4F, 0)));
     _LowerRight = camera.WorldToScreenPoint(transform.TransformPoint(_Center + new Vector3(SelectedInfo.renderer.bounds.extents.x * 1.4F, -SelectedInfo.renderer.bounds.extents.y * 1.4F, 0)));
     GUI.color = new Color(1.0f , 0, 0);//Set color to red
     GUI.DrawTexture(new Rect(_UpperLeft.x, Screen.height-_UpperLeft.y, _LowerRight.x-_UpperLeft.x, _UpperLeft.y-_LowerRight.y), _ReticleTexture);
 }

I took the center of the bounds and then created my own upper left and lower right coordinates using bounds.extents lengths, relative to the camera. This removes the world-based x-y plane issue. They'll always be x-y plane relative to the camera.

After that, I retranslate them into world coordinates and then get the x,y screen coordinates and draw away! You may notice that I added a little buffer to the extents. This is to make sure there's a little space within the rectangle. Works like a charm!

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 rutter · Mar 28, 2012 at 09:43 PM

Mesh and Renderer both expose a bounds property, but there is a key difference:

  • Mesh.bounds is pre-transform (local space)

  • Renderer.bounds is post-transform (world space)

The renderer bounds will probably give you better results.

Past that, you may need to execute some min/max operations when translating those points into screen coordinates.

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 Lethil · Mar 28, 2012 at 09:49 PM 0
Share

Those results were from Renderer.bounds. The issue seems to come from the fact the reported $$anonymous$$ and max do not seem to be camera-friendly. If you face the mesh "head on" relative to world space, it looks correct. Once the camera pivots around the mesh, it reveals that it's locked into the worldspace plane. Could you give me some details on the "$$anonymous$$/max" operations you were suggesting?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I add "..." to GUI.Label if it goes past its bounds? 1 Answer

WorldToScreenPoint returns the same value when facing the opposite direction. 0 Answers

How to set my text to real object 2 Answers

C# Using Variables for GUI Rectangles 0 Answers

Using Bounds.Size with GUI... 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