CEffect.cpp 9.5 KB

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