gpt4 book ai didi

javascript - 使用 THREE.Frustum 计算近/远平面顶点

转载 作者:数据小太阳 更新时间:2023-10-29 04:40:56 28 4
gpt4 key购买 nike

我需要一些帮助来处理 THREE.Frustum 对象。

我的问题:

我需要计算近/远平面顶点;我看过这些教程

  1. http://www.lighthouse3d.com/tutorials/view-frustum-culling/view-frustums-shape/
  2. http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-extracting-the-planes/

我已经勾画出这个函数完全实现(我希望如此)所解释的过程(只是为了获得左上角/右上角的顶点,假设相机只能向左和向右看):

        // 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;
}

这似乎可行,但是......

我的问题:

我能否使用 THREE.Frustum 对象以更优雅(也许更正确)的方式获得相同的结果?

最佳答案

Three.Frustum 并不能真正帮助您——它是一组平面。好消息是您的解决方案看起来是正确的,但有一种更简单的方法来考虑这个问题。

近平面的右上角是相机空间中具有以下坐标的点:

var ntr = new THREE.Vector3( wNear / 2, hNear / 2, -camera.near );

使用您对 wNearhNear 的定义,它们是正确的。

现在,确保 camera.matrixWorld 已更新,您将该点转换为世界坐标,如下所示:

camera.updateMatrixWorld();
ntr.applyMatrix4( camera.matrixWorld );

现在,翻转符号得到其他三个 Angular ,然后对远平面重复计算。

看,你做对了;你只是走了一条更复杂的路线。 :-)

编辑:更新到 three.js r.66

关于javascript - 使用 THREE.Frustum 计算近/远平面顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12018710/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com