THREE.js,正交相机,找到x和z,其中y = 0

时间:2016-08-11 17:00:51

标签: three.js location mouse orthographic

此代码适用于透视摄像头:

var vector = new THREE.Vector3((mouseX / window.innerWidth) * 2 - 1, -(mouseY / window.innerHeight) * 2 + 1, 0.5);
vector.unproject(camera);
var dir = vector.sub(camera.position).normalize();
var distance = -camera.position.y / dir.y;
var cursor3DPos = camera.position.clone().add(dir.multiplyScalar(distance));

如何使用正交相机获得相同的3D坐标?

1 个答案:

答案 0 :(得分:0)

如果您使用的是正交相机,并且想知道鼠标点击的光线与XZ平面相交的位置,您可以使用如下图案:

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

...

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

raycaster.setFromCamera( mouse, camera );

var origin = raycaster.ray.origin;
var dir = raycaster.ray.direction;
var distance = - origin.y / dir.y;

position.copy( origin ).add( dir.multiplyScalar( distance ) );

position将是鼠标下位于XZ平面内的3D点。

请务必正确初始化OrthographicCamera。请参阅Three.js - Orthographic camera

three.js r.79