Water Shader Effect

TrueVision3D 6.5 comes with a nifty HLSL water shader built into the TVGraphicEffect class. While it seems confusing to set up, once you understand it is is very easy. This was one of the first things that I tried to set up when I first got the beta, and I would like to create a full, in-depth tutorial covering the process so that it is easier for others. The tutorial’s code and sample project are in VB2005, but the concept applies to all languages. This tutorial was written with the fact that you already have a basic TV setup running, such as the Template from the beta page.

Firstly, some basic variables need to be declared, which include the GraphicEffect class, a Reflection surface, a Refraction surface, a Water Mesh, and a WaterPlane. TGlobals is included for later use.

Public GraphicEffects As New TVGraphicEffect
Public ReflectRS as TVRenderSurface
Public RefractRS as TVRenderSurface
Public WaterMesh as TVMesh
Public WaterPlane as TV_PLANE
Public TGLobals as New TVGlobals

Next, the two rendersurfaces need to be initialized. Notice that we also set the background color to a bluish color, which is shown at rare times when the camera is inside the water mesh. Feel free to change the color. This should be done in your initialization, as well as the following few segments of code up to the actual rendering.

ReflectRS = Scene.CreateRenderSurfaceEx(-1, -1, CONST_TV_RENDERSURFACEFORMAT.TV_TEXTUREFORMAT_DEFAULT, True, True, 1)
ReflectRS.SetBackgroundColor(TGlobals.RGBA(0, 0, 0.1906, 1))

RefractRS = Scene.CreateRenderSurfaceEx(-1, -1, CONST_TV_RENDERSURFACEFORMAT.TV_TEXTUREFORMAT_DEFAULT, True, True, 1)
RefractRS.SetBackgroundColor(TGlobals.RGBA(0, 0, 0.1906, 1))

After, the water mesh needs to be created and a floor needs to be added, but first a DUDV texture, which maps out the water ripples, must be loaded. The Amplitude parameter, here a 25, controls how much depth the ripples will seem to have. I have included a good DUDV texture in the sample download. You can freely customize the dimensions and altitude of the floor. Also, take note of the Tile values at the end, which are here set to 2. The larger these values, the smaller the ripples will be.

Dim iDUDV As Integer = TextureFactory.LoadDUDVTexture("C:\MyTexture.bmp", "DUDV", -1, -1, 25)

WaterMesh = Scene.CreateMeshBuilder()
WaterMesh.AddFloor(iDUDV, -256, -256, 256, 256, -3, 2, 2)

Next, the water plane needs to be created. The dist should be set to -Altitude of the watermesh. So, if the mesh’s altitude is 5, the dist will be -5. The normal is always 0, 1, 0.

WaterPlane = New TV_PLANE(TGlobals.Vector3(0, 1, 0), 3)

Finally, we need to apply the rendersurfaces, the mesh, and the plane into a water effect. Note that we apply 0 to the style property, meaning that it will be basic water. Other styles are not covered in this tutorial. Also note that SetWaterReflectionColor can be used to change the color of the reflection / refraction. This should be done after the code below.

GraphicEffects.SetWaterReflection(WaterMesh, ReflectRS, RefractRS, 0, WaterPlane)

Now, each frame before calling Tv.Clear/Tv.RenderToScreen, the water surfaces must be updated.

ReflectRS.StartRender

'Render all stuff here, excluding the water mesh

ReflectRS.EndRender
RefractRS.StartRender

'Render all stuff here, excluding the water mesh
RefractRS.EndRender

Finally, render the watermesh itself inbetween the Tv.Clear / TV.RenderToScreen calls.

WaterMesh.Render

Water Shader Effect Project: http://pizzayoyo.smithbower.com/other/WaterSample.rar Make sure to enter your beta key and copy the latest MTV3D65.dll into the bin\x86\release folder!

Picture:

 
tutorialsarticlesandexamples/using_tv3d_s_water_shader_effect.txt · Last modified: 2013/11/22 13:32