RimLighting Sample for Windows Phone 7
This sample demonstrates how to implement a rim lighting effect on Windows Phone 7.Sample Overview
Rim lighting is a common technique used in photography and computer graphics. The technique puts a light source on the opposite side of the object and away from the camera. This positioning is used to highlight the silhouette of the object and to better separate it from the background.
The sample also provides a simple arcball class and a ModelViewerCamera class. The arcball class handles rotations used internally in ModelViewerCamera class. ModelViewerCamera actually uses two arcballs: one for rotating the object in the World, and one for rotating the camera around the object. When the camera is rotated around the object, its position changes, but the camera is always looking at the object. This is similar to a satellite circling earth whose antenna always faces the earth.
Playing with the Sample
Once the sample starts you can drag on the screen to rotate the object. On the bottom of the screen there is a button to toggle between world rotation mode and camera rotation mode.
- In world rotation mode, only the object is rotated in world space. The position and direction of the light (meaning the normal light, not rim light, in the scene) and camera in world space remains the same.
- Because the light does not rotate with the object, the portion of the object located in *certain area of the screen* is always brighter.
- In camera rotation mode, only the position of the camera is rotated in world space. The position of light and object in world space remain unchanged.
- Because the light does rotate with the object with respect to the camera, we can see that *certain area of the object* is always brighter.
In either mode, the silhouette of the object is always correctly lit by the rim light.
The two sliders on the screen can be used to tweak the look of the rim light effect. The amount slider controls the weight of the effect, and the thickness slider controls the thickness of the effect.
How the Sample Works
On an Xbox 360 console or a PC, a rim lighting effect usually is implemented by using programmable shaders. On Windows Phone 7, however, there is only five fixed, but configurable, effects. Fortunately, with some mathematical tricks, the Environment Map Effect is close enough to mimic a rim lighting effect.
The key problem is that environment lighting is computed in world space, and rim lighting is computed in view space. The solution should be to fake all the matrices so that world space is replaced by view space, but screen space stays the same. This could be done by assigning:
matModelToWorld <— set to matModelToView
matWorldToView <— set to matIdentity
matViewToScreen <— set to matViewtoScreen
These assignments will cause the environment map lookup to use a view space vector, while leaving the overall position transform the same. Next, the cube map should be created so that all faces, except the back face, are dark. A bright color should be used on the back face.
If the scene has other lights, these lights also have to be transformed to view space. Such a transformation could be done by:
vLightPositionWorld <— set to vLightPositionView
vLightDirectionWorld <— set to vLightDirectionView
Finally, if there is an object where the rim lighting effect is to be applied only to certain parts, we could separate the object into different submeshes, and then render each independently. In this sample, the bottom of the neck is open, so it does not receive rim lighting.