Effect States (XNA Game Studio 4.0 Programming)

Topic 7 discusses setting the different device states using the different state objects. Changing these states requires your game code to create different state objects and to set them on the device.

Objects lit by a point light source

Figure 8.19 Objects lit by a point light source

Effect files have the capability to also set these states within their effect pass blocks. When your game calls EffectPass.Apply, if the effect pass contains any state changes, then they will be set on the device.

The default effect file template that is used when you create a new effect file in XNA Game Studio add a nice comment to let you know where you can set render states in the effect pass.

tmp14-74_thumb


There are many different states that can be set and the naming of the states and their values defers from the XNA Game Studio state objects.A listing of all of the states and values that are supported by an HLSL effect can be found at the following link:

http://msdn.microsoft.com/en-us/library/bb173347(VS.85).aspx

Note

XNA Game Studio limits some states and values that can be set in order to conform to the Reach and HiDef graphics profiles. This validation occurs when the effect file is built using the content pipeline. Reading any error from the build informs you of any states that are not supported in XNA Game Studio.

Alpha Blending Using Effect States

You can create an example of how to utilize the effect states by creating a custom effect that uses alpha blending to render the transparent objects.You utilize the existing specular sample that you created previously.

First, load a different model to draw.You use a model that has three separate objects that are close together so that when you make them transparent, you will see through each of them into the others. Update the LoadContent method with the following line of code.Also, add the model to your content project.

tmp14-75_thumb

To display the models with some transparency, update the diffuse colors to contain an extra alpha channel.The diffuseColor array needs to be updated to a Vector4 from the current Vector3.Then in the Initialize method, update the diffuseColor array with the following values:

tmp14-76_thumb

Unlink previous effects from this topic—the effect you will create has multiple passes.This means that you will draw the geometry of the models multiple times, once for each EffectPass. Update the section of the Draw method, which calls Apply on the EffectPass and draws the primitives that make up the model with the following.

tmp14-77_thumb

Because you have more than one pass, loop over all of the passes calling Apply and then drawing the indexed primitives that make up the ModelMeshPart you are currently drawing.

Now in the effect file, you need only a few small changes. The first is to update the pixel shader to use the w component of your diffuse color for the transparency value of the pixel.

tmp14-78_thumb

Now, update the existing effect pass and add another.

tmp14-79_thumb

Because you draw 3D models with transparency and you want to be able to see through them, you want to see the inside of the back of the object.Although you normally cull the backward facing triangles, update the cull mode to clockwise so that you will draw the inside triangles. The first effect pass also sets the normal alpha blending states. In the second effect pass, the cull mode is set back to the default counterclockwise so that you can draw the front-facing triangles. If you didn’t draw the inside of the objects or you drew the front first, the blending would not look correct if you want the objects to appear to be transparent and to have volume.

The states set by the effect file persists until they are changed either by state objects in the game code or by another effect pass, which defines the state.

Running the sample now displays two spheres with a cylinder between them. Figure 8.20 shows the three objects and how the different transparencies among them enables you to see through the objects to what is behind them.

Effect states used to draw multiple models with alpha blending

Figure 8.20 Effect states used to draw multiple models with alpha blending

Summary

In this topic, we covered the basics of creating your own effect files from the different parts of the effect file including the pixel shader, vertex shader, and the techniques and passes.We also covered how to use both vertex colors and textures to color the triangles you draw.

An introduction to different lighting models was covered with numerous samples that built on each other to create useful lighting effects.

Finally, effect states were discussed and how they can be used instead of state objects in your game code to set render states.

This topic contained many examples in which can be found in the accompanying code files that can be downloaded for this topic.

Now that you have a better understanding of what custom effects are and how to use them, you should be able to develop your own effects and be able to better understand other types of shading techniques.

Next post:

Previous post: