CEffect.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include "common.h"
  2. #include "CEffect.h"
  3. #include <utility>
  4. #include <iostream>
  5. #include "WallpaperEngine/Core/CScene.h"
  6. #include "WallpaperEngine/Core/CProject.h"
  7. #include "WallpaperEngine/Core/Objects/CImage.h"
  8. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstant.h"
  9. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantFloat.h"
  10. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantVector4.h"
  11. #include "WallpaperEngine/Core/Objects/Effects/Constants/CShaderConstantInteger.h"
  12. #include "WallpaperEngine/Core/UserSettings/CUserSettingBoolean.h"
  13. #include "WallpaperEngine/FileSystem/FileSystem.h"
  14. using namespace WallpaperEngine;
  15. using namespace WallpaperEngine::Core::Objects;
  16. using namespace WallpaperEngine::Core::UserSettings;
  17. CEffect::CEffect (
  18. std::string name,
  19. std::string description,
  20. std::string group,
  21. std::string preview,
  22. CObject* object,
  23. CUserSettingBoolean* visible):
  24. m_name (std::move(name)),
  25. m_description (std::move(description)),
  26. m_group (std::move(group)),
  27. m_preview (std::move(preview)),
  28. m_object (object),
  29. m_visible (visible)
  30. {
  31. }
  32. CEffect* CEffect::fromJSON (json data, CUserSettingBoolean* visible, CObject* object, CContainer* container)
  33. {
  34. auto file_it = jsonFindRequired (data, "file", "Object effect must have a file");
  35. auto effectpasses_it = data.find ("passes");
  36. json content = json::parse (WallpaperEngine::FileSystem::loadFullFile ((*file_it).get <std::string> (), container));
  37. auto name_it = jsonFindRequired (content, "name", "Effect must have a name");
  38. auto description = jsonFindDefault <std::string> (content, "description", "");
  39. auto group_it = jsonFindRequired (content, "group", "Effect must have a group");
  40. auto preview = jsonFindDefault <std::string> (content, "preview", "");
  41. auto passes_it = jsonFindRequired (content, "passes", "Effect must have a pass list");
  42. auto dependencies_it = jsonFindRequired (content, "dependencies", "");
  43. auto fbos_it = content.find ("fbos");
  44. auto* effect = new CEffect (
  45. *name_it,
  46. description,
  47. *group_it,
  48. preview,
  49. object,
  50. visible
  51. );
  52. CEffect::materialsFromJSON (passes_it, effect, container);
  53. CEffect::dependencyFromJSON (dependencies_it, effect);
  54. if (fbos_it != content.end ())
  55. CEffect::fbosFromJSON (fbos_it, effect);
  56. if (effectpasses_it != data.end ())
  57. {
  58. auto cur = (*effectpasses_it).begin ();
  59. auto end = (*effectpasses_it).end ();
  60. for (int passNumber = 0; cur != end; cur ++, passNumber ++)
  61. {
  62. auto constants_it = (*cur).find ("constantshadervalues");
  63. auto combos_it = (*cur).find ("combos");
  64. auto textures_it = (*cur).find ("textures");
  65. if (constants_it == (*cur).end () && combos_it == (*cur).end () && textures_it == (*cur).end ())
  66. continue;
  67. Images::CMaterial* material = effect->getMaterials ().at (passNumber);
  68. for (const auto& passCur : material->getPasses ())
  69. {
  70. if (textures_it != (*cur).end ())
  71. {
  72. int textureNumber = 0;
  73. for (const auto& texturesCur : (*textures_it))
  74. {
  75. std::string texture;
  76. if (texturesCur.is_null ())
  77. {
  78. if (textureNumber == 0)
  79. {
  80. auto* image = object->as <CImage> ();
  81. texture = (*(*image->getMaterial ()->getPasses ().begin ())->getTextures ().begin ());
  82. }
  83. else
  84. {
  85. texture = "";
  86. }
  87. }
  88. else
  89. {
  90. texture = texturesCur;
  91. }
  92. const auto& passTextures = passCur->getTextures ();
  93. if (textureNumber < passTextures.size ())
  94. passCur->setTexture (textureNumber, texture);
  95. else
  96. passCur->insertTexture (texture);
  97. textureNumber ++;
  98. }
  99. }
  100. if (combos_it != (*cur).end ())
  101. {
  102. CEffect::combosFromJSON (combos_it, passCur);
  103. }
  104. if (constants_it != (*cur).end ())
  105. {
  106. CEffect::constantsFromJSON (constants_it, passCur);
  107. }
  108. }
  109. }
  110. }
  111. return effect;
  112. }
  113. void CEffect::combosFromJSON (json::const_iterator combos_it, Core::Objects::Images::Materials::CPass* pass)
  114. {
  115. for (const auto& cur : (*combos_it).items ())
  116. pass->insertCombo (cur.key (), cur.value ());
  117. }
  118. void CEffect::constantsFromJSON (json::const_iterator constants_it, Core::Objects::Images::Materials::CPass* pass)
  119. {
  120. for (auto& cur : (*constants_it).items ())
  121. {
  122. auto val = cur.value ();
  123. Effects::Constants::CShaderConstant* constant;
  124. // if the constant is an object, that means the constant has some extra information
  125. // for the UI, take the value, which is what we need
  126. // TODO: SUPPORT USER SETTINGS HERE
  127. if (cur.value ().is_object ())
  128. {
  129. auto it = cur.value ().find ("value");
  130. if (it == cur.value ().end ())
  131. {
  132. sLog.error ("Found object for shader constant without \"value\" member");
  133. continue;
  134. }
  135. val = it.value ();
  136. }
  137. if (val.is_number_float ())
  138. {
  139. constant = new Effects::Constants::CShaderConstantFloat (val.get <float> ());
  140. }
  141. else if (val.is_number_integer ())
  142. {
  143. constant = new Effects::Constants::CShaderConstantInteger (val.get <int> ());
  144. }
  145. else if (val.is_string ())
  146. {
  147. // try a vector 4 first, then a vector3 and then a vector 2
  148. constant = new Effects::Constants::CShaderConstantVector4 (WallpaperEngine::Core::aToVector4 (val));
  149. }
  150. else
  151. {
  152. sLog.exception ("unknown shader constant type ", val);
  153. }
  154. pass->insertConstant (cur.key (), constant);
  155. }
  156. }
  157. void CEffect::fbosFromJSON (json::const_iterator fbos_it, CEffect* effect)
  158. {
  159. for (const auto& cur : (*fbos_it))
  160. effect->insertFBO (Effects::CFBO::fromJSON (cur));
  161. }
  162. void CEffect::dependencyFromJSON (json::const_iterator dependencies_it, CEffect* effect)
  163. {
  164. for (const auto& cur : (*dependencies_it))
  165. effect->insertDependency (cur);
  166. }
  167. void CEffect::materialsFromJSON (json::const_iterator passes_it, CEffect* effect, CContainer* container)
  168. {
  169. for (const auto& cur : (*passes_it))
  170. {
  171. auto materialfile = cur.find ("material");
  172. auto target = cur.find ("target");
  173. auto bind = cur.find ("bind");
  174. if (materialfile == cur.end ())
  175. sLog.exception ("Found an effect ", effect->m_name, " without material");
  176. Images::CMaterial* material;
  177. if (target == cur.end ())
  178. material = Images::CMaterial::fromFile ((*materialfile).get <std::string> (), container);
  179. else
  180. material = Images::CMaterial::fromFile ((*materialfile).get <std::string> (), *target, container);
  181. if (bind != cur.end ())
  182. {
  183. for (const auto& bindCur : (*bind))
  184. material->insertTextureBind (Effects::CBind::fromJSON (bindCur));
  185. }
  186. effect->insertMaterial (material);
  187. }
  188. }
  189. const std::vector<std::string>& CEffect::getDependencies () const
  190. {
  191. return this->m_dependencies;
  192. }
  193. const std::vector<Images::CMaterial*>& CEffect::getMaterials () const
  194. {
  195. return this->m_materials;
  196. }
  197. const std::vector<Effects::CFBO*>& CEffect::getFbos () const
  198. {
  199. return this->m_fbos;
  200. }
  201. bool CEffect::isVisible () const
  202. {
  203. return this->m_visible->processValue (this->m_object->getScene ()->getProject ().getProperties ());
  204. }
  205. Effects::CFBO* CEffect::findFBO (const std::string& name)
  206. {
  207. for (const auto& cur : this->m_fbos)
  208. if (cur->getName () == name)
  209. return cur;
  210. sLog.exception ("cannot find fbo ", name);
  211. }
  212. void CEffect::insertDependency (const std::string& dep)
  213. {
  214. this->m_dependencies.push_back (dep);
  215. }
  216. void CEffect::insertMaterial (Images::CMaterial* material)
  217. {
  218. this->m_materials.push_back (material);
  219. }
  220. void CEffect::insertFBO (Effects::CFBO* fbo)
  221. {
  222. this->m_fbos.push_back (fbo);
  223. }