successgraph®
Build a 3D product viewerThree.jsLight the RainBlenderThe Seven-Cent ShotAI filmmakingBuild a Cute Cartoon WorldThree.jsThe Seed ShotThree.js

Three.js / JavaScript 7:52

Three.js Tutorial for Beginners: Build a 3D Product Viewer

By SuccessGraph®

Rate this class

Chapters

  1. 0:00See the finished 3D speaker
  2. 0:21Download and extract the exercise files
  3. 0:33Run Three.js locally with Vite
  4. 1:12Create a scene, camera, and mesh
  5. 2:19Model the cabinet, grille, and dials
  6. 3:40Light the speaker and add shadows
  7. 4:40Add drag and zoom with OrbitControls
  8. 6:05Switch finishes and camera angles
  9. 7:05Adapt the viewer to another product

What you’ll learn

Build an interactive 3D product viewer in Three.js. Model a retro speaker, set up studio lighting, add OrbitControls, and switch product finishes in the browser.

Start with a cube to learn how the scene, camera, and renderer fit together. Build the cabinet, grille, and dials as one group, then add studio lights, drag and zoom controls, and clickable finishes. Reuse the viewer with a different product by replacing the group and adjusting the camera target.

Use a desktop browser with WebGL 2, Node.js, and a code editor. Basic JavaScript familiarity helps. The exercise folder includes a Vite project and five runnable checkpoints.

Project resources

Class transcript

English · 9 chapters
▶  0:00

See the finished 3D speaker

Turn the speaker a little and the light catches the edge of the dial. Scroll closer and you can see the grille. Now watch the cabinet change from Apricot to Moss without moving the camera.

In this tutorial, we’ll build this little product viewer in Three.js, starting with an empty scene and a single cube.

▶  0:21

Download and extract the exercise files

Before we start, download the exercise files at successgraph.com/relay.zip. Extract the ZIP, then find the viewer folder inside.

▶  0:33

Run Three.js locally with Vite

Open the viewer folder in your editor, then open a terminal there. You'll need Node.js. Run npm install once to fetch the dependencies, then run npm run dev. Open the local address printed by Vite. Keep the browser and your editor open side by side so you can see what changes as you save.

Add ?stage=1 to the end of that address. You should see a single cube. Each checkpoint keeps one more part of the finished viewer. We’ll read the code that makes the cube, change it ourselves, and use the checkpoints to compare what each new piece does.

▶  1:12

Create a scene, camera, and mesh

Begin with three things. The scene is where the objects live. The camera chooses the view, a little like a real camera on a tripod. And the renderer takes that scene and camera and draws them into a canvas on the page. If you can point to those three lines, you can already read the basic shape of a Three.js project.

The cube is a mesh. Its box geometry describes the shape, and its material describes the surface. We pass both to the mesh and add it to the scene. Try changing the cube’s width, the first number in BoxGeometry, and save.

The browser updates so you can see the box stretch. Then put the original value back. The camera is a perspective camera, so nearer things appear bigger. Its position sets where we stand.

Its target sets where we look. When the canvas changes size, update the camera’s aspect ratio from its width divided by its height. Then update its projection and resize the renderer.

That keeps the cube from stretching when you resize the window.

▶  2:19

Model the cabinet, grille, and dials

Go to stage two. The cube has become the speaker, but the scene follows the same rules. In this project Y points up and the front faces positive Z. Find the speaker file and start with the cabinet. Its outer shell is a rounded box.

A separate, darker plate sits just in front of it. Build the large pieces first and work inward. A rounded box gives the cabinet soft edges. For each dial, stack a shallow cylinder, a thicker body, and a flat circular face.

A little spacing along the Z axis gives each layer a face of its own. From the side, those offsets are much easier to see. Put the parts into a group. A group is a parent for related objects.

Position the knobs and grille relative to the cabinet, then add the whole speaker to the scene. If you move the parent, all those pieces move with it. That lets you lay out the speaker once and place it anywhere you need later.

The cabinet, front plate, and hardware each get a material. This checkpoint uses flat color so we can judge the shapes. In the next one, the materials react to light. Lower roughness means a sharper highlight.

Metalness controls how much the surface behaves like metal. We'll be able to see those differences once we add the lights.

▶  3:40

Light the speaker and add shadows

Keep an eye on the outline of the cabinet as we go to stage three. This adds the studio floor and the fuller lighting setup. Look again at the front edge. You can see the brighter face on one side and a softer shade on the other.

The geometry stayed the same. Now the light tells you more about its shape. The hemisphere light fills the scene softly, with one color from above and another from below. The first directional light acts like rays coming from one direction.

That's our key light. The second directional light is behind the speaker. It catches the rim so you can see where the rounded edge meets the background. The floor gives the speaker somewhere to sit.

Look underneath it for the contact shadow, the soft dark area where the product meets the ground. That little anchor helps your eye judge the distance between the object and the background.

Keep that cue visible even when you want the background to feel simple.

▶  4:40

Add drag and zoom with OrbitControls

Open stage four and drag across the product. The speaker stays in place while the camera orbits around a target. You can scroll to move closer and back away. When you let go after a drag, the motion settles. This is the first checkpoint where you can choose what part of the speaker to inspect.

Open the camera file. OrbitControls comes from the Three.js addons. Give it the camera and the renderer's canvas so it can listen where the viewer is drawn. Its target is the point the camera orbits around. Keep that target near the middle of the cabinet so the speaker stays the focus of a drag.

Enable damping to ease the end of a drag. Damping needs the controls to update each frame. In the animation loop, run that update before rendering, so the canvas shows the latest camera position.

The same loop gives us a place to move the camera gradually when someone picks a view in the next checkpoint. Try zooming all the way in and then all the way out. The controls have a minimum and a maximum distance.

Look for those settings in the camera file. They keep the camera from passing into the cabinet or drifting so far away that the detail is hard to see. A wider item might need different limits.

▶  6:05

Switch finishes and camera angles

Now open stage five. Pick Moss and watch the cabinet. The scene, camera, and speaker are already there. A swatch sends the finish choice to the speaker code, which changes the color on the cabinet material that's already in use. Try Ink next.

The grille and knobs keep their own materials, so they look the same. A named view is a shortcut to a useful angle. Choose Front to see the grille straight on. Each preset stores a different camera position.

We keep the same target, and let the camera sweep around it so your eye can follow the speaker. You can still drag around once you get there. Switch on auto-rotate to see the speaker sweep past the light.

Then use Reset. It stops the rotation and takes the camera back to the opening angle. The selected cabinet finish stays put. It helps to think of the finish and the camera as two different choices, so resetting the view only resets the part you expect.

▶  7:05

Adapt the viewer to another product

To make this yours, start with one small change. Find the finish colors and try a new color for Apricot. Save, reload if needed, and choose Apricot again. The effect runs through the same swatch and material code you just saw.

For a new product, replace the speaker parts as a group and adjust the camera target to match. We started with a cube and ended with a speaker you can turn, zoom into, and recolor. When you build your next viewer, try getting the shape on screen first. Then bring in the light, add the controls, and give people a few useful ways to see the thing you made.

Thanks for watching. If you enjoyed this video, subscribe to successgraph® for more. [bell chimes]