本文介绍了射线与平面的交点坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有射线和飞机.射线与平面相交,我想知道在哪里.我如何获得这个 interset 的世界坐标?
I have got a ray and a plane. The ray is intersecting the plane and I would like to know where. How do I get the world-coordinates of this intersetion?
(在我的特殊情况下,我将屏幕坐标取消投影到相机并创建光线.平面是场景的地面)
(My particular case I unproject screen-coordinates to the camera and create the ray. The plane is the ground of the scene)
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()
);
推荐答案
这是通常的模式.您可能需要对其进行调整以适应您的特定情况.
This is the usual pattern. You may have to adapt it to fit your particular situation.
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
是一个数组.例如,
Here, objects
is an array. For example,
var objects = [];
objects.push( plane_mesh );
更新了three.js r.84
Updated for three.js r.84
这篇关于射线与平面的交点坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!