Last active
October 3, 2019 06:22
-
-
Save up209d/0a1444f3d9a30cb8bde0af23f9de019d to your computer and use it in GitHub Desktop.
Most basic 1D noise generating (running from 0 - 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const generateNoise = (scale = 0.01, resolution = 1024) => { | |
let currentScale = scale; | |
let currentResolution = resolution >= 2 ? resolution : 2; | |
const randomArray = Array(currentResolution) | |
.fill(0) | |
.map(() => Math.random()); | |
return { | |
noiseAt: pos => { | |
const currentSeed = scale * pos; | |
const frag = currentSeed % 1; | |
const smooth = Math.pow(frag, 2) * (3 - 2 * frag); | |
const min = Math.floor(currentSeed) % (currentResolution - 1); | |
const max = min + 1; | |
return randomArray[min] * (1 - smooth) + randomArray[max] * smooth; | |
}, | |
updateScale: (updateScale = scale) => { | |
if (updateScale !== currentScale) { | |
currentScale = updateScale; | |
} | |
}, | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment