Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera Matching; Blender -> Unity

Discussion in 'Editor & General Support' started by mountainstorm, Aug 21, 2011.

  1. mountainstorm

    mountainstorm

    Joined:
    Aug 21, 2011
    Posts:
    2
    So I've spent the morning trying to match the camera position from Blender in Unity; so that 3D objects in Unity can be overlain on a 2D image rendered in blender.

    Heres what I've got so far ... if its wrong please correct me; if not its here to help other.

    The first problem I had was that Unity does't appear to want to import cameras from blender; so I have to manually transform between coordinate systems.

    Translation
    Xu = -Xb
    Yu = Zb
    Zu = -Yb

    Rotation
    Xu = 90 - Xb
    Yu = 180 - Zb
    Zu = 360 - Yb

    Then the hard part - the field of view. Now we hit proper issues; blender uses both mm and degrees - both of which seem somewhat inacurate (see here: http://www.blender.org/forum/viewtopic.php?t=12108 and http://www.metrocast.net/~chipartist/BlensesSite/)

    In the end I've come up with the following formula to convert from the blender camera FoV (in mm) to unity FoV (in ??):
    FoV (unity) = 2 atan(16 / (AspectRatio * FocalLength (blender in mm)))

    Thus with an aspect ratio of 1.7777777777 and a blender camera of 35mm, I end up with a unity camera of 28.842

    Thoughts

    Rich
     
    vexe likes this.
  2. dejayc_legacy

    dejayc_legacy

    Joined:
    Oct 24, 2011
    Posts:
    17
    Following this thread, thanks for the research!
     
  3. cwisbg

    cwisbg

    Joined:
    Jul 27, 2011
    Posts:
    88
    ran into this problem when bringing cams into unity from maya. We found that you can export a cube and line the corners of the box to the feild of view ( just look though cam and drag the corner vertecies into the corners of the camera. then export fbx bring into unity and change scale( from maya using meter as unitys scale into unity is 1, default is .01). now for maya to unity we had to put a negitive on scale X and Z ( after grouping it to an enpty game obj at world center) this part is for world placment, not sure if that matters on your end but doing this will get you obj in the same world location as you blender scene (assuming you run in X,Y,Z with Y world up)
    after getting the cube into the correct position place a unity created cam in that obj (as a child) and zero out cameras atributes, translate and rotate, keep scale at 1. then unparent the cam, if you see a shift ( as i did) flip the scale on the rotate X and Z. so if its a negitive make positive and vise versa. as for field of view line that up to the corner of the cubs verticies. you should be good. i know its a little complicated so feel free to ask me to explain it better.
     
  4. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    I wish I could understand the Atan formula... can someone explain here just in numbers how to calculate (clearly) how to recalculate the exact same position and rotation for the camera from Blender to Unity?
     
  5. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    This is what I used, works for me!

    Code (csharp):
    1.  
    2. public static void SetHFov(this Camera cam, float hFov) // hFov: Horizontal FOV from Blender
    3. {
    4.     var vFov = Mathf.Atan(Mathf.Tan(Mathf.Deg2Rad * hFov / 2) / cam.aspect) * 2;
    5.     vFov *= Mathf.Rad2Deg;
    6.     cam.fieldOfView = vFov;
    7. }
    8.  
     
  6. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Thank you! Do happen to also have a script for the objects that can be importet at the exact same position as in Blender?
     
  7. vexe

    vexe

    Joined:
    May 18, 2013
    Posts:
    644
    No not really. Shouldn't be too hard to write a python script that exports the positions to a file and read it from Unity. My blender knowledge is limited so there could be even an easier way. You could try to ask on blenderartists as well.
     
  8. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    So, after trying and trying to match a rendered image with a camera for over a month, I finally have found the solution. All the tutorials I found are not up to date, so to help others out which will come across this thread as speaking of Unity 5.2.1f:

    1. Import gameobjects into Unity with the camera from Blender, be sure to export all the objects WITH the camera into Unity.

    2. Once into Unity drop the grouped object into the scene (it doesn't matter where).

    3. Parent the Main camera to the camera of Blender so the Main camera is now a child of Blenders camera.

    4. Reset the Main camera's position and rotation.

    5. This is the last step and this is where all other tutorials are outdated: turn the Y axis of the Main camera +90 degrees (NOT MINUS like I read somewhere). Ok, now Unity's Main camera has the exact same position as the camera in Blender.

    6. Use the calculation of Mountainstorm (he is the maker of this thread) or if that is too complicated: drag the Fov slider back untill it matches the rendered image. And IT WILL MATCH! My resolution in Blender was 1920 x 1080 and in Unity my aspectratio 19:4 but I think that doesn't matter.
     
    vexe likes this.
  9. dwmitch

    dwmitch

    Joined:
    Mar 21, 2013
    Posts:
    2
    I know the initial post is old, but I figured I'd point this out for people who ran across it.

    The result of that formula is in radians. It may be obvious to most people, but some of us took two tries to go through basic algebra and didn't go any further. You need to multiply the result of that formula by 180/pi.

    So the full formula will look like FoV (unity) = 2 atan(16 / (AspectRatio * FocalLength (blender in mm))) * (180/pi)
     
    GimmeKittens likes this.
  10. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    One year later... I am using Unity 5.4.0


    As of speaking of this version the Camera must be rotated -90 on the Y-axis again (not +90)!
     
  11. Shubham_Zed

    Shubham_Zed

    Joined:
    Jul 12, 2019
    Posts:
    5

    Do you have any idea how to calculate blender FOV using Unity FOV?