const float animSpeed = 1.4;


const vec4 light1 = vec4(1.0000000,0.0000000,0.0000000,1.0000000);
const vec4 light2 = vec4(1.0000000,1.0000000,1.0000000,1.0000000);
const vec3 beamColor = vec3(1.0000000,1.0000000,1.0000000);
const float colorRange =  0.3500000;
const vec3 skyColor1 = vec3(0.0078431,0.0078431,0.0549020);
const vec3 skyColor2 = vec3(0.0313726,0.0313726,0.0470588);
const vec3 upDir = vec3(0,1,0);
const vec3 flip = vec3(-1,1,-1);
const vec2 tri = vec2(0,1);

const float root3 = 1.73205080756887;
const float root2 = 1.4142135623730951;
const float pi = 3.141592653589793;

const mat4 f1mat = mat4(1, 0, 0, 0, 0, -0.8164966, 0.5773503, 0, 0, 0.5773503, 0.8164966, 0, 0, 0.3333333, 0, 1);
const mat4 f2mat = mat4(0.5, -0.8164966, -0.2886752, 0, -0.5, -8.413279E-09, -0.8660254, 0, -0.7071068, -0.5773503, 0.4082483, 0, 0, 0.3333333, 0, 1);
const mat4 f3mat = mat4(0.5, -8.413279E-09, -0.8660254, 0, 0.5, 0.8164966, 0.2886752, 0, -0.7071068, 0.5773503, -0.4082483, 0, 0, 0.3333333, 0, 1);
const mat4 f4mat = mat4(-0.5, 0.8164966, 0.2886752, 0, 0.5, 8.413279E-09, 0.8660254, 0, -0.7071068, -0.5773503, 0.4082483, 0, 0, 0.3333333, 0, 1);
const mat3 warp = mat3(1, 0, 0, 0, 1, 0,1. / root3, 0, -2. / root3);

const vec3 bevelPlane1 = vec3(-0.5980844128924556, 0.8014331132758906, 0.0);
const vec3 bevelPlane2 = vec3(0.0, 0.8014331132758906, -0.5980844128924556);
const vec3 bevelPlane3 = vec3(0.47827732475813883, 0.7365470801275338, 0.47827732475813883);

const float surface = 0.002;
const int gridTraverseIterations = 18;
const int rayMarchIterations = 40;
const float tmax = 1000000000.;

const float tileThickness = 0.25;
const vec3 pyramid1Pos = vec3(0, 0, 0);
const vec3 pyramid2Pos = vec3(2, 16, -24);
const float pyramidSize =  9.;
const vec3 color1 = vec3(0.6468425,0.4779412,1.0000000);
const vec3 color2 = vec3(0.3088235,0.6035901,1.0000000);

const float beamStartHeight = 2.13;
const float beamWideningPoint = 7.82;
const float beamEnd = -0.31;
const float beamTailOff = 2.2;

const float fov = 2.6;

const float loopLen = 8.;
const float segmentDuration = 10.;

const float darkModeRoll = -2.;
const float transitionHeading = -0.8;

// Global variables, some WebGL compilers seems more stable using globals instead
// of function params
float darkMode;
float showInnerBeams;
float scaledTime;
float beamWidth;
vec3 orbitPos;

mat4 rot1, rot2, rotA, rotB;
vec3 ro_alt;
mat3 cam;
vec3 ro;

float fft[8];

vec2 hash2(vec2 n) { return fract(sin(n)*63763.8465261); }
float maxcomp(in vec3 v) { return max(max(v.x, v.y), v.z); }
float mincomp(in vec3 v) { return min(min(v.x, v.y), v.z); }

mat3 rotate_x(float t)
{
    return mat3(1, 0, 0,
                0, cos(t), sin(t),
                0, -sin(t), cos(t));
}
mat3 rotate_y(float t)
{
    return mat3(cos(t), 0, -sin(t),
                0, 1, 0,
                sin(t), 0, cos(t));
}
mat3 rotate_z(float t)
{
    return mat3(cos(t), sin(t), 0,
               -sin(t), cos(t), 0,
                0, 0, 1);
}

float _union(float a, float b)
{
    return min(a, b);
}

float intersect(float a, float b)
{
    return max(a, b);
}

float difference(float a, float b)
{
    return max(a, -b);
}

float plane(vec3 p, vec3 n, float d)
{
    return dot(p, n) - d;
}

