SelectionBox.js 3.2 KB

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