SelectionBox.js 5.5 KB

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