// lightscattering 

bool COLORING=true;
	#define REDAMOUNT  		0.4 		//power of red godray color
	#define GREENAMOUNT 		0.4 		//power of green godray color
	#define BLUEAMOUNT 		0.25 		//power of blue godray color
	
#include "mta-helper.fx"
float2 ScreenLightPos = {0.5,0.5};			//where the Godrays appear, 0.5, 0.5 means screen center
float Density = 0.80;					//power of the rays
static int NUM_SAMPLES = 128;				//how many samples
float Weight = 0.3;
float Decay = 1.0;
float SStresh = 0.40;					//pixel threshhold, above this level pixels get sampled, let it stay like this!
float SunExposure = 0.1;				//Sun gets brighter with this value

bool TINTCOLOR=true;

//--------------------------------------------------------------------------------------
// Textures
//--------------------------------------------------------------------------------------
texture sTex0 :TEX0;

//--------------------------------------------------------------------------------------
// Sampler Inputs
//--------------------------------------------------------------------------------------

sampler2D InputSampler = sampler_state
{
    Texture = (sTex0);
    MinFilter = POINT;
    MagFilter = POINT;
    MipFilter = POINT;
    AddressU   = Clamp;
	AddressV   = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
	
};


struct VSInput
{
    float3 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};

struct PSInput
{
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;
};
 
PSInput VertexShaderFunction(VSInput VS)
{
    PSInput PS = (PSInput)0;
    PS.Position = mul(float4(VS.Position, 1), gWorldViewProjection);
    PS.TexCoord = VS.TexCoord;
    return PS;
}


float4 PixelShaderFunctionMain(PSInput PS) : COLOR0
{
	float4 color = tex2D(InputSampler, PS.TexCoord.xy);
	return color;
}

float4 PixelShaderFunctionEffect(PSInput PS) : COLOR0
{	
	float2 texCoord = PS.TexCoord;
	// Calculate vector from pixel to light source in screen space.  
    half2 deltaTexCoord = (texCoord - ScreenLightPos.xy);  
    // Divide by number of samples and scale by control factor.  
    deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;  
    // Store initial sample.  
    half3 color = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
	// Set up illumination decay factor.  
	half illuminationDecay = 1.0f;  
	// Evaluate summation from Equation 3 NUM_SAMPLES iterations.  
	for (int i = 0; i < NUM_SAMPLES; i++)  
	{  
		// Step sample location along ray.  
		texCoord -= deltaTexCoord;  
		// Retrieve sample at new location.  
	    half3 sample = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
		// Apply sample attenuation scale/decay factors.  

if (TINTCOLOR==true) {
        sample.r *= REDAMOUNT;
		sample.g *= GREENAMOUNT;
		sample.b *= BLUEAMOUNT;
			 }

if (COLORING==true) {

sample = dot(sample, float3(0.3, 0.59, 0.11));
        sample.r *= REDAMOUNT;
		sample.g *= GREENAMOUNT;
		sample.b *= BLUEAMOUNT;
			}

		sample *= illuminationDecay * Weight;  
		// Accumulate combined color.  
		color += sample;  
		// Update exponential decay factor.  
		illuminationDecay *= Decay;  
	}
	

	// Output final color with a further scale control factor.  
	return float4( saturate(color*SunExposure) , color.x);

}




technique lightscattering
{

	pass P0
	{
	VertexShader = compile vs_3_0 VertexShaderFunction();
	PixelShader  = compile ps_3_0 PixelShaderFunctionMain();
	}
	
	pass P1
	{
		VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunctionEffect();
		AlphaBlendEnable=True;
		SrcBlend = One;
		DestBlend = One;
	}
}