Export (0) Print
Expand All

How To: Draw a Model with a Custom Effect

This example demonstrates how to load a Model file and draw the Model using a custom Effect without modifying the Content Pipeline.

The Complete Sample

The code in this tutorial illustrates the technique described in the text. A complete code sample for this tutorial is available for you to download, including full source code and any additional supporting files required by the sample.

When a Model is loaded, it is set by default to use the BasicEffect. This can be changed by customizing the Content Pipeline, or a new Effect can be applied to the Model when the Model is loaded.

To Draw a Model with a Custom Effect

  1. In your game's LoadContent method, load your Model normally using the ContentManager.

    terrain = Content.Load<Model>("terrain");
    
  2. In LoadContent, load your Effect normally using the ContentManager.

    MyEffect = content.Load<Effect>("CustomEffect");
    
  3. Next, iterate through each ModelMeshPart in your model, and assign your Effect to the Effect property of the ModelMeshPart.

    public static void RemapModel(Model model, Effect effect)
    {
        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (ModelMeshPart part in mesh.MeshParts)
            {
                part.Effect = effect;
            }
        }
    }
    
  4. Lastly, draw the Model normally using the steps outlined in How To: Render a Model, except instead of using BasicEffect, use the Effect attached to the model.

    foreach (ModelMesh mesh in terrain.Meshes)
    {
        foreach (Effect effect in mesh.Effects)
        {
            mesh.Draw();
        }
    }
    

Community Additions

ADD
Show:
© 2014 Microsoft