📄 object3d.as
字号:
/**
* project3D Engine
* @author John Sword
* @version 2 - AS3
*/
package engine.objects
{
import engine.camera.Camera3D;
import engine.geom.Face;
import engine.geom.Point3D;
import engine.geom.UV;
import engine.geom.Vector;
import engine.geom.Vertex;
import engine.geom.convexHull2D;
import engine.materials.Material;
import engine.math.Matrix3x3;
import engine.math.VectorUtils;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.MouseEvent;
import flash.geom.Point;
public class Object3D extends EventDispatcher
{
/* public */
public var faces:Array; // object faces
public var tVertices:Array; // the mesh that will suport transformations
public var objID:int;
public var objName:String;
public var material:Material;
public var flipNormals:Boolean = false;
public var cull:Boolean = true;
public var isVisible:Boolean = true;
public var facesVisible:Boolean = true;
public var interactive:Boolean = false;
public var pickPos:Vector = new Vector();
public var pickUV:UV = new UV();
public var pickFace:Face;
public var hull:convexHull2D;
public var OBB:Object = new Object();
public var screenZ:Number = Infinity;
/* only accessible within extended objects */
protected var renderMe:Boolean = true;
protected var oVertices:Array; // original object vertices information
protected var position:Vector;
protected var scale:Vector;
/* private */
// object transformation vectors
private var side:Vector;
private var up:Vector;
private var out:Vector;
private var target:Vector = new Vector ();
private var angleRotation:Vector = new Vector();
// bounding box
private var bBox:Object;
/* static for speed */
private static const sin:Function = Math.sin;
private static const cos:Function = Math.cos;
private static const rotation_matrix:Matrix3x3 = new Matrix3x3 ();
private static const matrix:Matrix3x3 = new Matrix3x3();
private static const vo:Vertex = new Vertex();
//private static const minA:UV = new UV( 1000, 1000 );
//private static const maxA:UV = new UV( -1000, -1000 );
//private static const area:UV = new UV();
private static const max:Function = Math.max;
private static const min:Function = Math.min;
private static const sqrt:Function = Math.sqrt;
public function Object3D ()
{
// geometry arrays
oVertices = new Array();
tVertices = new Array();
faces = new Array();
objName = "";
// model scale
scale = new Vector (1,1,1);
// position vectors
position = new Vector ();
// set rotation vectors
resetRotationVectors ();
}
public function resetRotationVectors () : void
{
side = new Vector (1.0, 0.0, 0.0);
up = new Vector (0.0, 1.0, 0.0);
out = new Vector (0.0, 0.0, 1.0);
}
public function setMaterial ( mat:Material ) : void
{
material = mat;
}
public function createVertex ( v:Vertex ) : int
{
oVertices.push(v.clone());
tVertices.push(v);
return oVertices.length - 1
}
public function createFace ( v1:Number, v2:Number, v3:Number,
uv1:UV, uv2:UV, uv3:UV, m:Material ) : void
{
faces.push( new Face ( tVertices[v1] as Vertex,
tVertices[v2] as Vertex,
tVertices[v3] as Vertex, uv1, uv2, uv3, m )
);
}
public function createFaceVs ( v1:Vertex, v2:Vertex, v3:Vertex,
uv1:UV, uv2:UV, uv3:UV, m:Material ) : void
{
var vlen:int = oVertices.length;
createVertex( v1 );
createVertex( v2 );
createVertex( v3 );
faces.push( new Face ( v1, v2, v3, uv1, uv2, uv3, m ) );
}
public function setPosition (v:Vector) : void
{
position = v;
renderMe = true;
}
public function getPosition() : Vector
{
return position;
}
public function getScale () : Vector
{
return scale;
}
// rotates the 'up' and 'out' vectors about the 'side' vector
public function Pitch (a:Number) : void
{
// load the elements of the rotation matrix
matrix.load_rotation_axis (side, sin(a), cos(a));
// rotate the 'up' and 'out' vectors
up = matrix.vector_multiplication (up);
out = matrix.vector_multiplication (out);
renderMe = true;
}
// rotates the 'side' and 'out' vectors about the 'up' vector
public function Yaw (a:Number) : void
{
// load the elements of the rotation matrix
matrix.load_rotation_axis (up, sin(a), cos(a));
// rotate the 'side' and 'out' vectors
side = matrix.vector_multiplication (side);
out = matrix.vector_multiplication (out);
renderMe = true;
}
// rotates the 'side' and 'up' vectors about the 'out' vector
public function Roll (a:Number) : void
{
// load the elements of the rotation matrix
matrix.load_rotation_axis (out, sin(a), cos(a));
// rotate the 'side' and 'up' vectors
side = matrix.vector_multiplication (side);
up = matrix.vector_multiplication (up);
renderMe = true;
}
// rotates around the world x-axis
public function rotateX (a:Number) : void
{
// rotation matrix that will be applied
//var matrix:Matrix3x3 = new Matrix3x3 ();
// load the elements of the rotation matrix
matrix.load_rotation_x (sin(a), cos(a));
// rotate
side = matrix.vector_multiplication (side);
up = matrix.vector_multiplication (up);
out = matrix.vector_multiplication (out);
renderMe = true;
}
// rotates around the world y-axis
public function rotateY ( a:Number ) : void
{
// rotation matrix that will be applied
//var matrix:Matrix3x3 = new Matrix3x3 ();
// load the elements of the rotation matrix
matrix.load_rotation_y (sin(a), cos(a));
// rotate
side = matrix.vector_multiplication (side);
up = matrix.vector_multiplication (up);
out = matrix.vector_multiplication (out);
renderMe = true;
}
// rotates around the world z-axis
public function rotateZ (a:Number) : void
{
// rotation matrix that will be applied
//var matrix:Matrix3x3 = new Matrix3x3 ();
// load the elements of the rotation matrix
matrix.load_rotation_z (sin(a), cos(a));
// rotate
side = matrix.vector_multiplication (side);
up = matrix.vector_multiplication (up);
out = matrix.vector_multiplication (out);
renderMe = true;
}
// rotates around center using degrees and a rotation vector
// the rotation is fixed
public function angleRotate ( a:Vector ) : void
{
resetRotationVectors();
// load the vector into the rotation matrix
matrix.angleRotation( a );
angleRotation = a;
// rotate
side = matrix.vector_multiplication (side);
up = matrix.vector_multiplication (up);
out = matrix.vector_multiplication (out);
renderMe = true;
}
public function moveXAxis (ammount:Number) : void
{
position.x += int(side.x * ammount);
position.y += int(side.y * ammount);
position.z += int(side.z * ammount);
renderMe = true;
}
public function moveYAxis (ammount:Number) : void
{
position.x += int(up.x * ammount);
position.y += int(up.y * ammount);
position.z += int(up.z * ammount);
renderMe = true;
}
public function moveZAxis (ammount:Number) : void
{
position.x += int(out.x * ammount);
position.y -= int(out.y * ammount);
position.z -= int(out.z * ammount);
renderMe = true;
}
// translate along the x-axis
public function translateX (a:Number) : void
{
position.x += a;
renderMe = true;
}
// translate along the y-axis
public function translateY (a:Number) : void
{
position.y += a;
renderMe = true;
}
// translate along the z-axis
public function translateZ (a:Number) : void
{
position.z += a;
renderMe = true;
}
// makes the object look at a point
// not sure if this is working properly
public function lookAt ( v:Vector ) : void
{
var v:Vector = new Vector(v.x,-v.y,v.z);
// set the out vector so that the object is looking at the point
out = VectorUtils.Normalize ( VectorUtils.difference(v,position) );
// calculate the side vector from the out vector and the world y-axis (world up vector)
side = VectorUtils.Normalize ( VectorUtils.cross( out, new Vector(0.0, 1.0, 0.0) ) );
// calculate the up vector from the out and side object vectors
up = VectorUtils.Normalize ( VectorUtils.cross ( side, out) );
side = VectorUtils.negate( side );
renderMe = true;
}
public function Scale( s:Vector ) : void
{
scale = s;
renderMe = true;
}
public function setInteractive () : void
{
var tvs:Array = tVertices; // current object vertices (transformed)
var vtx:Vertex; // vertex obj for holding current vertex
var i:int = tvs.length;
while( vtx = tvs[--i] )
{
vtx.o = this;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -