网络日志

FLASH 3D的数字7及as3代码

作者:小龙博客-网络日志 2010年01月10日  1条评论 分类:AS3 学习   

flash 3d的数字7效果及as3代码:

package {
public class Point3D {
public var fl:Number=250;
private var vpX:Number=0;
private var vpY:Number=0;
private var cX:Number=0;
private var cY:Number=0;
private var cZ:Number=0;
public var x:Number=0;
public var y:Number=0;
public var z:Number=0;

public function Point3D(x:Number=0, y:Number=0, z:Number=0) {
this.x=x;
this.y=y;
this.z=z;
}
public function setVanishingPoint(vpX:Number, vpY:Number):void {
this.vpX=vpX;
this.vpY=vpY;
}
public function setCenter(cX:Number,cY:Number,cZ:Number=0):void {
this.cX=cX;
this.cY=cY;
this.cZ=cZ;
}
public function get screenX():Number {
var scale:Number = fl / (fl + z + cZ);
return vpX + cX + x * scale;
}
public function get screenY():Number {
var scale:Number = fl / (fl + z + cZ);
return vpY + cY + y * scale;
}
public function rotateX(angleX:Number):void {
var cosX:Number=Math.cos(angleX);
var sinX:Number=Math.sin(angleX);
var y1:Number=y*cosX-z*sinX;
var z1:Number=z*cosX+y*sinX;
y=y1;
z=z1;
}
public function rotateY(angleY:Number):void {
var cosY:Number=Math.cos(angleY);
var sinY:Number=Math.sin(angleY);
var x1:Number=x*cosY-z*sinY;
var z1:Number=z*cosY+x*sinY;
x=x1;
z=z1;
}
public function rotateZ(angleZ:Number):void {
var cosZ:Number=Math.cos(angleZ);
var sinZ:Number=Math.sin(angleZ);
var x1:Number=x*cosZ-y*sinZ;
var y1:Number=y*cosZ+x*sinZ;
x=x1;
y=y1;
}
}
}
//================================

package {
import flash.display.Graphics;
public class Triangle {
private var pointA:Point3D;
private var pointB:Point3D;
private var pointC:Point3D;
private var color:uint;
public function Triangle(a:Point3D, b:Point3D, c:Point3D, color:uint) {
pointA=a;
pointB=b;
pointC=c;
this.color=color;
}
public function draw(g:Graphics):void {
/*if (isBackFace()) {
return;
}*/
g.lineStyle(0,0.3);
g.beginFill(color,0.3);
g.moveTo(pointA.screenX, pointA.screenY);
g.lineTo(pointB.screenX, pointB.screenY);
g.lineTo(pointC.screenX, pointC.screenY);
g.lineTo(pointA.screenX, pointA.screenY);
g.endFill();
}
/*private function isBackFace():Boolean {
var cax:Number=pointC.screenX-pointA.screenX;
var cay:Number=pointC.screenY-pointA.screenY;
var bcx:Number=pointB.screenX-pointC.screenX;
var bcy:Number=pointB.screenY-pointC.screenY;
return cax*bcy>cay*bcx;
}*/

}
}

//======================

package {
import flash.display.Sprite;
import flash.events.Event;
public class Extruded extends Sprite {
private var points:Array;
private var triangles:Array;
private var fl:Number=250;
private var vpX:Number=stage.stageWidth/2;
private var vpY:Number=stage.stageHeight/2;
public function Extruded() {
init();
}
private function init():void {
points=new Array ;
//===========6个点===================
points[0]=new Point3D(-100,-100,-50);
points[1]=new Point3D(100,-100,-50);
points[2]=new Point3D(100,0,-50);
points[3]=new Point3D(0,200,-50);
points[4]=new Point3D(-50,200,-50);
points[5]=new Point3D(50,0,-50);
points[6]=new Point3D(50,-50,-50);
points[7]=new Point3D(-100,-50,-50);

points[8]=new Point3D(-100,-100,50);
points[9]=new Point3D(100,-100,50);
points[10]=new Point3D(100,0,50);
points[11]=new Point3D(0,200,50);
points[12]=new Point3D(-50,200,50);
points[13]=new Point3D(50,0,50);
points[14]=new Point3D(50,-50,50);
points[15]=new Point3D(-100,-50,50);

for (var i:uint=0; i<points.length; i++) {
points[i].setVanishingPoint(vpX,vpY);//对每个点设置消失点
points[i].setCenter(0,0,200);//设置Z坐标向后推移
}
triangles=new Array ;
//================34个三角形==========
triangles[0]=new Triangle(points[0],points[1],points[6],0xff0055);//正面的顺时针
triangles[1]=new Triangle(points[0],points[6],points[7],0xff0055);
triangles[2]=new Triangle(points[6],points[1],points[2],0xff0055);
triangles[3]=new Triangle(points[6],points[2],points[5],0xff0055);
triangles[4]=new Triangle(points[5],points[2],points[3],0xff0055);
triangles[5]=new Triangle(points[5],points[3],points[4],0xff0055);

triangles[6]=new Triangle(points[8],points[14],points[9],0xff0055);//反
triangles[7]=new Triangle(points[8],points[15],points[14],0xff0055);
triangles[8]=new Triangle(points[14],points[10],points[9],0xff0055);
triangles[9]=new Triangle(points[14],points[13],points[10],0xff0055);
triangles[10]=new Triangle(points[13],points[11],points[10],0xff0055);
triangles[11]=new Triangle(points[13],points[12],points[11],0xff0055);

triangles[12]=new Triangle(points[0],points[8],points[9],0x00ff55);//侧
triangles[13]=new Triangle(points[0],points[9],points[1],0x00ff55);
triangles[14]=new Triangle(points[1],points[9],points[10],0x00ff55);
triangles[15]=new Triangle(points[1],points[10],points[2],0x00ff55);
triangles[16]=new Triangle(points[2],points[10],points[11],0x00ff55);
triangles[17]=new Triangle(points[2],points[11],points[3],0x00ff55);
triangles[18]=new Triangle(points[3],points[11],points[12],0x00ff55);
triangles[19]=new Triangle(points[3],points[12],points[4],0x00ff55);
triangles[20]=new Triangle(points[4],points[12],points[13],0x00ff55);
triangles[21]=new Triangle(points[4],points[13],points[5],0x00ff55);
triangles[22]=new Triangle(points[5],points[13],points[14],0x00ff55);
triangles[23]=new Triangle(points[5],points[14],points[6],0x00ff55);
triangles[24]=new Triangle(points[6],points[14],points[15],0x00ff55);
triangles[25]=new Triangle(points[6],points[15],points[7],0x00ff55);
triangles[26]=new Triangle(points[7],points[15],points[8],0x00ff55);
triangles[27]=new Triangle(points[7],points[8],points[0],0x00ff55);

addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function onEnterFrame(e:Event):void {
var angleX:Number=(mouseY-vpY)*.001;
var angleY:Number=(mouseX-vpX)*.001;
for (var i:uint=0; i<points.length; i++) {
var point:Point3D=points[i];
point.rotateX(angleX);
point.rotateY(angleY);
}
graphics.clear();
for (i=0; i<triangles.length; i++) {
triangles[i].draw(graphics);
}
}
}
}

本文标题:FLASH 3D的数字7及as3代码

网址:http://www.lxlong.com/blog/archives/380.html

其他文章:
   
« »

1条评论

  1. Batreders 说:

    在我们的网站上拥有超过280台角子机,在其中您可以按照任何反应,玩免费的,未经登记。每周更新的游戏,漂亮的设计和界面的网站。免费玩游戏,在游戏中培养自己!

发表评论 »