SelectionBox.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @author HypnosNova / https://www.threejs.org.cn/gallery
  3. * This is a class to check whether objects are in a selection area in 3D space
  4. */
  5. THREE.SelectionBox = ( function () {
  6. var frustum = new THREE.Frustum();
  7. var center = new THREE.Vector3();
  8. var tmpPoint = new THREE.Vector3();
  9. var vecNear = new THREE.Vector3();
  10. var vecTopLeft = new THREE.Vector3();
  11. var vecTopRight = new THREE.Vector3();
  12. var vecDownRight = new THREE.Vector3();
  13. var vecDownLeft = new THREE.Vector3();
  14. var vecFarTopLeft = new THREE.Vector3();
  15. var vecFarTopRight = new THREE.Vector3();
  16. var vecFarDownRight = new THREE.Vector3();
  17. var vecFarDownLeft = new THREE.Vector3();
  18. var vectemp1 = new THREE.Vector3();
  19. var vectemp2 = new THREE.Vector3();
  20. var vectemp3 = new THREE.Vector3();
  21. function SelectionBox( camera, scene, deep ) {
  22. this.camera = camera;
  23. this.scene = scene;
  24. this.startPoint = new THREE.Vector3();
  25. this.endPoint = new THREE.Vector3();
  26. this.collection = [];
  27. this.deep = deep || Number.MAX_VALUE;
  28. }
  29. SelectionBox.prototype.select = function ( startPoint, endPoint ) {
  30. this.startPoint = startPoint || this.startPoint;
  31. this.endPoint = endPoint || this.endPoint;
  32. this.collection = [];
  33. this.updateFrustum( this.startPoint, this.endPoint );
  34. this.searchChildInFrustum( frustum, this.scene );
  35. return this.collection;
  36. };
  37. SelectionBox.prototype.updateFrustum = function ( startPoint, endPoint ) {
  38. startPoint = startPoint || this.startPoint;
  39. endPoint = endPoint || this.endPoint;
  40. this.camera.updateProjectionMatrix();
  41. this.camera.updateMatrixWorld();
  42. if ( this.camera.isPerspectiveCamera ) {
  43. tmpPoint.copy( startPoint );
  44. tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  45. tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  46. endPoint.x = Math.max( startPoint.x, endPoint.x );
  47. endPoint.y = Math.min( startPoint.y, endPoint.y );
  48. vecNear.copy( this.camera.position );
  49. vecTopLeft.copy( tmpPoint );
  50. vecTopRight.set( endPoint.x, tmpPoint.y, 0 );
  51. vecDownRight.copy( endPoint );
  52. vecDownLeft.set( tmpPoint.x, endPoint.y, 0 );
  53. vecTopLeft.unproject( this.camera );
  54. vecTopRight.unproject( this.camera );
  55. vecDownRight.unproject( this.camera );
  56. vecDownLeft.unproject( this.camera );
  57. vectemp1.copy( vecTopLeft ).sub( vecNear );
  58. vectemp2.copy( vecTopRight ).sub( vecNear );
  59. vectemp3.copy( vecDownRight ).sub( vecNear );
  60. vectemp1.normalize();
  61. vectemp2.normalize();
  62. vectemp3.normalize();
  63. vectemp1.multiplyScalar( this.deep );
  64. vectemp2.multiplyScalar( this.deep );
  65. vectemp3.multiplyScalar( this.deep );
  66. vectemp1.add( vecNear );
  67. vectemp2.add( vecNear );
  68. vectemp3.add( vecNear );
  69. var planes = frustum.planes;
  70. planes[ 0 ].setFromCoplanarPoints( vecNear, vecTopLeft, vecTopRight );
  71. planes[ 1 ].setFromCoplanarPoints( vecNear, vecTopRight, vecDownRight );
  72. planes[ 2 ].setFromCoplanarPoints( vecDownRight, vecDownLeft, vecNear );
  73. planes[ 3 ].setFromCoplanarPoints( vecDownLeft, vecTopLeft, vecNear );
  74. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  75. planes[ 5 ].setFromCoplanarPoints( vectemp3, vectemp2, vectemp1 );
  76. planes[ 5 ].normal.multiplyScalar( - 1 );
  77. } else if ( this.camera.isOrthographicCamera ) {
  78. if ( startPoint.equals( endPoint ) ) endPoint.addScalar( Number.EPSILON ); // avoid invalid frustum
  79. var left = Math.min( startPoint.x, endPoint.x );
  80. var top = Math.max( startPoint.y, endPoint.y );
  81. var right = Math.max( startPoint.x, endPoint.x );
  82. var down = Math.min( startPoint.y, endPoint.y );
  83. vecTopLeft.set( left, top, - 1 );
  84. vecTopRight.set( right, top, - 1 );
  85. vecDownRight.set( right, down, - 1 );
  86. vecDownLeft.set( left, down, - 1 );
  87. vecFarTopLeft.set( left, top, 1 );
  88. vecFarTopRight.set( right, top, 1 );
  89. vecFarDownRight.set( right, down, 1 );
  90. vecFarDownLeft.set( left, down, 1 );
  91. vecTopLeft.unproject( this.camera );
  92. vecTopRight.unproject( this.camera );
  93. vecDownRight.unproject( this.camera );
  94. vecDownLeft.unproject( this.camera );
  95. vecFarTopLeft.unproject( this.camera );
  96. vecFarTopRight.unproject( this.camera );
  97. vecFarDownRight.unproject( this.camera );
  98. vecFarDownLeft.unproject( this.camera );
  99. var planes = frustum.planes;
  100. planes[ 0 ].setFromCoplanarPoints( vecTopLeft, vecFarTopLeft, vecFarTopRight );
  101. planes[ 1 ].setFromCoplanarPoints( vecTopRight, vecFarTopRight, vecFarDownRight );
  102. planes[ 2 ].setFromCoplanarPoints( vecFarDownRight, vecFarDownLeft, vecDownLeft );
  103. planes[ 3 ].setFromCoplanarPoints( vecFarDownLeft, vecFarTopLeft, vecTopLeft );
  104. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  105. planes[ 5 ].setFromCoplanarPoints( vecFarDownRight, vecFarTopRight, vecFarTopLeft );
  106. planes[ 5 ].normal.multiplyScalar( - 1 );
  107. } else {
  108. console.error( 'THREE.SelectionBox: Unsupported camera type.' );
  109. }
  110. };
  111. SelectionBox.prototype.searchChildInFrustum = function ( frustum, object ) {
  112. if ( object.isMesh || object.isLine || object.isPoints ) {
  113. if ( object.material !== undefined ) {
  114. if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
  115. center.copy( object.geometry.boundingSphere.center );
  116. center.applyMatrix4( object.matrixWorld );
  117. if ( frustum.containsPoint( center ) ) {
  118. this.collection.push( object );
  119. }
  120. }
  121. }
  122. if ( object.children.length > 0 ) {
  123. for ( var x = 0; x < object.children.length; x ++ ) {
  124. this.searchChildInFrustum( frustum, object.children[ x ] );
  125. }
  126. }
  127. };
  128. return SelectionBox;
  129. } )();