๐Fractal noise
Fractal noise is a technique of getting a more interesting noise by layering multiple gradient noises. Each gradient noise is called an octave and each consecutive octave decreases in amplitude and increase in frequency to add smaller details on top of previous larger noises.
The algorithm usually has 3 extra parameters:
octaves โ how many noise layers are used.
persistence controls how fast amplitude decreases. Increasing persistence will generate a more rugged terrain. Smaller values will generate a more smooth terrain
lacunarity controls how fast frequency increases. Higher values result in more and more small features being added. Lower values will add larger features.
Most common values are:
octaves = 8
persistence = 0.5
lacunarity = 2.0
Sample implementation
import type Simplex from 'rot-js/lib/noise/simplex';
import * as ROT from 'rot-js';
export type FractalNoiseOptions = {
seed: number;
octaves: number;
lacunarity: number;
persistence: number;
};
export class FractalNoise {
options: FractalNoiseOptions;
simplex: Simplex;
constructor(options: FractalNoiseOptions) {
this.options = options;
const storedState = ROT.RNG.getState();
ROT.RNG.setSeed(this.options.seed);
this.simplex = new ROT.Noise.Simplex();
ROT.RNG.setState(storedState);
}
get(x: number, y: number): number {
let value = 0;
for (let i = 0; i < this.options.octaves; ++i) {
const amplitude = this.options.persistence ** i;
const frequency = this.options.lacunarity ** i;
value +=
amplitude *
this.simplex.get(
(x + i * 1000) * frequency,
(y + i * 1000) * frequency
);
}
return value;
}
}