BabylonExporter.GLTFExporter.Material.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using BabylonExport.Entities;
  2. using GLTFExport.Entities;
  3. namespace Max2Babylon
  4. {
  5. partial class BabylonExporter
  6. {
  7. //readonly List<IIGameMaterial> referencedMaterials = new List<IIGameMaterial>();
  8. private void ExportMaterial(BabylonMaterial babylonMaterial, GLTF gltf)
  9. {
  10. var name = babylonMaterial.name;
  11. var id = babylonMaterial.id;
  12. RaiseMessage("GLTFExporter.Material | ExportMaterial name=" + name, 1);
  13. if (babylonMaterial.GetType() == typeof(BabylonStandardMaterial))
  14. {
  15. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial != null", 1);
  16. var babylonStandardMaterial = babylonMaterial as BabylonStandardMaterial;
  17. // --- prints ---
  18. RaiseMessage("GLTFExporter.Material | babylonMaterial data", 1);
  19. RaiseMessage("GLTFExporter.Material | babylonMaterial.alpha=" + babylonMaterial.alpha, 2);
  20. RaiseMessage("GLTFExporter.Material | babylonMaterial.backFaceCulling=" + babylonMaterial.backFaceCulling, 2);
  21. RaiseMessage("GLTFExporter.Material | babylonMaterial.wireframe=" + babylonMaterial.wireframe, 2);
  22. // Ambient
  23. for (int i = 0; i < babylonStandardMaterial.ambient.Length; i++)
  24. {
  25. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.ambient[" + i + "]=" + babylonStandardMaterial.ambient[i], 2);
  26. }
  27. // Diffuse
  28. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.diffuse.Length=" + babylonStandardMaterial.diffuse.Length, 2);
  29. for (int i = 0; i < babylonStandardMaterial.diffuse.Length; i++)
  30. {
  31. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.diffuse[" + i + "]=" + babylonStandardMaterial.diffuse[i], 2);
  32. }
  33. if (babylonStandardMaterial.diffuseTexture == null)
  34. {
  35. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.diffuseTexture=null", 2);
  36. }
  37. // Normal / bump
  38. if (babylonStandardMaterial.bumpTexture == null)
  39. {
  40. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.bumpTexture=null", 2);
  41. }
  42. // Specular
  43. for (int i = 0; i < babylonStandardMaterial.specular.Length; i++)
  44. {
  45. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.specular[" + i + "]=" + babylonStandardMaterial.specular[i], 2);
  46. }
  47. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.specularPower=" + babylonStandardMaterial.specularPower, 2);
  48. // Occlusion
  49. if (babylonStandardMaterial.ambientTexture == null)
  50. {
  51. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.ambientTexture=null", 2);
  52. }
  53. // Emissive
  54. for (int i = 0; i < babylonStandardMaterial.emissive.Length; i++)
  55. {
  56. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.emissive[" + i + "]=" + babylonStandardMaterial.emissive[i], 2);
  57. }
  58. if (babylonStandardMaterial.emissiveTexture == null)
  59. {
  60. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.emissiveTexture=null", 2);
  61. }
  62. // --------------------------------
  63. // --------- gltfMaterial ---------
  64. // --------------------------------
  65. RaiseMessage("GLTFExporter.Material | create gltfMaterial", 1);
  66. var gltfMaterial = new GLTFMaterial
  67. {
  68. name = name
  69. };
  70. gltfMaterial.id = babylonMaterial.id;
  71. gltfMaterial.index = gltf.MaterialsList.Count;
  72. gltf.MaterialsList.Add(gltfMaterial);
  73. // Alpha
  74. string alphaMode;
  75. float? alphaCutoff;
  76. getAlphaMode(babylonStandardMaterial, out alphaMode, out alphaCutoff);
  77. gltfMaterial.alphaMode = alphaMode;
  78. gltfMaterial.alphaCutoff = alphaCutoff;
  79. // DoubleSided
  80. gltfMaterial.doubleSided = !babylonMaterial.backFaceCulling;
  81. // Normal
  82. gltfMaterial.normalTexture = ExportTexture(babylonStandardMaterial.bumpTexture, gltf);
  83. // Occulison
  84. gltfMaterial.occlusionTexture = ExportTexture(babylonStandardMaterial.ambientTexture, gltf);
  85. // Emissive
  86. gltfMaterial.emissiveFactor = babylonStandardMaterial.emissive;
  87. gltfMaterial.emissiveTexture = ExportTexture(babylonStandardMaterial.emissiveTexture, gltf);
  88. // --------------------------------
  89. // --- gltfPbrMetallicRoughness ---
  90. // --------------------------------
  91. RaiseMessage("GLTFExporter.Material | create gltfPbrMetallicRoughness", 1);
  92. var gltfPbrMetallicRoughness = new GLTFPBRMetallicRoughness();
  93. gltfMaterial.pbrMetallicRoughness = gltfPbrMetallicRoughness;
  94. // Base color
  95. var babylonDiffuseColor = babylonStandardMaterial.diffuse;
  96. gltfPbrMetallicRoughness.baseColorFactor = new float[4]
  97. {
  98. babylonDiffuseColor[0],
  99. babylonDiffuseColor[1],
  100. babylonDiffuseColor[2],
  101. babylonMaterial.alpha
  102. };
  103. gltfPbrMetallicRoughness.baseColorTexture = ExportTexture(babylonStandardMaterial.diffuseTexture, gltf);
  104. // TODO - Metallic roughness
  105. gltfPbrMetallicRoughness.metallicFactor = 0; // Non metal
  106. // TODO - roughnessFactor
  107. // TODO - metallicRoughnessTexture
  108. }
  109. }
  110. private void getAlphaMode(BabylonStandardMaterial babylonMaterial, out string alphaMode, out float? alphaCutoff)
  111. {
  112. if (babylonMaterial.diffuseTexture.hasAlpha)
  113. {
  114. alphaMode = GLTFMaterial.AlphaMode.BLEND.ToString();
  115. }
  116. else
  117. {
  118. alphaMode = GLTFMaterial.AlphaMode.OPAQUE.ToString();
  119. }
  120. alphaCutoff = null;
  121. }
  122. }
  123. }