色表現練習7
/**
* Copyright gaziya ( http://wonderfl.net/user/gaziya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aBxX
*/
package {
import frocessing.color.ColorHSV
import flash.geom.ColorTransform
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() {
graphics.beginFill(0x5)
graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight)
var container:Sprite= addChild(new Sprite) as Sprite
with (container) {
x = stage.stageWidth/2
y = stage.stageHeight/2
z = -150
}
var shapes:Vector.<Shape> = new Vector.<Shape>
for (var i:int = 0; i <1000; i++){
var radius:Number = Math.sqrt(Math.random())*200 + 100
var theta_x:Number = Math.random()*360
var theta_y:Number = Math.random()*360
var rect:Shape = container.addChild(new Shape) as Shape
shapes.push(rect)
var rect_size:int = Math.random()*20 + 10
with (rect) {
graphics.beginFill(0)
graphics.drawRect(-rect_size/2, -rect_size/2,rect_size,rect_size)
rotationX = -theta_x
rotationY = theta_y
x = radius * Math.cos(theta_x*Math.PI/180) * Math.sin(theta_y*Math.PI/180)
y = radius * Math.sin(theta_x*Math.PI/180)
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
var tilt:Number = 0
function loop(e:Event):void {
theta = (theta+1)%360
tilt = (tilt+0.1)%360
var matrix:Matrix3D = new Matrix3D
matrix.appendRotation(theta,Vector3D.Y_AXIS)
var position_z:Array = new Array
var i:int
for (i = 0; i < shapes.length;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)
for (i = 0; i < container.numChildren; i++) {container.removeChildAt(0)}
var color_transform:ColorTransform = new ColorTransform
var color : ColorHSV = new ColorHSV(tilt)
color.s = 0.8
for (i = 0; i < shapes.length;i++) {
if (position_z[i].z > 30) {
container.addChild(shapes[position_z[i].idx])
color.v = 1 - position_z[i].z/300
color_transform.color = color.value
shapes[position_z[i].idx].transform.colorTransform = color_transform
}
}
container.rotationY = theta
container.rotationZ = tilt
}
}
}
}