user3591153
user3591153

Reputation: 419

raycaster Intersection isn't accurate after moving vertices

I'm having issue with raycaster.intersectObjects() after moving vertices of a geometry.. It doesn't intersect with the new geometry, it only intersects with the part that overlaps the original area. The object looks correctly modified in the view. I've been trying to search for an answer but none of the solutions seem to be working. I'm not sure what is wrong.

 //move parts/vertices
        for(var p=0; p<latticeModel.selected.length; p++){
            var partNum = latticeModel.selected[p];
            if(latticeModel.part[partNum].children[1].visible==true){
                latticeModel.part[partNum].position.add(vector);
                latticeCloud.tiles.part[partNum].position.add(vector);
            }else{
                var localVector = vector.clone();
                localVector.add(latticeCloud.tiles.part[partNum].position);
                latticeCloud.tiles.part[partNum].worldToLocal(localVector);
                for(var v=0; v<4; v++){
                    if( latticeCloud.tiles.colors[partNum][v].getHex() == 0xfe0000 ||
                        latticeCloud.tiles.colors[partNum][v].getHex() == 0xff00ff ){
                        latticeCloud.tiles.part[partNum].geometry.vertices[v].add(localVector);
                        latticeCloud.tiles.part[partNum].geometry.verticesNeedUpdate = true;
                        latticeModel.part[partNum].children[0].geometry.vertices[v].add(localVector);
                        latticeModel.part[partNum].children[0].geometry.verticesNeedUpdate = true;
                        //added these below to try and debug, but still won't help
                        latticeModel.part[partNum].children[0].geometry.normalsNeedUpdate = true;
                        latticeModel.part[partNum].children[0].geometry.elementsNeedUpdate = true;
                        latticeModel.part[partNum].children[0].geometry.tangentsNeedUpdate = true;
                        latticeModel.part[partNum].children[0].geometry.computeFaceNormals = true;
                        latticeModel.part[partNum].children[0].geometry.computeVertexNormals = true;
                        latticeModel.part[partNum].children[0].geometry.computeBoundingBox = true;
                        latticeModel.part[partNum].children[0].geometry.computeBoundingSphere = true;
                    }
                }
            }
        }

Here is the intersect part:

mouse.x = 2 * (e.clientX / window.innerWidth) - 1;
mouse.y = 1 - 2 * (e.clientY / window.innerHeight);
var vector = new THREE.Vector3(mouse.x, mouse.y, 0.5).unproject(camera);
var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
scene.updateMatrixWorld();

//find the part mouse is overlapping and highlight it.
mouse.intersects = raycaster.intersectObjects(latticeModel.part, true);
if(mouse.intersects.length){
    var foundPart = latticeModel.part.indexOf(mouse.intersects[0].object.parent);
    if(foundPart!=mouse.selectedPart){
        if(latticeModel.part.length>mouse.selectedPart && mouse.selectedPart!=-1){
            latticeModel.part[mouse.selectedPart].children[2].visible = false;
        }
        mouse.selectedPart = foundPart;
        latticeModel.part[mouse.selectedPart].children[2].visible = true;
    }
}else{
    if(latticeModel.part.length>mouse.selectedPart && mouse.selectedPart!=-1){
        latticeModel.part[mouse.selectedPart].children[2].visible = false;
        mouse.selectedPart = -1;
    }
}

Upvotes: 0

Views: 625

Answers (1)

WestLangley
WestLangley

Reputation: 104763

If you modify the vertices of a geometry, the geometry's bounding sphere and bounding box may become invalid.

The raycasting code will recompute them for you, but you need to set the following:

geometry.boundingSphere = null;
geometry.boundingBox = null;

three.js r.71

Upvotes: 3

Related Questions