sampler texture0 : register(s0);
sampler texture1 : register(s1);

float4x4 WorldViewProjectionTransform  : WORLDVIEWPROJ;
float2 fTextureSize;
float fCurrentTime;

struct VS_OUTPUT 
{
   float4 Position  : POSITION;
   float2 TexCoord  : TEXCOORD0;
};

struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color
};

VS_OUTPUT vertexMain(
				float4 vPosition : POSITION,
				float2 texCoord0 : TEXCOORD0)
{
	VS_OUTPUT Output; 

	Output.Position = mul(vPosition, WorldViewProjectionTransform);
	Output.TexCoord = texCoord0;   

	return Output;
}

PS_OUTPUT pixelMain(VS_OUTPUT In)
{
	PS_OUTPUT output;
	float2 incoord = In.TexCoord + 1.f / fTextureSize / 2.f; 
	
	float4 color = tex2D(texture0, incoord);

	float fGreyValue = dot(color, float4(0.299f, 0.587f, 0.114f, 0.f));

	// add some perlin noise
	float fAbsoluteTime = fCurrentTime / 1000.0;
	float2 move = float2(0.f, 4.f);
    float3 perlin = tex2D(texture1, incoord + fAbsoluteTime*move).rgb / 2;
    perlin += tex2D(texture1, In.TexCoord * 2 + fAbsoluteTime*move).rgb / 4;
    perlin += tex2D(texture1, In.TexCoord * 4 + fAbsoluteTime*move).rgb / 8;
    perlin += tex2D(texture1, In.TexCoord * 8 + fAbsoluteTime*move).rgb / 16;
    perlin += tex2D(texture1, In.TexCoord * 16 + fAbsoluteTime*move).rgb / 32;
    perlin += tex2D(texture1, In.TexCoord * 32 + fAbsoluteTime*move).rgb / 64; 

	color.rgb *= perlin;

	// create stripes
	float fCurrentLine = In.TexCoord.y * fTextureSize[0] / 6.f;
	modf(fCurrentLine, fCurrentLine);
	if ((fCurrentLine % 2) == 0.f)
		color *= 0.8;

	float fFactor = step(0.7f, fGreyValue);
	// make the color bluish
	color.g = saturate(color.g  + 0.1f * (1.f + fGreyValue * 10.0f));
	color.r = saturate(color.r * 0.1f * (1.f + fGreyValue * fFactor * 12.0f));
	color.b = saturate(color.b * 0.1f * (1.f + fGreyValue * fFactor * 12.0f));

	float2 dirVec = In.TexCoord - float2(0.5, 0.5);
	float dist = length(dirVec);
	color.rgb *= 1.f - (dist * 1.5f);


	output.RGBColor = color;
	return output;
}