
Procedural Generation in

Source: Google
CWBLOG is a blog on the study and practice of procedural generation in the Unity engine. The purpose of this blog is purely academic and will only serve as a means to share knowledge with the blog creator's peers.
​
In this blog, through a mixture of text and video, we will learn about multiple aspects of procedural generation. It will include what procedural generation is, its history, its current day uses, what Unity is and how it relates to procedural generation, the procedural generation technique that we will continue researching, and in the end, a working prototype.
Part 1:
What is procedural generation?
Procedural generation, at first, may sound like a complex thing to understand, but in its simplest terms can be a very easy subject to grasp. However, don't let that fool you on how complex and in-depth it really can be.
​
In the most basic terminology possible, procedural generation can be described as the automatic creation of data by a computer using algorithms instead of manual input. That data can be either as consistent or as random as desired. Procedural generation is not tied to any specific coding language or program, it is simply a technique in coding to make a computer produce it's own data.
​
A good example of basic procedural generation is a program that generates a username for you, as opposed to the user creating the username themselves.
This creation of data can take just about any form, from the creation of things as simple as random string generation and random number generation, all the way to complex visual scenery or audio generation.
​
This autonomy obviously doesn't come from nowhere though. Developers create the framework and assets for procedural generation and then create algorithms to set the specifications and restrictions for the automatic generation of the data before it can commence.
The history of procedural generation & its earliest uses
Procedural generation has been around longer than you probably think. The idea of procedural generation has existed since around the 1940's, when RAND Corporation created one of the first random number generator machines.
​
​
​
​
​
​
It was not until 1951 when random number generation was formalized for consumers with the Ferranti Mark 1; the worlds first commercial home computer.
​
​
​
​
​
​
The use of procedural generation, such as random number generation, was limited for many years as there was not yet much need for such primitive uses of it. It was very experimental at this time and the unpredictability that often comes with procedural generation was viewed as a negative due to the desire for consistency with early computers. It was not until the late 1970's when procedural generation really started to get experimented with in very early video games.

The RAND Corp random number generator

The Ferranti Mark 1
Source: Google
Source: Google
Procedural generation today & its uses
Today, we have a lot more power in terms of processing power on commercial computers. This allows users to be more creative and innovative through the use of procedural generation.
Procedural generation is used in many industries in a variety of ways, but most of the time now, it is talked about when regarding one of the following topics;
​
-
Various random value generation such as passwords, usernames, test data, etc.
-
You might see this used when a website randomly generates a username or password for your account. It is also seen when creating large sets of test data with random names, values, etc.
-
-
Animation generation
-
You might see this used in the animated film industry or likely in video games. This can allow for a much more natural look when creating animations.
-
-
Audio generation
-
You might see (or hear) this in the creation of electronic style music. Music creators can use procedural generation to create unique and algorithmic based pieces instead of manually writing it themselves.
-
-
3D model generation
-
Similar to animation generation, you might see 3D model generation in film or in video games to create vast and unique landscapes or objects without the work of having to make it all by hand.
-
-
2D Graphic generation
-
Very similar to 3D model generation but 2D. You might see this in modern art or in video games.
-
-
Video game systems and mechanics
-
Perhaps one of the most popular fields that relates to procedural generation. It is widely used to create mechanics in video games that offer long term replay-factor or large scale uniqueness and unpredictability.
-
​
​
​
​
​
There are of course many other uses for procedural generation that will not be covered in this blog, but the above listed will be our main areas of focus.

In the space exploration video game No Man's Sky, there are literally 18 quintillion unique planets you can explore. This was done with procedural generation.
Source: Google
Example of basic procedural generation
Later on in this blog we will go into more depth on procedural generation in the Unity Engine. In the mean time we will be exploring the understanding and concepts of basic procedural generation by creating a random string generator in the following video tutorial.
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
As you can see from the console output in the video, it can be quite useful to have the computer procedurally generate data for us! If we were tasked with creating a list of unique usernames, we now have one and we didn't have to create them all individually ourselves! It can help save time in a lot of ways when requiring or working with massive sets of data, it also helps give our data a bit of unique randomness.
​


In this small application from the video, we;
​
-
Create three small sets of data for our random generation to use
-
Ask the user for how many usernames we'd like to print out
-
Create random array index search values
-
Lastly, create the usernames with the searched up array values.

