本示例演示了在WebCamera视频表面浮现一层水的效果,水的游动与融合使用Metaball方式构建
源代码:WebCamera_Drip
package{ import com.quasimondo.bitmapdata.CameraBitmap; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BitmapDataChannel; import flash.display.Sprite; import flash.events.Event; import flash.filters.BevelFilter; import flash.filters.BlurFilter; import flash.filters.DisplacementMapFilter; import flash.filters.DisplacementMapFilterMode; import flash.filters.DropShadowFilter; import flash.geom.Point; import flash.geom.Rectangle; [SWF(width="400",height="300",frameRate="31",backgroundColor="0xffffff")] public class WebCamera_Drip extends Sprite{ private var w:Number = 400; private var h:Number = 300; private var pt:Point = new Point(0,0); private var rect:Rectangle = new Rectangle(0,0,w,h); private var camera:CameraBitmap; private var bmp:Bitmap; private var bmpd:BitmapData = new BitmapData(w,h,true,0xFFFFFF); private var perlinMap:BitmapData = new BitmapData(w,h); private var blobMap:BitmapData = new BitmapData(w,h); private var darken:DropShadowFilter; private var lighten:DropShadowFilter; private var dmf:DisplacementMapFilter; private var blur:BlurFilter; private var bevel:BevelFilter; private var shifts:Array = [new Point(1,1),new Point(3,3)]; public function WebCamera_Drip(){ camera = new CameraBitmap(w,h,25,true); addChild(new Bitmap(camera.bitmapData)); bevel = new BevelFilter(); bevel.blurX = 20; bevel.blurY = 20; bevel.distance = 10; bevel.highlightColor = 0xFFFFFF; bevel.shadowColor = 0xFFFFFF; bevel.shadowAlpha = 0.35; blur = new BlurFilter(2,2,2); lighten = new DropShadowFilter(4,250,0xFFFFFF,0.95,4,4,1,1,false,false,false); darken = new DropShadowFilter(4,25,0x000000,0.25,4,4,1,1,false,false,false); bmp = new Bitmap(bmpd); bmp.filters = [blur,bevel,lighten,darken]; dmf = new DisplacementMapFilter(perlinMap,pt,1,2,40,40,DisplacementMapFilterMode.CLAMP,0); addChild(bmp); addEventListener(Event.ENTER_FRAME,onEnterframe); } private function onEnterframe(e:Event):void{ shifts[0].y = shifts[0].y - 1; shifts[1].y = shifts[1].y - 2; bmpd.lock(); // 使用PerlinNoise生成噪波 perlinMap.perlinNoise(105,105,2,0,false,true,7,true,shifts); // 将噪波中大于140/255*0xFFFFFF的颜色调成0xFFFF8000(品色) blobMap.fillRect(rect,0x000000); blobMap.threshold(perlinMap,rect,pt,">",140/255*0xFFFFFF,0xFFFF8000,0xFFFFFF,false); // 复制原始的bmp和camera的内容 bmpd.draw(bmp); bmpd.copyPixels(camera.bitmapData,bmpd.rect,pt); bmpd.applyFilter(bmpd,bmpd.rect,pt,dmf); bmpd.applyFilter(bmpd,bmpd.rect,pt,blur); // 使用blobMap的Alpha通道替换bmpd中的alpha通道 bmpd.copyChannel(blobMap,blobMap.rect,pt,BitmapDataChannel.ALPHA,BitmapDataChannel.ALPHA); } } }
转载请注明:陈童的博客 » WebCamera与MetaBall构造的水