# Is there a method to do ray marching style modulus repeat with raytracing?

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

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?

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 : 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.

• 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 Oct 26 '16 at 17:30
• Shadertoy from sebbi showing his idea: shadertoy.com/view/lly3Rc Me exploring and formalizing some things: shadertoy.com/view/MlK3zt Limited success for ray vs infinite layers of concentric circles: 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 Oct 26 '16 at 17:34
• 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 Oct 26 '16 at 19:46