雨が降って波紋が広がる
クリック位置に雨が降って波紋が広がります
// forked from Kay's 水滴のようなフィルタ効果
/*
* クリック位置に雨が降って波紋が広がります
*/
package {
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.geom.Vector3D;
import flash.geom.Point;
import flash.filters.DisplacementMapFilter;
import flash.filters.DisplacementMapFilterMode
[SWF(width=400, height=400, backgroundColor=0x000000, frameRate=24)]
public class WaterDrop2 extends Sprite {
public var drops:Vector.<Vector3D> = new Vector.<Vector3D>;
function WaterDrop2():void {
const SW:Number = stage.stageWidth;
const SH:Number = stage.stageHeight;
// 背景を描画する
var patternA:BitmapData = new BitmapData(20,20,false,0x005544);
patternA.fillRect(new Rectangle(4,4,12,12),0x003322);
var backGround:Sprite = new Sprite();
backGround.graphics.beginBitmapFill(patternA);
backGround.graphics.drawRect(0, 0, SW, SH/2);
backGround.graphics.endFill();
var patternB:BitmapData = new BitmapData(20,12,false,0x666699);
patternB.fillRect(new Rectangle(0,0,20,6),0x111144);
backGround.graphics.beginBitmapFill(patternB);
backGround.graphics.drawRect(0, SH/2, SW, SH/2);
backGround.graphics.endFill();
addChild(backGround);
// マップを作る
var map:BitmapData = new BitmapData(SW,SH,false,0x7f7f7f);
var _bitmap:Bitmap = new Bitmap(map);
_bitmap.addEventListener(Event.ENTER_FRAME, mapUpdate);
//addChild(_bitmap);
// マップを更新してフィルターを掛ける
function mapUpdate(e:Event):void {
while(drops.length && drops[0].w > 20) {
drops.shift();
}
// クリアしてから
map.fillRect(new Rectangle(0,0,SW,SH),0x7f7f7f);
var _3Dview:Sprite = new Sprite();
for (var i:uint= 0; i < drops.length; i++) {
// dropsの状態によって波紋の描画が異なる
if (drops[i].y < SH+drops[i].z) {
var drop:Sprite = new Drop();
drop.x = drops[i].x;
drop.y = drops[i].y-80;
drop.z = -drops[i].z;
_3Dview.addChild(drop);
drops[i].y+=10;
} else {
var ripple:Sprite = new Ripple();
ripple.x = drops[i].x;
ripple.y = drops[i].y-80;
ripple.z = -drops[i].z;
ripple.rotationX = -80;
ripple.scaleX = ripple.scaleY = drops[i].w/15;
ripple.alpha = 1 - drops[i].w/20;
_3Dview.addChild(ripple);
drops[i].w+=1;
}
map.draw(_3Dview);
}
var xyFilter:DisplacementMapFilter = new DisplacementMapFilter();
xyFilter.mapBitmap=map;
xyFilter.mapPoint=new Point(0,0);
xyFilter.componentX=BitmapDataChannel.RED;
xyFilter.componentY=BitmapDataChannel.RED;
xyFilter.scaleX = 16;
xyFilter.scaleY = 16;
xyFilter.mode=DisplacementMapFilterMode.CLAMP;
backGround.filters=[xyFilter];
}
// 水滴を発生させる
stage.addEventListener(MouseEvent.CLICK, dropStart);
function dropStart(e:MouseEvent):void {
var v3D:Vector3D = new Vector3D();
v3D.x = mouseX;
v3D.y = 0;
v3D.z = mouseY-SH/4;
v3D.w = 0.01;
if (drops.length < 8) {
drops.push(v3D);
}
}
}
}
}
// 水滴
import flash.display.Sprite;
class Drop extends Sprite {
public function Drop():void {
graphics.beginFill(0xff0000);
graphics.drawCircle(0,0,10);
graphics.endFill();
}
}
// 波紋
class Ripple extends Sprite {
public function Ripple():void {
graphics.lineStyle(6,0xff0000);
graphics.drawCircle(0,0,100);
graphics.lineStyle(4,0xff0000);
graphics.drawCircle(0,0,100/5*3);
graphics.lineStyle(2,0xff0000);
graphics.drawCircle(0,0,100/5);
}
}