Three.js mouse picking object

前端 未结 1 1520
清歌不尽
清歌不尽 2021-01-03 17:21

So i found a lot of tutorials on how to use mouse picking on default objects available in Threejs itself, but is it possible to use mouse picking on imported models like a .

相关标签:
1条回答
  • 2021-01-03 17:46

    It doesn't matter if its an imported model or a default object. The mouse picking code in the Three.JS examples works with any sort of mesh.

    http://threejs.org/examples/#webgl_interactive_buffergeometry

    var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
    projector.unprojectVector( vector, camera );
    
    raycaster.set(camera.position, vector.sub(camera.position).normalize());
    
    var intersects = raycaster.intersectObject(mesh); 
    

    Use the meshes created by the loader in the raycaster.intersectObject function. In your case you could get all of the meshes that compose an object with the below loading code.

    var loader = new THREE.OBJLoader( manager );
    loader.load( 'example.obj', function ( object ) {
        object.traverse( function ( child ) {
            if ( child instanceof THREE.Mesh ) {
                //The child is the bit needed for the raycaster.intersectObject() method
            }
        } );
        object.position.y = - 80;
        scene.add( object );
    } );
    
    0 讨论(0)
提交回复
热议问题