5
\$\begingroup\$

In Unity with C#, I want to calculate the minimum distance that my perspective camera has to be from a given GameObject (a procedurally generated mesh), so that the object is fully framed by the camera and leaving the least space possible around it in the screen. In other words, fully framed means that there is no part of the object outside the view area.

To make things easier, my camera does not need to rotate or move - it's fully static, with the exception of one axis to zoom in or out in order to frame the target object. So, in fact, we could summarize my problem as a zoom problem (not using FOV to zoom) where I need to frame a procedurally generated mesh whose size varies considerably.

Would anyone be kind enough to point me in the right direction with code snippets, suggestions, etc?

\$\endgroup\$
  • 1
    \$\begingroup\$ In camera talk, "zoom" means to adjust the zoom lens which is the field of view. "Dolly" means to glide forward and back. Or just say "move along some axis". When you know the field of view angle, which may be different for horizontal and vertical of your screen, you can solve the view distance for each vertex of your mesh: "what camera position puts it at exactly an edge of the screen". Solve for all mesh vertexes, and choose the furthest-back camera position. Did! \$\endgroup\$ – david van brink Sep 17 '15 at 19:09
  • 1
    \$\begingroup\$ @davidvanbrink Make that an answer! \$\endgroup\$ – Anko Sep 17 '15 at 22:25
1
\$\begingroup\$

(Side note: In camera talk, "zoom" means to adjust the zoom lens which is the field of view. "Dolly" means to glide forward and back. Or just say "move along some axis".)

Here's a general approach, without the math:

When you know the field of view angle, which may be different for horizontal and vertical of your screen, you can solve the view distance for each vertex of your mesh: "what camera position puts it at exactly an edge of the screen". Solve for each of the mesh vertexes, and choose the furthest-back camera position. Did!

\$\endgroup\$
  • 1
    \$\begingroup\$ Thanks for clarifying the diference between zoom and " dolly" . That was helpful! Regarding the main question, however, I am afraid it was a bit too vague. Yes, I do know the FOV angle: at this point, my camera is positioned exactly upside down (i.e. perpendicular to the floor). But I don't get what should I do with such an info. What doest it mean to solve for each mesh vertice? There are infinite solutions for the triangle made of one of the screen edges and the height of the frustrum... \$\endgroup\$ – Andy Astro Sep 19 '15 at 14:26
  • \$\begingroup\$ @AndyAstro in this case you have only one free variable: the dolly distance along your camera's forward axis. The other camera position components are fixed. Now, for each vertex, there is some value of this dolly variable that puts the vertex on one of the frustum planes (specifically onto the triangle of the plane corresponding to a screen edge - you can reject solutions that lie behind the camera or outside the rendered bounds). By finding this value for each vertex, you can determine the limiting dolly amount. \$\endgroup\$ – DMGregory Sep 26 '15 at 15:39
0
\$\begingroup\$

You could calculate the camera's position using the signed distances between the camera's frustum planes and the mesh's bounding volume. That's the approach I'm using.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.