Page 16: Textures in THREE

Spring 2026 Sample Solution

Using textures is notoriously tricky because there are a lot of steps involved. Fortunately, THREE makes it remarkably easy - it takes care of all the little details, allowing you to focus on the big parts (setting coordinates, defining the texture image).

If you want a sense of how much had to happen to get a textured cube, have a look at the 2015 example! And this doesn’t do any lighting!

Fortunately, THREE takes care of all of this for us. And it gives us access to lots of fancier kinds of texture things as well.

2023 Video: Lecture 18C Textures in Three.JS  |  Slides

The Complete Example

We’re going to do this backwards. Here is a complete example program. The plane is included so you can see what the texture looks like when flat.

05-16-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
   05-16-01.html   05-16-01.js

We’ll explain how it works below, but you might want to have a look first (the program is 05-16-01.js ).

On the left, we have the triangles without texture, for reference. In the center, we put the texture on the triangles. On the right, we again put the texture on, but adjust the texture coordinates - the left triangle gets its colors from the lower-left part of the texture map (as it appears on the square), and the right triangle gets its colors from the upper-right corner.

Texture Coordinates in THREE

Most THREE primitives have reasonable texture coordinates defined. For example, the square (a THREE PlaneGeometry) in the demo 1 has its u,v going from 0 to 1 across the plane.

For user-defined geometries (of class BufferGeometry), we have to provide texture coordinates ourselves. We need to split a vertex if it needs to have different coordinates for the different triangles it belongs to (as we did with color and normals before).

Setting up the texture coordinates for the BufferGeometry is simple: we make a new BufferAttribute for the UVs. We give it an array of 2 * number of vertices (a uv pair for each vertex). Here’s the code from 05-16-01.js - it’s actually only 2 statements (since I put each vertex’s data on a separate line, it appears longer).

63
64
65
66
67
68
69
70
71
      const uvs = new Float32Array( [
        0,0,
        1,0,
        0,1,        
        1,1,
        0,1,
        1,0
      ]);
      geometry.setAttribute('uv',new T.BufferAttribute(uvs,2));

Notice that I need to make a typed array of the values (that’s what Float32Array does). It has 12 values (6 vertices, 2 values per vertex - u and v). I then use that array to make a BufferAttribute that gets assigned to “uv”.

Using the Texture

Once we have UV values, we simply need to make sure our material knows to use those UV values and color the triangles using the correct map. In 05-16-01.js , it is done by:

75
76
77
78
79
80
      let tl = new T.TextureLoader().load("../textures/UV_Grid_Sm.jpg");
      let material = new T.MeshStandardMaterial({
        color: "white",
        roughness: 0.75,
        map: tl
      });

There are two steps here: first, we load an image texture (more on that in a moment), and then we create the material. When we create the material, it can be as simple as setting the map attribute to the texture we want to use. Technically, we are using the texture as a color map. Note that we make the material white: the color will be blended with the texture color, so if you want to see the actual colors in the texture, you need to have the base color be white.

We can specify other properties for the material. In the example, I specified roughness. You can even specify color, and this color will be combined with the colors from the texture. Of course, with a MeshStandardMaterial, the colors are affected by lighting as well. If you want to use textures but do not want the color affected by lighting, use a MeshBasicMaterial.

THREE allows us to control many properties of how textures are used. These will make more sense once we learn about how texturing works in class. For now, the default parameters will probably be fine.

Texture Image Loading in THREE

In order to have a texture image, we need to get the image from somewhere. Usually, this is from some image file.

Loading an image into a texture is tricky. We need to load the image. We need to wait until the image is loaded. We need to process the image and put it into a special format. We need to attach the image to its surface.

Fortunately, THREE takes care of all this for you with a texture loader. You say “load me a texture”, and it loads the texture in and sets things up once it’s loaded. A cool feature: before the texture is loaded, things still work (it just shows up as blank). Remember, this is JavaScript asynchronicity, so there really isn’t any multi-threading. You need to make sure you return back to the main event loop for loading to happen (remember how requestAnimationFrame works?).

The line of code is:

75
      let tl = new T.TextureLoader().load("../textures/UV_Grid_Sm.jpg");

Texture Re-Use

It’s so easy to load and use textures with THREE that you might forget how many you’ve loaded. In general, using lots of textures can be a problem: they take up a lot of memory.

For example, in the example code 05-16-01.js , the texture file /textures/UV_Grid_Sm.jpg is loaded three times. A separate copy will be loaded for each instance of TwoTrianglesTextured and TwoTrianglesTextured2 that we create. Right now, there is just one of each (so that’s two copies of the texture - a third is used for the plane). If we made a lot of these objects, the memory usage would add up fast.

