Stateful Canvas Events Factory? #2979
-
Relates to #2948. The question there regarded getting canvas events within a useState, which was successfully implemented after @CodyJasonBennett's help. That being said, I was pulled to other features and didn't test this thoroughly. Now testing, I'm noticing that any updates to this state, whether putting a getter within the enabled flag (1 below) or rebuilding the whole events callback (2 below) doesn't actually trigger a change. 1. Getter on enabled attribute -> events not disabled on click & drag
2. Provide complete new events callback with enabled either true or false -> events not disabled on click & drag
My testing methodology is as follows: we have a canvas with a large model loaded. Each mesh on model has onPointerOver, onClick, etc. events attached to it. When click & dragging in our scene (orthographic camera, orbit controls), and ensuring not to hover over the model, the camera pans at a silky 55 FPS. When I instead drag the camera and allow the mouse to hover over the model, typically moving between meshes quickly, the events begin firing and framerate drops to 20-30 FPS. If I set the enabled parameter to false ,the canvas is always smooth even onPointerOver of a mesh, but our pointer events don't work when the camera is static, as expected. When I add this changing event logic which initializes as true, I see no performance improvement over our nominal state: smooth camera panning with mouse elsewhere in the scene, framerate drop when mouse hovers over. Looking into the source code for |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
What happens if you update events with the Zustand setter? This is what I'd do internally if all goes well, and diff via strict reference. const set = useThree((state) => state.set)
set({ events }) |
Beta Was this translation helpful? Give feedback.
This is what R3F does internally, store coming from
useStore
:react-three-fiber/packages/fiber/src/core/index.tsx
Lines 332 to 333 in ffd953e
You can also do partial updates with the
setEvents
method. Perhaps that's the best way for this case.