mat3 setLookAt(in vec3 ro, in vec3 ta, float roll)
{
    vec3  cw = normalize(ta - ro);
    vec3  cp = upDir;
    vec3  cu = normalize(cross(cw, cp));
    vec3  cv = cross(cu, cw);
    return mat3(cu, cv, cw) * rotate_z(roll);
}

float fftLookup(int index)
{
    if (index < 3)  // Webgl doesn't support non-const array indices
    {
        if (index == 0) return fft[0];
        if (index == 1) return fft[1];
        if (index == 2) return fft[2];
    }
    else
    {
        if (index == 3) return fft[3];
        if (index == 4) return fft[4];
        if (index == 5) return fft[5];
    }
}

// Returns height of both triangles in a square of the grid
vec2 mapH(vec2 ipos)
{
    vec2 realPos = vec2(ipos.x + 0.5 * ipos.y + 0.25, ipos.y * root3/2. + root3/12.);
    ivec2 layer =  ivec2(min(min (-tri + (8. - ipos.x - ipos.y) * 2.,  tri + ipos.x * 2.), tri + ipos.y * 2.));
    return vec2(fftLookup(layer.x), fftLookup(layer.y)) * (2.44 - length(realPos - vec2(0.4444,0.2633) * pyramidSize)*0.44) + 0.37;
}

// Returns color of tile (tri should be 0 or 1)
vec3 mapColour(vec2 ipos, float tri)
{
    vec2 seed =  vec2 (0,631) + (ipos.x*30. + ipos.y + tri * 2041.);
    vec2 random = hash2(seed);
    return vec3(vec3(mix(color1,color2,tri) + (vec3(random.x,random.y,1. - random.x - random.y)- 0.5) * colorRange) );    
}

// SDF function for two triangles
float twotris(vec3 p, vec2 h, int numTris)
{
    const float ss = 0.995; // Having this scale factor stops the normal calculation from spanning multiple cells
    float s = dot(p, vec3(1. / root2, 0., 1. / root2));

    //Vertical planes
    s = intersect(s, -p.x - 0.5*ss); 
    s = intersect(s, -p.z - 0.5*ss); 

    // horizontal planes
    s = intersect(s, p.y - h.x);
    s = intersect(s, -p.y + h.x - tileThickness);

    // bevel
    float bevelH = dot(bevelPlane1, vec3(-0.5, h.x - 0.1, 0));
    s = intersect(s, plane(p, bevelPlane1, bevelH));

    // other bevel
    s = intersect(s, plane(p, bevelPlane2, bevelH));

    // third bevel
    bevelH = bevelPlane3.y*(h.x - 0.1);
    s = intersect(s, plane(p, bevelPlane3, bevelH)); 

    if (numTris == 1)
        return s;
   
    // Now the other triangle...
    bevelH = dot(bevelPlane1, vec3(-0.5, h.y - 0.1, 0));
    float t = plane(p, vec3(-1. / root2, 0., -1. / root2), 0.);
    t = intersect(t, plane(p, vec3(1., 0., 0.), 0.5*ss));
    t = intersect(t, plane(p, vec3(0., 0., 1.), 0.5*ss));
    t = intersect(t, plane(p, vec3(0, 1, 0), h.y));
    t = intersect(t, plane(p, vec3(0, -1, 0), -h.y + tileThickness));
    p = p * flip;
    t = intersect(t, plane(p, bevelPlane1, bevelH));
    t = intersect(t, plane(p, bevelPlane2, bevelH));
    bevelH = bevelPlane3.y*(h.y - 0.1);
    t = intersect(t, plane(p, bevelPlane3, bevelH)); 
    return  _union(s, t);
}

// Get normal from twotris signed distance field
vec3 trisNormal(in vec3 pos, vec2 h, int numTris)
{
    const float eps = 0.0001;
    vec3 n;
    float d = twotris(pos, h, numTris);
    n.x = twotris(vec3(pos.x + eps, pos.y, pos.z), h, numTris) - d;
    n.y = twotris(vec3(pos.x, pos.y + eps, pos.z), h, numTris) - d;
    n.z = twotris(vec3(pos.x, pos.y, pos.z + eps), h, numTris) - d;
    return normalize(n);
}

