Skip to content

Instantly share code, notes, and snippets.

@andrusenn
Created October 28, 2022 21:17
Show Gist options
  • Save andrusenn/7d5958eff8557f64cd3a8f805df4a583 to your computer and use it in GitHub Desktop.
Save andrusenn/7d5958eff8557f64cd3a8f805df4a583 to your computer and use it in GitHub Desktop.
simplex noise by Jonas Wagner / Only 3d part
// simplex noise by Jonas Wagner
// Only 3d
const F3 = 1.0 / 3.0;
const G3 = 1.0 / 6.0;
const fastFloor = (x) => Math.floor(x) | 0;
const grad3 = new Float64Array([
1, 1, 0, -1, 1, 0, 1, -1, 0, -1, -1, 0, 1, 0, 1, -1, 0, 1, 1, 0, -1, -1, 0,
-1, 0, 1, 1, 0, -1, 1, 0, 1, -1, 0, -1, -1,
]);
function createNoise3D(random = Math.random) {
const perm = buildPermutationTable(random);
const permGrad3x = new Float64Array(perm).map((v) => grad3[(v % 12) * 3]);
const permGrad3y = new Float64Array(perm).map(
(v) => grad3[(v % 12) * 3 + 1],
);
const permGrad3z = new Float64Array(perm).map(
(v) => grad3[(v % 12) * 3 + 2],
);
return function noise3D(x, y, z) {
let n0, n1, n2, n3;
const s = (x + y + z) * F3;
const i = fastFloor(x + s);
const j = fastFloor(y + s);
const k = fastFloor(z + s);
const t = (i + j + k) * G3;
const X0 = i - t;
const Y0 = j - t;
const Z0 = k - t;
const x0 = x - X0;
const y0 = y - Y0;
const z0 = z - Z0;
let i1, j1, k1;
let i2, j2, k2;
if (x0 >= y0) {
if (y0 >= z0) {
i1 = 1;
j1 = 0;
k1 = 0;
i2 = 1;
j2 = 1;
k2 = 0;
} else if (x0 >= z0) {
i1 = 1;
j1 = 0;
k1 = 0;
i2 = 1;
j2 = 0;
k2 = 1;
} else {
i1 = 0;
j1 = 0;
k1 = 1;
i2 = 1;
j2 = 0;
k2 = 1;
}
} else {
if (y0 < z0) {
i1 = 0;
j1 = 0;
k1 = 1;
i2 = 0;
j2 = 1;
k2 = 1;
} else if (x0 < z0) {
i1 = 0;
j1 = 1;
k1 = 0;
i2 = 0;
j2 = 1;
k2 = 1;
} else {
i1 = 0;
j1 = 1;
k1 = 0;
i2 = 1;
j2 = 1;
k2 = 0;
}
}
const x1 = x0 - i1 + G3;
const y1 = y0 - j1 + G3;
const z1 = z0 - k1 + G3;
const x2 = x0 - i2 + 2.0 * G3;
const y2 = y0 - j2 + 2.0 * G3;
const z2 = z0 - k2 + 2.0 * G3;
const x3 = x0 - 1.0 + 3.0 * G3;
const y3 = y0 - 1.0 + 3.0 * G3;
const z3 = z0 - 1.0 + 3.0 * G3;
const ii = i & 255;
const jj = j & 255;
const kk = k & 255;
let t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
if (t0 < 0) n0 = 0.0;
else {
const gi0 = ii + perm[jj + perm[kk]];
t0 *= t0;
n0 =
t0 *
t0 *
(permGrad3x[gi0] * x0 +
permGrad3y[gi0] * y0 +
permGrad3z[gi0] * z0);
}
let t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
if (t1 < 0) n1 = 0.0;
else {
const gi1 = ii + i1 + perm[jj + j1 + perm[kk + k1]];
t1 *= t1;
n1 =
t1 *
t1 *
(permGrad3x[gi1] * x1 +
permGrad3y[gi1] * y1 +
permGrad3z[gi1] * z1);
}
let t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
if (t2 < 0) n2 = 0.0;
else {
const gi2 = ii + i2 + perm[jj + j2 + perm[kk + k2]];
t2 *= t2;
n2 =
t2 *
t2 *
(permGrad3x[gi2] * x2 +
permGrad3y[gi2] * y2 +
permGrad3z[gi2] * z2);
}
let t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
if (t3 < 0) n3 = 0.0;
else {
const gi3 = ii + 1 + perm[jj + 1 + perm[kk + 1]];
t3 *= t3;
n3 =
t3 *
t3 *
(permGrad3x[gi3] * x3 +
permGrad3y[gi3] * y3 +
permGrad3z[gi3] * z3);
}
return 32.0 * (n0 + n1 + n2 + n3);
};
}
function buildPermutationTable(random) {
const tableSize = 512;
const p = new Uint8Array(tableSize);
for (let i = 0; i < tableSize / 2; i++) {
p[i] = i;
}
for (let i = 0; i < tableSize / 2 - 1; i++) {
const r = i + ~~(random() * (256 - i));
const aux = p[i];
p[i] = p[r];
p[r] = aux;
}
for (let i = 256; i < tableSize; i++) {
p[i] = p[i - 256];
}
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment