Experiments in Generative Geometry

Building Meshes in Unity, Vertex by Vertex

By Alexander Pech (Website)

I'm an embedded software engineer from 9 to 5, but in my spare time I like to dabble with procedural geometry. Dissecting things into the smallest possible components and making them work for me is my jam, whether it's manipulating the registers of a CPU or finding new ways to coerce vertices and polygons into useful game assets.

Off and on in my spare time I've been jumping into Unity and, using the built-in Mesh class, just seeing what I could come up with. I started with the basics of programmatically placing vertices and triangles. Then I moved on to basic primitives like cubes, cylinders and spheres. Using these methods as building blocks I gradually progressed to more complex and organic shapes.

A particular area of focus for me has been trees. Trees lend themselves easily to this kind of algorithm for a number of reasons. The branching structure lends itself nicely to a recursive or iterative algorithm, similar to a fractal. The topology of a branch is fairly simple, really just a tapered cylinder. Also, all trees are unique but follow similar patterns, making them ideal candidates for creation via a set of rules plus randomness.

My basic procedural tree structure is a cylinder with some function controlling the direction of growth and radial thickness. I initially tried controlling branch direction via applying incremental rotations around a random axis. This gave a nice spiralled or twisted look to my branches but turned out to be quite difficult to control. My current solution uses spline curves. This gives me the control to generate a series of spline points however I want and then smoothly ‘grow’ the branch along the curve. I’ve also experimented with more physical based models, like growing towards a light source and applying gravity.

The radius function, even though it has less influence over the general shape of the tree, tends to be more complex. I like taking into account the angle of the current vertex as well to produce a bit of irregularity. This is particularly useful around the base of the trunk, where you don’t always want a perfect cylinder. So, here you end up with what is essentially a two-variable function for your branch, defining the radius at any particular point along the length and radial direction.

If this is confusing, here is the picture I have in my head when the geometry is being created. You start with a spline curve and at the start of the curve you draw an arrow perpendicularly out from the curve. The end of the arrow points to your first vertex. Then, you start to move the arrow up the curve while also rotating it, forming a spiral. The length of the arrow is controlled by our radius function. As it spirals up you periodically place a vertex at the tip of the arrow and connect to the surrounding ones. This is the basis for the current version of my algorithm. (You may have noticed the topology of these branches aren’t exactly the same as cylinders as they have a single edge that spirals all the way along. Visually there is very little difference, but it gives me greater control because I don’t need to place vertices ring by ring.)

From here I hope to keep building upon my toolkit, gradually producing more complex and interesting shapes and also making the meshes produced efficient and scalable so I can generate whole forests. This was really just an introduction into the methods I’ve used, as a lot of technical challenges have presented themselves along the way. Generating different levels of detail was a big one. At one point I threw out everything and started again just so I could have control over this aspect right from the start! But now I have a friendly ‘polygon density’ slider that does all this for me.

This has been a fairly technical explanation, but more than anything I’m inspired by the biological and evolutionary processes that created this life in the first place, so I hope to continue incorporating aspects of that into my models. Even though right now I’m just creating some funky geometry, there is a future vision that this may result in generating not just meshes, but functional environments with ecosystems and an evolutionary history. This will hopefully be just one piece of a vaster world-building engine.

For now, I hope my experiments find some value with others. You can find some of my work at my itch.io page https://rednax.itch.io/ (including some random unrelated game jam projects). Hopefully plenty more to come!