Tuesday, February 18, 2014

Fractal Noise Generation

Fractal noise (also called fractional Brownian noise or fBm) is probably the most popular method of generating noise, and with good reason. It gives very natural and beautiful looking results, while still allowing the coder to control many aspects of it's generation. Speedwise, it is pretty average, not the fastest, but by no means the slowest either.
Here is what it looks like:



Fun Fact: The name Fractal is not related to mathematical fractals, but is actaully a shortened version of the word fractional.

Fractal noise introduces a concept called octaves. An octave is a interpolated noise array with a period of 2 ^ x, with x being the octave number (it can also be any other type of noise that allows periods). Thus as the octave number rises, the smoothing of the noise becomes broader and broader.
Fractal noise is generated by adding multiple octaves together, with the lower octaves having a smaller amplitude than the upper octaves. The amplitude of a given octave is the persistence times the amplitude of the octave before it.
So if persistence was .5 and the starting amplitude was 1,
Octave      Amplitude
3               1
2               1/2
1               1/4
0               1/8

With all this infomation in mind, it is simple to make a method that creates Fractal noise, using interpolated noise octaves, given the start and end octaves, the persistence, and the starting amplitude:
double currentAmp = startingAmp;
for(int octave = broadOctave; octave >= fineOctave; octave--)
{
    int period = 1 << octave;// 2 ^ octave
    smooth = InterpNoise.interp_noise_array(rand.nextLong(), interp, period, period);
   
    for(int x = noise.minX; x <= noise.maxX; x++)
    {
        for(int y = noise.minY; y <= noise.maxY; y++)
        {
            noise[x][y] += smooth[x][y] * currentAmp;
            currentAmp *= persistence;
        }
    }
}
And there you have it, beautiful silky-smooth Fractal noise. If you want to play around with what Fractal noise looks like with various values, here is an applet that will let you play around.
Controls:
Space = next interpolation function
Q = increase fine octave
E = decrease fine octave
A = increase broad octave
D = decrease broad octave
Z = increase persistence
C = decrease persistence
S = new random seed
Shift = toggle color mode
Enter (downloadable version only) = save screenshot


If the applet doesn't load, trying refreshing the webpage. May not work on mobiles.

Demos: https://drive.google.com/folderview?id=0B_jXYEquMamINGVGQTM0cTJzdmM&usp=sharing
Source Code: https://github.com/f4113nb34st/Println/

No comments:

Post a Comment