I'm experimenting with Perlin Noise in C#. I managed to create a working 2D map generator. But now I want to make my noise tileable. I want that the right edge of the map fits in the left edge of the map.
I'vw been searching for this on the internet and I have tried different things but I could not get it to work. Is there someone who knows a formula for this?
Here is my code:
public float[,] GenerateNoise(float[,] map, int xsize ,int ysize ,float scale, int seed,
int octaves, float persistance,float lacunarity)
{
Random rnd = new Random(seed);
float offsetX = rnd.Next(0, 100000);
float offsetY = rnd.Next(0, 100000);
if (scale <= 0)
{
scale = 0.0001f;
}
float minNoiseHeight = float.MinValue;
float maxNoiseHeight = float.MaxValue;
float[,] fallmap;
//fallmap = FalloffGenerator.GenerateFalloffMap(xsize);
Perlin Noise = new Perlin();
//perlin noise
for (int y = 0; y <= ysize - 1; y++)
{
for (int x = 0; x <= xsize - 1; x++)
{
float amplitude = 1f;
float frequency = 1f;
float noiseHeight = 0;
for (int i = 0; i < octaves; i++)
{
float sampleX = (xsize - x) / scale * frequency + offsetX;
float sampleY = (ysize - y) / scale * frequency + offsetY;
float PerlinValue = (float)Noise.perlin(sampleX, sampleY, 0) * 2 - 1;
map[x, y] = PerlinValue;
noiseHeight += PerlinValue * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
// map[x, y] = map[x, y] - fallmap[x, y];
}
if(noiseHeight > maxNoiseHeight)
{
maxNoiseHeight = noiseHeight;
}else if (noiseHeight < minNoiseHeight)
{
minNoiseHeight = noiseHeight;
}
map[x, y] = noiseHeight;
//map[x, y] = map[x, y] - fallmap[x, y] ;
}
}
return map;
}