Object3D.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author mikael emtinger / http://gomo.se/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. THREE.Object3D = function () {
  7. this.id = THREE.Object3DIdCount ++;
  8. this.name = '';
  9. this.properties = {};
  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.boundRadius = 0.0;
  27. this.boundRadiusScale = 1.0;
  28. this.visible = true;
  29. this.castShadow = false;
  30. this.receiveShadow = false;
  31. this.frustumCulled = true;
  32. this._vector = new THREE.Vector3();
  33. THREE.Object3DLibrary[ this.id ] = this;
  34. };
  35. THREE.Object3D.prototype = {
  36. constructor: THREE.Object3D,
  37. applyMatrix: function ( matrix ) {
  38. this.matrix.multiply( matrix, this.matrix );
  39. this.scale.getScaleFromMatrix( this.matrix );
  40. var mat = new THREE.Matrix4().extractRotation( this.matrix );
  41. this.rotation.setEulerFromRotationMatrix( mat, this.eulerOrder );
  42. this.position.getPositionFromMatrix( this.matrix );
  43. },
  44. translate: function ( distance, axis ) {
  45. this.matrix.rotateAxis( axis );
  46. this.position.addSelf( axis.multiplyScalar( distance ) );
  47. },
  48. translateX: function ( distance ) {
  49. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  50. },
  51. translateY: function ( distance ) {
  52. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  53. },
  54. translateZ: function ( distance ) {
  55. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  56. },
  57. localToWorld: function ( vector ) {
  58. return this.matrixWorld.multiplyVector3( vector );
  59. },
  60. worldToLocal: function ( vector ) {
  61. return THREE.Object3D.__m1.getInverse( this.matrixWorld ).multiplyVector3( vector );
  62. },
  63. lookAt: function ( vector ) {
  64. // TODO: Add hierarchy support.
  65. this.matrix.lookAt( vector, this.position, this.up );
  66. if ( this.rotationAutoUpdate ) {
  67. if ( this.useQuaternion === false ) {
  68. this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
  69. } else {
  70. this.quaternion.copy( this.matrix.decompose()[ 1 ] );
  71. }
  72. }
  73. },
  74. add: function ( object ) {
  75. if ( object === this ) {
  76. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  77. return;
  78. }
  79. if ( object instanceof THREE.Object3D ) {
  80. if ( object.parent !== undefined ) {
  81. object.parent.remove( object );
  82. }
  83. object.parent = this;
  84. this.children.push( object );
  85. // add to scene
  86. var scene = this;
  87. while ( scene.parent !== undefined ) {
  88. scene = scene.parent;
  89. }
  90. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  91. scene.__addObject( object );
  92. }
  93. }
  94. },
  95. remove: function ( object ) {
  96. var index = this.children.indexOf( object );
  97. if ( index !== - 1 ) {
  98. object.parent = undefined;
  99. this.children.splice( index, 1 );
  100. // remove from scene
  101. var scene = this;
  102. while ( scene.parent !== undefined ) {
  103. scene = scene.parent;
  104. }
  105. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  106. scene.__removeObject( object );
  107. }
  108. }
  109. },
  110. traverse: function ( callback ) {
  111. callback( this );
  112. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  113. this.children[ i ].traverse( callback );
  114. }
  115. },
  116. getChildByName: function ( name, recursive ) {
  117. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  118. var child = this.children[ i ];
  119. if ( child.name === name ) {
  120. return child;
  121. }
  122. if ( recursive === true ) {
  123. child = child.getChildByName( name, recursive );
  124. if ( child !== undefined ) {
  125. return child;
  126. }
  127. }
  128. }
  129. return undefined;
  130. },
  131. getDescendants: function ( array ) {
  132. if ( array === undefined ) array = [];
  133. Array.prototype.push.apply( array, this.children );
  134. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  135. this.children[ i ].getDescendants( array );
  136. }
  137. return array;
  138. },
  139. updateMatrix: function () {
  140. this.matrix.setPosition( this.position );
  141. if ( this.useQuaternion === false ) {
  142. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  143. } else {
  144. this.matrix.setRotationFromQuaternion( this.quaternion );
  145. }
  146. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  147. this.matrix.scale( this.scale );
  148. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  149. }
  150. this.matrixWorldNeedsUpdate = true;
  151. },
  152. updateMatrixWorld: function ( force ) {
  153. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  154. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  155. if ( this.parent === undefined ) {
  156. this.matrixWorld.copy( this.matrix );
  157. } else {
  158. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  159. }
  160. this.matrixWorldNeedsUpdate = false;
  161. force = true;
  162. }
  163. // update children
  164. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  165. this.children[ i ].updateMatrixWorld( force );
  166. }
  167. },
  168. clone: function ( object ) {
  169. if ( object === undefined ) object = new THREE.Object3D();
  170. object.name = this.name;
  171. object.up.copy( this.up );
  172. object.position.copy( this.position );
  173. if ( object.rotation instanceof THREE.Vector3 ) object.rotation.copy( this.rotation ); // because of Sprite madness
  174. object.eulerOrder = this.eulerOrder;
  175. object.scale.copy( this.scale );
  176. object.renderDepth = this.renderDepth;
  177. object.rotationAutoUpdate = this.rotationAutoUpdate;
  178. object.matrix.copy( this.matrix );
  179. object.matrixWorld.copy( this.matrixWorld );
  180. object.matrixRotationWorld.copy( this.matrixRotationWorld );
  181. object.matrixAutoUpdate = this.matrixAutoUpdate;
  182. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  183. object.quaternion.copy( this.quaternion );
  184. object.useQuaternion = this.useQuaternion;
  185. object.boundRadius = this.boundRadius;
  186. object.boundRadiusScale = this.boundRadiusScale;
  187. object.visible = this.visible;
  188. object.castShadow = this.castShadow;
  189. object.receiveShadow = this.receiveShadow;
  190. object.frustumCulled = this.frustumCulled;
  191. for ( var i = 0; i < this.children.length; i ++ ) {
  192. var child = this.children[ i ];
  193. object.add( child.clone() );
  194. }
  195. return object;
  196. },
  197. deallocate: function () {
  198. delete THREE.Object3DLibrary[ this.id ];
  199. }
  200. };
  201. THREE.Object3D.__m1 = new THREE.Matrix4();
  202. THREE.Object3D.defaultEulerOrder = 'XYZ',
  203. THREE.Object3DIdCount = 0;
  204. THREE.Object3DLibrary = {};