Save Expand icon

Ron Valstar
front-end developer

Perlin noise in as3

When I started doing processing I fell in love with the Perlin noise function. A little while later Flash 8 came out including noise. But I was a bit disappointed in their noise implementation, since it was only (and still is) a BitmapData function (without falloff). Perlin noise can be used for much than just bitmaps. Of course you could just getpixel the generated noise data but that only gives you integer noise values.
So I did what I always wanted to do but never did (because I thought it would be too difficult): wrote a Perlin noise class. Actually I just ported the main part from Ken Perlin’s improved noise. But I still had to implement octaves, falloff and seed. The function now looks like Perlin.noise(x:Number,y:Number,z:Number):Number. I’d also like to insert a fourth dimension in there. So we would have three spacial- and one time dimension.

Flash does calculate it’s noise about a hundred times faster than this class does. So if you’re ok with integer input values for noise you might be better off getpixeling the noise data than using this class. But I don’t claim to be the god of code either; there is probably a lot that could be done to optimize this class. So if you have any suggestions please comment.

In the next example the bitmaps are created with the new noise function and setpixel. I’ve set the octaves horizontally and the falloff vertically. The first row has the same falloff as the second but a different seed for each bitmap. The x and y positions of the little ball are also controlled by Perlin noise with an octave of two and a falloff of one half. Click to view example
Here’s the download containing the above example. Things should become clear from the source code. If you still have any questions feel free to comment.
Here’s another example with multiple balls. Each cycle the balls gravity is calculated by feeding it’s position into the noise function. The collision in this example is managed by APE

-update-
There was a slight error in the falloff calculation. I’ve updated the download (version 1.2).

-another update-
Mario Klingemann made some optimizations. The class is now almost twice as fast! I haven’t had time to update my downloads yet so in the meantime get the optimized version here