Orthographic camera RECT based off points on screen

Hello everyone. I dont know whether it is too early/late(no sleep) or I am completely clueless. I cannot figure out how to fix my orthographic camera’s rect to match up to points on my main camera’s screen.

I have the screen coordinates of all of the blue points. I want to make my orthographic camera fit inside the smaller box. Anyone point me in the right direction for converting these 4 vector3 points into a rect I can use for a camera?

Explanation:
I have a UISprite in Ngui. From that sprite I am grabbing the 4 corners and converting those points to screen space using WorldToScreenPoint. I would like my orthographic camera to fit perfectly inside those 4 points.

Thanks!

You want to adjust the “Viewport Rect” of your orthographic camera to fit inside that smaller rectangle on your illustration:

The “Viewport Rect” is normalized so that (0, 0) = origin and (1, 1) = extent. To convert screen coordinates into this you can do something like follows:

orthoCamera.rect = new Rect(
    lowerLeftScreenPoint.x / Screen.width,
    lowerLeftScreenPoint.y / Screen.height,
    panelScreenWidth / Screen.width,
    panelScreenHeight / Screen.height
);

I have not tested the above, there is a chance that I have got the vertical axis inverted. If you find that is the case, you should be able to correct it using (1 - theValue).

I hope that this helps :slight_smile:

Thank you for your help, I was able to figure it out.

            //** NGUI SPECIFIC COMMANDS **
            Vector3[] corners = cameraContainer.worldCorners; //cameraContainer is my UISprite. collect the 4 corners of the UISprite: bottom left, top left, top right, bottom right
            Vector3 lowerLeftScreenPoint = UICamera.mainCamera.WorldToScreenPoint(corners[0]); //convert lower left point to screenspace
            Vector3 lowerRightScreenPoint = UICamera.mainCamera.WorldToScreenPoint(corners[3]);//convert lower right point to screenspace
            Vector3 topLeftScreenPoint = UICamera.mainCamera.WorldToScreenPoint(corners[1]); // convert top left point to screenspace
            //***************************//
            //
            //If you are able to get the Vector3 points you would like to use (remember to convert to screenpoint if you havent already)
            //you can use the following below.

            mapCam.rect = new Rect( // The orthographic camera I am using
                lowerLeftScreenPoint.x / Screen.width,
                lowerLeftScreenPoint.y / Screen.height,
                (lowerRightScreenPoint.x - lowerLeftScreenPoint.x)  / Screen.width,
                (topLeftScreenPoint.y - lowerLeftScreenPoint.y) / Screen.height
                );

As pointed out here: [Solved]Orthographic camera RECT based off points on screen - Questions & Answers - Unity Discussions

a much cleaner way to do this is to directly convert world points using WorldToViewportPoint() which will calculate into the coordinate system used by the camera.

 Vector3 llPoint = UICamera.mainCamera.WorldToViewportPoint(corners[0]);
Vector3 urPoint = UICamera.mainCamera.WorldToViewportPoint(corners[2]);
mapCam.rect = new Rect(llPoint.x, llPoint.y, urPoint.x - llPoint.x, urPoint.y - llPoint.y);