A real-time procedural landscape rendered entirely in a single HTML file.
Originally created by P_Malin on Shadertoy β ported to a standalone WebGL2 experience.
- About
- Features
- Quick Start
- Project Structure
- How It Works
- Controls
- Technical Details
- Performance
- Compatibility
- Credits
- License
A hacked flow and advection experiment that turned into something beautiful.
This project brings P_Malin's iconic Shadertoy shader to a self-contained HTML file. No build tools. No frameworks. No dependencies. Just open index.html in a browser and watch a river carve through procedural terrain in real time.
The entire scene β mountains, riverbed, water surface, foam, reflections, refractions, shadows, and atmospheric fog β is generated mathematically, pixel by pixel, every frame.
| Feature | Description |
|---|---|
| Raymarched Terrain | Procedural heightfield using Fractal Brownian Motion (FBM) |
| Flowing Water | Animated river with realistic flow rates and meandering |
| Water Foam | Dynamic foam generation based on flow velocity and depth |
| Refraction & Reflection | Physically-based water surface with Fresnel equations |
| Cone Stepping | Accelerated raymarching via heightfield cone optimization |
| Volumetric Fog | Exponential distance-based fog with extinction and inscattering |
| Shadow Mapping | Soft sun shadows on both terrain and water surface |
| Triplanar Texturing | Texture projection without UV seams |
| Auto Camera | Smooth camera flythrough following the river path |
| Mouse Control | Click and drag to steer the camera |
| High DPI | Automatic device pixel ratio scaling |
- Clone or download the repository
- Open
index.htmlin any modern browser - Wait for
texture.jpgto load - Enjoy the scene
Note: A local server may be required for texture loading in some browsers. You can use:
# Python python -m http.server 8000 # Node.js npx serve .
.
βββ index.html # Everything: HTML, JavaScript, and GLSL
βββ texture.jpg # Terrain texture sampled via triplanar mapping
βββ preview.jpg # Screenshot of the rendered scene
Single file. Zero configuration. The fragment shader, vertex shader, WebGL setup, texture loading, and render loop all live inside index.html.
Fragment Coord
β
βΌ
Camera Ray Generation
β
βΌ
βββββββββββββββββββ
β Raymarch Scene β β Cone stepping acceleration
ββββββββββ¬βββββββββ
β
βββββββ΄ββββββ
β β
Terrain Sky Color
β
βΌ
Water Trace βββ Refraction + Reflection
β
βΌ
Lighting (Sun + Sky)
β
βΌ
Fog Composition
β
βΌ
Vignette + Tonemap
β
βΌ
Pixel
The landscape is a heightmap built from layered FBM noise. Each octave doubles in frequency and halves in amplitude, creating natural-looking erosion patterns. A riverbed is carved by subtracting depth based on a sinusoidal meander function.
The water normal is computed via gradient-domain FBM with flow advection. Two noise samples are blended over time to create the illusion of moving water. Foam appears where flow velocity exceeds a threshold or where depth decreases near obstacles.
- Sun light β Blinn-Phong specular with Cook-Torrance distribution
- Sky light β Hemisphere ambient proportional to surface normal
- Fresnel β Schlick approximation blending reflection and refraction
- Shadows β Secondary raymarch from water/terrain surface toward the sun
| Input | Action |
|---|---|
| Mouse drag | Steer camera heading and distance |
| Automatic | Camera follows the river when idle |
| Parameter | Value |
|---|---|
| Raymarch steps | 64 |
| FBM octaves (terrain) | 3 |
| FBM octaves (water) | 4 |
| Water refraction index | 1 / 1.3333 (real water) |
| Far clip | 20.0 units |
| Sun direction | (-1.0, 0.7, 0.25) normalized |
| Define | Effect |
|---|---|
ENABLE_WATER |
Renders the water surface |
ENABLE_FOAM |
Adds foam to the water |
ENABLE_WATER_RECEIVE_SHADOW |
Shadows cast onto water |
ENABLE_CONE_STEPPING |
Accelerates terrain raymarching |
The shader is computationally intensive by design. Each pixel requires:
- Multiple FBM evaluations for terrain height
- A secondary raymarch for shadows
- Refraction ray bounce
- Reflection ray bounce
- Flow and foam calculations
For smoother performance on lower-end GPUs, consider:
- Reducing
k_raymarchStepsfrom 64 to 32 - Disabling
ENABLE_WATER_RECEIVE_SHADOW - Removing supersampling if enabled
| Browser | Status |
|---|---|
| Chrome 80+ | β Full support |
| Firefox 80+ | β Full support |
| Edge 80+ | β Full support |
| Safari 15+ | β Full support |
| Mobile browsers |
Requirements: WebGL2 (#version 300 ES)
- Shader Author: P_Malin β original "Where the River Goes" on Shadertoy
- Hash function: Dave Hoskins β Hash without Sine
- Block render technique: Shadertoy /ltlSWf
The original shader is authored by P_Malin and hosted on Shadertoy under their terms of use. This WebGL2 port is provided for educational and personal use.
Every pixel, every frame, computed from pure mathematics.
Made with β€οΈ by SebastiΓ‘n V
