Page 17: Shader Basics

Spring 2026 Sample Solution

Spring 2026 Note: Let me begin with an apology/warning: the landscape of how shaders work, and the right way to teach them, is in the midst of a change. The old way we taught things is being deprecated, and the new way isn’t quite ready yet. I’ll explain that more as we go along.

Spring 2026 Note 2: In the past, the workbook was meant as an addendum to class. But making the workbook stand alone is challenging right now, because I don’t want to develop new explanations of the old ways of doing things.

With those warnings…

We will learn about programming the graphics hardware by writing shaders: which are little programs that run on the graphics hardware. Over time, as the graphics hardware became more flexible, shader programming has evolved. We will focus on the two main types of shaders used in graphics programming: vertex shaders and fragment shaders. We will not talk about the many other kinds of shaders. In particular, we will not talk about how to use the graphics hardware to do “non-graphics” computations.

In order to learn to program the hardware, we will use one of the languages built into the web browser: GLSL. I will discuss this decision more when we discuss shading languages. GLSL is a great language for learning shader programming. However, it has some practical problems. It is good for class, but not much else. For now, I still think the best strategy is to learn shader programming with GLSL. It will be easier to transition to other languages when you understand the concepts.

Previous years’ lectures were based completely on GLSL and provide a good introduction. And the workbook basically said “pay attention to the lectures.”

These lectures provide a review of how the hardware is organized, which motivates how we program them. They move through many aspects of shader programming leading to some advanced examples by the end.

Learning about Shaders

There are (at least) 4 different things that you have to understand in order to really use shaders effectively:

  1. The basic concepts of what shaders are, and why they are the way they are. This really means understanding how the hardware works and where the shaders fit into the overall process of rendering. If you don’t understand these basics, shaders won’t make sense. Unless you understand how the pieces work, the rules about writing shaders will seem arbitrary. Shaders need to fit into the rasterization pipeline.
  2. The language that we write shaders in. In some sense, this is a detail: shading languages are very similar, and differ only slightly in their syntax and built-in functionality. For us, the language is GLSL (GLSL-ES to be precise).
  3. The ways in which the shader programs communicate with other programs. This includes the interface between shaders and the “host” program (in our case, the JavaScript code), as well as the way in which different shaders talk to each other. Appreciating these basic mechanisms is important, since the communications can be a hassle. However, it is also important that the shader programs interface well with the more complex host program.
  4. Some of the specific tricks we use in shader programming to address common problems while still working within the constraints of the rasterization model.

The most important thing for class is #1 - since if you understand what is going on, everything else will fall into place. GLSL (#2) will be a practical matter, since you will want to write some shaders.

#3 takes on two forms: one is the low level interface, how we’ll pass information between our three.js (JavaScript) programs and the shader programs that run on the graphics hardware. But, it also involves higher level issues as well: how do we make use of all that cool stuff that three.js provides?

#4 will seem like a collection of tricks. We’ll look at a few different things: noise, shader anti-aliasing, texture lookups. Seeing a few different tricks will help you appreciate other ones.

In class, we’ll only really talk about vertex shaders and fragment shaders (also known as pixel shaders). There are other kinds of shaders, but they are optional and not used as often. Until you master these two, the others are even less likely to make sense.

In fact, in class, we’ll focus on the fragment shaders. We’ll need to write vertex shaders (since vertex and fragment shaders always appear in pairs), but most of the work we do will be on fragment shaders. And in the future, three.js might not even want us to program vertex shaders.

Things You Need To Know Before Starting

You need to understand the rasterization pipeline - shader programming will not make sense if you don’t understand which pieces we are programming and how they fit into the overall process.

Here are some concepts to make sure you understand:

  1. The pipeline of how triangles go from a program to the screen. Knowing where the shaders fit into this process helps explain why they do what they do - and only what they do.
  2. The concepts of a vertex shader and a fragment shader (sometimes known as a pixel shader). You should know why we always need one of each.
  3. What kinds of things you can (and cannot) do in the shaders - based on what they do in the pipeline.
  4. What a fragment is - roughly it’s a pixel (we used to call them “pixel shaders”), but fragment is the more correct term.

If you start by trying to learn the GLSL language, or trying to read a fragment shader, it will seem really arbitrary. These basics tell you the rules.

