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?
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.
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.
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.
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?
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.
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();
}
}