📄 object3d.as
字号:
}
interactive = true;
}
public function broadcastCLICK () : void
{
//trace ("broadcastCLICK " + objName);
dispatchEvent ( new Event(MouseEvent.CLICK) );
}
public function broadcastMOUSEOVER () : void
{
//trace ("broadcastRollOver " + objName);
dispatchEvent ( new Event(MouseEvent.MOUSE_OVER) );
}
public function broadcastMOUSEOUT () : void
{
//trace("broadcastRollOut " + objName);+ objName);
dispatchEvent ( new Event(MouseEvent.MOUSE_OUT) );
}
public function broadcastMouseDown () : void
{
//trace ("broadcastMouseDown " + objName);
dispatchEvent ( new Event(MouseEvent.MOUSE_DOWN) );
}
public function broadcastMouseUp () : void
{
//trace ("broadcastMouseUp " + objName);
dispatchEvent ( new Event(MouseEvent.MOUSE_UP) );
}
/*
* project vertex points to camera coordinates
*/
public function project ( cam:Camera3D, occlusion:Boolean = false ) : int
{
var rme:Boolean = renderMe;
var rcam:Boolean = cam.renderMe;
renderMe = false;
if ((!rme && !rcam) || !isVisible ) return 0;
// get Camera parameters
var m:Matrix3x3 = cam.getRotationMatrix();
var camV:Vector = cam.getPosition();
var camPosX:int = camV.x,
camPosY:int = camV.y,
camPosZ:int = camV.z,
camEyeZ:int = cam.eyeZ, // also known as perspective or FOV
camEyeX:int = cam.eyeX,
camEyeY:int = cam.eyeY,
zClipping:int = cam.zClipping,
x:int,
y:int,
z:int,
zcalc:int,
visibleFaces:int = 0,
w:int = cam.w,
h:int = cam.h,
rvx:int,
rvy:int,
rvz:int;
if ( rme )
{
var rm:Matrix3x3 = update_rotation_matrix (); // get object transformed vertices
var oVs:Array = oVertices; // non transformed object (original) vertices
var xpos:int = X,
ypos:int = Y,
zpos:int = Z,
vox:int,
voy:int,
voz:int;
var rmaa:Number = rm.aa,
rmab:Number = rm.ab,
rmac:Number = rm.ac,
rmba:Number = rm.ba,
rmbb:Number = rm.bb,
rmbc:Number = rm.bc,
rmca:Number = rm.ca,
rmcb:Number = rm.cb,
rmcc:Number = rm.cc,
sx:Number = scale.x,
sy:Number = scale.y,
sz:Number = scale.z;
}
var tvs:Array = tVertices; // current object vertices (transformed)
var vtx:Vertex; // vertex obj for holding current vertex
if ( occlusion ) resetOBB ();
// transform each vertex and create screen coordinates
var i:int = tvs.length
//while (vtx = tvs[--i])
//{
while (--i > -1)
{
vtx = tvs[i] as Vertex;
// apply transformations to a temporary vertex that is holding original vertex data
if ( rme ) // only if needed
{
// points to the original vertex information
// and applies scale
vox = oVs[i].x * sx;
voy = oVs[i].y * sy;
voz = oVs[i].z * sz;
// use rotation matrix to apply rotation transformations
// & translate vertex to current object position
vtx.x = int((vox * rmaa + voy * rmab + voz * rmac) + xpos);
vtx.y = int((vox * rmba + voy * rmbb + voz * rmbc) + ypos); // Y goes down to conform with camera
vtx.z = int((vox * rmca + voy * rmcb + voz * rmcc) + zpos);
}
// translate the point to the camera
x = vtx.x - camPosX;
y = vtx.y - camPosY;
z = vtx.z - camPosZ;
// apply camera rotation matrix
rvx = x * m.aa + y * m.ab + z * m.ac;
rvy = x * m.ba + y * m.bb + z * m.bc;
rvz = x * m.ca + y * m.cb + z * m.cc;
// shift world to the camera to make it first person (the camera at your eye)
z = rvz - camEyeZ;
// check if vertex is visible
//vtx.visible = ( z > -camEyeZ );
vtx.visible = ( z > 0 );
// only calculate screen coords for visible vertices
if ( !vtx.visible ) continue;
// convert to screen coordinates
//vtx.screen.x = (camEyeX + (rvx * camEyeZ) / (z + camEyeZ)) >> 0;
//vtx.screen.y = (camEyeY + (rvy * camEyeZ) / (z + camEyeZ)) >> 0;
vtx.screen.x = int(camEyeX + (rvx * camEyeZ) / z);
vtx.screen.y = int(camEyeY + (rvy * camEyeZ) / z);
vtx.screen.z = z;
// calculate bounding box/*
if ( occlusion )
{
OBB.min.x = min( vtx.screen.x, OBB.min.x );
OBB.max.x = max( vtx.screen.x, OBB.max.x );
OBB.min.y = min( vtx.screen.y, OBB.min.y );
OBB.max.y = max( vtx.screen.y, OBB.max.y );
OBB.minz.z = min( z, OBB.minz.z );
OBB.maxz.z = max( z, OBB.maxz.z );
}
++visibleFaces;
}
// check if object has visible vertices which means faces can be rendered
if (visibleFaces > 3)
{
facesVisible = true;
// get distance to camera
if ( occlusion ) {
screenZ = distance( cam.getPosition() );
}
return 1;
}
return 0;
}
public function getObjectArea () : int
{
return int(((OBB.max.x - OBB.min.x) * (OBB.max.y - OBB.min.y)));
}
public function distance ( v:Vector ) : int
{
var xd:int = position.x - v.x;
var yd:int = position.y - v.y;
var zd:int = position.z - v.z;
return sqrt(xd*xd+yd*yd+zd*zd);
}
public function getRotationMatrix () : Matrix3x3
{
return rotation_matrix;
}
public function getAngleRotation () : Vector
{
return angleRotation;
}
// calculates the internal rotation matrix
private function update_rotation_matrix () : Matrix3x3
{
// set the elements of the rotation matrix
rotation_matrix.aa = side.x;
rotation_matrix.ab = side.y;
rotation_matrix.ac = side.z;
rotation_matrix.ba = up.x;
rotation_matrix.bb = up.y;
rotation_matrix.bc = up.z;
rotation_matrix.ca = out.x;
rotation_matrix.cb = out.y;
rotation_matrix.cc = out.z;
return rotation_matrix;
}
/*
protected function transform_vertices ( transformation:Matrix3x3 ) : void
{
var i:Number = tVertices.length;
// loop through the vertices to transform them
while (i--)
{
if(transformation) {
// calculate the transformed vertices
tVertices[i] = transformation.vertex_multiplication ( oVertices[i] );
} else {
// a transformation is not being performed
// so displace the vertices by the mesh's position
tVertices[i].x = oVertices[i].x;
tVertices[i].y = oVertices[i].y;
tVertices[i].z = oVertices[i].z;
}
tVertices[i].x += position.x;
tVertices[i].y += position.y; // make sure Y goes up??
tVertices[i].z += position.z;
}
}
*/
private function resetOBB () : void
{
//OBB = new Object();
OBB.min = new Point(Infinity,Infinity);
OBB.max = new Point(-Infinity,-Infinity);
OBB.maxz = new Point3D(1,1,-Infinity);
OBB.minz = new Point3D(1,1,Infinity)
//OBB.size = new Point(Infinity,Infinity);
}
public function boundingBox() : Object
{
var tvs:Array = tVertices;
var vt:Vertex;
var v:Vector = tvs[0].screen;
OBB.min = new Point(v.x,v.y);
OBB.max = new Point(v.x,v.y);
OBB.size = new Point(v.x,v.y);
for each ( vt in tvs )
{
v = vt.screen;
OBB.min.x = min( v.x, OBB.min.x );
OBB.max.x = max( v.x, OBB.max.x );
OBB.min.y = min( v.y, OBB.min.y );
OBB.max.y = max( v.y, OBB.max.y );
OBB.min.z = min( v.z, OBB.min.z );
OBB.max.z = max( v.z, OBB.max.z );
}
OBB.size.x = OBB.max.x - OBB.min.x;
OBB.size.y = OBB.max.y - OBB.min.y;
OBB.size.z = OBB.max.z - OBB.min.z;
return OBB;
}
public function get X() : Number
{
return position.x;
}
public function set X ( value:Number ) : void
{
position.x = value;
renderMe = true;
}
public function get Y() : Number
{
return position.y;
}
public function set Y ( value:Number ) : void
{
position.y = value;
renderMe = true;
}
public function get Z() : Number
{
return position.z;
}
public function set Z ( value:Number ) : void
{
position.z = value;
renderMe = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -