Hi all!
(I would first like to apologize for this lengthy post)
As the title says, I’m trying to implement screen space reflections by myself, and I got to the point where it’s “almost” working but there is a problem with ( what seems to be?) the calculation of the reflection vector because the reflections are working correctly (or seems to be :p) only when the camera is very close to the floor.
Also notice that I’m trying to make the implementation as naive/simple as possible so I can better understand the basic algorithm and then build up more advanced features from that ( like handling the cases where the rays go offscreen).
I wanted to know if the steps that I’m doing are correct, but first let me show you some screenshots.
Here is the camera very close to the floor, and ( aside from little artifacts) seems to be more or less okay.
Now this is what happens when I lift up the camera a little
It seems like the joints where the plane and wall meet are not being reflected correctly, its like the reflection image moved up with the camera as well. I can provide more screenshots if requested:)
Now, this is what I’m doing step by step:
-
In the vertex shader, I calculate a ray in view space that goes from the origin to the far plane ( one ray for each vertex, so they later get interpolated).
-
In the fragment shader I normalize that ray (so I know the direction we are looking at) and calculate the reflection vector with the view direction and normal taken from the DecodeDepthNormal function. The normal and the view direction are both in view space.
-
In view space I reconstruct point that we are looking at, by multiplying the “full ray” by the depth of the current fragment, and use that point as my starting position for the ray marching.
-
Then I calculate the “end point” of the ray march, adding the reflection vector (times some constant that represents how far the ray will go) to the starting position of the ray. So far the start and end point of the ray are in view space.
-
I multiply the start and end position of the ray by the projection matrix (and here is where I start having doubts about if what I’m doing is correct).
-
Here I start doing the ray marching thing. In each iteration I lerp between the post-projection start and ending point over the for loop, and the resulting point gets converted into normalized device coordinates.
-
(still inside the loop) I convert from the normalized device coordinates to screen coordinates and sample the depth texture, and check if that depth is higher or lower than the depth of the point calculated in step 6
-
If the depth of the ray is higher than the sampled depth, then we have intersected an object and should return the color of such object. Here I’m also applying a mask to know if the (original)object is reflective or not.
Below I leave the code of what I just described (it is simplified so it’s easier to read, for example it doesn’t have all of the calcultions in the vertex shader, only the important ones):
v2f vert (appdata v)
{
v2f o;
float4 cameraRay = mul(unity_CameraInvProjection, float4((float2(v.uv.x, 1-v.uv.y) - 0.5) * 2, 1, 1));
o.cameraRay = cameraRay.xyz / cameraRay.w;
return o;
}
fixed3 frag (v2f i) : SV_Target
{
float2 uv = i.uv;
// get_normal and get_depth are just wrappers that use DecodeDepthNormal function so the code is easier to read
//also some textures have the uvs inverted in Y so thats why some have the 1- uv.y fix
float3 decodedNormal = get_normal(float2(i.uv.x, 1 - i.uv.y));
float decodedDepth = get_depth(float2(i.uv.x, 1 - i.uv.y));
//in view space
float3 ray_dir = normalize(i.cameraRay);
float3 reflection = normalize( reflect( normalize( ray_dir), normalize(decodedNormal)));
//in view space
float3 start_point = i.cameraRay * decodedDepth;
float3 end_point = start_point + reflection * _MaxDistance ;
//in homogeneous coordinates
float4 projected_start_point = mul(unity_CameraProjection, float4(start_point,1));
float4 projected_end_point = mul(unity_CameraProjection, float4(end_point,1));
for(int i=0; i< 30; i++)
{
// I don't know if this lerp is conceptually correct
float4 current_projected_point = lerp(projected_start_point,
projected_end_point,i/30.0);
float3 normalized_coord = current_projected_point.xyz / current_projected_point.w;
float2 screen_pos = normalized_coord.xy * 0.5+0.5;
float screen_depth = get_depth(screen_pos);
if(screen_depth < Linear01Depth(normalized_coord.z ))
{
float3 current_color = tex2D(_MainTex, float2(screen_pos.x, 1- screen_pos.y));
return lerp(tex2D(_MainTex, uv), current_color, tex2D(_ReflectiveObjects, float2(uv.x, 1- uv.y)).r);
}
}
//if we didn't find an intersection or went offscreen, return the original value
return tex2D(_MainTex, uv);
}
The texture _ReflectiveObjects is just a mask that tells how much reflective the point of reflection is. I will leave some screenshots below, of the mask and depth/normals/reflection textures.
Depth:
Normals
Reflection vectors
And I wanted to upload the mask but It seems I’ve reached the file limit. I’m sorry if the post got too long ![]()
It’s been a few days since I’m scratching my head with this and can’t come up with a solution. Also I would like to note that I’ve read a lot of articles but unfortunately all of them present a solution that’s “complete” and that makes it harder for me to correctly understand the basic concepts.
Here are some references:
http://www.kode80.com/blog/2015/03/11/screen-space-reflections-in-unity-5/
http://www.gamedev.net/blog/1323/entry-2254101-real-time-local-reflections/
One last thing, I did my best efforts formatting the code here, it’s the best I could do, sorry ![]()




