Object3D.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. * @author mikael emtinger / http://gomo.se/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. THREE.Object3D = function() {
  7. this.name = '';
  8. this.id = THREE.Object3DCount ++;
  9. this.parent = undefined;
  10. this.children = [];
  11. this.up = new THREE.Vector3( 0, 1, 0 );
  12. this.position = new THREE.Vector3();
  13. this.rotation = new THREE.Vector3();
  14. this.eulerOrder = 'XYZ';
  15. this.scale = new THREE.Vector3( 1, 1, 1 );
  16. this.dynamic = false; // when true it retains arrays so they can be updated with __dirty*
  17. this.doubleSided = false;
  18. this.flipSided = false;
  19. this.renderDepth = null;
  20. this.rotationAutoUpdate = true;
  21. this.matrix = new THREE.Matrix4();
  22. this.matrixWorld = new THREE.Matrix4();
  23. this.matrixRotationWorld = new THREE.Matrix4();
  24. this.matrixAutoUpdate = true;
  25. this.matrixWorldNeedsUpdate = true;
  26. this.quaternion = new THREE.Quaternion();
  27. this.useQuaternion = false;
  28. this.boundRadius = 0.0;
  29. this.boundRadiusScale = 1.0;
  30. this.visible = true;
  31. this.castShadow = false;
  32. this.receiveShadow = false;
  33. this.frustumCulled = true;
  34. this._vector = new THREE.Vector3();
  35. };
  36. THREE.Object3D.prototype = {
  37. constructor: THREE.Object3D,
  38. translate: function ( distance, axis ) {
  39. this.matrix.rotateAxis( axis );
  40. this.position.addSelf( axis.multiplyScalar( distance ) );
  41. },
  42. translateX: function ( distance ) {
  43. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  44. },
  45. translateY: function ( distance ) {
  46. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  47. },
  48. translateZ: function ( distance ) {
  49. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  50. },
  51. lookAt: function ( vector ) {
  52. // TODO: Add hierarchy support.
  53. if ( this instanceof THREE.Camera ) {
  54. this.matrix.lookAt( this.position, vector, this.up );
  55. } else {
  56. this.matrix.lookAt( vector, this.position, this.up );
  57. }
  58. if ( this.rotationAutoUpdate ) {
  59. this.rotation.setRotationFromMatrix( this.matrix );
  60. }
  61. },
  62. add: function ( object ) {
  63. if ( this.children.indexOf( object ) === - 1 ) {
  64. if( object.parent !== undefined ) {
  65. object.parent.removeChild( object );
  66. }
  67. object.parent = this;
  68. this.children.push( object );
  69. // add to scene
  70. var scene = this;
  71. while ( scene.parent !== undefined ) {
  72. scene = scene.parent;
  73. }
  74. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  75. scene.addChildRecurse( object );
  76. }
  77. }
  78. },
  79. remove: function ( object ) {
  80. var scene = this;
  81. var childIndex = this.children.indexOf( object );
  82. if ( childIndex !== - 1 ) {
  83. object.parent = undefined;
  84. this.children.splice( childIndex, 1 );
  85. // remove from scene
  86. while ( scene.parent !== undefined ) {
  87. scene = scene.parent;
  88. }
  89. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  90. scene.removeChildRecurse( object );
  91. }
  92. }
  93. },
  94. getChildByName: function ( name, doRecurse ) {
  95. var c, cl, child, recurseResult;
  96. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  97. child = this.children[ c ];
  98. if ( child.name === name ) {
  99. return child;
  100. }
  101. if ( doRecurse ) {
  102. recurseResult = child.getChildByName( name, doRecurse );
  103. if ( recurseResult !== undefined ) {
  104. return recurseResult;
  105. }
  106. }
  107. }
  108. return undefined;
  109. },
  110. updateMatrix: function () {
  111. this.matrix.setPosition( this.position );
  112. if ( this.useQuaternion ) {
  113. this.matrix.setRotationFromQuaternion( this.quaternion );
  114. } else {
  115. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  116. }
  117. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  118. this.matrix.scale( this.scale );
  119. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  120. }
  121. this.matrixWorldNeedsUpdate = true;
  122. },
  123. update: function ( parentMatrixWorld, forceUpdate, camera ) {
  124. this.matrixAutoUpdate && this.updateMatrix();
  125. // update matrixWorld
  126. if ( this.matrixWorldNeedsUpdate || forceUpdate ) {
  127. if ( parentMatrixWorld ) {
  128. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  129. } else {
  130. this.matrixWorld.copy( this.matrix );
  131. }
  132. this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
  133. this.matrixWorldNeedsUpdate = false;
  134. forceUpdate = true;
  135. }
  136. // update children
  137. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  138. this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
  139. }
  140. },
  141. // DEPRECATED
  142. addChild: function ( child ) {
  143. console.warn( 'DEPRECATED: Object3D.addChild() is now Object3D.add().' );
  144. this.add( child );
  145. },
  146. removeChild: function ( child ) {
  147. console.warn( 'DEPRECATED: Object3D.removeChild() is now Object3D.remove().' );
  148. this.remove( child );
  149. }
  150. };
  151. THREE.Object3DCount = 0;