AnimatedDark feldspar slab flashing vivid peacock-blue and gold labradorescence across twin lamellae.
interlocking-granite-grains.glsl · 150 lines · GLSL ES 3.00 · MIT
#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);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Interlocking Granite Grains — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/06_interlocking_granite_grains.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-minerals-lib-06-interlocking-granite-grains
// Interlocking granite grains
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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p = (2.0 * pfx_frag_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);
}
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;
// Interlocking Granite Grains — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/06_interlocking_granite_grains.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-minerals-lib-06-interlocking-granite-grains
// Interlocking granite grains
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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p = (2.0 * pfx_frag_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);
}
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;
}
// Interlocking Granite Grains — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/06_interlocking_granite_grains.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-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);
}
`;
export function createInterlockingGraniteGrainsMaterial(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 | interlocking-granite-grains |
|---|---|
| Category | Stone & Masonry Shaders · Minerals & Gems |
| Upstream study | minerals/lib/06_interlocking_granite_grains.glsl (commit main) |
| Animation cycle | Static analytical PBR surface |
| Declared uniforms | uResolution |
| Source size | 150 lines · 6.1 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT impacts sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Procedural signed-distance and analytic surface shading
randCell adapts the two xorshift/multiply mixing rounds with a 2D seed, modified constants and truncated output (Public domain). Austin Appleby, MurmurHash3, fmix32
AnimatedDark feldspar slab flashing vivid peacock-blue and gold labradorescence across twin lamellae.
PBR StudyRiven dark-grey roof slates with cleaved foliar micro-steps and chipped lower edges.
PBR StudyCathedral seeded glass panes joined by soldered H-profile lead cames with backlit color.
PBR StudyUnmortared fieldstone courses wedged with chinking stones and deep recessed shadow joints.
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.