BabylonExporter.GLTFExporter.Material.cs 6.7 KB

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