@@ -54,6 +54,21 @@ public class ModelExporter : System.IDisposable
54
54
55
55
public const string PACKAGE_UI_NAME = "FBX Exporter" ;
56
56
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
+
57
72
/// <summary>
58
73
/// Create instance of exporter.
59
74
/// </summary>
@@ -747,6 +762,105 @@ protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbx
747
762
return true ;
748
763
}
749
764
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
+
750
864
/// <summary>
751
865
/// Ensures that the inputted name is unique.
752
866
/// If a duplicate name is found, then it is incremented.
@@ -1059,6 +1173,10 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
1059
1173
}
1060
1174
}
1061
1175
}
1176
+
1177
+ // Set the scene's default camera.
1178
+ SetDefaultCamera ( fbxScene ) ;
1179
+
1062
1180
// Export the scene to the file.
1063
1181
status = fbxExporter . Export ( fbxScene ) ;
1064
1182
0 commit comments