MaterialParser.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "MaterialParser.h"
  2. #include "TextureParser.h"
  3. #include "WallpaperEngine/Data/Model/Material.h"
  4. #include "WallpaperEngine/Data/Model/Project.h"
  5. #include "WallpaperEngine/Data/Parsers/ShaderConstantParser.h"
  6. #include "WallpaperEngine/FileSystem/Container.h"
  7. using namespace WallpaperEngine::Data::Parsers;
  8. using namespace WallpaperEngine::Data::Model;
  9. MaterialUniquePtr MaterialParser::load (const Project& project, const std::string& filename) {
  10. JSON materialJson;
  11. // WE (sub_1401515B0) logs a material it can't open or parse and builds it from an empty object instead, which
  12. // ends up as one pass with its "error" shader, a plain copy of g_Texture0: the effect using it changes nothing
  13. try {
  14. materialJson = JSON::parseAsset (project.assetLocator->readString (filename));
  15. } catch (const std::exception& e) {
  16. sLog.error ("Material ", filename, " error: ", e.what ());
  17. materialJson = JSON::object ();
  18. }
  19. return parse (materialJson, filename, project);
  20. }
  21. MaterialUniquePtr MaterialParser::parse (const JSON& it, const std::string& filename, const Project& project) {
  22. const auto passes = it.find ("passes");
  23. // without a non-empty "passes" array WE reads the material's root as its pass
  24. if (passes == it.end () || !passes->is_array () || passes->empty ()) {
  25. std::vector<MaterialPassUniquePtr> single;
  26. single.push_back (parsePass (it, project));
  27. return std::make_unique<Material> (Material {
  28. .filename = filename,
  29. .passes = std::move (single),
  30. });
  31. }
  32. return std::make_unique<Material> (Material {
  33. .filename = filename,
  34. .passes = parsePasses (*passes, project),
  35. });
  36. }
  37. std::vector<MaterialPassUniquePtr> MaterialParser::parsePasses (const JSON& it, const Project& project) {
  38. std::vector<MaterialPassUniquePtr> result = {};
  39. if (!it.is_array ()) {
  40. return result;
  41. }
  42. for (const auto& cur : it) {
  43. result.push_back (parsePass (cur, project));
  44. }
  45. return result;
  46. }
  47. MaterialPassUniquePtr MaterialParser::parsePass (const JSON& it, const Project& project) {
  48. const auto textures = it.optional ("textures");
  49. const auto usertextures = it.optional ("usertextures");
  50. const auto combos = it.optional ("combos");
  51. const auto constants = it.optional ("constantshadervalues");
  52. return std::make_unique<MaterialPass> (MaterialPass {
  53. // TODO: avoid this std::string construction
  54. .blending = parseBlendMode (it.optional ("blending", std::string ("normal"))),
  55. // left unknown when missing, which draws like nocull/disabled, 3D models pick their own defaults
  56. .cullmode = it.contains ("cullmode") ? parseCullMode (it["cullmode"]) : CullingMode_Unknown,
  57. .depthtest = it.contains ("depthtest") ? parseDepthtestMode (it["depthtest"]) : DepthtestMode_Unknown,
  58. .depthwrite = it.contains ("depthwrite") ? parseDepthwriteMode (it["depthwrite"]) : DepthwriteMode_Unknown,
  59. // no shader falls back to WE's "error" shader too (sub_140154480)
  60. .shader = it.optional<std::string> ("shader", "error"),
  61. .textures = textures.has_value () ? TextureParser::parseTextureMap (*textures) : TextureMap {},
  62. .usertextures = usertextures.has_value () ? TextureParser::parseTextureMap (*usertextures) : TextureMap {},
  63. .combos = combos.has_value () ? parseCombos (*combos) : ComboMap {},
  64. .constants = constants.has_value () ? ShaderConstantParser::parse (*constants, project) : ShaderConstantMap {},
  65. });
  66. }
  67. std::map<std::string, int> MaterialParser::parseCombos (const JSON& it) {
  68. std::map<std::string, int> result = {};
  69. if (!it.is_object ()) {
  70. return result;
  71. }
  72. for (const auto& cur : it.items ()) {
  73. result.emplace (cur.key (), cur.value ());
  74. }
  75. return result;
  76. }
  77. BlendingMode MaterialParser::parseBlendMode (const std::string& mode) {
  78. if (mode == "normal") {
  79. return BlendingMode_Normal;
  80. }
  81. if (mode == "additive") {
  82. return BlendingMode_Additive;
  83. }
  84. if (mode == "translucent") {
  85. return BlendingMode_Translucent;
  86. }
  87. if (mode == "alphatocoverage") {
  88. return BlendingMode_AlphaToCoverage;
  89. }
  90. sLog.error ("Unknown blending mode: ", mode, " defaulting to normal");
  91. return BlendingMode_Normal;
  92. }
  93. CullingMode MaterialParser::parseCullMode (const std::string& mode) {
  94. if (mode == "nocull") {
  95. return CullingMode_Disable;
  96. }
  97. if (mode == "normal") {
  98. return CullingMode_Normal;
  99. }
  100. sLog.error ("Unknown culling mode: ", mode, " defaulting to nocull");
  101. return CullingMode_Disable;
  102. }
  103. DepthtestMode MaterialParser::parseDepthtestMode (const std::string& mode) {
  104. if (mode == "disabled") {
  105. return DepthtestMode_Disabled;
  106. }
  107. if (mode == "enabled") {
  108. return DepthtestMode_Enabled;
  109. }
  110. sLog.error ("Unknown depthtest mode: ", mode, " defaulting to disabled");
  111. return DepthtestMode_Disabled;
  112. }
  113. DepthwriteMode MaterialParser::parseDepthwriteMode (const std::string& mode) {
  114. if (mode == "disabled") {
  115. return DepthwriteMode_Disabled;
  116. }
  117. if (mode == "enabled") {
  118. return DepthwriteMode_Enabled;
  119. }
  120. sLog.error ("Unknown depthwrite mode: ", mode, " defaulting to disabled");
  121. return DepthwriteMode_Disabled;
  122. }