Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by pyjamaslug · Oct 08, 2017 at 04:39 AM · scripting problemworldtoscreenpointcamera.main

Very strange results from Camera.WorldToScreenPoint()

I am trying to do a very simple thing: draw a bounding box around various objects. To do it, I get the mesh and transform it into screen coords using Camera.WorldToScreenPoint(); and then I find the x and y extents in screen coords but store the vertices that correspond to them in world coords.

I then take these 4 points, cast rays at them to ensure the object is visible and if it is, transform them into screen space. At this point, things start to go wrong. Depending on location relative to the camera, the transform produces the wrong result.

I have attached a couple of screenshots to illustrate, in the scene view, you can see that the boxes around the objects in front of the camera are drawn correctly but the object just outside the view frustum is not; you can see that its box is in the wrong place. Each object in the scene is repeated before and behind the camera: the effect appears as I move the camera backwards to bring the objects close to the frustum.

alt text

The game view shows the result after the points have been transformed back into world space and drawn as a 3D plane. The box at top right does not enclose anything (the object is offscreen)

alt text

Objects that are located near each other in the world are being flung apart, by thousands, sometimes tens of thousands of pixels: I'm beating my brains out over this and making no headway: any help is appreciated.

scene-view.png (93.2 kB)
game-view.png (126.5 kB)
Comment
Add comment · Show 1
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 pyjamaslug · Oct 10, 2017 at 09:37 PM 0
Share

Unfortunately, that workaround is not complete. It works fine for objects that stay still but for animated skinned meshes, it continues to fail. Culling to the view frustum does not work when the object is in that problem area. I also notice that moving the camera does not seem to update the frustum planes (even though they are recalculated every update) but only for skinned meshes. Ordinary meshes work fine.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by pyjamaslug · Oct 08, 2017 at 08:30 PM

I'm now convinced this is a unity bug; I can't see any other reason for the result to change like this.

I'm going to try for a workaround by culling all objects outside the camera frustum (in world space) using GeometryUtility.CalculateFrustumPlanes() and then testing each object using GeometryUtilityTestPlanesAABB() on the bounds of each object. The example for this shows the test using an object with a collider: does anyone know if this means a collider is mandatory?

Comment
Add comment · Show 2 · 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 pyjamaslug · Oct 09, 2017 at 02:11 AM 0
Share

That solution works. I had to temporarily narrow the camera fov in order to create a little guard band around the objects To answer my own question about the .TestPlanesAABB() method, it does not need a collider, I just passed it the bounds from the mesh and it all works fine. Here's the code: bool CullToFrustum( $$anonymous$$esh mesh, Camera cam, Transform trans, bool upDatePosition){ float fov = cam.fieldOfView; cam.fieldOfView = fov * 0.97f; Plane[] planes = GeometryUtility.CalculateFrustumPlanes (cam); cam.fieldOfView = fov; if (GeometryUtility.TestPlanesAABB (planes, mesh.bounds)) { return true; } else { Debug.Log ("Culling :" + trans.name); return false; } }

avatar image jpdevicente555 · Jul 09, 2019 at 04:45 AM 0
Share

Did you manage to solve this?

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

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Job System usage on Moving Different Transform in Different Speeds 0 Answers

Scene/Level changing problems 1 Answer

Broken Jump Physics 0 Answers

2D Projectile Script Not Working 1 Answer

Few opened editor windows 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