XNA How To: Draw a Model with a Custom Effect

Drawing a Model With a Custom Effect

When you load a Model, the model is set by default to use the BasicEffect. You can change this by customizing the content pipeline, or you can apply a newEffect to the Model when you load the Model.

To draw a model with a custom effect

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

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

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

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

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

你可能感兴趣的:(XNA How To: Draw a Model with a Custom Effect)