Three.js

Pat LiemoPat Liemo
3 min read

If you’ve ever looked at a 3D website and thought, “Wow, this is cool. I should try making one.”—congratulations, you’ve just unlocked a new level of developer suffering. Enter Three.js, the JavaScript library that lets you create 3D graphics in the browser.

Sounds simple, right? Wrong.

What Even Is Three.js?

Three.js is a WebGL wrapper that makes it easier to create 3D scenes in the browser. Without it, you'd have to write raw WebGL code, which is basically like writing assembly language for your GPU (aka, suffering).

With Three.js, you can do things like:
✅ Render 3D models in the browser
✅ Add lighting, shadows, and reflections
✅ Build interactive experiences
✅ Question your life choices when your scene refuses to render

Step 1: Setting Up Three.js

Before you dive in, you need to include Three.js in your project. You can install it via npm:

npm install three

Or just grab the CDN link if you like living on the edge:

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>

Step 2: Creating Your First Scene

Alright, time to conjure a 3D world. In Three.js, everything revolves around three main things:

  • Scene – The virtual world where everything lives.

  • Camera – Your eyes into that world.

  • Renderer – The thing that actually displays your suffering—I mean, your beautiful work.

Here’s the bare minimum setup:

// Create a scene
const scene = new THREE.Scene();

// Create a camera (PerspectiveCamera gives depth)
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

// Create a renderer and attach it to the document
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// Position the camera
camera.position.z = 5;

// Render the scene
function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}

animate();

At this point, if you run the code, you’ll see… nothing. Because we forgot to add an object. Classic.

Step 3: Adding a Cube (Because Every 3D Journey Starts With a Cube)

Every Three.js tutorial ever starts with a rotating cube, so let’s do the same:

// Create a cube
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);

// Add the cube to the scene
scene.add(cube);

// Animate the cube
function animate() {
    requestAnimationFrame(animate);

    // Rotate the cube
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;

    renderer.render(scene, camera);
}

animate();

And boom—you now have a spinning green cube. Is it groundbreaking? No. But does it make you feel like a 3D wizard? Absolutely.

Step 4: Lighting & Shadows (Because Life Needs Drama)

Right now, our cube looks flat. That’s because light doesn’t exist in this world yet. Let’s add some:

// Add a directional light
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);

Now the cube actually casts shadows and looks more realistic. At this point, you might start feeling powerful. Don't get too cocky—your next step is debugging why your 3D model isn’t loading.

Step 5: Loading 3D Models (Where Things Get Real)

Eventually, cubes won’t cut it. You’ll want to import real 3D models—which is where GLTF/GLB files come in.

import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';

const loader = new GLTFLoader();
loader.load('model.glb', function (gltf) {
    scene.add(gltf.scene);
}, undefined, function (error) {
    console.error('An error happened:', error);
});

If it works: You’re a genius.
If it doesn’t: Welcome to 3D debugging hell.

Final Thoughts: Is Three.js Worth the Pain?

Absolutely. Sure, you’ll rage, you’ll refresh your browser a thousand times, and you’ll question your existence when an object refuses to appear. But once you finally get it working, you’ll stare at your screen in awe, whispering, “I made that.”

So if you’re a developer looking to step into the third dimension, Three.js is a great place to start. Just be prepared for an adventure—one filled with floating cubes, misplaced textures, and a newfound appreciation for 3D artists.

#ThreeJS #WebGL #3DDevelopment #JavaScript #SendHelp

0
Subscribe to my newsletter

Read articles from Pat Liemo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pat Liemo
Pat Liemo

Software Engineer | Backend Developer | Full stack Developer | Prop Tech | Data Privacy Enthusiast