Custom Model Effect Sample

This sample shows how custom effects can be applied to a model using the XNA Framework Content Pipeline.

Sample Overview

This sample demonstrates how to use a custom effect to render a model with a static environment cube map, creating a shiny, reflective surface. The sample uses two custom content pipeline processors. The first processor applies the environment mapping effect to the model during the content build process. The run-time code does not then have to do anything special to render using the custom effect because the model will be loaded automatically with the new effect set up and ready to use. The second custom content pipeline processor converts a regular 2D photograph into a cube map that can be used for the environment mapping.

How the Sample Works

The EnvironmentMappedModelProcessor derives from the built-in ModelProcessor, and overrides the ConvertMaterial method to pass all the materials on the model through to the custom EnvironmentMappedMaterialProcessor.

EnvironmentMappedMaterialProcessor derives from the built-in MaterialProcessor, and overrides the Process and BuildTexture methods. Inside Process, it creates a new instance of EffectMaterialContent, then sets this to reference a custom effect and environment map texture. The base texture setting is copied across from the original material, before it chains to the base MaterialProcessor.Process.

The overload of BuildTexture checks which texture is being built. The environment map texture is passed to the custom CubemapProcessor, while all other textures go to the default base class implementation.

The CubemapProcessor converts arbitrary 2D photographs into static reflection cube maps. It does this by mirroring the image to make it tile seamlessly from left to right, wrapping the middle portion of it around the side of the cube, folding up the top and bottom to fill the top and bottom cube map faces, and then applying a blur to cover up the discontinuities at the very top and bottom. This technique provides a quick and easy way to turn any image into an environment map. For example, you could take a screen shot of your game and use it to reflect "typical" game surroundings in your shiny objects.

Note that although the environment-mapping effect is included as part of the Game Studio project file, it is not part of the content project. This effect will be built automatically because it is referenced by the EnvironmentMappedMaterialProcessor, so there is no need for it to be duplicated in the content project. It was only included here so you can easily locate the file for viewing or editing the effect code.