I have been attempting to assign colors from an array to my object when a key is pressed, but I am only able to access the first element of the array. Is there another method that could be used for this purpose?
One approach I considered was using a random color function, however, my specific requirement does not involve randomly choosing colors.
var onKeyDown = function(event) {
if (event.keyCode == 67) { // when 'c' is pressed
var index=0;
object.traverse( function( child ) { if ( child instanceof THREE.Mesh ) {
if (child.material.name == "Leather") {
var colors = [0xff0000, 0x00ff00, 0x0000ff]; // red, green and blue
if(index == colors.length) index = 0;
child.material.color.setHex(colors[index++]);
child.material.needsUpdate=true;
child.geometry.buffersNeedUpdate = true;
child.geometry.uvsNeedUpdate = true;
}
}});
} };
document.addEventListener('keydown', onKeyDown, true);