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

forked from: forked from: forked from: 空間に絵を描く PerspectiveProjection / curveTo

Get Adobe Flash player
by dizgid 18 Dec 2009
// forked from dizgid's forked from: forked from: 空間に絵を描く PerspectiveProjection / curveTo
// forked from dizgid's forked from: 空間に絵を描く PerspectiveProjection
// forked from miniapp's 空間に絵を描く
package { 
	
	import flash.display.GraphicsPathCommand;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.Graphics; 
	import flash.filters.GlowFilter;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix3D;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Utils3D;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Keyboard;
	
	[SWF(backgroundColor="0x000000", width="465", height="465", frameRate="60")]
	public class Projection1 extends Sprite {
		
		//private static var identityMatrix:Matrix3D = new Matrix3D();
		private var viewport:Shape;
		private var projection:PerspectiveProjection;
		private var worldMatrix:Matrix3D;
		private var viewMatrix:Matrix3D;
		private var g:Graphics;
		
		public function Projection1() {
			this.graphics.beginFill(0x000000);
			this.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight);
			this.graphics.endFill();
			
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			
			viewport = new Shape();
			viewport.x = centerX;
			viewport.y = centerY;
			viewport.filters = [new GlowFilter(0x3300CC, 0.7, 32, 32, 2, 2)];
            
			g = viewport.graphics
			addChild(viewport);
            
			projection = new PerspectiveProjection();
			projection.fieldOfView = 60;
			
			worldMatrix = new Matrix3D();
			viewMatrix = new Matrix3D();
			viewMatrix.appendTranslation(0, 0, projection.focalLength);
			
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.textColor = 0x666666;
			tf.text = 'マウスドラッグで空間に絵を描きます。 ';
			addChild(tf);

                        Wonderfl.capture_delay( 30 );
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		public var uv:Vector.<Number> = new Vector.<Number>();
		public var verts:Vector.<Number>  = new Vector.<Number>();
		public var projectedVerts:Vector.<Number> = new Vector.<Number>();
		public var commands:Vector.<int> = new Vector.<int>();
		public var matrix:Matrix3D = new Matrix3D();
		public var isMouseDowm:Boolean = false;
		public var shiftkeyIsDown:Boolean = false;
		public var centerX:Number = stage.stageWidth / 2;
		public var centerY:Number = stage.stageHeight / 2;
		private var _c:Number;
		
		private function keyUpHandler(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.SHIFT) shiftkeyIsDown = false;
		}
		
		private function keyDownHandler(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.SHIFT) shiftkeyIsDown = true;
		}
		
		private function enterFrameHandler(e:Event):void {
			if (isMouseDowm) {
				if(_c % 4 ==0){				
				commands.push(GraphicsPathCommand.LINE_TO);
				verts.push(viewport.mouseX, viewport.mouseY, 0);
				}
				_c ++;
			}
			
			//if (shiftkeyIsDown) { 
				//一フレーム前のマウス座標との差分を回転角度にする。
				//var dx:Number = mouseY - pmouseY;
				//var dy:Number = mouseX - pmouseX;
				var dx:Number = 0;
				var dy:Number = 2.5;
				worldMatrix.identity();
				worldMatrix.appendRotation(-dx, Vector3D.X_AXIS);
				worldMatrix.appendRotation(dy, Vector3D.Y_AXIS);
				// 頂点データを回転させる。
				worldMatrix.transformVectors(verts, verts);
			//}
			
			var matrix:Matrix3D = new Matrix3D();
			matrix.append(viewMatrix);
			matrix.append(projection.toMatrix3D());
			
			//2次元に投影
			Utils3D.projectVectors(matrix, verts, projectedVerts, uv);
			
			render(commands, projectedVerts);
		}
		
		private function render(commands:Vector.<int>, projectedVerts:Vector.<Number>):void
		{
			g.clear();
			var newCmd:Vector.<int> = new Vector.<int>();
			var newVerts:Vector.<Number> = new Vector.<Number>();
			for(var i:int = 0; i < commands.length-1; i++){
				if(commands[i] == GraphicsPathCommand.MOVE_TO){
					newCmd.push(commands[i]);
					newVerts.push(projectedVerts[i*2], projectedVerts[i*2+1]);
					/*
					g.beginFill(0xff0000);
					g.drawCircle(projectedVerts[i*2], projectedVerts[i*2+1], 5);
					g.endFill();
					*/
				}else if(commands[i] == GraphicsPathCommand.LINE_TO){
					newCmd.push(GraphicsPathCommand.CURVE_TO);
					var xc:Number = (projectedVerts[i*2] + projectedVerts[i*2+2])/2;
					var yc:Number = (projectedVerts[i*2+1] + projectedVerts[i*2+3])/2;
					newVerts.push(projectedVerts[i*2], projectedVerts[i*2+1], xc, yc);
				}
			}
			g.lineStyle(6, 0x3300CC);
			g.drawPath(newCmd, newVerts);
			
			g.lineStyle(1, 0xffffff);
			g.drawPath(newCmd, newVerts);
		}
		
		private function mouseDownHandler(e:MouseEvent):void {
			_c = 0;
			commands.push(GraphicsPathCommand.MOVE_TO);
			verts.push(viewport.mouseX, viewport.mouseY, 0);
			
			isMouseDowm = true;
		}
		
		private function mouseUpHandler(e:MouseEvent):void {
			commands.push(GraphicsPathCommand.MOVE_TO);
			verts.push(viewport.mouseX, viewport.mouseY, 0);
			isMouseDowm = false;
		}
	}
}