#version 300 es
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-wear-lib-04-pitting-corrosion
// Macro section of pitted steel beside a chipped sea-green enamel coating.
// WebGL2 fullscreen pass. Set uResolution to drawing-buffer pixels.
precision highp float;
precision highp int;
uniform vec2 uResolution;
out vec4 fragColor;

float hash(vec2 p) {
    uvec2 cell=uvec2(ivec2(floor(p)));
    uint h=(cell.x+731u)*15485863u ^ (cell.y+1871u)*32452843u;
    h=(h^(h>>13u))*49979687u;
    h=(h^(h>>16u))*67867967u;
    return float((h^(h>>11u))>>8u)/16777216.;
}

float noise(vec2 p) {
    vec2 i=floor(p), f=fract(p);
    f=f*f*(3.-2.*f);
    return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
               mix(hash(i+vec2(0,1)),hash(i+1.),f.x),f.y);
}

mat2 rotate(float a) { return mat2(cos(a),sin(a),-sin(a),cos(a)); }

float fbm(vec2 p) {
    mat2 m=mat2(1.73,1.12,-1.12,1.73);
    vec2 a=m*p+11., b=m*a+7.;
    return .53*noise(p)+.265*noise(a)+.135*noise(b)+.07*noise(m*b);
}

float pixel() { return 2./max(1.,min(uResolution.x,uResolution.y)); }

float sq(float x) { return x*x; }

float gauss(float x) { return exp(-sq(x)); }

vec3 heightNormal(float height) {
    return normalize(vec3(-dFdx(height),-dFdy(height),pixel()));
}

vec3 study(vec2 p) {
    vec2 q=rotate(.22)*p;
    float enamelEdge=q.y-.46-.27*sin(2.5*q.x)-.62*(fbm(q*5.)-.5)-.12*(fbm(q*36.)-.5);
    float enamel=smoothstep(-.004,.008,enamelEdge);
    float grain=noise(q*310.), tarnish=fbm(q*5.);
    float depth=0., rim=0., rust=0., throat=0.;
    for(int scale=0;scale<3;scale++){
        float k=float(scale), spacing=.74*exp2(-1.46*k);
        vec2 g=rotate(k*.83)*q/spacing+vec2(3.2,9.1)*k;
        vec2 cell=floor(g);
        for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++){
            vec2 id=cell+vec2(i,j);
            float seed=hash(id+41.);
            vec2 center=id+.10+.80*vec2(hash(id+1.),hash(id+8.));
            vec2 d=g-center;
            d=rotate(seed*6.)*d*vec2(.82+seed*.47,1.);
            float cluster=.66+.48*fbm(center*spacing*3.);
            float radius=(.04+.31*sq(hash(id+4.)))*cluster;
            float perimeter=length(d)/radius+.59*(fbm(g*13.)-.5);
            float bowl=clamp(1.-perimeter,0.,1.);
            // The mouth narrows over a steep dark throat and a shallower, lit floor.
            float wall=smoothstep(.0,.27,bowl);
            float floorLevel=.70+.30*sqrt(bowl);
            depth=max(depth,spacing*radius*wall*floorLevel*(.59+.55*seed));
            throat=max(throat,wall*(.51+.37*seed)*(1.-.32*smoothstep(.39,.88,bowl)));
            rim=max(rim,gauss((perimeter-.99)/.14)*(.43+.55*seed));
            rust=max(rust,gauss(max(0.,perimeter-.78)/.54)*(.42+.42*seed));
        }
    }
    float flake=gauss(enamelEdge/.024);
    float height=mix(-depth+.005*rim,.019+.003*tarnish,enamel);
    vec3 n=heightNormal(height);
    float light=max(0.,dot(n,normalize(vec3(-.63,.56,.75))));
    vec3 bare=mix(vec3(.33,.32,.295),vec3(.62,.61,.56),tarnish);
    bare=mix(bare,vec3(.32,.175,.097)*(.67+.7*tarnish),rust*.78);
    bare*=.50+.55*light+.115*grain;
    float sanding=noise(vec2(q.x*8.,q.y*345.));
    bare+=.052*(sanding-.5)*(1.-rust);
    bare*=1.-.60*throat;
    bare+=vec3(.25,.218,.164)*rim*light;
    vec3 paint=mix(vec3(.075,.225,.214),vec3(.23,.40,.35),tarnish);
    paint*=.89+.14*grain;
    vec3 col=mix(bare,paint,enamel);
    col+=vec3(.36,.24,.126)*flake*(.36+.44*light);
    return col;
}

void main() {
    vec2 position=(2.0*gl_FragCoord.xy-uResolution)/max(1.0,min(uResolution.x,uResolution.y));
    fragColor=vec4(clamp(study(position),0.0,1.0),1.0);
}
