At this point the astute reader may be wondering what the
ModelMesh.Effects property is all about. You thought I'd forgotten to
explain that, didn't you? Not in the least…
The actual effect
instances used to render your model are stored in the Effect property of
the ModelMeshPart class. This is inevitable, really, since each
ModelMeshPart can potentially have a different effect. If you want to
change the effect used to render your model, you will need loop over
each ModelMeshPart and assign a new value to their Effect property.
The
ModelMesh.Effects property is just a shortcut that gives you a combined
list of all the effects used on all the parts of the mesh. This allows
you to draw an entire mesh using this code:
Matrix[] transforms = new Matrix[model.Bones.Count]; Model.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh mesh in model.Meshes) { foreach (BasicEffect effect in mesh.Effects) { effect.World = transforms[mesh.ParentBone.Index]; effect.View = viewMatrix; effect.Projection = projectionMatrix; } mesh.Draw(); }
This is really just a shortcut for:
Matrix[] transforms = new Matrix[model.Bones.Count]; Model.CopyAbsoluteBoneTransformsTo(transforms); foreach (ModelMesh mesh in model.Meshes) { foreach (ModelMeshPart part in mesh.MeshParts) { if (part.Effect == null) throw new IneffectiveException("Huh?"); BasicEffect effect = (BasicEffect)part.Effect; effect.World = transforms[mesh.ParentBone.Index]; effect.View = viewMatrix; effect.Projection = projectionMatrix; } mesh.Draw(); }
But the first version is better because: