Hamed Mohamed Logo
EN
•Hamed Mohamed

How to Build an Interactive 3D Website with Three.js, Step by Step

From an empty page to an interactive Three.js scene: Draco model prep, raycasting, UI controls, and the performance rules that keep the page fast on mobile.

#threejs#nextjs#tutorial#webgl
How to Build an Interactive 3D Website with Three.js, Step by Step

TL;DR: An interactive 3D website is a normal web page with one well-behaved WebGL canvas inside it. Build the scene in isolation, compress the model with Draco, let regular HTML buttons drive the scene state, and lazy-load the whole thing so the page around it stays fast and indexable. The steps below are the same ones behind the 3D design lab I shipped for Genç Group in Istanbul.


Step 0: Decide What the 3D Has to Prove

Do not start with the model. Start with the decision the visitor has to make. On the Genç Group platform, the 3D studio exists so an investor or client can judge a building's massing, facade material, and light before any sales conversation. Every control in that scene answers a real question. If your scene cannot answer a question a product photo cannot, a photo is the cheaper tool.

Write down three things before writing code:

  • The one object the visitor inspects (a building, a machine, a product).
  • The two or three states they switch between (materials, configurations, day and night).
  • The action after exploring (a quote form, a booking, a call).

Step 1: Prepare the Model (glTF + Draco)

Export the model as glTF (.glb) and compress its geometry with Draco. Uncompressed CAD or DCC exports are usually far too heavy for a web page, and download time is where most 3D sites lose their visitors.

# gltfpack or the glTF Transform CLI both produce Draco-ready files
npx @gltf-transform/cli copy model.glb model-web.glb
npx @gltf-transform/cli draco model-web.glb model-draco.glb

Two preparation rules matter more than any setting:

  1. Merge what does not move. Parts the visitor never separates should be one mesh, not hundreds of draw calls.
  2. Bake what does not change. Static shadows and ambient occlusion belong in textures, not in a real-time light rig.

Step 2: The Minimal Scene

A Three.js scene needs four things: a scene, a camera, a renderer, and a light setup honest enough to read the object. This is the smallest version that is actually useful:

import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js";
 
export function createScene(canvas: HTMLCanvasElement) {
  const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
  renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
 
  const scene = new THREE.Scene();
  const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 500);
  camera.position.set(14, 9, 16);
 
  const sun = new THREE.DirectionalLight(0xffffff, 2.4);
  sun.position.set(18, 24, 10);
  scene.add(sun, new THREE.AmbientLight(0xffffff, 0.5));
 
  const draco = new DRACOLoader();
  draco.setDecoderPath("/draco/");
  const loader = new GLTFLoader();
  loader.setDRACOLoader(draco);
  loader.load("/models/project.glb", (gltf) => scene.add(gltf.scene));
 
  const controls = new OrbitControls(camera, canvas);
  controls.enableDamping = true;
  return { renderer, scene, camera, controls };
}

The pixel ratio cap at 2 is deliberate. Phones with a ratio of 3 or 4 render four times the pixels for a difference most visitors cannot see, and the GPU pays for it on every frame.


Step 3: Make It Interactive (Raycasting + Real UI)

Interactivity has two halves. Inside the canvas, raycasting turns pointer positions into object picks:

const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
 
canvas.addEventListener("pointermove", (event) => {
  const rect = canvas.getBoundingClientRect();
  pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
  pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
  raycaster.setFromCamera(pointer, camera);
  const hit = raycaster.intersectObjects(pickableMeshes)[0];
  canvas.style.cursor = hit ? "pointer" : "grab";
});

Outside the canvas, keep the controls as ordinary HTML. Buttons, tabs, and sliders that switch materials or lighting are easier to build, easier to translate, and easier for search engines and screen readers than anything drawn inside WebGL. In the Genç studio, the massing, facade, and lighting switches are plain page UI; the canvas only renders the result. State lives in the page, the scene subscribes to it.


Step 4: Put the Canvas on the Page Without Hurting the Page

  • Lazy-mount the scene. Load Three.js only when the section approaches the viewport (an IntersectionObserver is enough). The rest of the page should be server-rendered HTML that works with the canvas absent.
  • Show a poster first. A still render of the model with a "load 3D" state keeps layout stable and gives slow connections something real to look at.
  • Reserve the box. Give the canvas container a fixed aspect ratio so the page does not jump when WebGL starts.
  • Pause off-screen. Stop the render loop when the section leaves the viewport; a hidden animation loop is a battery and fan problem, not a feature.

Step 5: Clean Up Like You Mean It

Every geometry, material, and texture you allocate holds GPU memory until you release it. On unmount or model switch:

export function disposeScene(scene: THREE.Scene, renderer: THREE.WebGLRenderer) {
  scene.traverse((obj) => {
    if (obj instanceof THREE.Mesh) {
      obj.geometry.dispose();
      const materials = Array.isArray(obj.material) ? obj.material : [obj.material];
      materials.forEach((m) => m.dispose());
    }
  });
  renderer.dispose();
}

Skip this and a single-page site that swaps models will leak memory until the tab slows down. This is the bug I see most often in inherited 3D code.


Mistakes That Make 3D Sites Feel Broken

  1. The whole page waits for the model. If the headline and the text load only after a 20 MB file arrives, the 3D is decoration tax, not a feature.
  2. Controls fight the scroll. A canvas that traps every touch gesture makes the page unusable on phones. Limit orbit gestures to the canvas and let vertical page scroll win at the edges.
  3. No fallback. WebGL can be unavailable (old devices, locked-down browsers). Detect failure and leave the poster image and the text; the page must still sell without a single rendered frame.

Where This Pattern Runs in Production

This exact structure, client-isolated Three.js with Draco models and page-driven state, is what I built for the Genç Group corporate platform: an architectural studio that switches massing, facade materials, and lighting in real time, plus a 4-stage interactive automotive anatomy. The architecture-specific version of this tutorial is in Next.js and Three.js for architecture websites, and the framework integration details are in using Three.js inside Next.js. If you would rather have it built for you, see interactive 3D website development.

Want an interactive 3D section that does not slow your site down?

Send me your product or project on WhatsApp and I will tell you honestly whether 3D will help it sell, and what it would take to build.

355 sitemap pages registered in Search ConsoleLive Website

Healix Medical: 5-Language International Patient Coordination Platform

Case study: 5-language patient coordination platform in Istanbul. 355 pages in the live sitemap, Arabic RTL, 33 contracted healthcare facilities.

1,713

QR Attendance System

QR attendance system built by Hamed Mohamed for 1,713 users (1,666 students, 40 academics, 7 admins) across 6 faculties: 98% latency reduction, 99.9% uptime.