Flattening unwanted bones

Flattening unwanted bones

  • Comments 3

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.

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

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

  • 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) ?

Page 1 of 1 (3 items)
Leave a Comment
  • Please add 8 and 3 and type the answer here:
  • Post