GltfImporterMaterial.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Importer/GltfImporter.h>
  6. #include <AnKi/Resource/ImageLoader.h>
  7. #include <AnKi/Util/WeakArray.h>
  8. namespace anki {
  9. const char* MATERIAL_TEMPLATE = R"(<!-- This file is auto generated by ImporterMaterial.cpp -->
  10. <material shadows="1">
  11. <shaderPrograms>
  12. <shaderProgram filename="ShaderBinaries/GBufferGeneric.ankiprogbin">
  13. <mutation>
  14. <mutator name="DIFFUSE_TEX" value="%diffTexMutator%"/>
  15. <mutator name="SPECULAR_TEX" value="%specTexMutator%"/>
  16. <mutator name="ROUGHNESS_TEX" value="%roughnessTexMutator%"/>
  17. <mutator name="METAL_TEX" value="%metalTexMutator%"/>
  18. <mutator name="NORMAL_TEX" value="%normalTexMutator%"/>
  19. <mutator name="PARALLAX" value="%parallaxMutator%"/>
  20. <mutator name="EMISSIVE_TEX" value="%emissiveTexMutator%"/>
  21. <mutator name="ALPHA_TEST" value="%alphaTestMutator%"/>
  22. </mutation>
  23. </shaderProgram>
  24. %rayTracing%
  25. </shaderPrograms>
  26. <inputs>
  27. %parallaxInput%
  28. %diff%
  29. %spec%
  30. %roughness%
  31. %metallic%
  32. %normal%
  33. %emission%
  34. %subsurface%
  35. %height%
  36. </inputs>
  37. </material>
  38. )";
  39. const char* RT_MATERIAL_TEMPLATE = R"(
  40. <shaderProgram filename="ShaderBinaries/RtShadowsHit.ankiprogbin">
  41. <mutation>
  42. <mutator name="ALPHA_TEXTURE" value="0"/>
  43. </mutation>
  44. </shaderProgram>
  45. )";
  46. static CString getTextureUri(const cgltf_texture_view& view)
  47. {
  48. ANKI_ASSERT(view.texture);
  49. ANKI_ASSERT(view.texture->image);
  50. ANKI_ASSERT(view.texture->image->uri);
  51. return view.texture->image->uri;
  52. }
  53. /// Read the texture and find out if
  54. static Error findConstantColorsInImage(CString fname, Vec4& constantColor, GenericMemoryPoolAllocator<U8>& alloc)
  55. {
  56. ImageLoader iloader(alloc);
  57. ANKI_CHECK(iloader.load(fname));
  58. ANKI_ASSERT(iloader.getColorFormat() == ImageBinaryColorFormat::RGBA8);
  59. ANKI_ASSERT(iloader.getCompression() == ImageBinaryDataCompression::RAW);
  60. const U8Vec4* data = reinterpret_cast<const U8Vec4*>(&iloader.getSurface(0, 0, 0).m_data[0]);
  61. ConstWeakArray<U8Vec4> pixels(data, iloader.getWidth() * iloader.getHeight());
  62. const F32 epsilon = 1.0f / 255.0f;
  63. for(U32 y = 0; y < iloader.getHeight(); ++y)
  64. {
  65. for(U32 x = 0; x < iloader.getWidth(); ++x)
  66. {
  67. const U8Vec4& pixel = pixels[y * iloader.getWidth() + x];
  68. const Vec4 pixelf = Vec4(pixel) / 255.0f;
  69. if(x == 0 && y == 0)
  70. {
  71. // Initialize
  72. constantColor = pixelf;
  73. }
  74. else
  75. {
  76. for(U32 i = 0; i < 4; ++i)
  77. {
  78. if(absolute(pixelf[i] - constantColor[i]) > epsilon)
  79. {
  80. constantColor[i] = -1.0f;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. return Error::NONE;
  87. }
  88. Error GltfImporter::writeMaterial(const cgltf_material& mtl, Bool writeRayTracing)
  89. {
  90. StringAuto fname(m_alloc);
  91. fname.sprintf("%s%s", m_outDir.cstr(), computeMaterialResourceFilename(mtl).cstr());
  92. ANKI_IMPORTER_LOGV("Importing material %s", fname.cstr());
  93. if(!mtl.has_pbr_metallic_roughness)
  94. {
  95. ANKI_IMPORTER_LOGE("Expecting PBR metallic roughness");
  96. return Error::USER_DATA;
  97. }
  98. HashMapAuto<CString, StringAuto> extras(m_alloc);
  99. ANKI_CHECK(getExtras(mtl.extras, extras));
  100. StringAuto xml(m_alloc);
  101. xml.append(XML_HEADER);
  102. xml.append("\n");
  103. xml.append(MATERIAL_TEMPLATE);
  104. if(writeRayTracing)
  105. {
  106. xml.replaceAll("%rayTracing%", RT_MATERIAL_TEMPLATE);
  107. }
  108. // Diffuse
  109. if(mtl.pbr_metallic_roughness.base_color_texture.texture)
  110. {
  111. const CString fname = getTextureUri(mtl.pbr_metallic_roughness.base_color_texture);
  112. StringAuto uri(m_alloc);
  113. uri.sprintf("%s%s", m_texrpath.cstr(), fname.cstr());
  114. xml.replaceAll("%diff%", StringAuto(m_alloc).sprintf("<input name=\"m_diffTex\" value=\"%s\"/>", uri.cstr()));
  115. xml.replaceAll("%diffTexMutator%", "1");
  116. Vec4 constantColor;
  117. ANKI_CHECK(findConstantColorsInImage(fname, constantColor, m_alloc));
  118. const Bool constantAlpha = constantColor.w() >= 0.0f;
  119. xml.replaceAll("%alphaTestMutator%", (constantAlpha) ? "0" : "1");
  120. }
  121. else
  122. {
  123. const F32* diffCol = &mtl.pbr_metallic_roughness.base_color_factor[0];
  124. xml.replaceAll("%diff%", StringAuto(m_alloc).sprintf("<input name=\"m_diffColor\" value=\"%f %f %f\"/>",
  125. diffCol[0], diffCol[1], diffCol[2]));
  126. xml.replaceAll("%diffTexMutator%", "0");
  127. xml.replaceAll("%alphaTestMutator%", "0");
  128. }
  129. // Specular color (freshnel)
  130. {
  131. Vec3 specular;
  132. auto it = extras.find("specular");
  133. if(it != extras.getEnd())
  134. {
  135. StringListAuto tokens(m_alloc);
  136. tokens.splitString(it->toCString(), ' ');
  137. if(tokens.getSize() != 3)
  138. {
  139. ANKI_IMPORTER_LOGE("Wrong specular: %s", it->cstr());
  140. return Error::USER_DATA;
  141. }
  142. auto token = tokens.getBegin();
  143. ANKI_CHECK(token->toNumber(specular.x()));
  144. ++token;
  145. ANKI_CHECK(token->toNumber(specular.y()));
  146. ++token;
  147. ANKI_CHECK(token->toNumber(specular.z()));
  148. }
  149. else
  150. {
  151. specular = Vec3(0.04f);
  152. }
  153. xml.replaceAll("%spec%", StringAuto(m_alloc).sprintf("<input name=\"m_specColor\" value=\"%f %f %f\"/>",
  154. specular.x(), specular.y(), specular.z()));
  155. xml.replaceAll("%specTexMutator%", "0");
  156. }
  157. // Identify metallic/roughness texture
  158. F32 constantMetaliness = -1.0f, constantRoughness = -1.0f;
  159. if(mtl.pbr_metallic_roughness.metallic_roughness_texture.texture)
  160. {
  161. const CString fname = getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture);
  162. Vec4 constantColor;
  163. ANKI_CHECK(findConstantColorsInImage(fname, constantColor, m_alloc));
  164. constantRoughness = constantColor.y();
  165. constantMetaliness = constantColor.z();
  166. }
  167. // Roughness
  168. if(mtl.pbr_metallic_roughness.metallic_roughness_texture.texture && constantRoughness < 0.0f)
  169. {
  170. StringAuto uri(m_alloc);
  171. uri.sprintf("%s%s", m_texrpath.cstr(),
  172. getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
  173. xml.replaceAll("%roughness%",
  174. StringAuto(m_alloc).sprintf("<input name=\"m_roughnessTex\" value=\"%s\"/>", uri.cstr()));
  175. xml.replaceAll("%roughnessTexMutator%", "1");
  176. }
  177. else
  178. {
  179. const F32 roughness = (constantRoughness >= 0.0f)
  180. ? constantRoughness * mtl.pbr_metallic_roughness.roughness_factor
  181. : mtl.pbr_metallic_roughness.roughness_factor;
  182. xml.replaceAll("%roughness%",
  183. StringAuto(m_alloc).sprintf("<input name=\"m_roughness\" value=\"%f\"/>", roughness));
  184. xml.replaceAll("%roughnessTexMutator%", "0");
  185. }
  186. // Metallic
  187. if(mtl.pbr_metallic_roughness.metallic_roughness_texture.texture && constantMetaliness < 0.0f)
  188. {
  189. StringAuto uri(m_alloc);
  190. uri.sprintf("%s%s", m_texrpath.cstr(),
  191. getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
  192. xml.replaceAll("%metallic%",
  193. StringAuto(m_alloc).sprintf("<input name=\"m_metallicTex\" value=\"%s\"/>", uri.cstr()));
  194. xml.replaceAll("%metalTexMutator%", "1");
  195. }
  196. else
  197. {
  198. const F32 metalines = (constantMetaliness >= 0.0f)
  199. ? constantMetaliness * mtl.pbr_metallic_roughness.metallic_factor
  200. : mtl.pbr_metallic_roughness.metallic_factor;
  201. xml.replaceAll("%metallic%",
  202. StringAuto(m_alloc).sprintf("<input name=\"m_metallic\" value=\"%f\"/>", metalines));
  203. xml.replaceAll("%metalTexMutator%", "0");
  204. }
  205. // Normal texture
  206. if(mtl.normal_texture.texture)
  207. {
  208. Vec4 constantColor;
  209. ANKI_CHECK(findConstantColorsInImage(getTextureUri(mtl.normal_texture).cstr(), constantColor, m_alloc));
  210. if(constantColor.xyz() == -1.0f)
  211. {
  212. StringAuto uri(m_alloc);
  213. uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.normal_texture).cstr());
  214. xml.replaceAll("%normal%",
  215. StringAuto(m_alloc).sprintf("<input name=\"m_normalTex\" value=\"%s\"/>", uri.cstr()));
  216. xml.replaceAll("%normalTexMutator%", "1");
  217. }
  218. else
  219. {
  220. xml.replaceAll("%normal%", "");
  221. xml.replaceAll("%normalTexMutator%", "0");
  222. }
  223. }
  224. else
  225. {
  226. xml.replaceAll("%normal%", "");
  227. xml.replaceAll("%normalTexMutator%", "0");
  228. }
  229. // Emissive texture
  230. if(mtl.emissive_texture.texture)
  231. {
  232. StringAuto uri(m_alloc);
  233. uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.emissive_texture).cstr());
  234. xml.replaceAll("%emission%",
  235. StringAuto(m_alloc).sprintf("<input name=\"m_emissiveTex\" value=\"%s\"/>", uri.cstr()));
  236. xml.replaceAll("%emissiveTexMutator%", "1");
  237. }
  238. else
  239. {
  240. const F32* emissionCol = &mtl.emissive_factor[0];
  241. xml.replaceAll("%emission%", StringAuto(m_alloc).sprintf("<input name=\"m_emission\" value=\"%f %f %f\"/>",
  242. emissionCol[0], emissionCol[1], emissionCol[2]));
  243. xml.replaceAll("%emissiveTexMutator%", "0");
  244. }
  245. // Subsurface
  246. {
  247. F32 subsurface;
  248. auto it = extras.find("subsurface");
  249. if(it != extras.getEnd())
  250. {
  251. ANKI_CHECK(it->toNumber(subsurface));
  252. }
  253. else
  254. {
  255. subsurface = 0.0f;
  256. }
  257. xml.replaceAll("%subsurface%",
  258. StringAuto(m_alloc).sprintf("<input name=\"m_subsurface\" value=\"%f\"/>", subsurface));
  259. }
  260. // Height texture
  261. auto it = extras.find("height_map");
  262. if(it != extras.getEnd())
  263. {
  264. StringAuto uri(m_alloc);
  265. uri.sprintf("%s%s", m_texrpath.cstr(), it->cstr());
  266. xml.replaceAll("%height%", StringAuto(m_alloc).sprintf("<input name=\"m_heightTex\" value=\"%s\" \"/>\n"
  267. "\t\t<input name=\"m_heightmapScale\" value=\"0.05\"/>",
  268. uri.cstr()));
  269. xml.replaceAll("%parallaxMutator%", "1");
  270. }
  271. else
  272. {
  273. xml.replaceAll("%height%", "");
  274. xml.replaceAll("%parallaxInput%", "");
  275. xml.replaceAll("%parallaxMutator%", "0");
  276. }
  277. // Replace texture extensions with .anki
  278. xml.replaceAll(".tga", ".ankitex");
  279. xml.replaceAll(".png", ".ankitex");
  280. xml.replaceAll(".jpg", ".ankitex");
  281. xml.replaceAll(".jpeg", ".ankitex");
  282. // Write file
  283. File file;
  284. ANKI_CHECK(file.open(fname.toCString(), FileOpenFlag::WRITE));
  285. ANKI_CHECK(file.writeText("%s", xml.cstr()));
  286. return Error::NONE;
  287. }
  288. } // end namespace anki