Cubes


なんとなく。キューブが回転する奴。

▼Wonderfl

▼ActionScript AS3(FP10)
[sourcecode language=”as3″]
/*
なんとなく。キューブが回転する奴。
10年くらい前に3Dソフトでとりあえずこんな感じの
ものを作ったような。

*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.PerspectiveProjection;
import flash.geom.Point;
import flash.geom.Vector3D;

[SWF(backgroundColor="0xF8F8F8")]
public class Main extends Sprite
{
private var _box:Array = [];
private var _canvas:Sprite;
private var _bitmapData:BitmapData;
private var _bitmap:Bitmap;
public function Main()
{
//stage.scaleMode = "noScale";
_bitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0xF8F8F8);
_bitmap = new Bitmap(_bitmapData,"auto",true);
this.addChild(_bitmap)
_canvas = new Sprite();

stage.transform.perspectiveProjection = new PerspectiveProjection();
stage.transform.perspectiveProjection.projectionCenter = new Point(stage.stageWidth/2,stage.stageHeight/2);
var n:int = 10;
for (var i:int = 0; i < n; i++) {
_box[i] = new Box();
_box[i].x = stage.stageWidth/2-70*Math.sin(2*Math.PI*i/n)+Math.random()*40-20;
_box[i].y = stage.stageHeight/2-70*Math.cos(2*Math.PI*i/n)+Math.random()*40-20;
_box[i].z = 300*i-200;
_box[i].rotationZ = 5*i;
_canvas.addChild(_box[i]);
}
this.addEventListener(Event.ENTER_FRAME,onEnter);
}
private function onEnter(event:Event):void{
var n:int = _box.length;
for (var i:int = 0; i < n; i++) {
_box[i].rotationX ++;
var z:Number = _box[i].z-1;
_box[i].z = 2500-(2500-z)%3000;
_box[i].rotationY += 0.3;
_box[i].rotationZ += 0.005;
_box[i].alpha = Math.min(1,(500+z)/250)
}
zSort(_canvas);

_bitmapData.lock();
var bd:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0xF8F8F8);
bd.draw(_canvas);
_bitmapData.draw(bd,new Matrix(-1,0,0,-1,stage.stageWidth,stage.stageHeight));
_bitmapData.draw(bd,null,null,"darken");
_bitmapData.unlock();

}
private function zSort(target:DisplayObjectContainer):void {
var n:int = target.numChildren;
var array:Array = [];
var reference:Array = [];
for (var i:int = 0; i < n; i++) {
var poz:Vector3D = target.getChildAt(i).transform.getRelativeMatrix3D(stage).position;
var point:Point = stage.transform.perspectiveProjection.projectionCenter;
array[i] = poz.subtract(new Vector3D(point.x,point.y,-stage.transform.perspectiveProjection.focalLength)).length;
reference[i] = target.getChildAt(i);
}
var temp:Array = array.sort(Array.NUMERIC | Array.RETURNINDEXEDARRAY);
for (i = 0; i < n; i++) {
target.setChildIndex(reference[temp[i]],0);
if(reference[temp[i]].numChildren > 1){
zSort(reference[temp[i]]);
}
}
}
}
}
import flash.display.Sprite;

class Box extends Sprite{
public function Box(){
var colors:Array = [0x000000,0x111111,0x222222,0x333333,0x444444,0x555555];
var ran:int = Math.floor(Math.random()*6);
for (var i:int = 0; i < 6; i++) {
var sp:Sprite = new Sprite();
sp.graphics.beginFill(colors[(i+ran)%6],1);
sp.graphics.drawRect(-50,-50,100,100);
sp.graphics.endFill();
switch (i) {
case 0:
sp.z = -50;
break;
case 1:
sp.z = 50;
break;
case 2:
sp.rotationX = 90;
sp.y = 50;
break;
case 3:
sp.rotationX = 90;
sp.y = -50;
break;
case 4:
sp.rotationY = 90;
sp.x = -50;
break;
case 5:
sp.rotationY = 90;
sp.x = 50;
break;
}
this.addChild(sp);
}
}
}
[/sourcecode]