Object3D.js 4.0 KB

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