Skip to content

Instantly share code, notes, and snippets.

@KonradLinkowski
Created June 23, 2020 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KonradLinkowski/19e00a7ce96218926593b9f06f0fa385 to your computer and use it in GitHub Desktop.
Save KonradLinkowski/19e00a7ce96218926593b9f06f0fa385 to your computer and use it in GitHub Desktop.
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