I recently came across a raycasting tutorial for threejs on a website, and the author demonstrated adding cubes to the scene in an interesting way:
geom = new THREE.CubeGeometry( 5, 5, 5 );
cubes = new THREE.Object3D();
scene.add( cubes );
for(var i = 0; i < 100; i++ ) {
var grayness = Math.random() * 0.5 + 0.25,
mat = new THREE.MeshBasicMaterial(),
cube = new THREE.Mesh( geom, mat );
mat.color.setRGB( grayness, grayness, grayness );
cube.position.set( range * (0.5 - Math.random()), range * (0.5 - Math.random()), range * (0.5 - Math.random()) );
cube.rotation.set( Math.random(), Math.random(), Math.random() ).multiplyScalar( 2 * Math.PI );
cube.grayness = grayness; // *** NOTE THIS
cubes.add( cube );
}
The concept seems clear, but it got me thinking:
Is there an advantage to adding multiple cubes to an Object3D
instead of individually adding each cube to the scene?
I'm particularly interested in situations where you're utilizing raycasting and need to animate all the cubes (let's say around 200 cubes or planes) simultaneously.