A Custom Flower Generator Using Dynamic Meshes In Unity

Recently I had some free time and had the idea to make this. It’s my first time dynamically creating a mesh in code.

Vincent Taylor
Sep 9 · 3 min read
My custom flower generator with 2 petal layers

Design and Inspiration:

After watching a few YT videos by Brackeys and Code Monkey, I had a general idea how to create the meshes.

From there I just trial-and-error-ed my way to create the petal, then dynamically duplicated it in 360 degrees using some bullet hell code from a recent project.

After that, just duplicate everything for the 2nd layer of petals.

The generator uses 3 Animation Curves from Unity to control the width, height, and depth/length of each petal. The width and height of each vertices in the petal is calculated based on how far, from start to end, along the depth the vertices is.

The code:

CreateShape():

Don’t ask why I did it that way. ¯\_( ͡• ͜ʖ ͡•)_/¯

Point being, that I can change the detail level with relative ease.
This dynamically creates and positions the vertices in the petal. The first point then all the mid-points, then the end point.
I couldn’t think of a way to automate the creation of the triangles used to render the shape, so I just wrote them out by hand.

UpdateMesh():

This clears and updates the mesh each frame, to allow for runtime modification.

Start():

Initialize the two layers of petals, duplicating and rotating each based on the number to spawn per layer.

Update():

Every frame, recreate the mesh and update the mesh renderer.

The result is a pretty good looking procedural flower generator; Easily customized to almost any requirements.

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Vincent Taylor

Written by

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer since 8yo (18 years). Currently looking for games industry employment

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.