Intergrown Pyrite Cubes shader

intergrown-pyrite-cubes.glsl · 167 lines · GLSL ES 3.00 · MIT

Intergrown Pyrite CubesStone & Masonry
Intergrown Pyrite Cubes WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 167 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
intergrown-pyrite-cubes.glsl (167 lines)
#version 300 es
// Intergrown Pyrite Cubes (intergrown-pyrite-cubes.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/04_intergrown_pyrite_cubes.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-04-intergrown-pyrite-cubes
// Intergrown pyrite cubes
precision highp float;
precision highp int;
uniform vec2 uResolution;
uniform float uTime;
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);
}

mat3 crystalBasis(float spin,float tilt,float roll) {
    vec3 x=vec3(cos(spin),sin(spin),0.);
    vec3 y=vec3(-sin(spin),cos(spin),0.)*cos(tilt)+vec3(0,0,sin(tilt));
    vec3 z=cross(x,y);
    return mat3(x*cos(roll)+z*sin(roll),y,z*cos(roll)-x*sin(roll));
}
void cube(int i,out vec2 center,out float h,out mat3 basis,out float baseZ) {
        float k=float(i), seed=randCell(vec2(k,33)), t=randCell(vec2(k,17));

        if(i==0){center=vec2(-.34,.18); h=.30;}
        else if(i==1){center=vec2(.245,-.016); h=.246;}
        else if(i==2){center=vec2(.005,.32); h=.19;}
        else if(i==3){center=vec2(-.20,-.18);h=.197;}
        else {
            float a=k*2.39996;
            center=vec2(.82*cos(a),.425*sin(a))*(.37+.69*t);
            h=.047+.118*pow(seed,1.8);
        }
        basis=crystalBasis(.32+1.3*seed,.33+.50*t,.32+.47*seed);
        baseZ=.015+.083*t+.19*float(i<4);
}

float cubeShadow(vec3 samplePoint,int visible){
    // Test visibility toward a broad off-axis lamp in each cube's own frame.
    float occlusion=0.;
    vec3 lamp=normalize(vec3(-.64,.77,.86));
    for(int i=0;i<39;i++) {
        if(i==visible) continue;
        vec2 center;float h,baseZ;mat3 basis;
        cube(i,center,h,basis,baseZ);
        vec3 origin=transpose(basis)*(samplePoint-vec3(center,baseZ));
        vec3 ray=transpose(basis)*lamp;
        vec3 t1=(-vec3(h)-origin)/(ray+vec3(.000001));
        vec3 t2=(vec3(h)-origin)/(ray+vec3(.000001));
        vec3 near=min(t1,t2),far=max(t1,t2);
        float enter=max(0.,max(near.x,max(near.y,near.z)));
        float leave=min(far.x,min(far.y,far.z));
        occlusion=max(occlusion,smoothstep(-.018,.055,leave-enter)*exp(-enter*1.6));
    }
    return occlusion;
}

