Perspective & Orthographic Cameras
Three.js has two cameras you will use in games:
PerspectiveCamera: far things look smaller, like an eye or a real camera. First- and third-person games, racing, almost all 3D.OrthographicCamera: no perspective; an object is the same size on screen at any distance. Isometric strategy and city builders, 2.5D platformers, top-down minimaps, UI and level editors.
Setting up an orthographic camera
An orthographic camera sees a box instead of a pyramid. You give the box's sides in world units: left, right, top, bottom, near, far. The usual pattern picks a vertical size in world units and derives the width from the aspect ratio:
const viewHeight = 20; // world units visible from bottom to top
const aspect = innerWidth / innerHeight;
const ortho = new THREE.OrthographicCamera(
(-viewHeight * aspect) / 2, (viewHeight * aspect) / 2, // left, right
viewHeight / 2, -viewHeight / 2, // top, bottom
0.1, 200, // near, far
);
ortho.position.set(20, 20, 20); // classic isometric-style angle
ortho.lookAt(0, 0, 0);On resize, recompute left/right from the new aspect and call updateProjectionMatrix(). To zoom, change ortho.zoom (or viewHeight); moving an orthographic camera closer does not make things bigger.
Here is the same scene through both cameras side by side, using setViewport and setScissor to draw two pictures into one canvas. The row of pillars recedes into the distance on the left; on the right, all pillars are the same height:
<style>html, body { margin: 0; height: 100%; overflow: hidden } canvas { display: block }
.label { position: fixed; top: 6px; color: #fff; font: 13px system-ui, sans-serif; text-shadow: 0 1px 2px #000 }</style>
<div class="label" style="left: 10px">PerspectiveCamera</div><div class="label" style="left: calc(50% + 10px)">OrthographicCamera</div>
<script type="module">
import * as THREE from 'three';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.setScissorTest(true);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#1a2131');
scene.add(new THREE.HemisphereLight('#fff', '#334', 1.4));
const sun = new THREE.DirectionalLight('#fff', 2);
sun.position.set(5, 8, 3);
scene.add(sun);
const floor = new THREE.Mesh(new THREE.PlaneGeometry(10, 40), new THREE.MeshStandardMaterial({ color: '#37474f' }));
floor.rotation.x = -Math.PI / 2;
floor.position.z = -12;
scene.add(floor);
const pillarGeo = new THREE.BoxGeometry(0.8, 3, 0.8);
for (let i = 0; i < 8; i++) {
for (const x of [-3, 3]) {
const p = new THREE.Mesh(pillarGeo, new THREE.MeshStandardMaterial({ color: new THREE.Color().setHSL(i / 8, 0.6, 0.55) }));
p.position.set(x, 1.5, -i * 4);
scene.add(p);
}
}
const half = () => (innerWidth / 2) / innerHeight; // aspect of each half
const persp = new THREE.PerspectiveCamera(50, half(), 0.1, 100);
const viewHeight = 12;
const ortho = new THREE.OrthographicCamera(-viewHeight * half() / 2, viewHeight * half() / 2, viewHeight / 2, -viewHeight / 2, 0.1, 100);
for (const cam of [persp, ortho]) { cam.position.set(6, 5, 8); cam.lookAt(0, 1, -8); }
addEventListener('resize', () => {
renderer.setSize(innerWidth, innerHeight);
persp.aspect = half();
persp.updateProjectionMatrix();
ortho.left = -viewHeight * half() / 2;
ortho.right = viewHeight * half() / 2;
ortho.updateProjectionMatrix();
});
renderer.setAnimationLoop(() => {
const w = innerWidth / 2, h = innerHeight;
renderer.setViewport(0, 0, w, h); renderer.setScissor(0, 0, w, h); renderer.render(scene, persp);
renderer.setViewport(w, 0, w, h); renderer.setScissor(w, 0, w, h); renderer.render(scene, ortho);
});
</script>Viewports and scissors are given in CSS pixels from the bottom-left corner of the canvas. The same technique draws split-screen multiplayer, a rear-view mirror, or a minimap in a corner (render the main view full-size, then a small top-down orthographic view on top).
From world to screen
Both cameras turn a world position into normalized device coordinates (NDC): x and y from −1 (left/bottom) to +1 (right/top), and z from −1 (near plane) to +1 (far plane). The perspective camera divides by depth, which is what makes distant things small. This model projects the same points through a simple perspective and an orthographic projection:
// Project camera-space points (camera at origin, looking down -Z) to NDC x.
const fov = 60 * Math.PI / 180, aspect = 1;
const f = 1 / Math.tan(fov / 2); // focal length for a vertical fov
const perspX = ([x, , z]) => (f / aspect) * x / -z; // divide by depth
const viewHeight = 10;
const orthoX = ([x]) => x / (viewHeight * aspect / 2); // no division by depth
for (const p of [[2, 0, -5], [2, 0, -10], [2, 0, -20]]) {
console.log(`point x=2 at depth ${String(-p[2]).padStart(2)}: perspective ndc.x=${perspX(p).toFixed(3)} orthographic ndc.x=${orthoX(p).toFixed(3)}`);
}point x=2 at depth 5: perspective ndc.x=0.693 orthographic ndc.x=0.400
point x=2 at depth 10: perspective ndc.x=0.346 orthographic ndc.x=0.400
point x=2 at depth 20: perspective ndc.x=0.173 orthographic ndc.x=0.400
In perspective, doubling the depth halves the distance from the centre of the screen; in orthographic it doesn't change at all. Three.js does this with vector.project(camera), and vector.unproject(camera) goes back from NDC to world space, which is how mouse picking works (Pointer Raycasting).
Labels over 3D objects
Projecting is how games place HTML on top of 3D: nameplates, health bars, quest markers. Project the object's world position, then convert NDC to CSS pixels:
<style>html, body { margin: 0; height: 100%; overflow: hidden } canvas { display: block }
.tag { position: fixed; transform: translate(-50%, -100%); padding: 2px 6px; border-radius: 4px; background: rgba(0,0,0,.6); color: #fff; font: 12px system-ui, sans-serif; white-space: nowrap; pointer-events: none }</style>
<script type="module">
import * as THREE from 'three';
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color('#1d2535');
const camera = new THREE.PerspectiveCamera(50, innerWidth / innerHeight, 0.1, 100);
scene.add(new THREE.HemisphereLight('#fff', '#334', 1.4));
const sun = new THREE.DirectionalLight('#fff', 2);
sun.position.set(3, 6, 4);
scene.add(sun);
scene.add(new THREE.GridHelper(20, 20, '#445', '#2c3446'));
const units = [['Knight', '#90caf9', -3, 0], ['Archer', '#a5d6a7', 0, -2], ['Mage', '#ce93d8', 3, 1]].map(([name, color, x, z]) => {
const mesh = new THREE.Mesh(new THREE.CapsuleGeometry(0.4, 0.8, 4, 12), new THREE.MeshStandardMaterial({ color }));
mesh.position.set(x, 0.8, z);
scene.add(mesh);
const tag = document.createElement('div');
tag.className = 'tag';
tag.textContent = name;
document.body.appendChild(tag);
return { mesh, tag };
});
const v = new THREE.Vector3();
renderer.setAnimationLoop((t) => {
const a = t / 5000;
camera.position.set(Math.sin(a) * 9, 5, Math.cos(a) * 9);
camera.lookAt(0, 0.5, 0);
camera.updateMatrixWorld(); // project() needs the current camera matrices
for (const { mesh, tag } of units) {
v.copy(mesh.position).setY(mesh.position.y + 0.9); // a point just above the head
v.project(camera); // world -> NDC (-1..1)
const visible = v.z < 1; // z > 1 means behind the camera
tag.style.display = visible ? '' : 'none';
tag.style.left = `${(v.x * 0.5 + 0.5) * innerWidth}px`;
tag.style.top = `${(-v.y * 0.5 + 0.5) * innerHeight}px`; // NDC y is up, CSS y is down
}
renderer.render(scene, camera);
});
</script>HTML labels are crisp and easy to style, and fine for a few dozen. For hundreds, draw them in WebGL instead (sprites, or text meshes with a library such as troika-three-text).
Camera settings that affect gameplay
| Setting | Effect | Game advice |
|---|---|---|
fov |
vertical field of view | 60–75 for third person; first-person often lets players choose (convert from the horizontal FOV they expect) |
near / far |
clipping planes | keep far / near small for depth precision; use fog to hide the far plane |
zoom |
magnification (both camera types) | aim-down-sights effects, map zoom; call updateProjectionMatrix() |
layers |
which object layers this camera renders | e.g. a minimap camera that sees map icons the main camera skips |
A camera's layers work like a bit mask: object.layers.set(1) puts an object on layer 1 only, and camera.layers.enable(1) makes a camera render it. Raycasters have layers too.
Test yourself
In an orthographic view, you move the camera twice as far from the scene. What happens to the size of objects on screen?
- They become half as big
- They stay the same; only
zoomor the frustum size changes it - They become twice as big
- They disappear
After v.project(camera), v.x = 0 and v.y = 0. Where is the point on screen?
- Top-left corner
- The centre of the canvas
- Bottom-left corner
- Off screen
How do you draw a minimap in the corner with a second camera?
- Create a second renderer for it
- Render the main view, then set a small viewport and scissor and render again with the minimap camera
- Use
camera.layersalone - It needs a render target