BabylonExporter.GLTFExporter.Material.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using BabylonExport.Entities;
  2. using GLTFExport.Entities;
  3. using System;
  4. using System.Drawing;
  5. using System.IO;
  6. namespace Max2Babylon
  7. {
  8. partial class BabylonExporter
  9. {
  10. private void ExportMaterial(BabylonMaterial babylonMaterial, GLTF gltf)
  11. {
  12. var name = babylonMaterial.name;
  13. var id = babylonMaterial.id;
  14. RaiseMessage("GLTFExporter.Material | Export material named: " + name, 1);
  15. if (babylonMaterial.GetType() == typeof(BabylonStandardMaterial))
  16. {
  17. var babylonStandardMaterial = babylonMaterial as BabylonStandardMaterial;
  18. // --- prints ---
  19. RaiseMessage("GLTFExporter.Material | babylonMaterial data", 2);
  20. RaiseMessage("GLTFExporter.Material | babylonMaterial.alpha=" + babylonMaterial.alpha, 3);
  21. RaiseMessage("GLTFExporter.Material | babylonMaterial.backFaceCulling=" + babylonMaterial.backFaceCulling, 3);
  22. RaiseMessage("GLTFExporter.Material | babylonMaterial.wireframe=" + babylonMaterial.wireframe, 3);
  23. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.specularPower=" + babylonStandardMaterial.specularPower, 3);
  24. // Ambient
  25. for (int i = 0; i < babylonStandardMaterial.ambient.Length; i++)
  26. {
  27. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.ambient[" + i + "]=" + babylonStandardMaterial.ambient[i], 3);
  28. }
  29. // Diffuse
  30. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.diffuse.Length=" + babylonStandardMaterial.diffuse.Length, 3);
  31. for (int i = 0; i < babylonStandardMaterial.diffuse.Length; i++)
  32. {
  33. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.diffuse[" + i + "]=" + babylonStandardMaterial.diffuse[i], 3);
  34. }
  35. if (babylonStandardMaterial.diffuseTexture == null)
  36. {
  37. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.diffuseTexture=null", 3);
  38. }
  39. // Normal / bump
  40. if (babylonStandardMaterial.bumpTexture == null)
  41. {
  42. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.bumpTexture=null", 3);
  43. }
  44. // Specular
  45. for (int i = 0; i < babylonStandardMaterial.specular.Length; i++)
  46. {
  47. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.specular[" + i + "]=" + babylonStandardMaterial.specular[i], 3);
  48. }
  49. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.specularPower=" + babylonStandardMaterial.specularPower, 3);
  50. // Occlusion
  51. if (babylonStandardMaterial.ambientTexture == null)
  52. {
  53. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.ambientTexture=null", 3);
  54. }
  55. // Emissive
  56. for (int i = 0; i < babylonStandardMaterial.emissive.Length; i++)
  57. {
  58. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.emissive[" + i + "]=" + babylonStandardMaterial.emissive[i], 3);
  59. }
  60. if (babylonStandardMaterial.emissiveTexture == null)
  61. {
  62. RaiseMessage("GLTFExporter.Material | babylonStandardMaterial.emissiveTexture=null", 3);
  63. }
  64. // --------------------------------
  65. // --------- gltfMaterial ---------
  66. // --------------------------------
  67. RaiseMessage("GLTFExporter.Material | create gltfMaterial", 2);
  68. var gltfMaterial = new GLTFMaterial
  69. {
  70. name = name
  71. };
  72. gltfMaterial.id = babylonMaterial.id;
  73. gltfMaterial.index = gltf.MaterialsList.Count;
  74. gltf.MaterialsList.Add(gltfMaterial);
  75. // Alpha
  76. string alphaMode;
  77. float? alphaCutoff;
  78. getAlphaMode(babylonStandardMaterial, out alphaMode, out alphaCutoff);
  79. gltfMaterial.alphaMode = alphaMode;
  80. gltfMaterial.alphaCutoff = alphaCutoff;
  81. // DoubleSided
  82. gltfMaterial.doubleSided = !babylonMaterial.backFaceCulling;
  83. // Normal
  84. gltfMaterial.normalTexture = ExportTexture(babylonStandardMaterial.bumpTexture, gltf);
  85. // Occulison
  86. gltfMaterial.occlusionTexture = ExportTexture(babylonStandardMaterial.ambientTexture, gltf);
  87. // Emissive
  88. gltfMaterial.emissiveFactor = babylonStandardMaterial.emissive;
  89. gltfMaterial.emissiveTexture = ExportTexture(babylonStandardMaterial.emissiveTexture, gltf);
  90. // --------------------------------
  91. // --- gltfPbrMetallicRoughness ---
  92. // --------------------------------
  93. RaiseMessage("GLTFExporter.Material | create gltfPbrMetallicRoughness", 2);
  94. var gltfPbrMetallicRoughness = new GLTFPBRMetallicRoughness();
  95. gltfMaterial.pbrMetallicRoughness = gltfPbrMetallicRoughness;
  96. // --- Global ---
  97. SpecularGlossiness _specularGlossiness = new SpecularGlossiness
  98. {
  99. diffuse = new BabylonColor3(babylonStandardMaterial.diffuse),
  100. opacity = babylonMaterial.alpha,
  101. specular = new BabylonColor3(babylonStandardMaterial.specular),
  102. glossiness = babylonStandardMaterial.specularPower / 256
  103. };
  104. MetallicRoughness _metallicRoughness = ConvertToMetallicRoughness(_specularGlossiness, true);
  105. // Base color
  106. gltfPbrMetallicRoughness.baseColorFactor = new float[4]
  107. {
  108. _metallicRoughness.baseColor.r,
  109. _metallicRoughness.baseColor.g,
  110. _metallicRoughness.baseColor.b,
  111. _metallicRoughness.opacity
  112. };
  113. // Metallic roughness
  114. gltfPbrMetallicRoughness.metallicFactor = _metallicRoughness.metallic;
  115. gltfPbrMetallicRoughness.roughnessFactor = _metallicRoughness.roughness;
  116. // --- Textures ---
  117. if (babylonStandardMaterial.diffuseTexture != null)
  118. {
  119. Func<string, Bitmap> loadTexture = delegate (string textureName)
  120. {
  121. var pathDiffuse = Path.Combine(gltf.OutputPath, textureName);
  122. if (File.Exists(pathDiffuse))
  123. {
  124. return new Bitmap(pathDiffuse);
  125. }
  126. else
  127. {
  128. RaiseWarning(string.Format("GLTFExporter.Material | Texture {0} not found.", textureName), 2);
  129. return null;
  130. }
  131. };
  132. Bitmap diffuseBitmap = loadTexture(babylonStandardMaterial.diffuseTexture.name);
  133. if (diffuseBitmap != null)
  134. {
  135. Bitmap specularBitmap = null;
  136. if (babylonStandardMaterial.specularTexture != null)
  137. {
  138. specularBitmap = loadTexture(babylonStandardMaterial.specularTexture.name);
  139. }
  140. Bitmap opacityBitmap = null;
  141. if (babylonStandardMaterial.diffuseTexture.hasAlpha == false && babylonStandardMaterial.opacityTexture != null)
  142. {
  143. opacityBitmap = loadTexture(babylonStandardMaterial.opacityTexture.name);
  144. }
  145. // Retreive dimension from diffuse map
  146. var width = diffuseBitmap.Width;
  147. var height = diffuseBitmap.Height;
  148. // Create base color and metallic+roughness maps
  149. Bitmap baseColorBitmap = new Bitmap(width, height);
  150. Bitmap metallicRoughnessBitmap = new Bitmap(width, height);
  151. for (int x = 0; x < width; x++)
  152. {
  153. for (int y = 0; y < height; y++)
  154. {
  155. var diffuse = diffuseBitmap.GetPixel(x, y);
  156. SpecularGlossiness specularGlossinessTexture = new SpecularGlossiness
  157. {
  158. diffuse = new BabylonColor3(diffuse),
  159. opacity = babylonStandardMaterial.diffuseTexture.hasAlpha? diffuse.A / 255.0f :
  160. opacityBitmap != null && babylonStandardMaterial.opacityTexture.getAlphaFromRGB ? opacityBitmap.GetPixel(x, y).R / 255.0f :
  161. opacityBitmap != null && babylonStandardMaterial.opacityTexture.getAlphaFromRGB == false ? opacityBitmap.GetPixel(x, y).A / 255.0f :
  162. 1,
  163. specular = specularBitmap != null ? new BabylonColor3(specularBitmap.GetPixel(x, y)) :
  164. new BabylonColor3(),
  165. glossiness = babylonStandardMaterial.useGlossinessFromSpecularMapAlpha && specularBitmap != null ? specularBitmap.GetPixel(x, y).A / 255.0f :
  166. babylonStandardMaterial.specularPower / 256.0f
  167. };
  168. var displayPrints = x == width / 2 && y == height / 2;
  169. MetallicRoughness metallicRoughnessTexture = ConvertToMetallicRoughness(specularGlossinessTexture, displayPrints);
  170. Color colorBase = Color.FromArgb(
  171. (int)(metallicRoughnessTexture.opacity * 255),
  172. (int)(metallicRoughnessTexture.baseColor.r * 255),
  173. (int)(metallicRoughnessTexture.baseColor.g * 255),
  174. (int)(metallicRoughnessTexture.baseColor.b * 255)
  175. );
  176. baseColorBitmap.SetPixel(x, y, colorBase);
  177. // The metalness values are sampled from the B channel.
  178. // The roughness values are sampled from the G channel.
  179. // These values are linear. If other channels are present (R or A), they are ignored for metallic-roughness calculations.
  180. Color colorMetallicRoughness = Color.FromArgb(
  181. 0,
  182. (int)(metallicRoughnessTexture.roughness * 255),
  183. (int)(metallicRoughnessTexture.metallic * 255)
  184. );
  185. metallicRoughnessBitmap.SetPixel(x, y, colorMetallicRoughness);
  186. }
  187. }
  188. // Export maps and textures
  189. gltfPbrMetallicRoughness.baseColorTexture = ExportBitmapTexture(babylonStandardMaterial.diffuseTexture, baseColorBitmap, babylonMaterial.name + "_baseColor" + ".png", gltf);
  190. gltfPbrMetallicRoughness.metallicRoughnessTexture = ExportBitmapTexture(babylonStandardMaterial.diffuseTexture, metallicRoughnessBitmap, babylonMaterial.name + "_metallicRoughness" + ".jpg", gltf);
  191. }
  192. }
  193. }
  194. }
  195. private void getAlphaMode(BabylonStandardMaterial babylonMaterial, out string alphaMode, out float? alphaCutoff)
  196. {
  197. if ((babylonMaterial.diffuseTexture != null && babylonMaterial.diffuseTexture.hasAlpha) ||
  198. babylonMaterial.opacityTexture != null)
  199. {
  200. // TODO - Babylon standard material is assumed to useAlphaFromDiffuseTexture. If not, the alpha mode is a mask.
  201. alphaMode = GLTFMaterial.AlphaMode.BLEND.ToString();
  202. }
  203. else
  204. {
  205. // glTF alpha mode default value is "OPAQUE"
  206. alphaMode = null; // GLTFMaterial.AlphaMode.OPAQUE.ToString();
  207. }
  208. alphaCutoff = null;
  209. }
  210. BabylonColor3 dielectricSpecular = new BabylonColor3(0.04f, 0.04f, 0.04f);
  211. const float epsilon = 1e-6f;
  212. private MetallicRoughness ConvertToMetallicRoughness(SpecularGlossiness specularGlossiness, bool displayPrints = false)
  213. {
  214. var diffuse = specularGlossiness.diffuse;
  215. var opacity = specularGlossiness.opacity;
  216. var specular = specularGlossiness.specular;
  217. var glossiness = specularGlossiness.glossiness;
  218. var oneMinusSpecularStrength = 1 - specular.getMaxComponent();
  219. var metallic = solveMetallic(diffuse.getPerceivedBrightness(), specular.getPerceivedBrightness(), oneMinusSpecularStrength);
  220. var diffuseScaleFactor = oneMinusSpecularStrength / (1 - dielectricSpecular.r) / Math.Max(1 - metallic, epsilon);
  221. var baseColorFromDiffuse = diffuse.scale(diffuseScaleFactor);
  222. var baseColorFromSpecular = specular.subtract(dielectricSpecular.scale(1 - metallic)).scale(1 / Math.Max(metallic, epsilon));
  223. var baseColor = BabylonColor3.Lerp(baseColorFromDiffuse, baseColorFromSpecular, metallic * metallic).clamp();
  224. //var baseColor = baseColorFromDiffuse.clamp();
  225. if (displayPrints)
  226. {
  227. RaiseMessage("-----------------------", 3);
  228. RaiseMessage("diffuse=" + diffuse, 3);
  229. RaiseMessage("opacity=" + opacity, 3);
  230. RaiseMessage("specular=" + specular, 3);
  231. RaiseMessage("glossiness=" + glossiness, 3);
  232. RaiseMessage("oneMinusSpecularStrength=" + oneMinusSpecularStrength, 3);
  233. RaiseMessage("metallic=" + metallic, 3);
  234. RaiseMessage("diffuseScaleFactor=" + diffuseScaleFactor, 3);
  235. RaiseMessage("baseColorFromDiffuse=" + baseColorFromDiffuse, 3);
  236. RaiseMessage("baseColorFromSpecular=" + baseColorFromSpecular, 3);
  237. RaiseMessage("metallic * metallic=" + metallic * metallic, 3);
  238. RaiseMessage("baseColor=" + baseColor, 3);
  239. RaiseMessage("-----------------------", 3);
  240. }
  241. return new MetallicRoughness
  242. {
  243. baseColor = baseColor,
  244. opacity = opacity,
  245. metallic = metallic,
  246. roughness = 1 - glossiness
  247. };
  248. }
  249. private float solveMetallic(float diffuse, float specular, float oneMinusSpecularStrength)
  250. {
  251. if (specular < dielectricSpecular.r)
  252. {
  253. return 0;
  254. }
  255. var a = dielectricSpecular.r;
  256. var b = diffuse * oneMinusSpecularStrength / (1 - dielectricSpecular.r) + specular - 2 * dielectricSpecular.r;
  257. var c = dielectricSpecular.r - specular;
  258. var D = b * b - 4 * a * c;
  259. return ClampScalar((float)(-b + Math.Sqrt(D)) / (2 * a), 0, 1);
  260. }
  261. /**
  262. * Returns the value itself if it's between min and max.
  263. * Returns min if the value is lower than min.
  264. * Returns max if the value is greater than max.
  265. */
  266. private static float ClampScalar(float value, float min = 0, float max = 1)
  267. {
  268. return Math.Min(max, Math.Max(min, value));
  269. }
  270. private class SpecularGlossiness
  271. {
  272. public BabylonColor3 diffuse;
  273. public float opacity;
  274. public BabylonColor3 specular;
  275. public float glossiness;
  276. }
  277. private class MetallicRoughness
  278. {
  279. public BabylonColor3 baseColor;
  280. public float opacity;
  281. public float metallic;
  282. public float roughness;
  283. }
  284. }
  285. }