Object3D.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.parent = undefined;
  9. this.children = [];
  10. this.position = new THREE.Vector3();
  11. this.rotation = new THREE.Vector3();
  12. this.scale = new THREE.Vector3( 1.0, 1.0, 1.0 );
  13. this.matrixRotation = new THREE.Matrix4(); // this is just to cache it when somewhere it's computed somewhere else (stripped down globalMatrix)
  14. this.localMatrix = new THREE.Matrix4();
  15. this.globalMatrix = new THREE.Matrix4();
  16. this.matrixAutoUpdate = true;
  17. this.matrixNeedsUpdate = true;
  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. this.visible = true;
  24. };
  25. THREE.Object3D.prototype = {
  26. update: function ( parentGlobalMatrix, forceUpdate, camera ) {
  27. if ( this.visible ) {
  28. if ( this.matrixAutoUpdate ) {
  29. forceUpdate |= this.updateMatrix();
  30. }
  31. // update global
  32. if ( forceUpdate || this.matrixNeedsUpdate ) {
  33. if ( parentGlobalMatrix ) {
  34. this.globalMatrix.multiply( parentGlobalMatrix, this.localMatrix );
  35. } else {
  36. this.globalMatrix.copy( this.localMatrix );
  37. }
  38. this.matrixNeedsUpdate = false;
  39. forceUpdate = true;
  40. }
  41. // update children
  42. var i, l = this.children.length;
  43. for ( i = 0; i < l; i++ ) {
  44. this.children[ i ].update( this.globalMatrix, forceUpdate, camera );
  45. }
  46. }
  47. },
  48. updateMatrix: function () {
  49. this.localMatrix.setPosition( this.position );
  50. if ( this.useQuaternion ) {
  51. this.localMatrix.setRotationFromQuaternion( this.quaternion );
  52. } else {
  53. this.localMatrix.setRotationFromEuler( this.rotation );
  54. }
  55. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  56. this.localMatrix.scale( this.scale );
  57. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  58. }
  59. return true;
  60. },
  61. addChild: function ( child ) {
  62. if ( this.children.indexOf( child ) === -1 ) {
  63. if( child.parent !== undefined )
  64. child.parent.removeChild( child );
  65. child.parent = this;
  66. this.children.push( child );
  67. }
  68. },
  69. removeChild: function ( child ) {
  70. var childIndex = this.children.indexOf( child );
  71. if ( childIndex !== -1 ) {
  72. this.children.splice( childIndex, 1 );
  73. child.parent = undefined;
  74. }
  75. }
  76. }
  77. THREE.Object3DCounter = { value: 0 };