/*********************************************************************NVMH3****

******************************************************************************/

float4x4 worldViewProj : WorldViewProjection;
float4x4 Proj : Projection;

float3 campos : CAMERAPOS;

//float TimeElapsed : TimeElapsed;

texture diffuseTexture : DiffuseTexture;
sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = CLAMP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};


texture DistortionTexture : DistortionTexture;
sampler DistortionTextureSampler = sampler_state 
{
    texture = <DistortionTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};


texture ColorTexture : ColorTexture;
sampler ColorTextureSampler = sampler_state 
{
    texture = <ColorTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = POINT;
    MINFILTER = POINT;
    MAGFILTER = POINT;
};



texture depthTexture : Custom0;
sampler DepthTextureSampler = sampler_state 
{
    texture = <depthTexture>;
};


struct vertexInput {
    float4 position				: POSITION;
    float2 texCoordDiffuse		: TEXCOORD0;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
};



vertexOutput VS_Screen(vertexInput IN) 
{
    vertexOutput OUT;   
    OUT.hPosition = mul( IN.position, Proj );
    OUT.texCoordDiffuse = IN.texCoordDiffuse;
    return OUT;
}


//-----------------------------------
float4 PS_UnTextured( vertexOutput IN): COLOR
{
	float2 UVs = IN.texCoordDiffuse;
	
	UVs += sin( IN.texCoordDiffuse * 2 * 3.141592653589 ) * 0.05f;
	float3 sampled = tex2D( DiffuseTextureSampler, UVs ).xyz;  		

    float3 result = sampled;
	return float4( result, 1 );
}


//-----------------------------------
float4 PS_Textured( vertexOutput IN): COLOR
{
	float2 UVs = IN.texCoordDiffuse;
	
	float4 distort = tex2D( DistortionTextureSampler, IN.texCoordDiffuse ) * 2 -1;
    distort -= (1/255.0f);
    
	UVs += distort.xy * float2( .5, .5 );
	float3 sampled = tex2D( DiffuseTextureSampler, UVs ).xyz;
	
	
    float3 result = sampled;
    //result.rg = distort.rg;
	return float4( result, 1 );
}



//-----------------------------------
float4 PS_TexturedBaseColor( vertexOutput IN): COLOR
{
	float2 UVs = IN.texCoordDiffuse;
	
	float4 sampled = tex2D( DiffuseTextureSampler, UVs );

	float4 distort = tex2D( DistortionTextureSampler, IN.texCoordDiffuse );
	float3 baseColor = tex2D( ColorTextureSampler, IN.texCoordDiffuse ).xyz;
		
	UVs += distort.xy * 0.020; // + sin( TimeElapsed * 10.0f ) * 0.01f;
	float4 sampledShifted = tex2D( DiffuseTextureSampler, UVs );


	//   0->0.5f is from fullDraw to fully refracted.
	//	 0.5f-> 1.0f is fully refracted to faded away
	float3 result;
	float midPoint = 0.5f;
	if( distort.z < midPoint )
		result = lerp( baseColor, sampledShifted, distort.z / midPoint );
	else
    	result = lerp( sampledShifted, sampled, (distort.z - midPoint) / midPoint );
    
    // alpha channel of color buffer is tagged for distortion. 0 -> distorted
    //if( sampled.a > 0.0f )
    if( sampled.a > 0.15f )
    {
	  	result = sampled.xyz;
	 }
	 //else
	 //{	 
  		//result = float3( sampled.a, sampled.a, sampled.a );
  		//result = float3( distort.z, distort.z, distort.z );
	 //}
  
  	return float4( result, 1 );
}



//-----------------------------------
technique basic
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_UnTextured();
		cullmode = none;
    }
}


//-----------------------------------
technique textured
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_Textured();
		cullmode = none;
    }
}

//-----------------------------------
technique texturedAndBaseColor
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_TexturedBaseColor();
		cullmode = none;
    }
}

