Crafting Nature in Code: My Journey with Procedural Plant Generation in Unity

During a weekend, I took on the challenge of creating a procedural plant generation tool in "UNITY", and I’m thrilled with the results. It was a satisfying experience to see my efforts come to fruition. 


1. Approach to Procedural Mesh Generation: 

I started with no idea how to approach this. I knew I needed to calculate vertices and faces to create a mesh, but my math skills were limited. So, I relied on animation curves, which turned out to be amazing.




2. Calculating Vertices with Curves: 

I began Calculating the vertices of the leaf using two curves. One curve was for calculating the shape of the leaf, and the other was for the bend of the leaf.



3. Face Calculations: 

Understanding how Unity draws triangles took some time. It was frustrating, but I eventually figured it out and was able to create a mesh with vertices and indices.



4. Leaf Shape and Reshaping: 

I successfully made the leaf shape, it's easily reshapable just by tweaking curves and parameters.





5. Adding Gradient: 

I got bored of looking at a simple leaf, so I added a gradient and lerped the values to vertex colour based on subdivision count. It was fun to see the leaf in multiple colours.





6. Creating Multiple Leaves: 

I asked AI to update my code to support the rotation of the mesh based on a given angle. It updated my code to calculate vertices based on the angle in seconds. I also added another layer to combine multiple groups of leaves into a plant.




7. Normals and UV: 

The UV part was tricky. I had to scratch my brain to figure out how I could map the vertices' position in 0-1 UV. This required learning more math for me.




8. Shader:

 Finally, I wrote a shader that draws a midrib and moves the vertex with some noise and sin().




My weekend’s work on Unity’s procedural leaf generator is commendable. Despite the math challenges, I’ve crafted a tool that not only shapes leaves but also infuses them with life through colours and movement, reflecting a blend of creativity and technical prowess.






Comments

Popular Posts