vec3 study(vec2 p) {
    vec3 color=mix(vec3(.095,.112,.122),vec3(.23,.25,.25),.59*stone(p*3.)+.3*(1.-p.y));
    float closest=-8.,shadow=0.;
    vec3 chosen=vec3(0),local=vec3(0); float edge=0.;int visible=-1;
    // A backbone of large intergrown cubes, with satellite cubes at contacts.
    for(int i=0;i<39;i++) {
        vec2 center;float h,zBase;mat3 basis;
        cube(i,center,h,basis,zBase);
        vec2 xy=p-center;
        shadow=max(shadow,.60*exp(-dot((xy-vec2(.06,-.11))/h,(xy-vec2(.06,-.11))/h)*.42));
        if(length(xy)>1.74*h) continue;
        float hi=20.,lo=-20.;vec3 face=vec3(0);
        for(int j=0;j<3;j++) {
            clipPlane(xy,basis[j],h,hi,lo,face);clipPlane(xy,-basis[j],h,hi,lo,face);
        }
        // Tiny truncations catch reflected light along the cubic growth edges.
        for(int a=0;a<3;a++) for(int b=0;b<4;b++){
            vec3 bevel=basis[a]*(b<2?1.:-1.)+basis[(a+1)%3]*(b%2==0?1.:-1.);
            clipPlane(xy,bevel,1.977*h,hi,lo,face);
        }
        if(hi<lo || hi+zBase<closest) continue;
        closest=hi+zBase;
        chosen=normalize(face);local=transpose(basis)*vec3(xy,hi);visible=i;
        vec3 gap=h-abs(local);
        edge=max(min(gap.x,gap.y),max(min(gap.y,gap.z),min(gap.z,gap.x)));
        if(abs(dot(face,basis[0]))>.9) local=local.yzx;
        else if(abs(dot(face,basis[1]))>.9) local=local.zxy;
    }
    // A fractured gangue encloses the lower parts of the cube intergrowth.
    vec2 bed=rot(-.09)*(p-vec2(-.028,-.19))/vec2(.97,.47);
    float coarse=stone(p*8.), grit=stone(p*87.);
    float outline=length(bed)+.25*(coarse-.5)+.088*(grit-.5)
        +.048*sin(atan(bed.y,bed.x)*7.);
    float matrixZ=-.32+.54*sqrt(max(0.,1.-outline*outline))
                  +.27*(coarse-.5)+.15*(grit-.5)-.17*bed.y;
    color*=1.-max(shadow,.4*exp(-pow(max(0.,outline-.94)*8.,2.)));
    if(outline<1. && matrixZ>closest) {
        float fracture=stone(p*vec2(17.,29.))+.33*abs(2.*field(rot(-.25)*p*66.)-1.);
        float cleavage=field(rot(.36)*p*vec2(26.,91.));
        vec3 matrix=mix(vec3(.275,.259,.223),vec3(.68,.647,.554),.39*grit+.56*fracture);
        float stepSize=.004;
        float reliefX=stone((p+vec2(stepSize,0))*87.)-grit;
        float reliefY=stone((p+vec2(0,stepSize))*87.)-grit;
        vec3 normalM=normalize(vec3(-reliefX*.048,-reliefY*.048,stepSize));
        float diffuse=.65+.35*max(0.,dot(normalM,normalize(vec3(-.43,.68,.66))));
        matrix*=diffuse*(.66+.31*sqrt(max(0.,1.-outline*outline)));
        matrix+=vec3(.22,.191,.093)*smoothstep(.64,.78,cleavage)*smoothstep(.44,.66,grit);
        return matrix*(1.-.56*cubeShadow(vec3(p,matrixZ+.003),-1));
    }
    if(closest < -7.) return color;
    vec3 n=chosen;
    vec3 reflection=reflect(vec3(0,0,-1),n);
    float lampX=reflection.x+.4*reflection.y+.29*sin(uTime)+.20*p.y;
    float studio=.14+.20*smoothstep(-.71,-.36,reflection.y)+
                 .69*exp(-pow(abs(lampX+.23)/.37,4.))+.29*exp(-pow(abs((lampX-.66)/.15),2.));
    float striation=field(vec2(local.x*340.+.72*floor(field(local.xy*11.)*3.),local.y*9.));
    float terrace=max(abs(local.x+.025),abs(local.y-.061));
    float ledge=fract(terrace*39.+.54*field(local.xy*19.));
    float tarnish=smoothstep(.57,.78,stone(local.xy*44.+float(visible)));
    vec3 brass=vec3(.987,.895,.681)*studio;
    brass=mix(brass,vec3(.283,.232,.155),.34*tarnish);
    brass*=.87+.23*striation+.025*(ledge-.5);
    brass+=vec3(.104,.095,.067)*exp(-pow(abs((ledge-.83)*8.),2.))*tarnish;
    brass+=vec3(.115,.108,.08)*exp(-edge*170.);
    float contact=outline<1. ? smoothstep(0.,.082,closest-matrixZ) : 1.;
    brass*=.75+.25*contact;
    float occlusion=cubeShadow(vec3(p,closest)+chosen*.002,visible);
    return brass*(1.-.44*occlusion)*(.81+.22*smoothstep(-.2,.38,closest));
}

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

Identifierintergrown-pyrite-cubes
CategoryStone & Masonry Shaders · Minerals & Gems
Upstream studyminerals/lib/04_intergrown_pyrite_cubes.glsl (commit main)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime
Source size167 lines · 7.4 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT pickups 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