# Ideas for Water Foam

This topic is 3407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am attempting to render shoreline foam on my water plane when it intersects with the land. I have got the hardest part out of the way but I am struggling to think of ideas on how to make it blend properly. My method was to create a depth stencil buffer, and render the water plane and the land to it. This was output to a texture. I then lower my land and render the water plane using the depth texture(with proj coords) so that the sillhoutte formed will tell me where the outlines of the water is in screen space. This works well in that it tells me which pixels are near the edge of the land as I can just do this in the shader.
    float depth = tex2D(Depth, projTex.xy).a;
if(depth > 0.0f)
{
OUT.Colour.a = 0.5f;
}


As you can see the result is not very pleasing. Orginally I tried using a foam texture which was animated, this was then modulated with the water colour and applied, but as the stencil is a sharp cut off point there is a clear line where the water switchs to foam. Another thing I thought about doing was using the depth level as a weighting for the foam but due to inaccuracy issues with the depth buffer writes and limitations involved with the near/far clip planes I decided not to do it. Does anybody have any ideas on how I can make it blend effciently, ideally I want to avoid any more render pass's as the water shader is fairly heavy as it is.

##### Share on other sites
Quote:
 Original post by reaper93Does anybody have any ideas on how I can make it blend effciently, ideally I want to avoid any more render pass's as the water shader is fairly heavy as it is.

You can use range and noise maps in addition to make a foam,it will be enough,I think.In this case noise map used for water color,alpha and reflection change,because foam has another appearance then water.Range map used for finding places where foam must be.

##### Share on other sites
Hi Kronhin, thanks for your reply, please can you explain what you mean with the term "range maps"

I don't have any issues in making the foam colour, its just on what to use to determine where foam should be

##### Share on other sites
You could manually make a mask texture based on your terrain and fit it to the water mesh.

Or use a depth map to reconstruct xyz world space positions. This is a common method used in deferred rendering and other things. Then just project this onto the water using the camera view/projection matrices, and determine the water depth from the y position minus the water level. Then normalize this value to a 0-1 range (use pow() and such to make the transitions the way you want) and lerp between the water and the foam texture.

You mentioned you had a problem using depth rendering, but normally you handle this by writing to a separate floating point surface in a pixel shader (possibly using multiple render targets); this shouldn't be affected much by accuracy issues or far/near planes (use the farthest z plane if you have mulitple frustums when reconstructing world space xyz).

##### Share on other sites
Quote:
 Original post by reaper93Hi Kronhin, thanks for your reply, please can you explain what you mean with the term "range maps"I don't have any issues in making the foam colour, its just on what to use to determine where foam should be

I think Matt Aufderheide told enough in case of "plane" foam.

[Edited by - Krokhin on January 26, 2009 5:44:47 AM]

##### Share on other sites
Ok thanks, will look into giving that a go later this week.
Will update when I get it working.

##### Share on other sites

If it's an option for you to render the water using the terrain geometry, there may be no need to wrestle with intermediate maps. Basically you pass in the terrain patches to the vertex shader and set the y coord (or whatever you use for height) to the predifined water level, plus or minus some wave function. Since you have the original y coord from the terrain patch however, you can determine how deep the water is at a given vertex/pixel and blend in some foam where the water is very shallow, ie. where there's a coastline.

Additionally this 'water depth' information comes in handy for faking refraction, since typically you'll want shallow water to be transparent while deep waters should just get a darker blue hue.

Hope this makes sense [smile]

##### Share on other sites
Quote:
 Original post by remigiusIf it's an option for you to render the water using the terrain geometry, there may be no need to wrestle with intermediate maps. Basically you pass in the terrain patches to the vertex shader and set the y coord (or whatever you use for height) to the predifined water level, plus or minus some wave function. Since you have the original y coord from the terrain patch however, you can determine how deep the water is at a given vertex/pixel and blend in some foam where the water is very shallow, ie. where there's a coastline. Additionally this 'water depth' information comes in handy for faking refraction, since typically you'll want shallow water to be transparent while deep waters should just get a darker blue hue.Hope this makes sense [smile]

This is a good suggestion.

I use something similar for setting up shore line effects, blending water into the landscape where shallow, and making sure deep water looks deep... Luckily with a 4 component height map and bump map, which is still as fast as it gets on a GPU with textures, there are generally 2 channels free which can be used for this 'side' info....

• 9
• 16
• 9
• 13
• 41