varying vec2 		texcoord;
uniform sampler2D 	tex;
uniform sampler2D	vel;
vec4 				color;
uniform vec2		params;

void main()
{
	color=texture2D(tex,texcoord);

	// MOTION BLUR	//////////////////////////////////
	vec2 v=texture2D(vel, texcoord).ba*0.1-0.05;
	vec2 vInc=v.xy*0.25;
	float vlen=length(v.xy);
	if(vlen>0.0) v.xy=normalize(v.xy)*min(vlen,params.x);
	float count=1.0;
	float dd=0.0;
	
	// Sample the color buffer along the velocity vector.
	vec4 currentColor = texture2D(tex, texcoord+v.xy);
	vec2 d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;
	
	currentColor = texture2D(tex, texcoord+v.xy);
	d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;

	currentColor = texture2D(tex, texcoord+v.xy);
	d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;

	currentColor = texture2D(tex, texcoord+v.xy);
	d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;

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

	currentColor = texture2D(tex, texcoord+v.xy);
	d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;

	currentColor = texture2D(tex, texcoord+v.xy);
	d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;

	currentColor = texture2D(tex, texcoord+v.xy);
	d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;

	currentColor = texture2D(tex, texcoord+v.xy);
	d2=texture2D(vel, texcoord+v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;

	currentColor = texture2D(tex, texcoord-v.xy);
	d2=texture2D(vel, texcoord-v.xy).ba*0.1-0.05;
	vlen=length(d2.xy)*params.y;/*100.0;*/
	dd=clamp(vlen,0.0,1.0);
	color+=currentColor*dd;
	count+=dd;
	v.xy+=vInc;

	//////////////////////////////////////////////////
	
	color/=count;
	gl_FragColor.xyz = max(vec3(0.0),color.xyz);
}
