Page 23: Procedural Textures
Spring 2026 Sample Solution
The XYZ Texture
The idea of a texture is that we will compute a different color for each pixel. Since we can run a fragment shader on each pixel, we can do this.
As with image-based textures, we need to have a good coordinate system. We can use the UV coordinates, or anything else.
Here are two examples: for one of the spheres below, I’ll use the world space XYZ as the texture coordinates, and for the second I’ll use the object space XYZ as the texture coordinates. You should be able to tell which is which. For both, I’ll convert X into the red channel (in a way that makes it go from 0-1) and Z into the green channel.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
06-23-01.html 06-23-01.js
Notice how one object moves through colors in the world, while the other has the colors attached to it. Read the vertex shader 06-23-01.vs and the fragment shaders 06-23-01.fs and 06-23-01-2.fs to make sure you understand how we’re determining colors. Try hacking them to do something different.
In the JS program,
06-23-01.js
(06-23-01.html), there is a trick that can be useful when using the class software framework. Notice how we use a “non-lexical this” in order to make a function act like a method. We define spinner as a function, but when it is called, it is used as a method so it has access to the object’s this.
|
|
Usually, we’ll use texture coordinates (UV) for the coordinates to define our textures. But you should see at least one XYZ example.
A More Realistic Procedural Texture
Here’s a more “typical” procedural texturing example. Suppose we want a grid of dots. We could use an image texture, but then we would need a new image if we wanted to change the number of dots, the color of the dots, the size of the dots. Instead, we can write a shader that computes, for every pixel, whether or not it is in a dot. We can make the things we want to control (color, number of dots, size of dot) uniform variables that we can set from our program. Like this:
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
06-23-02.html 06-23-02.js
| Kind
Kind of rubric items: standard - expected from all students advanced - used to get a better grade creative - an opportunity to do something creative levels - rubric item graded by selected achievement level optional - not required, but encouraged |
Description | |
| standard | Make different colored dots |
Read through the shaders: 06-23-02.vs and 06-23-02.fs and understand what is happening and how the dots are getting drawn. You should read through 06-23-02.js (06-23-02.html ) to see how the uniform parameters are being connected to sliders.
The vertex shader needs to transform the vertex (we can’t draw otherwise). It also needs to pass along the uv values from the attribute buffers to the vertices, so they can be interpolated and passed to the fragment shader.
|
|
The actual interesting part is the fragment shader. First notice that it needs to take a bunch of inputs (as variables). The uniforms must be declared in the JavaScript; the varying variables are declared in the fragment shader:
|
|
These are used when we compute the color:
|
|
Comments should explain what is going on, but make sure you understand it. This is a basic shader, but it uses many standard tricks.
Notice in the fragment shader that I compute whether I am inside a dot or not using the step function. This returns 0 if we are inside of the dot, or 1 if we are outside of the dot (the distance d is greater than the radius). This “inside the dot” is used to control how the dark and light colors are mixed together (using the mix function). Generally, we prefer to use built-in functions rather than if statements (or more complex expressions) because they are optimized to run fast. However, here, the use of the step/mix combination will make future things easier. Make sure you understand this code before moving on to the next page.
If you look carefully, you will see evidence of “jaggies” - the circles aren’t smooth and you can see the pixels, so they look ugly. Dealing with this problem (called aliasing) is an advanced topic - we’ll get to it on a future page.
To check that you understand how the dots work, change the shader in 06-23-02.fs so that the dots are different colors - some are blue and some are green. You can choose which ones are which (any individual dot should be all blue or all green, but there needs to be some blue dots and some green dots).
Notice that we added a slider called “segs” that changes the number of triangles used to approximate the sphere. As you raise the number of segments, the sphere gets smoother (because it has more polygons). The shape changes, but the texture doesn’t.
Another Simple Example
Here is another simple example: A stripe pattern.
view - look at the box and experiment with it
examine - look at the code for the box
edit - change the box's content
rubric - several steps are suggested in the rubric page - form elements on page in rubric
06-23-03.html 06-23-03.js
I provide this as an example because it will give you one more to look at before trying to make one yourself.
Next: Page 24 - Aliasing