Predefined Functions - perlinNoise
Description:
The perlinNoise function creates some kind of "random noise". Perlin noise is called after Ken Perlin who invented it. Using some further processing you can apply displacements or color changes to a base value. "Further processing" actually means that calling the function will only return a random number which needs to be scaled. You then need to call the function again and scale again, and so on. The resulting value then can directly be used as an index into a color table in order to apply some kind of texture coloring like cloud effects. The standard algorithm for using perlin noise is as follows: real stepInv; real kCnt; real noiseResult,noiseSum; vector noisePos; real freq,octaveMult; // external parameters following... parameter real scaleStart; parameter real octaves; parameter double step; // at first, initialize the position according to "z", // we thus want to receive a random value for this position. noisePos=z; // we repeatedly call perlinNoise and scale each result // according to freq freq= scaleStart; // The result will be put into noiseSum noiseSum = 0; // octaves specifies how often we want to call perlinNoise. // The more often we call and scale the function, the more // "wild" the random pattern will be kCnt=octaves; stepInv=1/step; while (kCnt>0) { noiseResult=freq*perlinNoise(partx(noisePos), party(noisePos), partz(noisePos)); if (kCnt<1) { octaveMult=kCnt; } else { octaveMult=1; } noiseSum = noiseSum + octaveMult*noiseResult; // next time we call perlinNoise we scale the resulting // noise by freq, so let's change it to have different scales! freq = freq * step; // of course we must adjust noisePos to a new position // according to the scale factor: If we do not change // noisePos, perlinNoise will return exactly the same number... noisePos = stepInv*noisePos; kCnt=kCnt-1; } In order to see what this is all about, have a look at the images below: Here we use the above algorithm in order to assign a color value to a pixel:
As you can see, octaves=1 means: perlinNoise has been called once: The result is a smooth random function ranging from -1 to 1. It's smooth in all three input directions, i.e. the values smoothly change in each direction. In fact, it's almost too smooth... And that's the reason why one needs to call perlinNoise several times and add different scales of the noise: octaves=3 looks better, and octaves=8 actually calls perlinNoise 8 times, adds up the 8 differently scaled values, and the result is interesting... By the way, perlinNoise3 does almost the same, the only exception is that it does not return a real number, but a vector. perlinNoise is ideal if you want to retrieve a single real value (for example for coloring), perlinNoise3 is ideal if you need three random values, for example if you want to displace a value. |