Cameras and View Controls

Learn perspective and orthographic cameras, viewing frustums, and OrbitControls, and keep models framed across canvas sizes.

🎯 Introduction

By the end of this article, you will be able to choose a camera, understand fov, aspect, near, and far, orbit, zoom, and pan with OrbitControls, and update the view when its container changes size.

🧱 Two common camera types

PerspectiveCamera mimics human vision: nearby objects appear larger than distant ones. It suits product showcases and most 3D scenes:

const camera = new THREE.PerspectiveCamera(
    60, // fov: vertical field of view in degrees
    width / height, // aspect: canvas aspect ratio
    0.1, // near: near clipping plane
    100 // far: far clipping plane
);

These four parameters define a viewing frustum. Only objects within this visible volume are rendered. Do not set near to 0 or choose an unnecessarily large range between near and far, as this reduces depth precision.

OrthographicCamera keeps apparent size independent of distance. It suits engineering drawings, flat maps, and views that need consistent object sizes:

const camera = new THREE.OrthographicCamera(-4, 4, 3, -3, 0.1, 100);

This demo keeps the camera position fixed and changes only the perspective camera's fov, showing how the field of view affects the image:

fov = 75 (wide angle)

fov = 30 (telephoto)

🛠 OrbitControls

This course uses the add-ons included with Three.js 0.185.1. No additional package is needed:

import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.minDistance = 2;
controls.maxDistance = 12;
controls.target.set(0, 0.5, 0);
controls.update();

With damping enabled, call controls.update() continuously in the render loop. OrbitControls manages the camera's position and orientation, so avoid overriding those transforms with another system at the same time.

const animate = () => {
    requestAnimationFrame(animate);
    controls.update();
    renderer.render(scene, camera);
};
animate();

📐 Responsive canvases and model framing

When the canvas changes size, update both the renderer size and the camera aspect ratio:

const resize = () => {
    const width = container.clientWidth;
    const height = container.clientHeight;

    camera.aspect = width / height;
    camera.updateProjectionMatrix();
    renderer.setSize(width, height, false);
    renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
};

window.addEventListener('resize', resize);
resize();

For models of varying sizes, use Box3 to calculate a bounding box and set the camera target to its center. Bounding boxes and model loading are covered later.

Containers in third-party 3D examples may have different dimensions. A fixed aspect is not a universal setting. In your components, read the container's clientWidth and clientHeight instead.

🧾 Summary

  • Perspective cameras suit realistic depth; orthographic cameras suit views with consistent apparent sizes.
  • fov, aspect, near, and far define the camera's visible volume.
  • Pass the camera and renderer canvas to OrbitControls.
  • Call controls.update() each frame when damping is enabled.
  • When the canvas resizes, update the renderer size and camera projection matrix.

❓ Questions and answers

Q1: Why is an object invisible even after I add it to the scene?

Check that it is in front of the camera and within near, far, and the field of view. Then confirm that lookAt or the controls point the camera at it.

Q2: When should I use an orthographic camera?

Use one when object size should stay constant as distance changes, such as in CAD, flat maps, board games, or isometric views.

Q3: Why does the scene look distorted after resizing the window?

The camera still uses the old aspect ratio. After updating camera.aspect, call camera.updateProjectionMatrix().

🧪 Exercise

Add OrbitControls to a 320×240 cube scene. Enable damping, restrict camera distance to between 2 and 8, and listen for window resize events.

// Write your code here

🎉 You can now control the viewer's perspective. Next, you will explore geometry and how vertices represent it on the GPU.