Object3D.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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.Object3DLib.length;
  8. THREE.Object3DLib.push( this );
  9. this.name = '';
  10. this.properties = {};
  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.Vector3();
  16. this.eulerOrder = THREE.Object3D.defaultEulerOrder;
  17. this.scale = new THREE.Vector3( 1, 1, 1 );
  18. this.renderDepth = null;
  19. this.rotationAutoUpdate = true;
  20. this.matrix = new THREE.Matrix4();
  21. this.matrixWorld = new THREE.Matrix4();
  22. this.matrixRotationWorld = new THREE.Matrix4();
  23. this.matrixAutoUpdate = true;
  24. this.matrixWorldNeedsUpdate = true;
  25. this.quaternion = new THREE.Quaternion();
  26. this.useQuaternion = false;
  27. this.boundRadius = 0.0;
  28. this.boundRadiusScale = 1.0;
  29. this.visible = true;
  30. this.castShadow = false;
  31. this.receiveShadow = false;
  32. this.frustumCulled = true;
  33. this._vector = new THREE.Vector3();
  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. this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
  68. }
  69. },
  70. add: function ( object ) {
  71. if ( object === this ) {
  72. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  73. return;
  74. }
  75. if ( object instanceof THREE.Object3D ) {
  76. if ( object.parent !== undefined ) {
  77. object.parent.remove( object );
  78. }
  79. object.parent = this;
  80. this.children.push( object );
  81. // add to scene
  82. var scene = this;
  83. while ( scene.parent !== undefined ) {
  84. scene = scene.parent;
  85. }
  86. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  87. scene.__addObject( object );
  88. }
  89. }
  90. },
  91. remove: function ( object ) {
  92. var index = this.children.indexOf( object );
  93. if ( index !== - 1 ) {
  94. object.parent = undefined;
  95. this.children.splice( index, 1 );
  96. // remove from scene
  97. var scene = this;
  98. while ( scene.parent !== undefined ) {
  99. scene = scene.parent;
  100. }
  101. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  102. scene.__removeObject( object );
  103. }
  104. }
  105. },
  106. traverse: function ( callback ) {
  107. callback( this );
  108. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  109. this.children[ i ].traverse( callback );
  110. }
  111. },
  112. getChildByName: function ( name, recursive ) {
  113. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  114. var child = this.children[ i ];
  115. if ( child.name === name ) {
  116. return child;
  117. }
  118. if ( recursive === true ) {
  119. child = child.getChildByName( name, recursive );
  120. if ( child !== undefined ) {
  121. return child;
  122. }
  123. }
  124. }
  125. return undefined;
  126. },
  127. getDescendants: function ( array ) {
  128. if ( array === undefined ) array = [];
  129. Array.prototype.push.apply( array, this.children );
  130. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  131. this.children[ i ].getDescendants( array );
  132. }
  133. return array;
  134. },
  135. updateMatrix: function () {
  136. this.matrix.setPosition( this.position );
  137. if ( this.useQuaternion === false ) {
  138. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  139. } else {
  140. this.matrix.setRotationFromQuaternion( this.quaternion );
  141. }
  142. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  143. this.matrix.scale( this.scale );
  144. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  145. }
  146. this.matrixWorldNeedsUpdate = true;
  147. },
  148. updateMatrixWorld: function ( force ) {
  149. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  150. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  151. if ( this.parent === undefined ) {
  152. this.matrixWorld.copy( this.matrix );
  153. } else {
  154. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  155. }
  156. this.matrixWorldNeedsUpdate = false;
  157. force = true;
  158. }
  159. // update children
  160. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  161. this.children[ i ].updateMatrixWorld( force );
  162. }
  163. },
  164. clone: function ( object ) {
  165. if ( object === undefined ) object = new THREE.Object3D();
  166. object.name = this.name;
  167. object.up.copy( this.up );
  168. object.position.copy( this.position );
  169. if ( object.rotation instanceof THREE.Vector3 ) object.rotation.copy( this.rotation ); // because of Sprite madness
  170. object.eulerOrder = this.eulerOrder;
  171. object.scale.copy( this.scale );
  172. object.renderDepth = this.renderDepth;
  173. object.rotationAutoUpdate = this.rotationAutoUpdate;
  174. object.matrix.copy( this.matrix );
  175. object.matrixWorld.copy( this.matrixWorld );
  176. object.matrixRotationWorld.copy( this.matrixRotationWorld );
  177. object.matrixAutoUpdate = this.matrixAutoUpdate;
  178. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  179. object.quaternion.copy( this.quaternion );
  180. object.useQuaternion = this.useQuaternion;
  181. object.boundRadius = this.boundRadius;
  182. object.boundRadiusScale = this.boundRadiusScale;
  183. object.visible = this.visible;
  184. object.castShadow = this.castShadow;
  185. object.receiveShadow = this.receiveShadow;
  186. object.frustumCulled = this.frustumCulled;
  187. return object;
  188. },
  189. deallocate: function () {
  190. THREE.Object3DLib[ this.id ] = null;
  191. }
  192. };
  193. THREE.Object3D.__m1 = new THREE.Matrix4();
  194. THREE.Object3D.defaultEulerOrder = 'XYZ',
  195. THREE.Object3DLib = [];