遠近法のテスト5
/**
* Copyright gaziya ( http://wonderfl.net/user/gaziya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tG9g
*/
package {
import flash.geom.Vector3D
import flash.geom.Matrix3D
import flash.display.Shape
import flash.display.Sprite
import flash.events.Event
[SWF(width=465,height=465,frameRate=30)]
public class FlashTest extends Sprite {
public function FlashTest() {
x = stage.stageWidth/2
y = stage.stageHeight/2
z = -250
var container:Sprite = addChild(new Sprite) as Sprite
var shapes:Vector.<Shape> = new Vector.<Shape>
var radius:int = 200
var split:int = 25
var unit_theta:Number = 360 / split
for (var i:int = 0; i < split; i++) {
for (var j:int=0; j < split; j++) {
setShape(radius,unit_theta*i,unit_theta*j)
}
}
function setShape(radius:Number,theta_x:Number,theta_y:Number):void {
var circle:Shape = container.addChild(new Shape) as Shape
shapes.push(circle)
circle.graphics.beginFill(Math.round(Math.random()*0xffff)*0x100)
circle.graphics.drawCircle(0,0,10)
circle.rotationX = -theta_x
circle.rotationY = theta_y
circle.x = radius * Math.cos(theta_x*Math.PI/180) * Math.sin(theta_y*Math.PI/180)
circle.y = radius * Math.sin(theta_x*Math.PI/180)
circle.z = radius * Math.cos(theta_x*Math.PI/180) * Math.cos(theta_y*Math.PI/180)
}
addEventListener(Event.ENTER_FRAME, loop)
var theta:Number = 0
function loop(e:Event):void {
theta += Math.sqrt(Math.pow(mouseX,2)+Math.pow(mouseY,2))/80
theta %= 360
var tilt:Number = 180-Math.atan2(mouseX,mouseY)*20*Math.PI
var matrix:Matrix3D = new Matrix3D
matrix.appendRotation(theta,Vector3D.Y_AXIS)
var position_z:Array = new Array
var i:int
i = shapes.length
while (i--) {
var vector:Vector3D = matrix.transformVector(new Vector3D(shapes[i].x,shapes[i].y,shapes[i].z))
position_z.push({idx:i, z:vector.z})
}
i = container.numChildren
while (i--) {container.removeChildAt(0)}
i = shapes.length
while (i--){
if (position_z[i].z > 10) container.addChild(shapes[position_z[i].idx])
}
container.rotationY = theta
container.rotationZ = tilt
}
}
}
}