//https://www.shadertoy.com/view/MldSWB
#define PI 3.14159265359
#define INF 1e10
#define EPS 1e-10
#define ITER_MAX 10000

float crs(vec2 v1, vec2 v2) {
    return v1.x*v2.y - v1.y*v2.x;
}

vec2 rotate(vec2 p, float rad) {
    return mat2(cos(rad), -sin(rad), sin(rad), cos(rad)) * p;
}

// smooth min
// reference: http://iquilezles.org/www/articles/smin/smin.htm
float smin(float a, float b, float k) {
    float res = exp(-k*a) + exp(-k*b);
    return -log(res)/k;
}

float ellipse(vec2 p, vec2 r) {
    return (length(p/r) - 1.0) * min(r.x, r.y);
}

float line(vec2 p, vec2 v1, vec2 v2) {
    p  -= v1;
    vec2 v = v2-v1;
    float t = dot(p, normalize(v));
    if (t<0.0) {
        return length(p);
    } else if (t>length(v)) {
        return length(p-v);
    } else {
        return abs(crs(p, normalize(v)));
    }
}

bool innerTriangle(vec2 p, vec2 v1, vec2 v2, vec2 v3) {
    float c1 = crs(v2-v1, p-v1);
    float c2 = crs(v3-v2, p-v2);
    float c3 = crs(v1-v3, p-v3);
    return (c1>0.0&&c2>0.0&&c3>0.0) || (c1<0.0&&c2<0.0&&c3<0.0);
}

float StarPolygon(vec2 p, int n, int m, float r) {
    float d = INF;
    for (int i=0; i<ITER_MAX; i++) {
        if (i >= n) break;
        
        float rad1 = 2.0*PI*float(i)/float(n);
        float rad2 = 2.0*PI*float(i+m)/float(n);
        vec2 v1 = vec2(cos(rad1), sin(rad1)) * r;
        vec2 v2 = vec2(cos(rad2), sin(rad2)) * r;
        bool flg = innerTriangle(p, vec2(0.0), v1, v2);
        d = min(d, line(p, v1, v2) * (flg?-1.0:1.0));
    }
    return d;
}

vec3 calc(vec2 p) {
    float t = iTime;
    float r = (sin(t*PI)+1.0) * 0.3 + EPS;
    p = rotate(p, (length(p) * 2.0 - t*2.0)*PI);
    float d1 = StarPolygon(p, 5, 2, r);
    float d2 = ellipse(p, vec2(0.01));
    float d = smin(d1, d2, 3.5);
    return vec3(pow(clamp(1.0-d, 0.0, INF), 5.0)) * vec3(0.1, 0.35, 0.95);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
	vec2 uv = (2.0*fragCoord.xy-iResolution.xy) / min(iResolution.x, iResolution.y);
	fragColor = vec4(calc(uv),1.0);
}