SelectionBox.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import {
  2. Frustum,
  3. Vector3,
  4. Matrix4,
  5. Quaternion,
  6. } from 'three';
  7. const _frustum = new Frustum();
  8. const _center = new Vector3();
  9. const _tmpPoint = new Vector3();
  10. const _vecNear = new Vector3();
  11. const _vecTopLeft = new Vector3();
  12. const _vecTopRight = new Vector3();
  13. const _vecDownRight = new Vector3();
  14. const _vecDownLeft = new Vector3();
  15. const _vecFarTopLeft = new Vector3();
  16. const _vecFarTopRight = new Vector3();
  17. const _vecFarDownRight = new Vector3();
  18. const _vecFarDownLeft = new Vector3();
  19. const _vectemp1 = new Vector3();
  20. const _vectemp2 = new Vector3();
  21. const _vectemp3 = new Vector3();
  22. const _matrix = new Matrix4();
  23. const _quaternion = new Quaternion();
  24. const _scale = new Vector3();
  25. /**
  26. * This class can be used to select 3D objects in a scene with a selection box.
  27. * It is recommended to visualize the selected area with the help of {@link SelectionHelper}.
  28. *
  29. * ```js
  30. * const selectionBox = new SelectionBox( camera, scene );
  31. * const selectedObjects = selectionBox.select( startPoint, endPoint );
  32. * ```
  33. */
  34. class SelectionBox {
  35. /**
  36. * Constructs a new selection box.
  37. *
  38. * @param {Camera} camera - The camera the scene is rendered with.
  39. * @param {Scene} scene - The scene.
  40. * @param {number} [deep=Number.MAX_VALUE] - How deep the selection frustum of perspective cameras should extend.
  41. */
  42. constructor( camera, scene, deep = Number.MAX_VALUE ) {
  43. /**
  44. * The camera the scene is rendered with.
  45. *
  46. * @type {Camera}
  47. */
  48. this.camera = camera;
  49. /**
  50. * The camera the scene is rendered with.
  51. *
  52. * @type {Scene}
  53. */
  54. this.scene = scene;
  55. /**
  56. * The start point of the selection.
  57. *
  58. * @type {Vector3}
  59. */
  60. this.startPoint = new Vector3();
  61. /**
  62. * The end point of the selection.
  63. *
  64. * @type {Vector3}
  65. */
  66. this.endPoint = new Vector3();
  67. /**
  68. * The selected 3D objects.
  69. *
  70. * @type {Array<Object3D>}
  71. */
  72. this.collection = [];
  73. /**
  74. * The selected instance IDs of instanced meshes.
  75. *
  76. * @type {Object}
  77. */
  78. this.instances = {};
  79. /**
  80. * How deep the selection frustum of perspective cameras should extend.
  81. *
  82. * @type {number}
  83. * @default Number.MAX_VALUE
  84. */
  85. this.deep = deep;
  86. }
  87. /**
  88. * This method selects 3D objects in the scene based on the given start
  89. * and end point. If no parameters are provided, the method uses the start
  90. * and end values of the respective members.
  91. *
  92. * @param {Vector3} [startPoint] - The start point.
  93. * @param {Vector3} [endPoint] - The end point.
  94. * @return {Array<Object3D>} The selected 3D objects.
  95. */
  96. select( startPoint, endPoint ) {
  97. this.startPoint = startPoint || this.startPoint;
  98. this.endPoint = endPoint || this.endPoint;
  99. this.collection = [];
  100. this._updateFrustum( this.startPoint, this.endPoint );
  101. this._searchChildInFrustum( _frustum, this.scene );
  102. return this.collection;
  103. }
  104. // private
  105. _updateFrustum( startPoint, endPoint ) {
  106. startPoint = startPoint || this.startPoint;
  107. endPoint = endPoint || this.endPoint;
  108. // Avoid invalid frustum
  109. if ( startPoint.x === endPoint.x ) {
  110. endPoint.x += Number.EPSILON;
  111. }
  112. if ( startPoint.y === endPoint.y ) {
  113. endPoint.y += Number.EPSILON;
  114. }
  115. this.camera.updateProjectionMatrix();
  116. this.camera.updateMatrixWorld();
  117. if ( this.camera.isPerspectiveCamera ) {
  118. _tmpPoint.copy( startPoint );
  119. _tmpPoint.x = Math.min( startPoint.x, endPoint.x );
  120. _tmpPoint.y = Math.max( startPoint.y, endPoint.y );
  121. endPoint.x = Math.max( startPoint.x, endPoint.x );
  122. endPoint.y = Math.min( startPoint.y, endPoint.y );
  123. _vecNear.setFromMatrixPosition( this.camera.matrixWorld );
  124. _vecTopLeft.copy( _tmpPoint );
  125. _vecTopRight.set( endPoint.x, _tmpPoint.y, 0 );
  126. _vecDownRight.copy( endPoint );
  127. _vecDownLeft.set( _tmpPoint.x, endPoint.y, 0 );
  128. _vecTopLeft.unproject( this.camera );
  129. _vecTopRight.unproject( this.camera );
  130. _vecDownRight.unproject( this.camera );
  131. _vecDownLeft.unproject( this.camera );
  132. _vectemp1.copy( _vecTopLeft ).sub( _vecNear );
  133. _vectemp2.copy( _vecTopRight ).sub( _vecNear );
  134. _vectemp3.copy( _vecDownRight ).sub( _vecNear );
  135. _vectemp1.normalize();
  136. _vectemp2.normalize();
  137. _vectemp3.normalize();
  138. _vectemp1.multiplyScalar( this.deep );
  139. _vectemp2.multiplyScalar( this.deep );
  140. _vectemp3.multiplyScalar( this.deep );
  141. _vectemp1.add( _vecNear );
  142. _vectemp2.add( _vecNear );
  143. _vectemp3.add( _vecNear );
  144. const planes = _frustum.planes;
  145. planes[ 0 ].setFromCoplanarPoints( _vecNear, _vecTopLeft, _vecTopRight );
  146. planes[ 1 ].setFromCoplanarPoints( _vecNear, _vecTopRight, _vecDownRight );
  147. planes[ 2 ].setFromCoplanarPoints( _vecDownRight, _vecDownLeft, _vecNear );
  148. planes[ 3 ].setFromCoplanarPoints( _vecDownLeft, _vecTopLeft, _vecNear );
  149. planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
  150. planes[ 5 ].setFromCoplanarPoints( _vectemp3, _vectemp2, _vectemp1 );
  151. planes[ 5 ].normal.multiplyScalar( - 1 );
  152. } else if ( this.camera.isOrthographicCamera ) {
  153. const left = Math.min( startPoint.x, endPoint.x );
  154. const top = Math.max( startPoint.y, endPoint.y );
  155. const right = Math.max( startPoint.x, endPoint.x );
  156. const down = Math.min( startPoint.y, endPoint.y );
  157. _vecTopLeft.set( left, top, - 1 );
  158. _vecTopRight.set( right, top, - 1 );
  159. _vecDownRight.set( right, down, - 1 );
  160. _vecDownLeft.set( left, down, - 1 );
  161. _vecFarTopLeft.set( left, top, 1 );
  162. _vecFarTopRight.set( right, top, 1 );
  163. _vecFarDownRight.set( right, down, 1 );
  164. _vecFarDownLeft.set( left, down, 1 );
  165. _vecTopLeft.unproject( this.camera );
  166. _vecTopRight.unproject( this.camera );
  167. _vecDownRight.unproject( this.camera );
  168. _vecDownLeft.unproject( this.camera );
  169. _vecFarTopLeft.unproject( this.camera );
  170. _vecFarTopRight.unproject( this.camera );
  171. _vecFarDownRight.unproject( this.camera );
  172. _vecFarDownLeft.unproject( this.camera );
  173. const planes = _frustum.planes;
  174. planes[ 0 ].setFromCoplanarPoints( _vecTopLeft, _vecFarTopLeft, _vecFarTopRight );
  175. planes[ 1 ].setFromCoplanarPoints( _vecTopRight, _vecFarTopRight, _vecFarDownRight );
  176. planes[ 2 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarDownLeft, _vecDownLeft );
  177. planes[ 3 ].setFromCoplanarPoints( _vecFarDownLeft, _vecFarTopLeft, _vecTopLeft );
  178. planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
  179. planes[ 5 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarTopRight, _vecFarTopLeft );
  180. planes[ 5 ].normal.multiplyScalar( - 1 );
  181. } else {
  182. console.error( 'THREE.SelectionBox: Unsupported camera type.' );
  183. }
  184. }
  185. _searchChildInFrustum( frustum, object ) {
  186. if ( object.isMesh || object.isLine || object.isPoints ) {
  187. if ( object.isInstancedMesh ) {
  188. this.instances[ object.uuid ] = [];
  189. for ( let instanceId = 0; instanceId < object.count; instanceId ++ ) {
  190. object.getMatrixAt( instanceId, _matrix );
  191. _matrix.decompose( _center, _quaternion, _scale );
  192. _center.applyMatrix4( object.matrixWorld );
  193. if ( frustum.containsPoint( _center ) ) {
  194. this.instances[ object.uuid ].push( instanceId );
  195. }
  196. }
  197. } else {
  198. if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
  199. _center.copy( object.geometry.boundingSphere.center );
  200. _center.applyMatrix4( object.matrixWorld );
  201. if ( frustum.containsPoint( _center ) ) {
  202. this.collection.push( object );
  203. }
  204. }
  205. }
  206. if ( object.children.length > 0 ) {
  207. for ( let x = 0; x < object.children.length; x ++ ) {
  208. this._searchChildInFrustum( frustum, object.children[ x ] );
  209. }
  210. }
  211. }
  212. }
  213. export { SelectionBox };
粤ICP备19079148号