# I need a 2D noise generator/tutorial...

This topic is 2810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

...That I can use for regular, tiled terrain. Of the two I have now, one is meant to run on a GPU (and won't run on mine) and the other won't work for tiled terrain by reason of how it works (as best I can tell).

Thanks.

##### Share on other sites
Well, I did a hack of the GPU-coded noise generator to make it work properly for a CPU; got about a ~5x speedup.

Problem is, everything is coming out blocky. For example,

And, of course, the code:

public static void PerlinNoise2D2(double[,] map, Func<int, int, PointD> conversion,
double persistence, int octaves, int shift)
{
// double total = 0;
double frequency;
double amplitude;
int sizeX = map.GetLength(0);
int sizeY = map.GetLength(1);
double[,] map2 = new double[sizeX + 3, sizeY + 3];
PointD converted;

int integerX,
integerY,
n;
double fractionalX,
fractionalY;

for (int i = 0; i < octaves; ++i)
{
frequency = Math.Pow(2, i + shift);
amplitude = Math.Pow(persistence, i + shift);
for (int x = 0; x < sizeX + 3; ++x)
{
for (int y = 0; y < sizeY + 3; ++y)
{
converted = conversion(x - 1, y - 1);
converted = new PointD(converted.X * frequency, converted.Y * frequency);
integerX = (int)converted.X;
integerY = (int)converted.Y;

n = integerX + integerY * 1376312627;
n = (1 - ((n * (n * n * 15727 + 789181) + 1376312627) & 0x7fffffff)) /* / 1073741827) */;
map2[x, y] = Randoms.MersenneTwister.MercenneOnce((uint)n) * amplitude;
}
}

for (int x = 0; x < sizeX; ++x)
{
for (int y = 0; y < sizeY; ++y)
{
converted = conversion(x, y);
converted = new PointD(converted.X * frequency, converted.Y * frequency);
integerX = (int)converted.X;
integerY = (int)converted.Y;
fractionalX = converted.X - integerX;
fractionalY = converted.Y - integerY;

double v1 = SmoothedNoise2D2(x + 1, y + 1, map2);
double v2 = SmoothedNoise2D2(x + 2, y + 1, map2);
double v3 = SmoothedNoise2D2(x + 1, y + 2, map2);
double v4 = SmoothedNoise2D2(x + 2, y + 2, map2);

double i1 = CosineInterpolate(v1, v2, fractionalX);
double i2 = CosineInterpolate(v3, v4, fractionalX);

map[x, y] += CosineInterpolate(i1, i2, fractionalY);

// double corners = (Noise2D(x - 1, y - 1) + Noise2D(x + 1, y - 1) + Noise2D(x - 1, y + 1) + Noise2D(x + 1, y + 1)) / 16;
// double sides = (Noise2D(x - 1, y) + Noise2D(x + 1, y) + Noise2D(x, y - 1) + Noise2D(x, y + 1)) / 8;
// double center = Noise2D(x, y) / 4;
}
}
}

double persistenceDivisor = 0;
for (int t = shift; t <= octaves + shift; ++t)
persistenceDivisor += Math.Pow(persistence, t);

for (int x = 0; x < sizeX; ++x)
{
for (int y = 0; y < sizeY; ++y)
{
map[x, y] = ((map[x, y] / persistenceDivisor) / uint.MaxValue);
}
}
}

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631781
• Total Posts
3002314
×