2
0

ExporterMaterial.cpp 7.8 KB

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