PDA

View Full Version : scaling noise


pet-theory
09-21-2006, 07:36 PM
I'd like to scale a bitmap that is never seen but used in order to create an effect.

Basically, I'd like noise, but a more pixelated, blockier noise than a per-pixel noise. So I need to noise-up a bitmapdata, then magnify it.

Well, bitmapData's aren't displayObjects, so they don't have scaleX and scaleY properties. And their width and height propertes are read-only. So how to scale up a texture bitmap, then?

All I can come up with so far is to try to get the bitmapData to draw part of itself (with a clipping matrix half its size, and a transform matrix that doubles that).

package
{

import flash.geom.*;
import flash.display.*;

//
public class Test extends Sprite
{
public function Test() {
//make bitmapdata
var bdata=new BitmapData(100, 100)
bdata.noise(Math.random()*1000, 0, 255, 1)
//make matrix
var mtr=new Matrix()
var scaleFactor=2;
mtr.scale(scaleFactor,scaleFactor);
//apply it to the bitmapdata
bdata.draw(bdata, mtr, null, null, new Rectangle(0,0, bdata.width/scaleFactor, bdata.height/scaleFactor));
//display it
var bitmap=new Bitmap(bdata)
bitmap.x=200;
bitmap.y=200;
addChild(bitmap);
}

}

}

The result some weird distortion that I can't explain.

I suppose I could blit it, scale it, and draw it, but I'm sure there is an easier way.

Thanks.

pet-theory
09-21-2006, 09:10 PM
I can scale noise by putting a noised bitmapData into a bitmap, then scaling the bitmap, then drawing the bitmap, like this:

package
{
import flash.geom.*;
import flash.display.*;
//
public class Test extends Sprite
{
public function Test() {
//make original bitmapdata to pixelate
var bdata=new BitmapData(100, 100)
bdata.noise(Math.random()*1000, 0, 255, 1)
//do a virtual "blit" so bdata can be scaled
var bitmap=new Bitmap(bdata);
var scaleFactor=5;
bitmap.scaleX=bitmap.scaleY=scaleFactor;
//create bitmapdata another that copies the first, but at scaled size
var bdata2=new BitmapData(100, 100)
bdata2.draw(bitmap, bitmap.transform.matrix);
//put it on screen
addChild(new Bitmap(bdata2));
//bdata.draw(bitmap, bitmap.transform.matrix);
//bitmap.scaleX=bitmap.scaleY=1/scaleFactor;
//addChild(bitmap);
}

}

}

but still, it seems like something I should be able to do in 1 line, not three or four...

if I try to make do (the last three lines, commented out) with one bitmapdata that a) is noised, and b) is the basis for the bitmap, AND c) draws the same scaled bitmap, I get the same weird distortion that I got before.

I don't know why a bitmapData drawing a Bitmap which is based on the bitmap should be a problem...but self-drawing does not work in this try or the last.

jsebrech
09-25-2006, 07:00 AM
How about attaching the bitmapdata to a movieclip, and scaling the movieclip?

pet-theory
09-25-2006, 06:33 PM
...in my last post.

But in AS3 terms, not AS2.

In AS3, movieclips are one among many DisplayObjects, which have movieclip-like properties (scaling, position, etc--most everything but frames, which are the speciality of movieclips). So I put the BitmapData into a Bitmap (a DisplayObject) to scale it, then took a snapshot of it using BitmapData's draw.