AnimatedFree-surface Rankine whirlpool depression with spiral capillary ridges and Fresnel sky reflection.
Water & Ice
shoreline-swash-foam.glsl · 123 lines · GLSL ES 3.00 · MIT
#version 300 es
// Shoreline Swash & Foam (shoreline-swash-foam.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/08_shoreline_swash_and_retreat.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-liquids-lib-08-shoreline-swash-and-retreat
// Swash over shell sand, from the high-water stain to the next incoming bore.
// uResolution: framebuffer dimensions in pixels. Optional uTime: seconds (default 0).
precision highp float;
uniform vec2 uResolution;
uniform float uTime;
out vec4 fragColor;
const float PI=3.14159265;
vec2 frame() {
return (2.*gl_FragCoord.xy-uResolution)/min(uResolution.x,uResolution.y);
}
float hash(vec2 p) {
vec3 q=fract(p.xyx*vec3(.1031,.1030,.0973));
q+=dot(q,q.yzx+33.33);
return fract((q.x+q.y)*q.z);
}
float noise2(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 terrain(vec2 p) {
mat2 m=mat2(1.62,1.19,-1.19,1.62);
return .54*noise2(p)+.27*noise2(m*p+7.1)+.13*noise2(m*m*p+19.)+
.06*noise2(m*m*m*p-8.3);
}
float coast(float x) {
return -.22*x+.13*sin(2.9*x+.7)+.055*sin(7.2*x-1.3);
}
float excursion(float phase) {
return -.34*cos(phase)-.085*cos(2.*phase+.35);
}
float grains(vec2 p) {
mat2 grainAxes=mat2(.84,.54,-.54,.84);
return .45*noise2(118.*grainAxes*p)+.35*noise2(201.*transpose(grainAxes)*p)+.20*terrain(12.*p);
}
vec3 beach(vec2 p) {
float mineral=grains(p), pockets=smoothstep(.71,.90,noise2(130.*mat2(.77,.64,-.64,.77)*p));
vec3 sand=mix(vec3(.61,.47,.32),vec3(.93,.79,.58),mineral);
sand-=vec3(.16,.14,.10)*pockets;
vec2 cell=floor(11.*p),local=fract(11.*p)-.5;
vec2 center=.57*(vec2(hash(cell+7.),hash(cell+11.))-.5);
vec2 q=local-center; q.x+=.45*q.y;
float peb=length(q/vec2(.14,.085))/(.25+hash(cell+5.));
sand*=.86+.14*smoothstep(.8,1.5,peb);
sand=mix(sand,mix(vec3(.42,.36,.27),vec3(.91,.86,.71),hash(cell)),
(1.-smoothstep(.72,1.,peb))*smoothstep(.67,.85,hash(cell+18.)));
return sand;
}
vec3 shell(vec2 p,vec3 sand) {
vec2 q=mat2(.76,.65,-.65,.76)*(p-vec2(.51,-.49));
float rib=atan(q.x,q.y+.060);
float radial=length(q/vec2(.072,.062))+.045*cos(15.*rib);
float shadow=length((q-vec2(.007,-.006))/vec2(.076,.066));
sand*=.68+.32*smoothstep(.76,1.18,shadow);
float light=.84+.11*cos(15.*rib)+.08*q.y/.07;
vec3 nacre=mix(vec3(.67,.45,.33),vec3(.94,.81,.65),.52+.33*sin(12.*rib+1.));
return mix(sand,nacre*light,1.-smoothstep(.95,1.04,radial));
}
float bubbleWalls(vec2 p) {
vec2 cell=floor(p),f=fract(p);
float d1=5.,d2=5.;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id=vec2(i,j),q=id+.13+.74*vec2(hash(cell+id),hash(cell+id+19.))-f;
float d=dot(q,q);
if(d<d1) { d2=d1; d1=d; } else d2=min(d2,d);
}
return sqrt(d2)-sqrt(d1);
}
float freeSurface(vec2 p,float shoreY,float t) {
vec2 waves=p*vec2(2.3,5.2)+vec2(.16*t,-.62*t);
return .027*sin(11.*shoreY-1.8*t+1.7*terrain(3.*p))+
.031*terrain(waves)+.008*noise2(4.*waves);
}
void main() {
vec2 p=frame(); float t=uTime+4.25;
float phase=.84*t+.13*sin(2.6*p.x);
float y=p.y-coast(p.x);
float d=y-excursion(phase)+.058*(terrain(vec2(11.*p.x,4.*p.y))-.5);
float wet=smoothstep(-.55,-.40,y+.052*terrain(12.*p));
vec3 dry=shell(p,beach(p));
vec3 sand=dry*mix(vec3(1.),vec3(.70,.75,.76),wet);
float local=freeSurface(p,y,t);
vec2 e=vec2(.005,0.);
vec2 slope=(vec2(freeSurface(p+e,y+.002,t),freeSurface(p+e.yx,y+.005,t))-local)/.005;
float depth=smoothstep(-.02,1.38,d);
vec2 refracted=p-.08*slope*depth;
vec3 floorColor=shell(refracted,beach(refracted));
vec3 water=mix(floorColor*vec3(.66,.83,.79),vec3(.04,.39,.42),.23+.74*depth);
vec3 n=normalize(vec3(-slope,1.));
water+=vec3(.16,.20,.19)*smoothstep(.04,.65,n.y+.3*n.x)*depth;
float caustic=exp(-17.*abs(sin(15.*y+4.*terrain(8.*refracted)-2.*t)));
water+=vec3(.09,.11,.075)*caustic*depth*(1.-depth);
vec2 flow=vec2(p.x,d);
flow+=.076*vec2(terrain(12.*p+.27*t),terrain(8.*p-.22*t));
float cluster=terrain(vec2(7.,19.)*flow+vec2(.07*t,0.));
float cells=bubbleWalls(flow*vec2(20.,31.)+1.8*terrain(18.*flow));
float fine=bubbleWalls(flow*69.+.7*t);
float lip=exp(-abs(d-.024)/.055);
float foamArea=exp(-4.*abs(d-.15))*smoothstep(.34,.67,cluster);
float thickness=.039+.24*foamArea+.39*lip;
float foam=(1.-smoothstep(thickness,thickness+.12,cells))*(.90*lip+.82*foamArea);
foam+=.41*foamArea*(1.-smoothstep(.07,.19,fine));
foam+=.40*lip*smoothstep(.33,.66,terrain(87.*p-.4*t));
water*=1.-.11*smoothstep(.13,.49,foam)*(1.-smoothstep(.56,.91,foam));
// Small islands of stranded froth persist on the draining film.
float retreat=smoothstep(-.13,.36,sin(phase));
float residual=exp(-abs(d+.050)/.064)*retreat*
smoothstep(.52,.70,cluster)*(1.-smoothstep(.045,.16,cells));
sand=mix(sand,vec3(.82,.84,.74),.55*residual);
foam*=1.-.13*retreat*(1.-foamArea);
vec3 foamCol=mix(vec3(.69,.80,.76),vec3(.98,.97,.89),.33+.6*terrain(70.*p));
water=mix(water,foamCol,clamp(foam,0.,1.));
float aa=max(.002,fwidth(d));
sand*=.87+.13*smoothstep(0.,.018,abs(d+.006));
fragColor=vec4(mix(sand,water,smoothstep(-aa,aa,d)),1.);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Shoreline Swash & Foam — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/08_shoreline_swash_and_retreat.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-liquids-lib-08-shoreline-swash-and-retreat
// Swash over shell sand, from the high-water stain to the next incoming bore.
// uResolution: framebuffer dimensions in pixels. Optional uTime: seconds (default 0).
const float PI=3.14159265;
vec2 frame() {
return (2.*pfx_frag_xy-uResolution)/min(uResolution.x,uResolution.y);
}
float hash(vec2 p) {
vec3 q=fract(p.xyx*vec3(.1031,.1030,.0973));
q+=dot(q,q.yzx+33.33);
return fract((q.x+q.y)*q.z);
}
float noise2(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 terrain(vec2 p) {
mat2 m=mat2(1.62,1.19,-1.19,1.62);
return .54*noise2(p)+.27*noise2(m*p+7.1)+.13*noise2(m*m*p+19.)+
.06*noise2(m*m*m*p-8.3);
}
float coast(float x) {
return -.22*x+.13*sin(2.9*x+.7)+.055*sin(7.2*x-1.3);
}
float excursion(float phase) {
return -.34*cos(phase)-.085*cos(2.*phase+.35);
}
float grains(vec2 p) {
mat2 grainAxes=mat2(.84,.54,-.54,.84);
return .45*noise2(118.*grainAxes*p)+.35*noise2(201.*transpose(grainAxes)*p)+.20*terrain(12.*p);
}
vec3 beach(vec2 p) {
float mineral=grains(p), pockets=smoothstep(.71,.90,noise2(130.*mat2(.77,.64,-.64,.77)*p));
vec3 sand=mix(vec3(.61,.47,.32),vec3(.93,.79,.58),mineral);
sand-=vec3(.16,.14,.10)*pockets;
vec2 cell=floor(11.*p),local=fract(11.*p)-.5;
vec2 center=.57*(vec2(hash(cell+7.),hash(cell+11.))-.5);
vec2 q=local-center; q.x+=.45*q.y;
float peb=length(q/vec2(.14,.085))/(.25+hash(cell+5.));
sand*=.86+.14*smoothstep(.8,1.5,peb);
sand=mix(sand,mix(vec3(.42,.36,.27),vec3(.91,.86,.71),hash(cell)),
(1.-smoothstep(.72,1.,peb))*smoothstep(.67,.85,hash(cell+18.)));
return sand;
}
vec3 shell(vec2 p,vec3 sand) {
vec2 q=mat2(.76,.65,-.65,.76)*(p-vec2(.51,-.49));
float rib=atan(q.x,q.y+.060);
float radial=length(q/vec2(.072,.062))+.045*cos(15.*rib);
float shadow=length((q-vec2(.007,-.006))/vec2(.076,.066));
sand*=.68+.32*smoothstep(.76,1.18,shadow);
float light=.84+.11*cos(15.*rib)+.08*q.y/.07;
vec3 nacre=mix(vec3(.67,.45,.33),vec3(.94,.81,.65),.52+.33*sin(12.*rib+1.));
return mix(sand,nacre*light,1.-smoothstep(.95,1.04,radial));
}
float bubbleWalls(vec2 p) {
vec2 cell=floor(p),f=fract(p);
float d1=5.,d2=5.;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id=vec2(i,j),q=id+.13+.74*vec2(hash(cell+id),hash(cell+id+19.))-f;
float d=dot(q,q);
if(d<d1) { d2=d1; d1=d; } else d2=min(d2,d);
}
return sqrt(d2)-sqrt(d1);
}
float freeSurface(vec2 p,float shoreY,float t) {
vec2 waves=p*vec2(2.3,5.2)+vec2(.16*t,-.62*t);
return .027*sin(11.*shoreY-1.8*t+1.7*terrain(3.*p))+
.031*terrain(waves)+.008*noise2(4.*waves);
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p=frame(); float t=uTime+4.25;
float phase=.84*t+.13*sin(2.6*p.x);
float y=p.y-coast(p.x);
float d=y-excursion(phase)+.058*(terrain(vec2(11.*p.x,4.*p.y))-.5);
float wet=smoothstep(-.55,-.40,y+.052*terrain(12.*p));
vec3 dry=shell(p,beach(p));
vec3 sand=dry*mix(vec3(1.),vec3(.70,.75,.76),wet);
float local=freeSurface(p,y,t);
vec2 e=vec2(.005,0.);
vec2 slope=(vec2(freeSurface(p+e,y+.002,t),freeSurface(p+e.yx,y+.005,t))-local)/.005;
float depth=smoothstep(-.02,1.38,d);
vec2 refracted=p-.08*slope*depth;
vec3 floorColor=shell(refracted,beach(refracted));
vec3 water=mix(floorColor*vec3(.66,.83,.79),vec3(.04,.39,.42),.23+.74*depth);
vec3 n=normalize(vec3(-slope,1.));
water+=vec3(.16,.20,.19)*smoothstep(.04,.65,n.y+.3*n.x)*depth;
float caustic=exp(-17.*abs(sin(15.*y+4.*terrain(8.*refracted)-2.*t)));
water+=vec3(.09,.11,.075)*caustic*depth*(1.-depth);
vec2 flow=vec2(p.x,d);
flow+=.076*vec2(terrain(12.*p+.27*t),terrain(8.*p-.22*t));
float cluster=terrain(vec2(7.,19.)*flow+vec2(.07*t,0.));
float cells=bubbleWalls(flow*vec2(20.,31.)+1.8*terrain(18.*flow));
float fine=bubbleWalls(flow*69.+.7*t);
float lip=exp(-abs(d-.024)/.055);
float foamArea=exp(-4.*abs(d-.15))*smoothstep(.34,.67,cluster);
float thickness=.039+.24*foamArea+.39*lip;
float foam=(1.-smoothstep(thickness,thickness+.12,cells))*(.90*lip+.82*foamArea);
foam+=.41*foamArea*(1.-smoothstep(.07,.19,fine));
foam+=.40*lip*smoothstep(.33,.66,terrain(87.*p-.4*t));
water*=1.-.11*smoothstep(.13,.49,foam)*(1.-smoothstep(.56,.91,foam));
// Small islands of stranded froth persist on the draining film.
float retreat=smoothstep(-.13,.36,sin(phase));
float residual=exp(-abs(d+.050)/.064)*retreat*
smoothstep(.52,.70,cluster)*(1.-smoothstep(.045,.16,cells));
sand=mix(sand,vec3(.82,.84,.74),.55*residual);
foam*=1.-.13*retreat*(1.-foamArea);
vec3 foamCol=mix(vec3(.69,.80,.76),vec3(.98,.97,.89),.33+.6*terrain(70.*p));
water=mix(water,foamCol,clamp(foam,0.,1.));
float aa=max(.002,fwidth(d));
sand*=.87+.13*smoothstep(0.,.018,abs(d+.006));
fragColor=vec4(mix(sand,water,smoothstep(-aa,aa,d)),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;
// Shoreline Swash & Foam — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/08_shoreline_swash_and_retreat.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-liquids-lib-08-shoreline-swash-and-retreat
// Swash over shell sand, from the high-water stain to the next incoming bore.
// uResolution: framebuffer dimensions in pixels. Optional uTime: seconds (default 0).
const float PI=3.14159265;
vec2 frame() {
return (2.*pfx_frag_xy-uResolution)/min(uResolution.x,uResolution.y);
}
float hash(vec2 p) {
vec3 q=fract(p.xyx*vec3(.1031,.1030,.0973));
q+=dot(q,q.yzx+33.33);
return fract((q.x+q.y)*q.z);
}
float noise2(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 terrain(vec2 p) {
mat2 m=mat2(1.62,1.19,-1.19,1.62);
return .54*noise2(p)+.27*noise2(m*p+7.1)+.13*noise2(m*m*p+19.)+
.06*noise2(m*m*m*p-8.3);
}
float coast(float x) {
return -.22*x+.13*sin(2.9*x+.7)+.055*sin(7.2*x-1.3);
}
float excursion(float phase) {
return -.34*cos(phase)-.085*cos(2.*phase+.35);
}
float grains(vec2 p) {
mat2 grainAxes=mat2(.84,.54,-.54,.84);
return .45*noise2(118.*grainAxes*p)+.35*noise2(201.*transpose(grainAxes)*p)+.20*terrain(12.*p);
}
vec3 beach(vec2 p) {
float mineral=grains(p), pockets=smoothstep(.71,.90,noise2(130.*mat2(.77,.64,-.64,.77)*p));
vec3 sand=mix(vec3(.61,.47,.32),vec3(.93,.79,.58),mineral);
sand-=vec3(.16,.14,.10)*pockets;
vec2 cell=floor(11.*p),local=fract(11.*p)-.5;
vec2 center=.57*(vec2(hash(cell+7.),hash(cell+11.))-.5);
vec2 q=local-center; q.x+=.45*q.y;
float peb=length(q/vec2(.14,.085))/(.25+hash(cell+5.));
sand*=.86+.14*smoothstep(.8,1.5,peb);
sand=mix(sand,mix(vec3(.42,.36,.27),vec3(.91,.86,.71),hash(cell)),
(1.-smoothstep(.72,1.,peb))*smoothstep(.67,.85,hash(cell+18.)));
return sand;
}
vec3 shell(vec2 p,vec3 sand) {
vec2 q=mat2(.76,.65,-.65,.76)*(p-vec2(.51,-.49));
float rib=atan(q.x,q.y+.060);
float radial=length(q/vec2(.072,.062))+.045*cos(15.*rib);
float shadow=length((q-vec2(.007,-.006))/vec2(.076,.066));
sand*=.68+.32*smoothstep(.76,1.18,shadow);
float light=.84+.11*cos(15.*rib)+.08*q.y/.07;
vec3 nacre=mix(vec3(.67,.45,.33),vec3(.94,.81,.65),.52+.33*sin(12.*rib+1.));
return mix(sand,nacre*light,1.-smoothstep(.95,1.04,radial));
}
float bubbleWalls(vec2 p) {
vec2 cell=floor(p),f=fract(p);
float d1=5.,d2=5.;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id=vec2(i,j),q=id+.13+.74*vec2(hash(cell+id),hash(cell+id+19.))-f;
float d=dot(q,q);
if(d<d1) { d2=d1; d1=d; } else d2=min(d2,d);
}
return sqrt(d2)-sqrt(d1);
}
float freeSurface(vec2 p,float shoreY,float t) {
vec2 waves=p*vec2(2.3,5.2)+vec2(.16*t,-.62*t);
return .027*sin(11.*shoreY-1.8*t+1.7*terrain(3.*p))+
.031*terrain(waves)+.008*noise2(4.*waves);
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p=frame(); float t=uTime+4.25;
float phase=.84*t+.13*sin(2.6*p.x);
float y=p.y-coast(p.x);
float d=y-excursion(phase)+.058*(terrain(vec2(11.*p.x,4.*p.y))-.5);
float wet=smoothstep(-.55,-.40,y+.052*terrain(12.*p));
vec3 dry=shell(p,beach(p));
vec3 sand=dry*mix(vec3(1.),vec3(.70,.75,.76),wet);
float local=freeSurface(p,y,t);
vec2 e=vec2(.005,0.);
vec2 slope=(vec2(freeSurface(p+e,y+.002,t),freeSurface(p+e.yx,y+.005,t))-local)/.005;
float depth=smoothstep(-.02,1.38,d);
vec2 refracted=p-.08*slope*depth;
vec3 floorColor=shell(refracted,beach(refracted));
vec3 water=mix(floorColor*vec3(.66,.83,.79),vec3(.04,.39,.42),.23+.74*depth);
vec3 n=normalize(vec3(-slope,1.));
water+=vec3(.16,.20,.19)*smoothstep(.04,.65,n.y+.3*n.x)*depth;
float caustic=exp(-17.*abs(sin(15.*y+4.*terrain(8.*refracted)-2.*t)));
water+=vec3(.09,.11,.075)*caustic*depth*(1.-depth);
vec2 flow=vec2(p.x,d);
flow+=.076*vec2(terrain(12.*p+.27*t),terrain(8.*p-.22*t));
float cluster=terrain(vec2(7.,19.)*flow+vec2(.07*t,0.));
float cells=bubbleWalls(flow*vec2(20.,31.)+1.8*terrain(18.*flow));
float fine=bubbleWalls(flow*69.+.7*t);
float lip=exp(-abs(d-.024)/.055);
float foamArea=exp(-4.*abs(d-.15))*smoothstep(.34,.67,cluster);
float thickness=.039+.24*foamArea+.39*lip;
float foam=(1.-smoothstep(thickness,thickness+.12,cells))*(.90*lip+.82*foamArea);
foam+=.41*foamArea*(1.-smoothstep(.07,.19,fine));
foam+=.40*lip*smoothstep(.33,.66,terrain(87.*p-.4*t));
water*=1.-.11*smoothstep(.13,.49,foam)*(1.-smoothstep(.56,.91,foam));
// Small islands of stranded froth persist on the draining film.
float retreat=smoothstep(-.13,.36,sin(phase));
float residual=exp(-abs(d+.050)/.064)*retreat*
smoothstep(.52,.70,cluster)*(1.-smoothstep(.045,.16,cells));
sand=mix(sand,vec3(.82,.84,.74),.55*residual);
foam*=1.-.13*retreat*(1.-foamArea);
vec3 foamCol=mix(vec3(.69,.80,.76),vec3(.98,.97,.89),.33+.6*terrain(70.*p));
water=mix(water,foamCol,clamp(foam,0.,1.));
float aa=max(.002,fwidth(d));
sand*=.87+.13*smoothstep(0.,.018,abs(d+.006));
fragColor=vec4(mix(sand,water,smoothstep(-aa,aa,d)),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;
}
// Shoreline Swash & Foam — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/08_shoreline_swash_and_retreat.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-liquids-lib-08-shoreline-swash-and-retreat
// Swash over shell sand, from the high-water stain to the next incoming bore.
// uResolution: framebuffer dimensions in pixels. Optional uTime: seconds (default 0).
precision highp float;
uniform vec2 uResolution;
uniform float uTime;
out vec4 fragColor;
const float PI=3.14159265;
vec2 frame() {
return (2.*gl_FragCoord.xy-uResolution)/min(uResolution.x,uResolution.y);
}
float hash(vec2 p) {
vec3 q=fract(p.xyx*vec3(.1031,.1030,.0973));
q+=dot(q,q.yzx+33.33);
return fract((q.x+q.y)*q.z);
}
float noise2(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 terrain(vec2 p) {
mat2 m=mat2(1.62,1.19,-1.19,1.62);
return .54*noise2(p)+.27*noise2(m*p+7.1)+.13*noise2(m*m*p+19.)+
.06*noise2(m*m*m*p-8.3);
}
float coast(float x) {
return -.22*x+.13*sin(2.9*x+.7)+.055*sin(7.2*x-1.3);
}
float excursion(float phase) {
return -.34*cos(phase)-.085*cos(2.*phase+.35);
}
float grains(vec2 p) {
mat2 grainAxes=mat2(.84,.54,-.54,.84);
return .45*noise2(118.*grainAxes*p)+.35*noise2(201.*transpose(grainAxes)*p)+.20*terrain(12.*p);
}
vec3 beach(vec2 p) {
float mineral=grains(p), pockets=smoothstep(.71,.90,noise2(130.*mat2(.77,.64,-.64,.77)*p));
vec3 sand=mix(vec3(.61,.47,.32),vec3(.93,.79,.58),mineral);
sand-=vec3(.16,.14,.10)*pockets;
vec2 cell=floor(11.*p),local=fract(11.*p)-.5;
vec2 center=.57*(vec2(hash(cell+7.),hash(cell+11.))-.5);
vec2 q=local-center; q.x+=.45*q.y;
float peb=length(q/vec2(.14,.085))/(.25+hash(cell+5.));
sand*=.86+.14*smoothstep(.8,1.5,peb);
sand=mix(sand,mix(vec3(.42,.36,.27),vec3(.91,.86,.71),hash(cell)),
(1.-smoothstep(.72,1.,peb))*smoothstep(.67,.85,hash(cell+18.)));
return sand;
}
vec3 shell(vec2 p,vec3 sand) {
vec2 q=mat2(.76,.65,-.65,.76)*(p-vec2(.51,-.49));
float rib=atan(q.x,q.y+.060);
float radial=length(q/vec2(.072,.062))+.045*cos(15.*rib);
float shadow=length((q-vec2(.007,-.006))/vec2(.076,.066));
sand*=.68+.32*smoothstep(.76,1.18,shadow);
float light=.84+.11*cos(15.*rib)+.08*q.y/.07;
vec3 nacre=mix(vec3(.67,.45,.33),vec3(.94,.81,.65),.52+.33*sin(12.*rib+1.));
return mix(sand,nacre*light,1.-smoothstep(.95,1.04,radial));
}
float bubbleWalls(vec2 p) {
vec2 cell=floor(p),f=fract(p);
float d1=5.,d2=5.;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id=vec2(i,j),q=id+.13+.74*vec2(hash(cell+id),hash(cell+id+19.))-f;
float d=dot(q,q);
if(d<d1) { d2=d1; d1=d; } else d2=min(d2,d);
}
return sqrt(d2)-sqrt(d1);
}
float freeSurface(vec2 p,float shoreY,float t) {
vec2 waves=p*vec2(2.3,5.2)+vec2(.16*t,-.62*t);
return .027*sin(11.*shoreY-1.8*t+1.7*terrain(3.*p))+
.031*terrain(waves)+.008*noise2(4.*waves);
}
void main() {
vec2 p=frame(); float t=uTime+4.25;
float phase=.84*t+.13*sin(2.6*p.x);
float y=p.y-coast(p.x);
float d=y-excursion(phase)+.058*(terrain(vec2(11.*p.x,4.*p.y))-.5);
float wet=smoothstep(-.55,-.40,y+.052*terrain(12.*p));
vec3 dry=shell(p,beach(p));
vec3 sand=dry*mix(vec3(1.),vec3(.70,.75,.76),wet);
float local=freeSurface(p,y,t);
vec2 e=vec2(.005,0.);
vec2 slope=(vec2(freeSurface(p+e,y+.002,t),freeSurface(p+e.yx,y+.005,t))-local)/.005;
float depth=smoothstep(-.02,1.38,d);
vec2 refracted=p-.08*slope*depth;
vec3 floorColor=shell(refracted,beach(refracted));
vec3 water=mix(floorColor*vec3(.66,.83,.79),vec3(.04,.39,.42),.23+.74*depth);
vec3 n=normalize(vec3(-slope,1.));
water+=vec3(.16,.20,.19)*smoothstep(.04,.65,n.y+.3*n.x)*depth;
float caustic=exp(-17.*abs(sin(15.*y+4.*terrain(8.*refracted)-2.*t)));
water+=vec3(.09,.11,.075)*caustic*depth*(1.-depth);
vec2 flow=vec2(p.x,d);
flow+=.076*vec2(terrain(12.*p+.27*t),terrain(8.*p-.22*t));
float cluster=terrain(vec2(7.,19.)*flow+vec2(.07*t,0.));
float cells=bubbleWalls(flow*vec2(20.,31.)+1.8*terrain(18.*flow));
float fine=bubbleWalls(flow*69.+.7*t);
float lip=exp(-abs(d-.024)/.055);
float foamArea=exp(-4.*abs(d-.15))*smoothstep(.34,.67,cluster);
float thickness=.039+.24*foamArea+.39*lip;
float foam=(1.-smoothstep(thickness,thickness+.12,cells))*(.90*lip+.82*foamArea);
foam+=.41*foamArea*(1.-smoothstep(.07,.19,fine));
foam+=.40*lip*smoothstep(.33,.66,terrain(87.*p-.4*t));
water*=1.-.11*smoothstep(.13,.49,foam)*(1.-smoothstep(.56,.91,foam));
// Small islands of stranded froth persist on the draining film.
float retreat=smoothstep(-.13,.36,sin(phase));
float residual=exp(-abs(d+.050)/.064)*retreat*
smoothstep(.52,.70,cluster)*(1.-smoothstep(.045,.16,cells));
sand=mix(sand,vec3(.82,.84,.74),.55*residual);
foam*=1.-.13*retreat*(1.-foamArea);
vec3 foamCol=mix(vec3(.69,.80,.76),vec3(.98,.97,.89),.33+.6*terrain(70.*p));
water=mix(water,foamCol,clamp(foam,0.,1.));
float aa=max(.002,fwidth(d));
sand*=.87+.13*smoothstep(0.,.018,abs(d+.006));
fragColor=vec4(mix(sand,water,smoothstep(-aa,aa,d)),1.);
}
`;
export function createShorelineSwashFoamMaterial(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 | shoreline-swash-foam |
|---|---|
| Category | Water & Ice Shaders · Dynamic Liquids |
| Upstream study | liquids/lib/08_shoreline_swash_and_retreat.glsl (commit c3a6f6c) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime |
| Source size | 123 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 water sprite sheets |
| License | MIT License + CC0 / MIT Helpers |
Procedural signed-distance and analytic surface shading
David Hoskins, hash
AnimatedFree-surface Rankine whirlpool depression with spiral capillary ridges and Fresnel sky reflection.
AnimatedMulti-directional Gerstner ocean wave spectrum with subsurface teal scatter and sun glitter.
AnimatedOverlapping circular capillary wave rings from stochastic raindrops hitting calm dark water.
AnimatedClear shallow stream refracting submerged riverbed stones with stationary standing wave crests.
AnimatedVoronoi-stretched white surf foam webbing floating over aerated turquoise seawater.
AnimatedPolyhedral soap-bubble cluster with Plateau liquid borders and thin-film rainbow interference.