Adjusting camera for visible Three.js shape

后端 未结 2 1239

I have a CubeGeometry which the camera is looking at, and I want the camera to zoom so that the cube is entirely visible, but no larger.

My initial attempt was to co

2条回答
  •  一生所求
    2020-12-06 04:05

    Perspective Camera. If the camera is centered and viewing the cube head-on, define

    dist = distance from the camera to the front face ( important ! ) of the cube

    and

    height = height of the cube.

    If you set the camera field-of-view as follows

    fov = 2 * Math.atan( height / ( 2 * dist ) ) * ( 180 / Math.PI );
    

    then the cube height will match the visible height.

    Orthographic Camera. If the camera is centered and viewing the cube head-on, define

    aspect = the aspect ratio of your window (i.e., width / height)

    and

    height = height of the cube.

    Then construct your camera this way:

    camera = new THREE.OrthographicCamera( -aspect * height/2, aspect * height/2, height/2, -height/2, near, far );
    

    The cube height will match the visible height.

    In either case, if the camera is not centered, or is otherwise viewing the cube at an angle, then the problem is more complicated.

    Also, if the window is narrower than it is high, then the width is the constraining factor, and the problem is more complicated.

提交回复
热议问题