Skip to content

Instantly share code, notes, and snippets.

@up209d
Last active October 3, 2019 06:22
Show Gist options
  • Save up209d/0a1444f3d9a30cb8bde0af23f9de019d to your computer and use it in GitHub Desktop.
Save up209d/0a1444f3d9a30cb8bde0af23f9de019d to your computer and use it in GitHub Desktop.
Most basic 1D noise generating (running from 0 - 1)
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