Since I’ve been on a catlikecoding.com kick, and I happen to have the day off due to a furlough at my day job (hopefully there won’t be too many more of these furlough days off!), I figured it was a great time to jump into the next tutorial.

The next project departs from the previous two lessons on complex mathematical formulae in Unity and jumps over to the programmatic creation of fractals. Now, when I think of fractals, I think of the trippy visuals that always end up looking like a bad trip from the ’70s, or some sort of organic structure.

Of course an actual fractal is more robust and complex than just a trippy visual, and are actually a geometric and mathematical concept:

a curve or geometric figure, each part of which has the same statistical character as the whole. Fractals are useful in modeling structures (such as eroded coastlines or snowflakes) in which similar patterns recur at progressively smaller scales, and in describing partly random or chaotic phenomena such as crystal growth, fluid turbulence, and galaxy formation.

The Oxford Dictionary

So essentially, a fractal can be simulated by creating a repeating pattern at smaller and smaller scales (within a defined range). I could definitely see how fractals might be used in game design, even in less programmatically live applications like map modeling and such.

Let’s jump in and see how we might be able to model a simple fractal in 3D space in Unity!

Project Details

Project Goal: Expand on the creation of mathematical functions in Unity by generating functions in complex shapes and surfaces
Tutorial Link: Mathematical Surfaces at catlikecoding.com
Concepts Covered: Programmatically instantiate GameObjects, use recursion and coroutines, and use principles of randomness

Rendering a 3D Fractal

After spinning up a new Unity project and setting some things up, the tutorial shows how to start by instantiating new GameObjects at runtime and use some basic fractal setup to create a very exciting fractal:

Woah, slow down there. This is a pretty trippy fractal!

Now, obviously the image above is a little less… fractal-y than one might expect. That’s because the initial version of the code does generate a depth of GameObjects, but it’s not currently making any of the child objects actually look differently.

With a couple more tweaks, we get something that starts to look a little more like what we’d expect from a fractal:

A really… boxy fractal, at least

Now that we’re cooking with gas, we can start to make some additional enhancements to the code to build out more shapes in more directions. The tutorial really walks you through a relatively complex (for a beginner) concept and helps make something pretty neat.

The only issue I ran into was not updating the number of Meshes in the inspector, so my program kept rendering a blank window.

Lessons Learned

Building out fractals is a pretty neat trick, though this tutorial mostly helped reinforce some basics, especially around the use of IEnumerators / coroutines to allow for the dynamic execution of code without it being explicitly tied to an Update() or FixedUpdate() method. These IEnumerators are a powerful tool that I have yet to dive too deeply into, though there are already game mechanics from some of the other projects that I know they would solve problems with.

The Final Product

Note: This project is embedded using Unity‚Äôs WebGL player, which is not supported on mobile.

View the source for this finished project on GitHub

Leave a Reply