Crushed Grazing Velvet shader

crushed-grazing-velvet.glsl · 91 lines · GLSL ES 3.00 · MIT

Crushed Grazing VelvetFabrics & Organic
Crushed Grazing Velvet WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 91 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
crushed-grazing-velvet.glsl (91 lines)
#version 300 es
// Crushed Grazing Velvet (crushed-grazing-velvet.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/12_velvet.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-12-velvet
// Cut velvet | light grazes a folded and brushed garnet pile.
// 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 drape(vec2 p) {
    float tuck=2.4*p.x+.95*p.y+.38*sin(2.2*p.y);
    return .17*sin(tuck)-.115*exp(-sq((tuck-.18)*1.9))
           +.075*sin(4.9*p.y-1.9*p.x+.62*sin(2.*p.x));
}
vec3 pile(vec2 p,float face,float fp) {
    float e=.003;
    vec2 deriv=vec2(drape(p+vec2(e,0))-drape(p-vec2(e,0)),drape(p+vec2(0,e))-drape(p-vec2(0,e)))/(2.*e);
    vec3 n=normalize(vec3(-deriv,1.));
    // A dragged hand bends adjacent fibres along a broad curved path.
    float trail=p.y+.36*sin(2.1*p.x)-.17*p.x;
    float brush=exp(-sq((trail-.09)*3.9))*(.79+.21*noise(p*4.));
    float lay=1.03+face*1.82+2.05*brush+.58*deriv.x+.32*(noise(p*6.)-.5);
    vec2 axis=vec2(cos(lay),sin(lay));
    float nap=dot(axis,normalize(vec2(-.75,.53)));
    vec3 light=normalize(vec3(-.77,.48,.39));
    float rake=sat(.45+.80*dot(n,light));
    float sheen=pow(rake,1.9)*(.49+.49*nap);
    vec3 c=vec3(.255,.051,.108)*(.63+.53*rake)+vec3(.57,.188,.263)*sheen;
    vec2 g=p*117., cell=floor(g);
    float footprint=fp*117.;
    float radiance=0.;
    // Resolve directional shafts at gallery resolution, then average the
    // unresolved material response at a lower sampling density.
    for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
        vec2 id=cell+vec2(i,j);
        float seed=hash(id);
        vec2 start=id+vec2(hash(id+7.),seed);
        vec2 direction=turn((seed-.5)*.88)*axis;
        float len=.62+.70*hash(id+13.);
        vec2 rel=g-start;
        float t=sat(dot(rel,direction)/len);
        float dist=length(rel-direction*len*t);
        float fibre=aa(dist-(.067+.044*(1.-t)),max(.025,footprint));
        radiance+=fibre*mix(-.21,.64,t)*(.55+.45*seed);
    }
    vec2 perp=vec2(-axis.y,axis.x);
    float fluff=.56*noise(p*80.)+.44*noise(p*36.+axis*.7);
    // Evaluate the fine sheen in the tuft's local frame to keep it aligned
    // with the shafts even where the brushed direction turns sharply.
    float fibrePhase=dot(fract(g)-.5,perp)*TAU+TAU*hash(cell);
    float fly=.5+.5*band(fibrePhase,fp*735.);
    c*=.79+.26*fluff+.065*fly+radiance*(1.-smoothstep(.64,1.7,footprint));
    return c;
}
void main() {
    vec2 p=turn(-.18)*field();
    float fp=pixel();
    float crest=p.y+.61+.22*p.x-.12*sin(2.1*p.x);
    vec3 c=pile(p,0.,fp)*(1.-.64*exp(-max(crest,0.)*13.));
    // Velvet turned back onto itself exposes the cut tips in profile at the lip.
    float nap=.009*noise(vec2(p.x*278.,3.))+.006*noise(vec2(p.x*88.,6.));
    vec2 folded=vec2(p.x+.18,crest*.82-.32);
    vec3 face=pile(folded,1.,fp);
    face+=vec3(.18,.076,.11)*exp(-sq((crest+.055)*11.));
    face*=.86+.14*smoothstep(-.12,0.,crest);
    c=mix(c,face,aa(crest-nap,fp));
    float tips=exp(-sq((crest-nap)*170.));
    c+=vec3(.077,.035,.044)*tips*(.4+.6*noise(vec2(p.x*172.,1.)));
    fragColor=vec4(c,1.);
}

Mathematical models & shader specifications

Identifiercrushed-grazing-velvet
CategoryFabrics & Organic Shaders · Woven & Knit Fabrics
Upstream studytextiles/lib/12_velvet.glsl (commit 1fbd084)
Animation cycleStatic analytical PBR surface
Declared uniformsuResolution
Source size91 lines · 4.0 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT holy 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