cupofnestor
January 21st, 2010, 01:19 PM
I've often found uses for displacement via perlin noise generated via BitmapData objects. Usually, i've ended up using hack-y cut-and-paste code, and I want to get a better understanding. I've made a class to handle generating and animating the perlin fumction. Then, using the document class, attempted to displace a MovieClip with it. I've found that I can only use a "local" bitmapData object to serve as the first argument to the displacementMapfilter. And, the filter only "works" for the first few frames, before the appearance of displacement ceases. =-|
//warble.as (document class, needs a mc called Pic)
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.filters.DisplacementMapFilter;
import flash.geom.Point;
import flash.events.*
public class warble extends MovieClip
{
var displaceBitmap:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
var noisey:perlin=new perlin();
var filter:DisplacementMapFilter;
var img:Pic = new Pic();
var off:Point = new Point(0,0);
public function warble(){
addChild(noisey);
filter= new DisplacementMapFilter(displaceBitmap, off, 1,1,122,-111, "clamp");
addChild(img);
addEventListener(Event.ENTER_FRAME,animate);
}
private function animate(e:Event):void
{
displaceBitmap.draw(noisey);
img.filters=[filter];
}
}
}
//perlin.as
package
{
import flash.display.*;
import flash.geom.Point;
import flash.events.Event;
public class perlin extends MovieClip
{
public var bitmap:BitmapData;
public var image:Bitmap;
private var angle:Number=0;
private var _offsetX:Number=0;
private var _offsetY:Number=0;
public function perlin()
{
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event):void
{
this.bitmap=new BitmapData(stage.stageWidth,stage.stageHeight,true ,0xFFFFFFFF);
image = new Bitmap(this.bitmap);
this.addChild(image);
this.addEventListener(Event.ENTER_FRAME,update);
}
private function update(event:Event):void
{
var point:Point=new Point(this._offsetX,0);
var point2:Point=new Point(this._offsetY,this._offsetX);
this.bitmap.perlinNoise(150,75,3,1000,false,true,B itmapDataChannel.ALPHA,false,[point,point2]);
this._offsetX+=1.7856309;
this._offsetX+=1.7232900;
}
}
}
//warble.as (document class, needs a mc called Pic)
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.filters.DisplacementMapFilter;
import flash.geom.Point;
import flash.events.*
public class warble extends MovieClip
{
var displaceBitmap:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
var noisey:perlin=new perlin();
var filter:DisplacementMapFilter;
var img:Pic = new Pic();
var off:Point = new Point(0,0);
public function warble(){
addChild(noisey);
filter= new DisplacementMapFilter(displaceBitmap, off, 1,1,122,-111, "clamp");
addChild(img);
addEventListener(Event.ENTER_FRAME,animate);
}
private function animate(e:Event):void
{
displaceBitmap.draw(noisey);
img.filters=[filter];
}
}
}
//perlin.as
package
{
import flash.display.*;
import flash.geom.Point;
import flash.events.Event;
public class perlin extends MovieClip
{
public var bitmap:BitmapData;
public var image:Bitmap;
private var angle:Number=0;
private var _offsetX:Number=0;
private var _offsetY:Number=0;
public function perlin()
{
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event):void
{
this.bitmap=new BitmapData(stage.stageWidth,stage.stageHeight,true ,0xFFFFFFFF);
image = new Bitmap(this.bitmap);
this.addChild(image);
this.addEventListener(Event.ENTER_FRAME,update);
}
private function update(event:Event):void
{
var point:Point=new Point(this._offsetX,0);
var point2:Point=new Point(this._offsetY,this._offsetX);
this.bitmap.perlinNoise(150,75,3,1000,false,true,B itmapDataChannel.ALPHA,false,[point,point2]);
this._offsetX+=1.7856309;
this._offsetX+=1.7232900;
}
}
}