Getting Started with Three.js

Ishitababar
csivit
Published in
4 min readJul 31, 2024

Three.js is a powerful JavaScript library that simplifies the creation of 3D content in the browser, enabling developers to create immersive 3D experiences for web pages.

Example Websites

Here are some impressive examples of what can be achieved with Three.js:

- bruno-simon.com

- needle.tools

- david-hckh.com

- taotajima.jp

These websites showcase the potential of Three.js in creating visually stunning web experiences. Now, let’s dive into how you can create your own 3D web content using Three.js.

Understanding WebGL

Before diving into Three.js, it’s important to understand WebGL.

What is WebGL?

WebGL (Web Graphics Library) is a JavaScript API that allows us to draw graphics directly on a canvas element, leveraging the power of the GPU for high-performance rendering. It’s based on OpenGL ES, a subset of the OpenGL specification, and is supported by most modern browsers.

WebGL essentially allows you to create 3D graphics using triangles, the building blocks of digital 3D models. It enables parallel computation on the GPU, which means it can efficiently handle the rendering of complex scenes with thousands of triangles.

Using WebGL to Impart Color with Shaders

One of the core features of WebGL is the ability to use shaders — small programs that run on the GPU — to manipulate the rendering pipeline. Two critical shaders in this process are the vertex shader and the fragment shader, both of which play crucial roles in determining the final appearance of 3D objects, including their colors and lighting.

1. Vertex Shader

Imagine you’re rendering a 3D shape, like a cube. Each corner of this cube is represented by a vertex. The vertex shader’s job is to process these vertices, ultimately determining where they should appear on the screen. It takes each vertex’s 3D coordinates (x, y, z) and projects them onto a 2D surface.

To achieve this, the vertex shader must set a special variable called `gl_Position`, a 4D floating-point vector. This vector represents the final position of the vertex on the screen. The vertex shader can perform various transformations on the vertices, such as scaling, rotating, or translating, before setting the `gl_Position`.

2. Fragment Shader

Once the vertex shader has done its job, the next task is to decide the color of the rendered object. This is where the fragment shader comes into play. The fragment shader is responsible for calculating the color of each pixel (or “fragment”) that makes up the shape. It sets the `gl_FragColor` variable, another 4D floating-point vector, which determines the final color output for that pixel.

During the rasterization process, the shape’s surface is divided into smaller elements called fragments. Each fragment receives interpolated values from the vertices. For instance, if one vertex is blue and an adjacent one is green, the fragment shader calculates the color for the pixels between them, smoothly transitioning from blue to green.

By leveraging vertex and fragment shaders, WebGL provides a versatile platform for creating dynamic, colorful, and interactive 3D graphics directly within web browsers.

Challenges with WebGL

While WebGL is powerful, it can be complex and cumbersome to use directly. Drawing even a simple triangle can require a significant amount of code. Moreover, handling advanced features like camera perspective, lighting, and animations can be challenging.

Introducing Three.js

Three.js is an open-source JavaScript library that abstracts the complexities of WebGL, making it easier to create and display 3D graphics in the browser. Created by Ricardo Cabello (Mr.doob), Three.js simplifies the process of working with 3D graphics by providing higher-level constructs and built-in features.

Key Features of Three.js

- Simplified rendering of 3D scenes

- Built-in support for various geometries, materials, lights, and animations

- Easier handling of shaders, matrices, and other advanced concepts

- Active community and frequent updates

Creating Your First 3D Scene

Let’s walk through the process of creating a basic 3D scene with a rotating cube using Three.js.

Step 1: Setting Up the HTML File

Create an `index.html` file with the following content:

Step 2: Creating the JavaScript File

Create a `script.js` file with the following content:

Explanation

1. Import Three.js: We import the Three.js library.

2. Create a Scene: We create a scene, which is where all objects, lights, and cameras will be placed.

3. Create a Camera: We set up a perspective camera to view the scene.

4. Create a Renderer: The renderer is responsible for rendering the scene. We use `WebGLRenderer` and append the canvas to the DOM.

5. Create a Cube: We create a cube using `BoxGeometry` and `MeshBasicMaterial`, then add it to the scene.

Running the Code

To see the rotating cube, open your terminal and navigate to the directory containing your HTML and JavaScript files. Start a local server using:

Open your browser and go to `http://localhost:8000` to see the cube.

Conclusion

Three.js makes it significantly easier to create 3D graphics and animations in the browser by abstracting the complexities of WebGL. By following the steps above, you can create a basic rotating cube and start exploring more advanced features of Three.js. For more information and examples, check out the Three.js Official Website and Three.js Documentation

This is the final output :

--

--