// Happy Mothers' Day!

// Feature requests:
// weaving in and out
// Keziah: side veins on leaves
// "Happy Mothers' Day!"

#define npetals 5.0
#define nflowers 10.0
#define rflower 0.05
#define bigR 0.24
#define TWOPI (3.1415926 * 2.)
#define HOUR (TWOPI / 12.)
#define middleR 0.015
#define zoom 1.7

vec3 lerp(in vec3 from, in vec3 to, in float thingy) {
    return from + (to - from) * thingy;
}

vec2 threeoclock = vec2(cos(0.), sin(0.)),
    fiveoclock = vec2(cos(HOUR * 10.), sin(HOUR * 10.));

void leaf(in float a, inout vec3 col, in vec2 center, in vec2 uv) {
    // Change col to green if in shape of leaf
    float k = rflower * 2.;
    vec2 p1 = center + k * vec2(cos(a), sin(a)),
        p2 = center + k * vec2(cos(a + HOUR*10.), sin(a + HOUR*10.));
    float d1 = distance(uv, p1), d2 = distance(uv, p2);
    if (d1 < k && d2 < k) {
        col = lerp(vec3(0.), vec3(0., 0.1, 0.),
            log(abs(d1 - d2) * 20000.));
    }
}

vec3 fcol(in float f) {
    float sect = TWOPI / nflowers;
    float colAngle = f * sect;
    return vec3(sin(colAngle), sin(colAngle + sect*2.),
        sin(colAngle + sect*4.)) * .5 + vec3(.5);
}

void flower(in float f, in float dir, inout vec3 col, in vec2 center, in vec2 uv) {
    leaf(HOUR, col, center, uv);
    leaf(0., col, center, uv);

    // loop over petals
    for (float p=0.; p < npetals; p+=1.) {
        float a = p * TWOPI / npetals + sin(iTime) * 6. * dir;
        vec2 petalc = center + rflower * vec2(cos(a), sin(a));
        float d = distance(uv, petalc);
        if (d < rflower) {
            col = fcol(f) * (0.95 + 0.05 * sin(d * 600.)) *
                (1. - .2 * pow(d/rflower, 7.0));
        }
    }

    float duc = distance(uv, center);
    if (duc < middleR) {
        col = vec3(0.9, 0.9, 0.2) * (1. - .4 * pow(duc/middleR, 7.0));
    }


}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 center = vec2(0,0);

    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = (fragCoord - iResolution.xy * .5)/iResolution.xx * zoom;

    // blue sky gradient background
    vec3 col = lerp(vec3(0.8, 1.0, 0.8), vec3(0.8, 0.8, 1.0), uv.y + 0.5);

    for (float f = 0.; f < nflowers; f += 1.) {
        float dir = (int(f) % 2 == 0) ? 1. : -1.;
        float a = f * TWOPI / nflowers + iTime;
        center = bigR * vec2(sin(a), cos(a)) * (1.3 + .4 * dir * sin(iTime));
        flower(f, dir, col, center, uv);
    }

    // Output to screen
    fragColor = vec4(col,1.0);
}
