【修正】DisplacementMapFilterのテスト forked from: 【修正】DisplacementMapFilterのテスト
- // forked from miniapp's 【修正】DisplacementMapFilterのテスト
- /**
- * 結果は同じだけど、考え方が違ってたので修正
- *
- * http://miniapp.org/blog/2009/05/04/106/
- * DisplacementMapFilterを使ってマウスの位置に移動させる
- * ステージサイズ100 * 100で作ってるので拡大して表示してます。
- */
- package {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.BitmapDataChannel;
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.events.Event;
- import flash.filters.DisplacementMapFilter;
- import flash.filters.DisplacementMapFilterMode;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- [SWF(backgroundColor="0xFFFFFF", width="100", height="100", frameRate="60")]
- public class Main5_7 extends Sprite {
- private static const WIDTH_STAGE:uint = 100;
- private static const HEIGHT_STAGE:uint = 100;
- public function Main5_7() {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private var _widthBox:uint = 20;
- private var _heightBox:uint = 20;
- private var _rect:Rectangle = new Rectangle(
- int(WIDTH_STAGE * Math.random()), //ランダムな場所に配置する
- int(HEIGHT_STAGE * Math.random()),
- _widthBox,
- _heightBox
- );
- private var _canvas:Sprite = new Sprite();
- private var _mapBitmap:BitmapData = new BitmapData(WIDTH_STAGE, HEIGHT_STAGE, false);
- private var _dmf:DisplacementMapFilter = new DisplacementMapFilter(
- _mapBitmap,
- new Point(),
- BitmapDataChannel.BLUE,//x方向の移動に使う色
- BitmapDataChannel.GREEN,//y方向の移動に使う色
- WIDTH_STAGE * 2, //取り得るx値の最大 * 2
- HEIGHT_STAGE * 2, //取り得るy値の最大 * 2
- DisplacementMapFilterMode.COLOR
- );
- private function init(e:Event = null):void {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- //stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- addChild(_canvas);
- //ランダムな場所に四角を描きます。
- var bmpd:BitmapData = new BitmapData(WIDTH_STAGE, HEIGHT_STAGE, false);
- bmpd.fillRect(_rect, 0x0);
- var bmp:Bitmap = new Bitmap(bmpd);
- _canvas.addChild(bmp);
- //trace(_rect.x, _rect.y);
- //trueで色と場所を確認
- if (false) {
- //確認用
- addChild(new Bitmap(_mapBitmap));
- var sp:Sprite = new Sprite();
- sp.graphics.beginFill(0);
- sp.graphics.drawRect(_rect.x, _rect.y, _rect.width, _rect.height);
- addChild(sp);
- }
- addEventListener(Event.ENTER_FRAME, enterFrameHandler);
- }
- /**
- *
- * @param diff 動かしたい対象までの距離
- * @param scale 取り得る値の最大値
- * @return
- */
- private function getColor(diff:Number, scale:Number):uint {
- return Math.round((diff * 0x100) / scale) + 0x80;
- }
- private function enterFrameHandler(e:Event):void {
- _mapBitmap.lock();
- var diffX:Number = mouseX - _rect.x;
- var diffY:Number = mouseY - _rect.y;
- //目的地に色を塗る
- for (var yy:uint = 0; yy < HEIGHT_STAGE; ++yy) {
- for (var xx:uint = 0; xx < WIDTH_STAGE; ++xx) {
- //diffX離れた地点がxx地点に移る。
- var applyX:Number = xx - diffX;
- var applyY:Number = yy - diffY;
- var dx:Number = applyX - xx;
- var dy:Number = applyY - yy;
- var rateX:Number = dx / WIDTH_STAGE;
- var rateY:Number = dy / HEIGHT_STAGE;
- var colorX:uint = (rateX * 0x7F) + 0x80;
- var colorY:uint = (rateY * 0x7F) + 0x80;
- if (applyX == _rect.x && applyY == _rect.y) {
- //逆算テスト
- //フィルタが適用されるx値 = 目的地のx値 + ((色の強さ - 0x80) * dmf最大値) / 0x100
- dx = ((colorX - 0x80) * _dmf.scaleX) / 0x100;
- var originX:Number = xx + dx;
- trace(originX, applyX);
- }
- var color:uint = colorY << 8 | colorX;
- _mapBitmap.setPixel(xx, yy, color);
- }
- }
- _mapBitmap.unlock();
- _canvas.filters = [_dmf];
- }
- }
- }
notice: 
