Object3D.js 3.9 KB

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