Random Generated Displacement Map

So,  I was following this tutorial from activetuts+ to create the same effect,  a static distortion effect using the displacement map filter,  in one of my game projects and it was cool until I realized that my project is getting fatter and fatter by adding images of different sizes to use for different sized objects.

Ofcourse, this was not acceptable so I thought I could create a random generated image by code and reuse it than to create different images in Photoshop.

Anyway, here’s the code which is generating a random image to be used with the effect in the tutorial.

function rndImage(w:Number, h:Number, wl:int = 0, bl:int =0):BitmapData
{
var bitmapdata:BitmapData = new BitmapData(w,h,false, 0xDDDDDD);
var randY:int = 0;
var randX:int = 0;
var randW:int = 0;
var thick:Number = 0;
var a:int = 0;
var minWidth:int = 0;
var line:Shape = new Shape();

for ( a = 0; a< wl; a++)
{
thick = Math.random()* 1.3 + 0.2;
randY = Math.random() * h;
minWidth = (40 * w)/100;
randW = Math.random() * (w-minWidth) + minWidth;
randX = Math.random() * ((w-randW));
line.graphics.lineStyle(thick, 0xFFFFFF);
line.graphics.moveTo(randX,randY);
line.graphics.lineTo(randW, randY);
}

for ( a = 0; a< bl; a++)
{
thick = Math.random()* 1.3 + 0.2;
randY = Math.random() * h;
minWidth = (40 * w)/100;
randW = Math.random() * (w-minWidth) + minWidth;
randX = Math.random() * ((w-randW));
line.graphics.lineStyle(thick, 0x000000);
line.graphics.moveTo(randX,randY);
line.graphics.lineTo(randW+randX, randY);
}

bitmapdata.draw(line);

return bitmapdata;
}

And this is how you generate the image

rndImage(width,height, nr_white_lines,nr_dark_lines);

Advertisements

3 thoughts on “Random Generated Displacement Map

  1. I think this is among the most significant info for me. And i am glad reading your article. But should remark on some general things, The site style is great, the articles is really great : D. Good job, cheers

  2. Pingback: My Homepage

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s