// return (rgb, dist)
vec4 traceTiles(vec3 ro, in vec3 rd, in float tmin, in float tmax, inout vec3 normal)
{
    /* Transform into triangular space. Squares in this space
     * look like two equilateral triangles in world space
     * This allows me to use iq's grid traversal code  */
    ro = warp * ro; 
    rd = warp * rd; 
    ro += tmin * rd;

    vec3 rdi = 1.0 / rd;
    vec3 rda = abs(rdi);
    vec2 rds = sign(rd.xz);
    vec4 res = vec4(tmax*2.);

    float tgtz_x;
    float tgtz_z;
    float tgtz = 0.;

    // The following if statement checks whether the ray origin is a point within the bounds of the 
    // grid. If it is then we'll just start the grid traversal from where we are. If we're not then 
    // jump to a point on the edge of the grid. The + 0.0001 is so that we don't end up outside
    // the grid and do an early exit on the grid straight away

    if (!(ro.x > 0. && ro.x < pyramidSize && ro.z > 0. && ro.z < pyramidSize))
    {
        if (rds.x > 0.)
        {
            tgtz_x = (0.0001 - ro.x) * rda.x;
        }
        else
        {
            tgtz_x = -(pyramidSize - 0.0001 - ro.x) * rda.x;
        }
        if (rd.z > 0.)
        {
            tgtz_z = ( 0.0001 - ro.z) * rda.z;
        }
        else
        {
            tgtz_z = -(pyramidSize - 0.0001 - ro.z) * rda.z;
        }
        tgtz = max(tgtz_x, tgtz_z);
        ro += tgtz * rd;
    }

    vec2 pos = floor(ro.xz); //always points to corner of cell
    vec2 dis = (pos - ro.xz + 0.5 + rds*0.5) * rdi.xz; // a vector of how many rd vectors we need to add to get to next boundary

    for (int i = 0; i<gridTraverseIterations; i++)
    {
        if (pos.x >= pyramidSize || pos.y >= pyramidSize || pos.x < 0. || pos.y < 0.)
        {
            return vec4(0, 0, 0, tmax);
        }

        if ((pos.x + pos.y) < pyramidSize) // we're iterating over a grid, but we just ignore half so that it's a triangle
        {
            vec2 hs = mapH(pos); 

            float hmax = max(hs.x, hs.y);

            // intersect box
            vec3  ce = vec3(pos.x + 0.5, 0.33 + 0.5 * hmax, pos.y + 0.5);
            vec3  rb = vec3(0.5, hmax*0.5, 0.5);
            vec3  rc = ro - ce;
            float tN = maxcomp(-rdi*rc - rda*rb);
            float tF = mincomp(-rdi*rc + rda*rb);
            if (tN < tF)
            {
                rc.y = rc.y + 0.5 * hmax; // let rc be relative to ground plane
                                    
                float s = tN;

                int numTris = 2;

                if (pos.x + pos.y  + 1. == pyramidSize)
                    numTris = 1;  // Only one triangle in a 'square' for squares along the diagonal

                float h = 1.0;

                for (int j = 0; j<rayMarchIterations; j++) 
                {
                    vec3 p = rc + s * rd;
                    h = twotris(p, hs, numTris);
                    s += h;

                    if (s>tF) break;

                    if (h < surface)
                    {
                        res = vec4(mapColour(pos, step(p.x, -p.z) ) + 
                               (1. -darkMode * 1.3) * vec3(fft[5],fft[6],fft[7]),s + tmin + tgtz);
                        normal = trisNormal(p, hs, numTris);
                        break;
                    }
                }
                if (h < (surface))
                    break;
            }
        }

        // step to next cell          
        vec2 mm = step(dis.xy, dis.yx); 
        dis += mm*rda.xz;
        pos += mm*rds;
    }
    return res;
}

// returns (rgb, dist)
vec4 testPyramidFace(mat4 transform, vec3 ro, in vec3 rd, vec4 current, inout vec3 normal)
{
    ro = (transform*vec4(ro, 1)).xyz + vec3(0.5, -0.2, -0.288) * pyramidSize; 
    
    rd = (transform*vec4(rd, 0)).xyz; //direction
    vec3 rdi = 1.0 / rd;
    vec3 rda = abs(rdi);

    // intersect box
    vec3  ce = vec3(4.4, 0.97, -3.93);
    vec3  rb = vec3(4.69, 1., 3.98);
    vec3  ra = rb;
    vec3  rc = ro - ce;
    float tN = maxcomp(-rdi*rc - rda*ra);
    float tF = mincomp(-rdi*rc + rda*ra);

    // Early out based on enclosing box to improve perf
    if (tN > tF || tN > current.w)
    {
        return current;
    }
    
    vec3 newNormal;
    vec4 r = traceTiles(ro, rd, 0., current.w, newNormal);

    if (r.w < current.w)
    {
        normal = newNormal;
        return r;
    }
    else
    {
        return current;
    }
}

