yomotsu / camera-controls

A camera control for three.js, similar to THREE.OrbitControls yet supports smooth transitions and more features.

Home Page:https://yomotsu.github.io/camera-controls/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to update cameracontrols after the camera is manually changed?

mokargas opened this issue · comments

commented

I'm using react-three-fiber, but I'd like to explain what's going on.

Let's say I have my camera in one part of my scene. For example, at the bottom of an elevator shaft.

While the elevator (camera) moves up the shaft (e.g, the camera is animated), I want to disable Camera Controls, so I set controls.enabled = false

At the top, once the camera has finished it's animation, I set controls.enabled = true and set my look at to a new position.

The moment I do this, the camera teleports to its original position. I assume this is because it has an internal copy of the camera, so I try to use controls.update() but this doesn't seem to work.

So how can I animate the camera, then make camera-controls use the final position/rotation/etc of that camera, and then set an orbit point from that position?

This example might be helpful.
https://yomotsu.github.io/camera-controls/examples/path-animation.html

Also CameraContols#update( delta ) requires an argument. You could put update( 1000000 ) (or a large number).

commented

This example might be helpful. https://yomotsu.github.io/camera-controls/examples/path-animation.html

Also CameraContols#update( delta ) requires an argument. You could put update( 1000000 ) (or a large number).

Forgive me, I wrote update() but I am using update(delta) with delta coming from useFrame.

The example won't work for me as during my animation the lookAt point isn't set. I'd need to find a way to calculate from the camera on the last frame, no?

need to find a way to calculate from the camera on the last frame

I guess so.
(Without any reproducible code, I can't help precisely...)

commented

I figured this out, and yes it was due to the final frame of the animation not updating CC. So I'll post the process here

To do it, I did the following

        //Get direction
        var direction = new Vector3(0, 0, -1)
        direction.applyQuaternion(camera.quaternion)
        //Calculate look at point
        const pointLookingAt = new Vector3()
        pointLookingAt.addVectors(camera.position, direction)
        //Update CC
        controlsRef.current.setLookAt(camera.position.x, camera.position.y, camera.position.z, pointLookingAt.x, pointLookingAt.y, pointLookingAt.z, false)
        // Set orbit point 
        controlsRef.current.setOrbitPoint(p.x, p.y, p.z)

        controlsRef.current.update(31223)
   Thanks for your help @yomotsu 

Good! It seems you have solved your question.
Now you can close this issue then!