【发布时间】:2012-07-20 03:41:36
【问题描述】:
在 Three.js 中有几个关于 unprojecting 的优秀堆栈问题 (1, 2),即如何将浏览器中的 (x,y) 鼠标坐标转换为 (x,y,z) 坐标在 Three.js 画布空间中。他们大多遵循这种模式:
var elem = renderer.domElement,
boundingRect = elem.getBoundingClientRect(),
x = (event.clientX - boundingRect.left) * (elem.width / boundingRect.width),
y = (event.clientY - boundingRect.top) * (elem.height / boundingRect.height);
var vector = new THREE.Vector3(
( x / WIDTH ) * 2 - 1,
- ( y / HEIGHT ) * 2 + 1,
0.5
);
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var intersects = ray.intersectObjects( scene.children );
我一直试图做相反的事情——而不是从“屏幕到世界”空间,而是从“世界到屏幕”空间。如果我知道 Three.js 中对象的位置,如何确定它在屏幕上的位置?
似乎没有针对此问题的任何已发布解决方案。 Another question about this just showed up on Stack,但作者声称已经解决了一个对我不起作用的功能的问题。他们的解决方案不使用投影射线,我很确定由于 2D 到 3D 使用 unprojectVector(),因此 3D 到 2D 解决方案将需要 projectVector()。
还有this issue在Github上打开。
感谢任何帮助。
【问题讨论】:
-
非常感谢“屏幕到世界”的翻译模式。痛了好几个小时..
标签: javascript 3d three.js