Skip to content

Commit 7391baa

Browse files
committed
add functions to export camera
1 parent 7108dcd commit 7391baa

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ public class ModelExporter : System.IDisposable
5454

5555
public const string PACKAGE_UI_NAME = "FBX Exporter";
5656

57+
/// <summary>
58+
/// name of the scene's default camera
59+
/// </summary>
60+
private static string DefaultCamera = "";
61+
62+
/// <summary>
63+
/// name prefix for custom properties
64+
/// </summary>
65+
const string NamePrefix = "Unity_";
66+
67+
private static string MakeName (string basename)
68+
{
69+
return NamePrefix + basename;
70+
}
71+
5772
/// <summary>
5873
/// Create instance of exporter.
5974
/// </summary>
@@ -747,6 +762,105 @@ protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbx
747762
return true;
748763
}
749764

765+
/// <summary>
766+
/// Exports camera component
767+
/// </summary>
768+
protected FbxCamera ExportCamera (Camera unityCamera, FbxScene fbxScene, FbxNode fbxNode)
769+
{
770+
FbxCamera fbxCamera = FbxCamera.Create (fbxScene.GetFbxManager(), unityCamera.name);
771+
772+
float aspectRatio = unityCamera.aspect;
773+
774+
// Configure FilmBack settings: 35mm TV Projection (0.816 x 0.612)
775+
float apertureHeightInInches = 0.612f;
776+
float apertureWidthInInches = aspectRatio * apertureHeightInInches;
777+
778+
FbxCamera.EProjectionType projectionType =
779+
unityCamera.orthographic ? FbxCamera.EProjectionType.eOrthogonal : FbxCamera.EProjectionType.ePerspective;
780+
781+
fbxCamera.ProjectionType.Set(projectionType);
782+
fbxCamera.SetAspect (FbxCamera.EAspectRatioMode.eFixedRatio, aspectRatio, 1.0f);
783+
fbxCamera.FilmAspectRatio.Set(aspectRatio);
784+
fbxCamera.SetApertureWidth (apertureWidthInInches);
785+
fbxCamera.SetApertureHeight (apertureHeightInInches);
786+
fbxCamera.SetApertureMode (FbxCamera.EApertureMode.eFocalLength);
787+
788+
// FOV / Focal Length
789+
fbxCamera.FocalLength.Set(fbxCamera.ComputeFocalLength (unityCamera.fieldOfView));
790+
791+
// NearPlane
792+
fbxCamera.SetNearPlane (unityCamera.nearClipPlane);
793+
794+
// FarPlane
795+
fbxCamera.SetFarPlane (unityCamera.farClipPlane);
796+
797+
// Export backgroundColor as a custom property
798+
// NOTE: export on fbxNode so that it will show up in Maya
799+
ExportColorProperty (fbxNode, unityCamera.backgroundColor,
800+
MakeName("backgroundColor"),
801+
"The color with which the screen will be cleared.");
802+
803+
// Export clearFlags as a custom property
804+
// NOTE: export on fbxNode so that it will show up in Maya
805+
ExportIntProperty (fbxNode, (int)unityCamera.clearFlags,
806+
MakeName("clearFlags"),
807+
"How the camera clears the background.");
808+
809+
return fbxCamera;
810+
}
811+
812+
/// <summary>
813+
/// configures default camera for the scene
814+
/// </summary>
815+
protected void SetDefaultCamera (FbxScene fbxScene)
816+
{
817+
if (DefaultCamera == "")
818+
DefaultCamera = Globals.FBXSDK_CAMERA_PERSPECTIVE;
819+
820+
fbxScene.GetGlobalSettings ().SetDefaultCamera (DefaultCamera);
821+
}
822+
823+
/// <summary>
824+
/// Export Component's color property
825+
/// </summary>
826+
FbxProperty ExportColorProperty (FbxObject fbxObject, Color value, string name, string label)
827+
{
828+
// create a custom property for component value
829+
var fbxProperty = FbxProperty.Create (fbxObject, Globals.FbxColor4DT, name, label);
830+
if (!fbxProperty.IsValid ()) {
831+
throw new System.NullReferenceException ();
832+
}
833+
834+
FbxColor fbxColor = new FbxColor(value.r, value.g, value.b, value.a );
835+
836+
fbxProperty.Set (fbxColor);
837+
838+
// Must be marked user-defined or it won't be shown in most DCCs
839+
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eUserDefined, true);
840+
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eAnimatable, true);
841+
842+
return fbxProperty;
843+
}
844+
845+
/// <summary>
846+
/// Export Component's int property
847+
/// </summary>
848+
FbxProperty ExportIntProperty (FbxObject fbxObject, int value, string name, string label)
849+
{
850+
// create a custom property for component value
851+
var fbxProperty = FbxProperty.Create (fbxObject, Globals.FbxIntDT, name, label);
852+
if (!fbxProperty.IsValid ()) {
853+
throw new System.NullReferenceException ();
854+
}
855+
fbxProperty.Set (value);
856+
857+
// Must be marked user-defined or it won't be shown in most DCCs
858+
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eUserDefined, true);
859+
fbxProperty.ModifyFlag (FbxPropertyFlags.EFlags.eAnimatable, true);
860+
861+
return fbxProperty;
862+
}
863+
750864
/// <summary>
751865
/// Ensures that the inputted name is unique.
752866
/// If a duplicate name is found, then it is incremented.
@@ -1059,6 +1173,10 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
10591173
}
10601174
}
10611175
}
1176+
1177+
// Set the scene's default camera.
1178+
SetDefaultCamera (fbxScene);
1179+
10621180
// Export the scene to the file.
10631181
status = fbxExporter.Export (fbxScene);
10641182

0 commit comments

Comments
 (0)