Converting a DrawModel() using BasicEffect to one using Effect
- by Fibericon
Take this DrawModel() provided by MSDN:
private void DrawModel(Model m)
{
Matrix[] transforms = new Matrix[m.Bones.Count];
float aspectRatio = graphics.GraphicsDevice.Viewport.Width /
graphics.GraphicsDevice.Viewport.Height;
m.CopyAbsoluteBoneTransformsTo(transforms);
Matrix projection =
Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
aspectRatio, 1.0f, 10000.0f);
Matrix view = Matrix.CreateLookAt(new Vector3(0.0f, 50.0f, Zoom),
Vector3.Zero, Vector3.Up);
foreach (ModelMesh mesh in m.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.View = view;
effect.Projection = projection;
effect.World = gameWorldRotation *
transforms[mesh.ParentBone.Index] *
Matrix.CreateTranslation(Position);
}
mesh.Draw();
}
}
How would I apply a custom effect to a model with that? Effect doesn't have View, Projection, or World members. This is what they recommend replacing the foreach loop with:
foreach (ModelMesh mesh in terrain.Meshes)
{
foreach (Effect effect in mesh.Effects)
{
mesh.Draw();
}
}
Of course, that doesn't really work. What else needs to be done?