Careen
Careen

Reputation: 567

Three.js ray picking face segments of plane / mesh v69

As quads faces are no longer supported in three.js. I am having trouble Raycast picking segments between 4 vertices points as a square as they are now made up of triangle faces.

So my picking returns triangle shape not square:

                var faces = 10;
                var myplane = new THREE.Mesh(
                new THREE.PlaneGeometry(1000, 1000, faces, faces),
                new THREE.MeshNormalMaterial({
                color: 0x993333,
                wireframe: false
                }));

                objects.push(myplane);
                scene.add(myplane); 

                //raycast code


                if ( intersects.length > 0 ) {

                    _controls.noRotate = true;
                    var face = intersects[0].face;
                    var temp = intersects[0].object;
                    var geo = temp.geometry;

                    geo.vertices[face.a].z -= 20;
                    geo.vertices[face.b].z -= 20;
                    geo.vertices[face.c].z -= 20;
                    //want other face verts below basically
                    geo.vertices[face.d].z -= 20;
                    geo.vertices[face.e].z -= 20;
                    geo.vertices[face.f].z -= 20;   
                    geo.verticesNeedUpdate = true;

                    if(intersects[ 0 ].faceIndex % 2 == 0){
                        // geo.vertices[intersects[0].faceIndex + 1].z = 40;     
                       // console.log("Even face Index: " + intersects[ 0 ].faceIndex);

                     }else{
                        // console.log("Odd face Index: " + intersects[ 0 ].faceIndex -1);
                       //geo.vertices[intersects[0].faceIndex - 1].z = 40, of course doest work
                     }

                    }   

Below is a 2d example for a better vsual... Currently I get the red out-come , but I'm wanting to pick like the green example:

example of plane segments

Upvotes: 3

Views: 3224

Answers (2)

Hal50000
Hal50000

Reputation: 639

In the general case, getting and changing the geometry of a raycast to mesh triangle intersection is just:

var face = intersects[0].face;
var obj = intersects[0].object;
var geom = obj.geometry; 

var vertA = geom.vertices[face.a];
var vertB = geom.vertices[face.b];
var vertC = geom.vertices[face.c];

vertA.setY(100);
vertB.setY(110);
vertC.setY(105);

geom.verticesNeedUpdate = true;

No need to iterate through the faces array, or build a contrived face vertex array just to loop through it in the next step.

Upvotes: 0

Iam Coder
Iam Coder

Reputation: 1003

try this code to get intersection faces

if ( intersects.length > 0 )
{
    var faceIndex = intersects[0].faceIndex;
    var obj = intersects[0].object;
    var geom = obj.geometry;
    var faces = obj.geometry.faces;
    var facesIndices = ["a","b","c"];
    facesIndices.forEach(function(indices){
        geom.vertices[faces[faceIndex][indices]].setZ(-10);
    });
    if(faceIndex%2 == 0){
        faceIndex = faceIndex+1;
    }else{
        faceIndex = faceIndex-1;
    }
    facesIndices.forEach(function(indices){
        geom.vertices[faces[faceIndex][indices]].setZ(-10);
    }); 
    geom.verticesNeedUpdate = true;                               
}

Upvotes: 7

Related Questions