Object3D.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.positionScreen = new THREE.Vector4();
  12. this.rotation = new THREE.Vector3();
  13. this.scale = new THREE.Vector3( 1.0, 1.0, 1.0 );
  14. this.matrix = new THREE.Matrix4();
  15. this.matrixWorld = new THREE.Matrix4();
  16. this.matrixRotation = new THREE.Matrix4();
  17. this.matrixRotationWorld = new THREE.Matrix4();
  18. this.matrixNeedsUpdate = true;
  19. this.matrixAutoUpdate = 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. };
  26. THREE.Object3D.prototype = {
  27. addChild: function ( child ) {
  28. if ( this.children.indexOf( child ) === - 1 ) {
  29. if( child.parent !== undefined ) {
  30. child.parent.removeChild( child );
  31. }
  32. child.parent = this;
  33. this.children.push( child );
  34. // add to scene
  35. var scene = this;
  36. while( scene instanceof THREE.Scene === false && scene !== undefined )
  37. scene = scene.parent;
  38. if( scene !== undefined )
  39. scene.addChildRecurse( child );
  40. }
  41. },
  42. removeChild: function ( child ) {
  43. var childIndex = this.children.indexOf( child );
  44. if ( childIndex !== - 1 ) {
  45. child.parent = undefined;
  46. this.children.splice( childIndex, 1 );
  47. }
  48. },
  49. updateMatrix: function () {
  50. this.matrix.setPosition( this.position );
  51. if ( this.useQuaternion ) {
  52. this.matrixRotation.setRotationFromQuaternion( this.quaternion );
  53. } else {
  54. this.matrixRotation.setRotationFromEuler( this.rotation );
  55. }
  56. this.matrix.n11 = this.matrixRotation.n11;
  57. this.matrix.n12 = this.matrixRotation.n12;
  58. this.matrix.n13 = this.matrixRotation.n13;
  59. this.matrix.n21 = this.matrixRotation.n21;
  60. this.matrix.n22 = this.matrixRotation.n22;
  61. this.matrix.n23 = this.matrixRotation.n23;
  62. this.matrix.n31 = this.matrixRotation.n31;
  63. this.matrix.n32 = this.matrixRotation.n32;
  64. this.matrix.n33 = this.matrixRotation.n33;
  65. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  66. this.matrix.scale( this.scale );
  67. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  68. }
  69. return true;
  70. },
  71. // TODO: Add link to parent so rotationWorld can be updated too.
  72. update: function ( parentMatrixWorld, forceUpdate, camera ) {
  73. if ( this.visible ) {
  74. if ( this.matrixAutoUpdate ) {
  75. forceUpdate |= this.updateMatrix();
  76. }
  77. // update matrixWorld
  78. if ( forceUpdate || this.matrixNeedsUpdate ) {
  79. if ( parentMatrixWorld ) {
  80. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  81. } else {
  82. this.matrixWorld.copy( this.matrix );
  83. }
  84. this.matrixNeedsUpdate = false;
  85. forceUpdate = true;
  86. }
  87. // update children
  88. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  89. this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
  90. }
  91. }
  92. }
  93. }
  94. THREE.Object3DCounter = { value: 0 };