Hey! I’m not entirely sure I understand the need but if you want to get world coordinates of screen coordinates, you should just do the following:
var engine = scene.getEngine();
var globalViewport= scene.activeCamera.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
var projectedPosition = BABYLON.Vector3.Project(screenPosition, BABYLON.Matrix.Identity(), scene.getTransformMatrix(), globalViewport);
The Question
Is it possible to position the sphere at a precise location (X, Y) in the 2D viewport given
the camera, the camera position, the camera target, the viewport size?
yes, and it’s not that complex (not the time now to make a PG). Just search the forum (or the older one) for :
getAspectRatio(), camera.fov and Math.tan
Shortly, from my memory (haven’t been playing with BJS for a while) :
the camera.fov is half the vertical angle of view, from 0 to the maximum visible y in the screen at a given distance.
So if you want to set a ball in the viewport corner, assuming it’s located at the distance d from the camera, then :
y / d = tan(fov / 2) => y = d * tan(fov / 2)
The aspectRatio is the correction factor between the viewport width and height
x = y * aspectRatio
z = d
Set your ball at (x, y ,z), it should be exactly in the viewport corner.
This is simple when the camera looks toward Z, of course.
If the camera rotates/moves, then you have to use the camera viewMatrix… what is a bit more complex