Object3D.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. translate: function () {
  49. var v1 = new THREE.Vector3();
  50. return function ( distance, axis ) {
  51. // axis is assumed to be normalized
  52. v1.copy( axis );
  53. if ( this.useQuaternion === true ) {
  54. v1.applyQuaternion( this.quaternion );
  55. } else {
  56. v1.applyEuler( this.rotation, this.eulerOrder );
  57. }
  58. v1.multiplyScalar( distance );
  59. this.position.add( v1 );
  60. return this;
  61. };
  62. }(),
  63. translateX: function () {
  64. var v1 = new THREE.Vector3( 1, 0, 0 );
  65. return function ( distance ) {
  66. return this.translate( distance, v1 );
  67. };
  68. }(),
  69. translateY: function () {
  70. var v1 = new THREE.Vector3( 0, 1, 0 );
  71. return function ( distance ) {
  72. return this.translate( distance, v1 );
  73. };
  74. }(),
  75. translateZ: function () {
  76. var v1 = new THREE.Vector3( 0, 0, 1 );
  77. return function ( distance ) {
  78. return this.translate( distance, v1 );
  79. };
  80. }(),
  81. localToWorld: function ( vector ) {
  82. return vector.applyMatrix4( this.matrixWorld );
  83. },
  84. worldToLocal: function () {
  85. var m1 = new THREE.Matrix4();
  86. return function ( vector ) {
  87. return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
  88. };
  89. }(),
  90. lookAt: function () {
  91. // This routine does not support objects with rotated and/or translated parent(s)
  92. var m1 = new THREE.Matrix4();
  93. return function ( vector ) {
  94. m1.lookAt( vector, this.position, this.up );
  95. if ( this.useQuaternion === true ) {
  96. this.quaternion.setFromRotationMatrix( m1 );
  97. } else {
  98. this.rotation.setEulerFromRotationMatrix( m1, this.eulerOrder );
  99. }
  100. };
  101. }(),
  102. add: function ( object ) {
  103. if ( object === this ) {
  104. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  105. return;
  106. }
  107. if ( object instanceof THREE.Object3D ) {
  108. if ( object.parent !== undefined ) {
  109. object.parent.remove( object );
  110. }
  111. object.parent = this;
  112. this.children.push( object );
  113. // add to scene
  114. var scene = this;
  115. while ( scene.parent !== undefined ) {
  116. scene = scene.parent;
  117. }
  118. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  119. scene.__addObject( object );
  120. }
  121. }
  122. },
  123. remove: function ( object ) {
  124. var index = this.children.indexOf( object );
  125. if ( index !== - 1 ) {
  126. object.parent = undefined;
  127. this.children.splice( index, 1 );
  128. // remove from scene
  129. var scene = this;
  130. while ( scene.parent !== undefined ) {
  131. scene = scene.parent;
  132. }
  133. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  134. scene.__removeObject( object );
  135. }
  136. }
  137. },
  138. traverse: function ( callback ) {
  139. callback( this );
  140. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  141. this.children[ i ].traverse( callback );
  142. }
  143. },
  144. getChildByName: function ( name, recursive ) {
  145. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  146. var child = this.children[ i ];
  147. if ( child.name === name ) {
  148. return child;
  149. }
  150. if ( recursive === true ) {
  151. child = child.getChildByName( name, recursive );
  152. if ( child !== undefined ) {
  153. return child;
  154. }
  155. }
  156. }
  157. return undefined;
  158. },
  159. getDescendants: function ( array ) {
  160. if ( array === undefined ) array = [];
  161. Array.prototype.push.apply( array, this.children );
  162. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  163. this.children[ i ].getDescendants( array );
  164. }
  165. return array;
  166. },
  167. updateMatrix: function () {
  168. // if we are not using a quaternion directly, convert Euler rotation to this.quaternion.
  169. if ( this.useQuaternion === false ) {
  170. this.quaternion.setFromEuler( this.rotation, this.eulerOrder );
  171. }
  172. this.matrix.compose( this.position, this.quaternion, this.scale );
  173. this.matrixWorldNeedsUpdate = true;
  174. },
  175. updateMatrixWorld: function ( force ) {
  176. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  177. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  178. if ( this.parent === undefined ) {
  179. this.matrixWorld.copy( this.matrix );
  180. } else {
  181. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  182. }
  183. this.matrixWorldNeedsUpdate = false;
  184. force = true;
  185. }
  186. // update children
  187. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  188. this.children[ i ].updateMatrixWorld( force );
  189. }
  190. },
  191. clone: function ( object ) {
  192. if ( object === undefined ) object = new THREE.Object3D();
  193. object.name = this.name;
  194. object.up.copy( this.up );
  195. object.position.copy( this.position );
  196. if ( object.rotation instanceof THREE.Vector3 ) object.rotation.copy( this.rotation ); // because of Sprite madness
  197. object.eulerOrder = this.eulerOrder;
  198. object.scale.copy( this.scale );
  199. object.renderDepth = this.renderDepth;
  200. object.rotationAutoUpdate = this.rotationAutoUpdate;
  201. object.matrix.copy( this.matrix );
  202. object.matrixWorld.copy( this.matrixWorld );
  203. object.matrixRotationWorld.copy( this.matrixRotationWorld );
  204. object.matrixAutoUpdate = this.matrixAutoUpdate;
  205. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  206. object.quaternion.copy( this.quaternion );
  207. object.useQuaternion = this.useQuaternion;
  208. object.visible = this.visible;
  209. object.castShadow = this.castShadow;
  210. object.receiveShadow = this.receiveShadow;
  211. object.frustumCulled = this.frustumCulled;
  212. for ( var i = 0; i < this.children.length; i ++ ) {
  213. var child = this.children[ i ];
  214. object.add( child.clone() );
  215. }
  216. return object;
  217. }
  218. };
  219. THREE.Object3D.defaultEulerOrder = 'XYZ',
  220. THREE.Object3DIdCount = 0;