Three.js texture workflows for maps, UV mapping, environment maps, and texture configuration.
Real-world examples
Live HTML demos for this skill — rendered directly in the page. 4 examples.
- 01
Wrap, repeat & filtering
CanvasTexture albedo with SRGBColorSpace — toggle Clamp / Repeat / MirroredRepeat, repeat & rotation, Linear vs Nearest mag filter, and anisotropy.
- 02
CanvasTexture & DataTexture
Side-by-side procedural maps: live CanvasTexture (drawn 2D) vs DataTexture (Uint8Array gradient / noise) on MeshStandardMaterial.
- 03
PBR texture map set
Procedural color (sRGB), normal, roughness, metalness, and AO maps on MeshStandardMaterial — data maps stay linear; UV2 for aoMap.
- 04
CubeTexture environment
Procedural CubeTexture as scene background and envMap — metallic spheres pick up reflections from the six-face cubemap.
Skill markdown
# Three.js Textures
## Quick Start
```javascript
import * as THREE from "three";
// Load texture
const loader = new THREE.TextureLoader();
const texture = loader.load("texture.jpg");
// Apply to material
const material = new THREE.MeshStandardMaterial({
map: texture,
});
```
## Texture Loading
### Basic Loading
```javascript
const loader = new THREE.TextureLoader();
// Async with callbacks
loader.load(
"texture.jpg",
(texture) => console.log("Loaded"),
(progress) => console.log("Progress"),
(error) => console.error("Error"),
);
// Synchronous style (loads async internally)
const texture = loader.load("texture.jpg");
material.map = texture;
```
### Promise Wrapper
```javascript
function loadTexture(url) {
return new Promise((resolve, reject) => {
new THREE.TextureLoader().load(url, resolve, undefined, reject);
});
}
// Usage
const [colorMap, normalMap, roughnessMap] = await Promise.all([
loadTexture("color.jpg"),
loadTexture("normal.jpg"),
loadTexture("roughness.jpg"),
]);
```
## Texture Configuration
### Color Space
Critical for accurate color reproduction.
```javascript
// Color/albedo textures - use sRGB
colorTexture.colorSpace = THREE.SRGBColorSpace;
// Data textures (normal, roughness, metalness, AO) - leave as default
// Do NOT set colorSpace for data textures (NoColorSpace is default)
```
### Wrapping Modes
```javascript
texture.wrapS = THREE.RepeatWrapping; // Horizontal
texture.wrapT = THREE.RepeatWrapping; // Vertical
// Options:
// THREE.ClampToEdgeWrapping - Stretches edge pixels (default)
// THREE.RepeatWrapping - Tiles the texture
// THREE.MirroredRepeatWrapping - Tiles with mirror flip
```
### Repeat, Offset, Rotation
```javascript
// Tile texture 4x4
texture.repeat.set(4, 4);
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
// Offset (0-1 range)
texture.offset.set(0.5, 0.5);
// Rotation (radians, around center)
texture.rotation = Math.PI / 4;
texture.center.set(0.5, 0.5); // Rotation pivot
```
### Filtering
```javascript
// Minification (texture larger than screen pixels)
texture.minFilter = THREE.LinearMipmapLinearFilter; // Default, smooth
texture.minFilter = THREE.NearestFilter; // Pixelated
texture.minFilter = THREE.LinearFilter; // Smooth, no mipmaps
// Magnification (texture smaller than screen pixels)
texture.magFilter = THREE.LinearFilter; // Smooth (default)
texture.magFilter = THREE.NearestFilter; // Pixelated (retro games)
// Anisotropic filtering (sharper at angles)
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
```
### Generate Mipmaps
```javascript
// Usually true by default
texture.generateMipmaps = true;
// Disable for non-power-of-2 textures or data textures
texture.generateMipmaps = false;
texture.minFilter = THREE.LinearFilter;
```
## Texture Types
### Regular Texture
```javascript
const texture = new THREE.Texture(image);
texture.needsUpdate = true;
```
### Data Texture
Create texture from raw data.
```javascript
// Create gradient texture
const size = 256;
const data = new Uint8Array(size * size * 4);
for (let i = 0; i < size; i++) {
for (let j = 0; j < size; j++) {
const index = (i * size + j) * 4;
data[index] = i; // R
data[index + 1] = j; // G
data[index + 2] = 128; // B
data[index + 3] = 255; // A
}
}
const texture = new THREE.DataTexture(data, size, size);
texture.needsUpdate = true;
```
### Canvas Texture
```javascript
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const ctx = canvas.getContext("2d");
// Draw on canvas
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 256, 256);
ctx.fillStyle = "white";
ctx.font = "48px Arial";
ctx.fillText("Hello", 50, 150);
const texture = new THREE.CanvasTexture(canvas);
// Update when canvas changes
texture.needsUpdate = true;
```
### Video Texture
```javascript
const video = document.createElement("video");
video.src = "video.mp4";
video.loop = true;
video.muted = true;
video.play();
const texture = new THREE.VideoTexture(video);
texture.colorSpace = THREE.SRGBColorSpace;
// No need to set needsUpdate - auto-updates
```
### Compressed Textures
```javascript
import { KTX2Loader } from "three/examples/jsm/loaders/KTX2Loader.js";
const ktx2Loader = new KTX2Loader();
ktx2Loader.setTranscoderPath("path/to/basis/");
ktx2Loader.detectSupport(renderer);
ktx2Loader.load("texture.ktx2", (texture) => {
material.map = texture;
});
```
## Cube Textures
For environment maps and skyboxes.
### CubeTextureLoader
```javascript
const loader = new THREE.CubeTextureLoader();
const cubeTexture = loader.load([
"px.jpg",
"nx.jpg", // +X, -X
"py.jpg",
"ny.jpg", // +Y, -Y
"pz.jpg",
"nz.jpg", // +Z, -Z
]);
// As background
scene.background = cubeTexture;
// As environment map
scene.environment = cubeTexture;
material.envMap = cubeTexture;
```
### Equirectangular to Cubemap
```javascript
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader.js";
…More from cloudai-x
- Threejs AnimationThree.js animation guidance for keyframes, skeletal animation, morph targets, and animation blending.
- Threejs FundamentalsThree.js scene setup guidance for cameras, renderer configuration, object hierarchy, and transforms.
- Threejs GeometryThree.js geometry patterns for built-in shapes, BufferGeometry, custom meshes, and instancing.
- Threejs InteractionThree.js interaction patterns for raycasting, controls, pointer input, and object selection.
- Threejs LightingThree.js lighting guidance for light types, shadows, environment lighting, and performance tuning.
- Threejs LoadersThree.js asset loading patterns for GLTF, textures, HDR assets, async loading, and progress handling.