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.

Best.

Night.

Ever!

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, and generator:

L-system fractal axiomL-system fractal generator

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.

Awesomesauce

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:

L-system fractal tree

By creating the following two generators:

L-system fractal end branch generatorL-system fractal middle branch generator

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:

L-system fractal tree setup in Unity

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....

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.

L-system fractal tree iteration 0L-system fractal tree iteration 1L-system fractal tree iteration 2

Have fun!

Read More

Welcome!

Hi, thank you for visiting! I'm David York, a San Francisco software engineer.

My passions include game development, cooking, photography and travel.

I work as a mobile (iOS and Android) consultant and indie game developer.

Say hello, I love mail!

david@davideyork.com

linkedin.com/in/davideyork/

© 2021 David York