缩放后鼠标坐标无关紧要,是不是错误?

时间:2014-09-25 22:28:01

标签: three.js

我有关于获取鼠标坐标的问题,它在缩放后表现得无关紧要。

我有一个我的代码的JS小提琴链接,它将显示我面临的问题,是three.js中的错误或我绘制线条的方式是错误的,请提供您的反馈。

http://jsfiddle.net/ebeit303/ceej4jxq/1/

var elem = self.renderer.domElement,
                boundingRect = elem.getBoundingClientRect(),
                x = (e.clientX - boundingRect.left) * (elem.width / boundingRect.width),
                y = (e.clientY - boundingRect.top) * (elem.height / boundingRect.height);
var vector = new THREE.Vector3((x / $("container").width()) * 2 - 1, -(y / $("container").height()) * 2 + 1, 0.5);
var pos = projector.unprojectVector(vector, camera);

var dir = pos.clone().sub(camera.position).normalize().multiplyScalar(-1);
var distance = camera.position.z / dir.z;     
var pos1 = camera.position.clone().sub(dir.multiplyScalar(distance));

提前致谢..

1 个答案:

答案 0 :(得分:2)

您小提琴中的相机near平面为0.0001,相机far平面为10,000,000,000。

因此,当您致电unprojectVector()时,您的代码中存在数字问题。

该问题与此处描述的深度缓冲精度问题密切相关:http://www.opengl.org/wiki/Depth_Buffer_Precision

将近平面设置为1或更大,将远平面设置为您可以使用的最小值,例如10000。

three.js r.68

相关问题