Fractal Generator



Abstract

I used this project as an opportunity to explore the fascinating rendering method of Ray Marching and Compute Shaders.

The resulting application generates a threedimensional Mandelbulb fractal at runtime, complete with parameterized shape and color.
Additionally, a few basic camera controls allow for an in-depth exploration of the complex fractal shapes.

Details

Production 08/2020
Time 1 week
Engine Unity

Ray Marching

Ray Marching is a path tracing algorithm commonly used for volumetric image effects and for rendering objects based on signed distance fields.
For the latter purpose each screen pixel fires a ray into the camera‘s frustum and periodically calculates the closest distance to the scene objects along that ray. When the returned distance is extremely close to zero it is assumed that the ray intersected with an object.
Using information from the object‘s material as well as from the marching process itself, e.g. the distance and marching steps, the respective pixel is given a certain color.

A multitude of threedimensional primitives have a signed distance function that accurately returns the closest distance between their surface and a given position in space.
In the case of the Mandelbulb however, calculating the precise distance to its divergence border is impossible due to its fractal nature. Because of this, the algorithm calculates the distance only up to a certain number of iterations, effectively returning a sophisticated estimate.

Running hundreds of complex calculations per pixel per frame consecutively on the CPU wouldn‘t suffice for a realtime application, but that is where compute shaders shine.
After receiving the necessary parameters from a C# script, a compute shader runs the fractal distance estimation on multiple threads in parallel on the GPU and outputs the pixel colors onto a render texture.


Distance Calculation

A multitude of threedimensional primitives have a signed distance function that accurately returns the closest distance between their surface and a given position in space.

In the case of the Mandelbulb however, calculating the precise distance to its divergence border is impossible due to its fractal nature.

Because of this, the algorithm calculates the distance only up to a certain number of iterations, effectively returning a sophisticated estimate.

Compute Shaders

Running hundreds of complex calculations per pixel per frame consecutively on the CPU wouldn‘t suffice for a realtime application, but that is where compute shaders shine.

After receiving the necessary parameters from a C# script, a compute shader runs the fractal distance estimation on multiple threads in parallel on the GPU and outputs the pixel colors onto a render texture.


Parameters

Complexity the power n in the Mandelbulb equation z = zn + c
Detail the number of iterations that the equation is run before returning a result
Accuracy the maximum number of distance estimation iterations along a single ray
Base Color tints pixels based on the number of iterations before the equation result diverges from the set bound of 2
Glow tints pixels based on the number of marching steps performed on the pixel‘s ray