Projection of Mouse Click Location to World Coordinate

I’m building an extension with Omniverse Code to allow the user to draw lines on the scene that display the distance between the points. The idea is to use the tool to measure parts of a model in the scene in real-world coordinates.

I’m applying a ClickGesture to sc.Screen so that the lines can drawn anywhere in the scene, then capturing the position of the click from the gesture_payload. These points are stored in my model and used to draw linear curves connecting them.

The problem is that I can’t seem to convert the points returned by gesture_payload.ray_closest_point to proper world coordinates. That, or they are already in world coordinates, but the ray seems to extend only a millimeter or so out from the screen.

Above is a screenshot of the tool in the viewport of Code. I’m trying to draw these curves after applying a transform to world space with sc.Transform(scale_to=sc.Space.WORLD), but the disparity in the distances leads me to believe the points are still in screen coordinates.

For reference, the points used to draw the curves in this example are:
[498.5757728806386, 498.97768757014046, 498.9824375708]
[498.68196294281813, 498.6690254545543, 499.18490963068143]
[499.2023064157676, 498.66463690681223, 498.6689547382181]
[498.9927685830157, 498.97476187342147, 498.56836759138287]
[498.5757728806386, 498.97768757014046, 498.9824375708]

Is there a way to capture the position of a click in world coordinates, or else to extend the ray cast out farther?

Clarifying question: Are you always trying to use the ground plane intersection, or are you looking for something more complicated, such as intersections with the nearest object along the ray defined by the mouse position?

In theory, if you could access the properties of the active camera (namely the camera world position, aspect ratio, and fov) you could probably compute the ray yourself from the screen position, and if you want the ground plane intersection of that ray, you’d simply look for a point that satisfies the equation of the ray with a value of zero in the vertical direction (in the case of your figure, Y). However, this may not be worth the effort if this logic is already implemented in something you can call, and it won’t work if you want to look for object intersections.

You may want to take a look at the code for the asset painter extension (I believe in the extensions window it is simply titled “Paint”) and see if you can identify how it solves this problem, as it relies on the functionality you want to implement to paint assets on existing meshes.

1 Like

I’m looking to draw along intersections with the nearest object along the ray, yes. The Paint System Core extension seems to define that functionality for all the other painter tools, so I will look in to implementing it in a similar fashion.
Thanks for suggestion!

1 Like

LegacyViewport has a function to query the next click position in world space.

1 Like

Got it working now, thanks. All the painter tools rely on the MeshRaycast class to paint objects at points where there is collision with meshes, and I was able to adapt this to use a ray along the camera’s forward vector to find the points.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.