Object3D.js 7.9 KB

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