Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Make snap pick result return snapped Entity, not Mesh #1259

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions examples/picking/snapToEdge.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ <h3>Components Used</h3>

const viewer = new Viewer({
canvasId: "myCanvas",
logarithmicDepthBufferEnabled: true,
dtxEnabled: true // Enable data texture model representation
});

Expand All @@ -54,6 +53,9 @@ <h3>Components Used</h3>
// 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);
Expand All @@ -68,7 +70,11 @@ <h3>Components Used</h3>
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({
Expand All @@ -82,11 +88,26 @@ <h3>Components Used</h3>
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;
}
}
}

Expand All @@ -105,6 +126,6 @@ <h3>Components Used</h3>
document.addEventListener("touchstart", touchUpdateCursorPosition);
document.addEventListener("touchmove", touchUpdateCursorPosition);


window.viewer = viewer;
</script>
</html>
22 changes: 21 additions & 1 deletion examples/picking/snapToVertex.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ <h3>Components Used</h3>
// 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);
Expand All @@ -67,7 +70,9 @@ <h3>Components Used</h3>
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({
Expand All @@ -81,11 +86,26 @@ <h3>Components Used</h3>
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;
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/viewer/scene/webgl/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -1526,8 +1528,8 @@ const Renderer = function (scene, options) {
snappedWorldPos,
snappedWorldNormal,
snappedCanvasPos,
snappedEntity: snappedPickable,
entity: pickable
snappedEntity,
entity: snappedEntity
};
};

Expand Down