Perlin噪声平方不匹配

时间:2019-03-30 20:21:13

标签: c perlin-noise

我试图弄清楚Perlin噪声如何在二维中起作用。

article from Wikipediathis tutorial之后,我为C实现了一个实现:

#include <stdlib.h>

// array has 3 gradient vectors in each row
const float GRAD[] =
{
        -0.8,  0.5,  0.6, -0.3,  0.9, -0.1,
         0.5, -0.9,  0.4,  0.8, -0.5,  0.9,
        -0.1,  0.6, -0.4,  0.5,  0.7, -0.6
};

// dot product of gradient and distance vectors
float dot(float x0, float y0, float x1, float y1)
{
        return x0 * x1 + y0 * y1;
}

// linear interpolation
float lerp(float a0, float a1, float t)
{
        return a0 + t * (a1 - a0);
}

int perlin(int x, int y, int amp)
{
        float xf, yf, x0, x1;
        int i;
        div_t xi, yi;

        xi = div(x, amp);
        yi = div(y, amp);

        // local x and y
        xf = (float)xi.rem / (float)amp;
        yf = (float)yi.rem / (float)amp;

        i  = 3 * (2 * yi.quot + xi.quot);

        x0 = lerp
        (
                dot(xf       , yf      , GRAD[i    ], GRAD[i + 1]),
                dot(1.0f - xf, yf      , GRAD[i + 2], GRAD[i + 3]),
        xf);

        i += 6;

        x1 = lerp
        (
                dot(xf       , 1.0f - yf, GRAD[i    ], GRAD[i + 1]),
                dot(1.0f - xf, 1.0f - yf, GRAD[i + 2], GRAD[i + 3]),
        xf);

        // the final value should be in the range [0..255]
        return (int)(255.0f * lerp(x0, x1, yf));
}

使用amplitude = 400时,输出如下所示:

https://i.imgur.com/r6sxl6j.png

我还尝试通过使用t * t * t * (t * (t * 6 - 15) + 10)而不是xfyf来实现平滑步功能,但这没有帮助:

https://i.imgur.com/67F88I5.png

1 个答案:

答案 0 :(得分:0)

通过在整个smoothstep函数中将xf函数应用于yfperlin(而不是仅在lerp中使用)并替换{ {1}}至3 * (2 * yi.quot + xi.quot)