Perlin Noise算法似乎不会产生梯度噪声

Cod*_*gle 2 c++ random algorithm noise perlin-noise

我试图用c ++实现Perlin Noise.

首先,问题(我认为)是输出不是我所期望的.目前我只是在greyscaled图像中使用生成的Perlin Noise值,这是我得到的结果: Perlin噪声算法输出

但是,根据我的理解,它应该看起来更像是: 预期的Perlin噪音输出

也就是说,我目前产生的噪音似乎更像是"标准"不规则噪音.

这是我到目前为止实现的Perlin噪声算法:

float perlinNoise2D(float x, float y)
{
    // Find grid cell coordinates
    int x0 = (x > 0.0f ? static_cast<int>(x) : (static_cast<int>(x) - 1));
    int x1 = x0 + 1;
    int y0 = (y > 0.0f ? static_cast<int>(y) : (static_cast<int>(y) - 1));
    int y1 = y0 + 1;

    float s = calculateInfluence(x0, y0, x, y);
    float t = calculateInfluence(x1, y0, x, y);
    float u = calculateInfluence(x0, y1, x, y);
    float v = calculateInfluence(x1, y1, x, y);

    // Local position in the grid cell
    float localPosX = 3 * ((x - (float)x0) * (x - (float)x0)) - 2 * ((x - (float)x0) * (x - (float)x0) * (x - (float)x0));
    float localPosY = 3 * ((y - (float)y0) * (y - (float)y0)) - 2 * ((y - (float)y0) * (y - (float)y0) * (y - (float)y0));

    float a = s + localPosX * (t - s);
    float b = u + localPosX * (v - u);

    return lerp(a, b, localPosY);
}
Run Code Online (Sandbox Code Playgroud)

函数calculateInfluence的作用是为当前网格单元的一个角点生成随机梯度向量和距离向量,并返回这些的点积.它实现为:

float calculateInfluence(int xGrid, int yGrid, float x, float y)
{
    // Calculate gradient vector
    float gradientXComponent = dist(rdEngine);
    float gradientYComponent = dist(rdEngine);

    // Normalize gradient vector
    float magnitude = sqrt( pow(gradientXComponent, 2) + pow(gradientYComponent, 2) );
    gradientXComponent = gradientXComponent / magnitude;
    gradientYComponent = gradientYComponent / magnitude;
    magnitude = sqrt(pow(gradientXComponent, 2) + pow(gradientYComponent, 2));

    // Calculate distance vectors
    float dx = x - (float)xGrid;
    float dy = y - (float)yGrid;

    // Compute dot product
    return (dx * gradientXComponent + dy * gradientYComponent);
}
Run Code Online (Sandbox Code Playgroud)

这里,dist是来自C++ 11的随机数生成器:

std::mt19937 rdEngine(1);
std::normal_distribution<float> dist(0.0f, 1.0f);
Run Code Online (Sandbox Code Playgroud)

线性插值被简单地实现为:

float lerp(float v0, float v1, float t)
{
    return ( 1.0f - t ) * v0 + t * v1;
}
Run Code Online (Sandbox Code Playgroud)

为了实现该算法,我主要使用了以下两个资源:

Perlin Noise FAQ Perlin Noise Pseudo Code

我很难确切地指出我似乎在弄乱的地方.可能是我错误地生成了渐变向量,因为我不太确定它们应该具有什么类型的分布.我尝试过均匀分布,但这似乎会在纹理中产生重复的图案!

同样,可能是我正在平均影响值.从Perlin Noise常见问题解答文章中确切地看出它应该如何完成有点困难.

有没有人对代码可能有什么问题有任何暗示?:)

sam*_*gak 5

看起来你只会产生一个Perlin Noise的八度音阶.要获得类似于所示结果的结果,您需要生成多个八度音程并将它们一起添加.在一系列八度音程中,每个八度音程的网格单元大小应为最后一个八度.

要生成多倍频程噪声,请使用与此类似的内容:

float multiOctavePerlinNoise2D(float x, float y, int octaves)
{
    float v = 0.0f;
    float scale = 1.0f;
    float weight = 1.0f;
    float weightTotal = 0.0f;
    for(int i = 0; i < octaves; i++)
    {
        v += perlinNoise2D(x * scale, y * scale) * weight;
        weightTotal += weight;
        // "ever-increasing frequencies and ever-decreasing amplitudes"
        // (or conversely decreasing freqs and increasing amplitudes)
        scale *= 0.5f; 
        weight *= 2.0f;
    }
    return v / weightTotal;
}
Run Code Online (Sandbox Code Playgroud)

对于额外的随机性,您可以为每个八度音程使用不同种子的随机生成器.而且,可以改变给予每个八度音阶的权重以调整噪声的美学质量.如果每次迭代都没有调整权重变量,那么上面的例子就是"粉红噪声"(频率的每次加倍都带有相同的权重).

此外,您需要使用随机数生成器,每次为给定的xGrid,yGrid对返回相同的值.