Skip to content

erthium/ProceduralMapGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Procedural Map Generator

Procedural Map Generator Prototype, mainly made to use as a forest map creator in a strategy game with Unity Game Engine.

License

This project is lisenced under MIT 2022 license.

Setup

The directory 'project' is the main Unity Project. After you clone or download the repository, you can select that folder in Unity Hub and open the project.

Beware that used Unity Editor version of this project is 2020.3.22f1.

It Consists...

Mesh Generator

A basic Mesh Generator made by using Perlin Noise, to be able to create a smooth and continues land shape.

The built-in function that has been used for perlin noise

UnityEngine.Mathf.PerlinNoise();

For creating a mesh that has psuedo-random (perlin random) y-axis (height) values, we get random offset position and a 2-dimensional perlin map. Then use these values to have a psuedo-random height map for our mesh.

#width and height are the size values of the map

int offset_x = Random.Range(0, 10000);
int offset_z = Random.Range(0, 10000);

vertices = new Vector3[(widht + 1) * (height + 1)];
for(int i = 0, z = 0; z <= height; z++)
{
    for (int x = 0; x <= widht; x++)
    {
        float y = Mathf.PerlinNoise((x + offset_x) * perlin_multiplier, (z + offset_z) * perlin_multiplier) * sensitivity;
        vertices[i] = new Vector3(x, y, z);
        i++;
    }
}

Map Generator

After the land has been generated, comes the part where we locate the chosen amount of objects on the land. In this case, since it was a map for camp sides in the forest, our surroundings will be camps and trees. To inpersonate them in the prototype, there are simple wide and long rectangles.

We can adjust the offset between the entities. You can see the difference in the images below, with the Editor values of offsets(radius) and amounts.

Objects are placed considering the slope of the proceduraly generated mesh, by sending rays from four edges of the object and calculating the optimal slope. This helps the algorithm to make the placing process more natural.

Releases

No releases published

Packages

No packages published