cross icon
AnimationGetting Started with Three.js: A Beginner’s Guide to Web 3D

Getting Started with Three.js: A Beginner’s Guide to Web 3D

4 mins Read
mainImg

Build with Radial Code

Radial Code Enterprise gives you the power to create, deploy and manage sites collaboratively at scale while you focus on your business. See all services.

In the era of immersive web experiences, 3D graphics have become an integral part of modern web development. Three.js, a powerful JavaScript library, allows developers to create stunning 3D visuals directly in the browser. This guide will introduce you to the basics of Three.js and help you take your first steps into Web 3D.

What is Three.js?

What is Three.js?

Three.js is an open-source JavaScript library that simplifies the process of rendering 3D graphics using WebGL . It provides an abstraction over WebGL, making it easier for developers to create complex 3D scenes without dealing with low-level graphics programming.

Setting Up Three.js

To get started with Three.js, you need to include the library in your project. You can obtain Three.js from its official website or integrate it via a CDN.

Using a CDN:

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

Alternatively, you can install it via npm if you're using a build tool:

npm install three

Creating Your First Three.js Scene

Creating Your First Three.js Scene

A Three.js scene consists of three essential components:

  • Scene - The scene acts as a container that holds all objects, lighting, and cameras.
  • Camera - Defines the perspective of the viewer.
  • Renderer - Renders the scene using WebGL.

Basic Three.js Boilerplate:

import * as THREE from 'three';
// Create Scene
const scene = new THREE.Scene();
// Create Camera
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;

// Create Renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// Animation Loop
function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}
animate();

Adding Objects to the Scene

adding-object

Three.js provides a variety of geometries that you can add to your scene. The following example creates a spinning cube.

Adding a Cube:

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

// Update Animation Loop
function animate() {
    requestAnimationFrame(animate);
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;
    renderer.render(scene, camera);
}
animate();

Adding Lights

Lighting enhances the realism of your scene. Three.js supports multiple light types, such as ambient, directional, and point lights.

Adding a Light Source:

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

Applying Textures and Materials

Adding textures can greatly enhance the realism of your 3D objects. You can load textures using THREE.TextureLoader and apply them to materials.

Adding a Texture:

const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load('path/to/texture.jpg');
const material = new THREE.MeshBasicMaterial({ map: texture });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);

Handling User Interaction

You can allow users to interact with the scene using mouse or keyboard inputs. The OrbitControls module provides an easy way to enable camera movement.

Adding Orbit Controls:

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
const controls = new OrbitControls(camera, renderer.domElement); 

Animations and Motion

Animations bring your 3D objects to life. You can use requestAnimationFrame to create smooth transitions and movements.

Creating an Animated Object:

function animate() {
    requestAnimationFrame(animate);
    cube.rotation.x += 0.02;
    cube.rotation.y += 0.02;
    renderer.render(scene, camera);
}
animate(); 

Optimizing Performance

optimizing-performance

Rendering 3D graphics can be performance-intensive. Here are a few optimization tips:

  • Use Low-Poly Models: Reduce complexity where possible.
  • Optimize Textures: Use compressed and lower-resolution textures.
  • Limit the Number of Lights: Too many dynamic lights can slow down performance.
  • Enable Shadows Sparingly: Shadows are computationally expensive, so use them only when necessary.
  • Use Frustum Culling: Render only objects that are visible in the camera’s view.

Exporting and Loading 3D Models

exporting-and-loading

Instead of manually creating objects, you can import 3D models created in software like Blender.

Loading a 3D Model:

import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
const loader = new GLTFLoader(); 
loader.load('path/to/model.glb', (gltf) => {
    scene.add(gltf.scene);
}); 

Conclusion

Three.js is a powerful and flexible library for creating 3D web experiences. By mastering its basics, you can build engaging interactive graphics, visualizations, and even games. Start experimenting with different objects, materials, and lighting to bring your creativity to life!

cta

Share this

whatsapp
whatsapp
whatsapp
whatsapp
whatsapp

Keep Reading

Stay up to date with all news & articles.

Email address

Copyright @2025. All rights reserved | Radial Code