Matching Orthographic Camera Size to Perspective Camera

Hi all, I’ve been trying to sort this one out for a bit, and I can’t seem to find an answer via search.

I’m making a 3d Game with Unity URP. I have a base perspective camera, and an overlay orthographic camera for the UI (the camera needs to be orthographic as the UI will be a mix of 2d and orthographic 3d elements) stacked on top.

I’d like to change the orthographic camera’s size to match the perspective camera, so that the elements stay “in place” regardless of resizing the window, resolution, or changing the perspective camera’s FOV.

Here’s what I’ve tried to do since the orthographic size is in “Unity Units”:

public class OrthoCamAdjust : MonoBehaviour
{
    private Camera cam;
    public Camera PerspectiveCam;

    void Start()
    {
        cam = gameObject.GetComponent<Camera>();
    }

    void Update()
    {
        Vector3 middle = PerspectiveCam.ScreenToWorldPoint(new Vector3(0, 0, 0,3f));
        Vector3 top = PerspectiveCam.ScreenToWorldPoint(new Vector3(0, PerspectiveCam.pixelHeight, 0.3f));

        middle = PerspectiveCam.transform.InverseTransformPoint(middle);
        top = PerspectiveCam.transform.InverseTransformPoint(top);

        Vector3 result = top - middle;
      
        cam.orthographicSize = Mathf.Abs(result.y);
    }
}

I’ve also tried using the perspective camera’s FOV to figure out a solution, but I’m just not that great at math. Any pointers are much appreciated.

By mathematical necessity this will only match at ONE planar distance from the camera.

If that doesn’t immediately make sense, get out a piece of paper and draw your perspective and ortho frustums and see for yourself.

Choose that distance and the answers you seek are found in trig functions such as Mathf.Tan or Mathf.Atan

Remember trig functions accept values in radians, NOT degrees. Use Mathf.Rad2Deg/Deg2Rad to convert.

Great call out, I should have mentioned it in the OP, already have a set distance picked, same as my 2d UI elements (let’s say it’s 4).

I’ll check out Mathf.Tan and Mathf.Atan, though my trigonometry is as rusty as the titanic, so if you have a formula in mind you’d be saving me a considerable number of math-infused tears.

I’m now realizing I might be explaining my problem incorrectly. Maybe what I’m looking for is the other way around. Here are some pictures to illustrate:

Looking good here.

I’ve changed the horizontal scale of the viewport, and now it’s bad.

Is the obj in red circle drawn using orthographic camera same as other white UI lines? In that case it seems like instead of matching orthographic camera size to perspective camera what you actually need is positioning object within orthographic camera relative screen edges. The various XToY functions in camera API can help implementing that Unity - Scripting API: Camera .

1 Like

Ah thank you, that is probably more what I’m looking for. The white lines are on a canvas, and they don’t scale or move. What is XToY?

XToY here would mean the “‘ScreenToWorldPoint’ ‘ViewPortToScreenPoint’, etc”-functions.
Also, remember that ScreenSpace and ViewPortSpace start at the bottom-left point of the screen, so (0, 0) would be the bottom-left corner, and not the middle of the screen.
Thus, what you’re currently calling ‘middle’ in your code is actually the bottom-left corner.

From Camera.ViewportToScreenPoint:

1 Like