uniform sampler2D Color0;
uniform sampler2D Position;
uniform sampler2D Falloff;

uniform mat4 MotionMatrix;

varying vec3 eyeToScreen;

void main()
{
  // recover position from gbuffer
  float depth = texture2D( Position, gl_TexCoord[0].st ).w;//
  // scale?
  depth /= 2000.0;
  depth = min( depth, 0.1 );

  vec4 oldPos;
  oldPos.xyz = depth * normalize( eyeToScreen );
  oldPos.z = -oldPos.z;
  oldPos.w   = 1.0;

  // transform position to previous
  oldPos  = MotionMatrix * oldPos;
  vec2 oldScreen = oldPos.xy / oldPos.w * 0.5 + 0.5;
  vec2 delta = oldScreen - gl_TexCoord[0].st;
  //delta = clamp( delta, -offsetMax, offsetMax );

  delta /= 192.0;//256.0;//

  vec2 texCoord = gl_TexCoord[0].st;

  float attenuation = 1.0 - texture2D( Falloff, texCoord ).r;

  vec4 colorsharp = texture2D( Color0, texCoord );
  vec4 color = colorsharp;
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );
  texCoord += delta;
  color +=  texture2D( Color0, texCoord );

  float luma = dot( vec3( .299, .587, .114 ), color.rgb );
  luma /= 15.0; //8.0;// scale
  luma += 0.5;//luma = max( luma, 0.2 );

  attenuation *= luma;

  //color = mix( colorsharp, (1.0 / 8.0) * color, attenuation );
 
  // using alpha to store edge amount for anti-aliasing
  gl_FragColor = mix( colorsharp, (1.0 / 15.0) * color, attenuation );//vec4( color, 1.0 );
}

/*
void main()
{
  gl_FragColor = texture2D( Color0, gl_TexCoord[0].st );
}
*/