//https://www.shadertoy.com/view/3dySDm
// infinite blu fire
// need to add text


// useful white noise lattice points
// same for the same values of ix and iy

// yea i basically spammed random stuff
float wnoise (float ix, float iy) {
    // ix %= 10;
    // iy %= 5;
    // return ((600+ix*iy*12453+136*pow(ix+15, iy%16)+sin(iy*1600)*1376+ix*656+iy*75)%1000)/1000;
    
    ix = mod(ix, 10.);
    iy = mod(iy, 5.);
    return mod((600.+ix*iy*12453.+136.*pow(ix+15., mod(iy, 16.) )+sin(iy*1600.)*1376.+ix*656.+iy*75.), 1000.)/1000.;
    
}

float gnoise (float px, float py) {
    float mx = fract(px);
    float my = fract(py);
    px = floor(px);
    py = floor(py);
    // mx = 3.*my*my-2.*mx*mx*mx;
    return mix(
        mix(wnoise(px, py), wnoise(px+1., py), mx),
        mix(wnoise(px, py+1.), wnoise(px+1., py+1.), mx),
        my
    );
}

float fnoise (float px, float py) {
    float s = 0.;
    float a = .5;
    float f = 1.;
    for (int i=0; i<4; ++i) {
        s += a*gnoise(px*f, py*f);
        a *= .5;
        f *= 2.;
    }
    return s;
}

void mainImage (out vec4 fragColor, in vec2 fragCoord) {
    
    // Normalized pixel coordinates (from 0 to 1)
    vec2 uv = fragCoord/iResolution.xy;
    
    float arg = mod(float(iFrame), 50.)/50.;
    float px, py, fi=0., r=0., g=0., b=0.;
    px = (uv.x-.5)*iResolution.x/iResolution.y+.5;
    py = 1.-uv.y;
    fi = .1/((fnoise(px*9., py*9.+arg*5.)*( 
        .03+0.*length(vec2(px-.5, py/2.-.5))*.15+
        0.1/(pow(py, 1.1)+0.1)
    )))-0.5;
    // fi = fnoise(px*7, py*7);
    if (fi <= 0.) {
        r = 0.;
        g = 0.;
        b = 100./255.;
    }
    else if (0. < fi && fi < .7) {
        r = 0.;
        g = fi/0.7;
        // b = 100./255.+100./255.*fi/.7;
        b = 100./255.+155./255.*fi/.7;
    }
    else if (true) {
        r = 1.;
        g = 1.;
        b = 1.;
    }
    fragColor = vec4(r, g, b, 1.);
}








/*
// Old code for "lit texture"
// "Sneaky 2D texture generated using 3D lighting methods"
vec3 sund () {
    return normalize(
        vec3(
            cos(iTime),
            sin(iTime),
            -1.
         )
    );
}

float rand (float x, float y) {
    x = mod(x, 20.)+3.;
    y = mod(y, 20.)+3.;
    // return mod(x*x*256.+y*x*y*30.+256., 1200.)/1200.;
    return mod(245.+x*y+x*200.+floor(y)*20.*x*x, 1200.)/1200.;
}

float ease (float t) {
    return 3.*t*t-2.*t*t*t;
}

float lerp (float a, float b, float t) {
    return a+(b-a)*ease(t);
}

float gnoise (vec2 px) {
    float mx = mod(px.x+1e3, 1.);
    float my = mod(px.y+1e3, 1.);
    px.x = floor(px.x);
    px.y = floor(px.y);
    float tr = rand(px.x+1., px.y);
    float tl = rand(px.x, px.y);
    float bl = rand(px.x, px.y+1.);
    float br = rand(px.x+1., px.y+1.);
    return lerp(
        lerp(tl, tr, mx),
        lerp(bl, br, mx),
        my
    );
}

float fnoise (vec2 px) {
    //return (
    //    gnoise(px/4.)*1.+
    //    gnoise(px/6.)*1.+
    //    gnoise(px/10.)*2.+
    //    gnoise(px/20.)*2.
    //)/6.;
    // learned this here
    // https://thebookofshaders.com/13/
    float sum = 0.;
    float amp = .5;
    float freq = .07;
    for (int i=0; i<4; ++i) {
        sum += gnoise(px*freq)*amp;
        amp *= .5;
        freq *= 2.;
    }
    return sum;
}

float SDF (vec2 px) {
    // return sqrt(px.x*px.x+px.y*px.y);
    return lerp(fnoise(vec2(px.x*.4, px.y*.5)*60.*4.*3.), fnoise(px*60.*2.), .5+.5*sin(iTime) );
}

vec3 dSDF (vec2 px) {
    // so btw 2D so change in z does not affect shape...
    float inf = 1e-3;
    float SDFp = SDF(px);
    return normalize(
        vec3(
            SDF(vec2(px.x+inf, px.y))-SDFp,
            SDF(vec2(px.x, px.y+inf))-SDFp,
            inf
        )
    );
}

vec3 TEX (vec2 px, vec3 d) {
    float SDFp = SDF(px);
    vec3 dSDFp = dSDF(px);
    vec3 TEXp = vec3(0., 0., 0.);
    // TEXp += mod(floor(px.x*7.)+floor(px.y*7.), 2.)*.5;
    // diffuse
    TEXp += .8*(dot(dSDFp, sund())*.5+.5);
    TEXp += .2*clamp(dot(dSDFp, sund()), 0., 1.);
    TEXp = vec3(0., 1., .9)*TEXp;
    // speculsr
    TEXp += .5*pow(clamp(-dot(reflect(d, dSDFp), sund()), 0., 1.), 12.);
    TEXp = clamp(TEXp, 0., 1.);
    
    return TEXp;
}

void mainImage (out vec4 fragColor, in vec2 fragCoord) {
    vec2 screen = (fragCoord-iResolution.xy/2.)/iResolution.x;
    vec3 retina = TEX(screen, normalize(vec3(screen.x, screen.y, .25)) );
    // retina = vec3(fnoise(screen*60.*4.));
    fragColor = vec4(retina, 1.);
}
*/







