Skip to content

Commit 0d2fc99

Browse files
committed
export LOD according to selected LOD type
- if using Convert to Model, always export all LODs
1 parent c752d07 commit 0d2fc99

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public static GameObject Convert (
145145

146146
// Export to FBX. It refreshes the database.
147147
{
148-
var fbxActualPath = ModelExporter.ExportObject (fbxFullPath, toConvert);
148+
var fbxActualPath = ModelExporter.ExportObject (fbxFullPath, toConvert, lodExportType: EditorTools.ExportSettings.LODExportType.All);
149149
if (fbxActualPath != fbxFullPath) {
150150
throw new System.Exception ("Failed to convert " + toConvert.name);
151151
}

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using UnityEngine.Playables;
99
using UnityEngine.Timeline;
10+
using FbxExporters.EditorTools;
1011

1112
namespace FbxExporters
1213
{
@@ -1922,7 +1923,9 @@ private string GetUniqueName(string name)
19221923
protected int ExportTransformHierarchy(
19231924
GameObject unityGo, FbxScene fbxScene, FbxNode fbxNodeParent,
19241925
int exportProgress, int objectCount, Vector3 newCenter,
1925-
TransformExportType exportType = TransformExportType.Local)
1926+
TransformExportType exportType = TransformExportType.Local,
1927+
ExportSettings.LODExportType lodExportType = ExportSettings.LODExportType.All
1928+
)
19261929
{
19271930
int numObjectsExported = exportProgress;
19281931

@@ -1957,9 +1960,43 @@ protected int ExportTransformHierarchy(
19571960

19581961
fbxNodeParent.AddChild (fbxNode);
19591962

1963+
// if this object has an LOD group, then export according to the LOD preference setting
1964+
var lodGroup = unityGo.GetComponent<LODGroup>();
1965+
if (lodGroup && lodExportType != ExportSettings.LODExportType.All) {
1966+
LOD[] lods = lodGroup.GetLODs ();
1967+
1968+
// LODs are ordered from highest to lowest.
1969+
// If exporting lowest LOD, reverse the array
1970+
if (lodExportType == ExportSettings.LODExportType.Lowest) {
1971+
// reverse the array
1972+
LOD[] tempLods = new LOD[lods.Length];
1973+
System.Array.Copy (lods, tempLods, lods.Length);
1974+
System.Array.Reverse (tempLods);
1975+
lods = tempLods;
1976+
}
1977+
1978+
for(int i = 0; i < lods.Length; i++){
1979+
var lod = lods [i];
1980+
bool exportedRenderer = false;
1981+
foreach (var renderer in lod.renderers) {
1982+
// only export if parented under LOD group
1983+
if (renderer.transform.parent == unityGo.transform) {
1984+
numObjectsExported = ExportTransformHierarchy (renderer.gameObject, fbxScene, fbxNode, numObjectsExported, objectCount, newCenter, lodExportType: lodExportType);
1985+
exportedRenderer = true;
1986+
}
1987+
}
1988+
1989+
// if at least one renderer for this LOD was exported, then we succeeded
1990+
// so stop exporting.
1991+
if (exportedRenderer) {
1992+
return numObjectsExported;
1993+
}
1994+
}
1995+
}
1996+
19601997
// now unityGo through our children and recurse
19611998
foreach (Transform childT in unityGo.transform) {
1962-
numObjectsExported = ExportTransformHierarchy (childT.gameObject, fbxScene, fbxNode, numObjectsExported, objectCount, newCenter);
1999+
numObjectsExported = ExportTransformHierarchy (childT.gameObject, fbxScene, fbxNode, numObjectsExported, objectCount, newCenter, lodExportType: lodExportType);
19632000
}
19642001

19652002
return numObjectsExported;
@@ -2665,7 +2702,10 @@ public enum TransformExportType { Local, Global, Reset };
26652702
///
26662703
/// This refreshes the asset database.
26672704
/// </summary>
2668-
public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet, Dictionary<GameObject, AnimationOnlyExportData> animationExportData)
2705+
public int ExportAll (
2706+
IEnumerable<UnityEngine.Object> unityExportSet,
2707+
Dictionary<GameObject, AnimationOnlyExportData> animationExportData,
2708+
ExportSettings.LODExportType lodExportType = ExportSettings.LODExportType.All)
26692709
{
26702710
exportCancelled = false;
26712711

@@ -2782,7 +2822,7 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet, Dictionary
27822822
}
27832823
else {
27842824
exportProgress = this.ExportTransformHierarchy (unityGo, fbxScene, fbxRootNode,
2785-
exportProgress, count, center, exportType);
2825+
exportProgress, count, center, exportType, lodExportType);
27862826
}
27872827
if (exportCancelled || exportProgress < 0) {
27882828
Debug.LogWarning ("Export Cancelled");
@@ -3547,7 +3587,7 @@ private static void OnExport (AnimationExportType exportType = AnimationExportTy
35473587
return;
35483588
}
35493589

3550-
if (ExportObjects (filePath, exportType: exportType) != null) {
3590+
if (ExportObjects (filePath, exportType: exportType, lodExportType: ExportSettings.instance.lodExportType) != null) {
35513591
// refresh the asset database so that the file appears in the
35523592
// asset folder view.
35533593
AssetDatabase.Refresh ();
@@ -3558,7 +3598,11 @@ private static void OnExport (AnimationExportType exportType = AnimationExportTy
35583598
/// Export a list of (Game) objects to FBX file.
35593599
/// Use the SaveFile panel to allow user to enter a file name.
35603600
/// <summary>
3561-
public static string ExportObjects (string filePath, UnityEngine.Object[] objects = null, AnimationExportType exportType = AnimationExportType.all)
3601+
public static string ExportObjects (
3602+
string filePath,
3603+
UnityEngine.Object[] objects = null,
3604+
AnimationExportType exportType = AnimationExportType.all,
3605+
ExportSettings.LODExportType lodExportType = ExportSettings.LODExportType.All)
35623606
{
35633607
LastFilePath = filePath;
35643608

@@ -3599,7 +3643,7 @@ public static string ExportObjects (string filePath, UnityEngine.Object[] object
35993643
break;
36003644
}
36013645

3602-
if (fbxExporter.ExportAll (objects, animationExportData) > 0) {
3646+
if (fbxExporter.ExportAll (objects, animationExportData, lodExportType) > 0) {
36033647
string message = string.Format ("Successfully exported: {0}", filePath);
36043648
UnityEngine.Debug.Log (message);
36053649

@@ -3609,9 +3653,12 @@ public static string ExportObjects (string filePath, UnityEngine.Object[] object
36093653
return null;
36103654
}
36113655

3612-
public static string ExportObject (string filePath, UnityEngine.Object root, AnimationExportType exportType = AnimationExportType.all)
3656+
public static string ExportObject (
3657+
string filePath, UnityEngine.Object root,
3658+
AnimationExportType exportType = AnimationExportType.all,
3659+
ExportSettings.LODExportType lodExportType = ExportSettings.LODExportType.All)
36133660
{
3614-
return ExportObjects(filePath, new Object[] { root }, exportType);
3661+
return ExportObjects(filePath, new Object[] { root }, exportType, lodExportType);
36153662
}
36163663

36173664
private static void EnsureDirectory (string path)

0 commit comments

Comments
 (0)