SelectionBox.js 5.6 KB

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