【发布时间】:2021-08-19 01:22:40
【问题描述】:
我一直在尝试创建一个通用的梯度噪声生成器(它不使用哈希方法来获取梯度)。代码如下:
class GradientNoise {
std::uint64_t m_seed;
std::uniform_int_distribution<std::uint8_t> distribution;
const std::array<glm::vec2, 4> vector_choice = {glm::vec2(1.0, 1.0), glm::vec2(-1.0, 1.0), glm::vec2(1.0, -1.0),
glm::vec2(-1.0, -1.0)};
public:
GradientNoise(uint64_t seed) {
m_seed = seed;
distribution = std::uniform_int_distribution<std::uint8_t>(0, 3);
}
// 0 -> 1
// just passes the value through, origionally was perlin noise activation
double nonLinearActivationFunction(double value) {
//return value * value * value * (value * (value * 6.0 - 15.0) + 10.0);
return value;
}
// 0 -> 1
//cosine interpolation
double interpolate(double a, double b, double t) {
double mu2 = (1 - cos(t * M_PI)) / 2;
return (a * (1 - mu2) + b * mu2);
}
double noise(double x, double y) {
std::mt19937_64 rng;
//first get the bottom left corner associated
// with these coordinates
int corner_x = std::floor(x);
int corner_y = std::floor(y);
// then get the respective distance from that corner
double dist_x = x - corner_x;
double dist_y = y - corner_y;
double corner_0_contrib; // bottom left
double corner_1_contrib; // top left
double corner_2_contrib; // top right
double corner_3_contrib; // bottom right
std::uint64_t s1 = ((std::uint64_t(corner_x) << 32) + std::uint64_t(corner_y) + m_seed);
std::uint64_t s2 = ((std::uint64_t(corner_x) << 32) + std::uint64_t(corner_y + 1) + m_seed);
std::uint64_t s3 = ((std::uint64_t(corner_x + 1) << 32) + std::uint64_t(corner_y + 1) + m_seed);
std::uint64_t s4 = ((std::uint64_t(corner_x + 1) << 32) + std::uint64_t(corner_y) + m_seed);
// each xy pair turns into distance vector from respective corner, corner zero is our starting corner (bottom
// left)
rng.seed(s1);
corner_0_contrib = glm::dot(vector_choice[distribution(rng)], {dist_x, dist_y});
rng.seed(s2);
corner_1_contrib = glm::dot(vector_choice[distribution(rng)], {dist_x, dist_y - 1});
rng.seed(s3);
corner_2_contrib = glm::dot(vector_choice[distribution(rng)], {dist_x - 1, dist_y - 1});
rng.seed(s4);
corner_3_contrib = glm::dot(vector_choice[distribution(rng)], {dist_x - 1, dist_y});
double u = nonLinearActivationFunction(dist_x);
double v = nonLinearActivationFunction(dist_y);
double x_bottom = interpolate(corner_0_contrib, corner_3_contrib, u);
double x_top = interpolate(corner_1_contrib, corner_2_contrib, u);
double total_xy = interpolate(x_bottom, x_top, v);
return total_xy;
}
};
然后我生成一个 OpenGL 纹理以显示如下:
int width = 1024;
int height = 1024;
unsigned char *temp_texture = new unsigned char[width*height * 4];
double octaves[5] = {2,4,8,16,32};
for( int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
double d_noise = 0;
d_noise += temp_1.noise(j/octaves[0], i/octaves[0]);
d_noise += temp_1.noise(j/octaves[1], i/octaves[1]);
d_noise += temp_1.noise(j/octaves[2], i/octaves[2]);
d_noise += temp_1.noise(j/octaves[3], i/octaves[3]);
d_noise += temp_1.noise(j/octaves[4], i/octaves[4]);
d_noise/=5;
uint8_t noise = static_cast<uint8_t>(((d_noise * 128.0) + 128.0));
temp_texture[j*4 + (i * width * 4) + 0] = (noise);
temp_texture[j*4 + (i * width * 4) + 1] = (noise);
temp_texture[j*4 + (i * width * 4) + 2] = (noise);
temp_texture[j*4 + (i * width * 4) + 3] = (255);
}
}
效果不错:
但是 gprof 告诉我 Mersenne twister 占用了我 62.4% 的时间,并且随着纹理的增大而增长。没有其他人会花费如此多的时间。虽然 Mersenne twister 在初始化后很快,但我每次使用它时都对其进行初始化这一事实似乎使它变得相当慢。
这个初始化是 100% 需要的,以确保相同的 x 和 y 在每个整数点生成相同的梯度(因此您需要一个哈希函数或每次都为 RNG 播种)。
我尝试将 PRNG 更改为线性同余生成器和 Xorshiftplus,虽然两者的运行速度都快了几个数量级,但结果却很奇怪:
Xorshiftplus
我试过了:
在利用输出之前运行生成器数次,这会导致执行缓慢或只是不同的工件。
使用初始种子后两次连续运行的输出再次播种 PRNG 并使用病房后的值。结果没有区别。
发生了什么?我该怎么做才能获得与 mersenne twister 相同质量的更快结果?
好的大更新:
我不知道为什么会这样,我知道它与使用的素数有关,但是在搞砸了一点之后,似乎以下工作:
第 1 步,将 x 和 y 值分别合并为种子(并与它们合并一些其他偏移值或附加种子值,该数字应该是素数/非平凡因子)
第 2 步,使用这两个种子结果将生成器再次播种回函数中(就像 geza 所说,制作的种子不好)
第 3 步,当得到结果时,而不是使用模数项 (4) 试图得到,或者 & 3,将结果模数以素数 first 然后应用 & 3。我不确定素数是梅森素数是否重要。
这是 prime = 257 和使用 xorshiftplus 的结果! (注意我用的是 2048 乘 2048,其他的是 256 乘 256)
【问题讨论】:
-
顺便说一句,为什么 rng 是类成员,而不是自动变量?
-
您正在使用 PRNG 作为一个非常昂贵的哈希函数。尝试使用实际(加密?)散列函数。
-
@yurikilochek 我该怎么做?
-
@snb 什么不清楚?只需通过散列函数传递你的种子(或直接坐标),从结果中选择两个位来选择你的向量
-
@snb:你的最后一张(10000 次迭代)图片显示了什么?你的意思是,你播种了 xorshift,然后你生成并忽略了 10000 个数字,然后你使用了第 10001 个?然后,你甚至得到了这张带有这些图案的照片?
标签: c++ random noise perlin-noise procedural-generation