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

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

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

float FinalColorModulate : FinalColorModulate;

float TaggedBloom : TaggedBloom;
float CircularGradient : CircularGradient;

float4 InnerColor : InnerColor;
float4 MidColor : MidColor;
float4 OuterColor : OuterColor;

float InnerOpacity : InnerOpacity;
float MidOpacity : MidOpacity;
float OuterOpacity : OuterOpacity;

float ColorMidpoint : ColorMidpoint;
float OpacityMidpoint : OpacityMidpoint;


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;
    float2 texCoordDiffuse2	: TEXCOORD1;
};

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


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


//-----------------------------------
PS_OUTPUT PS_Untextured( vertexOutput IN)
{    
    float3 color;
    float opacity;
    float4 outColor;
    
	float where;	
	where = abs( IN.texCoordDiffuse.x - 0.5f );
	where *= 2.0f;
	where = saturate(where);
    
    color = lerp( InnerColor, MidColor, min(1, where/ColorMidpoint) );
    color = lerp( color, OuterColor, max(0, (where-ColorMidpoint)/(1.0f-ColorMidpoint)) );
          
    opacity = lerp( InnerOpacity, MidOpacity, min(1, where/OpacityMidpoint) );
    opacity = lerp( opacity, OuterOpacity, max(0, (where-OpacityMidpoint)/(1.0f-OpacityMidpoint)) );
  
  	outColor = float4( color, opacity );	
	outColor.xyz *= FinalColorModulate;	
	
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( 0, 0, 0, 0) );
	return Out;
}

//-----------------------------------
PS_OUTPUT PS_UntexturedCircular( vertexOutput IN)
{    
    float3 color;
    float opacity;
    float4 outColor;
    
	float where;	
	where = length( IN.texCoordDiffuse - float2(0.5f, 0.5f));
	where /= length( float2(0.5f, 0.5f) );
	where = saturate(where);
    
    color = lerp( InnerColor, MidColor, min(1, where/ColorMidpoint) );
    color = lerp( color, OuterColor, max(0, (where-ColorMidpoint)/(1.0f-ColorMidpoint)) );
          
    opacity = lerp( InnerOpacity, MidOpacity, min(1, where/OpacityMidpoint) );
    opacity = lerp( opacity, OuterOpacity, max(0, (where-OpacityMidpoint)/(1.0f-OpacityMidpoint)) );
  
  	outColor = float4( color, opacity );	
	outColor.xyz *= FinalColorModulate;	
	
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( 0,0,0,0 ) );
	return Out;
}
//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutput IN)
{    
    float4 outColor;
    
    float where;
    float3 color;
    
	where = abs( IN.texCoordDiffuse.x - 0.5f );
	where *= 2.0f;	
	where = saturate(where);
	
    color = lerp( InnerColor, MidColor, min(1, where/ColorMidpoint) );
    color = lerp( color, OuterColor, max(0, (where-ColorMidpoint)/(1.0f-ColorMidpoint)) );          
       
    outColor = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    outColor.xyz *= color;
	outColor.xyz *= FinalColorModulate;
	
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( 0,0,0,0 ) );
	return Out;
}


//-----------------------------------
PS_OUTPUT PS_TexturedCircular( vertexOutput IN)
{    
    float4 outColor;
    
    float where;
    float3 color;
    
	where = length( IN.texCoordDiffuse - float2(0.5f, 0.5f));
	where /= length( float2(0.5f, 0.5f) );
	
	where = saturate(where);
    color = lerp( InnerColor, MidColor, min(1, where/ColorMidpoint) );
    color = lerp( color, OuterColor, max(0, (where-ColorMidpoint)/(1.0f-ColorMidpoint)) );          
       
    outColor = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );
    outColor.xyz *= color;
	outColor.xyz *= FinalColorModulate;
	
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( 0,0,0,0 ) );
	return Out;
}



#ifndef FixedPipe
//-----------------------------------
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 untexturedCircular
{
    pass p0 
    {	
    	VertexShader = compile vs_2_0 VS_Transform();
		PixelShader  = compile ps_2_0 PS_UntexturedCircular();
    }
}

technique texturedCircular
{
    pass p0 
    {	
    	VertexShader = compile vs_2_0 VS_Transform();
		PixelShader  = compile ps_2_0 PS_TexturedCircular();
    }
}
#else
//-----------------------------------
technique untextured
{
    pass p0 
    {	
    	VertexShader = compile vs_2_0 VS_Transform();
		
		//PixelShader  = compile ps_2_0 PS_Untextured();
			PixelShader  = 0;
		
		ColorOp[0] = SelectArg1; ColorArg1[0] = Diffuse; ColorArg2[0] = Diffuse;
		AlphaOp[0] = SelectArg1; AlphaArg1[0] = Diffuse; AlphaArg2[0] = Diffuse;
		SpecularEnable = false;
		Sampler[0] = <DiffuseTextureSampler>;
		
		ColorOp[1] = Disable;
		AlphaOp[1] = Disable;
		
    }
}

technique textured
{
    pass p0 
    {	
    
    	VertexShader = compile vs_2_0 VS_TransformFixed();
		PixelShader  = 0;
		
		ColorOp[0] = SelectArg1; ColorArg1[0] = Texture; ColorArg2[0] = Diffuse;
		AlphaOp[0] = SelectArg1; AlphaArg1[0] = Texture; AlphaArg2[0] = Diffuse;
		SpecularEnable = false;
		Sampler[0] = <DiffuseTextureSampler>;
		
		ColorOp[1] = Disable;
		AlphaOp[1] = Disable;
    }
}

//-----------------------------------
technique untexturedCirculars
{
    pass p0 
    {	
    	VertexShader = compile vs_2_0 VS_Transform();
		
		//PixelShader  = compile ps_2_0 PS_Untextured();
			PixelShader  = 0;
		
		ColorOp[0] = SelectArg1; ColorArg1[0] = Diffuse; ColorArg2[0] = Diffuse;
		AlphaOp[0] = SelectArg1; AlphaArg1[0] = Diffuse; AlphaArg2[0] = Diffuse;
		SpecularEnable = false;
		Sampler[0] = <DiffuseTextureSampler>;
		
		ColorOp[1] = Disable;
		AlphaOp[1] = Disable;
		
    }
}

technique texturedCircular
{
    pass p0 
    {	
    
    	VertexShader = compile vs_2_0 VS_TransformFixed();
		PixelShader  = 0;
		
		ColorOp[0] = SelectArg1; ColorArg1[0] = Texture; ColorArg2[0] = Diffuse;
		AlphaOp[0] = SelectArg1; AlphaArg1[0] = Texture; AlphaArg2[0] = Diffuse;
		SpecularEnable = false;
		Sampler[0] = <DiffuseTextureSampler>;
		
		ColorOp[1] = Disable;
		AlphaOp[1] = Disable;
    }
}
#endif