ExporterMaterial.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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="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="rotationMat" builtin="ROTATION_MATRIX"/>
  22. %parallaxInput%
  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. + " " + std::to_string(diffCol[2]) + "\"/>");
  62. xml = replaceAllString(xml, "%diffTexMutator%", "0");
  63. }
  64. // Specular color
  65. if(mtl.GetTextureCount(aiTextureType_SPECULAR) > 0)
  66. {
  67. if(mtl.GetTexture(aiTextureType_SPECULAR, 0, &path) == AI_SUCCESS)
  68. {
  69. std::string specTex = m_texrpath + getFilename(path.C_Str());
  70. xml = replaceAllString(xml, "%spec%", "<input shaderInput=\"specTex\" value=\"" + specTex + "\"/>");
  71. xml = replaceAllString(xml, "%specTexMutator%", "1");
  72. }
  73. else
  74. {
  75. ERROR("Failed to retrieve texture");
  76. }
  77. }
  78. else
  79. {
  80. aiColor3D specCol = {0.0, 0.0, 0.0};
  81. mtl.Get(AI_MATKEY_COLOR_SPECULAR, specCol);
  82. xml = replaceAllString(xml,
  83. "%spec%",
  84. "<input shaderInput=\"specColor\" value=\"" + std::to_string(specCol[0]) + " " + std::to_string(specCol[1])
  85. + " " + std::to_string(specCol[2]) + "\"/>");
  86. xml = replaceAllString(xml, "%specTexMutator%", "0");
  87. }
  88. // Roughness
  89. if(mtl.GetTextureCount(aiTextureType_SHININESS) > 0)
  90. {
  91. if(mtl.GetTexture(aiTextureType_SHININESS, 0, &path) == AI_SUCCESS)
  92. {
  93. std::string shininessTex = m_texrpath + getFilename(path.C_Str());
  94. xml = replaceAllString(
  95. xml, "%roughness%", "<input shaderInput=\"roughnessTex\" value=\"" + shininessTex + "\"/>");
  96. xml = replaceAllString(xml, "%roughnessTexMutator%", "1");
  97. }
  98. else
  99. {
  100. ERROR("Failed to retrieve texture");
  101. }
  102. }
  103. else
  104. {
  105. float shininess = 0.0;
  106. mtl.Get(AI_MATKEY_SHININESS, shininess);
  107. const float MAX_SHININESS = 511.0;
  108. shininess = std::min(MAX_SHININESS, shininess);
  109. if(shininess > MAX_SHININESS)
  110. {
  111. LOGW("Shininness exceeds %f", MAX_SHININESS);
  112. }
  113. shininess = shininess / MAX_SHININESS;
  114. xml = replaceAllString(
  115. xml, "%roughness%", "<input shaderInput=\"roughness\" value=\"" + std::to_string(shininess) + "\" />");
  116. xml = replaceAllString(xml, "%roughnessTexMutator%", "0");
  117. }
  118. // Metallic texture
  119. if(mtl.GetTextureCount(aiTextureType_REFLECTION) > 0)
  120. {
  121. if(mtl.GetTexture(aiTextureType_REFLECTION, 0, &path) == AI_SUCCESS)
  122. {
  123. std::string metallicTex = m_texrpath + getFilename(path.C_Str());
  124. xml =
  125. replaceAllString(xml, "%metallic%", "<input shaderInput=\"metalTex\" value=\"" + metallicTex + "\"/>");
  126. xml = replaceAllString(xml, "%metalTexMutator%", "1");
  127. }
  128. else
  129. {
  130. ERROR("Failed to retrieve texture");
  131. }
  132. }
  133. else
  134. {
  135. float metallic = 0.0;
  136. if(mtl.mAnKiProperties.find("metallic") != mtl.mAnKiProperties.end())
  137. {
  138. metallic = std::stof(mtl.mAnKiProperties.at("metallic"));
  139. }
  140. xml = replaceAllString(
  141. xml, "%metallic%", "<input shaderInput=\"metallic\" value=\"" + std::to_string(metallic) + "\"/>");
  142. xml = replaceAllString(xml, "%metalTexMutator%", "0");
  143. }
  144. // Normal texture
  145. if(mtl.GetTextureCount(aiTextureType_NORMALS) > 0)
  146. {
  147. if(mtl.GetTexture(aiTextureType_NORMALS, 0, &path) == AI_SUCCESS)
  148. {
  149. std::string normTex = m_texrpath + getFilename(path.C_Str());
  150. xml = replaceAllString(xml, "%normal%", "<input shaderInput=\"normalTex\" value=\"" + normTex + "\"/>");
  151. xml = replaceAllString(xml, "%normalTexMutator%", "1");
  152. }
  153. else
  154. {
  155. ERROR("Failed to retrieve texture");
  156. }
  157. }
  158. else
  159. {
  160. xml = replaceAllString(xml, "%normal%", "");
  161. xml = replaceAllString(xml, "%normalTexMutator%", "0");
  162. }
  163. // Emissive texture
  164. if(mtl.GetTextureCount(aiTextureType_EMISSIVE) > 0)
  165. {
  166. if(mtl.GetTexture(aiTextureType_EMISSIVE, 0, &path) == AI_SUCCESS)
  167. {
  168. std::string emissiveTex = m_texrpath + getFilename(path.C_Str());
  169. xml = replaceAllString(
  170. xml, "%emission%", "<input shaderInput=\"emissiveTex\" value=\"" + emissiveTex + "\"/>");
  171. xml = replaceAllString(xml, "%emissiveTexMutator%", "1");
  172. }
  173. else
  174. {
  175. ERROR("Failed to retrieve texture");
  176. }
  177. }
  178. else
  179. {
  180. aiColor3D emissionCol = {0.0, 0.0, 0.0};
  181. mtl.Get(AI_MATKEY_COLOR_EMISSIVE, emissionCol);
  182. xml = replaceAllString(xml,
  183. "%emission%",
  184. "<input shaderInput=\"emission\" value=\"" + std::to_string(emissionCol[0]) + " "
  185. + std::to_string(emissionCol[1]) + " " + std::to_string(emissionCol[2]) + "\"/>");
  186. xml = replaceAllString(xml, "%emissiveTexMutator%", "0");
  187. }
  188. // Subsurface
  189. {
  190. float subsurface = 0.0;
  191. if(mtl.mAnKiProperties.find("subsurface") != mtl.mAnKiProperties.end())
  192. {
  193. subsurface = std::stof(mtl.mAnKiProperties.at("subsurface"));
  194. }
  195. xml = replaceAllString(
  196. xml, "%subsurface%", "<input shaderInput=\"subsurface\" value=\"" + std::to_string(subsurface) + "\"/>");
  197. }
  198. // Height texture
  199. if(mtl.GetTextureCount(aiTextureType_DISPLACEMENT) > 0)
  200. {
  201. if(mtl.GetTexture(aiTextureType_DISPLACEMENT, 0, &path) == AI_SUCCESS)
  202. {
  203. std::string dispTex = m_texrpath + getFilename(path.C_Str());
  204. xml = replaceAllString(xml,
  205. "%height%",
  206. "<input shaderInput=\"heightTex\" value=\"" + dispTex
  207. + "\"/>\n"
  208. "\t\t<input shaderInput=\"heightMapScale\" value=\"0.05\"/>");
  209. xml = replaceAllString(
  210. xml, "%parallaxInput%", "<input shaderInput=\"modelViewMat\" builtin=\"MODEL_VIEW_MATRIX\"/>");
  211. xml = replaceAllString(xml, "%parallaxMutator%", "1");
  212. }
  213. else
  214. {
  215. ERROR("Failed to retrieve texture");
  216. }
  217. }
  218. else
  219. {
  220. xml = replaceAllString(xml, "%height%", "");
  221. xml = replaceAllString(xml, "%parallaxInput%", "");
  222. xml = replaceAllString(xml, "%parallaxMutator%", "0");
  223. }
  224. // Replace texture extensions with .anki
  225. xml = replaceAllString(xml, ".tga", ".ankitex");
  226. xml = replaceAllString(xml, ".png", ".ankitex");
  227. xml = replaceAllString(xml, ".jpg", ".ankitex");
  228. xml = replaceAllString(xml, ".jpeg", ".ankitex");
  229. // Open and write file
  230. std::fstream file;
  231. file.open(m_outputDirectory + name + ".ankimtl", std::ios::out);
  232. file << xml;
  233. }