threejs-webgl-realistic-water
Skill4 starsChanged 19 days agoMITView on GitHub
Builds physically lit, painterly-realistic river/lake/pond water in three.js WebGL — depth-aware screen-space refraction, Beer–Lambert absorption and in-scatter, planar reflection with Schlick Fresnel, shadowed GGX sun glints, mipmapped slope-map ripples, ripple-ring sources, soft waterlines, chromatic caustics on submerged surfaces and a Voronoi pebble riverbed. Use when a three.js / React Three Fiber scene has flat, transparent or milky water, when the user asks for realistic water, refraction, reflection, caustics, a clear stream, a riverbed, or "water like Ghibli but physically lit".
In this repository30 other files it gives its agents.
- threejs-blender-glb-pipelineSkill
- threejs-boat-buoyancy-steeringSkill
- threejs-camera-guided-tourSkill
- threejs-cinematic-loading-screenSkill
- threejs-day-cycle-lightingSkill
- threejs-dreamy-glass-hudSkill
- threejs-first-person-bvh-walkSkill
- threejs-fish-school-boidsSkill
- threejs-floating-island-landmarkSkill
- threejs-ghibli-night-modeSkill
- threejs-ghibli-sky-cloudsSkill
- threejs-ghibli-toon-shadingSkill
- threejs-glb-realism-pipelineSkill
- threejs-gpu-butterfliesSkill
- threejs-headless-visual-verificationSkill
- threejs-hud-objective-markersSkill
- threejs-night-lights-lanternsSkill
- threejs-object-interactionSkill
- threejs-open-world-streamingSkill
- threejs-postprocessing-anime-gradeSkill
- threejs-procedural-vegetationSkill
- threejs-shell-fur-renderingSkill
- threejs-skeletonless-character-animationSkill
- threejs-stylized-rock-materialSkill
- threejs-webaudio-generative-soundtrackSkill
- threejs-webgl-performance-profilingSkill
- threejs-webgl-rain-effectSkill
- threejs-webgl-underwater-effectSkill
- threejs-webgl-wake-simulationSkill
- threejs-webgl-waterfall-effectSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 24
Follow-ups: go under the surface → threejs-webgl-underwater-effect.
- Distinctive: line 159
Look for black specks on caustic stones (Inf) and hard edges at the bank.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Realistic water (refraction, absorption, reflection, caustics, riverbed)
A water surface that reads as clear, deep and alive from max zoom-out to eye level: you see the bed
through it, the banks mirror in it at grazing angles, tree shadows dapple it, sun glints bloom off
ripples, and submerged stones carry a dancing caustic web. Proven on a three.js r186
WebGLRenderer + EffectComposer scene (vanilla, single HTML file); everything is built by
patching stock materials with onBeforeCompile, so it works the same inside R3F
(material.onBeforeCompile / useFrame for the pre-passes).
The water is a MeshStandardMaterial (roughness ≈ 0.1, side: DoubleSide) so it inherits
three's shadowed sun lighting and fog, and the final colour is composed yourself at
#include <opaque_fragment>.
When to use
- The scene has a water plane (river, pond, lake) at a known height and a terrain/bed height function.
- Water looks flat, plastic, milky, or like a transparent sheet with no depth.
- You want caustics on everything below the surface, or a riverbed that reads as pebbles, not tiles.
- Follow-ups: go under the surface →
threejs-webgl-underwater-effect; waves and floating objects →threejs-boat-buoyancy-steering; night water →threejs-ghibli-night-mode.
Paste-ready prompt
Enhance the water in
{{entry file, e.g. index.html}}. The look to match is{{reference image path}}— {{style: e.g. "Ghibli-painterly but physically lit"}}. Water surface is the planey = {{0}}; the terrain/bed height function is{{terrainHeight(x,z)}}. Keep every tunable as a named constant at the top of the script and in a lil-gui "Tune" panel. After each section, open the page in a browser, screenshot close-up, eye-level and max-zoom-out views, compare against the reference, and fix what's wrong before moving on.Replace any flat/transparent water with a
MeshStandardMaterial(roughness ≈ 0.1, DoubleSide) patched viaonBeforeCompile: a refraction pre-pass with a realDepthTexture(true thickness along the view ray), Beer–Lambert absorption(0.95, 0.24, 0.20)/m plus turquoise in-scatter scaled by the shadowed sun arriving at the surface, a half-res planar reflection mixed by Schlick Fresnel (F0 = 0.02),directSpecularglints ×~0.3, ripple normals from a precomputed mipmapped tileable slope map (4 octaves drifting downstream, faded with distance), avec4ripple-source array, and a soft waterline. Inject a chromatic caustic web into submerged materials, and make the bed read as pebbles set in sand (domain-warped Voronoi, one rounded stone per cell). Report frame cost per pass at the end.
Implementation
1. Render passes (per frame, before the composer)
- Refraction pre-pass. Hide the water; render the scene into a
HalfFloatTypetarget with aDepthTexture(UnsignedIntType). Keep above-water-only foliage on layer 1 and render this pass withcamera.layers.set(0), then restore the savedcamera.layers.mask(notlayers.enable(1), which drops any other layer the camera had). - Planar reflection. Mirror the camera through the plane (Reflector.js construction), render
at half resolution, and build the projective texture matrix
bias(0.5) × mirrorCam.projection × mirrorCam.matrixWorldInverse. Keep only what's above the surface (Plane((0,1,0), 0.015)) by making that plane the mirror camera's near plane (Lengyel's oblique projection, as three'sReflectordoes) — not withrenderer.clippingPlanes, which compiles a second variant of every shader in the scene (see Pitfalls). Build the texture matrix from the plain projection first, then skew it: rows x, y and w don't change. Copy the main camera's projection into the mirror camera every frame, so the skew never accumulates. - Main pass via the composer, water visible.
- Share one shadow render:
renderer.shadowMap.autoUpdate = false,needsUpdate = trueonce per frame.
The reflection draws the whole scene a second time. Put the densest ground cover (grass, foreground flowers) on a layer of its own that the main camera and the sun's shadow camera enable, and disable it on the mirror camera below the top quality tier: half-resolution blades at the bank edge are not worth the cost of the whole meadow again.
See references/water-passes.js (render loop + mirror camera + oblique near plane + slope-map generator).
2. Water fragment (at #include <opaque_fragment>)
- Thickness: linearise scene depth vs. surface depth:
linZ(d) = 2nf / (f+n − (2d−1)(f−n)).thick = (sceneZ − surfZ) · |viewPos| / viewPos.z→ true path length along the ray. - Refraction offset:
off = N.xz · uRefr · clamp(thick0·2.5, 0, 1) / (1 + surfZ·0.06)— shallows barely distort, distance calms it. If the offset sample is in front of the surface (sceneZ < surfZ) fall back to the un-offset UV so above-water objects never leak in. Sample R/G/B atruv ± off·0.12for dispersion. - Beer–Lambert + in-scatter:
T = exp(-thick·absorb), absorb ≈(0.95, 0.24, 0.20)/m (reds fade first).under = refr·T + scatter·(1 − exp(-thick·0.9))·(0.15 + 0.65·sunLit).sunLit= luminance ofreflectedLight.directDiffuse(white albedo)· π / (sunLum · N·L)— the lit/shadowed light arriving at the surface, so canopy shadows dapple the water. - Reflection: sample the mirror target at
rc.xy/rc.w + N.xz·0.06/(1 + surfZ·0.04);F = 0.02 + 0.98(1 − N·V)^5;col = mix(under, refl, F). - Glints:
col += reflectedLight.directSpecular · ~0.3— already GGX and shadowed; bloom does the rest. - Waterline:
col = mix(refr, col, smoothstep(0, 0.035, thick))— no hard edge at the bank.
3. Ripple normals
- Precompute a 512² tileable slope map in JS: periodic value noise, octaves
[[4,1],[8,.55],[16,.32],[32,.17],[64,.09]], soft swells for P ≤ 8 and ridged1−|2n−1|above, stored as RG slopes (central differences × 18),generateMipmaps,LinearMipmapLinearFilter, max anisotropy. - Sample 4 octaves drifting downstream at different angles/speeds (scales
.075, .19, .52, 1.45, weights.55, .38, .22, .10), multiply byuNStr / (1 + dist·0.035). - Ripple sources:
uniform vec4 uRipples[8](x, z, strength, w).w < 0→ steady rings (sin(L·36 − t·5.2)·exp(−L·4.5), feet/posts);w = startTime→ expanding ringR = age·0.32, envelopeexp(−((L−R)·16)²)·exp(−age·0.9)(a fish kiss, a thrown stone). - Normal =
normalize(vec3(-sl.x, 1, -sl.y)), flipped for back faces, then to view space innormal_fragment_mapsso three's lighting uses it.
See references/water-material.glsl.js for the full onBeforeCompile patch.
4. Caustics on submerged surfaces
Inject into #include <emissivemap_fragment> of bed, rocks, fish, posts:
- the iterated domain-warp caustic web (
causticF, 4 iterations, tiles every 2π), - sampled once per channel with ±0.004 offsets (chromatic),
- projected along the sun:
cp = wp.xz·0.42 + (sunDir.xz/sunDir.y)·wp.y·0.42, - faded in with depth:
smoothstep(0.02, −0.08, y) · exp(y·1.2) · smoothstep(0.02, 0.22, −y), totalEmissiveRadiance += albedo · c · sunColour·1.1.
See references/caustics.glsl.
5. Pebble riverbed
Domain-warped Voronoi (warp = (fbm(wp·1.7), fbm(wp·1.7+5.3)) − .5)·0.9) at two scales — gravel
(×8.5) and cobble patches (×3.6) picked by step(.58, fbm(wp·.35)). One rounded stone per cell
with jittered radius 0.30 + 0.16·hash, set in sand so sand shows in the gaps; dome shading
sqrt(1 − (dc/r)²), a speckle, a contact-shadow ring on the sand, algae thinning on stone
crowns. Perturb the view-space normal by the vector to the cell centre (× pebMask × 2.2) so each
pebble catches light. See references/riverbed-pebbles.glsl.
Numbers that worked
| Constant | Value |
| roughness | 0.11 |
| absorb (/m) | (0.95, 0.24, 0.20) |
| F0 | 0.02 |
| glint gain | 0.28 |
| refraction offset | < 0.05 |
| ripple distance fade | 1/(1 + d·0.035) |
| waterline blend | smoothstep(0, 0.035, thick) |
Pitfalls
- Per-pixel fbm normals alias at distance into sky-coloured noise → the water goes milky when zoomed out. Mipmapped slopes + the distance fade fix it.
- Absorption from vertical terrain depth instead of true thickness makes fish look grey.
- Strong in-scatter makes the water milky.
- Refraction offsets ≥ 0.05 smear the riverbed into mush.
- The classic caustic formula divides by
sin()→ infinities → black speckles. Clamp the denominators to±1e-3and the output to ≤ 4. - In
onBeforeCompile, inject helper GLSL before any code that calls it. readPixels(RGBA, UNSIGNED_BYTE)on a HalfFloat target isINVALID_OPERATION— time passes on the default framebuffer (seethreejs-webgl-performance-profiling).- Once the water displaces (waves), the plane needs real vertices (~58 cm cells) — a 1×1 plane can't move. A low quality tier got away with 79 cm cells: 2.4 samples across the shortest (1.9 m) wave train, whose light comes from the analytic slope anyway.
renderer.clippingPlanesfor the reflection pass doubles the shader programs (~540 instead of ~270 here): every material compiles a clipping variant, which costs seconds of compile on Windows and phones. The oblique near plane needs one variant and no per-pixel work; A/B'd against the clipping plane it gave the same image, bar sub-pixel depth ties in foliage.- The oblique near plane needs the mirror camera behind the cut: clamp the view-space plane constant to ≤ −0.02, so a lens skimming the surface moves the cut a centimetre or two instead of breaking the projection.
- Transparent sheets (foam, mist) near the water must be hidden from the AO pass or they print dark.
Verify
- Zoom out to max distance: you must still see the bed, no milky noise.
- Eye level: the banks must be mirrored.
- Close up: fish stay vividly coloured; no leak of above-water objects into the refraction.
- Look for black specks on caustic stones (Inf) and hard edges at the bank.
- Measure refraction, reflection and main pass cost separately.
- Count
renderer.info.programs.lengthafter the first frames: it must not double when the reflection pass is switched on.
Related skills
threejs-webgl-underwater-effect · threejs-boat-buoyancy-steering · threejs-webgl-wake-simulation
· threejs-webgl-waterfall-effect · threejs-ghibli-night-mode · threejs-webgl-rain-effect
· threejs-fish-school-boids · threejs-postprocessing-anime-grade · threejs-webgl-performance-profiling
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

