/*********************************************************************NVMH3****
Apply a post-process effect in a world-space locality
******************************************************************************/
float4x4 worldViewProj : WorldViewProjection;
float4x4 Proj : Projection;
float3 campos : CAMERAPOS;

float3 ObjectPosition 		: ObjectPosition;
float  Radius 				: Radius;
float  Falloff 				: Falloff;
float  BlendAmount 			: BlendAmount;
float  InvertEffect			: InvertEffect;
float3 ColorModulateColor	: ColorModulateColor;

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

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

texture decalTexture : DecalTexture;
sampler DecalTextureSampler = sampler_state 
{
    texture = <decalTexture>;
    AddressU  = CLAMP;        
    AddressV  = CLAMP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

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_Textured2( vertexOutput IN): COLOR
{
    float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		
	float4 camToWorld =  tex2D( DepthTextureSampler, IN.texCoordDiffuse );
	float3 worldPos = -camToWorld.xyz + campos;

	float2 coords = (worldPos.xz - ObjectPosition.xz)/22000*64;

	float lengthToObject = length( worldPos - ObjectPosition );
	float factor = 1.0f - ( lengthToObject / Radius );
	
	factor = saturate( factor );
	factor = pow( factor, Falloff );
	factor = factor * BlendAmount;
    	
	float3 grayXfer = float3(0.3,0.59,0.61);
	float3 gray = dot( grayXfer, sampled );
    
    float3 source = sampled;
    float3 destination = gray;
    destination = destination * ColorModulateColor;
    
    float3 outColor;
    
    // this part will apply the effect in the circle around the point
    if( InvertEffect >= 1.0f )
    {
		outColor = lerp( destination, source, factor );
	}
	else
	{
		// Add this to support texture
	    //factor *= tex2D( DecalTextureSampler, coords ).x;
		outColor = lerp( source, destination, factor );
	}

	/* This is to make an outline
    if( lengthToObject > Radius - 10.0f )
    {
    	if( lengthToObject < Radius )
    	{
    		outColor = float3( 0, 0, 0.2 + (0.7 * (1000 - lengthToObject ) / 10.0f ) );
    	}
    }
	*/
	
	return float4( outColor, 1.0 );
}

float4 PS_TexturedFunky( vertexOutput IN): COLOR
{
    float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		
	float4 camToWorld =  tex2D( DepthTextureSampler, IN.texCoordDiffuse );
	float3 worldPos = -camToWorld.xyz + campos;
	float4 camToWorldAbs = abs( camToWorld );

	float2 coords = (worldPos.xz - ObjectPosition.xz)/22000*64;

	float lengthToObject = length( worldPos - ObjectPosition );
	float factor = 1.0f - ( lengthToObject / Radius );
	
	factor = saturate( factor );
	factor = pow( factor, Falloff );
	factor = factor * BlendAmount;
    	
	float3 grayXfer = float3(0.3,0.59,0.61);
	float3 gray = dot( grayXfer, sampled );
    
    float3 source = sampled;
    float3 destination = gray;
    destination = destination * ColorModulateColor;
    
    float3 outColor;
    
    // this part will apply the effect in the circle around the point
    if( InvertEffect >= 1.0f )
    {
		outColor = lerp( destination, source, factor );
	}
	else
	{
		outColor = lerp( source, camToWorldAbs.xyz/length( camToWorldAbs.xyz ) * ColorModulateColor, factor );
	}

	return float4( outColor, 1.0 );
}

float4 PS_Textured( vertexOutput IN): COLOR
{
    float3 sampled = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse ).xyz;  		
	float4 camToWorld =  tex2D( DepthTextureSampler, IN.texCoordDiffuse );
	float3 worldPos = -camToWorld.xyz + campos;
	float4 camToWorldAbs = abs( camToWorld );

	float2 coords = (worldPos.xz - ObjectPosition.xz)/22000*64;

	float lengthToObject = length( worldPos - ObjectPosition );
	float factor = 1.0f - ( lengthToObject / Radius );
	
	factor = saturate( factor );
	factor = pow( factor, Falloff );
	factor = factor * BlendAmount;
    	
	float3 grayXfer = float3(0.3,0.59,0.61);
	float3 gray = dot( grayXfer, sampled );
    
    float3 source = sampled;
    float3 destination = gray;
    destination = destination * ColorModulateColor;
    
    float3 outColor;
    
    // this part will apply the effect in the circle around the point
    if( InvertEffect >= 1.0f )
    {
		//outColor = lerp( destination, source, factor );
		outColor = lerp( camToWorldAbs.xyz/1000 * ColorModulateColor, source, factor );
	}
	else
	{
		// Add this to support texture
		outColor = lerp( source, destination, factor );

		//for(int s=0; s<1; s++)
		//{
		//	//coord.x = IN.texCoordDiffuse.x + (pixelSizeW * thisGaussian.x); 
		//	coords.x += 1; 
		//	outColor += tex2D(DiffuseTextureSampler, coords) * factor;
		//}
	}

	/*
    if( lengthToObject > Radius - 10.0f )
    {
    	if( lengthToObject < Radius )
    	{
    		outColor = float3( 0, 0, 0.2 + (0.7 * (1000 - lengthToObject ) / 10.0f ) );
    	}
    }
	*/
	
	return float4( outColor, 1.0 );
}


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



technique basic2
{
    pass p0 
    {		
        VertexShader = compile vs_2_0 VS_Screen();
		PixelShader  = compile ps_2_0 PS_Textured2();
		cullmode = none;
    }
}