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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.