Object3D.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.parent = undefined;
  8. this.children = [];
  9. this.up = new THREE.Vector3( 0, 1, 0 );
  10. this.position = new THREE.Vector3();
  11. this.rotation = new THREE.Vector3();
  12. this.eulerOrder = 'XYZ';
  13. this.scale = new THREE.Vector3( 1, 1, 1 );
  14. this.dynamic = false; // when true it retains arrays so they can be updated with __dirty*
  15. this.doubleSided = false;
  16. this.flipSided = false;
  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._vector = new THREE.Vector3();
  30. this.name = "";
  31. };
  32. THREE.Object3D.prototype = {
  33. translate : function ( distance, axis ) {
  34. this.matrix.rotateAxis( axis );
  35. this.position.addSelf( axis.multiplyScalar( distance ) );
  36. },
  37. translateX : function ( distance ) {
  38. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  39. },
  40. translateY : function ( distance ) {
  41. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  42. },
  43. translateZ : function ( distance ) {
  44. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  45. },
  46. lookAt : function ( vector ) {
  47. // TODO: Add hierarchy support.
  48. this.matrix.lookAt( vector, this.position, this.up );
  49. if ( this.rotationAutoUpdate ) {
  50. this.rotation.setRotationFromMatrix( this.matrix );
  51. }
  52. },
  53. addChild: function ( child ) {
  54. if ( this.children.indexOf( child ) === - 1 ) {
  55. if( child.parent !== undefined ) {
  56. child.parent.removeChild( child );
  57. }
  58. child.parent = this;
  59. this.children.push( child );
  60. // add to scene
  61. var scene = this;
  62. while ( scene.parent !== undefined ) {
  63. scene = scene.parent;
  64. }
  65. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  66. scene.addChildRecurse( child );
  67. }
  68. }
  69. },
  70. removeChild: function ( child ) {
  71. var childIndex = this.children.indexOf( child );
  72. if ( childIndex !== - 1 ) {
  73. child.parent = undefined;
  74. this.children.splice( childIndex, 1 );
  75. }
  76. },
  77. getChildByName: function ( name, doRecurse ) {
  78. var c, cl, child, recurseResult;
  79. for ( c = 0, cl = this.children.length; c < cl; c++ ) {
  80. child = this.children[ c ];
  81. if ( child.name === name ) {
  82. return child;
  83. }
  84. if ( doRecurse ) {
  85. recurseResult = child.getChildByName( name, doRecurse );
  86. if ( recurseResult !== undefined ) {
  87. return recurseResult;
  88. }
  89. }
  90. }
  91. return undefined;
  92. },
  93. updateMatrix: function () {
  94. this.matrix.setPosition( this.position );
  95. if ( this.useQuaternion ) {
  96. this.matrix.setRotationFromQuaternion( this.quaternion );
  97. } else {
  98. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  99. }
  100. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  101. this.matrix.scale( this.scale );
  102. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  103. }
  104. this.matrixWorldNeedsUpdate = true;
  105. },
  106. update: function ( parentMatrixWorld, forceUpdate, camera ) {
  107. this.matrixAutoUpdate && this.updateMatrix();
  108. // update matrixWorld
  109. if ( this.matrixWorldNeedsUpdate || forceUpdate ) {
  110. if ( parentMatrixWorld ) {
  111. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  112. } else {
  113. this.matrixWorld.copy( this.matrix );
  114. }
  115. this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
  116. this.matrixWorldNeedsUpdate = false;
  117. forceUpdate = true;
  118. }
  119. // update children
  120. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  121. this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
  122. }
  123. },
  124. constructor : THREE.Object3D
  125. };
粤ICP备19079148号