
#ifdef VSPS_3
#define zzzpow( a, b ) pow((a),(b))

shared float4x4 projTM: register(c0);
shared float4x4 worldTM: register(c4);
shared float4 invprojTM[ 4 ]: register(c8);
shared float4x4 Bones[ 32 ]: register(c12);
shared float4 eyePosns[ 32 ]: register(c140);
shared float4 lightPosns[ 32 ]: register(c172);

shared float4 Colour_1: register(c204);
shared float4 Colour_2: register(c205);
shared float4 Specular_Colour: register(c206);
shared float4 OpacityAmount: register(c207);
shared float4 LightingBlend: register(c208);
shared float4 BumpReflectPerb: register(c209);
shared float4 FogDepth: register(c210);

shared float4 uvOffset_0: register(c211);
shared float4 uvOffset_1: register(c212);
shared float4 eyePos: register(c213);
shared float4 lightPos: register(c214);

// Samplers
sampler2D TextureSampler_1 : register(s0);
sampler2D ReflectionSampler_1 : register(s1);
sampler2D Normal_MapSampler : register(s2);
sampler2D TextureSampler_2 : register(s3);
sampler2D ReflectionSampler_2 : register(s4);
sampler2D PerbTextureSampler_1 : register(s5);
#else
#ifdef VSPS_2
#define zzzpow( a, b ) pow((a),(b))

shared float4x4 projTM;
shared float4x4 worldTM;
shared float4 invprojTM[ 4 ];
shared float4x4 Bones[ 32 ];
shared float4 eyePosns[ 32 ];
shared float4 lightPosns[ 32 ];

shared float4 Colour_1;
shared float4 Colour_2;
shared float4 Specular_Colour;
shared float4 OpacityAmount;
shared float4 LightingBlend;
shared float4 BumpReflectPerb;
shared float4 FogDepth;

shared float4 uvOffset_0;
shared float4 uvOffset_1;
shared float4 eyePos;
shared float4 lightPos;

// Samplers
sampler2D TextureSampler_1 : register(s0);
sampler2D ReflectionSampler_1 : register(s1);
sampler2D Normal_MapSampler : register(s2);
sampler2D TextureSampler_2 : register(s3);
sampler2D ReflectionSampler_2 : register(s4);
sampler2D PerbTextureSampler_1 : register(s5);
#else
#define zzzpow( a, b ) pow(abs((a)),(b))

uniform float4x4 projTM: C0;
uniform float4x4 worldTM: C4;
uniform float4 invprojTM[ 4 ]: C8;
uniform float4x4 Bones[ 32 ]: C12;
uniform float4 eyePosns[ 32 ]: C140;
uniform float4 lightPosns[ 32 ]: C172;

uniform float4 Colour_1: C204;
uniform float4 Colour_2: C205;
uniform float4 Specular_Colour: C206;
uniform float4 OpacityAmount: C207;
uniform float4 LightingBlend: C208;
uniform float4 BumpReflectPerb: C209;
uniform float4 FogDepth: C210;

uniform float4 uvOffset_0: C211;
uniform float4 uvOffset_1: C212;
uniform float4 eyePos: C213;
uniform float4 lightPos: C214;

// Samplers
uniform sampler2D TextureSampler_1 : register(s0);
uniform sampler2D ReflectionSampler_1 : register(s1);
uniform sampler2D Normal_MapSampler : register(s2);
uniform sampler2D TextureSampler_2 : register(s3);
uniform sampler2D ReflectionSampler_2 : register(s4);
uniform sampler2D PerbTextureSampler_1 : register(s5);
#endif
#endif


// Structures
struct Technique_BumpOrNormal_Pass1_VS_OUT 
{
	float4 position : POSITION;
	float4 uv0 : TEXCOORD0;
	float3 eyeVect1 : TEXCOORD1;
	float3 lightVect2 : TEXCOORD2;
#if USE_REFLECT_BLEND_1 == 1 || USE_REFLECT_BLEND_2 == 1
	float3 reflectVector3 : TEXCOORD3;
	float3 normX : TEXCOORD4;
#endif
	float4 vCol : COLOR0;
};


