ExporterMaterial.cpp 7.5 KB

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