Fractal Generation with Unity

L-system fractals

A was hanging out with my awesome friend Anatomecha in one of Seattle's new super awesome, super hip, super gentrified edison-bulb-and-a-million-beers-on-tap bars. He showed me a system he had been working on to create l-system fractals with Unity using very simple properties of how Maya and Unity represent 3D models in 3D space.


I like beer and fractals because they are both awesome, what can I say?

For the uninitiated an l-system fractal is basically where you take a shape, lets call it an "axiom", then you replace the axiom with another shape, let's call that a "generator" which just so happens to be made up of a bunch of axioms.

Rinse and repeat until you have created a self-referential fractal of pure awesomeness. Each rinse and repeat is called an "iteration."

Would you like to know more?

So axiom:

which, in the cases of these pyramids creates what's called a Sierpinski Pyramid.

For those of you FRACTAL EXPERTS out there there is nothing new yet.


Here is the awesomesauce.

Maya will let you create a model which contains named compenents in a hierarchy. Unity can import the model and preserve the hierarchy and the components. If you create a hierarchical model in Maya which is meant to be a generator, and you pick some of the components in that model's hierarchy to be axioms, then when you load it into Unity all you have to do to make an l-system fractal iteration on it is to find the leaf nodes and replace them with freshly instantiated generators.

Piece of cake right?

Essentially, yes.

Note there are always details. The position, scale, and rotation of the models needs to be preserved. Notice how the Sierpinsky Pyramid replaces a large pyramid with four smaller ones at various offsets?

Yes, these offsets and scale factors (and rotation as well) can be encoded in the Maya model trivially which will be explained now.

Maya representation of a generator

So we are going to create this:

By creating the following two generators:

Left: end branch, right: middle branch.

We then load them up in Unity and create a script that iterates n times over a GameObject looking for leaf nodes with a name that corresponds to the name of a generator.

Then when you find one, you Destroy it and replace it with a newly Instantiated instance of the generator of that name.

So have a look at what this looks like in Unity, with an attached script:

Notice the Transform in the Inspector. It contains the relative position, rotation and scale of an axiom.

This means when you replace that axiom with a newly instantiated generator you will need to set the generator's Local Scale, Local Rotation and Local Position to these values so it will be in the right spot.

Note the script has two generators listed, the end_branch_generator and middle_branch_generator. These are references to the models in the project, so during an iteration when a node is found with a name like end_branch__0 the LSystem.cs script knows to replace it with a end_branch instance.

You may notice the leaf nodes have names like end_branch__0 with a double underscore - this is because Maya requires unique names for it's scene nodes so the script simply discards the __ and everything after it during the iteration process.


So there is a lot you can do with this.

Essentially you can build a model in Maya, name nodes, and then replace those nodes with other models that have the same name (or, if you prefer, by any more sophisticated replacement algorithm you choose) to recursively create and define a more interesting creation.

Use your imagination and think on that for a minute, it's pretty huge.

Have fun!