Threejs editor How to convert units


How to convert to pixels I use in code
mesh = new THREE.Mesh(geometry, material);
mesh.position.z =2;
mesh.position.x = 1;
mesh.position.y = 1;

The THREE.Vector3 class has the .project(camera) and .unproject(camera) methods. In this case if I remember this stuff right you are going to want the project(camera) method to translate your 3D coordinates in to a 2d vector.

It’s been a while but I think that gives you a number between -1…1, so you’d want to add 1, then divide by 2, and multiply by your available canvas size to turn it in to px.

1 Like

Yes, that’s how it works. The code looks something like this:

    function to2D(pos) {

        let vector = pos.project(camera);
        vector.x = window.innerWidth * (vector.x + 1) / 2;
        vector.y = -window.innerHeight * (vector.y - 1) / 2;

        return vector;

I’m not really sure about +1 and -1. But this function returns a result similar to the correct one :laughing:

1 Like

thanks i try it

i mean threejs editor In what unit