Therefore, when we use textures, we need to be careful to reuse previously loaded assets and not load multiple copies. In fact, we might want to try to re-use the same texture over and over to save memory. Think about this when you make your own objects for future projects.

The obvious way to fix this in 05-16-01.js would be to move the line that loads the texture to near the top of the file (after the THREE library is loaded), outside of any function. That way tl would be a “module” variable (effectively a global), rather than a local variable. The texture would be loaded once, when the module is first loaded.

Setting Texture Parameters

Once you have loaded a texture, THREE allows you to control exactly how that image is sampled and mapped onto your geometry. It is important to note that these filtering and sampling options are properties of the THREE.Texture object itself, not the Material. While the material dictates how the surface reacts to light (using properties like roughness or color), the texture object controls how its own pixels are read and interpolated by the GPU. If you change a filtering property on a texture, every material that uses that specific texture object will inherit those rendering rules.

Magnification and Minification (Filtering)

When a textured object is rendered, the pixels of your texture (texels) rarely line up perfectly 1:1 with the pixels on your screen. THREE uses filters to dictate how the graphics hardware should calculate the final colors:

  • Magnification (magFilter): This applies when the camera is very close to the object, meaning a single texture pixel is stretched to cover multiple screen pixels.

  • THREE.LinearFilter (Default): Blends the closest texels together, resulting in a smooth, sometimes blurry appearance.

  • THREE.NearestFilter: Snaps to the exact color of the closest texel, resulting in a sharp, blocky, “pixel art” look.

  • Minification (minFilter): This applies when the object is far away, meaning a single screen pixel covers a large area of the texture. To handle minification efficiently without creating noisy visual artifacts (aliasing), THREE relies on mipmaps.

Mipmaps

Mipmaps are a sequence of pre-calculated, progressively smaller versions of your original texture image (e.g., half size, quarter size, down to a single pixel).

  • How they are created: By default, THREE automatically generates these mipmaps for you in the background when the texture is successfully loaded (texture.generateMipmaps = true).
  • How they are used: During rendering, the hardware automatically selects the mipmap level that best matches the object’s current size on the screen. This saves rendering performance and drastically reduces visual noise.

You control how these mipmaps are sampled using the minFilter property. The default setting is THREE.LinearMipmapLinearFilter (often called Trilinear Filtering). This is generally the highest quality standard option, as it smoothly interpolates both across the texture and between the two closest mipmap sizes to prevent jarring visual pops as objects move away.

Anisotropic Filtering

Standard mipmapping works beautifully when you are looking straight at a surface, but it breaks down at steep, oblique angles. For example, if you are rendering a textured floor stretching out toward the horizon, the shape the screen pixel covers on the texture is a long, skinny rectangle. Standard mipmapping assumes a square shape, which causes the distant floor to look heavily blurred and smudged.

Anisotropic filtering solves this by taking multiple samples along that elongated shape, restoring crisp details at sharp angles.

Because it requires more processing power, anisotropy is set to 1 (disabled) by default. You can turn it on by setting the anisotropy property on your texture. To get the best possible visual result without guessing what a user’s device can handle, you can query the renderer for the maximum hardware-supported value:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Example: Loading a texture and setting parameters
const textureLoader = new THREE.TextureLoader();
const myTexture = textureLoader.load('textures/my_image.jpg');

// These are the defaults, shown here for clarity
myTexture.magFilter = THREE.LinearFilter;
myTexture.minFilter = THREE.LinearMipmapLinearFilter;

// Enable the maximum anisotropic filtering supported by the device
myTexture.anisotropy = renderer.capabilities.getMaxAnisotropy();

// Apply to material
const material = new THREE.MeshStandardMaterial({ map: myTexture });

Summary: Textures in Three.js

While the underlying math of texture mapping is highly complex, THREE handles the heavy lifting for us. To successfully apply textures to your 3D objects, keep these key takeaways in mind:

  • Coordinates: Ensure your geometry has UV coordinates defined. Standard THREE geometries have them built-in, but for custom BufferGeometry, you must provide them manually via a BufferAttribute.
  • Loading: Use THREE.TextureLoader to asynchronously load your image files into texture objects. It automatically handles the tricky WebGL setup in the background.
  • Application: Assign the loaded texture to the map property of your material (e.g., THREE.MeshStandardMaterial). Remember that the base color of the material will blend with the texture, so you often want to set the material color to white.
  • Efficiency: Textures consume a lot of memory. Always try to load a texture only once and re-use that single THREE.Texture object across multiple materials or meshes.
  • Parameters: Configure sampling properties like magFilter, minFilter (for mipmaps), and anisotropy directly on the Texture object—not the material—to control how the image is filtered at different distances and viewing angles.
Next: Page  17 - Dice and Dominos