Object3D.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author mikael emtinger / http://gomo.se/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. THREE.Object3D = function () {
  7. this.id = THREE.Object3DIdCount ++;
  8. this.name = '';
  9. this.properties = {};
  10. this.parent = undefined;
  11. this.children = [];
  12. this.up = new THREE.Vector3( 0, 1, 0 );
  13. this.position = new THREE.Vector3();
  14. this.rotation = new THREE.Vector3();
  15. this.eulerOrder = THREE.Object3D.defaultEulerOrder;
  16. this.scale = new THREE.Vector3( 1, 1, 1 );
  17. this.renderDepth = null;
  18. this.rotationAutoUpdate = true;
  19. this.matrix = new THREE.Matrix4();
  20. this.matrixWorld = new THREE.Matrix4();
  21. this.matrixRotationWorld = new THREE.Matrix4();
  22. this.matrixAutoUpdate = true;
  23. this.matrixWorldNeedsUpdate = true;
  24. this.quaternion = new THREE.Quaternion();
  25. this.useQuaternion = false;
  26. this.visible = true;
  27. this.castShadow = false;
  28. this.receiveShadow = false;
  29. this.frustumCulled = true;
  30. this._vector = new THREE.Vector3();
  31. };
  32. THREE.Object3D.prototype = {
  33. constructor: THREE.Object3D,
  34. applyMatrix: function ( matrix ) {
  35. this.matrix.multiply( matrix, this.matrix );
  36. this.scale.getScaleFromMatrix( this.matrix );
  37. var mat = new THREE.Matrix4().extractRotation( this.matrix );
  38. this.rotation.setEulerFromRotationMatrix( mat, this.eulerOrder );
  39. this.position.getPositionFromMatrix( this.matrix );
  40. },
  41. translate: function ( distance, axis ) {
  42. this.matrix.rotateAxis( axis );
  43. this.position.addSelf( axis.multiplyScalar( distance ) );
  44. },
  45. translateX: function ( distance ) {
  46. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  47. },
  48. translateY: function ( distance ) {
  49. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  50. },
  51. translateZ: function ( distance ) {
  52. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  53. },
  54. localToWorld: function ( vector ) {
  55. return vector.applyMatrix4( this.matrixWorld );
  56. },
  57. worldToLocal: function ( vector ) {
  58. return vector.applyMatrix4( THREE.Object3D.__m1.getInverse( this.matrixWorld ) );
  59. },
  60. lookAt: function ( vector ) {
  61. // TODO: Add hierarchy support.
  62. this.matrix.lookAt( vector, this.position, this.up );
  63. if ( this.rotationAutoUpdate ) {
  64. if ( this.useQuaternion === false ) {
  65. this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
  66. } else {
  67. this.quaternion.copy( this.matrix.decompose()[ 1 ] );
  68. }
  69. }
  70. },
  71. add: function ( object ) {
  72. if ( object === this ) {
  73. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  74. return;
  75. }
  76. if ( object instanceof THREE.Object3D ) {
  77. if ( object.parent !== undefined ) {
  78. object.parent.remove( object );
  79. }
  80. object.parent = this;
  81. this.children.push( object );
  82. // add to scene
  83. var scene = this;
  84. while ( scene.parent !== undefined ) {
  85. scene = scene.parent;
  86. }
  87. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  88. scene.__addObject( object );
  89. }
  90. }
  91. },
  92. remove: function ( object ) {
  93. var index = this.children.indexOf( object );
  94. if ( index !== - 1 ) {
  95. object.parent = undefined;
  96. this.children.splice( index, 1 );
  97. // remove from scene
  98. var scene = this;
  99. while ( scene.parent !== undefined ) {
  100. scene = scene.parent;
  101. }
  102. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  103. scene.__removeObject( object );
  104. }
  105. }
  106. },
  107. traverse: function ( callback ) {
  108. callback( this );
  109. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  110. this.children[ i ].traverse( callback );
  111. }
  112. },
  113. getChildByName: function ( name, recursive ) {
  114. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  115. var child = this.children[ i ];
  116. if ( child.name === name ) {
  117. return child;
  118. }
  119. if ( recursive === true ) {
  120. child = child.getChildByName( name, recursive );
  121. if ( child !== undefined ) {
  122. return child;
  123. }
  124. }
  125. }
  126. return undefined;
  127. },
  128. getDescendants: function ( array ) {
  129. if ( array === undefined ) array = [];
  130. Array.prototype.push.apply( array, this.children );
  131. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  132. this.children[ i ].getDescendants( array );
  133. }
  134. return array;
  135. },
  136. updateMatrix: function () {
  137. this.matrix.setPosition( this.position );
  138. if ( this.useQuaternion === false ) {
  139. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  140. } else {
  141. this.matrix.setRotationFromQuaternion( this.quaternion );
  142. }
  143. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  144. this.matrix.scale( this.scale );
  145. }
  146. this.matrixWorldNeedsUpdate = true;
  147. },
  148. updateMatrixWorld: function ( force ) {
  149. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  150. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  151. if ( this.parent === undefined ) {
  152. this.matrixWorld.copy( this.matrix );
  153. } else {
  154. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  155. }
  156. this.matrixWorldNeedsUpdate = false;
  157. force = true;
  158. }
  159. // update children
  160. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  161. this.children[ i ].updateMatrixWorld( force );
  162. }
  163. },
  164. clone: function ( object ) {
  165. if ( object === undefined ) object = new THREE.Object3D();
  166. object.name = this.name;
  167. object.up.copy( this.up );
  168. object.position.copy( this.position );
  169. if ( object.rotation instanceof THREE.Vector3 ) object.rotation.copy( this.rotation ); // because of Sprite madness
  170. object.eulerOrder = this.eulerOrder;
  171. object.scale.copy( this.scale );
  172. object.renderDepth = this.renderDepth;
  173. object.rotationAutoUpdate = this.rotationAutoUpdate;
  174. object.matrix.copy( this.matrix );
  175. object.matrixWorld.copy( this.matrixWorld );
  176. object.matrixRotationWorld.copy( this.matrixRotationWorld );
  177. object.matrixAutoUpdate = this.matrixAutoUpdate;
  178. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  179. object.quaternion.copy( this.quaternion );
  180. object.useQuaternion = this.useQuaternion;
  181. object.visible = this.visible;
  182. object.castShadow = this.castShadow;
  183. object.receiveShadow = this.receiveShadow;
  184. object.frustumCulled = this.frustumCulled;
  185. for ( var i = 0; i < this.children.length; i ++ ) {
  186. var child = this.children[ i ];
  187. object.add( child.clone() );
  188. }
  189. return object;
  190. }
  191. };
  192. THREE.Object3D.__m1 = new THREE.Matrix4();
  193. THREE.Object3D.defaultEulerOrder = 'XYZ',
  194. THREE.Object3DIdCount = 0;