AnimatedVoronoi-stretched white surf foam webbing floating over aerated turquoise seawater.
Water & Ice
river-riffle-over-cobbles.glsl · 143 lines · GLSL ES 3.00 · MIT
#version 300 es
// River Riffle Over Cobbles (river-riffle-over-cobbles.glsl)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/15_river_riffle_over_cobbles.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-15-river-riffle-over-cobbles
// Shallow riffle across a mixed gravel bar with three water-breaking cobbles.
// uResolution is the viewport size in pixels. Optional uTime is elapsed seconds (default 0).
precision highp float;
uniform vec2 uResolution;
uniform float uTime;
out vec4 fragColor;
float hash(vec2 p) {
vec3 q = fract(p.xyx * vec3(.1031,.11369,.13787));
q += dot(q,q.yzx+19.19);
return fract((q.x+q.y)*q.z);
}
float noise2(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f*f*(3.0-2.0*f);
return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
mix(hash(i+vec2(0,1)),hash(i+1.0),f.x),f.y);
}
vec2 sceneCoord() {
return (2.0*gl_FragCoord.xy-uResolution)/max(1.0,min(uResolution.x,uResolution.y));
}
float fbm(vec2 p) {
mat2 twist = mat2(1.68,1.20,-1.20,1.68);
float sum = 0.0, amplitude = .53;
for(int i=0;i<4;i++) {
sum += amplitude*noise2(p);
p = twist*p+vec2(3.2,7.1);
amplitude *= .5;
}
return sum;
}
float sq(float x) { return x*x; }
mat2 rotate2(float a) {
return mat2(cos(a),sin(a),-sin(a),cos(a));
}
const vec3 sun = vec3(-.53,.42,.737);
struct Hit { float t; vec3 n; vec3 local; float seed; };
void stone(inout Hit hit,vec3 ro,vec3 rd,vec3 center,vec3 axes,float spin,float seed) {
mat2 turn = rotate2(spin);
vec3 oc = ro-center;
oc.xy = turn*oc.xy;
vec3 ray = vec3(turn*rd.xy,rd.z)/axes;
oc /= axes;
float a = dot(ray,ray), b = dot(oc,ray);
float disc = b*b-a*(dot(oc,oc)-1.0);
if(disc<=0.0) return;
float t = (-b-sqrt(disc))/a;
if(t<=0.0 || t>=hit.t) return;
hit.t = t;
hit.local = oc+ray*t;
vec3 normal = hit.local/axes;
hit.n = normalize(vec3(transpose(turn)*normal.xy,normal.z));
hit.seed = seed;
}
vec3 hero(int i) {
if(i==0) return vec3(-.37,.28,-.145);
if(i==1) return vec3(.69,.76,-.17);
return vec3(-.89,-.64,-.18);
}
void outcrops(inout Hit hit,vec3 ro,vec3 rd) {
for(int i=0;i<3;i++) {
float k = float(i);
stone(hit,ro,rd,hero(i),vec3(.33+.035*k,.25,.217),.63+.74*k,.21+.28*k);
}
}
vec3 mineral(Hit h) {
vec2 uv = h.local.xy+.49*h.local.z;
float fleck = fbm(uv*16.0+h.seed*27.0);
float fissure = abs(sin(6.7*uv.x+2.0*uv.y+4.2*fbm(uv*3.0+h.seed)));
vec3 c = mix(vec3(.24,.235,.207),vec3(.56,.42,.28),h.seed);
c *= .61+.76*fleck;
c += vec3(.19,.17,.13)*exp(-sq(fissure/.13))*smoothstep(.20,.62,fleck);
return c*(.53+.61*max(dot(sun,h.n),0.0));
}
float waveHeight(vec2 p) {
float flow = p.y+uTime*.55;
float h = .008*sin(flow*19.0+3.2*noise2(p*vec2(3,2)))
+.0045*sin(flow*32.0+p.x*10.0+1.3*sin(7.0*p.x));
for(int i=0;i<3;i++) {
vec2 d = p-hero(i).xy;
float flank = sqrt(d.x*d.x+.008);
float bow = d.y+.66*flank-.24;
h += .029*exp(-sq(bow/.095)-sq(flank/.46));
float wake = .5*flank+d.y;
h += .014*sin(43.0*wake+.6*sin(2.1*uTime))*exp(-sq(wake/.38)-sq(flank/.49));
}
return h;
}
void main() {
vec2 uv = sceneCoord();
vec3 ro = vec3(0,-1.46,1.67);
vec3 rd = normalize(vec3(.66*uv.x,.90+.59*uv.y,-1.0+.24*uv.y));
vec3 surface = ro-rd*ro.z/rd.z;
surface += rd*(waveHeight(surface.xy)/rd.z);
float e = .0028;
vec2 p = surface.xy;
vec3 n = normalize(vec3(waveHeight(p-vec2(e,0))-waveHeight(p+vec2(e,0)),
waveHeight(p-vec2(0,e))-waveHeight(p+vec2(0,e)),2.0*e));
vec3 bend = refract(rd,n,1.0/1.333);
vec2 floorPoint = p-bend.xy*(.34+surface.z)/bend.z;
Hit h = Hit(10.0,vec3(0,0,1),vec3(0),.5);
for(int level=0;level<2;level++) {
float size = level==0 ? .34 : .111;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id = floor(floorPoint/size)+vec2(i,j);
float s = hash(id+4.7);
vec2 c = size*(id+.5+.73*(vec2(hash(id+2.3),hash(id+.6))-.5));
vec3 axis = size*vec3(.31+.31*s,.24+.26*hash(id+3.3),.26+.3*s);
stone(h,surface,bend,vec3(c,-.36+.025*s),axis,6.28*s,s);
}
}
outcrops(h,surface,bend);
vec3 gravel = h.t<9.0 ? mineral(h) : vec3(.18,.17,.12)*(.55+fbm(floorPoint*94.0));
float focus = sin(19.0*p.y+22.0*n.y+5.0*p.x);
gravel *= .93+.15*exp(-sq(focus/.2));
vec3 reflection = reflect(rd,n);
float bank = smoothstep(.25,.64,fbm(reflection.xy*5.0)+.56*reflection.z-.23);
vec3 sky = mix(vec3(.11,.21,.17),vec3(.75,.85,.91),bank);
float fresnel = .036+.96*pow(1.0-max(0.0,dot(-rd,n)),4.2);
// Shorter paths near the exposed stones retain the warm mineral color.
vec3 transmission = exp(-min(h.t,.8)*vec3(.52,.21,.14));
vec3 submerged = mix(vec3(.12,.24,.23),gravel,transmission);
vec3 water = mix(submerged,sky,clamp(.073+fresnel*1.65,0.0,.93));
water += vec3(1.0,.95,.81)*pow(max(dot(reflection,sun),0.0),110.0);
Hit air = Hit(length(surface-ro),vec3(0),vec3(0),0.0);
outcrops(air,ro,rd);
if(air.t<length(surface-ro)) {
vec3 rock = mineral(air);
vec3 hitPoint = ro+rd*air.t;
float z = hitPoint.z-waveHeight(hitPoint.xy);
rock *= .84+.19*smoothstep(.004,.047,z);
rock += .045*fbm(hitPoint.xy*115.0);
rock += .29*pow(max(dot(reflect(rd,air.n),sun),0.0),39.0);
water = mix(water,rock,smoothstep(-.008,.003,z));
}
fragColor = vec4(water,1);
}
shader_type canvas_item;
// Attach to a ColorRect, Sprite2D, or SubViewport texture baker in Godot 4.2+.
// River Riffle Over Cobbles — Godot 4 (canvas_item) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/15_river_riffle_over_cobbles.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-15-river-riffle-over-cobbles
// Shallow riffle across a mixed gravel bar with three water-breaking cobbles.
// uResolution is the viewport size in pixels. Optional uTime is elapsed seconds (default 0).
float hash(vec2 p) {
vec3 q = fract(p.xyx * vec3(.1031,.11369,.13787));
q += dot(q,q.yzx+19.19);
return fract((q.x+q.y)*q.z);
}
float noise2(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f*f*(3.0-2.0*f);
return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
mix(hash(i+vec2(0,1)),hash(i+1.0),f.x),f.y);
}
vec2 sceneCoord() {
return (2.0*pfx_frag_xy-uResolution)/max(1.0,min(uResolution.x,uResolution.y));
}
float fbm(vec2 p) {
mat2 twist = mat2(1.68,1.20,-1.20,1.68);
float sum = 0.0, amplitude = .53;
for(int i=0;i<4;i++) {
sum += amplitude*noise2(p);
p = twist*p+vec2(3.2,7.1);
amplitude *= .5;
}
return sum;
}
float sq(float x) { return x*x; }
mat2 rotate2(float a) {
return mat2(cos(a),sin(a),-sin(a),cos(a));
}
const vec3 sun = vec3(-.53,.42,.737);
struct Hit { float t; vec3 n; vec3 local; float seed; };
void stone(inout Hit hit,vec3 ro,vec3 rd,vec3 center,vec3 axes,float spin,float seed) {
mat2 turn = rotate2(spin);
vec3 oc = ro-center;
oc.xy = turn*oc.xy;
vec3 ray = vec3(turn*rd.xy,rd.z)/axes;
oc /= axes;
float a = dot(ray,ray), b = dot(oc,ray);
float disc = b*b-a*(dot(oc,oc)-1.0);
if(disc<=0.0) return;
float t = (-b-sqrt(disc))/a;
if(t<=0.0 || t>=hit.t) return;
hit.t = t;
hit.local = oc+ray*t;
vec3 normal = hit.local/axes;
hit.n = normalize(vec3(transpose(turn)*normal.xy,normal.z));
hit.seed = seed;
}
vec3 hero(int i) {
if(i==0) return vec3(-.37,.28,-.145);
if(i==1) return vec3(.69,.76,-.17);
return vec3(-.89,-.64,-.18);
}
void outcrops(inout Hit hit,vec3 ro,vec3 rd) {
for(int i=0;i<3;i++) {
float k = float(i);
stone(hit,ro,rd,hero(i),vec3(.33+.035*k,.25,.217),.63+.74*k,.21+.28*k);
}
}
vec3 mineral(Hit h) {
vec2 uv = h.local.xy+.49*h.local.z;
float fleck = fbm(uv*16.0+h.seed*27.0);
float fissure = abs(sin(6.7*uv.x+2.0*uv.y+4.2*fbm(uv*3.0+h.seed)));
vec3 c = mix(vec3(.24,.235,.207),vec3(.56,.42,.28),h.seed);
c *= .61+.76*fleck;
c += vec3(.19,.17,.13)*exp(-sq(fissure/.13))*smoothstep(.20,.62,fleck);
return c*(.53+.61*max(dot(sun,h.n),0.0));
}
float waveHeight(vec2 p) {
float flow = p.y+uTime*.55;
float h = .008*sin(flow*19.0+3.2*noise2(p*vec2(3,2)))
+.0045*sin(flow*32.0+p.x*10.0+1.3*sin(7.0*p.x));
for(int i=0;i<3;i++) {
vec2 d = p-hero(i).xy;
float flank = sqrt(d.x*d.x+.008);
float bow = d.y+.66*flank-.24;
h += .029*exp(-sq(bow/.095)-sq(flank/.46));
float wake = .5*flank+d.y;
h += .014*sin(43.0*wake+.6*sin(2.1*uTime))*exp(-sq(wake/.38)-sq(flank/.49));
}
return h;
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = sceneCoord();
vec3 ro = vec3(0,-1.46,1.67);
vec3 rd = normalize(vec3(.66*uv.x,.90+.59*uv.y,-1.0+.24*uv.y));
vec3 surface = ro-rd*ro.z/rd.z;
surface += rd*(waveHeight(surface.xy)/rd.z);
float e = .0028;
vec2 p = surface.xy;
vec3 n = normalize(vec3(waveHeight(p-vec2(e,0))-waveHeight(p+vec2(e,0)),
waveHeight(p-vec2(0,e))-waveHeight(p+vec2(0,e)),2.0*e));
vec3 bend = refract(rd,n,1.0/1.333);
vec2 floorPoint = p-bend.xy*(.34+surface.z)/bend.z;
Hit h = Hit(10.0,vec3(0,0,1),vec3(0),.5);
for(int level=0;level<2;level++) {
float size = level==0 ? .34 : .111;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id = floor(floorPoint/size)+vec2(i,j);
float s = hash(id+4.7);
vec2 c = size*(id+.5+.73*(vec2(hash(id+2.3),hash(id+.6))-.5));
vec3 axis = size*vec3(.31+.31*s,.24+.26*hash(id+3.3),.26+.3*s);
stone(h,surface,bend,vec3(c,-.36+.025*s),axis,6.28*s,s);
}
}
outcrops(h,surface,bend);
vec3 gravel = h.t<9.0 ? mineral(h) : vec3(.18,.17,.12)*(.55+fbm(floorPoint*94.0));
float focus = sin(19.0*p.y+22.0*n.y+5.0*p.x);
gravel *= .93+.15*exp(-sq(focus/.2));
vec3 reflection = reflect(rd,n);
float bank = smoothstep(.25,.64,fbm(reflection.xy*5.0)+.56*reflection.z-.23);
vec3 sky = mix(vec3(.11,.21,.17),vec3(.75,.85,.91),bank);
float fresnel = .036+.96*pow(1.0-max(0.0,dot(-rd,n)),4.2);
// Shorter paths near the exposed stones retain the warm mineral color.
vec3 transmission = exp(-min(h.t,.8)*vec3(.52,.21,.14));
vec3 submerged = mix(vec3(.12,.24,.23),gravel,transmission);
vec3 water = mix(submerged,sky,clamp(.073+fresnel*1.65,0.0,.93));
water += vec3(1.0,.95,.81)*pow(max(dot(reflection,sun),0.0),110.0);
Hit air = Hit(length(surface-ro),vec3(0),vec3(0),0.0);
outcrops(air,ro,rd);
if(air.t<length(surface-ro)) {
vec3 rock = mineral(air);
vec3 hitPoint = ro+rd*air.t;
float z = hitPoint.z-waveHeight(hitPoint.xy);
rock *= .84+.19*smoothstep(.004,.047,z);
rock += .045*fbm(hitPoint.xy*115.0);
rock += .29*pow(max(dot(reflect(rd,air.n),sun),0.0),39.0);
water = mix(water,rock,smoothstep(-.008,.003,z));
}
fragColor = vec4(water,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;
// River Riffle Over Cobbles — Godot 4 (spatial) wrapper
// Source: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/15_river_riffle_over_cobbles.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-15-river-riffle-over-cobbles
// Shallow riffle across a mixed gravel bar with three water-breaking cobbles.
// uResolution is the viewport size in pixels. Optional uTime is elapsed seconds (default 0).
float hash(vec2 p) {
vec3 q = fract(p.xyx * vec3(.1031,.11369,.13787));
q += dot(q,q.yzx+19.19);
return fract((q.x+q.y)*q.z);
}
float noise2(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f*f*(3.0-2.0*f);
return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
mix(hash(i+vec2(0,1)),hash(i+1.0),f.x),f.y);
}
vec2 sceneCoord() {
return (2.0*pfx_frag_xy-uResolution)/max(1.0,min(uResolution.x,uResolution.y));
}
float fbm(vec2 p) {
mat2 twist = mat2(1.68,1.20,-1.20,1.68);
float sum = 0.0, amplitude = .53;
for(int i=0;i<4;i++) {
sum += amplitude*noise2(p);
p = twist*p+vec2(3.2,7.1);
amplitude *= .5;
}
return sum;
}
float sq(float x) { return x*x; }
mat2 rotate2(float a) {
return mat2(cos(a),sin(a),-sin(a),cos(a));
}
const vec3 sun = vec3(-.53,.42,.737);
struct Hit { float t; vec3 n; vec3 local; float seed; };
void stone(inout Hit hit,vec3 ro,vec3 rd,vec3 center,vec3 axes,float spin,float seed) {
mat2 turn = rotate2(spin);
vec3 oc = ro-center;
oc.xy = turn*oc.xy;
vec3 ray = vec3(turn*rd.xy,rd.z)/axes;
oc /= axes;
float a = dot(ray,ray), b = dot(oc,ray);
float disc = b*b-a*(dot(oc,oc)-1.0);
if(disc<=0.0) return;
float t = (-b-sqrt(disc))/a;
if(t<=0.0 || t>=hit.t) return;
hit.t = t;
hit.local = oc+ray*t;
vec3 normal = hit.local/axes;
hit.n = normalize(vec3(transpose(turn)*normal.xy,normal.z));
hit.seed = seed;
}
vec3 hero(int i) {
if(i==0) return vec3(-.37,.28,-.145);
if(i==1) return vec3(.69,.76,-.17);
return vec3(-.89,-.64,-.18);
}
void outcrops(inout Hit hit,vec3 ro,vec3 rd) {
for(int i=0;i<3;i++) {
float k = float(i);
stone(hit,ro,rd,hero(i),vec3(.33+.035*k,.25,.217),.63+.74*k,.21+.28*k);
}
}
vec3 mineral(Hit h) {
vec2 uv = h.local.xy+.49*h.local.z;
float fleck = fbm(uv*16.0+h.seed*27.0);
float fissure = abs(sin(6.7*uv.x+2.0*uv.y+4.2*fbm(uv*3.0+h.seed)));
vec3 c = mix(vec3(.24,.235,.207),vec3(.56,.42,.28),h.seed);
c *= .61+.76*fleck;
c += vec3(.19,.17,.13)*exp(-sq(fissure/.13))*smoothstep(.20,.62,fleck);
return c*(.53+.61*max(dot(sun,h.n),0.0));
}
float waveHeight(vec2 p) {
float flow = p.y+uTime*.55;
float h = .008*sin(flow*19.0+3.2*noise2(p*vec2(3,2)))
+.0045*sin(flow*32.0+p.x*10.0+1.3*sin(7.0*p.x));
for(int i=0;i<3;i++) {
vec2 d = p-hero(i).xy;
float flank = sqrt(d.x*d.x+.008);
float bow = d.y+.66*flank-.24;
h += .029*exp(-sq(bow/.095)-sq(flank/.46));
float wake = .5*flank+d.y;
h += .014*sin(43.0*wake+.6*sin(2.1*uTime))*exp(-sq(wake/.38)-sq(flank/.49));
}
return h;
}
void pixelfx_study_eval(vec2 pfx_frag_xy, out vec4 fragColor) {
vec2 uv = sceneCoord();
vec3 ro = vec3(0,-1.46,1.67);
vec3 rd = normalize(vec3(.66*uv.x,.90+.59*uv.y,-1.0+.24*uv.y));
vec3 surface = ro-rd*ro.z/rd.z;
surface += rd*(waveHeight(surface.xy)/rd.z);
float e = .0028;
vec2 p = surface.xy;
vec3 n = normalize(vec3(waveHeight(p-vec2(e,0))-waveHeight(p+vec2(e,0)),
waveHeight(p-vec2(0,e))-waveHeight(p+vec2(0,e)),2.0*e));
vec3 bend = refract(rd,n,1.0/1.333);
vec2 floorPoint = p-bend.xy*(.34+surface.z)/bend.z;
Hit h = Hit(10.0,vec3(0,0,1),vec3(0),.5);
for(int level=0;level<2;level++) {
float size = level==0 ? .34 : .111;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id = floor(floorPoint/size)+vec2(i,j);
float s = hash(id+4.7);
vec2 c = size*(id+.5+.73*(vec2(hash(id+2.3),hash(id+.6))-.5));
vec3 axis = size*vec3(.31+.31*s,.24+.26*hash(id+3.3),.26+.3*s);
stone(h,surface,bend,vec3(c,-.36+.025*s),axis,6.28*s,s);
}
}
outcrops(h,surface,bend);
vec3 gravel = h.t<9.0 ? mineral(h) : vec3(.18,.17,.12)*(.55+fbm(floorPoint*94.0));
float focus = sin(19.0*p.y+22.0*n.y+5.0*p.x);
gravel *= .93+.15*exp(-sq(focus/.2));
vec3 reflection = reflect(rd,n);
float bank = smoothstep(.25,.64,fbm(reflection.xy*5.0)+.56*reflection.z-.23);
vec3 sky = mix(vec3(.11,.21,.17),vec3(.75,.85,.91),bank);
float fresnel = .036+.96*pow(1.0-max(0.0,dot(-rd,n)),4.2);
// Shorter paths near the exposed stones retain the warm mineral color.
vec3 transmission = exp(-min(h.t,.8)*vec3(.52,.21,.14));
vec3 submerged = mix(vec3(.12,.24,.23),gravel,transmission);
vec3 water = mix(submerged,sky,clamp(.073+fresnel*1.65,0.0,.93));
water += vec3(1.0,.95,.81)*pow(max(dot(reflection,sun),0.0),110.0);
Hit air = Hit(length(surface-ro),vec3(0),vec3(0),0.0);
outcrops(air,ro,rd);
if(air.t<length(surface-ro)) {
vec3 rock = mineral(air);
vec3 hitPoint = ro+rd*air.t;
float z = hitPoint.z-waveHeight(hitPoint.xy);
rock *= .84+.19*smoothstep(.004,.047,z);
rock += .045*fbm(hitPoint.xy*115.0);
rock += .29*pow(max(dot(reflect(rd,air.n),sun),0.0),39.0);
water = mix(water,rock,smoothstep(-.008,.003,z));
}
fragColor = vec4(water,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;
}
// River Riffle Over Cobbles — Three.js WebGL2 ShaderMaterial (GLSL3)
// Upstream study: https://github.com/gokayfem/glsl-studies/blob/main/liquids/lib/15_river_riffle_over_cobbles.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-15-river-riffle-over-cobbles
// Shallow riffle across a mixed gravel bar with three water-breaking cobbles.
// uResolution is the viewport size in pixels. Optional uTime is elapsed seconds (default 0).
precision highp float;
uniform vec2 uResolution;
uniform float uTime;
out vec4 fragColor;
float hash(vec2 p) {
vec3 q = fract(p.xyx * vec3(.1031,.11369,.13787));
q += dot(q,q.yzx+19.19);
return fract((q.x+q.y)*q.z);
}
float noise2(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f*f*(3.0-2.0*f);
return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),
mix(hash(i+vec2(0,1)),hash(i+1.0),f.x),f.y);
}
vec2 sceneCoord() {
return (2.0*gl_FragCoord.xy-uResolution)/max(1.0,min(uResolution.x,uResolution.y));
}
float fbm(vec2 p) {
mat2 twist = mat2(1.68,1.20,-1.20,1.68);
float sum = 0.0, amplitude = .53;
for(int i=0;i<4;i++) {
sum += amplitude*noise2(p);
p = twist*p+vec2(3.2,7.1);
amplitude *= .5;
}
return sum;
}
float sq(float x) { return x*x; }
mat2 rotate2(float a) {
return mat2(cos(a),sin(a),-sin(a),cos(a));
}
const vec3 sun = vec3(-.53,.42,.737);
struct Hit { float t; vec3 n; vec3 local; float seed; };
void stone(inout Hit hit,vec3 ro,vec3 rd,vec3 center,vec3 axes,float spin,float seed) {
mat2 turn = rotate2(spin);
vec3 oc = ro-center;
oc.xy = turn*oc.xy;
vec3 ray = vec3(turn*rd.xy,rd.z)/axes;
oc /= axes;
float a = dot(ray,ray), b = dot(oc,ray);
float disc = b*b-a*(dot(oc,oc)-1.0);
if(disc<=0.0) return;
float t = (-b-sqrt(disc))/a;
if(t<=0.0 || t>=hit.t) return;
hit.t = t;
hit.local = oc+ray*t;
vec3 normal = hit.local/axes;
hit.n = normalize(vec3(transpose(turn)*normal.xy,normal.z));
hit.seed = seed;
}
vec3 hero(int i) {
if(i==0) return vec3(-.37,.28,-.145);
if(i==1) return vec3(.69,.76,-.17);
return vec3(-.89,-.64,-.18);
}
void outcrops(inout Hit hit,vec3 ro,vec3 rd) {
for(int i=0;i<3;i++) {
float k = float(i);
stone(hit,ro,rd,hero(i),vec3(.33+.035*k,.25,.217),.63+.74*k,.21+.28*k);
}
}
vec3 mineral(Hit h) {
vec2 uv = h.local.xy+.49*h.local.z;
float fleck = fbm(uv*16.0+h.seed*27.0);
float fissure = abs(sin(6.7*uv.x+2.0*uv.y+4.2*fbm(uv*3.0+h.seed)));
vec3 c = mix(vec3(.24,.235,.207),vec3(.56,.42,.28),h.seed);
c *= .61+.76*fleck;
c += vec3(.19,.17,.13)*exp(-sq(fissure/.13))*smoothstep(.20,.62,fleck);
return c*(.53+.61*max(dot(sun,h.n),0.0));
}
float waveHeight(vec2 p) {
float flow = p.y+uTime*.55;
float h = .008*sin(flow*19.0+3.2*noise2(p*vec2(3,2)))
+.0045*sin(flow*32.0+p.x*10.0+1.3*sin(7.0*p.x));
for(int i=0;i<3;i++) {
vec2 d = p-hero(i).xy;
float flank = sqrt(d.x*d.x+.008);
float bow = d.y+.66*flank-.24;
h += .029*exp(-sq(bow/.095)-sq(flank/.46));
float wake = .5*flank+d.y;
h += .014*sin(43.0*wake+.6*sin(2.1*uTime))*exp(-sq(wake/.38)-sq(flank/.49));
}
return h;
}
void main() {
vec2 uv = sceneCoord();
vec3 ro = vec3(0,-1.46,1.67);
vec3 rd = normalize(vec3(.66*uv.x,.90+.59*uv.y,-1.0+.24*uv.y));
vec3 surface = ro-rd*ro.z/rd.z;
surface += rd*(waveHeight(surface.xy)/rd.z);
float e = .0028;
vec2 p = surface.xy;
vec3 n = normalize(vec3(waveHeight(p-vec2(e,0))-waveHeight(p+vec2(e,0)),
waveHeight(p-vec2(0,e))-waveHeight(p+vec2(0,e)),2.0*e));
vec3 bend = refract(rd,n,1.0/1.333);
vec2 floorPoint = p-bend.xy*(.34+surface.z)/bend.z;
Hit h = Hit(10.0,vec3(0,0,1),vec3(0),.5);
for(int level=0;level<2;level++) {
float size = level==0 ? .34 : .111;
for(int j=-1;j<=1;j++) for(int i=-1;i<=1;i++) {
vec2 id = floor(floorPoint/size)+vec2(i,j);
float s = hash(id+4.7);
vec2 c = size*(id+.5+.73*(vec2(hash(id+2.3),hash(id+.6))-.5));
vec3 axis = size*vec3(.31+.31*s,.24+.26*hash(id+3.3),.26+.3*s);
stone(h,surface,bend,vec3(c,-.36+.025*s),axis,6.28*s,s);
}
}
outcrops(h,surface,bend);
vec3 gravel = h.t<9.0 ? mineral(h) : vec3(.18,.17,.12)*(.55+fbm(floorPoint*94.0));
float focus = sin(19.0*p.y+22.0*n.y+5.0*p.x);
gravel *= .93+.15*exp(-sq(focus/.2));
vec3 reflection = reflect(rd,n);
float bank = smoothstep(.25,.64,fbm(reflection.xy*5.0)+.56*reflection.z-.23);
vec3 sky = mix(vec3(.11,.21,.17),vec3(.75,.85,.91),bank);
float fresnel = .036+.96*pow(1.0-max(0.0,dot(-rd,n)),4.2);
// Shorter paths near the exposed stones retain the warm mineral color.
vec3 transmission = exp(-min(h.t,.8)*vec3(.52,.21,.14));
vec3 submerged = mix(vec3(.12,.24,.23),gravel,transmission);
vec3 water = mix(submerged,sky,clamp(.073+fresnel*1.65,0.0,.93));
water += vec3(1.0,.95,.81)*pow(max(dot(reflection,sun),0.0),110.0);
Hit air = Hit(length(surface-ro),vec3(0),vec3(0),0.0);
outcrops(air,ro,rd);
if(air.t<length(surface-ro)) {
vec3 rock = mineral(air);
vec3 hitPoint = ro+rd*air.t;
float z = hitPoint.z-waveHeight(hitPoint.xy);
rock *= .84+.19*smoothstep(.004,.047,z);
rock += .045*fbm(hitPoint.xy*115.0);
rock += .29*pow(max(dot(reflect(rd,air.n),sun),0.0),39.0);
water = mix(water,rock,smoothstep(-.008,.003,z));
}
fragColor = vec4(water,1);
}
`;
export function createRiverRiffleOverCobblesMaterial(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 | river-riffle-over-cobbles |
|---|---|
| Category | Water & Ice Shaders · Dynamic Liquids |
| Upstream study | liquids/lib/15_river_riffle_over_cobbles.glsl (commit c3a6f6c) |
| Animation cycle | Continuous procedural uTime |
| Declared uniforms | uResolution, uTime |
| Source size | 143 lines · 5.4 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
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.
PBR StudyFeathery granular rime ice feathers accreting into freezing alpine wind.
PBR StudyRounded polar ice plates with collision-raised slush rims floating on dark frigid water.
PBR StudySun-ablated high-altitude snow blades with subsurface cyan scattering in deep hollows.