shader_globals_editor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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("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("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. print_line("all good?");
  103. return true;
  104. }
  105. bool _get(const StringName &p_name, Variant &r_ret) const {
  106. r_ret = RS::get_singleton()->global_variable_get(p_name);
  107. return r_ret.get_type() != Variant::NIL;
  108. }
  109. void _get_property_list(List<PropertyInfo> *p_list) const {
  110. Vector<StringName> variables;
  111. variables = RS::get_singleton()->global_variable_get_list();
  112. for (int i = 0; i < variables.size(); i++) {
  113. PropertyInfo pinfo;
  114. pinfo.name = variables[i];
  115. switch (RS::get_singleton()->global_variable_get_type(variables[i])) {
  116. case RS::GLOBAL_VAR_TYPE_BOOL: {
  117. pinfo.type = Variant::BOOL;
  118. } break;
  119. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  120. pinfo.type = Variant::INT;
  121. pinfo.hint = PROPERTY_HINT_FLAGS;
  122. pinfo.hint_string = "x,y";
  123. } break;
  124. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  125. pinfo.type = Variant::INT;
  126. pinfo.hint = PROPERTY_HINT_FLAGS;
  127. pinfo.hint_string = "x,y,z";
  128. } break;
  129. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  130. pinfo.type = Variant::INT;
  131. pinfo.hint = PROPERTY_HINT_FLAGS;
  132. pinfo.hint_string = "x,y,z,w";
  133. } break;
  134. case RS::GLOBAL_VAR_TYPE_INT: {
  135. pinfo.type = Variant::INT;
  136. } break;
  137. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  138. pinfo.type = Variant::VECTOR2I;
  139. } break;
  140. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  141. pinfo.type = Variant::VECTOR3I;
  142. } break;
  143. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  144. pinfo.type = Variant::PACKED_INT32_ARRAY;
  145. } break;
  146. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  147. pinfo.type = Variant::RECT2I;
  148. } break;
  149. case RS::GLOBAL_VAR_TYPE_UINT: {
  150. pinfo.type = Variant::INT;
  151. } break;
  152. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  153. pinfo.type = Variant::VECTOR2I;
  154. } break;
  155. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  156. pinfo.type = Variant::VECTOR3I;
  157. } break;
  158. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  159. pinfo.type = Variant::PACKED_INT32_ARRAY;
  160. } break;
  161. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  162. pinfo.type = Variant::FLOAT;
  163. } break;
  164. case RS::GLOBAL_VAR_TYPE_VEC2: {
  165. pinfo.type = Variant::VECTOR2;
  166. } break;
  167. case RS::GLOBAL_VAR_TYPE_VEC3: {
  168. pinfo.type = Variant::VECTOR3;
  169. } break;
  170. case RS::GLOBAL_VAR_TYPE_VEC4: {
  171. pinfo.type = Variant::PLANE;
  172. } break;
  173. case RS::GLOBAL_VAR_TYPE_RECT2: {
  174. pinfo.type = Variant::RECT2;
  175. } break;
  176. case RS::GLOBAL_VAR_TYPE_COLOR: {
  177. pinfo.type = Variant::COLOR;
  178. } break;
  179. case RS::GLOBAL_VAR_TYPE_MAT2: {
  180. pinfo.type = Variant::PACKED_INT32_ARRAY;
  181. } break;
  182. case RS::GLOBAL_VAR_TYPE_MAT3: {
  183. pinfo.type = Variant::BASIS;
  184. } break;
  185. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  186. pinfo.type = Variant::TRANSFORM2D;
  187. } break;
  188. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  189. pinfo.type = Variant::TRANSFORM;
  190. } break;
  191. case RS::GLOBAL_VAR_TYPE_MAT4: {
  192. pinfo.type = Variant::PACKED_INT32_ARRAY;
  193. } break;
  194. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  195. pinfo.type = Variant::OBJECT;
  196. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  197. pinfo.hint_string = "Texture2D";
  198. } break;
  199. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  200. pinfo.type = Variant::OBJECT;
  201. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  202. pinfo.hint_string = "Texture2DArray";
  203. } break;
  204. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  205. pinfo.type = Variant::OBJECT;
  206. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  207. pinfo.hint_string = "Texture3D";
  208. } break;
  209. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  210. pinfo.type = Variant::OBJECT;
  211. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  212. pinfo.hint_string = "Cubemap";
  213. } break;
  214. default: {
  215. } break;
  216. }
  217. p_list->push_back(pinfo);
  218. }
  219. }
  220. public:
  221. bool block_update = false;
  222. ShaderGlobalsEditorInterface() {
  223. }
  224. };
  225. static Variant create_var(RS::GlobalVariableType p_type) {
  226. switch (p_type) {
  227. case RS::GLOBAL_VAR_TYPE_BOOL: {
  228. return false;
  229. }
  230. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  231. return 0; //bits
  232. }
  233. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  234. return 0; //bits
  235. }
  236. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  237. return 0; //bits
  238. }
  239. case RS::GLOBAL_VAR_TYPE_INT: {
  240. return 0; //bits
  241. }
  242. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  243. return Vector2i();
  244. }
  245. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  246. return Vector3i();
  247. }
  248. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  249. Vector<int> v4;
  250. v4.resize(4);
  251. v4.write[0] = 0;
  252. v4.write[1] = 0;
  253. v4.write[2] = 0;
  254. v4.write[3] = 0;
  255. return v4;
  256. }
  257. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  258. return Rect2i();
  259. }
  260. case RS::GLOBAL_VAR_TYPE_UINT: {
  261. return 0;
  262. }
  263. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  264. return Vector2i();
  265. }
  266. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  267. return Vector3i();
  268. }
  269. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  270. return Rect2i();
  271. }
  272. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  273. return 0.0;
  274. }
  275. case RS::GLOBAL_VAR_TYPE_VEC2: {
  276. return Vector2();
  277. }
  278. case RS::GLOBAL_VAR_TYPE_VEC3: {
  279. return Vector3();
  280. }
  281. case RS::GLOBAL_VAR_TYPE_VEC4: {
  282. return Plane();
  283. }
  284. case RS::GLOBAL_VAR_TYPE_RECT2: {
  285. return Rect2();
  286. }
  287. case RS::GLOBAL_VAR_TYPE_COLOR: {
  288. return Color();
  289. }
  290. case RS::GLOBAL_VAR_TYPE_MAT2: {
  291. Vector<real_t> xform;
  292. xform.resize(4);
  293. xform.write[0] = 1;
  294. xform.write[1] = 0;
  295. xform.write[2] = 0;
  296. xform.write[3] = 1;
  297. return xform;
  298. }
  299. case RS::GLOBAL_VAR_TYPE_MAT3: {
  300. return Basis();
  301. }
  302. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  303. return Transform2D();
  304. }
  305. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  306. return Transform();
  307. }
  308. case RS::GLOBAL_VAR_TYPE_MAT4: {
  309. Vector<real_t> xform;
  310. xform.resize(4);
  311. xform.write[0] = 1;
  312. xform.write[1] = 0;
  313. xform.write[2] = 0;
  314. xform.write[3] = 0;
  315. xform.write[4] = 0;
  316. xform.write[5] = 1;
  317. xform.write[6] = 0;
  318. xform.write[7] = 0;
  319. xform.write[8] = 0;
  320. xform.write[9] = 0;
  321. xform.write[10] = 1;
  322. xform.write[11] = 0;
  323. xform.write[12] = 0;
  324. xform.write[13] = 0;
  325. xform.write[14] = 0;
  326. xform.write[15] = 1;
  327. return xform;
  328. }
  329. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  330. return "";
  331. }
  332. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  333. return "";
  334. }
  335. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  336. return "";
  337. }
  338. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  339. return "";
  340. }
  341. default: {
  342. return Variant();
  343. }
  344. }
  345. }
  346. void ShaderGlobalsEditor::_variable_added() {
  347. String var = variable_name->get_text().strip_edges();
  348. if (var == "" || !var.is_valid_identifier()) {
  349. EditorNode::get_singleton()->show_warning(TTR("Please specify a valid variable identifier name."));
  350. return;
  351. }
  352. if (RenderingServer::get_singleton()->global_variable_get(var).get_type() != Variant::NIL) {
  353. EditorNode::get_singleton()->show_warning(vformat(TTR("Global variable '%s' already exists'"), var));
  354. return;
  355. }
  356. List<String> keywords;
  357. ShaderLanguage::get_keyword_list(&keywords);
  358. if (keywords.find(var) != nullptr || var == "script") {
  359. EditorNode::get_singleton()->show_warning(vformat(TTR("Name '%s' is a reserved shader language keyword."), var));
  360. return;
  361. }
  362. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  363. Variant value = create_var(RS::GlobalVariableType(variable_type->get_selected()));
  364. undo_redo->create_action("Add Shader Global Variable");
  365. undo_redo->add_do_method(RS::get_singleton(), "global_variable_add", var, RS::GlobalVariableType(variable_type->get_selected()), value);
  366. undo_redo->add_undo_method(RS::get_singleton(), "global_variable_remove", var);
  367. Dictionary gv;
  368. gv["type"] = global_var_type_names[variable_type->get_selected()];
  369. gv["value"] = value;
  370. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + var, gv);
  371. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + var, Variant());
  372. undo_redo->add_do_method(this, "_changed");
  373. undo_redo->add_undo_method(this, "_changed");
  374. undo_redo->commit_action();
  375. }
  376. void ShaderGlobalsEditor::_variable_deleted(const String &p_variable) {
  377. print_line("deleted " + p_variable);
  378. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  379. undo_redo->create_action("Add Shader Global Variable");
  380. undo_redo->add_do_method(RS::get_singleton(), "global_variable_remove", p_variable);
  381. 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));
  382. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, Variant());
  383. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, ProjectSettings::get_singleton()->get("shader_globals/" + p_variable));
  384. undo_redo->add_do_method(this, "_changed");
  385. undo_redo->add_undo_method(this, "_changed");
  386. undo_redo->commit_action();
  387. }
  388. void ShaderGlobalsEditor::_changed() {
  389. emit_signal("globals_changed");
  390. if (!interface->block_update) {
  391. interface->_change_notify();
  392. }
  393. }
  394. void ShaderGlobalsEditor::_bind_methods() {
  395. ClassDB::bind_method("_changed", &ShaderGlobalsEditor::_changed);
  396. ADD_SIGNAL(MethodInfo("globals_changed"));
  397. }
  398. void ShaderGlobalsEditor::_notification(int p_what) {
  399. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  400. if (is_visible_in_tree()) {
  401. print_line("OK load settings in globalseditor");
  402. inspector->edit(interface);
  403. }
  404. }
  405. }
  406. ShaderGlobalsEditor::ShaderGlobalsEditor() {
  407. HBoxContainer *add_menu_hb = memnew(HBoxContainer);
  408. add_child(add_menu_hb);
  409. add_menu_hb->add_child(memnew(Label(TTR("Name:"))));
  410. variable_name = memnew(LineEdit);
  411. variable_name->set_h_size_flags(SIZE_EXPAND_FILL);
  412. add_menu_hb->add_child(variable_name);
  413. add_menu_hb->add_child(memnew(Label(TTR("Type:"))));
  414. variable_type = memnew(OptionButton);
  415. variable_type->set_h_size_flags(SIZE_EXPAND_FILL);
  416. add_menu_hb->add_child(variable_type);
  417. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  418. variable_type->add_item(global_var_type_names[i]);
  419. }
  420. variable_add = memnew(Button(TTR("Add")));
  421. add_menu_hb->add_child(variable_add);
  422. variable_add->connect("pressed", callable_mp(this, &ShaderGlobalsEditor::_variable_added));
  423. inspector = memnew(EditorInspector);
  424. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  425. add_child(inspector);
  426. inspector->set_use_wide_editors(true);
  427. inspector->set_enable_capitalize_paths(false);
  428. inspector->set_use_deletable_properties(true);
  429. inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), varray(), CONNECT_DEFERRED);
  430. interface = memnew(ShaderGlobalsEditorInterface);
  431. interface->connect("var_changed", Callable(this, "_changed"));
  432. }
  433. ShaderGlobalsEditor::~ShaderGlobalsEditor() {
  434. inspector->edit(nullptr);
  435. memdelete(interface);
  436. }