AnimatedMulti-directional Gerstner ocean wave spectrum with subsurface teal scatter and sun glitter.
Water & Ice
whirlpool-vortex-funnel.glsl · 143 lines · GLSL ES 3.00 · MIT
#version 300 es
// Whirlpool Vortex Funnel (whirlpool-vortex-funnel.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/11_draining_vortex_funnel.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-11-draining-vortex-funnel
// Side view through a draining tank. An air core narrows into the outlet.
// 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);
}
vec3 studio(vec3 d) {
vec3 c=mix(vec3(.055,.093,.102),vec3(.48,.61,.62),smoothstep(-.76,.87,d.y));
float panel=smoothstep(-.8,-.76,d.x)*(1.-smoothstep(-.33,-.28,d.x))
*smoothstep(-.1,-.04,d.y)*(1.-smoothstep(.88,.93,d.y));
float strip=exp(-20.*abs(d.x-.68-.13*d.y))*smoothstep(-.55,.20,d.y);
return c+vec3(1.,.94,.79)*panel+vec3(.65,.81,.91)*strip;
}
const float waterline=.47;
float center(float y,float t) {
return .026*sin(3.4*y+.86*t)+.034*sin(1.72*t+2.8*y)*smoothstep(0.,1.,waterline-y);
}
float surface(vec2 p,float t) {
float r=length(p);
float depression=pow(.123/(r+.098),2.);
float a=atan(p.y,p.x+.00001);
return waterline-depression+.010*sin(3.*a+13.*r-1.8*t)*exp(-1.9*r)
+.003*sin(16.*p.x-10.*p.y-t);
}
float field(vec3 p,float t) {
return p.y-surface(vec2(p.x-center(p.y,t),p.z),t);
}
vec3 back(vec2 p) {
float window=exp(-3.8*abs(p.x+.40-.11*p.y));
float second=exp(-5.4*abs(p.x-.88+.20*p.y));
float frameLight=exp(-16.*abs(p.x+.68+.08*p.y));
return mix(vec3(.22,.30,.31),vec3(.87,.82,.65),.40+.34*window+.16*second+.07*p.y)
+vec3(.15,.16,.14)*frameLight;
}
vec3 tank(vec2 p,float t) {
float depth=waterline-p.y;
vec2 q=p+vec2(.018*sin(5.*p.y+t),.012*sin(4.*p.x-.6*t));
vec3 water=back(q)*exp(-vec3(.39,.15,.075)*clamp(depth+.36,.0,2.));
water+=vec3(.012,.057,.060);
// An etched column of graduations on the far wall supplies scale through the core.
float ruled=abs(fract((q.y+.038)*12.)-.5);
float tick=(1.-smoothstep(.013,.033,ruled))*smoothstep(-.94,-.93,q.x)
*(1.-smoothstep(-.88,-.87,q.x));
water*=1.-.48*tick;
float floorY=-.75+.033*p.x;
float bottom=(1.-smoothstep(floorY-.025,floorY+.025,p.y));
float metal=.50+.13*noise2(vec2(14.*p.x,165.*p.y))+.15*exp(-8.*abs(p.x+.33));
float hole=length(vec2((p.x-center(-.74,t))/.14,(p.y-floorY)/.027));
vec3 floorCol=mix(vec3(.70,.76,.70)*metal,vec3(.066,.096,.09),1.-smoothstep(.68,1.,hole));
water=mix(water,floorCol,bottom);
water=mix(water,vec3(.20,.29,.27),.5*(1.-smoothstep(.002,.015,abs(p.y-floorY))));
return water;
}
void main() {
vec2 p=frame(); float t=uTime+6.48;
float worldY=p.y,viewSlope=.20;
// Ray from the rear of the lens into the tank, sampled along the viewing axis.
// For each sample the rotational free surface partitions air from water.
float entry=10.,exitPoint=10.; bool previous=false;
for(int i=0;i<=98;i++) {
float z=1.22-.025*float(i);
vec3 pos=vec3(p.x,worldY+viewSlope*z,z);
bool air=field(pos,t)>0.;
if(i>0 && air!=previous) {
float left=z,right=z+.025;
for(int j=0;j<6;j++) {
float m=.5*(left+right);
bool mid=field(vec3(p.x,worldY+viewSlope*m,m),t)>0.;
if(mid==air) left=m; else right=m;
}
float crossing=.5*(left+right);
if(entry>5.) entry=crossing; else exitPoint=crossing;
// For a ray descending from above, the first crossing can be the
// nearly flat surface in front of the funnel. Prefer its curved wall.
if(exitPoint<5. && abs(crossing)<.68) break;
}
previous=air;
}
float frontLine=waterline-.21+.007*sin(5.*p.x+.58*t);
float backLine=waterline+.22+.005*sin(7.*p.x-.64*t);
vec3 col=mix(tank(p,t),back(p),smoothstep(backLine-.004,backLine+.004,p.y));
if(p.y>frontLine && p.y<backLine) {
float zz=clamp((waterline-p.y)/viewSlope,-1.2,1.2);
float rr=length(vec2(p.x,zz));
float ripple=sin(26.*rr+1.5*sin(2.2*p.x)-1.6*t);
col=mix(col,vec3(.65,.74,.70),.18+.095*ripple*exp(-.9*rr));
}
if(entry<5. && p.y>-.76) {
float z=entry;
if(exitPoint<5. && abs(entry)>.49) z=exitPoint;
vec3 point=vec3(p.x,worldY+viewSlope*z,z);
vec3 ray=normalize(vec3(0.,-.20,-1.));
vec2 e=vec2(.002,0.);
vec3 n=normalize(vec3(field(point+e.xyy,t)-field(point-e.xyy,t),
field(point+e.yxy,t)-field(point-e.yxy,t),
field(point+e.yyx,t)-field(point-e.yyx,t)));
if(dot(n,ray)>0.) n=-n;
vec3 ref=reflect(ray,n);
float fres=.04+.85*pow(1.-abs(dot(n,-ray)),3.);
vec2 xz=vec2(point.x-center(point.y,t),z);
float depth=clamp(waterline-point.y,0.,1.25);
vec3 lens=tank(p+.32*n.xy,t);
vec3 core=mix(lens*vec3(.81,.92,.95),studio(ref),.18+.64*fres);
float strip=exp(-10.*abs(ref.x-.52*ref.y+.37));
core+=vec3(.34,.38,.34)*strip;
float angle=atan(xz.y,xz.x+.00001);
float dyePhase=angle+5.*log(depth+.046)-1.06*t;
float ribbon=.5+.5*cos(dyePhase);
float dye=smoothstep(.962,.989,ribbon)*smoothstep(.02,.15,depth);
core=mix(core,vec3(.56,.17,.075),.65*dye);
float surfaceWeight=.32+.68*smoothstep(.009,.065,depth);
col=mix(col,core,surfaceWeight*smoothstep(-.76,-.73,point.y));
}
col+=vec3(.16,.20,.16)*exp(-abs(p.y-frontLine)/.006);
fragColor=vec4(col,1.);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// Whirlpool Vortex Funnel — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/11_draining_vortex_funnel.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-11-draining-vortex-funnel
// Side view through a draining tank. An air core narrows into the outlet.
// 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);
}
vec3 studio(vec3 d) {
vec3 c=mix(vec3(.055,.093,.102),vec3(.48,.61,.62),smoothstep(-.76,.87,d.y));
float panel=smoothstep(-.8,-.76,d.x)*(1.-smoothstep(-.33,-.28,d.x))
*smoothstep(-.1,-.04,d.y)*(1.-smoothstep(.88,.93,d.y));
float strip=exp(-20.*abs(d.x-.68-.13*d.y))*smoothstep(-.55,.20,d.y);
return c+vec3(1.,.94,.79)*panel+vec3(.65,.81,.91)*strip;
}
const float waterline=.47;
float center(float y,float t) {
return .026*sin(3.4*y+.86*t)+.034*sin(1.72*t+2.8*y)*smoothstep(0.,1.,waterline-y);
}
float surface(vec2 p,float t) {
float r=length(p);
float depression=pow(.123/(r+.098),2.);
float a=atan(p.y,p.x+.00001);
return waterline-depression+.010*sin(3.*a+13.*r-1.8*t)*exp(-1.9*r)
+.003*sin(16.*p.x-10.*p.y-t);
}
float field(vec3 p,float t) {
return p.y-surface(vec2(p.x-center(p.y,t),p.z),t);
}
vec3 back(vec2 p) {
float window=exp(-3.8*abs(p.x+.40-.11*p.y));
float second=exp(-5.4*abs(p.x-.88+.20*p.y));
float frameLight=exp(-16.*abs(p.x+.68+.08*p.y));
return mix(vec3(.22,.30,.31),vec3(.87,.82,.65),.40+.34*window+.16*second+.07*p.y)
+vec3(.15,.16,.14)*frameLight;
}
vec3 tank(vec2 p,float t) {
float depth=waterline-p.y;
vec2 q=p+vec2(.018*sin(5.*p.y+t),.012*sin(4.*p.x-.6*t));
vec3 water=back(q)*exp(-vec3(.39,.15,.075)*clamp(depth+.36,.0,2.));
water+=vec3(.012,.057,.060);
// An etched column of graduations on the far wall supplies scale through the core.
float ruled=abs(fract((q.y+.038)*12.)-.5);
float tick=(1.-smoothstep(.013,.033,ruled))*smoothstep(-.94,-.93,q.x)
*(1.-smoothstep(-.88,-.87,q.x));
water*=1.-.48*tick;
float floorY=-.75+.033*p.x;
float bottom=(1.-smoothstep(floorY-.025,floorY+.025,p.y));
float metal=.50+.13*noise2(vec2(14.*p.x,165.*p.y))+.15*exp(-8.*abs(p.x+.33));
float hole=length(vec2((p.x-center(-.74,t))/.14,(p.y-floorY)/.027));
vec3 floorCol=mix(vec3(.70,.76,.70)*metal,vec3(.066,.096,.09),1.-smoothstep(.68,1.,hole));
water=mix(water,floorCol,bottom);
water=mix(water,vec3(.20,.29,.27),.5*(1.-smoothstep(.002,.015,abs(p.y-floorY))));
return water;
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p=frame(); float t=uTime+6.48;
float worldY=p.y,viewSlope=.20;
// Ray from the rear of the lens into the tank, sampled along the viewing axis.
// For each sample the rotational free surface partitions air from water.
float entry=10.,exitPoint=10.; bool previous=false;
for(int i=0;i<=98;i++) {
float z=1.22-.025*float(i);
vec3 pos=vec3(p.x,worldY+viewSlope*z,z);
bool air=field(pos,t)>0.;
if(i>0 && air!=previous) {
float left=z,right=z+.025;
for(int j=0;j<6;j++) {
float m=.5*(left+right);
bool mid=field(vec3(p.x,worldY+viewSlope*m,m),t)>0.;
if(mid==air) left=m; else right=m;
}
float crossing=.5*(left+right);
if(entry>5.) entry=crossing; else exitPoint=crossing;
// For a ray descending from above, the first crossing can be the
// nearly flat surface in front of the funnel. Prefer its curved wall.
if(exitPoint<5. && abs(crossing)<.68) break;
}
previous=air;
}
float frontLine=waterline-.21+.007*sin(5.*p.x+.58*t);
float backLine=waterline+.22+.005*sin(7.*p.x-.64*t);
vec3 col=mix(tank(p,t),back(p),smoothstep(backLine-.004,backLine+.004,p.y));
if(p.y>frontLine && p.y<backLine) {
float zz=clamp((waterline-p.y)/viewSlope,-1.2,1.2);
float rr=length(vec2(p.x,zz));
float ripple=sin(26.*rr+1.5*sin(2.2*p.x)-1.6*t);
col=mix(col,vec3(.65,.74,.70),.18+.095*ripple*exp(-.9*rr));
}
if(entry<5. && p.y>-.76) {
float z=entry;
if(exitPoint<5. && abs(entry)>.49) z=exitPoint;
vec3 point=vec3(p.x,worldY+viewSlope*z,z);
vec3 ray=normalize(vec3(0.,-.20,-1.));
vec2 e=vec2(.002,0.);
vec3 n=normalize(vec3(field(point+e.xyy,t)-field(point-e.xyy,t),
field(point+e.yxy,t)-field(point-e.yxy,t),
field(point+e.yyx,t)-field(point-e.yyx,t)));
if(dot(n,ray)>0.) n=-n;
vec3 ref=reflect(ray,n);
float fres=.04+.85*pow(1.-abs(dot(n,-ray)),3.);
vec2 xz=vec2(point.x-center(point.y,t),z);
float depth=clamp(waterline-point.y,0.,1.25);
vec3 lens=tank(p+.32*n.xy,t);
vec3 core=mix(lens*vec3(.81,.92,.95),studio(ref),.18+.64*fres);
float strip=exp(-10.*abs(ref.x-.52*ref.y+.37));
core+=vec3(.34,.38,.34)*strip;
float angle=atan(xz.y,xz.x+.00001);
float dyePhase=angle+5.*log(depth+.046)-1.06*t;
float ribbon=.5+.5*cos(dyePhase);
float dye=smoothstep(.962,.989,ribbon)*smoothstep(.02,.15,depth);
core=mix(core,vec3(.56,.17,.075),.65*dye);
float surfaceWeight=.32+.68*smoothstep(.009,.065,depth);
col=mix(col,core,surfaceWeight*smoothstep(-.76,-.73,point.y));
}
col+=vec3(.16,.20,.16)*exp(-abs(p.y-frontLine)/.006);
fragColor=vec4(col,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;
// Whirlpool Vortex Funnel — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/11_draining_vortex_funnel.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-11-draining-vortex-funnel
// Side view through a draining tank. An air core narrows into the outlet.
// 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);
}
vec3 studio(vec3 d) {
vec3 c=mix(vec3(.055,.093,.102),vec3(.48,.61,.62),smoothstep(-.76,.87,d.y));
float panel=smoothstep(-.8,-.76,d.x)*(1.-smoothstep(-.33,-.28,d.x))
*smoothstep(-.1,-.04,d.y)*(1.-smoothstep(.88,.93,d.y));
float strip=exp(-20.*abs(d.x-.68-.13*d.y))*smoothstep(-.55,.20,d.y);
return c+vec3(1.,.94,.79)*panel+vec3(.65,.81,.91)*strip;
}
const float waterline=.47;
float center(float y,float t) {
return .026*sin(3.4*y+.86*t)+.034*sin(1.72*t+2.8*y)*smoothstep(0.,1.,waterline-y);
}
float surface(vec2 p,float t) {
float r=length(p);
float depression=pow(.123/(r+.098),2.);
float a=atan(p.y,p.x+.00001);
return waterline-depression+.010*sin(3.*a+13.*r-1.8*t)*exp(-1.9*r)
+.003*sin(16.*p.x-10.*p.y-t);
}
float field(vec3 p,float t) {
return p.y-surface(vec2(p.x-center(p.y,t),p.z),t);
}
vec3 back(vec2 p) {
float window=exp(-3.8*abs(p.x+.40-.11*p.y));
float second=exp(-5.4*abs(p.x-.88+.20*p.y));
float frameLight=exp(-16.*abs(p.x+.68+.08*p.y));
return mix(vec3(.22,.30,.31),vec3(.87,.82,.65),.40+.34*window+.16*second+.07*p.y)
+vec3(.15,.16,.14)*frameLight;
}
vec3 tank(vec2 p,float t) {
float depth=waterline-p.y;
vec2 q=p+vec2(.018*sin(5.*p.y+t),.012*sin(4.*p.x-.6*t));
vec3 water=back(q)*exp(-vec3(.39,.15,.075)*clamp(depth+.36,.0,2.));
water+=vec3(.012,.057,.060);
// An etched column of graduations on the far wall supplies scale through the core.
float ruled=abs(fract((q.y+.038)*12.)-.5);
float tick=(1.-smoothstep(.013,.033,ruled))*smoothstep(-.94,-.93,q.x)
*(1.-smoothstep(-.88,-.87,q.x));
water*=1.-.48*tick;
float floorY=-.75+.033*p.x;
float bottom=(1.-smoothstep(floorY-.025,floorY+.025,p.y));
float metal=.50+.13*noise2(vec2(14.*p.x,165.*p.y))+.15*exp(-8.*abs(p.x+.33));
float hole=length(vec2((p.x-center(-.74,t))/.14,(p.y-floorY)/.027));
vec3 floorCol=mix(vec3(.70,.76,.70)*metal,vec3(.066,.096,.09),1.-smoothstep(.68,1.,hole));
water=mix(water,floorCol,bottom);
water=mix(water,vec3(.20,.29,.27),.5*(1.-smoothstep(.002,.015,abs(p.y-floorY))));
return water;
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 p=frame(); float t=uTime+6.48;
float worldY=p.y,viewSlope=.20;
// Ray from the rear of the lens into the tank, sampled along the viewing axis.
// For each sample the rotational free surface partitions air from water.
float entry=10.,exitPoint=10.; bool previous=false;
for(int i=0;i<=98;i++) {
float z=1.22-.025*float(i);
vec3 pos=vec3(p.x,worldY+viewSlope*z,z);
bool air=field(pos,t)>0.;
if(i>0 && air!=previous) {
float left=z,right=z+.025;
for(int j=0;j<6;j++) {
float m=.5*(left+right);
bool mid=field(vec3(p.x,worldY+viewSlope*m,m),t)>0.;
if(mid==air) left=m; else right=m;
}
float crossing=.5*(left+right);
if(entry>5.) entry=crossing; else exitPoint=crossing;
// For a ray descending from above, the first crossing can be the
// nearly flat surface in front of the funnel. Prefer its curved wall.
if(exitPoint<5. && abs(crossing)<.68) break;
}
previous=air;
}
float frontLine=waterline-.21+.007*sin(5.*p.x+.58*t);
float backLine=waterline+.22+.005*sin(7.*p.x-.64*t);
vec3 col=mix(tank(p,t),back(p),smoothstep(backLine-.004,backLine+.004,p.y));
if(p.y>frontLine && p.y<backLine) {
float zz=clamp((waterline-p.y)/viewSlope,-1.2,1.2);
float rr=length(vec2(p.x,zz));
float ripple=sin(26.*rr+1.5*sin(2.2*p.x)-1.6*t);
col=mix(col,vec3(.65,.74,.70),.18+.095*ripple*exp(-.9*rr));
}
if(entry<5. && p.y>-.76) {
float z=entry;
if(exitPoint<5. && abs(entry)>.49) z=exitPoint;
vec3 point=vec3(p.x,worldY+viewSlope*z,z);
vec3 ray=normalize(vec3(0.,-.20,-1.));
vec2 e=vec2(.002,0.);
vec3 n=normalize(vec3(field(point+e.xyy,t)-field(point-e.xyy,t),
field(point+e.yxy,t)-field(point-e.yxy,t),
field(point+e.yyx,t)-field(point-e.yyx,t)));
if(dot(n,ray)>0.) n=-n;
vec3 ref=reflect(ray,n);
float fres=.04+.85*pow(1.-abs(dot(n,-ray)),3.);
vec2 xz=vec2(point.x-center(point.y,t),z);
float depth=clamp(waterline-point.y,0.,1.25);
vec3 lens=tank(p+.32*n.xy,t);
vec3 core=mix(lens*vec3(.81,.92,.95),studio(ref),.18+.64*fres);
float strip=exp(-10.*abs(ref.x-.52*ref.y+.37));
core+=vec3(.34,.38,.34)*strip;
float angle=atan(xz.y,xz.x+.00001);
float dyePhase=angle+5.*log(depth+.046)-1.06*t;
float ribbon=.5+.5*cos(dyePhase);
float dye=smoothstep(.962,.989,ribbon)*smoothstep(.02,.15,depth);
core=mix(core,vec3(.56,.17,.075),.65*dye);
float surfaceWeight=.32+.68*smoothstep(.009,.065,depth);
col=mix(col,core,surfaceWeight*smoothstep(-.76,-.73,point.y));
}
col+=vec3(.16,.20,.16)*exp(-abs(p.y-frontLine)/.006);
fragColor=vec4(col,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;
}
// Whirlpool Vortex Funnel — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/11_draining_vortex_funnel.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-11-draining-vortex-funnel
// Side view through a draining tank. An air core narrows into the outlet.
// 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);
}
vec3 studio(vec3 d) {
vec3 c=mix(vec3(.055,.093,.102),vec3(.48,.61,.62),smoothstep(-.76,.87,d.y));
float panel=smoothstep(-.8,-.76,d.x)*(1.-smoothstep(-.33,-.28,d.x))
*smoothstep(-.1,-.04,d.y)*(1.-smoothstep(.88,.93,d.y));
float strip=exp(-20.*abs(d.x-.68-.13*d.y))*smoothstep(-.55,.20,d.y);
return c+vec3(1.,.94,.79)*panel+vec3(.65,.81,.91)*strip;
}
const float waterline=.47;
float center(float y,float t) {
return .026*sin(3.4*y+.86*t)+.034*sin(1.72*t+2.8*y)*smoothstep(0.,1.,waterline-y);
}
float surface(vec2 p,float t) {
float r=length(p);
float depression=pow(.123/(r+.098),2.);
float a=atan(p.y,p.x+.00001);
return waterline-depression+.010*sin(3.*a+13.*r-1.8*t)*exp(-1.9*r)
+.003*sin(16.*p.x-10.*p.y-t);
}
float field(vec3 p,float t) {
return p.y-surface(vec2(p.x-center(p.y,t),p.z),t);
}
vec3 back(vec2 p) {
float window=exp(-3.8*abs(p.x+.40-.11*p.y));
float second=exp(-5.4*abs(p.x-.88+.20*p.y));
float frameLight=exp(-16.*abs(p.x+.68+.08*p.y));
return mix(vec3(.22,.30,.31),vec3(.87,.82,.65),.40+.34*window+.16*second+.07*p.y)
+vec3(.15,.16,.14)*frameLight;
}
vec3 tank(vec2 p,float t) {
float depth=waterline-p.y;
vec2 q=p+vec2(.018*sin(5.*p.y+t),.012*sin(4.*p.x-.6*t));
vec3 water=back(q)*exp(-vec3(.39,.15,.075)*clamp(depth+.36,.0,2.));
water+=vec3(.012,.057,.060);
// An etched column of graduations on the far wall supplies scale through the core.
float ruled=abs(fract((q.y+.038)*12.)-.5);
float tick=(1.-smoothstep(.013,.033,ruled))*smoothstep(-.94,-.93,q.x)
*(1.-smoothstep(-.88,-.87,q.x));
water*=1.-.48*tick;
float floorY=-.75+.033*p.x;
float bottom=(1.-smoothstep(floorY-.025,floorY+.025,p.y));
float metal=.50+.13*noise2(vec2(14.*p.x,165.*p.y))+.15*exp(-8.*abs(p.x+.33));
float hole=length(vec2((p.x-center(-.74,t))/.14,(p.y-floorY)/.027));
vec3 floorCol=mix(vec3(.70,.76,.70)*metal,vec3(.066,.096,.09),1.-smoothstep(.68,1.,hole));
water=mix(water,floorCol,bottom);
water=mix(water,vec3(.20,.29,.27),.5*(1.-smoothstep(.002,.015,abs(p.y-floorY))));
return water;
}
void main() {
vec2 p=frame(); float t=uTime+6.48;
float worldY=p.y,viewSlope=.20;
// Ray from the rear of the lens into the tank, sampled along the viewing axis.
// For each sample the rotational free surface partitions air from water.
float entry=10.,exitPoint=10.; bool previous=false;
for(int i=0;i<=98;i++) {
float z=1.22-.025*float(i);
vec3 pos=vec3(p.x,worldY+viewSlope*z,z);
bool air=field(pos,t)>0.;
if(i>0 && air!=previous) {
float left=z,right=z+.025;
for(int j=0;j<6;j++) {
float m=.5*(left+right);
bool mid=field(vec3(p.x,worldY+viewSlope*m,m),t)>0.;
if(mid==air) left=m; else right=m;
}
float crossing=.5*(left+right);
if(entry>5.) entry=crossing; else exitPoint=crossing;
// For a ray descending from above, the first crossing can be the
// nearly flat surface in front of the funnel. Prefer its curved wall.
if(exitPoint<5. && abs(crossing)<.68) break;
}
previous=air;
}
float frontLine=waterline-.21+.007*sin(5.*p.x+.58*t);
float backLine=waterline+.22+.005*sin(7.*p.x-.64*t);
vec3 col=mix(tank(p,t),back(p),smoothstep(backLine-.004,backLine+.004,p.y));
if(p.y>frontLine && p.y<backLine) {
float zz=clamp((waterline-p.y)/viewSlope,-1.2,1.2);
float rr=length(vec2(p.x,zz));
float ripple=sin(26.*rr+1.5*sin(2.2*p.x)-1.6*t);
col=mix(col,vec3(.65,.74,.70),.18+.095*ripple*exp(-.9*rr));
}
if(entry<5. && p.y>-.76) {
float z=entry;
if(exitPoint<5. && abs(entry)>.49) z=exitPoint;
vec3 point=vec3(p.x,worldY+viewSlope*z,z);
vec3 ray=normalize(vec3(0.,-.20,-1.));
vec2 e=vec2(.002,0.);
vec3 n=normalize(vec3(field(point+e.xyy,t)-field(point-e.xyy,t),
field(point+e.yxy,t)-field(point-e.yxy,t),
field(point+e.yyx,t)-field(point-e.yyx,t)));
if(dot(n,ray)>0.) n=-n;
vec3 ref=reflect(ray,n);
float fres=.04+.85*pow(1.-abs(dot(n,-ray)),3.);
vec2 xz=vec2(point.x-center(point.y,t),z);
float depth=clamp(waterline-point.y,0.,1.25);
vec3 lens=tank(p+.32*n.xy,t);
vec3 core=mix(lens*vec3(.81,.92,.95),studio(ref),.18+.64*fres);
float strip=exp(-10.*abs(ref.x-.52*ref.y+.37));
core+=vec3(.34,.38,.34)*strip;
float angle=atan(xz.y,xz.x+.00001);
float dyePhase=angle+5.*log(depth+.046)-1.06*t;
float ribbon=.5+.5*cos(dyePhase);
float dye=smoothstep(.962,.989,ribbon)*smoothstep(.02,.15,depth);
core=mix(core,vec3(.56,.17,.075),.65*dye);
float surfaceWeight=.32+.68*smoothstep(.009,.065,depth);
col=mix(col,core,surfaceWeight*smoothstep(-.76,-.73,point.y));
}
col+=vec3(.16,.20,.16)*exp(-abs(p.y-frontLine)/.006);
fragColor=vec4(col,1.);
}
`;
export function createWhirlpoolVortexFunnelMaterial(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 | whirlpool-vortex-funnel |
|---|---|
| Category | Water & Ice Shaders · Dynamic Liquids |
| Upstream study | liquids/lib/11_draining_vortex_funnel.glsl (commit c3a6f6c) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime |
| Source size | 143 lines · 6.1 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
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.
PBR StudyClear refractive ice stalactites with characteristic centimetre-scale horizontal growth ribs.