SelectionBox.js 3.4 KB

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