Object3D.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.name = '';
  8. this.id = THREE.Object3DCount ++;
  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.dynamic = false; // when true it retains arrays so they can be updated with __dirty*
  17. this.doubleSided = false;
  18. this.flipSided = false;
  19. this.renderDepth = null;
  20. this.rotationAutoUpdate = true;
  21. this.matrix = new THREE.Matrix4();
  22. this.matrixWorld = new THREE.Matrix4();
  23. this.matrixRotationWorld = new THREE.Matrix4();
  24. this.matrixAutoUpdate = true;
  25. this.matrixWorldNeedsUpdate = true;
  26. this.quaternion = new THREE.Quaternion();
  27. this.useQuaternion = false;
  28. this.boundRadius = 0.0;
  29. this.boundRadiusScale = 1.0;
  30. this.visible = true;
  31. this.castShadow = false;
  32. this.receiveShadow = false;
  33. this.frustumCulled = true;
  34. this._vector = new THREE.Vector3();
  35. };
  36. THREE.Object3D.prototype = {
  37. constructor: THREE.Object3D,
  38. translate: function ( distance, axis ) {
  39. this.matrix.rotateAxis( axis );
  40. this.position.addSelf( axis.multiplyScalar( distance ) );
  41. },
  42. translateX: function ( distance ) {
  43. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  44. },
  45. translateY: function ( distance ) {
  46. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  47. },
  48. translateZ: function ( distance ) {
  49. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  50. },
  51. lookAt: function ( vector ) {
  52. // TODO: Add hierarchy support.
  53. this.matrix.lookAt( vector, this.position, this.up );
  54. if ( this.rotationAutoUpdate ) {
  55. this.rotation.setRotationFromMatrix( this.matrix );
  56. }
  57. },
  58. add: function ( object ) {
  59. if ( this.children.indexOf( object ) === - 1 ) {
  60. if ( object.parent !== undefined ) {
  61. object.parent.remove( object );
  62. }
  63. object.parent = this;
  64. this.children.push( object );
  65. }
  66. },
  67. remove: function ( object ) {
  68. var index = this.children.indexOf( object );
  69. if ( index !== - 1 ) {
  70. object.parent = undefined;
  71. this.children.splice( index, 1 );
  72. }
  73. },
  74. getChildByName: function ( name, doRecurse ) {
  75. var c, cl, child, recurseResult;
  76. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  77. child = this.children[ c ];
  78. if ( child.name === name ) {
  79. return child;
  80. }
  81. if ( doRecurse ) {
  82. recurseResult = child.getChildByName( name, doRecurse );
  83. if ( recurseResult !== undefined ) {
  84. return recurseResult;
  85. }
  86. }
  87. }
  88. return undefined;
  89. },
  90. updateMatrix: function () {
  91. this.matrix.setPosition( this.position );
  92. if ( this.useQuaternion ) {
  93. this.matrix.setRotationFromQuaternion( this.quaternion );
  94. } else {
  95. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  96. }
  97. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  98. this.matrix.scale( this.scale );
  99. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  100. }
  101. this.matrixWorldNeedsUpdate = true;
  102. },
  103. updateMatrixWorld: function ( force ) {
  104. this.matrixAutoUpdate && this.updateMatrix();
  105. // update matrixWorld
  106. if ( this.matrixWorldNeedsUpdate || force ) {
  107. if ( this.parent ) {
  108. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  109. } else {
  110. this.matrixWorld.copy( this.matrix );
  111. }
  112. this.matrixWorldNeedsUpdate = false;
  113. force = true;
  114. }
  115. // update children
  116. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  117. this.children[ i ].updateMatrixWorld( force );
  118. }
  119. },
  120. // DEPRECATED
  121. addChild: function ( child ) {
  122. console.warn( 'DEPRECATED: Object3D.addChild() is now Object3D.add().' );
  123. this.add( child );
  124. },
  125. removeChild: function ( child ) {
  126. console.warn( 'DEPRECATED: Object3D.removeChild() is now Object3D.remove().' );
  127. this.remove( child );
  128. }
  129. };
  130. THREE.Object3DCount = 0;