Are there any examples of depth-based shaders?

I would like a shader for water such that the opacity of a pixel is given by the z-depth of the object behind the transparent material. Like fog but only applied on certain materials.

To make it more clear. Say I draw a triangle onto the screen, I want the things behind it that are nearest to be more visible than the things further away where they just become the colour of the material (e.g. blue.)

Are there any examples of this? (Can a shader even take the z-depth from the screen?) and would it only work on certain graphics cards?

This tutorial is simple to follow and seems to be doing what you want.

2 Likes

4156174--366556--water_screenshot.png [quote=“Peter77, post:2, topic: 730292, username:Peter77”]
This tutorial is simple to follow and seems to be doing what you want.
https://lindenreid.wordpress.com/2017/12/15/simple-water-shader-in-unity/
[/quote]
Yes perfect!!! Does exactly what I want!

I also used this one and I came up with quite a nice water shader.

Next I’ll have to work on when the camera goes half in and half out of the water when the character goes swimming.

It looks like I’ll have to create a new mesh each frame and place it in front of the camera. Alternatively I could just cheat and just use a transparent quad and just assume that most of the time the camera is fully in or fully out of the water… or… have a sort of gradient effect… lots of fun. I think most games cheat a bit with this.

(Ignore the cracks in the landscape that’s another problem with dynamic tessellation that I’ll have to fix.)

Edit: Well it worked in the editor. But not when it was built. :frowning:
I just had to set the camera to enable the depth texture now it works in the build. :slight_smile: I am awesome.

1 Like