connectedpixel.com

actionscript, web development

Flash8 Perlin Marble Texture

Submitted by joelmay on 8 November, 2005 - 2:25pm.

Last week, I wrote an article about using Flash 8 Perlin noise to generate a wood texture. Today, it's marble.

According to this link, the Perlin formula for marble is:

texture = cosine( x + perlin(x,y,z) )

Again, we don't want to do math on every pixel -- too slow. So, how do we do this with the existing Flash 8 API? First, let's pretend that the perlin term in the above equation is not there. Here's what our image would look like (this image is arbitrarily blue monochrome):
Sinusoid Base for Marble

One pixel scan line of the above image was generated with Math.cos() and BitmapData.setPixel(). The bitmap was then stretched vertially -- no need to call setPixel on w * h pixels -- w x 1 is enough.

The perlin term randomly distorts the phase. In the context of the above image, this means pixels are randomly displaced horizontally. A DisplacementMapFilter driven by perlin is exactly what we need:
Marble, Not-Color Corrected

Then we can map the colors to realistic marble colors using ColorMatrixFilter:
Marble, Not-Color Corrected

I find the sinusoid basis for the marble to be too regular. We can use perlin noise instead of a sinusoid and our pre-DisplacementMapFilter image looks like this:
Marble, Not-Color Corrected

You can play with the settings here. BTW, I find it takes a lot of trial-and-error to find parameters that result in a decent marble.

Here's the code:

Here are a few generated marbles.

Metah (not verified) Says:

Very nice perlin !

9 November, 2005 - 11:20am

Woaaw, thanks for this source. Seems very powerful ;)
Cheers.
Ahmet

joelmay Says:

Thanks. More to come.

9 November, 2005 - 12:01pm

Thanks. More to come.
Joel

Human (not verified) Says:

thanks

2 January, 2008 - 8:47am

Thanks for this great article.

oyunlar (not verified) Says:

flash

4 April, 2008 - 2:25am

Very good Graphics. Thanks For watch

DOOMy (not verified) Says:

wth!?

20 January, 2008 - 1:35am

Damn it, i'm to dumb for this... will anyone post a .fla file so that any idiot can understand? Please?

Charity cds (not verified) Says:

posted

20 January, 2008 - 5:54am

I think these have been posted on a later topic that deals with both the wood texture and the marble texture.

3D artist (not verified) Says:

I learn something new today

31 January, 2008 - 9:25am

As a 3D artist, I am familiar with perlin as a procedure texture for wood. But I never knew that you can do that with Flash too!

Wow.. thanks for the excellent tutorial.

Anonymous (not verified) Says:

Thanks for the source code

13 February, 2008 - 2:15am

Hi,

Just want to say thank you for the source code.

regards.

Alice (not verified) Says:

Very highend tutorial

3 March, 2008 - 8:00pm

I am a 3D graphics user. I uses Blender 3D and Rhino.

I also uses Flash but a very old basic version. I never knew that we can use flash for making texture.

Thank you for opening my eyes...