Weathered Rust Spread shader

weathered-rust-spread.glsl · 133 lines · GLSL ES 3.00 · MIT

Weathered Rust SpreadMetals & Armor
Weathered Rust Spread WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 133 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
weathered-rust-spread.glsl (133 lines)
#version 300 es
// Weathered Rust Spread (weathered-rust-spread.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/wear/lib/12_rust_spread.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-wear-lib-12-rust-spread
// Water has lifted the enamel around the barrel and fasteners of a steel hinge.
// uResolution in framebuffer pixels. Optional uTime in seconds; zero shows an intermediate stage.
precision highp float;
uniform vec2 uResolution;
uniform float uTime;
out vec4 fragColor;

float hash(vec2 v) {
    vec3 p=fract(v.xyx*vec3(.1031,.1030,.0973));
    p+=dot(p,p.yzx+33.33);
    return fract((p.x+p.y)*p.z);
}

float noise2(vec2 p) {
    vec2 i=floor(p), f=fract(p);
    f=f*f*(3.0-2.0*f);
    return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
               mix(hash(i+vec2(0,1)),hash(i+1.0),f.x),f.y);
}

float fbm(vec2 p) {
    mat2 octave=mat2(1.73,1.12,-1.12,1.73);
    float value=0.0, weight=.53;
    for(int i=0;i<4;i++){
        value+=weight*noise2(p); p=octave*p+vec2(9.4,17.1); weight*=.50;
    }
    return value;
}

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

float gauss(float v) { return exp(-v*v); }

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

float pixel() { return 2.0/min(uResolution.x,uResolution.y); }

vec2 screen() { return (gl_FragCoord.xy-.5*uResolution)*pixel(); }

float inside(float d) { return 1.0-smoothstep(-.65*pixel(),.65*pixel(),d); }

float box(vec2 p,vec2 halfSize,float radius) {
    vec2 d=abs(p)-halfSize+radius;
    return length(max(d,0.0))+min(max(d.x,d.y),0.0)-radius;
}

float bump(float h) {
    vec2 g=vec2(dFdx(h),dFdy(h))/pixel();
    return dot(normalize(vec3(-g,1)),normalize(vec3(-.52,.69,.74)));
}

vec4 finish(vec3 color) {
    return vec4(clamp(color,0.0,1.0),1.0);
}

