Now lets create a model animator for the dwarf that will allow us to view the dwarf and animate it.
Viewing the Dwarf with a ModelAnimator object
- First, create two member variables:
// Add these as member variables
ModelAnimator dwarfAnimator;
Matrix view;
- Load the dwarf model and create a new instance of the animator in the LoadGraphicsContent method.
- The ModelAnimator will render the model automatically by default, but we do need to set the view and projection matrices for the effects associated with the model.
- Here is an example of what your LoadGraphicsContent should look like after setting the effects. Notice that because the dwarf is skinned, it will be loaded with BasicPaletteEffect:
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
Model model = content.Load<Model>("dwarfmodel");
dwarfAnimator = new ModelAnimator(this, model);
Viewport port = graphics.GraphicsDevice.Viewport;
view = Matrix.CreateLookAt(
new Vector3(0, 15, -20), Vector3.Zero, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.PiOver4, (float)port.Width / port.Height, .1f, 100000f);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicPaletteEffect effect in mesh.Effects)
{
effect.View = view;
effect.Projection = projection;
}
}
}
}
Possible Questions
- Why do I have to set the View and Projection matrices but not the world matrix for ModelAnimator?
- How do I override the ModelAnimator's automatic rendering and draw the dwarf when I want to?
- What is a "skinned" model and how do I know when BasicEffect/BasicPaletteEffect/My own effects are loaded from the content pipeline?