Object3D.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.id = THREE.Object3DCounter.value ++;
  8. this.visible = true;
  9. this.autoUpdateMatrix = true;
  10. this.matrixNeedsToUpdate = true;
  11. this.parent = undefined;
  12. this.children = [];
  13. this.position = new THREE.Vector3();
  14. this.rotation = new THREE.Vector3();
  15. this.scale = new THREE.Vector3( 1.0, 1.0, 1.0 );
  16. this.localMatrix = new THREE.Matrix4();
  17. this.globalMatrix = new THREE.Matrix4();
  18. this.quaternion = new THREE.Quaternion();
  19. this.useQuaternion = false;
  20. this.screenPosition = new THREE.Vector4(); // xyzr
  21. this.boundRadius = 0.0;
  22. this.boundRadiusScale = 1.0;
  23. };
  24. /*
  25. * Update
  26. */
  27. THREE.Object3D.prototype.update = function( parentGlobalMatrix, forceUpdate, camera, renderer ) {
  28. // visible and auto update?
  29. if( this.visible ) {
  30. // update local
  31. if( this.autoUpdateMatrix )
  32. forceUpdate |= this.updateMatrix();
  33. // update global
  34. if( forceUpdate || this.matrixNeedsToUpdate ) {
  35. if( parentGlobalMatrix )
  36. this.globalMatrix.multiply( parentGlobalMatrix, this.localMatrix );
  37. else
  38. this.globalMatrix.copy( this.localMatrix );
  39. this.matrixNeedsToUpdate = false;
  40. forceUpdate = true;
  41. }
  42. // update children
  43. for( var i = 0; i < this.children.length; i++ ) {
  44. this.children[ i ].update( this.globalMatrix, forceUpdate, camera, renderer );
  45. }
  46. }
  47. };
  48. /*
  49. * Update Matrix
  50. */
  51. THREE.Object3D.prototype.updateMatrix = function() {
  52. // update position
  53. var isDirty = false;
  54. if( this.position.isDirty ) {
  55. this.localMatrix.setPosition( this.position );
  56. this.position.isDirty = false;
  57. isDirty = true;
  58. }
  59. // update quaternion
  60. if( this.useQuaternion ) {
  61. if( this.quaternion.isDirty ) {
  62. this.localMatrix.setRotationFromQuaternion( this.quaternion );
  63. this.quaternion.isDirty = false;
  64. this.rotation .isDirty = false;
  65. if( this.scale.isDirty || this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  66. this.localMatrix.scale( this.scale );
  67. this.scale.isDirty = false;
  68. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  69. }
  70. isDirty = true;
  71. }
  72. }
  73. // update rotation
  74. else if( this.rotation.isDirty ) {
  75. this.localMatrix.setRotationFromEuler( this.rotation );
  76. this.rotation.isDirty = false;
  77. if( this.scale.isDirty || this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  78. this.localMatrix.scale( this.scale );
  79. this.scale.isDirty = false;
  80. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  81. }
  82. isDirty = true;
  83. }
  84. // update scale
  85. if( this.scale.isDirty ) {
  86. if( this.useQuaternion )
  87. this.localMatrix.setRotationFromQuaternion( this.quaternion );
  88. else
  89. this.localMatrix.setRotationFromEuler( this.rotation );
  90. this.localMatrix.scale( this.scale );
  91. this.scale.isDirty = false;
  92. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ));
  93. isDirty = true;
  94. }
  95. return isDirty;
  96. };
  97. /*
  98. * AddChild
  99. */
  100. THREE.Object3D.prototype.addChild = function( child ) {
  101. if( this.children.indexOf( child ) === -1 ) {
  102. if( child.parent !== undefined )
  103. child.parent.removeChild( child );
  104. child.parent = this;
  105. this.children.push( child );
  106. }
  107. };
  108. /*
  109. * RemoveChild
  110. */
  111. THREE.Object3D.prototype.removeChild = function() {
  112. var childIndex = this.children.indexOf( child );
  113. if( childIndex !== -1 ) {
  114. this.children.splice( childIndex, 1 );
  115. child.parent = undefined;
  116. }
  117. };
  118. THREE.Object3DCounter = { value: 0 };