March 12, 2019

Raymarching and fractals in three dimensions

Raymarching is a variation of raytracing in which the length of steps for each ray is not constant. This technique is well suited for fractals in three dimensions because we can easily define distance estimators in folded space and expoit them to efficiently render complex details. This article describes briefly how this works, and presents several examples of fractals I have produced.

Ray marching and distance estimation

Our shader must determine the color of every pixel in our image, based on any object it hits as well as relevant lighting effects. Depending on the projection matrix, each ray will propagate from the camera location in some forward direction. The length of each step is determined by a distance estimator. In the simplest case of a unit sphere at the origin, the distance estimator would be

for a given vector v with x, y and z components. With multiple spheres, the distance estimator would then be the minimum of several, as it is the longest distance the ray can safely march without passing through an object.

Lighting, specular highlights and ambient occlusion

Once the estimator is zero (or sufficiently small), the ray has hit an object. The object normal is then estimated by comparing the distance estimation for several points near the impact. We can then use the normal and a diffuse color to render simple lighted spheres, as shown below. The diffuse color in this case changes slightly depending on position.

The normal and projection matrix can also be used to render a simple specular highlight, as shown below. The result is a classic phong lighting model.

Another advantage of raymarching is cheap ambient occlusion. Due to the nature of raymarching, complex surfaces will take more steps before the ray hits a surface. This is obvious if you consider the distance estimator an unsure estimate which is more uncertain when complex shapes are nearby. We can therefore use the number of steps taken as a rough ambient occlusion estimate. This is better shown on a slightly more complex shape as below.

You can see details much more clearly on this shape due to the ambient occlusion. Notice the left side especially, where the method generates quite convincing shadows. This model was generated using a cube distance estimator, and multiple spatial folds and rotations.

Spatial folding and rotation

In order to generate more complex shapes, we use folding and rotation. The general idea is to mirror or rotate the ray vector around some axis. There are several variations of these functions which I will not explain further in this post. The below illustrations do however visualize the general concept quite well.

Figures: (1) folding space between Z and Y axis, (2) rotating space on X axis

By combining a long sequence of spatial folding and rotation operations, beautiful and complex fractals can be created. Below is an example of this (somewhat large gif, may take a while to load).

Fractal Gallery