BasicEffect Shader
Provides the HLSL source code for the BasicEffect shader that comes built into the XNA Framework.Overview
This code is provided for educational purposes. It may be a useful starting point when you create more advanced shaders of your own.
Note that although the BasicEffect class is simple and easy to use, the shader code behind it is actually quite complicated! This is because BasicEffect supports many rendering options within a single shader. It enables you to provide toggle texturing, vertex colors, and lighting on or off, and to select per-vertex or per-pixel lighting. To support all the possible permutations of these options, BasicEffect ends up needing no less than 12 different vertex shaders and four pixel shaders, all of which are combined within this single effect file.
If you are looking for an easy starting point to learn shader programming, you would probably be better off with the Shader Series samples, which are simpler than BasicEffect because they do not include so many adjustable options.
Using the Shader
To use this shader, at minimum you must set the World, View, and Projection matrix parameters, plus the EyePosition vector parameter (which is specified in world space). This will produce untextured, unlit, and flat color triangles.
To alter the rendering mode, change the ShaderIndex parameter to one of these values:
0 | Flat color |
1 | Vertex colors |
2 | Textured |
3 | Textured + Vertex colors |
4 | Lit |
5 | Lit + Vertex colors |
6 | Lit + Textured |
7 | Lit + Textured + Vertex colors |
8 | Per-pixel lit |
9 | Per-pixel lit + Vertex colors |
10 | Per-pixel lit + Textured |
11 | Per-pixel lit + Textured + Vertex colors |
If you choose a mode that includes texturing, use the BasicTexture parameter to specify the texture.
If you choose a mode that includes lighting, use the following parameters to configure the lights:
- AmbientLightColor
- DirLight0Direction
- DirLight0DiffuseColor
- DirLight0SpecularColor
- DirLight1Direction
- DirLight1DiffuseColor
- DirLight1SpecularColor
- DirLight2Direction
- DirLight2DiffuseColor
- DirLight2SpecularColor
There is no direct way to disable individual directional lights. Instead, simply set the colors of unwanted lights to zero.
To adjust material properties, use these parameters:
- DiffuseColor
- EmissiveColor
- SpecularColor
- SpecularPower
- Alpha
Note that color values range from 0 to 1, not 255!
To enable fog, set these parameters:
- FogEnabled
- FogStart
- FogEnd
- FogColor
FogEnabled should be set to 0 to disable fog, or 1 to enable it.