What you'll learn
- How the three.js coordinate system works: x points right, y points up, z points toward you, and (0, 0, 0) is the center of the world.
- What the four numbers in PerspectiveCamera(fov, aspect, near, far) do, and why objects sometimes vanish when the camera gets close.
- The difference between a perspective camera and an orthographic camera, and when games use each one.
- The three-line resize rule every 3D page needs, including the step almost everyone forgets.
A world made of three numbers
In Part 1 you put a cube on stage and stepped the camera back with camera.position.z = 3. But what does z mean? Where is "3"? Every object in a three.js scene has a position: three numbers, x, y, and z, that say where it is. The point (0, 0, 0) is called the origin. It is the center of the world, and a brand new object always starts there.
Imagine standing in the exact middle of a room, facing a wall. Right and left is the x direction. Floor to ceiling is y. Toward you and away from you is z. three.js uses what is called a right-handed system, which is a fancy way of saying: positive x goes right, positive y goes up, and positive z comes toward you, out of the screen. So an object at z = -3 is three steps away from you, deeper into the screen.
x
red line: left and rightBigger x moves an object to the right. Negative x moves it left. The red line in the demo points toward positive x.
y
green line: up and downBigger y moves an object up, toward the ceiling. The grid in the demo is the floor, sitting at y = 0.
z
blue line: toward and awayBigger z moves an object toward you. Negative z pushes it away. That is why the camera stands at positive z to look at the origin.
You never see the world directly. What lands on your screen is a photo of it, taken by the camera and painted by the renderer. Every "why can't I see my object?" problem is really a question about the camera: where it is, which way it faces, how wide its lens is, and how far it can see.
Simple definition: a position is three numbers (x, y, z) measured from the origin. Units are whatever you decide: one unit can be a meter, a tile, or a cube. What matters is that everything in your scene, including the camera, agrees.
The camera and the renderer, properly
We reuse the setup from Part 1 (the scene, the renderer, and the two lights) and change what is on stage. Instead of one spinning cube, we draw the axes themselves and place four boxes at meaningful spots, so each number connects to a place in the world.
1. Making the axes visible
three.js ships with helpers: objects that exist only to help you see what is going on. AxesHelper(2) draws three lines from the origin, each 2 units long: red for x, green for y, blue for z. GridHelper(10, 10) draws a 10-by-10 floor at y = 0. Both are regular objects, so you scene.add them like anything else.
Then the boxes. A small makeBox function builds a 1-by-1-by-1 cube in any color, and each box gets exactly one position number changed so you can see what that number does.
// Helpers: red = x, green = y, blue = z. The grid is the floor at y = 0. const axes = new THREE.AxesHelper(2); const grid = new THREE.GridHelper(10, 10, 0x3b4a63, 0x1f2a40); scene.add(axes, grid); // A 1x1x1 cube in any color (same idea as Part 1's cube) function makeBox(color) { const geometry = new THREE.BoxGeometry(1, 1, 1); const material = new THREE.MeshStandardMaterial({ color }); return new THREE.Mesh(geometry, material); } // Four boxes, each moved along ONE axis so the numbers mean something const rightBox = makeBox(0xf4a261); rightBox.position.x = 2; // 2 steps to the right const upBox = makeBox(0x4cc9f0); upBox.position.y = 1.5; // 1.5 steps up const backBox = makeBox(0xa78bfa); backBox.position.z = -3; // 3 steps away from you const frontBox = makeBox(0x34d399); frontBox.position.z = 1.5; // 1.5 steps toward you scene.add(rightBox, upBox, backBox, frontBox);
This is exactly what both stages on this page are running. Notice the orange box is half sunk into the grid: position is the center of a box, and a 1-unit box centered at y = 0 reaches half a unit below the floor.
2. The camera, properly
A PerspectiveCamera works like a phone camera: far things look smaller, and lines that are really parallel seem to meet in the distance. Its four numbers are:
- fov, the field of view in degrees: how wide-angle the lens is. A small fov (20) is a zoomed-in telephoto lens: things look big and only a little of the world fits. A huge fov (120) is a fisheye lens: a lot fits, but the edges stretch and warp. Most games use 60 to 75.
- aspect, the width of the picture divided by its height. If this does not match the canvas, everything looks squished or stretched.
- near and far, the slice of the world that gets drawn. Anything closer than near or farther than far is clipped: simply not drawn. This is why objects sometimes "disappear" when the camera gets too close. Keep the numbers sensible (0.1 and 100 is a fine start); a gigantic far makes the GPU worse at telling which surface is in front.
Two more tools place the camera. camera.position.set(x, y, z) sets all three coordinates at once, and camera.lookAt(x, y, z) turns the camera so that point is in the middle of the picture. A camera in the right place but facing the wrong way sees nothing, so aim it.
// PerspectiveCamera(fov, aspect, near, far) const camera = new THREE.PerspectiveCamera( 75, container.clientWidth / container.clientHeight, 0.1, 100 ); camera.position.set(0, 2.5, 6); // up a little, and 6 steps back camera.lookAt(0, 0, 0); // aim at the center of the world // The resize rule. Run it whenever the canvas changes size. function resize() { const w = container.clientWidth; const h = container.clientHeight; camera.aspect = w / h; camera.updateProjectionMatrix(); // the step people forget! renderer.setSize(w, h); } window.addEventListener('resize', resize);
Changing fov, aspect, near, or far does nothing on its own. The camera bakes those settings into a projection matrix (the math that squashes 3D onto a flat picture), and updateProjectionMatrix() re-bakes it. The stages on this page listen with a ResizeObserver instead of the window event, because they are boxes inside the page rather than the whole window.
The renderer has fewer settings. setSize(w, h) makes the canvas that big. setPixelRatio(Math.min(window.devicePixelRatio, 2)) keeps the picture sharp on high-resolution screens without drawing more pixels than the screen can show. There are two ways to pick the backdrop color: scene.background = new THREE.Color(0x0b1220) belongs to the scene, and renderer.setClearColor(0x0b1220) belongs to the renderer. They look the same; the scene version wins if you set both.
3. Perspective vs. orthographic
There is a second kind of camera. An OrthographicCamera has no perspective at all: things do not get smaller with distance, and parallel lines stay parallel, the way a blueprint draws a building. That is exactly what you want for 2D-style games, isometric games, and on-screen menus. Instead of a field of view, you give it the edges of the box it can see: left, right, top, bottom, near, far.
To keep the picture from stretching, work out those edges from the canvas's aspect ratio and a frustumSize, the number of world units the view is tall. Keep both cameras alive and swap which one you hand to renderer.render; there is no need to rebuild the renderer.
// OrthographicCamera(left, right, top, bottom, near, far) const frustumSize = 8; // the view is 8 world units tall const aspect = container.clientWidth / container.clientHeight; const ortho = new THREE.OrthographicCamera( -frustumSize * aspect / 2, frustumSize * aspect / 2, // left, right frustumSize / 2, -frustumSize / 2, // top, bottom 0.1, 100 // near, far ); ortho.position.copy(camera.position); // same spot as the perspective camera ortho.lookAt(0, 0, 0); // Keep both cameras. Swap which one the renderer looks through. let activeCamera = camera; function useOrthographic(on) { activeCamera = on ? ortho : camera; } function animate() { renderer.render(scene, activeCamera); } renderer.setAnimationLoop(animate); // Inside resize(): the orthographic camera's edges depend on aspect too ortho.left = -frustumSize * aspect / 2; ortho.right = frustumSize * aspect / 2; ortho.updateProjectionMatrix();
The word frustum is the shape of everything a camera can see. For a perspective camera it is a pyramid with the tip cut off (wide far away, narrow up close). For an orthographic camera it is a plain box, which is why nothing shrinks.
Move the camera
This stage is running the code above. The sliders change the camera's numbers live, the toggles swap which camera the renderer uses and where it is aimed, and the "live code" box shows the lines that changed. The boxes never move. Only the camera does, and yet the picture changes completely.
Loading 3D preview…
PerspectiveCamera · fov 75°const camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 100); camera.position.set(0, 2.5, 6); camera.lookAt(0, 0, 0); renderer.render(scene, camera); // the perspective camera
- Push the field of view up to 120. Now the lens is a fisheye. The boxes in the middle shrink, and the grid lines near the edges stretch and bend outward. Slide it down to 20 and it turns into a telephoto lens: the boxes look huge and close, even though the camera has not moved an inch. That is the difference between zooming (fov) and walking closer (distance).
- Slide the near plane up slowly. Around 4.6 the green front box starts getting sliced open, and by about 5.6 it is gone. Nothing was deleted: the box is closer to the camera than near, so it is clipped, and the renderer skips it. Keep going and the grid and the other boxes start vanishing too. This is the "my object disappeared when I got close" bug, in slow motion.
- Switch to Orthographic. The purple back box is 3 units farther away than the center, but it is now drawn exactly the same size as the front box, and the grid lines run perfectly parallel instead of meeting in the distance. The field-of-view slider is greyed out because an orthographic camera has no fov. Try changing the distance too: in orthographic, moving the camera back does not make anything smaller.
Common mistakes
Camera bugs are the sneakiest bugs in 3D because nothing crashes. The screen just shows the wrong thing, or nothing at all.
You set camera.fov = 30 (or changed camera.aspect) and the picture did not change at all. The new number is stored, but the camera is still using the old projection matrix.
Fix: call camera.updateProjectionMatrix() right after changing fov, aspect, near, or far. Position and rotation do not need it; only those four settings do.
You moved the camera to (0, 5, 0), straight above the scene, and now you see only background. A new camera looks down the negative z axis. From up there, that is sideways, not down.
Fix: add camera.lookAt(0, 0, 0) after you set the position. Any time you move the camera, aim it again.
Your object is definitely in the scene, but it is invisible, or you see a hole cut through it. It is closer than near or farther than far, so the renderer skips it.
Fix: use a small near like 0.1 and a far big enough for your world, like 100 or 1000. Then call updateProjectionMatrix(). Avoid silly values like far = 1000000, which makes distant surfaces flicker.
The scene looked fine, then the window got resized and now every cube is a squashed rectangle. The canvas changed shape but camera.aspect still holds the old width-to-height ratio.
Fix: the three-line resize rule from camera.js: set camera.aspect = w / h, call camera.updateProjectionMatrix(), then renderer.setSize(w, h). Run it on every resize event.
Check your understanding
A friend's scene renders, but after they drag the browser window wider, everything looks squished wide. Name the two things they probably need to update when the window size changes, and say which function makes the camera change take effect.
Your answer:
Sample answer: they need to update the camera's aspect to the new width divided by height and then call camera.updateProjectionMatrix() so the camera actually uses it, and they need to call renderer.setSize(w, h) so the canvas matches the new window. If they only did one of those, the picture would still be stretched or the canvas would be the wrong size.
Quick quiz
1. In three.js's coordinate system, which way does the positive z axis point?
2. You change the camera's fov from 75 to 30 and call updateProjectionMatrix(). What happens to the picture?
3. A box sits 0.5 units in front of the camera, and the camera's near is set to 1. What do you see?
Keep learning
You can now place anything with three numbers, aim a camera on purpose, and fix a stretched or clipped scene in seconds. Next, look at the objects themselves: what a geometry really is, how shapes are built from triangles, and how to swap a box for a sphere, a cone, or a torus knot.
Part of the Games and Animations with Three.js tutorial series.