Im creating a scene using three.js and im adding 3 spheres to it. Then im trying to switching all spheres created from wireframe to non-wireframe material. I not using scene.traverse() because i have more objects on my scene and i only want to switch the spheres, but with this code i can only switch one sphere. How can i get to every sphere? Any help? Thanks!
var numSpheres = 3;
function createSphere (x, y, z){
sphere = new THREE.Object3D();
material = new THREE.MeshBasicMaterial({ color: 0XFFA500, wireframe: true});
geometry = new THREE.SphereGeometry (2, 8, 8);
mesh = new THREE.Mesh(geometry, material);
sphere.add(mesh);
sphere.position.set(x, y, z);
scene.add(sphere);
}
createSpheres(numSpheres){
for(i = 1; i <= numSpheres; i++){
var randomnumber1 = Math.floor(Math.random() * (29.5 - -29.5 + 1)) + -29.5;
var randomnumber2 = Math.floor(Math.random() * (29.5 - -29.5 + 1)) + -29.5;
createSphere(randomnumber1, 3, randomnumber2);
}
}
function onKeyDown(e){
case 65:
case 97:
sphere.traverse(function (node){
if(node instanceof THREE.mesh) {
node.material.wireframe = !node.material.wireframe;
}
});