// return (rgb, dist)
vec4 tracePyramid(vec3 centre, vec3 ro, in vec3 rd, vec4 a, mat4 rot, inout vec3 normal)
{
    ro -= centre;
    float d = length(ro);
    float sd = dot(rd,ro);
    
    if ((d*d) - (sd*sd) < 38.)  // Test against an enclosing sphere
    {
        a = testPyramidFace(f1mat * rot, ro, rd,  a, normal);
        a = testPyramidFace(f3mat * rot, ro, rd,  a, normal);
        a = testPyramidFace(f2mat * rot, ro, rd,  a, normal);
        a = testPyramidFace(f4mat * rot, ro, rd,  a, normal);
    }
    return a;
}

// Returns beam intensity for beam
float pyramidBeamFace(mat4 transform, vec3 ro, in vec3 rd, float tmax, float widthMod)
{
    ro = (transform*vec4(ro, 1)).xyz; 
    rd = (transform*vec4(rd, 0)).xyz; //direction
    
    float distance2D = length(ro.xz);
    float lengthToIntersection = distance2D / length(rd.xz);
    float beamExtent = -(lengthToIntersection * rd.y + ro.y - beamStartHeight); 
    float effBeamWidth = beamWidth * widthMod;

    if (beamExtent < beamWideningPoint)
        effBeamWidth += smoothstep(0., 11.7, beamWideningPoint - beamExtent) * 4.37;

    effBeamWidth = min(effBeamWidth, 5.);

    if (dot(ro.xz, rd.xz) < 0.  // viewed in 2D (looking down the beam) our rd goes towards the beam
        && beamExtent > beamEnd)
    {
        float distanceToCenterLine = abs(dot(normalize(vec3(-rd.z, 0, rd.x)),ro));
        if (distanceToCenterLine < effBeamWidth && -beamExtent < 0. * pyramidSize && lengthToIntersection < tmax)
        {
            float d = (effBeamWidth - distanceToCenterLine) / effBeamWidth;
            float tailOff = smoothstep(beamEnd, beamEnd + beamTailOff, beamExtent);
            return d * d * tailOff;
        }
    }
    return 0.;
}

// Returns beam intensity for a pyramid
float pyramidBeam(vec3 centre, vec3 ro, in vec3 rd, in float tmax, mat4 rot, float widthMod)
{
    float beamIntensity = 0.;
    ro -= centre;
    beamIntensity += pyramidBeamFace(f1mat * rot, ro, rd, tmax, widthMod);
    beamIntensity += pyramidBeamFace(f3mat * rot, ro, rd, tmax, widthMod);
    beamIntensity += pyramidBeamFace(f2mat * rot, ro, rd, tmax, widthMod);
    beamIntensity += pyramidBeamFace(f4mat * rot, ro, rd, tmax, widthMod);
    return beamIntensity;
}

// Returns beam intensity for whole scene
float traceBeams(vec3 ro, in vec3 rd, in float tmin, in float tmax)
{
    float beamIntensity = 0.;

    beamIntensity += pyramidBeam(pyramid1Pos, ro, rd, tmax, rot1, 1.);
    beamIntensity += pyramidBeam(pyramid2Pos, ro, rd, tmax, rot2, 1.);

    beamIntensity += 0.4 * pyramidBeam((vec3(1., -3.5, 0)) * 300., ro, rd, tmax, rot1 * rotA, 5.);
    beamIntensity += 0.4 * pyramidBeam((vec3(0., 2.5, -3)) * 300., ro, rd, tmax, rot1 * rotB, 5.);
    beamIntensity += 0.4 * pyramidBeam((vec3(-1., -0.5, 3)) * 300., ro, rd, tmax, rotB * rot1, 5.);

    return beamIntensity;
}

//returns (rgb,dist)
vec4 raytrace(vec3 ro, in vec3 rd, in float tmin, in float tmax, out vec3 normal, inout float beamIntensity)
{
    vec3 normal_;
    vec4 a = vec4(0, 0, 0, tmax);

    a = tracePyramid(pyramid2Pos, ro, rd, a, rot2, normal_);
    a = tracePyramid(pyramid1Pos, ro, rd, a, rot1, normal_);

    beamIntensity += traceBeams(ro,rd,tmin,a.w);

    normal = normal_;
    return a;
}

vec3 light(vec3 n, vec3 ld, vec3 c)
{
    return max(0., dot(n, ld)) *c;
}

