//Shader code by Mark Blosser
//WEBSITE:  www.hyrumark.com
//EMAIL:  mark@hyrumark.com

//Shader edited for Nomad Mod

//Description:  Shader for dynamic entities, normal mapping and specular.  "LightSource" variable is pulled from FPSC for accurate lighting.
//Textures: 
//"texture_D.dds"      diffuse texture
//"texture_I.dd"       specular texture
//"texture_N.dds"       normal map texture
/************* UNTWEAKABLES **************/

float4x4 World      : WORLD;

float4x4 WorldViewProj : WorldViewProjection;
float4x4 WorldIT : WorldInverseTranspose;
float4x4 ViewInv : ViewInverse;
float4x4 View : View;
float4 eyePos : CameraPosition;
float4 clipPlane : ClipPlane;




/******TWEAKABLES***************************/

float SpecExpon : Power
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 128.0;
    float UIStep = 1.0;
    string UIName =  "specular power";
> = 64.0;


/*********** SPOTFLASH VALUES FROM FPSC **********/

float4 SpotFlashPos;


float4 SpotFlashColor;


float SpotFlashRange //fixed value that FPSC uses
<
string UIName = "SpotFlash Range";

> = {600.00};


/******VALUES PULLED FROM FPSC - NON TWEAKABLE**********/

float4 AmbiColor : Ambient
<
    string UIName =  "Ambient Light Color";
> = {0.1f, 0.1f, 0.1f, 1.0f};

float4 SurfColor : Diffuse
<
    string UIName =  "Surface Color";
    string UIType = "Color";
> = {1.0f, 1.0f, 1.0f, 1.0f};

float4 LightSource
<
    string UIType = "Fixed Light Source";
> = {500000.0f,50000.0f, -0.0f, 1.0f};

//----------------
// FOG sensitivity
//----------------
float4 FogColor : Diffuse
<
    string UIName =  "Fog Color";
    string UIType = "Color";
> = {0.0f, 0.0f, 0.0f, 0.0000001f};



/****************** TEXTURES AND SAMPLERS*********************/



texture DiffuseMap : DiffuseMap
<
    string Name = "D.tga";
    string type = "2D";
>;

//could be anything here - ill,spec,normal,cube
texture EffectMap : DiffuseMap
<
    string Name = "I.tga";
    string type = "2D";
>;

texture NormalMap : DiffuseMap
<
    string Name = "N.tga";
    string type = "2D";
>;


