Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 joel_cambrian · Jan 28, 2018 at 04:28 PM · camera viewportworldtoscreenpointprojection-matrixviewporttoworldpoint

WorldToViewportPoint and ViewportToWorldPoint math

I'm having a hard time recreating the exact transformation using just the camera projection and view matrices. I've tried various approaches, using both the camera projection and the GL camera projection.

If everything works right, these two variables, _result and result, should match:

 static Vector3 worldToViewportPoint(Vector3 point3D) {
     Matrix4x4 P = GL.GetGPUProjectionMatrix(Camera.main.projectionMatrix, false);
     Matrix4x4 V = Camera.main.transform.worldToLocalMatrix;
     Matrix4x4 VP = P * V;
     var result = VP.MultiplyPoint(point3D);
     var _result = Camera.main.WorldToViewportPoint(point3D);
     
     return _result;
 }

Similarly, I need to calculate the ViewportToWorldPoint, which I imagine I would solve if the method above had the correct matrices. The reason we need this is for a computer vision library running beneath unity using linear algebra.

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
3
Best Answer

Answer by victorbisaev · Jan 28, 2018 at 08:51 PM

Try this:

 static Vector3 worldToViewportPoint(Vector3 point3D)
 {
     Matrix4x4 P = Camera.main.projectionMatrix;
     Matrix4x4 V = Camera.main.transform.worldToLocalMatrix;
     Matrix4x4 VP = P * V;
 
     Vector4 point4 = new Vector4(point3D.x, point3D.y, point3D.z, 1.0f);  // turn into (x,y,z,1)
     Vector4 result4 = VP * point4;  // multiply 4 components
 
     Vector3 result = result4;  // store 3 components of the resulting 4 components
 
     // normalize by "-w"
     result /= -result4.w;
 
     // clip space => view space
     result.x = result.x / 2 + 0.5f;
     result.y = result.y / 2 + 0.5f;
 
     // "The z position is in world units from the camera."
     result.z = -result4.w;
 
 
     var _result = Camera.main.WorldToViewportPoint(point3D);
     // result == _result
 
     return _result;
 }

The idea is to use 4-components matrix operations as V and P are of "Matrix4x4" type. Also please notice regular matrix multiplication "VP * point4" instead of "VP.MultiplyPoint(point3D)"

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
0

Answer by joel_cambrian · Jan 28, 2018 at 11:48 PM

@victorbisaev That worked perfectly, thank you. Now for the opposite, view to world point.

I reversed the clip space/view space and am still off. I assume its the denormalization part I'm missing.

 static Vector3 viewportToWorldPoint(Vector3 point2D) {
    
    Matrix4x4 P = Camera.main.projectionMatrix;
    Matrix4x4 V = Camera.main.transform.worldToLocalMatrix;
    Matrix4x4 VP = P * V;
 
    Vector3 point2DCopy = point2D;
    
    // view space => clip space
    point2DCopy.x = 2.0f * (point2DCopy.x - 0.5f);
    point2DCopy.y = 2.0f * (point2DCopy.y - 0.5f);
    
    Vector4 point4 = new Vector4(point2DCopy.x, point2DCopy.y, point2DCopy.z, 1.0f);  // turn into (x,y,z,1)
    
    Vector4 result4 = VP.inverse * point4;  // multiply 4 components
 
    Vector3 result = result4;  // store 3 components of the resulting 4 components
 
    var _result = Camera.main.ViewportToWorldPoint(point2D);
    
    return _result;
 }
Comment
Add comment · Show 4 · 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 victorbisaev · Jan 29, 2018 at 12:15 AM 0
Share

First, it needs to figure out should "point2D" be of "Vector2" type? Or of "Vector3" type then what is "Z" in this case for a screen point?

avatar image joel_cambrian victorbisaev · Jan 29, 2018 at 12:16 AM 0
Share

I am placing the winz parameter, or distance, in the z of point2D.

avatar image victorbisaev joel_cambrian · Jan 29, 2018 at 11:45 PM 1
Share
 static Vector3 viewportToWorldPoint(Vector3 point3D)
 {
     $$anonymous$$atrix4x4 P = Camera.main.projection$$anonymous$$atrix;
     $$anonymous$$atrix4x4 V = Camera.main.transform.worldToLocal$$anonymous$$atrix;
     $$anonymous$$atrix4x4 VP = P * V;
 
     // get projection W by Z
     Vector4 projW = P * new Vector4(0, 0, point3D.z, 1);
 
     // restore point4
     Vector4 point4 = new Vector4( 1.0f - (point3D.x * 2.0f) , 1.0f - (point3D.y * 2.0f) , projW.z/projW.w , 1);
 
     Vector4 result4 = VP.inverse * point4;  // multiply 4 components
 
     Vector3 resultInv = result4 / result4.w;  // store 3 components of the resulting 4 components
 
     var _resultInv = Camera.main.ViewportToWorldPoint(point3D);
     //resultInv == _resultInv
 
     return _resultInv;
 }
Show more comments

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

75 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

Related Questions

undrestanding WorldToScreenPoint and ViewportToWorldPoint 1 Answer

How to get the 2d Poisition (in camera) of the mesh vertices 3 Answers

ViewportToWorldPoint causing object to move 1 Answer

WorldToViewportPoint problem 0 Answers

How to modify the unity camera so that it renders like a GoPro camera 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