Object3D.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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.Object3DCount ++;
  8. this.name = '';
  9. this.parent = undefined;
  10. this.children = [];
  11. this.up = new THREE.Vector3( 0, 1, 0 );
  12. this.position = new THREE.Vector3();
  13. this.rotation = new THREE.Vector3();
  14. this.eulerOrder = 'XYZ';
  15. this.scale = new THREE.Vector3( 1, 1, 1 );
  16. this.doubleSided = false;
  17. this.flipSided = false;
  18. this.renderDepth = null;
  19. this.rotationAutoUpdate = true;
  20. this.matrix = new THREE.Matrix4();
  21. this.matrixWorld = new THREE.Matrix4();
  22. this.matrixRotationWorld = new THREE.Matrix4();
  23. this.matrixAutoUpdate = true;
  24. this.matrixWorldNeedsUpdate = true;
  25. this.quaternion = new THREE.Quaternion();
  26. this.useQuaternion = false;
  27. this.boundRadius = 0.0;
  28. this.boundRadiusScale = 1.0;
  29. this.visible = true;
  30. this.castShadow = false;
  31. this.receiveShadow = false;
  32. this.frustumCulled = true;
  33. this._vector = new THREE.Vector3();
  34. };
  35. THREE.Object3D.prototype = {
  36. constructor: THREE.Object3D,
  37. applyMatrix: function ( matrix ) {
  38. this.matrix.multiply( matrix, this.matrix );
  39. this.scale.getScaleFromMatrix( this.matrix );
  40. this.rotation.getRotationFromMatrix( this.matrix, this.scale );
  41. this.position.getPositionFromMatrix( this.matrix );
  42. },
  43. translate: function ( distance, axis ) {
  44. this.matrix.rotateAxis( axis );
  45. this.position.addSelf( axis.multiplyScalar( distance ) );
  46. },
  47. translateX: function ( distance ) {
  48. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  49. },
  50. translateY: function ( distance ) {
  51. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  52. },
  53. translateZ: function ( distance ) {
  54. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  55. },
  56. lookAt: function ( vector ) {
  57. // TODO: Add hierarchy support.
  58. this.matrix.lookAt( vector, this.position, this.up );
  59. if ( this.rotationAutoUpdate ) {
  60. this.rotation.getRotationFromMatrix( this.matrix );
  61. }
  62. },
  63. add: function ( object ) {
  64. if ( object === this ) {
  65. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  66. return;
  67. }
  68. if ( object instanceof THREE.Object3D ) { // && this.children.indexOf( object ) === - 1
  69. if ( object.parent !== undefined ) {
  70. object.parent.remove( object );
  71. }
  72. object.parent = this;
  73. this.children.push( object );
  74. // add to scene
  75. var scene = this;
  76. while ( scene.parent !== undefined ) {
  77. scene = scene.parent;
  78. }
  79. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  80. scene.__addObject( object );
  81. }
  82. }
  83. },
  84. remove: function ( object ) {
  85. var index = this.children.indexOf( object );
  86. if ( index !== - 1 ) {
  87. object.parent = undefined;
  88. this.children.splice( index, 1 );
  89. // remove from scene
  90. var scene = this;
  91. while ( scene.parent !== undefined ) {
  92. scene = scene.parent;
  93. }
  94. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  95. scene.__removeObject( object );
  96. }
  97. }
  98. },
  99. getChildByName: function ( name, recursive ) {
  100. var c, cl, child;
  101. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  102. child = this.children[ c ];
  103. if ( child.name === name ) {
  104. return child;
  105. }
  106. if ( recursive ) {
  107. child = child.getChildByName( name, recursive );
  108. if ( child !== undefined ) {
  109. return child;
  110. }
  111. }
  112. }
  113. return undefined;
  114. },
  115. updateMatrix: function () {
  116. this.matrix.setPosition( this.position );
  117. if ( this.useQuaternion ) {
  118. this.matrix.setRotationFromQuaternion( this.quaternion );
  119. } else {
  120. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  121. }
  122. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  123. this.matrix.scale( this.scale );
  124. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  125. }
  126. this.matrixWorldNeedsUpdate = true;
  127. },
  128. updateMatrixWorld: function ( force ) {
  129. this.matrixAutoUpdate && this.updateMatrix();
  130. // update matrixWorld
  131. if ( this.matrixWorldNeedsUpdate || force ) {
  132. if ( this.parent ) {
  133. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  134. } else {
  135. this.matrixWorld.copy( this.matrix );
  136. }
  137. this.matrixWorldNeedsUpdate = false;
  138. force = true;
  139. }
  140. // update children
  141. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  142. this.children[ i ].updateMatrixWorld( force );
  143. }
  144. }
  145. };
  146. THREE.Object3DCount = 0;