float4x4 worldViewProj;

sampler Tex0: register(s0);

struct VS_INPUT 
{
	float4 position		: POSITION0;
	float2 texCoord		: TEXCOORD0;
	float3 light		: TEXCOORD1;
	float3 center		: TEXCOORD2;
	float3 normal 		: NORMAL0;
	float3 tangent		: TANGENT0;
	float4 color		: COLOR0;
};

struct VS_OUTPUT
{
   float4 pos			: POSITION;
   float4 color			: COLOR;
   float2 texCoord		: TEXCOORD0;
   float3 light			: TEXCOORD1;
   float3 center		: TEXCOORD2;
   float3 normal		: TEXCOORD3;
   float3 tangent		: TEXCOORD4;
   float3 up			: TEXCOORD5;
   float4 worldPos		: TEXCOORD6;
};

struct PS_OUTPUT
{
	float4 color : COLOR0;
};

const float3 lightDir = normalize(float3(1,1,0));

void vs( in VS_INPUT input, out VS_OUTPUT Out )
{
   Out.pos = mul(worldViewProj, input.position);
   Out.texCoord = input.texCoord;
   Out.light = input.light;
   Out.normal = input.normal;
   Out.tangent = input.tangent;
   Out.up = cross(input.normal, input.tangent);
   Out.color = input.color;
   Out.worldPos = input.position;
   Out.center = input.center;
}

void ps(in VS_OUTPUT input, out PS_OUTPUT output)
{
    float4 tex = tex2D(Tex0, input.texCoord);
    
    // Recreate z-component of the normal
    float nz = sqrt( 1 - tex.x*tex.x + tex.y*tex.y );
    float3 normal = float3( tex.x+tex.x-1.0f, tex.y+tex.y-1.0f, nz );
    
    float3 n = -normal.x * input.tangent;
    n += normal.y * input.up;
    n += normal.z * input.normal;
    n = normalize(n);

    float intensity = tex.z;
    float alpha = tex.w;

//    float3 lightDir = normalize(float3(1,1,0));
    float d = dot(n, lightDir);

	// give the backside some light without discontinuities
    d = 0.5f + d * 0.5f;
    d = d * d;
	float light = 0.1f + d * 0.8f;
	intensity = light;
    
    float4 color = float4(intensity, intensity, intensity, alpha) * input.color;
    
    float  fFlashRadius = input.light.x;
    float  fFlashIntensity = input.light.y;
    float  l = length(input.worldPos-input.center + tex.z * input.normal * input.light.z);
    float  k = saturate(1-l/fFlashRadius); // falloff

    float4 yellow = float4(1,0.9f,0.8f,alpha);
    float4 red = float4(1,0.5f,0.0f,alpha);
    float4 c1 = lerp(yellow,red,1-k);
    output.color = saturate(color + c1*k*fFlashIntensity);
}

void vs2( in VS_INPUT input, out VS_OUTPUT Out )
{
   Out.pos = mul(worldViewProj, input.position);
   Out.texCoord = input.texCoord;
   Out.light = input.light;
   Out.normal = input.normal;
   Out.tangent = input.tangent;
   Out.up = cross(input.normal, input.tangent);
   Out.color = input.color;
   Out.worldPos = input.position;
   Out.center = input.center;
}

void ps2(in VS_OUTPUT input, out PS_OUTPUT output)
{
    float4 tex = tex2D(Tex0, input.texCoord);
    
    // Recreate z-component of the normal
    float nz = sqrt( 1 - tex.x*tex.x + tex.y*tex.y );
    float3 normal = float3( tex.x+tex.x-1.0f, tex.y+tex.y-1.0f, nz );
    
    float3 n = -normal.x * input.tangent;
    n += normal.y * input.up;
    n += normal.z * input.normal;
    n = normalize(n);

    float intensity = tex.z;
    float alpha = tex.w;

//    float3 lightDir = normalize(float3(1,1,0));
    float d = dot(n, lightDir);

	// give the backside some light without discontinuities
    d = 0.5f + d * 0.5f;
    d = d * d;
	float light = 0.1f + d * 0.8f;
	intensity = 0.9f + light * 0.1f;
    
    float4 color = float4(intensity, intensity, intensity, alpha) * input.color;
    
    float  fFlashRadius = input.light.x;
    float  fFlashIntensity = input.light.y;
    float  l = length(input.worldPos-input.center + tex.z * input.normal * input.light.z);
    float  k = saturate(1-l/fFlashRadius); // falloff

    float4 yellow = float4(1,0.9f,0.8f,alpha);
    float4 red = float4(1,0.5f,0.0f,alpha);
    float4 c1 = lerp(yellow,yellow,1-k);
    output.color = saturate(color + c1*k*fFlashIntensity);
}

void vs3( in VS_INPUT input, out VS_OUTPUT Out )
{
   Out.pos = mul(worldViewProj, input.position);
   Out.texCoord = input.texCoord;
   Out.light = input.light;
   Out.normal = input.normal;
   Out.tangent = input.tangent;
   Out.up = cross(input.normal, input.tangent);
   Out.color = input.color;
   Out.worldPos = input.position;
   Out.center = input.center;
}

void ps3(in VS_OUTPUT input, out PS_OUTPUT output)
{
    float4 tex = tex2D(Tex0, input.texCoord);
    
    // Recreate z-component of the normal
    float nz = sqrt( 1 - tex.x*tex.x + tex.y*tex.y );
    float3 normal = float3( tex.x+tex.x-1.0f, tex.y+tex.y-1.0f, nz );
    
    float3 n = -normal.x * input.tangent;
    n += normal.y * input.up;
    n += normal.z * input.normal;
    n = normalize(n);

//    float3 lightDir = normalize(float3(1,1,0));
    float d = dot(n, lightDir);

	// give the backside some light without discontinuities
    d = 0.5f + d * 0.5f;
	float light = 0.7f + d * 0.3f;
    
    //float s = 1.0f - input.color.w;
    float s = 1.0f;
    float4 color = float4(light*s, light*s, light*s, tex.w) * input.color;

    float  fFlashRadius = input.light.x;
    float  fFlashIntensity = input.light.y;
    float  l = length(input.worldPos-input.center + tex.z * input.normal * input.light.z);

    if (input.worldPos.y < input.center.y - 1.75f)
    {
		float3 p = input.worldPos - input.center;
		p.y = -1.75f;
		l = length(p + tex.z * input.normal * input.light.z);
    }

    float  k = saturate(1-l/fFlashRadius); // falloff

	float alpha = input.color.w*tex.w;

    float4 yellow = float4(1,1,1,alpha);
    float4 red = float4(1,0.5f,0.0f,alpha);
    float4 c1 = lerp(yellow,red,1-k);
    output.color = saturate(color + c1*k*fFlashIntensity);
}
