Terry-Cloth Loop Pile shader

terry-cloth-loop-pile.glsl · 171 lines · GLSL ES 3.00 · MIT

Terry-Cloth Loop PileFabrics & Organic
Terry-Cloth Loop Pile WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 171 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

Woven & Knit Fabrics
terry-cloth-loop-pile.glsl (171 lines)
#version 300 es
// Terry-Cloth Loop Pile (terry-cloth-loop-pile.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/05_terry_cloth_loop_pile.glsl
// License: MIT (Copyright 2026 Gokay Aydogan) + MIT/CC0 helpers — https://pixelfx.io/LICENSE-SHADERS.txt
// Uniforms: uResolution (vec2), uTime (float)
// Hash adapted from David Hoskins, Hash without Sine. See THIRD_PARTY_NOTICES.md.
// Credits: https://github.com/gokayfem/glsl-studies/blob/main/credit.md#shader-textiles-lib-05-terry-cloth-loop-pile
// Woven terry towelling | raised cotton loops above a woven ground.
// WebGL2: draw a fullscreen triangle with uResolution set to the framebuffer size.
precision highp float;
uniform vec2 uResolution;
out vec4 fragColor;
const float PI=3.14159265359;
const float TAU=6.28318530718;
float sat(float x) { return clamp(x,0.,1.); }
float sq(float x) { return x*x; }
float hash(vec2 p) {
    vec3 v=fract(p.xyx*vec3(.1031,.11369,.13787));
    v+=dot(v,v.yzx+19.19);
    return fract((v.x+v.y)*v.z);
}
float noise(vec2 p) {
    vec2 i=floor(p), f=fract(p);
    f=f*f*(3.-2.*f);
    return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
               mix(hash(i+vec2(0,1)),hash(i+1.),f.x),f.y);
}
float pixel() { return 2./max(1.,min(uResolution.x,uResolution.y)); }
vec2 field() { return (gl_FragCoord.xy-.5*uResolution)*pixel(); }
mat2 turn(float a) { return mat2(cos(a),sin(a),-sin(a),cos(a)); }
float aa(float d,float footprint) { return 1.-smoothstep(-footprint*.55,footprint*.55,d); }
float band(float phase,float footprint) { return cos(phase)*exp(-.11*sq(min(footprint,12.))); }
float segment(vec2 p,vec2 a,vec2 b) {
    vec2 v=b-a;
    return length(p-a-v*sat(dot(p-a,v)/max(dot(v,v),1e-8)));
}
float weave(vec2 uv,float fp) {
    // Two intersecting yarn systems. Each arches above alternate crossings.
    vec2 g=uv+.105*vec2(noise(vec2(uv.y*.17,1.)),noise(vec2(uv.x*.17,6.)));
    vec2 cell=floor(g+.5), f=fract(g+.5)-.5;
    float parity=1.-2.*mod(cell.x+cell.y,2.);
    vec2 roundness=sqrt(max(vec2(0.),1.-(f*f)/(.515*.515)));
    vec2 z=roundness*.56+parity*vec2(cos(PI*f.y),-cos(PI*f.x))*.21;
    float warp=smoothstep(-.075,.075,z.x-z.y);
    float arc=mix(roundness.y,roundness.x,warp);
    float face=mix(f.y,-f.x,warp);
    float thread=mix(g.x+f.y*.87,g.y-f.x*.87,warp);
    float twist=band(thread*15.,fp*15.);
    float contact=1.-.26*exp(-abs(z.x-z.y)*9.);
    float value=(.62+.36*arc+.24*face+.063*twist)*contact;
    return mix(value,.88,smoothstep(.30,1.15,fp));
}

