Camera arguments in Three.js

前端 未结 4 1361
说谎
说谎 2021-01-31 05:37

This is how a camera is instantiated:

var camera = new THREE.PerspectiveCamera(
    VIEW_ANGLE,
    ASPECT,
    NEAR,
    FAR
);

What do these

4条回答
  •  春和景丽
    2021-01-31 05:53

    I found this tutorial very useful for understanding all the camera parameters, and the difference between PerspectiveCamera and OrthographicCamera.

    PerspectiveCamera

    • Fov (Field of view) - This is part of scene that can be seen from the position of the camera. As you probably know, we, humans, have almost 180-degree field of view, while some birds might even have a complete 360-degree field of view. However, for computers, we usually use the field of view between 60 and 90 degrees.

    • Aspect - The aspect ratio is ratio between the horizontal and vertical size of the area where we render the output. As we usually use the entire window, we will just use that ratio. The aspect ratio determines the difference between the horizontal field of view and the vertical field of view as you can see in the figure on the following page. Ordinary value is window.innerWidth / window.innerHeight.

    • Near - This property defines a min distance from the camera the Three.js renders the scene. Usually, this is a very small value, e.g. 0.1.

    • Far - This property defines a max distance we see the scene from the position of the camera. If we set this as too low, a part of our scene might not be rendered; if we set it as too high, in some cases, it might affect the rendering performance. Normal value is between 500 and 2000.

    OrthographicCamera

    • Left (Camera frustum left plane) - You should see this as what is the left border of what will be rendered. If we set this value to -100, you won’t see any objects that are farther to the left.

    • Right (Camera frustum right plane) - Anything farther to the right won't be rendered.

    • Top (Camera frustum top plane) - The maximum top position to be rendered.

    • Bottom (Camera frustum bottom plane) The bottom position to be rendered.

    • Near (Camera frustum near plane) - From this point on, based on the position of the camera, the scene will be rendered.

    • Far (Camera frustum far plane) - The furthest point, based on the position of the camera, to which the scene will be rendered.

    The following picture should be very illustrative:

    The main difference between the two camera modes is that in the OrthographicCamera distance plays no role, so all the elements are of the same size, as you can see in the case of the red and yellow ball.

    Finally here is some code you can use to switch from one camera to the other:

    this.switchCamera = function(SCENE_WIDTH, SCENE_HEIGHT) {
      if (camera instanceof THREE.PerspectiveCamera) {
        camera = new THREE.OrthographicCamera( SCENE_WIDTH / - 2, SCENE_WIDTH / 2, SCENE_HEIGHT / 2, SCENE_HEIGHT / - 2, 0.1, 1000 );
        camera.position.x = 0;
        camera.position.y = 0;
        camera.position.z = -1;
        camera.lookAt(scene.position);
        this.perspective = "Orthographic";
      } else {
        camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 1000 );
        camera.position.x = 0;
        camera.position.y = 0;
        camera.position.z = -1;
        camera.lookAt(scene.position);
        this.perspective = "Perspective";
      }
    };
    

    Notes:

    • The function camera.lookAt(scene.position) orients the camera to where the scene is located, so it is visible.
    • Units in three.js are SI units, so the values of left,right,top,bottom should not assumed to be pixels.
    • The aspect ratio of the camera's frustum should normally match the canvas' aspect ratio.
    • SCENE_WIDTH, SCENE_HEIGHT, can be determined through the geometries that are added in the scene. The orthographic frustum could be much larger than the scene but it wouldn't be very parsimonious.

    Useful links:

    • http://www.glprogramming.com/red/chapter03.html
    • Three.js - Orthographic camera

提交回复
热议问题