## Raymarching Toolbox Thread

**category:**code [glöplog]

Thanks!

This works like a charm, and I ended up with a nice tunnel of spheres.

This works like a charm, and I ended up with a nice tunnel of spheres.

Is it possible to make an object that is repeated using the modulus repeat with an offset?

My intuition is no, but maybe I am missing something.

Like for instance, a square being repeated, but it's height being modulated by a sine wave for each iteration.

Am I missing something silly?

My intuition is no, but maybe I am missing something.

Like for instance, a square being repeated, but it's height being modulated by a sine wave for each iteration.

Am I missing something silly?

You just get a constant value for each "cell" the sphere is in, and use that for the sphere height. You can get that constant value easily, e.g. if you're modulating the repeat with a value of 1, it's just "value = floor(pos)". You can hash that or use a sin or whatever.

This will yield you many graphical bugs, also known as artefacts.

Because you will get a closest distance (to the grid of cells) of f.e. 0.7 and so you step 0.7 on your ray, 0.5 of those into the next cell...where a long cube resides, which was only 0.2 away, but because we got the info of 0.7 away in the last cell we just went 0.5 into its body.

The only workaround is to not walk the full distance, i.e.:

"rayPosition += rayDirection * ( distance * 0.25);".

You may want to only walk fractions of distances for just the objects you are using such techniques for (or which yield artefacts in other words), so you should not globally add the fraction of a distance, but do it in the return of your map()-function already instead, i.e.:

"return min(X, 0.25 * artefactObjectDistance);", where X=other distances.

or like this:

"return min(X, 0.25 * Cube(p, float3(1,1 + 0.5 * sin(floor(pos.x) * 0.1 + time),1));", where X=other distances.

Because you will get a closest distance (to the grid of cells) of f.e. 0.7 and so you step 0.7 on your ray, 0.5 of those into the next cell...where a long cube resides, which was only 0.2 away, but because we got the info of 0.7 away in the last cell we just went 0.5 into its body.

The only workaround is to not walk the full distance, i.e.:

"rayPosition += rayDirection * ( distance * 0.25);".

You may want to only walk fractions of distances for just the objects you are using such techniques for (or which yield artefacts in other words), so you should not globally add the fraction of a distance, but do it in the return of your map()-function already instead, i.e.:

"return min(X, 0.25 * artefactObjectDistance);", where X=other distances.

or like this:

"return min(X, 0.25 * Cube(p, float3(1,1 + 0.5 * sin(floor(pos.x) * 0.1 + time),1));", where X=other distances.

The "0.25" was just a random number...you want to finetune it, until you don´t get any artefacts anymore and of course being as high as possible (while not above 1), because this means many more calculations per pixel of course, the lower this number gets.

I think for a field of different y-sized cubes the 0.25 could even work, but i think it needs to go even lower! :(

I think for a field of different y-sized cubes the 0.25 could even work, but i think it needs to go even lower! :(