Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
11 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
2
Question by amit_nath30 · Jul 24, 2015 at 07:11 PM · cameramatrixworldtoscreenpointmatrix4x4worldtoscreen

Calculation behind camera.WorldToScreenPoint

Hello all,

Is there any way to map world point to screen point using matrix, which output value same as one get when using Camera.main.WorldToScreenPoint function?

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

Answer by zach-r-d · Jul 25, 2015 at 12:30 PM

Unfortunately, this is mathematically impossible. One of the operations involved in computing the screen point of a world coordinate (dehomogenizing a homogeneous coordinate) cannot be represented as a linear transformation, so there cannot exist a matrix (which is really just a convenient encoding of a linear map) that can represent it.

If you're curious how the function actually works, it probably looks something like this:

 Vector3 manualWorldToScreenPoint(Vector3 wp) {
         // calculate view-projection matrix
         Matrix4x4 mat = cam.projectionMatrix * cam.worldToCameraMatrix;
 
         // multiply world point by VP matrix
         Vector4 temp = mat * new Vector4(wp.x, wp.y, wp.z, 1f);
 
         if (temp.w == 0f) {
             // point is exactly on camera focus point, screen point is undefined
             // unity handles this by returning 0,0,0
             return Vector3.zero;
         } else {
             // convert x and y from clip space to window coordinates
             temp.x = (temp.x/temp.w + 1f)*.5f * cam.pixelWidth;
             temp.y = (temp.y/temp.w + 1f)*.5f * cam.pixelHeight;
             return new Vector3(temp.x, temp.y, wp.z);
         }
     }
Comment
Add comment · Show 12 · 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 amit_nath30 · Jul 25, 2015 at 12:36 PM 1
Share

This is exactly what i was looking for. thank you

avatar image zach-r-d · Jul 25, 2015 at 01:42 PM 0
Share

Glad it helped! Remember to mark an answer as accepted if it answers your question. :)

avatar image Phylliida · May 07, 2017 at 10:21 PM 0
Share

How do you invert this? (i.e., what is manualScreenToWorldPoint)

avatar image JanSenseVR · Nov 13, 2019 at 12:41 PM 0
Share

Inverted function - $$anonymous$$anualScreenToWorldPoint

To get the required matricies you can pass in camera.cameraToWorld$$anonymous$$atrix and camera.projection$$anonymous$$atrix.inverse.

Hope this helps someone someday :)

   public static Vector3 $$anonymous$$anualScreenToWorldPoint(Vector2 screenPoint, float distance, $$anonymous$$atrix4x4 cameraToWorld$$anonymous$$atrix, $$anonymous$$atrix4x4 projection$$anonymous$$atrixInverse) {
     // here we are converting screen point in screen space to camera space point placed on a plane "distance" away from the camera
     // screen point is in range [(0,0) - (Screen.Width, Screen.Height)]
     Vector2 pointViewportSpace = screenPoint / new Vector2(Screen.width, Screen.height); // convert space [(0,0) - (Screen.Width, Screen.Height)] to [(0,0) - (1,1)]
     Vector2 pointCameraSpaceNormalized = (pointViewportSpace * 2.0f) - Vector2.one; // convert space [(0,0) - (1,1)] to [(-1,-1) - (1,1)]
     Vector2 pointCameraSpace = pointCameraSpaceNormalized * distance; // convert space [(-1,-1) - (1,1)] to [(-dist,-dist) - (dist, dist)]
     Vector4 planePoint = new Vector4(pointCameraSpace.x, pointCameraSpace.y, distance, distance); // define the point (don't know why z and w components need to be set to distance)
 
     // calculate convertion matrix from camera space to world space
     $$anonymous$$atrix4x4 matrix = cameraToWorld$$anonymous$$atrix * projection$$anonymous$$atrixInverse;
     // multiply world point by VP matrix
     Vector4 worldPoint = matrix * planePoint;
 
     return worldPoint;
   }

avatar image cyz2727327 JanSenseVR · Sep 22, 2021 at 06:50 PM 0
Share

Thank you so much JanSenseVR, this works!!! I have been trying to find this for so LONG

avatar image JanSenseVR · Nov 13, 2019 at 12:53 PM 0
Share

The convertion other way around could look something like this. Tested and works without issues.


$$anonymous$$anualScreenPointToWorldPoint function

   public static Vector3 $$anonymous$$anualScreenPointToWorldPoint(Vector2 screenPoint, float distance, $$anonymous$$atrix4x4 cameraToWorld$$anonymous$$atrix, $$anonymous$$atrix4x4 projection$$anonymous$$atrixInverse) {
     // here we are converting screen point in screen space to camera space point placed on a plane "distance" away from the camera
     // screen point is in range [(0,0) - (Screen.Width, Screen.Height)]
     Vector2 pointViewportSpace = screenPoint / new Vector2(Screen.width, Screen.height); // convert space [(0,0) - (Screen.Width, Screen.Height)] to [(0,0) - (1,1)]
     Vector2 pointCameraSpaceNormalized = (pointViewportSpace * 2.0f) - Vector2.one; // convert space [(0,0) - (1,1)] to [(-1,-1) - (1,1)]
     Vector2 pointCameraSpace = pointCameraSpaceNormalized * distance; // convert space [(-1,-1) - (1,1)] to [(-dist,-dist) - (dist, dist)]
     Vector4 planePoint = new Vector4(pointCameraSpace.x, pointCameraSpace.y, distance, distance); // define the point (don't know why z and w components need to be set to distance)
 
     // calculate convertion matrix from camera space to world space
     $$anonymous$$atrix4x4 matrix = cameraToWorld$$anonymous$$atrix * projection$$anonymous$$atrixInverse;
     // multiply world point by VP matrix
     Vector4 worldPoint = matrix * planePoint;
 
     return worldPoint;
   }



Example usage (place on the camera gameobject and play around with screen point and distance fields)

 using UnityEngine;
 
 public class Draw$$anonymous$$anualScreenPointToWorldPoint : $$anonymous$$onoBehaviour {
   Camera cam;
 
   [SerializeField]
   float distance = 1000;
   [SerializeField]
   Vector2 screenPoint = Vector2.zero;
 
   void Start() {
     cam = GetComponent<Camera>();
   }
 
   void Update() {
     var point = $$anonymous$$yUtilityClass.$$anonymous$$anualScreenPointToWorldPoint(
       screenPoint,
       distance,
       cam.cameraToWorld$$anonymous$$atrix,
       cam.projection$$anonymous$$atrix.inverse);
 
     Debug.DrawLine(cam.transform.position, point, Color.green);
   }
 }










Show more comments
avatar image
0

Answer by cyz2727327 · Sep 22, 2021 at 06:49 PM

I can confirm JanSenseVR's code works, thank you so much I have been searching for this for so LONG !!! @JanSenseVR

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

10 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

Related Questions

How to get world position to screen position matrix? 1 Answer

WorldToScreenPoint, isn't getting it right? 1 Answer

WorldToViewportPoint problem 0 Answers

Setting the camera projection matrix does nothing 0 Answers

How do I calculate a view matrix using Matrix4x4.LookAt? 2 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