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: 透視投影テスト

単純なボックスを並べた透視投影テスト
* 各Boxに回転を加える
Get Adobe Flash player
by Kay 11 Mar 2010
/**
 * Copyright Kay ( http://wonderfl.net/user/Kay )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6GjL
 */

// forked from Kay's 透視投影テスト
/*
 * 単純なボックスを並べた透視投影テスト
 * 各Boxに回転を加える
 */

package {
	
	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	
	[SWF(width=465,height=465,frameRate=30,backgroundColor=0x88aaff)]
	public class Main extends Sprite {
		
		private const SW:Number = stage.stageWidth;
		private const SH:Number = stage.stageHeight;
		public var proj:PerspectiveProjection = new PerspectiveProjection();
		
		public function Main():void {
			proj.fieldOfView = 80;

			var container:Sprite = new Sprite();
			addChild(container);
			container.x = SW/2;
			container.y = SH/2;

			var boxNum:uint = 3;
			var dist:Number = 150;
			var shift:Number = dist*(boxNum-1)/2;
			for (var v:int = 0; v < boxNum; v++) {
				var nY:Number = dist*v-shift;
				for (var h:int = 0; h < boxNum; h++) {
					var nX:Number = dist*h-shift;
					var box:Box = new Box();
					container.addChild(box);
					box.mtx.prepend(proj.toMatrix3D());
					box.mtx.prependTranslation(nX, nY, proj.focalLength);
					box.xRender();
					box.addEventListener(Event.ENTER_FRAME, xRotate);
				}
			}
		}
		public function xRotate(e:Event):void {
			// Box空間ではなくBox座標を回転させる
			e.target.rMtx.appendRotation(2,Vector3D.Y_AXIS);
			e.target.xRender();
		}
	}
}

import flash.display.*;
import flash.geom.*;

class Box extends Sprite {
	
	private const len:Number = 50;
	public var vertices3D:Vector.<Number> = new Vector.<Number>();
	public var vertices2D:Vector.<Number> = new Vector.<Number>();
	public var indices:Vector.<int> = new Vector.<int>();
	public var uvt:Vector.<Number> = new Vector.<Number>();
	public var mtx:Matrix3D = new Matrix3D();
	public var rMtx:Matrix3D = new Matrix3D();
	
	public function Box():void {
		vertices3D.push(-len,-len, len);
		vertices3D.push( len,-len, len);
		vertices3D.push(-len,-len,-len);
		vertices3D.push( len,-len,-len);
		vertices3D.push(-len, len, len);
		vertices3D.push( len, len, len);
		vertices3D.push(-len, len,-len);
		vertices3D.push( len, len,-len);
		indices.push(2,3,7, 7,6,2);
		indices.push(3,1,5, 5,7,3);
		indices.push(1,0,4, 4,5,1);
		indices.push(0,2,6, 6,4,0);
		indices.push(0,1,3, 3,2,0);
		indices.push(6,7,5, 5,4,6);
	}
	
	public function xRender():void {
		var tempVerts:Vector.<Number> = new Vector.<Number>;
		rMtx.transformVectors(vertices3D, tempVerts);
		
		// 回転させた座標に透視投影を反映して2D化する
		Utils3D.projectVectors(mtx,tempVerts,vertices2D,uvt);
		graphics.clear();
		graphics.lineStyle(0,0xffffff,0.6);
		graphics.drawTriangles(vertices2D,indices,null,TriangleCulling.POSITIVE);
		graphics.beginFill(0xffffff,0.5);
		graphics.lineStyle(0,0xff9999);
		graphics.drawTriangles(vertices2D,indices,null,TriangleCulling.NEGATIVE);
		graphics.endFill();
	}
}