#ifdef VSPS_3
shared float4x4 projTM: register(c0);
shared float4x4 worldTM: register(c4);
shared float4x4 worldITM: register(c8);
shared float4x4 Bones[ 32 ]: register(c12);
shared float4 eyePosns[ 32 ]: register(c140);
shared float4 lightPosns[ 32 ]: register(c172);
shared float4 eyePos: register(c204);
shared float4 lightPos: register(c205);

shared float4 uvOffset_0: register(c206);
shared float4 uvOffset_1: register(c207);

shared float4 combo: register(c208);
// shared float Reflect_amt: register(c208);
// shared float Lighting: register(c209);
// shared float Bump: register(c210);
// shared float uvOffsetY: register(c211);
shared float4 Color_1: register(c212);
shared float4 Color_2: register(c213);
shared float4 Specular_Color: register(c214);
#else
shared float4x4 projTM;
shared float4x4 worldTM;
shared float4x4 worldITM;
shared float4x4 Bones[ 32 ];
shared float4 eyePosns[ 32 ];
shared float4 lightPosns[ 32 ];
shared float4 eyePos;
shared float4 lightPos;

shared float4 uvOffset_0;
shared float4 uvOffset_1;

shared float4 combo;
// shared float Reflect_amt;
// shared float Lighting;
// shared float Bump;
// shared float uvOffsetY;
shared float4 Color_1;
shared float4 Color_2;
shared float4 Specular_Color;
#endif


sampler2D TextureSampler: register(s0);
sampler2D bump_mapSampler: register(s1);
sampler2D ReflectionSampler: register(s2);


struct Technique1_Pass1_VS_OUT 
{
	float4 position : POSITION;
	float2 uv0 : TEXCOORD0;
	float3 eyeVect1 : TEXCOORD1;
	float3 lightVect2 : TEXCOORD2;
	float3 reflectVector : TEXCOORD3;
};


Technique1_Pass1_VS_OUT Technique1_Pass1_VS(
		float4 posB : POSITION,
		float2 uv0 : TEXCOORD0,
		float3 tangent : TANGENT,
		float3 binormal : BINORMAL,
		float3 norm : NORMAL,
		int BoneNum : BLENDINDICES
		)
{
	Technique1_Pass1_VS_OUT OUT;

	OUT.position = mul( posB, Bones[BoneNum]);
	float4 surface1 = posB;
	float3 norm1 = norm.xyz;

	float3 eyeVect = normalize(eyePosns[BoneNum].xyz-surface1.xyz);
	float3 lightVect = normalize(lightPosns[BoneNum].xyz-surface1.xyz);

	OUT.uv0 = uv0;
	float3x3 tangentSpaceTM;
	tangentSpaceTM[0] = tangent;
	tangentSpaceTM[1] = binormal;
	tangentSpaceTM[2] = normalize(norm1);

	OUT.eyeVect1 = normalize(mul(tangentSpaceTM, eyeVect));
	OUT.lightVect2 = normalize(mul(tangentSpaceTM, lightVect));

	OUT.reflectVector = reflect(-eyeVect, norm1).xyz;
	return OUT;
}

float4 Technique1_Pass1_PS(
		float2 uv0 : TEXCOORD0,
		float3 eyeVect1 : TEXCOORD1,
		float3 lightVect2 : TEXCOORD2,
		float3 reflectVector : TEXCOORD3
		) : COLOR
{
	float4 vect1 = tex2D(bump_mapSampler, uv0.xy);
	float3 normA1 = (vect1.xyz-0.5f)*2.0f;
// Bump
	float3 normal1 = normalize((normA1*combo.z)+(float3(0.0f,0.0f,0.1f)));
	float3 halfVec = normalize(lightVect2.xyz+eyeVect1.xyz);
	float NdotL = dot(lightVect2, normal1);
	float NdotH = dot(halfVec, normal1);
	float4 l = lit(NdotL, NdotH, 1.0f);
	float4 base1 = l.z + 0.0001f;

// Glossiness
	float4 spec1 = pow(base1, Specular_Color.w);
	float4 spec2 = spec1*Specular_Color;

	float4 color12 = tex2D(ReflectionSampler, reflectVector.xyz);
// Reflect_amt
	float4 color01 = lerp(Color_1, color12, combo.x);

	float fall1 = 1.0001f-dot(eyeVect1, normal1);
// FalloffAmount
	float fall2 = pow(fall1, Color_2.w);
	float4 fallcol1 = lerp(color01, Color_2, fall2);

	float4 color13 = tex2D(TextureSampler, uv0.xy);
	float4 b3 = color13*fallcol1;

	return spec2+b3;
}


technique Technique1
{
	pass Pass1
	{
#ifdef VSPS_3
		VertexShader = compile vs_3_0 Technique1_Pass1_VS();
		PixelShader = compile ps_3_0 Technique1_Pass1_PS();
#else
		VertexShader = compile vs_2_0 Technique1_Pass1_VS();
		PixelShader = compile ps_2_0 Technique1_Pass1_PS();
#endif
	}
}

