Display context menu in 3D when clicking on Node

Godot Version

4.6.2

Question

I want to display a context menu when the user is clicking on one of the green nodes of the path.

My idea was to build a scene that has a canvas layer as root and a vbox container with all the buttons (see red square) and then display that at the position of the node when a click on it has been detected.

But how do I know the 2D coordinates of the screen where to move the scene to so that it always appears next to the node?

Or is that whole approach stupid and there are better ways?

Here my script to detect the click on the node (the click on the node detection is working fine already):

func _unhandled_input(event: InputEvent) -> void:
	var mouse
	var result
	if event is InputEventMouse:
		mouse = event.position
		
		var worldspace= get_world_3d().direct_space_state
		var camera = Globals.camera
		var start = camera.project_ray_origin(mouse)
		var end = start + camera.project_ray_normal(mouse) * 10000
		var query = PhysicsRayQueryParameters3D.create(start, end)
			
		query.collide_with_areas = false
		result = worldspace.intersect_ray(query)

Found this post here: Convert 3D coordinates of objects to 2D coordinates for 2D HUD - #3 by nines so the answer is to use

get_viewport().get_camera_3d().unproject_position()

var object_of_interest_3D_position : Vector3 = self.selectedNode.global_position
var HUDposition = get_viewport().get_camera_3d().unproject_position( object_of_interest_3D_position )
var v2i : Vector2i  = DisplayServer.window_get_size()

%CmPathNode.offset = HUDposition
%CmPathNode.show()

In the end it looks like this but I am still not sure if its correct to use a CanvasLayer - that kinda feels wrong.

It is correct. That’s what CanvasLayer is there for - to display Control nodes above 2D and 3D scenes.