【问题标题】:RayCaster stops detecting object when i call camera.lookAt()当我调用 camera.lookAt() 时,RayCaster 停止检测对象
【发布时间】:2017-01-16 01:50:12
【问题描述】:

每当我点击场景时,我都会调用它。

function onMouseDown(event) {
    controls.update();
    var position = new THREE.Vector3();
    position.setFromMatrixPosition( scene.group.children[0].matrixWorld );
    position.normalize();
    console.log(position);

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

    mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
    mouse.y = - ( event.clientY / renderer.domElement.height ) * 2 + 1;
    //console.log(mouse);

    raycaster.setFromCamera( mouse, camera );

    console.log(raycaster.ray.direction);

    var intersects = raycaster.intersectObjects( scene.group.children , false );
    if ( intersects.length > 0 ) {
        intersects[ 0 ].object.material.color.set( 0xff0000 );
        console.log(intersects);
    }
}

最初,当我没有应用旋转时,点击可以工作,但是一旦我调用

scope.target.x = offset * Math.sin( (scope.phi + iphi) * Math.PI / 180 ) * Math.cos( (scope.theta + itheta) * Math.PI / 180 );
scope.target.y = offset * Math.cos( (scope.phi + iphi) * Math.PI / 180 );
scope.target.z = offset * Math.sin( (scope.phi + iphi) * Math.PI / 180 ) * Math.sin( (scope.theta + itheta) * Math.PI / 180 );

scope.camera.lookAt( scope.target );

RayCaster 不返回任何对象。

我还注意到,即使在旋转之后,我的光线投射器也会在屏幕的同一位置给出相同的方向矢量。

然后我尝试手动在 raycaster 上应用旋转

var e = new THREE.Euler(((controls.phi-90)/180)*Math.PI, 0, (controls.theta/180)*Math.PI, 'XYZ');
raycaster.setFromCamera( mouse, camera );
raycaster.ray.direction.applyEuler( e );

但现在我在附近的位置得到交叉点,而不是在对象的确切位置。

【问题讨论】:

    标签: 3d three.js raycasting


    【解决方案1】:

    问题是我没有根据相机旋转调用更新世界坐标

    camera.updateMatrixWorld();
    

    相机更新解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 2021-08-01
      • 2021-09-14
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多