📄 displayobject3d.as
字号:
/**
* tells Mesh3D's render() method to sort by measuring from the farthest point of a triangle
*/
public static const MESH_SORT_FAR:uint = 2;
/**
* tells Mesh3D's render() method to sort by measuring from the closest point of a triangle
*/
public static const MESH_SORT_CLOSE:uint = 3;
/**
* tells Mesh3D's render() method to compare the measurement choice of the user for a triangle's sorting
*/
public var meshSort:uint = MESH_SORT_CENTER;
/**
* Returns an empty DiplayObject3D object positioned in the center of the 3D coordinate system (0, 0 ,0).
*/
static public function get ZERO():DisplayObject3D
{
return new DisplayObject3D();
}
/**
* Relative directions.
*/
static private var FORWARD :Number3D = new Number3D( 0, 0, 1 );
static private var BACKWARD :Number3D = new Number3D( 0, 0, -1 );
static private var LEFT :Number3D = new Number3D( -1, 0, 0 );
static private var RIGHT :Number3D = new Number3D( 1, 0, 0 );
static private var UP :Number3D = new Number3D( 0, 1, 0 );
static private var DOWN :Number3D = new Number3D( 0, -1, 0 );
/**
* A Matrix3D object containing values that affect the scaling, rotation, and translation of the display object.
*/
public var transform :Matrix3D;
/**
* [internal-use] A camera transformed Matrix3D object.
*/
public var view :Matrix3D;
/**
* World transformation.
*/
public var world :Matrix3D;
/**
* [internal-use]
*/
public var faces :Array = new Array();
/**
* This allows objects faces to have their own containers.
*/
public static var faceLevelMode :Boolean;
/**
* The GeometryObject3D object that contains the 3D definition of this instance.
* <p/>
* When different objects share the same geometry, they become instances. They are the same object, displayed multiple times. Changing the shape of this object changes the shape of all of its instances.
* <p/>
* Instancing an object saves system memory, and is useful to display an object multiple times while maintaining its shape.
* <p/>
* For example, you could create armies and forests full of duplicate objects without needing the memory to handle that much actual geometry. Each instance has its own transform node so it can have its own position, rotation, and scaling.
*/
public var geometry :GeometryObject3D;
/**
* [internal-use] The average depth of the object faces center. Used internally for z-sorting.
*/
public var screenZ :Number;
// ___________________________________________________________________________________________________
// N E W
// NN NN EEEEEE WW WW
// NNN NN EE WW WW WW
// NNNNNN EEEE WWWWWWWW
// NN NNN EE WWW WWW
// NN NN EEEEEE WW WW
/**
* Creates a new DisplayObject3D instance. After creating the instance, call the addChild() method of a DisplayObjectContainer3D.
*
* @param name [optional] - The name of the newly created object.
* @param geometry [optional] - The geometry of the newly created object.
* @param initObject [optional] - An object that contains user defined properties with which to populate the newly created DisplayObject3D.
*
* <ul>
* <li><b>x</b></b>: An Number that sets the X coordinate of a object relative to the scene coordinate system.</li>
* <p/>
* <li><b>y</b>: An Number that sets the Y coordinate of a object relative to the scene coordinate system.</li>
* <p/>
* <li><b>z</b>: An Number that sets the Z coordinate of a object relative to the scene coordinate system.</li>
* <p/>
* <li><b>rotationX</b>: Specifies the rotation around the X axis from its original orientation.</li>
* <p/>
* <li><b>rotationY</b>: Specifies the rotation around the Y axis from its original orientation.</li>
* <p/>
* <li><b>rotationZ</b>: Specifies the rotation around the Z axis from its original orientation.</li>
* <p/>
* <li><b>scaleX</b>: Sets the scale along the local X axis as applied from the registration point of the object.</li>
* <p/>
* <li><b>scaleY</b>: Sets the scale along the local Y axis as applied from the registration point of the object.</li>
* <p/>
* <li><b>scaleZ</b>: Sets the scale along the local Z axis as applied from the registration point of the object.</li>
* <p/>
* <li><b>visible</b>: Whether or not the display object is visible.
* <p/>
* A Boolean value that indicates whether the object is projected, transformed and rendered. A value of false will effectively ignore the object. The default value is true.</li>
* <p/>
* <li><b>container</b>: The MovieClip that you draw into when rendering. Use only when the object is rendered in its own unique MovieClip.
* <p/>
* It's Boolean value determines whether the container MovieClip should be cleared before rendering.</li>
* <p/>
* <li><b>extra</b>: An object that contains user defined properties.
* <p/>
* All properties of the extra field are copied into the new instance. The properties specified with extra are publicly available.</li>
* </ul>
*/
public function DisplayObject3D( name:String=null, geometry:GeometryObject3D=null, initObject:Object=null ):void
{
super();
Papervision3D.log( "DisplayObject3D: " + name );
this.transform = Matrix3D.IDENTITY;
this.world = Matrix3D.IDENTITY;
this.view = Matrix3D.IDENTITY;
// TODO if( initObject )...
this.x = initObject? initObject.x || 0 : 0;
this.y = initObject? initObject.y || 0 : 0;
this.z = initObject? initObject.z || 0 : 0;
rotationX = initObject? initObject.rotationX || 0 : 0;
rotationY = initObject? initObject.rotationY || 0 : 0;
rotationZ = initObject? initObject.rotationZ || 0 : 0;
var scaleDefault:Number = Papervision3D.usePERCENT? 100 : 1;
scaleX = initObject? initObject.scaleX || scaleDefault : scaleDefault;
scaleY = initObject? initObject.scaleY || scaleDefault : scaleDefault;
scaleZ = initObject? initObject.scaleZ || scaleDefault : scaleDefault;
if( initObject && initObject.extra ) this.extra = initObject.extra;
if( initObject && initObject.container ) this.container = initObject.container;
this.visible = true;
this.id = _totalDisplayObjects++;
this.name = name || String( this.id );
if( geometry ) addGeometry( geometry );
}
// ___________________________________________________________________________________________________
// U T I L S
/**
* Adds a child DisplayObject3D instance to this DisplayObjectContainer instance.
*
* [TODO: If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container.]
*
* @param child The DisplayObject3D instance to add as a child of this DisplayObjectContainer3D instance.
* @param name An optional name of the child to add or create. If no name is provided, the child name will be used.
* @return The DisplayObject3D instance that you have added or created.
*/
public override function addChild( child :DisplayObject3D, name:String=null ):DisplayObject3D
{
child = super.addChild( child, name );
if( child.scene == null ) child.scene = scene;
return child;
}
/**
* Adds a geometry definition to the instance.
*
* A geometry describes the visual shape and appearance of an object in a scene.
*
* @param geometry A geometry definition.
*/
public function addGeometry( geometry:GeometryObject3D=null ):void
{
if( geometry )
this.geometry = geometry;
/*
if( geometry.material )
this.material = geometry.material.clone();
if( geometry.materials )
this.materials = geometry.materials.clone();
*/
}
// ___________________________________________________________________________________________________
// C O L L I S I O N
/**
* Gets the distance to the position of the given object.
*
* @param obj The display object to measure the distance to.
* @return The distance to the registration point of the given object.
*/
public function distanceTo( obj:DisplayObject3D ):Number
{
var x :Number = this.x - obj.x;
var y :Number = this.y - obj.y;
var z :Number = this.z - obj.z;
return Math.sqrt( x*x + y*y + z*z );
}
/**
* Evaluates the display object to see if it overlaps or intersects with the point specified by the x, y and z parameters.
* <p/>
* The x, y and z parameters specify a point in the coordinate space of the instance parent object, not the scene (unless that parent object is the scene).
*
* @param x The x coordinate to test against this object.
* @param y The y coordinate to test against this object.
* @param z The z coordinate to test against this object.
* @return true if the display object overlaps or intersects with the specified point; false otherwise.
*/
public function hitTestPoint( x:Number, y:Number, z:Number ):Boolean
{
var dx :Number = this.x - x;
var dy :Number = this.y - y;
var dz :Number = this.z - z;
var d2 :Number = x*x + y*y + z*z;
var sA :Number = this.geometry? this.geometry.boundingSphere2 : 0;
return sA > d2;
}
/**
* Evaluates the display object to see if it overlaps or intersects with the obj display object.
*
* @param obj The display object to test against.
* @return true if the display objects intersect; false if not.
*/
// TODO: Use group boundingSphere
public function hitTestObject( obj:DisplayObject3D, multiplier:Number=1 ):Boolean
{
var dx :Number = this.x - obj.x;
var dy :Number = this.y - obj.y;
var dz :Number = this.z - obj.z;
var d2 :Number = dx*dx + dy*dy + dz*dz;
var sA :Number = this.geometry? this.geometry.boundingSphere2 : 0;
var sB :Number = obj.geometry? obj.geometry.boundingSphere2 : 0;
sA = sA * multiplier;
return sA + sB > d2;
}
// ___________________________________________________________________________________________________
// M A T E R I A L S
/**
* Returns the material that exists with the specified name in the materials list.
* </p>
* If more that one material object has the specified name, the method returns the first material object in the materials list.
* </p>
* @param name The name of the material to return.
* @return The material object with the specified name.
*/
// TODO: Recursive
public function getMaterialByName( name:String ):MaterialObject3D
{
var material:MaterialObject3D = this.materials.getMaterialByName( name );
if( material )
return material;
else
for each( var child :DisplayObject3D in this._childrenByName )
{
material = child.getMaterialByName( name );
if( material ) return material;
}
return null;
}
/**
* Returns a string value with the list of material names of the materials list.
*
* @return A string.
*/
// TODO: Recursive
public function materialsList():String
{
var list:String = "";
for( var name:String in this.materials )
list += name + "\n";
for each( var child :DisplayObject3D in this._childrenByName )
{
for( name in child.materials.materialsByName )
list += "+ " + name + "\n";
}
return list;
}
// ___________________________________________________________________________________________________
// P R O J E C T
// PPPPP RRRRR OOOO JJ EEEEEE CCCC TTTTTT
// PP PP RR RR OO OO JJ EE CC CC TT
// PPPPP RRRRR OO OO JJ EEEE CC TT
// PP RR RR OO OO JJ JJ EE CC CC TT
// PP RR RR OOOO JJJJ EEEEEE CCCC TT
/**
* [internal-use] Projects three dimensional coordinates onto a two dimensional plane to simulate the relationship of the camera to subject.
* <p/>
* This is the first step in the process of representing three dimensional shapes two dimensionally.
*
* @param parent The DisplayObject3D object that contains this display object.
* @param camera Camera3D object to render from.
* @param sorted The list of faces of the current sort branch.
*/
public function project( parent :DisplayObject3D, camera :CameraObject3D, sorted :Array=null ):Number
{
if( this._transformDirty ) updateTransform();
this.view.calculateMultiply( parent.view, this.transform );
this.world.calculateMultiply( parent.world, this.transform );
calculateScreenCoords( camera );
var screenZs :Number = 0;
var children :Number = 0;
if( ! sorted ) this._sorted = sorted = new Array();
for each( var child:DisplayObject3D in this._childrenByName )
{
if( child.visible )
{
screenZs += child.project( this, camera, sorted );
children++;
}
}
return this.screenZ = screenZs / children;
}
private function calculateScreenCoords( camera :CameraObject3D ):void
{
var persp:Number = (camera.focus * camera.zoom) / (camera.focus + view.n34);
screen.x = view.n14 * persp;
screen.y = view.n24 * persp;
screen.z = view.n34;
}
// ___________________________________________________________________________________________________
// R E N D E R
// RRRRR EEEEEE NN NN DDDDD EEEEEE RRRRR
// RR RR EE NNN NN DD DD EE RR RR
// RRRRR EEEE NNNNNN DD DD EEEE RRRRR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -