Textures
A texture is an image drawn on an object's faces. Give a material a texture:
import { loadTexture } from "./trid/loaders/texture.js";
const crate = await loadTexture("textures/crate.png");
scene.add({ mesh: makeShape("cube"), material: { texture: crate } });import { Scene, makeShape, WebGLRenderer, loadTexture, OrbitCamera } from "./trid/index.js";
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 320, camera: { z: -2.4, fov: 0.7 },
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
});
// 1. An image file. Materials work before it loads; the texture shows up when it's ready.
const cube = scene.add({ mesh: makeShape("cube", { size: 0.5 }), pos: { x: -1, y: 0, z: 0 }, material: {} });
const globe = scene.add({ mesh: makeShape("sphere", { r: 0.3 }), pos: { x: -0.33, y: 0, z: 0 }, material: { smooth: true } });
// On the docs site, files live under BASE_URL. In your page: loadTexture("textures/grid.png")
loadTexture(import.meta.env.BASE_URL + "textures/grid.png").then((grid) => {
cube.material.texture = grid;
globe.material.texture = grid;
});
// 2. A canvas redrawn every frame: liveTexture reads it again each frame
// (a canvas painted once needs nothing; one redrawn now and then: updateTexture(canvas))
const live = document.createElement("canvas");
live.width = live.height = 128;
const g = live.getContext("2d");
const screen = scene.add({
mesh: makeShape("box", { w: 0.55, h: 0.45, d: 0.1 }), pos: { x: 0.36, y: 0, z: 0 },
material: { texture: live, liveTexture: true },
});
// 3. Pixel art: an 8 × 8 image, kept sharp
const pixels = document.createElement("canvas");
pixels.width = pixels.height = 8;
const p = pixels.getContext("2d");
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 8; x++) {
p.fillStyle = (x + y) % 2 ? "#E9806E" : "#f5f0e6";
p.fillRect(x, y, 1, 1);
}
}
const block = scene.add({
mesh: makeShape("cube", { size: 0.45 }), pos: { x: 1.05, y: 0, z: 0 },
material: { texture: pixels, pixelated: true },
});
scene.use(new OrbitCamera({ pitch: -0.3 }));
scene.start((dt, time) => {
for (const o of [cube, globe, screen, block]) o.rot.y += dt * 0.4;
// Redraw the live texture: a bar sweeping across
g.fillStyle = "#141414";
g.fillRect(0, 0, 128, 128);
g.fillStyle = "#7ae96e";
g.fillRect(0, 0, ((time * 60) % 128), 128);
g.fillStyle = "#ffffff";
g.font = "bold 28px sans-serif";
g.fillText(time.toFixed(1), 30, 74);
});
return () => scene.destroy();
}Texture sources
texture can be anything the canvas can draw:
| Source | Notes |
|---|---|
An image (<img>, loadTexture(url)) | Uploaded once |
A <canvas> | Uploaded once. After drawing on it again, call updateTexture(canvas), or set liveTexture: true to read it every frame. See Changing a canvas |
A <video> | Read every frame, so the video plays on the object |
An ImageBitmap | Uploaded once |
Until an image has loaded, the object is drawn with its fill color. You can also set or change material.texture at any time.
Changing a canvas
A canvas is read once, the first time it's drawn: that keeps it fast (no upload every frame) and sharp (the WebGL renderer makes mipmaps for it, so it doesn't shimmer far away). When you draw on it again, tell Trid:
paint(canvas); // your drawing code
updateTexture(canvas); // read again on the next frame, everywhere it's usedFor a canvas you redraw every frame (a clock, a mini game, a video effect), set liveTexture: true on its material instead, as the demo above does:
scene.add({ mesh, material: { texture: canvas, liveTexture: true } });Changed in 0.3
Before 0.3, every canvas was read again every frame. If a canvas texture stopped changing after you updated, add updateTexture(canvas) after drawing, or liveTexture: true.
Paint a canvas before the first frame that draws it, or call updateTexture once it's painted: a canvas drawn later (after an image loads, for example) would otherwise stay blank.
loadTexture(url) returns a promise for the loaded image. It asks for permission to use images from other sites (CORS): images from your own site always work, while images from other sites only work if that site allows it.
Procedural textures
makeTexture(style, options) paints a pattern in code and returns a canvas, ready to use as a texture: no image files needed. Every style tiles with no seam, so it wraps around spheres and lathes cleanly and repeats with textureRepeat.
import { Scene, makeShape, makeTexture, TEXTURE_STYLES, OrbitCamera, WebGLRenderer } from "./trid/index.js";
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 380, camera: { z: -3.1, fov: 0.7 },
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
});
// Every built-in style, on a sphere (they tile, so there's no seam where the texture wraps around)
const balls = TEXTURE_STYLES.map((style, i) => scene.add({
mesh: makeShape("sphere", { r: 0.28, rings: 24, segments: 48 }),
pos: { x: ((i % 5) - 2) * 0.72, y: i < 5 ? 0.38 : -0.38, z: 0 },
material: { texture: makeTexture(style, { size: 256, scale: 3, seed: 7, grain: style === "solid" ? 0.4 : 0.1 }), smooth: true },
}));
scene.use(new OrbitCamera());
scene.start((dt) => balls.forEach((b) => (b.rot.y += dt * 0.3)));
return () => scene.destroy();
}const floor = makeTexture("wood", { colors: ["#a87447", "#6b4426"], scale: 3, grain: 0.15 });
scene.add({ mesh: makeShape("plane", { size: 4 }), material: { texture: floor, textureRepeat: 4 } });| Style | Looks like | scale is |
|---|---|---|
"noise" | Soft clouds | Blobs across |
"marble" | Veined stone | Veins across |
"wood" | Boards with rings and grain (running along v) | Rings across, ×2 |
"bricks" | A brick wall, each brick its own shade | Bricks across (twice as many rows) |
"checker" | A checkerboard | Squares across (made even) |
"bark" | Rough ridges running along v | Ridges across, ×3 |
"spots" | Blotches | Spots across |
"stripes" | Wavy stripes across v | Stripes |
"solid" | The first color only (rough with grain) | — |
| Option | Default | Meaning |
|---|---|---|
size | 256 | Width and height in pixels. A power of two (128, 256, 512...) works best with WebGL 1 |
colors | suits the style | [first, second]: hex, rgb() / rgba(), CSS names or [r, g, b, a?] |
scale | 4 | How many times the pattern repeats across (a whole number, so it tiles) |
seed | 0 | Another whole number gives another texture of the same style |
grain | 0 | Fine speckles on top, from 0 to 1 |
canvas | a new one | Paint into this canvas instead. updateTexture is called for you |
style can also be your own function (u, v) => t, from 0 (first color) to 1 (second), for u and v from 0 to 1. Build it from the noise functions with a period and it tiles too:
const lava = makeTexture((u, v) => fbm2(u * 4, v * 4, { period: 4 }) > 0.5 ? 1 : 0, { colors: ["#300", "#f60"] });texturePixels(style, options) gives the same pixels without a canvas ({ width, height, data }), for a worker or your own processing.
Material options
| Option | Default | Meaning |
|---|---|---|
texture | none | The image |
fill | "#ffffff" with a texture | Multiplies the texture's colors. White keeps them as they are; any other color tints them |
pixelated | false | Keep pixels sharp (for pixel art) instead of smoothing them |
liveTexture | true for videos | Read the texture again every frame. For a canvas you redraw every frame; false freezes a video |
mapping | "uv" | Where texture coordinates come from. See Texture mapping |
textureRepeat | 1 | How many times the image repeats across: a number, or { u, v } |
textureOffset | { u: 0, v: 0 } | Moves the image; 1 is its whole width or height |
textureRotation | 0 | Turns the image around its center, in radians |
Everything else still applies: the light shades textured faces, smooth works, and effects see the texture's color in s.base and the texture coordinates in s.uv.
Texture coordinates
To know which part of the image goes where, a mesh needs texture coordinates (often called UVs): for each corner of each face, a point in the image. u goes from 0 (left edge) to 1 (right edge), and v from 0 (bottom) to 1 (top). Values past 1 repeat the image.
They're stored like this:
const quad = {
vertices: [
{ x: -0.5, y: -0.5, z: 0 }, { x: 0.5, y: -0.5, z: 0 },
{ x: 0.5, y: 0.5, z: 0 }, { x: -0.5, y: 0.5, z: 0 },
],
edges: [[0, 1, 2, 3]],
faces: [[0, 3, 2, 1]],
uvs: [{ u: 0, v: 0 }, { u: 1, v: 0 }, { u: 1, v: 1 }, { u: 0, v: 1 }],
faceUVs: [[0, 3, 2, 1]], // for each face, the uv of each corner, in the same order
};You rarely write them by hand:
- Built-in shapes have them. Cubes and boxes show the whole image on each face, upright as seen from outside. Spheres wrap it around like a world map:
uaround the equator,vfrom bottom to top. - OBJ files exported with texture coordinates (
vtlines) keep them. In Blender, that's the UV map, exported with UV Coordinates checked.
A mesh without texture coordinates is drawn without its texture, and a warning is printed in the console. Give it a mapping instead.
Texture mapping
A material's mapping makes texture coordinates from the mesh's shape, so any mesh can be textured, including OBJ files without vt lines. It also replaces coordinates a mesh already has.
scene.add({ mesh, material: { texture: wood, mapping: "box" } });import { Scene, makeShape, scaleMesh, WebGLRenderer, loadTexture, OrbitCamera } from "./trid/index.js";
export default function (canvas) {
const scene = new Scene(canvas, {
width: 800, height: 320, camera: { z: -2.4, fov: 0.7 },
renderer: WebGLRenderer.isSupported() ? new WebGLRenderer() : undefined,
});
// 1. A die: a list of materials, one per side of the cube (sides are numbered 0 to 5)
const side = (pips) => {
const c = document.createElement("canvas");
c.width = c.height = 96;
const g = c.getContext("2d");
g.fillStyle = "#f5f0e6";
g.fillRect(0, 0, 96, 96);
g.fillStyle = pips === 1 ? "#E9806E" : "#141414";
for (const [x, y] of PIPS[pips]) {
g.beginPath();
g.arc(48 + x * 26, 48 + y * 26, 9, 0, Math.PI * 2);
g.fill();
}
return { texture: c };
};
// Opposite sides add up to 7: front/back, top/bottom, right/left
const die = scene.add({
mesh: makeShape("cube", { size: 0.5 }), pos: { x: -0.95, y: 0, z: 0 },
material: [side(1), side(6), side(2), side(5), side(3), side(4)],
});
// 2. A tall ellipsoid with the image wrapped around like a label,
// repeated 4 times around and scrolled sideways every frame
const label = scene.add({
mesh: scaleMesh(makeShape("sphere", { r: 0.3 }), 0.8, 1.3, 0.8), pos: { x: 0, y: 0, z: 0 },
material: { mapping: "cylinder", textureRepeat: { u: 4, v: 2 }, smooth: true },
});
// 3. A sphere with box mapping: each face takes the image from the side it faces most
const boxed = scene.add({
mesh: makeShape("sphere", { r: 0.3 }), pos: { x: 0.95, y: 0, z: 0 },
material: { mapping: "box", smooth: true },
});
// On the docs site, files live under BASE_URL. In your page: loadTexture("textures/grid.png")
loadTexture(import.meta.env.BASE_URL + "textures/grid.png").then((grid) => {
label.material.texture = grid;
boxed.material.texture = grid;
});
scene.use(new OrbitCamera({ pitch: -0.3 }));
scene.start((dt, time) => {
die.rot.y += dt * 0.5;
die.rot.x += dt * 0.3;
boxed.rot.y += dt * 0.4;
label.material.textureOffset = { u: time * 0.1, v: 0 };
});
return () => scene.destroy();
}
// Pip positions on a 3 × 3 grid (-1, 0, 1)
const PIPS = {
1: [[0, 0]],
2: [[-1, -1], [1, 1]],
3: [[-1, -1], [0, 0], [1, 1]],
4: [[-1, -1], [1, -1], [-1, 1], [1, 1]],
5: [[-1, -1], [1, -1], [0, 0], [-1, 1], [1, 1]],
6: [[-1, -1], [1, -1], [-1, 0], [1, 0], [-1, 1], [1, 1]],
};| Mapping | What it does | Good for |
|---|---|---|
"uv" | The mesh's own texture coordinates (the default) | Built-in shapes, OBJ files with vt |
"planar" | Projects the image straight through the mesh, from the front | Flat things: floors, walls, signs |
"box" | Each face takes the image from the side of the box it faces most | Boxy models, terrain, buildings |
"sphere" | Wraps the image around like a world map | Round things: planets, balls |
"cylinder" | Wraps the image around the sides like a label; the ends get a flat image | Cans, bottles, columns |
Every mapping fits the image to the mesh's bounding box. Add textureRepeat to tile it instead of stretching it.
"planar", "sphere" and "cylinder" work along an axis. Pass an object to pick it:
material: { mapping: { type: "planar", axis: "y" } } // from above, like a floor
material: { mapping: { type: "cylinder", axis: "x" } } // a can lying on its side| Mapping | Default axis | Meaning of axis |
|---|---|---|
"planar" | "z" | The direction the image is projected along |
"sphere", "cylinder" | "y" | The line the image wraps around |
Baking a mapping into a mesh
mapUVs(mesh, mapping) writes the coordinates into the mesh itself, as uvs and faceUVs. Use it once instead of setting mapping on every material, or to keep the coordinates when you change or export the mesh:
import { loadOBJ, mapUVs } from "trid3d";
const rock = mapUVs(await loadOBJ("rock.obj", { fit: 1 }), "box");computeUVs(mesh, mapping) returns { uvs, faceUVs } without changing the mesh.
Repeat, offset and rotation
These three options move the image on the faces without changing the mesh:
material: {
texture: tiles,
textureRepeat: { u: 4, v: 2 }, // 4 times across, twice up
textureOffset: { u: 0.25, v: 0 }, // a quarter of the image to the side
textureRotation: Math.PI / 4, // turned 45°
}The image is turned around its center first, then repeated, then moved. They can change every frame, which makes scrolling textures like water or conveyor belts cheap:
scene.start((dt, time) => (belt.material.textureOffset = { u: time * 0.5, v: 0 }));WebGL 1 and repeating
In browsers that only have WebGL 1, the WebGL renderer can only repeat images whose width and height are powers of two (64, 128, 256…). Other sizes are stretched at the edges instead. WebGL 2, which every current browser has, doesn't have this limit.
Effects get the final coordinates in s.uv, with repeat, offset and rotation applied.
A texture per face
material can also be a list of materials. The mesh's faceMaterials says which one each face uses, by its position in the list:
scene.add({
mesh: makeShape("cube"),
material: [
{ texture: front }, { texture: back }, { texture: top },
{ texture: bottom }, { texture: right }, { texture: left },
],
});Each material in the list is complete on its own: it can have its own fill, texture, mapping, smooth or effects. Faces without an entry in faceMaterials, or pointing past the end of the list, use the first material.
Where faceMaterials comes from:
- Cubes and boxes number their sides: 0 front (−Z), 1 back (+Z), 2 top, 3 bottom, 4 right (+X), 5 left (−X).
- OBJ files with
usemtllines get one number per material name, listed inmesh.materialNames. See Loading OBJ files. - Your own meshes: set
mesh.faceMaterialsto a list with a number per face, likefaces.
// A sphere with a band around the middle
const ball = makeShape("sphere");
ball.faceMaterials = ball.faces.map((f) => {
const y = f.reduce((s, i) => s + ball.vertices[i].y, 0) / f.length;
return Math.abs(y) < 0.1 ? 1 : 0;
});
scene.add({ mesh: ball, material: [{ fill: "#8ab4f8" }, { fill: "#E9806E" }] });After changing faceMaterials in place, set mesh.dirty = true.
Renderers
The WebGL renderer maps textures per pixel, with correct perspective and smooth results on surfaces seen at a grazing angle. Pixels that are fully transparent in the image (like the empty parts of a PNG) are left out, so you can see through them.
Graphics cards have a largest texture size (usually 8192 or 16384 pixels). A bigger image is drawn at that size instead, and a warning is printed in the console.
The canvas renderer draws the image on each triangle with the 2D canvas. It works everywhere, but:
- The image bends a little along the diagonal of big faces seen up close. Splitting faces with
maxFaceSizefixes most of it. - Surfaces seen almost edge-on can look noisy.
- It's slower: each textured triangle is several drawing steps.
- See-through parts of an image and effects aren't supported on textured faces.
For textured models, prefer WebGL.