From b11291d7539045cebf1ca1ebd9102526fb72c85d Mon Sep 17 00:00:00 2001 From: lindsay Date: Thu, 23 Nov 2023 02:24:37 +0100 Subject: [PATCH] Make snap pick result return snapped Entity, not Mesh #1248 --- examples/picking/snapToEdge.html | 27 ++++++++++++++++++++++++--- examples/picking/snapToVertex.html | 22 +++++++++++++++++++++- src/viewer/scene/webgl/Renderer.js | 6 ++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/examples/picking/snapToEdge.html b/examples/picking/snapToEdge.html index 45727e0c00..f9eabd1fd4 100644 --- a/examples/picking/snapToEdge.html +++ b/examples/picking/snapToEdge.html @@ -34,7 +34,6 @@

Components Used

const viewer = new Viewer({ canvasId: "myCanvas", - logarithmicDepthBufferEnabled: true, dtxEnabled: true // Enable data texture model representation }); @@ -54,6 +53,9 @@

Components Used

// Mouse hover to snap to edge //------------------------------------------------------------------------------------------------------------------ + viewer.scene.highlightMaterial.fill = false; + viewer.scene.highlightMaterial.edgeAlpha = 0.5; + const markerDiv = document.createElement('div'); const canvas = viewer.scene.canvas.canvas; canvas.parentNode.insertBefore(markerDiv, canvas); @@ -68,7 +70,11 @@

Components Used

markerDiv.style.position = "absolute"; markerDiv.style.pointerEvents = "none"; - // Mouse and touch input + var lastEntity = null; + + sceneModel.on("loaded", () => { + viewer.scene.objects["1hOSvn6df7F8_7GcBWlS2V"].highlighted = true + }) function updateCursorPosition(canvasPos) { const snapPickResult = viewer.scene.snapPick({ @@ -82,11 +88,26 @@

Components Used

markerDiv.style.marginTop = `${snapPickResult.snappedCanvasPos[1] - 10}px`; markerDiv.style.background = "greenyellow"; markerDiv.style.border = "3px solid green"; + + if (!lastEntity || (snapPickResult.snappedEntity && snapPickResult.snappedEntity.id !== lastEntity.id)) { + if (lastEntity) { + lastEntity.highlighted = false; + } + lastEntity = snapPickResult.snappedEntity; + if (snapPickResult.snappedEntity) { + snapPickResult.snappedEntity.highlighted = true; + } + } } else { markerDiv.style.marginLeft = `${canvasPos[0] - 10}px`; markerDiv.style.marginTop = `${canvasPos[1] - 10}px`; markerDiv.style.background = "white"; markerDiv.style.border = "1px solid black"; + + if (lastEntity) { + lastEntity.highlighted = false; + lastEntity = null; + } } } @@ -105,6 +126,6 @@

Components Used

document.addEventListener("touchstart", touchUpdateCursorPosition); document.addEventListener("touchmove", touchUpdateCursorPosition); - + window.viewer = viewer; \ No newline at end of file diff --git a/examples/picking/snapToVertex.html b/examples/picking/snapToVertex.html index e48e838a65..56d1bc92c2 100644 --- a/examples/picking/snapToVertex.html +++ b/examples/picking/snapToVertex.html @@ -53,6 +53,9 @@

Components Used

// Mouse hover to snap to vertices //------------------------------------------------------------------------------------------------------------------ + viewer.scene.highlightMaterial.fill = false; + viewer.scene.highlightMaterial.edgeAlpha = 0.5; + const markerDiv = document.createElement('div'); const canvas = viewer.scene.canvas.canvas; canvas.parentNode.insertBefore(markerDiv, canvas); @@ -67,7 +70,9 @@

Components Used

markerDiv.style.position = "absolute"; markerDiv.style.pointerEvents = "none"; - // Mouse and touch input + // Mouse input + + var lastEntity = null; function updateCursorPosition(canvasPos) { const snapPickResult = viewer.scene.snapPick({ @@ -81,11 +86,26 @@

Components Used

markerDiv.style.marginTop = `${snapPickResult.snappedCanvasPos[1] - 10}px`; markerDiv.style.background = "greenyellow"; markerDiv.style.border = "3px solid green"; + + if (!lastEntity || (snapPickResult.snappedEntity && snapPickResult.snappedEntity.id !== lastEntity.id)) { + if (lastEntity) { + lastEntity.highlighted = false; + } + lastEntity = snapPickResult.snappedEntity; + if (snapPickResult.snappedEntity) { + snapPickResult.snappedEntity.highlighted = true; + } + } } else { markerDiv.style.marginLeft = `${canvasPos[0] - 10}px`; markerDiv.style.marginTop = `${canvasPos[1] - 10}px`; markerDiv.style.background = "white"; markerDiv.style.border = "1px solid black"; + + if (lastEntity) { + lastEntity.highlighted = false; + lastEntity = null; + } } } diff --git a/src/viewer/scene/webgl/Renderer.js b/src/viewer/scene/webgl/Renderer.js index fa3d805f43..974c748b2d 100644 --- a/src/viewer/scene/webgl/Renderer.js +++ b/src/viewer/scene/webgl/Renderer.js @@ -1517,6 +1517,8 @@ const Renderer = function (scene, options) { snappedCanvasPos = scene.camera.projectWorldPos(snappedWorldPos); } + const snappedEntity = (snappedPickable && snappedPickable.delegatePickedEntity) ? snappedPickable.delegatePickedEntity() : snappedPickable; + return { snapType, snappedToVertex: snapType === "vertex", @@ -1526,8 +1528,8 @@ const Renderer = function (scene, options) { snappedWorldPos, snappedWorldNormal, snappedCanvasPos, - snappedEntity: snappedPickable, - entity: pickable + snappedEntity, + entity: snappedEntity }; };