#ifdef GL_ES
precision highp float;
#endif

varying highp vec2 vTextureCoord;

uniform vec4 uGlobalColor;
uniform float uTime;
uniform float uDeltaTime;
uniform vec2 uMouse;
uniform vec2 uResolution;

#define C_BG vec3(0.157)
#define C_FG vec3(0.922, 0.859, 0.698)

float circle(vec2 p, vec2 offset, float rad){
    p -= offset;
    return length(p) - rad;
}

//src: https://iquilezles.org/articles/distfunctions/
float smooth_union(float a, float b, float smoothness){
    float h = clamp( 0.5 + 0.5*(a-b)/smoothness, 0.0, 1.0 );
    return mix( a, b, h ) - smoothness*h*(1.0-h);
}

float modI(float a,float b) {
    float m=a-floor((a+0.5)/b)*b;
    return floor(m+0.5);
}

///Simple sdf that positions 4 spheres is space, based on time. Then uses some hardcoded color transformation
///to turn that into funny colors.
vec4 sdf(vec2 p, float t){

    vec2 offset0 = vec2(sin(t / 2.0) , cos((t + 0.1) / 3.0));
    vec2 offset1 = vec2(sin(t / 5.0) , cos((t - 2.5) / 2.0));
    vec2 offset2 = vec2(cos((t+3.141) / 2.0) , sin((t + 3.0) / 3.0));
    vec2 offset3 = vec2(sin((t) / 4.0), cos(t+3.141 / 2.0));

    float sdf = smooth_union(
        smooth_union(circle(p, offset0, 0.3), circle(p, offset1, 0.45), 0.5),
        smooth_union(circle(p, offset2, 0.6), circle(p, offset3, 0.25), 0.3),
        0.4
    );

    float alpha = modI(sdf * 5.0, 1.01);;
    vec3 color = mix(C_BG, C_FG, alpha);

    return vec4(color, 1.0);

}

void main() {
    float aspect_ratio = uResolution.x / uResolution.y;
    vec2 uv = vTextureCoord;
    uv.x *= aspect_ratio;
    gl_FragColor = sdf(uv, uTime);
}
