Class: PerlinNoise
Perlin Noise In this code, the PerlinNoise class encapsulates the Perlin noise generation logic. The generatePermutation function generates a random permutation of integers used for gradient calculations. The generateGradient function generates random gradient vectors. The fade function provides smooth interpolation, and lerp performs linear interpolation. The grad function calculates the dot product between the gradient vector and the distance vector. The noise function generates Perlin noise for a given 3D coordinate (x, y, z).
Example
const scale = 10;
const perlin = new PerlinNoise();
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
for (let z = 0; z < depth; z++) {
const noiseValue = perlin.noise(x / scale, y / scale, z / scale);
}
}
}