SelectionBox.js 3.5 KB

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