<p>I've tried to figure this out as well, but I don't think there is a solution that is just as performant as tracing one sphere.  Basically you would want to cast one ray to hit a infinite set of repeating spheres with no marching iteration.  That would be awesome but is not possible:  Let me explain.</p>
<h1></h1>
<p><strong>Ray-tracing</strong> is basically finding the analytical solution to this equation:
<span class="math-container">$$
f(\vec{r(t)}) = 0 \ for \ t
$$</span></p>
<p>Where <span class="math-container">$f(\vec{p})$</span> is the implicit surface function (which gives the distance the given point <span class="math-container">$\vec{p}$</span> is from the surface) and <span class="math-container">$\vec{r(t)}$</span> is the parametric ray equation (<span class="math-container">$\vec{r(t)} = \vec{origin} + \vec{direction}*t$</span>).  <span class="math-container">$t$</span> would represent the distance along the ray the intersection happens.</p>
<p>In order to ray-trace domain manipulations (like repeating space), these domain manipulations need to be present in the implicit surface function <span class="math-container">$f(\vec{p})$</span> while finding an analytical solution.  Let's take a 3D repeating sphere for instance.
<span class="math-container">$$
f'(\vec{p}) = \|\vec{p}\bmod \vec{d}\| - r
$$</span>
(where <span class="math-container">$\vec{d}$</span> is a constant representing the grid size and <span class="math-container">$r$</span> is the radius)</p>
<p>In order to solve <span class="math-container">$f'(\vec{r(t)}) = 0$</span> for <span class="math-container">$t$</span> we would need the inverse of the modulo operator which doesn't really exist : <a href="https://web.archive.org/web/20171228030043/http://mathforum.org/library/drmath/view/51619.html" rel="nofollow noreferrer">Link</a>.  That means there is no analytical solution to this equation, but there is a numerical one. Hence <strong>Ray-marching</strong>.</p>
<h1></h1>
<p>This is true for a lot of cool distance functions you see on shadertoy.com, the analytical solution does not exist for these surfaces so you cannot ray-trace.</p>

CC BY-SA 4.0
2019-12-11T19:09:56.200Z
Couldnt you cheat a bit though. You could raycast on a cube and solve which parta of cube edge hits which sphere.
joojaa
CC BY-SA 4.0
2020-02-09T04:52:04.533Z
@joojaa if you were talking about cubemap stored infintie spheres that could work, but I devised an iterative process that works like light traveling through "portals" (mirrors except change position instead of direction) which gets the exact same image but using the classic recursive raytracing algorithm with imaginary objects.
cmarangu
CC BY-SA 4.0
2020-02-09T04:53:19.037Z
As for the tracing onto a plane, using that as coordinates for infinite spheres, here is a little project I am proud of I have been plotting for months and is nearly complete which serves as an excellent proof of concept https://www.shadertoy.com/view/3lXXR8
cmarangu
CC BY-SA 4.0
2020-02-09T04:56:11.327Z
that project basicallt traces onto a plane, and figures out which sphere has a chunk of itself in the checkerboard cell using floor(x), floor(y). Then it checks the 8 surrounding spheres (like conwats game of life) in case any of them has a chunk in that cell. Good luck figuring out exactly how I did it. Important thing is it runs 60FPS fullscreen, but maintains 100% accuracy for this fractal-ish shape
cmarangu
CC BY-SA 4.0
2020-02-09T05:26:09.703Z
I saw the +4 views. It is unlisted. If you guys are interested in exactly the technique I used to accurately trace hundreds of spheres in realtime, or at least appreciate my work of art, feel free to contact me in comments somewhere or leave a like respectively! I know some of you have shadertoy.com accounts.
cmarangu
CC BY-SA 3.0
2016-10-26T17:30:58.593Z
Thanks for the answer!  Interestingly, I have had some limited success at this, thanks to some ideas from someone on twitter.  I'm experimenting more to come up with some final info & a blog post, and will post an answer here with the details.  It's not a general solve, but it is still a bit interesting.  Maybe extensible.  Also had a friend mention he has been able to do this with other limited success by faking modulus mathematically, like using a badlimited saw wave.  I'll share some shadertoy links in the next comment for the curious.
Alan Wolfe
CC BY-SA 3.0
2016-10-26T17:34:08.840Z
Shadertoy from sebbi showing his idea: https://www.shadertoy.com/view/lly3Rc
Me exploring and formalizing some things: https://www.shadertoy.com/view/MlK3zt
Limited success for ray vs infinite layers of concentric circles: https://www.shadertoy.com/view/4tyGDK
I'm currently working on ray vs infinitely repeating pillars. Looking promising so far.
In the end these things are interesting but not in general very useful.  Maybe they can be extended. ::shrug::
Alan Wolfe
CC BY-SA 3.0
2016-10-26T19:46:23.730Z
Regarding there needing to be an inverse operation for modulus, there is the form: i = 3N where N is in Z, as a reverse of i%3 = 0.  If we had a solution of that form, we could plug in a value for N, presumably 0?
Alan Wolfe
CC BY-SA 4.0
2016-10-26T15:57:53.633Z
2022-09-20T17:38:27.043Z
2022-09-20T17:38:27.043Z
zackpudil
<p>If you want to warp the space as in the raymarching infinite spheres example,</p>