vec3 screw(vec3 base,vec2 p,float radius,float seed) {
    float r=length(p)/radius;
    vec2 n=p/radius;
    float patina=fbm(p*65.0+seed);
    float light=.61+.34*clamp(-n.x+n.y,-1.0,1.0);
    vec3 metal=mix(vec3(.39,.348,.26),vec3(.50,.262,.116),smoothstep(.34,.67,patina));
    base*=1.0-.68*inside((r-1.13)*radius);
    base=mix(base,metal*(light+.20*sqrt(max(1.0-r*r,0.0))),inside((r-1.0)*radius));
    vec2 cut=rot(seed)*p;
    float slot=box(cut,vec2(radius*.83,.012),.007);
    base+=vec3(.21,.165,.09)*inside(abs(slot-.004)-.0025)*inside((r-.98)*radius);
    return mix(base,vec3(.117,.081,.052),inside(slot));
}
void main() {
    vec2 p=screen();
    vec2 q=rot(-.13)*(p-vec2(-.12,.09));
    float grit=fbm(q*75.0), weather=fbm(q*8.0);
    float joint=abs(q.x);
    float spread=.05*(1.0-exp(-.22*max(uTime,0.0)));
    // Drop-shaped pressed leaves meet at three interleaved cylindrical knuckles.
    vec2 leaves=vec2(abs(q.x)-.365,q.y);
    float leaf=box(leaves,vec2(.348,.455-.16*smoothstep(.23,.72,abs(q.x))),.095);
    float barrel=box(q,vec2(.103,.527),.026);
    float contour=min(leaf,barrel);
    vec2 screwPos=vec2(abs(q.x)-.49,abs(q.y)-.175);
    float roughEdge=.38*(fbm(q*4.3)-.49)+.085*(fbm(q*27.0)-.50)+.016*(grit-.50);
    float origin=min(abs(leaf)*.84,length(screwPos)-.08);
    origin=min(origin,joint-.045+.43*smoothstep(.46,.83,abs(q.y)));
    float field=origin-.049-spread+roughEdge;
    float substrate=fbm(p*vec2(13.0,4.0));
    vec3 col=mix(vec3(.184,.283,.282),vec3(.317,.416,.383),substrate)*(.93+.14*grit);
    col*=1.0-.52*gauss(joint/.019);
    float trail=exp(-sq((abs(q.x)-.48-.017*sin(q.y*6.0))/.085))*smoothstep(.22,.70,-q.y);
    col=mix(col,vec3(.38,.218,.126),.40*trail);
    col*=1.0-.47*exp(-max(contour,0.0)*40.0);
    vec2 shadowPos=vec2(abs(q.x-.021)-.365,q.y+.030);
    float castEdge=box(shadowPos,vec2(.348,.455-.16*smoothstep(.23,.72,abs(q.x))),.095);
    col*=1.0-.23*exp(-max(castEdge,0.0)*56.0);
    vec3 enamel=mix(vec3(.39,.496,.443),vec3(.49,.567,.491),weather);
    float edgeLip=exp(-abs(leaf+.013)*135.0);
    enamel+=vec3(.15,.154,.12)*edgeLip;
    float scratch=abs(q.y+.24*q.x-.045+.003*sin(q.x*28.0));
    enamel+=vec3(.13,.12,.086)*gauss(scratch/.0027)*gauss((q.x+.39)/.19);
    col=mix(col,enamel,inside(leaf));
    // The loose paint margin stands proud of a pitted oxide bed.
    float rust=inside(field)*mix(.9,1.0,inside(leaf));
    float fissure=abs(sin(13.0*q.x+7.0*q.y+9.0*fbm(q*6.7)));
    float pit=pow(clamp((.47-grit)*4.7,0.0,1.0),2.0);
    float relief=.008*weather+.006*grit-.011*pit+.003*exp(-abs(field-.008)*170.0);
    float lambert=bump(relief);
    vec3 oxide=mix(vec3(.170,.094,.072),vec3(.614,.315,.153),smoothstep(.27,.68,weather+.28*(grit-.5)));
    oxide*=.65+.40*lambert+.1*grit-.18*pit;
    oxide*=1.0-.44*exp(-fissure*22.0)*smoothstep(.02,.13,-field);
    // The same corrosion attack crosses a stamped step in depth.
    float leafFace=inside(leaf);
    float rolledEdge=exp(-abs(leaf+.008)*128.0);
    oxide*=.81+.19*leafFace;
    oxide+=vec3(.12,.081,.044)*rolledEdge;
    float blister=smoothstep(.58,.75,grit)*gauss((field-.027)/.030);
    col=mix(col,oxide,max(rust,.69*blister));
    col*=1.0-.27*gauss((leaf-.013)/.015);
    col*=1.0-.34*exp(-abs(field)*135.0);
    col+=vec3(.21,.189,.133)*gauss((field-.010)/.005)*(.52+.48*weather);
    vec3 knuckle=mix(vec3(.23,.249,.225),oxide,.45+.45*smoothstep(.34,.67,weather));
    float cylinder=sqrt(max(0.0,1.0-sq(clamp(q.x/.106,-1.0,1.0))));
    knuckle*=.40+.52*cylinder-.24*clamp(q.x/.106,-1.0,1.0);
    knuckle+=vec3(.18,.14,.09)*gauss((q.x+.044)/.016)*(.50+.5*grit);
    float knuckleGap=abs(abs(q.y)-.189);
    knuckle*=1.0-.80*gauss(knuckleGap/.012);
    col=mix(col,knuckle,inside(barrel));
    col=screw(col,screwPos,.075,abs(q.x)+sign(q.x)*.47+sign(q.y)*.63);
    col*=.99-.08*min(length(p)*.66,1.0);
    fragColor=finish(col);
}

Mathematical models & shader specifications

Identifierweathered-rust-spread
CategoryMetals & Armor Shaders · Weathered Armor
Upstream studywear/lib/12_rust_spread.glsl (commit c3a6f6c)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime
Source size133 lines · 5.5 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT fire 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 · Inigo Quilez, distance formulation

All metals & armor shaders
Sand-Cast Iron Stipple WebGL2 procedural shaderAnimated

Heavy foundry cast iron with mold-sand stipple micro-relief, graphite dark oxide, and parting seam.

Metals & Armor