Material Effects

Material effects help you use all the rendering capabilities of the 3D hardware. Effects are a collection of different rendering techniques that can fit onto a variety of hardware devices. This enables you to not only to program games that make optimum use of video card functionality - effects also make it easy to upgrade an existing game to run on newer video cards as additional features are developed.

You must be familiar with the texture stage concept of DirectX for defining effects. Therefore, writing effects is only a matter for advanced users, although it's fun to try out the different render states and texture operators, and look which effects they produce on the screen. However, once an effect is properly written it can be used by anyone. Effects are split into three main sections:

Variable declarations - optional values that are set before rendering and then used in this effect, for instance textures, the world matrix, or lighting parameters.
Techniques and passes - they define how something is to be rendered, and include state information along with vertex and shader declarations.
Functions - optional shader code written in shader assembler or HLSL (see Shaders).

Variables

Variables are defined at the start of the effect file. You must specify a type and a unique name. You can then provide specific values depending on the type specifying the usage, user defined data and initial values. Example:

float4 diffuse : DIFFUSE = { 0, 0, 0, 1 };

float4 is the type (a floating point vector with 4 components), diffuse is the name. After the colon is a tag specifying the usage, in this case it is to be used as a diffuse colour. Finally the value is initialised to 0,0,0,1 (red, green, blue, alpha).

float4x4 matWorld : WORLD;
float4x4 matView : VIEW;
float4x4 matProj : PROJECTION;

This declares three matrices with the names matWorld, matView and matProj that will be used for the world, view and projection matrix during this effect. Those matrices are already predefined by the engine, and automatically set to the world, view and projection matrix of the object to be rendered. You'll find a list of all predefined effect variables in the material chapter.

Techniques

An effect contains one or more techniques. Each technique consists of one or more passes. Each pass consists of a setting of one or more texture stage registers of the 3D hardware, and optionally a pixel or vertex shader that redefines the behavior of the stage. The object is rendered once for each pass, using the given settings.

If a certain technique does not work because the 3D hardware does not support the given texture stage states, the next technique from the effect is automatically selected. Therefore an effect should always contain a simple fallback technique for supporting old hardware. For example, to create a realistic rippled pond of water that reflects light, you begin with the first technique that renders the water, adds specular highlights, adds caustic textures, and applies light to the water in a single pass. If your hardware cannot render this technique in a single pass, a second technique might render the water, add specular highlights or caustic textures, but not apply light to the water.

Special technique names

A technique is defined with the keyword technique followed by an optional unique name, e.g.

technique BumpMapping

Techniques can have arbitrary names, but some names have a special meaning for the engine:

Passes

Within the technique you can define the passes which contain the actual state assignments. In a pass you can set states to values or expressions. They are set in a very similar way as in the script, for instance:

ColorOp[0] = SelectArg1;

LightEnable[0] = true; // turn on light 0
ZEnable = true; // turn on depth tests

All the available states and options can be found in the DirectX documentation. So an example of two techniques, the second having two passes, could be:

technique FirstTechnique
{
   pass P0
   {
   // Set states here for pass 0
   }
}

technique SecondTechnique
{
   pass P0
   {
   // Set states here for pass 0
   }

   pass P1
   {
   // Set states here for pass 1
   }
}

Example for a simple texturing technique:

texture entSkin1; // Variable declaration

technique textureTechnique
{
   pass p0
   {
     Texture[0] = <entSkin1>;
   
     MinFilter[0] = Linear;
     MagFilter[0] = Linear;
     MipFilter[0] = Linear;
   
     ColorOp[0] = Modulate;
     ColorArg1[0] = Texture;
     ColorArg2[0] = Diffuse;
     AlphaOp[0] = SelectArg2;
     AlphaArg1[0] = Texture;
     AlphaArg2[0] = Diffuse;
   }
}
The texture is assigned to stage 0, then the filtering is set to bilinear. The texture is then modulated with the diffuse color by setting the color operators.

The above example did not include any shader code - it is a so-called Fixed Function Pipeline (FFP) effect. For information on writing shaders please see the Shader section of this manual and especially the Shader Workshops.

Edition

 C   P 

See Also:

MATERIAL, material.effect, Shaders

► latest version online