Hi everyone, I’m trying to implement screen based reflections, but I can’t get past the initial requirement of going from screen space to view space. Here’s what I’m actually doing now, keep in mind that I’m using this as a post processing effect using Blit:
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
//camera ray is a vector3 that points from the camera to the far plane
o.cameraRay = mul(_CameraInverseProjectionMatrix, float3((float2(v.uv.x, 1-v.uv.y) - 0.5) * 2, 1)).xyz;
return o;
}
fixed3 frag (v2f i) : SV_Target
{
float3 decodedNormal;
float decodedDepth;
DecodeDepthNormal( tex2D( _CameraDepthNormalsTexture, float2(i.uv.x, 1 - i.uv.y)), decodedDepth, decodedNormal);
// Here I should now have the position that I'm looking for in view space
float3 view_pos = i.cameraRay * decodedDepth;
// and if that is the case, then projecting it again should display the same image than the camera without this transformations...
float4 projected_view_pos = mul(UNITY_MATRIX_P, float4(view_pos ,1));
float3 screen_point = projected_view_pos.xyz / projected_view_pos.w;
return tex2D(_MainTex, float2(screen_point.x,1- screen_point.y) * 0.5 + 0.5);
}
And the result is…almost correct:
And if I show the screen positions like this:
return fixed3(float2(screen_point.x,1- screen_point.y) * 0.5 + 0.5, 0);
This is the result:
Now that doesn’t look correct at all.
I’ve read everything I could find but I can’t understand what I’m doing wrong. Also here I leave the normals and depth if that may be helpful:

Also I forgot to tell you that I’m setting the inverse projection matrix with:
mat.SetMatrix(“_CameraInverseProjectionMatrix”,Camera.main.projectionMatrix.inverse);

