Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
0
Question by BloodMarked · Jun 21, 2021 at 04:28 PM · shadershadersgraphicsshader programming

post shader: reconstruct worldspace plane coordinates

i want to create a postprocess-shader, which reconstructs the xy-coordinates on a plane at z=0, to then use it further.

somehow, i am not able to properly reconstruct the world positions from the Screenspace coordinates.


my fragment shader looks like this:

                 fixed4 col;
                 float3 rayStart = mul(unity_CameraToWorld, float4(i.uv, .0,0));
                 float3 rayEnd   = mul(unity_CameraToWorld, float4(i.uv, 1,0));
 
                 float3 direction = rayEnd - rayStart;
 
                 float distanceToDrawPlane = (.0 - rayStart.z) / direction.z;
                 float3 position = rayStart + direction * distanceToDrawPlane;
 
                 col.xyz = position;
 
                 //debug grid pattern
                 col.xyz = frac(position.x*10) < .5;
                 col.xyz *= frac(position.y * 10) < .5;
 
                 return col;


while the grid is on the correct axis, it has some problems:

  • it moves with the camera, instead of keeping its position relative to the origin

  • the perspective is always orthographic, even in the perspective camera.


now, i tried different approaches, but was unable to make it work properly. other code ive found uses "ComputeViewSpacePosition", but i couldnt find where this function comes from.

does anyone know how to do this conversion properly, or has any example in mind?

alt text

screenshot-2021-06-21-190237.png (11.0 kB)
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
2
Best Answer

Answer by Namey5 · Jun 22, 2021 at 01:39 AM

It's hard to tell exactly what your code is doing without also seeing what 'i.uv' consists of. That said, here's how I do it;


To start off, we're gonna have to pass some info to the shader - as much as Unity does define the camera matrices, from my experience the view-space matrices are never correct so it's easier to just do it ourselves.

 m_Material.SetMatrix ("_ViewToWorld", m_Camera.cameraToWorldMatrix);
 m_Material.SetMatrix ("_InvProjectionMatrix", m_Camera.projectionMatrix.inverse);

We also need to 'undo' the camera projection for this to work (and not appear orthographic as you said), so pass in the inverse projection matrix as well.

When it comes to the shader, the basic process is as follows:

In the vertex shader, we need to calculate the camera frustum corners that lie on the far clip plane, which we can do using the input UV coordinates and the inverse projection matrix.

 float4x4 _InvProjectionMatrix;
 float4x4 _ViewToWorld;
 
 struct appdata
 {
     float4 vertex : POSITION;
     float2 uv : TEXCOORD0;
 };
 
 struct v2f
 {
     float4 pos : SV_POSITION;
     float2 uv : TEXCOORD0;
     float4 viewDir : TEXCOORD1;
 };
 
 v2f vert (appdata v)
 {
     v2f o;
     o.pos = UnityObjectToClipPos (v.vertex);
     o.uv = v.uv;
     // Input position is just the UVs going from [-1,1] at the far clip plane (1.0 because we manually passed in the matrix, which Unity stores via GL convention)
     o.viewDir = mul (_InvProjectionMatrix, float4 (o.uv * 2.0 - 1.0, 1.0, 1.0));
     return o;
 }

Then, in the fragment shader, we can take those points on the far clip plane and convert them into world space using the inverse view matrix we passed in earlier. If you wanted to reconstruct the position of actual in game objects you could also multiply by scene depth at this point.

 // Perspective correction
 float3 viewPos = i.viewDir.xyz / i.viewDir.w;
 // Normalize for view-space view dir
 float3 viewDir = viewPos / abs (viewPos.z);
 
 // If you need normalized world-space view direction use this (don't normalize() otherwise you'll get perspective distortion)
 float3 worldSpaceViewDir = mul ((float3x3)_ViewToWorld, viewDir);
 
 // Start at the camera's position
 float3 rayStart = _WorldSpaceCameraPos.xyz;
 // Convert point on the far clip plane to world-space
 float3 rayEnd = mul (_ViewToWorld, float4 (viewPos, 1.0));
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 BloodMarked · Jun 22, 2021 at 07:41 PM 0
Share

this code works really well for perspective camera, thank you! for orthographic camera, more code is needed,

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

207 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 avatar image avatar image 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

The Best Way To Make Stylised Grass in Unity? 0 Answers

Adding a clip() to the default shader? 0 Answers

How to achive the same look? Which shaders to use? 2 Answers

Is it possible to avoid writing to certain g-buffers? 1 Answer

perform action on shader compilation/update 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