Here is the output sample information from running the above application
What is Unity?
How does Unity relate to procedural generation?
Now that you have a bit of background on what procedural generation is and how it might look like, we will next be looking into what Unity Engine is and how it relates to procedural generation.
​
Unity Engine is a large development platform and C# scripting API equipped with a myriad of different coding and graphic related tools as well as pre-built scripts, classes, and other assets. It is most often used for the creation of video games, but also has lots of functionality that is used in the film industry, the automotive industry, architecture, engineering, construction, and even by the US military for various training simulations.
​
​
​
​
​
​
​
​​
Procedural generation is used in Unity quite often due to the platforms focus on video game development; a field where procedural generation is quite popular and useful. Unity offers a built in rendered that can be used in conjunction with procedural generation to produce much more visual, tangible data such as randomly generated 2D or 3D landscapes, instead of basic text generation like we made earlier in our video example.
​
You can see why procedural generation might be appealing for artists, game designers, and other related fields. It lets you get large pieces of complex work like creating an entire unique 3D world done much quicker than it would be to create it by hand.
​

Here is an example of what the Unity program might look like
Source: Google

An example of what can be made with procedural generation.
Source: Google
The method we're going to look at for procedural generation
Now that you know what Unity Engine is and how procedural generation relates to it, we're going to take a bit of a dive into the technique we're going to be furthering our research on regarding procedural generation.
​
The procedural generation method we will be looking into for our next blog post is the Perlin noise method. For a bit of background on Perlin noise, it is a type of noise gradient that was developed by Ken Perlin in 1983. What sets it apart from noise or static you'd see on your TV, is that each pixel has some kind of relation to the others next to it, instead of being completely random. In other words, it's not 100% random which pixels are white, grey, or black. Because of this relation between the pixels, it creates more "organic" or real looking randomness than chaotic. It was invented for the sole purpose of improving the realism and look of computer generated imagery when texturing something. Fortunately for us, Perlin noise is readily available to use in the Unity Engine as a method (or function in this case because we will be using C#).
​
​
​
​
​
​
​
You might be wondering what the above images have to do with procedural generation, well noise is procedural generation. The above images are only two of an infinite amount of unique noise images. With a bit of tweaking when creating Perlin noise gradients, combined with the random nature of it, believable and lifelike textures and models can be created. With a noise gradient like the Perlin one above, it's then possible to create something like this...
​
​
​
​
​
​
​
​
I think you can see where this is going.
This is an example what Perlin noise looks like. You can see how some of the pixels connect or create "mountain ranges"



This is an example of 100% random noise. It's hard to see any connections or patterns because overall, it looks very consistent
Source: Google
Source: Google
Source: Google
Creating our own random Perlin noise generator
This next part will only be touched on briefly to save from the nitty gritty details and explaining of the differences between the Java programming language and C#. Our goal here is to make our own Perlin noise image like the one above. There is only one fairly short script file we need to create our own Perlin noise. It is a fairly simple and straight forward technique that you can find just about anywhere on the internet.
​
We first start off with setting the width and height of our noise texture that we will first make. So we choose 256 pixel by 256 pixels. The scale will be for upscaling our texture size in the end and the offset will be used to generate new random noise images every time we run the script.
​
​
​
​
​
Second, we set up our Start and Update functions (methods). The Start function will be called once, right when we generate a new noise texture, and the update method will be constantly updating once the texture is created, allowing us to manipulate it live.
​
​
​
​
​
​
Third, we will setup our GenerateTexture function. This function is responsible for generating the entire noise image. It goes line by line and places every single pixel in the image we are trying to make and gets the colour of each of them from the CalculateColor function.
​
​
​
​
​
​
Lastly, we create our CalculateColor function. This function is fairly simple, it calls the Mathf.PerlinNoise function to get the noise coordinates for our image, and sets the colour of each of them. This then returns the colour to the GenerateTexture function.
​
​
​
​
​
​
After all of that, here is what we are left with!
​
​
​
​
​
Now this might not seem that impressive or useful but the next step is where it gets interesting. It is possible to layer these images, change the colours and textures of them, and with a lot of tweaking we can end up with something like this...
​
​
​
​
​
​
​
​
​
Can you start to see maybe some of the similarities? Hopefully?
​
However, creating our own procedurally generated terrain like this will take quite a bit more work, so we'll have to wait until the next blog post to dive further into how this is done. Anyways, thank you for taking the time to read this blog, I hope it wasn't too vague, and I hope you learned something!
Stick around for part two.




It would look something like this when generating the noise texture. It would go line by line, starting at line 1, fill it out until the Y axis equals 256, then go to line 2, and repeat the process, filling them each out with white, grey, or black pixels



Source: Google
Part 2:
Recap
Welcome back to the second part of CWBLOG! Here in part 2, we will be going much deeper into what it takes to create our own 2D earth-like terrain map with procedural generation!
​
To start off, we will recap what we did last time in part 1, then we will cover and explain the new code we've created to generate our noise maps. Then, we'll move on to some cool little features of the Unity engine and how it can help us tweak our results, and lastly we'll show our end results.
​
​
Last time, we went over a basic understanding of what procedural generation is, it's history, we created a small example of it, and then in the end we created a basic Perlin noise image. Using a Perlin noise image, our ultimate goal this time around will be to create a 2D earth-like terrain map, similar to the one below, that we showed in part 1.
​
​
​
​
​
​
​
​
In part 1, we were able to create our noise image with only one script file as seen below:
​
​
In this file:
-
We set the base attributes of our noise image we were trying to create.
-
In our Start() function we created the randomness in each generated image by setting the offset to be random every time we generate a new noise image.
-
We created our GenerateTexture() function that handled populating each pixel, row by row, in the 256x256 pixel image we were trying to create.
-
We created our CalculateColor() function to be called by our GenerateTexture() function, that handles deciding which colour each pixel should be as we are generating our noise image
All of this left us with being able to generate this image:
​
​
​
​
​
​
​
​
This time around, we've changed up our base code quite a bit as we've found some better ways to do things! It will begin looking very similar, and our first goal will be to essentially reproduce a noise image like this, but after that we will begin adding layers and depth until we have something a little more recognizable.
​
​
​
​
​


Source: Google

2D Terrain Map Example
Diving back into procedural generation
With our new code for part 2, we will be creating five separate script files (or classes) to handle everything as opposed to the single file we used last time.
​
We will be creating:
-
Noise.cs
-
MapGenerator.cs
-
TextureGenerator.cs
-
MapDisplay.cs
-
MapGeneratorEditor.cs
​
Let's get into it!
​
First off, we created our Noise script. This script (class) will only contain one function (method) in the end called GenerateNoiseMap(). The purpose of this script is essentially for creating a large grid or image of perlin noise coordinates.
​
Here is what it will first look like:
​
​
​
​
​
​
​
​
-
We created our one function (method) that takes in a width, height, and a scale. The width and height control the amount of pixels we want our image to be and the scale is for upscaling the size.
-
We create an array to hold each pixel of the image.
-
We peform a basic check just to make sure that the scale isn't zero, as this can cause some problems.
-
We then use for-loops to go through every pixel coordinate in the image and assign it a perlin value from the Mathf.PerlinNoise() function.
-
Lastly, we return the noise image.
​
Incase you don't know, the Mathf.PerlinNoise() function is a built-in function in Unity. It accepts two floats that make a coordinate (x, y), and returns a float in between 0.0 and 1.0 for that coordinate. That returned value is then used to create the noise image.
​
Our Noise script should be in order for now to generate a basic noise image like we did before. We will be coming back to it later to make many more changes.
​
​
Next up we will start our MapGenerator script. This script, in the end, will contain two functions; GenerateMap() and OnValidate(), as well as a struct called TerrainTypes. The purpose of the MapGenerator script will be to take the perlin noise coordinates from our Noise script, as well as the drawn texture from another script we will create, and essentially assemble the terrain map.
​
OnValidate() is a unique editor-only function that will be called any time the script is loaded or when we change values of the noise image live in the Unity editor. We are only using to ensure that we cannot set certain values like the width and height of the image to be less than 1.
​
We won't go into too much detail on what a struct is, but it is essentially a small data structure that can hold values, as opposed to being a reference to memory, like an ordinary class. We will be using the TerrainTypes struct to hold the data on the different layers or regions of the terrain map we create.
​
​
​
​

Diving back into procedural generation cont.
Here is the beginning of our MapGenerator script containing our GenerateMap() function.
​
​
​
​
​
​
-
We declare width, height, and scale.
-
We create our function GenerateMap().
-
We call the function GenerateNoiseMap() from our Noise script and pass through our width, height, and scale.
-
We call our MapDisplay script that we will create next.
-
We pass through our retrieved noise coordinate map to the function that will assign a colour to each pixel coordinate.
​
Next we will be creating out MapDisplay script. This script will contain one function, and for the time being will be used to create our actual noise texture. In other words, it will be used to add colour to each pixel in our noise image.
​
​
​
​
​
​
​
​
​
-
We declare the function DrawNoiseMap() that accepts the noise coordinate array we made earlier.
-
We create a new empty texture that we will apply the colours to.
-
We create an array of colours that holds colours on a grid/map similar to our noise coordinate grid/map.
-
We then loop through each coordinate and assign a random colour somewhere in between black and white to each noise map coordinate. Color.Lerp essentially accepts a range of two parameters (black to white) and assigns a random value from in that range to the third parameter (noiseMap)
-
We apply the colour map we made to the empty texture.
-
We apple the texture to the texture renderer so that we can see it in the Unity editor.
​
Now just before we're able to start creating more perlin noise images, we're going to create one more script file that will simply add a bit more functionality for us in the Unity editor itself.
​
Here will will be making the MapGeneratorEditor script. This simple script's job is simply to create a button in Unity that we can use to generate a new noise image on demand. We won't go into too much detail about this as it's just supplementary.
​
​
​
​
After we create this script, we can now see this in Unity:
(Note that the width, height, and scale were already accessible but now we have access to the generate button)
​
​
​
​




Diving back into procedural generation cont.
And now we've caught back up to where we stopped in part 1! When we click the generate button in the editor, we are now presented with this:
​
​
​
​
​
​
​
​
I promise this is where it starts to get more interesting!
​
Now we don't want to be stuck with this plain noise image. We need to be able to tweak it and manipulate the values so it can start to actually look like something.
​
We're going to go back over the scripts we've created, add some more functionality, and clean some code up. This part will be fairly streamlined as we would be here all day if we were to go over every little detail, however we will still cover the important parts.
​
Going back to our Noise script, we're going to make some changes. The primary factors being the addition of seeds, octaves, persistence, and lacunarity.
​
-
Seeds: Essentially act as an ID to each noise image we generate. Seeds should always stay the same. We can keep track of specific noise images by remebering the seed.
-
Octaves: Essentailly the amount of "peaks" the noise map can have. Lower octaves make the image look smoother.
-
Persistence: Has a similar effect to octaves. Essentially how consistent/chaotic the noise map looks. Lower persistence makes the noise "mountain ranges" look smoother and more connected.
-
Lacunarity: Essentially the "gaps" in the noise image.
​
​
​
​
​
​
​
​
​
​
​
Next we will be updating our MapGenerator script to support our new attributes, as well as include the OnValidate() function and TerrainTypes struct we talked about earlier.
​
​
​
​
​



Diving back into procedural generation cont.
Next up we'll be separating the colour picking functionality from our MapDisplay script and we'll put it into it's own script called TextureGenerator. The reason for this is to allow easy switching of functionality later from creating colourless noise images like we made earlier, to full colour terrain maps.
​
So our MapDisplay script will only serve for rendering our created texture in the Unity editor and will end up looking like this:
​
​
​
​
And our TextureGenerator script will now be responcible for texturing our noise maps, as seen below.
​
​
​
​
​
​
​
After these changes we now have much more power when it comes to controlling and tweaking our noise images. We can access all of our new attributes directly through the Unity editor too.
​
​
​
​
​
​
​
We can now make use of the TerrainTypes struct too and begin creating layers on our noise map.
​
​
Here's what only a few layers can do:
​
​
​
​
​
​
​
​
Can you see how this sort of still looks like our original noise image? Just by adding a bit of colour, we can start to see drastic changes.
As we keep adding layers and adjusting their height in the noise map, it starts becoming more and more believable.
​
​
​
​
​
​
​
​
​
​






Wrapping up
You can tweak and change them as much as you like, and this is only the surface of procedural generation! Given the time, I would love to dive even deeper into this and learn to make even more detailed and believable landscapes.
It's really amazing the power computers have now-a-days and it never stops exciting me seeing what people can do with them. Just with a relitivley small amount of code, without any art-related skills, we're able to make entirely unique, detailed worlds.
​
I hope you enjoyed this blog, I hope you learned something, I wish I could have gone into further detail about procedural generation in Unity, and I thank you for taking the time to read this!
​​
​
​
​
​
​
​
​
​
​
