string Description = "Effect Animate Shader";

float4x4 World : WORLD;
float4x4 WorldViewProj : WorldViewProjection;
float4x4 WorldIT : WorldInverseTranspose;
float4x4 ViewInv : ViewInverse;
float4 eyePos : CameraPosition;
float Time: Time;
float sintime : SinTime;

// lee added these :)
float4x4 View : View;
float4 HudFogColor : Diffuse
<   string UIName =  "Hud Fog Color";    
> = { 0.000000, 0.000000, 0.000000, 0.000000 };
float4 HudFogDist : Diffuse
<   string UIName =  "Hud Fog Dist";    
> = { 1.000000, 0.000000, 0.000000, 0.000000 };

float SpriteRows : Power
<
    string UIName =  "Rows";
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 16.0;
    float UIStep = 1.0;
> = 4.000000;

float SpriteColumns : Power
<
    string UIName =  "Columns";
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 16.0;
    float UIStep = 1.0;
> = 4.000000;

float FramesPerSec : Power
<
    string UIName =  "Frames Per Second";
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 30.0;
    float UIStep = 0.25;
    
> = 15.000000;

float Looptime : Power
<
    string UIName =  "Loop Time";
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 200.0;
    float UIStep = 1.0;
> = 100.000000;

float4 AmbiColor : Ambient
<
    string UIName =  "Ambient Light Color";
> = { 0.010000, 0.010000, 0.010000, 1.000000 };

float4 SurfColor : Diffuse
<
    string UIName =  "Surface Color";
    string UIType = "Color";
> = { 0.050000, 0.050000, 0.050000, 1.000000 };

texture DiffuseMap : DiffuseMap
<
    string Name = "D.dds";
    string type = "2D";
    string ResourceName = "normal_test_D.dds"; 
>;
texture OcclusionMap : DiffuseMap
<
    string Name = "O.dds";
    string type = "2D";
    string ResourceName = "normal_test_N.dds"; 
>;
texture SpriteMap : DiffuseMap
<
    string Name = "N.dds";
    string type = "2D";
    string ResourceName = "atlas_test.dds"; 
>;

sampler2D DiffuseSampler = sampler_state
{
    Texture   = <DiffuseMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};
sampler2D OcclusionSampler = sampler_state
{
    Texture   = <OcclusionMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};
sampler2D SpriteSampler = sampler_state
{
    Texture   = <SpriteMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

struct appdata 
{
    float4 Position	: POSITION;
    float4 UV0		: TEXCOORD0;
    float4 UV1		: TEXCOORD1;
    float4 Normal	: NORMAL;
};

struct vertexOutput
{
    float4 Position    : POSITION;
    float2 TexCoord    : TEXCOORD0;
    float2 TexCoordLM  : TEXCOORD1;
    float3 WorldNormal : TEXCOORD3;
    float4 WPos        : TEXCOORD4;
    float2 atlasUV     :TEXCOORD5;
};

vertexOutput mainVS(appdata IN)   
{
	vertexOutput OUT;
    
    float4 worldSpacePos = mul(IN.Position, World);
    
    OUT.WorldNormal = normalize(mul(IN.Normal, WorldIT).xyz);
    OUT.Position = mul(IN.Position, WorldViewProj);
    OUT.TexCoord  = IN.UV0; 
    OUT.TexCoordLM  = IN.UV1; 
    OUT.WPos =   worldSpacePos;   
    
	float2 DimensionsXY = float2(SpriteRows,SpriteColumns); 
	float2 atlasUVtemp = IN.UV0; 				
	float loopcounter  = floor(Time/Looptime);
	float offset = Looptime*loopcounter;
	float speed =(Time*FramesPerSec) - (offset*FramesPerSec);
	float index = floor( speed);
	float rowCount = floor( (index / DimensionsXY.y) );
	float2 offsetVector = float2(index, rowCount);
	float2 atlas = (1.0 / DimensionsXY) ;
	float2 move = (offsetVector + atlasUVtemp);
	
	OUT.atlasUV = (atlas.xy *move);

    return OUT;
}

float4 mainPS(vertexOutput IN) : COLOR
{
	//clamp SurfColor to prevent zero lighting and overly bright lighting
    SurfColor = clamp(SurfColor,0.1,0.9);
    float4 diffuse = tex2D(DiffuseSampler,IN.TexCoord.xy);
    float4 diffuseContrib = (diffuse*SurfColor) + (diffuse*AmbiColor);
	//adding in a bit of contrast here and clamp value between 0,1 to keep from going BELOW zero and pulling brightness from final lighting
    float4 spriteContrib = tex2D(SpriteSampler,IN.atlasUV.xy)*5*diffuse-0.1;
    spriteContrib = clamp(spriteContrib,0,1);
    float4 result =   diffuseContrib + spriteContrib;
	
	//calculate hud pixel-fog
    float4 cameraPos = mul(IN.WPos, View);
    float hudfogfactor = saturate((cameraPos.z- HudFogDist.x)/(HudFogDist.y - HudFogDist.x));
    float4 hudfogresult = lerp(result,float4(HudFogColor.xyz,0),hudfogfactor*HudFogColor.w);
	
	return hudfogresult;
}

technique All
{
    pass P0
    {
        VertexShader = compile vs_3_0 mainVS();
        PixelShader  = compile ps_3_0 mainPS();
        Lighting         = FALSE;
        FogEnable        = FALSE;
        AlphaBlendEnable = TRUE;
		ZWriteEnable     = TRUE;
    }
}

