Welcome everyone to 2021 and a new tutorial. We're continuing the fractals series with 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 those who are comfortable with math, the logic behind this one is based on calculating midpoints so if you don't understand what that is, go to the other. There is also a more complicated handling of lists with Animation Nodes(AN) in this tutorial.
What you will need to follow along is an install of Blender and Animation Nodes. I used Blender 2.81 and Animation Nodes with Extra Nodes. 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 all. 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. We also create a list filled with (0, 0, 0) vectors to store the co-ordinates of our generated triangles.
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 we're 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, just string them together and duplicate them for however many iterations you want. I have provided an explanation below for anyone who is unfamiliar with AN otherwise, skip the next paragraph. :)
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. Note that the object list output is the same as the input(the option to display the output is in the properties tab when selecting the Loop Input node). 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. The Repeat List node triples the size of our zero vector list for each iteration.
This second image shows the second network, LoopTree, which contains our loop subprogram named My Loop by default. We start by adding a New Iterator and setting it to Vector List. We also make a New Parameter for our Object list. Pipe the object list into an Object Transform Input and create a Vector from Value set to 0.5. Plug this vector and the scales output of the Object Transforms Input node into a Vector Math node set to multiply. This will give us the half-scale value that we need for our generated triangles. Create a New Generator Output, set to Vector List in the drop-down menu, and feed the Vector Math result into it so we can use our scale vector later. To get our number of instances is easy since it is equal to the number of iterations our loop will take. Create a New Generator Output set to Integer List and pipe the Iterations from Loop Input into it.
Finally, for the location vectors, we start by getting the current object in our object list by connecting a Get List Element node to it as well as the Index output of our Loop Input node. To calculate the locations of each half-scale triangle we need to find the midpoints between the current object's polygon center and each of it's vertices. We can grab the object's center and vertices by connecting the list Element to a Mesh Object Input node. We then have to calculate using the midpoint formula by first adding the Vertex Location and Polygon Center together with a Vector Math node, and then using another Vector Math node to either divide by 2 or multiply by 0.5. We then replace the zero vector value in our vector list by using another Vector Math node to Add the new value to the list. We then pipe the result of the Vector Math (Add) out of the subprogram by creating another New Generator Output set to Vector List in the drop-down menu.
And that's everything! 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 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 or comments feel free to email me. Thank you to anyone who has already, I really appreciate the feedback. Stay safe everyone. :)
- Kit