BLOG ENTRY

DisplacementMapFilterとperlinNoiseで外部画像を変形[AS3]

avatar

DisplacementMapFilterクラスBitmapDataクラスのperlinNoiseメソッド
を使って画像を変形エフェクトさせてみるサンプルコード。

[SWF]/wp-content/uploads/swf/efects/MyDisplacementMapFilter.swf, 440, 270[/SWF]

MyDisplacementMapFilter.as

[as3]
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.geom.Point;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;

[SWF(width="440", height="270", backgroundColor="0xFFFFFF", frameRate="24")]
public class MyDisplacementMapFilter extends Sprite {
private var _bd:BitmapData;
private var _pt:Point = new Point(0, 0);
private var _imgW:Number = 0;
private var _imgH:Number = 0;
private var _baseX:int = 0;
private var _baseY:int = 0;
private const PERLIN_OCTAVES:Number = 1;
private const COLOR_CHANNEL:Number = BitmapDataChannel.BLUE | BitmapDataChannel.GREEN;
private const SCALE:Number = 15;

public function MyDisplacementMapFilter():void {

// 画像データ読み込み
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.load(new URLRequest("http://blog.quall.net/wp-content/uploads/swf/efects/image1.jpg"));
addChild(loader);

}

private function completeHandler(e:Event):void {
_imgW = e.target.width;
_imgH = e.target.height;
_bd = new BitmapData(_imgW, _imgH);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}

private function enterFrameHandler(e:Event):void {
_baseX = _imgW + ( Math.random() * 10);
_baseY = _imgH + ( Math.random() * 10);

_bd.perlinNoise(
_baseX / 10,
_baseY / 200,
PERLIN_OCTAVES,
Math.floor(Math.random() * 10),
false,
true,
COLOR_CHANNEL,
false);

var displacementMap:DisplacementMapFilter
= new DisplacementMapFilter(
_bd,
_pt,
BitmapDataChannel.BLUE,
BitmapDataChannel.GREEN,
SCALE * 2,
SCALE * 10,
DisplacementMapFilterMode.CLAMP);

filters = [displacementMap];
}
}
}

[/as3]

WRITE COMMENT


(required)


(required)


(required)

MENU

veltica creative of twitter