yomotsu / camera-controls

A camera control for three.js, similar to THREE.OrbitControls yet supports smooth transitions and more features.
https://yomotsu.github.io/camera-controls/
MIT License
2.05k stars 258 forks source link

Restore CameraPosition after setOrbitPoint dosn't work #316

Open EberleAutomatischeSysteme opened 2 years ago

EberleAutomatischeSysteme commented 2 years ago

Describe the bug

I rotate the scene the mouse point, for this I set the current hitpoint of the raycaster at setOrbitPoint().

This also works perfectly.

Now I reset the camera occasionally to a saved position. These are determined with cameraControls.getPosition() and cameraControls.getTarget().

The resetting of this position does not work after I changed the OrbitPoint once. Even if I reset the OrbitPoint to (0,0,0), which I set as default position, the camera is not set to the saved position anymore.

Saving the camera state as JSON is unfortunately not possible, because other controllers may be used. Which do not offer this function.

To Reproduce

  1. Store position with cameraControls.getPosition() and target with cameraControls.getTarget() in variable.

  2. Move and rotate an object, setOrbitPoint() must be called once to change the OrbitPoint.

  3. The previous saved position cannot be restored with cameraControls.setPosition(x,y,z) and cameraControls.setTarget(x,y,z,).

  4. The saved position matches the current one, but the image is not the same anymore.

Code

//save the current camera status
var position = cameraControls.getPosition() 
var target = cameraControls.getTarget() 

--> Move and rotate object

//restore camera state
cameraControls.setOrbitPoint(0,0,0) // -> does not work with or without restore the orbit point
cameraControls.setPosition(position.x, position.y, position.z)
cameraControls.setTarget(target.x, target.y, target.z)
cameraControls.update()

cameraControls.getPosition() == position and 
cameraControls.getTarget() == target
but scene images is not same as when it was when saved the camera position

Live example

https://yomotsu.github.io/camera-controls/examples/click-to-set-orbit-point.html

Expected behavior

Camera should return to the same position

Screenshots or Video

No response

Device

Desktop

OS

Windows

Browser

Chrome

yomotsu commented 2 years ago

setOrbitPoint() changes focalOffset as well. you may need to save focalOffset and restore that after setting the target and position.

like

const position = cameraControls.getPosition() 
const target = cameraControls.getTarget() 
const offset = cameraControls.getFocalOffset()

then

cameraControls.setOrbitPoint( 0, 0, 0, false )
cameraControls.setLookAt( position.x, position.y, position.z, target.x, target.y, target.z, false )
cameraControls.setFocalOffset( offset.x, offset.y, offset.z, false )
cameraControls.update( 0 )

(not tested tho)

EberleAutomatischeSysteme commented 2 years ago

All right, I understand. That's right, that's how to solve the problem.

Thanks for the answer and this great project!

EberleAutomatischeSysteme commented 2 years ago

I also noticed that if the orbit point is set or changed for an orthographic camera. The DollyToCourser is no longer calculated correctly. I then zoom by an offset around the actual point. If I set the focalOffset to (0,0,0) it works normally again but the image then jumps once according to the offset.
I assume that when calculating the zoom direction the offset has to be considered?

yomotsu commented 2 years ago

Actually, dolly-to-courser in Orthographic has a complex problem (dolly-to-courser for Orthographic should be removed until it's fixed, but keep it as it is for backward compat )

EberleAutomatischeSysteme commented 2 years ago

It would be cool if the dolly-to-courser with the rotate around mouse point would work. We would also like to offer our help to solve this problem, if that's ok?

yomotsu commented 2 years ago

Absolutely! Would it be possible for you to make a pull request?

Also, would you mind elaborating "the dolly-to-courser with the rotate around mouse point"? I think drag-to-rotate uses the mouse pointer move, thus cursor position keeps moving during rotation. Therefore I don't think "rotate around mouse point" would work.

EberleAutomatischeSysteme commented 2 years ago

With rotate-around-mouse-point I mean changing the orbit point (the camera rotates around the point of the cursor (mouse)). Currently there is the problem that when the orbit point (focal offset) is set or changed, the orthographic camera no longer zooms in the direction of the mouse cursor. The goal would be that, when changing the orbit point the orthographic camera zooms in the direction of the cursor.

yomotsu commented 2 years ago

I think we must fix zoom-to-cursor in Orthographic Camera first then. Currently, zoom-to-cursor in orthographic camera is wrong. It actually behaves as dolly-to-cursor even in Orthographic, and it causes problems. It has to be ZOOM-to-cursor, the camera position is stationary and changes FOV angle.

yomotsu commented 2 years ago

The problem I mentioned right above ("zoom-to-cursor in orthographic camera is wrong") has just been fixed! So, you don't need to think about that.

EberleAutomatischeSysteme commented 2 years ago

Great, thank you very much. As soon as I have some time again I will try to get the focal offset with the orthographic camera.