How can I raycast from the viewport location?

Im trying to select an object by using raycast but the location of the mouse is not correct, does anyone knows how to fix this?

Im trying to make it start from top 80px and from left 280px

Attach the event hander onclick to canvas and use event.offsetX and event.offsetY to get the offset coords.

You can see it here in action, Make sure mouse is on canvas and do ctrlKey click.

Model_Cammy.html (94.4 KB)

can you write the code a little, i’m confuse

No problem

// canvas global reference to canvas;
// camera global reference to camera;
// mesh global reference to target object;

ray = new THREE.Raycaster();

canvas.onclick = (event) => { var inx, xy = [ event.offsetX, event.offsetY ];

x3d.ray.setFromCamera( { 
x: 2 * xy[0] / event.target.clientWidth - 1 , 
y: -2 * xy[1] / event.target.clientHeight + 1 }, camera );

if( ! (inx = x3d.ray.intersectObject(mesh, !1)).length ) return alert('No hit');
alert( inx );
};

I suggest you run that attachment, to see how it works.

Thank You, I did it

You are welcome.

That screenshot looks interesting.
Can you share with us about the nature of your project?

It’s a game engine i’m trying to create, It’s going to use for 3d games, i’ll try to see if i can make it for 2d in the future but right now i’m focusing in the 3d game engine

Sounds interesting, looking forward to see the final product.

Thank You :pray:

Do you know how to disable raycaster while using the transformControls?