AnimatedDense cushion of star-shaped emerald sphagnum moss heads glistening with capillary moisture.
iridescent-fish-scales.glsl · 127 lines · GLSL ES 3.00 · MIT
#version 300 es
// Iridescent Fish Scales (iridescent-fish-scales.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/13_fish_scales.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-animal-lib-13-fish-scales
// Fish scales
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); }
// A patch of the flank between the dorsal pigmentation and the silver belly.
// The cylindrical section foreshortens the rows at the upper silhouette.
vec3 study(vec2 p){
vec2 body=rot(-.135)*p*.94-vec2(.36,.005);
float mid=.044-.023*body.x*body.x;
float radius=.804+.147*body.x-.10*body.x*body.x;
float v=(body.y-mid)/radius;
float curve=asin(clamp(v,-.996,.996));
float z=sqrt(max(.006,1.-v*v));
vec3 backdrop=mix(vec3(.096,.176,.173),vec3(.33,.41,.347),.69*stone(p*2.)+.18*(1.-p.y));
if(abs(v)>1.) return backdrop;
vec2 q=vec2(body.x+.06*curve*curve,curve);
q=vec2(q.y*5.25,q.x*5.2+.23*cos(q.y*2.4));
float view=sin(.83*uTime);
float freckle=stone(p*109.);
float pigment=stone(body*vec2(3.8,12.));
vec3 skin=mix(vec3(.68,.71,.65),vec3(.12,.234,.198),smoothstep(-.55,.88,v));
skin=mix(skin,vec3(.102,.151,.142),.48*exp(-pow(abs((curve-.12-.044*sin(body.x*3.))*8.),2.))*(.48+.72*pigment));
vec3 surf=skin*.64;
for(int j=-1;j<=2;j++){
float row=floor(q.y/.62)+float(j);
float phase=.5*mod(row,2.);
float col=floor(q.x-phase+.5);
for(int i=-1;i<=1;i++){
vec2 id=vec2(col+float(i),row);
float k=randCell(id+6.);
vec2 s=q-vec2(id.x+phase,row*.62)-.085*vec2(k-.5,randCell(id+24.)-.5);
vec2 e=s/vec2(.63+.059*k,.76+.068*k);
float r=length(e);
if(r>1.03)continue;
float ring=cos(95.*length(e+vec2(.01,-.19))+1.7*field(s*4.+id));
float spoke=pow(.5+.5*cos(atan(e.x,e.y+.002)*49.+4.*k),9.);
float mantle=smoothstep(.65,.985,r);
vec3 n=normalize(vec3(.14*e.y+.03*sin(curve*7.),v+.13*e.x,z+.07*(1.-r)));
vec3 h=normalize(vec3(-.28+.42*view,.08,.90));
float pearl=pow(max(0.,dot(n,h)),23.);
float flash=exp(-pow(abs(curve+.22+.17*view+.14*e.y+.19*e.x+.06*k)*4.5,2.));
float streak=exp(-pow(abs((dot(n,normalize(vec3(-.27,.51,.85)))-.957)*32.),2.));
vec3 nacre=mix(vec3(.41,.82,.86),vec3(.98,.71,.39),.5+.5*cos(4.1*curve+.82*e.x+.48*k+view));
vec3 bed=skin*(.63+.33*z+.11*k+.11*freckle);
bed*=1.-.4*smoothstep(.54,.83,field(18.*e+id))*smoothstep(-.32,.61,v);
vec3 plate=bed+nacre*(pearl*(.19+.39*k)+.24*flash+.10*streak);
float pore=length((s-vec2(.065,-.29))*vec2(1.8,1.));
if(id.x==0.)plate*=1.-.51*exp(-pore*pore*193.);
plate+=.022*ring-.021*spoke+.026*(freckle-.5);
// A hyaline lamina transmits the underlying neighbour at the free rim.
vec3 film=mix(surf,plate,.27)+.075*nacre*(.44+.56*pearl);
plate=mix(plate,film,mantle*.91);
plate+=nacre*.19*exp(-pow(abs((r-.958)*60.),2.))*max(0.,-.75*e.y-.42*e.x);
plate*=1.-.26*exp(-pow(abs((r-.991)*65.),2.));
surf=mix(surf,plate,inside(r-1.,.010));
}
}
// Smooth opercular bone overlaps the foremost scale row. The raised
// cheek shares the light that travels across the flank.
float gill=body.x-(.635+.28*v*v-.045*v+.021*sin(v*5.));
float cheek=smoothstep(-.006,.012,gill);
float cheekArc=length((body-vec2(1.15,.02))/vec2(.58,.78));
float ridge=exp(-pow(abs(gill-.015)*81.,2.))-.66*exp(-pow(abs(gill+.009)*83.,2.));
vec3 plate=mix(vec3(.59,.626,.536),vec3(.215,.333,.271),smoothstep(-.62,.77,v));
plate+=vec3(.28,.29,.224)*exp(-pow(abs(cheekArc-.76+.087*view)*3.7,2.));
plate+=.19*ridge+.065*(stone(body*65.)-.5);
surf=mix(surf,plate,cheek);
// A folded pectoral fin lies forward of the lateral line. Its narrow
// membrane reveals the scales and casts a displaced shadow on the flank.
vec2 fin=rot(-.28)*(body-vec2(.79,-.22));
float t=-fin.x/.86;
float span=.29*pow(clamp(t,0.,1.),.84)+.025;
float arch=.075*sin(clamp(t,0.,1.)*3.14);
float fan=(fin.y-arch)/span;
float end=1.-.27*fan*fan+.035*fan;
float silhouette=max(max(-t,t-end+.019*cos(31.*fan)*smoothstep(.62,.98,t)),abs(fan)-1.);
float shadowD=max(max(-t,t-end),abs((fin.y-arch+.042)/span)-1.);
surf*=1.-.27*exp(-max(0.,shadowD)*12.)*smoothstep(.0,.19,t);
float ribs=(fan*.5+.5)*14.+.29*t*t;
float rib=exp(-pow(abs(fract(ribs)-.50)*9.,2.));
float splitRib=exp(-pow(abs(fract(2.*ribs+.17)-.5)*10.,2.))*smoothstep(.44,.92,t);
vec3 membrane=mix(surf,vec3(.32,.29,.208),.27+.23*(1.-clamp(t,0.,1.)));
membrane+=vec3(.176,.148,.098)*(rib+.39*splitRib)*(.78+.22*cos(t*112.));
surf=mix(surf,membrane,inside(silhouette,pixel()/.22));
return mix(backdrop,surf,inside(abs(v)-1.,pixel()));
}
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+.
// Iridescent Fish Scales — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/13_fish_scales.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-animal-lib-13-fish-scales
// Fish scales
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); }
// A patch of the flank between the dorsal pigmentation and the silver belly.
// The cylindrical section foreshortens the rows at the upper silhouette.
vec3 study(vec2 p){
vec2 body=rot(-.135)*p*.94-vec2(.36,.005);
float mid=.044-.023*body.x*body.x;
float radius=.804+.147*body.x-.10*body.x*body.x;
float v=(body.y-mid)/radius;
float curve=asin(clamp(v,-.996,.996));
float z=sqrt(max(.006,1.-v*v));
vec3 backdrop=mix(vec3(.096,.176,.173),vec3(.33,.41,.347),.69*stone(p*2.)+.18*(1.-p.y));
if(abs(v)>1.) return backdrop;
vec2 q=vec2(body.x+.06*curve*curve,curve);
q=vec2(q.y*5.25,q.x*5.2+.23*cos(q.y*2.4));
float view=sin(.83*uTime);
float freckle=stone(p*109.);
float pigment=stone(body*vec2(3.8,12.));
vec3 skin=mix(vec3(.68,.71,.65),vec3(.12,.234,.198),smoothstep(-.55,.88,v));
skin=mix(skin,vec3(.102,.151,.142),.48*exp(-pow(abs((curve-.12-.044*sin(body.x*3.))*8.),2.))*(.48+.72*pigment));
vec3 surf=skin*.64;
for(int j=-1;j<=2;j++){
float row=floor(q.y/.62)+float(j);
float phase=.5*mod(row,2.);
float col=floor(q.x-phase+.5);
for(int i=-1;i<=1;i++){
vec2 id=vec2(col+float(i),row);
float k=randCell(id+6.);
vec2 s=q-vec2(id.x+phase,row*.62)-.085*vec2(k-.5,randCell(id+24.)-.5);
vec2 e=s/vec2(.63+.059*k,.76+.068*k);
float r=length(e);
if(r>1.03)continue;
float ring=cos(95.*length(e+vec2(.01,-.19))+1.7*field(s*4.+id));
float spoke=pow(.5+.5*cos(atan(e.x,e.y+.002)*49.+4.*k),9.);
float mantle=smoothstep(.65,.985,r);
vec3 n=normalize(vec3(.14*e.y+.03*sin(curve*7.),v+.13*e.x,z+.07*(1.-r)));
vec3 h=normalize(vec3(-.28+.42*view,.08,.90));
float pearl=pow(max(0.,dot(n,h)),23.);
float flash=exp(-pow(abs(curve+.22+.17*view+.14*e.y+.19*e.x+.06*k)*4.5,2.));
float streak=exp(-pow(abs((dot(n,normalize(vec3(-.27,.51,.85)))-.957)*32.),2.));
vec3 nacre=mix(vec3(.41,.82,.86),vec3(.98,.71,.39),.5+.5*cos(4.1*curve+.82*e.x+.48*k+view));
vec3 bed=skin*(.63+.33*z+.11*k+.11*freckle);
bed*=1.-.4*smoothstep(.54,.83,field(18.*e+id))*smoothstep(-.32,.61,v);
vec3 plate=bed+nacre*(pearl*(.19+.39*k)+.24*flash+.10*streak);
float pore=length((s-vec2(.065,-.29))*vec2(1.8,1.));
if(id.x==0.)plate*=1.-.51*exp(-pore*pore*193.);
plate+=.022*ring-.021*spoke+.026*(freckle-.5);
// A hyaline lamina transmits the underlying neighbour at the free rim.
vec3 film=mix(surf,plate,.27)+.075*nacre*(.44+.56*pearl);
plate=mix(plate,film,mantle*.91);
plate+=nacre*.19*exp(-pow(abs((r-.958)*60.),2.))*max(0.,-.75*e.y-.42*e.x);
plate*=1.-.26*exp(-pow(abs((r-.991)*65.),2.));
surf=mix(surf,plate,inside(r-1.,.010));
}
}
// Smooth opercular bone overlaps the foremost scale row. The raised
// cheek shares the light that travels across the flank.
float gill=body.x-(.635+.28*v*v-.045*v+.021*sin(v*5.));
float cheek=smoothstep(-.006,.012,gill);
float cheekArc=length((body-vec2(1.15,.02))/vec2(.58,.78));
float ridge=exp(-pow(abs(gill-.015)*81.,2.))-.66*exp(-pow(abs(gill+.009)*83.,2.));
vec3 plate=mix(vec3(.59,.626,.536),vec3(.215,.333,.271),smoothstep(-.62,.77,v));
plate+=vec3(.28,.29,.224)*exp(-pow(abs(cheekArc-.76+.087*view)*3.7,2.));
plate+=.19*ridge+.065*(stone(body*65.)-.5);
surf=mix(surf,plate,cheek);
// A folded pectoral fin lies forward of the lateral line. Its narrow
// membrane reveals the scales and casts a displaced shadow on the flank.
vec2 fin=rot(-.28)*(body-vec2(.79,-.22));
float t=-fin.x/.86;
float span=.29*pow(clamp(t,0.,1.),.84)+.025;
float arch=.075*sin(clamp(t,0.,1.)*3.14);
float fan=(fin.y-arch)/span;
float end=1.-.27*fan*fan+.035*fan;
float silhouette=max(max(-t,t-end+.019*cos(31.*fan)*smoothstep(.62,.98,t)),abs(fan)-1.);
float shadowD=max(max(-t,t-end),abs((fin.y-arch+.042)/span)-1.);
surf*=1.-.27*exp(-max(0.,shadowD)*12.)*smoothstep(.0,.19,t);
float ribs=(fan*.5+.5)*14.+.29*t*t;
float rib=exp(-pow(abs(fract(ribs)-.50)*9.,2.));
float splitRib=exp(-pow(abs(fract(2.*ribs+.17)-.5)*10.,2.))*smoothstep(.44,.92,t);
vec3 membrane=mix(surf,vec3(.32,.29,.208),.27+.23*(1.-clamp(t,0.,1.)));
membrane+=vec3(.176,.148,.098)*(rib+.39*splitRib)*(.78+.22*cos(t*112.));
surf=mix(surf,membrane,inside(silhouette,pixel()/.22));
return mix(backdrop,surf,inside(abs(v)-1.,pixel()));
}
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;
// Iridescent Fish Scales — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/13_fish_scales.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-animal-lib-13-fish-scales
// Fish scales
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); }
// A patch of the flank between the dorsal pigmentation and the silver belly.
// The cylindrical section foreshortens the rows at the upper silhouette.
vec3 study(vec2 p){
vec2 body=rot(-.135)*p*.94-vec2(.36,.005);
float mid=.044-.023*body.x*body.x;
float radius=.804+.147*body.x-.10*body.x*body.x;
float v=(body.y-mid)/radius;
float curve=asin(clamp(v,-.996,.996));
float z=sqrt(max(.006,1.-v*v));
vec3 backdrop=mix(vec3(.096,.176,.173),vec3(.33,.41,.347),.69*stone(p*2.)+.18*(1.-p.y));
if(abs(v)>1.) return backdrop;
vec2 q=vec2(body.x+.06*curve*curve,curve);
q=vec2(q.y*5.25,q.x*5.2+.23*cos(q.y*2.4));
float view=sin(.83*uTime);
float freckle=stone(p*109.);
float pigment=stone(body*vec2(3.8,12.));
vec3 skin=mix(vec3(.68,.71,.65),vec3(.12,.234,.198),smoothstep(-.55,.88,v));
skin=mix(skin,vec3(.102,.151,.142),.48*exp(-pow(abs((curve-.12-.044*sin(body.x*3.))*8.),2.))*(.48+.72*pigment));
vec3 surf=skin*.64;
for(int j=-1;j<=2;j++){
float row=floor(q.y/.62)+float(j);
float phase=.5*mod(row,2.);
float col=floor(q.x-phase+.5);
for(int i=-1;i<=1;i++){
vec2 id=vec2(col+float(i),row);
float k=randCell(id+6.);
vec2 s=q-vec2(id.x+phase,row*.62)-.085*vec2(k-.5,randCell(id+24.)-.5);
vec2 e=s/vec2(.63+.059*k,.76+.068*k);
float r=length(e);
if(r>1.03)continue;
float ring=cos(95.*length(e+vec2(.01,-.19))+1.7*field(s*4.+id));
float spoke=pow(.5+.5*cos(atan(e.x,e.y+.002)*49.+4.*k),9.);
float mantle=smoothstep(.65,.985,r);
vec3 n=normalize(vec3(.14*e.y+.03*sin(curve*7.),v+.13*e.x,z+.07*(1.-r)));
vec3 h=normalize(vec3(-.28+.42*view,.08,.90));
float pearl=pow(max(0.,dot(n,h)),23.);
float flash=exp(-pow(abs(curve+.22+.17*view+.14*e.y+.19*e.x+.06*k)*4.5,2.));
float streak=exp(-pow(abs((dot(n,normalize(vec3(-.27,.51,.85)))-.957)*32.),2.));
vec3 nacre=mix(vec3(.41,.82,.86),vec3(.98,.71,.39),.5+.5*cos(4.1*curve+.82*e.x+.48*k+view));
vec3 bed=skin*(.63+.33*z+.11*k+.11*freckle);
bed*=1.-.4*smoothstep(.54,.83,field(18.*e+id))*smoothstep(-.32,.61,v);
vec3 plate=bed+nacre*(pearl*(.19+.39*k)+.24*flash+.10*streak);
float pore=length((s-vec2(.065,-.29))*vec2(1.8,1.));
if(id.x==0.)plate*=1.-.51*exp(-pore*pore*193.);
plate+=.022*ring-.021*spoke+.026*(freckle-.5);
// A hyaline lamina transmits the underlying neighbour at the free rim.
vec3 film=mix(surf,plate,.27)+.075*nacre*(.44+.56*pearl);
plate=mix(plate,film,mantle*.91);
plate+=nacre*.19*exp(-pow(abs((r-.958)*60.),2.))*max(0.,-.75*e.y-.42*e.x);
plate*=1.-.26*exp(-pow(abs((r-.991)*65.),2.));
surf=mix(surf,plate,inside(r-1.,.010));
}
}
// Smooth opercular bone overlaps the foremost scale row. The raised
// cheek shares the light that travels across the flank.
float gill=body.x-(.635+.28*v*v-.045*v+.021*sin(v*5.));
float cheek=smoothstep(-.006,.012,gill);
float cheekArc=length((body-vec2(1.15,.02))/vec2(.58,.78));
float ridge=exp(-pow(abs(gill-.015)*81.,2.))-.66*exp(-pow(abs(gill+.009)*83.,2.));
vec3 plate=mix(vec3(.59,.626,.536),vec3(.215,.333,.271),smoothstep(-.62,.77,v));
plate+=vec3(.28,.29,.224)*exp(-pow(abs(cheekArc-.76+.087*view)*3.7,2.));
plate+=.19*ridge+.065*(stone(body*65.)-.5);
surf=mix(surf,plate,cheek);
// A folded pectoral fin lies forward of the lateral line. Its narrow
// membrane reveals the scales and casts a displaced shadow on the flank.
vec2 fin=rot(-.28)*(body-vec2(.79,-.22));
float t=-fin.x/.86;
float span=.29*pow(clamp(t,0.,1.),.84)+.025;
float arch=.075*sin(clamp(t,0.,1.)*3.14);
float fan=(fin.y-arch)/span;
float end=1.-.27*fan*fan+.035*fan;
float silhouette=max(max(-t,t-end+.019*cos(31.*fan)*smoothstep(.62,.98,t)),abs(fan)-1.);
float shadowD=max(max(-t,t-end),abs((fin.y-arch+.042)/span)-1.);
surf*=1.-.27*exp(-max(0.,shadowD)*12.)*smoothstep(.0,.19,t);
float ribs=(fan*.5+.5)*14.+.29*t*t;
float rib=exp(-pow(abs(fract(ribs)-.50)*9.,2.));
float splitRib=exp(-pow(abs(fract(2.*ribs+.17)-.5)*10.,2.))*smoothstep(.44,.92,t);
vec3 membrane=mix(surf,vec3(.32,.29,.208),.27+.23*(1.-clamp(t,0.,1.)));
membrane+=vec3(.176,.148,.098)*(rib+.39*splitRib)*(.78+.22*cos(t*112.));
surf=mix(surf,membrane,inside(silhouette,pixel()/.22));
return mix(backdrop,surf,inside(abs(v)-1.,pixel()));
}
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;
}
// Iridescent Fish Scales — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/13_fish_scales.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-animal-lib-13-fish-scales
// Fish scales
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); }
// A patch of the flank between the dorsal pigmentation and the silver belly.
// The cylindrical section foreshortens the rows at the upper silhouette.
vec3 study(vec2 p){
vec2 body=rot(-.135)*p*.94-vec2(.36,.005);
float mid=.044-.023*body.x*body.x;
float radius=.804+.147*body.x-.10*body.x*body.x;
float v=(body.y-mid)/radius;
float curve=asin(clamp(v,-.996,.996));
float z=sqrt(max(.006,1.-v*v));
vec3 backdrop=mix(vec3(.096,.176,.173),vec3(.33,.41,.347),.69*stone(p*2.)+.18*(1.-p.y));
if(abs(v)>1.) return backdrop;
vec2 q=vec2(body.x+.06*curve*curve,curve);
q=vec2(q.y*5.25,q.x*5.2+.23*cos(q.y*2.4));
float view=sin(.83*uTime);
float freckle=stone(p*109.);
float pigment=stone(body*vec2(3.8,12.));
vec3 skin=mix(vec3(.68,.71,.65),vec3(.12,.234,.198),smoothstep(-.55,.88,v));
skin=mix(skin,vec3(.102,.151,.142),.48*exp(-pow(abs((curve-.12-.044*sin(body.x*3.))*8.),2.))*(.48+.72*pigment));
vec3 surf=skin*.64;
for(int j=-1;j<=2;j++){
float row=floor(q.y/.62)+float(j);
float phase=.5*mod(row,2.);
float col=floor(q.x-phase+.5);
for(int i=-1;i<=1;i++){
vec2 id=vec2(col+float(i),row);
float k=randCell(id+6.);
vec2 s=q-vec2(id.x+phase,row*.62)-.085*vec2(k-.5,randCell(id+24.)-.5);
vec2 e=s/vec2(.63+.059*k,.76+.068*k);
float r=length(e);
if(r>1.03)continue;
float ring=cos(95.*length(e+vec2(.01,-.19))+1.7*field(s*4.+id));
float spoke=pow(.5+.5*cos(atan(e.x,e.y+.002)*49.+4.*k),9.);
float mantle=smoothstep(.65,.985,r);
vec3 n=normalize(vec3(.14*e.y+.03*sin(curve*7.),v+.13*e.x,z+.07*(1.-r)));
vec3 h=normalize(vec3(-.28+.42*view,.08,.90));
float pearl=pow(max(0.,dot(n,h)),23.);
float flash=exp(-pow(abs(curve+.22+.17*view+.14*e.y+.19*e.x+.06*k)*4.5,2.));
float streak=exp(-pow(abs((dot(n,normalize(vec3(-.27,.51,.85)))-.957)*32.),2.));
vec3 nacre=mix(vec3(.41,.82,.86),vec3(.98,.71,.39),.5+.5*cos(4.1*curve+.82*e.x+.48*k+view));
vec3 bed=skin*(.63+.33*z+.11*k+.11*freckle);
bed*=1.-.4*smoothstep(.54,.83,field(18.*e+id))*smoothstep(-.32,.61,v);
vec3 plate=bed+nacre*(pearl*(.19+.39*k)+.24*flash+.10*streak);
float pore=length((s-vec2(.065,-.29))*vec2(1.8,1.));
if(id.x==0.)plate*=1.-.51*exp(-pore*pore*193.);
plate+=.022*ring-.021*spoke+.026*(freckle-.5);
// A hyaline lamina transmits the underlying neighbour at the free rim.
vec3 film=mix(surf,plate,.27)+.075*nacre*(.44+.56*pearl);
plate=mix(plate,film,mantle*.91);
plate+=nacre*.19*exp(-pow(abs((r-.958)*60.),2.))*max(0.,-.75*e.y-.42*e.x);
plate*=1.-.26*exp(-pow(abs((r-.991)*65.),2.));
surf=mix(surf,plate,inside(r-1.,.010));
}
}
// Smooth opercular bone overlaps the foremost scale row. The raised
// cheek shares the light that travels across the flank.
float gill=body.x-(.635+.28*v*v-.045*v+.021*sin(v*5.));
float cheek=smoothstep(-.006,.012,gill);
float cheekArc=length((body-vec2(1.15,.02))/vec2(.58,.78));
float ridge=exp(-pow(abs(gill-.015)*81.,2.))-.66*exp(-pow(abs(gill+.009)*83.,2.));
vec3 plate=mix(vec3(.59,.626,.536),vec3(.215,.333,.271),smoothstep(-.62,.77,v));
plate+=vec3(.28,.29,.224)*exp(-pow(abs(cheekArc-.76+.087*view)*3.7,2.));
plate+=.19*ridge+.065*(stone(body*65.)-.5);
surf=mix(surf,plate,cheek);
// A folded pectoral fin lies forward of the lateral line. Its narrow
// membrane reveals the scales and casts a displaced shadow on the flank.
vec2 fin=rot(-.28)*(body-vec2(.79,-.22));
float t=-fin.x/.86;
float span=.29*pow(clamp(t,0.,1.),.84)+.025;
float arch=.075*sin(clamp(t,0.,1.)*3.14);
float fan=(fin.y-arch)/span;
float end=1.-.27*fan*fan+.035*fan;
float silhouette=max(max(-t,t-end+.019*cos(31.*fan)*smoothstep(.62,.98,t)),abs(fan)-1.);
float shadowD=max(max(-t,t-end),abs((fin.y-arch+.042)/span)-1.);
surf*=1.-.27*exp(-max(0.,shadowD)*12.)*smoothstep(.0,.19,t);
float ribs=(fan*.5+.5)*14.+.29*t*t;
float rib=exp(-pow(abs(fract(ribs)-.50)*9.,2.));
float splitRib=exp(-pow(abs(fract(2.*ribs+.17)-.5)*10.,2.))*smoothstep(.44,.92,t);
vec3 membrane=mix(surf,vec3(.32,.29,.208),.27+.23*(1.-clamp(t,0.,1.)));
membrane+=vec3(.176,.148,.098)*(rib+.39*splitRib)*(.78+.22*cos(t*112.));
surf=mix(surf,membrane,inside(silhouette,pixel()/.22));
return mix(backdrop,surf,inside(abs(v)-1.,pixel()));
}
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 createIridescentFishScalesMaterial(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 | iridescent-fish-scales |
|---|---|
| Category | Fabrics & Organic Shaders · Bark, Scales & Flora |
| Upstream study | animal/lib/13_fish_scales.glsl (commit main) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime |
| Source size | 127 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 water 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
AnimatedDense cushion of star-shaped emerald sphagnum moss heads glistening with capillary moisture.
AnimatedBranching cyan-green fungal rhizomorph veins pulsing across damp cavern bark.
PBR StudyPlush uncut cotton yarn loops casting soft micro-occlusion across woven backing.
PBR StudyDiamond-stitched gambeson / flight-suit padding with pillows puffing between taut thread seams.
PBR StudyCrisp warp-and-weft twill weave with continuous self-finished red-line selvedge edge.
PBR StudyDeep pile velvet fabric with strong grazing retro-reflective rim sheen and brushed nap swirls.