<pre><code>float SDF (vec3 p) {
    p = mod(p+3., 6.)-3.;
    return length(p-vec3(0., 0., 0.))-2.;
}
</code></pre>

<p>what you really need to do is <i>warp the space by turning it into a cyllindrical one</i> and what that means is that you need to make it so that if a ray hits a wall, the wall is like a portal to the wall opposite it and it comes out of the same point on the opposite wall. To do this you could just here is some pseudocode:</p>

<pre><code>Color trace (vec3 p, vec3 d) {
    if (intersects sphere) {
        *return sphere color with recursive tracing blah*
    }
    Plane frontwall = plane(vec3(0, 0, 3), vec3(0, 0, -1));
    Plane backwall = plane(vec3(0, 0, -3), vec3(0, 0, 1));
    Plane leftwall = plane(vec3(-3, 0, 0), vec3(1, 0, 0));
    ...
    *find closest wall intersection and then*
    return recursively traced color from opposite wall than the one that got hit
}
</code></pre>

<p>of note is that to do this enough times for it to look infinite, you would need like thousands of recursion iterations which would probably be slow and whatever language would probably not allow it, so your other option is to use some kind of a for loop since each portal/wall-intersection only requires base-1 recursion and therefore can be easily tranformed into an iterative process.</p>

<p>And instead of downvoting, I would request anyone who found my answer unhelpful to elaborate on how I could improve it; if someone really is interested in seeing a proof of concept of this in action then I will provide one but until that seems to be the case I will not waste my time.</p>

