Recreate Unity Camera in 3D package?

Hi guys,
Ive run into a bit of a snag. How do you go about recreating your Maya camera view in unity or the unity camera view in a 3D package?

Maya cameras use Focal Length (which makes sense, it simulates a camera) but unity cameras use Field of View. In real life, the field of view would be different depending on the focal length and the size of the sensor in your camera. Since Unity cameras dont go into that much detail, how do i recreate the camera view?

Any suggestions would be greatly appreciated.

The formula to get the field of view from the focal length is:-

var fov: float = Mathf.Rad2Deg * 2.0 * Math.Atan(viewHeight / (2.0 * focalLength));

…and in reverse:-

var focalLength: float = viewHeight / (2.0 * Mathf.Tan(0.5 * fov * Mathf.Deg2Rad);

Something about my implementation of your code isn’t working out. I’m new to Maya, so I’m not sure if I have an improper camera setting. When my camera in Unity has a field of view set to 55, the camera in Maya has a correct composition when the focal length is roughly 19.46. Unfortunately, the focal length equation above returns a value of 691.5535 when the field of view is 55. Below is the code I’m using. It’s in c# but it should match up.

float fov = 55;
float focalLength = viewHeight / (2.0f * Mathf.Tan(0.5f * fov * Mathf.Deg2Rad));
Debug.Log( focalLength );    // 691.5535

If you have any advice it would be very much appreciated.

How are you determining the view height? Is it possible you are using pixels or some other value instead of world units for the height?

//focalLengthVal = “focalLength” from maya cam
//verticalApertureVal = “verticalFilmAperture” from maya cam
//25.4 = inches to millimetres
float fov = 2f * Mathf.Rad2Deg * Mathf.Atan( (verticalApertureVal * 25.4f / 2f) / float.Parse(focalLengthVal) );

this works from maya vals to unity vals.

I recently had this problem as well and managed to solve it without any scripts.

I found that dividing the Unity FOV by .596, got me roughly the correct FOV in maya.

To get the right position and orientaion of the camera, I created a cube in unity and parented it to the camera. I zeroed out its transforms so it snapped to the location and orientation of the camera. I then exported the cube (and my scene geo) to an obj. Once in maya, I point constrained a camera to the cube to put it in the right spot. I then created a locator, point constrained that to the cube, broke the connection on the translation values, and moved it out along the cube’s normals. Then finally I used the locator as an aim constraint for the camera. I believe I had to set the aim vector as 0,0,-1.

I’m sure there are other ways to use the cube’s normals to get the desired result but this is what occurred to me.

Hello Guys,

Do you guys know how to calculate the View Height? I am trying to implement that equation but I failing to figure the right way to find the view height.

Appreciate your help.

Im not sure which height u might be needing but take a look here see if it helps:

What is this viewHeight? The height of a frustum plane at a given distance? near plane height? far plane height? height of the camera over ground (makes no sense, I know) - can you please be a little more descriptive in your answer, andeeeee?

Hi Guys,

I’m wondering about this too now.

According to this answer (Converting FOV to Focal Length? - Questions & Answers - Unity Discussions) there is no fitting mathematical calculation to handle focal length in relation to the magnification of the camera, as that’s handled purely through the camera’s field of view.

However according to andeee, the focal length can be found by:

var focalLength: float = viewHeight / (2.0 * Mathf.Tan(0.5 * fov * Mathf.Deg2Rad);

Using my own code (below), this calculates as:

Frustum Height: 0.3464102
Focal Length: 0.3855412

using UnityEngine;
using System.Collections;

public class FocalLength : MonoBehaviour {

    Camera cam;
    public float fFocalLength;
    public float frustumHeight;

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

        frustumHeight = 2.0f * cam.nearClipPlane * Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
        fFocalLength = frustumHeight / (2.0f * Mathf.Tan(0.5f * cam.fieldOfView * Mathf.Rad2Deg));
    }
}

The issue being that this CAN’T be right (from my flawed maths), as by the nearClipPlane increasing, so would the focal length, however the magnification stays the same. So the viewHeight can’t be calculated by world space in the frustum. It also won’t be calculated by camera.pixelHeight, as that’s dependent on the actual screen size and pixel per inch. However there must be some abstract number which goes into the camera’s projectionMatrix for it to calculate perspective distance.

Does anyone know anything more about this?

not sure what you are trying to do, but this script I wrote might be useful.
To test, just create a new quad, and drag this script onto it, then drag your target camera to the targetCam, then hit play:

using UnityEngine;

public class WrapQuadToCameraFrustrum : MonoBehaviour
{
    public Camera targetCam;
  
    void Start()
    {
        updateQuadToCameraBackPlane();
    }

    //modifies the mesh to fit the far plane of the view frustum
    void updateQuadToCameraBackPlane()
    {
        float distanceFromFrustrum = targetCam.farClipPlane; // targetCam.nearClipPlane;

        transform.position = new Vector3(0, 0, distanceFromFrustrum);
        transform.localScale = Vector3.one;

        Vector3[] verts = GetComponent<MeshFilter>().mesh.vertices;

        float frustumHeight = distanceFromFrustrum * Mathf.Tan(targetCam.fieldOfView * 0.5f * Mathf.Deg2Rad);
        float frustumWidth = frustumHeight * targetCam.aspect;
     
        verts [0] = new Vector3(-frustumWidth, -frustumHeight, 0);
        verts [1] = new Vector3(frustumWidth, frustumHeight, 0);
        verts [2] = new Vector3(frustumWidth, -frustumHeight, 0);
        verts [3] = new Vector3(-frustumWidth, frustumHeight, 0);

        GetComponent<MeshFilter>().mesh.vertices = verts;
        GetComponent<MeshFilter>().mesh.RecalculateBounds();
    }
}
1 Like