Brackeys Tutorials: Terrain Generation and Vertex Colors

September 11, 2019

Graphics Tutorials

Mesh Generation/Alteration and Vertex Colors

Youtube – MESH GENERATION in Unity – Basics

Tutorial #1

By: Brackeys


Youtube – PROCEDURAL TERRAIN in Unity! – Mesh Generation

Tutorial #2

By: Brackeys


Youtube – MESH COLOR in Unity – Terrain Generation

Tutorial #3

By: Brackeys


Tutorial #1

This tutorial just covers the basics of creating meshes in Unity. This covers the basics of vertices and tris in Unity, as well as the back-face culling done. This just means when creating tris for the triangle array that they must be input in a clockwise manner to display in the proper direction.

Tutorial #2

This tutorial gets into using the basics of Tutorial #1 to create an entire terrain. This process simply starts by creating a grid of a bunch of vertices, filling them in with tris, and then modifying the positions of some of those vertices.

This started with a process I have used before in mesh generation, where you use several for loops to generate all the vertices in an array of some given size, then fill the gaps between those vertices with tris using more for loops and some basic math. They did add a nice twist where they made the CreateShape method into a coroutine, so we could use WaitForSeconds and see the mesh be filled out. While this was done for a neat aesthetic purpose, this could possibly help in debugging meshes to see where the tris start to be created incorrectly.

The very simple for loop setup for going through all the vertices and filling in the tris did have one flaw that was addressed in the tutorial. When going from the end of a row to the next row, we were creating an extra tri which extended from the end of the row all the way back to the beginning of the next row. Weird errors like this have gotten me before in mesh generation, so I just wanted to point it out.

The setup in this tutorial did the whole quad for each vert, so basically each point was given its own square to cover as we went through the for loops. To avoid the issue of creating extra tris between rows, they simply “skipped” the final vert in each row by adding 1 to the vert index an extra time once a row was completed.

Example of tri generation snippet: for (int z = 0; z < zSize; z++) { for (int x = 0; x < xSize; x++) { triangles[tris + 0] = vert + 0; triangles[tris + 1] = vert + xSize + 1; triangles[tris + 2] = vert + 1; triangles[tris + 3] = vert + 1; triangles[tris + 4] = vert + xSize + 1; triangles[tris + 5] = vert + xSize + 2; vert++; tris += 6; yield return new WaitForSeconds(0.1f); } vert++; // This is the extra vert index added to ensure proper transition from one row to next }

Finally, to make it seem more like the terrain we wanted, we added some noise to the y position of our verts when creating them. Perlin noise was the choice used in the tutorial. Perlin noise is Unity takes in two coordinate parameters, then outputs a noise value between 0 and 1. You can further multiply this by another factor to create more significant noise effects.

There was an interesting issue with using perlin noise. They multiplied the input parameters by 0.3f, which looked very arbitrary. They mentioned that there was a reason for this covered in another video on perlin noise so I checked that and apparently perlin noise repeats on whole numbers. Since we were fedding in parameters based on our vertex indices, these were all whole numbers. Sure enough, when I removed the 0.3f multiplier, the entire terrain was flat again. Something about being “not whole numbers” allows the noise to work.

Tutorial #3

I logged this tutorial earlier, and just wanted to include it here since it went with the other tutorials. I’ll be looking to use this as my Next Step for this post, and hopefully get some more vertex color tutorials to go along with it. I would like to look into some more shader code focused ones if I can since it should be pretty straight forward/simple shader language to get some more practice.

NEXT STEP

Do Tutorial #3 and find more vertex color tutorials (preferably with focus on using shader language).