//Diffuse Texture _D
sampler2D DiffuseSampler = sampler_state
{
    Texture   = <DiffuseMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

//Effect Texture _I (could be anything here - ill,spec,normal,cube)
sampler2D EffectSampler = sampler_state
{
    Texture   = <EffectMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

//Effect Texture _N (could be anything here - ill,spec,normal,cube)
sampler2D NormalSampler = sampler_state
{
    Texture   = <NormalMap>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

;



/************* DATA STRUCTS **************/

struct appdata {
    float4 Position	: POSITION;
    float4 UV0		: TEXCOORD0;
    float4 Normal	: NORMAL;
    float4 Tangent	: TANGENT0;
    float4 Binormal	: BINORMAL0;
};



/*data passed to pixel shader*/
struct vertexOutput
{
    float4 Position    : POSITION;
    float2 TexCoord     : TEXCOORD0;
    float3 LightVec	    : TEXCOORD1;
    float3 WorldNormal	: TEXCOORD2;
    float3 WorldTangent	: TEXCOORD3;
    float3 WorldBinorm	: TEXCOORD4;
float Fog : TEXCOORD5; 
    float4 WPos : TEXCOORD6;
    float clip : TEXCOORD7;
};


/*******Vertex Shader***************************/

vertexOutput mainVS(appdata IN)   
{
    
	vertexOutput OUT;
    
    //float4 tempPos = float4(IN.Position, 1);
    float4 worldSpacePos = mul(IN.Position, World);
    
    OUT.WorldNormal = normalize(mul(IN.Normal, WorldIT).xyz);
    OUT.WorldTangent = mul(IN.Tangent, WorldIT).xyz;
    OUT.WorldBinorm = mul(IN.Binormal, WorldIT).xyz;


    OUT.LightVec = normalize (LightSource - worldSpacePos );
    OUT.Position = mul(IN.Position, WorldViewProj);
    OUT.TexCoord  = IN.UV0; 
    
    OUT.WPos =   worldSpacePos; 
    
    // calculate FOG colour
    float4 cameraPos = mul( worldSpacePos, View );
    float fogstrength = cameraPos.z * FogColor.w;
    OUT.Fog = min(fogstrength,1.0); 
    
    // all shaders should send the clip value to the pixel shader (for refr/refl)                                                                     
    OUT.clip = dot(worldSpacePos, clipPlane);                                                                      
                                           

    return OUT;
}


/****************Framgent Shader*****************/


float4 CalcSpotFlash( float3 worldNormal, float3 worldPos )
{
float4 output = (float4)0.0;
float3 toLight = SpotFlashPos.xyz - worldPos.xyz;
float3 lightDir = normalize( toLight );
float lightDist = length( toLight );

float MinFalloff = 200; //falloff start distance - 50,0,.01 are very cool too for lanterns
float LinearFalloff = 1;
float ExpFalloff = .005; // 1/200

float fAtten = 1.0/(MinFalloff + (LinearFalloff*lightDist)+(ExpFalloff*lightDist*lightDist));

SpotFlashPos.w = clamp(0,1,SpotFlashPos.w -.2);


output += max(0,dot( lightDir, worldNormal ) * 2.5*SpotFlashColor*fAtten * (SpotFlashPos.w) );

return output;
}


float4 mainPS(vertexOutput IN) : COLOR
{
    
    // all shaders should receive the clip value                                                                
    clip(IN.clip);

    
    float4 diffuse = tex2D(DiffuseSampler,IN.TexCoord.xy);    //sample diffuse texture    
    float4 effectmap = tex2D(EffectSampler,IN.TexCoord.xy);    //sample specular map texture 
    float4 normalmap = tex2D(NormalSampler,IN.TexCoord.xy) * 2 - 1;  //sample and expand normal map

    float3 Ln = (IN.LightVec);
    float3 Nn = (IN.WorldNormal);
    float3 Tn = (IN.WorldTangent);
    float3 Bn = (IN.WorldBinorm);
    float3 Nb = (normalmap.z * Nn) + (1.0*(normalmap.x * Tn + normalmap.y * Bn));
    Nb = normalize(Nb);

   
     
    
    //normalized view vector for accurate specular highlights from LightSource
    float3 V  = normalize(eyePos - IN.WPos);
	
        
    //half vector
    float3 Hn = normalize(V+Ln);
    
    
   //calculate lighting Half Lamert scale and bias 
    float4 lighting = lit(((dot(Ln,Nb))),dot(Hn,Nb),24);

    SurfColor = clamp(SurfColor,0.2,0.55);
    
    //diffuse light contribution
    float4 diffuseContrib = diffuse *1.0* lighting.y * (SurfColor) +(AmbiColor*diffuse);
    
    //specular light contribution
    float4 specContrib = lighting.z * effectmap*(SurfColor) ;

    //spotflashlighting contribution
    float4 spotflashlighting = CalcSpotFlash ( IN.WorldNormal, IN.WPos.xyz ); 
    
    //final lighting 
    float4 LMfinal = (spotflashlighting+AmbiColor)*diffuse;
       
    
    float4 result =  LMfinal + diffuseContrib + (specContrib);
        
    return float4((result.xyz*(1-IN.Fog))+(FogColor.xyz*IN.Fog),diffuse.w);
    
    }


/****** technique ********************************/

technique dx9textured
{
    pass P0
    {
        // lighting
        Lighting       = FALSE;
        FogEnable      = FALSE;

        // samplers
        //Sampler[0] = (LightmapSampler);
        //Sampler[1] = (DiffuseSampler);
        //Sampler[2] = (IllSpecSampler);
		//Sampler[3] = (NormalSampler);

        // shaders
        VertexShader = compile vs_2_0 mainVS();
        PixelShader  = compile ps_2_0 mainPS();
    }
}
