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 $$anonymous$$ · May 17, 2020 at 04:38 PM · cameramatrix

How calculated ScreenToWorldPoint

Hello

What does the ScreenToWorldPoint code look like?

It would be something like that, but it doesn’t return the value it should.

  public Vector3 ScreenToWorldPoint(Vector3 position) {
          float x = (2.0f * position.x / Camera.main.pixelWidth) - 1.0f;
          float y = (2.0f * position.y / Camera.main.pixelHeight) - 1.0f;
          float z = position.z * 2 - 1;
          Vector3 clip = new Vector3(x, y, -1.0f);
  
          Matrix4x4 viewInverse = Camera.main.cameraToWorldMatrix;
          Matrix4x4 projectionInverse = Matrix4x4.Inverse(Camera.main.projectionMatrix);
          Matrix4x4 viewProjectionInverse = viewInverse * projectionInverse;
  
          Vector3 worldPoint = viewProjectionInverse.MultiplyPoint(clip);
  
          return worldPoint;
      }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · May 18, 2020 at 01:43 AM

What's the exact issue you have with your code? What value do you get back from your code? I don't see a real issue with your code. Over here in the comment I posted two methods. One that calculates the unproject matrix from viewport space to world space and a second method which does the same as ViewportToRay. My code does pretty much the same thing that you did here. I've tested my code in Unity and it works just fine.


Note that using the cameras pixelwidth / height is pretty pointless if you ignore the potential offset. You usually would use Screen.width and Screen.height. If you use the camera specific values you should use the actual camera rect. Though If a camera only renders to a part of the screen the offset of that region also need to be taken into account. Though since you just talk about screen space to world space we usually assume a camera that renders to the full screen anyways.


Note that your code currently always returns the point on the near clipping plane since you hardcoded a z value of -1. If that's your issue you probably want to replace the "-1" with "z".

Comment
Add comment · Show 1 · 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 Bunny83 · May 18, 2020 at 01:56 AM 0
Share

Note that your as well as my method does not resemble what Unity's "ScreenToWorldPoint" method does. This method interprets the z value as the worldspace distance from the camera origin. So it's not related to the actual projection or the viewing frustum. How exactly Unity performs this conversion internally we don't know. They might use a similar approach to my method to deter$$anonymous$$e the worldspace direction and then just scale the normalized direction with the inco$$anonymous$$g z value and add the camera origin.

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

193 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 calculated ViewportToWorldPoint 0 Answers

GL.MultMatrix multiple instances drawing 0 Answers

Advanced info on Unity's camera matrix 0 Answers

Calculation behind camera.WorldToScreenPoint 2 Answers

changing camera view 3 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