Reversed Z-Buffer in 5.5 Update Breaking a Shader; Any Advice?

Hey, thanks for looking at this topic!

I recently updated a project to 5.5 and I noticed that a shader I’m building no longer works as intended, I think, due to the fact that the Z-Buffer near/far values have been reversed. I am using the z-buffer to create a ring of foam around objects sitting in water.

I’m using something similar to this shader by Chris Flynn.
Specifically this section,

//Get the distance to the camera from the depth buffer for this point
float sceneZ = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);

//Actual distance to the camera
float partZ = i.projPos.z;

//If the two are similar, then there is an object intersecting with our object
float diff = (abs(sceneZ - partZ)) / _HighlightThresholdMax;

if(diff <= 1)
{
      finalColor = lerp(_HighlightColor, _RegularColor, float4(diff, diff, diff, diff));
 }

However, with the depth buffer reversed, it’s doing the opposite, and rendering foam in the space closer to the camera. I’ve been messing with the values for quite a while without any real success. Can anyone give me advice on how to get the desired effect with the new 5.5 Depth Buffer?

The above is what I’m trying to accomplish, and below is what’s happening with the same code now, but with a much larger foam threshold.

89652-capture.png
89653-capture2.png

Interesting! I did some digging in the forums and found someone who had found a solution!
Keep the original LinearEyeDepth equation but change the screen position to compare it to i.projPos.w instead of i.projPos.z

float partZ = i.projPos.w;

I can’t really say that I understand why this change works, but my shader is working as intended again. Hope this helps someone else, and thanks for the replies!

if Z-depth returns inverted results (0 for far away and 1 for closest), just invert it during sampling

 //Get the inverted distance to the camera from the depth buffer for this point
 float sceneZ = LinearEyeDepth ( 1- tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)).r);