2014-04-08 76 views
2

我正在嘗試執行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)); 

回答

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。