Dry-Stone Rubble Wall shader

dry-stone-rubble-wall.glsl · 121 lines · GLSL ES 3.00 · MIT

Dry-Stone Rubble WallStone & Masonry
Dry-Stone Rubble Wall WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 121 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

Architectural Masonry
dry-stone-rubble-wall.glsl (121 lines)
#version 300 es
// Dry-Stone Rubble Wall (dry-stone-rubble-wall.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/construction/lib/12_dry_stone_wall.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float)
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-construction-lib-12-dry-stone-wall
// Field wall | irregular limestone flags, tight bearing beds and tilted coping stones.
// WebGL2: draw a fullscreen triangle. Set uResolution to the viewport in pixels.
precision highp float;
precision highp int;
uniform vec2 uResolution;
out vec4 fragColor;
vec2 screen(){return (gl_FragCoord.xy-.5*uResolution)/max(1.,min(uResolution.x,uResolution.y));}
float rnd(vec2 id){
    uvec2 p=uvec2(ivec2(floor(id)));
    uint v=p.x*1597334677u ^ p.y*3812015801u ^ 1013904223u;
    v=(v^(v>>16u))*2246822519u;
    return float(v^(v>>13u))*(1./4294967296.);
}
float noiseAt(vec2 p){
    vec2 i=floor(p),f=fract(p);f=f*f*(3.-2.*f);
    return mix(mix(rnd(i),rnd(i+vec2(1,0)),f.x),mix(rnd(i+vec2(0,1)),rnd(i+1.),f.x),f.y);
}
float fb(vec2 p){
    mat2 m=mat2(1.74,1.01,-1.01,1.74);
    return .55*noiseAt(p)+.27*noiseAt(m*p+11.)+.13*noiseAt(m*m*p-7.)+.05*noiseAt(m*m*m*p+29.);
}
mat2 rot(float a){return mat2(cos(a),sin(a),-sin(a),cos(a));}
float mask(float d,float aa){return 1.-smoothstep(-aa,aa,d);}
float box(vec2 p,vec2 b,float r){vec2 q=abs(p)-b+r;return length(max(q,0.))+min(max(q.x,q.y),0.)-r;}
vec3 finish(vec3 col,vec2 uv){
    return clamp(col*(1.-.12*smoothstep(.16,.9,length(uv)))+.009*(rnd(gl_FragCoord.xy)-.5),0.,1.);
}