vec3 shade(vec3 n)
{
    vec3 s = vec3(0.3, 0.3, 0.4);
    s += light(n, vec3(0.707, 0.707, 0), light1.xyz);
    s += light(n, vec3(-0.707, 0.707, 0), light2.xyz);
    return s;
}

// Star field
vec3 background(vec3 rd)  
{
    vec3 ird = floor(rd* 300. + 300.);
    vec2 f = hash2(vec2(11,17) + ird.x * 1.42 + ird.y + 529.38 + ird.z*12.59);
    float v = max(0.,(f.x-0.995)*200.);
    vec3 col = skyColor2;
    col = mix(col, skyColor1, smoothstep(-1.6, 1.6, rd.y + ((f.x + f.y)-1.) *0.2));
    col *= (1. - abs(rd.y)*0.4) ;
    col += v;
    return col;
}

vec3 bezier(vec3 c1, vec3 c2, vec3 c3, vec3 c4, float t)
{
    float u = 1.0 - t;
    vec3 p = u * u * u * c1; //first term
    p += 3. * u * u * t * c2; //second term
    p += 3. * u * t * t * c3; //third term
    p += t * t * t * c4; //fourth term
    return p;
}
vec3 evalCameraPath(int seg, float pos) 
{
    vec3 c1,c2,c3,c4;
    if (seg == 0)  // Webgl doesn't support non-const array indices
    {
        c1 = vec3(-69.15625,-37.01953,14.89063);
        c2 = vec3(14.695648,-3.814006,13.818855);
        c3 = vec3(-34.1875,-17.6355,4.976563);
        c4 = vec3(-27.82813,-8.571045,7.039063);
    }
    else if (seg == 1)
    {
        c1 = vec3(-27.82813,-8.571045,7.039063);
        c2 = vec3(-21.46875,0.4934082,9.101562);
        c3 = vec3(0.07781601,16.25562,11.31726);
        c4 = vec3(7.265625,15.62646,3.328125);
    }
    else if (seg == 2)
    {
        c1 = vec3(7.265625,15.62646,3.328125);
        c2 = vec3(14.45343,14.99731,-4.661015);
        c3 = vec3(12.55197,5.271613,17.65821);
        c4 = vec3(-2.833279,4.214956,7.524496);
    }
    else if (seg == 3)
    {
        c1= vec3(-2.833279,4.214956,7.524496);
        c2 = vec3(-18.21852,3.158299,-2.609218);
        c3 = vec3(7.624943,-10.94614,8.888105);
        c4 = vec3(20.26089,-2.148565,3.707624);
    }
    else if (seg == 4)
    {
        c1 = vec3(20.26089,-2.148565,3.707624);
        c2 = vec3(32.89684,6.649014,-1.472857);
        c3 = vec3(14.2766,37.21476,-26.53634);
        c4 = vec3(6.103022,24.72848,-31.09239);
    }
    else if (seg == 5)
    {
        c1 = vec3(6.103022,24.72848,-31.09239);
        c2 = vec3(-2.070557,12.24219,-35.64844);
        c3 = vec3(-28.85156,6.828128,-17.52344);
        c4 = vec3(6.235493,-9.495728,10.07811);
    }
    else if (seg == 6)
    {
        c1 = vec3(6.235493,-9.495728,10.07811);
        c2 = vec3(41.32255,-25.81958,37.67965);
        c3 = vec3(30.60639,-26.02213,49.62637);
        c4 = vec3(47.02772,-15.55794,56.22538);
    }
    else if (seg == 7)
    {
        c1 = vec3(47.02772,-15.55794,56.22538);
        c2 = vec3(63.44905,-5.09375,62.82439);
        c3 = vec3(68.44682,0.9838867,59.15281);
        c4 = vec3(0.06239128,0.1552048,-0.006980419);
    }
    return bezier(c1, c2, c3, c4, pos);
}

