SelectionBox.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ( function () {
  2. /**
  3. * This is a class to check whether objects are in a selection area in 3D space
  4. */
  5. const _frustum = new THREE.Frustum();
  6. const _center = new THREE.Vector3();
  7. const _tmpPoint = new THREE.Vector3();
  8. const _vecNear = new THREE.Vector3();
  9. const _vecTopLeft = new THREE.Vector3();
  10. const _vecTopRight = new THREE.Vector3();
  11. const _vecDownRight = new THREE.Vector3();
  12. const _vecDownLeft = new THREE.Vector3();
  13. const _vecFarTopLeft = new THREE.Vector3();
  14. const _vecFarTopRight = new THREE.Vector3();
  15. const _vecFarDownRight = new THREE.Vector3();
  16. const _vecFarDownLeft = new THREE.Vector3();
  17. const _vectemp1 = new THREE.Vector3();
  18. const _vectemp2 = new THREE.Vector3();
  19. const _vectemp3 = new THREE.Vector3();
  20. const _matrix = new THREE.Matrix4();
  21. const _quaternion = new THREE.Quaternion();
  22. const _scale = new THREE.Vector3();
  23. class SelectionBox {
  24. constructor( camera, scene, deep = Number.MAX_VALUE ) {
  25. this.camera = camera;
  26. this.scene = scene;
  27. this.startPoint = new THREE.Vector3();
  28. this.endPoint = new THREE.Vector3();
  29. this.collection = [];
  30. this.instances = {};
  31. this.deep = deep;
  32. }
  33. select( startPoint, endPoint ) {
  34. this.startPoint = startPoint || this.startPoint;
  35. this.endPoint = endPoint || this.endPoint;
  36. this.collection = [];
  37. this.updateFrustum( this.startPoint, this.endPoint );
  38. this.searchChildInFrustum( _frustum, this.scene );
  39. return this.collection;
  40. }
  41. updateFrustum( startPoint, endPoint ) {
  42. startPoint = startPoint || this.startPoint;
  43. endPoint = endPoint || this.endPoint;
  44. // Avoid invalid frustum
  45. if ( startPoint.x === endPoint.x ) {
  46. endPoint.x += Number.EPSILON;
  47. }
  48. if ( startPoint.y === endPoint.y ) {
  49. endPoint.y += Number.EPSILON;
  50. }
  51. this.camera.updateProjectionMatrix();
  52. this.camera.updateMatrixWorld();
  53. if ( this.camera.isPerspectiveCamera ) {
  54. _tmpPoint.copy( startPoint );
  55. _tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  56. _tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  57. endPoint.x = Math.max( startPoint.x, endPoint.x );
  58. endPoint.y = Math.min( startPoint.y, endPoint.y );
  59. _vecNear.setFromMatrixPosition( this.camera.matrixWorld );
  60. _vecTopLeft.copy( _tmpPoint );
  61. _vecTopRight.set( endPoint.x, _tmpPoint.y, 0 );
  62. _vecDownRight.copy( endPoint );
  63. _vecDownLeft.set( _tmpPoint.x, endPoint.y, 0 );
  64. _vecTopLeft.unproject( this.camera );
  65. _vecTopRight.unproject( this.camera );
  66. _vecDownRight.unproject( this.camera );
  67. _vecDownLeft.unproject( this.camera );
  68. _vectemp1.copy( _vecTopLeft ).sub( _vecNear );
  69. _vectemp2.copy( _vecTopRight ).sub( _vecNear );
  70. _vectemp3.copy( _vecDownRight ).sub( _vecNear );
  71. _vectemp1.normalize();
  72. _vectemp2.normalize();
  73. _vectemp3.normalize();
  74. _vectemp1.multiplyScalar( this.deep );
  75. _vectemp2.multiplyScalar( this.deep );
  76. _vectemp3.multiplyScalar( this.deep );
  77. _vectemp1.add( _vecNear );
  78. _vectemp2.add( _vecNear );
  79. _vectemp3.add( _vecNear );
  80. const planes = _frustum.planes;
  81. planes[ 0 ].setFromCoplanarPoints( _vecNear, _vecTopLeft, _vecTopRight );
  82. planes[ 1 ].setFromCoplanarPoints( _vecNear, _vecTopRight, _vecDownRight );
  83. planes[ 2 ].setFromCoplanarPoints( _vecDownRight, _vecDownLeft, _vecNear );
  84. planes[ 3 ].setFromCoplanarPoints( _vecDownLeft, _vecTopLeft, _vecNear );
  85. planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
  86. planes[ 5 ].setFromCoplanarPoints( _vectemp3, _vectemp2, _vectemp1 );
  87. planes[ 5 ].normal.multiplyScalar( - 1 );
  88. } else if ( this.camera.isOrthographicCamera ) {
  89. const left = Math.min( startPoint.x, endPoint.x );
  90. const top = Math.max( startPoint.y, endPoint.y );
  91. const right = Math.max( startPoint.x, endPoint.x );
  92. const down = Math.min( startPoint.y, endPoint.y );
  93. _vecTopLeft.set( left, top, - 1 );
  94. _vecTopRight.set( right, top, - 1 );
  95. _vecDownRight.set( right, down, - 1 );
  96. _vecDownLeft.set( left, down, - 1 );
  97. _vecFarTopLeft.set( left, top, 1 );
  98. _vecFarTopRight.set( right, top, 1 );
  99. _vecFarDownRight.set( right, down, 1 );
  100. _vecFarDownLeft.set( left, down, 1 );
  101. _vecTopLeft.unproject( this.camera );
  102. _vecTopRight.unproject( this.camera );
  103. _vecDownRight.unproject( this.camera );
  104. _vecDownLeft.unproject( this.camera );
  105. _vecFarTopLeft.unproject( this.camera );
  106. _vecFarTopRight.unproject( this.camera );
  107. _vecFarDownRight.unproject( this.camera );
  108. _vecFarDownLeft.unproject( this.camera );
  109. const planes = _frustum.planes;
  110. planes[ 0 ].setFromCoplanarPoints( _vecTopLeft, _vecFarTopLeft, _vecFarTopRight );
  111. planes[ 1 ].setFromCoplanarPoints( _vecTopRight, _vecFarTopRight, _vecFarDownRight );
  112. planes[ 2 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarDownLeft, _vecDownLeft );
  113. planes[ 3 ].setFromCoplanarPoints( _vecFarDownLeft, _vecFarTopLeft, _vecTopLeft );
  114. planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
  115. planes[ 5 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarTopRight, _vecFarTopLeft );
  116. planes[ 5 ].normal.multiplyScalar( - 1 );
  117. } else {
  118. console.error( 'THREE.SelectionBox: Unsupported camera type.' );
  119. }
  120. }
  121. searchChildInFrustum( frustum, object ) {
  122. if ( object.isMesh || object.isLine || object.isPoints ) {
  123. if ( object.isInstancedMesh ) {
  124. this.instances[ object.uuid ] = [];
  125. for ( let instanceId = 0; instanceId < object.count; instanceId ++ ) {
  126. object.getMatrixAt( instanceId, _matrix );
  127. _matrix.decompose( _center, _quaternion, _scale );
  128. _center.applyMatrix4( object.matrixWorld );
  129. if ( frustum.containsPoint( _center ) ) {
  130. this.instances[ object.uuid ].push( instanceId );
  131. }
  132. }
  133. } else {
  134. if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
  135. _center.copy( object.geometry.boundingSphere.center );
  136. _center.applyMatrix4( object.matrixWorld );
  137. if ( frustum.containsPoint( _center ) ) {
  138. this.collection.push( object );
  139. }
  140. }
  141. }
  142. if ( object.children.length > 0 ) {
  143. for ( let x = 0; x < object.children.length; x ++ ) {
  144. this.searchChildInFrustum( frustum, object.children[ x ] );
  145. }
  146. }
  147. }
  148. }
  149. THREE.SelectionBox = SelectionBox;
  150. } )();