O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Perlin Noise

Problem

You want to create random organic effects, such as clouds, smoke, or water.

Solution

Use the perlinNoise() method of the BitmapData class.

Discussion

Like the noise() method, perlinNoise() creates random patterns on a bitmap. However, Perlin noise uses an algorithm that produces smooth, organic-looking textures. It was created by Ken Perlin for creating textures in the movie Tron. These textures are perfect for use as explosions, smoke, water, and many other natural-looking effects, and since they are generated by an algorithm, they require much less memory than bitmap-based textures. The usage is as follows:

bitmap.perlinNoise(baseX, baseY, octaves, seed, stitch, fractal,
                channels, grayscale, offsets);

The first six parameters are necessary; the final three are optional. Since there are so many parameters to consider, let’s create a simple example and then see what each one does. The following code creates a bitmap, applies Perlin noise to it and then displays it:

bitmap = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0xff000000);
bitmap.perlinNoise(100, 100, 1, 1000, false, false, 1, true, null);
var image:Bitmap = new Bitmap(bitmap);
addChild(image);

Add this code to a new class, and run it to see a simple Perlin noise pattern. See Figure 8-1 for an example of what you should see. Now you can start changing parameters and see what effect the changes have.

First off, baseX and baseY determine the size of the pattern. Here they are set to 100 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required