Capítulo 805 de 859
Using a live 2D <canvas> as a texture source (CanvasTexture) to generate dynamic content — most commonly text labels — at runtime instead of loading static image files.
CanvasTexture: wraps a <canvas> element so its pixel contents can be used as a texture.texture.needsUpdate: must be set to true after redrawing the canvas so three.js re-uploads the new pixels.Sprite) if labels should always face the camera, or HTML overlays if labels should never be occluded by 3D geometry.function makePerson(x, size, name, color) {
const canvas = makeLabelCanvas(size, name);
const texture = new THREE.CanvasTexture(canvas);
texture.minFilter = THREE.LinearFilter;
texture.wrapS = THREE.ClampToEdgeWrapping;
texture.wrapT = THREE.ClampToEdgeWrapping;
const labelMaterial = new THREE.MeshBasicMaterial({
map: texture,
side: THREE.DoubleSide,
transparent: true,
});
// ...attach label mesh to a root Object3D alongside body/head meshes
}
CanvasTexture from a 2D-drawn label and applying it to a plane material.CanvasTexture lets you texture objects with anything the 2D canvas API can draw, including dynamic text.texture.needsUpdate = true whenever the canvas contents change, or three.js won't re-upload them.