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 /
avatar image
0
Question by Mr_FJ · Aug 13, 2014 at 11:16 AM · c#cameraworldtoscreenpoint

WorldToScreenPoint, wrapping around when outside screen.

So I'm drawing a 2d box on screen, around 8 points in 3d space, but manually finding the min, and max value, as bounds would do it.

 Vector3[] points = new Vector3[8];
         for (int i = 0; i < 8; i++)
         {
             points[i] = pointsIn[i].position;
         } 
 float[] points2dX = new float[8]
         {
             Camera.main.WorldToScreenPoint(points[0]).x,
             Camera.main.WorldToScreenPoint(points[1]).x,
             Camera.main.WorldToScreenPoint(points[2]).x,
             Camera.main.WorldToScreenPoint(points[3]).x,
             Camera.main.WorldToScreenPoint(points[4]).x,
             Camera.main.WorldToScreenPoint(points[5]).x,
             Camera.main.WorldToScreenPoint(points[6]).x,
             Camera.main.WorldToScreenPoint(points[7]).x
         };
         float[] points2dY = new float[8]
         {
             Camera.main.WorldToScreenPoint(points[0]).y,
             Camera.main.WorldToScreenPoint(points[1]).y,
             Camera.main.WorldToScreenPoint(points[2]).y,
             Camera.main.WorldToScreenPoint(points[3]).y,
             Camera.main.WorldToScreenPoint(points[4]).y,
             Camera.main.WorldToScreenPoint(points[5]).y,
             Camera.main.WorldToScreenPoint(points[6]).y,
             Camera.main.WorldToScreenPoint(points[7]).y
         };

But if one, or more, of the points go outside screen space, I still want the box to be drawn correctly. However, because of the fish eye effect (I'm guessing), when the point goes too far off screen, the values that WorldToScreenPoint returns, approach -/+ infinity, and eventually wrap around, producing wrong reuslts. So far I've been able to overcome the wrap around problem, by checking the WorldToScreenPoint coords, to the local 3d position of the object.

 for (int i = 0; i < 8; i++)
         {
             if (points2dX[i] > Screen.width && Camera.main.transform.InverseTransformPoint(points[i]).x < Camera.main.transform.position.x)
             {
                 points2dX[i] = 0;
             }
             if (points2dX[i] < 0 && Camera.main.transform.InverseTransformPoint(points[i]).x > Camera.main.transform.position.x)
             {
                 points2dX[i] = Screen.width;
             }
             if (points2dY[i] > Screen.height && Camera.main.transform.InverseTransformPoint(points[i]).y < Camera.main.transform.position.y)
             {
                 points2dY[i] = 0;
             }
             if (points2dY[i] < 0 && Camera.main.transform.InverseTransformDirection(points[i]).y > Camera.main.transform.position.y)
             {
                 points2dY[i] = Screen.height;
             }

It doesn't always work, though. Sometimes it gets it wrong, and the points are indeed offset, when out of the screen. So my question is, is there a way to completely overcome the fish eye effect (Without using orthographic camera), or is there a better way, to get the correct screen position, of the points?

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 robertbu · Aug 13, 2014 at 04:24 PM

Visualize an infinite plane running in front of you. Image you are casting a ray against that plane as you rotate right. At higher angles from forward, small increases in angle make huge changes the x coordinate. As you approach 90 degrees from forward, you approach infinity. And for Unity we don't need infinity to have a problem, just something that overflows a float when doing the WorldToScreen() calculation. I believe this is the issue you are facing.

One thing you can try to mitigate the problem (assuming your game mechanic will allow it) is to narrow the field of view of your camera and move the camera back from your points to compensate (keep their relative position and distance as seen by the camera). The resulting geometry means that a points will need to be further out of view before you suffer a problem. The greater you narrow the field of view, the further the point will need to be out of view before you suffer a problem.

A real solution to the problem would be to clip the points passed to (or near) the bounding rect of the screen. The difficulty of this task depends on your geometry. For an axes aligned camera looking at points on a plane parallel to the camera plane, it is easy. For an arbitrary camera rotation and/or points in arbitrary 3D space, it is much more difficult task. One reference:

http://docs.unity3d.com/Manual/FrustumSizeAtDistance.html

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

2 People are following this question.

avatar image avatar image

Related Questions

C# 2d Instantiate GUIText and lock it to position on map, not to follow camera. 1 Answer

Distribute terrain in zones 3 Answers

Incorrect X position from worldToScreen method 0 Answers

Multiple Cars not working 1 Answer

getting the position of player for the minimap to follow 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