如何在三个J中使用正交相机时选择对象

时间:2014-09-17 05:26:00

标签: javascript three.js ray-picking

我有一个等距关卡,我试图在关卡中选择对象。

我已经查看了一些Stackoverflow的答案,包括这一个Orthographic camera and selecting objects with raycast,但目前似乎没有任何工作。有些东西已关闭,也许它与我的相机设置有关,所以这是我正在使用的相关代码。

// set up camera
scope.camera = new THREE.OrthographicCamera( - scope.cameraDistance * aspect, scope.cameraDistance * aspect, scope.cameraDistance, - scope.cameraDistance, - height, 1000 );
scope.camera.position.set(0 , 0 , 0);
scope.camera.rotation.order = scope.rotationOrder; // = "YXZ"
scope.camera.rotation.y = - Math.PI / 4;
scope.camera.rotation.x = Math.atan(- 1 / Math.sqrt(2));

当我遍历矩阵并添加切片时,我将它们全部添加到new THREE.Object3D(),然后将该对象添加到场景中。我的onMouseMove事件看起来像这样

onMouseMove:function(event) {
    event.preventDefault();

    var scope = Game.GSThree,
        $container = $(scope.container.element),
        width = $container.width(),
        height = $container.height(),
        vector,
        ray,
        intersects;

    scope.mouse.x = (event.clientX / width) * 2 - 1;
    scope.mouse.y = - (event.clientY / height) * 2 + 1;

    vector = new THREE.Vector3(scope.mouse.x , scope.mouse.y , 0.5);
    ray = scope.projector.pickingRay(vector , scope.camera);
    intersects = ray.intersectObjects(scope.tiles.children);

    if(intersects.length) {
        console.log(intersects[0]);
    }
}

现在的问题是某些事情非常糟糕。当光线不在它们附近时,光线与事物相交,并且似乎每次与tiles个多个孩子相交。如果我记录intersects.length,它有时会返回3个,2个或1个对象。以防它是相关的;我的每个对象网格的材质是new new THREE.MeshFaceMaterial(),其中包含一个包含6 new THREE.MeshBasicMaterial()的数组。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

它始终是最愚蠢的事情;我的容器元素上有padding-left:250px;。删除它,它的工作原理。始终纠正您的抵消!