Welcome everyone to a new year and a new tutorial. This one will be about creating the Sierpinski Triangle. The tutorial is available in two flavours: one for the math averse and one for math lovers. This is the tutorial for the math averse; whether you hate math are anxious about maths or have trouble applying it in practice the logic behind this tutorial does not require you to have any understanding of math. However, that does not mean it is math-free! But don't be alarmed, you only need a basic understanding of arithmetic(addition, subtraction, multiplication etc.)
What you will need to follow along with this tutorial is an install of Blender and Animation Nodes. I used Blender 2.81 and Animation Nodes with Extra Nodes.
The approach we are going to be using to create the Sierpinski triangle fractal can be expressed with the following algorithm:
- Create a triangle
- Scale it down to half size
- Create copies at each vertex
- Repeat steps 1-3 with new triangles
I discovered this algorithmic approach for creating the Sierpinski triangle fractal (ironically) by doing the math and found that the centres of each of the three new 1/2 scaled triangles connected together formed another 1/2 scale triangle. The following image shows this approach applied in practice. Note, that I have split my network into two networks: one showing how to link the iterations together, and another for the loop sub-program.
Here you can see a Sierpinski triangle of three iterations in the viewport. The yellow box is the input object that we string into our loop subprogram. It starts with a Unity Triangle Mesh generator output to a Mesh Object Output node. We compose a matrix with the scale that we want our original triangle to start with. Here I have set it to 10. Then we take the mesh object and our matrix and combine them with an Object Matrix Output node which we then have to make into an Object List because that is going to be the key input type for our loop subprogram.
The pink boxes are where our iterations are being created. Note that a limitation of AN is that you cannot use an Object Instancer inside a loop. So the three things were going to need to grab from our loop to generate our triangles are the number of instances, the scale and location of each triangle. There's nothing particularly unusual happening in these 'Iterations' boxes but I have provided an explanation below for anyone who is unfamiliar with Animation Nodes.
The Get List Element nodes convert the 'instances' and 'objects' from lists to a single integer and object which we can then plug into our Object Instancer which won't output properly without an Object Transforms Output node with our location, scales and objects plugged in. The Object Visibility Output is there to easily hide the objects you don't want to see. I probably could have devised a way to delete the objects we don't want after running the iterations but this was the easiest and quickest solution.
In this second image we have the loop that is the main logic for our node network. We take an object list as a new parameter, which we expect to be a list of triangle objects. The first string in our loop calculates how many instances of the half-scale triangles we will need to create, which is the same as multiplying the number of objects in our list by three. We can output this number with a New Generator Output set to Integer List(Generator output always seems to want lists). The rest of the loop is where the real work is happening. We get list objects by index using the Get List Element node, we put that into an Object Transforms Input so that we can grab the object's scale which we then multiply by half using a Vector Math node. We output this new 1/2 scale value with a new generator output set to Vector List. So far we have our number of instances and the scale but we still need to find the locations of the triangles we want to generate.
To do this, we plug our element from the Get List Element node and our scale we calculated with the Vector Math node and plug these into an Object Transforms Output node. This changes our original triangle into that half of that size so, according to our algorithm, we need its vertices. To get them we connect a Mesh Object Input node to our Object Transforms Output node. Create a New Parameter in the Loop Input node and set it to vector list. This will create an empty vector list which we can plug into a Combine Vector List node with the vertex locations list from the Mesh Object Input node. We take this combined vector list and reassign it to our vector list parameter in our loop input because this list will change as our loop indexes through each triangle in our object list input. You may need to select the Loop Input node, press 'n' on your keyboard to bring up the properties tab, go to 'item', scroll down to 'Vector List' and click on the 'Reassign' button to get the correct node.
And that's it, that's the loop. Go back to the first image to see how to string the subprograms together. Note that you don't have to break the network up into two networks, I have just done this for organisational purposes and because it's easier to see how something is working if it's broken up. You could spice up your Sierpinski Triangle by creating some animations in AN, extrude the triangles, maybe bevel them a bit and add some materials. If I ever get around to rendering something out with this I will post it on this site.
In the meantime, I am working on creating other fractals like this in 2-D and 3-D, so stay tuned. As always, if you have any questions, comments or suggestions you're welcome to email me. Stay safe everyone. :)
- Kit