Generate Random Bushes, Rocks, and Trees in Minutes

William McGonagle
5 min readNov 23, 2020

There are a ton of tutorials about how to create virtual 3d terrains, I even wrote one yesterday. But, I have yet to find any tutorials that will show you how to populate that terrain with procedural plants, rocks, trees, and some other fancy stuff. All of these plants will be built using a moldable and easy to use architecture. Which, will allow the designers on your team to quickly prototype changes to the vegetation without bugging you. This will all be done in Unity, but like my other tutorials, you can easily port the code to any other language. So, without further ado, let’s get right into it!

This will be a tutorial series; in this part, I will show you how to get the systems set up so you have a jumping-off point for future tutorials. In the next couple of articles, I will walk you through how to get the plants, rocks, and trees generated.

Building the System

First, we have to create a new class known as “VegetationObject”. This class will work as a base class, or in layman’s terms, an example for other classes that can be modeled after it. This class will contain functions and variables, that child classes will inherit automatically. This can simplify our life dramatically because it allows for much more modularity with the different pieces of vegetation that can be created.

Inside of the VegetationObject class, we are going to declare a variable and a couple of functions. We need a string named VegetationName; this string will be used to simplify organization for the designers who will be creating many different objects and will need to keep track of them. On top of that, we need a couple of different functions- I’ll go over them below. The insides of the functions are just placeholders for the child classes that we will create in future tutorials, so please note that they will only generate a placeholder plane with a random color. When you set up the functions, remember that you need to make them virtual, so that the other classes can override them.

GenerateMaterial

The GenerateMaterial function will take in an integer as an argument, and output a Material. The integer represents the index that the material will be when rendered on the object. This will allow our vegetation to have multiple materials- something useful in the case of trees where there are two distinct parts.

For other types of vegetation, this will not be necessary, but this function can still be utilized to increase the flexibility of the overall architecture- something that this project is striving to do.

The code for this function will be shown below- it will just generate a default material for the plane generated from the GenerateMesh function. So, the programming for the function is fairly self-explanatory. First, we create the material from a blank shader. We want the colors on the cube to be randomly generated, so we need to set the material’s color parameter to a new color with a random hue.

GenerateMesh

For the GenerateMesh function, we don’t need to take in any inputs, but we will need to output a new Mesh. This new mesh will be what the computer renders to the screen, with the material from the previous function applied.

For our example class, the mesh will just be a basic 3D plane, so the code for it will be fairly static. Just to spice things up — we will be generating a random height and width for the planes so that they look at least slightly interesting.

The code is shown below and requires a slight amount of explanation. First, we are generating the width and height of the cube using the Random class. Then, we generate an array of points and an array of indices. We set the value of both of those using our knowledge of mesh generation in Unity (if you don’t understand how it works, watch this great tutorial series). Then, after we have generated the points and indices, we will set them up on a new mesh, and return the new mesh as the output of our function.

That’s it! (For Now)

That’s all that I have for you today, but tomorrow I am planning on showing you how to generate bushes. In the following few days, I hope to cover rocks, trees, shrubs, and other plants.

To get notified when I release those articles, follow my account on medium, and give this article a clap. You might not realize how much it helps, but it helps a ton. So, until then, thanks for reading this far, and have a splendid day!

--

--

William McGonagle

Bridgeporter. Georgetown Student. Bass Player. Programmer. Brother. Celtics Fan.