Terrain Generation (new map approach) - Kraddak/UnityStudy GitHub Wiki

#Terrain Generation In this section I am going to talk about a different way of generating a map using the "terrain" class of unity. This approach differs from using a custom mesh, by using the unity terrain a lot of groundwork is already prepared for us. My objective is to create an infrastructure that allows me to generate maps quickly and efficently, and later use it to generate scenarios randomically. terrain

A cell approach

A cell C can be of different types, consider the following notation: C ∈ {F, V, O, A, W, D, S}

F = a flat floor cell

floorF

V = a pit (hole) cell

pitV

S = a slope cell climbing ↓

slopS

D = a slope cell climbing →

slopeD

W = a slope cell climbing ↑

slopeW

A = a slope cell climbing ←

slopeA

O = a obstacle of spikes

obstacleO

Let's expand the notation putting a number (call it X) on the side of the type of cell, CX. The number X ∈ [0, max altitude] represent the altitude of the cell. So if C = F, and X = 1, we could say then that "F1" is the cell flat floor at altitude 1. In the following example we have a matrix that represent a possible terrain of all flat cells, with only a central cell that stands on higher grounds.

F0 F0 F0 F0 F0
F0 F0 F0 F0 F0
F0 F0 F1 F0 F0
F0 F0 F0 F0 F0
F0 F0 F0 F0 F0

This example matrix would then be visualized in reality like the following image cellFloor

The Cell class

I defined a cell class which will be later used to record if a character is on this cell or not. A collection of these cells would then represent a field of possible positions that a character could move or not move into. I divided each type of cells with inheritance from this class, and built a method to create a cell from string using the previous notation CX.

F cell

S cell

O cell

V cell

The important aspect to remember about the cells is that, given a cell, you should always be able to ask where the cell is in the Unity world (Real world), and where it is in the matrix/grid world.

Terrain controller

As an esthetic choice I decided to not use the whole map, and leave a small border between where the map actually starts (RED), and where the terrain actually starts. The terrain is also very high on purpose, again an esthetic choice. Eventually I will create a shader that will make invisible the lower part of the map, and only show the upper "real parts" of the terrain. We could say that half of the terrain is actually invisible to the user (BLUE). side hidden

Once the shaders will be in place the map would appear very different than what is in reality. The shader is out of the scope of the current objective, and therefore we are going to ignore it for now.

To understand how the Terrain controller class work, we need to understand that the unity terrain has an options to control directly the height values.

private float[,] mesh;
terrainResolution = terrain.terrainData.heightmapResolution;
mesh = new float[terrainResolution, terrainResolution];
terrain.terrainData.SetHeights(0, 0, mesh); // assigning the height values directly

Let's now take an arbitrary area of space, like a square 5x5 or 6x6, and say that is a cell. cellFloor

It would be nice to be able to set a cell to the wanted height and to the wanted form (slope, obstacle, etc...). This is what the method SetCell is doing. By giving a "curve" function in the parameters, we can decide the cell height and how it will look like (This is how I can make obstacle and slope cells). So basically by doing SetCell(2, 2, (_,_) => 1), we are basically saying "Make the cell [2,2], and every point of the cell will have altitude 1" (a flat cell).

In the alias function SetCell, when an instance of Cell is passed, the cell itself will give the lambda function that will describe how the cell will look like, and it's altitude.

The last important step to consider is how we give the Cell class the coordinate of it's real position in the scenario. This is what the SetCoorWorld function does. The method GetPosition calculates the coordinates Vector3 of the four edges of the square cell. By calculating the four points, we can establish the real coordinates of the center of the cell.