ExporterMaterial.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "Exporter.h"
  6. #include <iostream>
  7. const char* MATERIAL_TEMPLATE = R"(<?xml version="1.0" encoding="UTF-8" ?>
  8. <!-- This file is auto generated by ExporterMaterial.cpp -->
  9. <material shaderProgram="shaders/GBufferGeneric.glslp">
  10. <mutators>
  11. <mutator name="DIFFUSE_TEX" value="%diffTexMutator%"/>
  12. <mutator name="SPECULAR_TEX" value="%specTexMutator%"/>
  13. <mutator name="ROUGHNESS_TEX" value="%roughnessTexMutator%"/>
  14. <mutator name="METAL_TEX" value="%metalTexMutator%"/>
  15. <mutator name="NORMAL_TEX" value="%normalTexMutator%"/>
  16. <mutator name="PARALLAX" value="%parallaxMutator%"/>
  17. <mutator name="EMISSIVE_TEX" value="%emissiveTexMutator%"/>
  18. </mutators>
  19. <inputs>
  20. <input shaderInput="mvp" builtin="MODEL_VIEW_PROJECTION_MATRIX"/>
  21. <input shaderInput="prevMvp" builtin="PREVIOUS_MODEL_VIEW_PROJECTION_MATRIX"/>
  22. <input shaderInput="rotationMat" builtin="ROTATION_MATRIX"/>
  23. %parallaxInput%
  24. %diff%
  25. %spec%
  26. %roughness%
  27. %metallic%
  28. %normal%
  29. %emission%
  30. %subsurface%
  31. %height%
  32. </inputs>
  33. </material>
  34. )";
  35. void Exporter::exportMaterial(const aiMaterial& mtl) const
  36. {
  37. aiString path;
  38. std::string name = getMaterialName(mtl);
  39. LOGI("Exporting material %s", name.c_str());
  40. std::string xml = MATERIAL_TEMPLATE;
  41. // Diffuse texture
  42. if(mtl.GetTextureCount(aiTextureType_DIFFUSE) > 0)
  43. {
  44. if(mtl.GetTexture(aiTextureType_DIFFUSE, 0, &path) == AI_SUCCESS)
  45. {
  46. std::string diffTex = m_texrpath + getFilename(path.C_Str());
  47. xml = replaceAllString(xml, "%diff%", "<input shaderInput=\"diffTex\" value=\"" + diffTex + "\"/>");
  48. xml = replaceAllString(xml, "%diffTexMutator%", "1");
  49. }
  50. else
  51. {
  52. ERROR("Failed to retrieve texture");
  53. }
  54. }
  55. else
  56. {
  57. aiColor3D diffCol = {0.0, 0.0, 0.0};
  58. mtl.Get(AI_MATKEY_COLOR_DIFFUSE, diffCol);
  59. xml = replaceAllString(xml,
  60. "%diff%",
  61. "<input shaderInput=\"diffColor\" value=\"" + std::to_string(diffCol[0]) + " " + std::to_string(diffCol[1])
  62. + " " + std::to_string(diffCol[2]) + "\"/>");
  63. xml = replaceAllString(xml, "%diffTexMutator%", "0");
  64. }
  65. // Specular color
  66. if(mtl.GetTextureCount(aiTextureType_SPECULAR) > 0)
  67. {
  68. if(mtl.GetTexture(aiTextureType_SPECULAR, 0, &path) == AI_SUCCESS)
  69. {
  70. std::string specTex = m_texrpath + getFilename(path.C_Str());
  71. xml = replaceAllString(xml, "%spec%", "<input shaderInput=\"specTex\" value=\"" + specTex + "\"/>");
  72. xml = replaceAllString(xml, "%specTexMutator%", "1");
  73. }
  74. else
  75. {
  76. ERROR("Failed to retrieve texture");
  77. }
  78. }
  79. else
  80. {
  81. aiColor3D specCol = {0.0, 0.0, 0.0};
  82. mtl.Get(AI_MATKEY_COLOR_SPECULAR, specCol);
  83. xml = replaceAllString(xml,
  84. "%spec%",
  85. "<input shaderInput=\"specColor\" value=\"" + std::to_string(specCol[0]) + " " + std::to_string(specCol[1])
  86. + " " + std::to_string(specCol[2]) + "\"/>");
  87. xml = replaceAllString(xml, "%specTexMutator%", "0");
  88. }
  89. // Roughness
  90. if(mtl.GetTextureCount(aiTextureType_SHININESS) > 0)
  91. {
  92. if(mtl.GetTexture(aiTextureType_SHININESS, 0, &path) == AI_SUCCESS)
  93. {
  94. std::string shininessTex = m_texrpath + getFilename(path.C_Str());
  95. xml = replaceAllString(
  96. xml, "%roughness%", "<input shaderInput=\"roughnessTex\" value=\"" + shininessTex + "\"/>");
  97. xml = replaceAllString(xml, "%roughnessTexMutator%", "1");
  98. }
  99. else
  100. {
  101. ERROR("Failed to retrieve texture");
  102. }
  103. }
  104. else
  105. {
  106. float roughness = 0.0;
  107. if(mtl.mAnKiProperties.find("roughness") != mtl.mAnKiProperties.end())
  108. {
  109. roughness = std::stof(mtl.mAnKiProperties.at("roughness"));
  110. }
  111. else
  112. {
  113. mtl.Get(AI_MATKEY_SHININESS, roughness);
  114. const float MAX_SHININESS = 511.0;
  115. roughness = std::min(MAX_SHININESS, roughness);
  116. if(roughness > MAX_SHININESS)
  117. {
  118. LOGW("Shininness exceeds %f", MAX_SHININESS);
  119. }
  120. roughness = roughness / MAX_SHININESS;
  121. }
  122. xml = replaceAllString(
  123. xml, "%roughness%", "<input shaderInput=\"roughness\" value=\"" + std::to_string(roughness) + "\" />");
  124. xml = replaceAllString(xml, "%roughnessTexMutator%", "0");
  125. }
  126. // Metallic texture
  127. if(mtl.GetTextureCount(aiTextureType_REFLECTION) > 0)
  128. {
  129. if(mtl.GetTexture(aiTextureType_REFLECTION, 0, &path) == AI_SUCCESS)
  130. {
  131. std::string metallicTex = m_texrpath + getFilename(path.C_Str());
  132. xml =
  133. replaceAllString(xml, "%metallic%", "<input shaderInput=\"metalTex\" value=\"" + metallicTex + "\"/>");
  134. xml = replaceAllString(xml, "%metalTexMutator%", "1");
  135. }
  136. else
  137. {
  138. ERROR("Failed to retrieve texture");
  139. }
  140. }
  141. else
  142. {
  143. float metallic = 0.0;
  144. if(mtl.mAnKiProperties.find("metallic") != mtl.mAnKiProperties.end())
  145. {
  146. metallic = std::stof(mtl.mAnKiProperties.at("metallic"));
  147. }
  148. xml = replaceAllString(
  149. xml, "%metallic%", "<input shaderInput=\"metallic\" value=\"" + std::to_string(metallic) + "\"/>");
  150. xml = replaceAllString(xml, "%metalTexMutator%", "0");
  151. }
  152. // Normal texture
  153. if(mtl.GetTextureCount(aiTextureType_NORMALS) > 0)
  154. {
  155. if(mtl.GetTexture(aiTextureType_NORMALS, 0, &path) == AI_SUCCESS)
  156. {
  157. std::string normTex = m_texrpath + getFilename(path.C_Str());
  158. xml = replaceAllString(xml, "%normal%", "<input shaderInput=\"normalTex\" value=\"" + normTex + "\"/>");
  159. xml = replaceAllString(xml, "%normalTexMutator%", "1");
  160. }
  161. else
  162. {
  163. ERROR("Failed to retrieve texture");
  164. }
  165. }
  166. else
  167. {
  168. xml = replaceAllString(xml, "%normal%", "");
  169. xml = replaceAllString(xml, "%normalTexMutator%", "0");
  170. }
  171. // Emissive texture
  172. if(mtl.GetTextureCount(aiTextureType_EMISSIVE) > 0)
  173. {
  174. if(mtl.GetTexture(aiTextureType_EMISSIVE, 0, &path) == AI_SUCCESS)
  175. {
  176. std::string emissiveTex = m_texrpath + getFilename(path.C_Str());
  177. xml = replaceAllString(
  178. xml, "%emission%", "<input shaderInput=\"emissiveTex\" value=\"" + emissiveTex + "\"/>");
  179. xml = replaceAllString(xml, "%emissiveTexMutator%", "1");
  180. }
  181. else
  182. {
  183. ERROR("Failed to retrieve texture");
  184. }
  185. }
  186. else
  187. {
  188. aiColor3D emissionCol = {0.0, 0.0, 0.0};
  189. mtl.Get(AI_MATKEY_COLOR_EMISSIVE, emissionCol);
  190. xml = replaceAllString(xml,
  191. "%emission%",
  192. "<input shaderInput=\"emission\" value=\"" + std::to_string(emissionCol[0]) + " "
  193. + std::to_string(emissionCol[1]) + " " + std::to_string(emissionCol[2]) + "\"/>");
  194. xml = replaceAllString(xml, "%emissiveTexMutator%", "0");
  195. }
  196. // Subsurface
  197. {
  198. float subsurface = 0.0;
  199. if(mtl.mAnKiProperties.find("subsurface") != mtl.mAnKiProperties.end())
  200. {
  201. subsurface = std::stof(mtl.mAnKiProperties.at("subsurface"));
  202. }
  203. xml = replaceAllString(
  204. xml, "%subsurface%", "<input shaderInput=\"subsurface\" value=\"" + std::to_string(subsurface) + "\"/>");
  205. }
  206. // Height texture
  207. if(mtl.GetTextureCount(aiTextureType_DISPLACEMENT) > 0)
  208. {
  209. if(mtl.GetTexture(aiTextureType_DISPLACEMENT, 0, &path) == AI_SUCCESS)
  210. {
  211. std::string dispTex = m_texrpath + getFilename(path.C_Str());
  212. xml = replaceAllString(xml,
  213. "%height%",
  214. "<input shaderInput=\"heightTex\" value=\"" + dispTex
  215. + "\"/>\n"
  216. "\t\t<input shaderInput=\"heightMapScale\" value=\"0.05\"/>");
  217. xml = replaceAllString(
  218. xml, "%parallaxInput%", "<input shaderInput=\"modelViewMat\" builtin=\"MODEL_VIEW_MATRIX\"/>");
  219. xml = replaceAllString(xml, "%parallaxMutator%", "1");
  220. }
  221. else
  222. {
  223. ERROR("Failed to retrieve texture");
  224. }
  225. }
  226. else
  227. {
  228. xml = replaceAllString(xml, "%height%", "");
  229. xml = replaceAllString(xml, "%parallaxInput%", "");
  230. xml = replaceAllString(xml, "%parallaxMutator%", "0");
  231. }
  232. // Replace texture extensions with .anki
  233. xml = replaceAllString(xml, ".tga", ".ankitex");
  234. xml = replaceAllString(xml, ".png", ".ankitex");
  235. xml = replaceAllString(xml, ".jpg", ".ankitex");
  236. xml = replaceAllString(xml, ".jpeg", ".ankitex");
  237. // Open and write file
  238. std::fstream file;
  239. file.open(m_outputDirectory + name + ".ankimtl", std::ios::out);
  240. file << xml;
  241. }