Shoreline Swash & Foam shader

shoreline-swash-foam.glsl · 123 lines · GLSL ES 3.00 · MIT

Shoreline Swash & FoamWater & Ice
Shoreline Swash & Foam WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 123 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

Dynamic Liquids
shoreline-swash-foam.glsl (123 lines)
#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.);
}

Mathematical models & shader specifications

Identifiershoreline-swash-foam
CategoryWater & Ice Shaders · Dynamic Liquids
Upstream studyliquids/lib/08_shoreline_swash_and_retreat.glsl (commit c3a6f6c)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime
Source size123 lines · 5.2 KB standalone GLSL ES 3.00
PNG baker outputs512px · 1024px · 2048px RGBA8 (4-tap seamless + alpha key)
Companion pixel VFXFree MIT water 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 water & ice shaders