【发布时间】: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