float4x4 worldViewProj : WorldViewProjection;
float4x4 worldView : WorldView;
float4x4 viewProj : ViewProjection;
float4x4 world : World;

float3 campos : CAMERAPOS;
float3 ObjectPos : ObjectPos;

#include "mrt.fx"

texture diffuseTexture : Custom0;
sampler DiffuseTextureSampler = sampler_state 
{
    texture = <diffuseTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

struct vertexInputUnboned {
    float4 position				: POSITION;
    float3 normal				: NORMAL;
    float2 texCoordDiffuse		: TEXCOORD0;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse1	: TEXCOORD0;
    float2 texCoordDiffuse2	: TEXCOORD1;
    float2 texCoordDiffuse3	: TEXCOORD2;
    float2 texCoordDiffuse4	: TEXCOORD3;
    float cameraFacing 		: TEXCOORD4;
    float dist		: TEXCOORD5;
    float3 normalWorld		: TEXCOORD6;
    float3 posWorld			: TEXCOORD7;
};


float FinalColorModulate : FinalColorModulate = 1;

float animframe : anim
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 2.5;
    float UIStep = 0.01;
    string UIName = "anim";
> = 1;

float hotspots : hotspots
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 20.0;
    float UIStep = 0.1;
    string UIName = "hotspots";
> = 8.0;

float hotspotsreduction : hotspots
<
    string UIWidget = "slider";
    float UIMin = 0.0;
    float UIMax = 20.0;
    float UIStep = 0.1;
    string UIName = "hotspotsreduction";
> = 0.1;

float hotpow : hotpow
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 20.0;
    float UIStep = 0.1;
    string UIName = "hotpow";
> = 3.1;

float darklightscale : darklightscale
<
    string UIWidget = "slider";
    float UIMin = 1.0;
    float UIMax = 20.0;
    float UIStep = 0.1;
    string UIName = "darklightscale";
> = 5.3;

float uvscale : uvscale
<
    string UIWidget = "slider";
    float UIMin = 0.01;
    float UIMax = 5.0;
    float UIStep = 0.01;
    string UIName = "uvscale";
> = 1.02;

float glowFalloff : glowFalloff
<
    string UIWidget = "slider";
    float UIMin = 0.01;
    float UIMax = 2.0;
    float UIStep = 0.01;
    string UIName = "glowFalloff";
> = 1.00;

float glowBase : glowBase
<
    string UIWidget = "slider";
    float UIMin = 1.00;
    float UIMax = 2.0;
    float UIStep = 0.01;
    string UIName = "glowBase";
> = 1.57;

float4 darkColor : darkColor
<
    string UIName = "Dark Color";
> = {0.00f, 0.00f, 0.00f, 1.0f};

float4 lightColor : lightColor
<
    string UIName = "L1.0ight Color";
> = {0.50f, 0.00f, 0.00f, 1.0f};

float4 hotColor : hotColor
<
    string UIName = "Hot Color";
> = {0.5f, 0.24f, 0.0f, 1.0f};

float4 FalloffColor : FalloffColor
<
    string UIName = "Falloff Color";
> = {0.5f, 0.24f, 0.0f, 1.0f};

float FalloffExponent : FalloffExponent = 1;
float FalloffRange : FalloffRange = 1;


vertexOutput VS_TransformUnboned(vertexInputUnboned IN) 
{
    float4 pos;
    float4 posLast;
    float3 norm;
    vertexOutput OUT;
        
    pos = mul(IN.position, world);
    norm = mul(IN.normal, world);    
    
    OUT.texCoordDiffuse1 = (IN.texCoordDiffuse * float2(1.325, 1.436) * uvscale + float2(0.343, 0.743) + float2(0.134, 0.1634) * animframe);
    OUT.texCoordDiffuse2 = (IN.texCoordDiffuse * float2(1.436, 1.476) * uvscale + float2(0.765, 0.234) + float2(0.135, -0.1455) * animframe);
    OUT.texCoordDiffuse3 = (IN.texCoordDiffuse * float2(1.325, 1.235) * uvscale + float2(0.635, 0.765) + float2(-0.178, 0.1644) * animframe);
    OUT.texCoordDiffuse4 = (IN.texCoordDiffuse * float2(1.354, 1.345) * uvscale + float2(0.376, 0.245) + float2(-0.165, -0.1532) * animframe);
    
    OUT.hPosition = mul( pos , viewProj);
    OUT.cameraFacing = -mul(IN.normal, worldView).z;
    
    OUT.dist = length( pos/100000 - campos/100000 );
    
    OUT.normalWorld = mul(IN.normal, world);
    OUT.posWorld = pos;
    return OUT;
}

PS_OUTPUT PS_Textured( vertexOutput IN) : COLOR
{
    float4 a = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse1);
    float4 b = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse2);
    float4 c = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse3);
    float4 d = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse4);

	float amount1 = saturate(a.b * b.b * c.b * d.b * darklightscale);
	float glowReduce = pow(clamp(IN.cameraFacing,0.7,1), glowFalloff);
	float amount2 = pow(clamp(a.b * b.b * c.b * d.b * hotspots * (glowBase - glowReduce) - hotspotsreduction,0,3), hotpow);
	float4 lightdark = lerp(darkColor, lightColor, amount1);
	float4 lightdarkhot = lightdark + hotColor * amount2;

#if PerVertexPreference
  // skip fresnel
#else
	// Falloff color	
	float fBase;
	float3 pointToCamera = normalize( campos - ObjectPos );	
	fBase = 1 - dot( pointToCamera, normalize(IN.normalWorld) );
	
	fBase =  pow( fBase, FalloffExponent ) * FalloffRange ;
	lightdarkhot.xyz += FalloffColor * fBase;	
	lightdarkhot.xyz *= FinalColorModulate;
#endif
	
	PS_OUTPUT Out;
	// output distance to 2nd RT.z
    SetPSOutput( Out, lightdarkhot, float4( 0, 0, IN.dist, 1) );
  	return Out;
}


//-----------------------------------
technique unboned
{
    pass p0 
    {		
		VertexShader = compile vs_2_0 VS_TransformUnboned();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}
