
// Shader output, position and diffuse color
struct VS_OUTPUT
{
    float4 pos  : POSITION;
    float4 diff : COLOR0;
    float2 Txcoord0 : TEXCOORD0;
};

// The world view and projection matrices
float4x4 World;
float4x4 ViewProj;

float4 EyeLocation;

float4 LightDir0;
float4 LightColor0;
float4 LightColorSpecular0;

float4 LightDir1;
float4 LightColor1;
float4 LightColorSpecular1;

float4 DiffuseIn;
float4 AmbientColor;
float SpecularPower;
float4 SpecularColor;

float TweenPos1;
float TweenPos2;
float TweenPos3;

float GlowSize;

VS_OUTPUT Tween1Glow(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;

	if(TweenPos1 != 0)
	{	
		float invTweenPos = 1 - TweenPos1;
		pos = invTweenPos*Pos0 + TweenPos1*Pos1;
		normal = invTweenPos*Normal0 + TweenPos1*Normal1;	
	}
	
	float3 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);				
			
	wPos += wNormal*GlowSize;
	
    Out.pos = mul(float4(wPos.xyz, 1.0f), ViewProj);	
    Out.Txcoord0 = Txcoord0;
    
	Out.diff = DiffuseIn;		

    return Out;
}
VS_OUTPUT Tween1(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;

	if(TweenPos1 != 0)
	{	
		float invTweenPos = 1 - TweenPos1;
		pos = invTweenPos*Pos0 + TweenPos1*Pos1;
		normal = invTweenPos*Normal0 + TweenPos1*Normal1;	
	}
		
	float4 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);				
		
    Out.pos = mul(wPos, ViewProj);
    Out.Txcoord0 = Txcoord0;
    
    //Set our color 
    
    // 1st light
    float diffuse0 = max(dot(wNormal, LightDir0), 0);  
	float4 diffuseColor0 = LightColor0*diffuse0;

    
    // 2nd light  
    float diffuse1 = max(dot(wNormal, LightDir1), 0);
	float4 diffuseColor1 = LightColor1*diffuse1;
       
       
    //    
    float4 diffuseColorSum = diffuseColor0 + diffuseColor1;
	Out.diff = AmbientColor + DiffuseIn*diffuseColorSum;	
	Out.diff.a = DiffuseIn.a;

    return Out;
}
VS_OUTPUT Tween1Specular(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;

	if(TweenPos1 != 0)
	{	
		float invTweenPos = 1 - TweenPos1;
		pos = invTweenPos*Pos0 + TweenPos1*Pos1;
		normal = invTweenPos*Normal0 + TweenPos1*Normal1;
	}
		
	float4 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);				
		
    Out.pos = mul(wPos, ViewProj);
    Out.Txcoord0 = Txcoord0;
    
    //Set our color
    float3 eyeDir = normalize(EyeLocation - wPos);    
    
    // 1st light
    float diffuse0 = dot(wNormal, LightDir0);  
	float4 diffuseColor0 = LightColor0*max(diffuse0, 0);    
   
    float4 specular0 = 0.0f;
    if(diffuse0 > 0.0f)
    {
		float3 reflection0 = normalize(2*diffuse0*wNormal - LightDir0);
		float baseSpecular = dot(reflection0, eyeDir);
		if(baseSpecular > 0.0f)
			specular0 = pow(baseSpecular, SpecularPower);
    }
	float4 specularColor0 = LightColorSpecular0*specular0;


    
    // 2nd light  
    float diffuse1 = dot(wNormal, LightDir1);
	float4 diffuseColor1 = LightColor1*max(diffuse1, 0);    
    
    float4 specular1 = 0.0f;
    if(diffuse1 > 0.0f)
    {
		float3 reflection1 = normalize(2*diffuse1*wNormal - LightDir1);
		float baseSpecular = dot(reflection1, eyeDir);
		if(baseSpecular > 0.0f)		
			specular1 = pow(baseSpecular, SpecularPower);
    }
	float4 specularColor1 = LightColorSpecular1*specular1;

       
       
    //    
    float4 specularColorSum = specularColor0 + specularColor1;
    float4 diffuseColorSum = diffuseColor0 + diffuseColor1;
	Out.diff = AmbientColor + DiffuseIn*diffuseColorSum + SpecularColor*specularColorSum;		
	Out.diff.a = DiffuseIn.a;

    return Out;
}

