Created
June 23, 2020 23:05
-
-
Save KonradLinkowski/19e00a7ce96218926593b9f06f0fa385 to your computer and use it in GitHub Desktop.
Simple1DNoise ESNext version of this: https://www.michaelbromley.co.uk/blog/simple-1d-noise-in-javascript/
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
class Simple1DNoise { | |
#MAX_VERTICES = 256; | |
#MAX_VERTICES_MASK = this.#MAX_VERTICES - 1; | |
amplitude = 1; | |
scale = 0.5; | |
#r = []; | |
constructor() { | |
for (let i = 0; i < this.#MAX_VERTICES; ++i) { | |
this.#r.push(Math.random()); | |
} | |
} | |
getVal(x) { | |
const scaledX = x * this.scale; | |
const xFloor = Math.floor(scaledX); | |
const t = scaledX - xFloor; | |
const tRemapSmoothstep = t * t * (3 - 2 * t); | |
const xMin = xFloor & this.#MAX_VERTICES_MASK; | |
const xMax = (xMin + 1) & this.#MAX_VERTICES_MASK; | |
const y = this.#lerp(this.#r[xMin], this.#r[xMax], tRemapSmoothstep); | |
return y * this.amplitude; | |
} | |
#lerp(a, b, t) { | |
return a * (1 - t) + b * t; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment