Welcome to MSDN Blogs Sign in | Join | Help

Flattening unwanted bones

Sometimes it is useful to have multiple bones and meshes inside a single model (see my previous post), but other times this just causes needless complexity. All that messing around with Model.CopyAbsoluteBoneTransformsTo and setting the effect World matrix is important if you want to do things like making your car wheels spin and the doors open, but it's a waste of time if you just want to draw a single static object without any internal animation.

If you happen to know that your source artwork does not contain any nested meshes with local transforms, you obviously don't need to bother looking up or setting the bone transforms. 

Often, though, you will find that the source artwork contains many unnecessary local transforms, just because that's the way the artist happened to build it. You could go back and tell them to change their data, but it is probably easier to fix this automatically using a custom content pipeline processor.

This example derives from the built-in ModelProcessor, overrides the main Process method, and uses the MeshHelper.TransformScene method to flatten out any local transform matrices in the input data: 

    [ContentProcessor]
    class FlattenTransformsProcessor : ModelProcessor
    {
        public override ModelContent Process(NodeContent input, ContentProcessorContext context)
        {
            FlattenTransforms(input);
         
            return base.Process(input, context);
        }


        static void FlattenTransforms(NodeContent node)
        {
            MeshHelper.TransformScene(node, node.Transform);

            node.Transform = Matrix.Identity;

            foreach (NodeContent child in node.Children)
            {
                FlattenTransforms(child);
            }
        }
    }

After running your model through this custom processor, all the bone matrices will be set to identity, so you can happily ignore them in your rendering code.

Published Wednesday, November 22, 2006 1:52 PM by ShawnHargreaves

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Thursday, November 23, 2006 8:54 AM by JoelMartinez

# re: Flattening unwanted bones

being able to plug in at this level, and with such ease makes things really nice.  Good stuff Shawn :-)

Thursday, November 23, 2006 9:23 PM by JudahGabriel

# re: Flattening unwanted bones

Cool, thanks for the info and the snippet. I'd enjoy more little tidbits like this, keep 'em coming. :-)

Wednesday, August 06, 2008 11:04 PM by Cyrille Lagarigue

# re: Flattening unwanted bones

For static geometry, I think it would be even more efficient to have an unique MeshContent object with a GeometryContent per material.

It would require to build a new mesh using the previous mesh data. Is it a simple way to do that (using MeshBuilder for example) ?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker