Multiple materials for a single mesh

When creating THREE.Mesh, so far we've used a single material. It is also possible to define a specific material for each of the faces of a geometry. For instance, if you have a cube, which has 12 faces (remember, Three.js works with triangles), you can assign a different material (for example, with a different color) to each side of the cube. Doing this is really straightforward, as you can see from the following piece of code:

var matArray = []; matArray.push(new THREE.MeshBasicMaterial( { color: 0x009e60 })); matArray.push(new THREE.MeshBasicMaterial( { color: 0x0051ba })); matArray.push(new THREE.MeshBasicMaterial( { color: 0xffd500 })); matArray.push(new THREE.MeshBasicMaterial( { color: 0xff5800 ...

Get Learn Three.js - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.