shader_globals_editor.cpp 16 KB

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