The understanding I get is that LinearEyeDepth returns world scaled view space depth (ranging from near clip plane to far clip plane). Then you have raw ScreenPos.w which is… also world scaled view depth? I may be mixing some terminology here, but the thing is I just don’t understand how the difference between these two values magically return a value where all the contact points return black.
Is there a slight offset between them that allows this to happen? I’d love to be able to visualize the logic here.
The Scene Depth node is sampling the camera depth texture, which is a texture that’s created by rendering all the depths of opaque material queue objects in the view to a texture prior to main rendering. The linear “Eye” depth is a world scale view space depth, as you guessed.
The Screen Pos raw w is indeed also the world scaled view space depth, but for the triangle surface currently being rendered. And that’s how it works. You’re getting the difference between the opaque scene geometry’s view depth and the currently rendered surface’s view depth at each pixel.
One caveat is the Screen Pos raw w is only the view space depth if you’re using a perspective camera. For an orthographic camera it’s always just 1.0. The reason why it’s the view space depth for a perspective camera is more like a coincidence here, and has to do with how projection matrices and homogeneous coordinate systems work. If you’re intending to use an orthographic camera, you’d need to use the Position node with the Space set to View, and then use the negative z to match the Scene Depth’s Eye depth.
Whoa, thank you so much for the answer! It makes sense now.
Do you have Ko-fi or anything of the sorts? Your answers are always so helpful and I’d love to show my appreciation for the thoughtful posts and articles you write.
I tried this nodes in SRP and it render nothing. They seem to work only in URP. I was looking for the scene depth different between URP and SRP but can’t get an answer. Still don’t know why this happen.
SRP stands for Scriptable Render Pipeline. The URP (Universal Render Pipeline) and HDRP (High Definition Render Pipeline) are both an SRP. If by “SRP” you mean “Standard Render Pipeline”, or are otherwise refering to the built in render pipeline (BIRP) then the above shader should work just fine, though there are cases where the camera depth texture, which is what the scene depth node is reading, may not be being generated. If for example you have no opaque geometry in the scene, or are not using a shadow casting directional light, and aren’t using any post processes that make use of the depth, then Unity’s BIRP won’t render the depth texture. You can force it to render which a script that enables it on the camera. You can also check if it’s being rendered using the frame debugger.
yes I mean BIRP. And you’re right, I tested in 2 new project and this work just fine. Somehow they are all black in my project after switching from URP to BIRP. I export the whole scene to a new project without any code and this still happen. Still working out to find the reason
— EDIT —
I found out it happen after switching platform to android:
after switching back to Windows:
Mobile platforms don’t render the depth texture by default, even if you’re using a shadow casting directional light. It has to be forced on either through a script on the camera. Or you can enable soft particles in the quality settings.