Page 21: Shading in Practice
Spring 2026 Sample Solution
With three.js (and most modern, high-level graphics systems), there are excellent shaders built in. We have high-quality implementations of important lighting models that are incredibly flexible. We can use these for a wide range of effects using different types of maps. So this raises the question…
Why write your own?
If your goal is to have good, realistic material shaders that include pretty much every trick in the book for appearances (shadow maps, bump maps, normal maps, reflection maps, etc.), modern high-level APIs have them built into their default shaders. Three.js’ MeshStandardMaterial and MeshPhysicalMaterial are pretty impressive.
We generally will write our own shaders when we want to do something specialized or unusual. Something that isn’t built in. Some examples:
-
Procedural Textures: We want to write programs that create texture patterns, rather than making pictures of them (for image-based textures). We’ll explore this in the workbook.
-
Dynamic Effects: We want our objects to change - either over time, or in response to other objects.
-
Data Visualization: We use the shaders to display data in different ways, for example, mapping measurements to colors.
-
Special Effects: While the built-in shaders are good for realistic lighting effects, they are less good for more “stylized” looks where we want more control.
-
Computations on the GPU: The highly parallel hardware is good for more than just drawing triangles. If we’re clever, we can use all that power for other things. This is the backbone of modern AI. But it’s also useful for graphics when we want to do something intensive (say, simulate a complex physical system involving large numbers of particles).
A point to emphasize: we want to avoid duplicating what three.js has already done. I probably could not write as good a lighting shader as they have. And, even if I could, trying to wire it into all of their infrastructure for setting up lights and textures and environment maps and multiple passes would be tricky (it’s hard enough to build your own, never mind figure out how to fit it into someone else’s). With GLSL, integrating into a big system is hard.
Shading in Practice
When you write a shader, you need to write a separate program that runs on each of the little processors. Originally, these programs needed to be small to run on the little processors. But over time, these little processors became capable of doing many things.
However, for older shading languages, like GLSL, the design for “little programs” remains.
This means that the GLSL programs need to be self-contained. Your shader must take care of everything: transforming the points, computing the lighting, etc. The program must be a single string that can be passed to the compiler.
Three.js supports this a little: it will add some text to your program to help it interface with the rest of the system. In the old days, it provided ways for you to get copies of the built-in shaders so you could put parts of them into custom shaders (by performing string manipulation). Unfortunately, this interface is poorly documented and being deprecated. Instead, the focus is a new shading language designed around integrating with the existing parts. We’ll discuss that after we learn the basics using WebGL.
But, for now, this means that when we program in GLSL we need to do everything ourselves. That includes lighting and anti-aliasing.
What should we learn?
I think it’s best to learn about shaders by writing procedural textures. These are quite useful.
In fact, if you want a textbook/tutorial, The Book of Shaders promises “a gentle step-by-step guide through the abstract and complex universe of Fragment Shaders”. It’s oriented more towards artists, and it only covers fragment shaders. But that’s our focus, and the art aspects are fun, because the authors create cool things with them. It’s a little tricky to apply directly (since all of its examples are designed to run standalone in the browser using the author’s framework), but not that tricky because things are simplified (it’s only fragment shaders, so the examples expect less than other documents). The book also tries to do all computer graphics in a fragment shader - which leads to interesting examples, but not necessarily practical ones.
But, in order to do procedural shading, we need to be able to put our objects in the scene. So we’ll start by considering lighting.
Next: Page 22 - Lighting in Shaders