voxeltest/src/noise.js

56 lines
1.7 KiB
JavaScript

import * as SimplexNoise from 'simplex-noise'
class BrownianSimplexNoise extends SimplexNoise {
// amplitude - Controls the amount the height changes. The higher, the taller the hills.
// persistence - Controls details, value in [0,1]. Higher increases grain, lower increases smoothness.
// octaves - Number of noise layers
// period - Distance above which we start to see similarities. The higher, the longer "hills" will be on a terrain.
// lacunarity - Controls period change across octaves. 2 is usually a good value to address all detail levels.
constructor (rand, amplitude = 15, period = 0.01, persistence = 0.4, lacunarity = 2, octaves = 5) {
super(rand)
this.amplitude = amplitude
this.period = period
this.lacunarity = lacunarity
this.persistence = persistence
this.octaves = octaves
}
// Fractal/Fractional Brownian Motion (fBm) summation of 3D Perlin Simplex noise
getNoise3D (x, y, z) {
let output = 0.0
let denom = 0.0
let frequency = this.period
let amplitude = this.amplitude
for (let i = 0; i < this.octaves; i++) {
output += (amplitude * this.noise3D(x * frequency, y * frequency, z * frequency))
denom += amplitude
frequency *= this.lacunarity
amplitude *= this.persistence
}
return (output / denom)
}
getNoise2D (x, y) {
let output = 0.0
let denom = 0.0
let frequency = this.period
let amplitude = this.amplitude
for (let i = 0; i < this.octaves; i++) {
output += (amplitude * this.noise2D(x * frequency, y * frequency))
denom += amplitude
frequency *= this.lacunarity
amplitude *= this.persistence
}
return (output / denom)
}
}
export { BrownianSimplexNoise }