THREE.js Raycasting from a child camera to the scene

前端 未结 1 964
耶瑟儿~
耶瑟儿~ 2020-12-19 06:42

I am trying to raycast the mouse from my camera to do some hover and click events on meshes in my scene.

My problem is, that my camera is currently the child object

相关标签:
1条回答
  • 2020-12-19 07:06

    You can use the following pattern for raycasting, and it will work correctly even if the camera is the child of another object. It works for both perspective and orthographic cameras.

    var raycaster = new THREE.Raycaster(); // create once and reuse
    var mouse = new THREE.Vector2(); // create once and reuse
    
    ...
    
    mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
    
    raycaster.setFromCamera( mouse, camera );
    
    var intersects = raycaster.intersectObjects( objects, recursiveFlag );
    

    three.js r.84

    0 讨论(0)
提交回复
热议问题