Overlapping Reptile Scales shader

overlapping-reptile-scales.glsl · 141 lines · GLSL ES 3.00 · MIT

Overlapping Reptile ScalesFabrics & Organic
Overlapping Reptile Scales WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 141 linesSPEC GLSL ES 3.00

Seamless PNG Texture Baker
1-Click Engine Shader Export
WebGL2 .glsl
.glsl
Godot 4 .gdshader
Three.js GLSL3

Engine shader code & wrappers

Bark, Scales & Flora
overlapping-reptile-scales.glsl (141 lines)
#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.);
}

Mathematical models & shader specifications

Identifieroverlapping-reptile-scales
CategoryFabrics & Organic Shaders · Bark, Scales & Flora
Upstream studyanimal/lib/12_snake_scales.glsl (commit main)
Animation cycleStatic analytical PBR surface
Declared uniformsuResolution
Source size141 lines · 6.2 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT critters sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Procedural signed-distance and analytic surface shading

Adapted upstream mathematical helpers

visibility() uses the published clearance-to-travel ratio in its finite shadow march. Iñigo Quilez, Free penumbra shadows for raymarching distance fields — softshadow()

All fabrics & organic shaders
Sphagnum Forest Moss WebGL2 procedural shaderAnimated

Dense cushion of star-shaped emerald sphagnum moss heads glistening with capillary moisture.

Fabrics & Organic
Quilted Padded Fabric WebGL2 procedural shaderPBR Study

Diamond-stitched gambeson / flight-suit padding with pillows puffing between taut thread seams.

Fabrics & Organic