6
\$\begingroup\$

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.

\$\endgroup\$
11
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Nic Foster Dec 21 '12 at 15:04
  • \$\begingroup\$ A fog is not enough due to color absorption underwater. See en.wikipedia.org/wiki/Underwater_vision#Color_vision The water absorbs the light. For example, it absorbs for example red light 100x more than blue light. \$\endgroup\$ – Laurent Jan 19 '17 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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