スポンサーサイト

-------- | --:--

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

[ActionScript 3.0] 画像の折り曲げ

2007-08-17 | 23:54

↑の青い線の近くにマウスを動かしてみてください。

package{
    import flash.display.*;
    import flash.geom.Rectangle;
    import flash.geom.Point;
    import flash.events.*;
    import flash.utils.*;

    [SWF(width="500", height="300",backgroundColor="0xffffff")]
        public class Distort extends Sprite{

            private var bitmapSlices:Array = new Array();

            private var numOfSlices:int = 100;				
            private var rad:Number = 0;

            // camera position
            private var camera:Vector3 = new Vector3(-100,50,50);

            // view position
            private var viewTo:Vector3 = new Vector3(100,50,200);

            private var faceZ:Number = 150;

            private static var cornerX:int = 200;
            private static var scale:int = 4;

            private var pos:Vector3 = new Vector3(cornerX,400,400);

            [Embed(source="lena.jpg")]
                private var myImage:Class;

            public function Distort(){

                var myBitmap:Bitmap = new myImage();

                var l:int = Math.round(myBitmap.width/numOfSlices);	
                var h:int = myBitmap.height;		

                for(var i:int =0; i < numOfSlices; i++){
                    var myBitmapSlice:BitmapData = new BitmapData(l,h,true,0x00000000);

                    var rect:Rectangle = new Rectangle(i*l,0,l,h);
                    myBitmapSlice.copyPixels(myBitmap.bitmapData,rect,new Point(0,0));	
                    var bm:Bitmap = new Bitmap(myBitmapSlice);
                    bitmapSlices.push(bm);

                    addChild(bm);

                    bm.x = mouseX + i*l;	// init x position of clip rectangle in bitMap
                    bm.y = mouseY - h/2;
                }

                stage.addEventListener(MouseEvent.MOUSE_MOVE, loop);

                graphics.lineStyle(4,0x0000ff,0.2);
                var p:Point = view2local(world2view(new Vector3(cornerX,500,400)));
                graphics.moveTo(p.x,p.y);
                p = view2local(world2view(new Vector3(cornerX,-100,400)));
                graphics.lineTo(p.x,p.y);
            }

            private function loop(e:MouseEvent):void{
                if(mouseX > cornerX)
                    pos.x = mouseX*2;
                else{
                    pos.x = cornerX;
                    pos.z = 400 + (cornerX -mouseX)*10;
                }
                trace("pos.x=" + pos.x);
                trace("pos.z=" + pos.z);

                for(var i:int =0;i < numOfSlices;i++){
                    var bm:Bitmap = bitmapSlices[i];
                    var myX:int;
                    var myZ:int;
                    var diff:int = i*bm.width;
                    if(pos.x > cornerX) { 
                        myX = pos.x + diff;
                        myZ = 400;
                    }else if((pos.z - 400) > diff){
                        myX = pos.x;
                        myZ = pos.z - diff;
                    }else{
                        myX = pos.x + diff - (pos.z - 400);
                        myZ = 400;
                    }
                    var topV:Vector3 = new Vector3(myX, 400, myZ);
                    var bottomV:Vector3 = new Vector3(myX, 0, myZ);
                    var top:Point = view2local(world2view(topV));
                    var bottom:Point = view2local(world2view(bottomV));

                    bm.x = top.x;
                    bm.y = top.y;
                    bm.scaleY = (bottom.y - top.y)/400*scale;
                    bm.scaleX = scaleY*4;
                }
            }

            private function world2view(v:Vector3):Point{
                return new Point((faceZ - camera.z) * (v.x - camera.x) / (v.z - camera.z) 
                        - (faceZ - camera.z) * (viewTo.x - camera.x) / (viewTo.z - camera.z),
                        (faceZ - camera.z) * (v.y - camera.y) / (v.z - camera.z)
                        -(faceZ - camera.z)* (viewTo.y - camera.y) / (viewTo.z - camera.z));
            }

            private function view2local(p:Point):Point{
                return new Point(p.x + 200, 200 - p.y);
            }
        }
}
package{
    public class Vector3{
        private var _x:Number;
        private var _y:Number;
        private var _z:Number;

        public function Vector3( x:Number=0.0,y:Number=0.0,z:Number=0.0){
            _x = x ;
            _y = y ;
            _z = z ;
        }

        public function normalize():Vector3{
            var lenv:Number = Math.sqrt(_x*_x + _y*_y + _z*_z);
            _x = _x / lenv;
            _y = _y / lenv;
            _z = _z / lenv;

            return this;
        }

        public function length():Number{
            return Math.sqrt(_x*_x + _y*_y + _z*_z);
        }

        public function cross(rhs:Vector3):Number{
            return _y*rhs.z - _z*rhs.y + _z*rhs.x - _x*rhs.z + _x*rhs.y - _y*rhs.x;
        }

        public function crossProduct(rhs:Vector3):Vector3{
            return new Vector3(_y*rhs.z - _z*rhs.y , _z*rhs.x - _x*rhs.z , _x*rhs.y - _y*rhs.x);
        }

        public static function dotS(lhs:Vector3, rhs:Vector3):Number{
            return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
        }

        public static function crossProductS(lhs:Vector3, rhs:Vector3):Vector3{
            return lhs.crossProduct(rhs);
        }

        public static function subtractS(lhs:Vector3, rhs:Vector3):Vector3{
            return new Vector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
        }

        public function get x():Number{
            return _x;
        }

        public function get y():Number{
            return _y;
        }

        public function get z():Number{
            return _z;
        }

        public function set x(v:Number):void{
            _x = v;
        }

        public function set y(v:Number):void{
            _y = v;
        }

        public function set z(v:Number):void{
            _z = v;
        }
    }
}
スポンサーサイト
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。