#version 330 core

in vec2 UV;

out vec4 color;

uniform sampler2D clearTex;
uniform sampler2D blurTex;
uniform float iGlobalTime;

float nrand(vec2 n)
{
  return fract(sin(dot(n.xy, vec2(19.9898, 78.233))) * 43758.5453);
}

void main()
{
  vec3 mval = vec3(0.0);
  mval += texture(clearTex, UV).rgb / 2.0;
  mval += texture(blurTex, UV).rgb;
  mval = max(vec3(0.0), mval - 0.5);
  mval *= mval;

  float flicker =  0.5 + nrand(vec2(iGlobalTime));
  flicker *= (flicker);
  //flicker = sqrt(flicker);
  //flicker = pow(flicker, 1.0/8.0);
  //mval *= flicker;
  mval *= mix(vec3(0.0), mval, flicker);

  mval += texture(clearTex, UV).rgb;
  mval = clamp(mval, vec3(0.0), vec3(1.0));

  color = vec4(mval, 1.0);
}