export default `#define MAX_STEPS 1000
#define MAX_DIST 500.
#define E 0.000001

float sdCapsule(vec3 p, vec3 a, vec3 b, float r)
{
    vec3 ab = b - a;
    vec3 ap = p - a;
    
    float t = dot(ab, ap) / dot(ab, ab);
    t = clamp(t, 0., 1.);
    
    vec3 c = a + t * ab;
    return length(p - c) - r;
}

float sdTorus(vec3 p, vec2 r)
{
    float x = length(p.xz) - r.x;
    return length(vec2(x, p.y)) - r.y;
}

float sdBox(vec3 p, vec3 s)
{
    return length(max(abs(p) - s, 0.));
}

float GetDist(vec3 p)
{
    vec4 s = vec4(0, 0, 0, 2);
    
    p.x += iTime;
    float s_dist = (length(mod(p, 10.) - s.xyz) - s.w);
	
    float b_dist1 = sdBox(mod(p + 5., 10.) - vec3(5.) -   vec3(0., sin(p.x * 0.2) * 2., sin(p.x * 0.2) * 2.),      vec3(10., 0.5, 0.5)) * 0.3;
    float b_dist2 = sdBox(mod(p + 5., 10.) - vec3(5.) -   vec3(sin(p.y * 0.2) * 2., 0., sin(p.y * 0.2) * 2.),      vec3(0.5, 10., 0.5)) * 0.3;
    float b_dist3 = sdBox(mod(p + 5., 10.) - vec3(5.) -   vec3(sin(p.z * 0.2) * 2., sin(p.z * 0.2) * 2., 0.),      vec3(0.5, 0.5, 10.)) * 0.3;
    
    float d = min(b_dist1, b_dist2);
    d = min(d, b_dist3);
    return d;
}

float RayMarch(vec3 ro, vec3 rd) {
    float dO = 0.;
    
    for (int i = 0; i < MAX_STEPS; i++)
    {
        vec3 p = ro + rd * dO;
        float dS = GetDist(p);
        dO += dS;
        if (dO > MAX_DIST || dS < E)
           	break;
    }
    
    return dO;
}

vec3 GetNormal(vec3 p)
{
    float d = GetDist(p);
    vec2 e = vec2(0.3, 0);
    
    vec3 n = d - vec3(
        		GetDist(p - e.xyy),
                GetDist(p - e.yxy),
                GetDist(p - e.yyx));
    return (normalize(n));
}

float GetLight(vec3 p)
{
    vec3 lightPos = vec3(0, 10, iTime* 3.);
    lightPos.xz += vec2(sin(iTime), cos(iTime)) * 5.;
    vec3 l = normalize(lightPos - p);
    vec3 n = GetNormal(p);
    
    float dif = clamp(dot(l, n), 0., 1.);
    //float d = RayMarch(p+n*E * 30., l);
    //if (d < length(lightPos - p))
    //    	dif *= 0.1; 
    return (dif);
}

vec2 rotate(vec2 p, float theta)
{
    return vec2(cos(p.x) - sin(p.y), sin(p.x) + cos(p.y));
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = ((fragCoord - .5 * iResolution.xy) / iResolution.y);
    
    vec3 up = normalize(vec3(cos(iTime), sin(iTime), 0));
    vec3 ro = vec3(5. + cos(iTime), 5. + sin(iTime), iTime* 3.);    
    vec3 rd = normalize(vec3(uv.x + 1., uv.y + 1., 1 + 1));
    
    float d = RayMarch(ro, rd);
    
    vec3 p = ro + rd * d;
    
    float dif = GetLight(p);
    vec3 col = vec3(abs(mod((p.x + sin(iTime))/ 10., 1.) - 0.5) * dif, abs(mod((p.y + sin(iTime)) / 10., 1.) - 0.5)  * dif, abs(mod((p.z + sin(iTime)) / 10., 1.) - 0.5)  * dif);
    //col = myvec;
    fragColor = vec4(col,1.0);
}`;
