float random(in float x) 
{
    return fract(sin(x)*1e4);
}

float random(in vec2 st) 
{
    return fract(sin(dot(st.xy, vec2(12.9898,78.233)))* 43758.5453123);
}

mat2 rotate2D(float angle)
{
    return mat2(cos(angle), -sin(angle), 
                sin(angle), cos(angle));
}

float flower(vec2 uv, vec2 center, float r, int petals, float smoothR)
{
    vec2 v = uv - center;
    float d = length(v);
    float theta = atan(v.y, v.x);   
    float curve = abs(sin(theta * float(petals))) * (r * 0.85);
    return 1.0 - smoothstep(r - curve - smoothR, r - curve, d);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    const vec2 TILES = vec2(400.0, 300.0);
    const float PI = 3.1415;
    float RATIO = iResolution.x / iResolution.y;
    vec2 TILES_RATIO = TILES * vec2(RATIO, 1.0);
    
    vec2 st = fragCoord.xy / iResolution.xy;   
    st *= TILES;
    st.x *= RATIO;
    
    vec2 ipos = floor(st);
 
    vec3 color = vec3(0.0);
    
    vec2 vel = vec2(0.0, iTime * (0.08 + random(ipos.x)) * 100.0);    
    vec2 anim = floor(st + vel);
	color.g = random(anim) - random(ipos.x);
    
    vec2 rot = st - (TILES_RATIO * 0.5);
    rot = rotate2D(sin(iTime * 0.2) * PI) * rot;
    rot += (TILES_RATIO * 0.5);
    float f1 = flower(rot, TILES_RATIO * 0.5, TILES_RATIO.x * 0.9, 4, 100.0);
    
    rot = st - (TILES_RATIO * 0.5);
    rot = rotate2D(sin(iTime * 0.2 + 32.14) * -PI) * rot;
    rot += (TILES_RATIO * 0.5);
    float f2 = flower(rot, TILES_RATIO * 0.5, TILES_RATIO.x * 0.9, 2, 100.0);

	color.g += 0.3 * f1;
    color.g -= 0.35 * f2;
    color.r = step(0.96, color.g);
    color.b = step(0.9, color.g);
    
    float sm = 0.4;
    color = mix(vec3(0.0), color, smoothstep(0.0, sm, fract(st.x)));
    color = mix(color, vec3(0.0), smoothstep(1.0 - sm, 1.0, fract(st.x)));
     
    fragColor = vec4(color, 1.0);
}
