diff --git a/source/engine/viewer/viewermodel.js b/source/engine/viewer/viewermodel.js index 4c951ba..b8454cf 100644 --- a/source/engine/viewer/viewermodel.js +++ b/source/engine/viewer/viewermodel.js @@ -121,6 +121,7 @@ export class ViewerMainModel this.edgeSettings = new EdgeSettings (false, new RGBColor (0, 0, 0), 1); this.hasLines = false; this.hasPolygonOffset = false; + this.fullBoundingBox = null; } SetMainObject (mainObject) @@ -128,6 +129,7 @@ export class ViewerMainModel this.mainModel.SetRootObject (mainObject); this.hasLines = false; this.hasPolygonOffset = false; + this.fullBoundingBox = null; this.EnumerateLines ((line) => { this.hasLines = true; @@ -192,6 +194,17 @@ export class ViewerMainModel this.UpdatePolygonOffset (); } + GetFullBoundingBox () + { + if (this.fullBoundingBox !== null) { + return this.fullBoundingBox; + } + this.fullBoundingBox = this.GetBoundingBox (() => { + return true; + }); + return this.fullBoundingBox; + } + GetBoundingBox (needToProcess) { let hasMesh = false; @@ -292,6 +305,18 @@ export class ViewerMainModel GetMeshIntersectionUnderMouse (mouseCoords, camera, width, height) { + function CalculateLineThreshold (mousePos, camera, boundingBoxCenter) + { + let thresholdInScreenCoordinates = 15.0; + let frustumRange = camera.far - camera.near; + let cameraDistanceFromCenter = boundingBoxCenter.distanceTo (camera.position); + let distanceInFrustumRatio = cameraDistanceFromCenter / frustumRange; + let zValue = -1.0 + 2.0 * distanceInFrustumRatio; + let referencePoint1 = new THREE.Vector3 (mousePos.x, mousePos.y, zValue).unproject (camera); + let referencePoint2 = new THREE.Vector3 (mousePos.x + thresholdInScreenCoordinates, mousePos.y, zValue).unproject (camera); + return referencePoint1.distanceTo (referencePoint2); + } + if (this.mainModel.IsEmpty ()) { return null; } @@ -300,17 +325,20 @@ export class ViewerMainModel return null; } - let raycaster = new THREE.Raycaster (); - if (this.hasLines) { - let boundingSphere = this.GetBoundingSphere (() => { - return true; - }); - raycaster.params.Line.threshold = boundingSphere.radius / 100.0; - } - let mousePos = new THREE.Vector2 (); mousePos.x = (mouseCoords.x / width) * 2 - 1; mousePos.y = -(mouseCoords.y / height) * 2 + 1; + + let raycaster = new THREE.Raycaster (); + if (this.hasLines) { + let boundingBox = this.GetFullBoundingBox (); + if (boundingBox !== null) { + let boundingBoxCenter = new THREE.Vector3 (0.0, 0.0, 0.0); + boundingBox.getCenter (boundingBoxCenter); + raycaster.params.Line.threshold = CalculateLineThreshold (mousePos, camera, boundingBoxCenter); + } + } + raycaster.setFromCamera (mousePos, camera); let iSectObjects = raycaster.intersectObject (this.mainModel.GetRootObject (), true); for (let i = 0; i < iSectObjects.length; i++) {