Object3D.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author mikael emtinger / http://gomo.se/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. */
  7. THREE.Object3D = function () {
  8. this.id = THREE.Math.generateUUID();
  9. this.name = '';
  10. this.parent = undefined;
  11. this.children = [];
  12. this.up = new THREE.Vector3( 0, 1, 0 );
  13. this.position = new THREE.Vector3();
  14. this.quaternion = new THREE.Quaternion();
  15. this.scale = new THREE.Vector3( 1, 1, 1 );
  16. // for backwards compatibility (maps changes to this.rotation onto this.quaternion)
  17. this.rotation = new THREE.Rotation( this.quaternion );
  18. this.renderDepth = null;
  19. this.rotationAutoUpdate = true;
  20. this.matrix = new THREE.Matrix4();
  21. this.matrixWorld = new THREE.Matrix4();
  22. this.matrixAutoUpdate = true;
  23. this.matrixWorldNeedsUpdate = true;
  24. this.visible = true;
  25. this.castShadow = false;
  26. this.receiveShadow = false;
  27. this.frustumCulled = true;
  28. this.userData = {};
  29. };
  30. THREE.Object3D.prototype = {
  31. constructor: THREE.Object3D,
  32. addEventListener: THREE.EventDispatcher.prototype.addEventListener,
  33. hasEventListener: THREE.EventDispatcher.prototype.hasEventListener,
  34. removeEventListener: THREE.EventDispatcher.prototype.removeEventListener,
  35. dispatchEvent: THREE.EventDispatcher.prototype.dispatchEvent,
  36. applyMatrix: function () {
  37. var m1 = new THREE.Matrix4();
  38. return function ( matrix ) {
  39. this.matrix.multiplyMatrices( matrix, this.matrix );
  40. this.position.getPositionFromMatrix( this.matrix );
  41. this.scale.getScaleFromMatrix( this.matrix );
  42. m1.extractRotation( this.matrix );
  43. this.quaternion.setFromRotationMatrix( m1 );
  44. }
  45. }(),
  46. rotateOnAxis: function() {
  47. // rotate object on axis in object space
  48. // axis is assumed to be normalized
  49. var q1 = new THREE.Quaternion();
  50. var q2 = new THREE.Quaternion();
  51. return function ( axis, angle ) {
  52. q1.setFromAxisAngle( axis, angle );
  53. this.quaternion.multiply( q1 );
  54. return this;
  55. }
  56. }(),
  57. translateOnAxis: function () {
  58. // translate object by distance along axis in object space
  59. // axis is assumed to be normalized
  60. var v1 = new THREE.Vector3();
  61. return function ( axis, distance ) {
  62. v1.copy( axis );
  63. v1.applyQuaternion( this.quaternion );
  64. this.position.add( v1.multiplyScalar( distance ) );
  65. return this;
  66. }
  67. }(),
  68. translate: function ( distance, axis ) {
  69. console.warn( 'DEPRECATED: Object3D\'s .translate() has been removed. Use .translateOnAxis( axis, distance ) instead. Note args have been changed.' );
  70. return this.translateOnAxis( axis, distance );
  71. },
  72. translateX: function () {
  73. var v1 = new THREE.Vector3( 1, 0, 0 );
  74. return function ( distance ) {
  75. return this.translateOnAxis( v1, distance );
  76. };
  77. }(),
  78. translateY: function () {
  79. var v1 = new THREE.Vector3( 0, 1, 0 );
  80. return function ( distance ) {
  81. return this.translateOnAxis( v1, distance );
  82. };
  83. }(),
  84. translateZ: function () {
  85. var v1 = new THREE.Vector3( 0, 0, 1 );
  86. return function ( distance ) {
  87. return this.translateOnAxis( v1, distance );
  88. };
  89. }(),
  90. localToWorld: function ( vector ) {
  91. return vector.applyMatrix4( this.matrixWorld );
  92. },
  93. worldToLocal: function () {
  94. var m1 = new THREE.Matrix4();
  95. return function ( vector ) {
  96. return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
  97. };
  98. }(),
  99. lookAt: function () {
  100. // This routine does not support objects with rotated and/or translated parent(s)
  101. var m1 = new THREE.Matrix4();
  102. return function ( vector ) {
  103. m1.lookAt( vector, this.position, this.up );
  104. this.quaternion.setFromRotationMatrix( m1 );
  105. };
  106. }(),
  107. add: function ( object ) {
  108. if ( object === this ) {
  109. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  110. return;
  111. }
  112. if ( object instanceof THREE.Object3D ) {
  113. if ( object.parent !== undefined ) {
  114. object.parent.remove( object );
  115. }
  116. object.parent = this;
  117. this.children.push( object );
  118. // add to scene
  119. var scene = this;
  120. while ( scene.parent !== undefined ) {
  121. scene = scene.parent;
  122. }
  123. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  124. scene.__addObject( object );
  125. }
  126. }
  127. },
  128. remove: function ( object ) {
  129. var index = this.children.indexOf( object );
  130. if ( index !== - 1 ) {
  131. object.parent = undefined;
  132. this.children.splice( index, 1 );
  133. // remove from scene
  134. var scene = this;
  135. while ( scene.parent !== undefined ) {
  136. scene = scene.parent;
  137. }
  138. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  139. scene.__removeObject( object );
  140. }
  141. }
  142. },
  143. traverse: function ( callback ) {
  144. callback( this );
  145. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  146. this.children[ i ].traverse( callback );
  147. }
  148. },
  149. getObjectById: function ( id, recursive ) {
  150. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  151. var child = this.children[ i ];
  152. if ( child.id === id ) {
  153. return child;
  154. }
  155. if ( recursive === true ) {
  156. child = child.getObjectById( id, recursive );
  157. if ( child !== undefined ) {
  158. return child;
  159. }
  160. }
  161. }
  162. return undefined;
  163. },
  164. getObjectByName: function ( name, recursive ) {
  165. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  166. var child = this.children[ i ];
  167. if ( child.name === name ) {
  168. return child;
  169. }
  170. if ( recursive === true ) {
  171. child = child.getObjectByName( name, recursive );
  172. if ( child !== undefined ) {
  173. return child;
  174. }
  175. }
  176. }
  177. return undefined;
  178. },
  179. getChildByName: function ( name, recursive ) {
  180. console.warn( 'DEPRECATED: Object3D\'s .getChildByName() has been renamed to .getObjectByName().' );
  181. return this.getObjectByName( name, recursive );
  182. },
  183. getDescendants: function ( array ) {
  184. if ( array === undefined ) array = [];
  185. Array.prototype.push.apply( array, this.children );
  186. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  187. this.children[ i ].getDescendants( array );
  188. }
  189. return array;
  190. },
  191. updateMatrix: function () {
  192. this.matrix.compose( this.position, this.quaternion, this.scale );
  193. this.matrixWorldNeedsUpdate = true;
  194. },
  195. updateMatrixWorld: function ( force ) {
  196. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  197. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  198. if ( this.parent === undefined ) {
  199. this.matrixWorld.copy( this.matrix );
  200. } else {
  201. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  202. }
  203. this.matrixWorldNeedsUpdate = false;
  204. force = true;
  205. }
  206. // update children
  207. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  208. this.children[ i ].updateMatrixWorld( force );
  209. }
  210. },
  211. clone: function ( object, recursive ) {
  212. if ( object === undefined ) object = new THREE.Object3D();
  213. if ( recursive === undefined ) recursive = true;
  214. object.name = this.name;
  215. object.up.copy( this.up );
  216. object.position.copy( this.position );
  217. object.quaternion.copy( this.quaternion );
  218. object.scale.copy( this.scale );
  219. object.renderDepth = this.renderDepth;
  220. object.rotationAutoUpdate = this.rotationAutoUpdate;
  221. object.matrix.copy( this.matrix );
  222. object.matrixWorld.copy( this.matrixWorld );
  223. object.matrixAutoUpdate = this.matrixAutoUpdate;
  224. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  225. object.visible = this.visible;
  226. object.castShadow = this.castShadow;
  227. object.receiveShadow = this.receiveShadow;
  228. object.frustumCulled = this.frustumCulled;
  229. object.userData = JSON.parse( JSON.stringify( this.userData ) );
  230. if ( recursive === true ) {
  231. for ( var i = 0; i < this.children.length; i ++ ) {
  232. var child = this.children[ i ];
  233. object.add( child.clone() );
  234. }
  235. }
  236. return object;
  237. }
  238. };
  239. THREE.Object3D.defaultEulerOrder = 'XYZ';