SelectionBox.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. console.warn( "THREE.SelectionBox: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  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. // Avoid invalid frustum
  41. if ( startPoint.x === endPoint.x ) {
  42. endPoint.x += Number.EPSILON;
  43. }
  44. if ( startPoint.y === endPoint.y ) {
  45. endPoint.y += Number.EPSILON;
  46. }
  47. this.camera.updateProjectionMatrix();
  48. this.camera.updateMatrixWorld();
  49. if ( this.camera.isPerspectiveCamera ) {
  50. tmpPoint.copy( startPoint );
  51. tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  52. tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  53. endPoint.x = Math.max( startPoint.x, endPoint.x );
  54. endPoint.y = Math.min( startPoint.y, endPoint.y );
  55. vecNear.setFromMatrixPosition( this.camera.matrixWorld );
  56. vecTopLeft.copy( tmpPoint );
  57. vecTopRight.set( endPoint.x, tmpPoint.y, 0 );
  58. vecDownRight.copy( endPoint );
  59. vecDownLeft.set( tmpPoint.x, endPoint.y, 0 );
  60. vecTopLeft.unproject( this.camera );
  61. vecTopRight.unproject( this.camera );
  62. vecDownRight.unproject( this.camera );
  63. vecDownLeft.unproject( this.camera );
  64. vectemp1.copy( vecTopLeft ).sub( vecNear );
  65. vectemp2.copy( vecTopRight ).sub( vecNear );
  66. vectemp3.copy( vecDownRight ).sub( vecNear );
  67. vectemp1.normalize();
  68. vectemp2.normalize();
  69. vectemp3.normalize();
  70. vectemp1.multiplyScalar( this.deep );
  71. vectemp2.multiplyScalar( this.deep );
  72. vectemp3.multiplyScalar( this.deep );
  73. vectemp1.add( vecNear );
  74. vectemp2.add( vecNear );
  75. vectemp3.add( vecNear );
  76. var planes = frustum.planes;
  77. planes[ 0 ].setFromCoplanarPoints( vecNear, vecTopLeft, vecTopRight );
  78. planes[ 1 ].setFromCoplanarPoints( vecNear, vecTopRight, vecDownRight );
  79. planes[ 2 ].setFromCoplanarPoints( vecDownRight, vecDownLeft, vecNear );
  80. planes[ 3 ].setFromCoplanarPoints( vecDownLeft, vecTopLeft, vecNear );
  81. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  82. planes[ 5 ].setFromCoplanarPoints( vectemp3, vectemp2, vectemp1 );
  83. planes[ 5 ].normal.multiplyScalar( - 1 );
  84. } else if ( this.camera.isOrthographicCamera ) {
  85. var left = Math.min( startPoint.x, endPoint.x );
  86. var top = Math.max( startPoint.y, endPoint.y );
  87. var right = Math.max( startPoint.x, endPoint.x );
  88. var down = Math.min( startPoint.y, endPoint.y );
  89. vecTopLeft.set( left, top, - 1 );
  90. vecTopRight.set( right, top, - 1 );
  91. vecDownRight.set( right, down, - 1 );
  92. vecDownLeft.set( left, down, - 1 );
  93. vecFarTopLeft.set( left, top, 1 );
  94. vecFarTopRight.set( right, top, 1 );
  95. vecFarDownRight.set( right, down, 1 );
  96. vecFarDownLeft.set( left, down, 1 );
  97. vecTopLeft.unproject( this.camera );
  98. vecTopRight.unproject( this.camera );
  99. vecDownRight.unproject( this.camera );
  100. vecDownLeft.unproject( this.camera );
  101. vecFarTopLeft.unproject( this.camera );
  102. vecFarTopRight.unproject( this.camera );
  103. vecFarDownRight.unproject( this.camera );
  104. vecFarDownLeft.unproject( this.camera );
  105. var planes = frustum.planes;
  106. planes[ 0 ].setFromCoplanarPoints( vecTopLeft, vecFarTopLeft, vecFarTopRight );
  107. planes[ 1 ].setFromCoplanarPoints( vecTopRight, vecFarTopRight, vecFarDownRight );
  108. planes[ 2 ].setFromCoplanarPoints( vecFarDownRight, vecFarDownLeft, vecDownLeft );
  109. planes[ 3 ].setFromCoplanarPoints( vecFarDownLeft, vecFarTopLeft, vecTopLeft );
  110. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  111. planes[ 5 ].setFromCoplanarPoints( vecFarDownRight, vecFarTopRight, vecFarTopLeft );
  112. planes[ 5 ].normal.multiplyScalar( - 1 );
  113. } else {
  114. console.error( 'THREE.SelectionBox: Unsupported camera type.' );
  115. }
  116. };
  117. SelectionBox.prototype.searchChildInFrustum = function ( frustum, object ) {
  118. if ( object.isMesh || object.isLine || object.isPoints ) {
  119. if ( object.material !== undefined ) {
  120. if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
  121. center.copy( object.geometry.boundingSphere.center );
  122. center.applyMatrix4( object.matrixWorld );
  123. if ( frustum.containsPoint( center ) ) {
  124. this.collection.push( object );
  125. }
  126. }
  127. }
  128. if ( object.children.length > 0 ) {
  129. for ( var x = 0; x < object.children.length; x ++ ) {
  130. this.searchChildInFrustum( frustum, object.children[ x ] );
  131. }
  132. }
  133. };
  134. return SelectionBox;
  135. } )();