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
1
Question by Lemures64 · Jun 23, 2017 at 06:23 PM · cameramatrixworldtoscreen

How to get world position to screen position matrix?

How to get a matrix that simulates behaviour of function Camera.WorldToScreenPoint? I need to be able to calculate Screen position of certain points being given their world position inside Compute Shader.

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

Answer by IgorAherne · Jun 24, 2017 at 09:37 AM

c#:

 Matrix4x4 matrixVP = myCamera.projectionMatrix  * myCamera.worldToCameraMatrix; //multipication order matters
 
 Vector4 clipCoord = matrixVP.MultiplyPoint(myWorldXYZW);

notice, you can now check clipCoord.w and if its -1, then you know the vertex will NOT be seen by the camera

 Vector4 perspectiveCoord = clipCoord / clipCoord.w; //goes from -1 to 1 on each axis and z is 0 to 1, w is now 1

What we've done is taken frustum-shaped volume and shrunken it into a cube.

now, you need to take your perspectiveCoord.xy and re-specify it relative to bot left corner (u said u need screen space)

 perspectiveCoord = perspectiveCoord*0.5 + 0.5;
 perspectiveCoord *= new Vector4(myCamera.width, myCamera.height,1,1);//screen space coord (bot left 0,0 top right 1920, 1080 or other resolution which u have)


Comment
Add comment · Show 2 · 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 Colin4T2 · Aug 17, 2017 at 10:52 AM 0
Share

Hi Igor

What version of Unity are you using? I'm using 5.5.2 and the API does not support your code. Eg $$anonymous$$atrix4x4.$$anonymous$$ultiplyPoint returns a Vector3, so I'd lose the w component of clipCoord.

There is a multiplication operator for $$anonymous$$atrix4x4 and Vector4 which returns a Vector4. I tried that and I'm not getting the correct results (I think). The clip coord has xy=0, but the position is actually offset left and down in the camera's view.

What am I doing wrong?

avatar image $$anonymous$$ · May 17, 2020 at 12:53 PM 0
Share

and z koords?

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

99 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

Related Questions

Calculation behind camera.WorldToScreenPoint 2 Answers

How calculated ViewportToWorldPoint 0 Answers

GL.MultMatrix multiple instances drawing 0 Answers

Advanced info on Unity's camera matrix 0 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