Get your API key

Lesson 4 of 6

Add a third dimension with Three.js

Scene, camera, light, object, renderer and loop. Why a 3D file refuses to load when you double-click it, and how a local server fixes it.

9 min Log in to open
One step at a time, like flipping pages.

By the end of this lesson you will have a 3D scene in the browser, and you will know why a 3D page that works from a server shows nothing when you open the file directly.

You need: the folder from the last lesson, and the local server command from it · Time: 9 minutes

  1. A 2D game knows two directions: left and right, and up and down. A 3D scene adds a third direction, depth, which is distance away from the camera. People write these three directions as x (left to right), y (down to up) and z (near to far).

    That single extra direction changes how everything is drawn, because the browser can no longer paint flat shapes in an order. It has to work out which object is in front of which, and it has to shrink objects that are further away, exactly like your eye does. A camera does that work: it is the position and the direction from which the scene is looked at.

    Why?

    In 2D you draw what you want to see. In 3D you place objects in a world and a camera decides what is visible. That is a different way of thinking, and it is why 3D code looks unfamiliar even when the ideas are the same.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Six parts, and one missing part means a black screen
  2. The first 3D prompt
  3. Why the file must be opened through a server
  4. Matching the six parts to what they do
  5. Recap