3D Experimentation
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class FlashTest extends Sprite {
public var rx:Number;
public var ry:Number;
public var rz:Number;
public var Models:Array;
public var ox:Number;
public var oy:Number;
public var vx:Number;
public var vy:Number;
public var drag:Boolean;
public var flickmode:Boolean;
public var light:Vector3D;
public var Polygons:Array;
public function FlashTest() {
Models = new Array();
Polygons = new Array();
light = new Vector3D(-2,-3,0);
//addModel(new Wing());
//addModel(new Ship());
//addModel(new Wing());
var t:Model = new Ship();
addModel(t);
//t.drawModel();
//Models[2].rotate(0,Math.PI,0);
drawPolygons();
x = stage.stageWidth/2;
y = stage.stageHeight/2;
scaleX = 2;
scaleY = 2;
rx = 0;
ry = 0.1;
rz = 0;
ox = 0;
oy = 0;
vx = 0;
vy = 0;
drag = false;
flickmode = false;
stage.addEventListener(Event.ENTER_FRAME,main);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseup);
}
public function main(e:Event):void{
if(flickmode){
if(vx > 0){
vx -= 0.001;
}else if(vx < 0){
vx += 0.001;
}
if(vy > 0){
vy -= 0.001;
}else if(vy < 0){
vy += 0.001;
}
for(var i:int=0;i<Models.length;i++){
Models[i].rotate(-vy*Math.PI,vx*Math.PI,0);
//Models[i].drawModel();
}
drawPolygons();
}
}
public function addModel(M:Model):void{
for(var i:int=0;i<M.Polygons.length;i++){
Polygons.push(M.Polygons[i]);
}
//Models.push(M);
//addChild(M);
}
public function drawPolygons():void{
graphics.clear();
Polygons.sortOn("zsort",Array.NUMERIC|Array.DESCENDING);
for(var i:int=0;i<Polygons.length;i++){
//Polygons[i].drawPolygon();
var P:Polygon = Polygons[i];
if(P.normal.z*P.D >= 0){
//graphics.lineStyle(1,0x000000,1);
graphics.beginFill(P.color,P.Alpha);
graphics.moveTo(P.p1.x,P.p1.y);
graphics.lineTo(P.p2.x,P.p2.y);
graphics.lineTo(P.p3.x,P.p3.y);
graphics.lineTo(P.p1.x,P.p1.y);
graphics.endFill();
if(P.shading){
var a:Number = Vector3D.angleBetween(P.normal,light);
graphics.beginFill(0x000000,1-a/(Math.PI/2));
graphics.moveTo(P.p1.x,P.p1.y);
graphics.lineTo(P.p2.x,P.p2.y);
graphics.lineTo(P.p3.x,P.p3.y);
graphics.lineTo(P.p1.x,P.p1.y);
graphics.endFill();
}
}
}
}
public function mousemove(e:MouseEvent):void{
if(drag){
if(flickmode){
vx = (vx+(mouseX - ox)/80)/5;
vy = (vy+(mouseY - oy)/80)/5;
}else{
vx = (mouseX-ox)/80;
vy = (mouseY-oy)/80;
//for(var i:int=0;i<Models.length;i++){
for(var i:int=0;i<Polygons.length;i++){
Polygons[i].rotate(-vy*Math.PI,vx*Math.PI,0);
//Models[i].rotate(-vy*Math.PI,vx*Math.PI,0);
//Models[i].drawModel();
}
drawPolygons();
}
}
ox = mouseX;
oy = mouseY;
}
public function mousedown(e:MouseEvent):void{
drag = true;
}
public function mouseup(e:MouseEvent):void{
drag = false;
}
}
}
import flash.display.MovieClip;
import flash.geom.*;
class Point3D extends MovieClip{
function Point3D(X:Number,Y:Number,Z:Number):void{
x = X;
y = Y;
z = Z;
}
public function rotate(X:Number,Y:Number,Z:Number):void{
var d:Number = Math.sqrt(Math.pow(y,2)+Math.pow(z,2));
var a:Number = Math.atan(y/z)+X;
if(z < 0){
a += Math.PI;
}
if(d){
z = Math.cos(a)*d;
y = Math.sin(a)*d;
}
d = Math.sqrt(Math.pow(x,2)+Math.pow(z,2));
a = Math.atan(z/x)+Y;
if(x < 0){
a += Math.PI;
}
if(d){
x = Math.cos(a)*d;
z = Math.sin(a)*d;
}
d = Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
a = Math.atan(y/x)+Z;
if(x < 0){
a += Math.PI;
}
if(d){
x = Math.cos(a)*d;
y = Math.sin(a)*d;
}
}
}
class Polygon extends MovieClip{
public var p1:Point3D;
public var p2:Point3D;
public var p3:Point3D;
public var v1:Vector3D;
public var v2:Vector3D;
public var normal:Vector3D;
public var rx:Number;
public var ry:Number;
public var rz:Number;
public var color:uint;
public var Alpha:Number;
public var D:Number;
public var shading:Boolean;
public var zsort:Number;
function Polygon(P1:Point3D,P2:Point3D,P3:Point3D, d:Number=1,c:uint=0xff00ff,a:Number=1):void{
p1 = P1;
p2 = P2;
p3 = P3;
D = d;
color = c;
Alpha = a;
rx = 0;
ry = 0;
rz = 0;
shading = true;
zsort = 0;
rotate(0,0,0);
}
public function rotate(X:Number,Y:Number,Z:Number):void{
rx += x;
ry += y;
rz += z;
p1.rotate(X,Y,Z);
p2.rotate(X,Y,Z);
p3.rotate(X,Y,Z);
v1 = new Vector3D(p2.x-p1.x,p2.y-p1.y,p2.z-p1.z);
v2 = new Vector3D(p3.x-p1.x,p3.y-p1.y,p3.z-p1.z);
normal = v1.crossProduct(v2);
zsort = (p1.z+p2.z+p3.z)/3;
}
}
class Model extends MovieClip{
public var Polygons:Array;
public var rx:Number;
public var ry:Number;
public var rz:Number;
public function Model():void{
Polygons = new Array();
rx = 0;
ry = 0;
rz = 0;
}
public function rotate(X:Number,Y:Number,Z:Number):void{
for(var i:int=0;i<Polygons.length;i++){
rx += X;
ry += Y;
rz += Z;
Polygons[i].rotate(X,Y,Z);
}
}
public function addPolygon(X1:Number,Y1:Number,Z1:Number,X2:Number,Y2:Number,Z2:Number,X3:Number,Y3:Number,Z3:Number,d:Number=1,c:uint=0xff00ff,a:Number=1):void{
var p1:Point3D = new Point3D(X1,Y1,Z1);
var p2:Point3D = new Point3D(X2,Y2,Z2);
var p3:Point3D = new Point3D(X3,Y3,Z3);
var v:Polygon = new Polygon(p1,p2,p3,d,c,a);
addChild(v);
Polygons.push(v);
}
}
class Trian extends Model{
public override function Trian():void{
super();
addPolygon(
0,20,0,
10,0,10,
-10,0,10);
addPolygon(
0,20,0,
-10,0,10,
-10,0,-10);
addPolygon(
0,20,0,
-10,0,-10,
10,0,-10);
addPolygon(
0,20,0,
10,0,-10,
10,0,10);
}
}
class Ship extends Model{
public override function Ship():void{
super();
//left wing
addPolygon(
-20,10,0,
-20,0,10,
-30,0,0,
1,0xff0000);
addPolygon(
-20,10,0,
-30,0,0,
-20,0,-10,
1,0xff0000);
addPolygon(
-20,-40,0,
-20,0,-10,
-30,0,0,
1,0xff0000);
addPolygon(
-20,-40,0,
-30,0,0,
-20,0,10,
1,0xff0000);
addPolygon(
-20,10,0,
-20,0,-10,
-20,0,10,
1,0xff0000);
addPolygon(
-20,-40,0,
-20,0,10,
-20,0,-10,
1,0xff0000);
//right wing
addPolygon(
20,10,0,
30,0,0,
20,0,10,
1,0xff0000);
addPolygon(
20,10,0,
20,0,-10,
30,0,0,
1,0xff0000);
addPolygon(
20,-40,0,
30,0,0,
20,0,-10,
1,0xff0000);
addPolygon(
20,-40,0,
20,0,10,
30,0,0,
1,0xff0000);
addPolygon(
20,10,0,
20,0,10,
20,0,-10,
1,0xff0000);
addPolygon(
20,-40,0,
20,0,-10,
20,0,10,
1,0xff0000);
//window
addPolygon(
0,-40,0,
0,-5,-10,
-10,-10,0,
1,0x0000ff);
addPolygon(
0,-40,0,
10,-10,0,
0,-5,-10,
1,0x0000ff);
//top
addPolygon(
0,-5,-10,
-20,0,0,
-10,-10,0,
1,0xff0000);
addPolygon(
0,-5,-10,
10,-10,0,
20,0,0,
1,0xff0000);
addPolygon(
0,-5,-10,
0,5,0,
-20,0,0,
1,0xff0000);
addPolygon(
0,-5,-10,
20,0,0,
0,5,0,
1,0xff0000);
//underside
addPolygon(
0,-40,0,
-10,-10,0,
0,0,10,
1,0xff0000);
addPolygon(
0,-40,0,
0,0,10,
10,-10,0,
1,0xff0000);
addPolygon(
-10,-10,0,
-20,0,0,
0,0,10,
1,0xff0000);
addPolygon(
10,-10,0,
0,0,10,
20,0,0,
1,0xff0000);
addPolygon(
0,0,10,
-20,0,0,
0,5,0,
1,0xff0000);
addPolygon(
0,0,10,
0,5,0,
20,0,0,
1,0xff0000);
//rear
addPolygon(
0,5,0,
-20,10,0,
-20,5,-5,
1,0xff0000);
addPolygon(
0,5,0,
20,5,-5,
20,10,0,
1,0xff0000);
addPolygon(
0,5,0,
-20,5,5,
-20,10,0,
1,0xff0000);
addPolygon(
0,5,0,
20,10,0,
20,5,5,
1,0xff0000);
addPolygon(
0,5,0,
-20,5,-5,
-20,0,0,
1,0xff0000);
addPolygon(
0,5,0,
20,0,0,
20,5,-5,
1,0xff0000);
addPolygon(
0,5,0,
-20,0,0,
-20,5,5,
1,0xff0000);
addPolygon(
0,5,0,
20,5,5,
20,0,0,
1,0xff0000);
}
}