Skip to content

Commit fea6ca6

Browse files
committed
add support for instance export
1 parent 8815d2c commit fea6ca6

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>
@@ -335,10 +340,10 @@ private void AssignLayerElementMaterial(FbxMesh fbxMesh)
335340
/// Unconditionally export this mesh object to the file.
336341
/// We have decided; this mesh is definitely getting exported.
337342
/// </summary>
338-
public void ExportMesh (MeshInfo meshInfo, FbxNode fbxNode, FbxScene fbxScene, bool weldVertices = true)
343+
public FbxMesh ExportMesh (MeshInfo meshInfo, FbxNode fbxNode, FbxScene fbxScene, bool weldVertices = true)
339344
{
340345
if (!meshInfo.IsValid)
341-
return;
346+
return null;
342347

343348
NumMeshes++;
344349
NumTriangles += meshInfo.Triangles.Length / 3;
@@ -424,6 +429,8 @@ meshInfo.Vertices [v].z*UnitScaleFactor
424429
// set the fbxNode containing the mesh
425430
fbxNode.SetNodeAttribute (fbxMesh);
426431
fbxNode.SetShadingMode (FbxNode.EShadingMode.eWireFrame);
432+
433+
return fbxMesh;
427434
}
428435

429436
// get a fbxNode's global default position.
@@ -477,6 +484,40 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
477484
return;
478485
}
479486

487+
/// <summary>
488+
/// if this game object is a model prefab then export with shared components
489+
/// </summary>
490+
protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbxScene)
491+
{
492+
PrefabType unityPrefabType = PrefabUtility.GetPrefabType(unityGo);
493+
494+
if (unityPrefabType != PrefabType.PrefabInstance) return false;
495+
496+
Object unityPrefabParent = PrefabUtility.GetPrefabParent (unityGo);
497+
498+
if (Verbose)
499+
Debug.Log (string.Format ("exporting instance {0}({1})", unityGo.name, unityPrefabParent.name));
500+
501+
FbxMesh fbxMesh = null;
502+
503+
if (!SharedMeshes.TryGetValue (unityPrefabParent.name, out fbxMesh))
504+
{
505+
bool weldVertices = FbxExporters.EditorTools.ExportSettings.instance.weldVertices;
506+
fbxMesh = ExportMesh (GetMeshInfo (unityGo), fbxNode, fbxScene, weldVertices);
507+
if (fbxMesh != null) {
508+
SharedMeshes [unityPrefabParent.name] = fbxMesh;
509+
}
510+
}
511+
512+
if (fbxMesh == null) return false;
513+
514+
// set the fbxNode containing the mesh
515+
fbxNode.SetNodeAttribute (fbxMesh);
516+
fbxNode.SetShadingMode (FbxNode.EShadingMode.eWireFrame);
517+
518+
return true;
519+
}
520+
480521
/// <summary>
481522
/// Unconditionally export components on this game object
482523
/// </summary>
@@ -505,8 +546,11 @@ protected int ExportComponents (
505546

506547
ExportTransform ( unityGo.transform, fbxNode, exportType);
507548

508-
bool weldVertices = FbxExporters.EditorTools.ExportSettings.instance.weldVertices;
509-
ExportMesh (GetMeshInfo( unityGo ), fbxNode, fbxScene, weldVertices);
549+
// try exporting mesh as an instance, export regularly if we cannot
550+
if (!ExportInstance (unityGo, fbxNode, fbxScene)) {
551+
bool weldVertices = FbxExporters.EditorTools.ExportSettings.instance.weldVertices;
552+
ExportMesh (GetMeshInfo (unityGo), fbxNode, fbxScene, weldVertices);
553+
}
510554

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

0 commit comments

Comments
 (0)