out vec4 fragColor;
in lowp vec2 tc;
in lowp float aspect;
uniform lowp float phase;
in lowp float dpth;
uniform lowp float feedback;

uniform mediump float numStages;




float frac(float v)
{
  return v - floor(v);
}

float lineAt(float dist) {
    // float a = dist - 0.2;
    float _LineThickness = 0.007;
    float distanceChange = 0.003;
    float _LineDistance = 0.15;
    float majorLineDistance = abs(frac(dist / _LineDistance + 0.5) - 0.5) * _LineDistance;

    // return 1.0 - smoothstep(majorLineDistance, _LineThickness - distanceChange, _LineThickness + distanceChange);
    float d = abs(dist - 0.25);
    float a = max(0.0, 1.0 - d*d*40000.0);
    // a = a*a*a*a;
float smoothing = 0.003;
float f = 0.2;
    float alpha = sin(dist);//smoothstep(f + smoothing, f - smoothing, dist);
    // a = smoothstep(a, 0.2, 0.3);
    return a;

    // return a;
}

float fun(float angle) {
// return sin(angle*3.0 - phase*3.0);
return sin(angle*3.0 - phase*3.0)
+ sin(angle*5.0 - phase*10.0)*feedback*0.1;
}
void main()
{

    vec2 d = tc - vec2(0.5);
    d.y += 0.15;
    d.y *= aspect;
    
    vec4 col = vec4(0.2, 0.2, 0.2, 1.0);

    float amt = 0.0;

    for(float i = 0.0; i < numStages*2.0; i++) {
        float a = length(d);

        float angle = atan(d.y, d.x);
        angle += i*0.13*feedback;
        angle -= a*2.4*dpth*feedback;
        //float dpth =0.5;
        a = a * (1.0 - dpth) + a * dpth*2.1*(0.8 + 0.2* (fun(angle)))*(i/numStages/2.0);// + a * dpth*(0.8 + 0.2* sin(angle*2.0 - phase*0.4));
        a = lineAt(a);
        a *= i / numStages/2.0;
        // a = a - 0.1;
        // a = sin(a*40.0-phz) * (1.0 - smoothstep(0.4, 0.5, a));

        amt += a;
        
    }
    fragColor = col*min(amt*0.75, 1.0);
}
