7

我有射线和飞机。射线与平面相交,我想知道在哪里。我如何获得这个交叉点的世界坐标?

(我的特殊情况是我将屏幕坐标取消投影到相机并创建射线。平面是场景的地面)

var vector = new THREE.Vector3( mousePosition.x, mousePosition.y, 1 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray(
    camera.position,
    vector.subSelf( camera.position ).normalize()
);
4

2 回答 2

10

这是通常的模式。您可能必须对其进行调整以适应您的特定情况。

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

...

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 );

if ( intersects.length > 0 ) {

    console.log( intersects[ 0 ].point; );

}

这里,objects是一个数组。例如,

var objects = [];

objects.push( plane_mesh );

编辑:更新为three.js r.84

于 2012-10-05T15:20:34.953 回答
1

从 r55 开始,您可以使用此快捷方式。它很相似,但将光线投射简化到您甚至不必担心 unprojectVector() 的程度。

这是 WestLangley 示例的修改版本:

    var vector = new THREE.Vector3( ( 
        event.clientX / window.innerWidth ) * 2 - 1, 
        - ( event.clientY / window.innerHeight ) * 2 + 1, 
        0.5 
    );

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

    var intersects = ray.intersectObjects( objects );

    if ( intersects.length > 0 ) {

        console.log( intersects[ 0 ].point; );

    }

这假设相同的对象数组

var objects = [];

objects.push( plane_mesh );
于 2014-10-03T22:29:21.653 回答