假如想要创建一个全屏物体,或者在屏幕边缘显示,已知物体在Z轴的位置,即可用下面函数计算得出:
const visibleHeightAtZDepth = ( depth, camera ) => {
// compensate for cameras not positioned at z=0
const cameraOffset = camera.position.z;
if ( depth < cameraOffset ) depth -= cameraOffset;
else depth += cameraOffset;
// vertical fov in radians
const vFOV = camera.fov * Math.PI / 180;
// Math.abs to ensure the result is always positive
return 2 * Math.tan( vFOV / 2 ) * Math.abs( depth );
};
const visibleWidthAtZDepth = ( depth, camera ) => {
const height = visibleHeightAtZDepth( depth, camera );
return height * camera.aspect;
};