Izzimach / react-three-legacy

React bindings to create and control a 3D scene using three.js
Other
1.52k stars 128 forks source link

orbitcontrols keep state #87

Open macrozone opened 7 years ago

macrozone commented 7 years ago

Hi there,

if my scene gets re-rendered because a prop or state changes, orbit-controls resets to the initial position.

How can i keep it's state?

Izzimach commented 7 years ago

Yeah, this is because the orbits controls don't work in the normal flow of React updates. I could maybe add a flag to the Camera component so that it ignores location settings after the initial mount. A better solution would be to rewrite the orbit camera to send props down to the camera, but that would be a chunk of work.

macrozone commented 7 years ago

Sorry for the late response.

In my project (for school) I ended up extending the scene-object with a property onOrbit, which is called when the scene is rotated or changed with the orbit controls.

It takes a function and passed the current camera as the only argument.

I further disabled the automatic raycasting to every child on the scene in projectPointerEvent. I had a pointCloud with over a million points in the scene and this killed the performance. Instead i added two properties:

I think automatic raycasting should be optional

See this gist: https://gist.github.com/macrozone/5d6b08289c45e17399ce6caf55dc6ed2