Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Last active December 2, 2021 09:42
Show Gist options
  • Save mattdesl/2cd4e19150862dd5d923f4456fd245e1 to your computer and use it in GitHub Desktop.
Save mattdesl/2cd4e19150862dd5d923f4456fd245e1 to your computer and use it in GitHub Desktop.
curl noise for ThreeJS; this gist is open source under MIT license.
const SimplexNoise = require('simplex-noise');
const simplex = new SimplexNoise();
module.exports = curlNoise;
function curlNoise (x, y, z, out = new THREE.Vector3()) {
const eps = 1.0;
let n1, n2, a, b;
n1 = simplex.noise3D(x, y + eps, z);
n2 = simplex.noise3D(x, y - eps, z);
a = (n1 - n2) / (2 * eps);
n1 = simplex.noise3D(x, y, z + eps);
n2 = simplex.noise3D(x, y, z - eps);
b = (n1 - n2) / (2 * eps);
out.x = a - b;
n1 = simplex.noise3D(x, y, z + eps);
n2 = simplex.noise3D(x, y, z - eps);
a = (n1 - n2)/(2 * eps);
n1 = simplex.noise3D(x + eps, y, z);
n2 = simplex.noise3D(x + eps, y, z);
b = (n1 - n2)/(2 * eps);
out.y = a - b;
n1 = simplex.noise3D(x + eps, y, z);
n2 = simplex.noise3D(x - eps, y, z);
a = (n1 - n2)/(2 * eps);
n1 = simplex.noise3D(x, y + eps, z);
n2 = simplex.noise3D(x, y - eps, z);
b = (n1 - n2)/(2 * eps);
out.z = a - b;
return out;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment