Hi,
Okay well, my scene is pretty optimized I’d say runs at 60fps when not animating.
but when I animate the mesh there is a sudden horrible drop in fps down to 7 fps and if I move mouse rigorously, the app crashes.
Here’s how I’m doing it. I’m managing the hover state and mouse position in a parent component and on mouse hover on canvas I’m doing all the required calculations and storing them in a state like so:
const handlePointerMove = (e: MouseEvent) => {
// isMouse is set true when mouse hovers the canvas
if (isMouse == true && canvas.current) {
let w = canvas.current.clientWidth
let h = canvas.current.clientHeight
let x = e.pageX
let y = e.pageY
// setting the range [-0.5, 0.5] to map accross the screen horizontally and vertically
x = (x - w / 2) / w
y = (y - h / 2) / h
// storing the coordinates in the state
setMCoord({
x: x,
y: y,
})
}
}
Then I pass this to my model as a prop.
Then I animate the mesh group using useFrame likeso:
useFrame(({ gl, scene, camera }) => {
perfume.current.rotation.x = 0
perfume.current.rotation.y = -Math.PI * mCoords.x
perfume.current.rotation.z = 0
gl.render(scene, camera)
}, 1)
the group looks something like this
<group
ref={perfume}
position={[0, -0.6, 0]}
>
<mesh
ref={model}
castShadow
receiveShadow
position={[0, 0.3, -0.25]}
geometry={nodes.Bottle.geometry}
>
<meshPhysicalMaterial
attach="material"
{...bottleConfig}
/>
</mesh>
<mesh
castShadow
receiveShadow
geometry={nodes.Head.geometry}
material={materials.Head}
/>
<mesh
castShadow
receiveShadow
geometry={nodes.SprayPipe.geometry}
material={materials.FrontColor}
/>
</group>
What I think the problem is: the state change is triggering a rerender when the mouse moves.
What I tried: I tried to memoize the state but idk that seems stupid bcoz this will also change every time the state changes. but might increase a tiny bit of performance when rerender is triggered by other states.
Can someone please guide how I can optimize this? it has to be at least 50+ fps