From 9371abf56f4c127334b34c8451253ecfd992e60c Mon Sep 17 00:00:00 2001 From: kovacsv Date: Sat, 11 Dec 2021 08:42:38 +0100 Subject: [PATCH] Add the possibility to add extra model parts to the viewer. --- source/viewer/navigation.js | 21 ++++++--- source/viewer/viewer.js | 86 +++++++++++++++++++++++++++---------- website/o3dv/js/website.js | 8 +++- 3 files changed, 86 insertions(+), 29 deletions(-) diff --git a/source/viewer/navigation.js b/source/viewer/navigation.js index 6a9d3fa..a218cc5 100644 --- a/source/viewer/navigation.js +++ b/source/viewer/navigation.js @@ -252,7 +252,8 @@ OV.Navigation = class this.clickDetector = new OV.ClickDetector (); this.onUpdate = null; - this.onClick = null; + this.onMouseClick = null; + this.onMouseMove = null; this.onContext = null; if (this.canvas.addEventListener) { @@ -275,9 +276,14 @@ OV.Navigation = class this.onUpdate = onUpdate; } - SetClickHandler (onClick) + SetMouseClickHandler (onMouseClick) { - this.onClick = onClick; + this.onMouseClick = onMouseClick; + } + + SetMouseMoveHandler (onMouseMove) + { + this.onMouseMove = onMouseMove; } SetContextMenuHandler (onContext) @@ -398,6 +404,11 @@ OV.Navigation = class { this.mouse.Move (this.canvas, ev); this.clickDetector.Move (); + if (this.onMouseMove) { + let mouseCoords = OV.GetClientCoordinates (this.canvas, ev.clientX, ev.clientY); + this.onMouseMove (mouseCoords); + } + if (!this.mouse.IsButtonDown ()) { return; } @@ -577,9 +588,9 @@ OV.Navigation = class Click (button, clientX, clientY) { - if (this.onClick) { + if (this.onMouseClick) { let mouseCoords = OV.GetClientCoordinates (this.canvas, clientX, clientY); - this.onClick (button, mouseCoords); + this.onMouseClick (button, mouseCoords); } } diff --git a/source/viewer/viewer.js b/source/viewer/viewer.js index ecb5439..b8b8342 100644 --- a/source/viewer/viewer.js +++ b/source/viewer/viewer.js @@ -262,44 +262,61 @@ OV.ViewerGeometry = class }); } - GetModelMeshUnderMouse (mouseCoords, camera, width, height) + GetMeshIntersectionUnderMouse (mouseCoords, camera, width, height) { if (this.mainObject === null) { return null; } + let raycaster = new THREE.Raycaster (); let mousePos = new THREE.Vector2 (); mousePos.x = (mouseCoords.x / width) * 2 - 1; mousePos.y = -(mouseCoords.y / height) * 2 + 1; raycaster.setFromCamera (mousePos, camera); let iSectObjects = raycaster.intersectObject (this.mainObject, true); - for (let i = 0; i < iSectObjects.length; i++) { - let iSectObject = iSectObjects[i]; - if (iSectObject.object.type === 'Mesh' && iSectObject.object.visible) { - return iSectObject.object; - } - } - return null; - } - - GetModelIntersectionUnderMouse (mouseCoords, camera, width, height) - { - let raycaster = new THREE.Raycaster (); - let mousePos = new THREE.Vector2 (); - mousePos.x = (mouseCoords.x / width) * 2 - 1; - mousePos.y = -(mouseCoords.y / height) * 2 + 1; - raycaster.setFromCamera (mousePos, camera); - let iSectObjects = raycaster.intersectObjects (this.modelMeshes); for (let i = 0; i < iSectObjects.length; i++) { let iSectObject = iSectObjects[i]; if (iSectObject.object.type === 'Mesh' && iSectObject.object.visible) { return iSectObject; } } + return null; } }; +OV.ViewerExtraGeometry = class +{ + constructor (scene) + { + this.scene = scene; + this.mainObject = null; + } + + AddObject (object) + { + if (this.mainObject === null) { + this.mainObject = new THREE.Object3D (); + this.scene.add (this.mainObject); + } + this.mainObject.add (object); + } + + Clear () + { + if (this.mainObject === null) { + return; + } + this.mainObject.traverse ((obj) => { + if (obj.isMesh || obj.isLineSegments) { + obj.geometry.dispose (); + } + }); + this.scene.remove (this.mainObject); + this.mainObject = null; + } +}; + OV.ViewerAxis = class { constructor (scene) @@ -438,6 +455,7 @@ OV.Viewer = class this.renderer = null; this.scene = null; this.geometry = null; + this.extraGeometry = null; this.axis = null; this.camera = null; this.shading = null; @@ -467,6 +485,7 @@ OV.Viewer = class this.scene = new THREE.Scene (); this.geometry = new OV.ViewerGeometry (this.scene); + this.extraGeometry = new OV.ViewerExtraGeometry (this.scene); this.axis = new OV.ViewerAxis (this.scene); this.InitCamera (); @@ -475,9 +494,14 @@ OV.Viewer = class this.Render (); } - SetClickHandler (onClick) + SetMouseClickHandler (onMouseClick) { - this.navigation.SetClickHandler (onClick); + this.navigation.SetMouseClickHandler (onMouseClick); + } + + SetMouseMoveHandler (onMouseMove) + { + this.navigation.SetMouseMoveHandler (onMouseMove); } SetContextMenuHandler (onContext) @@ -630,9 +654,16 @@ OV.Viewer = class this.Render (); } + AddExtraObject (object) + { + this.extraGeometry.AddObject (object); + this.Render (); + } + Clear () { this.geometry.Clear (); + this.extraGeometry.Clear (); this.Render (); } @@ -685,6 +716,15 @@ OV.Viewer = class } GetMeshUserDataUnderMouse (mouseCoords) + { + let intersection = this.GetMeshIntersectionUnderMouse (mouseCoords); + if (intersection === null) { + return null; + } + return intersection.object.userData; + } + + GetMeshIntersectionUnderMouse (mouseCoords) { let width = this.canvas.width; let height = this.canvas.height; @@ -692,11 +732,11 @@ OV.Viewer = class width /= window.devicePixelRatio; height /= window.devicePixelRatio; } - let mesh = this.geometry.GetModelMeshUnderMouse (mouseCoords, this.camera, width, height); - if (mesh === null) { + let intersection = this.geometry.GetMeshIntersectionUnderMouse (mouseCoords, this.camera, width, height); + if (intersection === null) { return null; } - return mesh.userData; + return intersection; } GetBoundingBox (needToProcess) diff --git a/website/o3dv/js/website.js b/website/o3dv/js/website.js index e88f770..c131bd7 100644 --- a/website/o3dv/js/website.js +++ b/website/o3dv/js/website.js @@ -39,7 +39,8 @@ OV.Website = class this.InitNavigator (); this.InitCookieConsent (); - this.viewer.SetClickHandler (this.OnModelClicked.bind (this)); + this.viewer.SetMouseClickHandler (this.OnModelClicked.bind (this)); + this.viewer.SetMouseMoveHandler (this.OnModelMouseMoved.bind (this)); this.viewer.SetContextMenuHandler (this.OnModelContextMenu.bind (this)); this.Resize (); @@ -160,6 +161,11 @@ OV.Website = class } } + OnModelMouseMoved (mouseCoordinates) + { + + } + OnModelContextMenu (globalMouseCoordinates, mouseCoordinates) { let meshUserData = this.viewer.GetMeshUserDataUnderMouse (mouseCoordinates);