Stefano Maglione
Stefano Maglione

Reputation: 4160

Three.js raycaster to intersect only specified part of shape

I need to get an intersection using raycaster only with vertices of a geometry and not with all the geometry shape. I wrote this code but nothing happen if I click on vertice setted.

         function onDocumentMouseClick(event) {

            event.preventDefault();

            mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
            mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;

            raycaster = projector.pickingRay(mouse2D.clone(), camera);

            var intersects = raycaster.intersectObjects(objects[0].geometry.vertices[0]);// I want intersection only with vertices

            if (intersects.length > 0) {

                console.log("ok");}       

Upvotes: 0

Views: 1270

Answers (1)

caseygrun
caseygrun

Reputation: 2170

First, you need to pass an array of objects to raycaster.intersectObjects, as suggested by pixelmike:

var intersects = raycaster.intersectObjects([objects[0]])

You can't get the vertex of the intersection per se, but you can get the face for each intersection:

for ( var i = 0; i < intersects.length; i++ ) {
    if ( intersect.face == myTargetFace ) {
        console.log( "ok" );
    }
}

If you really want the particular object to only check for intersections with a particular subset of faces, you could override the raycast method of that object (a la https://github.com/mrdoob/three.js/blob/master/src/objects/Mesh.js#L56-L340 ) to only search those faces.

three.js r68

Upvotes: 2

Related Questions