float4x4 WorldViewProj : WorldViewProjection;
float2 ViewSize = {800,600};

float2 BlurOffsets[16] ={
{ .2, -.5 },{ .2, .5 },{ -.2, .5 },{ -.2, -.5 },
{ .5, -.2 },{ .5, .2 },{ -.5, .2 },{ -.5, -.2 },
{ .7, -.7 },{ .7,  .7 },{ -.7,  .7},{ -.7, -.7 },
{ 0, -1 },{ 1,  0 },{ -1,  0  },{ 0,  1 }
};





float NoiseStrength<
	string UIWidget = "slider";
	float UIMax = 1;
	float UIMin = 0;
	float UIStep = .01;
> = .1;

float NoiseX<
	string UIWidget = "slider";
	float UIMax = 1;
	float UIMin = 0;
	float UIStep = .01;
> = 0;
float NoiseY<
	string UIWidget = "slider";
	float UIMax = 1;
	float UIMin = 0;
	float UIStep = .01;
> = 0;

float Alpha = 1;

bool Noise = 1;


float Blur <
	string UIWidget = "slider";
	float UIMax = 10;
	float UIMin = 0;
	float UIStep = 0.01;
> = 10;
float BlurScreen <
	string UIWidget = "slider";
	float UIMax = 1;
	float UIMin = 0;
	float UIStep = 0.01;
> = 0;





texture BaseLImg< string ResourceName = "";>;
sampler BaseLMap = sampler_state {
	Texture = <BaseLImg>;
	AddressU = Clamp; AddressV = Clamp;
};
texture BaseRImg< string ResourceName = "";>;
sampler BaseRMap = sampler_state {
	Texture = <BaseRImg>;
	AddressU = Clamp; AddressV = Clamp;
};

texture NoiseImg< string ResourceName = "";>;
sampler NoiseMap = sampler_state {
	Texture = <NoiseImg>;
	AddressU = Wrap; AddressV = Wrap;
};










struct app_in
{
	float4 pos : POSITION;
};

struct vs_out
{
	float4 pos : POSITION;
	float2 uv : TEXCOORD0;
};

vs_out VertexShader( app_in IN, vs_out OUT )
{
	OUT.pos = float4( IN.pos.xy + float2( 1/-ViewSize.x, 1/ViewSize.y ),0.0,1.0 );
	OUT.uv = float4( OUT.pos.x*0.5 + 0.5*OUT.pos.w, 0.5*OUT.pos.w - OUT.pos.y*0.5, OUT.pos.w, OUT.pos.w );
	
	return OUT;
}






float4 CollectHigh( vs_out IN ) : COLOR
{
	float2 color=tex2D(BaseLMap,IN.uv).xy;
	
	float dist=saturate(distance(IN.uv,float2(.5,.5))+BlurScreen);
	
	for (int i = 0; i < 16; i++){
		float2 scale = (Blur/ViewSize)*dist;
		float2 offset=BlurOffsets[i]*scale;
		float2 colorblur=tex2D(BaseLMap,IN.uv+offset.xy).xy;
		
		color+=colorblur;
	}
	color/=17;
	
	
	color+=(tex2D(NoiseMap,(IN.uv*2)+float2( NoiseX, NoiseY)).x-.5)*NoiseStrength*.35;
	color+=(tex2D(NoiseMap,(IN.uv/2)+float2(-NoiseX,-NoiseY)).x-.5)*NoiseStrength*.35;
	
	return float4(color,1,1);
}


float4 CollectLow( vs_out IN ) : COLOR
{
	float color=tex2D(BaseLMap,IN.uv).x;
	
	color+=(tex2D(NoiseMap,(IN.uv*2)+float2( NoiseX, NoiseY)).x-.5)*NoiseStrength*.35;
	color+=(tex2D(NoiseMap,(IN.uv/2)+float2(-NoiseX,-NoiseY)).x-.5)*NoiseStrength*.35;
	
	return float4(color,1,1,1);
}




float4 Composite( vs_out IN ) : COLOR
{
	return float4(tex2D(BaseLMap,IN.uv).x,1,1,Alpha);
}

float4 Composite3D( vs_out IN ) : COLOR
{
	return float4(tex2D(BaseLMap,IN.uv).x,tex2D(BaseRMap,IN.uv).x,1,Alpha);
}







technique High
{
	pass p0{
		VertexShader = compile vs_1_1 VertexShader();
		PixelShader = compile ps_2_0 CollectHigh();
	}
}

technique Low
{
	pass p0{
		VertexShader = compile vs_1_1 VertexShader();
		PixelShader = compile ps_2_0 CollectLow();
	}
}

technique Composite
{
	pass p0{
		VertexShader = compile vs_1_1 VertexShader();
		PixelShader = compile ps_2_0 Composite();
	}
}

technique Composite3D
{
	pass p0{
		VertexShader = compile vs_1_1 VertexShader();
		PixelShader = compile ps_2_0 Composite3D();
	}
}