<p>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 <em>solid</em> geometry, where inside/outside tests are fast.</p>

<p><a href="https://i.sstatic.net/JhSP2.png" rel="noreferrer"><img src="https://i.sstatic.net/JhSP2.png" alt="enter image description here"></a>
<br/><sup>Images from <a href="http://artis.imag.fr/Publications/2006/BD06/relief05.pdf" rel="noreferrer">Rendering Geometry with Relief Textures</a></sup></p>

<p>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. <a href="http://www.inf.ufrgs.br/~oliveira/pubs_files/Policarpo_Oliveira_RTM_multilayer_I3D2006.pdf" rel="noreferrer">Relief Mapping of Non-Height-Field Surface Details</a> is a good read!</p>

<p><a href="https://i.sstatic.net/msLZp.png" rel="noreferrer"><img src="https://i.sstatic.net/msLZp.png" alt="enter image description here"></a></p>

<p>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.</p>

<p><strike>Sphere tracing may refer to an implicit ray-sphere intersection test,</strike> but it's also the name of a space leaping technique by John Hart, as @joojaa mentions, and used by William Donnelly (<a href="http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter08.html" rel="noreferrer"><em>Per-Pixel Displacement Mapping with Distance Functions</em></a>), where a 3D texture encodes spheres radii in which no geometry exists.</p>

