I’m working with React Three Fiber and Three.js and am having trouble getting raycasting to work correctly on a 3D model. Specifically, I want to detect clicks on various parts of the model and trigger events, such as showing an alert or updating the UI.
I’ve set up a basic component with raycasting, but it’s not detecting clicks reliably on certain parts of the model. For example, when clicking on the engine heads of my model, the raycasting fails to register the clicks.
I’d appreciate any advice on why the raycasting might not be working as expected and how I can fix it. If there are any common issues or configuration settings I should check, please let me know!
pointer events are inbuilt, you don’t (and shouldn’t) create your own handlers as you gain nothing from them. all the calculations are also done for you and fiber makes it the fastest it can possibly be as it only raycasts stuff that has actual handlers on them.
the e object will contain the dom pointer event data, the three event data and r3f extras. everything you need is in there. it supports all common pointer events, bubbling, stop propagation (if you want the closest object to the camera for instance) and pointer capture (if you want to drag stuff and the pointer may leave the window), see Events - React Three Fiber
PS your code has issues with separation of concern. the model component should hold a model, not a canvas. you also don’t need to give canvas 100vw/100vh, it stretches ootb to 100% of its parent. basic reset css styles take care of it.
Thank you for the suggestion and pointing out the issues with separation of concern !
I tried the approach using the built-in pointer events, and while it works in some cases, I’m still facing a couple of issues:
Inconsistent Event Detection: The raycasting seems to work only about 1 out of 5 times. For example, if I click the same object 5 times, the event only triggers once. The other 4 clicks don’t seem to register at all, and I’m not sure why the detection is so inconsistent. This happens especially when rotating the model or interacting with certain parts.
Certain Objects Not Registering: There are parts of the model where clicking doesn’t trigger any console logs or event data at all. It seems like raycasting isn’t being applied to those objects, though they should be part of the same scene.
I’ve made sure that the event handlers are set up correctly, but it feels like something is missing or not working as expected, especially for certain parts of the model. Any ideas on what could be causing this behavior?