Created
April 30, 2023 16:36
-
-
Save loucadufault/107ac1289b52b6312cfb3ad707623adf to your computer and use it in GitHub Desktop.
A pattern for emulating multiple seeded gradient noise functions with a single seeded function
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
let globalNoiseOffset = 0 | |
function getReservedNoiseSpace(reservedSize=1) { | |
const ownNoiseOffset = globalNoiseOffset | |
globalNoiseOffset += reservedSize + 1 + Math.random() // introduce a (random) buffer of size at least 1 to prevent continuity between spaces | |
return (...args) => noise(args.map(arg => arg + ownNoiseOffset)) // e.g. https://p5js.org/reference/#/p5/noise | |
} |
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
function optimizedUsage() { | |
noiseSeed(123) // the noise function is seeded only once | |
const myNoise1 = getReservedNoiseSpace() | |
const myNoise2 = getReservedNoiseSpace() | |
// these noise functions are backed by a single-seeded noise function, | |
// but appear as differently-seeded noise functions over the entirety of the reserved size in each n dimensions | |
console.log(myNoise1(0.5)) | |
console.log(myNoise2(0.5)) | |
} | |
function unoptimizedUsage() { | |
const noiseSeed1 = 123, noiseSeed2 = 456 | |
noiseSeed(noiseSeed1) // the noise function must be seeded before each usage | |
const myNoiseValue1 = noise(0.5) | |
// the noise function must be seeded again | |
noiseSeed(noiseSeed2) | |
const myNoiseValue2 = noise(0.5) | |
console.log(myNoiseValue1) | |
console.log(myNoiseValue2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment