Wednesday, April 21, 2010

Flaw in XSI: Skinned Vertex Shader

There is a flaw in the skinned vertex shader (xsi_defaultvs.hlsl) provided by AutoDesk SoftImage tool. In the VSSkinned vertex shader, the weighted position has not been transformed in the world space. This made the 3D model to ignore every transformation (translation and rotation) made on it. The line of code mistaken is:

// Skin the vertex position
float4 weightedposition = mul(IN.position, skinTransform);

should be changed into:

float4 weightedposition = mul(mul(IN.position, skinTransform), Model);

After this change you should be able to transform (translation, rotation and scale) your 3D model in the DrawModel() method in this way:

Model model = character.GetModel(); // my model
float rotation = character.GetRotation(); // desire rotation
Vector3 position = character.GetPosition(); // desire position
// transformation Matrix
Matrix worldMatrix = Matrix.CreateRotationY( rotation ) * Matrix.CreateTranslation( position );

UpdateSASData( game, camera );
Matrix[] bones = GetBones( model );
bool isSkinned = (bones.Length > 0);
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo( transforms ); // apply default transforms

foreach( ModelMesh mesh in model.Meshes )
{
//apply our transform to each mesh
this.SASData.Model = transforms[mesh.ParentBone.Index] * worldMatrix;
//...
}


Thanks to: http://www.matthughson.com/

No comments:

Post a Comment