我一直试图弄清楚如何使用perlin噪声在3D中创建无限地形 . 到目前为止,我已经得到了一个由块组成的地形,当玩家离开一个块时,会产生新的块 . 超出范围的块被卸载 . 所以我有一个无限世界的幻想 .

我正在使用perlin噪声为块创建高度贴图(每个块都有自己的高度贴图)

我的问题是如何无缝地平铺每个块的高度图,以便在块之间的世界中没有可怕的间隙,并且没有为每个块具有相同的高度图 .

"gaps in the world"我的意思是this .

我跟着this实施了柏林噪音

这是我的perlin噪音代码:

private float[][] perlinNoise(int width, int height, int octave, float[][] whiteNoise)
{
    float[][] 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 < height; j++)
        {
            int y1 = (j / samplePeriod) * samplePeriod;
            int y2 = (y1 + samplePeriod) % height;
            float yBlend = (j - y1) * sampleFrequency;

            float top = (float) MathHelper.interpolateLinear(whiteNoise[x1][y1], whiteNoise[x2][y1], xBlend);

            float bottom = (float) MathHelper.interpolateLinear(whiteNoise[x1][y2], whiteNoise[x2][y2], xBlend);

            result[i][j] = (float) MathHelper.interpolateLinear(top, bottom, yBlend);
        }
    }
    return result;
}

public float[][] generatePerlinNoise(int width, int height, 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 < height; j++)
        {
            whiteNoise[i][j] = random.nextFloat() % 1;
        }
    }
    for (int i = 0; i < octaveCount; i++)
    {
        totalNoise[i] = perlinNoise(width, height, 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 < height; j++)
            {
                perlinNoise[i][j] += totalNoise[o][i][j] * amplitude;
            }
        }
    }
    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            perlinNoise[i][j] /= totalAmplitude;
        }
    }
    return perlinNoise;
}

编辑:我刚刚问过this on GameDev我的问题可能会更详细,更有帮助 . Please try and answer there if possible ,因为我会更少看这篇文章 .

编辑:我已经意识到我的噪音代码是 NOT PERLIN NOISE . 它实际上是一种叫做 VALUE NOISE 的东西,它实际上看起来并不那么好,而且无论如何都不适用于我需要的东西 . 我找不到任何关于perlin噪声的java实现 . 我只是想要一个我可以使用的代码的链接,我想有一个教程,在那里我可以理解算法实际上是如何工作的 .