I am building a 3D data annotation tool that will allow users to draw 3D bounding boxes around objects in things like medical scans, that I am loading in as a mesh. I would like to allow for some assisted annotations, specifically I want to be able to draw a 3D box around whatever part of the mesh the user clicks on.
I have a point-cloud representation of the background scan saved, and I can easily provide the bounding box coordinates of any object inside the scan, provided I know the location in ThreeJS coordinates where the click is happening. The issue is that I have orbit controls enabled, so after rotating the background scan the mouse coordinates X and Y do not correspond to the local X and Y of the scan inside ThreeJS. I have tried raycasting but have been unsuccessful because of the coordinates not matching.
This is my rayCaster code (which may be wrong). I was unable to get the casting to work using the camera I am actually viewing the mesh with, so I had to create another camera to use for casting.
e.preventDefault();
mouse_position.x = (e.clientX / renderer.domElement.clientWidth) * 2 - 1;
mouse_position.y = -(e.clientY / renderer.domElement.clientHeight) * 2 + 1;
console.log(mouse_position)
const ray_camera = new THREE.PerspectiveCamera(50, container.offsetWidth / container.offsetHeight, 1, 35000)
rayCaster.setFromCamera(mouse_position, ray_camera);
const mesh = scene.getObjectByName("current_background")
console.log(mesh)
const intersects = rayCaster.intersectObjects([mesh], true);
console.log(intersects.length)
if (intersects.length > 0) {
console.log(intersects[0].point)
}
This is the camera I’m using to view the scene.
new THREE.PerspectiveCamera(50, aspect, 0.01, 30000);
The issue is that when I inspect the mesh object that I want to annotate, I see that the position
is {x: 0, y: 0, z: 0}
so it is centered at the origin. The geometry bounding box is {"max": {x: 68.31328582763672, y: 50.31889343261719, z: 46.79656982421875}, "min": {x: -68.31328582763672, y: --50.31889343261719, z: -46.79656982421875}
. When I use rayCaster
and log the point of intersection, it is always a point very similar to this regardless of where I click in the scene: {x: 0.3289284805381883, y: 2.5495817904626703, z: -2.22214220339741}
.
My intuition tells me that I can use the camera’s position to determine where in ThreeJS space the click is happening. I love this community and you have all helped me more than you know. Any help you can offer would be greatly appreciated.