Sign in to follow this  
Narf the Mouse

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

Recommended Posts

Narf the Mouse    322
...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 this post


Link to post
Share on other sites
Narf the Mouse    322
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);
}
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this