//
// You are free to use this code in part or whole in any way you seem fit!
// If you find this code useful we would to know how,
// you can find us at www.spellofplay.com feel free to drop by!
//

texture g_texture0 : texture0;
texture g_texture1 : texture1;
texture g_texture2 : texture2;

float g_resolutionX : editable;
float g_resolutionY : editable;
float g_time : editable;
float g_squeeze = 1.8f;



sampler g_diffuseMap = sampler_state {
   Texture = (g_texture0);
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   AddressU = WRAP;
   AddressV = WRAP;
};

sampler g_diffuseMap2 = sampler_state {
   Texture = (g_texture1);
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   AddressU = WRAP;
   AddressV = WRAP;
};

sampler g_ramp = sampler_state {
   Texture = (g_texture2);
   MAGFILTER = LINEAR;
   MINFILTER = LINEAR;
   AddressU = Clamp;
   AddressV = Clamp;
};

struct PERPIXEL_VS_OUTPUT { 
	float4 m_pos   : POSITION;
	float2 m_uv		: TEXCOORD0;
	float4 m_color	: COLOR0;
};

struct PERPIXEL_VS_OUTPUT2 { 
	float4 m_pos   : POSITION;
	float2 m_uv		: TEXCOORD0;
	float4 m_color	: COLOR0;
	float2 m_angleLength		: TEXCOORD1;
};

PERPIXEL_VS_OUTPUT PerPixel_VS_Main(float4 a_pos  : POSITION, float2 a_uv  : TEXCOORD0, float4 a_color : COLOR0) {
   PERPIXEL_VS_OUTPUT Out = (PERPIXEL_VS_OUTPUT) 0; 

   // Output transformed position:
   
   
   Out.m_pos.x = a_pos.x / (g_resolutionX * 0.5f) - 1.0f;
   Out.m_pos.y = 1.0 - (a_pos.y / (g_resolutionY * 0.5f));
   Out.m_pos.z = 0.0f;
   Out.m_pos.w = 1.0f;
   a_uv = a_uv * 2.0f - 1.0f;
   Out.m_uv = a_uv;
   
   Out.m_color.r = a_color.b;
   Out.m_color.g = a_color.g;
   Out.m_color.b = a_color.r;
   Out.m_color.a = a_color.a;
    
   return Out;
}

PERPIXEL_VS_OUTPUT2 PerPixel_VS_Main2(float4 a_pos  : POSITION, float2 a_uv  : TEXCOORD0, float4 a_color : COLOR0) {
   PERPIXEL_VS_OUTPUT2 Out = (PERPIXEL_VS_OUTPUT2 ) 0; 

   // Output transformed position:
   
   
   Out.m_pos.x = a_pos.x / (g_resolutionX * 0.5f) - 1.0f;
   Out.m_pos.y = 1.0 - (a_pos.y / (g_resolutionY * 0.5f));
   Out.m_pos.z = 0.0f;
   Out.m_pos.w = 1.0f;
   a_uv = a_uv * 2.0f - 1.0f;
   Out.m_uv = a_uv;
   
   Out.m_angleLength.x = atan2(Out.m_uv.x, Out.m_uv.y);
   Out.m_angleLength.y = pow(length(Out.m_uv), 1.0 / g_squeeze) * 0.6; // squeeze
   
   Out.m_color.r = a_color.b;
   Out.m_color.g = a_color.g;
   Out.m_color.b = a_color.r;
   Out.m_color.a = a_color.a;
    
   return Out;
}

float4 PerPixel_PS_Main(	float4 a_color : COLOR0, float2 a_uv : TEXCOORD0) : COLOR { 
	float2 uv1;
	float2 uv2;
	
	float phi = atan2(a_uv.x, a_uv.y);
	float len = pow(length(a_uv), 1.0 / g_squeeze) * 0.6; // squeeze
	
	phi += g_time * 0.1;
	uv1.x = cos(phi);
	uv1.y = sin(phi);
	uv1 *= len;
	
	uv1 = (uv1 + 1.0f) / 2.0f;
	
	phi -= g_time * 0.2;
	uv2.x = cos(phi);
	uv2.y = sin(phi);
	uv2 *= len;
	uv2 = (uv2 + 1.0f) / 2.0f;
	
   float4 finalColor = tex2D(g_diffuseMap, uv1) * 0.85f;
	 finalColor += tex2D(g_diffuseMap, uv2) * 0.25f * cos(g_time * 0.5f);
   //finalColor += tex2D(g_diffuseMap2, uv1) * 0.15f * sin(g_time * 0.5f);
   finalColor += tex2D(g_diffuseMap2, uv2) * 0.85f;
   float ramp = finalColor.r;
   float4 rampColor = tex1D(g_ramp, ramp);
   finalColor = (finalColor * finalColor * finalColor + finalColor * a_color) * rampColor.r + rampColor.b * 0.45f;
   finalColor.a = rampColor.g;
   
   //finalColor = tex2D(g_diffuseMap, uv1) * 0.85f;
   
   return finalColor;
}

