1. 程式人生 > >已知物體Z軸位置計算出物體的全屏長寬

已知物體Z軸位置計算出物體的全屏長寬

假如想要建立一個全屏物體,或者在螢幕邊緣顯示,已知物體在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; };