计算相机近距离和远距离平面边界

时间:2014-04-08 10:47:50

标签: javascript three.js

我试图执行Calculate near/far plane vertices using THREE.Frustum中提到的计算。我使用问题和答案https://stackoverflow.com/a/12022005/299037来组合一个完整的工作示例http://jsfiddle.net/cZb66/,将粒子放置在远平面的边界,在这种情况下,近平面非常接近粒子会遮挡视图。出于某种原因,远平面点不在摄像机的范围内(窗口的角落),正如我所期望的那样。

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

var farTopLeft = new THREE.Vector3( wFar / 2, hFar / 2, -camera.far );
var farBottomRight = new THREE.Vector3( -wFar / 2, -hFar / 2, -camera.far );
var farTopRight = new THREE.Vector3( -wFar / 2, hFar / 2, -camera.far );
var farBottomLeft = new THREE.Vector3( wFar / 2, -hFar / 2, -camera.far );

// adjust the vectors to the camera location and direction
camera.updateMatrixWorld();
farTopLeft.applyMatrix4( camera.matrixWorld );
farBottomRight.applyMatrix4( camera.matrixWorld );
farTopRight.applyMatrix4(camera.matrixWorld);
farBottomLeft.applyMatrix4(camera.matrixWorld);

var z = 1;
var farParticles = new THREE.Geometry();
farParticles.vertices.push(new THREE.Vector3(farTopLeft.x, farTopLeft.y, z));
farParticles.vertices.push(new THREE.Vector3(farBottomRight.x, farBottomRight.y, z));
farParticles.vertices.push(new THREE.Vector3(farTopRight.x, farTopRight.y, z));
farParticles.vertices.push(new THREE.Vector3(farBottomLeft.x, farBottomLeft.y, z));
farParticles.vertices.push(new THREE.Vector3(0, 0, 0));

1 个答案:

答案 0 :(得分:4)

你很亲密。 camera.fov是度数。您需要在计算可见高度时将camera.fov转换为弧度。

hNear = 2 * Math.tan( camera.fov * Math.PI / 180 / 2 ) * camera.near;

还有一些其他错误。这是一个工作小提琴:

http://jsfiddle.net/cZb66/3/

three.js r.66

编辑:更新了jsfiddle链接到1x1近粒子的版本。