Saturday, February 15, 2014

Basic Noise Generation

In order to produce more complex (and better looking) noise like Fractal Noise, it is important to have a good, robust noise generator. A noise generator is simply a random number generator that takes a seed and one or more coordinates. It must also produce the same value over and over again for a given set of inputs. Ideally, these values should be between 0 and 1, but that is not required.



The important thing in a noise generator is that it just mangles the inputted numbers. Unlike pretty much everything else that involves primitives, overflowing your ints is actually a good thing. Also, primes are important in noise generation. Using primes in your calculations reduces the chance that patterns will emerge, which makes your random number generator suddenly not so random.

Here is an example of one that takes four args:

public static final double noise_gen(double x, double y, double z, long seed)
{
    rand.setSeed(seed);
    double xmulti = rand.nextDouble() * 1000;
    double ymulti = rand.nextDouble() * 1000;
    double zmulti = rand.nextDouble() * 1000;
    int n = (int)((x + ((y + (z * zmulti)) * ymulti)) * xmulti);
    n = (n << 13) ^ n;
    return ((1.0 - ((n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff) / 1073741824.0) * .5) + .5;
}

(rand is a private static final instance of Random)

It takes four args: a seed, and three coordinates: x, y and z. The nice thing about this function is that is is scalable. If I want fewer args, I replace unneeded ones with 0's. If I need more args, more can be added in. It produces images that look like this:


It's not very pretty to look at is it? Lucky this is just a base for some more complex noise functions that are more easy on the eyes.

Source Code: https://github.com/f4113nb34st/Println/

No comments:

Post a Comment