简体   繁体   English

噪音功能如何实际起作用?

[英]How does the noise function actually work?

I've looked into the libnoise sources and found the ValuNoise3D function: 我查看了libnoise源并找到了ValuNoise3D函数:

double noise::ValueNoise3D (int x, int y, int z, int seed)
{
    return 1.0 - ((double)IntValueNoise3D (x, y, z, seed) / 1073741824.0);
}

int noise::IntValueNoise3D (int x, int y, int z, int seed)
{
    // All constants are primes and must remain prime in order for this noise
    // function to work correctly.
    int n = (
        X_NOISE_GEN    * x
      + Y_NOISE_GEN    * y
      + Z_NOISE_GEN    * z
      + SEED_NOISE_GEN * seed)
      & 0x7fffffff;

    n = (n >> 13) ^ n;
    return (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff;
}

But when I am looking at this, it is a magic for me. 但是当我看到这个时,这对我来说是一种魔力。 How does this actually work? 这实际上是如何工作的? I mean why the guy who wrote this, took those prime numbers instead of others? 我的意思是为什么写这篇文章的人拿这些素数代替其他人? Why such equations? 为什么这样的等式 How did he decide to use those equations instead of others? 他是如何决定使用这些方程而不是其他方程式的? Just... how to understand this? 只是......怎么理解这个?

The libnoise Web site has a good explanation of the mathematics behind this noise function. libnoise网站对这种噪声函数背后的数学有很好的解释。 In particular, with regards to the prime numbers: 特别是关于素数:

These large integers are primes. 这些大整数是素数。 These integers may be modified as long as they remain prime; 这些整数可以修改,只要它们保持为素数; non-prime numbers may introduce discernible patterns to the output. 非素数可能会在输出中引入可辨别的模式。

noise::IntValueNoise3D actually operates in two steps: the first step converts the (x, y, z) coordinates to a single integer, and the second step puts this integer through an integer noise function to produce a noise value roughly between -1073741824 and 1073741824. noise::ValueNoise3D just converts that integer to a floating-point value between -1 and 1. noise::IntValueNoise3D实际上noise::IntValueNoise3D操作:第一步将(x,y,z)坐标转换为单个整数,第二步将整数通过整数噪声函数产生一个大致在-1073741824之间的噪声值。 1073741824. noise::ValueNoise3D只是将该整数转换为介于-1和1之间的浮点值。

As for why noise::IntValueNoise3D performs all those convoluted operations, it basically boils down to the fact that this particular sequence of operations produces a nice, noisy result with no clear pattern visible. 至于为什么noise::IntValueNoise3D执行所有那些复杂的操作,它基本上归结为这个特定的操作序列产生一个漂亮,嘈杂的结果,没有明显的模式可见。 This is not the only sequence of operations that could have been used; 这不是唯一可以使用的操作顺序; anything that produces a sufficiently noisy result would have worked. 任何产生足够嘈杂结果的东西都会起作用。

There is an art to randomness. 随机性有一种艺术。 There are are many things that make a pseudorandom number "look good." 有很多东西使伪随机数“看起来很好”。 For a lot of 3d function, the most important thing in making it "look good" is having a proper looking frequency distribution. 对于很多3D功能而言,使其“看起来很好”最重要的是拥有适当的频率分布。 Anything which ensures a good frequency distribution modulo 2^32 will yield very good looking numbers. 确保以2 ^ 32为模数的良好频率分布的任何东西都将产生非常好看的数字。 Multiplying by a large prime number yields good frequency distributions because they share no factors with 2^32. 乘以大素数会产生良好的频率分布,因为它们没有2 ^ 32的因子共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM