SelectionBox.js 6.0 KB

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