top of page

Menger Sponge Fractal in UE4 C++

The Box class needed a function that would create a numerous amount of little boxes within itself.

To do this, an iteration over the 3 dimensions of a cube was necessary. Using the three dimensions, it was possible to offset each smaller cube into the right segment of the original cube. The Else statement here was some experimentation to adjust the direction of the fractal, so it actually inverted the effect, due to the signs in the statement.

I was researching into other areas of programming, that could be pretty fun, I stumbled upon Daniel Shiffmans YouTube page where he does a bunch of graphics programming using variations on Java.

 

I figured doing something similar but in Unreal Engine 4, would be an excellent challenge.

In order to create this fractal you only really need two things, a box class, and a class to manipulate boxes with.

Here are some small utility functions to help me visualize what I was creating. Mainly changing mesh and alternating colours between the recursions

Now into the Fractal Class, to start the chain off, make a box in begin play and set the timers going, for the number of times you want to iterate over. In Unreal anything beyond 3 iterations, causes severe stress on the CPU, due to the ridiculous amounts of boxes it creates

The function to create a box was very simple, spawn a box class object, then adjusting its size

This function was named MousePress, since it was going to be done on click, but i figured easier to delay it.

Anyways, this is where the actual self dividing occurs, using the Generate function from within the box. So the original "Full Box" calls generate, which subdivides itself, and that returns all the mini boxes using a TArray structure, then some simple maths to change colour every other iteration.

Finally. the function clears up all the holes that are required for that version of the fractal

bottom of page