Search Unity

Question Weird result transforming positions from world space to clip space

Discussion in 'Shaders' started by maorachow, Mar 18, 2024.

  1. maorachow

    maorachow

    Joined:
    Dec 12, 2021
    Posts:
    5
    I was trying to add SSR into my project, but I found that there are something wrong in matrix multiplcation. Codes are below.
    Code (CSharp):
    1. float3 GetUVFromPosition(float3 worldPos)
    2. {
    3.  
    4. float4 projectionPos = mul(worldPos, UNITY_MATRIX_VP);
    5. projectionPos.xyz /= projectionPos.w;
    6.  
    7. projectionPos.xy = projectionPos.xy * 0.5 + 0.5;
    8. return projectionPos.xyz;
    9. }
    10.  
    11. float3 GetWorldPosition(float2 vTexCoord)
    12. {
    13. float z = SampleSceneDepth(vTexCoord);
    14. float3 vPositionWS=ComputeWorldSpacePosition(vTexCoord, z, UNITY_MATRIX_I_VP);
    15. return vPositionWS.xyz ;
    16. }
    The function GetWorldPosition reconstructs world space position from depth texture, it worked well. The problem is that the GetUVFromPosition function is not transforming world space position to clip space correctly.
    If I do the operation below in pixel shader
    Code (CSharp):
    1.   float3 FragPos = GetWorldPosition(input.TexCoord).rgb;
    2.   return float4(GetUVFromPosition(FragPos).xy,1,1);
    get the world space position of the pixel then transform it back to the clip space, I should get the same result as the code below:
    Code (CSharp):
    1.    return float4(input.TexCoord.xy,1,1);
    but I am NOT getting the same result! As the screenshots below the results are not the same, that means these two functions are not reservable, so I am stuck here. How to solve the issue?
     

    Attached Files: