Back to the Shapespark home page

3D world position to screen space coordinates

Hi there.

So I am using the viewer api and I would like to get the 2d screen coordinates of a 3d location in the scene.

I am not finding anything in the docs, is there a way of getting this info?
I can’t seem to use the THREE js matrixWorld of the objects since they are always ‘null’

Thanks in advance!

Do you use Viewer.onNodeTypeClicked? This callback doesn’t include such information. Maybe try adding your own mouseup, touchend event handlers, store the screen position passed by a web browser to these handlers, and then in onNodeTypeClicked use the stored values.

I was going with that approach now but it felt a bit “hacky”.

I am indeed using the onNodeTypeClicked and thus get the 3d position.
Thought there was maybe an existing way of converting to screen position.

Thanks for answering!

This does help me to get the 2d position once when I click.
But I also want when I’m moving around in the scene to get the updated screen position for that 3d position.

So that’s why I was looking for a 3d to 2d method

Unfortunately, Shapespark doesn’t have an API for this. This would require taking into account current camera position and projection matrix.

1 Like

I see

There is the Viewer.getCameraPosition() that I can use to get the camera position.
But is there a way to get the projection matrix as well from the camera?

No, projection matrix is not available.