GLSL shader example

Ray-Marched Heightfield Terrain

A height function defines the surface. Reduce step size conservatively to mitigate tunneling; steep terrain still needs a tighter distance bound.

Live example

Edit the code and the preview updates as you type.

What to observe

Ray-Marched Heightfield Terrain.

Mechanism

A height function defines the surface. Reduce step size conservatively to mitigate tunneling; steep terrain still needs a tighter distance bound.

The controlling code

return (p.y+ .4 - .28*fbm(p.xz*1.4) - .07*sin(p.x*2.))/1.8;

Experiment

In scene, lower the terrain amplitude .28*fbm(p.xz*1.4) to .14*fbm(p.xz*1.4). The large hills become lower while the separate .07*sin(p.x*2.) ridges keep their amplitude.

Debugging and cost

Each ray step evaluates four-octave fbm, and normalAt adds six more scene queries at a hit. Increase the scene divisor 1.8 to 2.4 to shorten steps; this may reduce overshoot but can exhaust the 80-step budget sooner.

Built-in functions used

Learn it step by step