SelectionBox.js 5.9 KB

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