herrdu / notes

0 stars 0 forks source link

toScreenPosition #13

Open herrdu opened 2 years ago

herrdu commented 2 years ago
function toScreenPosition(obj, camera)
{
    var vector = new THREE.Vector3();

    var widthHalf = 0.5*renderer.context.canvas.width;
    var heightHalf = 0.5*renderer.context.canvas.height;

    obj.updateMatrixWorld();
    vector.setFromMatrixPosition(obj.matrixWorld);
    vector.project(camera);

    vector.x = ( vector.x * widthHalf ) + widthHalf;
    vector.y = - ( vector.y * heightHalf ) + heightHalf;

    return { 
        x: vector.x,
        y: vector.y
    };

};
herrdu commented 2 years ago

https://stackoverflow.com/questions/27409074/converting-3d-position-to-2d-screen-position-r69