#define R iResolution
#define T iTime
#define PI 3.141593
#define DtR 0.01745

vec3 rgb2hsv(vec3 c)
{
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

// From iq - https://www.shadertoy.com/view/MsS3Wc
vec3 hsv2rgb_smooth( in vec3 c )
{
    vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );

	rgb = rgb*rgb*(3.0-2.0*rgb); // cubic smoothing	

	return c.z * mix( vec3(1.0), rgb, c.y);
}

float tunnel(vec3 p)
{
    p.z+=T;
    p.xy+=sin(p.z)*.5+sin(p.xz*12.)*.05+sin(p.zy*20.)*.025; 
    return 1.-length(p.xy);
}

float map(vec3 p)
{
    return tunnel(p);
}

void mainImage( out vec4 O, in vec2 U )
{
    vec2 uv = (2.*U-R.xy)/R.y;
    
    vec3 cz = normalize(vec3(0,0,1));
    vec3 cx = normalize(cross(cz,vec3(0,1,0)));
    vec3 cy = normalize(cross(cx,cz));
    
    vec3 z = vec3(cx*uv.x + cy*uv.y + cz*.15);
    vec3 pos = vec3(0);
    
    float d = 0.;
    float att = 0.;
    
    for(int i = 0; i < 256; ++i)
    {
        d=map(pos);
        if(d < .0001) break;
        if(d > 10000.0)break;
        pos+= z * d;
        att+=0.2/(abs(d)+0.2);
    }
    
    vec3 color = rgb2hsv(vec3(.75,.2,0.10));
    color.r=pos.z*0.01+sin(-T*0.05);
    color = hsv2rgb_smooth(color);
    
    if(d < 10000.0)
    {
        color *= 0.3 + att*0.006;
    }
    
   	//color=pow(color,vec3(0.4545));
    color*=1.0-length(uv);
    
    O = vec4(color,1);
}
