PBR StudyDiamond-stitched gambeson / flight-suit padding with pillows puffing between taut thread seams.
Fabrics & Organic
terry-cloth-loop-pile.glsl · 171 lines · GLSL ES 3.00 · MIT
#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.);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Terry-Cloth Loop Pile — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/05_terry_cloth_loop_pile.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-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.
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)));
}
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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
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.);
}
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;
// Terry-Cloth Loop Pile — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/05_terry_cloth_loop_pile.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-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.
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)));
}
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 pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
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.);
}
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;
}
// Terry-Cloth Loop Pile — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/textiles/lib/05_terry_cloth_loop_pile.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-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.);
}
`;
export function createTerryClothLoopPileMaterial(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 | terry-cloth-loop-pile |
|---|---|
| Category | Fabrics & Organic Shaders · Woven & Knit Fabrics |
| Upstream study | textiles/lib/05_terry_cloth_loop_pile.glsl (commit 1fbd084) |
| Animation cycle | Static analytical PBR surface |
| Declared uniforms | uResolution |
| Source size | 171 lines · 7.3 KB standalone GLSL ES 3.00 |
| PNG baker outputs | 512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key) |
| Companion pixel VFX | Free MIT nature sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Procedural signed-distance and analytic surface shading
David Hoskins, hash
PBR StudyDiamond-stitched gambeson / flight-suit padding with pillows puffing between taut thread seams.
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.