Object3D.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.id = THREE.Object3DCount ++;
  8. this.name = "";
  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. this.matrix.lookAt( vector, this.position, this.up );
  54. if ( this.rotationAutoUpdate ) {
  55. this.rotation.setRotationFromMatrix( this.matrix );
  56. }
  57. },
  58. add: function ( object ) {
  59. if ( this.children.indexOf( object ) === - 1 ) {
  60. if( object.parent !== undefined ) {
  61. object.parent.removeChild( object );
  62. }
  63. object.parent = this;
  64. this.children.push( object );
  65. // add to scene
  66. var scene = this;
  67. while ( scene.parent !== undefined ) {
  68. scene = scene.parent;
  69. }
  70. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  71. scene.addChildRecurse( object );
  72. }
  73. }
  74. },
  75. remove: function ( object ) {
  76. var scene = this;
  77. var childIndex = this.children.indexOf( object );
  78. if ( childIndex !== - 1 ) {
  79. object.parent = undefined;
  80. this.children.splice( childIndex, 1 );
  81. // remove from scene
  82. while ( scene.parent !== undefined ) {
  83. scene = scene.parent;
  84. }
  85. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  86. scene.removeChildRecurse( object );
  87. }
  88. }
  89. },
  90. getChildByName: function ( name, doRecurse ) {
  91. var c, cl, child, recurseResult;
  92. for ( c = 0, cl = this.children.length; c < cl; c++ ) {
  93. child = this.children[ c ];
  94. if ( child.name === name ) {
  95. return child;
  96. }
  97. if ( doRecurse ) {
  98. recurseResult = child.getChildByName( name, doRecurse );
  99. if ( recurseResult !== undefined ) {
  100. return recurseResult;
  101. }
  102. }
  103. }
  104. return undefined;
  105. },
  106. updateMatrix: function () {
  107. this.matrix.setPosition( this.position );
  108. if ( this.useQuaternion ) {
  109. this.matrix.setRotationFromQuaternion( this.quaternion );
  110. } else {
  111. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  112. }
  113. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  114. this.matrix.scale( this.scale );
  115. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  116. }
  117. this.matrixWorldNeedsUpdate = true;
  118. },
  119. update: function ( parentMatrixWorld, forceUpdate, camera ) {
  120. this.matrixAutoUpdate && this.updateMatrix();
  121. // update matrixWorld
  122. if ( this.matrixWorldNeedsUpdate || forceUpdate ) {
  123. if ( parentMatrixWorld ) {
  124. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  125. } else {
  126. this.matrixWorld.copy( this.matrix );
  127. }
  128. this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
  129. this.matrixWorldNeedsUpdate = false;
  130. forceUpdate = true;
  131. }
  132. // update children
  133. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  134. this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
  135. }
  136. },
  137. // DEPRECATED
  138. addChild: function ( child ) {
  139. console.warn( 'DEPRECATED: Object3D.addChild() is now Object3D.add()' );
  140. this.add( child );
  141. },
  142. removeChild: function ( child ) {
  143. console.warn( 'DEPRECATED: Object3D.removeChild() is now Object3D.remove()' );
  144. this.remove( child );
  145. }
  146. };
  147. THREE.Object3DCount = 0;
粤ICP备19079148号