Object3D.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * @author mrdoob / 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.properties = {};
  10. this.parent = undefined;
  11. this.children = [];
  12. this.up = new THREE.Vector3( 0, 1, 0 );
  13. this.position = new THREE.Vector3();
  14. this.rotation = new THREE.Vector3();
  15. this.eulerOrder = 'XYZ';
  16. this.scale = new THREE.Vector3( 1, 1, 1 );
  17. this.renderDepth = null;
  18. this.rotationAutoUpdate = true;
  19. this.matrix = new THREE.Matrix4();
  20. this.matrixWorld = new THREE.Matrix4();
  21. this.matrixRotationWorld = new THREE.Matrix4();
  22. this.matrixAutoUpdate = true;
  23. this.matrixWorldNeedsUpdate = true;
  24. this.quaternion = new THREE.Quaternion();
  25. this.useQuaternion = false;
  26. this.boundRadius = 0.0;
  27. this.boundRadiusScale = 1.0;
  28. this.visible = true;
  29. this.castShadow = false;
  30. this.receiveShadow = false;
  31. this.frustumCulled = true;
  32. this._vector = new THREE.Vector3();
  33. };
  34. THREE.Object3D.prototype = {
  35. constructor: THREE.Object3D,
  36. applyMatrix: function ( matrix ) {
  37. this.matrix.multiply( matrix, this.matrix );
  38. this.scale.getScaleFromMatrix( this.matrix );
  39. var mat = new THREE.Matrix4().extractRotation( this.matrix );
  40. this.rotation.setEulerFromRotationMatrix( mat, this.eulerOrder );
  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. localToWorld: function ( vector ) {
  57. return this.matrixWorld.multiplyVector3( vector );
  58. },
  59. worldToLocal: function ( vector ) {
  60. return THREE.Object3D.__m1.getInverse( this.matrixWorld ).multiplyVector3( vector );
  61. },
  62. lookAt: function ( vector ) {
  63. // TODO: Add hierarchy support.
  64. this.matrix.lookAt( vector, this.position, this.up );
  65. if ( this.rotationAutoUpdate ) {
  66. this.rotation.setEulerFromRotationMatrix( this.matrix, this.eulerOrder );
  67. }
  68. },
  69. add: function ( object ) {
  70. if ( object === this ) {
  71. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  72. return;
  73. }
  74. if ( object instanceof THREE.Object3D ) {
  75. if ( object.parent !== undefined ) {
  76. object.parent.remove( object );
  77. }
  78. object.parent = this;
  79. this.children.push( object );
  80. // add to scene
  81. var scene = this;
  82. while ( scene.parent !== undefined ) {
  83. scene = scene.parent;
  84. }
  85. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  86. scene.__addObject( object );
  87. }
  88. }
  89. },
  90. remove: function ( object ) {
  91. var index = this.children.indexOf( object );
  92. if ( index !== - 1 ) {
  93. object.parent = undefined;
  94. this.children.splice( index, 1 );
  95. // remove from scene
  96. var scene = this;
  97. while ( scene.parent !== undefined ) {
  98. scene = scene.parent;
  99. }
  100. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  101. scene.__removeObject( object );
  102. }
  103. }
  104. },
  105. getChildByName: function ( name, recursive ) {
  106. var c, cl, child;
  107. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  108. child = this.children[ c ];
  109. if ( child.name === name ) {
  110. return child;
  111. }
  112. if ( recursive ) {
  113. child = child.getChildByName( name, recursive );
  114. if ( child !== undefined ) {
  115. return child;
  116. }
  117. }
  118. }
  119. return undefined;
  120. },
  121. getDescendants: function ( array ) {
  122. if ( array === undefined ) array = [];
  123. Array.prototype.push.apply( array, this.children );
  124. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  125. this.children[ i ].getDescendants( array );
  126. };
  127. return array;
  128. },
  129. updateMatrix: function () {
  130. this.matrix.setPosition( this.position );
  131. if ( this.useQuaternion === false ) {
  132. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  133. } else {
  134. this.matrix.setRotationFromQuaternion( this.quaternion );
  135. }
  136. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  137. this.matrix.scale( this.scale );
  138. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  139. }
  140. this.matrixWorldNeedsUpdate = true;
  141. },
  142. updateMatrixWorld: function ( force ) {
  143. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  144. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  145. if ( this.parent === undefined ) {
  146. this.matrixWorld.copy( this.matrix );
  147. } else {
  148. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  149. }
  150. this.matrixWorldNeedsUpdate = false;
  151. force = true;
  152. }
  153. // update children
  154. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  155. this.children[ i ].updateMatrixWorld( force );
  156. }
  157. },
  158. clone: function () {
  159. // TODO
  160. }
  161. };
  162. THREE.Object3D.__m1 = new THREE.Matrix4();
  163. THREE.Object3DCount = 0;
粤ICP备19079148号