Water Rendering

Water rendering is easy to achieve in Awakening. You first use a Portal or Ocean to generate reflected / refractive images, the use water shaders to compose them.

All shader files for water rendering can be found at the "effects\water" folder.


Shader File
Preview Explanation
bump_nops.fx   Use a bump map to distort reflected / refractive  image.

Factor:
number
bump_texture
number
reflected_texture
number
refractive_texture

vec4 
vecWaterNormal -- the normalize normal vector of water surface
vec4 __vFactor -- x : wave speed

The Alpha value of material's Diffuse color control the transparency of water, 100 is appropriate commonly.

Usage:
material.setTexture(0, bump_texture)
material.setTexture(1,
reflected_texture)
material.setTexture(2,
refractive_texture)

local waternormal=vec4.new(0,1,0)
local wavespeed=0.2
material.setEffectVector('vecWaterNormal',waternormal)
material.setFactor( wavespeed, 0,0,0 )

PixelShader: No
bump_norefra_nops.fx   Use a bump map to distort reflected image, no refraction; fast than above shader.
You should check material's
Transparence style for this shader.

Factor:
number
bump_texture
number
reflected_texture

vec4 
vecWaterNormal -- the normalize normal vector of water surface
vec4 __vFactor -- x : wave speed

The Alpha value of material's Diffuse color control the transparency of water, 100 is appropriate commonly. Also can use RGB as water color.

Usage:
material.setTexture(0, bump_texture)
material.setTexture(1,
reflected_texture)

local waternormal=vec4.new(0,1,0)
local wavespeed=0.2
material.setEffectVector('vecWaterNormal',waternormal)
material.setFactor( wavespeed, 0,0,0 )

PixelShader: No
lake.fx   Compose reflected / refractive images  and water color by depth map, and use a bump map to distort result.
See
Render scene depth info to a texture for depth map, and you need hide ocean self when rendering the map.

Factor:
number
reflected_texture
number
refractive_texture
number
depth_texture
number
bump_texture

vec4 
vecWaterNormal -- the normalize normal vector of water surface
vec4 __vFactor -- x : wave speed,  y: depth power, z: depth offset, w : bump strength

The Alpha value of material's Diffuse color control the transparency of water, 100 is appropriate commonly. Also can use RGB as water color.

Usage:
material.setTexture(0, reflected_texture)
material.setTexture(1,
refractive_texture)
material.setTexture(2,
depth_texture)
material.setTexture(3,
bump_texture)

local waternormal = vec4.new(0,1,0)
local wavespeed = 0.2
local depthpower = 2
local depthoffset = 1
local bump=2

material.setEffectVector('vecWaterNormal',waternormal)
material.setFactor( wavespeed, depthpower, depthoffset, bump )


PixelShader: 2.0
lakeRipple.fx   Mainly same with lake.fx, but also use a ripple map to plus the distort result.

Factor:

( ... same with lake.fx here )
number
ripple_texture -- the ripple texture essentially is a height map, border should be black

vec4 
vecUVRipple -- uvRipple = uvBump* vecUVRipple.xy + vecUVRipple.zw
number fRippleStrength


Usage:
material.setTexture(4, ripple_texture)

-- sample codes for using ripple map
local g_ripple_position = vec.new();
local g_ripple_uvscale = 0;

function OnLButtonDown(x,y)
   local ray=GetRayFromPoint( x,y )
   local bInter, fDis = ocean.intersectRay(ray,true)
   g_ripple_position = ray.getOrg() + ray.getDir()*fDis;
   g_ripple_uvscale = 4;
end


local lasttime = 0;

function FrameMove()
   local AppTimeD = GetAppTime() - lasttime;
   lasttime = GetAppTime();

   -- set ripple uv
   local uvofs = - g_ripple_position * ocean.getUVScale()/10000;
   uvofs = uvofs * g_ripple_uvscale
   local vUV = vec4.new( g_ripple_uvscale, g_ripple_uvscale, uvofs.x+0.5,-uvofs.z+0.5 )
   lake.setEffectVector('vecUVRipple',vUV)
   lake.setEffectFloat('fRippleStrength', (g_ripple_uvscale-0.5)*0.1)

   --- grow ripple ---
   g_ripple_uvscale = g_ripple_uvscale*( 1 - AppTimeD*2 )
end

PixelShader: 2.0

     

(Remark: if use  the lake.fx or lakeRipple.fx, you needs use Back Buffer Aligned render target for reflected / refractive / depth map, and do not check RTT's 'Consistent with back buffer' style.)

Workflow:

1. Create a surface ( or Ocean ) as water, and check it's bump style.

2. Create a portal on the surface ( no needed for Ocean )

3. Add two render targets
Right-Click in the Texture Browse Window, select "
Add Render Target" from the context menu. Then select a image file in Open File Dialog. Please ensure the image width is same as height (square image). Do same for another render target. ( use "Add Back Buffer Aligned > Render Target" if use Ocean )

4. Select render targets
Select the portal ( Ocean ), in the Render To Texture rollout of portal panel, select one render target; and check '
Consistent with back buffer' style ( no needed for Ocean ). Click the 'Actions' button, add two actions:

Message Function Object
Render Texture Begin Hide water surface
Render Texture End Show water surface

In the Render To Texture (Mirror) rollout, select another render target, and do same as above.

( also need depth map if use
lake.fx , see Render scene depth info to a texture )

5. Add bump texture
Right-Click in the Texture Browse Window, select 'Add Bump Texture' from the context menu. Then select "textures\wave_height.png" in Open File Dialog.

6. Create material
In the Material panel, new a material; set 1st texture to the bump texture; set 2nd texture to the render target in RTT(Mirror); set 3rd texture to the render target in RTT; load shader file "
effects\water\bump_nops.fx". ( or other shader, and matching textures setting )

6. Assign the material to water surface
Please ensure no texture assign to water surface. You can use UV Tool to adjust water surface's texture coordinates for changing wave size.

( if use bump_norefra_nops.fx, no need the 3rd texture; and only need set RTT(Mirror) in the portal. )

Tip: set  Clip Offset for RTT of portal to avoid seams near bank.