使用THREE.Frustum计算近/远平面顶点 [英] Calculate near/far plane vertices using THREE.Frustum
问题描述
我需要一些帮助来处理THREE.Frustum对象。
I need some help to deal with THREE.Frustum object.
我的问题:
我需要计算近/远平面顶点;我看过这些教程
I need to calculate near/far plane vertices; I've taken a look at these tutorials
- http://www.lighthouse3d.com/tutorials/view-frustum-culling/view-frustums-shape/
- http:// www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-extracting-the-planes/
- http://www.lighthouse3d.com/tutorials/view-frustum-culling/view-frustums-shape/
- http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-extracting-the-planes/
并且我已经勾勒出这个功能完全实现(我希望如此)所解释的程序(只是为了得到左上/右上顶点,假设相机只能向左和向右看):
and I've sketched this function implementing exactly (I hope so) the procedure explained (just to get top-left/right vertices, assuming the camera can only look left and right):
// Near Plane dimensions
hNear = 2 * Math.tan(camera.fov / 2) * camera.near; // height
wNear = hNear * camera.aspect; // width
// Far Plane dimensions
hFar = 2 * Math.tan(camera.fov / 2) * camera.far; // height
wFar = hFar * camera.aspect; // width
getVertices : function() {
var p = camera.position.clone();
var l = getCurrentTarget(); // see below
var u = new THREE.Vector3(0, 1, 0);
var d = new THREE.Vector3();
d.sub(l, p);
d.normalize();
var r = new THREE.Vector3();
r.cross(u, d);
r.normalize();
// Near Plane center
var dTmp = d.clone();
var nc = new THREE.Vector3();
nc.add(p, dTmp.multiplyScalar(camera.near));
// Near Plane top-right and top-left vertices
var uTmp = u.clone();
var rTmp = r.clone();
var ntr = new THREE.Vector3();
ntr.add(nc, uTmp.multiplyScalar(hNear / 2));
ntr.subSelf(rTmp.multiplyScalar(wNear / 2));
uTmp.copy(u);
rTmp.copy(r);
var ntl = new THREE.Vector3();
ntl.add(nc, uTmp.multiplyScalar(hNear / 2));
ntl.addSelf(rTmp.multiplyScalar(wNear / 2));
// Far Plane center
dTmp.copy(d);
var fc = new THREE.Vector3();
fc.add(p, dTmp.multiplyScalar(camera.far));
// Far Plane top-right and top-left vertices
uTmp.copy(u);
rTmp.copy(r);
var ftr = new THREE.Vector3();
ftr.add(fc, uTmp.multiplyScalar(hFar / 2));
ftr.subSelf(rTmp.multiplyScalar(wFar / 2));
uTmp.copy(u);
rTmp.copy(r);
var ftl = new THREE.Vector3();
ftl.add(fc, uTmp.multiplyScalar(hFar / 2));
ftl.addSelf(rTmp.multiplyScalar(wFar / 2));
getCurrentTarget : function() {
var l = new THREE.Vector3(0, 0, -100);
this.camera.updateMatrixWorld();
this.camera.matrixWorld.multiplyVector3(l);
return l;
}
这似乎有效但......
This seems to work but...
我的问题:
我可以使用THREE.Frustum对象以更优雅(可能更正确)的方式获得相同的结果吗?
Can I obtain the same result in a more elegant (maybe more correct) way, using a THREE.Frustum object?
推荐答案
Three.Frustum
并不能真正帮助你 - 它是一组飞机。好消息是你的解决方案看起来是正确的,但有一种更容易的思考方式。
Three.Frustum
is not really going to help you -- it is a set of planes. The good news is your solution appears correct, but there is an easier way to think about this.
近平面的右上角是相机空间中的一个点这些坐标:
The upper right corner of the near plane is a point in camera space with these coordinates:
var ntr = new THREE.Vector3( wNear / 2, hNear / 2, -camera.near );
使用 wNear
和<$的定义c $ c> hNear ,这是正确的。
现在,确保 camera.matrixWorld
已更新,您将该点转换为世界坐标,如下所示:
Now, making sure that camera.matrixWorld
is updated, you convert that point to world coordinates like so:
camera.updateMatrixWorld();
ntr.applyMatrix4( camera.matrixWorld );
现在,翻转标志以获得其他三个角,然后重复远平面的计算。
Now, flip the signs to get the other three corners, and then repeat the calculation for the far plane.
看,你做对了;你刚刚走了一条更复杂的路线。 : - )
See, you had it right; you just took a more complicated route. :-)
编辑:更新为three.js r.66
updated to three.js r.66
这篇关于使用THREE.Frustum计算近/远平面顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!