Windows Phone 7 Game Development: Adding Lighting to 3D Games With XNA
Windows Phone 7 games are written with XNA Game Studio 4.0. The phone profile doesn't allow custom shaders, so lighting comes from XNA's built-in effects, mainly BasicEffect. Used well, it makes 3D scenes look far more convincing.
Normals are required
Lighting depends on surface normals. Use vertex types that include them, such as VertexPositionNormalTexture, or models exported with normals. Without normals, lit objects appear black or flat.
Enabling default lighting
The quickest option:
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = world; effect.View = view; effect.Projection = projection;
}
mesh.Draw();
}
EnableDefaultLighting sets up a standard three-light rig with ambient light, which looks good for most objects.
Configuring lights yourself
BasicEffect supports three directional lights plus ambient light:
effect.LightingEnabled = true;
effect.AmbientLightColor = new Vector3(0.2f, 0.2f, 0.2f);
effect.DirectionalLight0.Enabled = true;
effect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-1, -1, -1));
effect.DirectionalLight0.DiffuseColor = new Vector3(1f, 0.95f, 0.8f);
effect.DirectionalLight0.SpecularColor = new Vector3(0.5f);
effect.SpecularPower = 32f;
- Ambient light lifts dark areas so nothing is completely black.
- Diffuse light gives shape depending on the angle to the light.
- Specular adds highlights; higher
SpecularPowergives smaller, sharper highlights.
Per-pixel lighting
PreferPerPixelLighting = true calculates lighting per pixel for smoother results on curved surfaces, at a higher GPU cost. On phone hardware, test frame rate before enabling it everywhere; use it for the main character and keep per-vertex lighting for background objects.
Other built-in effects
- EnvironmentMapEffect for shiny, reflective surfaces,
- DualTextureEffect for pre-baked light maps, a cheap way to get detailed static lighting,
- SkinnedEffect for animated characters, with the same lighting model.
Fog
Combine lighting with FogEnabled, FogStart, FogEnd and FogColor to fade distant objects and hide the far clipping plane.
Today's equivalent
Windows Phone and XNA are discontinued. MonoGame keeps the XNA API alive across platforms, and modern mobile engines such as Unity offer real-time lighting with physically based materials.