A lot of ShaderToy demos share the Ray Marching algorithm to render the scene, but they are often written with a very compact style and i can't find any straightforward examples or explanation.

So what is Ray Marching? Some comments suggests that it is a variation of Sphere Tracing. What are the computational advantages of a such approach?



They belong to the same family of solvers, where sphere tracing is one method of ray marching, which is the family name.

Raymarching a definition

Raymarching is a technique a bit like traditional raytracing where the surface function is not easy to solve (or impossible without numeric iterative methods). In raytracing you just look up the ray intersection, whereas in ray marching you march forward (or back and forth) until you find the intersection, have enough samples or whatever it is your trying to solve. Try to think of it like a newton-raphson method for surface finding, or summing for integrating a varying function.

This can be useful if you:

  • Need to render volumetrics that arenot uniform
  • Rendering implicit functions, fractals
  • Rendering other kinds of parametric surfaces where intersection is not known ahead of time, like paralax mapping
  • Etc

Traditional raymarching

Image 1: Traditional ray marching for surface finding

Related posts:

Sphere tracing

Sphere tracing is one possible Ray marching algorithm. Not all raymarching uses benefit form this method, as they can not be converted into this kind of scheme.

Sphere tracing is used for rendering implicit surfaces. Implicit surfaces are formed at some level of a continuous function. In essence solving the equation

F(X,Y,Z) = 0

Because of how this function can be solved at each point, one can go ahead and estimate the biggest possible sphere that can fit the current march step (or if not exactly reasonably safely). You then know that next march distance is at least this big. This way you can have adaptive ray marching steps speeding up the process.

Sphere tracing has a adaptive step size

Image 2: Sphere tracing* in action note how the step size is adaptive

For more info see:

* Perhaps in 2d it's should be called circle tracing :)

  • 1
    $\begingroup$ I would recommend this article from Scratchapixel for an in-depth explanation: scratchapixel.com/lessons/advanced-rendering/… $\endgroup$ – user18490 Dec 24 '16 at 12:54
  • $\begingroup$ @user18490 's link to Scratchapixel has moved to scratchapixel.com/lessons/advanced-rendering/… . The rest of the site (scratchapixel.com/index.php) looks useful as well. $\endgroup$ – WillC Jun 21 '18 at 8:22
  • $\begingroup$ Wikipedia defines ray marching as a synonym of volume ray casting, but I'm not sure if this is correct. $\endgroup$ – Anderson Green yesterday
  • $\begingroup$ @AndersonGreen you can solve volumes with it thats probably one of the common cases. But there is no reason you cant solve surfaces with volume functions. (Implicit surfaces are surfaces of volumes) There are some advantages to marching for surfaces like ease of implementation. $\endgroup$ – joojaa yesterday

Ray marching is an iterative ray intersection test in which you step along a ray and test for intersections, normally used to find intersections with solid geometry, where inside/outside tests are fast.

enter image description here
Images from Rendering Geometry with Relief Textures

A fixed step size is pretty common if you really have no idea where an intersection may occur, but sometimes root finding methods such as a binary or secant search are used instead. Often a fixed step size is used to find the first intersection, followed by a binary search. I first came across ray marching in per-pixel displacement mapping techniques. Relief Mapping of Non-Height-Field Surface Details is a good read!

enter image description here

It's commonly used with space leaping, an acceleration technique where some preprocessing gives a safety distance that you can move along the ray without intersecting geometry, or better yet, without intersecting and then leaving geometry so that you miss it. For example, cone step mapping, and relaxed cone step mapping.

Sphere tracing may refer to an implicit ray-sphere intersection test, but it's also the name of a space leaping technique by John Hart, as @joojaa mentions, and used by William Donnelly (Per-Pixel Displacement Mapping with Distance Functions), where a 3D texture encodes spheres radii in which no geometry exists.

  • $\begingroup$ Ha, there you go, taking images from others is more efficient than drawing them yourself. $\endgroup$ – joojaa Aug 10 '15 at 8:05
  • $\begingroup$ @joojaa Yes, faster if you remember where they were, but without that gratifying feeling of doing it yourself :P. Also I have this stupid sub-pixel rendering bug in chrome so the text is all colourful. $\endgroup$ – jozxyqk Aug 10 '15 at 8:44
  • 1
    $\begingroup$ Well that subpixel rendering is something that has not been asked. Yet. $\endgroup$ – joojaa Aug 10 '15 at 8:59
  • 1
    $\begingroup$ done. $\endgroup$ – jozxyqk Aug 10 '15 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.