Sign in to follow this  
spacerat

GPU Skinned Skeletal Animation Tutorial

Recommended Posts

Ashaman73    13715

Today I completed a skinned skeletal animation tutorial, which is very helpful if you are just about to start with game development.

If you just started with game dev you should avoid skeletal animation for a looonnng time smile.png

Nvm, I really appreciate that someone took the time to write an article about this horrible topic wub.png . I just want to point out some stuff, you could consider if you want to update your tutorial in the future.

 

 

 


vec3 weights= gl_Color.xyz;
vec3 boneid = gl_Vertex.w * vec3( 1.0/128.0 , 1.0 , 128.0 );
boneid = (boneid - floor(boneid))*128.0;

mat4 mskin = bones[int(boneid.x)]*weights.x+
bones[int(boneid.y)]*weights.y+
bones[int(boneid.z)]*weights.z;

Some thoughts:

1. I would use custom attributes in times of shaders, no need to reuse all this fixed pipeline stuff, atleast to transport the bone weights. It would be much clearer.

2. Encoding the bone ids in the vertex.w coord is a little bit hacky smile.png A float component has 32bit, so why not use a standard 4x8bit channel. This way it is by far more robust, no magic needed, you get access to 255 bones and it uses the same bandwidth.

3. The same for the weights. Test is out with a 4x8bit channel. You have 4 channels and precision down to 0.5% for skinning, which is enough for me. If you still have issues with the precision, take 4x16bit channel, which is still less bandwidth and supports 4 channels instead of just 3.

 

Advanced:

4. If you have space for 128 bones, using matrix uniforms, then  you could use a combination of a quaternion (4 component) + position (3 component). This way you can use up to 256 bones for the same uniform param space. Btw. you dont need to tranform the quanterion into matrix in the vertex shader, you can rotate the incoming vertex directly by the quaternion.

Edited by Ashaman73

Share this post


Link to post
Share on other sites
spacerat    1182

Ashaman, thank you very much for your detailed reply! 

 

 


If you just started with game dev you should avoid skeletal animation for a looonnng time smile.png

 

Well, thats true from the difficulty point of view happy.png  

On the other hand, character animation is the most crucial thing that you need right at the beginning to get your game started.

Considering just having character animation and a flat plane with a few boxes (no offense to minecraft here smile.png ) already makes you having a game.

 

For making the class easy to use also for ppl just getting started with GDev, I have modified the tutorial description on top.

It also includes sample code for drawing a character, getting a bone matrix and setting a bone matrix.

 

 

 


Some thoughts:
1. I would use custom attributes in times of shaders, no need to reuse all this fixed pipeline stuff, atleast to transport the bone weights. It would be much clearer.

 

Hm.. that would require a major re-write from display-list based to VBO based.

I am not sure it would reduce the number of code-lines, but could be clearer indeed.

I cannot promise to do it anytime soon, but I certainly will post an update in case its done.

 

 


2. Encoding the bone ids in the vertex.w coord is a little bit hacky A float component has 32bit, so why not use a standard 4x8bit channel. This way it is by far more robust, no magic needed, you get access to 255 bones and it uses the same bandwidth.

 

When changing to custom attributes, this will also be done.

 

 


3. The same for the weights. Test is out with a 4x8bit channel. You have 4 channels and precision down to 0.5% for skinning, which is enough for me. If you still have issues with the precision, take 4x16bit channel, which is still less bandwidth and supports 4 channels instead of just 3.

 

Ah, in case somebody would like to change this, thats an easy one:

 

replace line 180 in mesh.h from

 

glColor3f(ps.weight[0],ps.weight[1],ps.weight[2]);
 
to
 
int w1=ps.weight[0]*255;
int w2=ps.weight[1]*255;
glColor3ub(w1,w2,255-(w1+w2));
 

Advanced:
4. If you have space for 128 bones, using matrix uniforms, then  you could use a combination of a quaternion (4 component) + position (3 component). This way you can use up to 256 bones for the same uniform param space. Btw. you dont need to tranform the quanterion into matrix in the vertex shader, you can rotate the incoming vertex directly by the quaternion.

 

That sounds advanced indeed. Might not suit for beginners in game development, but sounds like a good improvement when using the class in a commercial project.

 

Another optimization would be to preprocess the animation tracks. Rrightnow, the entire skeleton is traversed each time. Simply storing the final bone matrices in the animation tracks would already speed up the entire animation.

 

Further, you could upload the animation tracks as RGBA 16 bit float textures ( in the current version it would be one matrix = 4 pixels). Then,when sampling the texture with linear filtering, you already get the linear interpolated matrix. This would offload the entire animation from cpu to gpu.

Edited by spacerat

Share this post


Link to post
Share on other sites
Ashaman73    13715

you already get the linear interpolated matrix

This is a bad idea, because the matrix interpolation would be unkind to the rotation of bones. Therefor bone rotation is done with slerps (using quaternions). Just keep an eye on the quality of the animation, the vertex shader (where most of the gpu animation stuff is done) isn't the bottleneck in modern game engines ( fragment/pixel shader is often the bottleneck). Btw. using multi-threading to do the bone interpolation plus advanced stuff like blending,overlaying or IK on CPU really works great. Offloading whole animation sequences to the GPU might be useful only in some special case, like massive instancing of animated models etc., but if animation quality is more important, than doing most of the complex stuff on the CPU might be the better and easier solution.

Edited by Ashaman73

Share this post


Link to post
Share on other sites
spacerat    1182

This is a bad idea, because the matrix interpolation would be unkind to the rotation of bones. Therefor bone rotation is done with slerps (using quaternions). Just keep an eye on the quality of the animation, the vertex shader (where most of the gpu animation stuff is done) isn't the bottleneck in modern game engines ( fragment/pixel shader is often the bottleneck). Btw. using multi-threading to do the bone interpolation plus advanced stuff like blending,overlaying or IK on CPU really works great. Offloading whole animation sequences to the GPU might be useful only in some special case, like massive instancing of animated models etc., but if animation quality is more important, than doing most of the complex stuff on the CPU might be the better and easier solution.

 

Yes, in case of large angles and long interpolation times between two keyframes you might certainly figure out a difference between slerp and linear. 

In the tutorial here, the animation is resampled to 20 keyframes per second at the beginning. It takes a bit more memory, but I havent figured out an issue yet when interpolating linearly, since the keyframes are very close to each other.

 

In case you add complex stuff like rag doll physics etc, CPU is much more flexible of course and shader programming would be a pain in that case.

 

When benchmarking the current implementation, I found that about half the time from rendering and animation was used for the bone computation on my PC. Maybe pre-calculating the positions/rotations (as written previously) could help, switching to quaternions or also optimizing the math for SSE.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this