Skip to content
Shape · TechniqueAdvanced

Raymarched 3D shapes

Draw whole 3D scenes from distance functions inside one fragment shader, with no meshes at all.

UVTimeFull screen

Cards

One live example to tweak, learn and copy.

How it works

Most 3D graphics start from triangles. Raymarching starts from a function instead. The scene is a 3D signed distance field: give it any point and it returns how far that point is from the nearest surface. A sphere is length(p) - r; a box, a torus or a capsule each take a line or two; and the operators from 2D composition work unchanged, so min joins shapes and a smooth minimum melts them together.

To see the scene, a fragment shader over a full-screen quad shoots one ray per pixel from a virtual camera. It steps along the ray by the scene’s distance, which is always a safe step because nothing is closer, until the distance is almost zero: the ray has hit. The surface normal comes from the field’s gradient, a few more samples around the hit, and from there lighting is the same dot product as on any mesh.

What you get is a kind of geometry triangles can’t easily give: shapes that blend smoothly, repeat to infinity with a mod on the point, bend and twist, all exact at any zoom and changing every frame with no mesh to rebuild. What it costs is a loop in every pixel, each step evaluating the whole scene. That makes it the tool for full-screen demos, backgrounds, title cards and effects inside a box, and the wrong tool for a game’s main geometry.

In an engine it runs on a full-screen rectangle (Godot’s ColorRect, a quad in front of the camera elsewhere). Mixing it with the engine’s own meshes needs the hit’s depth written out, which each engine handles differently.

Further reading