Object3D.js 10 KB

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