遠近法のテスト2
/**
* Copyright gaziya ( http://wonderfl.net/user/gaziya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8AkZ
*/
package {
import flash.geom.Matrix3D;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Vector3D;
import flash.display.Sprite;
[SWF(width=465,height=465,frameRate=30)]
public class FlashTest extends Sprite {
public function FlashTest() {
x = stage.stageWidth/2
y = stage.stageHeight/2
var points:Vector.<Vector3D> = new Vector.<Vector3D>
var container:Sprite= new Sprite
var shapes:Vector.<Shape> = new Vector.<Shape>
var length:Number = 300
for (var i:int = 0; i < 100; i++){
var vector:Vector3D = new Vector3D(
Math.round((Math.random()-0.5)*length),
Math.round((Math.random()-0.5)*length),
Math.round((Math.random()-0.5)*length))
points.push(vector)
var rect_size:int = Math.random()*30 +15
shapes.push(new Shape)
shapes[i].graphics.beginFill(Math.round(Math.random()*0xffff)*0x100)
shapes[i].graphics.drawRect(-rect_size/2, -rect_size/2,rect_size,rect_size)
shapes[i].x = vector.x
shapes[i].y = vector.y
shapes[i].z = vector.z
shapes[i].rotationX = Math.random()*180
shapes[i].rotationY = Math.random()*180
shapes[i].rotationZ = Math.random()*180
container.addChild(shapes[i])
}
addChild(container)
var theta:int = 0
addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event):void {
theta += Math.sqrt(Math.pow(mouseX,2)+Math.pow(mouseY,2))/35
theta %= 360
var tilt:Number = 180-Math.atan2(mouseX,mouseY)*20*Math.PI
var matrix:Matrix3D = new Matrix3D
matrix.appendRotation(theta,Vector3D.Y_AXIS)
matrix.appendRotation(tilt,Vector3D.Z_AXIS)
var sort_items:Array = new Array
var i:int
for (i=0; i<points.length; i++) {
var vector:Vector3D = matrix.transformVector(points[i])
vector.w = i
sort_items.push(vector)
container.removeChild(shapes[i])
}
sort_items.sortOn("z", Array.NUMERIC | Array.DESCENDING)
for (i=0; i<points.length; i++) {
container.addChild(shapes[sort_items[i].w])
}
container.rotationY = theta
container.rotationZ = tilt
}
}
}
}