Interlocking Granite Grains shader

interlocking-granite-grains.glsl · 150 lines · GLSL ES 3.00 · MIT

Interlocking Granite GrainsStone & Masonry
Interlocking Granite Grains WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 150 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

Minerals & Gems
interlocking-granite-grains.glsl (150 lines)
#version 300 es
// Interlocking Granite Grains (interlocking-granite-grains.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/06_interlocking_granite_grains.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-minerals-lib-06-interlocking-granite-grains
// Interlocking granite grains
precision highp float;
precision highp int;
uniform vec2 uResolution;
out vec4 fragColor;

const float TAU = 6.28318530718;
mat2 rot(float a) { return mat2(cos(a),sin(a),-sin(a),cos(a)); }
// randCell uses the two multiply/xorshift rounds of Austin Appleby’s public-domain
// fmix32 from MurmurHash3: https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp
float randCell(vec2 id) {
    uvec2 k=uvec2(ivec2(floor(id)));
    uint b=(k.x*1664525u+1013904223u) ^ (k.y*747796405u);
    b=(b^(b>>16u))*2246822519u;
    b=(b^(b>>13u))*3266489917u;
    return float(b>>8u)*(1./16777216.);
}
float field(vec2 p) {
    vec2 k=floor(p), s=fract(p);
    s=s*s*(3.-2.*s);
    return mix(mix(randCell(k),randCell(k+vec2(1,0)),s.x),
               mix(randCell(k+vec2(0,1)),randCell(k+1.),s.x),s.y);
}
float stone(vec2 p) {
    mat2 octave=mat2(1.82,1.07,-1.07,1.82);
    float v=.55*field(p);
    p=octave*p+8.3; v+=.26*field(p);
    p=octave*p+5.7; v+=.13*field(p);
    return v+.06*field(octave*p);
}
float pixel() { return 2./min(uResolution.x,uResolution.y); }
float inside(float d,float w) { return 1.-smoothstep(-w,w,d); }

// Intersect the viewing line (x,y,z) with n·point <= offset.
// Upper and lower bounds on z retain the nearest visible facet.
void clipPlane(vec2 xy,vec3 n,float offset,inout float top,inout float bottom,inout vec3 face) {
    float room=offset-dot(xy,n.xy);
    if(abs(n.z)<.00001) { if(room<0.) bottom=10000.; }
    else if(n.z>0.) {
        float z=room/n.z;
        if(z<top) {top=z;face=n;}
    } else bottom=max(bottom,room/n.z);
}

