#version 300 es
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-textiles-lib-15-sequins
// Fish-scale sequin embroidery on folded garnet silk, with pierced cups and anchoring thread.
// 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)); }

float capsule(vec2 p,vec2 a,vec2 b) {
    vec2 v=b-a;
    return length(p-a-v*clamp(dot(p-a,v)/dot(v,v),0.,1.));
}

vec3 studio(vec3 n,float rough) {
    vec3 r=reflect(vec3(0.,0.,-1.),n);
    float strip=gauss((r.x+.48*r.y+.32)/(.08+.42*rough));
    float window=smoothstep(-.18-rough,.05+rough,r.y+.43*r.x);
    float warm=gauss((r.y+.2*r.x+.40)/(.12+.26*rough));
    return vec3(.14,.17,.21)+window*vec3(.52,.58,.64)+strip*vec3(.85,.91,.97)+warm*vec3(.57,.37,.19);
}

vec3 study(vec2 p) {
    vec2 t=rotate(-.32)*p;
    float fold=sin(3.9*t.y+.52*sin(2.6*t.x));
    vec2 g=vec2(t.x+.11*sin(2.5*t.y),t.y+.105*sin(2.7*t.x))*vec2(4.05,5.15);
    float weave=.5+.5*sin(285.*t.x+2.*sin(235.*t.y));
    vec3 silk=vec3(.13,.035,.057)*(.65+.30*fold+.42*weave)+.014*fbm(t*120.);
    vec3 col=silk;
    for(int j=-1;j<=1;j++) {
        float row=floor(g.y)+float(j);
        float offset=.5*mod(row,2.);
        for(int i=-1;i<=1;i++){
            vec2 id=vec2(floor(g.x-offset)+float(i),row);
            float seed=hash(id), lean=hash(id+11.);
            // Only the embroidered panel carries scales; its curved selvage reveals silk.
            if(id.y>3.5+.65*sin(id.x*.69) || id.y< -4.6+.54*cos(id.x*.7)) continue;
            vec2 center=id+vec2(.5+offset,.48)+.09*(vec2(seed,lean)-.5);
            vec2 d=rotate(.20*(lean-.5))*(g-center);
            vec2 q=d/vec2(.602,.78);
            float r=length(q), angle=atan(q.y,q.x+.00001);
            float shadow=1.-smoothstep(.87,1.14,length((d-vec2(.043,-.14))/vec2(.61,.78)));
            col*=1.-.49*shadow;
            // An octagonal crease around the dished center changes the reflected softbox.
            float facet=floor((angle+3.14159)*1.27324);
            float direction=(facet+.5)*.785398-3.14159;
            vec2 slope=.42*vec2(cos(direction),sin(direction))*smoothstep(.46,.69,r);
            slope+=.29*q+vec2(.54*(lean-.5)+.24*cos(2.5*t.y),.22+.32*fold+.49*(seed-.5));
            vec3 reflection=studio(normalize(vec3(slope,1.)),.035);
            float champagne=smoothstep(-.56,-.31,sin(id.x*.42+id.y*.59)+.07*seed);
            vec3 foil=mix(vec3(.70,.33,.25),vec3(.88,.71,.47),champagne);
            float burnish=.5+.5*cos(54.*r+.12*angle);
            vec3 metal=foil*(reflection*.84+.09+.028*burnish)+.014*noise(q*120.);
            metal*=.96+.07*cos(angle-.9)*r;
            metal+=.24*gauss((r-.958)/.026)*(.5+.5*cos(angle-2.));
            float hole=length(q-vec2(0.,.04));
            float w=pixel()*4.4;
            metal=mix(metal,silk*.46,1.-smoothstep(.098-w,.098+w,hole));
            vec2 end=vec2(.09*(lean-.45),.99);
            float thread=capsule(q,vec2(.0,.06),end);
            float yarn=1.-smoothstep(.019-w,.019+w,thread);
            metal=mix(metal,vec3(.36,.19,.128)+.19*gauss(thread/.014)*(.65+.35*cos(83.*q.y+10.*q.x)),yarn);
            col=mix(col,metal,1.-smoothstep(1.-w,1.+w,r));
        }
    }
    return col*(.94-.065*min(dot(p,p),2.));
}

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);
}
