|
@@ -7,53 +7,53 @@ import {
|
|
|
* This is a class to check whether objects are in a selection area in 3D space
|
|
|
*/
|
|
|
|
|
|
-var SelectionBox = ( function () {
|
|
|
+const _frustum = new Frustum();
|
|
|
+const _center = new Vector3();
|
|
|
|
|
|
- var frustum = new Frustum();
|
|
|
- var center = new Vector3();
|
|
|
+const _tmpPoint = new Vector3();
|
|
|
|
|
|
- var tmpPoint = new Vector3();
|
|
|
+const _vecNear = new Vector3();
|
|
|
+const _vecTopLeft = new Vector3();
|
|
|
+const _vecTopRight = new Vector3();
|
|
|
+const _vecDownRight = new Vector3();
|
|
|
+const _vecDownLeft = new Vector3();
|
|
|
|
|
|
- var vecNear = new Vector3();
|
|
|
- var vecTopLeft = new Vector3();
|
|
|
- var vecTopRight = new Vector3();
|
|
|
- var vecDownRight = new Vector3();
|
|
|
- var vecDownLeft = new Vector3();
|
|
|
+const _vecFarTopLeft = new Vector3();
|
|
|
+const _vecFarTopRight = new Vector3();
|
|
|
+const _vecFarDownRight = new Vector3();
|
|
|
+const _vecFarDownLeft = new Vector3();
|
|
|
|
|
|
- var vecFarTopLeft = new Vector3();
|
|
|
- var vecFarTopRight = new Vector3();
|
|
|
- var vecFarDownRight = new Vector3();
|
|
|
- var vecFarDownLeft = new Vector3();
|
|
|
+const _vectemp1 = new Vector3();
|
|
|
+const _vectemp2 = new Vector3();
|
|
|
+const _vectemp3 = new Vector3();
|
|
|
|
|
|
- var vectemp1 = new Vector3();
|
|
|
- var vectemp2 = new Vector3();
|
|
|
- var vectemp3 = new Vector3();
|
|
|
+class SelectionBox {
|
|
|
|
|
|
- function SelectionBox( camera, scene, deep ) {
|
|
|
+ constructor( camera, scene, deep = Number.MAX_VALUE ) {
|
|
|
|
|
|
this.camera = camera;
|
|
|
this.scene = scene;
|
|
|
this.startPoint = new Vector3();
|
|
|
this.endPoint = new Vector3();
|
|
|
this.collection = [];
|
|
|
- this.deep = deep || Number.MAX_VALUE;
|
|
|
+ this.deep = deep;
|
|
|
|
|
|
}
|
|
|
|
|
|
- SelectionBox.prototype.select = function ( startPoint, endPoint ) {
|
|
|
+ select( startPoint, endPoint ) {
|
|
|
|
|
|
this.startPoint = startPoint || this.startPoint;
|
|
|
this.endPoint = endPoint || this.endPoint;
|
|
|
this.collection = [];
|
|
|
|
|
|
this.updateFrustum( this.startPoint, this.endPoint );
|
|
|
- this.searchChildInFrustum( frustum, this.scene );
|
|
|
+ this.searchChildInFrustum( _frustum, this.scene );
|
|
|
|
|
|
return this.collection;
|
|
|
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
- SelectionBox.prototype.updateFrustum = function ( startPoint, endPoint ) {
|
|
|
+ updateFrustum( startPoint, endPoint ) {
|
|
|
|
|
|
startPoint = startPoint || this.startPoint;
|
|
|
endPoint = endPoint || this.endPoint;
|
|
@@ -77,82 +77,82 @@ var SelectionBox = ( function () {
|
|
|
|
|
|
if ( this.camera.isPerspectiveCamera ) {
|
|
|
|
|
|
- tmpPoint.copy( startPoint );
|
|
|
- tmpPoint.x = Math.min( startPoint.x, endPoint.x );
|
|
|
- tmpPoint.y = Math.max( startPoint.y, endPoint.y );
|
|
|
+ _tmpPoint.copy( startPoint );
|
|
|
+ _tmpPoint.x = Math.min( startPoint.x, endPoint.x );
|
|
|
+ _tmpPoint.y = Math.max( startPoint.y, endPoint.y );
|
|
|
endPoint.x = Math.max( startPoint.x, endPoint.x );
|
|
|
endPoint.y = Math.min( startPoint.y, endPoint.y );
|
|
|
|
|
|
- vecNear.setFromMatrixPosition( this.camera.matrixWorld );
|
|
|
- vecTopLeft.copy( tmpPoint );
|
|
|
- vecTopRight.set( endPoint.x, tmpPoint.y, 0 );
|
|
|
- vecDownRight.copy( endPoint );
|
|
|
- vecDownLeft.set( tmpPoint.x, endPoint.y, 0 );
|
|
|
-
|
|
|
- vecTopLeft.unproject( this.camera );
|
|
|
- vecTopRight.unproject( this.camera );
|
|
|
- vecDownRight.unproject( this.camera );
|
|
|
- vecDownLeft.unproject( this.camera );
|
|
|
-
|
|
|
- vectemp1.copy( vecTopLeft ).sub( vecNear );
|
|
|
- vectemp2.copy( vecTopRight ).sub( vecNear );
|
|
|
- vectemp3.copy( vecDownRight ).sub( vecNear );
|
|
|
- vectemp1.normalize();
|
|
|
- vectemp2.normalize();
|
|
|
- vectemp3.normalize();
|
|
|
-
|
|
|
- vectemp1.multiplyScalar( this.deep );
|
|
|
- vectemp2.multiplyScalar( this.deep );
|
|
|
- vectemp3.multiplyScalar( this.deep );
|
|
|
- vectemp1.add( vecNear );
|
|
|
- vectemp2.add( vecNear );
|
|
|
- vectemp3.add( vecNear );
|
|
|
-
|
|
|
- var planes = frustum.planes;
|
|
|
-
|
|
|
- planes[ 0 ].setFromCoplanarPoints( vecNear, vecTopLeft, vecTopRight );
|
|
|
- planes[ 1 ].setFromCoplanarPoints( vecNear, vecTopRight, vecDownRight );
|
|
|
- planes[ 2 ].setFromCoplanarPoints( vecDownRight, vecDownLeft, vecNear );
|
|
|
- planes[ 3 ].setFromCoplanarPoints( vecDownLeft, vecTopLeft, vecNear );
|
|
|
- planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
|
|
|
- planes[ 5 ].setFromCoplanarPoints( vectemp3, vectemp2, vectemp1 );
|
|
|
+ _vecNear.setFromMatrixPosition( this.camera.matrixWorld );
|
|
|
+ _vecTopLeft.copy( _tmpPoint );
|
|
|
+ _vecTopRight.set( endPoint.x, _tmpPoint.y, 0 );
|
|
|
+ _vecDownRight.copy( endPoint );
|
|
|
+ _vecDownLeft.set( _tmpPoint.x, endPoint.y, 0 );
|
|
|
+
|
|
|
+ _vecTopLeft.unproject( this.camera );
|
|
|
+ _vecTopRight.unproject( this.camera );
|
|
|
+ _vecDownRight.unproject( this.camera );
|
|
|
+ _vecDownLeft.unproject( this.camera );
|
|
|
+
|
|
|
+ _vectemp1.copy( _vecTopLeft ).sub( _vecNear );
|
|
|
+ _vectemp2.copy( _vecTopRight ).sub( _vecNear );
|
|
|
+ _vectemp3.copy( _vecDownRight ).sub( _vecNear );
|
|
|
+ _vectemp1.normalize();
|
|
|
+ _vectemp2.normalize();
|
|
|
+ _vectemp3.normalize();
|
|
|
+
|
|
|
+ _vectemp1.multiplyScalar( this.deep );
|
|
|
+ _vectemp2.multiplyScalar( this.deep );
|
|
|
+ _vectemp3.multiplyScalar( this.deep );
|
|
|
+ _vectemp1.add( _vecNear );
|
|
|
+ _vectemp2.add( _vecNear );
|
|
|
+ _vectemp3.add( _vecNear );
|
|
|
+
|
|
|
+ const planes = _frustum.planes;
|
|
|
+
|
|
|
+ planes[ 0 ].setFromCoplanarPoints( _vecNear, _vecTopLeft, _vecTopRight );
|
|
|
+ planes[ 1 ].setFromCoplanarPoints( _vecNear, _vecTopRight, _vecDownRight );
|
|
|
+ planes[ 2 ].setFromCoplanarPoints( _vecDownRight, _vecDownLeft, _vecNear );
|
|
|
+ planes[ 3 ].setFromCoplanarPoints( _vecDownLeft, _vecTopLeft, _vecNear );
|
|
|
+ planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
|
|
|
+ planes[ 5 ].setFromCoplanarPoints( _vectemp3, _vectemp2, _vectemp1 );
|
|
|
planes[ 5 ].normal.multiplyScalar( - 1 );
|
|
|
|
|
|
} else if ( this.camera.isOrthographicCamera ) {
|
|
|
|
|
|
- var left = Math.min( startPoint.x, endPoint.x );
|
|
|
- var top = Math.max( startPoint.y, endPoint.y );
|
|
|
- var right = Math.max( startPoint.x, endPoint.x );
|
|
|
- var down = Math.min( startPoint.y, endPoint.y );
|
|
|
-
|
|
|
- vecTopLeft.set( left, top, - 1 );
|
|
|
- vecTopRight.set( right, top, - 1 );
|
|
|
- vecDownRight.set( right, down, - 1 );
|
|
|
- vecDownLeft.set( left, down, - 1 );
|
|
|
-
|
|
|
- vecFarTopLeft.set( left, top, 1 );
|
|
|
- vecFarTopRight.set( right, top, 1 );
|
|
|
- vecFarDownRight.set( right, down, 1 );
|
|
|
- vecFarDownLeft.set( left, down, 1 );
|
|
|
-
|
|
|
- vecTopLeft.unproject( this.camera );
|
|
|
- vecTopRight.unproject( this.camera );
|
|
|
- vecDownRight.unproject( this.camera );
|
|
|
- vecDownLeft.unproject( this.camera );
|
|
|
-
|
|
|
- vecFarTopLeft.unproject( this.camera );
|
|
|
- vecFarTopRight.unproject( this.camera );
|
|
|
- vecFarDownRight.unproject( this.camera );
|
|
|
- vecFarDownLeft.unproject( this.camera );
|
|
|
-
|
|
|
- var planes = frustum.planes;
|
|
|
-
|
|
|
- planes[ 0 ].setFromCoplanarPoints( vecTopLeft, vecFarTopLeft, vecFarTopRight );
|
|
|
- planes[ 1 ].setFromCoplanarPoints( vecTopRight, vecFarTopRight, vecFarDownRight );
|
|
|
- planes[ 2 ].setFromCoplanarPoints( vecFarDownRight, vecFarDownLeft, vecDownLeft );
|
|
|
- planes[ 3 ].setFromCoplanarPoints( vecFarDownLeft, vecFarTopLeft, vecTopLeft );
|
|
|
- planes[ 4 ].setFromCoplanarPoints( vecTopRight, vecDownRight, vecDownLeft );
|
|
|
- planes[ 5 ].setFromCoplanarPoints( vecFarDownRight, vecFarTopRight, vecFarTopLeft );
|
|
|
+ const left = Math.min( startPoint.x, endPoint.x );
|
|
|
+ const top = Math.max( startPoint.y, endPoint.y );
|
|
|
+ const right = Math.max( startPoint.x, endPoint.x );
|
|
|
+ const down = Math.min( startPoint.y, endPoint.y );
|
|
|
+
|
|
|
+ _vecTopLeft.set( left, top, - 1 );
|
|
|
+ _vecTopRight.set( right, top, - 1 );
|
|
|
+ _vecDownRight.set( right, down, - 1 );
|
|
|
+ _vecDownLeft.set( left, down, - 1 );
|
|
|
+
|
|
|
+ _vecFarTopLeft.set( left, top, 1 );
|
|
|
+ _vecFarTopRight.set( right, top, 1 );
|
|
|
+ _vecFarDownRight.set( right, down, 1 );
|
|
|
+ _vecFarDownLeft.set( left, down, 1 );
|
|
|
+
|
|
|
+ _vecTopLeft.unproject( this.camera );
|
|
|
+ _vecTopRight.unproject( this.camera );
|
|
|
+ _vecDownRight.unproject( this.camera );
|
|
|
+ _vecDownLeft.unproject( this.camera );
|
|
|
+
|
|
|
+ _vecFarTopLeft.unproject( this.camera );
|
|
|
+ _vecFarTopRight.unproject( this.camera );
|
|
|
+ _vecFarDownRight.unproject( this.camera );
|
|
|
+ _vecFarDownLeft.unproject( this.camera );
|
|
|
+
|
|
|
+ const planes = _frustum.planes;
|
|
|
+
|
|
|
+ planes[ 0 ].setFromCoplanarPoints( _vecTopLeft, _vecFarTopLeft, _vecFarTopRight );
|
|
|
+ planes[ 1 ].setFromCoplanarPoints( _vecTopRight, _vecFarTopRight, _vecFarDownRight );
|
|
|
+ planes[ 2 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarDownLeft, _vecDownLeft );
|
|
|
+ planes[ 3 ].setFromCoplanarPoints( _vecFarDownLeft, _vecFarTopLeft, _vecTopLeft );
|
|
|
+ planes[ 4 ].setFromCoplanarPoints( _vecTopRight, _vecDownRight, _vecDownLeft );
|
|
|
+ planes[ 5 ].setFromCoplanarPoints( _vecFarDownRight, _vecFarTopRight, _vecFarTopLeft );
|
|
|
planes[ 5 ].normal.multiplyScalar( - 1 );
|
|
|
|
|
|
} else {
|
|
@@ -161,9 +161,9 @@ var SelectionBox = ( function () {
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
- SelectionBox.prototype.searchChildInFrustum = function ( frustum, object ) {
|
|
|
+ searchChildInFrustum( frustum, object ) {
|
|
|
|
|
|
if ( object.isMesh || object.isLine || object.isPoints ) {
|
|
|
|
|
@@ -171,11 +171,11 @@ var SelectionBox = ( function () {
|
|
|
|
|
|
if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
|
|
|
|
|
|
- center.copy( object.geometry.boundingSphere.center );
|
|
|
+ _center.copy( object.geometry.boundingSphere.center );
|
|
|
|
|
|
- center.applyMatrix4( object.matrixWorld );
|
|
|
+ _center.applyMatrix4( object.matrixWorld );
|
|
|
|
|
|
- if ( frustum.containsPoint( center ) ) {
|
|
|
+ if ( frustum.containsPoint( _center ) ) {
|
|
|
|
|
|
this.collection.push( object );
|
|
|
|
|
@@ -187,7 +187,7 @@ var SelectionBox = ( function () {
|
|
|
|
|
|
if ( object.children.length > 0 ) {
|
|
|
|
|
|
- for ( var x = 0; x < object.children.length; x ++ ) {
|
|
|
+ for ( let x = 0; x < object.children.length; x ++ ) {
|
|
|
|
|
|
this.searchChildInFrustum( frustum, object.children[ x ] );
|
|
|
|
|
@@ -195,10 +195,8 @@ var SelectionBox = ( function () {
|
|
|
|
|
|
}
|
|
|
|
|
|
- };
|
|
|
-
|
|
|
- return SelectionBox;
|
|
|
+ }
|
|
|
|
|
|
-} )();
|
|
|
+}
|
|
|
|
|
|
export { SelectionBox };
|