shader_globals_editor.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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_INT32_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. Vector<int> v4;
  256. v4.resize(4);
  257. v4.write[0] = 0;
  258. v4.write[1] = 0;
  259. v4.write[2] = 0;
  260. v4.write[3] = 0;
  261. return v4;
  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. Vector<int> v4;
  277. v4.resize(4);
  278. v4.write[0] = 0;
  279. v4.write[1] = 0;
  280. v4.write[2] = 0;
  281. v4.write[3] = 0;
  282. return v4;
  283. }
  284. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  285. return 0.0;
  286. }
  287. case RS::GLOBAL_VAR_TYPE_VEC2: {
  288. return Vector2();
  289. }
  290. case RS::GLOBAL_VAR_TYPE_VEC3: {
  291. return Vector3();
  292. }
  293. case RS::GLOBAL_VAR_TYPE_VEC4: {
  294. return Quaternion();
  295. }
  296. case RS::GLOBAL_VAR_TYPE_RECT2: {
  297. return Rect2();
  298. }
  299. case RS::GLOBAL_VAR_TYPE_COLOR: {
  300. return Color();
  301. }
  302. case RS::GLOBAL_VAR_TYPE_MAT2: {
  303. Vector<real_t> xform;
  304. xform.resize(4);
  305. xform.write[0] = 1;
  306. xform.write[1] = 0;
  307. xform.write[2] = 0;
  308. xform.write[3] = 1;
  309. return xform;
  310. }
  311. case RS::GLOBAL_VAR_TYPE_MAT3: {
  312. return Basis();
  313. }
  314. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  315. return Transform2D();
  316. }
  317. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  318. return Transform3D();
  319. }
  320. case RS::GLOBAL_VAR_TYPE_MAT4: {
  321. Vector<real_t> xform;
  322. xform.resize(16);
  323. xform.write[0] = 1;
  324. xform.write[1] = 0;
  325. xform.write[2] = 0;
  326. xform.write[3] = 0;
  327. xform.write[4] = 0;
  328. xform.write[5] = 1;
  329. xform.write[6] = 0;
  330. xform.write[7] = 0;
  331. xform.write[8] = 0;
  332. xform.write[9] = 0;
  333. xform.write[10] = 1;
  334. xform.write[11] = 0;
  335. xform.write[12] = 0;
  336. xform.write[13] = 0;
  337. xform.write[14] = 0;
  338. xform.write[15] = 1;
  339. return xform;
  340. }
  341. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  342. return "";
  343. }
  344. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  345. return "";
  346. }
  347. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  348. return "";
  349. }
  350. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  351. return "";
  352. }
  353. default: {
  354. return Variant();
  355. }
  356. }
  357. }
  358. void ShaderGlobalsEditor::_variable_added() {
  359. String var = variable_name->get_text().strip_edges();
  360. if (var.is_empty() || !var.is_valid_identifier()) {
  361. EditorNode::get_singleton()->show_warning(TTR("Please specify a valid shader uniform identifier name."));
  362. return;
  363. }
  364. if (RenderingServer::get_singleton()->global_shader_parameter_get(var).get_type() != Variant::NIL) {
  365. EditorNode::get_singleton()->show_warning(vformat(TTR("Global shader parameter '%s' already exists'"), var));
  366. return;
  367. }
  368. List<String> keywords;
  369. ShaderLanguage::get_keyword_list(&keywords);
  370. if (keywords.find(var) != nullptr || var == "script") {
  371. EditorNode::get_singleton()->show_warning(vformat(TTR("Name '%s' is a reserved shader language keyword."), var));
  372. return;
  373. }
  374. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  375. Variant value = create_var(RS::GlobalShaderParameterType(variable_type->get_selected()));
  376. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  377. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_add", var, RS::GlobalShaderParameterType(variable_type->get_selected()), value);
  378. undo_redo->add_undo_method(RS::get_singleton(), "global_shader_parameter_remove", var);
  379. Dictionary gv;
  380. gv["type"] = global_var_type_names[variable_type->get_selected()];
  381. gv["value"] = value;
  382. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + var, gv);
  383. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + var, Variant());
  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::_variable_deleted(const String &p_variable) {
  389. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  390. undo_redo->create_action(TTR("Add Shader Global Parameter"));
  391. undo_redo->add_do_method(RS::get_singleton(), "global_shader_parameter_remove", p_variable);
  392. 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));
  393. undo_redo->add_do_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, Variant());
  394. undo_redo->add_undo_property(ProjectSettings::get_singleton(), "shader_globals/" + p_variable, GLOBAL_GET("shader_globals/" + p_variable));
  395. undo_redo->add_do_method(this, "_changed");
  396. undo_redo->add_undo_method(this, "_changed");
  397. undo_redo->commit_action();
  398. }
  399. void ShaderGlobalsEditor::_changed() {
  400. emit_signal(SNAME("globals_changed"));
  401. if (!interface->block_update) {
  402. interface->notify_property_list_changed();
  403. }
  404. }
  405. void ShaderGlobalsEditor::_bind_methods() {
  406. ClassDB::bind_method("_changed", &ShaderGlobalsEditor::_changed);
  407. ADD_SIGNAL(MethodInfo("globals_changed"));
  408. }
  409. void ShaderGlobalsEditor::_notification(int p_what) {
  410. switch (p_what) {
  411. case NOTIFICATION_VISIBILITY_CHANGED: {
  412. if (is_visible_in_tree()) {
  413. inspector->edit(interface);
  414. }
  415. } break;
  416. case NOTIFICATION_PREDELETE: {
  417. inspector->edit(nullptr);
  418. } break;
  419. }
  420. }
  421. ShaderGlobalsEditor::ShaderGlobalsEditor() {
  422. HBoxContainer *add_menu_hb = memnew(HBoxContainer);
  423. add_child(add_menu_hb);
  424. add_menu_hb->add_child(memnew(Label(TTR("Name:"))));
  425. variable_name = memnew(LineEdit);
  426. variable_name->set_h_size_flags(SIZE_EXPAND_FILL);
  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. add_menu_hb->add_child(variable_add);
  437. variable_add->connect("pressed", callable_mp(this, &ShaderGlobalsEditor::_variable_added));
  438. inspector = memnew(EditorInspector);
  439. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  440. add_child(inspector);
  441. inspector->set_use_wide_editors(true);
  442. inspector->set_property_name_style(EditorPropertyNameProcessor::STYLE_RAW);
  443. inspector->set_use_deletable_properties(true);
  444. inspector->connect("property_deleted", callable_mp(this, &ShaderGlobalsEditor::_variable_deleted), CONNECT_DEFERRED);
  445. interface = memnew(ShaderGlobalsEditorInterface);
  446. interface->connect("var_changed", callable_mp(this, &ShaderGlobalsEditor::_changed));
  447. }
  448. ShaderGlobalsEditor::~ShaderGlobalsEditor() {
  449. memdelete(interface);
  450. }