VS_OUTPUT Tween2Glow(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1, float4 Pos2  : POSITION2, float blendWeight2 : BLENDWEIGHT2, float3 Normal2 : NORMAL2, float2 Txcoord2 : TEXCOORD2)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;
	TweenPos2 *= blendWeight2;
	float fTemp = TweenPos1 + TweenPos2;

	if(fTemp != 0)
	{	
		float invTweenPos = 1 - TweenPos1;
		float4 pos01 = invTweenPos*Pos0 + TweenPos1*Pos1;
		float3 normal01 = invTweenPos*Normal0 + TweenPos1*Normal1;	
		
		float invTweenPos2 = 1 - TweenPos2;
		float4 pos02 = invTweenPos2*Pos0 + TweenPos2*Pos2;
		float3 normal02 = invTweenPos2*Normal0 + TweenPos2*Normal2;

		pos = (pos01*TweenPos1 + pos02*TweenPos2)/fTemp;
		normal = (normal01*TweenPos1 + normal02*TweenPos2)/fTemp;
	}
	
	float3 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);
	
	wPos += wNormal*GlowSize;
	
    Out.pos = mul(float4(wPos.xyz, 1.0f), ViewProj);
    Out.Txcoord0 = Txcoord0;
    
	Out.diff = DiffuseIn;		

    return Out;
}
VS_OUTPUT Tween2(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1, float4 Pos2  : POSITION2, float blendWeight2 : BLENDWEIGHT2, float3 Normal2 : NORMAL2, float2 Txcoord2 : TEXCOORD2)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;
	TweenPos2 *= blendWeight2;
	float fTemp = TweenPos1 + TweenPos2;

	if(fTemp != 0)
	{	
		float invTweenPos = 1 - TweenPos1;
		float4 pos01 = invTweenPos*Pos0 + TweenPos1*Pos1;
		float3 normal01 = invTweenPos*Normal0 + TweenPos1*Normal1;	
		
		float invTweenPos2 = 1 - TweenPos2;
		float4 pos02 = invTweenPos2*Pos0 + TweenPos2*Pos2;
		float3 normal02 = invTweenPos2*Normal0 + TweenPos2*Normal2;

		pos = (pos01*TweenPos1 + pos02*TweenPos2)/fTemp;
		normal = (normal01*TweenPos1 + normal02*TweenPos2)/fTemp;
	}
	
	float4 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);				
		
    Out.pos = mul(wPos, ViewProj);
    Out.Txcoord0 = Txcoord0;
    
    //Set our color  
    
    // 1st light
    float diffuse0 = max(dot(wNormal, LightDir0), 0);  
	float4 diffuseColor0 = LightColor0*diffuse0;

    
    // 2nd light  
    float diffuse1 = max(dot(wNormal, LightDir1), 0);
	float4 diffuseColor1 = LightColor1*diffuse1;
       
       
    //    
    float4 diffuseColorSum = diffuseColor0 + diffuseColor1;
	Out.diff = AmbientColor + DiffuseIn*diffuseColorSum;
	Out.diff.a = DiffuseIn.a;	

    return Out;
}
VS_OUTPUT Tween2Specular(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1, float4 Pos2  : POSITION2, float blendWeight2 : BLENDWEIGHT2, float blendWeight : BLENDWEIGHT2, float3 Normal2 : NORMAL2, float2 Txcoord2 : TEXCOORD2)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;
	TweenPos2 *= blendWeight2;
	float fTemp = TweenPos1 + TweenPos2;

	if(fTemp != 0)
	{		
		float invTweenPos = 1 - TweenPos1;
		float4 pos01 = invTweenPos*Pos0 + TweenPos1*Pos1;
		float3 normal01 = invTweenPos*Normal0 + TweenPos1*Normal1;	
		
		float invTweenPos2 = 1 - TweenPos2;
		float4 pos02 = invTweenPos2*Pos0 + TweenPos2*Pos2;
		float3 normal02 = invTweenPos2*Normal0 + TweenPos2*Normal2;

		pos = (pos01*TweenPos1 + pos02*TweenPos2)/fTemp;
		normal = (normal01*TweenPos1 + normal02*TweenPos2)/fTemp;
	}
	
	float4 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);				
		
    Out.pos = mul(wPos, ViewProj);
    Out.Txcoord0 = Txcoord0;
    
    //Set our color
    float3 eyeDir = normalize(EyeLocation - wPos);    
    
    // 1st light
    float diffuse0 = dot(wNormal, LightDir0);  
	float4 diffuseColor0 = LightColor0*max(diffuse0, 0);    
   
    float4 specular0 = 0.0f;
    if(diffuse0 > 0.0f)
    {
		float3 reflection0 = normalize(2*diffuse0*wNormal - LightDir0);
		float baseSpecular = dot(reflection0, eyeDir);
		if(baseSpecular > 0.0f)
			specular0 = pow(baseSpecular, SpecularPower);
    }
	float4 specularColor0 = LightColorSpecular0*specular0;


    
    // 2nd light  
    float diffuse1 = dot(wNormal, LightDir1);
	float4 diffuseColor1 = LightColor1*max(diffuse1, 0);    
    
    float4 specular1 = 0.0f;
    if(diffuse1 > 0.0f)
    {
		float3 reflection1 = normalize(2*diffuse1*wNormal - LightDir1);
		float baseSpecular = dot(reflection1, eyeDir);
		if(baseSpecular > 0.0f)		
			specular1 = pow(baseSpecular, SpecularPower);
    }
	float4 specularColor1 = LightColorSpecular1*specular1;
       
       
    //    
    float4 specularColorSum = specularColor0 + specularColor1;
    float4 diffuseColorSum = diffuseColor0 + diffuseColor1;
	Out.diff = AmbientColor + DiffuseIn*diffuseColorSum + SpecularColor*specularColorSum;		
	Out.diff.a = DiffuseIn.a;

    return Out;
}

