Three.js RepeatWrapping vs MirroredRepeatWrapping

When texturing 3D models in Three.js, controlling how textures tile across a surface is crucial for achieving realistic visuals. This article explains the differences between RepeatWrapping and MirroredRepeatWrapping, demonstrating how each mode affects texture coordinates and helping you choose the right wrapping method to eliminate harsh seams in your 3D scenes.

Understanding Texture Wrapping in Three.js

By default, Three.js textures use ClampToEdgeWrapping, meaning the last pixel of the texture is stretched to the edge of the mesh if the texture coordinates exceed the 0-to-1 range. To repeat a texture, you must define the wrapping behavior on the horizontal (wrapS) and vertical (wrapT) axes and define a scale using the repeat property.

Three.js offers two primary wrapping modes for tiling: RepeatWrapping and MirroredRepeatWrapping.

1. RepeatWrapping

In RepeatWrapping mode, the texture simply repeats itself continuously at its original orientation. When the texture coordinate exceeds the boundary of the image, it wraps directly back to the opposite edge (from 1.0 back to 0.0).

2. MirroredRepeatWrapping

In MirroredRepeatWrapping mode, the texture repeats, but every alternate tile is mirrored (flipped horizontally or vertically) along the axis of repetition.


Code Implementation Comparison

To apply these wrapping modes in Three.js, you must set the wrapS and wrapT properties of your texture object, and then define how many times the texture should repeat using repeat.set().

Implementing RepeatWrapping

const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load('path/to/texture.jpg');

// Set both horizontal and vertical wrapping to Repeat
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;

// Repeat the texture 4 times horizontally and 4 times vertically
texture.repeat.set(4, 4);

Implementing MirroredRepeatWrapping

const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load('path/to/texture.jpg');

// Set both horizontal and vertical wrapping to MirroredRepeat
texture.wrapS = THREE.MirroredRepeatWrapping;
texture.wrapT = THREE.MirroredRepeatWrapping;

// Repeat the texture 4 times horizontally and 4 times vertically
texture.repeat.set(4, 4);

Summary of Key Differences

Feature RepeatWrapping MirroredRepeatWrapping
Edge Transition Abrupt transition from edge to edge. Smooth transition as edges mirror each other.
Symmetry Keeps the original orientation of the image. Creates mirrored, symmetrical patterns.
Seam Visibility High risk of visible seams unless the texture is designed to be seamless. No hard seams, but the repetition pattern is highly visible due to symmetry.
Typical Material Man-made structures (bricks, tiles, grids). Organic materials (terrain, clouds, water).