Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
avatar image
0
Question by FirefoxMetzger · Aug 12, 2018 at 07:54 PM · cameracoordinate-systemperspective cameratransformation

manual MVP projection gives different results then WorldToViewportPoint

I'm trying to manually perform an MVP transformation for a single point. I think (hope) I have it sort of figured out, except for one quirk: my Z coordinate in Viewport space is off.

Here is the code:

 var pos = Vector3.zero;
 var pos_homogeneous_coordinates = new Vector4(pos.x, pos.y, pos.z, 1);
 var pos_local = cam.transform.localToWorldMatrix.inverse * pos_homogeneous_coordinates;
 pos_local.z *= -1; // unity uses a left-handed coordinate system for the projection
 var pos_in_clip_space = cam.projectionMatrix * pos_local;
 var pos_in_NDC = (pos_in_clip_space / pos_in_clip_space.w);
 var pos_in_viewport_space = (Vector3) ((pos_in_NDC + new Vector4(1,1,1,0)) / 2f);
 
 print(pos_in_viewport_space);
 print(cam.WorldToViewportPoint(pos));
           

where cam is the camera. The result of this in my case is:

Result of a position within the cameras viewing area

And I would expect them to be identical. Also, my code seems to produce strange results in edge cases or when the position is outside the clipping area. E.g. using pos = cam.transform.position:

Result outside the viewing area

My question simply is where I am going wrong, as I don't see it. Any pointers or help is highly appreciated =)

failure-case-2.png (6.1 kB)
failure-case-1.png (6.1 kB)
Comment
Add comment · Show 2
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 Nebukam · Aug 12, 2018 at 09:21 PM 0
Share

out of curiosity, do you still have the offset if you use the non-jittered projection matrix ?

avatar image FirefoxMetzger Nebukam · Aug 13, 2018 at 05:34 AM 0
Share

Yes, the same situation occurs. Actually I figured out why my the z-values differ: WorldToViewportPoint(...).z is NOT the normalized distance in viewport space. Ins$$anonymous$$d it is the distance between the viewport plane to the point in the scale of the world frame. I can see why the devs made that decision; however, for me that was a bit unexpected.

The only thing I haven't figured out yet is why my normalization gets funny when the distance approaches cam.nearClipPlane. After transfor$$anonymous$$g to NDC, I would expect a constant z=1; which is the case for most values, just not around cam.nearClipPlane. Any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FirefoxMetzger · Aug 13, 2018 at 01:30 PM

The strange behavior is actually not very strange at all. After consulting the documentation of WorldToViewportPoint (again) I found


Viewport space is normalized and relative to the camera. The bottom-left of the camera is (0,0); the top-right is (1,1). The z position is in world units from the camera.


After further investigation, I also found that this distance is measured as the normal distance between the point and the projection plane.

In other words, both ways are correct. Unity gives an absolute difference, I return the normalized distance (which may be a lot less meaningful since it usually is 1.


Regarding the strange behavior around cam.transform.position I picked the most unfortunate point to look at. When z --> 0 (distance between camera and point), w --> 0; thus, at the camera's origin one would divide by 0 which is not a good idea.

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

88 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

Related Questions

How does ScreenToWorldPoint work with split screen with multiple cameras? 1 Answer

How do I implement orthographic elements into a perspective camera? 0 Answers

Object moving faster when camera moves with it than without camera movement 0 Answers

How do projection matrices work in Unity? 1 Answer

perspective distortion for selected objects 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