【问题标题】:Trying to use perlinnoise for generation of terrain尝试使用 perlinnoise 生成地形
【发布时间】:2021-05-02 14:51:33
【问题描述】:

我正在尝试使用柏林噪声生成程序地形。之前,我只是创建一个 1000 * 1000 的顶点地形,所以我只有一个简单的函数,可以用噪声值填充高度图。

但是现在我正在尝试生成一个地形,该地形会在相机四处移动时生成块,我认为这种方法不合适,因为每个块都将使用它自己的随机高度图生成,并且块看起来彼此不一致。相反,我使用这个头文件来生成只需要 x 和 y 的噪声,但是我给它的任何值似乎都不会生成一致的值,我不知道为什么

class Noise {    
public:
int seed;

Noise(int seed) {
    this->seed = seed;
}

float noise(int x, int y) {
    int n = x + y * 57 + seed;
    n = ( n << 13 ) ^ n;
    float rand = 1 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
    return rand;
}

float noise(int x, int y, int z) {
    int n = x + y + z * 57 + seed;
    n = ( n << 13 ) ^ n;
    float rand = 1 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0f;
    return rand;
}

float smoothNoise(int x, int y) {
    float corners = (noise(x+1, y+1) + noise(x+1, y-1) + noise(x-1, y+1) + noise(x-1, y-1)) / 16.0f;
    float sides = (noise(x, y+1) + noise(x, y-1) + noise(x-1, y) + noise(x+1, y)) / 8.0f;
    float center = noise(x, y) / 4.0f;
    
    return corners + sides + center;
}

float interpolatedNoise(float x, float y) {
    int integerX = (int)x;
    float fractionalX = x - integerX;
    
    int integerY = (int)y;
    float fractionalY = y - integerY;
    
    float v1 = smoothNoise(integerX, integerY);
    float v2 = smoothNoise(integerX + 1, integerY);
    float v3 = smoothNoise(integerX, integerY + 1);
    float v4 = smoothNoise(integerX + 1, integerY + 1);
    
    float i1 = interpolateCosine(v1, v2, fractionalX);
    float i2 = interpolateCosine(v3, v4, fractionalX);
    
    return interpolateCosine(i1, i2, fractionalY);
    
}

// x must be in the range [0,1]
float interpolateLinear(float a, float b, float x) {
    return a * (1 - x) + b * x;
}

float interpolateCosine(float a, float b, float x) {
    float ft = x * (float)glm::pi<float>();
    float f = (1 - ((float)glm::pi<float>())) * 0.5f;
    return a * (1 - f) + b * f;
} 

};`

我这样调用 interpolatedNoise():

for(int y=x; x < config->width;x++)
{
   for(int y = 0; y < config->height;y++)
   { 
       map[x * config->height + y] = noise.interpolatedNoise((config->width * gridPosX + x) * 0.1f,(config->height * gridPosY + y) * 0.1f)/2.0f + 0.5f; // convert from -1:1 to 0:1
   }  
} 

这就是使用这个的地形

但我希望地形平坦。我传递给函数的参数除以某个值。我使这个值越大,地形越随机,盒子越小,我越小,我只会得到更大的盒子,但地形看起来更一致。

【问题讨论】:

    标签: c++ noise procedural-generation


    【解决方案1】:

    我不太确定您是如何调用 Noise 构造函数的,但我看到的一个问题是构造函数中的问题。它应该是this-&gt;seed = seed;,否则您将参数种子分配给其自身的值。噪声的种子变量将只是一个随机数,我觉得这会导致问题。同样,不确定您如何调用 Noise 构造函数,但这是我的最佳猜测。

    【讨论】:

    • 我只是在尝试整理这篇文章中的代码时意外删除了“this->”。感谢您指出这一点。
    • 是的,不确定是不是故意的。会留下评论,但没有足够的代表。
    猜你喜欢
    • 2020-10-03
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多