SelectionBox.js 5.5 KB

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