Skip to content

Commit c51ec5e

Browse files
authored
Merge pull request #36 from Unity-Technologies/UNI-20480-export-mesh-instances
UNI-20480 add instance export
2 parents 5ad3998 + fea6ca6 commit c51ec5e

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public static ModelExporter Create ()
6464
/// </summary>
6565
Dictionary<string, FbxTexture> TextureMap = new Dictionary<string, FbxTexture> ();
6666

67+
/// <summary>
68+
/// Map the name of a prefab to an FbxMesh (for preserving instances)
69+
/// </summary>
70+
Dictionary<string, FbxMesh> SharedMeshes = new Dictionary<string, FbxMesh>();
71+
6772
/// <summary>
6873
/// Export the mesh's attributes using layer 0.
6974
/// </summary>
@@ -359,10 +364,10 @@ private void AssignLayerElementMaterial(FbxMesh fbxMesh, Mesh mesh, int material
359364
/// Unconditionally export this mesh object to the file.
360365
/// We have decided; this mesh is definitely getting exported.
361366
/// </summary>
362-
public void ExportMesh (MeshInfo meshInfo, FbxNode fbxNode, FbxScene fbxScene, bool weldVertices = true)
367+
public FbxMesh ExportMesh (MeshInfo meshInfo, FbxNode fbxNode, FbxScene fbxScene, bool weldVertices = true)
363368
{
364369
if (!meshInfo.IsValid)
365-
return;
370+
return null;
366371

367372
NumMeshes++;
368373
NumTriangles += meshInfo.Triangles.Length / 3;
@@ -452,6 +457,8 @@ meshInfo.Vertices [v].z*UnitScaleFactor
452457
// set the fbxNode containing the mesh
453458
fbxNode.SetNodeAttribute (fbxMesh);
454459
fbxNode.SetShadingMode (FbxNode.EShadingMode.eWireFrame);
460+
461+
return fbxMesh;
455462
}
456463

457464
// get a fbxNode's global default position.
@@ -505,6 +512,40 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
505512
return;
506513
}
507514

515+
/// <summary>
516+
/// if this game object is a model prefab then export with shared components
517+
/// </summary>
518+
protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbxScene)
519+
{
520+
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(unityGo);
521+
522+
if (unityPrefabType != PrefabType.PrefabInstance) return false;
523+
524+
Object unityPrefabParent = PrefabUtility.GetPrefabParent (unityGo);
525+
526+
if (Verbose)
527+
Debug.Log (string.Format ("exporting instance {0}({1})", unityGo.name, unityPrefabParent.name));
528+
529+
FbxMesh fbxMesh = null;
530+
531+
if (!SharedMeshes.TryGetValue (unityPrefabParent.name, out fbxMesh))
532+
{
533+
bool weldVertices = FbxExporters.EditorTools.ExportSettings.instance.weldVertices;
534+
fbxMesh = ExportMesh (GetMeshInfo (unityGo), fbxNode, fbxScene, weldVertices);
535+
if (fbxMesh != null) {
536+
SharedMeshes [unityPrefabParent.name] = fbxMesh;
537+
}
538+
}
539+
540+
if (fbxMesh == null) return false;
541+
542+
// set the fbxNode containing the mesh
543+
fbxNode.SetNodeAttribute (fbxMesh);
544+
fbxNode.SetShadingMode (FbxNode.EShadingMode.eWireFrame);
545+
546+
return true;
547+
}
548+
508549
/// <summary>
509550
/// Unconditionally export components on this game object
510551
/// </summary>
@@ -533,8 +574,11 @@ protected int ExportComponents (
533574

534575
ExportTransform ( unityGo.transform, fbxNode, exportType);
535576

536-
bool weldVertices = FbxExporters.EditorTools.ExportSettings.instance.weldVertices;
537-
ExportMesh (GetMeshInfo( unityGo ), fbxNode, fbxScene, weldVertices);
577+
// try exporting mesh as an instance, export regularly if we cannot
578+
if (!ExportInstance (unityGo, fbxNode, fbxScene)) {
579+
bool weldVertices = FbxExporters.EditorTools.ExportSettings.instance.weldVertices;
580+
ExportMesh (GetMeshInfo (unityGo), fbxNode, fbxScene, weldVertices);
581+
}
538582

539583
if (Verbose)
540584
Debug.Log (string.Format ("exporting {0}", fbxNode.GetName ()));

0 commit comments

Comments
 (0)