connectedpixel.com

actionscript, web development

Flash8 Perlin Wood Texture

Submitted by joelmay on 1 November, 2005 - 12:36pm.

The Flash 8 documentation for perlinNoise() has this intriguing statement:

You can use Perlin noise functions to simulate natural phenomena and landscapes, such as wood grain, clouds, and mountain ranges.

OK. That sounds like fun. But how is this done? It's not immediately obvious, at least not to me. So I googled .

According to these links (here and here), the formula for wood is:

g = perlin(x,y) * 20;
grain = g - int(g);

It looks like we have to do the math on every pixel. After calling perlinNoise, we use getPixel(), mulitply by 20, get the fractional part, then stuff the pixel back in the bitmap with setPixel(). And do that with ever pixel.

That would take forever.

Maybe in AS3 we'll be able to process each pixel individually, but not in AS2. It's too slow. Instead, we need to use the available BitmapData functions to get the same result.

This article describes a way to do that.

Here's an imaginary scan line from a perlin noise bitmap -- just one rgb channel:
Initial Perlin Noise

Notice that the signal is bounded by 0 and 255 -- the limits of the r, g and b channels in a pixel.

The multiplier of 20 in the above formula is arbitrary. It determines how many tree rings you have in your wood. Let's use 4 instead for diagram purposes:
Multiplied Perlin Noise

The g - int(g) part is the same as fraction(g). You can get this by chopping each band and collapsing then together like so:
Chopped Perlin Noise

Believe it or not, if you do this in two dimensions with the amplitude controlling the color, here's what you get:

The key Flash api functions and classes are:

  • BitmapData.perlinNoise() (of course)
  • ColorMatrixFilter
  • BitmapData.threshold()
  • BitmapData.draw() with blend modes.

Here's the code:

The above code will render a wood texture to a bitmap.

I made a component to add a wood texture to a movieclip by dragging and dropping the component onto the movieclip. I used it to quickly add the wood texture to the shapes in the Flash movie below.

Problems:

Performance

It seems that the first time the perlin noise is used in a movie, it takes a long time. But subsquent uses are quick. I'm not sure about this. I need to dig in and figure out whether the problem really is the perlinNoise function, or one of the other bitmap api functions.

Perlin Persistance

Perlin noise is supposed to have a parameter called persistance that is used to control how the amplitude decreases with each perlin octave. The Flash version does not have this. Presumably, the Flash perlin function uses a persistance of 0.5, which seems to be the default in the perlin documentation I've read.

If you want more detail in your wood texture, you need to use more than one octave. But wood is supposed to use a low persistance (< 0.5). If you use more than one octave in your wood with too high a persistance, it looks swirly.

I tried to implement persistance other than 0.5 by layering perlin noise bitmaps with different baseX and baseY values (lots of shifting with ColorMatrix and blending with add and subtract). But I couldn't get it too work and was spending too much time on it. Maybe I'll go back to it later.

Conclusion

Of course, if you want a wood texture, you can simply use a jpeg. It's up to you whether this method is worthwhile.

It was a chance for me to get a handle on perlin noise. I probably will use it, however. I want to make a game with Scrabble-like tiles, and I'd like them to not look identical.

Now, how about those clouds and mountain ranges...

Mike J (not verified) Says:

Clouds and Landscape?

1 November, 2005 - 1:55pm

I had done a quick sample with clouds and landscape generation using the perlin noise before flash 8 was released (IE, no actual documentation). Could probably be improved now... :)

http://mx.coldstorageonline.com/blog.php?bid=42

Ralph Hauwert (not verified) Says:

Clouds

1 November, 2005 - 3:19pm

Hej, great work! Was actually doing generative textures just the other day, hadn't thought of would at all. You can do alot of stuff with perlin noise indeed.

Did this in the pre-release days (and as opposed to mike, had documentation) Perlin Clouds! :

http://www.unitzeroone.com/old/eightball/clouds/

Very subtle effect (sometimes it goes wrong, and shows no clouds, just refresh), but it works realistic, and the clouds move over time....

Ralph.

joelmay Says:

It looks like the distant

2 November, 2005 - 9:21am

It looks like the distant clouds are smaller. Are they really scaled? Or is it random and my preconceived notions of clouds are skewing my perception?

Anonymous (not verified) Says:

Cloud Source Code

21 August, 2006 - 5:41pm

Cloud Source Code?

joelmay Says:

Very cool. I like the idea

2 November, 2005 - 8:52am

Very cool terrain. I like the idea of using a threshold in the noise and assigning blue to colors below that and orange to higher colors. The BitmapData.threshold function would also be useful here.

I wonder if there is a way to use the DisplacementMapFilter to do generate the 3D terrain from the perlin noise instead of doing trig on every pixel. Maybe not, I don't know. It would be tricky.

haber (not verified) Says:

sweet :) thanks.

20 March, 2008 - 10:41am

sweet :) thanks.