pmndrs / react-three-fiber

🇨🇭 A React renderer for Three.js
https://docs.pmnd.rs/react-three-fiber
MIT License
27.62k stars 1.6k forks source link

instancedMesh onClick behavior #3289

Open RobertAron opened 5 months ago

RobertAron commented 5 months ago

I have some really weird issues with instancedMesh on click. I'm pretty sure this is a r3f issue because it involves onClick. It's also a degradation - it used to work on older versions.

SANDBOX: https://codesandbox.io/p/sandbox/instanced-vertex-colors-forked-zyjxkd?file=%2Fsrc%2FApp.js%3A15%2C44

There are a few different conditions where the onClick prop stops functioning correctly.

In my sandbox there are a few useState(false) that have toggle buttons attached to them.

Thanks so much for all your hard work <3

RobertAron commented 1 month ago

I had to come back around to this. It seems like the core issue has to do with the bounding sphere. It's not being updated properly. I'm not exactly sure I think maybe this should be called whenever needs update is true?

instancedMeshRef.current.instanceMatrix.needsUpdate = true;
instancedMeshRef.current.computeBoundingSphere();

The above might be slow though.

For now I'm going to fix it in my application by calling the update on an interval...but that's a pretty hacky fix. I'd appreciate a fix/insights if possible! I also wouldn't mind contributing but I think this is a pretty unfriendly first PR type problem 😓

Better sandbox. Look for the commented out code which will fix the problem by calling the compute.