PBR StudyMediterranean barrel mission tiles in warm kiln-fired terracotta with weathered lichen variation.
dry-stone-rubble-wall.glsl · 121 lines · GLSL ES 3.00 · MIT
#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.);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Dry-Stone Rubble Wall — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/construction/lib/12_dry_stone_wall.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;
// 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.
vec2 screen(){return (pfx_frag_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(pfx_frag_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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor){
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.);
}
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;
// Dry-Stone Rubble Wall — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/construction/lib/12_dry_stone_wall.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;
// 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.
vec2 screen(){return (pfx_frag_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(pfx_frag_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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor){
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.);
}
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.04;
EMISSION = tinted * 0.0;
}
// Dry-Stone Rubble Wall — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/construction/lib/12_dry_stone_wall.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 */ `
// 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.);
}
`;
export function createDryStoneRubbleWallMaterial(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 | dry-stone-rubble-wall |
|---|---|
| Category | Stone & Masonry Shaders · Architectural Masonry |
| Upstream study | construction/lib/12_dry_stone_wall.glsl (commit 1fbd084) |
| Animation cycle | Static analytical PBR surface |
| Declared uniforms | uResolution |
| Source size | 121 lines · 5.3 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT nature sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Procedural signed-distance and analytic surface shading
Inigo Quilez, distance formulation
PBR StudyMediterranean barrel mission tiles in warm kiln-fired terracotta with weathered lichen variation.
PBR StudyRounded granite street setts polished smooth on their crowns with sandy compacted joints.
AnimatedCrushed basalt road aggregate in dark bitumen binder crossed by glossy poured-tar crack sealant.
AnimatedBrutalist poured concrete imprinted with timber plank grain, form-tie cones, and air bug-holes.
AnimatedRain-soaked street puddle with thin-film rainbow petroleum sheen lapping aggregate stones.
AnimatedTroweled lime-terracotta facade plaster with hairline craze cracks and sun-bleached wash.