THREEJS Cameras

ASHABB ReactJS & ThreeJS
5 min readJul 9, 2023

--

Three.js Cameras: Exploring Perspectives in 3D Graphics

Photo by NordWood Themes on Unsplash

1. Title

2. Description

3. Snippet

4. Explanation

5. Full code

6. Output

7. Summary

8. Next and Previous Blog

2. Description

Three.js, a popular JavaScript library, offers a variety of camera options for creating captivating 3D graphics and animations. These cameras allow you to define different perspectives and viewpoints within your virtual scene. Here’s a short description of Three.js cameras and their types:

  1. PerspectiveCamera: This camera simulates the way human eyes perceive the world. It provides realistic depth and perspective, making objects appear larger when they are closer and smaller when they are farther away. It is commonly used for creating immersive experiences and realistic 3D scenes.
  2. OrthographicCamera: Unlike the PerspectiveCamera, an OrthographicCamera does not exhibit depth or perspective. It projects the scene onto a flat, 2D plane, maintaining equal sizing for objects regardless of their distance from the camera. This camera type is suitable for technical visualizations or stylized, non-realistic graphics.
  3. ArrayCamera: An ArrayCamera allows you to render scenes from multiple perspectives using an array of sub-cameras. It is useful for creating multi-view rendering or simulating a camera rig with different angles or focal lengths.

PerspectiveCamera

3. Snippet :

// Create a PerspectiveCamera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Set the camera position
camera.position.set(0, 0, 5);
// Add the camera to the scene
scene.add(camera);

4. Explanation

In this code, we create a PerspectiveCamera with a field of view of 75 degrees, an aspect ratio matching the window dimensions, a near clipping plane of 0.1, and a far clipping plane of 1000. We set the camera position to (0, 0, 5) to place it a bit further away from the scene. Then, we add the camera to the scene using scene.add(camera).

OrthographicCamera

3. Snippet :

// Define the dimensions for the orthographic camera
const width = window.innerWidth / 10; // Adjust the width as needed
const height = window.innerHeight / 10; // Adjust the height as needed
const near = 1;
const far = 1000;

// Create the OrthographicCamera
const camera = new THREE.OrthographicCamera(
width / -2,
width / 2,
height / 2,
height / -2,
near,
far
);
// Set the camera position
camera.position.set(0, 0, 5);
// Add the camera to the scene
scene.add(camera);

4. Explanation

In this code, we create an Orthographic Camera with a specified width and height. The width and height are divided by 10 to reduce the size of the camera’s viewing area. You can adjust these values as needed. The near and far parameters define the clipping planes for the camera. We set the camera position to (0, 0, 5) and add it to the scene using scene.add(camera).

ArrayCamera

3. Snippet :

// Create an array of cameras for the ArrayCamera
const cameras = [];
const camera1 = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera1.position.set(0, 0, 5);
const camera2 = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera2.position.set(0, 0, 10);
const camera3 = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera3.position.set(0, 0, 15);
cameras.push(camera1, camera2, camera3);

4. Explanation

In this code, we create an array of cameras (cameras) with different positions. Each camera is an instance of PerspectiveCamera with its own position set. We push the cameras into the cameras array.

Then, we create an ArrayCamera using the cameras array. We set the position of the ArrayCamera and add it to the scene using scene.add(camera).

5. Full code

// Set up the scene, renderer, and other necessary variables
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer();
// Set up the renderer size to match the container
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// Create a PerspectiveCamera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
// Set the camera position
camera.position.set(0, 0, 5);
// Add the camera to the scene
scene.add(camera);

// Create a geometry and material for a simple cube
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
// Add the cube to the scene
scene.add(cube);

// Create an animation loop
function animate() {
requestAnimationFrame(animate);

// Rotate the cube
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;

// Render the scene with the camera
renderer.render(scene, camera);
}
animate();
output for above code

7. Summary

In this tutorial, we explored the concepts of PerspectiveCamera, OrthographicCamera, and ArrayCamera in Three.js.

- PerspectiveCamera: This camera simulates the way human eyes perceive the world, providing a realistic sense of depth and perspective. It is commonly used for creating immersive 3D scenes.

- OrthographicCamera: Unlike the PerspectiveCamera, an OrthographicCamera does not exhibit depth or perspective. It projects the scene onto a flat, 2D plane, maintaining equal sizing for objects regardless of their distance from the camera. It is often used for technical visualizations or stylized, non-realistic graphics.

- ArrayCamera: An ArrayCamera allows you to render scenes from multiple perspectives using an array of sub-cameras. This camera type is useful for creating multi-view rendering or simulating a camera rig with different angles or focal lengths.

By understanding these camera types, you can control the viewpoints and perspectives in your Three.js projects. Each camera type offers unique capabilities and can be used to achieve different visual effects and storytelling techniques.

Experiment with different camera settings, positions, and rotations to enhance the visual appeal and impact of your 3D scenes. Remember to consider factors like field of view, aspect ratio, near and far clipping planes, and camera positioning to achieve the desired composition and visual effects.

With this knowledge, you can take your Three.js projects to the next level by leveraging the power of different camera types to create compelling and immersive 3D experiences.

8. Next and Previous Blog

previous blog: THREEJS Animation

do visit us on Instagram & Threads

--

--