Object3D.js 9.5 KB

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