These basics connect with some terminology (we’ll explain more as we go):

  1. Attribute buffers are the blocks of memory that contain vertex information. An attribute is a property that all vertices (in a block) have, such as position, normal, or uv value.
  2. Pipeline stages have different types of information that lead to different types of variables we will encounter in our shader programs. In particular, there are three different types of variables used to pass information to and from shaders: uniform, attribute, and varying variables. Uniform variables are constant for the entire scene (or at least for each block of triangles). Attribute variables are information about a vertex. Varying variables are information about a specific fragment.

Overview: What are all the pieces?

There are a bunch of concepts - and then there are the places where they get used in a program. Here we’ll try to make a connection.

  1. Before we draw any triangles, we need to define the shader programs that will be run on the graphics hardware for every vertex (the vertex shader) and for every fragment (the fragment shader). In THREE, these shaders are defined as part of a Material, so we can have different programs for each Material. When we make a material, the compiler is run to compile the shader programs. In fact, when we change the material, the compiler may need to be run (which is why we need to be careful about changing materials). Most THREE materials use built-in shaders, but we can use the special ShaderMaterial to allow us to provide our own program.
  2. When we draw the triangles for an object (a set of triangles that a set of shaders will process), our host program needs to send those triangles to the graphics hardware. It does this by putting all of the information about the vertices into an attribute buffer, which is a data structure that stores the vertices in a way that makes it efficient to send to the graphics hardware. THREE takes care of this: if we use a BufferGeometry we have to work with the data structures directly. The attribute buffer has all the information about our vertices: our shaders will be given the information about vertices one at a time. (Modern versions of THREE only have BufferGeometry - older versions of THREE had more convenient data structures that were converted.)
  3. As the triangles are drawn, for each vertex, a vertex shader is run. The shader executions for each vertex are independent. The vertex shader is passed the attributes of the one vertex it is meant to process. This is why vertex splitting is a big deal: if a vertex is “split” (so it has different attributes for different triangles), it has to become two vertices so each one can be processed separately. The result of vertex shading is that each vertex has some new properties associated with it, including a screen-space position.
  4. The vertices are re-assembled into triangles, and the rasterizer creates a list of fragments (pixels) that the triangle “covers”. Each of these fragments is then sent to be shaded. After this stage, each fragment is processed independently. The properties of the vertices of the triangle are interpolated to provide values for each fragment.
  5. For each fragment, a fragment shader is run. The fragment shader sees the fragments independently (effectively one at a time, but in parallel). The fragment shader program processes one fragment - it gets the information about the fragment as varying variables (values that we determined by interpolating the vertices that made up the triangle that caused the fragment). The fragment shader can determine the color of the fragment, and it is allowed to change the depth. However, a fragment’s position in the image cannot be changed - that would cause it to become a different fragment!
  6. After the fragment is processed, the fragment is tested and, assuming it passes the tests, the color and depth values are written to the image. The most important test is the depth test (z-buffer), which checks that the fragment is not occluded by other fragments.

All this has been true for all the triangles we’ve been drawing throughout this semester. The only thing that is new is that we’re going to write the shader programs in steps 3 and 5, rather than using the ones built-in to THREE. In the past, you’ve used materials such as MeshStandardMaterial for which THREE has built-in shader programs. (You can try to read them if you want!)

So What Do Shaders Actually Do?

A vertex shader is a small program that runs on one vertex (each vertex is independent). It receives as input the attributes of that vertex and things that are constant (the same over all vertices) called uniforms. The only thing a vertex shader can do is add new attributes to the vertices. It must provide the screen-position of the vertex. It may optionally provide other attributes (we’ll call these output attributes, or varying variables) that can be used in later stages.

A fragment shader is a small program that runs on one fragment (think of this as a pixel of a triangle). Each fragment is processed independently. It receives as input the properties of the fragment. These are determined by interpolating the “output attributes” (varying variables) from the vertices (this is done automatically, by the rasterizer). The inputs to a fragment shader are these varying variables as well as the values that are constant over the whole object (the uniform variables). The output of the fragment shader are more properties of the fragment. The most important one is the color (the thing that will get written to the image, if the fragment makes it through the tests).

Summary: Vertex and Fragment Shaders

Make sure that you understand what a vertex shader is and what a fragment shader is. If you don’t, re-read this page, watch the lectures again, or read one of the recommended readings.

To help you check your basic understanding, answer the following 2 questions - a sentence or two for each is OK. This is mainly for you to check you are ready to go on (but we’ll give you a few points for writing something).

Question 1: What are the inputs and outputs of a vertex shader?

Question 2: What are the inputs and outputs of a fragment shader?

Now that we understand what these programs do, we need to pick a programming language to write them in. We’ll discuss that on the next page.

Next: Page  18 - Shading Languages