磨りガラスを表現する

| No Comments | No TrackBacks

どこかのサイトで見かけたのがきっかけで、「ムービー上で磨りガラスを表現しようとしたらどうしたらいいか?」というのをしばらく考えていました。

ちょうど波紋エフェクトなどを作成したときに、ひとコマずつBitmapDataにして加工するのをやったので「磨りガラスもこれでイケるな」と思い、早速作成してみました。

ソースはこんな感じ

package jp.norisuke {

	import flash.display.*;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Rectangle;
	import flash.geom.Point;
	import flash.text.TextField;

	public class GroundGlass extends MovieClip {
		public var bitmap:Bitmap;
		public var source:DisplayObject;
		public var applyWidth:Number;
		public var applyHeight:Number;
		public var blurX:Number;
		public var blurQuality:int;
		public var backgroundColor:uint;
		//
		private var frameRateText:TextField;
		//コンストラクタ
		public function GroundGlass(obj:DisplayObject, w:Number, h:Number, b:Number = 16, q:int = 2, gColor:uint = 0xffffff, gAlpha:Number = 0.5, bgColor:uint = 0xffffffff)
		{
			source = obj;
			applyWidth = w;
			applyHeight = h;
			blurX = b;
			blurQuality = q;
			backgroundColor = bgColor;
			//
			bitmap = new Bitmap();
			addChild(bitmap);
			//
			var shape:Shape = new Shape();
			shape.graphics.beginFill(gColor, gAlpha);
			shape.graphics.drawRect(0, 0, applyWidth, applyHeight);
			addChild(shape);
			//
			frameRateText = new TextField()
			//addChild(frameRateText);
			//
			addEventListener(Event.ENTER_FRAME, start);
		}
		public function start(e:Event):void {
			//
			var s:Number=new Date().getMilliseconds()
			//
			var dx:Number
			var sourceBitmapData:BitmapData = new BitmapData(source.width, source.height,true,backgroundColor)
			var targetBitmapData:BitmapData = new BitmapData(applyWidth,applyHeight,true,0);
			var sourceRect:Rectangle = new Rectangle(x, y, applyWidth, applyHeight);
			sourceBitmapData.draw(source);
			targetBitmapData.copyPixels(sourceBitmapData, sourceRect, new Point(0, 0));
			
			targetBitmapData.applyFilter(targetBitmapData, new Rectangle(0, 0, targetBitmapData.width, targetBitmapData.height), new Point(0, 0), new BlurFilter(blurX,blurX,blurQuality));
			bitmap.bitmapData = targetBitmapData;
			//
			if (new Date().getMilliseconds() - s >0) {
				frameRateText.text = Math.floor(1000 / (new Date().getMilliseconds() - s)) + 'fps';
			}
			//
		}
	}
}

使い方は

addChild(obj);
var gg:GroundGlass = new GroundGlass(obj,width,height,blurStrength,quority,color,alha);
gg.x =10;
gg.y = 50;
addChild(gg);

といった感じです。引数は(磨りガラス効果をかけるオブジェクト、巾、高さ、ぼかしの強さ、ぼかしのクオリティ、ガラスの色、ガラスの透明度(アルファ))。与えるオブジェクトは(0,0)の位置にあるのが前提です。

No TrackBacks

TrackBack URL: http://www.norisuke.jp/mt/mt-tb.cgi/9

Leave a comment

About this Entry

This page contains a single entry by ノリスケ published on November 8, 2008 3:48 PM.

[書評]ActionScript3.0 アニメーション was the previous entry in this blog.

FileReference でうまくダウンロードできない is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Powered by Movable Type 4.21-en