SelectionBox.js 3.5 KB

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