#version 460 core
#include <flutter/runtime_effect.glsl>

uniform vec2 uSize;
uniform float iTime;
vec2 iResolution;
out vec4 fragColor;

#define PI 3.1415926535897932384626433832795
#define TWO_PI 6.28318530718

#define SCALE_FACTOR 8
#define TIME_SCALE 0.005

mat2 rotate2d(float _angle){
    return mat2(cos(_angle),-sin(_angle),
                sin(_angle),cos(_angle));
}

#define pi 3.14159265

float drawLine (vec2 p1, vec2 p2, vec2 uv, float a)
{
    float r = 0.;
    float one_px = 1. / iResolution.x; //not really one px
    
    // get dist between points
    float d = distance(p1, p2);
    
    // get dist between current pixel and p1
    float duv = distance(p1, uv);

    //if point is on line, according to dist, it should match current uv 
    r = 1.-floor(1.-(a*one_px)+distance (mix(p1, p2, clamp(duv/d, 0., 1.)),  uv));
        
    return r;
}

float drawCircle(vec2 p, float d, vec2 uv)
{
    return (distance(p, uv) <= d) ? 1. : 0.;
}

void main(void) {

    iResolution = uSize;
    vec2 fragCoord = gl_FragCoord.xy; // Get the fragment coordinates
    vec2 center = 2.0 * vec2(fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
    
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;

    float t = iTime/2; //Pro Tip™: multipluy time to go faster!
    
    //in uv space
    vec2 p1 = vec2(0.5 + sin(t)*.2, 0.25 + cos(t+pi)*.1);
    vec2 p2 = vec2(0.5 + sin(t+pi)*.2, 0.25 + cos(t)*.1);
    vec2 p3 = vec2(0.5 + sin(t+pi/2.)*.2, 0.25 + cos(t-.5*pi)*.1);
    vec2 p4 = vec2(0.5 + sin(t-pi/2.)*.2, 0.25 + cos(t+.5*pi)*.1);
    vec2 p5 = vec2(0.5 + sin(t)*.2, 0.75 + cos(t+pi)*.1);
    vec2 p6 = vec2(0.5 + sin(t+pi)*.2, 0.75 + cos(t)*.1);
    vec2 p7 = vec2(0.5 + sin(t+pi/2.)*.2, 0.75 + cos(t-.5*pi)*.1);
    vec2 p8 = vec2(0.5 + sin(t-pi/2.)*.2, 0.75 + cos(t+.5*pi)*.1);
    
    
    float lines = drawLine(p1, p5, uv, 1.)
        		  + drawLine(p2, p6, uv, 1.)
        		  + drawLine(p1, p3, uv, 1.)
        		  + drawLine(p3, p2, uv, 1.)
        		  + drawLine(p1, p4, uv, 1.)
        		  + drawLine(p4, p2, uv, 1.)
        		  + drawLine(p5, p7, uv, 1.)
        		  + drawLine(p7, p6, uv, 1.)
        		  + drawLine(p6, p8, uv, 1.)
        		  + drawLine(p8, p5, uv, 1.)
        		  + drawLine(p3, p7, uv, 1.)
        		  + drawLine(p4, p8, uv, 1.);
    
    float d = 0.003;
    float dots = drawCircle(p1, d, uv)
				 + drawCircle(p2, d, uv)
        		 + drawCircle(p3, d, uv)
				 + drawCircle(p4, d, uv)
        		 + drawCircle(p5, d, uv)
				 + drawCircle(p6, d, uv)
        		 + drawCircle(p7, d, uv)
				 + drawCircle(p8, d, uv);
    
	fragColor = vec4(lines*.6-dots, dots, dots, 1.);
}
