
[ad_1]
I’ve been engaged on a 3D recreation in java utilizing LWJGL for some time now. I’m making an attempt to make procedurally generated infinite 3D terrain that’s generated across the participant.
So far I’ve:
- 3D terrain chunks which are loaded and unloaded when the participant strikes. These chunks are made up of triangles not quads, and are 128×128 vertices.
- A Perlin noise class that may efficiently generate Perlin noise. (I feel it’s really one thing referred to as worth noise, however it works)
- A world class that handles loading and unloading of chunks, and making use of the peak maps to the chunks.
All of this works as it’s coded to do, however not how I need it to. I do not know methods to tile the noise seamlessly.
How would I make the noise tile, and moreover, how would I generate giant constructions equivalent to mountains that occupy most likely as much as a whole lot of chunks.
Here is what’s presently taking place with the chunks and noise not tiling:
Here is my Perlin noise code:
non-public float[][] perlinNoise(int width, int peak, int octave, float[][] whiteNoise)
{
float[][] end result = new float[width][height];
int samplePeriod = 1 << octave;
float sampleFrequency = 1.0f / samplePeriod;
for (int i = 0; i < width; i++)
{
int x1 = (i / samplePeriod) * samplePeriod;
int x2 = (x1 + samplePeriod) % width;
float xBlend = (i - x1) * sampleFrequency;
for (int j = 0; j < peak; j++)
{
int y1 = (j / samplePeriod) * samplePeriod;
int y2 = (y1 + samplePeriod) % peak;
float yBlend = (j - y1) * sampleFrequency;
float prime = (float) MathHelper.interpolateLinear(whiteNoise[x1][y1], whiteNoise[x2][y1], xBlend);
float backside = (float) MathHelper.interpolateLinear(whiteNoise[x1][y2], whiteNoise[x2][y2], xBlend);
end result[i][j] = (float) MathHelper.interpolateLinear(prime, backside, yBlend);
}
}
return end result;
}
public float[][] generatePerlinNoise(int width, int peak, Random random, int octaveCount)
{
float[][] whiteNoise = new float[width][height];
float[][][] totalNoise = new float[octaveCount][][];
float[][] perlinNoise = new float[width][height];
float amplitude = 1.0f;
float totalAmplitude = 0.0f;
float persistance = 0.5f;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < peak; j++)
{
whiteNoise[i][j] = random.nextFloat() % 1;
}
}
for (int i = 0; i < octaveCount; i++)
{
totalNoise[i] = perlinNoise(width, peak, i, whiteNoise);
}
for (int o = octaveCount - 1; o >= 0; o--)
{
amplitude *= persistance;
totalAmplitude += amplitude;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < peak; j++)
{
perlinNoise[i][j] += totalNoise[o][i][j] * amplitude;
}
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < peak; j++)
{
perlinNoise[i][j] /= totalAmplitude;
}
}
return perlinNoise;
}
I feel it could even be value mentioning that I’ve requested about this on StackOverflow as properly.
[ad_2]