Our lessons on webgl continue. Today we review Threejs cameras. Threejs library provides two different cameras: orthographic camera (OrthographicCamera) and perspective camera (PerspectiveCamera). The best way to understand the difference between these cameras – try every one of them with your own eyes.
PerspectiveCamera
This is camera with perspective projection.
![PerspectiveCamera](https://script-tutorials.com/demos/456/1.png)
PerspectiveCamera parameters:
Argument |
Description |
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
This is camera with orthographic projection.
![OrthographicCamera](https://script-tutorials.com/demos/456/2.png)
OrthographicCamera parameters:
Argument |
Description |
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 top position to be rendered. |
bottom |
Camera frustum bottom plane – the bottom position to be rendered. |
near |
Camera frustum near plane – from this point, based on the position of the camera, the scene will be rendered. |
Far |
Camera frustum far plane – to this point, based on the position of the camera, the scene will be rendered. |
As you can see, if we use the OrthographicCamera, all the elements are of the same size. Distance plays no role. This camera type is usually used in old 2D games (like Diablo (1 and 2), Civilization or SimCity).
![OrthographicCamera 2](https://script-tutorials.com/demos/456/3.jpg)
Now, let’s have a look how both cameras work:
![PerspectiveCamera and OrthographicCamera cameras](https://script-tutorials.com/demos/456/4.png)
To make a better (ordinary) perspective, we usually use the Perspective camera, it is more like the real world. If you would like to test it in your demo, here is switching camera function:
01 |
this .switchCamera = function () { |
02 |
if (camera instanceof THREE.PerspectiveCamera) { |
03 |
camera = new THREE.OrthographicCamera( |
04 |
window.innerWidth / - 16, window.innerWidth / 16,window.innerHeight / 16, window.innerHeight / - 16, -200, 500 ); |
05 |
camera.position.x = 2; |
06 |
camera.position.y = 1; |
07 |
camera.position.z = 3; |
08 |
camera.lookAt(scene.position); |
09 |
this .perspective = "Orthographic" ; |
11 |
camera = new THREE.PerspectiveCamera(45, |
12 |
window.innerWidth / window.innerHeight, 0.1, 1000); |
13 |
camera.position.x = 120; |
14 |
camera.position.y = 60; |
15 |
camera.position.z = 180; |
16 |
camera.lookAt(scene.position); |
17 |
this .perspective = "Perspective" ; |
After you switch the camera, we usually need to focus the camera to a certain position, you can use lookAt
function to do it:
1 |
camera.lookAt( new THREE.Vector3(x,y,z)); |