@@ -254,7 +254,7 @@ public FbxDouble3 GetMaterialColor (Material unityMaterial, string unityPropName
254
254
/// <summary>
255
255
/// Export (and map) a Unity PBS material to FBX classic material
256
256
/// </summary>
257
- public FbxSurfaceMaterial ExportMaterial ( Material unityMaterial , FbxScene fbxScene )
257
+ public FbxSurfaceMaterial ExportMaterial ( Material unityMaterial , FbxScene fbxScene , FbxMesh fbxMesh )
258
258
{
259
259
if ( Verbose )
260
260
Debug . Log ( string . Format ( "exporting material {0}" , unityMaterial . name ) ) ;
@@ -292,6 +292,26 @@ public FbxSurfaceMaterial ExportMaterial (Material unityMaterial, FbxScene fbxSc
292
292
ExportTexture ( unityMaterial , "_SpecGlosMap" , fbxMaterial , FbxSurfaceMaterial . sSpecular ) ;
293
293
}
294
294
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
+
295
315
MaterialMap . Add ( materialName , fbxMaterial ) ;
296
316
return fbxMaterial ;
297
317
}
@@ -350,7 +370,7 @@ meshInfo.Vertices [v].z
350
370
}
351
371
}
352
372
353
- var fbxMaterial = ExportMaterial ( meshInfo . Material , fbxScene ) ;
373
+ var fbxMaterial = ExportMaterial ( meshInfo . Material , fbxScene , fbxMesh ) ;
354
374
fbxNode . AddMaterial ( fbxMaterial ) ;
355
375
356
376
/*
@@ -487,13 +507,18 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
487
507
fbxManager . SetIOSettings ( FbxIOSettings . Create ( fbxManager , Globals . IOSROOT ) ) ;
488
508
489
509
// 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
+ }
491
513
492
514
// Create the exporter
493
515
var fbxExporter = FbxExporter . Create ( fbxManager , "Exporter" ) ;
494
516
495
517
// 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
+
497
522
bool status = fbxExporter . Initialize ( LastFilePath , fileFormat , fbxManager . GetIOSettings ( ) ) ;
498
523
// Check that initialization of the fbxExporter was successful
499
524
if ( ! status )
0 commit comments