Hello everyone! I have made a random map generator using Mathf.Perlin noise, which makes a random heightmap and assigns color values based on the output. The pixels are then read and assigned a number in an array that tells the “Game manager” what tile to spawn in the tilemap.
Although I think it serves my most basic uses, I feel like it looks pretty fake and I want to incorporate more ‘biomes’ but in a more random fashion. Is there any way to fuse perlin noise together to create more consistent landmasses (Like continents), and potentially add clumps of other colors? For instance, I want the grass color to be replaced with my snow color in some areas, same with swamp and desert colors. I think a solution would be to generate a separate perlin noise map with my other biomes and apply it like a mask, but I have no idea how to do this.
Here’s my “Generate map” function and a gif of it in action. If you’re just trying to achieve something similar, feel free to use it as you see fit. I just modified the code from unity’s scripting api website here: Unity - Scripting API: Mathf.PerlinNoise
void Generatemap()
{
// Set up the texture and a Color array to hold pixels during processing.
noiseTex = new Texture2D(pixWidth, pixHeight);
pix = new Color[noiseTex.width * noiseTex.height];
float randomorg = Random.Range(0, 100);
// For each pixel in the texture...
float y = 0.0F;
while (y < noiseTex.height)
{
float x = 0.0F;
while (x < noiseTex.width)
{
float xCoord = randomorg + x / noiseTex.width * scale;
float yCoord = randomorg + y / noiseTex.height * scale;
float sample = Mathf.PerlinNoise(xCoord, yCoord);
if (sample == Mathf.Clamp(sample, 0, 0.5f))
pix[(int)y * noiseTex.width + (int)x] = water;
else if (sample == Mathf.Clamp(sample, 0.5f, 0.6f))
pix[(int)y * noiseTex.width + (int)x] = sand;
else if (sample == Mathf.Clamp(sample, 0.6f, 0.7f))
pix[(int)y * noiseTex.width + (int)x] = grass;
else if (sample == Mathf.Clamp(sample, 0.7f, 0.8f))
pix[(int)y * noiseTex.width + (int)x] = forest;
else if (sample == Mathf.Clamp(sample, 0.8f, 1f))
pix[(int)y * noiseTex.width + (int)x] = mountains;
else
pix[(int)y * noiseTex.width + (int)x] = water;
x++;
}
y++;
}
// Copy the pixel data to the texture and load it into the GPU.
noiseTex.SetPixels(pix);
noiseTex.Apply();
mappreview.texture = noiseTex;
worldmap = noiseTex;
}