AnimatedCycloid silver-blue scales with concentric growth circuli and guanine thin-film shimmer.
overlapping-reptile-scales.glsl · 141 lines · GLSL ES 3.00 · MIT
#version 300 es
// Overlapping Reptile Scales (overlapping-reptile-scales.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/12_snake_scales.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float)
// Snake scales
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-animal-lib-12-snake-scales
precision highp float;
precision highp int;
uniform vec2 uResolution;
out vec4 fragColor;
const float PI=3.14159265359, TAU=6.28318530718;
float sat(float x){return clamp(x,0.,1.);}
float sq(float x){return x*x;}
float gauss(float x){return exp(-x*x);}
mat2 spin(float a){float c=cos(a),s=sin(a);return mat2(c,s,-s,c);}
float rand(vec2 cell){
uvec2 k=uvec2(ivec2(cell)); uint h=k.x*1597334677u+k.y*3812015801u+1136930381u;
h=(h^(h>>16u))*2246822519u;return float(h^(h>>13u))/4294967295.;
}
float noise2(vec2 p){
vec2 i=floor(p),f=fract(p); f=f*f*(3.-2.*f);
return mix(mix(rand(i),rand(i+vec2(1,0)),f.x),mix(rand(i+vec2(0,1)),rand(i+1.),f.x),f.y);
}
float grain(vec2 p){return .52*noise2(p)+.30*noise2(3.7*p)+.18*noise2(12.6*p);}
vec2 screen(){return (2.*gl_FragCoord.xy-uResolution)/min(uResolution.y,.95*uResolution.x);}
float pixel(){return 2./min(uResolution.y,.95*uResolution.x);}
vec3 softLight(vec3 paint,vec3 n,vec3 view,vec3 key,float spec,float polish,float shadow){
float lit=max(0.,dot(n,key));
vec3 halfVector=normalize(key+view);
vec3 ambient=mix(vec3(.22,.22,.24),vec3(.56,.59,.60),.5+.5*n.z);
vec3 light=ambient+vec3(.79,.72,.61)*lit*shadow;
float glint=pow(max(0.,dot(n,halfVector)),polish)*spec*shadow;
return paint*light+glint*vec3(1.,.93,.80);
}
const vec3 KEY=vec3(-.50,-.58,.83);
vec3 bodyFrame(vec3 p){
float width=.47+.063*sin(.84*p.x+.6);
float center=.20*sin(1.8*p.x+.31);
return vec3((p.y-center)/width,(p.z+.03)/(.77*width),width);
}
vec4 oneScale(vec2 uv,vec2 id){
vec2 q=uv-id-vec2(.5*mod(id.y,2.),0);
float seed=rand(id);
float t=(q.x+.67)/1.34;
float mid=q.y-.047*sin(PI*t+.8*seed);
float halfWidth=.60*pow(max(0.,sin(PI*sat(t))),.83)*(1.12-.26*t);
float edge=max(abs(mid)-halfWidth,max(-t,t-1.));
float arch=sqrt(max(0.,1.-sq(mid/max(.002,halfWidth))));
float keel=gauss(mid/.067)*sin(PI*sat(t))*(.81+.19*seed);
float rise=(.001+.0055*arch+.005*t+.0055*keel)*smoothstep(-.012,.05,-edge);
return vec4(rise,edge,keel,t);
}
vec4 scaleAtlas(vec2 uv){
vec4 result=vec4(.0,.0,.0,.0);
float row=floor(uv.y+.5);
for(int j=-1;j<=1;j++){
float line=row+float(j);
float col=floor(uv.x-.5*mod(line,2.)+.5);
for(int i=-1;i<=1;i++){
vec4 sc=oneScale(uv,vec2(col+float(i),line));
if(sc.x>result.x){result=sc;result.y=col+float(i);result.w=line;}
}
}
return result;
}
vec2 atlasPoint(vec3 p,vec3 f){return vec2(p.x*5.1,atan(f.x,f.y)*6.2);}
float floorZ(vec2 p){return -.44+.044*noise2(p*3.2)+.010*noise2(p*14.);}
vec2 field(vec3 p){
vec3 f=bodyFrame(p);float tube=(length(f.xy)-1.)*f.z*.74;
float shell=tube;
if(abs(tube)<.12)shell-=scaleAtlas(atlasPoint(p,f)).x;
float floorD=(p.z-floorZ(p.xy))*.73;
return shell<floorD?vec2(shell*.72,1.):vec2(floorD,0.);
}
vec3 backdrop(vec2 uv){return vec3(.30,.274,.189);}
vec3 colorHit(vec3 p,vec3 n,vec3 v,vec3 l,float sh,float id){
if(id<.5){
vec2 q=spin(.51)*p.xy;float grainDir=grain(vec2(q.x*8.,q.y*23.));
return softLight(mix(vec3(.252,.221,.15),vec3(.48,.423,.291),grainDir),n,v,l,.03,25.,sh);
}
vec3 f=bodyFrame(p);vec2 uv=atlasPoint(p,f);vec4 sc=scaleAtlas(uv);
float angle=abs(atan(f.x,f.y));
float lateral=angle+.06*sin(1.9*p.x)+.05*sin(p.x*5.);
float buff=gauss((lateral-.81)/.179);
float lower=gauss((lateral-1.65)/.095);
float bars=.5+.5*cos(7.*p.x+1.65*sin(1.5*p.x)+2.9*angle);
float mott=grain(vec2(uv.x*.58,uv.y*.55));
vec3 olive=mix(vec3(.155,.172,.11),vec3(.52,.46,.28),.31+.32*mott+.23*bars);
vec2 row=sc.yw;
float plateNoise=mix(noise2(uv*vec2(.58,.55)),noise2(row*vec2(.58,.55)),smoothstep(.0008,.0038,sc.x));
float melanism=smoothstep(.36,.65,.48*plateNoise+.50*noise2(uv*1.54)+.14*cos(uv.x*1.12-angle));
vec3 dark=mix(vec3(.064,.073,.085),vec3(.163,.177,.143),mott);
vec3 tone=mix(mix(olive,dark,.89*melanism),vec3(.80,.69,.47),.90*buff+.33*lower);
tone*=1.-.39*smoothstep(.52,.89,bars)*smoothstep(1.,1.31,angle)*(1.-smoothstep(1.85,2.1,angle));
tone=mix(tone,vec3(.79,.708,.535),smoothstep(1.81,2.60,angle));
tone*=.82+.17*grain(vec2(uv.x*18.,uv.y*36.))+.045*sc.z;
tone*=.87+.13*smoothstep(.001,.006,sc.x);
return softLight(tone,n,v,l,.22,67.,sh);
}
// visibility uses the clearance / travel penumbra estimate described by Inigo Quilez in his softshadow() article (archived at https://vodacek.zvb.cz/archiv/102.html).
// Orthographic macro camera with a depth-tested surface and a finite contact shadow.
vec3 surfaceNormal(vec3 p,float e){
vec3 a=vec3(e,0,0);
vec3 gradient=vec3(field(p+a).x-field(p-a).x,field(p+a.yxy).x-field(p-a.yxy).x,field(p+a.yyx).x-field(p-a.yyx).x);
return dot(gradient,gradient)>1e-15?normalize(gradient):vec3(0.,0.,1.);
}
float visibility(vec3 p,vec3 light,float bias){
float travel=bias,illumination=1.;
for(int j=0;j<28;j++){
float clearance=field(p+light*travel).x;
illumination=min(illumination,13.*max(0.,clearance)/travel);
travel+=clamp(clearance*.83,.008,.20);
if(travel>1.65||illumination<.02)break;
}
return mix(.38,1.,sat(illumination));
}
vec3 traceScene(vec2 uv,vec3 eye,vec3 target,float zoom){
vec3 view=normalize(eye-target);
vec3 right=normalize(cross(vec3(0,0,1),view)),up=cross(view,right);
vec3 origin=target+view*3.7+zoom*(uv.x*right+uv.y*up);
float travel=0.; vec2 hit=vec2(1.,0.);
for(int i=0;i<210;i++){
hit=field(origin-view*travel);
if(hit.x<.0008||travel>13.)break;
travel+=clamp(hit.x*.74,.0006,.40);
}
vec3 p=origin-view*travel;
if(travel>13.)return backdrop(uv);
vec3 normal=surfaceNormal(p,.0018);
vec3 key=normalize(KEY);
float shadow=visibility(p+normal*.004,key,.013);
return colorHit(p,normal,view,key,shadow,hit.y);
}
vec3 sampleFrame(vec2 uvScreen){return traceScene(spin(-.29)*uvScreen,vec3(.76,-2.4,1.68),vec3(.02,.02,.02),.60);}
void main(){
vec2 uv=screen();
const vec2 taps[4]=vec2[4](vec2(-.36,-.13),vec2(.13,-.36),vec2(.36,.13),vec2(-.13,.36));
vec3 sum=vec3(0.);
for(int i=0;i<4;i++)sum+=sampleFrame(uv+pixel()*taps[i]);
fragColor=vec4(.25*sum,1.);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Overlapping Reptile Scales — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/12_snake_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;
// Snake scales
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-animal-lib-12-snake-scales
const float PI=3.14159265359, TAU=6.28318530718;
float sat(float x){return clamp(x,0.,1.);}
float sq(float x){return x*x;}
float gauss(float x){return exp(-x*x);}
mat2 spin(float a){float c=cos(a),s=sin(a);return mat2(c,s,-s,c);}
float rand(vec2 cell){
uvec2 k=uvec2(ivec2(cell)); uint h=k.x*1597334677u+k.y*3812015801u+1136930381u;
h=(h^(h>>16u))*2246822519u;return float(h^(h>>13u))/4294967295.;
}
float noise2(vec2 p){
vec2 i=floor(p),f=fract(p); f=f*f*(3.-2.*f);
return mix(mix(rand(i),rand(i+vec2(1,0)),f.x),mix(rand(i+vec2(0,1)),rand(i+1.),f.x),f.y);
}
float grain(vec2 p){return .52*noise2(p)+.30*noise2(3.7*p)+.18*noise2(12.6*p);}
vec2 screen(){return (2.*pfx_frag_xy-uResolution)/min(uResolution.y,.95*uResolution.x);}
float pixel(){return 2./min(uResolution.y,.95*uResolution.x);}
vec3 softLight(vec3 paint,vec3 n,vec3 view,vec3 key,float spec,float polish,float shadow){
float lit=max(0.,dot(n,key));
vec3 halfVector=normalize(key+view);
vec3 ambient=mix(vec3(.22,.22,.24),vec3(.56,.59,.60),.5+.5*n.z);
vec3 light=ambient+vec3(.79,.72,.61)*lit*shadow;
float glint=pow(max(0.,dot(n,halfVector)),polish)*spec*shadow;
return paint*light+glint*vec3(1.,.93,.80);
}
const vec3 KEY=vec3(-.50,-.58,.83);
vec3 bodyFrame(vec3 p){
float width=.47+.063*sin(.84*p.x+.6);
float center=.20*sin(1.8*p.x+.31);
return vec3((p.y-center)/width,(p.z+.03)/(.77*width),width);
}
vec4 oneScale(vec2 uv,vec2 id){
vec2 q=uv-id-vec2(.5*mod(id.y,2.),0);
float seed=rand(id);
float t=(q.x+.67)/1.34;
float mid=q.y-.047*sin(PI*t+.8*seed);
float halfWidth=.60*pow(max(0.,sin(PI*sat(t))),.83)*(1.12-.26*t);
float edge=max(abs(mid)-halfWidth,max(-t,t-1.));
float arch=sqrt(max(0.,1.-sq(mid/max(.002,halfWidth))));
float keel=gauss(mid/.067)*sin(PI*sat(t))*(.81+.19*seed);
float rise=(.001+.0055*arch+.005*t+.0055*keel)*smoothstep(-.012,.05,-edge);
return vec4(rise,edge,keel,t);
}
vec4 scaleAtlas(vec2 uv){
vec4 result=vec4(.0,.0,.0,.0);
float row=floor(uv.y+.5);
for(int j=-1;j<=1;j++){
float line=row+float(j);
float col=floor(uv.x-.5*mod(line,2.)+.5);
for(int i=-1;i<=1;i++){
vec4 sc=oneScale(uv,vec2(col+float(i),line));
if(sc.x>result.x){result=sc;result.y=col+float(i);result.w=line;}
}
}
return result;
}
vec2 atlasPoint(vec3 p,vec3 f){return vec2(p.x*5.1,atan(f.x,f.y)*6.2);}
float floorZ(vec2 p){return -.44+.044*noise2(p*3.2)+.010*noise2(p*14.);}
vec2 field(vec3 p){
vec3 f=bodyFrame(p);float tube=(length(f.xy)-1.)*f.z*.74;
float shell=tube;
if(abs(tube)<.12)shell-=scaleAtlas(atlasPoint(p,f)).x;
float floorD=(p.z-floorZ(p.xy))*.73;
return shell<floorD?vec2(shell*.72,1.):vec2(floorD,0.);
}
vec3 backdrop(vec2 uv){return vec3(.30,.274,.189);}
vec3 colorHit(vec3 p,vec3 n,vec3 v,vec3 l,float sh,float id){
if(id<.5){
vec2 q=spin(.51)*p.xy;float grainDir=grain(vec2(q.x*8.,q.y*23.));
return softLight(mix(vec3(.252,.221,.15),vec3(.48,.423,.291),grainDir),n,v,l,.03,25.,sh);
}
vec3 f=bodyFrame(p);vec2 uv=atlasPoint(p,f);vec4 sc=scaleAtlas(uv);
float angle=abs(atan(f.x,f.y));
float lateral=angle+.06*sin(1.9*p.x)+.05*sin(p.x*5.);
float buff=gauss((lateral-.81)/.179);
float lower=gauss((lateral-1.65)/.095);
float bars=.5+.5*cos(7.*p.x+1.65*sin(1.5*p.x)+2.9*angle);
float mott=grain(vec2(uv.x*.58,uv.y*.55));
vec3 olive=mix(vec3(.155,.172,.11),vec3(.52,.46,.28),.31+.32*mott+.23*bars);
vec2 row=sc.yw;
float plateNoise=mix(noise2(uv*vec2(.58,.55)),noise2(row*vec2(.58,.55)),smoothstep(.0008,.0038,sc.x));
float melanism=smoothstep(.36,.65,.48*plateNoise+.50*noise2(uv*1.54)+.14*cos(uv.x*1.12-angle));
vec3 dark=mix(vec3(.064,.073,.085),vec3(.163,.177,.143),mott);
vec3 tone=mix(mix(olive,dark,.89*melanism),vec3(.80,.69,.47),.90*buff+.33*lower);
tone*=1.-.39*smoothstep(.52,.89,bars)*smoothstep(1.,1.31,angle)*(1.-smoothstep(1.85,2.1,angle));
tone=mix(tone,vec3(.79,.708,.535),smoothstep(1.81,2.60,angle));
tone*=.82+.17*grain(vec2(uv.x*18.,uv.y*36.))+.045*sc.z;
tone*=.87+.13*smoothstep(.001,.006,sc.x);
return softLight(tone,n,v,l,.22,67.,sh);
}
// visibility uses the clearance / travel penumbra estimate described by Inigo Quilez in his softshadow() article (archived at https://vodacek.zvb.cz/archiv/102.html).
// Orthographic macro camera with a depth-tested surface and a finite contact shadow.
vec3 surfaceNormal(vec3 p,float e){
vec3 a=vec3(e,0,0);
vec3 gradient=vec3(field(p+a).x-field(p-a).x,field(p+a.yxy).x-field(p-a.yxy).x,field(p+a.yyx).x-field(p-a.yyx).x);
return dot(gradient,gradient)>1e-15?normalize(gradient):vec3(0.,0.,1.);
}
float visibility(vec3 p,vec3 light,float bias){
float travel=bias,illumination=1.;
for(int j=0;j<28;j++){
float clearance=field(p+light*travel).x;
illumination=min(illumination,13.*max(0.,clearance)/travel);
travel+=clamp(clearance*.83,.008,.20);
if(travel>1.65||illumination<.02)break;
}
return mix(.38,1.,sat(illumination));
}
vec3 traceScene(vec2 uv,vec3 eye,vec3 target,float zoom){
vec3 view=normalize(eye-target);
vec3 right=normalize(cross(vec3(0,0,1),view)),up=cross(view,right);
vec3 origin=target+view*3.7+zoom*(uv.x*right+uv.y*up);
float travel=0.; vec2 hit=vec2(1.,0.);
for(int i=0;i<210;i++){
hit=field(origin-view*travel);
if(hit.x<.0008||travel>13.)break;
travel+=clamp(hit.x*.74,.0006,.40);
}
vec3 p=origin-view*travel;
if(travel>13.)return backdrop(uv);
vec3 normal=surfaceNormal(p,.0018);
vec3 key=normalize(KEY);
float shadow=visibility(p+normal*.004,key,.013);
return colorHit(p,normal,view,key,shadow,hit.y);
}
vec3 sampleFrame(vec2 uvScreen){return traceScene(spin(-.29)*uvScreen,vec3(.76,-2.4,1.68),vec3(.02,.02,.02),.60);}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor){
vec2 uv=screen();
const vec2 taps[4]=vec2[4](vec2(-.36,-.13),vec2(.13,-.36),vec2(.36,.13),vec2(-.13,.36));
vec3 sum=vec3(0.);
for(int i=0;i<4;i++)sum+=sampleFrame(uv+pixel()*taps[i]);
fragColor=vec4(.25*sum,1.);
}
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;
// Overlapping Reptile Scales — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/12_snake_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;
// Snake scales
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-animal-lib-12-snake-scales
const float PI=3.14159265359, TAU=6.28318530718;
float sat(float x){return clamp(x,0.,1.);}
float sq(float x){return x*x;}
float gauss(float x){return exp(-x*x);}
mat2 spin(float a){float c=cos(a),s=sin(a);return mat2(c,s,-s,c);}
float rand(vec2 cell){
uvec2 k=uvec2(ivec2(cell)); uint h=k.x*1597334677u+k.y*3812015801u+1136930381u;
h=(h^(h>>16u))*2246822519u;return float(h^(h>>13u))/4294967295.;
}
float noise2(vec2 p){
vec2 i=floor(p),f=fract(p); f=f*f*(3.-2.*f);
return mix(mix(rand(i),rand(i+vec2(1,0)),f.x),mix(rand(i+vec2(0,1)),rand(i+1.),f.x),f.y);
}
float grain(vec2 p){return .52*noise2(p)+.30*noise2(3.7*p)+.18*noise2(12.6*p);}
vec2 screen(){return (2.*pfx_frag_xy-uResolution)/min(uResolution.y,.95*uResolution.x);}
float pixel(){return 2./min(uResolution.y,.95*uResolution.x);}
vec3 softLight(vec3 paint,vec3 n,vec3 view,vec3 key,float spec,float polish,float shadow){
float lit=max(0.,dot(n,key));
vec3 halfVector=normalize(key+view);
vec3 ambient=mix(vec3(.22,.22,.24),vec3(.56,.59,.60),.5+.5*n.z);
vec3 light=ambient+vec3(.79,.72,.61)*lit*shadow;
float glint=pow(max(0.,dot(n,halfVector)),polish)*spec*shadow;
return paint*light+glint*vec3(1.,.93,.80);
}
const vec3 KEY=vec3(-.50,-.58,.83);
vec3 bodyFrame(vec3 p){
float width=.47+.063*sin(.84*p.x+.6);
float center=.20*sin(1.8*p.x+.31);
return vec3((p.y-center)/width,(p.z+.03)/(.77*width),width);
}
vec4 oneScale(vec2 uv,vec2 id){
vec2 q=uv-id-vec2(.5*mod(id.y,2.),0);
float seed=rand(id);
float t=(q.x+.67)/1.34;
float mid=q.y-.047*sin(PI*t+.8*seed);
float halfWidth=.60*pow(max(0.,sin(PI*sat(t))),.83)*(1.12-.26*t);
float edge=max(abs(mid)-halfWidth,max(-t,t-1.));
float arch=sqrt(max(0.,1.-sq(mid/max(.002,halfWidth))));
float keel=gauss(mid/.067)*sin(PI*sat(t))*(.81+.19*seed);
float rise=(.001+.0055*arch+.005*t+.0055*keel)*smoothstep(-.012,.05,-edge);
return vec4(rise,edge,keel,t);
}
vec4 scaleAtlas(vec2 uv){
vec4 result=vec4(.0,.0,.0,.0);
float row=floor(uv.y+.5);
for(int j=-1;j<=1;j++){
float line=row+float(j);
float col=floor(uv.x-.5*mod(line,2.)+.5);
for(int i=-1;i<=1;i++){
vec4 sc=oneScale(uv,vec2(col+float(i),line));
if(sc.x>result.x){result=sc;result.y=col+float(i);result.w=line;}
}
}
return result;
}
vec2 atlasPoint(vec3 p,vec3 f){return vec2(p.x*5.1,atan(f.x,f.y)*6.2);}
float floorZ(vec2 p){return -.44+.044*noise2(p*3.2)+.010*noise2(p*14.);}
vec2 field(vec3 p){
vec3 f=bodyFrame(p);float tube=(length(f.xy)-1.)*f.z*.74;
float shell=tube;
if(abs(tube)<.12)shell-=scaleAtlas(atlasPoint(p,f)).x;
float floorD=(p.z-floorZ(p.xy))*.73;
return shell<floorD?vec2(shell*.72,1.):vec2(floorD,0.);
}
vec3 backdrop(vec2 uv){return vec3(.30,.274,.189);}
vec3 colorHit(vec3 p,vec3 n,vec3 v,vec3 l,float sh,float id){
if(id<.5){
vec2 q=spin(.51)*p.xy;float grainDir=grain(vec2(q.x*8.,q.y*23.));
return softLight(mix(vec3(.252,.221,.15),vec3(.48,.423,.291),grainDir),n,v,l,.03,25.,sh);
}
vec3 f=bodyFrame(p);vec2 uv=atlasPoint(p,f);vec4 sc=scaleAtlas(uv);
float angle=abs(atan(f.x,f.y));
float lateral=angle+.06*sin(1.9*p.x)+.05*sin(p.x*5.);
float buff=gauss((lateral-.81)/.179);
float lower=gauss((lateral-1.65)/.095);
float bars=.5+.5*cos(7.*p.x+1.65*sin(1.5*p.x)+2.9*angle);
float mott=grain(vec2(uv.x*.58,uv.y*.55));
vec3 olive=mix(vec3(.155,.172,.11),vec3(.52,.46,.28),.31+.32*mott+.23*bars);
vec2 row=sc.yw;
float plateNoise=mix(noise2(uv*vec2(.58,.55)),noise2(row*vec2(.58,.55)),smoothstep(.0008,.0038,sc.x));
float melanism=smoothstep(.36,.65,.48*plateNoise+.50*noise2(uv*1.54)+.14*cos(uv.x*1.12-angle));
vec3 dark=mix(vec3(.064,.073,.085),vec3(.163,.177,.143),mott);
vec3 tone=mix(mix(olive,dark,.89*melanism),vec3(.80,.69,.47),.90*buff+.33*lower);
tone*=1.-.39*smoothstep(.52,.89,bars)*smoothstep(1.,1.31,angle)*(1.-smoothstep(1.85,2.1,angle));
tone=mix(tone,vec3(.79,.708,.535),smoothstep(1.81,2.60,angle));
tone*=.82+.17*grain(vec2(uv.x*18.,uv.y*36.))+.045*sc.z;
tone*=.87+.13*smoothstep(.001,.006,sc.x);
return softLight(tone,n,v,l,.22,67.,sh);
}
// visibility uses the clearance / travel penumbra estimate described by Inigo Quilez in his softshadow() article (archived at https://vodacek.zvb.cz/archiv/102.html).
// Orthographic macro camera with a depth-tested surface and a finite contact shadow.
vec3 surfaceNormal(vec3 p,float e){
vec3 a=vec3(e,0,0);
vec3 gradient=vec3(field(p+a).x-field(p-a).x,field(p+a.yxy).x-field(p-a.yxy).x,field(p+a.yyx).x-field(p-a.yyx).x);
return dot(gradient,gradient)>1e-15?normalize(gradient):vec3(0.,0.,1.);
}
float visibility(vec3 p,vec3 light,float bias){
float travel=bias,illumination=1.;
for(int j=0;j<28;j++){
float clearance=field(p+light*travel).x;
illumination=min(illumination,13.*max(0.,clearance)/travel);
travel+=clamp(clearance*.83,.008,.20);
if(travel>1.65||illumination<.02)break;
}
return mix(.38,1.,sat(illumination));
}
vec3 traceScene(vec2 uv,vec3 eye,vec3 target,float zoom){
vec3 view=normalize(eye-target);
vec3 right=normalize(cross(vec3(0,0,1),view)),up=cross(view,right);
vec3 origin=target+view*3.7+zoom*(uv.x*right+uv.y*up);
float travel=0.; vec2 hit=vec2(1.,0.);
for(int i=0;i<210;i++){
hit=field(origin-view*travel);
if(hit.x<.0008||travel>13.)break;
travel+=clamp(hit.x*.74,.0006,.40);
}
vec3 p=origin-view*travel;
if(travel>13.)return backdrop(uv);
vec3 normal=surfaceNormal(p,.0018);
vec3 key=normalize(KEY);
float shadow=visibility(p+normal*.004,key,.013);
return colorHit(p,normal,view,key,shadow,hit.y);
}
vec3 sampleFrame(vec2 uvScreen){return traceScene(spin(-.29)*uvScreen,vec3(.76,-2.4,1.68),vec3(.02,.02,.02),.60);}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor){
vec2 uv=screen();
const vec2 taps[4]=vec2[4](vec2(-.36,-.13),vec2(.13,-.36),vec2(.36,.13),vec2(-.13,.36));
vec3 sum=vec3(0.);
for(int i=0;i<4;i++)sum+=sampleFrame(uv+pixel()*taps[i]);
fragColor=vec4(.25*sum,1.);
}
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;
}
// Overlapping Reptile Scales — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/animal/lib/12_snake_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 */ `
// Snake scales
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-animal-lib-12-snake-scales
precision highp float;
precision highp int;
uniform vec2 uResolution;
out vec4 fragColor;
const float PI=3.14159265359, TAU=6.28318530718;
float sat(float x){return clamp(x,0.,1.);}
float sq(float x){return x*x;}
float gauss(float x){return exp(-x*x);}
mat2 spin(float a){float c=cos(a),s=sin(a);return mat2(c,s,-s,c);}
float rand(vec2 cell){
uvec2 k=uvec2(ivec2(cell)); uint h=k.x*1597334677u+k.y*3812015801u+1136930381u;
h=(h^(h>>16u))*2246822519u;return float(h^(h>>13u))/4294967295.;
}
float noise2(vec2 p){
vec2 i=floor(p),f=fract(p); f=f*f*(3.-2.*f);
return mix(mix(rand(i),rand(i+vec2(1,0)),f.x),mix(rand(i+vec2(0,1)),rand(i+1.),f.x),f.y);
}
float grain(vec2 p){return .52*noise2(p)+.30*noise2(3.7*p)+.18*noise2(12.6*p);}
vec2 screen(){return (2.*gl_FragCoord.xy-uResolution)/min(uResolution.y,.95*uResolution.x);}
float pixel(){return 2./min(uResolution.y,.95*uResolution.x);}
vec3 softLight(vec3 paint,vec3 n,vec3 view,vec3 key,float spec,float polish,float shadow){
float lit=max(0.,dot(n,key));
vec3 halfVector=normalize(key+view);
vec3 ambient=mix(vec3(.22,.22,.24),vec3(.56,.59,.60),.5+.5*n.z);
vec3 light=ambient+vec3(.79,.72,.61)*lit*shadow;
float glint=pow(max(0.,dot(n,halfVector)),polish)*spec*shadow;
return paint*light+glint*vec3(1.,.93,.80);
}
const vec3 KEY=vec3(-.50,-.58,.83);
vec3 bodyFrame(vec3 p){
float width=.47+.063*sin(.84*p.x+.6);
float center=.20*sin(1.8*p.x+.31);
return vec3((p.y-center)/width,(p.z+.03)/(.77*width),width);
}
vec4 oneScale(vec2 uv,vec2 id){
vec2 q=uv-id-vec2(.5*mod(id.y,2.),0);
float seed=rand(id);
float t=(q.x+.67)/1.34;
float mid=q.y-.047*sin(PI*t+.8*seed);
float halfWidth=.60*pow(max(0.,sin(PI*sat(t))),.83)*(1.12-.26*t);
float edge=max(abs(mid)-halfWidth,max(-t,t-1.));
float arch=sqrt(max(0.,1.-sq(mid/max(.002,halfWidth))));
float keel=gauss(mid/.067)*sin(PI*sat(t))*(.81+.19*seed);
float rise=(.001+.0055*arch+.005*t+.0055*keel)*smoothstep(-.012,.05,-edge);
return vec4(rise,edge,keel,t);
}
vec4 scaleAtlas(vec2 uv){
vec4 result=vec4(.0,.0,.0,.0);
float row=floor(uv.y+.5);
for(int j=-1;j<=1;j++){
float line=row+float(j);
float col=floor(uv.x-.5*mod(line,2.)+.5);
for(int i=-1;i<=1;i++){
vec4 sc=oneScale(uv,vec2(col+float(i),line));
if(sc.x>result.x){result=sc;result.y=col+float(i);result.w=line;}
}
}
return result;
}
vec2 atlasPoint(vec3 p,vec3 f){return vec2(p.x*5.1,atan(f.x,f.y)*6.2);}
float floorZ(vec2 p){return -.44+.044*noise2(p*3.2)+.010*noise2(p*14.);}
vec2 field(vec3 p){
vec3 f=bodyFrame(p);float tube=(length(f.xy)-1.)*f.z*.74;
float shell=tube;
if(abs(tube)<.12)shell-=scaleAtlas(atlasPoint(p,f)).x;
float floorD=(p.z-floorZ(p.xy))*.73;
return shell<floorD?vec2(shell*.72,1.):vec2(floorD,0.);
}
vec3 backdrop(vec2 uv){return vec3(.30,.274,.189);}
vec3 colorHit(vec3 p,vec3 n,vec3 v,vec3 l,float sh,float id){
if(id<.5){
vec2 q=spin(.51)*p.xy;float grainDir=grain(vec2(q.x*8.,q.y*23.));
return softLight(mix(vec3(.252,.221,.15),vec3(.48,.423,.291),grainDir),n,v,l,.03,25.,sh);
}
vec3 f=bodyFrame(p);vec2 uv=atlasPoint(p,f);vec4 sc=scaleAtlas(uv);
float angle=abs(atan(f.x,f.y));
float lateral=angle+.06*sin(1.9*p.x)+.05*sin(p.x*5.);
float buff=gauss((lateral-.81)/.179);
float lower=gauss((lateral-1.65)/.095);
float bars=.5+.5*cos(7.*p.x+1.65*sin(1.5*p.x)+2.9*angle);
float mott=grain(vec2(uv.x*.58,uv.y*.55));
vec3 olive=mix(vec3(.155,.172,.11),vec3(.52,.46,.28),.31+.32*mott+.23*bars);
vec2 row=sc.yw;
float plateNoise=mix(noise2(uv*vec2(.58,.55)),noise2(row*vec2(.58,.55)),smoothstep(.0008,.0038,sc.x));
float melanism=smoothstep(.36,.65,.48*plateNoise+.50*noise2(uv*1.54)+.14*cos(uv.x*1.12-angle));
vec3 dark=mix(vec3(.064,.073,.085),vec3(.163,.177,.143),mott);
vec3 tone=mix(mix(olive,dark,.89*melanism),vec3(.80,.69,.47),.90*buff+.33*lower);
tone*=1.-.39*smoothstep(.52,.89,bars)*smoothstep(1.,1.31,angle)*(1.-smoothstep(1.85,2.1,angle));
tone=mix(tone,vec3(.79,.708,.535),smoothstep(1.81,2.60,angle));
tone*=.82+.17*grain(vec2(uv.x*18.,uv.y*36.))+.045*sc.z;
tone*=.87+.13*smoothstep(.001,.006,sc.x);
return softLight(tone,n,v,l,.22,67.,sh);
}
// visibility uses the clearance / travel penumbra estimate described by Inigo Quilez in his softshadow() article (archived at https://vodacek.zvb.cz/archiv/102.html).
// Orthographic macro camera with a depth-tested surface and a finite contact shadow.
vec3 surfaceNormal(vec3 p,float e){
vec3 a=vec3(e,0,0);
vec3 gradient=vec3(field(p+a).x-field(p-a).x,field(p+a.yxy).x-field(p-a.yxy).x,field(p+a.yyx).x-field(p-a.yyx).x);
return dot(gradient,gradient)>1e-15?normalize(gradient):vec3(0.,0.,1.);
}
float visibility(vec3 p,vec3 light,float bias){
float travel=bias,illumination=1.;
for(int j=0;j<28;j++){
float clearance=field(p+light*travel).x;
illumination=min(illumination,13.*max(0.,clearance)/travel);
travel+=clamp(clearance*.83,.008,.20);
if(travel>1.65||illumination<.02)break;
}
return mix(.38,1.,sat(illumination));
}
vec3 traceScene(vec2 uv,vec3 eye,vec3 target,float zoom){
vec3 view=normalize(eye-target);
vec3 right=normalize(cross(vec3(0,0,1),view)),up=cross(view,right);
vec3 origin=target+view*3.7+zoom*(uv.x*right+uv.y*up);
float travel=0.; vec2 hit=vec2(1.,0.);
for(int i=0;i<210;i++){
hit=field(origin-view*travel);
if(hit.x<.0008||travel>13.)break;
travel+=clamp(hit.x*.74,.0006,.40);
}
vec3 p=origin-view*travel;
if(travel>13.)return backdrop(uv);
vec3 normal=surfaceNormal(p,.0018);
vec3 key=normalize(KEY);
float shadow=visibility(p+normal*.004,key,.013);
return colorHit(p,normal,view,key,shadow,hit.y);
}
vec3 sampleFrame(vec2 uvScreen){return traceScene(spin(-.29)*uvScreen,vec3(.76,-2.4,1.68),vec3(.02,.02,.02),.60);}
void main(){
vec2 uv=screen();
const vec2 taps[4]=vec2[4](vec2(-.36,-.13),vec2(.13,-.36),vec2(.36,.13),vec2(-.13,.36));
vec3 sum=vec3(0.);
for(int i=0;i<4;i++)sum+=sampleFrame(uv+pixel()*taps[i]);
fragColor=vec4(.25*sum,1.);
}
`;
export function createOverlappingReptileScalesMaterial(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 | overlapping-reptile-scales |
|---|---|
| Category | Fabrics & Organic Shaders · Bark, Scales & Flora |
| Upstream study | animal/lib/12_snake_scales.glsl (commit main) |
| Animation cycle | Static analytical PBR surface |
| Declared uniforms | uResolution |
| Source size | 141 lines · 6.2 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT critters sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Procedural signed-distance and analytic surface shading
visibility() uses the published clearance-to-travel ratio in its finite shadow march. Iñigo Quilez, Free penumbra shadows for raymarching distance fields — softshadow()
AnimatedCycloid silver-blue scales with concentric growth circuli and guanine thin-film shimmer.
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.