float4 PerPixel_PS20_Main(	float4 a_color : COLOR0, float2 a_uv : TEXCOORD0, float2 a_angleLenght : TEXCOORD1) : COLOR { 
	float2 uv1 = a_uv;
	float2 uv2 = a_uv;
	
//  	float phi = atan2(a_uv.x, a_uv.y);
	float len = pow(length(a_uv), 1.0 / g_squeeze) * 0.6; // squeeze

	
	//phi += g_time * 0.1;
	a_angleLenght.x += g_time * 0.1f;
	uv1.x = cos(a_angleLenght.x);
	uv1.y = sin(a_angleLenght.x);
	uv1 *= len;
	
    uv1 = (uv1 + 1.0f) / 2.0f;
	
	a_angleLenght.x -= g_time * 0.2;
	uv2.x = cos(a_angleLenght.x);
	uv2.y = sin(a_angleLenght.x);
	uv2 *= len;
	uv2 = (uv2 + 1.0f) / 2.0f;
	
   float4 finalColor = tex2D(g_diffuseMap, uv1) * 0.85f;
	 finalColor += tex2D(g_diffuseMap, uv2) * 0.25f * cos(g_time * 0.5f);
   //finalColor += tex2D(g_diffuseMap2, uv1) * 0.15f * sin(g_time * 0.5f);
   finalColor += tex2D(g_diffuseMap2, uv2) * 0.85f;
   float ramp = finalColor.r;
   float4 rampColor = tex1D(g_ramp, ramp);
   finalColor = (finalColor * finalColor * finalColor + finalColor * a_color) * rampColor.r + rampColor.b * 0.45f;
   finalColor.a = rampColor.g;
   
   //finalColor = tex2D(g_diffuseMap, uv1) * 0.85f;

   
   return finalColor;
}

technique PerPixel_3 {
   pass Single_Pass {
   	ZFunc = ALWAYS;
   	ZEnable = FALSE;
   	ZWriteEnable = FALSE;
      SrcBlend = ONE;
      DestBlend = INVSRCALPHA;
      FogEnable = FALSE;
      Lighting = FALSE;
      AlphaBlendEnable = TRUE;
      CullMode = NONE;
      VertexShader = NULL;
      VertexShader = compile vs_1_1 PerPixel_VS_Main();
      PixelShader = compile ps_3_0 PerPixel_PS_Main();
   }
}

technique PerPixel_1 {
   pass Single_Pass {
   	ZFunc = ALWAYS;
   	ZEnable = FALSE;
   	ZWriteEnable = FALSE;
      SrcBlend = ONE;
      DestBlend = INVSRCALPHA;
      FogEnable = FALSE;
      Lighting = FALSE;
      AlphaBlendEnable = TRUE;
      CullMode = NONE;
      VertexShader = NULL;
      VertexShader = compile vs_1_1 PerPixel_VS_Main();
      PixelShader = compile ps_2_a PerPixel_PS_Main();
   }
}

technique PerPixel_2 {
   pass Single_Pass {
   	ZFunc = ALWAYS;
   	ZEnable = FALSE;
   	ZWriteEnable = FALSE;
      SrcBlend = ONE;
      DestBlend = INVSRCALPHA;
      FogEnable = FALSE;
      Lighting = FALSE;
      AlphaBlendEnable = TRUE;
      CullMode = NONE;
      VertexShader = NULL;
      VertexShader = compile vs_1_1 PerPixel_VS_Main();
      PixelShader = compile ps_2_b PerPixel_PS_Main();
   }
}




technique PerPixel_4 {
   pass Single_Pass {
   	ZFunc = ALWAYS;
   	ZEnable = FALSE;
   	ZWriteEnable = FALSE;
      SrcBlend = ONE;
      DestBlend = INVSRCALPHA;
      FogEnable = FALSE;
      Lighting = FALSE;
      AlphaBlendEnable = TRUE;
      CullMode = NONE;
      VertexShader = NULL;
      VertexShader = compile vs_1_1 PerPixel_VS_Main2();
      PixelShader = compile ps_2_0 PerPixel_PS20_Main();
   }
}
