Page 19: GLSL and three.js

Spring 2026 Sample Solution

Part of writing shaders is that you have to work in a shading language, which is yet another programming language to learn. What is challenging for most students is the concept of shaders, the details of the language follow. In fact, in old assignments we used to make students translate shaders from one language to another to emphasize that the languages aren’t that different.

GLSL is convenient: it is one shading language that works almost everywhere, and the compiler is built into the graphics drivers. Back in the old days, you had to worry about getting the compiler and hoping it worked with your graphics card.

While it is a bit of a pain to have “yet another” language to learn, the fact that GLSL is highly specialized for shaders means it has a lot of nice features for doing shader programming. It has excellent support for vectors and matrices (since graphics programming always has vectors and matrices). It has all kinds of convenient math functions built in.

Technically, we are using “GLSL-ES” - which is a variant of GLSL. This only comes up because the documentation often refers to it as “GLSL-ES”.

A good way to learn to program in GLSL is to try to read and write shaders, and then look up features in a reference card (for example, the WebGL Reference Card). Expect to make lots of mistakes (which is painful, since you don’t get to see the errors until the compiler runs).

Here are a few things that will help you get started:

GLSL is C-like in syntax. It uses the same basic syntax, but uses different keywords and operators, has different built-in types, and a different feature set.

GLSL is strongly typed. Everything has a type, and must be of that type. For example, integers and floats are different. 1 is an integer. 1.0 is a float. 1 + 1.0 is not 2.0 or 2, it is a type error.

GLSL has many vector and matrix types. In graphics, we use 2-, 3-, and 4-component vectors and 2x2, 3x3, and 4x4 matrices. GLSL has all of these built in. They are different. You cannot assign a vec2 to a vec3.

GLSL has very flexible constructors for matrices and vectors. You can construct a vec4 from a vec3 and a float, or two vec2s, or … You do have to explicitly construct things (vec3 p = vec3(vec2(1,2),3)).

GLSL has very flexible accessors for vectors. If you have a vec3 variable p, you can access the first component of the vector as p[0] or p.x or p.r (as in RGB). You can also refer to other subparts, like p.xy (which is a 2-vector), or even p.zy (which selects and re-orders the subparts). The ability to select and re-order vector parts is called swizzling.

Tools for GLSL

Having good tools for writing shaders is helpful. But, unfortunately, most of the tools for GLSL are either poor or deprecated. In fact, the challenge of developing good tools for GLSL was one of the language’s downfalls.

So, for now (Spring 2026) - you’ll need to write shaders in VS Code. There are extensions for VS Code (and “glsl mode” for syntax highlighting).

When we want better tools, we’ll switch to a different shading language.

Shaders in three.js

For using shaders in three.js: there are tutorials out there. Not as many as for other topics, because the built-in shaders for three.js are so good that you usually don’t need to write your own.

However, we want to write shaders (1) to learn how they work and (2) to create custom shaders for specific tasks (we’ll see some examples in a bit).

Generally, three.js makes adding shaders easy. With the ShaderMaterial (see the documentation), you can give it shader code, and it adds GLSL declarations to the beginning so your program can access the information it needs (see the docs). It also takes care of converting between JavaScript and GLSL data types, putting your vertex information into buffers to send as attributes, and a host of other details you don’t want to worry about.

The CS559 Framework provides support for handling shaders (see the documentation). It makes using three.js ShaderMaterial even easier.

One tricky thing: three.js has very fancy lighting and materials. We will ignore three.js’s lights, and do something simpler for ourselves in GLSL. When we want to interface with three.js’s lighting, we’ll use a different shading language.

Some Advice

Some GLSL/WebGL/three.js gotchas:

  • If compilation fails, it will dump error information into the console. However, since three.js adds a lot of code to your program, the line numbers will no longer work. If you don’t see your object/shader, check the console for errors!
  • Integers are not automatically coerced to floats. This is the opposite of JavaScript.
  • Unless it knows otherwise, GLSL treats numbers without decimal points as integers, and then creates compilation errors. 1 + 2.0 is an error (since 1 is an integer). Always use 1.0 if you mean a floating point number.
  • WebGL is very good at assembling vectors from smaller pieces. For example, vec4(vec2(1.0,2.0),vec2(3.0,4.0)) creates a 4-dimensional vector from a pair of 2-dimensional vectors. However, you need to explicitly create the correctly sized vectors.
  • Some things in regular GLSL are not built into GLSL-ES (the variant of the language used by WebGL). Many of them are available as “extensions”. Three.js sets up the most common extensions for us, but shaders need to enable them.
  • You can write if/then/else statements, but it’s often better to try to write them using computation functions (step, smoothstep, clamp, etc). These are not only more efficient, but also easier to change to anti-aliased versions later.
  • To GLSL, a vector is a vector. You can refer to a vec3 as xyz, or rgb; x is the same as r.
  • You can also refer to parts of vectors: if you have vec3 p, you can refer to p.xz (which is a 2-vector).

Summary: The GLSL Language and three.js

The best way to learn GLSL is to look at some examples, which we will do on the next page.

Next: Page  20 - Simple Shaders.