I'm trying to figure out how to calculate the screen position (x,y) of a 3D object with the coordinates (x, y, z). I've researched and found one solution that involves finding the projection matrix and multiplying the 3D position by it to project onto a 2D viewing surface (such as a computer screen). However, I'm unsure of how to locate this matrix in Three.js.
I attempted to use a conversion function like the one below, but it yielded incorrect results:
function Point3DToScreen2D(point3D){
var screenX = 0;
var screenY = 0;
var inputX = point3D.x - camera.position.x;
var inputY = point3D.y - camera.position.y;
var inputZ = point3D.z - camera.position.z;
var aspectRatio = renderer.domElement.width / renderer.domElement.height;
screenX = inputX / (-inputZ * Math.tan(camera.fov/2));
screenY = (inputY * aspectRatio) / (-inputZ * Math.tan(camera.fov / 2));
screenX = screenX * renderer.domElement.width;
screenY = renderer.domElement.height * (1-screenY);
return {x: screenX, y: screenY};
}
Any help would be greatly appreciated!