Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 39 ยท Projects and Practice

Project: Build an Interactive Solar System

Build a Three.js solar system step by step: orbit pivots, procedural planet textures, rings, moons, a starfield, OrbitControls, hover labels and click-to-focus camera.

Time to use everything from the previous chapters in one program. In this project you build a solar system that you can orbit around, speed up and slow down, and click to fly to a planet. It combines the scene graph (nested orbits), textures (drawn in code, so no downloads), lights, Points for stars, raycasting for hover and click, and controls for the camera.

The planets use stylized sizes and distances, not real scale: if Earth were one pixel, the Sun would be over a hundred pixels wide and Neptune would be kilometres away. Making it readable matters more than being exact.

The plan

Build order

  1. Sun and light
A glowing sphere plus a PointLight
  1. Orbit pivots
Empty groups that rotate; planets sit at an offset
  1. Textures
Painted on a canvas per planet
  1. Rings, moons, stars
RingGeometry, nested pivot, Points

Then add controls, labels and click-to-focus.

1 / 2

Step 1: orbits with pivots

You never compute x = cos(angle) * radius for every planet. Instead, put an invisible Group (a pivot) at the center of the orbit, make the planet a child offset along x by the orbit radius, and rotate the pivot. The planet then circles the center automatically. A moon is the same idea one level deeper: a pivot inside the planet's group, with the moon as its child. Because transforms are inherited, when the planet moves, the moon's pivot moves with it.

The first version has just a sun and three planets with solid colors. All the motion comes from one function, setTime(t), so we can also test it with numbers.

Step 1: the pivot trick

At t = 0.25 Earth has moved a quarter turn but is still 7 units from the Sun: the pivot rotates, the offset stays. The same pattern drives every orbit, spin and moon in the full version.

Step 2: textures, rings, moons and stars

Now make it look good. Everything is generated in code:

  • Planet textures: a function paints bands (gas giants) or blotches (rocky planets) on a small 2D canvas, then wraps it in a CanvasTexture. Cheap, and the sphere's UVs wrap the image around.
  • Saturn's rings: RingGeometry's UVs run in a flat square, which stretches a texture badly. The fix is to remap the UVs by distance from the center, so a horizontal gradient becomes concentric bands.
  • Moon: a nested pivot inside the Earth group.
  • Stars: a few thousand random points on a large sphere in a Points object, with a single draw call.
  • Spin: each planet also rotates around its own axis (mesh.rotation.y).

Step 3: interaction

  • OrbitControls for exploring.
  • A speed slider that scales time.
  • Hover labels: a raycast against the planet meshes each pointer move, showing a small HTML tooltip.
  • Click to focus: clicking a planet sets focus; each frame the controls' target is smoothed toward the planet's world position (controls.target.lerp(position, 0.08)), so the camera follows it around its orbit.
The complete solar system

Drag to orbit, scroll to zoom, hover for names, click a planet to follow it and click the Sun (or press Reset view) to let go. Notice the camera follow: moving both controls.target and camera.position by the same amount keeps your chosen viewing angle while the planet travels.

Extensions to try

  1. Add axial tilt: rotate the planet mesh's parent Group around z by 0.4 rad (Earth's tilt is about 23 degrees).
  2. Replace the canvas textures with real texture images served from your own origin (see textures).
  3. Add bloom around the Sun with post-processing, or a glow sprite.
  4. Show a distance scale or an info panel with facts (HTML overlay) when a planet is selected.
  5. Add an asteroid belt as an InstancedMesh of 2,000 rocks (instancing); it is one draw call.
  6. Use eccentric orbits: place planets on an ellipse with x = a * cos(theta) and z = b * sin(theta) instead of a pivot (then speed changes with distance, as Kepler described).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do we put each planet inside a rotating pivot Group instead of computing cos and sin positions?

    Transforms are inherited down the scene graph. A rotating parent moves the offset child on a circle, and a child pivot inside a planet makes a moon follow that planet.
  2. 2. Why must the Saturn ring UVs be remapped?

    We recompute u from the distance to the ring's center so that a horizontal gradient maps onto radial bands.
  3. 3. How does the camera follow a clicked planet while still letting the user orbit?

    Shifting both the target and the camera by the same amount keeps the relative viewing direction while tracking the planet.
  4. 4. How many draw calls do the 3,000 stars cost?

    A Points object draws all its vertices in a single call.

Summary

  • Nested pivot groups give you orbits, moons and spin with a single rotation per level.
  • Textures can be generated on a canvas; remap UVs when a built-in geometry's mapping does not suit the texture (rings).
  • Points draws thousands of stars in one call; raycasting gives hover labels and clicking.
  • A follow-camera moves the controls' target and the camera together.
  • Extend it with tilt, bloom, asteroid belts and real textures.

Next: Project: A Product Configurator