Object3D.js 9.2 KB

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