Game Development Community

dev|Pro Game Development Curriculum

Adding Water to Torque X

by John Kanalakis · 06/12/2009 (12:23 pm) · 2 comments

Here is a a simple ocean shader effect for Torque X. It is composed of a single shader effect file that is then wrapped by a custom material class. This custom material is then applied to a 3D plane. It's only an ocean surface shader, so there's no 'underwater' effect. The water is very customizable from flow speed to wave amplitude (height), color, etc.

Here's a preview of how it looks in the plain StarterGame3D level:



It still needs some improvement, but not bad for a first crack at it. Here are the details with links to the source code and assets at the bottom.

The Custom Material


The WaterMaterial class is a custom material that descends from the GenericMaterial class. Its constructor points to the Ocean shader effect file and sets the shader values in the _LoadParameters() method. And there are a lot of parameters that can be set to customize the look of the water surface. This material definition also passes instances of a cubemap for the reflective water surface and the normal map for the wave definition into the shader.

The Water Object


The WaterPlane object is a basic TorqueObject descendant that creates a renderable T3DBox and places it within the game scene. It also associates the WaterMaterial with this object in order to render the water effect. It exposes a dozen or so properties that let you easily set the values on the water shader, such as width, height, wave amplitude, flow speed, etc.

Using the Water Plane

You can use the water plane like any other TorqueObject, by creating an instance of it, setting some properties, and then registering it with the TorqueObjectDatabase.

WaterPlane water = new WaterPlane();
water.WaterLength = 1024;
water.WaterWidth = 1024;
water.NormalMapFilename = @"dataimageswaves2";
water.EnvironmentMapFilename = @"dataimagescubeMap";
T3DSceneComponent componentScene = new T3DSceneComponent();
componentScene.Position = new Vector3(512, 512, 255);
water.Components.AddComponent(componentScene);
TorqueObjectDatabase.Instance.Register(water);

You can download the source code and related files from here.

Have fun at the beach!

John K.
www.envygames.com

About the author

John Kanalakis is the owner of EnvyGames, an independent game development studio in Silicon Valley that produces games and tools for Xbox 360, Windows, and the Web.


#1
06/13/2009 (12:15 am)
I see you have started getting ready, for TX3D game blog?
#2
06/16/2009 (5:18 pm)
John, you never cease to amaze. Will add this to the TDN community links section.

Brian