VS_OUTPUT Tween3Glow(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1, float4 Pos2  : POSITION2, float blendWeight2 : BLENDWEIGHT2, float3 Normal2 : NORMAL2, float2 Txcoord2 : TEXCOORD2, float4 Pos3  : POSITION3, float blendWeight3 : BLENDWEIGHT3, float3 Normal3 : NORMAL3, float2 Txcoord3 : TEXCOORD3)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;
	TweenPos2 *= blendWeight2;
	TweenPos3 *= blendWeight3;
	float fTemp = TweenPos1 + TweenPos2 + TweenPos3;

	if(fTemp != 0)
	{			
		float invTweenPos = 1 - TweenPos1;
		float4 pos01 = invTweenPos*Pos0 + TweenPos1*Pos1;
		float3 normal01 = invTweenPos*Normal0 + TweenPos1*Normal1;	
		
		float invTweenPos2 = 1 - TweenPos2;
		float4 pos02 = invTweenPos2*Pos0 + TweenPos2*Pos2;
		float3 normal02 = invTweenPos2*Normal0 + TweenPos2*Normal2;
	
		float invTweenPos3 = 1 - TweenPos3;
		float4 pos03 = invTweenPos3*Pos0 + TweenPos3*Pos3;
		float3 normal03 = invTweenPos3*Normal0 + TweenPos3*Normal3;		

		pos = (pos01*TweenPos1 + pos02*TweenPos2 + pos03*TweenPos3)/fTemp;
		normal = (normal01*TweenPos1 + normal02*TweenPos2 + normal03*TweenPos3)/fTemp;
	}	
				
	float3 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);
	
	wPos += wNormal*GlowSize;
	
    Out.pos = mul(float4(wPos.xyz, 1.0f), ViewProj);
    Out.Txcoord0 = Txcoord0;
    
	Out.diff = DiffuseIn;		

    return Out;
}
VS_OUTPUT Tween3(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1, float4 Pos2  : POSITION2, float blendWeight2 : BLENDWEIGHT2, float3 Normal2 : NORMAL2, float2 Txcoord2 : TEXCOORD2, float4 Pos3  : POSITION3, float blendWeight3 : BLENDWEIGHT3, float3 Normal3 : NORMAL3, float2 Txcoord3 : TEXCOORD3)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;
	TweenPos2 *= blendWeight2;
	TweenPos3 *= blendWeight3;
	float fTemp = TweenPos1 + TweenPos2 + TweenPos3;

	if(fTemp != 0)
	{			
		float invTweenPos = 1 - TweenPos1;
		float4 pos01 = invTweenPos*Pos0 + TweenPos1*Pos1;
		float3 normal01 = invTweenPos*Normal0 + TweenPos1*Normal1;	
		
		float invTweenPos2 = 1 - TweenPos2;
		float4 pos02 = invTweenPos2*Pos0 + TweenPos2*Pos2;
		float3 normal02 = invTweenPos2*Normal0 + TweenPos2*Normal2;
	
		float invTweenPos3 = 1 - TweenPos3;
		float4 pos03 = invTweenPos3*Pos0 + TweenPos3*Pos3;
		float3 normal03 = invTweenPos3*Normal0 + TweenPos3*Normal3;		

		pos = (pos01*TweenPos1 + pos02*TweenPos2 + pos03*TweenPos3)/fTemp;
		normal = (normal01*TweenPos1 + normal02*TweenPos2 + normal03*TweenPos3)/fTemp;
	}	
				
	float4 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);
	
    Out.pos = mul(wPos, ViewProj);
    Out.Txcoord0 = Txcoord0;
    
    //Set our color   
    
    // 1st light
    float diffuse0 = max(dot(wNormal, LightDir0), 0);  
	float4 diffuseColor0 = LightColor0*diffuse0;

    
    // 2nd light  
    float diffuse1 = max(dot(wNormal, LightDir1), 0);
	float4 diffuseColor1 = LightColor1*diffuse1;
       
       
    //    
    float4 diffuseColorSum = diffuseColor0 + diffuseColor1;
	Out.diff = AmbientColor + DiffuseIn*diffuseColorSum;		
	Out.diff.a = DiffuseIn.a;

    return Out;
}
VS_OUTPUT Tween3Specular(float4 Pos0  : POSITION0, float3 Normal0 : NORMAL0, float2 Txcoord0 : TEXCOORD0, float4 Pos1  : POSITION1, float blendWeight1 : BLENDWEIGHT1, float3 Normal1 : NORMAL1, float2 Txcoord1 : TEXCOORD1, float4 Pos2  : POSITION2, float blendWeight2 : BLENDWEIGHT2, float3 Normal2 : NORMAL2, float2 Txcoord2 : TEXCOORD2, float4 Pos3  : POSITION3, float blendWeight3 : BLENDWEIGHT3, float3 Normal3 : NORMAL3, float2 Txcoord3 : TEXCOORD3)
{
    VS_OUTPUT Out = (VS_OUTPUT)0;
		
	float4 pos = Pos0;
	float3 normal = Normal0;
	
	TweenPos1 *= blendWeight1;
	TweenPos2 *= blendWeight2;
	TweenPos3 *= blendWeight3;
	float fTemp = TweenPos1 + TweenPos2 + TweenPos3;

	if(fTemp != 0)
	{	
		float invTweenPos = 1 - TweenPos1;
		float4 pos01 = invTweenPos*Pos0 + TweenPos1*Pos1;
		float3 normal01 = invTweenPos*Normal0 + TweenPos1*Normal1;	
		
		float invTweenPos2 = 1 - TweenPos2;
		float4 pos02 = invTweenPos2*Pos0 + TweenPos2*Pos2;
		float3 normal02 = invTweenPos2*Normal0 + TweenPos2*Normal2;
	
		float invTweenPos3 = 1 - TweenPos3;
		float4 pos03 = invTweenPos3*Pos0 + TweenPos3*Pos3;
		float3 normal03 = invTweenPos3*Normal0 + TweenPos3*Normal3;		
	
		pos = (pos01*TweenPos1 + pos02*TweenPos2 + pos03*TweenPos3)/fTemp;
		normal = (normal01*TweenPos1 + normal02*TweenPos2 + normal03*TweenPos3)/fTemp;
	}	
				
	float4 wPos = mul(pos, World);
	float3 wNormal = mul(normal, World);
	wNormal = normalize(wNormal);
	
    Out.pos = mul(wPos, ViewProj);
    Out.Txcoord0 = Txcoord0;
    
    //Set our color
    float3 eyeDir = normalize(EyeLocation - wPos);    
    
    // 1st light
    float diffuse0 = dot(wNormal, LightDir0);  
	float4 diffuseColor0 = LightColor0*max(diffuse0, 0);    
   
    float4 specular0 = 0.0f;
    if(diffuse0 > 0.0f)
    {
		float3 reflection0 = normalize(2*diffuse0*wNormal - LightDir0);
		float baseSpecular = dot(reflection0, eyeDir);
		if(baseSpecular > 0.0f)
			specular0 = pow(baseSpecular, SpecularPower);
    }
	float4 specularColor0 = LightColorSpecular0*specular0;


    
    // 2nd light  
    float diffuse1 = dot(wNormal, LightDir1);
	float4 diffuseColor1 = LightColor1*max(diffuse1, 0);    
    
    float4 specular1 = 0.0f;
    if(diffuse1 > 0.0f)
    {
		float3 reflection1 = normalize(2*diffuse1*wNormal - LightDir1);
		float baseSpecular = dot(reflection1, eyeDir);
		if(baseSpecular > 0.0f)		
			specular1 = pow(baseSpecular, SpecularPower);
    }
	float4 specularColor1 = LightColorSpecular1*specular1;
       
       
    //    
    float4 specularColorSum = specularColor0 + specularColor1;
    float4 diffuseColorSum = diffuseColor0 + diffuseColor1;
	Out.diff = AmbientColor + DiffuseIn*diffuseColorSum + SpecularColor*specularColorSum;		
	Out.diff.a = DiffuseIn.a;
	
    return Out;
}


technique Tween1Glow
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween1Glow();
        PixelShader  = NULL;
    }
}
technique Tween2Glow
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween2Glow();
        PixelShader  = NULL;
    }
}
technique Tween3Glow
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween3Glow();
        PixelShader  = NULL;
    }
}

//// 
technique Tween1
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween1();
        PixelShader  = NULL;
    }
}
technique Tween2
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween2();
        PixelShader  = NULL;
    }
}
technique Tween3
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween3();
        PixelShader  = NULL;
    }
}
////
technique Tween1Specular
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween1Specular();
        PixelShader  = NULL;      
    }
}
technique Tween2Specular
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween2Specular();
        PixelShader  = NULL;
    }
}
technique Tween3Specular
{
    pass P0
    {    
        // shaders
        VertexShader = compile vs_1_1 Tween3Specular();
        PixelShader  = NULL;
    }
}

