3D Voxel Terrain Generation

About the project
For my final project at Ljud- och Bildskolan (LBS) I decided to work on a 3D Voxel Terrain Generation. I had made a 2D Terrain Generator right before this project started and I had a better understanding on how this was supposed to work.
​
During this period of time I was very invested in Minecraft, it being my favorite game for many years in a row, so I decided that I wanted to try and replicate part of its Terrain Generation. This project was developed in Unity.​
What I did

I started with learning how to create custom meshes and how vertices and triangles worked. A mesh is created by a list of vertices and triangles. A vertex is a point in the mesh, and the triangles is the visible area. To create a triangle, a set of three vertices are needed. The vertices will generate a one way face of a triangle
I went slowly, trying to make 1 cube work, testing in what order the vertices needed to be created into triangles.
​
At last I managed to generate one cube.

The next step was to generate more than one cube. This was quite easy, since I just had to give the algorithm a new position and it would just create another cube. Problem is that the vertices where duplicated. Instead of there only being 12 vertices to create 2 cubes, there was 8 vertices.
I did manage to solve it and now I had a way to generate multiple cubes correctly.

The next big step was to generate terrain using the algorithm that generated the cubes.
I created a small tool to visualize and modify Perlin noise, and used it to try to improve the algorithm that was used in my 2D Terrain Generator.
​
With the perlin map generated, I used it as a heightmap to decide how far up the terrain could be in each point in space. I gave it a maximum and minimum value for height and then multiplied the max height with the value from the heightmap (value between 0-1).
​​
This now gave me the ability to generate the world in chunks. Every chunk is one mesh, and is 16x16 blocks big and can go up to 255 blocks tall. I used the metrics of Minecraft chunks. The only issue with this was that all the blocks were generating, even inside of the terrain, where they are not visible to the player and this took a lot of performance to generate.
​

What I did was to created a dictionary map using world positions as keys, and gave each block the ability to read the values of their neighbours. If the block type is air, then generate the face , if not, don't generate a face.

After a lot of work, I had finally some kind of terrain generator. It wasn't perfect, and it was quite slow, but it's something I was quite proud to accomplish. The next step was to give each blocks textures and at the end fix the performance.

I had a lot of issues generating the textures on the mesh. I got a sprite atlas from Minecraft (Developed by Mojang) and set it on a material and set the material on the chunk mesh.
​
In the Editor I made it so I could create block types, and give them the UV coordinates for the texture.
​
I don't quite remember how I solved this issue but this was one of the biggest time sinks in the whole project, trying to get the UV coordinates to work correctly.
​
​

After a while I had finally managed to solve the texturing issue and now I had a pretty solid terrain generator. I added grass for the top layer, dirt for 2 layers under the grass and then I filled the rest with stone blocks.
​

This was the end product. A 3D Voxel Terrain Generator, heavily inspired by Minecraft. I am quite happy that I managed to accomplish this, although are things that could be improved upon, that being the performance and the polish the Perlin noise. I never managed to improve the performance considerably. I tried to thread the program, and use tasks and queues but I never got it working.
​
I think if I would do this project again, it would turn out way better than before. I feel like I've gained a lot of experience between the end of this project and now, and could probably replicate it with my own Graphics Engine, instead of using Unity and also having way more performance that I did in Unity.
​