For читает только последний элемент
Имеется цикл, который прогоняет возможные варианты столкновения двух или более объектов с массивом BB_array. Всё бы хорошо, но только когда цикл запускается, он читает только последний элемент массива "BB_move_brush_array".
P.s. console.log('bruh!') выводится при столкновении всех элементов BB_move_brush_array с массивом BB_array, но visible false/true позволяет делать только с последним BB_move_brush_array
function init(){
brush1 = new Brush( new THREE.BoxBufferGeometry( 5, 5, 0.0001 ), material );
brush1_oper = new Operation( new THREE.BoxBufferGeometry( 5, 5, 0.0001 ), material );
wall_mesh_array.push(brush1);
scene.add(wall_mesh_array[0]);
wall_mesh_array[0].position.x = 0;
brush1BB = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
brush1BB.setFromObject(brush1);
BB_array.push(brush1BB);
brush2 = new Brush( new THREE.BoxBufferGeometry( 5, 5, 0.0001 ), material );
brush2_oper = new Operation( new THREE.BoxBufferGeometry( 5, 5, 0.0001 ), material );
wall_mesh_array.push(brush2);
scene.add(wall_mesh_array[1]);
wall_mesh_array[1].position.x = 6;
brush2BB = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
brush2BB.setFromObject(brush2);
BB_array.push(brush2BB);
brush3 = new Brush( new THREE.BoxBufferGeometry( 5, 5, 0.0001 ), material );
brush3_oper = new Operation( new THREE.BoxBufferGeometry( 5, 5, 0.0001 ), material );
wall_mesh_array.push(brush3);
scene.add(wall_mesh_array[2]);
wall_mesh_array[2].position.x = 12;
brush3BB = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
brush3BB.setFromObject(brush3);
BB_array.push(brush3BB);
brush4 = new Brush( new THREE.BoxGeometry(1, 1, 5), material_test );
brush4_oper = new Operation(new THREE.BoxGeometry(1, 1, 5), material_test);
brush4_oper.operation = SUBTRACTION;
brush4_oper.position.x = 0;
oper_move_mesh_array.push(brush4_oper);
move_mesh_array.push(brush4);
scene.add(move_mesh_array[0]);
move_mesh_array[0].position.x = 0;
move_mesh_array[0].position.z = 5;
move_mesh_array[0].isDraggable = true;
brush4BB = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
brush4BB.setFromObject(brush4);
BB_move_brush_array.push(brush4BB);
brush5 = new Brush( new THREE.BoxGeometry(1, 1, 5), material_test );
brush5_oper = new Operation(new THREE.BoxGeometry(1, 1, 5), material_test);
brush5_oper.operation = SUBTRACTION;
brush5_oper.position.x = 0;
oper_move_mesh_array.push(brush5_oper);
move_mesh_array.push(brush5);
scene.add(move_mesh_array[1]);
move_mesh_array[1].position.x = 6;
move_mesh_array[1].position.z = 5;
move_mesh_array[1].isDraggable = true;
brush5BB = new THREE.Box3(new THREE.Vector3(), new THREE.Vector3());
brush5BB.setFromObject(brush5);
BB_move_brush_array.push(brush5BB);
}
function updateCSG() {
for(let i = 0; i < BB_move_brush_array.length; i++){
for(let j = 0; j < BB_array.length; j++){
if(BB_move_brush_array[i].intersectsBox(BB_array[j])){
wall_mesh_array[j].visible = false;
console.log('bruh!')
}else{
wall_mesh_array[j].visible = true;
}
}
}
}
function animate() {
requestAnimationFrame(animate);
updateCSG();
}
Источник: Stack Overflow на русском