three.js orthographic camera object picking

孤街醉人 提交于 2019-11-27 06:32:21

问题


i am trying to pick objects in a scene where i use an orthographic camera. my code fragment already works, but it is not precise. i already found some answers on stackoverflow, but those are deprecated or won't work anymore at all. here is my code onMouseDown

function onDocumentMouseUp( event ) {
    event.preventDefault();

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

    var vector = new THREE.Vector3(mouse.x, mouse.y, 0.5);
    var pos = camera.position;
    var ray = new THREE.Raycaster(pos, vector.unproject(camera).sub(camera.position).normalize());

    var intersects = ray.intersectObjects(objects);

    if (intersects.length > 0) {
        console.log("touched:" + intersects[0]);
    }
    else {
        console.log("not touched");
    }
}

please see http://jsfiddle.net/ujzpe07t/1/

if you click some pixels away left/right/above/below the cube, it still tells me that the object was touched.

i am using three.js r69.

any hints would be very much appreciated. thanks, cheers!


回答1:


Here is the pattern to use when raycasting (picking) with either an orthographic camera or a perspective camera:

var raycaster = new THREE.Raycaster(); // create once
var mouse = new THREE.Vector2(); // create once

...

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



来源:https://stackoverflow.com/questions/26652888/three-js-orthographic-camera-object-picking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!