#define POINTS 32 // try between 2 and 256, gets slow fast
#define PI 3.1415926536
#define TAU (2.0 * PI)

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
	vec2 uv = fragCoord.xy / iResolution.xy;

    float bias = 0.0001;
    float power = 2.0;
    // mouse x controls bias, mouse y controls power
	vec2 mouse = iMouse.xy / iResolution.xy;
    bias = iMouse.z <= 0.0 ? bias : pow(10.0, (-0.5 + mouse.x) * 10.0);
	power = iMouse.z <= 0.0 ? power : 0.5 + mouse.y * 9.5;
    
    float cN = 0.0;
    // array used to store contributions in first loop
    float contribution[POINTS];
    
    for (int i = 0; i < POINTS; i++)
    {
        float f = float(i) / float(POINTS) * TAU;
	    vec2 pos = 0.5 + 0.35 * vec2(cos(iTime * 0.25 + f), sin(iTime * 0.8 + f * 2.0));
		pos = uv - pos;
        float dist = length(pos);
        // calculate contribution
    	float c = 1.0 / (bias + pow(dist, power));
        contribution[i] = c;
        // sum total contribution
        cN += c;
    }
    
    // normalize contributions and weigh colors
    vec3 col = vec3(0, 0, 0);
    cN = 1.0 / cN;
    for (int i = 0; i < POINTS; i++)
    {
        float f = float(i) / float(POINTS) * TAU + iTime * 0.1;
	    vec3 pcol = 0.5 + 0.5 * cos(vec3(f * 2.0, f, f * 4.0));
        col += contribution[i] * cN * pcol;
    }

    fragColor = vec4(col, 1.0);
}
