Hi,
I am trying to measure the distance between two points in a scene.
Here is the reference I used: Raycaster Measurements - Three.js Tutorials.
It works fine when the canvas is only present, but when there are other components the ray is getting hit somewhere else. Initially I thought it was because I gave document.addEventListener("mousemove", onMouseMove, false);
and changed document
with canvas
element. But there was no difference.
this is the function used for mouse move :
function onMouseMove(event) {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
if (drawingLine) {
raycaster.setFromCamera(mouse, camera);
intersects = raycaster.intersectObjects(pickableObjects, false);
if (intersects.length > 0) {
const positions = line.geometry.attributes.position.array;
const v0 = new THREE.Vector3(positions[0], positions[1], positions[2]);
const v1 = new THREE.Vector3(
intersects[0].point.x,
intersects[0].point.y,
intersects[0].point.z
);
positions[3] = intersects[0].point.x;
positions[4] = intersects[0].point.y;
positions[5] = intersects[0].point.z;
line.geometry.attributes.position.needsUpdate = true;
const distance = v0.distanceTo(v1);
measurementLabels[lineId].element.innerText = distance.toFixed(3) + "mm";
measurementLabels[lineId].position.set(
(v0.x + v1.x) / 2,
(v0.y + v1.y) / 2,
(v0.z + v1.z) / 2
);
}
}
}
I would like to make the raycaster accurate.
Thanks,
Binoy