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

Beam effects
******************************************************************************/
#include "..\mrt.fx"

float4x4 viewProj : ViewProjection;
float4x4 world : World;
float3 campos : CAMERAPOS;

float TaggedBloom : TaggedBloom;
float FinalColorModulate : FinalColorModulate;
float4 ColorModulate : ColorModulate;

float XTranslate : XTranslate;
float3 ClampX : ClampX;

float Tiling : Tiling;

float3 VScales : VScales;
float3 VTranslations : VTranslations;

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

};

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

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

struct vertexOutputScroll {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
    float2 texCoordDiffuse2	: TEXCOORD1;
    float3 posWorld         : TEXCOORD2;
};

vertexOutput VS_Transform(vertexInput IN) 
{
    float4 pos;
    vertexOutput OUT;
    
    pos = mul(IN.position, world);    
    OUT.posWorld = pos;
    OUT.hPosition = mul( pos , viewProj);       
     
    OUT.texCoordDiffuse = IN.texCoordDiffuse;
    return OUT;
}


vertexOutput VS_TransformTiled(vertexInput IN) 
{
    float4 pos;
    vertexOutput OUT;
    
    pos = mul(IN.position, world);    
    OUT.posWorld = pos;
    OUT.hPosition = mul( pos , viewProj);       
     
    OUT.texCoordDiffuse = float2(IN.texCoordDiffuse.x + XTranslate, IN.texCoordDiffuse.y );
    //OUT.texCoordDiffuse = IN.texCoordDiffuse;
    return OUT;
}

vertexOutputScroll VS_TransformScroll(vertexInput IN) 
{
    float4 pos;
    vertexOutputScroll OUT;
    
    pos = mul(IN.position, world);    
    OUT.posWorld = pos;
    OUT.hPosition = mul( pos , viewProj);       
     
    OUT.texCoordDiffuse = (IN.texCoordDiffuse + float2(VTranslations.x, 0)) * float2( VScales.x, 1) ;
    OUT.texCoordDiffuse2 = (IN.texCoordDiffuse + float2(VTranslations.y, 0)) * float2( VScales.y, 1 ) ;
    return OUT;
}



//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutput IN)
{    
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    
    float4 outColor = diffuseTexture;
    
    outColor.xyz *= FinalColorModulate*ColorModulate;
       	
		PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, TaggedBloom ));
	return Out;
}



//-----------------------------------
PS_OUTPUT PS_TexturedTiled( vertexOutput IN)
{   
	float x = IN.texCoordDiffuse.x - ClampX.x; // -min
	x = clamp( x, 0, ClampX.y );
	x /= ClampX.y - ClampX.x;
	
	x *= Tiling;
	float2 newUV =  float2( x, IN.texCoordDiffuse.y  );
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, newUV );
    
    float4 outColor = diffuseTexture;
    
    outColor.xyz *= FinalColorModulate*ColorModulate;
    
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, 0 ));
	return Out;
}

//-----------------------------------
PS_OUTPUT PS_TexturedScroll( vertexOutputScroll IN)
{    
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    float4 diffuseTexture2 = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse2 );
    
    float4 outColor = diffuseTexture * diffuseTexture2;
    
    outColor.xyz *= FinalColorModulate*ColorModulate;
    
    //outColor.x = 1;
	
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, TaggedBloom ));
	return Out;
}



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


//-----------------------------------
technique texturedTiled
{
    pass p0 
    {		
    	VertexShader = compile vs_2_0 VS_TransformTiled();
		PixelShader  = compile ps_2_0 PS_TexturedTiled();
    }
}


//-----------------------------------
technique texturedScroll
{
    pass p0 
    {		
    	VertexShader = compile vs_2_0 VS_TransformScroll();
		PixelShader  = compile ps_2_0 PS_TexturedScroll();
    }
}
