Object3D.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. var mat = new THREE.Matrix4().extractRotation( this.matrix );
  41. this.rotation.setEulerFromRotationMatrix( mat, this.eulerOrder );
  42. this.position.getPositionFromMatrix( this.matrix );
  43. },
  44. translate: function ( distance, axis ) {
  45. this.matrix.rotateAxis( axis );
  46. this.position.addSelf( axis.multiplyScalar( distance ) );
  47. },
  48. translateX: function ( distance ) {
  49. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  50. },
  51. translateY: function ( distance ) {
  52. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  53. },
  54. translateZ: function ( distance ) {
  55. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  56. },
  57. lookAt: function ( vector ) {
  58. // TODO: Add hierarchy support.
  59. this.matrix.lookAt( vector, this.position, this.up );
  60. if ( this.rotationAutoUpdate ) {
  61. this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
  62. }
  63. },
  64. add: function ( object ) {
  65. if ( object === this ) {
  66. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  67. return;
  68. }
  69. if ( object instanceof THREE.Object3D ) {
  70. if ( object.parent !== undefined ) {
  71. object.parent.remove( object );
  72. }
  73. object.parent = this;
  74. this.children.push( object );
  75. // add to scene
  76. var scene = this;
  77. while ( scene.parent !== undefined ) {
  78. scene = scene.parent;
  79. }
  80. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  81. scene.__addObject( object );
  82. }
  83. }
  84. },
  85. remove: function ( object ) {
  86. var index = this.children.indexOf( object );
  87. if ( index !== - 1 ) {
  88. object.parent = undefined;
  89. this.children.splice( index, 1 );
  90. // remove from scene
  91. var scene = this;
  92. while ( scene.parent !== undefined ) {
  93. scene = scene.parent;
  94. }
  95. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  96. scene.__removeObject( object );
  97. }
  98. }
  99. },
  100. getChildByName: function ( name, recursive ) {
  101. var c, cl, child;
  102. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  103. child = this.children[ c ];
  104. if ( child.name === name ) {
  105. return child;
  106. }
  107. if ( recursive ) {
  108. child = child.getChildByName( name, recursive );
  109. if ( child !== undefined ) {
  110. return child;
  111. }
  112. }
  113. }
  114. return undefined;
  115. },
  116. updateMatrix: function () {
  117. this.matrix.setPosition( this.position );
  118. if ( this.useQuaternion === true ) {
  119. this.matrix.setRotationFromQuaternion( this.quaternion );
  120. } else {
  121. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  122. }
  123. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  124. this.matrix.scale( this.scale );
  125. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  126. }
  127. this.matrixWorldNeedsUpdate = true;
  128. },
  129. updateMatrixWorld: function ( force ) {
  130. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  131. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  132. if ( this.parent !== undefined ) {
  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. worldToLocal: function ( vector ) {
  146. return THREE.Object3D.__m1.getInverse( this.matrixWorld ).multiplyVector3( vector );
  147. },
  148. localToWorld: function ( vector ) {
  149. return this.matrixWorld.multiplyVector3( vector );
  150. }
  151. };
  152. THREE.Object3D.__m1 = new THREE.Matrix4();
  153. THREE.Object3DCount = 0;
粤ICP备19079148号