//Authors: Mark McKenna
//Additional code by: Kenneth Lombardi

float4x4 WorldViewProj : WorldViewProjection;
float4 color = {1,1,1,1};
float2 light_position;
texture texture0;

sampler Sampler0
{
    Texture = texture0;
    //MipFilter = Linear;
    //MinFilter = Linear;
    //MagFilter = Linear;
    AddressU = Wrap;
    AddressV = Wrap;
};


struct VS_INPUT
{
    float3 pos	: POSITION;
	float2 tex0 : TEXCOORD0;
	float3 normal : NORMAL;
};

struct VS_OUTPUT
{
	float4 screenposition : POSITION;
	float3 pos : TEXCOORD4;
	float2 tex0  : TEXCOORD1;
	float3 normal : TEXCOORD2;
	float2 light : TEXCOORD3;
	
};

VS_OUTPUT VertexShader0( VS_INPUT IN )
{
	VS_OUTPUT OUT;
	OUT.screenposition = mul( float4(IN.pos, 1) , WorldViewProj);
	OUT.pos = IN.pos.xyz;
	//OUT.screenposition = float4(0, 0, 0, 0);
	OUT.tex0 = IN.tex0;
	OUT.light = light_position;
	OUT.normal = normalize(mul(IN.normal, WorldViewProj));

	return OUT;
}

float4 PixelShader0( VS_OUTPUT IN ) : COLOR
{		
	return tex2D(Sampler0, IN.tex0);
}

float4 Falloff(VS_OUTPUT IN) : COLOR {	
	float2 difference = light_position - IN.pos;
	float dist = dot(difference, difference);
	float m = 75;
	dist = clamp(dist/m, 0, 1);
	dist = max(1 - dist, 0.3);
	float4 result = tex2D(Sampler0, IN.tex0);
	result.r *= dist;
	result.g *= dist * 0.6;
	result.b *= dist * 0.3;
	return result;
}

technique Technique0
{
	pass Pass0
	{
		Lighting = TRUE;
		AlphaBlendEnable = TRUE;
		VertexShader = compile vs_2_0 VertexShader0();
		PixelShader  = compile ps_2_0 PixelShader0();
	}
}

technique Cave {
	pass Pass0 {
		Lighting = TRUE;
		AlphaBlendEnable = FALSE;
		VertexShader = compile vs_2_0 VertexShader0();
		PixelShader  = compile ps_2_0 Falloff();
	}
}



