- Home /
improve generate tilemap with perlin noise
Hi, I am fiddling with perlinnoise to generate a tilemap using the following code: i want the map to contain mostly green tiles, and blobs here and there of resources iron , copper etc...
using UnityEngine;
using System.Collections;
public class SpawnGrid : MonoBehaviour
{
[Range(0.1f,100f)]
public float seedX, seedY;
public GameObject gridTile;
public float gridX, gridY, perlinThresholdiron, perlinThresholdcoal, perlinThresholdcopper, perlinThresholdstone;
void Start ()
{
for (float x = -gridX; x < gridX; x += 0.1f)
{
for (float y = -gridY; y < gridY; y += 0.1f)
{
Vector3 spawnPos = new Vector3(x*10,y*10,0);
GameObject tile = Instantiate(gridTile,spawnPos,Quaternion.identity) as GameObject;
SpriteRenderer rend = tile.GetComponent<SpriteRenderer>();
TileController tileController = tile.GetComponent<TileController>();
float perlinNum = Mathf.PerlinNoise(x/seedX, y/seedY);
if (perlinNum < perlinThresholdiron)
{
rend.color = Color.gray;
tileController.isResource = true;
tileController.tileType = ResourceType.iron;
}
else if (perlinNum > perlinThresholdiron && perlinNum < perlinThresholdcoal)
{
rend.color = Color.black;
tileController.isResource = true;
tileController.tileType = ResourceType.coal;
}
else if (perlinNum > perlinThresholdcoal && perlinNum < perlinThresholdcopper)
{
rend.color = Color.yellow;
tileController.isResource = true;
tileController.tileType = ResourceType.copper;
}
else if (perlinNum > perlinThresholdcopper && perlinNum < perlinThresholdstone)
{
rend.color = Color.blue;
tileController.isResource = true;
tileController.tileType = ResourceType.stone;
}
else
{
rend.color = Color.green;
}
}
}
}
}
I can move the seedX and Seedy sliders around to genrate different patterns, but they are always very repetitive and the resources are always "surrounding" eachother: how can i change this code so that it generates blobs of resources of random size/shape (think factorio)
hope you guys understand what im trying to do
As you have found, perlin is mirrored around the origin. Add an offset to x and y to get values away from the origin, eg:
float perlinNum = $$anonymous$$athf.PerlinNoise( (x+offset) / seedX, (y+offset) / seedY );
$$anonymous$$ake the offset larger than gridX/gridY and the mirroring will go away.
For separating the ores, consider sampling separate perlin with different seeds, eg:
float perlinCopper = $$anonymous$$athf.PerlinNoise( (x+offset) / seedCopperX, (y+offset) / seedCopperY );
float perlinIron = $$anonymous$$athf.PerlinNoise( (x+offset) / seedIronX, (y+offset) / seedIronY );
Then say if iron is more common than copper :
if ( perlinIron > perlinThresholdiron )
{
// do iron
}
else if ( perlinCopper > perlinThresholdcopper )
{
// do copper
}
How you manage what resource is more do$$anonymous$$ant is a design choice that is up to you.
thanks man! I ended up modifiying my code to your example and it works beautifully for my needs!
Answer by cjdev · May 28, 2016 at 06:46 AM
Perlin Noise and most noise generating functions are very repetitious by nature. However, in order to get the random-looking patterns Perlin Noise is known for (the "clouds" effect) you'll need to use Fractional Brownian Noise, which is a long name for adding Perlin a bunch of times, each time with a higher frequency and lower amplitude. This answer has a lot of good info and resources on Perlin Noise in general if you're interested in learning more about it and FBN.
For your application you can sum together several functions of PerlinNoise (to taste, it will take some experimentation) but as you have it now I believe you've got the use of the function wrong. Mathf.PerlinNoise gets the x and y value of a 2d perlin noise result. To modify the amplitude of that you'd just multiply a constant times the function as a whole, as if you were changing the 'height'. For the frequency you'd multiply x and y by a constant, in effect determining how close successive values are to each other. And for the random seed you would add a constant to each x and y based on your seed to offset the 2d lookup for each unique value. So it looks like this:
Perlin Noise = Amplitude * Mathf.PerlinNoise(Frequency * x + Seed, Frequency * y + Seed)
In short, sum up a bunch of the above with increasing frequency and decreasing amplitude and you should see something like the effect you're after. As an aside, note that Perlin Noise is not the most efficient method of random generation so if performance is a factor you might want to consider a custom implementation over Unity's function.
Your answer
Follow this Question
Related Questions
Perlin Noise Implementation 2 Answers
How to use Perlin Noise for waves 0 Answers
Adding rivers to procedural generated 2D map 1 Answer
2d Top-Down Perlin noise map using Mathf.PerlinNoise 3 Answers
Perlin noise not being changed runtime 2 Answers