Shawn Hargreaves Blog
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:
PingBack from http://www.xnatutorial.com/?p=45
That's very good for beginners, like me. Thanks again.
This saved me many hours of hunting and head-scratching. Microsoft should have this explanation someplace conspicuous in their documentation. But, of course, they don't.
Thanks a million!
I love you!
Hello Shawn
How does XNA set the bone order when it imports a FBX model?
I would love to draw a bone after another, since one of the bones uses alpha transparent textures and I would like to draw these last.