shader_globals_editor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*************************************************************************/
  2. /* shader_globals_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "shader_globals_editor.h"
  31. #include "editor_node.h"
  32. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  33. "bool",
  34. "bvec2",
  35. "bvec3",
  36. "bvec4",
  37. "int",
  38. "ivec2",
  39. "ivec3",
  40. "ivec4",
  41. "rect2i",
  42. "uint",
  43. "uvec2",
  44. "uvec3",
  45. "uvec4",
  46. "float",
  47. "vec2",
  48. "vec3",
  49. "vec4",
  50. "color",
  51. "rect2",
  52. "mat2",
  53. "mat3",
  54. "mat4",
  55. "transform_2d",
  56. "transform",
  57. "sampler2D",
  58. "sampler2DArray",
  59. "sampler3D",
  60. "samplerCube",
  61. };
  62. class ShaderGlobalsEditorInterface : public Object {
  63. GDCLASS(ShaderGlobalsEditorInterface, Object)
  64. void _var_changed() {
  65. emit_signal(SNAME("var_changed"));
  66. }
  67. protected:
  68. static void _bind_methods() {
  69. ClassDB::bind_method("_var_changed", &ShaderGlobalsEditorInterface::_var_changed);
  70. ADD_SIGNAL(MethodInfo("var_changed"));
  71. }
  72. bool _set(const StringName &p_name, const Variant &p_value) {
  73. Variant existing = RS::get_singleton()->global_variable_get(p_name);
  74. if (existing.get_type() == Variant::NIL) {
  75. return false;
  76. }
  77. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  78. undo_redo->create_action(TTR("Set Shader Global Variable"));
  79. undo_redo->add_do_method(RS::get_singleton(), "global_variable_set", p_name, p_value);
  80. undo_redo->add_undo_method(RS::get_singleton(), "global_variable_set", p_name, existing);
  81. RS::GlobalVariableType type = RS::get_singleton()->global_variable_get_type(p_name);
  82. Dictionary gv;
  83. gv["type"] = global_var_type_names[type];
  84. if (type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  85. RES res = p_value;
  86. if (res.is_valid()) {
  87. gv["value"] = res->get_path();
  88. } else {
  89. gv["value"] = "";
  90. }
  91. } else {
  92. gv["value"] = p_value;
  93. }
  94. String path = "shader_globals/" + String(p_name);
  95. undo_redo->add_do_property(ProjectSettings::get_singleton(), path, gv);
  96. undo_redo->add_undo_property(ProjectSettings::get_singleton(), path, ProjectSettings::get_singleton()->get(path));
  97. undo_redo->add_do_method(this, "_var_changed");
  98. undo_redo->add_undo_method(this, "_var_changed");
  99. block_update = true;
  100. undo_redo->commit_action();
  101. block_update = false;
  102. return true;
  103. }
  104. bool _get(const StringName &p_name, Variant &r_ret) const {
  105. r_ret = RS::get_singleton()->global_variable_get(p_name);
  106. return r_ret.get_type() != Variant::NIL;
  107. }
  108. void _get_property_list(List<PropertyInfo> *p_list) const {
  109. Vector<StringName> variables;
  110. variables = RS::get_singleton()->global_variable_get_list();
  111. for (int i = 0; i < variables.size(); i++) {
  112. PropertyInfo pinfo;
  113. pinfo.name = variables[i];
  114. switch (RS::get_singleton()->global_variable_get_type(variables[i])) {
  115. case RS::GLOBAL_VAR_TYPE_BOOL: {
  116. pinfo.type = Variant::BOOL;
  117. } break;
  118. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  119. pinfo.type = Variant::INT;
  120. pinfo.hint = PROPERTY_HINT_FLAGS;
  121. pinfo.hint_string = "x,y";
  122. } break;
  123. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  124. pinfo.type = Variant::INT;
  125. pinfo.hint = PROPERTY_HINT_FLAGS;
  126. pinfo.hint_string = "x,y,z";
  127. } break;
  128. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  129. pinfo.type = Variant::INT;
  130. pinfo.hint = PROPERTY_HINT_FLAGS;
  131. pinfo.hint_string = "x,y,z,w";
  132. } break;
  133. case RS::GLOBAL_VAR_TYPE_INT: {
  134. pinfo.type = Variant::INT;
  135. } break;
  136. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  137. pinfo.type = Variant::VECTOR2I;
  138. } break;
  139. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  140. pinfo.type = Variant::VECTOR3I;
  141. } break;
  142. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  143. pinfo.type = Variant::PACKED_INT32_ARRAY;
  144. } break;
  145. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  146. pinfo.type = Variant::RECT2I;
  147. } break;
  148. case RS::GLOBAL_VAR_TYPE_UINT: {
  149. pinfo.type = Variant::INT;
  150. } break;
  151. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  152. pinfo.type = Variant::VECTOR2I;
  153. } break;
  154. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  155. pinfo.type = Variant::VECTOR3I;
  156. } break;
  157. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  158. pinfo.type = Variant::PACKED_INT32_ARRAY;
  159. } break;
  160. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  161. pinfo.type = Variant::FLOAT;
  162. } break;
  163. case RS::GLOBAL_VAR_TYPE_VEC2: {
  164. pinfo.type = Variant::VECTOR2;
  165. } break;
  166. case RS::GLOBAL_VAR_TYPE_VEC3: {
  167. pinfo.type = Variant::VECTOR3;
  168. } break;
  169. case RS::GLOBAL_VAR_TYPE_VEC4: {
  170. pinfo.type = Variant::PLANE;
  171. } break;
  172. case RS::GLOBAL_VAR_TYPE_RECT2: {
  173. pinfo.type = Variant::RECT2;
  174. } break;
  175. case RS::GLOBAL_VAR_TYPE_COLOR: {
  176. pinfo.type = Variant::COLOR;
  177. } break;
  178. case RS::GLOBAL_VAR_TYPE_MAT2: {
  179. pinfo.type = Variant::PACKED_INT32_ARRAY;
  180. } break;
  181. case RS::GLOBAL_VAR_TYPE_MAT3: {
  182. pinfo.type = Variant::BASIS;
  183. } break;
  184. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  185. pinfo.type = Variant::TRANSFORM2D;
  186. } break;
  187. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  188. pinfo.type = Variant::TRANSFORM3D;
  189. } break;
  190. case RS::GLOBAL_VAR_TYPE_MAT4: {
  191. pinfo.type = Variant::PACKED_INT32_ARRAY;
  192. } break;
  193. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  194. pinfo.type = Variant::OBJECT;
  195. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  196. pinfo.hint_string = "Texture2D";
  197. } break;
  198. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  199. pinfo.type = Variant::OBJECT;
  200. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  201. pinfo.hint_string = "Texture2DArray";
  202. } break;
  203. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  204. pinfo.type = Variant::OBJECT;
  205. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  206. pinfo.hint_string = "Texture3D";
  207. } break;
  208. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  209. pinfo.type = Variant::OBJECT;
  210. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  211. pinfo.hint_string = "Cubemap";
  212. } break;
  213. default: {
  214. } break;
  215. }
  216. p_list->push_back(pinfo);
  217. }
  218. }
  219. public:
  220. bool block_update = false;
  221. ShaderGlobalsEditorInterface() {
  222. }
  223. };
  224. static Variant create_var(RS::GlobalVariableType p_type) {
  225. switch (p_type) {
  226. case RS::GLOBAL_VAR_TYPE_BOOL: {
  227. return false;
  228. }
  229. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  230. return 0; //bits
  231. }
  232. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  233. return 0; //bits
  234. }
  235. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  236. return 0; //bits
  237. }
  238. case RS::GLOBAL_VAR_TYPE_INT: {
  239. return 0; //bits
  240. }
  241. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  242. return Vector2i();
  243. }
  244. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  245. return Vector3i();
  246. }
  247. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  248. Vector<int> v4;
  249. v4.resize(4);
  250. v4.write[0] = 0;
  251. v4.write[1] = 0;
  252. v4.write[2] = 0;
  253. v4.write[3] = 0;
  254. return v4;
  255. }
  256. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  257. return Rect2i();
  258. }
  259. case RS::GLOBAL_VAR_TYPE_UINT: {
  260. return 0;
  261. }
  262. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  263. return Vector2i();
  264. }
  265. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  266. return Vector3i();
  267. }
  268. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  269. Vector<int> v4;
  270. v4.resize(4);
  271. v4.write[0] = 0;
  272. v4.write[1] = 0;
  273. v4.write[2] = 0;
  274. v4.write[3] = 0;
  275. return v4;
  276. }
  277. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  278. return 0.0;
  279. }
  280. case RS::GLOBAL_VAR_TYPE_VEC2: {
  281. return Vector2();
  282. }
  283. case RS::GLOBAL_VAR_TYPE_VEC3: {
  284. return Vector3();
  285. }
  286. case RS::GLOBAL_VAR_TYPE_VEC4: {
  287. return Plane();
  288. }
  289. case RS::GLOBAL_VAR_TYPE_RECT2: {
  290. return Rect2();
  291. }
  292. case RS::GLOBAL_VAR_TYPE_COLOR: {
  293. return Color();
  294. }
  295. case RS::GLOBAL_VAR_TYPE_MAT2: {
  296. Vector<real_t> xform;
  297. xform.resize(4);
  298. xform.write[0] = 1;
  299. xform.write[1] = 0;
  300. xform.write[2] = 0;
  301. xform.write[3] = 1;
  302. return xform;
  303. }
  304. case RS::GLOBAL_VAR_TYPE_MAT3: {
  305. return Basis();
  306. }
  307. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  308. return Transform2D();
  309. }
  310. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  311. return Transform3D();
  312. }
  313. case RS::GLOBAL_VAR_TYPE_MAT4: {
  314. Vector<real_t> xform;
  315. xform.resize(16);
  316. xform.write[0] = 1;
  317. xform.write[1] = 0;
  318. xform.write[2] = 0;
  319. xform.write[3] = 0;
  320. xform.write[4] = 0;
  321. xform.write[5] = 1;
  322. xform.write[6] = 0;
  323. xform.write[7] = 0;
  324. xform.write[8] = 0;
  325. xform.write[9] = 0;
  326. xform.write[10] = 1;
  327. xform.write[11] = 0;
  328. xform.write[12] = 0;
  329. xform.write[13] = 0;
  330. xform.write[14] = 0;
  331. xform.write[15] = 1;
  332. return xform;
  333. }
  334. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  335. return "";
  336. }
  337. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  338. return "";
  339. }
  340. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  341. return "";
  342. }
  343. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  344. return "";
  345. }
  346. default: {
  347. return Variant();
  348. }
  349. }
  350. }
  351. void ShaderGlobalsEditor::_variable_added() {
  352. String var = variable_name->get_text().strip_edges();
  353. if (var == "" || !var.is_valid_identifier()) {
  354. EditorNode::get_singleton()->show_warning(TTR("Please specify a valid variable identifier name."));
  355. return;
  356. }
  357. if (RenderingServer::get_singleton()->global_variable_get(var).get_type() != Variant::NIL) {
  358. EditorNode::get_singleton()->show_warning(vformat(TTR("Global variable '%s' already exists'"), var));
  359. return;
  360. }
  361. List<String> keywords;
  362. ShaderLanguage::get_keyword_list(&keywords);
  363. if (keywords.find(var) != nullptr || var == "script") {
  364. EditorNode::get_singleton()->show_warning(vformat(TTR("Name '%s' is a reserved shader language keyword."), var));
  365. return;
  366. }
  367. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  368. Variant value = create_var(RS::GlobalVariableType(variable_type->get_selected()));
  369. undo_redo->create_action(TTR("Add Shader Global Variable"));
  370. undo_redo->add_do_method(RS::get_singleton(), "global_variable_add", var, RS::GlobalVariableType(variable_type->get_selected()), value);
  371. undo_redo->add_undo_method(RS::get_singleton(), "global_variable_remove", var);
  372. Dictionary gv;
  373. gv["type"] = global_var_type_names[variable_type->get_selected()];
  374. gv["value"] = value;
  375. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + var, gv);
  376. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + var, Variant());
  377. undo_redo->add_do_method(this, "_changed");
  378. undo_redo->add_undo_method(this, "_changed");
  379. undo_redo->commit_action();
  380. }
  381. void ShaderGlobalsEditor::_variable_deleted(const String &p_variable) {
  382. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  383. undo_redo->create_action(TTR("Add Shader Global Variable"));
  384. undo_redo->add_do_method(RS::get_singleton(), "global_variable_remove", p_variable);
  385. undo_redo->add_undo_method(RS::get_singleton(), "global_variable_add", p_variable, RS::get_singleton()->global_variable_get_type(p_variable), RS::get_singleton()->global_variable_get(p_variable));
  386. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, Variant());
  387. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, ProjectSettings::get_singleton()->get("shader_globals/" + p_variable));
  388. undo_redo->add_do_method(this, "_changed");
  389. undo_redo->add_undo_method(this, "_changed");
  390. undo_redo->commit_action();
  391. }
  392. void ShaderGlobalsEditor::_changed() {
  393. emit_signal(SNAME("globals_changed"));
  394. if (!interface->block_update) {
  395. interface->notify_property_list_changed();
  396. }
  397. }
  398. void ShaderGlobalsEditor::_bind_methods() {
  399. ClassDB::bind_method("_changed", &ShaderGlobalsEditor::_changed);
  400. ADD_SIGNAL(MethodInfo("globals_changed"));
  401. }
  402. void ShaderGlobalsEditor::_notification(int p_what) {
  403. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  404. if (is_visible_in_tree()) {
  405. inspector->edit(interface);
  406. }
  407. }
  408. if (p_what == NOTIFICATION_PREDELETE) {
  409. inspector->edit(nullptr);
  410. }
  411. }
  412. ShaderGlobalsEditor::ShaderGlobalsEditor() {
  413. HBoxContainer *add_menu_hb = memnew(HBoxContainer);
  414. add_child(add_menu_hb);
  415. add_menu_hb->add_child(memnew(Label(TTR("Name:"))));
  416. variable_name = memnew(LineEdit);
  417. variable_name->set_h_size_flags(SIZE_EXPAND_FILL);
  418. add_menu_hb->add_child(variable_name);
  419. add_menu_hb->add_child(memnew(Label(TTR("Type:"))));
  420. variable_type = memnew(OptionButton);
  421. variable_type->set_h_size_flags(SIZE_EXPAND_FILL);
  422. add_menu_hb->add_child(variable_type);
  423. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  424. variable_type->add_item(global_var_type_names[i]);
  425. }
  426. variable_add = memnew(Button(TTR("Add")));
  427. add_menu_hb->add_child(variable_add);
  428. variable_add->connect("pressed", callable_mp(this, &ShaderGlobalsEditor::_variable_added));
  429. inspector = memnew(EditorInspector);
  430. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  431. add_child(inspector);
  432. inspector->set_use_wide_editors(true);
  433. inspector->set_enable_capitalize_paths(false);
  434. inspector->set_use_deletable_properties(true);
  435. inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), varray(), CONNECT_DEFERRED);
  436. interface = memnew(ShaderGlobalsEditorInterface);
  437. interface->connect("var_changed", Callable(this, "_changed"));
  438. }
  439. ShaderGlobalsEditor::~ShaderGlobalsEditor() {
  440. memdelete(interface);
  441. }