2
0

SelectionBox.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * This is a class to check whether objects are in a selection area in 3D space
  3. */
  4. THREE.SelectionBox = ( function () {
  5. var frustum = new THREE.Frustum();
  6. var center = new THREE.Vector3();
  7. var tmpPoint = new THREE.Vector3();
  8. var vecNear = new THREE.Vector3();
  9. var vecTopLeft = new THREE.Vector3();
  10. var vecTopRight = new THREE.Vector3();
  11. var vecDownRight = new THREE.Vector3();
  12. var vecDownLeft = new THREE.Vector3();
  13. var vecFarTopLeft = new THREE.Vector3();
  14. var vecFarTopRight = new THREE.Vector3();
  15. var vecFarDownRight = new THREE.Vector3();
  16. var vecFarDownLeft = new THREE.Vector3();
  17. var vectemp1 = new THREE.Vector3();
  18. var vectemp2 = new THREE.Vector3();
  19. var vectemp3 = new THREE.Vector3();
  20. function SelectionBox( camera, scene, deep ) {
  21. this.camera = camera;
  22. this.scene = scene;
  23. this.startPoint = new THREE.Vector3();
  24. this.endPoint = new THREE.Vector3();
  25. this.collection = [];
  26. this.deep = deep || Number.MAX_VALUE;
  27. }
  28. SelectionBox.prototype.select = function ( startPoint, endPoint ) {
  29. this.startPoint = startPoint || this.startPoint;
  30. this.endPoint = endPoint || this.endPoint;
  31. this.collection = [];
  32. this.updateFrustum( this.startPoint, this.endPoint );
  33. this.searchChildInFrustum( frustum, this.scene );
  34. return this.collection;
  35. };
  36. SelectionBox.prototype.updateFrustum = function ( startPoint, endPoint ) {
  37. startPoint = startPoint || this.startPoint;
  38. endPoint = endPoint || this.endPoint;
  39. // Avoid invalid frustum
  40. if ( startPoint.x === endPoint.x ) {
  41. endPoint.x += Number.EPSILON;
  42. }
  43. if ( startPoint.y === endPoint.y ) {
  44. endPoint.y += Number.EPSILON;
  45. }
  46. this.camera.updateProjectionMatrix();
  47. this.camera.updateMatrixWorld();
  48. if ( this.camera.isPerspectiveCamera ) {
  49. tmpPoint.copy( startPoint );
  50. tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  51. tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  52. endPoint.x = Math.max( startPoint.x, endPoint.x );
  53. endPoint.y = Math.min( startPoint.y, endPoint.y );
  54. vecNear.setFromMatrixPosition( this.camera.matrixWorld );
  55. vecTopLeft.copy( tmpPoint );
  56. vecTopRight.set( endPoint.x, tmpPoint.y, 0 );
  57. vecDownRight.copy( endPoint );
  58. vecDownLeft.set( tmpPoint.x, endPoint.y, 0 );
  59. vecTopLeft.unproject( this.camera );
  60. vecTopRight.unproject( this.camera );
  61. vecDownRight.unproject( this.camera );
  62. vecDownLeft.unproject( this.camera );
  63. vectemp1.copy( vecTopLeft ).sub( vecNear );
  64. vectemp2.copy( vecTopRight ).sub( vecNear );
  65. vectemp3.copy( vecDownRight ).sub( vecNear );
  66. vectemp1.normalize();
  67. vectemp2.normalize();
  68. vectemp3.normalize();
  69. vectemp1.multiplyScalar( this.deep );
  70. vectemp2.multiplyScalar( this.deep );
  71. vectemp3.multiplyScalar( this.deep );
  72. vectemp1.add( vecNear );
  73. vectemp2.add( vecNear );
  74. vectemp3.add( vecNear );
  75. var planes = frustum.planes;
  76. planes[ 0 ].setFromCoplanarPoints( vecNear, vecTopLeft, vecTopRight );
  77. planes[ 1 ].setFromCoplanarPoints( vecNear, vecTopRight, vecDownRight );
  78. planes[ 2 ].setFromCoplanarPoints( vecDownRight, vecDownLeft, vecNear );
  79. planes[ 3 ].setFromCoplanarPoints( vecDownLeft, vecTopLeft, vecNear );
  80. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  81. planes[ 5 ].setFromCoplanarPoints( vectemp3, vectemp2, vectemp1 );
  82. planes[ 5 ].normal.multiplyScalar( - 1 );
  83. } else if ( this.camera.isOrthographicCamera ) {
  84. var left = Math.min( startPoint.x, endPoint.x );
  85. var top = Math.max( startPoint.y, endPoint.y );
  86. var right = Math.max( startPoint.x, endPoint.x );
  87. var down = Math.min( startPoint.y, endPoint.y );
  88. vecTopLeft.set( left, top, - 1 );
  89. vecTopRight.set( right, top, - 1 );
  90. vecDownRight.set( right, down, - 1 );
  91. vecDownLeft.set( left, down, - 1 );
  92. vecFarTopLeft.set( left, top, 1 );
  93. vecFarTopRight.set( right, top, 1 );
  94. vecFarDownRight.set( right, down, 1 );
  95. vecFarDownLeft.set( left, down, 1 );
  96. vecTopLeft.unproject( this.camera );
  97. vecTopRight.unproject( this.camera );
  98. vecDownRight.unproject( this.camera );
  99. vecDownLeft.unproject( this.camera );
  100. vecFarTopLeft.unproject( this.camera );
  101. vecFarTopRight.unproject( this.camera );
  102. vecFarDownRight.unproject( this.camera );
  103. vecFarDownLeft.unproject( this.camera );
  104. var planes = frustum.planes;
  105. planes[ 0 ].setFromCoplanarPoints( vecTopLeft, vecFarTopLeft, vecFarTopRight );
  106. planes[ 1 ].setFromCoplanarPoints( vecTopRight, vecFarTopRight, vecFarDownRight );
  107. planes[ 2 ].setFromCoplanarPoints( vecFarDownRight, vecFarDownLeft, vecDownLeft );
  108. planes[ 3 ].setFromCoplanarPoints( vecFarDownLeft, vecFarTopLeft, vecTopLeft );
  109. planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
  110. planes[ 5 ].setFromCoplanarPoints( vecFarDownRight, vecFarTopRight, vecFarTopLeft );
  111. planes[ 5 ].normal.multiplyScalar( - 1 );
  112. } else {
  113. console.error( 'THREE.SelectionBox: Unsupported camera type.' );
  114. }
  115. };
  116. SelectionBox.prototype.searchChildInFrustum = function ( frustum, object ) {
  117. if ( object.isMesh || object.isLine || object.isPoints ) {
  118. if ( object.material !== undefined ) {
  119. if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
  120. center.copy( object.geometry.boundingSphere.center );
  121. center.applyMatrix4( object.matrixWorld );
  122. if ( frustum.containsPoint( center ) ) {
  123. this.collection.push( object );
  124. }
  125. }
  126. }
  127. if ( object.children.length > 0 ) {
  128. for ( var x = 0; x < object.children.length; x ++ ) {
  129. this.searchChildInFrustum( frustum, object.children[ x ] );
  130. }
  131. }
  132. };
  133. return SelectionBox;
  134. } )();