Skip to content

Raymarched Scene

You'll write a raymarcher in a fragment shader, with a distance field, a camera, a lamp and shadows, and get a scene turning in the browser at full frame rate.

About

Nearly every modern intro and every shader round is a raymarcher: a fragment shader that, for each pixel, walks a ray forward by the distance to the nearest surface until it hits something. Graphics Programming's Fragment Shader Shapes gives you the 2D version; this badge takes it into 3D, and Shader Showdown and Four Kilobytes both assume you can type one from memory. Shadertoy runs in a browser for free and is where most of the scene learned this, and the distance function articles on iquilezles.org are the reference everyone keeps open. What counts is a shader you wrote yourself with a camera, at least three primitives combined with union, subtraction and a smooth blend, one light, shadows and a ground plane. Forking a Shadertoy you did not write and changing the colours does not count. The thing nobody tells you is that most of the ugly artefacts come from stepping too far or too few times, so draw the step count as colour before you touch anything else.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
One weekend
Skill level
Intermediate (months of practice)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in Demoscene to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Set up a full-screen shader with a camera ray for every pixel. Turn the pixel coordinate into a ray from an eye point through a virtual screen, and draw the ray direction as colour so you can see it change across the screen. Shadertoy gives you the quad and the resolution for free, and a local WebGL or OpenGL window with two triangles works too. Take a screenshot of the direction gradient with your camera code visible. Photo
  2. 2
    March a sphere and a plane and draw the step count. The distance to a sphere is the length to its centre minus the radius, and to a plane it is the height above it. Step along the ray by the smaller of the two until it drops under 0.001 or you have taken 100 steps. Draw the number of steps as a shade of grey, so the halos around edges show where the marcher works hardest. Take a screenshot of the step-count view. Photo
  3. 3
    Compute normals and light the scene with one lamp and a shadow. Sample the distance field a tiny offset either side in x, y and z, and the differences give you the normal. Dot it with the direction to the lamp for diffuse, then march a second ray from the hit point toward the lamp and darken anything that hits something on the way. The classic bug is a surface shadowing itself, which goes away when you start the shadow ray a little off the surface. Take a screenshot with the shadow visible. Photo
  4. 4
    Combine at least three primitives with union, subtraction and a smooth minimum. Add a box and a torus to the sphere, cut a hole with subtraction (the max of one distance and the negative of another), and blend two shapes with a smooth minimum so they merge like wax. The smooth minimum is what makes a raymarched scene look raymarched. Film the scene with the camera orbiting so the joins are visible. Video
  5. 5
    Add fog, a repeated element and a moving camera at a steady 60 frames. Fade to a sky colour by distance, use mod on one coordinate to repeat a shape forever at no cost, and move the camera along a path in time. If the frame rate drops, lower the step count or the resolution before anything else, because at 1080p every step is two million distance evaluations. Film 30 seconds with the frame counter showing. Video
  6. 6
    Post it on Shadertoy or as one HTML file and write what you'd type from memory. Publish it with the source visible and a short description. Write down the lines you would type first with no reference, and the two features you would drop to fit the scene into 25 minutes, because Shader Showdown is that round. Photo or video