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

float4x4 WorldViewProj : WorldViewProjection;
float4 color = {1,1,1,1};
int num_lights;

float3 light_position0;
float3 light_position1;
float3 light_position2;
float3 light_position3;
float3 light_position4;
float3 light_position5;
float3 light_position6;
float3 light_position7;
float3 light_position8;
float3 light_position9;
float3 light_positionA;
float3 light_positionB;

texture texture0;

sampler Sampler0 {
    Texture = texture0;
    MipFilter = None;
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Clamp;
    AddressV = Clamp;
};

sampler Sampler1 {
    Texture = texture0;
    MipFilter = None;
    MinFilter = Linear;
    MagFilter = Linear;
    AddressU = Wrap;
  
    AddressV = Wrap;
};

struct VS_INPUT {
    float3 pos	: POSITION;
	float2 tex0 : TEXCOORD0;
	float ambient : PSIZE;
};

struct VS_OUTPUT {
	float4 screenposition : POSITION;
	float3 pos : TEXCOORD0;
	float2 tex0  : TEXCOORD1;
    float ambient : TEXCOORD2;
    float4 lights0 : TEXCOORD3;
    float4 lights1 : TEXCOORD4;
    float4 lights2 : TEXCOORD5;
};

VS_OUTPUT VertexShader0( VS_INPUT IN ) {
	VS_OUTPUT OUT;
	OUT.ambient = IN.ambient;
	OUT.screenposition = mul( float4(IN.pos, 1) , WorldViewProj);
	OUT.pos = IN.pos.xyz;
	OUT.tex0 = IN.tex0;
    OUT.lights0.x = distance(light_position0.xy, OUT.pos.xy) / light_position0.z;
    OUT.lights0.y = distance(light_position1.xy, OUT.pos.xy) / light_position1.z;
    OUT.lights0.z = distance(light_position2.xy, OUT.pos.xy) / light_position2.z;
    OUT.lights0.w = distance(light_position3.xy, OUT.pos.xy) / light_position3.z;
    OUT.lights1.x = distance(light_position4.xy, OUT.pos.xy) / light_position4.z;
    OUT.lights1.y = distance(light_position5.xy, OUT.pos.xy) / light_position5.z;
    OUT.lights1.z = distance(light_position6.xy, OUT.pos.xy) / light_position6.z;
    OUT.lights1.w = distance(light_position7.xy, OUT.pos.xy) / light_position7.z;
    OUT.lights2.x = distance(light_position8.xy, OUT.pos.xy) / light_position8.z;
    OUT.lights2.y = distance(light_position9.xy, OUT.pos.xy) / light_position9.z;
    OUT.lights2.z = distance(light_positionA.xy, OUT.pos.xy) / light_positionA.z;
    OUT.lights2.w = distance(light_positionB.xy, OUT.pos.xy) / light_positionB.z;
	return OUT;
}

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

float4 Environment(VS_OUTPUT IN) : COLOR {	
	return tex2D(Sampler1, IN.tex0);
}

float4 Falloff(VS_OUTPUT IN) : COLOR {	
	float4 result = tex2D(Sampler0, IN.tex0);;
    float4 d0 = IN.lights0 * IN.lights0;
    float4 d1 = IN.lights1 * IN.lights1;
    float4 d2 = IN.lights2 * IN.lights2;
	
    float m = 110;
    d0 = clamp(d0/m, 0, 1);
    d1 = clamp(d1/m, 0, 1);
    d2 = clamp(d2/m, 0, 1);
    d0 = float4(1, 1, 1, 1) - d0;
    d1 = float4(1, 1, 1, 1) - d1;
    d2 = float4(1, 1, 1, 1) - d2;
    float i = d0.x + d0.y + d0.z + d0.w + d1.x + d1.y + d1.z + d1.w + d2.x + d2.y + d2.z + d2.w;
    float3 c = float3(0.8, 0.7, 0.5);
    c *= i;
    c = clamp(c, IN.ambient, 1);
    result.rgb *= c;
	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 = TRUE;
		VertexShader = compile vs_2_0 VertexShader0();
		PixelShader  = compile ps_2_0 Falloff();
	}
}

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

