@@ -165,26 +165,38 @@ internal enum FbxNodeRelationType
165
165
Dictionary < FbxNode , Dictionary < FbxConstraint , System . Type > > MapConstrainedObjectToConstraints = new Dictionary < FbxNode , Dictionary < FbxConstraint , System . Type > > ( ) ;
166
166
167
167
/// <summary>
168
- /// Map Unity material name to FBX material object
168
+ /// Map Unity material ID to FBX material object
169
169
/// </summary>
170
- Dictionary < string , FbxSurfaceMaterial > MaterialMap = new Dictionary < string , FbxSurfaceMaterial > ( ) ;
170
+ Dictionary < int , FbxSurfaceMaterial > MaterialMap = new Dictionary < int , FbxSurfaceMaterial > ( ) ;
171
171
172
172
/// <summary>
173
173
/// Map texture filename name to FBX texture object
174
174
/// </summary>
175
175
Dictionary < string , FbxTexture > TextureMap = new Dictionary < string , FbxTexture > ( ) ;
176
176
177
177
/// <summary>
178
- /// Map the name of a prefab to an FbxMesh (for preserving instances)
178
+ /// Map the ID of a prefab to an FbxMesh (for preserving instances)
179
179
/// </summary>
180
- Dictionary < string , FbxMesh > SharedMeshes = new Dictionary < string , FbxMesh > ( ) ;
180
+ Dictionary < int , FbxMesh > SharedMeshes = new Dictionary < int , FbxMesh > ( ) ;
181
181
182
182
/// <summary>
183
183
/// Map for the Name of an Object to number of objects with this name.
184
184
/// Used for enforcing unique names on export.
185
185
/// </summary>
186
186
Dictionary < string , int > NameToIndexMap = new Dictionary < string , int > ( ) ;
187
187
188
+ /// <summary>
189
+ /// Map for the Material Name to number of materials with this name.
190
+ /// Used for enforcing unique names on export.
191
+ /// </summary>
192
+ Dictionary < string , int > MaterialNameToIndexMap = new Dictionary < string , int > ( ) ;
193
+
194
+ /// <summary>
195
+ /// Map for the Texture Name to number of textures with this name.
196
+ /// Used for enforcing unique names on export.
197
+ /// </summary>
198
+ Dictionary < string , int > TextureNameToIndexMap = new Dictionary < string , int > ( ) ;
199
+
188
200
/// <summary>
189
201
/// Format for creating unique names
190
202
/// </summary>
@@ -620,7 +632,8 @@ internal bool ExportTexture (Material unityMaterial, string unityPropName,
620
632
621
633
// Find or create an fbx texture and link it up to the fbx material.
622
634
if ( ! TextureMap . ContainsKey ( textureSourceFullPath ) ) {
623
- var fbxTexture = FbxFileTexture . Create ( fbxMaterial , fbxPropName + "_Texture" ) ;
635
+ var textureName = GetUniqueTextureName ( fbxPropName + "_Texture" ) ;
636
+ var fbxTexture = FbxFileTexture . Create ( fbxMaterial , textureName ) ;
624
637
fbxTexture . SetFileName ( textureSourceFullPath ) ;
625
638
fbxTexture . SetTextureUse ( FbxTexture . ETextureUse . eStandard ) ;
626
639
fbxTexture . SetMappingType ( FbxTexture . EMappingType . eUV ) ;
@@ -655,15 +668,19 @@ internal bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode
655
668
unityMaterial = DefaultMaterial ;
656
669
}
657
670
658
- var unityName = unityMaterial . name ;
659
- if ( MaterialMap . ContainsKey ( unityName ) ) {
660
- fbxNode . AddMaterial ( MaterialMap [ unityName ] ) ;
671
+ var unityID = unityMaterial . GetInstanceID ( ) ;
672
+ FbxSurfaceMaterial mappedMaterial ;
673
+ if ( MaterialMap . TryGetValue ( unityID , out mappedMaterial ) ) {
674
+ fbxNode . AddMaterial ( mappedMaterial ) ;
661
675
return true ;
662
676
}
663
677
678
+ var unityName = unityMaterial . name ;
664
679
var fbxName = ExportOptions . UseMayaCompatibleNames
665
680
? ConvertToMayaCompatibleName ( unityName ) : unityName ;
666
681
682
+ fbxName = GetUniqueMaterialName ( fbxName ) ;
683
+
667
684
if ( Verbose ) {
668
685
if ( unityName != fbxName ) {
669
686
Debug . Log ( string . Format ( "exporting material {0} as {1}" , unityName , fbxName ) ) ;
@@ -700,7 +717,7 @@ internal bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode
700
717
ExportTexture ( unityMaterial , "_SpecGlosMap" , fbxMaterial , FbxSurfaceMaterial . sSpecular ) ;
701
718
}
702
719
703
- MaterialMap . Add ( unityName , fbxMaterial ) ;
720
+ MaterialMap . Add ( unityID , fbxMaterial ) ;
704
721
fbxNode . AddMaterial ( fbxMaterial ) ;
705
722
return true ;
706
723
}
@@ -1219,7 +1236,7 @@ internal bool ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbx
1219
1236
/// if this game object is a model prefab then export with shared components
1220
1237
/// </summary>
1221
1238
[ SecurityPermission ( SecurityAction . LinkDemand ) ]
1222
- private bool ExportInstance ( GameObject unityGo , FbxNode fbxNode )
1239
+ private bool ExportInstance ( GameObject unityGo , FbxScene fbxScene , FbxNode fbxNode )
1223
1240
{
1224
1241
if ( ! unityGo || fbxNode == null )
1225
1242
{
@@ -1242,10 +1259,10 @@ private bool ExportInstance(GameObject unityGo, FbxNode fbxNode)
1242
1259
1243
1260
FbxMesh fbxMesh = null ;
1244
1261
1245
- if ( ! SharedMeshes . TryGetValue ( unityPrefabParent . name , out fbxMesh ) )
1262
+ if ( ! SharedMeshes . TryGetValue ( unityPrefabParent . GetInstanceID ( ) , out fbxMesh ) )
1246
1263
{
1247
1264
if ( ExportMesh ( unityGo , fbxNode ) && fbxNode . GetMesh ( ) != null ) {
1248
- SharedMeshes [ unityPrefabParent . name ] = fbxNode . GetMesh ( ) ;
1265
+ SharedMeshes [ unityPrefabParent . GetInstanceID ( ) ] = fbxNode . GetMesh ( ) ;
1249
1266
return true ;
1250
1267
}
1251
1268
return false ;
@@ -1259,10 +1276,15 @@ private bool ExportInstance(GameObject unityGo, FbxNode fbxNode)
1259
1276
if ( materials != null )
1260
1277
{
1261
1278
foreach ( var mat in materials ) {
1262
- if ( MaterialMap . TryGetValue ( mat . name , out newMaterial ) )
1279
+ if ( MaterialMap . TryGetValue ( mat . GetInstanceID ( ) , out newMaterial ) )
1263
1280
{
1264
1281
fbxNode . AddMaterial ( newMaterial ) ;
1265
1282
}
1283
+ else
1284
+ {
1285
+ // create new material
1286
+ ExportMaterial ( mat , fbxScene , fbxNode ) ;
1287
+ }
1266
1288
}
1267
1289
}
1268
1290
@@ -2515,18 +2537,59 @@ private void SetDefaultCamera (FbxScene fbxScene)
2515
2537
/// </summary>
2516
2538
/// <returns>Unique name</returns>
2517
2539
/// <param name="name">Name</param>
2518
- private string GetUniqueName ( string name )
2540
+ /// <param name="nameToCountMap">The dictionary to use to map name to # of occurences</param>
2541
+ private string GetUniqueName ( string name , Dictionary < string , int > nameToCountMap )
2519
2542
{
2520
2543
var uniqueName = name ;
2521
- if ( NameToIndexMap . ContainsKey ( name ) ) {
2522
- uniqueName = string . Format ( UniqueNameFormat , name , NameToIndexMap [ name ] ) ;
2523
- NameToIndexMap [ name ] ++ ;
2524
- } else {
2525
- NameToIndexMap [ name ] = 1 ;
2544
+ int count ;
2545
+ if ( nameToCountMap . TryGetValue ( name , out count ) )
2546
+ {
2547
+ uniqueName = string . Format ( UniqueNameFormat , name , count ) ;
2548
+ }
2549
+ else
2550
+ {
2551
+ count = 0 ;
2526
2552
}
2553
+ nameToCountMap [ name ] = count + 1 ;
2527
2554
return uniqueName ;
2528
2555
}
2529
2556
2557
+ /// <summary>
2558
+ /// Ensures that the inputted name is unique.
2559
+ /// If a duplicate name is found, then it is incremented.
2560
+ /// e.g. Sphere becomes Sphere_1
2561
+ /// </summary>
2562
+ /// <returns>Unique name</returns>
2563
+ /// <param name="name">Name</param>
2564
+ private string GetUniqueFbxNodeName ( string name )
2565
+ {
2566
+ return GetUniqueName ( name , NameToIndexMap ) ;
2567
+ }
2568
+
2569
+ /// <summary>
2570
+ /// Ensures that the inputted material name is unique.
2571
+ /// If a duplicate name is found, then it is incremented.
2572
+ /// e.g. mat becomes mat_1
2573
+ /// </summary>
2574
+ /// <param name="name">Name</param>
2575
+ /// <returns>Unique material name</returns>
2576
+ private string GetUniqueMaterialName ( string name )
2577
+ {
2578
+ return GetUniqueName ( name , MaterialNameToIndexMap ) ;
2579
+ }
2580
+
2581
+ /// <summary>
2582
+ /// Ensures that the inputted texture name is unique.
2583
+ /// If a duplicate name is found, then it is incremented.
2584
+ /// e.g. tex becomes tex_1
2585
+ /// </summary>
2586
+ /// <param name="name">Name</param>
2587
+ /// <returns>Unique texture name</returns>
2588
+ private string GetUniqueTextureName ( string name )
2589
+ {
2590
+ return GetUniqueName ( name , TextureNameToIndexMap ) ;
2591
+ }
2592
+
2530
2593
/// <summary>
2531
2594
/// Create a fbxNode from unityGo.
2532
2595
/// </summary>
@@ -2546,7 +2609,7 @@ private FbxNode CreateFbxNode(GameObject unityGo, FbxScene fbxScene)
2546
2609
}
2547
2610
}
2548
2611
2549
- FbxNode fbxNode = FbxNode . Create ( fbxScene , GetUniqueName ( fbxName ) ) ;
2612
+ FbxNode fbxNode = FbxNode . Create ( fbxScene , GetUniqueFbxNodeName ( fbxName ) ) ;
2550
2613
2551
2614
// Default inheritance type in FBX is RrSs, which causes scaling issues in Maya as
2552
2615
// both Maya and Unity use RSrs inheritance by default.
@@ -3126,7 +3189,7 @@ private bool ExportComponents(FbxScene fbxScene)
3126
3189
var fbxNode = entry . Value ;
3127
3190
3128
3191
// try export mesh
3129
- bool exportedMesh = ExportInstance ( unityGo , fbxNode ) ;
3192
+ bool exportedMesh = ExportInstance ( unityGo , fbxScene , fbxNode ) ;
3130
3193
3131
3194
if ( ! exportedMesh ) {
3132
3195
exportedMesh = ExportMesh ( unityGo , fbxNode ) ;
0 commit comments