Learn with me: An Introduction to Shaders and the Three.js Shading Language

Today, I'm starting a new series on my blog called "Learn with me", where I write about technical subjects while I'm learning them. For the first one, I picked a topic that both fascinates and terrifies me: shaders, and especially the new TSL (Three.js Shading Language).

I chose that name because I believe the best way to learn is to teach, even when you're still learning yourself. It's a bit like live-coding my brain while it takes in new concepts. Expect some eureka moments, some honest confusion and probably a few mistakes along the way.

Let's get into shaders!

What exactly is a shader?

If you've worked with Three.js or WebGL before, you've probably heard of shaders. Picture your GPU as an army of tiny, super-fast robots with paintbrushes that only understand very specific commands. A shader is the instruction manual that tells each robot which color to use and where to put each brushstroke. And all these robots work at the same time, very fast: millions of them painting your screen several times per second!

The fundamental role of shaders in graphic rendering

Shaders are special programs written in GLSL (OpenGL Shading Language) that run directly on your GPU. They're one of the main parts of WebGL and handle two tasks: positioning each "vertex" (point) of a 3D geometry and coloring each visible pixel of that geometry.

The term "pixel" isn't quite accurate here, though. In the shader world, we talk about "fragments", which are graphical processing units before they become pixels on the screen.

To do this, we send a lot of data to the shaders, including vertex coordinates, mesh transformations, camera information, colors, textures, lighting parameters, and much more.

The two main types of shaders

You'll mostly work with two types of shaders:

  1. The vertex shader runs first. It determines where each vertex (point) of your geometry goes in 3D space, before it's projected onto your 2D screen. It's like commanding your robot army's positioning squad: "Robot #1, stand at coordinates X,Y,Z! Robot #2, three steps to the left and two up! Robot #3, form a triangle with the others!"
  2. The fragment shader runs once all the vertices are in place. It determines the color of each fragment (future pixel) visible in your geometry. Now you're instructing your coloring squad: "Robots between points A and B, use red paint! Robots near the edge, create a blue-to-green gradient! And you robots in the center, apply this fancy texture pattern!"

How do shaders work?

The GLSL language

Shaders are written in GLSL, a language that resembles C with some specifics for graphical processing. If you've never written GLSL before, here's a little preview:

precision mediump float;

void main()
{
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // it's like #ff0000 or rgb(255 0 0 / 100%) in CSS
}

This simple code is a fragment shader that colors all fragments in red. A few points to note:

  • precision mediump float; defines the precision of floating-point numbers. Options are highp, mediump, and lowp.
  • gl_FragColor is a predefined variable that contains the final color of the fragment.
  • vec4(1.0, 0.0, 0.0, 1.0) represents an RGBA color (Red, Green, Blue, Alpha).

GLSL is a typed language, which means you must specify the type of each variable:

float a = 1.0;        // Floating-point number
int b = 2;            // Integer
bool c = true;        // Boolean
vec2 d = vec2(1.0, 2.0);  // 2-dimensional vector
vec3 e = vec3(1.0, 2.0, 3.0);  // 3-dimensional vector
vec4 f = vec4(1.0, 2.0, 3.0, 4.0);  // 4-dimensional vector

The rendering pipeline

Here's how the rendering pipeline works with shaders:

The process begins when you send a geometry (a set of vertices) to the GPU. The vertex shader processes each vertex individually, determining its final position. Then, the GPU determines which fragments (potential pixels) are covered by the geometry. Next, the fragment shader processes each fragment individually, determining its final color. Finally, the result is displayed on the screen.

It's a bit like an assembly line where each worker (shader) has a specific task to accomplish.

Essential components of shaders

Uniforms - Constant data for all vertices

"Uniforms" are variables that remain constant for all vertices or fragments during a render cycle. They're perfect for values like elapsed time (for animations), transformation matrices, global colors, and configuration parameters.

Here's how you might define and use a uniform in Three.js:

const material = new THREE.RawShaderMaterial({
    vertexShader: vertexShader,
    fragmentShader: fragmentShader,
    uniforms: {
        uTime: { value: 0 },
        uColor: { value: new THREE.Color('orange') }
    }
});

// In your animation loop
function animate() {
    material.uniforms.uTime.value = clock.getElapsedTime();
    requestAnimationFrame(animate);
}

And in your shader:

uniform float uTime;
uniform vec3 uColor;

// Usage in code...

Attributes - Unique data for each vertex

Unlike uniforms, "attributes" are data that vary for each vertex. Typical examples include vertex position, vertex normal, UV coordinates for textures, and custom values for special effects.

Here's how you might define a custom attribute in Three.js:

const geometry = new THREE.PlaneGeometry(1, 1, 32, 32);
const count = geometry.attributes.position.count;
const randoms = new Float32Array(count);

for(let i = 0; i < count; i++) {
    randoms[i] = Math.random();
}

geometry.setAttribute('aRandom', new THREE.BufferAttribute(randoms, 1));

And in your vertex shader:

attribute float aRandom;

void main() {
    // Using aRandom to modify position, for example
    vec4 modelPosition = modelMatrix * vec4(position, 1.0);
    modelPosition.z += aRandom * 0.1;

    // Rest of code...
}

Varyings - Communication between vertex and fragment shaders

"Varyings" are variables that allow data to be passed from the vertex shader to the fragment shader.

In the vertex shader:

attribute float aRandom;
varying float vRandom;

void main() {
    // ...
    vRandom = aRandom;
    // ...
}

In the fragment shader:

varying float vRandom;

void main() {
    gl_FragColor = vec4(vRandom, 0.5, 1.0, 1.0);
}

Three.js and shaders

RawShaderMaterial vs ShaderMaterial

Three.js offers two main classes for working with custom shaders:

  1. RawShaderMaterial gives you total control, but you have to define all variables yourself, including projection, view, and model matrices. It's like cooking from scratch.
// With RawShaderMaterial, you need to define everything yourself
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

attribute vec3 position;

void main() {
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
  1. ShaderMaterial comes with many commonly used variables already defined, so you can focus on the logic specific to your shader. It's like using a cooking kit with pre-measured ingredients.
// With ShaderMaterial, many things are predefined
void main() {
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}

The choice between the two depends on your comfort level with GLSL and how much control you need.

Introduction to TSL (Three.js Shading Language)

This is where TSL, the Three.js Shading Language, comes in. It's a new way to write shaders in Three.js, and it aims to make shaders easier to write while keeping the power and flexibility of GLSL.

What TSL changes

TSL is easier to pick up for JavaScript developers: the syntax is more familiar, and it hides some of the complexity of GLSL. It also fits better with the rest of Three.js, which makes shaders easier to share and reuse.

Conclusion

Shaders feel like the black magic of graphics development: powerful, mysterious and sometimes intimidating. But once you understand the principles behind them, you can do a lot with them.

In this "Learn with me" series, we'll figure out shaders and try the new TSL together. I'm not an expert, I'm learning just like you. We'll experiment and sometimes fail along the way, but we'll all understand shaders better at the end.

Whether you're a Three.js developer curious about TSL or a complete beginner with shaders, I hope you'll follow along!