Graphics Reference
In-Depth Information
8.
Create a new HLSL method called SkinVertex .
void SkinVertex(float4 weights, uint4 bones,
inout float4 position, inout float3 normal)
{
// If there are skin weights apply vertex skinning
if (weights.x != 0)
{
// Calculate the skin transform combining up to
// four bone influences
float4x4 skinTransform =
Bones[bones.x] * weights.x +
Bones[bones.y] * weights.y +
Bones[bones.z] * weights.z +
Bones[bones.w] * weights.w;
// Apply skinning to vertex and normal
position = mul(position, skinTransform);
// We assume here that the skin transform includes
// only uniform scaling (if any)
normal = mul(normal, (float3x3)skinTransform);
}
9.
Immediately before applying the WorldViewProjection matrix to the vertex
position, call the new SkinVertex method as shown in the following code:
// Apply vertex skinning if any
SkinVertex(vertex.SkinWeights, vertex.SkinIndices,
vertex.Position, vertex.Normal);
We are done with our shaders for the moment. As we have just added a new constant
buffer, we need to open ConstantBuffers.cs and make the appropriate changes.
10. Create a new class to store our per armature data. Note that we are using class
here instead of struct , as we will be passing through the Bones array when
updating the constant buffer. This simplifies the marshalling of the structure
to the Direct3D buffer, and we can initialize the array more easily.
// Per armature/skeleton constant buffer
public class PerArmature
{
// The maximum number of bones supported by the shader
public const int MaxBones = 1024;
public Matrix[] Bones;
 
Search WWH ::




Custom Search