Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Samsy/6408928a88eee2ff271d96e1a2be17b4 to your computer and use it in GitHub Desktop.
Save Samsy/6408928a88eee2ff271d96e1a2be17b4 to your computer and use it in GitHub Desktop.
Visible width/height with threejs perspective camera
// https://discourse.threejs.org/t/functions-to-calculate-the-visible-width-height-at-a-given-z-depth-from-a-perspective-camera/269
function visibleHeightAtDepth(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 );
}
function visibleWidthAtDepth(depth, camera) {
const height = visibleHeightAtDepth( depth, camera );
return height * camera.aspect;
}
export default {
visibleHeightAtDepth,
visibleWidthAtDepth
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment