Sign in to follow this  

Diffuse lighting for water shader

Recommended Posts

Hi everyone,

I have a water (pixel) shader which works like this:

- Sample a normal map twice using two sets of moving coordinates. Average these to get the normal.
- Sample reflection and refraction maps using screen-space coordinates which are perturbed slightly according to the normal.
- Add a 'murky' colour to the refraction colour based on depth.
- Interpolate between reflection and refraction maps based on the fresnel term (i.e. how shallow the viewing angle is).
- Add specular highlights using the sampled normal.

I'm liking the results but I feel like there should be a diffuse lighting element too, so that waves created by the normal map can be seen when you aren't looking in the direction of the Sun. This is simple enough on a solid object (I'm already doing it on the terrain in the above pictures) but I'm not sure of the most accurate way to do it for water. Should I apply a diffuse factor to both reflections and refractions? Should I do it the same way as I would for solid objects?

Anyone with experience creating their own water shader, some input would be very helpful :)


Share this post

Link to post
Share on other sites

I could be wrong, but from the images you linked it looks like you're using a single color for the entire sky. Before hacking the water lighting calculations, I would try to replace it with something more "interesting", like a proper texture of a clear sky. It should help at least somewhat, because if you use a homogenous sky, the direction of the reflection vector won't really matter, you will fetch the same color regardless. With a proper sky texture that won't be the case, because different parts of the sky are colored different kinds of blue.

Share this post

Link to post
Share on other sites

Your missing Fresnel, here is a image showing a very simple Fresnel work around.




This is simply using a blend between white and blue, nothing else. In your shader the blue will be the part you have and the white will be a reflection map.

The work around I use here is the same as a matcap shader, it's the old way of doing it. If you look around the web you will find much better Fresnel functions.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this