import flash.display.BitmapData; import flash.filters.ConvolutionFilter; import flash.filters.DisplacementMapFilter; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; import mx.utils.Delegate; /** * ... * @author binhdocco */ class WaterEffect extends Object { private var mc: MovieClip; private var IMAGE_WIDTH: Number; private var IMAGE_HEIGHT: Number; private var IMAGE_ID: String; private var damper: BitmapData; private var result: BitmapData; private var result2: BitmapData; private var source: BitmapData; private var buffer: BitmapData; private var output: BitmapData; private var surface: BitmapData; private var bounds: Rectangle; private var origin: Point; private var wave: ConvolutionFilter; private var damp: ColorTransform; private var water: DisplacementMapFilter; private var matrix: Matrix; private var matrix2: Matrix; private var mouseUp: Boolean = true;
public function WaterEffect(mc: MovieClip, imageId: String) { this.mc = mc; this.IMAGE_ID = imageId; surface= BitmapData.loadBitmap(this.IMAGE_ID); var tempMC: MovieClip = mc.createEmptyMovieClip("dummy_mc", mc.getNextHighestDepth()); tempMC.attachBitmap(surface, 1); this.IMAGE_WIDTH = tempMC._width; this.IMAGE_HEIGHT = tempMC._height; tempMC.removeMovieClip(); damper= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128); result= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128); result2= new BitmapData(IMAGE_WIDTH, IMAGE_HEIGHT, false, 128); source= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128); buffer= new BitmapData(IMAGE_WIDTH/2, IMAGE_HEIGHT/2, false, 128); output= new BitmapData(IMAGE_WIDTH, IMAGE_HEIGHT, true, 128);
bounds= new Rectangle(0, 0, IMAGE_WIDTH/2, IMAGE_HEIGHT/2); origin= new Point(); matrix= new Matrix(); matrix2= new Matrix(); matrix2.a = matrix2.d = 2; wave= new ConvolutionFilter(3, 3, [1, 1, 1, 1, 1, 1, 1, 1, 1], 9, 0); damp= new ColorTransform(0, 0, 9.960937E-001, 1, 0, 0, 2, 0); water= new DisplacementMapFilter(result2, origin, 4, 4, 32, 32, "ignore");
this.mc.attachBitmap(output, 0);
var thisObj = this; mc.onEnterFrame = Delegate.create(this, enterFrame); mc.onMouseUp = function() { thisObj.mouseUp = true; } mc.onMouseDown = function() { thisObj.mouseUp = false; } }
private function enterFrame():Void { if (mouseUp) { var xMouse = this.mc._xmouse / 2; var yMouse = this.mc._ymouse / 2; source.setPixel(xMouse + 1, yMouse, 16777215); source.setPixel(xMouse - 1, yMouse, 16777215); source.setPixel(xMouse, yMouse + 1, 16777215); source.setPixel(xMouse, yMouse - 1, 16777215); source.setPixel(xMouse, yMouse, 16777215); } // end if result.applyFilter(source, bounds, origin, wave); result.draw(result, matrix, null, "add"); result.draw(buffer, matrix, null, "difference"); result.draw(result, matrix, damp); result2.draw(result, matrix2, null, null, null, true); output.applyFilter(surface, new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT), origin, water);
buffer = source; source = result.clone(); }
} |
No comments:
Post a Comment