Depth Buffer Textures: Back - Rear Faces = Distance Dependent?

I am writing a volumetric fog shader, following the techniques listed below:

I looked at the Unity Replacement Shaders example and wrote a script which renders two depth textures, one for rear and one for front faces of meshes in a certain mask. From here, I have a post-processing shader which renders the fog on top of the image. I have no implemented any of the complex cases yet, only the first thickness calculation. Unfortuantely, my results are throwing me for a loop.

I have two depth textures and am subtracting the front depths from the rear depths and displaying this difference as a fog overlay for the pixel. This seems to give a decent result, so long as I do not move toward/away from the object. Moving towards/away from the object changes the thickness, which is very unexpected. I’ve attached the scene.

1122284–42357–$FogScene.unitypackage (48.4 KB)

Keep in mind that the zBuffer is stored non-linearly, so to be able to do linear math on it, you need to convert it first with DECODE_EYEDEPTH(i)

I have been trying to decode the documentation about those functions but can’t figure out when I am supposed to use them. What is the argument (i) for DECODE_EYEDEPTH? I suppose I can delve into the UnityCG file, but a newbie explanation would be very helpful. :slight_smile:

Haha, yeah, I had the same problem. The documentation seems rather sparse.
‘i’ is the value you sample from the DepthTexture.

DECODE_EYEDEPTH is just tex2d(depth).r

You need Linear01Depth to make the depth in range 0-1 or LinearEyeDepth for eye range.