/**
* Copyright KinkumaDesign ( http://wonderfl.net/user/KinkumaDesign )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/l9Br
*/
/*
* local3DToGlobalを使ったドライブ
*/
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.PerspectiveProjection;
import flash.geom.Point;
import flash.geom.Vector3D;
[SWF(width="600",height="400",frameRate="30")]
public class Drive extends Sprite
{
public var canvas:Sprite;
public var rects:Array;
public var numOfRect:int = 20;
public var pcenter:Point;
public var frontRect:RectPoints2;
public static var fl:Number = 250;
public function Drive()
{
var hw:Number = this.stage.stageWidth / 2;
var hh:Number = this.stage.stageHeight / 2;
transform.perspectiveProjection.focalLength = fl;
canvas = new Sprite();
canvas.z = 0;
pcenter = new Point();
pcenter.x = hw;
pcenter.y = hh;
addChild(canvas);
frontRect = new RectPoints2(-fl + 50, new Point(hw, hh));
var i:int;
var r:RectPoints2;
rects = new Array();
for (i = 0; i < numOfRect; i++) {
r = new RectPoints2(10000 / numOfRect * i, new Point(hw, hh));
rects[i] = r;
}
this.addEventListener(Event.ENTER_FRAME, ef);
}
private function ef(e:Event):void
{
pcenter.x += (this.stage.mouseX - pcenter.x) * 0.2;
pcenter.y += (this.stage.mouseY - pcenter.y) * 0.2;
transform.perspectiveProjection.projectionCenter = new Point(pcenter.x, pcenter.y);
render();
}
public function render():void
{
zSort();
var i:int;
var r:RectPoints2;
var g:Graphics = canvas.graphics;
g.clear();
for (i = 0; i < numOfRect; i++) {
r = rects[i];
r.move();
drawRect(r);
}
g.lineStyle(1, 0xd4dfe5, 0.5);
var v:Vector3D;
var pt:Point;
var p:PerspectiveProjection = transform.perspectiveProjection;
var currentCenter:Point = new Point(p.projectionCenter.x, p.projectionCenter.y);
for (i = 0; i < 4; i++) {
g.moveTo(currentCenter.x, currentCenter.y);
v = frontRect["v" + (i + 1)];
pt = canvas.local3DToGlobal(v);
g.lineTo(pt.x, pt.y);
}
}
public function drawRect(r:RectPoints2):void
{
var g:Graphics = canvas.graphics;
var pt:Point;
g.beginFill(r.color, 0.3);
pt = canvas.local3DToGlobal(r.v1);
g.moveTo(pt.x, pt.y);
pt = canvas.local3DToGlobal(r.v2);
g.lineTo(pt.x, pt.y);
pt = canvas.local3DToGlobal(r.v3);
g.lineTo(pt.x, pt.y);
pt = canvas.local3DToGlobal(r.v4);
g.lineTo(pt.x, pt.y);
pt = canvas.local3DToGlobal(r.v1);
g.lineTo(pt.x, pt.y);
}
public function zSort():void
{
rects.sortOn("z", Array.NUMERIC | Array.DESCENDING);
}
}
}
import flash.geom.Point;
import flash.geom.Vector3D;
class RectPoints2
{
public var v1:Vector3D;
public var v2:Vector3D;
public var v3:Vector3D;
public var v4:Vector3D;
public var vectors:Vector.<Vector3D>;
public var z:Number;
public var vz:Number = -20;
public var color:uint;
public function RectPoints2(_z:Number, offset:Point)
{
this.z = _z;
var i:int;
var w:int = 100;
var h:int = 60;
v1 = new Vector3D( -w + offset.x, -h + offset.y, z);
v2 = new Vector3D( w + offset.x, -h + offset.y, z);
v3 = new Vector3D( w + offset.x, h + offset.y, z);
v4 = new Vector3D( -w + offset.x, h + offset.y, z);
vectors = new Vector.<Vector3D>();
vectors.push(v1);
vectors.push(v2);
vectors.push(v3);
vectors.push(v4);
color = Math.floor(0xffffff * Math.random());
}
public function move():void
{
var i:int;
var v:Vector3D;
this.z += vz;
if (this.z < -1 * (Drive.fl - 30)) {
this.z = 10000;
}
for (i = 0; i < 4; i++) {
v = vectors[i];
v.z = z;
}
}
}