precision highp float;

// source of entropy
uniform sampler2D u_entropy;

// current random
uniform sampler2D u_rand;

// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;

void main() {

    vec4 rand = texture2D(u_rand, v_texCoord);
    vec2 x = rand.ba;
    vec4 s = texture2D(u_entropy, x);

    x = 0.999 * x + 0.001 * s.ba;
    
    // sum the current and 'next' value
    vec2 m = s.xy + rand.xy;
    // the condition is like a modulus 1, such that the value is [0,1], and uniformly distributed
    gl_FragColor = vec4((m.x > 1.0) ? m.x - 1.0 : m.x, (m.y > 1.0) ? m.y - 1.0 : m.y, 4.0 * x * (1.0 - x));

}