Perspective Projections: Screen to World Point

Hi everyone,
I’m new to babylonjs, switching over from Unity.

I’m trying to project 2d coordinates into 3d space kind of like this:

Unity has a really helpful function called Camera.screenToWorldPoint(screen.x, screen.y, nearClipPlane). The result is a preliminary 3D coordinate that looks something like (world.x, world.y, nearClipPlane.z). A great part of this function is that the camera didn’t have to be active in order for it to work, so I had this function running across multiple cameras at the same time.

My step after this was to shoot a raycast from the camera in the direction of the preliminary 3d point and if it hit the ground plane, that was the final 3D coordinate.

The issue I’m having in babylon is getting that preliminary 3D point. How to I go from screen coordinates to a 3d coordinate on an imaginary plane that sits in front of the camera? And how do I do this from a camera that isn’t active?

Thank you!

Hi lunboks925,

Welcome to Babylon!

I think the createPickingRay family of methods – there are InCameraSpace and ToRef variations – will do what you’re looking for. They’re on the Scene instead of the Camera, mostly for legacy/backwards-compatibility reasons, but all the variables should be readily available in most use cases. (If for whatever reason you need to do this operation without the scene – or without a camera, for that matter – there are convenience methods in Vector3 that will do most of the math for you: example here. That’s probably more complex and less convenient than the Scene method, though. :smiley:)

This looks promising, I’ll look into it.

Thanks a lot!