Skip to content

Commit e6b2f9b

Browse files
committed
add unit tests
1 parent b8925f9 commit e6b2f9b

File tree

19 files changed

+1946
-1646
lines changed

19 files changed

+1946
-1646
lines changed

Assets/com.unity.formats.fbx.tests/FbxCameraTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
using NUnit.Framework;
88
using System.Collections.Generic;
99
using System.Collections;
10+
using FbxExporters.CustomExtensions;
1011

1112
namespace FbxExporters.UnitTests
1213
{
1314
public class FbxCameraTest : ExporterTestBase
1415
{
16+
const float EPSILON = 0.00001f;
17+
1518
[TearDown]
1619
public override void Term ()
1720
{
@@ -34,5 +37,84 @@ public void AnimationWithCameraFOVTest()
3437

3538
tester.DoIt();
3639
}
40+
41+
[Test]
42+
public void GameCameraTest()
43+
{
44+
var filename = GetRandomFileNamePath();
45+
46+
var original = FbxAnimationTest.AnimTester.CreateTargetObject("GameCamera", typeof (Camera));
47+
48+
var origCam = original.GetComponent<Camera>();
49+
50+
// Configure Game Camera
51+
origCam.fieldOfView = 59;
52+
53+
// FBX Value range is [0.001, 600000.0] centimeters
54+
// Unity Property Inspector range is [0.01, MAX_FLT] meters
55+
// Unity Importer range is [0.3, MAX_FLT] meters
56+
origCam.nearClipPlane = 30f.Centimeters().ToMeters(); // minumum
57+
origCam.farClipPlane = 6000f;
58+
59+
// Convert it to FBX. The asset file will be deleted automatically
60+
// on termination.
61+
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
62+
filename, original);
63+
64+
// refresh the assetdata base so that we can query for the model
65+
AssetDatabase.Refresh ();
66+
67+
var source = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
68+
var srcCam = source.GetComponent<Camera>();
69+
70+
Assert.That( srcCam.aspect, Is.EqualTo(origCam.aspect));
71+
Assert.That( srcCam.fieldOfView, Is.EqualTo(origCam.fieldOfView));
72+
Assert.That( srcCam.farClipPlane, Is.EqualTo(origCam.farClipPlane));
73+
Assert.That( srcCam.nearClipPlane, Is.EqualTo(origCam.nearClipPlane));
74+
}
75+
76+
#if UNITY_2018_2_OR_NEWER || UNITY_2018_2_OR_LATER
77+
[Test]
78+
public void PhysicalCameraTest()
79+
{
80+
var filename = GetRandomFileNamePath();
81+
82+
var original = FbxAnimationTest.AnimTester.CreateTargetObject("GameCamera", typeof (Camera));
83+
84+
var origCam = original.GetComponent<Camera>();
85+
86+
// Configure FilmBack Super 8mm, 5.79f x 4.01mm
87+
origCam.usePhysicalProperties = true;
88+
origCam.aspect = 4.01f / 5.79f;
89+
origCam.focalLength = 50f.Centimeters().ToMillimeters();
90+
origCam.sensorSize = new Vector2(5.79f, 4.01f);
91+
92+
// Lens Shift ( Film Offset ) as a percentage between 0 and 1
93+
origCam.lensShift = new Vector2(1f, 1f);
94+
95+
origCam.nearClipPlane = 30f.Centimeters().ToMeters();
96+
origCam.farClipPlane = 600000f.Centimeters().ToMeters();
97+
98+
// Convert it to FBX. The asset file will be deleted automatically
99+
// on termination.
100+
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
101+
filename, original);
102+
103+
var source = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
104+
var srcCam = source.GetComponent<Camera>();
105+
106+
Assert.That( srcCam.fieldOfView, Is.EqualTo(origCam.fieldOfView).Within(EPSILON));
107+
Assert.That( srcCam.focalLength, Is.EqualTo(origCam.focalLength));
108+
Assert.That( srcCam.sensorSize.x, Is.EqualTo(origCam.sensorSize.x).Within(EPSILON));
109+
Assert.That( srcCam.sensorSize.y, Is.EqualTo(origCam.sensorSize.y).Within(EPSILON));
110+
Assert.That( srcCam.usePhysicalProperties, Is.EqualTo(origCam.usePhysicalProperties));
111+
Assert.That( srcCam.lensShift.x, Is.EqualTo(origCam.lensShift.x).Within(EPSILON));
112+
Assert.That( srcCam.lensShift.y, Is.EqualTo(origCam.lensShift.y).Within(EPSILON));
113+
Assert.That( srcCam.aspect, Is.EqualTo(origCam.aspect).Within(EPSILON));
114+
Assert.That( srcCam.nearClipPlane, Is.EqualTo(origCam.nearClipPlane));
115+
Assert.That( srcCam.farClipPlane, Is.EqualTo(origCam.farClipPlane));
116+
}
117+
118+
#endif
37119
}
38120
}

