ThreeJS camera.lookAt() has no effect, is there something I'm doing wrong?

前端 未结 6 411
半阙折子戏
半阙折子戏 2021-02-05 02:13

In Three.js, I want a camera to be pointed at a point in 3D space.

For this purpose, I tried using the camera.lookAt function like so:

camer         


        
相关标签:
6条回答
  • 2021-02-05 02:29

    Yes Please beware... It seems that having THREE.TrackballControls or THREE.OrbitControls seems to override the camera.lookAt function as your are passing in your camera when you instantiate an instance of the controls. You might want to get rid of the controls and then performing camera.lookAt() or tween your camera some other way to verify that the controls are having a overriding effect on your Camera. I googled for a while why camera.lookat() seemed to have no effect.

    0 讨论(0)
  • 2021-02-05 02:30

    Looking at the source code of THREE.TrackballControls, I figured out that I can make the camera look where I want by setting trackballControls.target to the THREE.Vector3 I want it to look at, and then rerendering the scene.

    0 讨论(0)
  • 2021-02-05 02:31

    Here's an alternative solution: create an object (i.e. cube) with 0 dimensions.

    var cameraTarget = new THREE.Mesh( new THREE.CubeGeometry(0,0,0));
    

    In the render function set the camera.lookAt to the position of the cameraTarget.

    function render() {
        camera.lookAt( cameraTarget.position );
        renderer.render( scene, camera );
    }
    

    Then just move cameraTarget around as you wish.

    0 讨论(0)
  • 2021-02-05 02:37

    In my opinion, we are not supposed to mess with the original code. I found a way around to achieve the objective of looking at any particular point. After having declared your "control" variable, simply execute these two lines of code:

    // Assuming you know how to set the camera and myCanvas variables
    control = new THREE.OrbitControls(camera, myCanvas);
    
    // Later in your code
    control.object.position.set(camX, camY, camZ);
    control.target = new THREE.Vector3(targetX, targetY, targetZ);
    

    Keep in my mind that this will switch the center of the focus to your new target. In other words, your new target will be the center of all rotations of the camera. Some parts will be difficult to look at as you became familiar to manipulate the camera assuming the default center. Try zoom in as much as you can and you will have a sense of what I am saying Hope this help.

    0 讨论(0)
  • 2021-02-05 02:43

    I figured it out. To prevent THREE.TrackballControls or THREE.OrbitControls from overriding camera.lookAt upon initialization, you need to change the line that sets the control's target property to equal the sum of the camera.position vector and the camera.getWorldDirection() vector, instead of how it's currently implemented using a new THREE.Vector3() (which defaults to (0, 0, 0)).

    So, for THREE.TrackballControls, change line 39 to:

    this.target = new THREE.Vector3().addVectors(/*new line for readability*/
        object.position, object.getWorldDirection());
    

    Same goes for THREE.OrbitControls, on line 36. I actaully haven't tested it on TrackballControls.js but it does work on OrbitControls.js. Hope this helps.

    0 讨论(0)
  • 2021-02-05 02:48

    I ran into the same problem and was able to make it work by using OrbitControls.target. Below is what I did after declaring a controller.

        controller = new THREE.OrbitControls(camera, renderer.domElement);
        controller.addEventListener('change', renderer.domElement);
        controller.target = new THREE.Vector3(0, 1, 0);
    
    0 讨论(0)
提交回复
热议问题