SelectionBox.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. ( function () {
  2. /**
  3. * This is a class to check whether objects are in a selection area in 3D space
  4. */
  5. const _frustum = new THREE.Frustum();
  6. const _center = new THREE.Vector3();
  7. const _tmpPoint = new THREE.Vector3();
  8. const _vecNear = new THREE.Vector3();
  9. const _vecTopLeft = new THREE.Vector3();
  10. const _vecTopRight = new THREE.Vector3();
  11. const _vecDownRight = new THREE.Vector3();
  12. const _vecDownLeft = new THREE.Vector3();
  13. const _vecFarTopLeft = new THREE.Vector3();
  14. const _vecFarTopRight = new THREE.Vector3();
  15. const _vecFarDownRight = new THREE.Vector3();
  16. const _vecFarDownLeft = new THREE.Vector3();
  17. const _vectemp1 = new THREE.Vector3();
  18. const _vectemp2 = new THREE.Vector3();
  19. const _vectemp3 = new THREE.Vector3();
  20. class SelectionBox {
  21. constructor( camera, scene, deep = Number.MAX_VALUE ) {
  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;
  28. }
  29. select( 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. updateFrustum( startPoint, endPoint ) {
  38. startPoint = startPoint || this.startPoint;
  39. endPoint = endPoint || this.endPoint; // Avoid invalid frustum
  40. if ( startPoint.x === endPoint.x ) {
  41. endPoint.x += Number.EPSILON;
  42. }
  43. if ( startPoint.y === endPoint.y ) {
  44. endPoint.y += Number.EPSILON;
  45. }
  46. this.camera.updateProjectionMatrix();
  47. this.camera.updateMatrixWorld();
  48. if ( this.camera.isPerspectiveCamera ) {
  49. _tmpPoint.copy( startPoint );
  50. _tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  51. _tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  52. endPoint.x = Math.max( startPoint.x, endPoint.x );
  53. endPoint.y = Math.min( startPoint.y, endPoint.y );
  54. _vecNear.setFromMatrixPosition( this.camera.matrixWorld );
  55. _vecTopLeft.copy( _tmpPoint );
  56. _vecTopRight.set( endPoint.x, _tmpPoint.y, 0 );
  57. _vecDownRight.copy( endPoint );
  58. _vecDownLeft.set( _tmpPoint.x, endPoint.y, 0 );
  59. _vecTopLeft.unproject( this.camera );
  60. _vecTopRight.unproject( this.camera );
  61. _vecDownRight.unproject( this.camera );
  62. _vecDownLeft.unproject( this.camera );
  63. _vectemp1.copy( _vecTopLeft ).sub( _vecNear );
  64. _vectemp2.copy( _vecTopRight ).sub( _vecNear );
  65. _vectemp3.copy( _vecDownRight ).sub( _vecNear );
  66. _vectemp1.normalize();
  67. _vectemp2.normalize();
  68. _vectemp3.normalize();
  69. _vectemp1.multiplyScalar( this.deep );
  70. _vectemp2.multiplyScalar( this.deep );
  71. _vectemp3.multiplyScalar( this.deep );
  72. _vectemp1.add( _vecNear );
  73. _vectemp2.add( _vecNear );
  74. _vectemp3.add( _vecNear );
  75. const planes = _frustum.planes;
  76. planes[ 0 ].setFromCoplanarPoints( _vecNear, _vecTopLeft, _vecTopRight );
  77. planes[ 1 ].setFromCoplanarPoints( _vecNear, _vecTopRight, _vecDownRight );
  78. planes[ 2 ].setFromCoplanarPoints( _vecDownRight, _vecDownLeft, _vecNear );
  79. planes[ 3 ].setFromCoplanarPoints( _vecDownLeft, _vecTopLeft, _vecNear );
  80. planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
  81. planes[ 5 ].setFromCoplanarPoints( _vectemp3, _vectemp2, _vectemp1 );
  82. planes[ 5 ].normal.multiplyScalar( - 1 );
  83. } else if ( this.camera.isOrthographicCamera ) {
  84. const left = Math.min( startPoint.x, endPoint.x );
  85. const top = Math.max( startPoint.y, endPoint.y );
  86. const right = Math.max( startPoint.x, endPoint.x );
  87. const down = Math.min( startPoint.y, endPoint.y );
  88. _vecTopLeft.set( left, top, - 1 );
  89. _vecTopRight.set( right, top, - 1 );
  90. _vecDownRight.set( right, down, - 1 );
  91. _vecDownLeft.set( left, down, - 1 );
  92. _vecFarTopLeft.set( left, top, 1 );
  93. _vecFarTopRight.set( right, top, 1 );
  94. _vecFarDownRight.set( right, down, 1 );
  95. _vecFarDownLeft.set( left, down, 1 );
  96. _vecTopLeft.unproject( this.camera );
  97. _vecTopRight.unproject( this.camera );
  98. _vecDownRight.unproject( this.camera );
  99. _vecDownLeft.unproject( this.camera );
  100. _vecFarTopLeft.unproject( this.camera );
  101. _vecFarTopRight.unproject( this.camera );
  102. _vecFarDownRight.unproject( this.camera );
  103. _vecFarDownLeft.unproject( this.camera );
  104. const planes = _frustum.planes;
  105. planes[ 0 ].setFromCoplanarPoints( _vecTopLeft, _vecFarTopLeft, _vecFarTopRight );
  106. planes[ 1 ].setFromCoplanarPoints( _vecTopRight, _vecFarTopRight, _vecFarDownRight );
  107. planes[ 2 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarDownLeft, _vecDownLeft );
  108. planes[ 3 ].setFromCoplanarPoints( _vecFarDownLeft, _vecFarTopLeft, _vecTopLeft );
  109. planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
  110. planes[ 5 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarTopRight, _vecFarTopLeft );
  111. planes[ 5 ].normal.multiplyScalar( - 1 );
  112. } else {
  113. console.error( 'THREE.SelectionBox: Unsupported camera type.' );
  114. }
  115. }
  116. searchChildInFrustum( frustum, object ) {
  117. if ( object.isMesh || object.isLine || object.isPoints ) {
  118. if ( object.material !== undefined ) {
  119. if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
  120. _center.copy( object.geometry.boundingSphere.center );
  121. _center.applyMatrix4( object.matrixWorld );
  122. if ( frustum.containsPoint( _center ) ) {
  123. this.collection.push( object );
  124. }
  125. }
  126. }
  127. if ( object.children.length > 0 ) {
  128. for ( let x = 0; x < object.children.length; x ++ ) {
  129. this.searchChildInFrustum( frustum, object.children[ x ] );
  130. }
  131. }
  132. }
  133. }
  134. THREE.SelectionBox = SelectionBox;
  135. } )();