Packages/com.unity.formats.fbx/Editor/Integrations.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.formats.fbx/Editor/Integrations/Autodesk.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.formats.fbx/Editor/Integrations/Autodesk/max.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.formats.fbx/Editor/Integrations/Autodesk/max/scripts.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.formats.fbx/Editor/Integrations/Autodesk/maya.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.formats.fbx/Editor/Integrations/Autodesk/maya/scripts.meta

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using UnityEngine;
3+
using Unity.FbxSdk;
4+
using FbxExporters.CustomExtensions;
5+
6+
namespace FbxExporters
7+
{
8+
namespace Visitors
9+
{
10+
public static class CameraVisitor
11+
{
12+
/// <summary>
13+
/// Visit Object and configure FbxCamera
14+
/// </summary>
15+
public static FbxNodeAttribute VisitNodeAttribute (UnityEngine.Object unityGo, FbxNodeAttribute fbxNodeAttr)
16+
{
17+
#if UNITY_2018_2_OR_NEWER
18+
Camera unityCamera = unityGo as Camera;
19+
20+
return unityCamera.usePhysicalProperties
21+
? ConfigurePhysicalCamera(fbxNodeAttr as FbxCamera, unityCamera)
22+
: ConfigureGameCamera(fbxNodeAttr as FbxCamera, unityCamera);
23+
#else
24+
return ConfigureGameCamera(fbxCamera as FbxCamera, unityGo as Camera);
25+
#endif
26+
}
27+
28+
/// <summary>
29+
/// Configure FbxCameras from GameCamera
30+
/// </summary>
31+
private static FbxCamera ConfigureGameCamera (FbxCamera fbxCamera, Camera unityCamera)
32+
{
33+
// Configure FilmBack settings as a 35mm TV Projection (0.816 x 0.612)
34+
float aspectRatio = unityCamera.aspect;
35+
36+
float apertureHeightInInches = 0.612f;
37+
float apertureWidthInInches = aspectRatio * apertureHeightInInches;
38+
39+
FbxCamera.EProjectionType projectionType =
40+
unityCamera.orthographic ? FbxCamera.EProjectionType.eOrthogonal : FbxCamera.EProjectionType.ePerspective;
41+
42+
fbxCamera.ProjectionType.Set(projectionType);
43+
fbxCamera.FilmAspectRatio.Set(aspectRatio);
44+
fbxCamera.SetApertureWidth (apertureWidthInInches);
45+
fbxCamera.SetApertureHeight (apertureHeightInInches);
46+
fbxCamera.SetApertureMode (FbxCamera.EApertureMode.eVertical);
47+
48+
// Focal Length
49+
double focalLength = fbxCamera.ComputeFocalLength (unityCamera.fieldOfView);
50+
51+
fbxCamera.FocalLength.Set(focalLength);
52+
53+
// Field of View
54+
fbxCamera.FieldOfView.Set (unityCamera.fieldOfView);
55+
56+
// NearPlane
57+
fbxCamera.SetNearPlane (unityCamera.nearClipPlane.Meters().ToCentimeters());
58+
59+
// FarPlane
60+
fbxCamera.SetFarPlane (unityCamera.farClipPlane.Meters().ToCentimeters());
61+
62+
return fbxCamera;
63+
}
64+
65+
#if UNITY_2018_2_OR_NEWER || UNITY_2018_2_OR_LATER
66+
/// <summary>
67+
/// Configure FbxCameras from a Physical Camera
68+
/// </summary>
69+
private static FbxCamera ConfigurePhysicalCamera (FbxCamera fbxCamera, Camera unityCamera)
70+
{
71+
Debug.Assert(unityCamera.usePhysicalProperties);
72+
73+
// Configure FilmBack settings
74+
float apertureHeightInInches = unityCamera.sensorSize.y.Millimeters().ToInches();
75+
float apertureWidthInInches = unityCamera.sensorSize.x.Millimeters().ToInches();
76+
float aspectRatio = apertureWidthInInches / apertureHeightInInches;
77+
78+
FbxCamera.EProjectionType projectionType = unityCamera.orthographic
79+
? FbxCamera.EProjectionType.eOrthogonal
80+
: FbxCamera.EProjectionType.ePerspective;
81+
82+
// NOTE: it is possible to match some of the sensor sizes to the
83+
// predefined EApertureFormats : e16mmTheatrical, eSuper16mm,
84+
// e35mmFullAperture, eIMAX. However the round in the sizes is not
85+
// consistent between Unity and FBX so we choose
86+
// to leave the values as a eCustomAperture setting.
87+
88+
fbxCamera.ProjectionType.Set(projectionType);
89+
fbxCamera.FilmAspectRatio.Set(aspectRatio);
90+
fbxCamera.SetApertureWidth (apertureWidthInInches);
91+
fbxCamera.SetApertureHeight (apertureHeightInInches);
92+
93+
// Fit the resolution gate horizontally within the film gate.
94+
fbxCamera.GateFit.Set(FbxCamera.EGateFit.eFitHorizontal);
95+
96+
// Lens Shift ( Film Offset ) as a percentage 0..1
97+
// FBX FilmOffset is in inches
98+
fbxCamera.FilmOffsetX.Set(apertureWidthInInches * Mathf.Clamp(unityCamera.lensShift.x, 0f, 1f));
99+
fbxCamera.FilmOffsetY.Set(apertureHeightInInches * Mathf.Clamp(unityCamera.lensShift.y, 0f, 1f));
100+
101+
// Focal Length
102+
fbxCamera.SetApertureMode (FbxCamera.EApertureMode.eFocalLength);
103+
104+
double focalLength = (double)unityCamera.focalLength;
105+
fbxCamera.FocalLength.Set(focalLength); /* in millimeters */
106+
107+
// NearPlane
108+
fbxCamera.SetNearPlane ((double)unityCamera.nearClipPlane.Meters().ToCentimeters());
109+
110+
// FarPlane
111+
fbxCamera.SetFarPlane ((float)unityCamera.farClipPlane.Meters().ToCentimeters());
112+
113+
return fbxCamera;
114+
}
115+
#endif
116+
}
117+
}
118+
}
119+

Packages/com.unity.formats.fbx/Editor/Scripts/CameraVisitor.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)