Interlinked Ring Chainmail shader

interlinked-ring-chainmail.glsl · 118 lines · GLSL ES 3.00 · MIT

Interlinked Ring ChainmailMetals & Armor
Interlinked Ring Chainmail WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 118 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Weathered Armor
interlinked-ring-chainmail.glsl (118 lines)
#version 300 es
// Interlinked Ring Chainmail (interlinked-ring-chainmail.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/14_chainmail.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float)
// Hash adapted from David Hoskins, Hash without Sine. See THIRD_PARTY_NOTICES.md.
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-textiles-lib-14-chainmail
// Four-in-one mail | riveted and solid iron links on a suede backing.
// WebGL2: draw a fullscreen triangle with uResolution set to the framebuffer size.
precision highp float;
uniform vec2 uResolution;
out vec4 fragColor;
const float PI=3.14159265359;
const float TAU=6.28318530718;
float sat(float x) { return clamp(x,0.,1.); }
float sq(float x) { return x*x; }
float hash(vec2 p) {
    vec3 v=fract(p.xyx*vec3(.1031,.11369,.13787));
    v+=dot(v,v.yzx+19.19);
    return fract((v.x+v.y)*v.z);
}
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);
}
float pixel() { return 2./max(1.,min(uResolution.x,uResolution.y)); }
vec2 field() { return (gl_FragCoord.xy-.5*uResolution)*pixel(); }
mat2 turn(float a) { return mat2(cos(a),sin(a),-sin(a),cos(a)); }
float aa(float d,float footprint) { return 1.-smoothstep(-footprint*.55,footprint*.55,d); }
float band(float phase,float footprint) { return cos(phase)*exp(-.11*sq(min(footprint,12.))); }
vec3 backdrop(vec2 p,vec3 tint) {
    return tint*(.86+.20*noise(p*3.)+.07*noise(p*32.)+.045*noise(p*130.));
}

struct Link {
    float z,alpha,angle,side,rivet,seed;
    vec3 normal;
};
void testLink(inout Link best,vec2 p,vec2 id,float fp) {
    float parity=mod(id.y,2.),hand=1.-2.*parity;
    float seed=hash(id);
    vec2 center=vec2((id.x+.5*parity)*1.95,id.y*.65);
    center+=.037*vec2(hash(id+7.)-.5,seed-.5);
    float roll=.095*sin(.75*id.x+.36*id.y)+.073*(seed-.5);
    mat2 rot=turn(roll);
    vec2 q=rot*(p-center);
    // The circular link is tipped about its long screen axis. At the two
    // intersections with a neighbouring link its depth ordering reverses.
    vec2 axes=vec2(.86+.028*seed,.496+.022*parity);
    float a=atan(q.y/axes.y,q.x/axes.x+.000001);
    // Closest point on the projected ellipse.
    for(int k=0;k<3;k++) {
        vec2 dir=vec2(cos(a),sin(a));
        vec2 s=axes*dir-q,ds=axes*vec2(-dir.y,dir.x);
        float deriv=dot(ds,ds)-dot(s,axes*dir);
        a-=clamp(dot(s,ds)/max(.12,deriv),-.39,.39);
    }
    vec2 dir=vec2(cos(a),sin(a));
    vec2 centerline=axes*dir;
    vec2 delta=q-centerline;
    float joinAngle=-PI+TAU*seed;
    float joint=atan(sin(a-joinAngle),cos(a-joinAngle));
    float scarf=exp(-sq(joint*4.9))*parity;
    float gauge=.115+.047*scarf+.005*noise(vec2(a*15.+19.,seed));
    float dist=length(delta);
    if(dist>gauge+fp) return;
    float side=clamp(dot(delta,normalize(dir/axes))/gauge,-1.,1.);
    float crown=sqrt(max(0.,1.-sq(sat(dist/gauge))));
    vec3 tangent=normalize(vec3(axes*vec2(-dir.y,dir.x),hand*.68*dir.x));
    vec2 lateral=normalize(vec2(tangent.y,-tangent.x));
    vec3 up=normalize(vec3(-tangent.xy*tangent.z,dot(tangent.xy,tangent.xy)));
    vec3 n=normalize(vec3(lateral*side,0.)+up*max(.01,crown));
    n.xy=transpose(rot)*n.xy;
    vec2 pinUV=vec2(joint*.81,side*gauge);
    float pinRadius=length(pinUV/vec2(1.,.85));
    float pin=parity*aa(pinRadius-.048,fp);
    float dome=sqrt(max(0.,1.-sq(pinRadius/.050)));
    n=normalize(n+vec3(transpose(rot)*pinUV*pin*15.,.32*pin*dome));
    float z=hand*.68*dir.y+.105*crown+.029*pin;
    if(z<=best.z) return;
    best=Link(z-(1.-aa(dist-gauge,fp))*.09,aa(dist-gauge,fp),a,side,pin,seed,n);
}
Link surface(vec2 p,float fp) {
    Link h=Link(-5.,0.,0.,0.,0.,0.,vec3(0,0,1));
    float row=floor(p.y/.65+.5);
    for(int j=-1;j<=1;j++) {
        float r=row+float(j);
        if(r< -3.) continue;
        float col=floor(p.x/1.95-.5*mod(r,2.)+.5);
        for(int i=-1;i<=1;i++) testLink(h,p,vec2(col+float(i),r),fp);
    }
    return h;
}
void main() {
    vec2 p=turn(-.21)*field();
    vec2 g=p*3.12;
    g.y+=.34*sin(.84*g.x)+.046*g.x*g.x;
    float fp=3.12*pixel();
    Link h=surface(g,fp);
    Link occluder=surface(g+vec2(-.085,.125),fp);
    vec3 c=backdrop(p,vec3(.34,.249,.188));
    c*=1.-.32*occluder.alpha;
    if(h.alpha>0.) {
        vec3 r=reflect(vec3(0,0,-1),h.normal);
        float strip=exp(-sq((r.x+.45*r.y+.42)*2.5));
        float warm=exp(-sq((r.y-.66+.16*r.x)*4.4));
        float scratch=band(154.*h.angle+6.*h.side+7.*h.seed,fp*154.);
        float scale=noise(g*35.);
        vec3 steel=vec3(.29,.305,.297)*(.63+.45*max(h.normal.y,0.)+.16*scale)
                  +vec3(.54,.58,.59)*strip+vec3(.37,.27,.16)*warm;
        float pitting=noise(g*78.+h.seed);
        steel*=.82+.028*scratch+.10*noise(g*6.+h.seed)+.10*pitting;
        steel=mix(steel,vec3(.59,.43,.25)*(.70+.53*strip),h.rivet*.92);
        float contact=smoothstep(.03,.24,occluder.z-h.z);
        steel*=1.-.48*occluder.alpha*contact;
        c=mix(c,steel,h.alpha);
    }
    fragColor=vec4(c,1.);
}

Mathematical models & shader specifications

Identifierinterlinked-ring-chainmail
CategoryMetals & Armor Shaders · Weathered Armor
Upstream studytextiles/lib/14_chainmail.glsl (commit 1fbd084)
Animation cycleStatic analytical PBR surface
Declared uniformsuResolution
Source size118 lines · 4.8 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT melee sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Procedural signed-distance and analytic surface shading

Adapted upstream mathematical helpers

David Hoskins, hash

All metals & armor shaders