【问题标题】:Perlin noise squares don't matchPerlin 噪声平方不匹配
【发布时间】:2019-08-21 10:51:53
【问题描述】:

我试图弄清楚 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 的输出如下所示:

我也尝试过使用t * t * t * (t * (t * 6 - 15) + 10) 代替xfyf 的smoothstep 功能,但没有帮助:

【问题讨论】:

    标签: c perlin-noise


    【解决方案1】:

    通过将smoothstep 函数应用于整个perlin 函数中的xfyf(而不是仅在lerp 中使用它)并将3 * (2 * yi.quot + xi.quot) 替换为2 * (3 * yi.quot + xi.quot) 来解决问题

    【讨论】:

      猜你喜欢
      • 2014-02-15
      • 2012-03-08
      • 2016-08-17
      • 2020-06-06
      • 2011-09-20
      • 2021-06-30
      • 2013-07-23
      • 2020-06-22
      • 2011-03-19
      相关资源
      最近更新 更多