Spaces:
Running
Running
File size: 6,139 Bytes
a28eca3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
import {
Frustum,
Vector3,
Matrix4,
Quaternion,
} from 'three';
/**
* This is a class to check whether objects are in a selection area in 3D space
*/
const _frustum = new Frustum();
const _center = new Vector3();
const _tmpPoint = new Vector3();
const _vecNear = new Vector3();
const _vecTopLeft = new Vector3();
const _vecTopRight = new Vector3();
const _vecDownRight = new Vector3();
const _vecDownLeft = new Vector3();
const _vecFarTopLeft = new Vector3();
const _vecFarTopRight = new Vector3();
const _vecFarDownRight = new Vector3();
const _vecFarDownLeft = new Vector3();
const _vectemp1 = new Vector3();
const _vectemp2 = new Vector3();
const _vectemp3 = new Vector3();
const _matrix = new Matrix4();
const _quaternion = new Quaternion();
const _scale = new Vector3();
class SelectionBox {
constructor( camera, scene, deep = Number.MAX_VALUE ) {
this.camera = camera;
this.scene = scene;
this.startPoint = new Vector3();
this.endPoint = new Vector3();
this.collection = [];
this.instances = {};
this.deep = deep;
}
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 );
return this.collection;
}
updateFrustum( startPoint, endPoint ) {
startPoint = startPoint || this.startPoint;
endPoint = endPoint || this.endPoint;
// Avoid invalid frustum
if ( startPoint.x === endPoint.x ) {
endPoint.x += Number.EPSILON;
}
if ( startPoint.y === endPoint.y ) {
endPoint.y += Number.EPSILON;
}
this.camera.updateProjectionMatrix();
this.camera.updateMatrixWorld();
if ( this.camera.isPerspectiveCamera ) {
_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 );
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 ) {
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 {
console.error( 'THREE.SelectionBox: Unsupported camera type.' );
}
}
searchChildInFrustum( frustum, object ) {
if ( object.isMesh || object.isLine || object.isPoints ) {
if ( object.isInstancedMesh ) {
this.instances[ object.uuid ] = [];
for ( let instanceId = 0; instanceId < object.count; instanceId ++ ) {
object.getMatrixAt( instanceId, _matrix );
_matrix.decompose( _center, _quaternion, _scale );
_center.applyMatrix4( object.matrixWorld );
if ( frustum.containsPoint( _center ) ) {
this.instances[ object.uuid ].push( instanceId );
}
}
} else {
if ( object.geometry.boundingSphere === null ) object.geometry.computeBoundingSphere();
_center.copy( object.geometry.boundingSphere.center );
_center.applyMatrix4( object.matrixWorld );
if ( frustum.containsPoint( _center ) ) {
this.collection.push( object );
}
}
}
if ( object.children.length > 0 ) {
for ( let x = 0; x < object.children.length; x ++ ) {
this.searchChildInFrustum( frustum, object.children[ x ] );
}
}
}
}
export { SelectionBox };
|