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.

06-23-01   view Kinds of boxes:
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.

26
27
28
29
30
31
  function spinner(delta, tod) {
    this.time += delta;
    this.objects[0].position.x = 2 * Math.sin(this.time / 1000);
    this.objects[0].position.z = 2 * Math.cos(this.time / 1000);
  }
  s1.stepWorld = spinner;

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:

06-23-02   rubric Kinds of boxes:
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.

20
21
22
23
24
25
26
void main() {
    // pass the texture coordinate to the fragment
    v_uv = uv;

    // the main output of the shader (the vertex position)
    gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}

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:

 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/* pass interpolated variables to from the vertex */
varying vec2 v_uv;

/* colors for the dots */
uniform vec3 light;
uniform vec3 dark;

/* number of dots over the UV range */
uniform float dots;

/* how big are the circles */
uniform float radius;

These are used when we compute the color:

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void main()
{
    // have the pattern be 1x1, but repeat the pattern to make more dots
    float x = v_uv.x * dots;
    float y = v_uv.y * dots;

    // find the center of the current "tile" 
    float xc = floor(x);
    float yc = floor(y);

    // compute the distance to the center
    float dx = x-xc-.5;
    float dy = y-yc-.5;
    float d = sqrt(dx*dx + dy*dy);

    // determine if we're close enough to the center
    float dc = 1.0 - step(radius,d);

    // choose a color based on how close we are
    gl_FragColor = vec4(mix(light,dark,dc), 1.);
}

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.

06-23-03   view Kinds of boxes:
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