- Home /
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.
Even though the documentation says it should work the same way, I spent about a month developing a water shader, and 2 days after I finished I updated to 5.5 only to have this exact problem. If Unity think it should be identical in values (which it isn't), I guess we just kinda have to wait and see what they do.
Yeh, I'm not sure why LinearEyeDepth and Linear01Depth are acting strange, when Unity says they should work the same. When I get home from work, I think I might try getting the depth manually and flipping it with 1 - z like it says here in the update guide
Answer by FinchWorks · Mar 10, 2017 at 05:57 AM
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!
This is the answer I have been looking for. It honestly makes no sense, but I'm so glad you found it. Gonna take a bit of a deeper look into changes to ScreenPos to see if anything's out of the ordinary.
Answer by IgorAherne · Mar 09, 2017 at 11:30 PM
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);
See, I had the same thought and tried that the other day, but it still doesn't seem to work properly.