Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm looking to make a water shader that colors the water based on its depth. Up until now my water shader that I've used has basically been extremely reflective and only looked somewhat blue because it was reflecting a blue sky. The effect I'm shooting for looks something like the one from the first page in this article.

I don't necessarily need exact shader code, just the gist of how I calculate the color of the pixel based on the depth, while still getting a proper reflection and refraction. I should mention that I already have the water's depth calculated, per-pixel, and I also have working reflection and refraction. All I'm lacking is a color based on depth.

I've tried a bunch of stuff, the next thing I'm considering is actually coloring the terrain that is underwater, and coloring it based on depth.

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

My impression is that this color is essentially a fog color, thinking of the water as being a fog volume with a shiny surface. The simplest thing to do is probably to just let your artists pick a shallow color and a deep color, and lerp between them based on the depth. Something like:

lerp(shallowColor, deepColor, saturate(depth * depthColorScale));

Here depthColorScale is 1.0 / the depth at which the water should reach deepColor (probably a couple meters, I'd guess). Then treat the result as a fog color applied to the terrain under the water in the usual way: namely, lerp from the terrain color to the fog color based on the length of the refracted underwater eye ray.

You could also use a smoothstep instead of a lerp, or some other interpolation function, if you find it looks better.

share|improve this answer
    
Thanks, this seems to get me pretty close to what I want. This is for an open-source game engine, so when it's done I'll post the results so anybody can see the code/shaders if they'd like. –  Nic Foster Dec 21 '12 at 15:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.