• 12
• 12
• 9
• 10
• 13

# Instancing and skeletal animation

This topic is 4399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Me and some friends has a very cool idea we want to try out. However, this idea requires a lot of skinned meshes to be visible on the screen at the same time. Since the meshes share the base geometry, I have solved one part using hardware instancing, but I am not sure how to solve the skinning part since I haven't done any animation earlier. I know that each vertex of the mesh will have one or several transformation-dependencies, and I know how the transformation of the bones are done, but I am not sure how to get these transformations to the shader. I *THINK* the only way to do this is to send the transformation data to the shader in a separate stream.
[source lang = "cpp"]

struct InInstancingSkinningVS
{
float3 inVertexPosition : POSITION; // from the instance geometry stream
float3 inVertexNormal : NORMAL; // from the instance geometry stream
float4 inVertexColor : COLOR; // from the instance geometry stream
float2 inTexcoord0 : TEXCOORD0; // from the instance geometry stream
float2 inTexcoord1 : TEXCOORD1; // from the instance geometry stream

float3 inInstancePosition : TEXCOORD2; // from the instance index stream

// first joint dependency
float3 inVertexPositionDependency0 : TEXCOORD3; // from the skeletal stream
float3 inVertexRotationDependency0 : TEXCOORD4; // from the skeletal stream

// second joint dependency
float3 inVertexPositionDependency1 : TEXCOORD5; // from the skeletal stream
float3 inVertexRotationDepenedncy1 : TEXCOORD6; // from the skeletal stream
}

void InstancedSkinningVS( inInstancingSkinningVS ) { ... }


As far as I know, this is the only way to send per-vertex data into an instancing shader and the amount of data sent to the shader is clearly huge. So if someone knows a better way to send per-vertex data into an instancing shader, you are welcome to tell me :) edit: some typos

##### Share on other sites

hmm... well, in my skinned meshes I use this vertex format:

float3 position
float3 normal
float4 weights
WORD4 matrix indices

to perform the skinning, you use the matrix indices to offset into the constant shader table, you grab 4 matrices, then you transform your vertex by them, and finally, you blend the results using the weights. If you already have the vertex shader code, it's not too difficult... what's difficult is actually computing the matrices that will go into the constant shaders.

##### Share on other sites
Yeah, that could be done if you're not using instancing. Remember that you have to share constant tables between all the instaces.

##### Share on other sites
Well, you might be able to incorporate the bone transformation matrices for each instance in the vertex declaration for the instance data stream. This is the cleanest way to do this I think, since the matrices are instance data anyway. I'm not sure how to do this exactly, but I guess you can work this out by using a pile of float4's in the vertex declaration and reconstructing matrices from these in the shader.

This approach will require SM3 hardware though (or ATI SM2 hardware, using their intancing hack). The max safe stridesize for a vertex declaration should be about 500 bytes, allowing for about 30 active bonematrices per instance.

I hope this somehow proves useful for you, I know I'm itching to try it now at any rate [smile] IIRC there should be a thread on instancing skinned meshes here on GameDev from about 6 months ago, but I can't seem to find it.... again :p

Good luck!