shader_globals_editor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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("_set_var", &ShaderGlobalsEditorInterface::_set_var);
  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. call_deferred("_set_var", 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";
  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";
  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. void ShaderGlobalsEditor::_variable_added() {
  329. String var = variable_name->get_text().strip_edges();
  330. if (var.is_empty() || !var.is_valid_identifier()) {
  331. EditorNode::get_singleton()->show_warning(TTR("Please specify a valid shader uniform identifier name."));
  332. return;
  333. }
  334. if (RenderingServer::get_singleton()->global_shader_parameter_get(var).get_type() != Variant::NIL) {
  335. EditorNode::get_singleton()->show_warning(vformat(TTR("Global shader parameter '%s' already exists'"), var));
  336. return;
  337. }
  338. List<String> keywords;
  339. ShaderLanguage::get_keyword_list(&keywords);
  340. if (keywords.find(var) != nullptr || var == "script") {
  341. EditorNode::get_singleton()->show_warning(vformat(TTR("Name '%s' is a reserved shader language keyword."), var));
  342. return;
  343. }
  344. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  345. Variant value = create_var(RS::GlobalShaderParameterType(variable_type->get_selected()));
  346. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  347. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_add", var, RS::GlobalShaderParameterType(variable_type->get_selected()), value);
  348. undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_remove", var);
  349. Dictionary gv;
  350. gv["type"] = global_var_type_names[variable_type->get_selected()];
  351. gv["value"] = value;
  352. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + var, gv);
  353. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + var, Variant());
  354. undo_redo->add_do_method(this, "_changed");
  355. undo_redo->add_undo_method(this, "_changed");
  356. undo_redo->commit_action();
  357. }
  358. void ShaderGlobalsEditor::_variable_deleted(const String &p_variable) {
  359. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  360. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  361. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_remove", p_variable);
  362. 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));
  363. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, Variant());
  364. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, GLOBAL_GET("shader_globals/" + p_variable));
  365. undo_redo->add_do_method(this, "_changed");
  366. undo_redo->add_undo_method(this, "_changed");
  367. undo_redo->commit_action();
  368. }
  369. void ShaderGlobalsEditor::_changed() {
  370. emit_signal(SNAME("globals_changed"));
  371. if (!interface->block_update) {
  372. interface->notify_property_list_changed();
  373. }
  374. }
  375. void ShaderGlobalsEditor::_bind_methods() {
  376. ClassDB::bind_method("_changed", &ShaderGlobalsEditor::_changed);
  377. ADD_SIGNAL(MethodInfo("globals_changed"));
  378. }
  379. void ShaderGlobalsEditor::_notification(int p_what) {
  380. switch (p_what) {
  381. case NOTIFICATION_VISIBILITY_CHANGED: {
  382. if (is_visible_in_tree()) {
  383. inspector->edit(interface);
  384. }
  385. } break;
  386. case NOTIFICATION_PREDELETE: {
  387. inspector->edit(nullptr);
  388. } break;
  389. }
  390. }
  391. ShaderGlobalsEditor::ShaderGlobalsEditor() {
  392. ProjectSettings::get_singleton()->add_hidden_prefix("shader_globals/");
  393. HBoxContainer *add_menu_hb = memnew(HBoxContainer);
  394. add_child(add_menu_hb);
  395. add_menu_hb->add_child(memnew(Label(TTR("Name:"))));
  396. variable_name = memnew(LineEdit);
  397. variable_name->set_h_size_flags(SIZE_EXPAND_FILL);
  398. add_menu_hb->add_child(variable_name);
  399. add_menu_hb->add_child(memnew(Label(TTR("Type:"))));
  400. variable_type = memnew(OptionButton);
  401. variable_type->set_h_size_flags(SIZE_EXPAND_FILL);
  402. add_menu_hb->add_child(variable_type);
  403. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  404. variable_type->add_item(global_var_type_names[i]);
  405. }
  406. variable_add = memnew(Button(TTR("Add")));
  407. add_menu_hb->add_child(variable_add);
  408. variable_add->connect("pressed", callable_mp(this, &ShaderGlobalsEditor::_variable_added));
  409. inspector = memnew(EditorInspector);
  410. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  411. add_child(inspector);
  412. inspector->set_use_wide_editors(true);
  413. inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
  414. inspector->set_use_deletable_properties(true);
  415. inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), CONNECT_DEFERRED);
  416. interface = memnew(ShaderGlobalsEditorInterface);
  417. interface->connect("var_changed", callable_mp(this, &ShaderGlobalsEditor::_changed));
  418. }
  419. ShaderGlobalsEditor::~ShaderGlobalsEditor() {
  420. memdelete(interface);
  421. }