CC BY-SA 4.0
2019-12-02T03:21:28.800Z
This is true, and worth putting the answer up. The unspoken (sorry, I'll edit) goal is to find something not iterative.
Alan Wolfe
CC BY-SA 4.0
2019-12-03T13:32:53.537Z
oh. well in that case it might be worth looking into this thread about some strange connection between the projected cubic lattice, complex numbers, and polynomials https://twitter.com/stevejtrettel/status/1192827919447547905
cmarangu
CC BY-SA 4.0
2019-12-03T14:38:50.013Z
Very interesting!
Alan Wolfe
CC BY-SA 4.0
2019-12-02T02:46:49.847Z
2019-12-02T02:46:49.847Z
cmarangu
<p>One of the features of ray marching is that you can use modulus to repeat shapes infinitely, like in the image below, which is from <a href="https://www.shadertoy.com/view/MsBGW1" rel="noreferrer">https://www.shadertoy.com/view/MsBGW1</a></p>

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

<p>I was curious if there exists any technique which allows you to do the same thing with ray tracing instead of ray marching?</p>

<p>One method I do know of is to ray trace a plane from above, and then where you hit the plane, calculate your location on a grid on that plane, and use the relative position in that grid cell as an absolute position to raytrace a scene.  That will repeat the scene across the grid.</p>

<p>However, a problem with that is if your ray doesn't hit anything in the grid cell, and it would then enter another grid cell, this technique won't catch those other shapes without walking the grid cells down the path of the ray until it exits the back side of the grid, which is very ray-marching-esque and iterative.</p>

<p>Does anyone know of a technique that allows you to have ray marching type repetition in ray tracing?</p>

<raytracing><raymarching>
Is there a method to do ray marching style modulus repeat with raytracing?
Alan Wolfe
# Is there a method to do ray marching style modulus repeat with raytracing?

(Asked by: username_0 on 2016-10-06)

One of the features of ray marching is that you can use modulus to repeat shapes infinitely, like in the image below, which is from <https://www.shadertoy.com/view/MsBGW1>

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

I was curious if there exists any technique which allows you to do the same thing with ray tracing instead of ray marching?

One method I do know of is to ray trace a plane from above, and then where you hit the plane, calculate your location on a grid on that plane, and use the relative position in that grid cell as an absolute position to raytrace a scene. That will repeat the scene across the grid.

However, a problem with that is if your ray doesn't hit anything in the grid cell, and it would then enter another grid cell, this technique won't catch those other shapes without walking the grid cells down the path of the ray until it exits the back side of the grid, which is very ray-marching-esque and iterative.

Does anyone know of a technique that allows you to have ray marching type repetition in ray tracing?

**Tags:** raytracing, raymarching


---

## 2 answers

### Answer by username_1 on 2016-10-26. Score: 7

I've tried to figure this out as well, but I don't think there is a solution that is just as performant as tracing one sphere. Basically you would want to cast one ray to hit a infinite set of repeating spheres with no marching iteration. That would be awesome but is not possible: Let me explain.

# 

**Ray-tracing** is basically finding the analytical solution to this equation:
$$
f(\vec{r(t)}) = 0 \ for \ t
$$

Where $f(\vec{p})$ is the implicit surface function (which gives the distance the given point $\vec{p}$ is from the surface) and $\vec{r(t)}$ is the parametric ray equation ($\vec{r(t)} = \vec{origin} + \vec{direction}\*t$). $t$ would represent the distance along the ray the intersection happens.

In order to ray-trace domain manipulations (like repeating space), these domain manipulations need to be present in the implicit surface function $f(\vec{p})$ while finding an analytical solution. Let's take a 3D repeating sphere for instance.
$$
f'(\vec{p}) = \|\vec{p}\bmod \vec{d}\| - r
$$
(where $\vec{d}$ is a constant representing the grid size and $r$ is the radius)

In order to solve $f'(\vec{r(t)}) = 0$ for $t$ we would need the inverse of the modulo operator which doesn't really exist : [Link](https://web.archive.org/web/20171228030043/http://mathforum.org/library/drmath/view/51619.html). That means there is no analytical solution to this equation, but there is a numerical one. Hence **Ray-marching**.

# 

This is true for a lot of cool distance functions you see on shadertoy.com, the analytical solution does not exist for these surfaces so you cannot ray-trace.

> **Comments:**
> **username_0**: Thanks for the answer!  Interestingly, I have had some limited success at this, thanks to some ideas from someone on twitter.  I'm experimenting more to come up with some final info & a blog post, and will post an answer here with the details.  It's not a general solve, but it is still a bit interesting.  Maybe extensible.  Also had a friend mention he has been able to do this with other limited success by faking modulus mathematically, like using a badlimited saw wave.  I'll share some shadertoy links in the next comment for the curious.  
> **username_0**: Shadertoy from sebbi showing his idea: https://www.shadertoy.com/view/lly3Rc Me exploring and formalizing some things: https://www.shadertoy.com/view/MlK3zt Limited success for ray vs infinite layers of concentric circles: https://www.shadertoy.com/view/4tyGDK I'm currently working on ray vs infinitely repeating pillars. Looking promising so far. In the end these things are interesting but not in general very useful.  Maybe they can be extended. ::shrug::  
> **username_0**: Regarding there needing to be an inverse operation for modulus, there is the form: i = 3N where N is in Z, as a reverse of i%3 = 0.  If we had a solution of that form, we could plug in a value for N, presumably 0?  
> **username_2**: Couldnt you cheat a bit though. You could raycast on a cube and solve which parta of cube edge hits which sphere.  
> **username_3**: @username_2 if you were talking about cubemap stored infintie spheres that could work, but I devised an iterative process that works like light traveling through "portals" (mirrors except change position instead of direction) which gets the exact same image but using the classic recursive raytracing algorithm with imaginary objects.  
> **username_3**: As for the tracing onto a plane, using that as coordinates for infinite spheres, here is a little project I am proud of I have been plotting for months and is nearly complete which serves as an excellent proof of concept https://www.shadertoy.com/view/3lXXR8  
> **username_3**: that project basicallt traces onto a plane, and figures out which sphere has a chunk of itself in the checkerboard cell using floor(x), floor(y). Then it checks the 8 surrounding spheres (like conwats game of life) in case any of them has a chunk in that cell. Good luck figuring out exactly how I did it. Important thing is it runs 60FPS fullscreen, but maintains 100% accuracy for this fractal-ish shape  
> **username_3**: I saw the +4 views. It is unlisted. If you guys are interested in exactly the technique I used to accurately trace hundreds of spheres in realtime, or at least appreciate my work of art, feel free to contact me in comments somewhere or leave a like respectively! I know some of you have shadertoy.com accounts.  

---

### Answer by username_3 on 2019-12-02. Score: 2

If you want to warp the space as in the raymarching infinite spheres example,

```
float SDF (vec3 p) {
    p = mod(p+3., 6.)-3.;
    return length(p-vec3(0., 0., 0.))-2.;
}

```

what you really need to do is *warp the space by turning it into a cyllindrical one* and what that means is that you need to make it so that if a ray hits a wall, the wall is like a portal to the wall opposite it and it comes out of the same point on the opposite wall. To do this you could just here is some pseudocode:

```
Color trace (vec3 p, vec3 d) {
    if (intersects sphere) {
        *return sphere color with recursive tracing blah*
    }
    Plane frontwall = plane(vec3(0, 0, 3), vec3(0, 0, -1));
    Plane backwall = plane(vec3(0, 0, -3), vec3(0, 0, 1));
    Plane leftwall = plane(vec3(-3, 0, 0), vec3(1, 0, 0));
    ...
    *find closest wall intersection and then*
    return recursively traced color from opposite wall than the one that got hit
}

```

of note is that to do this enough times for it to look infinite, you would need like thousands of recursion iterations which would probably be slow and whatever language would probably not allow it, so your other option is to use some kind of a for loop since each portal/wall-intersection only requires base-1 recursion and therefore can be easily tranformed into an iterative process.

And instead of downvoting, I would request anyone who found my answer unhelpful to elaborate on how I could improve it; if someone really is interested in seeing a proof of concept of this in action then I will provide one but until that seems to be the case I will not waste my time.

> **Comments:**
> **username_0**: This is true, and worth putting the answer up. The unspoken (sorry, I'll edit) goal is to find something not iterative.  
> **username_3**: oh. well in that case it might be worth looking into this thread about some strange connection between the projected cubic lattice, complex numbers, and polynomials https://twitter.com/stevejtrettel/status/1192827919447547905  
> **username_0**: Very interesting!  

---

