• 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 kennux · Sep 02, 2014 at 08:20 PM · c#shader

[SOLVED] Screenspace to Worldspace in computeshader

How exactly does Camera.WorldToScreenPoint() work? I need to do this calculations in a compute shader and i'm not sure how to do it. I tried building a MVP matrix, inverse it and multiplicate a vertex with it but the results are not what i expected.

My code is:

         bool d3d = SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D") > -1;
         Matrix4x4 M = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3.one);
         Matrix4x4 V = Camera.main.worldToCameraMatrix;
         Matrix4x4 P = Camera.main.projectionMatrix;
         if (d3d) {
             // Invert Y for rendering to a render texture
             for ( int i = 0; i < 4; i++) { P[1,i] = -P[1,i]; }
             // Scale and bias from OpenGL -> D3D depth range
             for ( int i = 0; i < 4; i++) { P[2,i] = P[2,i]*0.5f + P[3,i]*0.5f;}
         }
         Matrix4x4 MVP = P*V*M;
         Matrix4x4 InvMVP = MVP.inverse;
         Debug.Log(InvMVP * new Vector3(50,100,100));

I am using DirectX and my depth value is in world uints.

Anyone got an idea?

Edit:// I've seen that there is a function called MultiplyPoint() in the Matrix4x4 class. I tried the example above with his function but it also didn't got me the correct position.

Edit2://

Found a solution. In my fragment shader i save the fragment's position like this:

 float3 fragmentPosition = float3(In.screenPos.xy, In.Position.z);

Where In.screenPos.xy gets calculated in the vertex shader:

 vertexPosition = mul(UNITY_MATRIX_VP, vertexPosition);
 float4 screenPos = ComputeScreenPos(vertexPosition);
 screenPos.xy /= screenPos.w;

In.Position is the vertex position after VP multiplication. I then pass a matrix to the compute shader built like this:

     bool d3d = SystemInfo.graphicsDeviceVersion.IndexOf("Direct3D") > -1;
     Matrix4x4 V = Camera.main.worldToCameraMatrix;
     Matrix4x4 P = Camera.main.projectionMatrix;
     if (d3d)
     {
         // Scale and bias from OpenGL -> D3D depth range
         for ( int i = 0; i < 4; i++) { P[2,i] = P[2,i]*0.5f + P[3,i]*0.5f;}
     }
     Matrix4x4 InvVP = (P*V).inverse;

In the compute shader then i transform the pixel positions into d3d clipping space and do an Inverse VP multiplication like this:

 float3 ComputeWorldPosition(float3 screenPos)
 {
     // Transform pixel space to clipping space
     screenPos.x = 2.0f*screenPos.x * (1.0f/screenSize.x) - 1.0f;
     screenPos.y =  1.0f - 2.0f * screenPos.y * (1.0f / screenSize.y);
     
     // Do inverse VP multiplication
     float4 worldPos = mul(float4(screenPos, 1), InvVPMatrix);
     
     // Calculate final world position
     return worldPos.xyz * worldPos.w;
 }

And that's it :)

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

How do I paint the part of a model that is embedded in another model?, 0 Answers

How to render a RenderTexture into a specific mipLevel 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