Wednesday, April 27, 2011

Skeletal Animation – An overview


I have been meaning to write a series of blog entries around the basics of skeletal animation for a while now but have been unfortunately too busy to do so – until now, so let’s put our learning hats on!

When you need to animate a 3D object in a game or simulation there are 3 primary ways to accomplish the task:

(1)Hierarchical object animation using keyframes
(2)Vertex Animation
(3)Skeletal Animation

Hierarchical object animation is when you have multiple meshes which are, typically, arranged in a parent-child fashion through some sort of scenegraph.  Animation data is typically stored as separate translation and rotation (and sometimes scale) components and each object is animated relative to its local frame of reference.  This works fine for when the object is something like a helicopter and you need to rotate the main and tail rotors; however, when trying to articulate a more complicated and less segmented logical object such as a humanoid character, animation can only occur on the transforms for the object – no vertex data can be changed.  This means that constructing a human out a mesh hierarchy (with a head mesh, neck mesh, chest mesh, upper arm mesh, et cetera – all the appendages you wish to articulate) leads to block or segmented animation because, for example, the forearm object isn’t really connected to the upper arm so that when it is animated the ends of the objects don’t touch completely and your characters looks like they were created out of blocks.  Interstate 76, an amazing old game which I still occasionally replay just for the cut scenes, was animated in this fashion.  Suffice it to say that since that time animators and game developers have been looking for solutions with better visual fidelity.

Vertex animation is when a mesh is manipulated on a per vertex level by comparing one complete set of the mesh’s vertices against another complete set of the mesh’s vertices (each of the sets of vertices representing a particular keyframe of animation data) and interpolating a position between the two based upon how far between keyframe times the current animation clock is.  This approach gives the artist the most creative control and the greatest ability to animate; however, it is very memory intensive and requires art content files that are very large since each keyframe of animation is a complete snapshot of the mesh at a given point and time.  Quake’s MD2 file format is an example of vertex animation – another problem with vertex animation is that it is difficult to apply multiple simultaneous animations to a mesh being animated in this fashion.  Since the animation data is a complete snapshot of the mesh each animation stomps all over the previous one that was applied; now, there are solutions to this that modify vertex animation so that you are using ‘relative vertex animation’ data and you can combine and blend somewhat (and save art content file space by discarding unchanging vertices on a given keyframe) but ultimately as your models scale up in complexity, vertices, and polygons this approach has diminishing returns.  Vertex animation is still a very important approach to the non real-time rendering world; however, in the real-time arena it is rarely seen except for extremely complicated situations and sometimes for facial animation or lip synching.

Skeletal animation is a hybrid of the previous two methods.  The mesh representing the object is not manipulated by interpolating between two complete sets of vertex data (as is the case in vertex animation) but instead by moving treating the vertices of the mesh as if they were the ‘skin’ of the mesh and moving the skin to fit over a skeleton.  The skeleton is like the hierarchical object animation from the old days except that the skeleton, just like the skeleton in your body, is a hierarchy of ‘bones’ (sometimes referred to as ‘joints’) and even though you animate the skeleton, you never actually bother to draw it.  This is because we only want to animate the skeleton because we’re going to force the skin to go along with it.  Half-Life is an early example of a game that used skeletal animation.  Why don’t we simply animate the skin this way?  For all the reasons why we just explained that vertex animation is no longer in favor in the real-time world.  By only storing the rotation and translation (potentially other factors as well) data for each bone for a keyframe of animation we save tons of space in our art content files, and we gain the ability to simply combine animations and blend between them.  We gain performance advantages over complicated relative vertex animation systems when the model’s topology scales (more polygons, more vertices), and we gain the ability to add FK/IK capabilities into our system that would be virtually impossible with vertex animation (any system I can imagine to support this via vertex animation would ultimately require you emulating a skeletal system.)

So, now that I have explained the reasoning why I, and many others, use skeletal animation, let’s start explaining how it works.

The good news is that skeletal animation is actually VERY simple.  The bad news is that it usually either partially explained or people use different terms for differing things.  I will do my best to explain it as simply and yet thoroughly as possible.  The other bad news is because the world is an imperfect place, obtaining an example of skeletal animation data you can use can be difficult because you’re just now learning how it is supposed to work so it can be difficult to discern why art content produced by your artist doesn’t seem to work (is it you?  Is it the artist?  Is it his/her exporter?  Et cetera.)

Luckily, I will provide you with an example COLLADA XML file (*.dae) that has a walk animation included.  We will use the COLLADA format because there are moderately usable exporters for all the major art pipeline tools (Maya/Max/XSi) and your art guy/gal will most likely have experience with one of those tools.

So, ultimately, when this series of posts is over, you’ll be able to import a COLLADA XML file, pull out the mesh geometry, pull out the skeleton, and pull out the animation data, then play it back in your application.