Object3D.js 8.8 KB

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