Pregunta

I've looked into the libnoise sources and found the ValuNoise3D function:

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?

¿Fue útil?

Solución

The libnoise Web site has a good explanation of the mathematics behind this noise function. 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.

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. This is not the only sequence of operations that could have been used; anything that produces a sufficiently noisy result would have worked.

Otros consejos

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. Anything which ensures a good frequency distribution modulo 2^32 will yield very good looking numbers. Multiplying by a large prime number yields good frequency distributions because they share no factors with 2^32.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top