An explanation of the Scene Depth node.

An explanation of the Scene Depth node.

I was trying to use the Scene Depth node, but couldn't understand how it worked. After searching the internet for a while I found many sites showing examples that used it, but never an explanation of the values it returned, so I spent a while trying just about everything I could in order to figure out how it worked, this is the result, no guarantee it's perfect, but hope it helps someone.

The Scene Depth node returns a value between 0 and 1, this value corresponds to the distance between the camera and an opaque object.

For every pixel of the object that has a shader containing the Scene Depth node, you can imagine a line going from the camera straight to that pixel and continuing forward until the camera's depth buffer, if in this line's path lies an opaque object, then the Scene Depth node returns a value equal to the distance from the camera to that object divided by the camera's depth buffer. However, if that imaginary line doesn't find any opaque objects, then it will return 1.

For example, if the camera's depth buffer is 1000 units and you have an box opaque box 500 units away, then the Scene Depth node of any transparent object between the camera and the box will return 0.5 for every pixel that "covers" the box and 1 for the rest of the pixels.

Here you have an example of a shader that uses the Scene Depth node and an image showing it's effect:

4yjn47det4y51.webp

The multiplications on the left (0.5 x 0.1 x 0.1 x 0.1) is because 0.0005 displays itself as 0.0 because of the size of the input field, sticking 0.0005 into the Vector 1 and removing the multiplications works exactly the same.

3iapic2gt4y51.webp

The pole is a standard Unity cube, with it's standard Material, but stretched out. The blue plane is a standard Unity plane with a material that uses the shader resulting from the above Shader Graph