shader_globals_editor.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. "samplerExternalOES",
  66. };
  67. class ShaderGlobalsEditorInterface : public Object {
  68. GDCLASS(ShaderGlobalsEditorInterface, Object)
  69. void _set_var(const StringName &p_name, const Variant &p_value, const Variant &p_prev_value) {
  70. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  71. undo_redo->create_action(TTR("Set Shader Global Variable"));
  72. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_set", p_name, p_value);
  73. undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_set", p_name, p_prev_value);
  74. RS::GlobalShaderParameterType type = RS::get_singleton()->global_shader_parameter_get_type(p_name);
  75. Dictionary gv;
  76. gv["type"] = global_var_type_names[type];
  77. if (type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  78. Ref<Resource> res = p_value;
  79. if (res.is_valid()) {
  80. gv["value"] = res->get_path();
  81. } else {
  82. gv["value"] = "";
  83. }
  84. } else {
  85. gv["value"] = p_value;
  86. }
  87. String path = "shader_globals/" + String(p_name);
  88. undo_redo->add_do_property(ProjectSettings::get_singleton(), path, gv);
  89. undo_redo->add_undo_property(ProjectSettings::get_singleton(), path, GLOBAL_GET(path));
  90. undo_redo->add_do_method(this, "_var_changed");
  91. undo_redo->add_undo_method(this, "_var_changed");
  92. block_update = true;
  93. undo_redo->commit_action();
  94. block_update = false;
  95. }
  96. void _var_changed() {
  97. emit_signal(SNAME("var_changed"));
  98. }
  99. protected:
  100. static void _bind_methods() {
  101. ClassDB::bind_method("_var_changed", &ShaderGlobalsEditorInterface::_var_changed);
  102. ADD_SIGNAL(MethodInfo("var_changed"));
  103. }
  104. bool _set(const StringName &p_name, const Variant &p_value) {
  105. Variant existing = RS::get_singleton()->global_shader_parameter_get(p_name);
  106. if (existing.get_type() == Variant::NIL) {
  107. return false;
  108. }
  109. callable_mp(this, &ShaderGlobalsEditorInterface::_set_var).call_deferred(p_name, p_value, existing);
  110. return true;
  111. }
  112. bool _get(const StringName &p_name, Variant &r_ret) const {
  113. r_ret = RS::get_singleton()->global_shader_parameter_get(p_name);
  114. return r_ret.get_type() != Variant::NIL;
  115. }
  116. void _get_property_list(List<PropertyInfo> *p_list) const {
  117. Vector<StringName> variables;
  118. variables = RS::get_singleton()->global_shader_parameter_get_list();
  119. for (int i = 0; i < variables.size(); i++) {
  120. PropertyInfo pinfo;
  121. pinfo.name = variables[i];
  122. switch (RS::get_singleton()->global_shader_parameter_get_type(variables[i])) {
  123. case RS::GLOBAL_VAR_TYPE_BOOL: {
  124. pinfo.type = Variant::BOOL;
  125. } break;
  126. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  127. pinfo.type = Variant::INT;
  128. pinfo.hint = PROPERTY_HINT_FLAGS;
  129. pinfo.hint_string = "x,y";
  130. } break;
  131. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  132. pinfo.type = Variant::INT;
  133. pinfo.hint = PROPERTY_HINT_FLAGS;
  134. pinfo.hint_string = "x,y,z";
  135. } break;
  136. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  137. pinfo.type = Variant::INT;
  138. pinfo.hint = PROPERTY_HINT_FLAGS;
  139. pinfo.hint_string = "x,y,z,w";
  140. } break;
  141. case RS::GLOBAL_VAR_TYPE_INT: {
  142. pinfo.type = Variant::INT;
  143. } break;
  144. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  145. pinfo.type = Variant::VECTOR2I;
  146. } break;
  147. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  148. pinfo.type = Variant::VECTOR3I;
  149. } break;
  150. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  151. pinfo.type = Variant::VECTOR4I;
  152. } break;
  153. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  154. pinfo.type = Variant::RECT2I;
  155. } break;
  156. case RS::GLOBAL_VAR_TYPE_UINT: {
  157. pinfo.type = Variant::INT;
  158. } break;
  159. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  160. pinfo.type = Variant::VECTOR2I;
  161. } break;
  162. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  163. pinfo.type = Variant::VECTOR3I;
  164. } break;
  165. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  166. pinfo.type = Variant::VECTOR4I;
  167. } break;
  168. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  169. pinfo.type = Variant::FLOAT;
  170. } break;
  171. case RS::GLOBAL_VAR_TYPE_VEC2: {
  172. pinfo.type = Variant::VECTOR2;
  173. } break;
  174. case RS::GLOBAL_VAR_TYPE_VEC3: {
  175. pinfo.type = Variant::VECTOR3;
  176. } break;
  177. case RS::GLOBAL_VAR_TYPE_VEC4: {
  178. pinfo.type = Variant::VECTOR4;
  179. } break;
  180. case RS::GLOBAL_VAR_TYPE_RECT2: {
  181. pinfo.type = Variant::RECT2;
  182. } break;
  183. case RS::GLOBAL_VAR_TYPE_COLOR: {
  184. pinfo.type = Variant::COLOR;
  185. } break;
  186. case RS::GLOBAL_VAR_TYPE_MAT2: {
  187. pinfo.type = Variant::PACKED_FLOAT32_ARRAY;
  188. } break;
  189. case RS::GLOBAL_VAR_TYPE_MAT3: {
  190. pinfo.type = Variant::BASIS;
  191. } break;
  192. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  193. pinfo.type = Variant::TRANSFORM2D;
  194. } break;
  195. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  196. pinfo.type = Variant::TRANSFORM3D;
  197. } break;
  198. case RS::GLOBAL_VAR_TYPE_MAT4: {
  199. pinfo.type = Variant::PROJECTION;
  200. } break;
  201. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  202. pinfo.type = Variant::OBJECT;
  203. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  204. pinfo.hint_string = "Texture2D";
  205. } break;
  206. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  207. pinfo.type = Variant::OBJECT;
  208. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  209. pinfo.hint_string = "Texture2DArray,CompressedTexture2DArray";
  210. } break;
  211. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  212. pinfo.type = Variant::OBJECT;
  213. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  214. pinfo.hint_string = "Texture3D";
  215. } break;
  216. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  217. pinfo.type = Variant::OBJECT;
  218. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  219. pinfo.hint_string = "Cubemap,CompressedCubemap";
  220. } break;
  221. case RS::GLOBAL_VAR_TYPE_SAMPLEREXT: {
  222. pinfo.type = Variant::OBJECT;
  223. pinfo.hint = PROPERTY_HINT_RESOURCE_TYPE;
  224. pinfo.hint_string = "ExternalTexture";
  225. } break;
  226. default: {
  227. } break;
  228. }
  229. p_list->push_back(pinfo);
  230. }
  231. }
  232. public:
  233. bool block_update = false;
  234. ShaderGlobalsEditorInterface() {
  235. }
  236. };
  237. static Variant create_var(RS::GlobalShaderParameterType p_type) {
  238. switch (p_type) {
  239. case RS::GLOBAL_VAR_TYPE_BOOL: {
  240. return false;
  241. }
  242. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  243. return 0; //bits
  244. }
  245. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  246. return 0; //bits
  247. }
  248. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  249. return 0; //bits
  250. }
  251. case RS::GLOBAL_VAR_TYPE_INT: {
  252. return 0; //bits
  253. }
  254. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  255. return Vector2i();
  256. }
  257. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  258. return Vector3i();
  259. }
  260. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  261. return Vector4i();
  262. }
  263. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  264. return Rect2i();
  265. }
  266. case RS::GLOBAL_VAR_TYPE_UINT: {
  267. return 0;
  268. }
  269. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  270. return Vector2i();
  271. }
  272. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  273. return Vector3i();
  274. }
  275. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  276. return Vector4i();
  277. }
  278. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  279. return 0.0;
  280. }
  281. case RS::GLOBAL_VAR_TYPE_VEC2: {
  282. return Vector2();
  283. }
  284. case RS::GLOBAL_VAR_TYPE_VEC3: {
  285. return Vector3();
  286. }
  287. case RS::GLOBAL_VAR_TYPE_VEC4: {
  288. return Vector4();
  289. }
  290. case RS::GLOBAL_VAR_TYPE_RECT2: {
  291. return Rect2();
  292. }
  293. case RS::GLOBAL_VAR_TYPE_COLOR: {
  294. return Color();
  295. }
  296. case RS::GLOBAL_VAR_TYPE_MAT2: {
  297. Vector<float> xform;
  298. xform.resize(4);
  299. xform.write[0] = 1;
  300. xform.write[1] = 0;
  301. xform.write[2] = 0;
  302. xform.write[3] = 1;
  303. return xform;
  304. }
  305. case RS::GLOBAL_VAR_TYPE_MAT3: {
  306. return Basis();
  307. }
  308. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  309. return Transform2D();
  310. }
  311. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  312. return Transform3D();
  313. }
  314. case RS::GLOBAL_VAR_TYPE_MAT4: {
  315. return Projection();
  316. }
  317. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  318. return "";
  319. }
  320. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  321. return "";
  322. }
  323. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  324. return "";
  325. }
  326. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  327. return "";
  328. }
  329. case RS::GLOBAL_VAR_TYPE_SAMPLEREXT: {
  330. return "";
  331. }
  332. default: {
  333. return Variant();
  334. }
  335. }
  336. }
  337. String ShaderGlobalsEditor::_check_new_variable_name(const String &p_variable_name) {
  338. if (p_variable_name.is_empty()) {
  339. return TTR("Name cannot be empty.");
  340. }
  341. if (!p_variable_name.is_valid_ascii_identifier()) {
  342. return TTR("Name must be a valid identifier.");
  343. }
  344. return "";
  345. }
  346. LineEdit *ShaderGlobalsEditor::get_name_box() const {
  347. return variable_name;
  348. }
  349. void ShaderGlobalsEditor::_variable_name_text_changed(const String &p_variable_name) {
  350. const String &warning = _check_new_variable_name(p_variable_name.strip_edges());
  351. variable_add->set_tooltip_text(warning);
  352. variable_add->set_disabled(!warning.is_empty());
  353. }
  354. void ShaderGlobalsEditor::_variable_added() {
  355. String var = variable_name->get_text().strip_edges();
  356. if (RenderingServer::get_singleton()->global_shader_parameter_get(var).get_type() != Variant::NIL) {
  357. EditorNode::get_singleton()->show_warning(vformat(TTR("Global shader parameter '%s' already exists."), var));
  358. return;
  359. }
  360. List<String> keywords;
  361. ShaderLanguage::get_keyword_list(&keywords);
  362. if (keywords.find(var) != nullptr || var == "script") {
  363. EditorNode::get_singleton()->show_warning(vformat(TTR("Name '%s' is a reserved shader language keyword."), var));
  364. return;
  365. }
  366. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  367. Variant value = create_var(RS::GlobalShaderParameterType(variable_type->get_selected()));
  368. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  369. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_add", var, RS::GlobalShaderParameterType(variable_type->get_selected()), value);
  370. undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_remove", var);
  371. Dictionary gv;
  372. gv["type"] = global_var_type_names[variable_type->get_selected()];
  373. gv["value"] = value;
  374. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + var, gv);
  375. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + var, Variant());
  376. undo_redo->add_do_method(this, "_changed");
  377. undo_redo->add_undo_method(this, "_changed");
  378. undo_redo->commit_action();
  379. variable_name->clear();
  380. }
  381. void ShaderGlobalsEditor::_variable_deleted(const String &p_variable) {
  382. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  383. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  384. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_remove", p_variable);
  385. 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));
  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, GLOBAL_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. switch (p_what) {
  404. case NOTIFICATION_VISIBILITY_CHANGED: {
  405. if (is_visible_in_tree()) {
  406. inspector->edit(interface);
  407. }
  408. } break;
  409. case NOTIFICATION_THEME_CHANGED: {
  410. variable_add->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  411. } break;
  412. case NOTIFICATION_PREDELETE: {
  413. inspector->edit(nullptr);
  414. } break;
  415. }
  416. }
  417. ShaderGlobalsEditor::ShaderGlobalsEditor() {
  418. ProjectSettings::get_singleton()->add_hidden_prefix("shader_globals/");
  419. HBoxContainer *add_menu_hb = memnew(HBoxContainer);
  420. add_child(add_menu_hb);
  421. add_menu_hb->add_child(memnew(Label(TTR("Name:"))));
  422. variable_name = memnew(LineEdit);
  423. variable_name->set_h_size_flags(SIZE_EXPAND_FILL);
  424. variable_name->set_clear_button_enabled(true);
  425. variable_name->connect(SceneStringName(text_changed), callable_mp(this, &ShaderGlobalsEditor::_variable_name_text_changed));
  426. variable_name->connect(SceneStringName(text_submitted), callable_mp(this, &ShaderGlobalsEditor::_variable_added).unbind(1));
  427. add_menu_hb->add_child(variable_name);
  428. add_menu_hb->add_child(memnew(Label(TTR("Type:"))));
  429. variable_type = memnew(OptionButton);
  430. variable_type->set_h_size_flags(SIZE_EXPAND_FILL);
  431. add_menu_hb->add_child(variable_type);
  432. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  433. variable_type->add_item(global_var_type_names[i]);
  434. }
  435. variable_add = memnew(Button(TTR("Add")));
  436. variable_add->set_disabled(true);
  437. add_menu_hb->add_child(variable_add);
  438. variable_add->connect(SceneStringName(pressed), callable_mp(this, &ShaderGlobalsEditor::_variable_added));
  439. inspector = memnew(EditorInspector);
  440. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  441. add_child(inspector);
  442. inspector->set_use_wide_editors(true);
  443. inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
  444. inspector->set_use_deletable_properties(true);
  445. inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), CONNECT_DEFERRED);
  446. interface = memnew(ShaderGlobalsEditorInterface);
  447. interface->connect("var_changed", callable_mp(this, &ShaderGlobalsEditor::_changed));
  448. }
  449. ShaderGlobalsEditor::~ShaderGlobalsEditor() {
  450. memdelete(interface);
  451. }