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

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

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

float Opacity : Opacity;

float4 ColorModulate : ColorModulate;

float4 SphereInnerColor : SphereInnerColor;
float4 SphereOuterColor : SphereOuterColor;
float  FalloffExponent : FalloffExponent;
float FadeAlpha : FadeAlpha;
float FadeScale : FadeScale;
float CalculatedOcclusion : CalculatedOcclusion;
float FinalColorModulate : FinalColorModulate;
float SkipForceBloom : SkipForceBloom;


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

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

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
#ifdef PS_OUTPUT_MRT_CAMERATOWORLD    
    float3 posWorld         : TEXCOORD1;
#endif    
};

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


//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutput IN)
{    
    float4 diffuseTexture = tex2D( MyDiffuseTextureSampler, IN.texCoordDiffuse );
    float4 colorSum;
    
    colorSum = diffuseTexture * Opacity * ColorModulate * (1.0f - CalculatedOcclusion);
        
    colorSum.xyz *= FinalColorModulate;
    
	PS_OUTPUT Out;
#ifdef PS_OUTPUT_MRT_CAMERATOWORLD  	
	SetPSOutput( Out, colorSum, float4( 0,0,0,0 ) );
#else
	SetPSOutput( Out, colorSum, float4( 0,0,0,0 ) );
#endif	

	return Out;
}

//-----------------------------------
PS_OUTPUT PS_UnTextured( vertexOutput IN)
{        
    float4 colorSum;
    float centerX = 0.5f;    
    float centerY = 0.5f;
    
    float2 toCenter = float2(centerX,centerY) - IN.texCoordDiffuse;
    
    float where = length( toCenter );
      
    where = where / length( float2(0.5,0.5) ) ; //0.7071068f; 
    where = saturate( 1.0f - where );
    
    where = pow( where, FalloffExponent );
    
    colorSum = ColorModulate * lerp( SphereOuterColor, SphereInnerColor,  where );
    colorSum.a *= Opacity * (1.0f - CalculatedOcclusion);    
    
    colorSum.xyz *= FinalColorModulate;
    
	PS_OUTPUT Out;
	
#ifdef PS_OUTPUT_MRT_CAMERATOWORLD  	
	SetPSOutput( Out, colorSum, float4( 0,0,0,0 ));
#else
	SetPSOutput( Out, colorSum, float4( 0,0,0,0 ) );
#endif	
	return Out;
}


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

technique unTextured
{
    pass p0 
    {		
    	VertexShader = compile vs_1_1 VS_Transform();
		PixelShader  = compile ps_2_0 PS_UnTextured();
    }
}

