Object3D.js 3.3 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.matrixRotationWorld = new THREE.Matrix4();
  17. this.matrixNeedsUpdate = true;
  18. this.matrixAutoUpdate = true;
  19. this.quaternion = new THREE.Quaternion();
  20. this.useQuaternion = false;
  21. this.boundRadius = 0.0;
  22. this.boundRadiusScale = 1.0;
  23. this.visible = true;
  24. };
  25. THREE.Object3D.prototype = {
  26. addChild: function ( child ) {
  27. if ( this.children.indexOf( child ) === - 1 ) {
  28. if( child.parent !== undefined ) {
  29. child.parent.removeChild( child );
  30. }
  31. child.parent = this;
  32. this.children.push( child );
  33. // add to scene
  34. var scene = this;
  35. while( scene instanceof THREE.Scene === false && scene !== undefined )
  36. scene = scene.parent;
  37. if( scene !== undefined )
  38. scene.addChildRecurse( child );
  39. }
  40. },
  41. removeChild: function ( child ) {
  42. var childIndex = this.children.indexOf( child );
  43. if ( childIndex !== - 1 ) {
  44. child.parent = undefined;
  45. this.children.splice( childIndex, 1 );
  46. }
  47. },
  48. updateMatrix: function () {
  49. this.matrix.setPosition( this.position );
  50. if ( this.useQuaternion ) {
  51. this.matrix.setRotationFromQuaternion( this.quaternion );
  52. } else {
  53. this.matrix.setRotationFromEuler( this.rotation );
  54. }
  55. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  56. this.matrix.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. update: function ( parentMatrixWorld, forceUpdate, camera ) {
  62. if ( this.visible ) {
  63. if ( this.matrixAutoUpdate ) {
  64. forceUpdate |= this.updateMatrix();
  65. }
  66. // update matrixWorld
  67. if ( forceUpdate || this.matrixNeedsUpdate ) {
  68. if ( parentMatrixWorld ) {
  69. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  70. } else {
  71. this.matrixWorld.copy( this.matrix );
  72. }
  73. // extract rotation matrix
  74. var invScaleX = 1 / this.scale.x, invScaleY = 1 / this.scale.y, invScaleZ = 1 / this.scale.z;
  75. this.matrixRotationWorld.n11 = this.matrixWorld.n11 * invScaleX;
  76. this.matrixRotationWorld.n21 = this.matrixWorld.n21 * invScaleX;
  77. this.matrixRotationWorld.n31 = this.matrixWorld.n31 * invScaleX;
  78. this.matrixRotationWorld.n12 = this.matrixWorld.n12 * invScaleY;
  79. this.matrixRotationWorld.n22 = this.matrixWorld.n22 * invScaleY;
  80. this.matrixRotationWorld.n32 = this.matrixWorld.n32 * invScaleY;
  81. this.matrixRotationWorld.n13 = this.matrixWorld.n13 * invScaleZ;
  82. this.matrixRotationWorld.n23 = this.matrixWorld.n23 * invScaleZ;
  83. this.matrixRotationWorld.n33 = this.matrixWorld.n33 * invScaleZ;
  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 };
粤ICP备19079148号