void grains(vec3 p,out vec3 v,out float kind,out float seed,out float seam){
    vec3 q=p*18.6;
    q+=.19*vec3(field(q.xy*2.+q.z),field(q.yz*2.+q.x+5.),field(q.xz*2.-q.y));
    vec3 cell=floor(q);float first=20.,second=20.;
    for(int z=-1;z<=1;z++)for(int y=-1;y<=1;y++)for(int x=-1;x<=1;x++){
        vec3 c=cell+vec3(x,y,z);
        vec2 id=c.xy+vec2(43.,97.)*c.z;
        float r=randCell(id+24.),m=randCell(id+63.);
        vec3 s=q-c-.12-.75*vec3(randCell(id),randCell(id+11.),randCell(id-16.));
        s.xy=rot(r*TAU)*s.xy;
        s.xz=rot(m*2.8)*s.xz;
        vec3 g=abs(s);
        float d=m<.43?max(g.x*.78,max(g.y*1.19,g.z*1.31))+.15*length(g)
                     :length(s*vec3(.91,1.08,1.12));
        d-=.42*r;
        if(d<first){second=first;first=d;v=s;kind=m;seed=r;}
        else second=min(second,d);
    }
    seam=second-first;
}
vec3 study(vec2 p){
    vec2 s=rot(-.12)*(p-vec2(-.018,.02));
    vec3 x=normalize(vec3(.94,-.17,.31));
    vec3 y=normalize(cross(vec3(-.11,.62,.78),x)),z=cross(x,y);
    mat3 basis=mat3(x,y,z);
    float top=9.,bottom=-9.;vec3 face=z;
    vec3 sz=vec3(1.015,.69,.205);
    for(int i=0;i<3;i++){
        clipPlane(s,basis[i],sz[i],top,bottom,face);
        clipPlane(s,-basis[i],sz[i],top,bottom,face);
    }
    clipPlane(s,x-y*.57,1.17,top,bottom,face);
    clipPlane(s,-x+y*.76,1.265,top,bottom,face);
    clipPlane(s,-y+z*.53,.716,top,bottom,face);
    clipPlane(s,x+z*.69,1.135,top,bottom,face);
    vec3 ground=vec3(.243,.239,.224)*(.91+.11*stone(p*13.));
    float floorZ=(-sz.z-dot(z.xy,s))/z.z;
    vec2 floorPoint=(transpose(basis)*vec3(s,floorZ)).xy;
    float shade=0.;
    for(int i=0;i<=24;i++){
        float h=.018*float(i);
        vec2 q=floorPoint+h*vec2(-.34,.56);
        vec2 edge=abs(q)-sz.xy;
        float d=max(max(edge.x,edge.y),max(q.x-.57*q.y-1.17,-q.x+.76*q.y-1.265));
        shade=max(shade,exp(-max(0.,d)/(.019+.24*h))*(.63-.69*h));
    }
    ground*=1.-shade;
    if(top<bottom)return ground;
    vec3 point=transpose(basis)*vec3(s,top);
    vec3 n=normalize(face),v;float kind,seed,seam;
    grains(point,v,kind,seed,seam);
    // Coarse feldspar laths project into an interstitial groundmass that
    // crystallized at a finer scale. Its quartz is irregular and smoky.
    float coarseRim=max(abs(v.x)*.75,max(abs(v.y)*1.13,abs(v.z)*1.24));
    if(kind>.34 || coarseRim>.43+.17*seed){
        grains(point*2.28+vec3(2.,.37,-.91),v,kind,seed,seam);
        kind=.25+.75*kind;
    }
    float polish=pow(max(0.,dot(n,z)),12.);
    float plane=field(v.xy*vec2(5.,11.)+v.z);
    float interior=stone(v.xz*4.+v.y+seed*11.);
    float dust=field(point.xy*340.+point.z*135.);
    vec3 albedo;
    if(kind<.43){
        albedo=mix(vec3(.50,.34,.296),vec3(.82,.64,.548),.41*seed+.59*interior);
        float twin=step(.52,fract(v.x*2.6+seed));
        albedo+=.047*twin+.036*plane;
    }else if(kind<.63){
        albedo=mix(vec3(.63,.62,.59),vec3(.90,.86,.78),.55*interior+.45*seed);
        albedo+=.033*cos(v.x*48.)*smoothstep(.34,.62,plane);
    }else if(kind<.87){
        float smoke=stone(v.xy*4.+v.z*1.4+vec2(.17,.29));
        albedo=mix(vec3(.32,.33,.336),vec3(.71,.70,.67),smoke*.76+.24*seed);
        float fissure=abs(v.y-.43*v.x+.065*interior-.1);
        albedo+=.13*exp(-fissure*50.)*plane;
    }else{
        albedo=mix(vec3(.061,.065,.060),vec3(.233,.221,.193),.51*seed+.49*plane);
    }
    vec3 cleave=normalize(n+.32*(1.-polish)*vec3(cos(seed*TAU),sin(seed*TAU),.2)
                         +.043*(1.-polish)*(plane-.5));
    vec3 lamp=normalize(vec3(-.54,.66,.78));
    float light=.44+.57*max(0.,dot(cleave,lamp));
    float bevel=exp(-seam*23.);
    albedo*=light*(1.-(.026+.14*(1.-polish))*bevel);
    float ref=exp(-pow(abs(s.y+.42*s.x-.21)*3.6,4.));
    // The cut reflects one window; mineral phase modulates its brightness.
    float sheen=kind>.87?.036+.13*seed:kind>.63?.093:.055;
    albedo+=polish*ref*(sheen+.071);
    float grit=stone(point.xy*145.+point.z*74.);
    float defect=smoothstep(.62,.79,grit)*smoothstep(.38,.68,interior);
    albedo*=1.-.35*defect;
    albedo+=.10*(grit-.5)*(1.-.53*polish);
    albedo+=.052*(dust-.5)+.042*(plane-.5);
    return albedo;
}

void main() {
    vec2 p = (2.0 * gl_FragCoord.xy - uResolution) / min(uResolution.x, uResolution.y);
    // Two diagonally staggered samples soften subpixel grain and crystal boundaries.
    vec2 offset = vec2(0.28, -0.19) * pixel();
    vec3 rgb = (study(p - offset) + study(p + offset)) * 0.5;
    fragColor = vec4(clamp(rgb, 0.0, 1.0), 1.0);
}

Mathematical models & shader specifications

Identifierinterlocking-granite-grains
CategoryStone & Masonry Shaders · Minerals & Gems
Upstream studyminerals/lib/06_interlocking_granite_grains.glsl (commit main)
Animation cycleStatic analytical PBR surface
Declared uniformsuResolution
Source size150 lines · 6.1 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT impacts sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Procedural signed-distance and analytic surface shading

Adapted upstream mathematical helpers

randCell adapts the two xorshift/multiply mixing rounds with a 2D seed, modified constants and truncated output (Public domain). Austin Appleby, MurmurHash3, fmix32

All stone & masonry shaders