How does Three.js copy other camera information to the current camera

I have a glb file with five PerspectiveCamera cameras (this glb file is derived from other models), and now I want to take data from one of the other cameras to update my current camera.

How do I do this, what properties do I update, such as position, rotation, up, anything else? And how does lookat copy updates?

setModel('./model/SM_CQ_AC_ZHGL_01_摄像头.glb').then((res: any) => {
    let pipeInnner_camera_folder = gui.addFolder('管廊内部镜头');
    let pipeInnner_camera_names: any = {};
    pipeInnner_camera_names['Camera_JT_00'] = () => {
      let perspectiveCamera = initCamera as THREE.PerspectiveCamera;
      setCameraAnimation(camera, perspectiveCamera.position, 2000, () => {
        camera.rotation.copy(perspectiveCamera.rotation);
      });
    };
    res.model.traverse((object3D: THREE.Object3D) => {
      if (object3D.type === 'PerspectiveCamera') {
        pipeInnner_camera_names[object3D.name] = () => {
          let perspectiveCamera = object3D as THREE.PerspectiveCamera;
          camera.up.copy(perspectiveCamera.up);
          const cameraDirection = new THREE.Vector3();
          perspectiveCamera.getWorldDirection(cameraDirection);
          camera.lookAt(camera.position.clone().add(cameraDirection));
          camera.position.copy(perspectiveCamera.position);
          camera.rotation.copy(perspectiveCamera.rotation);
        };
      }
    });
    Object.keys(pipeInnner_camera_names).forEach((item) => {
      pipeInnner_camera_folder.add(pipeInnner_camera_names, item);
    });
  });

SM_CQ_AC_ZHGL_01_摄像头.glb (2.9 KB)

You don’t need both .lookAt and camera.rotation.copy

camera.rotation.copy will undo/override what you just did with the .lookAt.

You probably don’t need to copy .up vector… that’s usually just defaulted to 0,1,0 in threejs and is probably the same for whatever cameras you import.

You should only really need to copy .position and .rotation…

And if you’re using any of the camera controls… you’ll have to either disable them… (since they will immediately override the rotation of the camera that you’ve just updated) … or in the case of OrbitControls… you would have to update the controls.target to something valid for the camera you’re trying to copy. For instance:

camera.position.copy(perspectiveCamera.position)
camera.rotation.copy(perspectiveCamera.rotation)
controls.target.copy(camera.position).add(cameraDirection);

Also make sure that your camera is added to the scene, or you will have to update its matrices manually via camera.updateMatrix() and camera.updateMatrixWorld()

If you also want the perspective/fov to copy over… you’ll probably need to copy…
camera.fov and camera.near and camera.far…
and then do a camera.updateProjectionMatrix() to update the projection matrix. This update is mandatory since its not done automatically on render for cameras added to the scene.

1 Like

Thank you for your reply and guidance. I have tried the ideas and methods you provided and achieved success. Thank you! :+1: :smile:

1 Like