PBR StudyBanded cream limestone slab pitted with elongated gas-escape vugs and calcite layers.
intergrown-pyrite-cubes.glsl · 167 lines · GLSL ES 3.00 · MIT
#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);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Intergrown Pyrite Cubes — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/04_intergrown_pyrite_cubes.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-04-intergrown-pyrite-cubes
// Intergrown pyrite cubes
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 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;
// Intergrown Pyrite Cubes — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/04_intergrown_pyrite_cubes.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-04-intergrown-pyrite-cubes
// Intergrown pyrite cubes
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 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;
}
// Intergrown Pyrite Cubes — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/minerals/lib/04_intergrown_pyrite_cubes.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-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);
}
`;
export function createIntergrownPyriteCubesMaterial(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 | intergrown-pyrite-cubes |
|---|---|
| Category | Stone & Masonry Shaders · Minerals & Gems |
| Upstream study | minerals/lib/04_intergrown_pyrite_cubes.glsl (commit main) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime |
| Source size | 167 lines · 7.4 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT pickups 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
PBR StudyBanded cream limestone slab pitted with elongated gas-escape vugs and calcite layers.
PBR StudyCoarse plutonic rock mosaic of pink orthoclase feldspar, smoky quartz, and glinting black biotite.
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.