Search Unity

reconstruct worldspace plane coordinates in post process

Discussion in 'Shaders' started by BloodMarked, Jun 21, 2021.

  1. BloodMarked

    BloodMarked

    Joined:
    Dec 11, 2013
    Posts:
    28
    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 correctly.

    _

    my fragment shader looks like this:

    Code (CSharp):
    1.                     fixed4 col;
    2.                     float3 rayStart = mul(unity_CameraToWorld, float4(i.uv, .0,0));
    3.                     float3 rayEnd   = mul(unity_CameraToWorld, float4(i.uv, 1,0));
    4.  
    5.                     float3 direction = rayEnd - rayStart;
    6.  
    7.                     float distanceToDrawPlane = (.0 - rayStart.z) / direction.z;
    8.                     float3 position = rayStart + direction * distanceToDrawPlane;
    9.  
    10.                     col.xyz = position;
    11.  
    12.                     //debug grid pattern
    13.                     col.xyz = frac(position.x*10) < .5;
    14.                     col.xyz *= frac(position.y * 10) < .5;
    15.  
    16.                     return col;
    Screenshot 2021-06-21 190237.png
    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.

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

    edit: attached all files for the render pass and shader
     

    Attached Files:

    Last edited: Jun 21, 2021
  2. BloodMarked

    BloodMarked

    Joined:
    Dec 11, 2013
    Posts:
    28
    note: passing c
    am.cameraToWorldMatrix
    into the shader yield the same result,
    unity_CameraInvProjection
    does not work at all.

    i also tried these two, but none of them seem to work:

                material.SetMatrix("_CamMatrix", cam.cameraToWorldMatrix); //is same as unity_CameraToWorld

                material.SetMatrix("_CamMatrix", cam.projectionMatrix.inverse); //does nothing at all
     
    Last edited: Jun 21, 2021