SelectionBox.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. function SelectionBox( camera, scene, deep ) {
  13. this.camera = camera;
  14. this.scene = scene;
  15. this.startPoint = new Vector3();
  16. this.endPoint = new Vector3();
  17. this.collection = [];
  18. this.deep = deep || Number.MAX_VALUE;
  19. }
  20. SelectionBox.prototype.select = function ( startPoint, endPoint ) {
  21. this.startPoint = startPoint || this.startPoint;
  22. this.endPoint = endPoint || this.endPoint;
  23. this.collection = [];
  24. this.updateFrustum( this.startPoint, this.endPoint );
  25. this.searchChildInFrustum( frustum, this.scene );
  26. return this.collection;
  27. };
  28. SelectionBox.prototype.updateFrustum = function ( startPoint, endPoint ) {
  29. startPoint = startPoint || this.startPoint;
  30. endPoint = endPoint || this.endPoint;
  31. this.camera.updateProjectionMatrix();
  32. this.camera.updateMatrixWorld();
  33. var tmpPoint = startPoint.clone();
  34. tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  35. tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  36. endPoint.x = Math.max( startPoint.x, endPoint.x );
  37. endPoint.y = Math.min( startPoint.y, endPoint.y );
  38. var vecNear = this.camera.position.clone();
  39. var vecTopLeft = tmpPoint.clone();
  40. var vecTopRight = new Vector3( endPoint.x, tmpPoint.y, 0 );
  41. var vecDownRight = endPoint.clone();
  42. var vecDownLeft = new Vector3( tmpPoint.x, endPoint.y, 0 );
  43. vecTopLeft.unproject( this.camera );
  44. vecTopRight.unproject( this.camera );
  45. vecDownRight.unproject( this.camera );
  46. vecDownLeft.unproject( this.camera );
  47. var vectemp1 = vecTopLeft.clone().sub( vecNear );
  48. var vectemp2 = vecTopRight.clone().sub( vecNear );
  49. var vectemp3 = vecDownRight.clone().sub( vecNear );
  50. vectemp1.normalize();
  51. vectemp2.normalize();
  52. vectemp3.normalize();
  53. vectemp1.multiplyScalar( this.deep );
  54. vectemp2.multiplyScalar( this.deep );
  55. vectemp3.multiplyScalar( this.deep );
  56. vectemp1.add( vecNear );
  57. vectemp2.add( vecNear );
  58. vectemp3.add( vecNear );
  59. var planes = frustum.planes;
  60. planes[ 0 ].setFromCoplanarPoints( vecNear, vecTopLeft, vecTopRight );
  61. planes[ 1 ].setFromCoplanarPoints( vecNear, vecTopRight, vecDownRight );
  62. planes[ 2 ].setFromCoplanarPoints( vecDownRight, vecDownLeft, vecNear );
  63. planes[ 3 ].setFromCoplanarPoints( vecDownLeft, vecTopLeft, vecNear );
  64. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  65. planes[ 5 ].setFromCoplanarPoints( vectemp3, vectemp2, vectemp1 );
  66. planes[ 5 ].normal.multiplyScalar( - 1 );
  67. };
  68. SelectionBox.prototype.searchChildInFrustum = function ( frustum, object ) {
  69. if ( object.isMesh ) {
  70. if ( object.material !== undefined ) {
  71. object.geometry.computeBoundingSphere();
  72. center.copy( object.geometry.boundingSphere.center );
  73. center.applyMatrix4( object.matrixWorld );
  74. if ( frustum.containsPoint( center ) ) {
  75. this.collection.push( object );
  76. }
  77. }
  78. }
  79. if ( object.children.length > 0 ) {
  80. for ( var x = 0; x < object.children.length; x ++ ) {
  81. this.searchChildInFrustum( frustum, object.children[ x ] );
  82. }
  83. }
  84. };
  85. return SelectionBox;
  86. } )();
  87. export { SelectionBox };