27. January 2011 07:10
XNA . Tutorial
Since XNA Game Studio Express 1.0 (later the Express was removed from the name) we have had the built-in BasicEffect. This has allowed developers to create games without actually needing to learn HLSL. HLSL is awesome, so I’d highly encourage you to learn it. It is not only beneficial in XNA but also in Silverlight and WPF. Regardless, if someone is just getting started learning HLSL can slow down the process of getting something on the screen. Microsoft decided to create the BasicEffect to handle scenarios where creating custom shaders in HLSL is not needed or maybe just not wanted to begin with.
In XNA Game Studio 4.0 Microsoft has added new built in effects. Currently, Windows Phone 7 does not provide support for custom shaders. The team found that many scenarios can be handled with the built in effects they built. They are: BasicEffect, DualTextureEffect, AlphaTestEffect, EnvironmentMapEffect, SkinnedEffect.
This post will discuss the DualTextureEffect. BasicEffect allows you to use a single texture. As expected the DualTextureEffect allows you to use two. It allows you to blend 2 textures together. A typical purpose for using this texture is to use Light Maps. The idea behind light maps is to basically figure out your lighting at build time and store those to a texture. Then apply that light texture to the scene to make it look like you have complex lighting even though none exists.
To actually use the effect is very straightforward. Simply instantiate the effect and set the camera up normally, set the Texture and Texture2 properties on the effect and apply the effect to the model. Texture will most likely be your color map (colored texture for your model) and Texture2 will most likely be your light map. If your second texture was all black, then the resulting image that will be applied to the model will be all black. If the second texture is all white, then it will use the color as it exists from the other texture. Any other color will blend – 50% gray will have the color unchanged, > 50% gray and the color becomes brighter, < 50% gray and the color becomes darker. Shawn Hargreaves has a great article on this topic.
To see Microsoft’s HLSL code in how they created these effects, take a look at the stock effects sample: