varying vec2 v_texCoord;
varying vec4 v_fragmentColor;

uniform sampler2D u_texture;
uniform float time;
uniform float noise;
uniform float beam_lenght;
uniform vec3 beam_color;


//////////////////////////////////////////////////////////

void main()
{
	vec2 uv = v_texCoord;
	vec4 color = vec4(0);

	//Fade beam out if it does not hit anything
	if(beam_lenght < 0)
	{
		if(uv.x > 0.9)
		{
			float ratio = 1.0 - (uv.x - 0.9)/0.1;
			color = vec4(beam_color, 1.0) * texture2D( u_texture, v_texCoord).a * (3.5 + 2.0*noise) * ratio;
		}
		else
		{
			color = vec4(beam_color, 1.0)  * texture2D( u_texture, v_texCoord).a * (3.5 + 2.0*noise);
		}
	}
	else
	{
		color = vec4(beam_color, 1.0) * texture2D( u_texture, v_texCoord).a * (3.5 + 2.0*noise);
	}

	//Beam start fade
	float START_FADE_RATIO = uv.x/0.005;
	if(START_FADE_RATIO > 1.0) START_FADE_RATIO = 1.0;

	//Create forward anim for the beam
	float DIST_AMPLIFIER = 1.0;

	float WAVE = (sin(time*-50.0 + uv.x * 100)+1)/2.0 * 0.25;
	gl_FragColor = (color * DIST_AMPLIFIER) * (0.75 + WAVE + 0.25*noise) * START_FADE_RATIO;

	//gl_FragColor = vec4(1,0,0,1);//texture2D( u_texture, v_texCoord);
}