Is there ANY way to have the three.js camera lookat being rendered off-center?

前端 未结 2 509
情话喂你
情话喂你 2021-01-22 06:34

Is there a way to setup the Three.js renderer in such a way that the lookat point of the camera is not in the center of the rendered image?

To clarify: image a scene wit

2条回答
  •  北海茫月
    2021-01-22 07:30

    Here's a simple solution:

    Assuming your cube is 4 x 4 x 4, at position 0, 0, 0:

    var geometry = new THREE.BoxGeometry( 4, 4, 4 );
    var material = new THREE.MeshBasicMaterial( { color: 0x777777 } );
    var cube = new THREE.Mesh( geometry, material );
    cube.position.set( 0, 0, 0 );
    

    Get cube's position:

    var Vx = cube.position.x, 
        Vy = cube.position.y, 
        Vz = cube.position.z;
    

    Then deduct by 2 from x position, then add 2 to y and z position, and use the values to create a new Vector3:

    var newVx = Vx - 2,
        newVy = Vy + 2;
        newVz = Vz + 2;
    
    var xyz = new THREE.Vector3(newVx, newVy, newVz)
    

    Then camera lookAt:

    camera.lookAt(xyz);   
    

    Using console log, it would show that the camera is now looking at -2, 2, 2, which is the upper-left of your cube.

    console.log(xyz);
    

提交回复
热议问题