PBR StudyLocalized chloride-eaten cavities breaking through aged steel plate with dark ferric rims.
Metals & Armor
interlinked-ring-chainmail.glsl · 118 lines · GLSL ES 3.00 · MIT
#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.);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Interlinked Ring Chainmail — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/14_chainmail.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
// 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.
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 (pfx_frag_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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
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.);
}
void fragment() {
vec2 tiled_uv = fract(UV * u_tiling);
vec2 frag_xy = vec2(tiled_uv.x, 1.0 - tiled_uv.y) * uResolution;
vec4 study_col = vec4(0.0, 0.0, 0.0, 1.0);
pixelfx_study_eval(frag_xy, study_col);
vec3 tinted = clamp(mix(study_col.rgb, study_col.rgb * u_tint.rgb * 1.35, u_tint_mix), 0.0, 1.0);
float out_alpha = study_col.a * COLOR.a;
if (u_luma_alpha_key) {
float luma = dot(tinted, vec3(0.2126, 0.7152, 0.0722));
out_alpha *= smoothstep(0.035, 0.22, luma);
}
COLOR = vec4(tinted, out_alpha);
}
shader_type spatial;
render_mode cull_disabled;
// Interlinked Ring Chainmail — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/14_chainmail.glsl (MIT License)
// Drive uTime from GDScript if animating helper functions: material.set_shader_parameter("uTime", Time.get_ticks_msec() * 0.001 * u_speed)
uniform vec2 uResolution = vec2(1024.0, 1024.0);
uniform float uTime = 1.2;
uniform float u_speed : hint_range(0.0, 3.0, 0.05) = 1.0;
uniform float u_tiling : hint_range(0.25, 8.0, 0.25) = 1.0;
uniform vec4 u_tint : source_color = vec4(1.0, 0.722, 0.42, 1.0);
uniform float u_tint_mix : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float uOpen : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform int uLightMode : hint_range(0, 3, 1) = 0;
uniform int uCamMode : hint_range(0, 1, 1) = 1;
uniform bool u_luma_alpha_key = false;
// 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.
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 (pfx_frag_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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
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.);
}
void fragment() {
vec2 tiled_uv = fract(UV * u_tiling);
vec2 frag_xy = vec2(tiled_uv.x, 1.0 - tiled_uv.y) * uResolution;
vec4 study_col = vec4(0.0, 0.0, 0.0, 1.0);
pixelfx_study_eval(frag_xy, study_col);
vec3 tinted = clamp(mix(study_col.rgb, study_col.rgb * u_tint.rgb * 1.35, u_tint_mix), 0.0, 1.0);
float luma = dot(tinted, vec3(0.2126, 0.7152, 0.0722));
ALBEDO = tinted;
ROUGHNESS = clamp(0.85 - luma * 0.45, 0.08, 0.96);
METALLIC = 0.85;
EMISSION = tinted * 0.0;
}
// Interlinked Ring Chainmail — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/14_chainmail.glsl (MIT License)
import * as THREE from 'three';
const vertexShader = /* glsl */ `
out vec2 vUv;
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fragmentShader = /* glsl */ `
// 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.);
}
`;
export function createInterlinkedRingChainmailMaterial(width = 1024, height = 1024) {
const material = new THREE.ShaderMaterial({
glslVersion: THREE.GLSL3,
vertexShader,
fragmentShader,
uniforms: {
uResolution: { value: new THREE.Vector2(width, height) },
uTime: { value: 1.2 },
uOpen: { value: 0.0 },
uLightMode: { value: 0 },
uCamMode: { value: 1 },
},
});
return {
material,
update(elapsedSeconds) {
material.uniforms.uTime.value = elapsedSeconds * 1.0;
},
resize(nextW, nextH) {
material.uniforms.uResolution.value.set(nextW, nextH);
},
};
}
| Identifier | interlinked-ring-chainmail |
|---|---|
| Category | Metals & Armor Shaders · Weathered Armor |
| Upstream study | textiles/lib/14_chainmail.glsl (commit 1fbd084) |
| Animation cycle | Static analytical PBR surface |
| Declared uniforms | uResolution |
| Source size | 118 lines · 4.8 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT melee sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Procedural signed-distance and analytic surface shading
David Hoskins, hash
PBR StudyLocalized chloride-eaten cavities breaking through aged steel plate with dark ferric rims.
PBR StudyOxidized bronze and copper plating streaked with seafoam-green carbonate verdigris runoff.
AnimatedEncroaching flaky orange-ochre iron oxide eating across painted industrial steel.
AnimatedCNC-lathed 6061 aluminum surface with concentric anisotropic micro-grooves and bowtie specular.
AnimatedHot-dip galvanized steel sheet with Voronoi crystalline zinc spangle facets and grain orientation.
AnimatedDual-layer metallic paint with stochastic aluminum micro-flakes under glossy dielectric clearcoat.