Forked from ayamflow/gist:a99fd49b773a53bc757df41f77fb369c
Created
April 7, 2018 17:11
-
-
Save Samsy/6408928a88eee2ff271d96e1a2be17b4 to your computer and use it in GitHub Desktop.
Visible width/height with threejs perspective camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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