Bitmap 海浪实现方式
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Shape; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.filters.DisplacementMapFilter; import flash.geom.Point; import flash.utils.getTimer; import tween.EasyTween; public class Test2 extends Sprite { // [Embed(source="_preset.png")] [Embed(source="sea.png")] public var waterBitmapCls:Class; private var refractTmpBitmap:Bitmap; private var refractBmData:BitmapData; private var refractDisplaceFilter:DisplacementMapFilter; private var woBitmapData:BitmapData; private var perlinOffsetPoint:Point = new Point(); private var perlinOffset:Array = []; private var filterArray:Array; private var p:Point = new Point(10, 200); private var _waveLayer:Sprite; private var _shipLayer:Sprite; private var _s:AAA; public static const WATERSIZE:int = 300; public function Test2() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; // var bbb:BitmapData = new BitmapData(WATERSIZE, WATERSIZE); // bbb.perlinNoise(150, 150, 1, 50, true, true, 7, true, this.perlinOffset); // // var bb:Bitmap = new Bitmap(bbb); // addChild(bb); // return; this.refractTmpBitmap = new waterBitmapCls(); this.refractBmData = new BitmapData(WATERSIZE, WATERSIZE); this.refractDisplaceFilter = new DisplacementMapFilter(this.refractBmData, new Point(0, 0), 1, 2, 10, 20); this.woBitmapData = this.refractTmpBitmap.bitmapData.clone(); this.perlinOffsetPoint.x = 0; this.perlinOffsetPoint.y = 0; this.perlinOffset = [this.perlinOffsetPoint]; this.filterArray = [this.refractDisplaceFilter]; generateWater = true; this.addEventListener(Event.ENTER_FRAME, enterframeHandler); for(var i:int = 0; i < 4; i++) { for(var j:int = 0; j < 4; j++) { var b:Bitmap = new Bitmap(woBitmapData); b.x = i * WATERSIZE; b.y = j * WATERSIZE; this.addChild(b); } } _waveLayer = new Sprite(); addChild(_waveLayer); _shipLayer = new Sprite(); addChild(_shipLayer); _s = new AAA(); _shipLayer.addChild(_s); var l:Shape = new Shape(); l.graphics.lineStyle(1, 0xFF0000); l.graphics.moveTo(WATERSIZE, 100); l.graphics.lineTo(WATERSIZE, WATERSIZE); stage.addChild(l); } private var _t:int = getTimer(); private var generateWater:Boolean = false; private var waterIndex:int = 0; private var waterTiles:Vector.= new Vector. (); private var waterDirection:int = -1; private static var waterTileAmount:int = 32; private function enterframeHandler(e:*):void { // this.removeEventListener(Event.ENTER_FRAME, enterframeHandler); var v:Number = 2;//Math.sin(getTimer()) + 10; this.perlinOffsetPoint.x = this.perlinOffsetPoint.x + v; this.perlinOffsetPoint.y = this.perlinOffsetPoint.y - v; this.refractBmData.perlinNoise(80, 80, 1, 50, true, true, 7, true, this.perlinOffset); this.refractTmpBitmap.filters = this.filterArray; this.woBitmapData.lock(); this.woBitmapData.draw(this.refractTmpBitmap); this.woBitmapData.unlock(); // if(generateWater) // { // var v:Number = 2;//Math.sin(getTimer()) + 10; // this.perlinOffsetPoint.x = this.perlinOffsetPoint.x + v; // this.perlinOffsetPoint.y = this.perlinOffsetPoint.y - v; // // this.refractBmData.perlinNoise(80, 80, 1, 50, true, true, 7, true, this.perlinOffset); // this.refractTmpBitmap.filters = this.filterArray; // // this.woBitmapData.lock(); // this.woBitmapData.draw(this.refractTmpBitmap); // this.woBitmapData.unlock(); // // var bitmapData2:BitmapData = this.woBitmapData.clone(); // // this.waterTiles[this.waterIndex] = bitmapData2; // waterIndex++; // if(waterIndex > waterTileAmount) // { // generateWater = false; // waterDirection = -1; // } // } // else // { // waterIndex += waterDirection; // // if (this.waterIndex >= waterTileAmount) // { // this.waterIndex = waterTileAmount; // this.waterDirection = -1; // } // else if (this.waterIndex <= 0) // { // this.waterIndex = 0; // this.waterDirection = 1; // } // // var bitmapData3:BitmapData = this.waterTiles[waterIndex]; // this.woBitmapData.copyPixels(bitmapData3, bitmapData3.rect, new Point()); // } // this.refractTmpBitmap.bitmapData.applyFilter(this.woBitmapData, this.woBitmapData.rect, new Point(), refractDisplaceFilter); // this.woBitmapData.copyPixels(this.refractTmpBitmap.bitmapData, this.refractTmpBitmap.bitmapData.rect, new Point()); // var dltaT:int = getTimer() - _t; EasyTween.updateAll(33); // p.x += 2; _s.x = this.mouseX; _s.y = this.mouseY; Wave.playWave(_s.x, _s.y, _waveLayer); } } }