uniform sampler2D tex0;
//uniform float amount, inner, outer;
uniform int time;
uniform float blur;

uint wang_hash(uint seed)
{
    seed = (seed ^ 61) ^ (seed >> 16);
    seed *= 9;
    seed = seed ^ (seed >> 4);
    seed *= 0x27d4eb2d;
    seed = seed ^ (seed >> 15);
    return seed;
}

float rand(uint value)
{
	return float(wang_hash(value)) * (1.0 / 4294967296.0);
}

void main(void)
{
	float amount, inner, outer;

	amount = 1.2;
	inner = 0.7;
	outer = 1.7;

	vec4 color = texture2D(tex0, gl_TexCoord[0].st);

	float distance = length(gl_TexCoord[0].st - vec2(0.5, 0.5)) * 2.0;
	float vig = smoothstep(outer, inner, distance);

	//vec3 vigColor = vec3(0.02, 0.03, 0.12);
	vec3 vigColor = vec3(0.0, 0.01, 0.07);
	//vec3 vigColor = vec3(0);



  vec4 sample;
  vec4 accum = 0.0;
  float sum = 0.0;
  
  blur = 0.02;
  
  vec2 offset = vec2(0,0);
  
  for (uint i = 0; i < 500; i++)
  {
    offset = (-1.0 + 2.0 * vec2(rand(81*i+0), rand(81*i+13))) * (1.0 - vig) * blur;
    sample = texture2D(tex0, gl_TexCoord[0].st + offset);
    accum += sample;
    sum++;  
  }
  accum /= sum;


  float discolor = 1.0;

	vec3 _ = mix(accum.rgb, vigColor, pow(clamp(1.0 - (accum.r + accum.g + accum.b) / 3.0, 0.0, 1.0), 0.5));

	vec3 blurred = mix(accum.rgb, _, (1.0 - vig) * amount);

	vec3 baseColor = mix(_, vec3(_.r * 0.3 + _.g * 0.5 + _.b * 0.2), 1.0);

	gl_FragData[0] = vec4(blurred, color.a);

	//gl_FragData[0] = vec4(mix(accum, vigColor, (1.0 - vig) * amount), color.a);
  //gl_FragData[0] = texture2D(tex0, gl_TexCoord[0].st);
}
