Rendering Water as Post-Process Effect

The awesome SeaMonkey WaterFilter is highly configurable. It can render any type of water and also simulates the underwater part of the effect, including light effects called caustics. The effect is based on Wojciech Toman’s Rendering Water as a Post-process Effect published on Here’s a video:


The SeaMonkey WaterFilter is ideal for oceans and lakes, and especially for under-water scenes. If you only need a small simple water surface, such as a water trough or a shallow fountain, the SimpleWaterProcessor may already be all you need.

The Theory

The effect is part of a deferred rendering process, taking advantage of the pre-computed position buffer and back buffer (a texture representing the screen’s pixels position in view space, and a texture of the rendered scene).

After some calculation, this allows to reconstruct the position in world space for each pixel on the screen. If a pixel is under a given water height, let’s render it as a blue pixel! Blue pixel? Not exactly, we want waves, we want ripples, we want foam, we want reflection and refraction.

The article describes how those effects are achieved, but the main idea is to generate waves from a height map, create ripples from a normal map, blend in the foam texture when the water depth is below a certain height, compute the refraction color with a clever color extinction algorithm, and then, display the reflection and specular effect by computing a Fresnel term (like in the simple water effect). In addition, this effect allows to blend the water shore with the ground to avoid the hard edges of classic water effects based on grids or quads.

How Did We Implement it in jME3?

jME3 default behavior is to use a forward rendering process, so there is no position buffer rendered that we can take advantage of. But while rendering the main scene to a frame buffer in the FilterPostProcessor, we can write the hardware depth buffer to a texture, with nearly no additional cost.

There are several ways of reconstructing the world space position of a pixel from the depth buffer. The computational cost is higher than just fetching the position from a position buffer, but the bandwidth and the memory required is a lot lower.

Now we have the rendered scene in a texture, and we can reconstruct the position in world space of each pixel. We’re good to go!

– Nehon

Sample Code

These are test cases in the jME3 repository:

Using the Water Filter

In the simpleInitApp() method, you attach your scene to the rootNode, typically a terrain with a sky. Remember to add a directional light, since the water relies on the light direction vector. The WaterFilter constructor expects a node with the scene attached that should be reflected in the water, and vector information from the light source’s direction.

This is how you use the water filter post-processor code in your code:

private FilterPostProcessor fpp;
private WaterFilter water;
private Vector3f lightDir = new Vector3f(-4.9f, -1.3f, 5.9f); // same as light source
private float initialWaterHeight = 0.8f; // choose a value for your scene

public void simpleInitApp() {
  fpp = new FilterPostProcessor(assetManager);
  water = new WaterFilter(rootNode, lightDir);

Usually you make the water reflect everything attached to the rootNode. But you can also give a custom node (a subnode of the rootNode) to the WaterFilter constructor that has only a subset of scene nodes attached. This would be a relevant optimization if you have lots of nodes that are far away from the water, or covered, and will never be reflected.

Optional: Waves

If you want waves, set the water height in the update loop. We reuse the initialWaterHeight variable, and repeatedly reset the waterHeight value according to time. This causes the waves.

private float time = 0.0f;
private float waterHeight = 0.0f;

public void simpleUpdate(float tpf) {
  time += tpf;
  waterHeight = (float) Math.cos(((time * 0.6f) % FastMath.TWO_PI)) * 1.5f;
  water.setWaterHeight(initialWaterHeight + waterHeight);

Optional: Water Wave and Color Effects


All these effects are optional. Every setter also has a getter.

Water method example Effects: Waves Default


Use this waterheight method for causing waves.



How high the highest waves are.



Sets the scale factor of the waves height map. The smaller the value, the bigger the waves!


water.setWindDirection(new Vector2f(0,1))

Sets the wind direction, which is the direction where the waves move

Vector2f(0.0f, -1.0f)


How fast the waves move. Set it to 0.0f for still water.


water.setHeightTexture( (Texture2D)
manager.loadTexture("Textures/waveheight.png") )

This height map describes the shape of the waves


water.setNormalTexture( (Texture2D)
manager.loadTexture("Textures/wavenormals.png") )

This normal map describes the shape of the waves



Switches the ripples effect on or off.



Sets the normal scaling factors to apply to the normal map. The higher the value, the more small ripples will be visible on the waves.


Water method example Effects: Color Default

water.setLightDirection(new Vector3f(-0.37f,-0.50f,-0.78f))

Usually you set this to the same as the light source’s direction. Use this to set the light direction if the sun is moving.

Value given to WaterFilter() constructor.


Usually you set this to the same as the light source’s color.



Sets the main water color.

greenish blue


Sets the deep water color.

dark blue
ColorRGBA(0.0f, 0.0f,0.2f,1.0f)


Sets how fast colors fade out. use this to control how clear (e.g. 0.05f) or muddy (0.2f) water is.


water.setColorExtinction(new Vector3f(10f,20f,30f));

Sets At what depth the refraction color extincts. The three values are RGB (red, green, blue) in this order. Play with these parameters to “muddy” the water.


Water method example Effects: Shore Default


Limit the water filter to a hemisphere with the given center and radius. Use this for lakes and smaller bodies of water. Skip this for oceans.



Sets how soft the transition between shore and water should be. High values mean a harder transition between shore and water.



Renders shoreline with better quality ?


Water method example Effects: Foam Default


Switches the white foam on or off



Sets how much the foam will blend with the shore to avoid a hard edged water plane.


water.setFoamExistence(new Vector3f(0.5f,5f,1.0f))

The three values describe what depth foam starts to fade out, at what depth it is completely invisible, at what height foam for waves appears (+ waterHeight).


water.setFoamTexture( (Texture2D)
manager.loadTexture("Textures/foam.png") )

This foam texture will be used with WrapMode.Repeat


Water method example Effects: Light Default


Sets how big the sun should appear in the light’s specular effect on the water.



Switches specular effect on or off



Sets the shininess of the water reflections



Switches the refraction effect on or off.



The lower the value, the less reflection can be seen on water. This is a constant related to the index of refraction (IOR) used to compute the fresnel term.



This value modifies the current Fresnel term. If you want to weaken reflections use bigger value. If you want to emphasize them, use a value smaller than 0.



Sets the size of the reflection map. The higher, the better the quality, but the slower the effect.


Sound Effects

You should also add audio nodes with water sounds to complete the effect.

AudioNode waves = new AudioNode(assetManager, "Sounds/Environment/Ocean Waves.ogg", false);

See also: audio.

See also: