⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displayobject3d.as.svn-base

📁 Flex3实现的掼蛋游戏
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
	// RR  RR EE     NN NNN DD  DD EE     RR  RR
	// RR  RR EEEEEE NN  NN DDDDD  EEEEEE RR  RR

	/**
	* [internal-use] Render the projected object.
	*
	* @param	scene	The scene where the object belongs.
	*/
	public function render( scene :SceneObject3D ):void
	{
		var iFaces :Array = this._sorted;

		iFaces.sortOn( 'screenZ', Array.DESCENDING | Array.NUMERIC );

		// Render
		
		var container 	:Sprite = this.container || scene.container,
		rendered  		:Number = 0,
		iFace     		:Face3DInstance, 
		length			:Number = iFaces.length,
		i				:int = 0;	
		//for( var i:int = 0; iFace = iFaces[i]; i++ )
		//for( var i:int = 0; i < length ; i++ )
		while(i < length)
		{
			iFace = iFaces[i];
			if( faceLevelMode )
			{
				if( !iFace.container )
				{
					iFace.container = new InteractiveSprite(this);
					scene.container.addChild(iFace.container);
				}
				else
				{
					iFace.container.graphics.clear();
				}
				
				if( iFace.visible )
					rendered += iFace.face.render( iFace.instance,  iFace.container)
			}else
			{
				// if we're not in faceLevelMode, then it's render as usual - no extra checks in the render loop
				if( iFace.visible )
					rendered += iFace.face.render( iFace.instance, container );
			}	
			
			i++;		
		}

		// Update stats
		scene.stats.rendered += rendered;
	}

	// ___________________________________________________________________________________________________
	//                                                                     L O C A L   T R A N S F O R M S
	// LL      OOOO   CCCC    AA   LL
	// LL     OO  OO CC  CC  AAAA  LL
	// LL     OO  OO CC     AA  AA LL
	// LL     OO  OO CC  CC AAAAAA LL
	// LLLLLL  OOOO   CCCC  AA  AA LLLLLL

	/**
	* Translate the display object in the direction it is facing, i.e. it's positive Z axis.
	*
	* @param	distance	The distance that the object should move forward.
	*/
	public function moveForward  ( distance:Number ):void { translate( distance, FORWARD  ); }

	/**
	* Translate the display object in the opposite direction it is facing, i.e. it's negative Z axis.
	*
	* @param	distance	The distance that the object should move backward.
	*/
	public function moveBackward ( distance:Number ):void { translate( distance, BACKWARD ); }

	/**
	* Translate the display object lateraly, to the left of the direction it is facing, i.e. it's negative X axis.
	*
	* @param	distance	The distance that the object should move left.
	*/
	public function moveLeft     ( distance:Number ):void { translate( distance, LEFT     ); }

	/**
	* Translate the display object lateraly, to the right of the direction it is facing, i.e. it's positive X axis.
	*
	* @param	distance	The distance that the object should move right.
	*/
	public function moveRight    ( distance:Number ):void { translate( distance, RIGHT    ); }

	/**
	* Translate the display object upwards, with respect to the direction it is facing, i.e. it's positive Y axis.
	*
	* @param	distance	The distance that the object should move up.
	*/
	public function moveUp       ( distance:Number ):void { translate( distance, UP       ); }

	/**
	* Translate the display object downwards, with respect to the direction it is facing, i.e. it's negative Y axis.
	*
	* @param	distance	The distance that the object should move down.
	*/
	public function moveDown     ( distance:Number ):void { translate( distance, DOWN     ); }

	// ___________________________________________________________________________________________________
	//                                                                   L O C A L   T R A N S L A T I O N

	/**
	* Move the object along a given direction.
	*
	* @param	distance	The distance that the object should travel.
	* @param	axis		The direction that the object should move towards.
	*/
	public function translate( distance:Number, axis:Number3D ):void
	{
		var vector:Number3D = axis.clone();

		if( this._transformDirty ) updateTransform();

		Matrix3D.rotateAxis( transform, vector )

		this.x += distance * vector.x;
		this.y += distance * vector.y;
		this.z += distance * vector.z;
	}

	// ___________________________________________________________________________________________________
	//                                                                         L O C A L   R O T A T I O N

	/**
	* Rotate the display object around its lateral or transverse axis —an axis running from the pilot's left to right in piloted aircraft, and parallel to the wings of a winged aircraft; thus the nose pitches up and the tail down, or vice-versa.
	*
	* @param	angle	The angle to rotate.
	*/
	public function pitch( angle:Number ):void
	{
		angle = Papervision3D.useDEGREES? angle * toRADIANS : angle;

		var vector:Number3D = RIGHT.clone();

		if( this._transformDirty ) updateTransform();

		Matrix3D.rotateAxis( transform, vector );
		var m:Matrix3D = Matrix3D.rotationMatrix( vector.x, vector.y, vector.z, angle );

//		this.transform.copy3x3( Matrix3D.multiply3x3( m ,transform ) );
		this.transform.calculateMultiply3x3( m ,transform );

		this._rotationDirty = true;
	}


	/**
	* Rotate the display object around about the vertical axis —an axis drawn from top to bottom.
	*
	* @param	angle	The angle to rotate.
	*/
	public function yaw( angle:Number ):void
	{
		angle = Papervision3D.useDEGREES? angle * toRADIANS : angle;

		var vector:Number3D = UP.clone();

		if( this._transformDirty ) updateTransform();

		Matrix3D.rotateAxis( transform, vector );
		var m:Matrix3D = Matrix3D.rotationMatrix( vector.x, vector.y, vector.z, angle );

		this.transform.calculateMultiply3x3( m ,transform );

		this._rotationDirty = true;
	}


	/**
	* Rotate the display object around the longitudinal axis —an axis drawn through the body of the vehicle from tail to nose in the normal direction of flight, or the direction the object is facing.
	*
	* @param	angle
	*/
	public function roll( angle:Number ):void
	{
		angle = Papervision3D.useDEGREES? angle * toRADIANS : angle;

		var vector:Number3D = FORWARD.clone();

		if( this._transformDirty ) updateTransform();

		Matrix3D.rotateAxis( transform, vector );
		var m:Matrix3D = Matrix3D.rotationMatrix( vector.x, vector.y, vector.z, angle );

		this.transform.calculateMultiply3x3( m ,transform );

		this._rotationDirty = true;
	}


	/**
	* Make the object look at a specific position.
	*
	* @param	targetObject	Object to look at.
	* @param	upAxis			The vertical axis of the universe. Normally the positive Y axis.
	*/
	public function lookAt( targetObject:DisplayObject3D, upAxis:Number3D=null ):void
	{
		var position :Number3D = new Number3D( this.x, this.y, this.z );
		var target   :Number3D = new Number3D( targetObject.x, targetObject.y, targetObject.z );

		var zAxis    :Number3D = Number3D.sub( target, position );
		zAxis.normalize();

		if( zAxis.modulo > 0.1 )
		{
			var xAxis :Number3D = Number3D.cross( zAxis, upAxis || UP );
			xAxis.normalize();

			var yAxis :Number3D = Number3D.cross( zAxis, xAxis );
			yAxis.normalize();

			var look  :Matrix3D = this.transform;

			/*
			look.n11 = xAxis.x;
			look.n21 = xAxis.y;
			look.n31 = xAxis.z;

			look.n12 = -yAxis.x;
			look.n22 = -yAxis.y;
			look.n32 = -yAxis.z;

			look.n13 = zAxis.x;
			look.n23 = zAxis.y;
			look.n33 = zAxis.z;
			*/
			
			// scale fix for lookAt()
			look.n11 =  xAxis.x * _scaleX;
			look.n21 =  xAxis.y * _scaleX;
			look.n31 =  xAxis.z * _scaleX;
			
			look.n12 = -yAxis.x * _scaleY;
			look.n22 = -yAxis.y * _scaleY;
			look.n32 = -yAxis.z * _scaleY;
			
			look.n13 =  zAxis.x * _scaleZ;
			look.n23 =  zAxis.y * _scaleZ;
			look.n33 =  zAxis.z * _scaleZ;

			this._transformDirty = false;
			this._rotationDirty = true;
			// TODO: Implement scale
		}
		else
		{
			var log:XrayLog = new XrayLog();
			log.debug( "lookAt Error" );
		}
	}

	// ___________________________________________________________________________________________________
	//                                                                                   T R A N S F O R M
	// TTTTTT RRRRR    AA   NN  NN  SSSSS FFFFFF OOOO  RRRRR  MM   MM
	//   TT   RR  RR  AAAA  NNN NN SS     FF    OO  OO RR  RR MMM MMM
	//   TT   RRRRR  AA  AA NNNNNN  SSSS  FFFF  OO  OO RRRRR  MMMMMMM
	//   TT   RR  RR AAAAAA NN NNN     SS FF    OO  OO RR  RR MM M MM
	//   TT   RR  RR AA  AA NN  NN SSSSS  FF     OOOO  RR  RR MM   MM

	/**
	* Copies the position information (x, y and z coordinates) from another object or Matrix3D.
	*
	* @param	reference	A DisplayObject3D or Matrix3D object to copy the position from.
	*/
	public function copyPosition( reference:* ):void
	{
		var trans  :Matrix3D = this.transform;
		var matrix :Matrix3D = (reference is DisplayObject3D)? reference.transform : reference;

		trans.n14 = matrix.n14;
		trans.n24 = matrix.n24;
		trans.n34 = matrix.n34;
	}

	/**
	* Copies the transformation information (position, rotation and scale) from another object or Matrix3D.
	*
	* @param	reference	A DisplayObject3D or Matrix3D object to copy the position from.
	*/
	public function copyTransform( reference:* ):void
	{
		var trans  :Matrix3D = this.transform;
		var matrix :Matrix3D = (reference is DisplayObject3D)? reference.transform : reference;

		trans.n11 = matrix.n11;		trans.n12 = matrix.n12;
		trans.n13 = matrix.n13;		trans.n14 = matrix.n14;

		trans.n21 = matrix.n21;		trans.n22 = matrix.n22;
		trans.n23 = matrix.n23;		trans.n24 = matrix.n24;

		trans.n31 = matrix.n31;		trans.n32 = matrix.n32;
		trans.n33 = matrix.n33;		trans.n34 = matrix.n34;

		this._transformDirty = false;
		this._rotationDirty  = true;
	}


	/**
	* [internal-use] Updates the transform Matrix3D with the current rotation and scale values.
	*/
	// TODO OPTIMIZE (HIGH)
	protected function updateTransform():void
	{
		var q:Object = Matrix3D.euler2quaternion( -this._rotationY, -this._rotationZ, this._rotationX ); // Swapped

		var m:Matrix3D = Matrix3D.quaternion2matrix( q.x, q.y, q.z, q.w );

		var transform:Matrix3D = this.transform;

		m.n14 = transform.n14;
		m.n24 = transform.n24;
		m.n34 = transform.n34;

		transform.copy( m );

		// Scale
		var scaleM:Matrix3D = Matrix3D.IDENTITY;
		scaleM.n11 = this._scaleX;
		scaleM.n22 = this._scaleY;
		scaleM.n33 = this._scaleZ;

		this.transform.calculateMultiply( transform, scaleM );

		this._transformDirty = false;
	}


	// ___________________________________________________________________________________________________

	/**
	* Returns a string value representing the three-dimensional position values of the display object instance.
	*
	* @return	A string.
	*/
	public override function toString(): String
	{
		return this.name + ': x:' + Math.round(this.x) + ' y:' + Math.round(this.y) + ' z:' + Math.round(this.z);
	}

	// ___________________________________________________________________________________________________
	//                                                                                       P R I V A T E

	/**
	* [internal-use]
	*/
	protected var _transformDirty :Boolean = false;

	private var _rotationX      :Number;
	private var _rotationY      :Number;
	private var _rotationZ      :Number;
	private var _rotationDirty  :Boolean = false;

	private var _scaleX         :Number;
	private var _scaleY         :Number;
	private var _scaleZ         :Number;
	private var _scaleDirty     :Boolean = false;

	protected var _sorted       :Array;

	static private var _totalDisplayObjects :int = 0;

	static private var toDEGREES :Number = 180/Math.PI;
	static private var toRADIANS :Number = Math.PI/180;
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -