flash on 2010-5-20
/**
* Copyright kenichirowatanabe ( http://wonderfl.net/user/kenichirowatanabe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/czqr
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.ActivityEvent;
import flash.events.Event;
import flash.filters.DisplacementMapFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.media.Camera;
import flash.media.Video;
public class cameratest extends Sprite
{
private var bmpd:BitmapData;
private var bmp:Bitmap;
private var video:Video;
private var mat:Matrix;
private var gridW:int = 12;
private var gridH:int = 12;
private var xTotal:int;
private var yTotal:int;
private var mirrorList:Vector.<Vector.<BitmapData>>;
private var camera:Camera;
private var total:int;
private var octaves:uint = 5
private var randseed:int;
private var offset:Array;
private var spx:Array;
private var spy:Array;
private var noiseBmp:BitmapData;
private var dfil:DisplacementMapFilter;
private var counter:int = 0
public function cameratest()
{
//stage.quality = StageQuality.HIGH;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
mat = new Matrix();
randseed = 1;
spx = [];
spy = [];
offset = [];
for(var i:int = 0; i<octaves; i++){
offset[i] = new Point(0,i*2);
spx[i] = Math.random()*2+1
spy[i] = Math.random()*1.5+1
}
noiseBmp = new BitmapData(400,400,false,0x000000);
dfil = new DisplacementMapFilter(noiseBmp,new Point(0,0),2,4,20,20);
this.filters = [dfil]
mirrorList = new Vector.<Vector.<BitmapData>>;
bmpd = new BitmapData(400,400,false,0x000000);
var camera:Camera = Camera.getCamera();
camera.setMode(400,400,12);
video = new Video();
video.width = stage.stageWidth;
video.height = stage.stageHeight;
video.attachCamera(camera);
video.smoothing = true;
xTotal = 400/gridW;
yTotal = 400/gridH;
total = xTotal*yTotal;
setBitmap();
}
private function setBitmap():void{
var index:int = 0;
for(var ix:int = 0; ix<xTotal; ix++){
mirrorList[ix] = new Vector.<BitmapData>;
for(var iy:int = 0; iy<yTotal; iy++){
var copyd:BitmapData = new BitmapData(gridW,gridH,false,0xFF2244);
copyd.copyPixels(bmpd,new Rectangle(ix*gridW,iy*gridH,gridW,gridH),new Point(0,0));
var mirror:Bitmap = new Bitmap(copyd);
mirrorList[ix][iy] = copyd;
mirror.x = ix*(gridW+.5);
mirror.y = iy*(gridH+.5);
mirror.scaleX = 1
mirror.scaleY = 1
addChild(mirror);
index++;
if(index == total){
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
}
}
}
private function enterFrameHandler(e:Event):void{
if(counter%2 == 0){
bmpd.draw(video,mat);
}
for(var i:int =0; i<octaves; i++){
offset[i].x += spx[i];
offset[i].y += spy[i];
}
if (dfil.mapBitmap) {
dfil.mapBitmap.dispose();
}
var noiseBmp2:BitmapData = new BitmapData(400,400,false,0xFFFFFF);
noiseBmp2.perlinNoise(400,400,octaves,randseed,true,false,7,true,offset);
dfil.mapBitmap = noiseBmp2;
this.filters = [dfil];
bmpd.lock();
for(var ix:int = 0; ix<xTotal; ix++){
for(var iy:int = 0; iy<yTotal; iy++){
mirrorList[ix][iy].copyPixels(bmpd,new Rectangle(ix*gridW,iy*gridH,gridW,gridH),new Point(0,0));
}
}
bmpd.unlock();
counter++;
}
private function activeHandler(e:ActivityEvent):void{
if(e.activating == true){
};
}
}
}