Object3D.js 3.8 KB

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