Skip to content

Commit f819ae4

Browse files
authored
Merge pull request #13 from Unity-Technologies/UNI-20478-maintain-material-connection
UNI-20478 maintain material connection on export and reimport
2 parents 8514ad1 + f7a6b25 commit f819ae4

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ namespace FbxExporters.EditorTools {
99
[CustomEditor(typeof(ExportSettings))]
1010
public class ExportSettingsEditor : UnityEditor.Editor {
1111
public override void OnInspectorGUI() {
12-
ExportSettings temp = (ExportSettings)target;
12+
ExportSettings exportSettings = (ExportSettings)target;
1313

14-
temp.weldVertices = EditorGUILayout.Toggle ("Weld Vertices:", temp.weldVertices);
14+
exportSettings.weldVertices = EditorGUILayout.Toggle ("Weld Vertices:", exportSettings.weldVertices);
15+
exportSettings.embedTextures = EditorGUILayout.Toggle ("Embed Textures:", exportSettings.embedTextures);
1516

1617
if (GUI.changed) {
17-
EditorUtility.SetDirty (temp);
18-
temp.Dirty ();
18+
EditorUtility.SetDirty (exportSettings);
19+
exportSettings.Save ();
1920
}
2021
}
2122
}
@@ -24,6 +25,7 @@ public override void OnInspectorGUI() {
2425
public class ExportSettings : FbxExporters.EditorTools.ScriptableSingleton<ExportSettings>
2526
{
2627
public bool weldVertices = true;
28+
public bool embedTextures = false;
2729

2830
[MenuItem("Edit/Project Settings/Fbx Export", priority = 300)]
2931
static void ShowManager()
@@ -32,7 +34,7 @@ static void ShowManager()
3234
Selection.activeObject = instance;
3335
}
3436

35-
public void Dirty()
37+
public void Save()
3638
{
3739
instance.Save (true);
3840
}

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public FbxDouble3 GetMaterialColor (Material unityMaterial, string unityPropName
254254
/// <summary>
255255
/// Export (and map) a Unity PBS material to FBX classic material
256256
/// </summary>
257-
public FbxSurfaceMaterial ExportMaterial (Material unityMaterial, FbxScene fbxScene)
257+
public FbxSurfaceMaterial ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxMesh fbxMesh)
258258
{
259259
if (Verbose)
260260
Debug.Log (string.Format ("exporting material {0}", unityMaterial.name));
@@ -292,6 +292,26 @@ public FbxSurfaceMaterial ExportMaterial (Material unityMaterial, FbxScene fbxSc
292292
ExportTexture (unityMaterial, "_SpecGlosMap", fbxMaterial, FbxSurfaceMaterial.sSpecular);
293293
}
294294

295+
// Add FbxLayerElementMaterial to layer 0 of the node
296+
FbxLayer fbxLayer = fbxMesh.GetLayer (0 /* default layer */);
297+
if (fbxLayer == null) {
298+
fbxMesh.CreateLayer ();
299+
fbxLayer = fbxMesh.GetLayer (0 /* default layer */);
300+
}
301+
302+
using (var fbxLayerElement = FbxLayerElementMaterial.Create (fbxMesh, "Material")) {
303+
304+
// Using all same means that the entire mesh uses the same material
305+
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eAllSame);
306+
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eIndexToDirect);
307+
308+
FbxLayerElementArray fbxElementArray = fbxLayerElement.GetIndexArray ();
309+
310+
// Map the entire geometry to the FbxNode material at index 0
311+
fbxElementArray.Add (0);
312+
fbxLayer.SetMaterials (fbxLayerElement);
313+
}
314+
295315
MaterialMap.Add (materialName, fbxMaterial);
296316
return fbxMaterial;
297317
}
@@ -350,7 +370,7 @@ meshInfo.Vertices [v].z
350370
}
351371
}
352372

353-
var fbxMaterial = ExportMaterial (meshInfo.Material, fbxScene);
373+
var fbxMaterial = ExportMaterial (meshInfo.Material, fbxScene, fbxMesh);
354374
fbxNode.AddMaterial (fbxMaterial);
355375

356376
/*
@@ -487,13 +507,18 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
487507
fbxManager.SetIOSettings (FbxIOSettings.Create (fbxManager, Globals.IOSROOT));
488508

489509
// Export texture as embedded
490-
fbxManager.GetIOSettings ().SetBoolProp (Globals.EXP_FBX_EMBEDDED, true);
510+
if(EditorTools.ExportSettings.instance.embedTextures){
511+
fbxManager.GetIOSettings ().SetBoolProp (Globals.EXP_FBX_EMBEDDED, true);
512+
}
491513

492514
// Create the exporter
493515
var fbxExporter = FbxExporter.Create (fbxManager, "Exporter");
494516

495517
// Initialize the exporter.
496-
int fileFormat = fbxManager.GetIOPluginRegistry ().FindWriterIDByDescription ("FBX ascii (*.fbx)");
518+
// fileFormat must be binary if we are embedding textures
519+
int fileFormat = EditorTools.ExportSettings.instance.embedTextures? -1 :
520+
fbxManager.GetIOPluginRegistry ().FindWriterIDByDescription ("FBX ascii (*.fbx)");
521+
497522
bool status = fbxExporter.Initialize (LastFilePath, fileFormat, fbxManager.GetIOSettings ());
498523
// Check that initialization of the fbxExporter was successful
499524
if (!status)

0 commit comments

Comments
 (0)