shader_globals_editor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /**************************************************************************/
  2. /* shader_globals_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/config/project_settings.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "servers/rendering/shader_language.h"
  35. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  36. "bool",
  37. "bvec2",
  38. "bvec3",
  39. "bvec4",
  40. "int",
  41. "ivec2",
  42. "ivec3",
  43. "ivec4",
  44. "rect2i",
  45. "uint",
  46. "uvec2",
  47. "uvec3",
  48. "uvec4",
  49. "float",
  50. "vec2",
  51. "vec3",
  52. "vec4",
  53. "color",
  54. "rect2",
  55. "mat2",
  56. "mat3",
  57. "mat4",
  58. "transform_2d",
  59. "transform",
  60. "sampler2D",
  61. "sampler2DArray",
  62. "sampler3D",
  63. "samplerCube",
  64. };
  65. class ShaderGlobalsEditorInterface : public Object {
  66. GDCLASS(ShaderGlobalsEditorInterface, Object)
  67. void _set_var(const StringName &p_name, const Variant &p_value, const Variant &p_prev_value) {
  68. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  69. undo_redo->create_action(TTR("Set Shader Global Variable"));
  70. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_set", p_name, p_value);
  71. undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_set", p_name, p_prev_value);
  72. RS::GlobalShaderParameterType type = RS::get_singleton()->global_shader_parameter_get_type(p_name);
  73. Dictionary gv;
  74. gv["type"] = global_var_type_names[type];
  75. if (type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  76. Ref<Resource> res = p_value;
  77. if (res.is_valid()) {
  78. gv["value"] = res->get_path();
  79. } else {
  80. gv["value"] = "";
  81. }
  82. } else {
  83. gv["value"] = p_value;
  84. }
  85. String path = "shader_globals/" + String(p_name);
  86. undo_redo->add_do_property(ProjectSettings::get_singleton(), path, gv);
  87. undo_redo->add_undo_property(ProjectSettings::get_singleton(), path, GLOBAL_GET(path));
  88. undo_redo->add_do_method(this, "_var_changed");
  89. undo_redo->add_undo_method(this, "_var_changed");
  90. block_update = true;
  91. undo_redo->commit_action();
  92. block_update = false;
  93. }
  94. void _var_changed() {
  95. emit_signal(SNAME("var_changed"));
  96. }
  97. protected:
  98. static void _bind_methods() {
  99. ClassDB::bind_method("_var_changed", &ShaderGlobalsEditorInterface::_var_changed);
  100. ADD_SIGNAL(MethodInfo("var_changed"));
  101. }
  102. bool _set(const StringName &p_name, const Variant &p_value) {
  103. Variant existing = RS::get_singleton()->global_shader_parameter_get(p_name);
  104. if (existing.get_type() == Variant::NIL) {
  105. return false;
  106. }
  107. callable_mp(this, &ShaderGlobalsEditorInterface::_set_var).call_deferred(p_name, p_value, existing);
  108. return true;
  109. }
  110. bool _get(const StringName &p_name, Variant &r_ret) const {
  111. r_ret = RS::get_singleton()->global_shader_parameter_get(p_name);
  112. return r_ret.get_type() != Variant::NIL;
  113. }
  114. void _get_property_list(List<PropertyInfo> *p_list) const {
  115. Vector<StringName> variables;
  116. variables = RS::get_singleton()->global_shader_parameter_get_list();
  117. for (int i = 0; i < variables.size(); i++) {
  118. PropertyInfo pinfo;
  119. pinfo.name = variables[i];
  120. switch (RS::get_singleton()->global_shader_parameter_get_type(variables[i])) {
  121. case RS::GLOBAL_VAR_TYPE_BOOL: {
  122. pinfo.type = Variant::BOOL;
  123. } break;
  124. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  125. pinfo.type = Variant::INT;
  126. pinfo.hint = PROPERTY_HINT_FLAGS;
  127. pinfo.hint_string = "x,y";
  128. } break;
  129. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  130. pinfo.type = Variant::INT;
  131. pinfo.hint = PROPERTY_HINT_FLAGS;
  132. pinfo.hint_string = "x,y,z";
  133. } break;
  134. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  135. pinfo.type = Variant::INT;
  136. pinfo.hint = PROPERTY_HINT_FLAGS;
  137. pinfo.hint_string = "x,y,z,w";
  138. } break;
  139. case RS::GLOBAL_VAR_TYPE_INT: {
  140. pinfo.type = Variant::INT;
  141. } break;
  142. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  143. pinfo.type = Variant::VECTOR2I;
  144. } break;
  145. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  146. pinfo.type = Variant::VECTOR3I;
  147. } break;
  148. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  149. pinfo.type = Variant::VECTOR4I;
  150. } break;
  151. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  152. pinfo.type = Variant::RECT2I;
  153. } break;
  154. case RS::GLOBAL_VAR_TYPE_UINT: {
  155. pinfo.type = Variant::INT;
  156. } break;
  157. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  158. pinfo.type = Variant::VECTOR2I;
  159. } break;
  160. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  161. pinfo.type = Variant::VECTOR3I;
  162. } break;
  163. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  164. pinfo.type = Variant::VECTOR4I;
  165. } break;
  166. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  167. pinfo.type = Variant::FLOAT;
  168. } break;
  169. case RS::GLOBAL_VAR_TYPE_VEC2: {
  170. pinfo.type = Variant::VECTOR2;
  171. } break;
  172. case RS::GLOBAL_VAR_TYPE_VEC3: {
  173. pinfo.type = Variant::VECTOR3;
  174. } break;
  175. case RS::GLOBAL_VAR_TYPE_VEC4: {
  176. pinfo.type = Variant::VECTOR4;
  177. } break;
  178. case RS::GLOBAL_VAR_TYPE_RECT2: {
  179. pinfo.type = Variant::RECT2;
  180. } break;
  181. case RS::GLOBAL_VAR_TYPE_COLOR: {
  182. pinfo.type = Variant::COLOR;
  183. } break;
  184. case RS::GLOBAL_VAR_TYPE_MAT2: {
  185. pinfo.type = Variant::PACKED_FLOAT32_ARRAY;
  186. } break;
  187. case RS::GLOBAL_VAR_TYPE_MAT3: {
  188. pinfo.type = Variant::BASIS;
  189. } break;
  190. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  191. pinfo.type = Variant::TRANSFORM2D;
  192. } break;
  193. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  194. pinfo.type = Variant::TRANSFORM3D;
  195. } break;
  196. case RS::GLOBAL_VAR_TYPE_MAT4: {
  197. pinfo.type = Variant::PROJECTION;
  198. } break;
  199. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  200. pinfo.type = Variant::OBJECT;
  201. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  202. pinfo.hint_string = "Texture2D";
  203. } break;
  204. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  205. pinfo.type = Variant::OBJECT;
  206. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  207. pinfo.hint_string = "Texture2DArray,CompressedTexture2DArray";
  208. } break;
  209. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  210. pinfo.type = Variant::OBJECT;
  211. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  212. pinfo.hint_string = "Texture3D";
  213. } break;
  214. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  215. pinfo.type = Variant::OBJECT;
  216. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  217. pinfo.hint_string = "Cubemap,CompressedCubemap";
  218. } break;
  219. default: {
  220. } break;
  221. }
  222. p_list->push_back(pinfo);
  223. }
  224. }
  225. public:
  226. bool block_update = false;
  227. ShaderGlobalsEditorInterface() {
  228. }
  229. };
  230. static Variant create_var(RS::GlobalShaderParameterType p_type) {
  231. switch (p_type) {
  232. case RS::GLOBAL_VAR_TYPE_BOOL: {
  233. return false;
  234. }
  235. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  236. return 0; //bits
  237. }
  238. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  239. return 0; //bits
  240. }
  241. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  242. return 0; //bits
  243. }
  244. case RS::GLOBAL_VAR_TYPE_INT: {
  245. return 0; //bits
  246. }
  247. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  248. return Vector2i();
  249. }
  250. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  251. return Vector3i();
  252. }
  253. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  254. return Vector4i();
  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. return Vector4i();
  270. }
  271. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  272. return 0.0;
  273. }
  274. case RS::GLOBAL_VAR_TYPE_VEC2: {
  275. return Vector2();
  276. }
  277. case RS::GLOBAL_VAR_TYPE_VEC3: {
  278. return Vector3();
  279. }
  280. case RS::GLOBAL_VAR_TYPE_VEC4: {
  281. return Vector4();
  282. }
  283. case RS::GLOBAL_VAR_TYPE_RECT2: {
  284. return Rect2();
  285. }
  286. case RS::GLOBAL_VAR_TYPE_COLOR: {
  287. return Color();
  288. }
  289. case RS::GLOBAL_VAR_TYPE_MAT2: {
  290. Vector<float> xform;
  291. xform.resize(4);
  292. xform.write[0] = 1;
  293. xform.write[1] = 0;
  294. xform.write[2] = 0;
  295. xform.write[3] = 1;
  296. return xform;
  297. }
  298. case RS::GLOBAL_VAR_TYPE_MAT3: {
  299. return Basis();
  300. }
  301. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  302. return Transform2D();
  303. }
  304. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  305. return Transform3D();
  306. }
  307. case RS::GLOBAL_VAR_TYPE_MAT4: {
  308. return Projection();
  309. }
  310. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  311. return "";
  312. }
  313. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  314. return "";
  315. }
  316. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  317. return "";
  318. }
  319. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  320. return "";
  321. }
  322. default: {
  323. return Variant();
  324. }
  325. }
  326. }
  327. String ShaderGlobalsEditor::_check_new_variable_name(const String &p_variable_name) {
  328. if (p_variable_name.is_empty()) {
  329. return TTR("Name cannot be empty.");
  330. }
  331. if (!p_variable_name.is_valid_identifier()) {
  332. return TTR("Name must be a valid identifier.");
  333. }
  334. return "";
  335. }
  336. LineEdit *ShaderGlobalsEditor::get_name_box() const {
  337. return variable_name;
  338. }
  339. void ShaderGlobalsEditor::_variable_name_text_changed(const String &p_variable_name) {
  340. const String &warning = _check_new_variable_name(p_variable_name.strip_edges());
  341. variable_add->set_tooltip_text(warning);
  342. variable_add->set_disabled(!warning.is_empty());
  343. }
  344. void ShaderGlobalsEditor::_variable_added() {
  345. String var = variable_name->get_text().strip_edges();
  346. if (RenderingServer::get_singleton()->global_shader_parameter_get(var).get_type() != Variant::NIL) {
  347. EditorNode::get_singleton()->show_warning(vformat(TTR("Global shader parameter '%s' already exists."), var));
  348. return;
  349. }
  350. List<String> keywords;
  351. ShaderLanguage::get_keyword_list(&keywords);
  352. if (keywords.find(var) != nullptr || var == "script") {
  353. EditorNode::get_singleton()->show_warning(vformat(TTR("Name '%s' is a reserved shader language keyword."), var));
  354. return;
  355. }
  356. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  357. Variant value = create_var(RS::GlobalShaderParameterType(variable_type->get_selected()));
  358. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  359. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_add", var, RS::GlobalShaderParameterType(variable_type->get_selected()), value);
  360. undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_remove", var);
  361. Dictionary gv;
  362. gv["type"] = global_var_type_names[variable_type->get_selected()];
  363. gv["value"] = value;
  364. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + var, gv);
  365. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + var, Variant());
  366. undo_redo->add_do_method(this, "_changed");
  367. undo_redo->add_undo_method(this, "_changed");
  368. undo_redo->commit_action();
  369. variable_name->clear();
  370. }
  371. void ShaderGlobalsEditor::_variable_deleted(const String &p_variable) {
  372. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  373. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  374. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_remove", p_variable);
  375. undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_add", p_variable, RS::get_singleton()->global_shader_parameter_get_type(p_variable), RS::get_singleton()->global_shader_parameter_get(p_variable));
  376. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, Variant());
  377. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, GLOBAL_GET("shader_globals/" + p_variable));
  378. undo_redo->add_do_method(this, "_changed");
  379. undo_redo->add_undo_method(this, "_changed");
  380. undo_redo->commit_action();
  381. }
  382. void ShaderGlobalsEditor::_changed() {
  383. emit_signal(SNAME("globals_changed"));
  384. if (!interface->block_update) {
  385. interface->notify_property_list_changed();
  386. }
  387. }
  388. void ShaderGlobalsEditor::_bind_methods() {
  389. ClassDB::bind_method("_changed", &ShaderGlobalsEditor::_changed);
  390. ADD_SIGNAL(MethodInfo("globals_changed"));
  391. }
  392. void ShaderGlobalsEditor::_notification(int p_what) {
  393. switch (p_what) {
  394. case NOTIFICATION_VISIBILITY_CHANGED: {
  395. if (is_visible_in_tree()) {
  396. inspector->edit(interface);
  397. }
  398. } break;
  399. case NOTIFICATION_THEME_CHANGED: {
  400. variable_add->set_icon(get_editor_theme_icon(SNAME("Add")));
  401. } break;
  402. case NOTIFICATION_PREDELETE: {
  403. inspector->edit(nullptr);
  404. } break;
  405. }
  406. }
  407. ShaderGlobalsEditor::ShaderGlobalsEditor() {
  408. ProjectSettings::get_singleton()->add_hidden_prefix("shader_globals/");
  409. HBoxContainer *add_menu_hb = memnew(HBoxContainer);
  410. add_child(add_menu_hb);
  411. add_menu_hb->add_child(memnew(Label(TTR("Name:"))));
  412. variable_name = memnew(LineEdit);
  413. variable_name->set_h_size_flags(SIZE_EXPAND_FILL);
  414. variable_name->set_clear_button_enabled(true);
  415. variable_name->connect("text_changed", callable_mp(this, &ShaderGlobalsEditor::_variable_name_text_changed));
  416. variable_name->connect("text_submitted", callable_mp(this, &ShaderGlobalsEditor::_variable_added).unbind(1));
  417. add_menu_hb->add_child(variable_name);
  418. add_menu_hb->add_child(memnew(Label(TTR("Type:"))));
  419. variable_type = memnew(OptionButton);
  420. variable_type->set_h_size_flags(SIZE_EXPAND_FILL);
  421. add_menu_hb->add_child(variable_type);
  422. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  423. variable_type->add_item(global_var_type_names[i]);
  424. }
  425. variable_add = memnew(Button(TTR("Add")));
  426. variable_add->set_disabled(true);
  427. add_menu_hb->add_child(variable_add);
  428. variable_add->connect(SceneStringName(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_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
  434. inspector->set_use_deletable_properties(true);
  435. inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), CONNECT_DEFERRED);
  436. interface = memnew(ShaderGlobalsEditorInterface);
  437. interface->connect("var_changed", callable_mp(this, &ShaderGlobalsEditor::_changed));
  438. }
  439. ShaderGlobalsEditor::~ShaderGlobalsEditor() {
  440. memdelete(interface);
  441. }