River Riffle Over Cobbles shader

river-riffle-over-cobbles.glsl · 143 lines · GLSL ES 3.00 · MIT

River Riffle Over CobblesWater & Ice
River Riffle Over Cobbles WebGL2 procedural shader preview
FPS 60BUFFER 960×540SOURCE 143 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
river-riffle-over-cobbles.glsl (143 lines)
#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);
}

Mathematical models & shader specifications

Identifierriver-riffle-over-cobbles
CategoryWater & Ice Shaders · Dynamic Liquids
Upstream studyliquids/lib/15_river_riffle_over_cobbles.glsl (commit c3a6f6c)
Animation cycleContinuous procedural uTime
Declared uniformsuResolution, uTime
Source size143 lines · 5.4 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