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

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

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

float FinalColorModulate : FinalColorModulate;

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

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

float ColorMidpoint : ColorMidpoint;
float OpacityMidpoint : OpacityMidpoint;

float Age : Age;

// Rings
float LayerBlend : LayerBlend;

float SineSpeed0 : SineSpeed0;
float SineSpeed1 : SineSpeed1;
float SineFreq0 : SineFreq0;
float SineFreq1 : SineFreq1;

float OverallOpacity : OverallOpacity;

float4x4 UVRotate0 : UVRotate0;
float4x4 UVRotate1 : UVRotate1;


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         : 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;
}


//-----------------------------------
PS_OUTPUT PS_Untextured( vertexOutput IN)
{    
    float3 color;
    float opacity;
    
    float3 color2;
    float opacity2;
   
    float4 outColor;
    
    float4 noiseTexture0;
    float4 noiseTexture1;
    float4 mixedNoiseTexture;
    
    float4 baseUVs = float4(IN.texCoordDiffuse.x,0,IN.texCoordDiffuse.y,1);      
    float4 uvTransformed = mul( baseUVs, UVRotate0 );
    float2 uv0 =     float2( uvTransformed.x, uvTransformed.z );    
    uv0 += float2( 0.5f, 0.5f );    
    noiseTexture0 = tex2D( DiffuseTextureSampler, uv0);
	
	uvTransformed = mul( baseUVs, UVRotate1 );
    uv0 =     float2( uvTransformed.x, uvTransformed.z );    
    uv0 += float2( 0.5f, 0.5f );    
    noiseTexture1 = tex2D( DiffuseTextureSampler, uv0);
    
    mixedNoiseTexture = lerp( noiseTexture0, noiseTexture1, 0.5f );
	    
	float where;	
	where = length( IN.texCoordDiffuse - float2(0.5f, 0.5f));
	where /= length( float2(0.5f, 0.5f) );
	
	where = saturate(where);
	
	float whereSine = ( 1.0f + sin( -(Age*SineSpeed0) + where * SineFreq0 ) ) / 2.0f;
	float whereSine2 = ( 1.0f + sin( -(Age*SineSpeed1) + where * SineFreq1 ) ) / 2.0f;
	
	float whereColor;
	float whereColor2;
	
	whereColor = whereSine;
	whereColor2 = whereSine2;
	
	whereColor = saturate(whereColor);
    
    // color
    color = lerp( InnerColor, MidColor, min(1, whereColor/ColorMidpoint) );
 	color = lerp( color, OuterColor, max(0, (whereColor-ColorMidpoint)/(1.0f-ColorMidpoint)) );
  
    // opacity
    opacity = lerp( InnerOpacity, MidOpacity, min(1, whereColor/OpacityMidpoint) );
 	opacity = lerp( opacity, OuterOpacity, max(0, (whereColor-OpacityMidpoint)/(1.0f-OpacityMidpoint)) );
  
    
    // 2nd layer
    whereColor2 = saturate(whereColor2);
    // color
    color2 = lerp( InnerColor, MidColor, min(1, whereColor2/ColorMidpoint) );
 	color2 = lerp( color2, OuterColor, max(0, (whereColor2-ColorMidpoint)/(1.0f-ColorMidpoint)) );
 
    
    // opacity
    opacity2 = lerp( InnerOpacity, MidOpacity, min(1, whereColor2/OpacityMidpoint) );
 	opacity2 = lerp( opacity, OuterOpacity, max(0, (whereColor2-OpacityMidpoint)/(1.0f-OpacityMidpoint)) );
  
  
    // fade out edges
	opacity *= ( 1.0f - where );	
	opacity2 *= ( 1.0f - where );
	
	
  	outColor = lerp( float4( color, opacity ), float4( color2, opacity2 ), LayerBlend );
	
	//outColor.xyz *= noiseTexture;
	outColor.xyz *= FinalColorModulate;	
	
	outColor.w *= OverallOpacity * mixedNoiseTexture.x;
		
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, 0. ) );
	return Out;
}


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



//-----------------------------------
technique untextured
{
    pass p0 
    {	
    	VertexShader = compile vs_3_0 VS_Transform();
		PixelShader  = compile ps_3_0 PS_Untextured();
    }
}

technique textured
{
    pass p0 
    {	
    	VertexShader = compile vs_3_0 VS_Transform();
		PixelShader  = compile ps_3_0 PS_Textured();
    }
}
