123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author mikael emtinger / http://gomo.se/
- * @author alteredq / http://alteredqualia.com/
- */
- THREE.Object3D = function () {
- this.id = THREE.Object3DLib.length;
- THREE.Object3DLib.push( this );
- this.name = '';
- this.properties = {};
- this.parent = undefined;
- this.children = [];
- this.up = new THREE.Vector3( 0, 1, 0 );
- this.position = new THREE.Vector3();
- this.rotation = new THREE.Vector3();
- this.eulerOrder = THREE.Object3D.defaultEulerOrder;
- this.scale = new THREE.Vector3( 1, 1, 1 );
- this.renderDepth = null;
- this.rotationAutoUpdate = true;
- this.matrix = new THREE.Matrix4();
- this.matrixWorld = new THREE.Matrix4();
- this.matrixRotationWorld = new THREE.Matrix4();
- this.matrixAutoUpdate = true;
- this.matrixWorldNeedsUpdate = true;
- this.quaternion = new THREE.Quaternion();
- this.useQuaternion = false;
- this.boundRadius = 0.0;
- this.boundRadiusScale = 1.0;
- this.visible = true;
- this.castShadow = false;
- this.receiveShadow = false;
- this.frustumCulled = true;
- this._vector = new THREE.Vector3();
- };
- THREE.Object3D.prototype = {
- constructor: THREE.Object3D,
- applyMatrix: function ( matrix ) {
- this.matrix.multiply( matrix, this.matrix );
- this.scale.getScaleFromMatrix( this.matrix );
- var mat = new THREE.Matrix4().extractRotation( this.matrix );
- this.rotation.setEulerFromRotationMatrix( mat, this.eulerOrder );
- this.position.getPositionFromMatrix( this.matrix );
- },
- translate: function ( distance, axis ) {
- this.matrix.rotateAxis( axis );
- this.position.addSelf( axis.multiplyScalar( distance ) );
- },
- translateX: function ( distance ) {
- this.translate( distance, this._vector.set( 1, 0, 0 ) );
- },
- translateY: function ( distance ) {
- this.translate( distance, this._vector.set( 0, 1, 0 ) );
- },
- translateZ: function ( distance ) {
- this.translate( distance, this._vector.set( 0, 0, 1 ) );
- },
- localToWorld: function ( vector ) {
- return this.matrixWorld.multiplyVector3( vector );
- },
- worldToLocal: function ( vector ) {
- return THREE.Object3D.__m1.getInverse( this.matrixWorld ).multiplyVector3( vector );
- },
- lookAt: function ( vector ) {
- // TODO: Add hierarchy support.
- this.matrix.lookAt( vector, this.position, this.up );
- if ( this.rotationAutoUpdate ) {
- this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
- }
- },
- add: function ( object ) {
- if ( object === this ) {
- console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
- return;
- }
- if ( object instanceof THREE.Object3D ) {
- if ( object.parent !== undefined ) {
- object.parent.remove( object );
- }
- object.parent = this;
- this.children.push( object );
- // add to scene
- var scene = this;
- while ( scene.parent !== undefined ) {
- scene = scene.parent;
- }
- if ( scene !== undefined && scene instanceof THREE.Scene ) {
- scene.__addObject( object );
- }
- }
- },
- remove: function ( object ) {
- var index = this.children.indexOf( object );
- if ( index !== - 1 ) {
- object.parent = undefined;
- this.children.splice( index, 1 );
- // remove from scene
- var scene = this;
- while ( scene.parent !== undefined ) {
- scene = scene.parent;
- }
- if ( scene !== undefined && scene instanceof THREE.Scene ) {
- scene.__removeObject( object );
- }
- }
- },
- traverse: function ( callback ) {
- callback( this );
- for ( var i = 0, l = this.children.length; i < l; i ++ ) {
- this.children[ i ].traverse( callback );
- }
- },
- getChildByName: function ( name, recursive ) {
- for ( var i = 0, l = this.children.length; i < l; i ++ ) {
- var child = this.children[ i ];
- if ( child.name === name ) {
- return child;
- }
- if ( recursive === true ) {
- child = child.getChildByName( name, recursive );
- if ( child !== undefined ) {
- return child;
- }
- }
- }
- return undefined;
- },
- getDescendants: function ( array ) {
- if ( array === undefined ) array = [];
- Array.prototype.push.apply( array, this.children );
- for ( var i = 0, l = this.children.length; i < l; i ++ ) {
- this.children[ i ].getDescendants( array );
- }
- return array;
- },
- updateMatrix: function () {
- this.matrix.setPosition( this.position );
- if ( this.useQuaternion === false ) {
- this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
- } else {
- this.matrix.setRotationFromQuaternion( this.quaternion );
- }
- if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
- this.matrix.scale( this.scale );
- this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
- }
- this.matrixWorldNeedsUpdate = true;
- },
- updateMatrixWorld: function ( force ) {
- if ( this.matrixAutoUpdate === true ) this.updateMatrix();
- if ( this.matrixWorldNeedsUpdate === true || force === true ) {
- if ( this.parent === undefined ) {
- this.matrixWorld.copy( this.matrix );
- } else {
- this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
- }
- this.matrixWorldNeedsUpdate = false;
- force = true;
- }
- // update children
- for ( var i = 0, l = this.children.length; i < l; i ++ ) {
- this.children[ i ].updateMatrixWorld( force );
- }
- },
- clone: function ( object ) {
- if ( object === undefined ) object = new THREE.Object3D();
- object.name = this.name;
- object.up.copy( this.up );
- object.position.copy( this.position );
- if ( object.rotation instanceof THREE.Vector3 ) object.rotation.copy( this.rotation ); // because of Sprite madness
- object.eulerOrder = this.eulerOrder;
- object.scale.copy( this.scale );
- object.renderDepth = this.renderDepth;
- object.rotationAutoUpdate = this.rotationAutoUpdate;
- object.matrix.copy( this.matrix );
- object.matrixWorld.copy( this.matrixWorld );
- object.matrixRotationWorld.copy( this.matrixRotationWorld );
- object.matrixAutoUpdate = this.matrixAutoUpdate;
- object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
- object.quaternion.copy( this.quaternion );
- object.useQuaternion = this.useQuaternion;
- object.boundRadius = this.boundRadius;
- object.boundRadiusScale = this.boundRadiusScale;
- object.visible = this.visible;
- object.castShadow = this.castShadow;
- object.receiveShadow = this.receiveShadow;
- object.frustumCulled = this.frustumCulled;
- return object;
- },
- deallocate: function () {
- THREE.Object3DLib[ this.id ] = null;
- }
- };
- THREE.Object3D.__m1 = new THREE.Matrix4();
- THREE.Object3D.defaultEulerOrder = 'XYZ',
- THREE.Object3DLib = [];
|