Ribbed Cotton Corduroy shader

ribbed-cotton-corduroy.glsl · 109 lines · GLSL ES 3.00 · MIT

Ribbed Cotton CorduroyFabrics & Organic
Ribbed Cotton Corduroy WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 109 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
ribbed-cotton-corduroy.glsl (109 lines)
#version 300 es
// Ribbed Cotton Corduroy (ribbed-cotton-corduroy.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/13_corduroy.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-13-corduroy
// Wide-wale corduroy | cut filling tufts on a worn trouser-pocket edge.
// 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));
}

vec3 wales(vec2 p,float fp) {
    vec2 uv=p;
    uv.x+=.045*sin(2.6*p.y-.6*p.x)+.025*sin(4.8*p.y+.4*p.x);
    float u=uv.x*6.75;
    float id=floor(u+.5), q=fract(u+.5)-.5;
    float seed=hash(vec2(id,4.));
    float ridges=noise(vec2(id,p.y*61.));
    q+=.014*(ridges-.5)+.009*band(159.*p.y+6.*seed,fp*159.);
    float width=.412+.027*seed+.015*(noise(vec2(id,p.y*16.))-.5);
    float side=clamp(q/width,-1.,1.);
    float crown=sqrt(max(0.,1.-side*side));
    vec3 n=normalize(vec3(side,.09*sin(p.y*5.),.29+.76*crown));
    float light=max(0.,dot(n,normalize(vec3(-.65,.42,.64))));
    vec3 ground=vec3(.29,.235,.151)*weave(p*83.,fp*83.);
    // Short cut tufts splay away from the spine of the wale; the furrows
    // expose transverse binder picks. Random tuft groups interrupt the edge.
    vec2 tuft=vec2((q*7.2),p.y*100.+2.*abs(q)+.2*noise(p*39.));
    float cluster=noise(tuft*1.8);
    float pile=.5+.5*band(TAU*(tuft.y+1.8*tuft.x+1.3*cluster),fp*720.);
    float ripple=.5+.5*band(TAU*(tuft.y*.43-.9*tuft.x+.55*noise(tuft)),fp*320.);
    float fringe=.026*(cluster-.4)+.015*(noise(p*172.)-.5);
    vec3 dye=mix(vec3(.55,.39,.226),vec3(.735,.56,.345),seed*.34+.38*cluster);
    vec2 grid=p*111.;
    vec2 cell=floor(grid);
    float lint=0.;
    for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
        vec2 cellID=cell+vec2(i,j);
        float rand=hash(cellID);
        vec2 root=cellID+vec2(rand,hash(cellID+5.));
        vec2 tangent=normalize(vec2(1.2*side+.61*(rand-.5),.70+.37*rand));
        vec2 delta=grid-root;
        float t=sat(dot(delta,tangent));
        float dist=length(delta-t*tangent);
        lint+=aa(dist-.10,fp*111.)*mix(-.15,.53,t);
    }
    vec3 rib=dye*(.51+.55*light+.11*pile+.09*ripple+lint*(1.-smoothstep(.7,1.6,fp*111.)));
    rib+=vec3(.103,.083,.052)*crown*(.3+.7*cluster);
    float surface=abs(q)-width-fringe;
    return mix(ground*(.77+.20*crown),rib,aa(surface,fp*6.75));
}
void main() {
    vec2 p=turn(.24)*field();
    float fp=pixel();
    float lip=p.y+.50-.16*p.x+.087*sin(2.3*p.x);
    vec3 c=wales(p,fp)*(1.-.50*exp(-max(lip,0.)*20.));
    vec2 pocket=turn(-.58)*vec2(p.x,lip+.05);
    vec3 front=wales(pocket,fp);
    front*=.82+.20*exp(-sq((lip+.056)*14.));
    c=mix(c,front,aa(lip,fp));
    for(int i=0;i<2;i++) {
        vec2 q=vec2(mod(p.x+.035*float(i),.112)-.056,lip+.043+.070*float(i));
        float d=segment(q,vec2(-.035,0),vec2(.035,-.002));
        c*=1.-.47*exp(-sq(d/.014));
        vec3 thread=vec3(.87,.69,.43)*(.77+.13*band(420.*q.x+280.*q.y,fp*470.));
        c=mix(c,thread,aa(d-.006,fp));
    }
    c*=.93+.10*noise(p*1.9);
    fragColor=vec4(c,1.);
}

Mathematical models & shader specifications

Identifierribbed-cotton-corduroy
CategoryFabrics & Organic Shaders · Woven & Knit Fabrics
Upstream studytextiles/lib/13_corduroy.glsl (commit 1fbd084)
Animation cycleStatic analytical PBR surface
Declared uniformsuResolution
Source size109 lines · 4.7 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