// Orthographic depth test for a round swept yarn. Arc length transports the ply
// phase across segment boundaries; the normal is reconstructed from the tube.
struct Yarn {
    float z, coverage, side, crown, phase;
    vec3 normal, dye;
};
Yarn emptyYarn() { return Yarn(-1000.,0.,0.,0.,0.,vec3(0,0,1),vec3(0)); }
void strand(inout Yarn hit,vec2 p,vec3 a,vec3 b,float radius,float u,float pitch,
            float fp,vec3 dye) {
    vec3 v=b-a;
    // The irregular boundary is continuous across adjoining swept segments.
    radius*=.971+.063*noise(p*51.)+.019*noise(p*133.);
    float len=max(length(v),.00001);
    vec3 axis=v/len;
    float projected=max(dot(v.xy,v.xy),.0000001);
    float along=dot(p-a.xy,v.xy)/projected;
    vec2 across=vec2(-v.y,v.x)/sqrt(projected);
    float signedDistance=dot(p-a.xy,across);
    float dist=length(p-a.xy-sat(along)*v.xy);
    if(dist>radius+.61*fp) {
        if(dist>radius*1.75 || fp>radius*.42) return;
        float random=fract(u*41.327+radius*83.);
        float hand=random<.5?-1.:1.;
        vec2 base=mix(a.xy,b.xy,random)+across*hand*radius*.78;
        vec2 end=base+across*hand*radius*(.29+.52*random)+v.xy*.51;
        float tHair=sat(dot(p-base,end-base)/max(1e-7,dot(end-base,end-base)));
        float distanceHair=length(p-mix(base,end,tHair));
        float alpha=.52*aa(distanceHair-radius*.021*(1.-.62*tHair),fp);
        float zHair=mix(a.z,b.z,random)-radius*.21;
        if(alpha>.01 && zHair>hit.z && hit.coverage<.35)
            hit=Yarn(zHair,alpha,.5,.56,u*pitch,vec3(-.4,.5,.76),dye);
        return;
    }
    // Front intersection of an orthographic ray with the infinite cylinder.
    // Beyond a segment end the spherical cap closes the sweep continuously.
    float height=sqrt(max(0.,radius*radius-signedDistance*signedDistance))*len/sqrt(projected);
    float t=along+height*v.z/(len*len);
    float z=a.z+along*v.z+height;
    if(t<0. || t>1.) {
        t=sat(t);
        vec3 end=a+t*v;
        vec2 radial=p-end.xy;
        z=end.z+sqrt(max(0.,radius*radius-dot(radial,radial)));
    }
    float coverage=aa(dist-radius,fp);
    if(coverage<.002) return;
    // Fractional edge samples must not punch holes in a solid lower strand.
    float depth=z-(1.-coverage)*radius;
    if(depth<=hit.z) return;
    vec3 n=normalize(vec3(p,z)-(a+t*v)+vec3(0.,0.,.000001));
    float side=clamp(dot(n.xy,across),-1.,1.);
    float helix=(u+t*len)*pitch+3.*asin(side);
    n=normalize(n+vec3(across,0.)*.19*sin(helix)*exp(-.10*sq(min(pitch*fp,10.))));
    hit=Yarn(depth,coverage,side,max(0.,n.z),helix,n,dye);
}
vec3 yarnColor(Yarn hit,float fp,float pitch,float softness) {
    float broad=band(hit.phase,pitch*fp);
    float hair=band(3.2*hit.phase+11.*hit.side,3.2*pitch*fp);
    float filaments=band(60.*hit.side+1.3*hit.phase+1.6*sin(hit.phase*.83),92.*fp);
    vec3 key=normalize(vec3(-.52,.65,.58));
    float lambert=max(0.,dot(hit.normal,key));
    float satin=pow(max(0.,dot(hit.normal,normalize(key+vec3(0,0,1)))),9.);
    return hit.dye*(.44+.61*lambert+.085*broad+.04*hair+.047*filaments)
          +vec3(.12,.10,.075)*satin*softness;
}

vec3 loopPoint(float t,vec2 root,float seed,float lean) {
    float a=PI*t;
    float lift=sin(a);
    float sway=sin(TAU*t);
    float x=-.37*cos(a)-.070*sway+.04*sin(3.*a)+lean*lift;
    float y=(.55+.37*seed)*lift+.14*sway+.08*sin(3.*a)*seed;
    return vec3(root+vec2(x,y),lift*(.44+.18*seed)-.25*root.y);
}
void main() {
    vec2 p=turn(-.25)*field();
    p.x+=.10*sin(1.9*p.y);
    vec2 g=p*7.1;
    float fp=pixel()*7.1;
    vec3 ground=vec3(.44,.53,.48)*weave(g*3.5,fp*3.5);
    Yarn hit=emptyYarn();
    float shade=1.;
    // A loop-free dobby stripe exposes the anchoring weave.
    for(int j=-2;j<=1;j++) {
        float row=floor(g.y/.64)+float(j);
        float y=row*.64;
        if(y> -3.65 && y< -2.15) continue;
        float offset=.42*mod(row,2.);
        for(int i=-1;i<=1;i++) {
            float col=floor(g.x-offset+.5)+float(i);
            vec2 id=vec2(col,row);
            float seed=hash(id);
            vec2 foot=vec2(col+offset+.25*(hash(id+6.)-.5),y+.22*(seed-.5));
            float lean=.28*sin(.36*col+.73*row)+.51*(hash(id+19.)-.5);
            vec3 dye=mix(vec3(.49,.67,.60),vec3(.82,.86,.72),.29+.42*seed);
            if(mod(row+6.,27.)<2.) dye=vec3(.27,.43,.42);
            // The free pile leans away from the compressed border.
            lean+=.21*sin(col*.81+seed*3.4)+.26*sin(row*.32);
            vec3 a=loopPoint(0.,foot,seed,lean);
            float u=seed;
            for(int k=1;k<=14;k++) {
                vec3 b=loopPoint(float(k)/14.,foot,seed,lean);
                float radius=.132+.032*seed;
                float contact=segment(g+vec2(.10,.11),a.xy,b.xy);
                shade=min(shade,.50+.50*smoothstep(radius,radius+.23,contact));
                strand(hit,g,a,b,radius,u,34.,fp,dye);
                u+=length(b-a); a=b;
            }
        }
    }
    vec3 c=ground*shade;
    if(hit.coverage>0.) {
        vec3 loops=yarnColor(hit,fp,34.,.67);
        float wool=noise(g*37.+vec2(2.*hit.phase));
        loops*=.90+.18*wool;
        loops+=vec3(.039,.042,.030)*pow(1.-hit.crown,2.)*wool;
        c=mix(c,loops*(.93+.12*hit.crown),hit.coverage);
    }
    c*=.95+.09*noise(p*1.7);
    fragColor=vec4(c,1.);
}

Mathematical models & shader specifications

Identifierterry-cloth-loop-pile
CategoryFabrics & Organic Shaders · Woven & Knit Fabrics
Upstream studytextiles/lib/05_terry_cloth_loop_pile.glsl (commit 1fbd084)
Animation cycleStatic analytical PBR surface
Declared uniformsuResolution
Source size171 lines · 7.3 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT nature sprite sheets
LicenseMIT License + CC0 / MIT Helpers

Physical & shading models implemented

Procedural signed-distance and analytic surface shading

Adapted upstream mathematical helpers

David Hoskins, hash

All fabrics & organic shaders
Quilted Padded Fabric WebGL2 procedural shaderPBR Study

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

Fabrics & Organic