How to check if ray don`t intersect the face more

I create cubes and rectangles on my scene with userData, that have 6 parameters “false”. From every face i cast a ray and if it intersect face from other object, this face became “true”. All works very well, but i cant to chenge this parameter back to “false” if ray don`t intersect this face more

Piece of my code

box.userData.sides = {
                    0: false,
                    1: false,
                    2: false,
                    3: false,
                    4: false,
                    5: false
                }


            collision.push(box);
            scene.add(box);

            checkRaycast();


}


load(0, 0, 0, 'cube', 0, 0, 0);

function checkRaycast() {
    var raycaster = new THREE.Raycaster();
    var intersects = [];
    for (let j = 0; j < collision.length; j++) {

        var pos = collision[j].geometry.attributes.position; 
        var ori = new THREE.Vector3();
        var dir = new THREE.Vector3();
        var a = new THREE.Vector3(),
            b = new THREE.Vector3(),
            c = new THREE.Vector3(),
            tri = new THREE.Triangle();

        var index = collision[j].geometry.index;    
        var faces = index.count / 3;
        scene.updateMatrixWorld()
        for (let i = 0; i < faces; i++) {
            a.fromBufferAttribute(pos, index.array[i * 3 + 0]);
            b.fromBufferAttribute(pos, index.array[i * 3 + 1]);
            c.fromBufferAttribute(pos, index.array[i * 3 + 2]);
            a.set(a.x + collision[j].position.x, a.y + collision[j].position.y, a.z + collision[j].position.z);
            b.set(b.x + collision[j].position.x, b.y + collision[j].position.y, b.z + collision[j].position.z);
            c.set(c.x + collision[j].position.x, c.y + collision[j].position.y, c.z + collision[j].position.z);
            tri.set(a, b, c);
            tri.getMidpoint(ori);
            tri.getNormal(dir);

            raycaster.set(ori, dir);    

            intersects = raycaster.intersectObjects(collision, true);

            //scene.add(new THREE.ArrowHelper(dir, ori, 500, 0xff0000));
            if (intersects.length > 0) {
                var intFace = Math.floor(intersects[0].faceIndex / 2);
                if (intersects[0].distance > 0 && intersects[0].distance < 0.2) {
                    intersects[0].object.userData.sides[intFace] = true;
                }
            } // this works but with all sides. I need to check if ray don`t intersect specific side more
              else {

                    collision[j].userData.sides[0] = false;
                    collision[j].userData.sides[1] = false;
                    collision[j].userData.sides[2] = false;
                    collision[j].userData.sides[3] = false;
                    collision[j].userData.sides[4] = false;
                    collision[j].userData.sides[5] = false;

                }

            }
        }
    }

Why don’t you just reset the flags to false before performing the raycast per frame?

Thank you for your replying! Could you explain a bit more? I am really noob in three js

When reading your post, it seems you need these flags to indicate whether an intersection occurs with one of the cubes faces or not. If you reset these flags to their initial values before doing a raycast, they automatically evaluate to false if there is no intersection.

If i add this code to render function

for (let j = 0; j < collision.length; j++) {
    for (let k = 0; k < 6; k++) {
	    collision[j].userData.sides[k] = false;

	}
	}

falgs resets to false, whatever intersection occurs or not

Im sorry. I find my problem. Thank you very much!!! Now its works fine

I set ray from center every face, but how i can to move ray`s origin a little further automatically?