Created
December 2, 2021 08:40
-
-
Save Iranon/926442ba2a4eee095d9f53c47997ba36 to your computer and use it in GitHub Desktop.
Perlin Noise from p5.js with an example that feed the function with a value incremented each second.
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
/*============================================================================== | |
|| From Perlin Noise implementation in p5.js || | |
|| @'https://github.com/processing/p5.js/blob/v1.4.0/src/math/noise.js#L36' || | |
==============================================================================*/ | |
//--- The resulting value will always be between 0.0 and 1.0 --- | |
const PERLIN_YWRAPB = 4; | |
const PERLIN_YWRAP = 1 << PERLIN_YWRAPB; | |
const PERLIN_ZWRAPB = 8; | |
const PERLIN_ZWRAP = 1 << PERLIN_ZWRAPB; | |
const PERLIN_SIZE = 4095; | |
let perlin_octaves = 4; // default to medium smooth | |
let perlin_amp_falloff = 0.5; // 50% reduction/octave | |
const scaled_cosine = i => 0.5 * (1.0 - Math.cos(i * Math.PI)); | |
let perlin; // will be initialized lazily by noise() or noiseSeed() | |
pNoise = function(x, y = 0, z = 0) { | |
if (perlin == null) { | |
perlin = new Array(PERLIN_SIZE + 1); | |
for (let i = 0; i < PERLIN_SIZE + 1; i++) { | |
perlin[i] = Math.random(); | |
} | |
} | |
if (x < 0) { | |
x = -x; | |
} | |
if (y < 0) { | |
y = -y; | |
} | |
if (z < 0) { | |
z = -z; | |
} | |
let xi = Math.floor(x), | |
yi = Math.floor(y), | |
zi = Math.floor(z); | |
let xf = x - xi; | |
let yf = y - yi; | |
let zf = z - zi; | |
let rxf, ryf; | |
let r = 0; | |
let ampl = 0.5; | |
let n1, n2, n3; | |
for (let o = 0; o < perlin_octaves; o++) { | |
let of = xi + (yi << PERLIN_YWRAPB) + (zi << PERLIN_ZWRAPB); | |
rxf = scaled_cosine(xf); | |
ryf = scaled_cosine(yf); | |
n1 = perlin[of & PERLIN_SIZE]; | |
n1 += rxf * (perlin[(of + 1) & PERLIN_SIZE] - n1); | |
n2 = perlin[(of + PERLIN_YWRAP) & PERLIN_SIZE]; | |
n2 += rxf * (perlin[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n2); | |
n1 += ryf * (n2 - n1); | |
of += PERLIN_ZWRAP; | |
n2 = perlin[of & PERLIN_SIZE]; | |
n2 += rxf * (perlin[(of + 1) & PERLIN_SIZE] - n2); | |
n3 = perlin[(of + PERLIN_YWRAP) & PERLIN_SIZE]; | |
n3 += rxf * (perlin[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n3); | |
n2 += ryf * (n3 - n2); | |
n1 += scaled_cosine(zf) * (n2 - n1); | |
r += n1 * ampl; | |
ampl *= perlin_amp_falloff; | |
xi <<= 1; | |
xf *= 2; | |
yi <<= 1; | |
yf *= 2; | |
zi <<= 1; | |
zf *= 2; | |
if (xf >= 1.0) { | |
xi++; | |
xf--; | |
} | |
if (yf >= 1.0) { | |
yi++; | |
yf--; | |
} | |
if (zf >= 1.0) { | |
zi++; | |
zf--; | |
} | |
} | |
return r; | |
}; | |
//============================================================= | |
//- Example | |
//--------------------------------------- | |
//Feeding the noise function with a value | |
//incremented by 1 each second | |
let t = 0; | |
let interval = () => { | |
setTimeout(() => { | |
t += 1; | |
console.log(pNoise(t)); | |
interval(); | |
}, 1000); | |
} | |
interval(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment