Hi Guys!
I’m encountering an issue with the Raycaster in my Three.js application. When I click on a specific object, the Raycaster is incorrectly selecting the geometry behind it instead of the intended target object. I’ve tried adjusting the render order and other properties, but the problem persists. It seems like there may be factors like transparency or object hierarchy affecting the selection process.
I’d appreciate any insights or suggestions on how to resolve this issue. Please let me know if you need more information or code snippets.
Thank you!
applyDefaults(
model: Object3D,
userData: UserData,
opacity = 1,
wireframe = false
): void {
model.userData = userData;
model.traverse((child: Object3D): void => {
if (child instanceof Mesh) {
child.userData = userData;
child.material.metalness = 0;
child.material.transparent = true;
child.material.opacity = opacity;
child.material.wireframe = wireframe;
}
});
}
export function raycast({
mouse,
e,
width,
height,
camera,
scene,
raycaster
}: Props): UserData | undefined {
mouse.x = (e.clientX / width) * 2 - 1;
mouse.y = -(e.clientY / height) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const models = scene.children.filter((model) => model.userData.name);
let intersectedModel: undefined | UserData = undefined;
for (const model of models) {
const intersects = raycaster.intersectObject(model, true);
if (intersects.length > 0) {
intersectedModel = intersects[0].object.userData as UserData;
break;
}
}
return intersectedModel;
}