attribute vec3 vPosition;
attribute vec2 vTexCoord;
attribute vec4 aBoneIndices0;
attribute vec2 aBoneIndices1;
attribute vec4 aBoneWeights0;
attribute vec2 aBoneWeights1;

uniform mat4 ProjectionModelViewMatrix;
uniform mat4 uBoneMatrices[64];

void main()
{
	vec4 skinnedPos = vec4(0.0, 0.0, 0.0, 0.0);
	vec4 originalPos = vec4(vPosition, 1.0);
	float totalWeight = 0.0;

	if (aBoneWeights0.x > 0.0) {
		skinnedPos += uBoneMatrices[int(aBoneIndices0.x)] * originalPos * aBoneWeights0.x;
		totalWeight += aBoneWeights0.x;
	}
	if (aBoneWeights0.y > 0.0) {
		skinnedPos += uBoneMatrices[int(aBoneIndices0.y)] * originalPos * aBoneWeights0.y;
		totalWeight += aBoneWeights0.y;
	}
	if (aBoneWeights0.z > 0.0) {
		skinnedPos += uBoneMatrices[int(aBoneIndices0.z)] * originalPos * aBoneWeights0.z;
		totalWeight += aBoneWeights0.z;
	}
	if (aBoneWeights0.w > 0.0) {
		skinnedPos += uBoneMatrices[int(aBoneIndices0.w)] * originalPos * aBoneWeights0.w;
		totalWeight += aBoneWeights0.w;
	}
	if (aBoneWeights1.x > 0.0) {
		skinnedPos += uBoneMatrices[int(aBoneIndices1.x)] * originalPos * aBoneWeights1.x;
		totalWeight += aBoneWeights1.x;
	}
	if (aBoneWeights1.y > 0.0) {
		skinnedPos += uBoneMatrices[int(aBoneIndices1.y)] * originalPos * aBoneWeights1.y;
		totalWeight += aBoneWeights1.y;
	}

	if (totalWeight < 0.001) {
		skinnedPos = originalPos;
	}

	gl_Position = ProjectionModelViewMatrix * skinnedPos;
}
