import
flash.display.Shape;
import
flash.geom.Vector3D;
import
flash.geom.Matrix3D;
import
flash.events.MouseEvent;
var
scale:
int
=
160
;
var
density:
int
=
10
;
var
densityCube:
int
= density*density*density;
var
points:Vector.<Shape> =
new
Vector.<Shape>(densityCube);
var
pointsPosition:Vector.<Vector3D> =
new
Vector.<Vector3D>(densityCube);
var
centralPoint:Vector3D =
new
Vector3D(stage.stageWidth/
2
, stage.stageHeight/
2
, scale/
2
);
var
angleSpd:
Number
=
0
;
var
mouseAngle:
Number
=
0
;
var
mouseDistance:
Number
=
0
;
var
mouseSin:
Number
=
0
;
var
mouseCos:
Number
=
1
;
for
(
var
i_z:
int
=
0
; i_z<density; i_z++) {
for
(
var
i_y:
int
=
0
; i_y<density; i_y++) {
for
(
var
i_x:
int
=
0
; i_x<density; i_x++) {
var
index:
int
= density*density*i_z+density*i_y+i_x;
var
color:
uint
=
int
(i_z/density*
256
)<<
16
|
int
(i_y/density*
256
)<<
8
|
int
(i_x/density*
256
);
points[index] =
new
Shape();
points[index].graphics.beginFill(color);
points[index].graphics.drawRect(
0
,
0
,
4
,
4
);
points[index].graphics.endFill();
pointsPosition[index] =
new
Vector3D();
pointsPosition[index].x = centralPoint.x-scale/
2
+ i_x*scale/(density-
1
);
pointsPosition[index].y = centralPoint.y-scale/
2
+ i_y*scale/(density-
1
);
pointsPosition[index].z = centralPoint.z-scale/
2
+ i_z*scale/(density-
1
);
points[index].x = pointsPosition[index].x;
points[index].y = pointsPosition[index].y;
points[index].z = pointsPosition[index].z;
addChild(points[index]);
}
}
}
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function
enterFrameHandler(evt:Event):
void
{
for
(
var
index:
int
=
0
; index<densityCube; index++) {
pointsPosition[index].x -= centralPoint.x;
pointsPosition[index].y -= centralPoint.y;
pointsPosition[index].z -= centralPoint.z;
var
old:Vector3D =
new
Vector3D(pointsPosition[index].x, pointsPosition[index].y, pointsPosition[index].z);
pointsPosition[index].y = Math.cos(angleSpd*mouseSin)*old.y - Math.sin(angleSpd*mouseSin)*old.z;
pointsPosition[index].z = Math.sin(angleSpd*mouseSin)*old.y + Math.cos(angleSpd*mouseSin)*old.z;
old.x=pointsPosition[index].x; old.y=pointsPosition[index].y; old.z=pointsPosition[index].z;
pointsPosition[index].x = Math.cos(angleSpd*mouseCos)*old.x - Math.sin(angleSpd*mouseCos)*old.z;
pointsPosition[index].z = Math.sin(angleSpd*mouseCos)*old.x + Math.cos(angleSpd*mouseCos)*old.z;
pointsPosition[index].x += centralPoint.x;
pointsPosition[index].y += centralPoint.y;
pointsPosition[index].z += centralPoint.z;
points[index].x = pointsPosition[index].x;
points[index].y = pointsPosition[index].y;
points[index].z = pointsPosition[index].z;
}
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
function
mouseMoveHandler(evt:MouseEvent):
void
{
var
dx:
Number
= stage.mouseX-stage.stageWidth/
2
;
var
dy:
Number
= stage.mouseY-stage.stageHeight/
2
;
mouseDistance = Math.sqrt(dx*dx+dy*dy);
mouseCos = dx/mouseDistance;
mouseSin = dy/mouseDistance;
angleSpd = mouseDistance/
2000
;
}