1. Code

2D Scene Lighting

Screenshot of normal-mapped lit scene

This sample XNA project demonstrates a method of using normal mapping to light a scene rendered entirely in 2D in such a way that it appears 3D.

Normal mapping is a technique that uses an image's R, G, and B channels to encode the direction of the surface normals of each pixel in that image. This sample achieves the lighting effect by first rendering the normal scene's sprites to an image, then rendering the corresponding normal maps to a second image in a second pass. These two images, as well as some other information like the location and color of the light, are passed into a custom HLSL shader, which decides how much effect the light should have on each pixel based on how much its normal faces toward the light.


This code is not intended to provide a usable library or functionality, but to demonstrate a concept. I am working on a more fully-featured engine in Camera Obscura, which includes ambient light, directional light, multiple point lights, and specular mapping, and I may release it as a library at some point.

It also is a little heavy in that it makes use of my comprehensive sprite animation library to play the running loop (the library is included in the project).


You are free to use and implement this code, explicitly the code in this file (Game1.cs), the shader (lighting.fx) and sprite libraries (SpriteSheet.cs and the SpriteSheetPipeline project), in your own projects.

You may NOT use any of the art (.PNG) resources provided for this demonstration.


The XNA project for VS2010

Comments, bug reports, questions? Send 'em over! forms@brianmacintosh.com