Index: binaries/data/mods/public/maps/random/rmgen/perlinNoise.js =================================================================== --- /dev/null +++ binaries/data/mods/public/maps/random/rmgen/perlinNoise.js @@ -0,0 +1,29 @@ +/** + * Perlin noise. + * @param {object} point - Vector2d instance. + * @param {number} iterations - Must be > 0. The higher the more detailed the noise. + * @param {number} scale - A value > 1 zooms in and < 1 zooms out the noise. + * @param {number} multiplier - Range (0,1). Frequency reduction after each iteration. + * @return {number} - Range [-1,1]. + */ +function perlinNoise(point, iterations = 3, scale = 5, multiplier = 0.75) +{ + if (iterations <= 0) + return 0; + + let value = 0; + let frequency = 1 / scale; + let weight = 1; + let weightAccumulator = 0; + + for (let i = 0; i < iterations; ++i) + { + value += weight * valueNoise2d(point.x * frequency, point.y * frequency); + weightAccumulator += weight; + + frequency /= multiplier; + weight *= multiplier; + } + + return value / weightAccumulator; +}