Managed and unmanaged DirectX development.
The evolution of the Randomchaos 3D Engine and Generic XNA samples.

Enter your search termsSubmit search form

Web

www.riemers.net

www.ziggyware.com

www.xna-uk.net

Thursday, June 14, 2007

Engine Design - Water (Ocean)

Now, once I had a terrain object I thought it would be great to get a water model to go with it. I thought I would just use the terrain class and modify it so that it was flat. I then needed a shader to do my water effect, and being new to all this 3D stuff and HLSL, did not have the skills to write my own, so I had a mooch on line and found NVIDIA's Ocean shader, with a few tweaks I got it working with my engine.

I am going to give you the whole class at the end along with the modified shader. What I will do now is show the effects of various parameter setting's.

Basic WaterHere is a basic implementation of the classAs you can see, in the picture above, I am reflecting the surrounding skybox and the bumpmap is also applied. The wave amplitude and frequency are set to 0 so you cant see any waves in this example.

BumpMapThis is the bumpmap I am using to ripple the surface of the water

If I remove the bump map from the render this is the effectKinda takes the depth out of the water doesn't it.

You can manage the bump height with the BumpMapHeight parameter

water.BumpMapHeight = 0.1f;

The initial picture above uses the default BumpMapHeight of 0.1 the best range is from 0 to 2, so here are both those extremes

With a BumpMapHeight of 0, the water renders like still water, no ripples at all.

With a BumpMapHeight of 2, the water renders like a raging river

Waves

OK, I will now set the BumpMapHeight back to default and start creating some waves.As you can see this is a nice wave effect, this was done by setting the wave amplitude to 1 and the frequency to .1

water.WaveFrequency = .1f; water.WaveAmplitude = 1f;

Lets see what this looks like now with the maximum recommended BumpMapHeight of 2Now that just makes me feel a little sea sick...

Water ColorAll the examples so far have been setting the DeepWaterColor to Navy, ShallowWaterColor to DarkSeaGreen and the ReflectionColor to DarkGrey, but as you guessed you can alter these colors too.

So here is the ocean rendered without the AlphaBlendColor parameter being set and then with it set as in the code snippet above.

You still get all the lovely reflection and bump effects, they are just now rendered against a transparent mesh.

SynopsisThis is a very configurable shader and coupled with my RCWater class is easily accessible. As you can see in the images above it can be used in a variety of ways and I have only really touched on the possible parameter variations. However, this shader is GPU heavy, but then I am running it on my laptop which is not exactly designed for high performance 3D rendering.

/*********************************************************************NVMH3**** Path: $Id: //sw/devtools/ShaderLibrary/1.0/HLSL/Ocean.fx#1 $ File: ocean.fx Copyright NVIDIA Corporation 2003-2007 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. % Simple ocean shader with animated bump map and geometric waves % Based partly on "Effective Water Simulation From Physical Models", GPU Gems keywords: material animation environment bumpmap Sparkle Mod: C.Humphrey Altered the shader so that the "HALO" sparkle effect could be switched on and off rather than having either or. ******************************************************************************/