
#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_Solid_Pass1_VS_OUT 
{
	float4 position : POSITION;
	float4 uv0 : TEXCOORD0;
#if USE_FALLOFF == 1
	float falloff1 : TEXCOORD1;
#endif
#if USE_REFLECT_BLEND_1 == 1 || USE_REFLECT_BLEND_2 == 1
	float3 reflectVector2 : TEXCOORD2;
#endif
	float4 diffuseColour : COLOR0;
	float4 specularColour : COLOR1;
#if USE_FALLOFF == 1
	float4 diffuseColour2 : TEXCOORD4;
#endif
};


// Techniques
Technique_Solid_Pass1_VS_OUT Technique1_Pass1_VS(
		float4 posB : POSITION,
		float2 uv0 : TEXCOORD0,
		float4 vCol : COLOR0,
#if ZPS3 == 1
		float4 norm : NORMAL
#else
		float3 norm : NORMAL,
		int BoneNum : BLENDINDICES
#endif
		)
{
	Technique_Solid_Pass1_VS_OUT OUT;

#if ZPS3 == 1
	int BoneNum = int( norm.w );
#endif
	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.xyxy + uvOffset_0;

#if USE_FALLOFF == 1
	float fall1 = 1.0f-dot(eyeVect, norm1);
	OUT.falloff1 = zzzpow( fall1, OpacityAmount.y ) * OpacityAmount.z;
#endif

	float3 halfVec = normalize(lightVect.xyz+eyeVect.xyz);
	float NdotL = dot(lightVect, norm1);
	float NdotH = dot(norm1, halfVec);
	float4 l = lit(NdotL, NdotH, 8.0f);
	OUT.specularColour = l.z;

#if USE_FOG == 1
	float fogpos = dot( OUT.position, invprojTM[0] );
	OUT.specularColour.xyz = 1.0f - saturate( fogpos );
#endif

#if USE_REFLECT_BLEND_1 == 1 || USE_REFLECT_BLEND_2 == 1
	OUT.reflectVector2 = reflect(-eyeVect, norm1).xyz;
#endif

	float4 v = ( ( l.y * LightingBlend.x ) - LightingBlend.x ) + 1.0f;
	OUT.diffuseColour = v * vCol;

#if USE_FALLOFF == 1
	v = ( ( l.y * LightingBlend.z ) - LightingBlend.z ) + 1.0f;
	OUT.diffuseColour2 = v * vCol;
#endif

	return OUT;
}


float4 Technique1_Pass1_PS(
		float4 diffuse : COLOR0,
		float4 specular : COLOR1,
		float4 uv0 : TEXCOORD0
#if USE_FALLOFF == 1
		, float falloff1 : TEXCOORD1
#endif
#if USE_REFLECT_BLEND_1 == 1 || USE_REFLECT_BLEND_2 == 1
		, float3 reflectVector2 : TEXCOORD2
#endif
#if USE_FALLOFF == 1
		, float4 diffuse2 : TEXCOORD4
#endif
		) : Color
{
float4 c1; // = Colour_1;
float4 c2; // = Colour_2;
float4 t1;
float4 l1;
float2 uv;

#if USE_PERB_SCALE == 1
float4 p1 = tex2D(PerbTextureSampler_1,uv0.zw);
#endif
		
	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, reflectVector2.xyz);
	c1 += ( t1 * BumpReflectPerb.y );
#endif

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

#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, reflectVector2.xyz);
	c2 += ( t1 * BumpReflectPerb.z );
#endif

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

	float4 b1 = lerp(cl1, cl2, falloff1);

#else
	float4 b1 = cl1;
#endif

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

	b1 += s3;
	
#if USE_FOG == 1
	s1 = b1;
	b1.xyz = lerp(FogDepth.xyz, s1.xyz, specular.xyz);
#if USE_FOG_BLUR == 1
	b1.w = specular.y;
#endif
#endif

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