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

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


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

float TaggedBloom : TaggedBloom;
float FinalColorModulate : FinalColorModulate;
float OpacityModifier : OpacityModifier;

float4 StartColor : StartColor;
float4 MidColor : MidColor;
float4 EndColor : EndColor;

float StreakLength : StreakLength;
float SegmentStart : SegmentStart;

float OscillationFrequency : OscillationFrequency;
float OscillationTimeScale : OscillationTimeScale;
float OscillationRevealRadius : OscillationRevealRadius;

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;
    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;
    OUT.texCoordDiffuse2 = IN.texCoordDiffuse2;
    return OUT;
}


//-----------------------------------
PS_OUTPUT PS_UnTextured( vertexOutput IN)
{        
    float whereTime = IN.texCoordDiffuse2.x;
    float whereWidth = 1.0f - abs(IN.texCoordDiffuse.y -0.5f) * 2.0f;
    float whereLength = IN.texCoordDiffuse.x;
    float thisLength = StreakLength * IN.texCoordDiffuse2.y; // length deviation
    float4 outColor;
    
    float timeModifier = 0.0f;
    if( abs( whereTime - whereLength ) < thisLength )
    {
    	timeModifier = 1.0f - abs( whereTime - whereLength ) / thisLength;
    }
    
    if( whereLength < SegmentStart )
    {
    	timeModifier *= pow(whereLength/SegmentStart, 8 );
    }
    
    float midPoint = (1.0f - SegmentStart) / 2.0f;
	outColor = lerp( StartColor, MidColor, min(1, whereLength/midPoint) );
 	outColor = lerp( outColor, EndColor, max(0, (whereLength-midPoint)/(1.0f-midPoint)) );     
     
    // modulate by relationship of time to where we are in the length         
    outColor.a *= whereWidth * timeModifier * OpacityModifier;
    
    outColor.xyz *= FinalColorModulate;
    
    if( whereTime < 0.0f )
    	outColor = float4( 0, 0, 0, 0 );
    	
    PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, TaggedBloom  ));
	return Out;
}




//-----------------------------------
PS_OUTPUT PS_Oscillate( vertexOutput IN)
{        
    float whereTime = IN.texCoordDiffuse2.x;
    float whereWidth = 1.0f - abs(IN.texCoordDiffuse.y -0.5f) * 2.0f;
    float whereLength = IN.texCoordDiffuse.x;
    float thisLength = StreakLength * IN.texCoordDiffuse2.y; // lenght deviation
    float4 outColor;
    
    float timeModifier = 0.0f;
    if( abs( whereTime - whereLength ) < thisLength )
    {
    	timeModifier = 1.0f - abs( whereTime - whereLength ) / thisLength;
    }
    
    if( whereLength < SegmentStart )
    {
    	timeModifier *= pow(whereLength/SegmentStart, 8 );
    }
    
    float midPoint = (1.0f - SegmentStart) / 2.0f;
  	outColor = lerp( StartColor, MidColor, min(1, whereLength/midPoint) );
 	outColor = lerp( outColor, EndColor, max(0, (whereLength-midPoint)/(1.0f-midPoint)) );     
    
    
    // dampened oscillation
    float currentPoint = sin( (1.0f -whereTime) * OscillationTimeScale * whereLength * OscillationFrequency ); //1 - ( pow( base, whereTime ) * sin( whereTime * frequency ) );
    currentPoint += 1.0f;  // [0,2]
    currentPoint *= 0.4f;  // [0, .8]
    currentPoint += 0.1f;  // [.1, .9]
    
    float whereSin;
    if( abs(IN.texCoordDiffuse.y - currentPoint) < OscillationRevealRadius )
    {
        whereSin = 1.0f - (abs(IN.texCoordDiffuse.y - currentPoint) / OscillationRevealRadius);
    }
    else 
        whereSin = 0;
            
    // modulate by relationship of time to where we are in the length         
    outColor.a *= whereSin * whereWidth * timeModifier * OpacityModifier;    
    outColor.xyz *= FinalColorModulate;
    
    if( whereTime < 0.0f )
    	outColor = float4( 0, 0, 0, 0 );
    	
	// Force bloom based on how hot    
    float hot = dot( outColor.xyz, float3(0.3,0.59,0.11) );    
    
    PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, TaggedBloom * hot ));
	return Out;
}


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

technique oscillate
{
    pass p0 
    {		
    	VertexShader = compile vs_2_0 VS_Transform();
		PixelShader  = compile ps_2_0 PS_Oscillate();
    }
}
