//Author: peachykeen
//Title: Sin City
//Pack: MGE Shader Library
//Desc:
//		Grayscale with primaries vibrant

texture lastpass;
texture thisframe;
//float4 HDR;
vector rcpres;
sampler s0 = sampler_state { texture=<thisframe>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=clamp; addressv = clamp; };
sampler s1 = sampler_state { texture=<lastpass>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=clamp; addressv = clamp; };


float4 blurHor(in float2 Tex : TEXCOORD) : COLOR0
{
	float4 Color = tex2D(s0, Tex);

	Color += tex2D(s0, float2(Tex.x-(rcpres.x), Tex.y)) * 0.7;
	Color += tex2D(s0, float2(Tex.x+(rcpres.x), Tex.y)) * 0.7;

	Color += tex2D(s0, float2(Tex.x-(rcpres.x*2), Tex.y)) * 0.4;
	Color += tex2D(s0, float2(Tex.x+(rcpres.x*2), Tex.y)) * 0.4;

	Color += tex2D(s0, float2(Tex.x-(rcpres.x*3), Tex.y)) * 0.2;
	Color += tex2D(s0, float2(Tex.x+(rcpres.x*3), Tex.y)) * 0.2;


	Color /= 3.7;

	return Color;
}

float4 blurVert(in float2 Tex : TEXCOORD) : COLOR0
{
	float4 Color = tex2D(s1, Tex);

	Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y))) * 0.7;
	Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y))) * 0.7;

	Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*2))) * 0.4;
	Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*2))) * 0.4;

	Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*3))) * 0.2;
	Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*3))) * 0.2;


	Color /= 3.7;

	return Color;
}
	
	

float4 SinCity(in float2 Tex : TEXCOORD) : COLOR0
{
	float4 Color = 1;
	float4 mask = tex2D(s1, Tex);
	float4 ori = tex2D(s0, Tex);
	float temp;
	float gray2 = ( ori.r + ori.g + ori.b ) / 3;
	float gray = ( mask.r + mask.g + mask.b ) / 3;
	gray *= gray2 * gray;
	gray2 *= gray2;
	ori *= ori;
	
	
	if ( mask.r > ( mask.b + mask.g ) )
	{
		temp = ( ori.g + ori.b ) / 2;
		Color.r = ori.r;
		Color.g = ( temp * gray ) + ( ori.g * ( 1 - gray ) );
		Color.b = ( temp * gray ) + ( ori.b * ( 1 - gray ) );
	}
	else if ( mask.g > ( mask.r + mask.b ) ) 
	{
		temp = ( ori.r + ori.b ) / 2;
		Color.g = ori.g;
		Color.r = ( temp * gray ) + ( ori.r * ( 1 - gray ) );
		Color.b = ( temp * gray ) + ( ori.b * ( 1 - gray ) );
	}
	else if ( mask.b > ( mask.r + mask.g ) ) 
	{
		temp = ( ori.r + ori.g ) / 2;
		Color.b = ori.b;
		Color.r = ( temp * gray ) + ( ori.r * ( 1 - gray ) );
		Color.g = ( temp * gray ) + ( ori.g * ( 1 - gray ) );
	}
	else
	{
		Color.rgb = gray2;	}

	return Color;
}


technique T0
{
	pass p0
	{
		PixelShader = compile ps_2_0 blurHor();
	}
	pass p1
	{
		PixelShader = compile ps_2_0 blurVert();
	}
	pass p2
	{
		PixelShader = compile ps_2_0 SinCity();
	}
}