void animate(float t)
{
    float tos = t / segmentDuration;
    float segment = floor(tos);
    float posInSegment = fract(tos);
    float posInLoop = fract(tos/(loopLen*2.0));
    float pathSeg = mod(segment, loopLen);

    float roll = 0.;
    float heading = 0.;
    darkMode = 0.;

    if (posInLoop > 0.5)
    {
        darkMode = 1.;
        roll = darkModeRoll;
    }
   
    showInnerBeams = smoothstep(0.47,0.49,posInLoop);
    beamWidth = fft[2]*1.6 + 0.16;

    float rotTime = 24. * pi * posInLoop;

    rot1 = mat4(rotate_y(2.5) * rotate_x(rotTime) * rotate_y(-2.5));
    rot2 = mat4(rotate_y(rotTime));
    rotA = mat4(rotate_z(5.));
    rotB = mat4(rotate_x(rotTime*0.08333333));

    if (pathSeg == 0.)
        heading = transitionHeading * (1. - smoothstep(0.2, 1., posInSegment));

    ro = evalCameraPath(int(pathSeg),posInSegment);

    float op = posInLoop*20.*pi+5.2;
    vec2 orbit = vec2(sin(op), cos(op));
    orbitPos = orbit.xxy * 3. * smoothstep(0.01,0.025,abs(0.25-mod((posInLoop+0.25),0.5)));
    cam = rotate_y(heading) *setLookAt(ro, orbitPos, roll);
    ro_alt = length (ro- vec3(0.06239128,0.1552048,-0.006980419)) * vec3(-0.9296872740068233, -0.36815807186055766, 0.011883041) + vec3(-69.15625,-37.01953,14.89063);
}

//returns rgb
vec3 scene(vec2 fragCoord)
{
    vec3 color = vec3(0, 0, 0);
    float beamIntensity =  0.;
    vec2 xy0 = (fragCoord - iResolution.xy * 0.5) * 2. / iResolution.y;

    float sampleScale = 1.;

#ifdef ANTIALIAS  // Doing AA on these two loops separately is quicker than on the whole function
    sampleScale = 0.25;
    float aastep = 1./ iResolution.y;
    for (float i = 0.; i < 1.; i+=0.5)
        for (float j = 0.; j < 1.; j+=0.5)
        {
            vec2 xy = xy0 + vec2(i,j) * aastep;
#else
            vec2 xy = xy0 ;
#endif
            vec3 rd = (cam*vec3(xy , fov));
            rd = normalize(rd);
            vec3 normal;
            vec4 res = raytrace(ro, rd, 0., tmax, normal, beamIntensity); 

            if (res.w < tmax - surface)
            {
                color += vec3(res.xyz  * (0.2 + 0.8 * shade(normal)));
            }
            else
            {
                color += background(rd) * (1.-darkMode);
            }
#ifdef ANTIALIAS
        }
#endif
    color *= sampleScale; 
    beamIntensity *= sampleScale;
    color += beamColor * beamIntensity * 2.5;
    color = clamp(mix(color, 1.- color, darkMode),0.,1.);
   
    if (beamIntensity > 0.165 && showInnerBeams > 0.01)  //todo fade in this effect
    {
        float roll = darkModeRoll * (1.-darkMode);
        mat3 cam = rotate_y(transitionHeading) *setLookAt(ro_alt, orbitPos, roll);
#ifdef ANTIALIAS
        for (float i = 0.; i < 1.; i+=0.5)
            for (float j = 0.; j < 1.; j+=0.5)
            {
            vec2 xy = xy0 + vec2(i,j) * aastep;
#else
            vec2 xy = xy0 ;
#endif
            vec3 rdd = normalize(cam*vec3(xy, fov));
            color +=  (-1. + darkMode*2.) *traceBeams(ro_alt, rdd, 0., tmax)
                      * smoothstep(0.165,0.24, beamIntensity) *2. * showInnerBeams * sampleScale;
            if (darkMode > 0.)
               color += darkMode * background(rdd) * sampleScale;  
            }
#ifdef ANTIALIAS         
    }
#endif   
    return color;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord)
{
    scaledTime = iTime * animSpeed + 40.18; 
     
    // Control points for bezier camera animation
    

    // fourier transform samples from music channel
    fft[0] = texture( iChannel0, vec2( 96 ,0 )/512. ).x;
    fft[1] = texture( iChannel0, vec2(113,0)/512.).x * 1.4; 
    fft[2] = texture( iChannel0, vec2(133,0)/512.).x * 1.3; 
    fft[3] = texture( iChannel0, vec2(146,0)/512.).x; 
    fft[4] = texture( iChannel0, vec2(232,0)/512.).x; 
    fft[5] = texture( iChannel0, vec2(424,0)/512.).x * 1.7; 
    fft[6] = texture( iChannel0, vec2(432,0)/512.).x * 2.; 
    fft[7] = texture( iChannel0, vec2(463,0)/512.).x * 2.; 

    animate(scaledTime);

    fragColor = vec4(scene(fragCoord), 1.) ;
}