/*
// Old code: Original code to generate this shader on
// https://www.khanacademy.org/computer-programming/new/pjs
// infinite blu fire
// need to add text
// useful white noise lattice points
// same for the same values of ix and iy
var wnoise = function(ix, iy) {
    // ix %= 10;
    iy %= 5;
    return ((600+ix*iy*12453+136*pow(ix+15, iy%16)+sin(iy*1600)*1376+ix*656+iy*75)%1000)/1000;
};
var gnoise = function(px, py) {
    var mx = px%1;
    var my = py%1;
    px = floor(px);
    py = floor(py);
    // mx = 3*my*my-2*mx*mx*mx;
    return lerp(
        lerp(wnoise(px, py), wnoise(px+1, py), mx),
        lerp(wnoise(px, py+1), wnoise(px+1, py+1), mx),
        my
    );
};
var fnoise = function(px, py) {
    var s = 0;
    var a = 0.5;
    var f = 1;
    for (var i=0; i<4; ++i) {
        s += a*gnoise(px*f, py*f);
        a *= 0.5;
        f *= 2;
    }
    return s;
    // return noise(px, py);
};
var imgs = [];
var FRAMES = 50;
var C = 0;
var index = 0;
var draw = function() {
    if (C < FRAMES) {
        var arg = (C%FRAMES)/FRAMES;
        if (!this.loadPixels) {
            return;
        }
        this.loadPixels();
        var pix = this.imageData.data;
        var px, py, fi=0, r=0, g=0, b=0;
        for (var jndex=0; jndex<1000; ++jndex) {
            px = (index%width)/width;
            py = floor(index/width)/height;
            fi = 0.1/((fnoise(px*9, py*9+arg*5)*( 
                dist(px, py/2, 0.5, 1/2)*0.15+
                0.1/(pow(py, 1.1)+0.1)
            )))-0.5;
            // fi = fnoise(px*7, py*7);
            if (fi <= 0) {
                r = 0;
                g = 0;
                b = 100;
            }
            else if (0 < fi && fi < 0.7) {
                r = 0;
                g = 255*fi/0.7;
                b = 100+100*fi/0.7;
            }
            else if (fi) {
                r = 255;
                g = 255;
                b = 255;
            }
            pix[index*4] = r;
            pix[index*4+1] = g;
            pix[index*4+2] = b;
            ++index;
            if (index >= width*height) {
                break;
            }
        }
        this.updatePixels();
        if (index >= width*height) {
            index = 0;
            imgs[C] = get(0, 0, width, height);
            C++;
	    println(floor(C/FRAMES*100)+"%");
        }
    }
    else {
        image(imgs[C%FRAMES], 0, 0);
        C++;
    }
};
*/


