Object3D.js 4.0 KB

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