Object3D.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.0, 1.0, 0.0 );
  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.rotationAutoUpdate = true;
  14. this.matrix = new THREE.Matrix4();
  15. this.matrixWorld = new THREE.Matrix4();
  16. this.matrixRotationWorld = new THREE.Matrix4();
  17. this.matrixAutoUpdate = true;
  18. this.matrixWorldNeedsUpdate = 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. /*
  27. translateX : function () {
  28. },
  29. translateY : function () {
  30. },
  31. translateZ : function () {
  32. },
  33. */
  34. lookAt : function ( target ) {
  35. // TODO: Add hierarchy support.
  36. this.matrix.lookAt( this.position, target, this.up );
  37. if ( this.rotationAutoUpdate ) {
  38. this.rotation.setRotationFromMatrix( this.matrix );
  39. }
  40. },
  41. addChild: function ( child ) {
  42. if ( this.children.indexOf( child ) === - 1 ) {
  43. if( child.parent !== undefined ) {
  44. child.parent.removeChild( child );
  45. }
  46. child.parent = this;
  47. this.children.push( child );
  48. // add to scene
  49. var scene = this;
  50. while ( scene instanceof THREE.Scene === false && scene !== undefined ) {
  51. scene = scene.parent;
  52. }
  53. if ( scene !== undefined ) {
  54. scene.addChildRecurse( child );
  55. }
  56. }
  57. },
  58. removeChild: function ( child ) {
  59. var childIndex = this.children.indexOf( child );
  60. if ( childIndex !== - 1 ) {
  61. child.parent = undefined;
  62. this.children.splice( childIndex, 1 );
  63. }
  64. },
  65. updateMatrix: function () {
  66. this.matrix.setPosition( this.position );
  67. if ( this.useQuaternion ) {
  68. this.matrix.setRotationFromQuaternion( this.quaternion );
  69. } else {
  70. this.matrix.setRotationFromEuler( this.rotation );
  71. }
  72. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  73. this.matrix.scale( this.scale );
  74. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  75. }
  76. return true;
  77. },
  78. update: function ( parentMatrixWorld, forceUpdate, camera ) {
  79. if ( this.visible ) {
  80. if ( this.matrixAutoUpdate ) {
  81. forceUpdate |= this.updateMatrix();
  82. }
  83. // update matrixWorld
  84. if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
  85. if ( parentMatrixWorld ) {
  86. this.matrixWorld.multiply( parentMatrixWorld, this.matrix );
  87. } else {
  88. this.matrixWorld.copy( this.matrix );
  89. }
  90. this.matrixRotationWorld.extractRotation( this.matrixWorld, this.scale );
  91. this.matrixWorldNeedsUpdate = false;
  92. forceUpdate = true;
  93. }
  94. // update children
  95. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  96. this.children[ i ].update( this.matrixWorld, forceUpdate, camera );
  97. }
  98. }
  99. }
  100. }