CC BY-SA 3.0
2015-08-10T08:05:00.250Z
Ha, there you go, taking images from others is more efficient than drawing them yourself.
joojaa
CC BY-SA 3.0
2015-08-10T08:44:38.337Z
@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](http://superuser.com/q/930036/264276) in chrome so the text is all colourful.
jozxyqk
CC BY-SA 3.0
2015-08-10T08:59:04.347Z
Well that subpixel rendering is something that has not been asked. Yet.
joojaa
CC BY-SA 3.0
2015-08-10T09:41:23.373Z
[done](http://computergraphics.stackexchange.com/q/165/198).
jozxyqk
CC BY-SA 3.0
2015-08-10T07:34:19.733Z
2015-08-10T07:41:52.513Z
2015-08-10T07:41:52.513Z
jozxyqk
<h3>TL;DR</h3>
<p>They belong to the same family of solvers, where sphere tracing is one method of ray marching, which is the family name.</p>
<h3>Raymarching a definition</h3>
<p>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.</p>
<p>This can be useful if you:</p>
<ul>
<li>Need to render volumetrics that arenot uniform</li>
<li>Rendering implicit functions, fractals</li>
<li>Rendering other kinds of parametric surfaces where intersection is not known ahead of time, like paralax mapping</li>
<li>Etc</li>
</ul>
<p><img src="https://i.sstatic.net/bc4D5.png" alt="Traditional raymarching" /></p>
<p><strong>Image 1</strong>: Traditional ray marching for surface finding</p>
<p>Related posts:</p>
<ul>
<li><a href="https://gamedev.stackexchange.com/questions/67719/how-do-raymarch-shaders-work">how-do-raymarch-shaders-work</a> (GameDev.SE)</li>
</ul>
<h3>Sphere tracing</h3>
<p>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.</p>
<p>Sphere tracing is used for rendering <a href="https://en.wikipedia.org/wiki/Implicit_surface" rel="noreferrer">implicit surfaces</a>. Implicit surfaces are formed at some level of a continuous function. In essence solving the equation</p>
<pre><code>F(X,Y,Z) = 0
</code></pre>
<p>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.</p>
<p><img src="https://i.sstatic.net/dFHFY.png" alt="Sphere tracing has a adaptive step size" /></p>
<p><strong>Image 2</strong>: Sphere tracing* in action note how the step size is adaptive</p>
<p>For more info see:</p>
<ul>
<li><a href="http://graphics.cs.illinois.edu/sites/default/files/zeno.pdf" rel="noreferrer">Sphere tracing: a geometric method for the antialiased ray tracing of implicit surfaces</a></li>
</ul>
<p>* Perhaps in 2d it's should be called circle tracing :)</p>

CC BY-SA 3.0
2016-12-24T12:54:46.943Z
I would recommend this article from Scratchapixel for an in-depth explanation: https://www.scratchapixel.com/lessons/advanced-rendering/ray-tracing-distance-fields
user18490
CC BY-SA 4.0
2018-06-21T08:22:27.337Z
@user18490 's link to Scratchapixel has moved to https://www.scratchapixel.com/lessons/advanced-rendering/rendering-distance-fields . The rest of the site (https://www.scratchapixel.com/index.php) looks useful as well.
WillC
CC BY-SA 4.0
2021-01-14T17:32:14.990Z
Wikipedia defines [*ray marching*](https://en.wikipedia.org/wiki/Ray%20marching) as a synonym of *volume ray casting*, but I'm not sure if this is correct.
Anderson Green
CC BY-SA 4.0
2021-01-14T17:51:20.830Z
@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.
joojaa
CC BY-SA 3.0
2015-08-10T07:35:06.200Z
2015-08-10T13:44:05.627Z
2020-06-18T08:32:12.800Z
joojaa
<p>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.</p>

<p>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?</p>

<raytracing><demoscene><raymarching>
What is Ray Marching? Is Sphere Tracing the same thing?
psicomante
# What is Ray Marching? Is Sphere Tracing the same thing?

(Asked by: username_0 on 2015-08-10)

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?

**Tags:** raytracing, demoscene, raymarching


---

## 2 answers

### Answer by username_2 on 2015-08-10. Score: 51 | Accepted answer

### TL;DR

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](https://i.sstatic.net/bc4D5.png)

**Image 1**: Traditional ray marching for surface finding

Related posts:

* [how-do-raymarch-shaders-work](https://gamedev.stackexchange.com/questions/67719/how-do-raymarch-shaders-work) (GameDev.SE)

### 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](https://en.wikipedia.org/wiki/Implicit_surface). 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](https://i.sstatic.net/dFHFY.png)

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

For more info see:

* [Sphere tracing: a geometric method for the antialiased ray tracing of implicit surfaces](http://graphics.cs.illinois.edu/sites/default/files/zeno.pdf)

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

> **Comments:**
> **username_3**: I would recommend this article from Scratchapixel for an in-depth explanation: https://www.scratchapixel.com/lessons/advanced-rendering/ray-tracing-distance-fields  
> **username_4**: @username_3 's link to Scratchapixel has moved to https://www.scratchapixel.com/lessons/advanced-rendering/rendering-distance-fields . The rest of the site (https://www.scratchapixel.com/index.php) looks useful as well.  
> **username_5**: Wikipedia defines [*ray marching*](https://en.wikipedia.org/wiki/Ray%20marching) as a synonym of *volume ray casting*, but I'm not sure if this is correct.  
> **username_2**: @username_5 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.  

---

### Answer by username_1 on 2015-08-10. Score: 18

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](https://i.sstatic.net/JhSP2.png)](https://i.sstatic.net/JhSP2.png)
  
Images from [Rendering Geometry with Relief Textures](http://artis.imag.fr/Publications/2006/BD06/relief05.pdf)

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](http://www.inf.ufrgs.br/~oliveira/pubs_files/Policarpo_Oliveira_RTM_multilayer_I3D2006.pdf) is a good read!

[![enter image description here](https://i.sstatic.net/msLZp.png)](https://i.sstatic.net/msLZp.png)

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 @username_2 mentions, and used by William Donnelly ([*Per-Pixel Displacement Mapping with Distance Functions*](http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter08.html)), where a 3D texture encodes spheres radii in which no geometry exists.

> **Comments:**
> **username_2**: Ha, there you go, taking images from others is more efficient than drawing them yourself.  
> **username_1**: @username_2 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](http://superuser.com/q/930036/264276) in chrome so the text is all colourful.  
> **username_2**: Well that subpixel rendering is something that has not been asked. Yet.  
> **username_1**: [done](http://computergraphics.stackexchange.com/q/165/198).  

---

