问题
I am trying to design a map. So, when I select any geometry, I want the object to come at the centre of the viewport and the camera to be looking at it. I have spent quite a lot of time looking everywhere on the web but failed to find the answer. Please note that I don't want to rotate the object. I just want to update the camera. I am trying to get something like in this example where when we click the number, it comes to the centre : https://sketchfab.com/3d-models/jurassic-aquarium-diorama-with-annotations-d82bea156a1c4176b5d09b82b176e84c
Here is what I have tried and has been closest to giving me any output:
`camera.position.x = select.position.x;
camera.position.y = select.position.y;
camera.position.z = select.position.z;
camera.Translate(0,0,-1);
camera.updateProjectionMatrix ();`
select has the selected object. Although the above code does rotate the camera but it fails to look at the selected object.
I have also tried lookAT(select.position)
and that didn't work either.
Please help me with this.
回答1:
If you are using Orbit or Trackball controls in your project it won't work by just changing lookAt of the camera. You need to change the target of the controls as well. To experiment, just try to disable the controls and then change look at for Camera.
来源:https://stackoverflow.com/questions/58740616/rotate-camera-to-look-at-selected-object-in-three-js