noise()

Math / Random

Description

Returns the Perlin noise value at specified coordinates. Produces smoother and more natural sequences than random().

Syntax

float noise(float x) float noise(float x, float y) float noise(float x, float y, float z)

Parameters

NameTypeDescription
xfloatx-coordinate in noise space
yfloaty-coordinate (optional)
zfloatz-coordinate (optional)

Returns

float

Related

Under the Hood

From Processing.h:

float noise(float x); float noise(float x, float y); float noise(float x, float y, float z);

Under the Hood

From Processing.cpp:

float PApplet::noise(float x, float y, float z) { if (!perlinInit) initPerlin(0); // default seed 0 like Java if (x < 0) x = -x; if (y < 0) y = -y; if (z < 0) z = -z; int xi = (int)x, yi = (int)y, zi = (int)z; float xf = x - xi, yf = y - yi, zf = z - zi; float r = 0.0f, ampl = 0.5f; for (int oct = 0; oct < noiseOctaves; oct++) { int of = xi + (yi << PERLIN_YWRAPB) + (zi << PERLIN_ZWRAPB); float rxf = noise_fsc(xf), ryf = noise_fsc(yf); float n1 = perlinTable[of & PERLIN_SIZE]; n1 += rxf * (perlinTable[(of+1) & PERLIN_SIZE] - n1); float n2 = perlinTable[(of + PERLIN_YWRAP) & PERLIN_SIZE]; n2 += rxf * (perlinTable[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n2); n1 += ryf * (n2 - n1); of += PERLIN_ZWRAP; n2 = perlinTable[of & PERLIN_SIZE]; n2 += rxf * (perlinTable[(of+1) & PERLIN_SIZE] - n2); float n3 = perlinTable[(of + PERLIN_YWRAP) & PERLIN_SIZE]; n3 += rxf * (perlinTable[(of + PERLIN_YWRAP + 1) & PERLIN_SIZE] - n3); n2 += ryf * (n3 - n2); n1 += noise_fsc(zf) * (n2 - n1); r += n1 * ampl; ampl *= noiseFalloff; // Double frequency each octave xi <<= 1; xf *= 2.0f; if (xf >= 1.0f) { xi++; xf--; } yi <<= 1; yf *= 2.0f; if (yf >= 1.0f) { yi++; yf--; } zi <<= 1; zf *= 2.0f; if (zf >= 1.0f) { zi++; zf--; } } return r; } float PApplet::noise(float x) { return noise(x, 0.0f, 0.0f); } float PApplet::noise(float x) { return noise(x, 0.0f, 0.0f); float PApplet::noise(float x, float y) { return noise(x, y, 0.0f); } float PApplet::noise(float x, float y) { return noise(x, y, 0.0f);