// DropShadow

//@Begin_vertex
#ifdef GL_ES
 precision mediump float;
#endif
uniform mat4 transformMatrix;
uniform mat4 projectionMatrix;

attribute vec4 position;
attribute vec2 texCoord;
varying vec2 textureCoordinate;

void main(void)
{
	textureCoordinate = texCoord;
	gl_Position = projectionMatrix * transformMatrix * position;
}
//@End
//@Begin_fragment
#ifdef GL_ES
 precision mediump float;
#endif
varying vec2 textureCoordinate;

uniform float x;
uniform float y;
uniform float angle;
uniform float alpha;
uniform float radius;
uniform vec4 color;

uniform sampler2D imgTexture;

uniform lowp vec4 blendColor;
uniform float fPixelWidth;
uniform float fPixelHeight;

void main(void)
{
	//Source Color
	vec4 fragcolor = texture2D(imgTexture, textureCoordinate);

	//Determine shadow pixel
	vec2 pixel;
	if (angle != 0.0)
	{
		float theta = angle/180.0*3.154159;
		vec2 point = vec2(cos(theta)*x/fPixelWidth-sin(theta)*y/fPixelHeight, sin(theta)*x/fPixelWidth+cos(theta)*y/fPixelHeight);
		pixel = textureCoordinate - vec2(point.x, point.y);
	}
	//No angle, skip some calculations
	else
	{
		pixel = textureCoordinate - vec2(x/fPixelWidth, y/fPixelHeight);
	}

	//Exit if no shadow
	if(pixel.x < 0.0 || pixel.x > 1.0 || pixel.y < 0.0 || pixel.y > 1.0)
	{
		discard;
	}
	else
	{
		vec4 shadow = color;
		shadow.a = texture2D(imgTexture, pixel).a * alpha;
		//Thank, you Wikipedia. Thanks. *sniffs*
		float new_a = 1.0-(1.0-fragcolor.a)*(1.0-shadow.a);
		fragcolor.rgb = (fragcolor.rgb*fragcolor.a+shadow.rgb*shadow.a*(1.0-fragcolor.a))/new_a;
		fragcolor.a = new_a;
	}

    fragcolor *= blendColor;

	gl_FragColor = fragcolor;
}
//@End