Perlin Noise 在 Y 轴上得到错误的值(C++)

Perlin Noise getting wrong values in Y axis (C++)

问题

我正在尝试使用大小为 16x16 的单个八度音阶在 2D 中实现 Perlin 噪声算法。我将其用作地形的高度图数据,但它似乎只能在一个轴上工作。每当样本点移动到 Perlin Noise 网格中新的 Y 部分时,梯度与我预期的非常不同(例如,它经常从 0.98 翻转到 -0.97,这是一个非常突然的变化)。

此图显示了z方向交错的地形(即2D Perlin Noise网格中的y轴)

代码

我把计算要使用哪个样本点的代码放在最后,因为它很长,我相信这不是问题所在,但基本上我缩小了地形以匹配 Perlin 噪声网格 (16x16 ) 然后对所有点进行采样。

点的梯度

所以计算样本点梯度的代码如下:

// Find the gradient at a certain sample point
float PerlinNoise::gradientAt(Vector2 point)
{
    // Decimal part of float
    float relativeX = point.x - (int)point.x;
    float relativeY = point.y - (int)point.y;
    Vector2 relativePoint = Vector2(relativeX, relativeY);

    vector<float> weights(4);

    // Find the weights of the 4 surrounding points
    weights = surroundingWeights(point);


    float fadeX = fadeFunction(relativePoint.x);
    float fadeY = fadeFunction(relativePoint.y);


    float lerpA = MathUtils::lerp(weights[0], weights[1], fadeX);
    float lerpB = MathUtils::lerp(weights[2], weights[3], fadeX);
    float lerpC = MathUtils::lerp(lerpA, lerpB, fadeY);


    return lerpC;
}

点的周边权重

我认为问题出在此处,在计算样本点周围 4 个点的权重的函数中,但我似乎无法弄清楚哪里出了问题,因为所有值在单步执行时的功能。

// Find the surrounding weight of a point
vector<float> PerlinNoise::surroundingWeights(Vector2 point){

    // Produces correct values
    vector<Vector2> surroundingPoints = surroundingPointsOf(point);

    vector<float> weights;

    for (unsigned i = 0; i < surroundingPoints.size(); ++i) {
        // The corner to the sample point
        Vector2 cornerToPoint = surroundingPoints[i].toVector(point);

        // Getting the seeded vector from the grid
        float x = surroundingPoints[i].x;
        float y = surroundingPoints[i].y;
        Vector2 seededVector = baseGrid[x][y];

        // Dot product between the seededVector and corner to the sample point vector
        float dotProduct = cornerToPoint.dot(seededVector);

        weights.push_back(dotProduct);
    }

    return weights;

}

OpenGL 设置和采样点

设置高度图并获取采样点。变量 'wrongA' 和 'wrongA' 是梯度翻转和突然变化的例子。

void HeightMap::GenerateRandomTerrain() {

int perlinGridSize = 16;

PerlinNoise perlin_noise = PerlinNoise(perlinGridSize, perlinGridSize);

numVertices = RAW_WIDTH * RAW_HEIGHT;
numIndices = (RAW_WIDTH - 1) * (RAW_HEIGHT - 1) * 6;

vertices = new Vector3[numVertices];
textureCoords = new Vector2[numVertices];
indices = new GLuint[numIndices];

float perlinScale = RAW_HEIGHT/ (float) (perlinGridSize -1);

float height = 50;

float wrongA = perlin_noise.gradientAt(Vector2(0, 68.0f / perlinScale));
float wrongB = perlin_noise.gradientAt(Vector2(0, 69.0f / perlinScale));

for (int x = 0; x < RAW_WIDTH; ++x) {
    for (int z = 0; z < RAW_HEIGHT; ++z) {
        int offset = (x* RAW_WIDTH) + z;

        float xVal = (float)x / perlinScale;
        float yVal = (float)z / perlinScale;

        float noise = perlin_noise.gradientAt(Vector2( xVal , yVal));

        vertices[offset]        = Vector3(x * HEIGHTMAP_X,      noise * height,  z * HEIGHTMAP_Z);
        textureCoords[offset]   = Vector2(x * HEIGHTMAP_TEX_X,  z * HEIGHTMAP_TEX_Z);

    }
}
numIndices = 0;
for (int x = 0; x < RAW_WIDTH - 1; ++x) {
    for (int z = 0; z < RAW_HEIGHT - 1; ++z) {
        int a = (x      * (RAW_WIDTH)) + z;
        int b = ((x + 1)* (RAW_WIDTH)) + z;
        int c = ((x + 1)* (RAW_WIDTH)) + (z + 1);
        int d = (x      * (RAW_WIDTH)) + (z + 1);

        indices[numIndices++] = c;
        indices[numIndices++] = b;
        indices[numIndices++] = a;

        indices[numIndices++] = a;
        indices[numIndices++] = d;
        indices[numIndices++] = c;
    }
}
BufferData();

}

原来问题出在插值阶段:

 float lerpA = MathUtils::lerp(weights[0], weights[1], fadeX);
 float lerpB = MathUtils::lerp(weights[2], weights[3], fadeX);
 float lerpC = MathUtils::lerp(lerpA, lerpB, fadeY);

我在 y 轴上的插值方式错误,所以它应该是:

lerp(lerpB, lerpA, fadeY)

而不是:

lerp(lerpA, lerpB, fadeY)