SelectionBox.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. this.camera.updateProjectionMatrix();
  42. this.camera.updateMatrixWorld();
  43. if ( this.camera.isPerspectiveCamera ) {
  44. tmpPoint.copy( startPoint );
  45. tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  46. tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  47. endPoint.x = Math.max( startPoint.x, endPoint.x );
  48. endPoint.y = Math.min( startPoint.y, endPoint.y );
  49. vecNear.copy( this.camera.position );
  50. vecTopLeft.copy( tmpPoint );
  51. vecTopRight.set( endPoint.x, tmpPoint.y, 0 );
  52. vecDownRight.copy( endPoint );
  53. vecDownLeft.set( tmpPoint.x, endPoint.y, 0 );
  54. vecTopLeft.unproject( this.camera );
  55. vecTopRight.unproject( this.camera );
  56. vecDownRight.unproject( this.camera );
  57. vecDownLeft.unproject( this.camera );
  58. vectemp1.copy( vecTopLeft ).sub( vecNear );
  59. vectemp2.copy( vecTopRight ).sub( vecNear );
  60. vectemp3.copy( vecDownRight ).sub( vecNear );
  61. vectemp1.normalize();
  62. vectemp2.normalize();
  63. vectemp3.normalize();
  64. vectemp1.multiplyScalar( this.deep );
  65. vectemp2.multiplyScalar( this.deep );
  66. vectemp3.multiplyScalar( this.deep );
  67. vectemp1.add( vecNear );
  68. vectemp2.add( vecNear );
  69. vectemp3.add( vecNear );
  70. var planes = frustum.planes;
  71. planes[ 0 ].setFromCoplanarPoints( vecNear, vecTopLeft, vecTopRight );
  72. planes[ 1 ].setFromCoplanarPoints( vecNear, vecTopRight, vecDownRight );
  73. planes[ 2 ].setFromCoplanarPoints( vecDownRight, vecDownLeft, vecNear );
  74. planes[ 3 ].setFromCoplanarPoints( vecDownLeft, vecTopLeft, vecNear );
  75. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  76. planes[ 5 ].setFromCoplanarPoints( vectemp3, vectemp2, vectemp1 );
  77. planes[ 5 ].normal.multiplyScalar( - 1 );
  78. } else if ( this.camera.isOrthographicCamera ) {
  79. if ( startPoint.equals( endPoint ) ) endPoint.addScalar( Number.EPSILON ); // avoid invalid frustum
  80. var left = Math.min( startPoint.x, endPoint.x );
  81. var top = Math.max( startPoint.y, endPoint.y );
  82. var right = Math.max( startPoint.x, endPoint.x );
  83. var down = Math.min( startPoint.y, endPoint.y );
  84. vecTopLeft.set( left, top, - 1 );
  85. vecTopRight.set( right, top, - 1 );
  86. vecDownRight.set( right, down, - 1 );
  87. vecDownLeft.set( left, down, - 1 );
  88. vecFarTopLeft.set( left, top, 1 );
  89. vecFarTopRight.set( right, top, 1 );
  90. vecFarDownRight.set( right, down, 1 );
  91. vecFarDownLeft.set( left, down, 1 );
  92. vecTopLeft.unproject( this.camera );
  93. vecTopRight.unproject( this.camera );
  94. vecDownRight.unproject( this.camera );
  95. vecDownLeft.unproject( this.camera );
  96. vecFarTopLeft.unproject( this.camera );
  97. vecFarTopRight.unproject( this.camera );
  98. vecFarDownRight.unproject( this.camera );
  99. vecFarDownLeft.unproject( this.camera );
  100. var planes = frustum.planes;
  101. planes[ 0 ].setFromCoplanarPoints( vecTopLeft, vecFarTopLeft, vecFarTopRight );
  102. planes[ 1 ].setFromCoplanarPoints( vecTopRight, vecFarTopRight, vecFarDownRight );
  103. planes[ 2 ].setFromCoplanarPoints( vecFarDownRight, vecFarDownLeft, vecDownLeft );
  104. planes[ 3 ].setFromCoplanarPoints( vecFarDownLeft, vecFarTopLeft, vecTopLeft );
  105. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  106. planes[ 5 ].setFromCoplanarPoints( vecFarDownRight, vecFarTopRight, vecFarTopLeft );
  107. planes[ 5 ].normal.multiplyScalar( - 1 );
  108. } else {
  109. console.error( 'THREE.SelectionBox: Unsupported camera type.' );
  110. }
  111. };
  112. SelectionBox.prototype.searchChildInFrustum = function ( frustum, object ) {
  113. if ( object.isMesh || object.isLine || object.isPoints ) {
  114. if ( object.material !== undefined ) {
  115. if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
  116. center.copy( object.geometry.boundingSphere.center );
  117. center.applyMatrix4( object.matrixWorld );
  118. if ( frustum.containsPoint( center ) ) {
  119. this.collection.push( object );
  120. }
  121. }
  122. }
  123. if ( object.children.length > 0 ) {
  124. for ( var x = 0; x < object.children.length; x ++ ) {
  125. this.searchChildInFrustum( frustum, object.children[ x ] );
  126. }
  127. }
  128. };
  129. return SelectionBox;
  130. } )();