cbuffer MatrixBuffer
{
	matrix worldMatrix;
	matrix viewMatrix;
	matrix projectionMatrix;
}

cbuffer SkeletonInfoBuffer : register(vs, cb2)
{
	matrix mWorldMatrixArray[128];
}

struct TInput
{
    float4 position : POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
	float3 tangent : TANGENT;
	int bones[4] : BONES;
	float weights[3] : WEIGHTS;
	uint materialId : MATERIALID;
};

struct TOutput
{
    float4 position : SV_POSITION;
};

TOutput ZPassVertexShader(TInput input)
{
    TOutput output;
    float4 worldPosition;

    // Change the position vector to be 4 units for proper matrix calculations.
    input.position.w = 1.0f;

    // cast the vectors to arrays for use in the for loop below
    float BlendWeightsArray[3] = (float[3])input.weights;
	
   	float LastWeight = 0.0f;
	matrix transformMatrix = 0.0f;
	[unroll]
    for (int iBone = 0; iBone < 3; iBone++)
    {
		int matrixIndex = input.bones[iBone]+1;
        LastWeight += input.weights[iBone];
        
        transformMatrix += mWorldMatrixArray[matrixIndex] * input.weights[iBone];
    }
    LastWeight = 1.0f - LastWeight; 

	int matrixIndex = input.bones[3]+1;
    transformMatrix +=  mWorldMatrixArray[matrixIndex] * LastWeight;
	
	output.position = mul(input.position, transformMatrix);
	
    // Calculate the position of the vertex against the world, view, and projection matrices.
    output.position = mul(output.position, worldMatrix);
    output.position = mul(output.position, viewMatrix);
    output.position = mul(output.position, projectionMatrix);

	
    return output;
}