I'm using THREE.js to display several thousand cubes at a time. The geometries of these cubes have been merged (I'm displaying several thousand at a time, too slow if they aren't merged) and a requirement I have is to colour the cubes based on a value. The example here colours each cube individually but is grindingly slow once more than 1000 cubes are generated. If I simply use one material for all objects, I can render 100,000+ cubes and the animation is still smooth.
Is there a better way to accomplish what I need?
Code
/* snip */
materials = [];
geometry = new THREE.CubeGeometry(20, 20, 20);
mesh = new THREE.Mesh(geometry);
mergedCubes = new THREE.Geometry();
// Create randomly positioned cubes
for (var i = 0; i < numCubes; i++) {
material = new THREE.MeshBasicMaterial({color:0xffffff * Math.random(),wireframe:false});
materials.push(material);
THREE.GeometryUtils.setMaterialIndex(mesh.geometry, i);
mesh.position.x = Math.random() * 300 - 150;
mesh.position.y = Math.random() * 300 - 150;
mesh.position.z = Math.random() * 300 - 150;
THREE.GeometryUtils.merge(mergedCubes, mesh);
}
// Create merged mesh
group = new THREE.Mesh(mergedCubes, new THREE.MeshFaceMaterial( materials ));
group.matrixAutoUpdate = false;
group.updateMatrix();
/* snip */