Calculate the camera z position based on a number of factors

What would be a function to output a number for the camera position’s z value based on a number of factors:

  • fov: (default 50)
  • windowWidth: (window.innerWidth)
  • 3DObject size + position: (PlaneGeometry with width 5, height 5 and position [0,0,0])
  • camera X,Y is 0,0
  • what is the Z value so that the object’s width fits exactly the browser width (don’t care about the height fitting)

so something like:

function getZ(fov, objectWidth, browserWidth) {
}
  1. Would this solve your issue, without coding the math manually?
  2. Deriving from point 1, this is most likely the part you’d need if you’d like to code it by hand.
  3. Deriving from point 2, this and this are the precise parts you’d be looking for.
1 Like

YES! The getDistanceToFitBox did the trick, thank you!