// Techniques
Technique_BumpOrNormal_Pass1_VS_OUT Technique1_Pass1_VS(
		float4 posB : POSITION,
		float2 uv0 : TEXCOORD0,
		float4 vCol : COLOR0,
		float3 tangent : TANGENT,
		float3 binormal : BINORMAL,
		float3 norm : NORMAL
		)
{
	Technique_BumpOrNormal_Pass1_VS_OUT OUT;

	OUT.position = mul( posB, projTM );

	float3 norm1 = normalize(norm.xyz);
//	float4 surface1 = mul( float4( posB.xyz, 1.0f ), worldTM );
	float4 surface1 = mul( posB, worldTM );

	float3 eyeVect = (eyePos.xyz-surface1.xyz);
	float3 lightVect = (lightPos.xyz-surface1.xyz);

	OUT.uv0 = uv0.xyxy + uvOffset_0;

	float3x3 tangentSpaceTM;
	tangentSpaceTM[0] = tangent;
	tangentSpaceTM[1] = binormal;
	tangentSpaceTM[2] = norm1;

	float3 foo1 = mul((float3x3)worldTM,eyeVect);
	OUT.eyeVect1 = normalize(mul(tangentSpaceTM,foo1));
	float3 foo2 = mul((float3x3)worldTM,lightVect);
	OUT.lightVect2 = normalize(mul(tangentSpaceTM,foo2));

#if USE_REFLECT_BLEND_1 == 1 || USE_REFLECT_BLEND_2 == 1
//	OUT.reflectVector3 = normalize(surface1-eyePos);
	OUT.reflectVector3 = normalize(-eyeVect);
	OUT.normX = norm1.xyz;
#endif

	OUT.vCol = vCol;

	return OUT;
}


float4 Technique1_Pass1_PS(
		float4 uv0 : TEXCOORD0,
		float3 eyeVect1 : TEXCOORD1,
		float3 lightVect2 : TEXCOORD2
#if USE_REFLECT_BLEND_1 == 1 || USE_REFLECT_BLEND_2 == 1
		, float3 reflectVector3 : TEXCOORD3
		, float3 normX : TEXCOORD4
#endif
		, float4 vCol : COLOR0
		) : COLOR
{
float2 uv = uv0.xy;
// float4 p1 = {0.0f,0.0f,0.0f,0.0f};

//	uv = uv0.xy;
#if USE_PERB_SCALE == 1
float4 p1 = tex2D(PerbTextureSampler_1,uv0.zw);
	uv += p1.b*BumpReflectPerb.w;
#endif
	float4 vect1 = tex2D(Normal_MapSampler,uv.xy);

	float3 normA1 = (vect1.xyz-0.5f)*2.0f;
	float3 normA2 = normA1*BumpReflectPerb.x;
	float3 normal1 = normalize(normA2+(float3(0,0,0.1)));

#if USE_REFLECT_BLEND_1 == 1 || USE_REFLECT_BLEND_2 == 1
	float3 refl3 = normalize(normX.xyz + normA2);
	float3 reflectVector = reflect(reflectVector3, refl3).xyz;
#endif

	float3 halfVec = normalize(lightVect2.xyz+eyeVect1.xyz);
	float NdotL = dot(lightVect2, normal1);
	float NdotH = dot(halfVec, normal1);
	float4 l = lit(NdotL, NdotH, 8.0f);
//	float4 specular = l.z;
//	float4 diffuse = l.y;
//	float4 diffuse = float4(l.yyy, 1.0f);
	float4 diffuse = float4(l.yyy, OpacityAmount.x);


float4 c1; // = Colour_1;
float4 c2; // = Colour_2;
float4 t1;
float4 l1;
float4 b1;
float4 o1 = float4(1.0f,1.0f,1.0f,OpacityAmount.x);

	uv = uv0.xy;
#if USE_PERB_SCALE == 1
	uv += p1.r*BumpReflectPerb.w;
#endif
	t1 = tex2D(TextureSampler_1, uv.xy);
	c1 = lerp(Colour_1, t1, LightingBlend.y);

#if USE_REFLECT_BLEND_1 == 1
	t1 = tex2D(ReflectionSampler_1, reflectVector.xyz);
	c1.rgb += ( t1.rgb * BumpReflectPerb.y );
#endif

//	l1 = lerp(float4(1.0f,1.0f,1.0f,1.0f), diffuse, LightingBlend.x);
	l1 = lerp(o1, diffuse, LightingBlend.x);
	float4 cl1 = c1*l1;

#if USE_FALLOFF == 1

	uv = uv0.xy;
#if USE_PERB_SCALE == 1
	uv += p1.g*BumpReflectPerb.w;
#endif
	t1 = tex2D(TextureSampler_2, uv.xy);
	c2 = lerp(Colour_2, t1, LightingBlend.w);

#if USE_REFLECT_BLEND_2 == 1
	t1 = tex2D(ReflectionSampler_2, reflectVector.xyz);
	c2.rgb += ( t1.rgb * BumpReflectPerb.z );
#endif

//	l1 = lerp(float4(1.0f,1.0f,1.0f,1.0f), diffuse, LightingBlend.z);
	l1 = lerp(o1, diffuse, LightingBlend.z);
	float4 cl2 = c2*l1;

	float fall1 = 1.0f-dot(eyeVect1, normal1);
	float falloff1 = zzzpow( fall1, OpacityAmount.y ) * OpacityAmount.z;
	b1 = lerp(cl1, cl2, falloff1);

#else
	b1 = cl1;
#endif

	float4 s1 = zzzpow( l.z, Specular_Colour.w );
	float4 s3 = s1*Specular_Colour;

	b1 *= vCol;

	b1.rgb += s3.rgb;

	return b1;
}


// Techniques
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
	}
}
