

VS_LIGHTING_OUTPUT vs_lighting_2_0(VS_INPUT Input)
{
	VS_LIGHTING_OUTPUT Output = (VS_LIGHTING_OUTPUT)0;

    //getting the position of the vertex in the world
    float4 posWorld = mul(Input.position, matW);
	float4 normal = normalize(mul(Input.normal, matW));
	
	if(useWorldScale)
	{
		float prc = posWorld.y / towerHeight;
		posWorld.x = prc * posWorld.x + (1.0f - prc) * posWorld.x * worldScale;
		posWorld.z = prc * posWorld.z + (1.0f - prc) * posWorld.z * worldScale;
	}
	
	//getting to position to object space
	float4 pos = mul(posWorld, matVP);
	
	Output.Position = pos;
	Output.Normal  = normal;
	//Reflection coordinate
    Output.envUV = reflect(-normalize(camPos - posWorld), normal);


	float3 fvLightPosition = lightDir;

	Output.Texcoord         = Input.tex0;
	
	float3 fvObjectPosition = mul( Input.position, matV );

	Output.ViewDirection    = cameraPos - fvObjectPosition;
	Output.LightDirection   = fvLightPosition - fvObjectPosition;

   return( Output );

}

float4 ps_lighting_2_0(PS_LIGHTING_INPUT  Input) : COLOR0
{

	float4 shadowColor = float4(0.04, 0.02, 0.0, 1.0);
	float4 lightColor = float4(1.0, 1.0, 1.0, 1.0);
	float ambient = 0.5;
	
	float4 color = tex2D(DiffuseSampler, Input.Texcoord)*lightColor;
	
	float4 param = tex2D(ParamSampler, Input.Texcoord);
	
	float2 envTC = float2(Input.envUV.xy)*0.5+0.5;
	//Reflection    	
	float4 reflectedColor = tex2D(EnvironmentSampler, envTC);		


	// Lighting
	
	float lighting;
	float4 output; 

	float3 fvLightDirection = normalize( Input.LightDirection );
	float3 fvNormal         = normalize( Input.Normal );

	 
	float  fNDotL = dot( fvNormal, fvLightDirection );
	fNDotL = (clamp(fNDotL, 0.4f, 0.6f)-0.5)*(6.6667f);
	lighting = fNDotL + (1.0-fNDotL)*0.6;

	// self illumination
	lighting = max(ambient+param.b, lighting);
	

	output = lerp(shadowColor,color,lighting);
	
	// Reflection
	//if(param.r > 0.0)
	reflectedColor = output*reflectedColor*1.5;
	output = lerp(output, reflectedColor, param.r);
	
	// Add some fake "radiosity"
	output *= texCUBE(LightMapSampler, fvNormal).r*3.0;
	// Alpha
	output.a = 1.0-param.g;
	
	
	return output;
}

float4 ps_lighting_2_0_LOW(PS_LIGHTING_INPUT  Input) : COLOR0
{
	float4 color = tex2D(DiffuseSampler, Input.Texcoord);
	
	float4 param = tex2D(ParamSampler, Input.Texcoord);
	
	// Lighting
	
	float4 col;
	float ambient = 0.5;

	float3 fvLightDirection = normalize( Input.LightDirection );
	float3 fvNormal         = normalize( Input.Normal );

	 
	float  fNDotL = dot( fvNormal, fvLightDirection );
	fNDotL = (clamp(fNDotL, 0.4f, 0.6f)-0.5)*(6.6667f);
	
	col = fNDotL + (1.0-fNDotL)*0.6;
	// self illumination
	col = max(ambient+param.b, col);
	
	
	col *= color*1.1;	
	
	// Alpha
	col.a = 1.0-param.g;

	

	return col;
}

//Vertex Shader
VS_OUTPUT vs_lighting(VS_INPUT IN)
{
	VS_OUTPUT OUT = (VS_OUTPUT)0;

    //getting the position of the vertex in the world
    float4 posWorld = mul(IN.position, matW);
	float4 normal = normalize(mul(IN.normal, matW));
	
	if(useWorldScale)
	{
		float prc = posWorld.y / towerHeight;
		posWorld.x = prc * posWorld.x + (1.0f - prc) * posWorld.x * worldScale;
		posWorld.z = prc * posWorld.z + (1.0f - prc) * posWorld.z * worldScale;
	}
	
	//getting to position to object space
	float4 pos = mul(posWorld, matVP);
	OUT.position = pos;
	 
	// Shade
    OUT.params.x = max(dot(normal, normalize(lightDir)) * 1.4f, ambientLight);

    //Reflection coordinate
    OUT.envUV = reflect(-normalize(camPos - posWorld), normal);

    OUT.tex0 = IN.tex0;
    OUT.tex1 = IN.tex0;
    
    return OUT;
}
//Pixel Shader
float4 ps_lighting(VS_OUTPUT IN) : COLOR0
{
	float shade = IN.params.x;
		
	float4 color = tex2D(DiffuseSampler, IN.tex0);
	
	float4 param = tex2D(ParamSampler, IN.tex1);
	
	
	float4 col = (color * shade);
	col.a = param.g;

	return col;
}

//Lighting Technique
technique Lighting_2_0
{
	pass P0
	{     
		
		Lighting = false;
		ZWriteEnable = true;
		StencilEnable = false;
		VertexShader = compile vs_2_0 vs_lighting_2_0();
		PixelShader  = compile ps_2_0 ps_lighting_2_0();
			
		
	}
}

technique Lighting_2_0_LOW
{
	pass P0 
	{        
		Lighting = false;
		ZWriteEnable = true;
		StencilEnable = false;
		VertexShader = compile vs_2_0 vs_lighting_2_0();
		PixelShader  = compile ps_2_0 ps_lighting_2_0_LOW();
	}
}

technique Lighting_1_1
{
	pass P0
	{        
		Lighting = false;	
		ZWriteEnable = true;	
		StencilEnable = false;
		VertexShader = compile vs_1_1 vs_lighting();
		PixelShader  = compile ps_1_1 ps_lighting();
	}
}

technique Lighting_FFP
{
	pass P0
	{        
		Lighting = false;
		ZWriteEnable = true;
		StencilEnable = false;
		WorldTransform[0] = <matW>;
		ViewTransform = <matV>;
		ProjectionTransform = <matP>;
		Texture[0] = <texDiffuse>;
	}
}