﻿ Perlin Noise
Perlin Noise
 Site Map Feedback

 Mosaic Perlin Simplex Voronoi

The ideas for creating coherent noise discussed in the Noise page are now going to be extended to two dimensions. Perlin Noise is used extensively by the film industry for clouds, water, smoke, and dirt. The methods in Noise.h which generate Perlin Noise are simply called Signed and Unsigned and return noise (approximately) in the range [-1,1] and [0,1] respectively. If many samples are going to be added, the Signed version keeps the average sum near in the Interval [-1,1].

The following code was used to create the images below using the 2D Perlin Noise method which takes an x and y coordinate, which should be scaled to pass through several integers for the algorithm to produce good coherent noise. The next parameter is the Octave count: how many samples to add together, and the final Level of Detail (Persistence) parameter is defines the level of each subsequent octave that will be added to the total. The code as written will take five samples per pixel, the first sample at full amplitude, half of the next sample (Persistence=0.5), a quarter of the next sample (Persistence*Persistence), an eighth of the third sample (Persistence*Persistence*Persistence) etc.

for(WORD x=GetWidth(); x--; ) { // Loops are best comparing the counter with zero, so this counts down.
double u=double(x)/Width; // Calculate the parameter (ratio in the range [0,1]) indicating how far through the loop is.
for(DWORD y=GetHeight(); y--;) {
double v=double(y)/GetHeight(); // Calculate the parameter (ratio in the range [0,1]) indicating how far through the loop is.
BYTE B=BYTE(Round(255*Noise::Unsigned(x/10.0,y/10.0, 5, 0.5))); // Turn the parameter into a Greyscale Level.
SetPixel(x,y, RGB(B,B,B));
} }
GetWidth(), GetHeight() and SetPixel are members of CPixelBlock which was used as a base class.
Round is declared in Global.h.

These are the resulting images (the number on the right is the number of Octaves):

The image where x and y are not scaled shows the raw coherent noise before it is processed to give 'improved' results. At four octaves the image looks distinctly cloud-like and going up to ten octaves doesn't change things much. This cloud-like image is very valuable. Because it was made from a Psuedo Random Number Generator that isn't using a Seed it will have the same shape given the same numbers and can be made to create tileable textures. With the help of the Filters and Colour Mixers photorealistic images can be generated by the computer.

### Another Dimension

Adding a third dimension allows noise over a three dimensional surface, or 2D animation with time as the third dimension. To create a looping animation, the following code uses z in the Interval [0,1] and uses that to blend between two adjacent noise function cells:

for(int Frame=20; Frame; --Frame) {
double z=Frame*1/20.0;
for(WORD x=GetWidth(); x--;) {
for(WORD y=GetHeight(); y--;) {
double d=(1-z)*Noise::Get(x/10.0, y/10.0, z  , 10,0.5)
+z *Noise::Get(x/10.0, y/10.0, z-1, 10,0.5);
SetLevel(x,y, Round(255*Clamped(0.0, d, 1.0)));
} }
CString S;
S.Format("Frame%02u.bmp",Frame);
Save(S);
}
GetWidth(), GetHeight(), SetLevel() and Save are members of CPixelBlock which was used as a base class.
Round and Clamped are declared in Global.h.

An example of the simplest use of Perlin Noise is given in the Button Tutorial which ends with code showing the unwrapped code. The button reflection has noise added to make it look like clouds or 'something' are being reflected.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.