Distance to next model in shader


I want my water shader to take the depth of the water into account; where the water is deeper it should be less transparant as to where it is shallow.

What i need is a way to find (in the vertex shader) the distance to the next furthest vertex from the camera’s position. I read something about the depth buffer though i have no idea if and how i can implement this.

Any help (or thoughts on different approaches) will be much appreciated!

I think this is how it would be done but i am not trying for this effect for a couple of weeks.

You would need
1.geomtry below your water surface
2.create buffer with a camera looking down with the water object hidden from it.
3.render this buffers camera depth buffer into a texture.
4. send texture to water shader and do whatever you need done with it.

Thanks, i will have a look at it. If i am not mistaken the fireflies example also uses this approach

You’d probably need to render in two passes; first the terrain geometry, then render the water. When rendering the water, you can compare the clipspace z/w with the terrain depth to get the distance.

It’s not straightforward, but this method does also allow for refraction. Perhaps there are other methods, though.

This one’s not perfect, but you could send the heightmap as a texture to your water shader, and calculate the height of the terrain using that at every given position. It’s a lot easier than using multiple passes, though.