正射相机和pickingRay?

时间:2013-12-03 21:13:22

标签: three.js orthographic picking

我尝试实现一个功能来选择和移动正交相机的舞台复合物中的对象。

我希望得到与http://threejs.org/examples/webgl_interactive_draggablecubes.html

相同的示例

但不是透视相机。

我已经取代了:

var camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );

with:

var camera =  new THREE.OrthographicCamera(window.innerWidth / -zoom, window.innerWidth / zoom, window.innerHeight / zoom, window.innerHeight / -zoom, -1000, 1000);

并取而代之:

var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );

由此:

var ray = projector.pickingRay( vector, camera );

但是,它似乎不足以运行。

那么,它缺少什么?

是否可以使用Projector和Ray with OrthographicCamera ??

感谢。

2 个答案:

答案 0 :(得分:2)

请在您的示例中尝试此代码:

event.preventDefault();
    if (event.button === 0) {

        var projector = new THREE.Projector();
        var vector = new THREE.Vector3((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0.5);
        // use picking ray since it's an orthographic camera
        var raycaster = projector.pickingRay(vector, camera);
        //ditermine whether any of the supplied objects are hit by this ray    
        var intersertsObj = raycaster.intersectObjects(_entities);
        // _entities : is array entities you put to scence
        // you can store this list before you entity to scence: 
        // _entities.push(entity);
        // scene.add(entity);
        if (intersertsObj.length > 0) {           
            var pickedObject = intersertsObj[0];               
        }       
    }
    event = null;

它在我的项目中运行良好。

答案 1 :(得分:0)

最初询问此问题后引入了一个函数Raycaster.setFromCamera()。它专门处理正交相机,所以我认为现在是解决这个问题的首选方法。