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?