Random and Perlin Noise
Random numbers have nothing to do with their neighbours; Perlin noise changes smoothly, so it looks natural: clouds, terrain, wood, smoke.
How it works¶
frand(0, 1) gives a new, unrelated number every time it is called, so neighbouring pixels look like television static.
noise2(x, y) (Ken Perlin's improved noise) gives a value for every point that changes smoothly, so close points have close values. It works out a random slope at each corner of a grid of whole numbers and blends between them with a smooth curve, so the result has hills and valleys about one grid square wide. The helper adds four layers ("octaves"), each half the size and half as strong, for detail inside the shapes. It comes out at about 0..1, like p5.js's noise().
The numbers x and y are multiplied by set the zoom: small ones like 0.005 give large, slow shapes; large ones like 0.1 give fine detail.
Put noise through wave() with a big multiplier and the smooth hills turn into contour lines, a lot like wood grain.
noise_seed(n) chooses which noise you get; the same seed always gives the same picture, and the starters' R key moves to the next seed. noise3(x, y, z) is the same with a third input, usually time.
The C is for the raylib pixels starter.
Random: no connection between pixels¶
function shade(x, y, t) {
return grey(frand(0, 1));
}
Noise: smooth variation¶
function shade(x, y, t) {
return grey(noise2(x * 0.02, y * 0.02));
}
Wood grain: noise through a wave¶
static RGB shade(float x, float y, float t)
{
return grey(wave(noise2(x * 0.01f, y * 0.01f) * 40));
}
function shade(x, y, t) {
return grey(wave(noise2(x * 0.01, y * 0.01) * 40));
}
Clouds: time as the third input¶
static RGB shade(float x, float y, float t)
{
return grey(noise3(x * 0.015f, y * 0.015f, t * 0.6f));
}
function shade(x, y, t) {
return grey(noise3(x * 0.015, y * 0.015, t * 0.6));
}
Related¶
- Flow Fields: noise as a direction.
- Domain Warping: noise moving the input of another pattern.
- Layering Detail at a Low Weight: large noise plus fine noise.
- Random Number Generator:
rand()in plain C.