遠近法のテスト4
/**
* Copyright gaziya ( http://wonderfl.net/user/gaziya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/90KE
*/
package {
import flash.filters.BlurFilter
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
//z = -100
var container:Sprite= addChild(new Sprite) as Sprite
var shapes:Vector.<Shape> = new Vector.<Shape>
/*
var max_radius:Number = 200
for (var i:int = 0; i <200; i++){
var radius:Number = Math.sqrt(Math.random())*max_radius
var theta_x:Number = Math.random()*360
var theta_y:Number = Math.random()*360
setShape(radius,theta_x,theta_y)
}
*/
var radius:int = 200
var split:int = 15
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 rect:Shape = container.addChild(new Shape) as Shape
shapes.push(rect)
var rect_size:int = 30
rect.graphics.beginFill(Math.round(Math.random()*0xffff)*0x100)
rect.graphics.drawRect(-rect_size/2, -rect_size/2,rect_size,rect_size)
rect.rotationX = -theta_x
rect.rotationY = theta_y
rect.x = radius * Math.cos(theta_x*Math.PI/180) * Math.sin(theta_y*Math.PI/180)
rect.y = radius * Math.sin(theta_x*Math.PI/180)
rect.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})
}
position_z.sortOn("z", Array.NUMERIC | Array.DESCENDING)
i = shapes.length
while (i--) {
container.setChildIndex(shapes[position_z[i].idx],i)
var blur:Number = position_z[i].z/20
shapes[position_z[i].idx].filters = (position_z[i].z > 0) ? [new BlurFilter(blur, blur, 2)] : []
}
container.rotationY = theta
container.rotationZ = tilt
}
}
}
}