PBR StudyCrisp warp-and-weft twill weave with continuous self-finished red-line selvedge edge.
Fabrics & Organic
quilted-padded-fabric.glsl · 117 lines · GLSL ES 3.00 · MIT
#version 300 es
// Quilted Padded Fabric (quilted-padded-fabric.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/09_quilted_batting_between_stitches.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-09-quilted-batting-between-stitches
// Wholecloth quilt | a cut edge exposes the cotton batting under the stitched channels.
// 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)));
}
vec3 backdrop(vec2 p,vec3 tint) {
return tint*(.86+.20*noise(p*3.)+.07*noise(p*32.)+.045*noise(p*130.));
}
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));
}
vec2 quilting(vec2 p) {
// A pair of gently bowed seam families compress a diamond of filling.
return vec2(p.x+.76*p.y+.10*sin(2.8*p.y),p.x-.76*p.y+.06*sin(4.*p.y))*1.53;
}
float loft(vec2 p) {
vec2 g=quilting(p), f=abs(sin(PI*g));
float fill=pow(max(.00001,f.x*f.y),.52);
vec2 edge=abs(fract(g+.5)-.5);
float seam=min(edge.x,edge.y);
float along=edge.x<edge.y?g.y:g.x;
float gathers=sin(35.*along+16.*seam+1.2*sin(13.*along));
return .165*fill+.012*gathers*exp(-seam*10.)*smoothstep(0.,.065,seam);
}
vec3 topCloth(vec2 p,float fp) {
float e=.0025,h=loft(p);
vec2 slope=vec2(loft(p+vec2(e,0))-loft(p-vec2(e,0)),loft(p+vec2(0,e))-loft(p-vec2(0,e)))/(2.*e);
vec3 n=normalize(vec3(-slope,1.));
float light=.54+.48*max(dot(n,normalize(vec3(-.55,.6,.66))),0.);
vec2 g=quilting(p), f=fract(g+.5)-.5;
bool first=abs(f.x)<abs(f.y);
float across=first?f.x:f.y, along=first?g.y:g.x;
float course=along*9.;
float id=floor(course+.5);
float stitch=segment(vec2(across,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
vec3 cloth=vec3(.26,.49,.48)*weave((p+slope*.009)*76.,fp*76.)*light;
cloth*=.93+.08*noise(p*4.);
cloth*=1.-.30*exp(-abs(across)*73.);
float stitchCast=segment(vec2(across+.0028,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
cloth*=1.-.30*exp(-sq(stitchCast/.0085));
float eye=max(abs(across)-.004,abs(abs(fract(course+.5)-.5)-.24)*.11-.006);
cloth*=1.-.41*aa(eye,fp);
vec3 cotton=vec3(.84,.77,.58)*(.78+.17*band(course*21.+id,fp*260.));
return mix(cloth,cotton,aa(stitch-.0048,fp*1.65));
}
void main() {
vec2 p=turn(-.18)*field();
float fp=pixel();
float edge=-.38+.074*sin(2.7*p.x+.7);
float height=loft(vec2(p.x,edge));
float top=edge+.78*height;
float bottom=edge-.073-.71*height;
float nap=.024*(noise(vec2(p.x*178.,3.))-.5)+.006*sin(166.*p.x);
vec3 c=backdrop(p,vec3(.46,.382,.30));
c*=1.-.46*exp(-max(bottom-.028-p.y,0.)*14.);
float filling=aa(bottom-p.y+nap,fp);
float t=sat((p.y-bottom)/max(.01,top-bottom));
float fleecy=noise(vec2(p.x*53.,p.y*82.))+.5*noise(p*176.);
float lanes=p.y*159.+2.4*sin(18.*p.x)+1.2*noise(p*36.);
float filaments=band(TAU*lanes,fp*770.);
float fibre=.5+.5*filaments;
float slubs=noise(vec2(p.x*71.,p.y*163.));
vec3 wadding=vec3(.88,.85,.72)*(.57+.19*fleecy+.10*fibre+.11*t+.12*slubs);
wadding*=.84+.16*sin(PI*t);
c=mix(c,wadding,filling);
float backing=abs(p.y-bottom+.012)-.019-nap*.32;
c=mix(c,vec3(.33,.42,.35)*weave(p*111.,fp*111.),aa(backing,fp));
vec2 clothUV=vec2(p.x,p.y-.78*height*exp(-max(p.y-top,0.)*9.));
c=mix(c,topCloth(clothUV,fp),aa(top-p.y+nap*.21,fp));
float lip=abs(p.y-top+.004*(noise(p*147.)-.5))-.0045;
c=mix(c,vec3(.44,.60,.54)*(.91+.09*band(p.x*493.,fp*493.)),aa(lip,fp));
fragColor=vec4(c,1.);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Quilted Padded Fabric — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/09_quilted_batting_between_stitches.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;
// 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-09-quilted-batting-between-stitches
// Wholecloth quilt | a cut edge exposes the cotton batting under the stitched channels.
// WebGL2: draw a fullscreen triangle with uResolution set to the framebuffer size.
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 (pfx_frag_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)));
}
vec3 backdrop(vec2 p,vec3 tint) {
return tint*(.86+.20*noise(p*3.)+.07*noise(p*32.)+.045*noise(p*130.));
}
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));
}
vec2 quilting(vec2 p) {
// A pair of gently bowed seam families compress a diamond of filling.
return vec2(p.x+.76*p.y+.10*sin(2.8*p.y),p.x-.76*p.y+.06*sin(4.*p.y))*1.53;
}
float loft(vec2 p) {
vec2 g=quilting(p), f=abs(sin(PI*g));
float fill=pow(max(.00001,f.x*f.y),.52);
vec2 edge=abs(fract(g+.5)-.5);
float seam=min(edge.x,edge.y);
float along=edge.x<edge.y?g.y:g.x;
float gathers=sin(35.*along+16.*seam+1.2*sin(13.*along));
return .165*fill+.012*gathers*exp(-seam*10.)*smoothstep(0.,.065,seam);
}
vec3 topCloth(vec2 p,float fp) {
float e=.0025,h=loft(p);
vec2 slope=vec2(loft(p+vec2(e,0))-loft(p-vec2(e,0)),loft(p+vec2(0,e))-loft(p-vec2(0,e)))/(2.*e);
vec3 n=normalize(vec3(-slope,1.));
float light=.54+.48*max(dot(n,normalize(vec3(-.55,.6,.66))),0.);
vec2 g=quilting(p), f=fract(g+.5)-.5;
bool first=abs(f.x)<abs(f.y);
float across=first?f.x:f.y, along=first?g.y:g.x;
float course=along*9.;
float id=floor(course+.5);
float stitch=segment(vec2(across,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
vec3 cloth=vec3(.26,.49,.48)*weave((p+slope*.009)*76.,fp*76.)*light;
cloth*=.93+.08*noise(p*4.);
cloth*=1.-.30*exp(-abs(across)*73.);
float stitchCast=segment(vec2(across+.0028,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
cloth*=1.-.30*exp(-sq(stitchCast/.0085));
float eye=max(abs(across)-.004,abs(abs(fract(course+.5)-.5)-.24)*.11-.006);
cloth*=1.-.41*aa(eye,fp);
vec3 cotton=vec3(.84,.77,.58)*(.78+.17*band(course*21.+id,fp*260.));
return mix(cloth,cotton,aa(stitch-.0048,fp*1.65));
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p=turn(-.18)*field();
float fp=pixel();
float edge=-.38+.074*sin(2.7*p.x+.7);
float height=loft(vec2(p.x,edge));
float top=edge+.78*height;
float bottom=edge-.073-.71*height;
float nap=.024*(noise(vec2(p.x*178.,3.))-.5)+.006*sin(166.*p.x);
vec3 c=backdrop(p,vec3(.46,.382,.30));
c*=1.-.46*exp(-max(bottom-.028-p.y,0.)*14.);
float filling=aa(bottom-p.y+nap,fp);
float t=sat((p.y-bottom)/max(.01,top-bottom));
float fleecy=noise(vec2(p.x*53.,p.y*82.))+.5*noise(p*176.);
float lanes=p.y*159.+2.4*sin(18.*p.x)+1.2*noise(p*36.);
float filaments=band(TAU*lanes,fp*770.);
float fibre=.5+.5*filaments;
float slubs=noise(vec2(p.x*71.,p.y*163.));
vec3 wadding=vec3(.88,.85,.72)*(.57+.19*fleecy+.10*fibre+.11*t+.12*slubs);
wadding*=.84+.16*sin(PI*t);
c=mix(c,wadding,filling);
float backing=abs(p.y-bottom+.012)-.019-nap*.32;
c=mix(c,vec3(.33,.42,.35)*weave(p*111.,fp*111.),aa(backing,fp));
vec2 clothUV=vec2(p.x,p.y-.78*height*exp(-max(p.y-top,0.)*9.));
c=mix(c,topCloth(clothUV,fp),aa(top-p.y+nap*.21,fp));
float lip=abs(p.y-top+.004*(noise(p*147.)-.5))-.0045;
c=mix(c,vec3(.44,.60,.54)*(.91+.09*band(p.x*493.,fp*493.)),aa(lip,fp));
fragColor=vec4(c,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;
// Quilted Padded Fabric — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/09_quilted_batting_between_stitches.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;
// 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-09-quilted-batting-between-stitches
// Wholecloth quilt | a cut edge exposes the cotton batting under the stitched channels.
// WebGL2: draw a fullscreen triangle with uResolution set to the framebuffer size.
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 (pfx_frag_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)));
}
vec3 backdrop(vec2 p,vec3 tint) {
return tint*(.86+.20*noise(p*3.)+.07*noise(p*32.)+.045*noise(p*130.));
}
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));
}
vec2 quilting(vec2 p) {
// A pair of gently bowed seam families compress a diamond of filling.
return vec2(p.x+.76*p.y+.10*sin(2.8*p.y),p.x-.76*p.y+.06*sin(4.*p.y))*1.53;
}
float loft(vec2 p) {
vec2 g=quilting(p), f=abs(sin(PI*g));
float fill=pow(max(.00001,f.x*f.y),.52);
vec2 edge=abs(fract(g+.5)-.5);
float seam=min(edge.x,edge.y);
float along=edge.x<edge.y?g.y:g.x;
float gathers=sin(35.*along+16.*seam+1.2*sin(13.*along));
return .165*fill+.012*gathers*exp(-seam*10.)*smoothstep(0.,.065,seam);
}
vec3 topCloth(vec2 p,float fp) {
float e=.0025,h=loft(p);
vec2 slope=vec2(loft(p+vec2(e,0))-loft(p-vec2(e,0)),loft(p+vec2(0,e))-loft(p-vec2(0,e)))/(2.*e);
vec3 n=normalize(vec3(-slope,1.));
float light=.54+.48*max(dot(n,normalize(vec3(-.55,.6,.66))),0.);
vec2 g=quilting(p), f=fract(g+.5)-.5;
bool first=abs(f.x)<abs(f.y);
float across=first?f.x:f.y, along=first?g.y:g.x;
float course=along*9.;
float id=floor(course+.5);
float stitch=segment(vec2(across,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
vec3 cloth=vec3(.26,.49,.48)*weave((p+slope*.009)*76.,fp*76.)*light;
cloth*=.93+.08*noise(p*4.);
cloth*=1.-.30*exp(-abs(across)*73.);
float stitchCast=segment(vec2(across+.0028,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
cloth*=1.-.30*exp(-sq(stitchCast/.0085));
float eye=max(abs(across)-.004,abs(abs(fract(course+.5)-.5)-.24)*.11-.006);
cloth*=1.-.41*aa(eye,fp);
vec3 cotton=vec3(.84,.77,.58)*(.78+.17*band(course*21.+id,fp*260.));
return mix(cloth,cotton,aa(stitch-.0048,fp*1.65));
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p=turn(-.18)*field();
float fp=pixel();
float edge=-.38+.074*sin(2.7*p.x+.7);
float height=loft(vec2(p.x,edge));
float top=edge+.78*height;
float bottom=edge-.073-.71*height;
float nap=.024*(noise(vec2(p.x*178.,3.))-.5)+.006*sin(166.*p.x);
vec3 c=backdrop(p,vec3(.46,.382,.30));
c*=1.-.46*exp(-max(bottom-.028-p.y,0.)*14.);
float filling=aa(bottom-p.y+nap,fp);
float t=sat((p.y-bottom)/max(.01,top-bottom));
float fleecy=noise(vec2(p.x*53.,p.y*82.))+.5*noise(p*176.);
float lanes=p.y*159.+2.4*sin(18.*p.x)+1.2*noise(p*36.);
float filaments=band(TAU*lanes,fp*770.);
float fibre=.5+.5*filaments;
float slubs=noise(vec2(p.x*71.,p.y*163.));
vec3 wadding=vec3(.88,.85,.72)*(.57+.19*fleecy+.10*fibre+.11*t+.12*slubs);
wadding*=.84+.16*sin(PI*t);
c=mix(c,wadding,filling);
float backing=abs(p.y-bottom+.012)-.019-nap*.32;
c=mix(c,vec3(.33,.42,.35)*weave(p*111.,fp*111.),aa(backing,fp));
vec2 clothUV=vec2(p.x,p.y-.78*height*exp(-max(p.y-top,0.)*9.));
c=mix(c,topCloth(clothUV,fp),aa(top-p.y+nap*.21,fp));
float lip=abs(p.y-top+.004*(noise(p*147.)-.5))-.0045;
c=mix(c,vec3(.44,.60,.54)*(.91+.09*band(p.x*493.,fp*493.)),aa(lip,fp));
fragColor=vec4(c,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;
}
// Quilted Padded Fabric — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/09_quilted_batting_between_stitches.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 */ `
// 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-09-quilted-batting-between-stitches
// Wholecloth quilt | a cut edge exposes the cotton batting under the stitched channels.
// 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)));
}
vec3 backdrop(vec2 p,vec3 tint) {
return tint*(.86+.20*noise(p*3.)+.07*noise(p*32.)+.045*noise(p*130.));
}
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));
}
vec2 quilting(vec2 p) {
// A pair of gently bowed seam families compress a diamond of filling.
return vec2(p.x+.76*p.y+.10*sin(2.8*p.y),p.x-.76*p.y+.06*sin(4.*p.y))*1.53;
}
float loft(vec2 p) {
vec2 g=quilting(p), f=abs(sin(PI*g));
float fill=pow(max(.00001,f.x*f.y),.52);
vec2 edge=abs(fract(g+.5)-.5);
float seam=min(edge.x,edge.y);
float along=edge.x<edge.y?g.y:g.x;
float gathers=sin(35.*along+16.*seam+1.2*sin(13.*along));
return .165*fill+.012*gathers*exp(-seam*10.)*smoothstep(0.,.065,seam);
}
vec3 topCloth(vec2 p,float fp) {
float e=.0025,h=loft(p);
vec2 slope=vec2(loft(p+vec2(e,0))-loft(p-vec2(e,0)),loft(p+vec2(0,e))-loft(p-vec2(0,e)))/(2.*e);
vec3 n=normalize(vec3(-slope,1.));
float light=.54+.48*max(dot(n,normalize(vec3(-.55,.6,.66))),0.);
vec2 g=quilting(p), f=fract(g+.5)-.5;
bool first=abs(f.x)<abs(f.y);
float across=first?f.x:f.y, along=first?g.y:g.x;
float course=along*9.;
float id=floor(course+.5);
float stitch=segment(vec2(across,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
vec3 cloth=vec3(.26,.49,.48)*weave((p+slope*.009)*76.,fp*76.)*light;
cloth*=.93+.08*noise(p*4.);
cloth*=1.-.30*exp(-abs(across)*73.);
float stitchCast=segment(vec2(across+.0028,fract(course+.5)-.5)*vec2(1.,1./9.),
vec2(0,-.025),vec2(.001,.025));
cloth*=1.-.30*exp(-sq(stitchCast/.0085));
float eye=max(abs(across)-.004,abs(abs(fract(course+.5)-.5)-.24)*.11-.006);
cloth*=1.-.41*aa(eye,fp);
vec3 cotton=vec3(.84,.77,.58)*(.78+.17*band(course*21.+id,fp*260.));
return mix(cloth,cotton,aa(stitch-.0048,fp*1.65));
}
void main() {
vec2 p=turn(-.18)*field();
float fp=pixel();
float edge=-.38+.074*sin(2.7*p.x+.7);
float height=loft(vec2(p.x,edge));
float top=edge+.78*height;
float bottom=edge-.073-.71*height;
float nap=.024*(noise(vec2(p.x*178.,3.))-.5)+.006*sin(166.*p.x);
vec3 c=backdrop(p,vec3(.46,.382,.30));
c*=1.-.46*exp(-max(bottom-.028-p.y,0.)*14.);
float filling=aa(bottom-p.y+nap,fp);
float t=sat((p.y-bottom)/max(.01,top-bottom));
float fleecy=noise(vec2(p.x*53.,p.y*82.))+.5*noise(p*176.);
float lanes=p.y*159.+2.4*sin(18.*p.x)+1.2*noise(p*36.);
float filaments=band(TAU*lanes,fp*770.);
float fibre=.5+.5*filaments;
float slubs=noise(vec2(p.x*71.,p.y*163.));
vec3 wadding=vec3(.88,.85,.72)*(.57+.19*fleecy+.10*fibre+.11*t+.12*slubs);
wadding*=.84+.16*sin(PI*t);
c=mix(c,wadding,filling);
float backing=abs(p.y-bottom+.012)-.019-nap*.32;
c=mix(c,vec3(.33,.42,.35)*weave(p*111.,fp*111.),aa(backing,fp));
vec2 clothUV=vec2(p.x,p.y-.78*height*exp(-max(p.y-top,0.)*9.));
c=mix(c,topCloth(clothUV,fp),aa(top-p.y+nap*.21,fp));
float lip=abs(p.y-top+.004*(noise(p*147.)-.5))-.0045;
c=mix(c,vec3(.44,.60,.54)*(.91+.09*band(p.x*493.,fp*493.)),aa(lip,fp));
fragColor=vec4(c,1.);
}
`;
export function createQuiltedPaddedFabricMaterial(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 | quilted-padded-fabric |
|---|---|
| Category | Fabrics & Organic Shaders · Woven & Knit Fabrics |
| Upstream study | textiles/lib/09_quilted_batting_between_stitches.glsl (commit 1fbd084) |
| Animation cycle | Static analytical PBR surface |
| Declared uniforms | uResolution |
| Source size | 117 lines · 5.2 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT melee sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Procedural signed-distance and analytic surface shading
David Hoskins, hash
PBR StudyCrisp warp-and-weft twill weave with continuous self-finished red-line selvedge edge.
PBR StudyDeep pile velvet fabric with strong grazing retro-reflective rim sheen and brushed nap swirls.
PBR StudyParallel tufted velveteen wales separated by crisp base-weave channels with fibrous sheen.
PBR StudyShingled metallic mylar discs tilting individually to throw glittering specular flashes.
PBR StudySmooth cherry and alder tree bark stretched horizontally by corky breathing lenticels.
PBR StudyChalk-white papery birch periderm curling back to expose warm cinnamon inner bark.