In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

カメラ映像を無理矢理法線マップにしてみる

...
@author @kndys
Get Adobe Flash player
by knd 20 Jan 2010
    Embed
package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
	import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.ColorMatrixFilter;
    import flash.filters.ConvolutionFilter;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.media.Camera;
    import flash.media.Video;
	
	/**
     * ...
     * @author @kndys
     */
    [SWF(width="465",height="465",backgroundColor="0")]
    public class NormalLineCam extends Sprite
    {
        private const PARAM:Number = 5.0;
        
        private var vid:Video;
        private var cam:Camera;
        private var bmp:Bitmap;
        private var bmd_00:BitmapData;
        private var bmd_01:BitmapData;
        private var bmd_10:BitmapData;
        private var bmd_11:BitmapData;
        private var cmf:ColorMatrixFilter;
        private var rect:Rectangle;
        private var pt0:Point;
        private var fdx:ConvolutionFilter;
        private var fdy:ConvolutionFilter;
        private var dx:BitmapData;
        private var dy:BitmapData;
        private var cos30:Number;
        private var sin30:Number;
        private var theta:Number;
        private var map:Array;
        private var ntsc:Array;
        private var pt_00:Point;
        private var pt_01:Point;
        private var pt_10:Point;
        private var pt_11:Point;
        
        public function NormalLineCam() 
        {
            cam = Camera.getCamera();
            cam.setMode(231, 231, 15);
            vid = new Video(231, 231);
            vid.attachCamera(cam);
            bmp = new Bitmap(new BitmapData(465, 465));
            addChild(bmp);
            
            bmd_00 = new BitmapData(231, 231);
            bmd_01 = bmd_00.clone();
            dx = bmd_00.clone();
            dy = bmd_00.clone();
            bmd_10 = bmd_00.clone();
            bmd_11 = bmd_00.clone();
            
            cmf = new ColorMatrixFilter();
            ntsc = [
                0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 
                0.298912, 0.586611, 0.114478, 0, 0,
                0, 0, 0, 1, 0
            ]
            
            rect = bmd_00.rect;
            pt0 = new Point();
            
            fdx = new ConvolutionFilter(3, 1, [ -0.5, 0, 0.5], 1, 128);
            fdy = new ConvolutionFilter(1, 3, [ -0.5, 0, 0.5], 1, 128);
            
            cos30 = PARAM * Math.cos(Math.PI / 6);
            sin30 = PARAM * Math.sin(Math.PI / 6);
            
            pt_00 = new Point(1, 1);
            pt_01 = new Point(233, 1);
            pt_10 = new Point(1, 233);
            pt_11 = new Point(233, 233);
            
            bmd_10.fillRect(rect, 0xff000081);
            
            theta = 0.0;
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        private function loop(e:Event):void 
        {
            bmd_00.draw(vid);
            
            cmf.matrix = ntsc;
            bmd_01.applyFilter(bmd_00, rect, pt0, cmf);
            
            dx.applyFilter(bmd_01, rect, pt0, fdx);
            dy.applyFilter(bmd_01, rect, pt0, fdy);
            
            bmd_10.copyChannel(dx, rect, pt0, BitmapDataChannel.BLUE, BitmapDataChannel.RED);
            bmd_10.copyChannel(dy, rect, pt0, BitmapDataChannel.BLUE, BitmapDataChannel.GREEN);
            
            var cos:Number = sin30 * Math.cos(theta);
            var sin:Number = sin30 * Math.sin(theta);
            var bias:Number = 128 * (1.0 - cos - sin - cos30);
            cmf.matrix = [
                cos, sin, cos30, 0, bias,
                cos, sin, cos30, 0, bias,
                cos, sin, cos30, 0, bias,
                0,0,0,1,0
            ];
            bmd_11.applyFilter(bmd_10, rect, pt0, cmf);
            
            bmp.bitmapData.copyPixels(bmd_00, rect, pt_00);
            bmp.bitmapData.copyPixels(bmd_01, rect, pt_01);
            bmp.bitmapData.copyPixels(bmd_10, rect, pt_10);
            bmp.bitmapData.copyPixels(bmd_11, rect, pt_11);
            
            theta += 0.05;
        }
        
        
    }

}