Skip to content

Instantly share code, notes, and snippets.

@wiledal
Created October 21, 2021 15:13
Show Gist options
  • Save wiledal/150b3c7763aca1f5d29b224901afad42 to your computer and use it in GitHub Desktop.
Save wiledal/150b3c7763aca1f5d29b224901afad42 to your computer and use it in GitHub Desktop.
Calculate ThreeJS viewport at depth
import { PerspectiveCamera } from "three";
export const getDepthPositionMult = (depth: number, camera: PerspectiveCamera) => {
const { aspect, fov } = camera;
const vFov = (fov * Math.PI) / 180;
const planeHeightAtDistance =
2 * Math.tan(vFov / 2) * (depth + camera.position.z);
const planeWidthAtDistance = planeHeightAtDistance * aspect;
return {
width: planeWidthAtDistance,
height: planeHeightAtDistance
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment