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 rageingnonsense · Feb 17, 2015 at 10:00 PM · camerascreenconversionworldtoscreenpoint

WorldToScreenPoint returning VERY high values

So I am trying to implement quad-tree terrain. I want the terrain chunk to subdivide into 4 child chunks on the condition that the chunk is taking 25% or more of the screen space.

Here is the code I use to determine that (found elsewhere on here):

 private void CheckForSubdivision() {
         Vector3[] corners = boundsCorners;
 
         float minX = Mathf.Infinity;
         float minY = Mathf.Infinity;
         float maxX = -Mathf.Infinity;
         float maxY = -Mathf.Infinity;
 
         for (var i = 0; i < corners.Length; i++) {
             var corner = transform.TransformPoint(corners[i]);
             corner = Camera.main.WorldToScreenPoint(corner);
             if (corner.x > maxX) maxX = corner.x;
             if (corner.x < minX) minX = corner.x;
             if (corner.y > maxY) maxY = corner.y;
             if (corner.y < minY) minY = corner.y;
             minX = Mathf.Clamp(minX, 0, Screen.width);
             maxX = Mathf.Clamp(maxX, 0, Screen.width);
             minY = Mathf.Clamp(minY, 0, Screen.height);
             maxY = Mathf.Clamp(maxY, 0, Screen.height);
         }
 
         float width = maxX - minX;
         float height = maxY - minY;
         float area = width * height;
         float percentage = area / (Screen.width * Screen.height);
 
         if (percentage >= Terrain.MAX_CHUNK_SCREEN_PERCENTAGE) {            
             Debug.Log("Chunk " + transform.name + " uses " + (percentage * 100) + " screen space; subdividing");
             Subdivide();   
         }
     }

boundsCorners is an array of 4 vertices that represent the 4 corners of the plane that make up the current chunk.

This works perfectly at first. As I move my camera closer to the terrain, the chunks are subdivded further and further. However...

After about the 5th subdivision, the screen space being used always registers as 100%, and unity hangs. Debugging showed me that when this happens, the corner variable has values like this after being processed by WorldToScreenPoint:

BEFORE:

  • x: 2.605358f

  • y: -1.97875226f

  • z: -4.22133827f

AFTER:

  • x: 126353.117f

  • y: 54890.8555f

  • z: 0.00229765475f

These are clearly WAY off the boundaries of my screen! What could possibly cause this? As far as I understand; the values should be within the range of my viewport resolution no?

It is worth noting that this terrain is spherical, and as such we will almost never be looking at it from the global x/y/z orientation (aka: camera will almost never point at -Vector3.up for instance).

I'm just trying to understand why values like this are being returned when I get very close to the mesh, and also interested in hearing about alternative approaches to get the same result.

Comment
Add comment · Show 6
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 Owen-Reynolds · Feb 18, 2015 at 01:42 AM 0
Share

I think the numbers can legitimately get that big.

If the position is off the screen, the resulting numbers will (and should) be off the screen. In theory, if you're in really tight on a plane, the corners could give huge numbers (meaning they are off-screen by many, many screens.)

avatar image rageingnonsense · Feb 18, 2015 at 02:32 AM 0
Share

I thought of that after posting this, and maybe it is a sign of a deeper issue. The thing that throws me off is that my log messages consistently say 25%, then jump to 100% after the 5th subdivision.

avatar image Glurth · Feb 18, 2015 at 02:50 AM 0
Share

Just to make sure I understand, what SHOULD be happening; the screen space percentage used by each subdivision should grow LESS with each subdivision, right?

Assu$$anonymous$$g Owen is correct (usually as safe bet): this means you may have multiple subdivision happening BEFORE you get to less than 100% screen size (if the initial corners are way off screen.) But, the part where you clamp the $$anonymous$$ and max values to the screen size, means that even if greater than 100% screen size is actually used, it will still yield only 100% value.

$$anonymous$$gest a debug.log() with an UNCLA$$anonymous$$PED final percentage number to help figure things out. Since you compare to 25%, shouldn't even need to change any logic.

avatar image rageingnonsense · Feb 18, 2015 at 03:52 PM 0
Share

Here is what is supposed to happen:

  1. presented with terrain, camera looking straight at it from a distance.

  2. zoom in (by moving camera transform)

  3. as i get closer, the chunks i am near take up more and more space on screen.

  4. once they take up 25% of screesn space, subdivide, and disable self; showing children.

  5. should now be 4 - 16 children (4 if hovering over 1 chunk, 16 if i was zoo$$anonymous$$g into a corner). chunks are smaller, so take up less screen space now.

  6. goto step 2

Perhaps I should try to limit this check to chunks that are actually within the view frustrum of the camera as well.

avatar image Glurth · Feb 18, 2015 at 04:10 PM 0
Share

Ok, that's what I suspected. So, I think this is an important clue:

After about the 5th subdivision, the screen space being used always registers as 100%

Why is the percentage going UP, rather than down? The code looks correct to me, if we ASSU$$anonymous$$E, that each Subdivide() call yields smaller ....

AH! because you are clamping those values, even if ALL the corners for a particular subdivision are far off the right side of the screen, you will still have a $$anonymous$$X of 0 (left side of the screen).

Just remark out those clamps, I think that will help.

edit: had a brain fart- that just ain't right.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

WorldToScreenPoint, isn't getting it right? 1 Answer

How to make WorldToScreenPoint work with an orthographic camera? 1 Answer

How to make WorldToScreenPoint work with an orthographic camera? 0 Answers

Camera.ScreenToWorldPoint Not Giving Right Point 3 Answers

Lock the screen 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