SelectionBox.js 8.2 KB

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