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

ComplexPolytail.fx
******************************************************************************/
#include "..\mrt.fx"

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

float StartOpacity : StartOpacity;
float MidOpacity : MidOpacity;
float EndOpacity : EndOpacity;

float InnerOpacity : InnerOpacity;
float OuterOpacity : OuterOpacity;

float TaggedBloom : TaggedBloom;

float4 ColorStart : ColorStart;
float4 ColorMid : ColorMid;
float4 ColorEnd : ColorEnd;

float ColorMidpoint : ColorMidpoint;

float FinalColorModulate : FinalColorModulate;

float2 UVScale : UVScale;
float2 UVScale2ndLayer : UVScale2ndLayer;

float2 UVTranslate : UVTranslate;
float2 UVTranslate2ndLayer : UVTranslate2ndLayer;

float  LayerBlend : LayerBlend;
float  LayerModulateOrBlend : LayerModulateOrBlend;


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

texture MyDiffuseTexture2ndLayer : MyDiffuseTexture2ndLayer;
sampler DiffuseTextureSampler2ndLayer = sampler_state 
{
    texture = <MyDiffuseTexture2ndLayer>;
    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;
    float2 texCoordDiffuseLayer2	: TEXCOORD1;
    float2 texCoordDiffuse2	: TEXCOORD2;
    float3 posWorld         : TEXCOORD3;
};

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 + UVTranslate) * UVScale;
    OUT.texCoordDiffuseLayer2 = (IN.texCoordDiffuse + UVTranslate2ndLayer) * UVScale2ndLayer;
    
    OUT.texCoordDiffuse2 = IN.texCoordDiffuse2;
    
    return OUT;
}

//-----------------------------------
PS_OUTPUT PS_UnTextured( vertexOutput IN)
{    
    
    float4 colorSum;  
    colorSum = float4(1.0f, 1.0f, 1.0f, 1.0f);
    
    float lengthOpacity = lerp( StartOpacity, MidOpacity, min( 1.0f, IN.texCoordDiffuse2.x / 0.5f) );
    lengthOpacity = lerp( lengthOpacity, EndOpacity, max( 0.0f, (IN.texCoordDiffuse2.x - 0.5f) / 0.5f) );
    
    float whereWidth = 1.0f - (abs (IN.texCoordDiffuse2.y - 0.5f)) * 2.0f;
    float widthOpacity = lerp( OuterOpacity, InnerOpacity, whereWidth );    
    
    colorSum.a = lengthOpacity * widthOpacity; // * tipOpacity;
    
    float3 colorModulate;
    colorModulate = lerp( ColorStart, ColorMid, min(1, IN.texCoordDiffuse2.x / ColorMidpoint) ).xyz;
    colorModulate = lerp( colorModulate, ColorEnd, max(0, (IN.texCoordDiffuse2.x-ColorMidpoint)/(1.0f-ColorMidpoint)) ).xyz;
    
    colorSum.xyz *= (colorModulate * FinalColorModulate);
        
    PS_OUTPUT Out;        
	SetPSOutput( Out, colorSum, float4( campos - IN.posWorld.xyz, MRT_FORCE_BLOOM) );
	return Out;
}


//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutput IN)
{        
    float4 colorSum;  
    
    float4 diffuseTexture = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    float4 diffuseTexture2ndLayer = tex2D( DiffuseTextureSampler2ndLayer, IN.texCoordDiffuse);
    
    // blend
    if( LayerModulateOrBlend < 1.0f ) 
    {
        diffuseTexture = lerp( diffuseTexture, diffuseTexture2ndLayer, LayerBlend );
    }
    else // modulate
    {
        diffuseTexture = diffuseTexture * diffuseTexture2ndLayer * LayerBlend;
    }
    
    colorSum = diffuseTexture;
    
    //float lengthOpacity = lerp( StartOpacity, EndOpacity, IN.texCoordDiffuse2.x );
    float lengthOpacity = lerp( StartOpacity, MidOpacity, min( 1.0f, IN.texCoordDiffuse2.x / 0.5f) );
    lengthOpacity = lerp( lengthOpacity, EndOpacity, max( 0.0f, (IN.texCoordDiffuse2.x - 0.5f) / 0.5f) );
   
    float whereWidth = 1.0f - (abs (IN.texCoordDiffuse2.y - 0.5f)) * 2.0f;
    float widthOpacity = lerp( OuterOpacity, InnerOpacity, whereWidth );    
    
    colorSum.a = lengthOpacity * widthOpacity; // * tipOpacity;
    
    float3 colorModulate;
    colorModulate = lerp( ColorStart, ColorMid, min(1, IN.texCoordDiffuse2.x / ColorMidpoint) ).xyz;
    colorModulate = lerp( colorModulate, ColorEnd, max(0, (IN.texCoordDiffuse2.x-ColorMidpoint)/(1.0f-ColorMidpoint)) ).xyz;
  
    colorSum.xyz *= (colorModulate * FinalColorModulate);

    PS_OUTPUT Out;
 	SetPSOutput( Out, colorSum, float4( campos - IN.posWorld.xyz, MRT_FORCE_BLOOM) );
	return Out;
}


//-----------------------------------
PS_OUTPUT PS_Tubed( vertexOutput IN)
{    
    
    float4 colorSum;  
    colorSum = float4(1.0f, 1.0f, 1.0f, 1.0f);
    
    float3 colorModulate;
    colorModulate = lerp( ColorStart, ColorMid, min(1, IN.texCoordDiffuse2.x / ColorMidpoint) ).xyz;
    colorModulate = lerp( colorModulate, ColorEnd, max(0, (IN.texCoordDiffuse2.x-ColorMidpoint)/(1.0f-ColorMidpoint)) ).xyz;
    
     
    float lengthOpacity = lerp( StartOpacity, MidOpacity, min( 1.0f, IN.texCoordDiffuse2.x / 0.5f) );
    lengthOpacity = lerp( lengthOpacity, EndOpacity, max( 0.0f, (IN.texCoordDiffuse2.x - 0.5f) / 0.5f) );
    
     colorSum.xyz *= (colorModulate * FinalColorModulate * lengthOpacity);
        
    PS_OUTPUT Out;
        
    SetPSOutput( Out, colorSum, float4( 0, 0, 0, 0) );
	return Out;
}


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

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

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

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