Calcutating the field of view for a render texture's camera

Hi Guys,

Here is a list of everything:

  • main camera field of view is 60.
  • screen res is 1024 x 768.
  • render texture displayed in the middle of the screen on a 128 x 96 texture.
  • Render texture Camera.
  • The render texture camera and main camera are always looking at my ball in the
    center of the screen and are both in the same position.

I am trying to build a “portal”. I first do a raycast from my ball to my camera. This allows me to determine if something is obstructing my view. If something is in the way, I draw the render texture. My render texture’s camera only culls the ball and ground so the obstructing object doesn’t get drawn in my render texture.

I need help with calculating the field of view for my render texture’s camera so that the render texture is correctly scaled and blends in with the world. I originally thought that since my render texture is 1/8th the size of my screen res I could simply divide my main camera’s field of view by 8 to get my render texture camera’s field of view. Unfortunately, that is not the case. The render texture still seems larger and I am not too sure why.

Any thoughts or ideas would be greatly appreciated.

Essentially, this is about setting the FOV so that it perfectly encloses a rectangle in the scene a certain distance from the camera. You can get the FOV to view a rectangle of a particular height with:-

fov = Mathf.Atan((0.5 * height) / distance) * 2.0 * Mathf.Rad2Deg;

The width of the view will automatically be right if you have the camera’s aspect ratio the same as that of the rectangle.

2 Likes

Awesome. That was the formula i was looking for. Thanks a million.

I was just looking for the reverse, how to find the correct camera position given the fov and height of the target. This is for an overhead camera looking down.

Found andeeee’s formula, and working off it I ended up with this:

        float distance;
        float height = bounds.max.z - bounds.min.z;
        float fov = camera.fov;

        distance = 0.5f * height / (Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad));
        camera.transform.position = bounds.center + (Vector3.up * (bounds.extents.y + distance));
        camera.transform.LookAt(bounds.center);

Posting here just to make everything orthogonal.

andeeee, cannon, thank you! :sunglasses: