(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three")); } else { var mod = { exports: {} }; factory(mod.exports, global.three); global.SelectionBox = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.SelectionBox = void 0; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } /** * This is a class to check whether objects are in a selection area in 3D space */ var _frustum = new _three.Frustum(); var _center = new _three.Vector3(); var _tmpPoint = new _three.Vector3(); var _vecNear = new _three.Vector3(); var _vecTopLeft = new _three.Vector3(); var _vecTopRight = new _three.Vector3(); var _vecDownRight = new _three.Vector3(); var _vecDownLeft = new _three.Vector3(); var _vecFarTopLeft = new _three.Vector3(); var _vecFarTopRight = new _three.Vector3(); var _vecFarDownRight = new _three.Vector3(); var _vecFarDownLeft = new _three.Vector3(); var _vectemp1 = new _three.Vector3(); var _vectemp2 = new _three.Vector3(); var _vectemp3 = new _three.Vector3(); var _matrix = new _three.Matrix4(); var _quaternion = new _three.Quaternion(); var _scale = new _three.Vector3(); var SelectionBox = /*#__PURE__*/function () { function SelectionBox(camera, scene) { var deep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Number.MAX_VALUE; _classCallCheck(this, SelectionBox); this.camera = camera; this.scene = scene; this.startPoint = new _three.Vector3(); this.endPoint = new _three.Vector3(); this.collection = []; this.instances = {}; this.deep = deep; } _createClass(SelectionBox, [{ key: "select", value: function 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; } }, { key: "updateFrustum", value: function 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); 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); 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); _planes[5].normal.multiplyScalar(-1); } else { console.error('THREE.SelectionBox: Unsupported camera type.'); } } }, { key: "searchChildInFrustum", value: function searchChildInFrustum(frustum, object) { if (object.isMesh || object.isLine || object.isPoints) { if (object.isInstancedMesh) { this.instances[object.uuid] = []; for (var instanceId = 0; instanceId < object.count; instanceId++) { object.getMatrixAt(instanceId, _matrix); _matrix.decompose(_center, _quaternion, _scale); 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 (var x = 0; x < object.children.length; x++) { this.searchChildInFrustum(frustum, object.children[x]); } } } }]); return SelectionBox; }(); _exports.SelectionBox = SelectionBox; });