【问题标题】:How to select objects when using the Orthographic camera in Three Js三J中使用正交相机时如何选择物体
【发布时间】:2014-11-11 00:52:01
【问题描述】:

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

我查看了一些 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() 传入的数组。

有什么想法吗?

【问题讨论】:

    标签: javascript three.js ray-picking


    【解决方案1】:

    这总是最愚蠢的事情;我的容器元素上有padding-left:250px;。删除它,它的工作原理。始终为您的偏移量正确!

    【讨论】:

      猜你喜欢
      • 2015-02-25
      • 2021-04-15
      • 2014-10-13
      • 1970-01-01
      • 2013-09-04
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多