float bed(float x,float row){
    return row*.49+.073*(noiseAt(vec2(x*1.8,row*5.))-.5)+.066*sin(x*1.86+row*.71);
}
float joint(float col,float row){
    return col-.5*mod(row,2.)+.39*(rnd(vec2(col,row))-.5);
}
struct Stone { vec2 q;vec2 size;float h; };
float edge(Stone s,vec2 delta){
    vec2 q=s.q+delta;
    // Four chipped corners create an irregular polygon with long flat bearing faces.
    float r=.024+.048*s.h;
    float d=-box(q,s.size,r);
    vec2 corner=sign(q);
    float chip=.04+.13*fract(s.h*7.+dot(corner,vec2(.23,.31)));
    d=min(d,(s.size.x-abs(q.x))*.65+(s.size.y-abs(q.y))-.63*chip);
    return d+.013*(noiseAt(q*17.+s.h*19.)-.5);
}
float surface(vec2 p,Stone s){
    vec2 k=p+s.h*26.;
    float relief=fb(k*vec2(3.,4.6));
    float cleavage=abs(noiseAt(k*vec2(3.5,7.))-.5);
    return .046*relief+.029*cleavage+.018*fb(k*vec2(11.,26.))+.007*noiseAt(k*65.);
}
float heightAt(vec2 p,Stone s,vec2 stepDir){
    return .074*smoothstep(-.009,.111+.023*s.h,edge(s,stepDir))
          +surface(p+stepDir,s)+.025*abs(dot(s.q+stepDir,vec2(.54,.71))+.41*s.h);
}
vec3 limestone(vec2 p,Stone s,float d){
    float eps=.0075;
    vec3 n=normalize(vec3(heightAt(p,s,vec2(-eps,0))-heightAt(p,s,vec2(eps,0)),
                          heightAt(p,s,vec2(0,-eps))-heightAt(p,s,vec2(0,eps)),2.*eps));
    vec2 k=p+s.h*24.;
    float grain=fb(k*14.),fine=noiseAt(k*131.);
    vec3 rock=mix(vec3(.405,.374,.302),vec3(.686,.644,.530),
                  .42*s.h+.35*fb(k*2.3)+.18*grain);
    rock+=.15*(grain-.5)+.094*(fine-.5)+.076*smoothstep(.73,.87,noiseAt(k*65.));
    float seam=abs(noiseAt(k*vec2(3.2,14.)+fb(k*4.))-.43);
    rock*=1.-.12*exp(-seam*65.)*smoothstep(.38,.63,grain);
    float colony=fb(k*4.8)+.25*fb(k*28.)+.095*fine;
    rock=mix(rock,vec3(.65,.668,.496)*(.84+.26*grain),.67*smoothstep(.78,.89,colony));
    rock*=.58+.52*max(0.,dot(n,normalize(vec3(-.51,.71,.51))));
    return rock*(.74+.26*smoothstep(0.,.054,d));
}
void main(){
    vec2 uv=screen(),p=rot(-.047)*uv*3.55;p.y+=.56;
    float aa=max(.0004,length(fwidth(p))*.51);
    float cope=bed(p.x,3.);
    vec3 sky=mix(vec3(.53,.60,.44),vec3(.81,.832,.76),smoothstep(1.43,2.13,p.y+.21*sin(p.x*1.12)));
    vec3 col=sky+.11*(fb(p*2.)-.5);
    if(p.y<cope+.04){
        // Small recessed hearting and pins are visible through the widest interstices.
        vec2 packingCell=p*vec2(4.3,12.2);
        packingCell.x+=.43*floor(packingCell.y);
        float packing=fb(p*vec2(8.,19.));
        col=mix(vec3(.074,.076,.062),vec3(.315,.293,.221),smoothstep(.28,.67,packing));
        vec2 pin=fract(packingCell)-.5;
        float chip=-box(pin,vec2(.44,.37),.20);
        col+=vec3(.097,.086,.056)*smoothstep(-.035,.09,chip)*(.58+.45*packing);
    }
    float approx=floor(p.y/.49);
    for(int j=-1;j<=1;j++){
        float row=approx+float(j);if(row>=3.)continue;
        float lo=bed(p.x,row),hi=bed(p.x,row+1.);
        if(p.y<lo-.025||p.y>hi+.025)continue;
        float c0=floor(p.x+.5*mod(row,2.));
        for(int i=-1;i<=1;i++){
            float c=c0+float(i),l=joint(c,row),r=joint(c+1.,row);
            float seed=rnd(vec2(c,row)+32.);
            float y0=lo,y1=hi;
            if(seed>.72){
                float split=mix(lo,hi,.43)+.019*sin(p.x*5.+seed);
                if(p.y>split){y0=split;seed=fract(seed+.43);}else y1=split;
            }
            float relative=(p.y-lo)/(hi-lo)-.5;
            // Adjacent blocks share each tilted joint.
            l+=relative*.14*(rnd(vec2(c,row)+71.)-.5);
            r+=relative*.14*(rnd(vec2(c+1.,row)+71.)-.5);
            Stone s=Stone(p-vec2(.5*(r+l),.5*(y0+y1)),vec2(.5*(r-l)-.004,.5*(y1-y0)-.002),seed);
            float d=edge(s,vec2(0));
            if(d>-aa)col=mix(col,limestone(p,s,d),mask(-d,aa));
        }
    }
    float skew=p.x+.24*p.y,idx=floor(skew/.40),h=rnd(vec2(idx,49.));
    float top=.36+.245*h+.070*sin(9.*p.x+h);
    vec2 q=vec2(mod(skew,.40)-.20,p.y-cope-.5*top+.035);
    Stone s=Stone(q,vec2(.196,.5*top+.035),h);
    float d=edge(s,vec2(0));
    if(d>-aa)col=mix(col,limestone(p,s,d)*1.07,mask(-d,aa));
    fragColor=vec4(finish(col,uv),1.);
}

Mathematical models & shader specifications

Identifierdry-stone-rubble-wall
CategoryStone & Masonry Shaders · Architectural Masonry
Upstream studyconstruction/lib/12_dry_stone_wall.glsl (commit 1fbd084)
Animation cycleStatic analytical PBR surface
Declared uniformsuResolution
Source size121 lines · 5.3 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT nature sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Procedural signed-distance and analytic surface shading

Adapted upstream mathematical helpers

Inigo Quilez, distance formulation

All stone & masonry shaders
Board-Formed Concrete WebGL2 procedural shaderAnimated

Brutalist poured concrete imprinted with timber plank grain, form-tie cones, and air bug-holes.

Stone & Masonry