7

I loaded the model using OBJLoader, here is the code for loading the obj file:

        var loader = new THREE.OBJLoader();
        loader.load('obj/teeth/teeth4_5.obj', function(object) {
            model = object;
            scene.add( model );
            objects.push( model );
        });

And I'm trying to use raycaster to find the intersection. I implemented my code from the canvas_interactive_cubes example (http://mrdoob.github.io/three.js/examples/canvas_interactive_cubes.html) in three.js. Here is the code to find the intersection:

    function onDocumentMouseDown( event ){
        event.preventDefault();
        var mouseX = (event.clientX / window.innerWidth)*2-1;
        var mouseY = -(event.clientY /window.innerHeight)*2+1;
        var vector = new THREE.Vector3( mouseX, mouseY, 0.5 );
        projector.unprojectVector( vector, camera );
        var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
        var intersects = raycaster.intersectObjects( scene.children );
        console.log( intersects[0].point);
    }

Unfortunately I'm not able to get the x,y,z coordinates of the intersection, no matter where I clicked, it always showed "TypeError: intersects[0] is undefined".

I'm getting stuck here for several days. Can someone tell me a way to get the intersection on a loaded obj file? I appreciate your help.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2309002
  • 73
  • 1
  • 3

1 Answers1

10

Try adding the recursive flag like so:

var intersects = raycaster.intersectObjects( objects, true );

three.js r.58

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • WOW, it works... that's so easy. Seems like I asked a silly question. And it takes long to find a intersection on a large obj file. Thanks a lot for your answer!! – user2309002 Apr 23 '13 at 04:17
  • I’ve spent 2 hours on this. Add `,true` and it works. I've never seen it on several examples that i studied. Thanks ! – Sébastien Gicquel Jun 15 '15 at 11:41