//--------------------------------------------------------------------------
// Globals
//--------------------------------------------------------------------------
matrix World;
matrix ViewProj;
vector CamPos;
float4 Color;
texture BaseTexture;
texture NoiseTexture;
float g_fTime;  
float Alpha;
float Distorsion = 0.08;
float Refraction;
//--------------------------------------------------------------------------
// Structures
//--------------------------------------------------------------------------
struct VS_INPUT
{
 float4 Position : POSITION;
 float3 Normal : NORMAL;
 float2 TextureUV : TEXCOORD0;
};
//--------------------------------------------------------------------------
struct VS_OUTPUT
{
 float4 Position : POSITION;
 float2 TextureUV1 : TEXCOORD0;
 float2 TextureUV2 : TEXCOORD1;
};
//--------------------------------------------------------------------------
VS_OUTPUT Main(VS_INPUT input)
{
 VS_OUTPUT output;
 
 vector wpos = mul(input.Position, World);
 output.Position = mul(wpos, ViewProj);
 
 output.TextureUV1 = input.TextureUV; 
 output.TextureUV2 = input.TextureUV+g_fTime;

 return output;
}
//--------------------------------------------------------------------------
sampler2D Sampler0 = 
sampler_state
{
    Texture = <BaseTexture>;    
	MinFilter = Linear;
	MagFilter = Linear;
	MipFilter = Linear;
	AddressU = Clamp; 
	AddressV = Clamp;   
};

uniform sampler   NoiseTex = 
sampler_state 
{
 texture = <NoiseTexture>;
 MinFilter = Linear;
 MagFilter = Linear;
 MipFilter = Linear;

 AddressU = Wrap; 
 AddressV = Wrap; 
 AddressW = Wrap; 
};
//--------------------------------------------------------------------------
struct PS_OUTPUT
{
 vector diffuse : COLOR0;
 //vector diffuse2 : COLOR1;
};
//--------------------------------------------------------------------------
PS_OUTPUT PShade(VS_OUTPUT input)
{
 PS_OUTPUT output;
 vector c2 = tex2D(NoiseTex, input.TextureUV2);
 
 float x =(0.5-c2.r)*Distorsion;
 vector c1 = tex2D(Sampler0, float2(input.TextureUV1.x+x, input.TextureUV1.y+x)); 
 float RefFac = c1.a;
 c1.a = c1.a*saturate(Alpha);
 
 c2.rgb = c1.rgb * Color.rgb;
 output.diffuse = c1.a*c2;
 //output.diffuse2 = Refraction * RefFac*1.2f*saturate(Alpha*Alpha*Alpha*Alpha);
 
 return output;
}
//--------------------------------------------------------------------------
technique PostProcess
{
 pass p0
 {
  VertexShader = compile vs_1_1 Main();
  PixelShader = compile ps_1_4 PShade();
  AlphaBlendEnable = TRUE;
  SrcBlend =  ONE;
  DestBlend = ONE;  
 }
}
//-----------------------------------------------------------------------------