// ChromaticAbberation

float ChromaticAmount = 0.0; 				// Amount of chromatic aberration on the edges
float LensSize = 0.5;   				// 0.5 = Original image size; 1.0 = Zoomed
float LensDistortion = 0.0;   				// Can be negative to stretch the image
float LensDistortionCubic = 0.0;			// similiar to above, better results if same like LensDistortion


#include "mta-helper.fx"
//--------------------------------------------------------------------------------------
// 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 PixelShaderFunction(PSInput PS) : COLOR0
{
float4 coord=0.0;

coord.xy=PS.TexCoord.xy;
coord.w=0.0;

float3 eta = float3(1.0+ChromaticAmount*0.9,1.0+ChromaticAmount*0.6,1.0+ChromaticAmount*0.3);
float2 center;
center.x = coord.x-0.5;
center.y = coord.y-0.5;
float LensZoom = 1.0/LensSize;

float r2 = (PS.TexCoord.x-0.5) * (PS.TexCoord.x-0.5) + (PS.TexCoord.y-0.5) * (PS.TexCoord.y-0.5);     
float f = 0;

if( LensDistortionCubic == 0.0){
	f = 1 + r2 * LensDistortion;
}else{
                f = 1 + r2 * (LensDistortion + LensDistortionCubic * sqrt(r2));
};

float x = f*LensZoom*(coord.x-0.5)+0.5;
float y = f*LensZoom*(coord.y-0.5)+0.5;
float2 rCoords = (f*eta.r)*LensZoom*(center.xy*0.5)+0.5;
float2 gCoords = (f*eta.g)*LensZoom*(center.xy*0.5)+0.5;
float2 bCoords = (f*eta.b)*LensZoom*(center.xy*0.5)+0.5;

//float3 inputDistord = tex2D(InputSampler,float2(x,y));
float4 inputDistord = float4(tex2D(InputSampler,rCoords).r , tex2D(InputSampler,gCoords).g ,tex2D(InputSampler,bCoords).b, tex2D(InputSampler,float2(x,y)).a);
return float4(inputDistord.r,inputDistord.g,inputDistord.b,1);

}


technique chromaticAbberation
{
	pass P0
	{
	VertexShader = compile vs_3_0 VertexShaderFunction();
	PixelShader  = compile ps_3_0 PixelShaderFunction();

	DitherEnable=FALSE;
	ZEnable=FALSE;
	CullMode=NONE;
	ALPHATESTENABLE=FALSE;
	SEPARATEALPHABLENDENABLE=FALSE;
	AlphaBlendEnable=FALSE;
	StencilEnable=FALSE;
	FogEnable=FALSE;
	SRGBWRITEENABLE=FALSE;
	}
}