visual_shader_editor_plugin.cpp 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348
  1. /*************************************************************************/
  2. /* visual_shader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "visual_shader_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/math/math_defs.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/project_settings.h"
  36. #include "editor/editor_log.h"
  37. #include "editor/editor_properties.h"
  38. #include "editor/editor_scale.h"
  39. #include "scene/animation/animation_player.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/panel.h"
  42. #include "scene/main/viewport.h"
  43. #include "scene/resources/visual_shader_nodes.h"
  44. #include "servers/visual/shader_types.h"
  45. Control *VisualShaderNodePlugin::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  46. if (get_script_instance()) {
  47. return get_script_instance()->call("create_editor", p_parent_resource, p_node);
  48. }
  49. return NULL;
  50. }
  51. void VisualShaderNodePlugin::_bind_methods() {
  52. BIND_VMETHOD(MethodInfo(Variant::OBJECT, "create_editor", PropertyInfo(Variant::OBJECT, "parent_resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::OBJECT, "for_node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode")));
  53. }
  54. ///////////////////
  55. void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
  56. bool changed = false;
  57. if (p_visual_shader) {
  58. if (visual_shader.is_null()) {
  59. changed = true;
  60. } else {
  61. if (visual_shader.ptr() != p_visual_shader) {
  62. changed = true;
  63. }
  64. }
  65. visual_shader = Ref<VisualShader>(p_visual_shader);
  66. if (!visual_shader->is_connected("changed", this, "_update_preview")) {
  67. visual_shader->connect("changed", this, "_update_preview");
  68. }
  69. visual_shader->set_graph_offset(graph->get_scroll_ofs() / EDSCALE);
  70. } else {
  71. if (visual_shader.is_valid()) {
  72. if (visual_shader->is_connected("changed", this, "")) {
  73. visual_shader->disconnect("changed", this, "_update_preview");
  74. }
  75. }
  76. visual_shader.unref();
  77. }
  78. if (visual_shader.is_null()) {
  79. hide();
  80. } else {
  81. if (changed) { // to avoid tree collapse
  82. _clear_buffer();
  83. _update_options_menu();
  84. _update_preview();
  85. }
  86. _update_graph();
  87. }
  88. }
  89. void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  90. if (plugins.find(p_plugin) != -1)
  91. return;
  92. plugins.push_back(p_plugin);
  93. }
  94. void VisualShaderEditor::remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  95. plugins.erase(p_plugin);
  96. }
  97. void VisualShaderEditor::clear_custom_types() {
  98. for (int i = 0; i < add_options.size(); i++) {
  99. if (add_options[i].is_custom) {
  100. add_options.remove(i);
  101. i--;
  102. }
  103. }
  104. }
  105. void VisualShaderEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script, const String &p_description, int p_return_icon_type, const String &p_category, const String &p_subcategory, bool p_highend) {
  106. ERR_FAIL_COND(!p_name.is_valid_identifier());
  107. ERR_FAIL_COND(!p_script.is_valid());
  108. for (int i = 0; i < add_options.size(); i++) {
  109. if (add_options[i].is_custom) {
  110. if (add_options[i].script == p_script)
  111. return;
  112. }
  113. }
  114. AddOption ao;
  115. ao.name = p_name;
  116. ao.script = p_script;
  117. ao.return_type = p_return_icon_type;
  118. ao.description = p_description;
  119. ao.category = p_category;
  120. ao.sub_category = p_subcategory;
  121. ao.highend = p_highend;
  122. ao.is_custom = true;
  123. bool begin = false;
  124. for (int i = 0; i < add_options.size(); i++) {
  125. if (add_options[i].is_custom) {
  126. if (add_options[i].category == p_category) {
  127. if (!begin) {
  128. begin = true;
  129. }
  130. } else {
  131. if (begin) {
  132. add_options.insert(i, ao);
  133. return;
  134. }
  135. }
  136. }
  137. }
  138. add_options.push_back(ao);
  139. }
  140. bool VisualShaderEditor::_is_available(int p_mode) {
  141. int current_mode = edit_type->get_selected();
  142. if (p_mode != -1) {
  143. switch (current_mode) {
  144. case VisualShader::TYPE_VERTEX:
  145. current_mode = 1;
  146. break;
  147. case VisualShader::TYPE_FRAGMENT:
  148. current_mode = 2;
  149. break;
  150. case VisualShader::TYPE_LIGHT:
  151. current_mode = 4;
  152. break;
  153. default:
  154. break;
  155. }
  156. int temp_mode = 0;
  157. if (p_mode & VisualShader::TYPE_FRAGMENT) {
  158. temp_mode |= 2;
  159. }
  160. if (p_mode & VisualShader::TYPE_LIGHT) {
  161. temp_mode |= 4;
  162. }
  163. if (temp_mode == 0) {
  164. temp_mode |= 1;
  165. }
  166. p_mode = temp_mode;
  167. }
  168. return (p_mode == -1 || (p_mode & current_mode) != 0);
  169. }
  170. void VisualShaderEditor::update_custom_nodes() {
  171. if (members_dialog->is_visible()) {
  172. return;
  173. }
  174. clear_custom_types();
  175. List<StringName> class_list;
  176. ScriptServer::get_global_class_list(&class_list);
  177. Dictionary added;
  178. for (int i = 0; i < class_list.size(); i++) {
  179. if (ScriptServer::get_global_class_native_base(class_list[i]) == "VisualShaderNodeCustom") {
  180. String script_path = ScriptServer::get_global_class_path(class_list[i]);
  181. Ref<Resource> res = ResourceLoader::load(script_path);
  182. ERR_FAIL_COND(res.is_null());
  183. ERR_FAIL_COND(!res->is_class("Script"));
  184. Ref<Script> script = Ref<Script>(res);
  185. Ref<VisualShaderNodeCustom> ref;
  186. ref.instance();
  187. ref->set_script(script.get_ref_ptr());
  188. String name;
  189. if (ref->has_method("_get_name")) {
  190. name = (String)ref->call("_get_name");
  191. } else {
  192. name = "Unnamed";
  193. }
  194. String description = "";
  195. if (ref->has_method("_get_description")) {
  196. description = (String)ref->call("_get_description");
  197. }
  198. int return_icon_type = -1;
  199. if (ref->has_method("_get_return_icon_type")) {
  200. return_icon_type = (int)ref->call("_get_return_icon_type");
  201. }
  202. String category = "";
  203. if (ref->has_method("_get_category")) {
  204. category = (String)ref->call("_get_category");
  205. }
  206. if (category == "") {
  207. category = "Custom";
  208. }
  209. String subcategory = "";
  210. if (ref->has_method("_get_subcategory")) {
  211. subcategory = (String)ref->call("_get_subcategory");
  212. }
  213. bool highend = false;
  214. if (ref->has_method("_is_highend")) {
  215. highend = (bool)ref->call("_is_highend");
  216. }
  217. Dictionary dict;
  218. dict["name"] = name;
  219. dict["script"] = script;
  220. dict["description"] = description;
  221. dict["return_icon_type"] = return_icon_type;
  222. dict["category"] = category;
  223. dict["subcategory"] = subcategory;
  224. dict["highend"] = highend;
  225. String key;
  226. key = category;
  227. key += "/";
  228. if (subcategory != "") {
  229. key += subcategory;
  230. key += "/";
  231. }
  232. key += name;
  233. added[key] = dict;
  234. }
  235. }
  236. Array keys = added.keys();
  237. keys.sort();
  238. for (int i = 0; i < keys.size(); i++) {
  239. const Variant &key = keys.get(i);
  240. const Dictionary &value = (Dictionary)added[key];
  241. add_custom_type(value["name"], value["script"], value["description"], value["return_icon_type"], value["category"], value["subcategory"], value["highend"]);
  242. }
  243. _update_options_menu();
  244. }
  245. String VisualShaderEditor::_get_description(int p_idx) {
  246. return add_options[p_idx].description;
  247. }
  248. void VisualShaderEditor::_update_options_menu() {
  249. node_desc->set_text("");
  250. members_dialog->get_ok()->set_disabled(true);
  251. String prev_category;
  252. String prev_sub_category;
  253. members->clear();
  254. TreeItem *root = members->create_item();
  255. TreeItem *category = NULL;
  256. TreeItem *sub_category = NULL;
  257. String filter = node_filter->get_text().strip_edges();
  258. bool use_filter = !filter.empty();
  259. Vector<String> categories;
  260. Vector<String> sub_categories;
  261. int item_count = 0;
  262. int item_count2 = 0;
  263. bool is_first_item = true;
  264. Color unsupported_color = get_color("error_color", "Editor");
  265. Color supported_color = get_color("warning_color", "Editor");
  266. static bool low_driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name") == "GLES2";
  267. int current_func = -1;
  268. if (!visual_shader.is_null()) {
  269. current_func = visual_shader->get_mode();
  270. }
  271. for (int i = 0; i < add_options.size() + 1; i++) {
  272. if (i == add_options.size()) {
  273. if (sub_category != NULL && item_count2 == 0) {
  274. memdelete(sub_category);
  275. --item_count;
  276. }
  277. if (category != NULL && item_count == 0) {
  278. memdelete(category);
  279. }
  280. break;
  281. }
  282. if (!use_filter || add_options[i].name.findn(filter) != -1) {
  283. if ((add_options[i].func != current_func && add_options[i].func != -1) || !_is_available(add_options[i].mode))
  284. continue;
  285. if (prev_category != add_options[i].category) {
  286. if (category != NULL && item_count == 0) {
  287. memdelete(category);
  288. }
  289. item_count = 0;
  290. prev_sub_category = "";
  291. category = members->create_item(root);
  292. category->set_text(0, add_options[i].category);
  293. category->set_selectable(0, false);
  294. if (!use_filter)
  295. category->set_collapsed(true);
  296. }
  297. if (add_options[i].sub_category != "") {
  298. if (prev_sub_category != add_options[i].sub_category) {
  299. if (category != NULL) {
  300. if (sub_category != NULL && item_count2 == 0) {
  301. memdelete(sub_category);
  302. --item_count;
  303. }
  304. ++item_count;
  305. item_count2 = 0;
  306. sub_category = members->create_item(category);
  307. sub_category->set_text(0, add_options[i].sub_category);
  308. sub_category->set_selectable(0, false);
  309. if (!use_filter)
  310. sub_category->set_collapsed(true);
  311. }
  312. }
  313. } else {
  314. sub_category = NULL;
  315. }
  316. TreeItem *p_category = NULL;
  317. if (sub_category != NULL) {
  318. p_category = sub_category;
  319. ++item_count2;
  320. } else if (category != NULL) {
  321. p_category = category;
  322. ++item_count;
  323. }
  324. if (p_category != NULL) {
  325. TreeItem *item = members->create_item(p_category);
  326. if (add_options[i].highend && low_driver)
  327. item->set_custom_color(0, unsupported_color);
  328. else if (add_options[i].highend)
  329. item->set_custom_color(0, supported_color);
  330. item->set_text(0, add_options[i].name);
  331. if (is_first_item && use_filter) {
  332. item->select(0);
  333. node_desc->set_text(_get_description(i));
  334. is_first_item = false;
  335. }
  336. switch (add_options[i].return_type) {
  337. case VisualShaderNode::PORT_TYPE_SCALAR:
  338. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"));
  339. break;
  340. case VisualShaderNode::PORT_TYPE_VECTOR:
  341. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"));
  342. break;
  343. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  344. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"));
  345. break;
  346. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  347. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"));
  348. break;
  349. case VisualShaderNode::PORT_TYPE_SAMPLER:
  350. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"));
  351. break;
  352. default:
  353. break;
  354. }
  355. item->set_meta("id", i);
  356. }
  357. prev_sub_category = add_options[i].sub_category;
  358. prev_category = add_options[i].category;
  359. }
  360. }
  361. }
  362. Size2 VisualShaderEditor::get_minimum_size() const {
  363. return Size2(10, 200);
  364. }
  365. void VisualShaderEditor::_draw_color_over_button(Object *obj, Color p_color) {
  366. Button *button = Object::cast_to<Button>(obj);
  367. if (!button)
  368. return;
  369. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  370. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  371. }
  372. static Ref<StyleBoxEmpty> make_empty_stylebox(float p_margin_left = -1, float p_margin_top = -1, float p_margin_right = -1, float p_margin_bottom = -1) {
  373. Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));
  374. style->set_default_margin(MARGIN_LEFT, p_margin_left * EDSCALE);
  375. style->set_default_margin(MARGIN_RIGHT, p_margin_right * EDSCALE);
  376. style->set_default_margin(MARGIN_BOTTOM, p_margin_bottom * EDSCALE);
  377. style->set_default_margin(MARGIN_TOP, p_margin_top * EDSCALE);
  378. return style;
  379. }
  380. void VisualShaderEditor::_update_created_node(GraphNode *node) {
  381. if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
  382. Ref<StyleBoxFlat> sb = node->get_stylebox("frame", "GraphNode");
  383. Color c = sb->get_border_color();
  384. Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0) : Color(0.0, 0.0, 0.0);
  385. mono_color.a = 0.85;
  386. c = mono_color;
  387. node->add_color_override("title_color", c);
  388. c.a = 0.7;
  389. node->add_color_override("close_color", c);
  390. node->add_color_override("resizer_color", c);
  391. }
  392. }
  393. void VisualShaderEditor::_update_graph() {
  394. if (updating)
  395. return;
  396. if (visual_shader.is_null())
  397. return;
  398. graph->set_scroll_ofs(visual_shader->get_graph_offset() * EDSCALE);
  399. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  400. graph->clear_connections();
  401. //erase all nodes
  402. for (int i = 0; i < graph->get_child_count(); i++) {
  403. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  404. Node *node = graph->get_child(i);
  405. graph->remove_child(node);
  406. memdelete(node);
  407. i--;
  408. }
  409. }
  410. static const Color type_color[5] = {
  411. Color(0.38, 0.85, 0.96), // scalar
  412. Color(0.84, 0.49, 0.93), // vector
  413. Color(0.55, 0.65, 0.94), // boolean
  414. Color(0.96, 0.66, 0.43), // transform
  415. Color(1.0, 1.0, 0.0) // sampler
  416. };
  417. List<VisualShader::Connection> connections;
  418. visual_shader->get_node_connections(type, &connections);
  419. Ref<StyleBoxEmpty> label_style = make_empty_stylebox(2, 1, 2, 1);
  420. Vector<int> nodes = visual_shader->get_node_list(type);
  421. Control *offset;
  422. for (int n_i = 0; n_i < nodes.size(); n_i++) {
  423. Vector2 position = visual_shader->get_node_position(type, nodes[n_i]);
  424. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, nodes[n_i]);
  425. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(vsnode.ptr());
  426. bool is_group = !group_node.is_null();
  427. Size2 size = Size2(0, 0);
  428. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(group_node.ptr());
  429. bool is_expression = !expression_node.is_null();
  430. String expression = "";
  431. GraphNode *node = memnew(GraphNode);
  432. if (is_group) {
  433. size = group_node->get_size();
  434. node->set_resizable(true);
  435. node->connect("resize_request", this, "_node_resized", varray((int)type, nodes[n_i]));
  436. }
  437. if (is_expression) {
  438. expression = expression_node->get_expression();
  439. }
  440. /*if (!vsnode->is_connected("changed", this, "_node_changed")) {
  441. vsnode->connect("changed", this, "_node_changed", varray(vsnode->get_instance_id()), CONNECT_DEFERRED);
  442. }*/
  443. node->set_offset(position);
  444. node->set_title(vsnode->get_caption());
  445. node->set_name(itos(nodes[n_i]));
  446. if (nodes[n_i] >= 2) {
  447. node->set_show_close_button(true);
  448. node->connect("close_request", this, "_delete_request", varray(nodes[n_i]), CONNECT_DEFERRED);
  449. }
  450. node->connect("dragged", this, "_node_dragged", varray(nodes[n_i]));
  451. Control *custom_editor = NULL;
  452. int port_offset = 0;
  453. if (is_group) {
  454. port_offset += 2;
  455. }
  456. Ref<VisualShaderNodeUniform> uniform = vsnode;
  457. Ref<VisualShaderNodeScalarUniform> scalar_uniform = vsnode;
  458. if (uniform.is_valid()) {
  459. graph->add_child(node);
  460. _update_created_node(node);
  461. LineEdit *uniform_name = memnew(LineEdit);
  462. uniform_name->set_text(uniform->get_uniform_name());
  463. node->add_child(uniform_name);
  464. uniform_name->connect("text_entered", this, "_line_edit_changed", varray(uniform_name, nodes[n_i]));
  465. uniform_name->connect("focus_exited", this, "_line_edit_focus_out", varray(uniform_name, nodes[n_i]));
  466. if (vsnode->get_input_port_count() == 0 && vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
  467. //shortcut
  468. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  469. node->set_slot(0, false, VisualShaderNode::PORT_TYPE_SCALAR, Color(), true, port_right, type_color[port_right]);
  470. if (!scalar_uniform.is_valid()) {
  471. continue;
  472. }
  473. }
  474. port_offset++;
  475. }
  476. for (int i = 0; i < plugins.size(); i++) {
  477. custom_editor = plugins.write[i]->create_editor(visual_shader, vsnode);
  478. if (custom_editor) {
  479. break;
  480. }
  481. }
  482. if (custom_editor && !scalar_uniform.is_valid() && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) {
  483. //will be embedded in first port
  484. } else if (custom_editor) {
  485. port_offset++;
  486. node->add_child(custom_editor);
  487. if (scalar_uniform.is_valid()) {
  488. custom_editor->call_deferred("_show_prop_names", true);
  489. continue;
  490. }
  491. custom_editor = NULL;
  492. }
  493. if (is_group) {
  494. offset = memnew(Control);
  495. offset->set_custom_minimum_size(Size2(0, 6 * EDSCALE));
  496. node->add_child(offset);
  497. if (group_node->is_editable()) {
  498. HBoxContainer *hb2 = memnew(HBoxContainer);
  499. Button *add_input_btn = memnew(Button);
  500. add_input_btn->set_text(TTR("Add Input"));
  501. add_input_btn->connect("pressed", this, "_add_input_port", varray(nodes[n_i], group_node->get_free_input_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "input" + itos(group_node->get_free_input_port_id())), CONNECT_DEFERRED);
  502. hb2->add_child(add_input_btn);
  503. hb2->add_spacer();
  504. Button *add_output_btn = memnew(Button);
  505. add_output_btn->set_text(TTR("Add Output"));
  506. add_output_btn->connect("pressed", this, "_add_output_port", varray(nodes[n_i], group_node->get_free_output_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "output" + itos(group_node->get_free_output_port_id())), CONNECT_DEFERRED);
  507. hb2->add_child(add_output_btn);
  508. node->add_child(hb2);
  509. }
  510. }
  511. for (int i = 0; i < MAX(vsnode->get_input_port_count(), vsnode->get_output_port_count()); i++) {
  512. if (vsnode->is_port_separator(i)) {
  513. node->add_child(memnew(HSeparator));
  514. port_offset++;
  515. }
  516. bool valid_left = i < vsnode->get_input_port_count();
  517. VisualShaderNode::PortType port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  518. bool port_left_used = false;
  519. String name_left;
  520. if (valid_left) {
  521. name_left = vsnode->get_input_port_name(i);
  522. port_left = vsnode->get_input_port_type(i);
  523. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  524. if (E->get().to_node == nodes[n_i] && E->get().to_port == i) {
  525. port_left_used = true;
  526. }
  527. }
  528. }
  529. bool valid_right = i < vsnode->get_output_port_count();
  530. VisualShaderNode::PortType port_right = VisualShaderNode::PORT_TYPE_SCALAR;
  531. String name_right;
  532. if (valid_right) {
  533. name_right = vsnode->get_output_port_name(i);
  534. port_right = vsnode->get_output_port_type(i);
  535. }
  536. HBoxContainer *hb = memnew(HBoxContainer);
  537. hb->add_constant_override("separation", 7 * EDSCALE);
  538. Variant default_value;
  539. if (valid_left && !port_left_used) {
  540. default_value = vsnode->get_input_port_default_value(i);
  541. }
  542. if (default_value.get_type() != Variant::NIL) { // only a label
  543. Button *button = memnew(Button);
  544. hb->add_child(button);
  545. button->connect("pressed", this, "_edit_port_default_input", varray(button, nodes[n_i], i));
  546. switch (default_value.get_type()) {
  547. case Variant::COLOR: {
  548. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  549. button->connect("draw", this, "_draw_color_over_button", varray(button, default_value));
  550. } break;
  551. case Variant::BOOL: {
  552. button->set_text(((bool)default_value) ? "true" : "false");
  553. } break;
  554. case Variant::INT:
  555. case Variant::REAL: {
  556. button->set_text(String::num(default_value, 4));
  557. } break;
  558. case Variant::VECTOR3: {
  559. Vector3 v = default_value;
  560. button->set_text(String::num(v.x, 3) + "," + String::num(v.y, 3) + "," + String::num(v.z, 3));
  561. } break;
  562. default: {
  563. }
  564. }
  565. }
  566. if (i == 0 && custom_editor) {
  567. hb->add_child(custom_editor);
  568. custom_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  569. } else {
  570. if (valid_left) {
  571. if (is_group) {
  572. OptionButton *type_box = memnew(OptionButton);
  573. hb->add_child(type_box);
  574. type_box->add_item(TTR("Scalar"));
  575. type_box->add_item(TTR("Vector"));
  576. type_box->add_item(TTR("Boolean"));
  577. type_box->add_item(TTR("Transform"));
  578. type_box->add_item(TTR("Sampler"));
  579. type_box->select(group_node->get_input_port_type(i));
  580. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  581. type_box->connect("item_selected", this, "_change_input_port_type", varray(nodes[n_i], i), CONNECT_DEFERRED);
  582. LineEdit *name_box = memnew(LineEdit);
  583. hb->add_child(name_box);
  584. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  585. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  586. name_box->set_text(name_left);
  587. name_box->connect("text_entered", this, "_change_input_port_name", varray(name_box, nodes[n_i], i));
  588. name_box->connect("focus_exited", this, "_port_name_focus_out", varray(name_box, nodes[n_i], i, false));
  589. Button *remove_btn = memnew(Button);
  590. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Remove", "EditorIcons"));
  591. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  592. remove_btn->connect("pressed", this, "_remove_input_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  593. hb->add_child(remove_btn);
  594. } else {
  595. Label *label = memnew(Label);
  596. label->set_text(name_left);
  597. label->add_style_override("normal", label_style); //more compact
  598. hb->add_child(label);
  599. if (vsnode->get_input_port_default_hint(i) != "" && !port_left_used) {
  600. Label *hint_label = memnew(Label);
  601. hint_label->set_text("[" + vsnode->get_input_port_default_hint(i) + "]");
  602. hint_label->add_color_override("font_color", get_color("font_color_readonly", "TextEdit"));
  603. hint_label->add_style_override("normal", label_style);
  604. hb->add_child(hint_label);
  605. }
  606. }
  607. }
  608. if (!is_group) {
  609. hb->add_spacer();
  610. }
  611. if (valid_right) {
  612. if (is_group) {
  613. Button *remove_btn = memnew(Button);
  614. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Remove", "EditorIcons"));
  615. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  616. remove_btn->connect("pressed", this, "_remove_output_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  617. hb->add_child(remove_btn);
  618. LineEdit *name_box = memnew(LineEdit);
  619. hb->add_child(name_box);
  620. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  621. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  622. name_box->set_text(name_right);
  623. name_box->connect("text_entered", this, "_change_output_port_name", varray(name_box, nodes[n_i], i));
  624. name_box->connect("focus_exited", this, "_port_name_focus_out", varray(name_box, nodes[n_i], i, true));
  625. OptionButton *type_box = memnew(OptionButton);
  626. hb->add_child(type_box);
  627. type_box->add_item(TTR("Scalar"));
  628. type_box->add_item(TTR("Vector"));
  629. type_box->add_item(TTR("Boolean"));
  630. type_box->add_item(TTR("Transform"));
  631. type_box->select(group_node->get_output_port_type(i));
  632. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  633. type_box->connect("item_selected", this, "_change_output_port_type", varray(nodes[n_i], i), CONNECT_DEFERRED);
  634. } else {
  635. Label *label = memnew(Label);
  636. label->set_text(name_right);
  637. label->add_style_override("normal", label_style); //more compact
  638. hb->add_child(label);
  639. }
  640. }
  641. }
  642. if (valid_right && edit_type->get_selected() == VisualShader::TYPE_FRAGMENT && port_right != VisualShaderNode::PORT_TYPE_TRANSFORM && port_right != VisualShaderNode::PORT_TYPE_SAMPLER) {
  643. TextureButton *preview = memnew(TextureButton);
  644. preview->set_toggle_mode(true);
  645. preview->set_normal_texture(get_icon("GuiVisibilityHidden", "EditorIcons"));
  646. preview->set_pressed_texture(get_icon("GuiVisibilityVisible", "EditorIcons"));
  647. preview->set_v_size_flags(SIZE_SHRINK_CENTER);
  648. if (vsnode->get_output_port_for_preview() == i) {
  649. preview->set_pressed(true);
  650. }
  651. preview->connect("pressed", this, "_preview_select_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  652. hb->add_child(preview);
  653. }
  654. if (is_group) {
  655. offset = memnew(Control);
  656. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  657. node->add_child(offset);
  658. port_offset++;
  659. }
  660. node->add_child(hb);
  661. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
  662. }
  663. if (vsnode->get_output_port_for_preview() >= 0) {
  664. int port_type = vsnode->get_output_port_type(vsnode->get_output_port_for_preview());
  665. if (port_type != VisualShaderNode::PORT_TYPE_TRANSFORM && port_type != VisualShaderNode::PORT_TYPE_SAMPLER) {
  666. offset = memnew(Control);
  667. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  668. node->add_child(offset);
  669. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  670. port_preview->setup(visual_shader, type, nodes[n_i], vsnode->get_output_port_for_preview());
  671. port_preview->set_h_size_flags(SIZE_SHRINK_CENTER);
  672. node->add_child(port_preview);
  673. }
  674. }
  675. offset = memnew(Control);
  676. offset->set_custom_minimum_size(Size2(0, 4 * EDSCALE));
  677. node->add_child(offset);
  678. String error = vsnode->get_warning(visual_shader->get_mode(), type);
  679. if (error != String()) {
  680. Label *error_label = memnew(Label);
  681. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  682. error_label->set_text(error);
  683. node->add_child(error_label);
  684. }
  685. if (is_expression) {
  686. TextEdit *expression_box = memnew(TextEdit);
  687. expression_node->set_control(expression_box, 0);
  688. node->add_child(expression_box);
  689. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  690. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  691. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  692. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  693. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  694. expression_box->set_syntax_coloring(true);
  695. expression_box->add_color_override("background_color", background_color);
  696. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  697. expression_box->add_keyword_color(E->get(), keyword_color);
  698. }
  699. expression_box->add_font_override("font", get_font("expression", "EditorFonts"));
  700. expression_box->add_color_override("font_color", text_color);
  701. expression_box->add_color_override("symbol_color", symbol_color);
  702. expression_box->add_color_region("/*", "*/", comment_color, false);
  703. expression_box->add_color_region("//", "", comment_color, false);
  704. expression_box->set_text(expression);
  705. expression_box->set_context_menu_enabled(false);
  706. expression_box->set_show_line_numbers(true);
  707. expression_box->connect("focus_exited", this, "_expression_focus_out", varray(expression_box, nodes[n_i]));
  708. }
  709. if (!uniform.is_valid()) {
  710. graph->add_child(node);
  711. _update_created_node(node);
  712. if (is_group)
  713. call_deferred("_set_node_size", (int)type, nodes[n_i], size);
  714. }
  715. }
  716. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  717. int from = E->get().from_node;
  718. int from_idx = E->get().from_port;
  719. int to = E->get().to_node;
  720. int to_idx = E->get().to_port;
  721. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  722. }
  723. }
  724. void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  725. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  726. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  727. if (node.is_null()) {
  728. return;
  729. }
  730. undo_redo->create_action(TTR("Add input port"));
  731. undo_redo->add_do_method(node.ptr(), "add_input_port", p_port, p_port_type, p_name);
  732. undo_redo->add_undo_method(node.ptr(), "remove_input_port", p_port);
  733. undo_redo->add_do_method(this, "_update_graph");
  734. undo_redo->add_undo_method(this, "_update_graph");
  735. undo_redo->add_do_method(this, "_rebuild");
  736. undo_redo->add_undo_method(this, "_rebuild");
  737. undo_redo->commit_action();
  738. }
  739. void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  740. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  741. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  742. if (node.is_null()) {
  743. return;
  744. }
  745. undo_redo->create_action(TTR("Add output port"));
  746. undo_redo->add_do_method(node.ptr(), "add_output_port", p_port, p_port_type, p_name);
  747. undo_redo->add_undo_method(node.ptr(), "remove_output_port", p_port);
  748. undo_redo->add_do_method(this, "_update_graph");
  749. undo_redo->add_undo_method(this, "_update_graph");
  750. undo_redo->add_do_method(this, "_rebuild");
  751. undo_redo->add_undo_method(this, "_rebuild");
  752. undo_redo->commit_action();
  753. }
  754. void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_port) {
  755. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  756. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  757. if (node.is_null()) {
  758. return;
  759. }
  760. undo_redo->create_action(TTR("Change input port type"));
  761. undo_redo->add_do_method(node.ptr(), "set_input_port_type", p_port, p_type);
  762. undo_redo->add_undo_method(node.ptr(), "set_input_port_type", p_port, node->get_input_port_type(p_port));
  763. undo_redo->add_do_method(this, "_update_graph");
  764. undo_redo->add_undo_method(this, "_update_graph");
  765. undo_redo->add_do_method(this, "_rebuild");
  766. undo_redo->add_undo_method(this, "_rebuild");
  767. undo_redo->commit_action();
  768. }
  769. void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_port) {
  770. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  771. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  772. if (node.is_null()) {
  773. return;
  774. }
  775. undo_redo->create_action(TTR("Change output port type"));
  776. undo_redo->add_do_method(node.ptr(), "set_output_port_type", p_port, p_type);
  777. undo_redo->add_undo_method(node.ptr(), "set_output_port_type", p_port, node->get_output_port_type(p_port));
  778. undo_redo->add_do_method(this, "_update_graph");
  779. undo_redo->add_undo_method(this, "_update_graph");
  780. undo_redo->add_do_method(this, "_rebuild");
  781. undo_redo->add_undo_method(this, "_rebuild");
  782. undo_redo->commit_action();
  783. }
  784. void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  785. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  786. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  787. ERR_FAIL_COND(!node.is_valid());
  788. undo_redo->create_action(TTR("Change input port name"));
  789. undo_redo->add_do_method(node.ptr(), "set_input_port_name", p_port_id, p_text);
  790. undo_redo->add_undo_method(node.ptr(), "set_input_port_name", p_port_id, node->get_input_port_name(p_port_id));
  791. undo_redo->add_do_method(this, "_rebuild");
  792. undo_redo->add_undo_method(this, "_rebuild");
  793. undo_redo->commit_action();
  794. }
  795. void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  796. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  797. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  798. ERR_FAIL_COND(!node.is_valid());
  799. undo_redo->create_action(TTR("Change output port name"));
  800. undo_redo->add_do_method(node.ptr(), "set_output_port_name", p_port_id, p_text);
  801. undo_redo->add_undo_method(node.ptr(), "set_output_port_name", p_port_id, node->get_output_port_name(p_port_id));
  802. undo_redo->add_do_method(this, "_rebuild");
  803. undo_redo->add_undo_method(this, "_rebuild");
  804. undo_redo->commit_action();
  805. }
  806. void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
  807. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  808. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  809. if (node.is_null()) {
  810. return;
  811. }
  812. undo_redo->create_action(TTR("Remove input port"));
  813. List<VisualShader::Connection> conns;
  814. visual_shader->get_node_connections(type, &conns);
  815. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  816. int from_node = E->get().from_node;
  817. int from_port = E->get().from_port;
  818. int to_node = E->get().to_node;
  819. int to_port = E->get().to_port;
  820. if (to_node == p_node) {
  821. if (to_port == p_port) {
  822. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  823. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  824. } else if (to_port > p_port) {
  825. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  826. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  827. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port - 1);
  828. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  829. }
  830. }
  831. }
  832. undo_redo->add_do_method(node.ptr(), "remove_input_port", p_port);
  833. undo_redo->add_undo_method(node.ptr(), "add_input_port", p_port, (int)node->get_input_port_type(p_port), node->get_input_port_name(p_port));
  834. undo_redo->add_do_method(this, "_update_graph");
  835. undo_redo->add_undo_method(this, "_update_graph");
  836. undo_redo->add_do_method(this, "_rebuild");
  837. undo_redo->add_undo_method(this, "_rebuild");
  838. undo_redo->commit_action();
  839. }
  840. void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
  841. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  842. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  843. if (node.is_null()) {
  844. return;
  845. }
  846. undo_redo->create_action(TTR("Remove output port"));
  847. List<VisualShader::Connection> conns;
  848. visual_shader->get_node_connections(type, &conns);
  849. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  850. int from_node = E->get().from_node;
  851. int from_port = E->get().from_port;
  852. int to_node = E->get().to_node;
  853. int to_port = E->get().to_port;
  854. if (from_node == p_node) {
  855. if (from_port == p_port) {
  856. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  857. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  858. } else if (from_port > p_port) {
  859. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  860. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  861. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port - 1, to_node, to_port);
  862. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  863. }
  864. }
  865. }
  866. undo_redo->add_do_method(node.ptr(), "remove_output_port", p_port);
  867. undo_redo->add_undo_method(node.ptr(), "add_output_port", p_port, (int)node->get_output_port_type(p_port), node->get_output_port_name(p_port));
  868. undo_redo->add_do_method(this, "_update_graph");
  869. undo_redo->add_undo_method(this, "_update_graph");
  870. undo_redo->add_do_method(this, "_rebuild");
  871. undo_redo->add_undo_method(this, "_rebuild");
  872. undo_redo->commit_action();
  873. }
  874. void VisualShaderEditor::_expression_focus_out(Object *text_edit, int p_node) {
  875. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  876. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  877. if (node.is_null()) {
  878. return;
  879. }
  880. TextEdit *expression_box = Object::cast_to<TextEdit>(text_edit);
  881. if (node->get_expression() == expression_box->get_text())
  882. return;
  883. undo_redo->create_action(TTR("Set expression"));
  884. undo_redo->add_do_method(node.ptr(), "set_expression", expression_box->get_text());
  885. undo_redo->add_undo_method(node.ptr(), "set_expression", node->get_expression());
  886. undo_redo->add_do_method(this, "_rebuild");
  887. undo_redo->add_undo_method(this, "_rebuild");
  888. undo_redo->commit_action();
  889. }
  890. void VisualShaderEditor::_rebuild() {
  891. if (visual_shader != NULL) {
  892. EditorNode::get_singleton()->get_log()->clear();
  893. visual_shader->rebuild();
  894. }
  895. }
  896. void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p_size) {
  897. VisualShader::Type type = VisualShader::Type(p_type);
  898. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  899. if (node.is_null()) {
  900. return;
  901. }
  902. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  903. if (group_node.is_null()) {
  904. return;
  905. }
  906. Vector2 size = p_size;
  907. group_node->set_size(size);
  908. GraphNode *gn = NULL;
  909. if (edit_type->get_selected() == p_type) { // check - otherwise the error will be emitted
  910. Node *node2 = graph->get_node(itos(p_node));
  911. gn = Object::cast_to<GraphNode>(node2);
  912. if (!gn)
  913. return;
  914. gn->set_custom_minimum_size(size);
  915. gn->set_size(Size2(1, 1));
  916. }
  917. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  918. if (!expression_node.is_null()) {
  919. Control *text_box = expression_node->get_control(0);
  920. Size2 box_size = size;
  921. if (gn != NULL) {
  922. if (box_size.x < 150 * EDSCALE || box_size.y < 0) {
  923. box_size.x = gn->get_size().x;
  924. }
  925. }
  926. box_size.x -= text_box->get_margin(MARGIN_LEFT);
  927. box_size.x -= 28 * EDSCALE;
  928. box_size.y -= text_box->get_margin(MARGIN_TOP);
  929. box_size.y -= 28 * EDSCALE;
  930. text_box->set_custom_minimum_size(Size2(box_size.x, box_size.y));
  931. text_box->set_size(Size2(1, 1));
  932. }
  933. }
  934. void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, int p_node) {
  935. VisualShader::Type type = VisualShader::Type(p_type);
  936. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  937. if (node.is_null()) {
  938. return;
  939. }
  940. undo_redo->create_action(TTR("Resize VisualShader node"), UndoRedo::MERGE_ENDS);
  941. undo_redo->add_do_method(this, "_set_node_size", p_type, p_node, p_new_size);
  942. undo_redo->add_undo_method(this, "_set_node_size", p_type, p_node, node->get_size());
  943. undo_redo->commit_action();
  944. }
  945. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  946. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  947. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  948. if (node.is_null()) {
  949. return;
  950. }
  951. if (node->get_output_port_for_preview() == p_port) {
  952. p_port = -1; //toggle it
  953. }
  954. undo_redo->create_action(TTR("Set Uniform Name"));
  955. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  956. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", node->get_output_port_for_preview());
  957. undo_redo->add_do_method(this, "_update_graph");
  958. undo_redo->add_undo_method(this, "_update_graph");
  959. undo_redo->commit_action();
  960. }
  961. void VisualShaderEditor::_line_edit_changed(const String &p_text, Object *line_edit, int p_node_id) {
  962. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  963. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  964. ERR_FAIL_COND(!node.is_valid());
  965. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  966. updating = true;
  967. undo_redo->create_action(TTR("Set Uniform Name"));
  968. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  969. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  970. undo_redo->add_do_method(this, "_update_graph");
  971. undo_redo->add_undo_method(this, "_update_graph");
  972. undo_redo->commit_action();
  973. updating = false;
  974. Object::cast_to<LineEdit>(line_edit)->set_text(validated_name);
  975. }
  976. void VisualShaderEditor::_line_edit_focus_out(Object *line_edit, int p_node_id) {
  977. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  978. _line_edit_changed(text, line_edit, p_node_id);
  979. }
  980. void VisualShaderEditor::_port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output) {
  981. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  982. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  983. ERR_FAIL_COND(!node.is_valid());
  984. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  985. if (!p_output) {
  986. if (node->get_input_port_name(p_port_id) == text)
  987. return;
  988. } else {
  989. if (node->get_output_port_name(p_port_id) == text)
  990. return;
  991. }
  992. List<String> input_names;
  993. List<String> output_names;
  994. for (int i = 0; i < node->get_input_port_count(); i++) {
  995. if (!p_output && i == p_port_id) continue;
  996. input_names.push_back(node->get_input_port_name(i));
  997. }
  998. for (int i = 0; i < node->get_output_port_count(); i++) {
  999. if (p_output && i == p_port_id) continue;
  1000. output_names.push_back(node->get_output_port_name(i));
  1001. }
  1002. String validated_name = visual_shader->validate_port_name(text, input_names, output_names);
  1003. if (validated_name == "") {
  1004. if (!p_output) {
  1005. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_input_port_name(p_port_id));
  1006. } else {
  1007. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_output_port_name(p_port_id));
  1008. }
  1009. return;
  1010. }
  1011. if (!p_output) {
  1012. _change_input_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1013. } else {
  1014. _change_output_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1015. }
  1016. }
  1017. void VisualShaderEditor::_port_edited() {
  1018. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1019. Variant value = property_editor->get_variant();
  1020. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  1021. ERR_FAIL_COND(!vsn.is_valid());
  1022. undo_redo->create_action(TTR("Set Input Default Port"));
  1023. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  1024. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1025. undo_redo->add_do_method(this, "_update_graph");
  1026. undo_redo->add_undo_method(this, "_update_graph");
  1027. undo_redo->commit_action();
  1028. property_editor->hide();
  1029. }
  1030. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  1031. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1032. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  1033. Button *button = Object::cast_to<Button>(p_button);
  1034. ERR_FAIL_COND(!button);
  1035. Variant value = vsn->get_input_port_default_value(p_port);
  1036. property_editor->set_global_position(button->get_global_position() + Vector2(0, button->get_size().height));
  1037. property_editor->edit(NULL, "", value.get_type(), value, 0, "");
  1038. property_editor->popup();
  1039. editing_node = p_node;
  1040. editing_port = p_port;
  1041. }
  1042. void VisualShaderEditor::_add_custom_node(const String &p_path) {
  1043. int idx = -1;
  1044. for (int i = custom_node_option_idx; i < add_options.size(); i++) {
  1045. if (add_options[i].script.is_valid()) {
  1046. if (add_options[i].script->get_path() == p_path) {
  1047. idx = i;
  1048. break;
  1049. }
  1050. }
  1051. }
  1052. if (idx != -1) {
  1053. _add_node(idx);
  1054. }
  1055. }
  1056. void VisualShaderEditor::_add_texture_node(const String &p_path) {
  1057. VisualShaderNodeTexture *texture = (VisualShaderNodeTexture *)_add_node(texture_node_option_idx, -1);
  1058. texture->set_texture(ResourceLoader::load(p_path));
  1059. }
  1060. VisualShaderNode *VisualShaderEditor::_add_node(int p_idx, int p_op_idx) {
  1061. ERR_FAIL_INDEX_V(p_idx, add_options.size(), NULL);
  1062. Ref<VisualShaderNode> vsnode;
  1063. bool is_custom = add_options[p_idx].is_custom;
  1064. if (!is_custom && add_options[p_idx].type != String()) {
  1065. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(add_options[p_idx].type));
  1066. ERR_FAIL_COND_V(!vsn, NULL);
  1067. VisualShaderNodeScalarConstant *constant = Object::cast_to<VisualShaderNodeScalarConstant>(vsn);
  1068. if (constant) {
  1069. if ((int)add_options[p_idx].value != -1)
  1070. constant->set_constant(add_options[p_idx].value);
  1071. }
  1072. if (p_op_idx != -1) {
  1073. VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(vsn);
  1074. if (input) {
  1075. input->set_input_name(add_options[p_idx].sub_func_str);
  1076. }
  1077. VisualShaderNodeIs *is = Object::cast_to<VisualShaderNodeIs>(vsn);
  1078. if (is) {
  1079. is->set_function((VisualShaderNodeIs::Function)p_op_idx);
  1080. }
  1081. VisualShaderNodeCompare *cmp = Object::cast_to<VisualShaderNodeCompare>(vsn);
  1082. if (cmp) {
  1083. cmp->set_function((VisualShaderNodeCompare::Function)p_op_idx);
  1084. }
  1085. VisualShaderNodeColorOp *colorOp = Object::cast_to<VisualShaderNodeColorOp>(vsn);
  1086. if (colorOp) {
  1087. colorOp->set_operator((VisualShaderNodeColorOp::Operator)p_op_idx);
  1088. }
  1089. VisualShaderNodeColorFunc *colorFunc = Object::cast_to<VisualShaderNodeColorFunc>(vsn);
  1090. if (colorFunc) {
  1091. colorFunc->set_function((VisualShaderNodeColorFunc::Function)p_op_idx);
  1092. }
  1093. VisualShaderNodeScalarOp *scalarOp = Object::cast_to<VisualShaderNodeScalarOp>(vsn);
  1094. if (scalarOp) {
  1095. scalarOp->set_operator((VisualShaderNodeScalarOp::Operator)p_op_idx);
  1096. }
  1097. VisualShaderNodeScalarFunc *scalarFunc = Object::cast_to<VisualShaderNodeScalarFunc>(vsn);
  1098. if (scalarFunc) {
  1099. scalarFunc->set_function((VisualShaderNodeScalarFunc::Function)p_op_idx);
  1100. }
  1101. VisualShaderNodeVectorOp *vecOp = Object::cast_to<VisualShaderNodeVectorOp>(vsn);
  1102. if (vecOp) {
  1103. vecOp->set_operator((VisualShaderNodeVectorOp::Operator)p_op_idx);
  1104. }
  1105. VisualShaderNodeVectorFunc *vecFunc = Object::cast_to<VisualShaderNodeVectorFunc>(vsn);
  1106. if (vecFunc) {
  1107. vecFunc->set_function((VisualShaderNodeVectorFunc::Function)p_op_idx);
  1108. }
  1109. VisualShaderNodeTransformFunc *matFunc = Object::cast_to<VisualShaderNodeTransformFunc>(vsn);
  1110. if (matFunc) {
  1111. matFunc->set_function((VisualShaderNodeTransformFunc::Function)p_op_idx);
  1112. }
  1113. VisualShaderNodeScalarDerivativeFunc *sderFunc = Object::cast_to<VisualShaderNodeScalarDerivativeFunc>(vsn);
  1114. if (sderFunc) {
  1115. sderFunc->set_function((VisualShaderNodeScalarDerivativeFunc::Function)p_op_idx);
  1116. }
  1117. VisualShaderNodeVectorDerivativeFunc *vderFunc = Object::cast_to<VisualShaderNodeVectorDerivativeFunc>(vsn);
  1118. if (vderFunc) {
  1119. vderFunc->set_function((VisualShaderNodeVectorDerivativeFunc::Function)p_op_idx);
  1120. }
  1121. }
  1122. vsnode = Ref<VisualShaderNode>(vsn);
  1123. } else {
  1124. ERR_FAIL_COND_V(add_options[p_idx].script.is_null(), NULL);
  1125. String base_type = add_options[p_idx].script->get_instance_base_type();
  1126. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(base_type));
  1127. ERR_FAIL_COND_V(!vsn, NULL);
  1128. vsnode = Ref<VisualShaderNode>(vsn);
  1129. vsnode->set_script(add_options[p_idx].script.get_ref_ptr());
  1130. }
  1131. Point2 position = graph->get_scroll_ofs();
  1132. if (saved_node_pos_dirty) {
  1133. position += saved_node_pos;
  1134. } else {
  1135. position += graph->get_size() * 0.5;
  1136. position /= EDSCALE;
  1137. }
  1138. saved_node_pos_dirty = false;
  1139. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1140. int id_to_use = visual_shader->get_valid_node_id(type);
  1141. undo_redo->create_action(TTR("Add Node to Visual Shader"));
  1142. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  1143. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  1144. VisualShaderNodeExpression *expr = Object::cast_to<VisualShaderNodeExpression>(vsnode.ptr());
  1145. if (expr) {
  1146. undo_redo->add_do_method(expr, "set_size", Size2(250 * EDSCALE, 150 * EDSCALE));
  1147. }
  1148. if (to_node != -1 && to_slot != -1) {
  1149. if (vsnode->get_output_port_count() > 0) {
  1150. int _from_node = id_to_use;
  1151. int _from_slot = 0;
  1152. if (visual_shader->is_port_types_compatible(vsnode->get_output_port_type(_from_slot), visual_shader->get_node(type, to_node)->get_input_port_type(to_slot))) {
  1153. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1154. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1155. }
  1156. }
  1157. } else if (from_node != -1 && from_slot != -1) {
  1158. if (vsnode->get_input_port_count() > 0) {
  1159. int _to_node = id_to_use;
  1160. int _to_slot = 0;
  1161. if (visual_shader->is_port_types_compatible(visual_shader->get_node(type, from_node)->get_output_port_type(from_slot), vsnode->get_input_port_type(_to_slot))) {
  1162. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1163. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1164. }
  1165. }
  1166. }
  1167. undo_redo->add_do_method(this, "_update_graph");
  1168. undo_redo->add_undo_method(this, "_update_graph");
  1169. undo_redo->commit_action();
  1170. return vsnode.ptr();
  1171. }
  1172. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  1173. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1174. updating = true;
  1175. undo_redo->create_action(TTR("Node Moved"));
  1176. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", type, p_node, p_to);
  1177. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", type, p_node, p_from);
  1178. undo_redo->add_do_method(this, "_update_graph");
  1179. undo_redo->add_undo_method(this, "_update_graph");
  1180. undo_redo->commit_action();
  1181. updating = false;
  1182. }
  1183. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1184. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1185. int from = p_from.to_int();
  1186. int to = p_to.to_int();
  1187. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  1188. return;
  1189. }
  1190. undo_redo->create_action(TTR("Nodes Connected"));
  1191. List<VisualShader::Connection> conns;
  1192. visual_shader->get_node_connections(type, &conns);
  1193. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1194. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  1195. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1196. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1197. }
  1198. }
  1199. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1200. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1201. undo_redo->add_do_method(this, "_update_graph");
  1202. undo_redo->add_undo_method(this, "_update_graph");
  1203. undo_redo->commit_action();
  1204. }
  1205. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1206. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  1207. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1208. int from = p_from.to_int();
  1209. int to = p_to.to_int();
  1210. //updating = true; seems graph edit can handle this, no need to protect
  1211. undo_redo->create_action(TTR("Nodes Disconnected"));
  1212. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1213. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1214. undo_redo->add_do_method(this, "_update_graph");
  1215. undo_redo->add_undo_method(this, "_update_graph");
  1216. undo_redo->commit_action();
  1217. //updating = false;
  1218. }
  1219. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  1220. from_node = p_from.to_int();
  1221. from_slot = p_from_slot;
  1222. _show_members_dialog(true);
  1223. }
  1224. void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  1225. to_node = p_to.to_int();
  1226. to_slot = p_to_slot;
  1227. _show_members_dialog(true);
  1228. }
  1229. void VisualShaderEditor::_delete_request(int which) {
  1230. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1231. Ref<VisualShaderNode> node = Ref<VisualShaderNode>(visual_shader->get_node(type, which));
  1232. undo_redo->create_action(TTR("Delete Node"));
  1233. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, which);
  1234. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, which), which);
  1235. undo_redo->add_do_method(this, "_clear_buffer");
  1236. undo_redo->add_undo_method(this, "_clear_buffer");
  1237. // restore size, inputs and outputs if node is group
  1238. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1239. if (group) {
  1240. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1241. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1242. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1243. }
  1244. // restore expression text if node is expression
  1245. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1246. if (expression) {
  1247. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1248. }
  1249. List<VisualShader::Connection> conns;
  1250. visual_shader->get_node_connections(type, &conns);
  1251. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1252. if (E->get().from_node == which || E->get().to_node == which) {
  1253. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1254. }
  1255. }
  1256. undo_redo->add_do_method(this, "_update_graph");
  1257. undo_redo->add_undo_method(this, "_update_graph");
  1258. undo_redo->commit_action();
  1259. }
  1260. void VisualShaderEditor::_node_selected(Object *p_node) {
  1261. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1262. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  1263. ERR_FAIL_COND(!gn);
  1264. int id = String(gn->get_name()).to_int();
  1265. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  1266. ERR_FAIL_COND(!vsnode.is_valid());
  1267. //do not rely on this, makes editor more complex
  1268. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  1269. }
  1270. void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1271. Ref<InputEventMouseButton> mb = p_event;
  1272. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT)
  1273. _show_members_dialog(true);
  1274. }
  1275. void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos) {
  1276. if (at_mouse_pos) {
  1277. saved_node_pos_dirty = true;
  1278. saved_node_pos = graph->get_local_mouse_position();
  1279. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1280. members_dialog->popup();
  1281. members_dialog->set_position(gpos);
  1282. } else {
  1283. members_dialog->popup();
  1284. saved_node_pos_dirty = false;
  1285. members_dialog->set_position(graph->get_global_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
  1286. }
  1287. // keep dialog within window bounds
  1288. Size2 window_size = OS::get_singleton()->get_window_size();
  1289. Rect2 dialog_rect = members_dialog->get_global_rect();
  1290. if (dialog_rect.position.y + dialog_rect.size.y > window_size.y) {
  1291. int difference = dialog_rect.position.y + dialog_rect.size.y - window_size.y;
  1292. members_dialog->set_position(members_dialog->get_position() - Point2(0, difference));
  1293. }
  1294. if (dialog_rect.position.x + dialog_rect.size.x > window_size.x) {
  1295. int difference = dialog_rect.position.x + dialog_rect.size.x - window_size.x;
  1296. members_dialog->set_position(members_dialog->get_position() - Point2(difference, 0));
  1297. }
  1298. node_filter->call_deferred("grab_focus"); // still not visible
  1299. node_filter->select_all();
  1300. }
  1301. void VisualShaderEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  1302. Ref<InputEventKey> ie = p_ie;
  1303. if (ie.is_valid() && (ie->get_scancode() == KEY_UP ||
  1304. ie->get_scancode() == KEY_DOWN ||
  1305. ie->get_scancode() == KEY_ENTER ||
  1306. ie->get_scancode() == KEY_KP_ENTER)) {
  1307. members->call("_gui_input", ie);
  1308. node_filter->accept_event();
  1309. }
  1310. }
  1311. void VisualShaderEditor::_notification(int p_what) {
  1312. if (p_what == NOTIFICATION_ENTER_TREE) {
  1313. node_filter->set_clear_button_enabled(true);
  1314. // collapse tree by default
  1315. TreeItem *category = members->get_root()->get_children();
  1316. while (category) {
  1317. category->set_collapsed(true);
  1318. TreeItem *sub_category = category->get_children();
  1319. while (sub_category) {
  1320. sub_category->set_collapsed(true);
  1321. sub_category = sub_category->get_next();
  1322. }
  1323. category = category->get_next();
  1324. }
  1325. }
  1326. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1327. Dictionary dd = get_viewport()->gui_get_drag_data();
  1328. if (members->is_visible_in_tree() && dd.has("id")) {
  1329. members->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1330. }
  1331. } else if (p_what == NOTIFICATION_DRAG_END) {
  1332. members->set_drop_mode_flags(0);
  1333. }
  1334. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1335. highend_label->set_modulate(get_color("vulkan_color", "Editor"));
  1336. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  1337. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  1338. node_filter->set_right_icon(Control::get_icon("Search", "EditorIcons"));
  1339. preview_shader->set_icon(Control::get_icon("Shader", "EditorIcons"));
  1340. {
  1341. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  1342. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  1343. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  1344. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  1345. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  1346. preview_text->add_color_override("background_color", background_color);
  1347. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  1348. preview_text->add_keyword_color(E->get(), keyword_color);
  1349. }
  1350. preview_text->add_font_override("font", get_font("expression", "EditorFonts"));
  1351. preview_text->add_color_override("font_color", text_color);
  1352. preview_text->add_color_override("symbol_color", symbol_color);
  1353. preview_text->add_color_region("/*", "*/", comment_color, false);
  1354. preview_text->add_color_region("//", "", comment_color, false);
  1355. error_text->add_font_override("font", get_font("status_source", "EditorFonts"));
  1356. error_text->add_color_override("font_color", get_color("error_color", "Editor"));
  1357. }
  1358. tools->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Tools", "EditorIcons"));
  1359. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree())
  1360. _update_graph();
  1361. }
  1362. }
  1363. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  1364. if (updating)
  1365. return;
  1366. updating = true;
  1367. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  1368. updating = false;
  1369. }
  1370. void VisualShaderEditor::_node_changed(int p_id) {
  1371. if (updating)
  1372. return;
  1373. if (is_visible_in_tree()) {
  1374. _update_graph();
  1375. }
  1376. }
  1377. void VisualShaderEditor::_dup_update_excluded(int p_type, Set<int> &r_excluded) {
  1378. r_excluded.clear();
  1379. VisualShader::Type type = (VisualShader::Type)p_type;
  1380. for (int i = 0; i < graph->get_child_count(); i++) {
  1381. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1382. if (gn) {
  1383. int id = String(gn->get_name()).to_int();
  1384. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1385. Ref<VisualShaderNodeOutput> output = node;
  1386. if (output.is_valid()) {
  1387. r_excluded.insert(id);
  1388. continue;
  1389. }
  1390. r_excluded.insert(id);
  1391. }
  1392. }
  1393. }
  1394. void VisualShaderEditor::_dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded) {
  1395. VisualShader::Type type = (VisualShader::Type)p_type;
  1396. selection_center.x = 0.0f;
  1397. selection_center.y = 0.0f;
  1398. for (int i = 0; i < graph->get_child_count(); i++) {
  1399. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1400. if (gn) {
  1401. int id = String(gn->get_name()).to_int();
  1402. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1403. Ref<VisualShaderNodeOutput> output = node;
  1404. if (output.is_valid()) { // can't duplicate output
  1405. r_excluded.insert(id);
  1406. continue;
  1407. }
  1408. if (node.is_valid() && gn->is_selected()) {
  1409. Vector2 pos = visual_shader->get_node_position(type, id);
  1410. selection_center += pos;
  1411. r_nodes.push_back(id);
  1412. }
  1413. r_excluded.insert(id);
  1414. }
  1415. }
  1416. selection_center /= (float)r_nodes.size();
  1417. }
  1418. void VisualShaderEditor::_dup_paste_nodes(int p_type, int p_pasted_type, List<int> &r_nodes, Set<int> &r_excluded, const Vector2 &p_offset, bool p_select) {
  1419. VisualShader::Type type = (VisualShader::Type)p_type;
  1420. VisualShader::Type pasted_type = (VisualShader::Type)p_pasted_type;
  1421. int base_id = visual_shader->get_valid_node_id(type);
  1422. int id_from = base_id;
  1423. Map<int, int> connection_remap;
  1424. Set<int> unsupported_set;
  1425. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  1426. connection_remap[E->get()] = id_from;
  1427. Ref<VisualShaderNode> node = visual_shader->get_node(pasted_type, E->get());
  1428. bool unsupported = false;
  1429. for (int i = 0; i < add_options.size(); i++) {
  1430. if (add_options[i].type == node->get_class_name()) {
  1431. if (!_is_available(add_options[i].mode)) {
  1432. unsupported = true;
  1433. }
  1434. break;
  1435. }
  1436. }
  1437. if (unsupported) {
  1438. unsupported_set.insert(E->get());
  1439. continue;
  1440. }
  1441. Ref<VisualShaderNode> dupli = node->duplicate();
  1442. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, dupli, visual_shader->get_node_position(pasted_type, E->get()) + p_offset, id_from);
  1443. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  1444. // duplicate size, inputs and outputs if node is group
  1445. Ref<VisualShaderNodeGroupBase> group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1446. if (!group.is_null()) {
  1447. undo_redo->add_do_method(dupli.ptr(), "set_size", group->get_size());
  1448. undo_redo->add_do_method(dupli.ptr(), "set_inputs", group->get_inputs());
  1449. undo_redo->add_do_method(dupli.ptr(), "set_outputs", group->get_outputs());
  1450. }
  1451. // duplicate expression text if node is expression
  1452. Ref<VisualShaderNodeExpression> expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1453. if (!expression.is_null()) {
  1454. undo_redo->add_do_method(dupli.ptr(), "set_expression", expression->get_expression());
  1455. }
  1456. id_from++;
  1457. }
  1458. List<VisualShader::Connection> conns;
  1459. visual_shader->get_node_connections(pasted_type, &conns);
  1460. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1461. if (unsupported_set.has(E->get().from_node) || unsupported_set.has(E->get().to_node)) {
  1462. continue;
  1463. }
  1464. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  1465. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1466. }
  1467. }
  1468. undo_redo->add_do_method(this, "_update_graph");
  1469. undo_redo->add_undo_method(this, "_update_graph");
  1470. undo_redo->commit_action();
  1471. if (p_select) {
  1472. // reselect duplicated nodes by excluding the other ones
  1473. for (int i = 0; i < graph->get_child_count(); i++) {
  1474. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1475. if (gn) {
  1476. int id = String(gn->get_name()).to_int();
  1477. if (!r_excluded.has(id)) {
  1478. gn->set_selected(true);
  1479. } else {
  1480. gn->set_selected(false);
  1481. }
  1482. }
  1483. }
  1484. }
  1485. }
  1486. void VisualShaderEditor::_clear_buffer() {
  1487. copy_nodes_buffer.clear();
  1488. copy_nodes_excluded_buffer.clear();
  1489. }
  1490. void VisualShaderEditor::_duplicate_nodes() {
  1491. int type = edit_type->get_selected();
  1492. List<int> nodes;
  1493. Set<int> excluded;
  1494. _dup_copy_nodes(type, nodes, excluded);
  1495. if (nodes.empty())
  1496. return;
  1497. undo_redo->create_action(TTR("Duplicate Nodes"));
  1498. _dup_paste_nodes(type, type, nodes, excluded, Vector2(10, 10) * EDSCALE, true);
  1499. }
  1500. void VisualShaderEditor::_copy_nodes() {
  1501. copy_type = edit_type->get_selected();
  1502. _clear_buffer();
  1503. _dup_copy_nodes(copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer);
  1504. }
  1505. void VisualShaderEditor::_paste_nodes() {
  1506. if (copy_nodes_buffer.empty())
  1507. return;
  1508. int type = edit_type->get_selected();
  1509. undo_redo->create_action(TTR("Paste Nodes"));
  1510. float scale = graph->get_zoom();
  1511. _dup_paste_nodes(type, copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + graph->get_local_mouse_position() / scale - selection_center), false);
  1512. _dup_update_excluded(type, copy_nodes_excluded_buffer); // to prevent selection of previous copies at new paste
  1513. }
  1514. void VisualShaderEditor::_on_nodes_delete() {
  1515. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1516. List<int> to_erase;
  1517. for (int i = 0; i < graph->get_child_count(); i++) {
  1518. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1519. if (gn) {
  1520. if (gn->is_selected() && gn->is_close_button_visible()) {
  1521. to_erase.push_back(gn->get_name().operator String().to_int());
  1522. }
  1523. }
  1524. }
  1525. if (to_erase.empty())
  1526. return;
  1527. undo_redo->create_action(TTR("Delete Nodes"));
  1528. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1529. Ref<VisualShaderNode> node = visual_shader->get_node(type, F->get());
  1530. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, F->get());
  1531. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, F->get()), F->get());
  1532. undo_redo->add_do_method(this, "_clear_buffer");
  1533. undo_redo->add_undo_method(this, "_clear_buffer");
  1534. // restore size, inputs and outputs if node is group
  1535. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1536. if (group) {
  1537. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1538. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1539. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1540. }
  1541. // restore expression text if node is expression
  1542. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1543. if (expression) {
  1544. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1545. }
  1546. }
  1547. List<VisualShader::Connection> conns;
  1548. visual_shader->get_node_connections(type, &conns);
  1549. List<VisualShader::Connection> used_conns;
  1550. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1551. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1552. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1553. bool cancel = false;
  1554. for (List<VisualShader::Connection>::Element *R = used_conns.front(); R; R = R->next()) {
  1555. if (R->get().from_node == E->get().from_node && R->get().from_port == E->get().from_port && R->get().to_node == E->get().to_node && R->get().to_port == E->get().to_port) {
  1556. cancel = true; // to avoid ERR_ALREADY_EXISTS warning
  1557. break;
  1558. }
  1559. }
  1560. if (!cancel) {
  1561. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1562. used_conns.push_back(E->get());
  1563. }
  1564. }
  1565. }
  1566. }
  1567. undo_redo->add_do_method(this, "_update_graph");
  1568. undo_redo->add_undo_method(this, "_update_graph");
  1569. undo_redo->commit_action();
  1570. }
  1571. void VisualShaderEditor::_mode_selected(int p_id) {
  1572. _update_options_menu();
  1573. _update_graph();
  1574. }
  1575. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> input, String name) {
  1576. String prev_name = input->get_input_name();
  1577. if (name == prev_name)
  1578. return;
  1579. bool type_changed = input->get_input_type_by_name(name) != input->get_input_type_by_name(prev_name);
  1580. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1581. undo_redo->create_action(TTR("Visual Shader Input Type Changed"));
  1582. undo_redo->add_do_method(input.ptr(), "set_input_name", name);
  1583. undo_redo->add_undo_method(input.ptr(), "set_input_name", prev_name);
  1584. if (type_changed) {
  1585. //restore connections if type changed
  1586. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1587. int id = visual_shader->find_node_id(type, input);
  1588. List<VisualShader::Connection> conns;
  1589. visual_shader->get_node_connections(type, &conns);
  1590. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1591. if (E->get().from_node == id) {
  1592. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1593. }
  1594. }
  1595. }
  1596. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1597. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1598. undo_redo->commit_action();
  1599. }
  1600. void VisualShaderEditor::_member_filter_changed(const String &p_text) {
  1601. _update_options_menu();
  1602. }
  1603. void VisualShaderEditor::_member_selected() {
  1604. TreeItem *item = members->get_selected();
  1605. if (item != NULL && item->has_meta("id")) {
  1606. members_dialog->get_ok()->set_disabled(false);
  1607. highend_label->set_visible(add_options[item->get_meta("id")].highend);
  1608. node_desc->set_text(_get_description(item->get_meta("id")));
  1609. } else {
  1610. highend_label->set_visible(false);
  1611. members_dialog->get_ok()->set_disabled(true);
  1612. node_desc->set_text("");
  1613. }
  1614. }
  1615. void VisualShaderEditor::_member_unselected() {
  1616. }
  1617. void VisualShaderEditor::_member_create() {
  1618. TreeItem *item = members->get_selected();
  1619. if (item != NULL && item->has_meta("id")) {
  1620. int idx = members->get_selected()->get_meta("id");
  1621. _add_node(idx, add_options[idx].sub_func);
  1622. members_dialog->hide();
  1623. }
  1624. }
  1625. void VisualShaderEditor::_member_cancel() {
  1626. to_node = -1;
  1627. to_slot = -1;
  1628. from_node = -1;
  1629. from_slot = -1;
  1630. }
  1631. void VisualShaderEditor::_tools_menu_option(int p_idx) {
  1632. TreeItem *category = members->get_root()->get_children();
  1633. switch (p_idx) {
  1634. case EXPAND_ALL:
  1635. while (category) {
  1636. category->set_collapsed(false);
  1637. TreeItem *sub_category = category->get_children();
  1638. while (sub_category) {
  1639. sub_category->set_collapsed(false);
  1640. sub_category = sub_category->get_next();
  1641. }
  1642. category = category->get_next();
  1643. }
  1644. break;
  1645. case COLLAPSE_ALL:
  1646. while (category) {
  1647. category->set_collapsed(true);
  1648. TreeItem *sub_category = category->get_children();
  1649. while (sub_category) {
  1650. sub_category->set_collapsed(true);
  1651. sub_category = sub_category->get_next();
  1652. }
  1653. category = category->get_next();
  1654. }
  1655. break;
  1656. default:
  1657. break;
  1658. }
  1659. }
  1660. Variant VisualShaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1661. if (p_from == members) {
  1662. TreeItem *it = members->get_item_at_position(p_point);
  1663. if (!it)
  1664. return Variant();
  1665. if (!it->has_meta("id"))
  1666. return Variant();
  1667. int id = it->get_meta("id");
  1668. AddOption op = add_options[id];
  1669. Dictionary d;
  1670. d["id"] = id;
  1671. if (op.sub_func == -1) {
  1672. d["sub_func"] = op.sub_func_str;
  1673. } else {
  1674. d["sub_func"] = op.sub_func;
  1675. }
  1676. Label *label = memnew(Label);
  1677. label->set_text(it->get_text(0));
  1678. set_drag_preview(label);
  1679. return d;
  1680. }
  1681. return Variant();
  1682. }
  1683. bool VisualShaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1684. if (p_from == graph) {
  1685. Dictionary d = p_data;
  1686. if (d.has("id")) {
  1687. return true;
  1688. }
  1689. if (d.has("files")) {
  1690. return true;
  1691. }
  1692. }
  1693. return false;
  1694. }
  1695. void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1696. if (p_from == graph) {
  1697. Dictionary d = p_data;
  1698. if (d.has("id")) {
  1699. int idx = d["id"];
  1700. saved_node_pos = p_point;
  1701. saved_node_pos_dirty = true;
  1702. _add_node(idx, add_options[idx].sub_func);
  1703. } else if (d.has("files")) {
  1704. if (d["files"].get_type() == Variant::POOL_STRING_ARRAY) {
  1705. int j = 0;
  1706. PoolStringArray arr = d["files"];
  1707. for (int i = 0; i < arr.size(); i++) {
  1708. String type = ResourceLoader::get_resource_type(arr[i]);
  1709. if (type == "GDScript") {
  1710. Ref<Script> script = ResourceLoader::load(arr[i]);
  1711. if (script->get_instance_base_type() == "VisualShaderNodeCustom") {
  1712. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  1713. saved_node_pos_dirty = true;
  1714. _add_custom_node(arr[i]);
  1715. j++;
  1716. }
  1717. } else if (ClassDB::get_parent_class(type) == "Texture2D") {
  1718. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  1719. saved_node_pos_dirty = true;
  1720. _add_texture_node(arr[i]);
  1721. j++;
  1722. }
  1723. }
  1724. }
  1725. }
  1726. }
  1727. }
  1728. void VisualShaderEditor::_show_preview_text() {
  1729. preview_showed = !preview_showed;
  1730. preview_vbox->set_visible(preview_showed);
  1731. if (preview_showed) {
  1732. if (pending_update_preview) {
  1733. _update_preview();
  1734. pending_update_preview = false;
  1735. }
  1736. }
  1737. }
  1738. void VisualShaderEditor::_update_preview() {
  1739. if (!preview_showed) {
  1740. pending_update_preview = true;
  1741. return;
  1742. }
  1743. String code = visual_shader->get_code();
  1744. preview_text->set_text(code);
  1745. ShaderLanguage sl;
  1746. Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_types());
  1747. for (int i = 0; i < preview_text->get_line_count(); i++) {
  1748. preview_text->set_line_as_marked(i, false);
  1749. }
  1750. if (err != OK) {
  1751. preview_text->set_line_as_marked(sl.get_error_line() - 1, true);
  1752. error_text->set_visible(true);
  1753. String text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  1754. error_text->set_text(text);
  1755. shader_error = true;
  1756. } else {
  1757. error_text->set_visible(false);
  1758. shader_error = false;
  1759. }
  1760. }
  1761. void VisualShaderEditor::_bind_methods() {
  1762. ClassDB::bind_method("_rebuild", &VisualShaderEditor::_rebuild);
  1763. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  1764. ClassDB::bind_method("_update_options_menu", &VisualShaderEditor::_update_options_menu);
  1765. ClassDB::bind_method("_expression_focus_out", &VisualShaderEditor::_expression_focus_out);
  1766. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  1767. ClassDB::bind_method("_node_dragged", &VisualShaderEditor::_node_dragged);
  1768. ClassDB::bind_method("_connection_request", &VisualShaderEditor::_connection_request);
  1769. ClassDB::bind_method("_disconnection_request", &VisualShaderEditor::_disconnection_request);
  1770. ClassDB::bind_method("_node_selected", &VisualShaderEditor::_node_selected);
  1771. ClassDB::bind_method("_scroll_changed", &VisualShaderEditor::_scroll_changed);
  1772. ClassDB::bind_method("_delete_request", &VisualShaderEditor::_delete_request);
  1773. ClassDB::bind_method("_on_nodes_delete", &VisualShaderEditor::_on_nodes_delete);
  1774. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  1775. ClassDB::bind_method("_edit_port_default_input", &VisualShaderEditor::_edit_port_default_input);
  1776. ClassDB::bind_method("_port_edited", &VisualShaderEditor::_port_edited);
  1777. ClassDB::bind_method("_connection_to_empty", &VisualShaderEditor::_connection_to_empty);
  1778. ClassDB::bind_method("_connection_from_empty", &VisualShaderEditor::_connection_from_empty);
  1779. ClassDB::bind_method("_line_edit_focus_out", &VisualShaderEditor::_line_edit_focus_out);
  1780. ClassDB::bind_method("_line_edit_changed", &VisualShaderEditor::_line_edit_changed);
  1781. ClassDB::bind_method("_port_name_focus_out", &VisualShaderEditor::_port_name_focus_out);
  1782. ClassDB::bind_method("_duplicate_nodes", &VisualShaderEditor::_duplicate_nodes);
  1783. ClassDB::bind_method("_copy_nodes", &VisualShaderEditor::_copy_nodes);
  1784. ClassDB::bind_method("_paste_nodes", &VisualShaderEditor::_paste_nodes);
  1785. ClassDB::bind_method("_mode_selected", &VisualShaderEditor::_mode_selected);
  1786. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  1787. ClassDB::bind_method("_preview_select_port", &VisualShaderEditor::_preview_select_port);
  1788. ClassDB::bind_method("_graph_gui_input", &VisualShaderEditor::_graph_gui_input);
  1789. ClassDB::bind_method("_add_input_port", &VisualShaderEditor::_add_input_port);
  1790. ClassDB::bind_method("_change_input_port_type", &VisualShaderEditor::_change_input_port_type);
  1791. ClassDB::bind_method("_change_input_port_name", &VisualShaderEditor::_change_input_port_name);
  1792. ClassDB::bind_method("_remove_input_port", &VisualShaderEditor::_remove_input_port);
  1793. ClassDB::bind_method("_add_output_port", &VisualShaderEditor::_add_output_port);
  1794. ClassDB::bind_method("_change_output_port_type", &VisualShaderEditor::_change_output_port_type);
  1795. ClassDB::bind_method("_change_output_port_name", &VisualShaderEditor::_change_output_port_name);
  1796. ClassDB::bind_method("_remove_output_port", &VisualShaderEditor::_remove_output_port);
  1797. ClassDB::bind_method("_node_resized", &VisualShaderEditor::_node_resized);
  1798. ClassDB::bind_method("_set_node_size", &VisualShaderEditor::_set_node_size);
  1799. ClassDB::bind_method("_clear_buffer", &VisualShaderEditor::_clear_buffer);
  1800. ClassDB::bind_method("_show_preview_text", &VisualShaderEditor::_show_preview_text);
  1801. ClassDB::bind_method("_update_preview", &VisualShaderEditor::_update_preview);
  1802. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &VisualShaderEditor::get_drag_data_fw);
  1803. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &VisualShaderEditor::can_drop_data_fw);
  1804. ClassDB::bind_method(D_METHOD("drop_data_fw"), &VisualShaderEditor::drop_data_fw);
  1805. ClassDB::bind_method("_is_available", &VisualShaderEditor::_is_available);
  1806. ClassDB::bind_method("_tools_menu_option", &VisualShaderEditor::_tools_menu_option);
  1807. ClassDB::bind_method("_show_members_dialog", &VisualShaderEditor::_show_members_dialog);
  1808. ClassDB::bind_method("_sbox_input", &VisualShaderEditor::_sbox_input);
  1809. ClassDB::bind_method("_member_filter_changed", &VisualShaderEditor::_member_filter_changed);
  1810. ClassDB::bind_method("_member_selected", &VisualShaderEditor::_member_selected);
  1811. ClassDB::bind_method("_member_unselected", &VisualShaderEditor::_member_unselected);
  1812. ClassDB::bind_method("_member_create", &VisualShaderEditor::_member_create);
  1813. ClassDB::bind_method("_member_cancel", &VisualShaderEditor::_member_cancel);
  1814. }
  1815. VisualShaderEditor *VisualShaderEditor::singleton = NULL;
  1816. VisualShaderEditor::VisualShaderEditor() {
  1817. singleton = this;
  1818. updating = false;
  1819. saved_node_pos_dirty = false;
  1820. saved_node_pos = Point2(0, 0);
  1821. ShaderLanguage::get_keyword_list(&keyword_list);
  1822. preview_showed = false;
  1823. pending_update_preview = false;
  1824. shader_error = false;
  1825. to_node = -1;
  1826. to_slot = -1;
  1827. from_node = -1;
  1828. from_slot = -1;
  1829. main_box = memnew(HSplitContainer);
  1830. main_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1831. main_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1832. add_child(main_box);
  1833. graph = memnew(GraphEdit);
  1834. graph->get_zoom_hbox()->set_h_size_flags(SIZE_EXPAND_FILL);
  1835. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  1836. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  1837. main_box->add_child(graph);
  1838. graph->set_drag_forwarding(this);
  1839. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  1840. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_BOOLEAN);
  1841. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  1842. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  1843. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SAMPLER);
  1844. //graph->add_valid_left_disconnect_type(0);
  1845. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  1846. graph->connect("connection_request", this, "_connection_request", varray(), CONNECT_DEFERRED);
  1847. graph->connect("disconnection_request", this, "_disconnection_request", varray(), CONNECT_DEFERRED);
  1848. graph->connect("node_selected", this, "_node_selected");
  1849. graph->connect("scroll_offset_changed", this, "_scroll_changed");
  1850. graph->connect("duplicate_nodes_request", this, "_duplicate_nodes");
  1851. graph->connect("copy_nodes_request", this, "_copy_nodes");
  1852. graph->connect("paste_nodes_request", this, "_paste_nodes");
  1853. graph->connect("delete_nodes_request", this, "_on_nodes_delete");
  1854. graph->connect("gui_input", this, "_graph_gui_input");
  1855. graph->connect("connection_to_empty", this, "_connection_to_empty");
  1856. graph->connect("connection_from_empty", this, "_connection_from_empty");
  1857. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  1858. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  1859. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1860. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  1861. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  1862. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1863. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR);
  1864. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_VECTOR);
  1865. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1866. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  1867. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SAMPLER, VisualShaderNode::PORT_TYPE_SAMPLER);
  1868. VSeparator *vs = memnew(VSeparator);
  1869. graph->get_zoom_hbox()->add_child(vs);
  1870. graph->get_zoom_hbox()->move_child(vs, 0);
  1871. edit_type = memnew(OptionButton);
  1872. edit_type->add_item(TTR("Vertex"));
  1873. edit_type->add_item(TTR("Fragment"));
  1874. edit_type->add_item(TTR("Light"));
  1875. edit_type->select(1);
  1876. edit_type->connect("item_selected", this, "_mode_selected");
  1877. graph->get_zoom_hbox()->add_child(edit_type);
  1878. graph->get_zoom_hbox()->move_child(edit_type, 0);
  1879. add_node = memnew(ToolButton);
  1880. graph->get_zoom_hbox()->add_child(add_node);
  1881. add_node->set_text(TTR("Add Node..."));
  1882. graph->get_zoom_hbox()->move_child(add_node, 0);
  1883. add_node->connect("pressed", this, "_show_members_dialog", varray(false));
  1884. preview_shader = memnew(ToolButton);
  1885. preview_shader->set_toggle_mode(true);
  1886. preview_shader->set_tooltip(TTR("Show resulted shader code."));
  1887. graph->get_zoom_hbox()->add_child(preview_shader);
  1888. preview_shader->connect("pressed", this, "_show_preview_text");
  1889. ///////////////////////////////////////
  1890. // PREVIEW PANEL
  1891. ///////////////////////////////////////
  1892. preview_vbox = memnew(VBoxContainer);
  1893. preview_vbox->set_visible(preview_showed);
  1894. main_box->add_child(preview_vbox);
  1895. preview_text = memnew(TextEdit);
  1896. preview_vbox->add_child(preview_text);
  1897. preview_text->set_h_size_flags(SIZE_EXPAND_FILL);
  1898. preview_text->set_v_size_flags(SIZE_EXPAND_FILL);
  1899. preview_text->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
  1900. preview_text->set_syntax_coloring(true);
  1901. preview_text->set_show_line_numbers(true);
  1902. preview_text->set_readonly(true);
  1903. error_text = memnew(Label);
  1904. preview_vbox->add_child(error_text);
  1905. error_text->set_visible(false);
  1906. ///////////////////////////////////////
  1907. // SHADER NODES TREE
  1908. ///////////////////////////////////////
  1909. VBoxContainer *members_vb = memnew(VBoxContainer);
  1910. members_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1911. HBoxContainer *filter_hb = memnew(HBoxContainer);
  1912. members_vb->add_child(filter_hb);
  1913. node_filter = memnew(LineEdit);
  1914. filter_hb->add_child(node_filter);
  1915. node_filter->connect("text_changed", this, "_member_filter_changed");
  1916. node_filter->connect("gui_input", this, "_sbox_input");
  1917. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1918. node_filter->set_placeholder(TTR("Search"));
  1919. tools = memnew(MenuButton);
  1920. filter_hb->add_child(tools);
  1921. tools->set_tooltip(TTR("Options"));
  1922. tools->get_popup()->connect("id_pressed", this, "_tools_menu_option");
  1923. tools->get_popup()->add_item(TTR("Expand All"), EXPAND_ALL);
  1924. tools->get_popup()->add_item(TTR("Collapse All"), COLLAPSE_ALL);
  1925. members = memnew(Tree);
  1926. members_vb->add_child(members);
  1927. members->set_drag_forwarding(this);
  1928. members->set_h_size_flags(SIZE_EXPAND_FILL);
  1929. members->set_v_size_flags(SIZE_EXPAND_FILL);
  1930. members->set_hide_root(true);
  1931. members->set_allow_reselect(true);
  1932. members->set_hide_folding(false);
  1933. members->set_custom_minimum_size(Size2(180 * EDSCALE, 200 * EDSCALE));
  1934. members->connect("item_activated", this, "_member_create");
  1935. members->connect("item_selected", this, "_member_selected");
  1936. members->connect("nothing_selected", this, "_member_unselected");
  1937. HBoxContainer *desc_hbox = memnew(HBoxContainer);
  1938. members_vb->add_child(desc_hbox);
  1939. Label *desc_label = memnew(Label);
  1940. desc_hbox->add_child(desc_label);
  1941. desc_label->set_text(TTR("Description:"));
  1942. desc_hbox->add_spacer();
  1943. highend_label = memnew(Label);
  1944. desc_hbox->add_child(highend_label);
  1945. highend_label->set_visible(false);
  1946. highend_label->set_text("Vulkan");
  1947. highend_label->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  1948. highend_label->set_tooltip(TTR("High-end node"));
  1949. node_desc = memnew(RichTextLabel);
  1950. members_vb->add_child(node_desc);
  1951. node_desc->set_h_size_flags(SIZE_EXPAND_FILL);
  1952. node_desc->set_v_size_flags(SIZE_FILL);
  1953. node_desc->set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  1954. members_dialog = memnew(ConfirmationDialog);
  1955. members_dialog->set_title(TTR("Create Shader Node"));
  1956. members_dialog->add_child(members_vb);
  1957. members_dialog->get_ok()->set_text(TTR("Create"));
  1958. members_dialog->get_ok()->connect("pressed", this, "_member_create");
  1959. members_dialog->get_ok()->set_disabled(true);
  1960. members_dialog->set_resizable(true);
  1961. members_dialog->set_as_minsize();
  1962. members_dialog->connect("hide", this, "_member_cancel");
  1963. add_child(members_dialog);
  1964. alert = memnew(AcceptDialog);
  1965. alert->set_as_minsize();
  1966. alert->get_label()->set_autowrap(true);
  1967. alert->get_label()->set_align(Label::ALIGN_CENTER);
  1968. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  1969. alert->get_label()->set_custom_minimum_size(Size2(400, 60) * EDSCALE);
  1970. add_child(alert);
  1971. ///////////////////////////////////////
  1972. // SHADER NODES TREE OPTIONS
  1973. ///////////////////////////////////////
  1974. // COLOR
  1975. add_options.push_back(AddOption("ColorFunc", "Color", "Common", "VisualShaderNodeColorFunc", TTR("Color function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  1976. add_options.push_back(AddOption("ColorOp", "Color", "Common", "VisualShaderNodeColorOp", TTR("Color operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  1977. add_options.push_back(AddOption("Grayscale", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Grayscale function."), VisualShaderNodeColorFunc::FUNC_GRAYSCALE, VisualShaderNode::PORT_TYPE_VECTOR));
  1978. add_options.push_back(AddOption("HSV2RGB", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts HSV vector to RGB equivalent."), VisualShaderNodeVectorFunc::FUNC_HSV2RGB, VisualShaderNode::PORT_TYPE_VECTOR));
  1979. add_options.push_back(AddOption("RGB2HSV", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts RGB vector to HSV equivalent."), VisualShaderNodeVectorFunc::FUNC_RGB2HSV, VisualShaderNode::PORT_TYPE_VECTOR));
  1980. add_options.push_back(AddOption("Sepia", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Sepia function."), VisualShaderNodeColorFunc::FUNC_SEPIA, VisualShaderNode::PORT_TYPE_VECTOR));
  1981. add_options.push_back(AddOption("Burn", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Burn operator."), VisualShaderNodeColorOp::OP_BURN, VisualShaderNode::PORT_TYPE_VECTOR));
  1982. add_options.push_back(AddOption("Darken", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Darken operator."), VisualShaderNodeColorOp::OP_DARKEN, VisualShaderNode::PORT_TYPE_VECTOR));
  1983. add_options.push_back(AddOption("Difference", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Difference operator."), VisualShaderNodeColorOp::OP_DIFFERENCE, VisualShaderNode::PORT_TYPE_VECTOR));
  1984. add_options.push_back(AddOption("Dodge", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Dodge operator."), VisualShaderNodeColorOp::OP_DODGE, VisualShaderNode::PORT_TYPE_VECTOR));
  1985. add_options.push_back(AddOption("HardLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("HardLight operator."), VisualShaderNodeColorOp::OP_HARD_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  1986. add_options.push_back(AddOption("Lighten", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Lighten operator."), VisualShaderNodeColorOp::OP_LIGHTEN, VisualShaderNode::PORT_TYPE_VECTOR));
  1987. add_options.push_back(AddOption("Overlay", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Overlay operator."), VisualShaderNodeColorOp::OP_OVERLAY, VisualShaderNode::PORT_TYPE_VECTOR));
  1988. add_options.push_back(AddOption("Screen", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Screen operator."), VisualShaderNodeColorOp::OP_SCREEN, VisualShaderNode::PORT_TYPE_VECTOR));
  1989. add_options.push_back(AddOption("SoftLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("SoftLight operator."), VisualShaderNodeColorOp::OP_SOFT_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  1990. add_options.push_back(AddOption("ColorConstant", "Color", "Variables", "VisualShaderNodeColorConstant", TTR("Color constant."), -1, -1));
  1991. add_options.push_back(AddOption("ColorUniform", "Color", "Variables", "VisualShaderNodeColorUniform", TTR("Color uniform."), -1, -1));
  1992. // CONDITIONAL
  1993. const String &compare_func_desc = TTR("Returns the boolean result of the %s comparison between two parameters.");
  1994. add_options.push_back(AddOption("Equal", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Equal (==)")), VisualShaderNodeCompare::FUNC_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  1995. add_options.push_back(AddOption("GreaterThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than (>)")), VisualShaderNodeCompare::FUNC_GREATER_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  1996. add_options.push_back(AddOption("GreaterThanEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than or Equal (>=)")), VisualShaderNodeCompare::FUNC_GREATER_THAN_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  1997. add_options.push_back(AddOption("If", "Conditional", "Functions", "VisualShaderNodeIf", TTR("Returns an associated vector if the provided scalars are equal, greater or less."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  1998. add_options.push_back(AddOption("IsInf", "Conditional", "Functions", "VisualShaderNodeIs", TTR("Returns the boolean result of the comparison between INF and a scalar parameter."), VisualShaderNodeIs::FUNC_IS_INF, VisualShaderNode::PORT_TYPE_BOOLEAN));
  1999. add_options.push_back(AddOption("IsNaN", "Conditional", "Functions", "VisualShaderNodeIs", TTR("Returns the boolean result of the comparison between NaN and a scalar parameter."), VisualShaderNodeIs::FUNC_IS_NAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2000. add_options.push_back(AddOption("LessThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than (<)")), VisualShaderNodeCompare::FUNC_LESS_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2001. add_options.push_back(AddOption("LessThanEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than or Equal (<=)")), VisualShaderNodeCompare::FUNC_LESS_THAN_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2002. add_options.push_back(AddOption("NotEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Not Equal (!=)")), VisualShaderNodeCompare::FUNC_NOT_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2003. add_options.push_back(AddOption("Switch", "Conditional", "Functions", "VisualShaderNodeSwitch", TTR("Returns an associated vector if the provided boolean value is true or false."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2004. add_options.push_back(AddOption("SwitchS", "Conditional", "Functions", "VisualShaderNodeScalarSwitch", TTR("Returns an associated scalar if the provided boolean value is true or false."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2005. add_options.push_back(AddOption("Compare", "Conditional", "Common", "VisualShaderNodeCompare", TTR("Returns the boolean result of the comparison between two parameters."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2006. add_options.push_back(AddOption("Is", "Conditional", "Common", "VisualShaderNodeIs", TTR("Returns the boolean result of the comparison between INF (or NaN) and a scalar parameter."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2007. add_options.push_back(AddOption("BooleanConstant", "Conditional", "Variables", "VisualShaderNodeBooleanConstant", TTR("Boolean constant."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2008. add_options.push_back(AddOption("BooleanUniform", "Conditional", "Variables", "VisualShaderNodeBooleanUniform", TTR("Boolean uniform."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2009. // INPUT
  2010. // SPATIAL-FOR-ALL
  2011. const String input_param_shader_modes = TTR("'%s' input parameter for all shader modes.");
  2012. add_options.push_back(AddOption("Camera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2013. add_options.push_back(AddOption("InvCamera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "inv_camera"), "inv_camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2014. add_options.push_back(AddOption("InvProjection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "inv_projection"), "inv_projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2015. add_options.push_back(AddOption("Normal", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  2016. add_options.push_back(AddOption("OutputIsSRGB", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "output_is_srgb"), "output_is_srgb", VisualShaderNode::PORT_TYPE_BOOLEAN, -1, Shader::MODE_SPATIAL));
  2017. add_options.push_back(AddOption("Projection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2018. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_SPATIAL));
  2019. add_options.push_back(AddOption("ViewportSize", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "viewport_size"), "viewport_size", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  2020. add_options.push_back(AddOption("World", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2021. // CANVASITEM-FOR-ALL
  2022. add_options.push_back(AddOption("Alpha", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_CANVAS_ITEM));
  2023. add_options.push_back(AddOption("Color", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2024. add_options.push_back(AddOption("TexturePixelSize", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "texture_pixel_size"), "texture_pixel_size", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2025. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_CANVAS_ITEM));
  2026. add_options.push_back(AddOption("UV", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2027. /////////////////
  2028. add_options.push_back(AddOption("Input", "Input", "Common", "VisualShaderNodeInput", TTR("Input parameter.")));
  2029. // SPATIAL INPUTS
  2030. const String input_param_for_vertex_and_fragment_shader_modes = TTR("'%s' input parameter for vertex and fragment shader modes.");
  2031. const String input_param_for_fragment_and_light_shader_modes = TTR("'%s' input parameter for fragment and light shader modes.");
  2032. const String input_param_for_fragment_shader_mode = TTR("'%s' input parameter for fragment shader mode.");
  2033. const String input_param_for_light_shader_mode = TTR("'%s' input parameter for light shader mode.");
  2034. const String input_param_for_vertex_shader_mode = TTR("'%s' input parameter for vertex shader mode.");
  2035. const String input_param_for_vertex_and_fragment_shader_mode = TTR("'%s' input parameter for vertex and fragment shader mode.");
  2036. add_options.push_back(AddOption("Alpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2037. add_options.push_back(AddOption("Binormal", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2038. add_options.push_back(AddOption("Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2039. add_options.push_back(AddOption("DepthTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "depth_texture"), "depth_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2040. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2041. add_options.push_back(AddOption("FrontFacing", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "front_facing"), "front_facing", VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2042. add_options.push_back(AddOption("PointCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2043. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2044. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2045. add_options.push_back(AddOption("Side", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "side"), "side", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2046. add_options.push_back(AddOption("Tangent", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2047. add_options.push_back(AddOption("UV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2048. add_options.push_back(AddOption("UV2", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2049. add_options.push_back(AddOption("Vertex", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2050. add_options.push_back(AddOption("View", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2051. add_options.push_back(AddOption("Albedo", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "albedo"), "albedo", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2052. add_options.push_back(AddOption("Attenuation", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "attenuation"), "attenuation", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2053. add_options.push_back(AddOption("Diffuse", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "diffuse"), "diffuse", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2054. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2055. add_options.push_back(AddOption("Light", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light"), "light", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2056. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2057. add_options.push_back(AddOption("Roughness", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "roughness"), "roughness", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2058. add_options.push_back(AddOption("Specular", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "specular"), "specular", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2059. add_options.push_back(AddOption("Transmission", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "transmission"), "transmission", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2060. add_options.push_back(AddOption("View", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2061. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2062. add_options.push_back(AddOption("Binormal", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2063. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2064. add_options.push_back(AddOption("ModelView", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "modelview"), "modelview", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2065. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2066. add_options.push_back(AddOption("Tangent", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_mode, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2067. add_options.push_back(AddOption("UV", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2068. add_options.push_back(AddOption("UV2", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2069. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2070. // CANVASITEM INPUTS
  2071. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2072. add_options.push_back(AddOption("LightPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "light_pass"), "light_pass", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2073. add_options.push_back(AddOption("NormalTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "normal_texture"), "normal_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2074. add_options.push_back(AddOption("PointCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2075. add_options.push_back(AddOption("ScreenPixelSize", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_pixel_size"), "screen_pixel_size", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2076. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2077. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2078. add_options.push_back(AddOption("Texture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2079. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2080. add_options.push_back(AddOption("LightAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_alpha"), "light_alpha", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2081. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2082. add_options.push_back(AddOption("LightHeight", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_height"), "light_height", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2083. add_options.push_back(AddOption("LightUV", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_uv"), "light_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2084. add_options.push_back(AddOption("LightVector", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_vec"), "light_vec", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2085. add_options.push_back(AddOption("Normal", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2086. add_options.push_back(AddOption("PointCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2087. add_options.push_back(AddOption("ScreenUV", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2088. add_options.push_back(AddOption("ShadowColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_color"), "shadow_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2089. add_options.push_back(AddOption("Texture", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2090. add_options.push_back(AddOption("Extra", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "extra"), "extra", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2091. add_options.push_back(AddOption("LightPass", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "light_pass"), "light_pass", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2092. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2093. add_options.push_back(AddOption("Projection", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "projection"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2094. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2095. add_options.push_back(AddOption("World", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2096. // PARTICLES INPUTS
  2097. add_options.push_back(AddOption("Active", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2098. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2099. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2100. add_options.push_back(AddOption("Custom", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2101. add_options.push_back(AddOption("CustomAlpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2102. add_options.push_back(AddOption("Delta", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2103. add_options.push_back(AddOption("EmissionTransform", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2104. add_options.push_back(AddOption("Index", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2105. add_options.push_back(AddOption("LifeTime", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2106. add_options.push_back(AddOption("Restart", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2107. add_options.push_back(AddOption("Time", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2108. add_options.push_back(AddOption("Transform", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2109. add_options.push_back(AddOption("Velocity", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2110. // SCALAR
  2111. add_options.push_back(AddOption("ScalarFunc", "Scalar", "Common", "VisualShaderNodeScalarFunc", TTR("Scalar function."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2112. add_options.push_back(AddOption("ScalarOp", "Scalar", "Common", "VisualShaderNodeScalarOp", TTR("Scalar operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2113. //CONSTANTS
  2114. add_options.push_back(AddOption("E", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("E constant (2.718282). Represents the base of the natural logarithm."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_E));
  2115. add_options.push_back(AddOption("Epsilon", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Epsilon constant (0.00001). Smallest possible scalar number."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, CMP_EPSILON));
  2116. add_options.push_back(AddOption("Phi", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Phi constant (1.618034). Golden ratio."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, 1.618034f));
  2117. add_options.push_back(AddOption("Pi/4", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi/4 constant (0.785398) or 45 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 4));
  2118. add_options.push_back(AddOption("Pi/2", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi/2 constant (1.570796) or 90 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 2));
  2119. add_options.push_back(AddOption("Pi", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi constant (3.141593) or 180 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI));
  2120. add_options.push_back(AddOption("Tau", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Tau constant (6.283185) or 360 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_TAU));
  2121. add_options.push_back(AddOption("Sqrt2", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Sqrt2 constant (1.414214). Square root of 2."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_SQRT2));
  2122. // FUNCTIONS
  2123. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeScalarFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR));
  2124. add_options.push_back(AddOption("ACos", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_SCALAR));
  2125. add_options.push_back(AddOption("ACosH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ACOSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2126. add_options.push_back(AddOption("ASin", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2127. add_options.push_back(AddOption("ASinH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ASINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2128. add_options.push_back(AddOption("ATan", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2129. add_options.push_back(AddOption("ATan2", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeScalarOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_SCALAR));
  2130. add_options.push_back(AddOption("ATanH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_ATANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2131. add_options.push_back(AddOption("Ceil", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer that is greater than or equal to the parameter."), VisualShaderNodeScalarFunc::FUNC_CEIL, VisualShaderNode::PORT_TYPE_SCALAR));
  2132. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeScalarClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2133. add_options.push_back(AddOption("Cos", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_SCALAR));
  2134. add_options.push_back(AddOption("CosH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2135. add_options.push_back(AddOption("Degrees", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeScalarFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_SCALAR));
  2136. add_options.push_back(AddOption("Exp", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-e Exponential."), VisualShaderNodeScalarFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_SCALAR));
  2137. add_options.push_back(AddOption("Exp2", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-2 Exponential."), VisualShaderNodeScalarFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_SCALAR));
  2138. add_options.push_back(AddOption("Floor", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer less than or equal to the parameter."), VisualShaderNodeScalarFunc::FUNC_FLOOR, VisualShaderNode::PORT_TYPE_SCALAR));
  2139. add_options.push_back(AddOption("Fract", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeScalarFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_SCALAR));
  2140. add_options.push_back(AddOption("InverseSqrt", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse of the square root of the parameter."), VisualShaderNodeScalarFunc::FUNC_INVERSE_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2141. add_options.push_back(AddOption("Log", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Natural logarithm."), VisualShaderNodeScalarFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_SCALAR));
  2142. add_options.push_back(AddOption("Log2", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-2 logarithm."), VisualShaderNodeScalarFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_SCALAR));
  2143. add_options.push_back(AddOption("Max", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the greater of two values."), VisualShaderNodeScalarOp::OP_MAX, VisualShaderNode::PORT_TYPE_SCALAR));
  2144. add_options.push_back(AddOption("Min", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the lesser of two values."), VisualShaderNodeScalarOp::OP_MIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2145. add_options.push_back(AddOption("Mix", "Scalar", "Functions", "VisualShaderNodeScalarInterp", TTR("Linear interpolation between two scalars."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2146. add_options.push_back(AddOption("Negate", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeScalarFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2147. add_options.push_back(AddOption("OneMinus", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("1.0 - scalar"), VisualShaderNodeScalarFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_SCALAR));
  2148. add_options.push_back(AddOption("Pow", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the value of the first parameter raised to the power of the second."), VisualShaderNodeScalarOp::OP_POW, VisualShaderNode::PORT_TYPE_SCALAR));
  2149. add_options.push_back(AddOption("Radians", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeScalarFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_SCALAR));
  2150. add_options.push_back(AddOption("Reciprocal", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("1.0 / scalar"), VisualShaderNodeScalarFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_SCALAR));
  2151. add_options.push_back(AddOption("Round", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeScalarFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_SCALAR));
  2152. add_options.push_back(AddOption("RoundEven", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest even integer to the parameter."), VisualShaderNodeScalarFunc::FUNC_ROUNDEVEN, VisualShaderNode::PORT_TYPE_SCALAR));
  2153. add_options.push_back(AddOption("Saturate", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Clamps the value between 0.0 and 1.0."), VisualShaderNodeScalarFunc::FUNC_SATURATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2154. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeScalarFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR));
  2155. add_options.push_back(AddOption("Sin", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2156. add_options.push_back(AddOption("SinH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2157. add_options.push_back(AddOption("Sqrt", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeScalarFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2158. add_options.push_back(AddOption("SmoothStep", "Scalar", "Functions", "VisualShaderNodeScalarSmoothStep", TTR("SmoothStep function( scalar(edge0), scalar(edge1), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge0' and 1.0 if x is larger than 'edge1'. Otherwise the return value is interpolated between 0.0 and 1.0 using Hermite polynomials."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2159. add_options.push_back(AddOption("Step", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Step function( scalar(edge), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeScalarOp::OP_STEP, VisualShaderNode::PORT_TYPE_SCALAR));
  2160. add_options.push_back(AddOption("Tan", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2161. add_options.push_back(AddOption("TanH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2162. add_options.push_back(AddOption("Trunc", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeScalarFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_SCALAR));
  2163. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Adds scalar to scalar."), VisualShaderNodeScalarOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR));
  2164. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Divides scalar by scalar."), VisualShaderNodeScalarOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR));
  2165. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Multiplies scalar by scalar."), VisualShaderNodeScalarOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR));
  2166. add_options.push_back(AddOption("Remainder", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Returns the remainder of the two scalars."), VisualShaderNodeScalarOp::OP_MOD, VisualShaderNode::PORT_TYPE_SCALAR));
  2167. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Subtracts scalar from scalar."), VisualShaderNodeScalarOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR));
  2168. add_options.push_back(AddOption("ScalarConstant", "Scalar", "Variables", "VisualShaderNodeScalarConstant", TTR("Scalar constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2169. add_options.push_back(AddOption("ScalarUniform", "Scalar", "Variables", "VisualShaderNodeScalarUniform", TTR("Scalar uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2170. // TEXTURES
  2171. add_options.push_back(AddOption("CubeMap", "Textures", "Functions", "VisualShaderNodeCubeMap", TTR("Perform the cubic texture lookup."), -1, -1));
  2172. texture_node_option_idx = add_options.size();
  2173. add_options.push_back(AddOption("Texture2D", "Textures", "Functions", "VisualShaderNodeTexture", TTR("Perform the texture lookup."), -1, -1));
  2174. add_options.push_back(AddOption("CubeMapUniform", "Textures", "Variables", "VisualShaderNodeCubeMapUniform", TTR("Cubic texture uniform lookup."), -1, -1));
  2175. add_options.push_back(AddOption("TextureUniform", "Textures", "Variables", "VisualShaderNodeTextureUniform", TTR("2D texture uniform lookup."), -1, -1));
  2176. add_options.push_back(AddOption("TextureUniformTriplanar", "Textures", "Variables", "VisualShaderNodeTextureUniformTriplanar", TTR("2D texture uniform lookup with triplanar."), -1, -1, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2177. // TRANSFORM
  2178. add_options.push_back(AddOption("TransformFunc", "Transform", "Common", "VisualShaderNodeTransformFunc", TTR("Transform function."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2179. add_options.push_back(AddOption("OuterProduct", "Transform", "Composition", "VisualShaderNodeOuterProduct", TTR("Calculate the outer product of a pair of vectors.\n\nOuterProduct treats the first parameter 'c' as a column vector (matrix with one column) and the second parameter 'r' as a row vector (matrix with one row) and does a linear algebraic matrix multiply 'c * r', yielding a matrix whose number of rows is the number of components in 'c' and whose number of columns is the number of components in 'r'."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2180. add_options.push_back(AddOption("TransformCompose", "Transform", "Composition", "VisualShaderNodeTransformCompose", TTR("Composes transform from four vectors."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2181. add_options.push_back(AddOption("TransformDecompose", "Transform", "Composition", "VisualShaderNodeTransformDecompose", TTR("Decomposes transform to four vectors.")));
  2182. add_options.push_back(AddOption("Determinant", "Transform", "Functions", "VisualShaderNodeDeterminant", TTR("Calculates the determinant of a transform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2183. add_options.push_back(AddOption("Inverse", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the inverse of a transform."), VisualShaderNodeTransformFunc::FUNC_INVERSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2184. add_options.push_back(AddOption("Transpose", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the transpose of a transform."), VisualShaderNodeTransformFunc::FUNC_TRANSPOSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2185. add_options.push_back(AddOption("TransformMult", "Transform", "Operators", "VisualShaderNodeTransformMult", TTR("Multiplies transform by transform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2186. add_options.push_back(AddOption("TransformVectorMult", "Transform", "Operators", "VisualShaderNodeTransformVecMult", TTR("Multiplies vector by transform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2187. add_options.push_back(AddOption("TransformConstant", "Transform", "Variables", "VisualShaderNodeTransformConstant", TTR("Transform constant."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2188. add_options.push_back(AddOption("TransformUniform", "Transform", "Variables", "VisualShaderNodeTransformUniform", TTR("Transform uniform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2189. // VECTOR
  2190. add_options.push_back(AddOption("VectorFunc", "Vector", "Common", "VisualShaderNodeVectorFunc", TTR("Vector function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2191. add_options.push_back(AddOption("VectorOp", "Vector", "Common", "VisualShaderNodeVectorOp", TTR("Vector operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2192. add_options.push_back(AddOption("VectorCompose", "Vector", "Composition", "VisualShaderNodeVectorCompose", TTR("Composes vector from three scalars."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2193. add_options.push_back(AddOption("VectorDecompose", "Vector", "Composition", "VisualShaderNodeVectorDecompose", TTR("Decomposes vector to three scalars.")));
  2194. add_options.push_back(AddOption("Abs", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeVectorFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_VECTOR));
  2195. add_options.push_back(AddOption("ACos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_VECTOR));
  2196. add_options.push_back(AddOption("ACosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOSH, VisualShaderNode::PORT_TYPE_VECTOR));
  2197. add_options.push_back(AddOption("ASin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2198. add_options.push_back(AddOption("ASinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASINH, VisualShaderNode::PORT_TYPE_VECTOR));
  2199. add_options.push_back(AddOption("ATan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2200. add_options.push_back(AddOption("ATan2", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeVectorOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_VECTOR));
  2201. add_options.push_back(AddOption("ATanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATANH, VisualShaderNode::PORT_TYPE_VECTOR));
  2202. add_options.push_back(AddOption("Ceil", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer that is greater than or equal to the parameter."), VisualShaderNodeVectorFunc::FUNC_CEIL, VisualShaderNode::PORT_TYPE_VECTOR));
  2203. add_options.push_back(AddOption("Clamp", "Vector", "Functions", "VisualShaderNodeVectorClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2204. add_options.push_back(AddOption("Cos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_VECTOR));
  2205. add_options.push_back(AddOption("CosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_VECTOR));
  2206. add_options.push_back(AddOption("Cross", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Calculates the cross product of two vectors."), VisualShaderNodeVectorOp::OP_CROSS, VisualShaderNode::PORT_TYPE_VECTOR));
  2207. add_options.push_back(AddOption("Degrees", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeVectorFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_VECTOR));
  2208. add_options.push_back(AddOption("Distance", "Vector", "Functions", "VisualShaderNodeVectorDistance", TTR("Returns the distance between two points."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2209. add_options.push_back(AddOption("Dot", "Vector", "Functions", "VisualShaderNodeDotProduct", TTR("Calculates the dot product of two vectors."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2210. add_options.push_back(AddOption("Exp", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-e Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_VECTOR));
  2211. add_options.push_back(AddOption("Exp2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_VECTOR));
  2212. add_options.push_back(AddOption("FaceForward", "Vector", "Functions", "VisualShaderNodeFaceForward", TTR("Returns the vector that points in the same direction as a reference vector. The function has three vector parameters : N, the vector to orient, I, the incident vector, and Nref, the reference vector. If the dot product of I and Nref is smaller than zero the return value is N. Otherwise -N is returned."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2213. add_options.push_back(AddOption("Floor", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer less than or equal to the parameter."), VisualShaderNodeVectorFunc::FUNC_FLOOR, VisualShaderNode::PORT_TYPE_VECTOR));
  2214. add_options.push_back(AddOption("Fract", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeVectorFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_VECTOR));
  2215. add_options.push_back(AddOption("InverseSqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse of the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_INVERSE_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  2216. add_options.push_back(AddOption("Length", "Vector", "Functions", "VisualShaderNodeVectorLen", TTR("Calculates the length of a vector."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2217. add_options.push_back(AddOption("Log", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Natural logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_VECTOR));
  2218. add_options.push_back(AddOption("Log2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_VECTOR));
  2219. add_options.push_back(AddOption("Max", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the greater of two values."), VisualShaderNodeVectorOp::OP_MAX, VisualShaderNode::PORT_TYPE_VECTOR));
  2220. add_options.push_back(AddOption("Min", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the lesser of two values."), VisualShaderNodeVectorOp::OP_MIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2221. add_options.push_back(AddOption("Mix", "Vector", "Functions", "VisualShaderNodeVectorInterp", TTR("Linear interpolation between two vectors."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2222. add_options.push_back(AddOption("MixS", "Vector", "Functions", "VisualShaderNodeVectorScalarMix", TTR("Linear interpolation between two vectors using scalar."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2223. add_options.push_back(AddOption("Negate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeVectorFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_VECTOR));
  2224. add_options.push_back(AddOption("Normalize", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Calculates the normalize product of vector."), VisualShaderNodeVectorFunc::FUNC_NORMALIZE, VisualShaderNode::PORT_TYPE_VECTOR));
  2225. add_options.push_back(AddOption("OneMinus", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 - vector"), VisualShaderNodeVectorFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_VECTOR));
  2226. add_options.push_back(AddOption("Pow", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the value of the first parameter raised to the power of the second."), VisualShaderNodeVectorOp::OP_POW, VisualShaderNode::PORT_TYPE_VECTOR));
  2227. add_options.push_back(AddOption("Radians", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeVectorFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_VECTOR));
  2228. add_options.push_back(AddOption("Reciprocal", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 / vector"), VisualShaderNodeVectorFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_VECTOR));
  2229. add_options.push_back(AddOption("Reflect", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the vector that points in the direction of reflection ( a : incident vector, b : normal vector )."), VisualShaderNodeVectorOp::OP_REFLECT, VisualShaderNode::PORT_TYPE_VECTOR));
  2230. add_options.push_back(AddOption("Refract", "Vector", "Functions", "VisualShaderNodeVectorRefract", TTR("Returns the vector that points in the direction of refraction."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2231. add_options.push_back(AddOption("Round", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_VECTOR));
  2232. add_options.push_back(AddOption("RoundEven", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest even integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUNDEVEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2233. add_options.push_back(AddOption("Saturate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Clamps the value between 0.0 and 1.0."), VisualShaderNodeVectorFunc::FUNC_SATURATE, VisualShaderNode::PORT_TYPE_VECTOR));
  2234. add_options.push_back(AddOption("Sign", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_VECTOR));
  2235. add_options.push_back(AddOption("Sin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2236. add_options.push_back(AddOption("SinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_VECTOR));
  2237. add_options.push_back(AddOption("Sqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  2238. add_options.push_back(AddOption("SmoothStep", "Vector", "Functions", "VisualShaderNodeVectorSmoothStep", TTR("SmoothStep function( vector(edge0), vector(edge1), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge0' and 1.0 if 'x' is larger than 'edge1'. Otherwise the return value is interpolated between 0.0 and 1.0 using Hermite polynomials."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2239. add_options.push_back(AddOption("SmoothStepS", "Vector", "Functions", "VisualShaderNodeVectorScalarSmoothStep", TTR("SmoothStep function( scalar(edge0), scalar(edge1), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge0' and 1.0 if 'x' is larger than 'edge1'. Otherwise the return value is interpolated between 0.0 and 1.0 using Hermite polynomials."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2240. add_options.push_back(AddOption("Step", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Step function( vector(edge), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeVectorOp::OP_STEP, VisualShaderNode::PORT_TYPE_VECTOR));
  2241. add_options.push_back(AddOption("StepS", "Vector", "Functions", "VisualShaderNodeVectorScalarStep", TTR("Step function( scalar(edge), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2242. add_options.push_back(AddOption("Tan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2243. add_options.push_back(AddOption("TanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_VECTOR));
  2244. add_options.push_back(AddOption("Trunc", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeVectorFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_VECTOR));
  2245. add_options.push_back(AddOption("Add", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Adds vector to vector."), VisualShaderNodeVectorOp::OP_ADD, VisualShaderNode::PORT_TYPE_VECTOR));
  2246. add_options.push_back(AddOption("Divide", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Divides vector by vector."), VisualShaderNodeVectorOp::OP_DIV, VisualShaderNode::PORT_TYPE_VECTOR));
  2247. add_options.push_back(AddOption("Multiply", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Multiplies vector by vector."), VisualShaderNodeVectorOp::OP_MUL, VisualShaderNode::PORT_TYPE_VECTOR));
  2248. add_options.push_back(AddOption("Remainder", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Returns the remainder of the two vectors."), VisualShaderNodeVectorOp::OP_MOD, VisualShaderNode::PORT_TYPE_VECTOR));
  2249. add_options.push_back(AddOption("Subtract", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Subtracts vector from vector."), VisualShaderNodeVectorOp::OP_SUB, VisualShaderNode::PORT_TYPE_VECTOR));
  2250. add_options.push_back(AddOption("VectorConstant", "Vector", "Variables", "VisualShaderNodeVec3Constant", TTR("Vector constant."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2251. add_options.push_back(AddOption("VectorUniform", "Vector", "Variables", "VisualShaderNodeVec3Uniform", TTR("Vector uniform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2252. // SPECIAL
  2253. add_options.push_back(AddOption("Expression", "Special", "", "VisualShaderNodeExpression", TTR("Custom Godot Shader Language expression, with custom amount of input and output ports. This is a direct injection of code into the vertex/fragment/light function, do not use it to write the function declarations inside.")));
  2254. add_options.push_back(AddOption("Fresnel", "Special", "", "VisualShaderNodeFresnel", TTR("Returns falloff based on the dot product of surface normal and view direction of camera (pass associated inputs to it)."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2255. add_options.push_back(AddOption("GlobalExpression", "Special", "", "VisualShaderNodeGlobalExpression", TTR("Custom Godot Shader Language expression, which is placed on top of the resulted shader. You can place various function definitions inside and call it later in the Expressions. You can also declare varyings, uniforms and constants.")));
  2256. add_options.push_back(AddOption("ScalarDerivativeFunc", "Special", "Common", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) Scalar derivative function."), -1, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2257. add_options.push_back(AddOption("VectorDerivativeFunc", "Special", "Common", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) Vector derivative function."), -1, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2258. add_options.push_back(AddOption("DdX", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Derivative in 'x' using local differencing."), VisualShaderNodeVectorDerivativeFunc::FUNC_X, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2259. add_options.push_back(AddOption("DdXS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Derivative in 'x' using local differencing."), VisualShaderNodeScalarDerivativeFunc::FUNC_X, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2260. add_options.push_back(AddOption("DdY", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Derivative in 'y' using local differencing."), VisualShaderNodeVectorDerivativeFunc::FUNC_Y, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2261. add_options.push_back(AddOption("DdYS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Derivative in 'y' using local differencing."), VisualShaderNodeScalarDerivativeFunc::FUNC_Y, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2262. add_options.push_back(AddOption("Sum", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeVectorDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2263. add_options.push_back(AddOption("SumS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeScalarDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2264. custom_node_option_idx = add_options.size();
  2265. /////////////////////////////////////////////////////////////////////
  2266. _update_options_menu();
  2267. error_panel = memnew(PanelContainer);
  2268. add_child(error_panel);
  2269. error_label = memnew(Label);
  2270. error_panel->add_child(error_label);
  2271. error_label->set_text("eh");
  2272. error_panel->hide();
  2273. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2274. Ref<VisualShaderNodePluginDefault> default_plugin;
  2275. default_plugin.instance();
  2276. add_plugin(default_plugin);
  2277. property_editor = memnew(CustomPropertyEditor);
  2278. add_child(property_editor);
  2279. property_editor->connect("variant_changed", this, "_port_edited");
  2280. }
  2281. void VisualShaderEditorPlugin::edit(Object *p_object) {
  2282. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  2283. }
  2284. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  2285. return p_object->is_class("VisualShader");
  2286. }
  2287. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  2288. if (p_visible) {
  2289. //editor->hide_animation_player_editors();
  2290. //editor->animation_panel_make_visible(true);
  2291. button->show();
  2292. editor->make_bottom_panel_item_visible(visual_shader_editor);
  2293. visual_shader_editor->update_custom_nodes();
  2294. visual_shader_editor->set_process_input(true);
  2295. //visual_shader_editor->set_process(true);
  2296. } else {
  2297. if (visual_shader_editor->is_visible_in_tree())
  2298. editor->hide_bottom_panel();
  2299. button->hide();
  2300. visual_shader_editor->set_process_input(false);
  2301. //visual_shader_editor->set_process(false);
  2302. }
  2303. }
  2304. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  2305. editor = p_node;
  2306. visual_shader_editor = memnew(VisualShaderEditor);
  2307. visual_shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  2308. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  2309. button->hide();
  2310. }
  2311. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  2312. }
  2313. ////////////////
  2314. class VisualShaderNodePluginInputEditor : public OptionButton {
  2315. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton);
  2316. Ref<VisualShaderNodeInput> input;
  2317. protected:
  2318. static void _bind_methods() {
  2319. ClassDB::bind_method("_item_selected", &VisualShaderNodePluginInputEditor::_item_selected);
  2320. }
  2321. public:
  2322. void _notification(int p_what) {
  2323. if (p_what == NOTIFICATION_READY) {
  2324. connect("item_selected", this, "_item_selected");
  2325. }
  2326. }
  2327. void _item_selected(int p_item) {
  2328. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  2329. }
  2330. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  2331. input = p_input;
  2332. Ref<Texture2D> type_icon[5] = {
  2333. EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"),
  2334. EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"),
  2335. EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"),
  2336. EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"),
  2337. EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"),
  2338. };
  2339. add_item("[None]");
  2340. int to_select = -1;
  2341. for (int i = 0; i < input->get_input_index_count(); i++) {
  2342. if (input->get_input_name() == input->get_input_index_name(i)) {
  2343. to_select = i + 1;
  2344. }
  2345. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  2346. }
  2347. if (to_select >= 0) {
  2348. select(to_select);
  2349. }
  2350. }
  2351. };
  2352. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  2353. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer);
  2354. Ref<Resource> parent_resource;
  2355. public:
  2356. void _property_changed(const String &prop, const Variant &p_value, const String &p_field, bool p_changing = false) {
  2357. if (p_changing)
  2358. return;
  2359. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2360. updating = true;
  2361. undo_redo->create_action(TTR("Edit Visual Property") + ": " + prop, UndoRedo::MERGE_ENDS);
  2362. undo_redo->add_do_property(node.ptr(), prop, p_value);
  2363. undo_redo->add_undo_property(node.ptr(), prop, node->get(prop));
  2364. if (p_value.get_type() == Variant::OBJECT) {
  2365. RES prev_res = node->get(prop);
  2366. RES curr_res = p_value;
  2367. if (curr_res.is_null()) {
  2368. undo_redo->add_do_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2369. } else {
  2370. undo_redo->add_do_method(this, "_open_inspector", (RES)curr_res.ptr());
  2371. }
  2372. if (!prev_res.is_null()) {
  2373. undo_redo->add_undo_method(this, "_open_inspector", (RES)prev_res.ptr());
  2374. } else {
  2375. undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2376. }
  2377. undo_redo->add_do_method(this, "_refresh_request");
  2378. undo_redo->add_undo_method(this, "_refresh_request");
  2379. }
  2380. undo_redo->commit_action();
  2381. updating = false;
  2382. }
  2383. void _node_changed() {
  2384. if (updating)
  2385. return;
  2386. for (int i = 0; i < properties.size(); i++) {
  2387. properties[i]->update_property();
  2388. }
  2389. }
  2390. void _refresh_request() {
  2391. VisualShaderEditor::get_singleton()->call_deferred("_update_graph");
  2392. }
  2393. void _resource_selected(const String &p_path, RES p_resource) {
  2394. _open_inspector(p_resource);
  2395. }
  2396. void _open_inspector(RES p_resource) {
  2397. EditorNode::get_singleton()->get_inspector()->edit(p_resource.ptr());
  2398. }
  2399. bool updating;
  2400. Ref<VisualShaderNode> node;
  2401. Vector<EditorProperty *> properties;
  2402. Vector<Label *> prop_names;
  2403. void _show_prop_names(bool p_show) {
  2404. for (int i = 0; i < prop_names.size(); i++) {
  2405. prop_names[i]->set_visible(p_show);
  2406. }
  2407. }
  2408. void setup(Ref<Resource> p_parent_resource, Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  2409. parent_resource = p_parent_resource;
  2410. updating = false;
  2411. node = p_node;
  2412. properties = p_properties;
  2413. for (int i = 0; i < p_properties.size(); i++) {
  2414. HBoxContainer *hbox = memnew(HBoxContainer);
  2415. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2416. add_child(hbox);
  2417. Label *prop_name = memnew(Label);
  2418. String prop_name_str = p_names[i];
  2419. prop_name_str = prop_name_str.capitalize() + ":";
  2420. prop_name->set_text(prop_name_str);
  2421. prop_name->set_visible(false);
  2422. hbox->add_child(prop_name);
  2423. prop_names.push_back(prop_name);
  2424. p_properties[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  2425. hbox->add_child(p_properties[i]);
  2426. bool res_prop = Object::cast_to<EditorPropertyResource>(p_properties[i]);
  2427. if (res_prop) {
  2428. p_properties[i]->connect("resource_selected", this, "_resource_selected");
  2429. }
  2430. properties[i]->connect("property_changed", this, "_property_changed");
  2431. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  2432. properties[i]->update_property();
  2433. properties[i]->set_name_split_ratio(0);
  2434. }
  2435. node->connect("changed", this, "_node_changed");
  2436. node->connect("editor_refresh_request", this, "_refresh_request", varray(), CONNECT_DEFERRED);
  2437. }
  2438. static void _bind_methods() {
  2439. ClassDB::bind_method("_property_changed", &VisualShaderNodePluginDefaultEditor::_property_changed, DEFVAL(String()), DEFVAL(false));
  2440. ClassDB::bind_method("_node_changed", &VisualShaderNodePluginDefaultEditor::_node_changed);
  2441. ClassDB::bind_method("_refresh_request", &VisualShaderNodePluginDefaultEditor::_refresh_request);
  2442. ClassDB::bind_method("_resource_selected", &VisualShaderNodePluginDefaultEditor::_resource_selected);
  2443. ClassDB::bind_method("_open_inspector", &VisualShaderNodePluginDefaultEditor::_open_inspector);
  2444. ClassDB::bind_method("_show_prop_names", &VisualShaderNodePluginDefaultEditor::_show_prop_names);
  2445. }
  2446. };
  2447. Control *VisualShaderNodePluginDefault::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  2448. if (p_node->is_class("VisualShaderNodeInput")) {
  2449. //create input
  2450. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  2451. input_editor->setup(p_node);
  2452. return input_editor;
  2453. }
  2454. Vector<StringName> properties = p_node->get_editable_properties();
  2455. if (properties.size() == 0) {
  2456. return NULL;
  2457. }
  2458. List<PropertyInfo> props;
  2459. p_node->get_property_list(&props);
  2460. Vector<PropertyInfo> pinfo;
  2461. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2462. for (int i = 0; i < properties.size(); i++) {
  2463. if (E->get().name == String(properties[i])) {
  2464. pinfo.push_back(E->get());
  2465. }
  2466. }
  2467. }
  2468. if (pinfo.size() == 0)
  2469. return NULL;
  2470. properties.clear();
  2471. Ref<VisualShaderNode> node = p_node;
  2472. Vector<EditorProperty *> editors;
  2473. for (int i = 0; i < pinfo.size(); i++) {
  2474. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  2475. if (!prop)
  2476. return NULL;
  2477. if (Object::cast_to<EditorPropertyResource>(prop)) {
  2478. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  2479. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2480. } else if (Object::cast_to<EditorPropertyTransform>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  2481. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  2482. } else if (Object::cast_to<EditorPropertyFloat>(prop)) {
  2483. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2484. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  2485. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2486. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  2487. }
  2488. editors.push_back(prop);
  2489. properties.push_back(pinfo[i].name);
  2490. }
  2491. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  2492. editor->setup(p_parent_resource, editors, properties, p_node);
  2493. return editor;
  2494. }
  2495. void EditorPropertyShaderMode::_option_selected(int p_which) {
  2496. //will not use this, instead will do all the logic setting manually
  2497. //emit_signal("property_changed", get_edited_property(), p_which);
  2498. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  2499. if (visual_shader->get_mode() == p_which)
  2500. return;
  2501. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2502. undo_redo->create_action(TTR("Visual Shader Mode Changed"));
  2503. //do is easy
  2504. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  2505. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  2506. //now undo is hell
  2507. //1. restore connections to output
  2508. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  2509. VisualShader::Type type = VisualShader::Type(i);
  2510. List<VisualShader::Connection> conns;
  2511. visual_shader->get_node_connections(type, &conns);
  2512. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2513. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  2514. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2515. }
  2516. }
  2517. }
  2518. //2. restore input indices
  2519. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  2520. VisualShader::Type type = VisualShader::Type(i);
  2521. Vector<int> nodes = visual_shader->get_node_list(type);
  2522. for (int j = 0; j < nodes.size(); j++) {
  2523. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[j]);
  2524. if (!input.is_valid()) {
  2525. continue;
  2526. }
  2527. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  2528. }
  2529. }
  2530. //3. restore enums and flags
  2531. List<PropertyInfo> props;
  2532. visual_shader->get_property_list(&props);
  2533. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2534. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  2535. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  2536. }
  2537. }
  2538. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  2539. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  2540. //update graph
  2541. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  2542. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  2543. undo_redo->commit_action();
  2544. }
  2545. void EditorPropertyShaderMode::update_property() {
  2546. int which = get_edited_object()->get(get_edited_property());
  2547. options->select(which);
  2548. }
  2549. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  2550. for (int i = 0; i < p_options.size(); i++) {
  2551. options->add_item(p_options[i], i);
  2552. }
  2553. }
  2554. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  2555. options->set_clip_text(p_enable);
  2556. }
  2557. void EditorPropertyShaderMode::_bind_methods() {
  2558. ClassDB::bind_method(D_METHOD("_option_selected"), &EditorPropertyShaderMode::_option_selected);
  2559. }
  2560. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  2561. options = memnew(OptionButton);
  2562. options->set_clip_text(true);
  2563. add_child(options);
  2564. add_focusable(options);
  2565. options->connect("item_selected", this, "_option_selected");
  2566. }
  2567. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  2568. return true; //can handle everything
  2569. }
  2570. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  2571. //do none
  2572. }
  2573. bool EditorInspectorShaderModePlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
  2574. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  2575. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  2576. Vector<String> options = p_hint_text.split(",");
  2577. editor->setup(options);
  2578. add_property_editor(p_path, editor);
  2579. return true;
  2580. }
  2581. return false; //can be overridden, although it will most likely be last anyway
  2582. }
  2583. void EditorInspectorShaderModePlugin::parse_end() {
  2584. //do none
  2585. }
  2586. //////////////////////////////////
  2587. void VisualShaderNodePortPreview::_shader_changed() {
  2588. if (shader.is_null()) {
  2589. return;
  2590. }
  2591. Vector<VisualShader::DefaultTextureParam> default_textures;
  2592. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  2593. Ref<Shader> preview_shader;
  2594. preview_shader.instance();
  2595. preview_shader->set_code(shader_code);
  2596. for (int i = 0; i < default_textures.size(); i++) {
  2597. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  2598. }
  2599. Ref<ShaderMaterial> material;
  2600. material.instance();
  2601. material->set_shader(preview_shader);
  2602. //find if a material is also being edited and copy parameters to this one
  2603. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  2604. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  2605. if (!object)
  2606. continue;
  2607. ShaderMaterial *src_mat = Object::cast_to<ShaderMaterial>(object);
  2608. if (src_mat && src_mat->get_shader().is_valid()) {
  2609. List<PropertyInfo> params;
  2610. src_mat->get_shader()->get_param_list(&params);
  2611. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  2612. material->set(E->get().name, src_mat->get(E->get().name));
  2613. }
  2614. }
  2615. }
  2616. set_material(material);
  2617. }
  2618. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  2619. shader = p_shader;
  2620. shader->connect("changed", this, "_shader_changed");
  2621. type = p_type;
  2622. port = p_port;
  2623. node = p_node;
  2624. update();
  2625. _shader_changed();
  2626. }
  2627. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  2628. return Size2(100, 100) * EDSCALE;
  2629. }
  2630. void VisualShaderNodePortPreview::_notification(int p_what) {
  2631. if (p_what == NOTIFICATION_DRAW) {
  2632. Vector<Vector2> points;
  2633. Vector<Vector2> uvs;
  2634. Vector<Color> colors;
  2635. points.push_back(Vector2());
  2636. uvs.push_back(Vector2(0, 0));
  2637. colors.push_back(Color(1, 1, 1, 1));
  2638. points.push_back(Vector2(get_size().width, 0));
  2639. uvs.push_back(Vector2(1, 0));
  2640. colors.push_back(Color(1, 1, 1, 1));
  2641. points.push_back(get_size());
  2642. uvs.push_back(Vector2(1, 1));
  2643. colors.push_back(Color(1, 1, 1, 1));
  2644. points.push_back(Vector2(0, get_size().height));
  2645. uvs.push_back(Vector2(0, 1));
  2646. colors.push_back(Color(1, 1, 1, 1));
  2647. draw_primitive(points, colors, uvs);
  2648. }
  2649. }
  2650. void VisualShaderNodePortPreview::_bind_methods() {
  2651. ClassDB::bind_method("_shader_changed", &VisualShaderNodePortPreview::_shader_changed);
  2652. }
  2653. VisualShaderNodePortPreview::VisualShaderNodePortPreview() {
  2654. }
  2655. //////////////////////////////////
  2656. String VisualShaderConversionPlugin::converts_to() const {
  2657. return "Shader";
  2658. }
  2659. bool VisualShaderConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  2660. Ref<VisualShader> vshader = p_resource;
  2661. return vshader.is_valid();
  2662. }
  2663. Ref<Resource> VisualShaderConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  2664. Ref<VisualShader> vshader = p_resource;
  2665. ERR_FAIL_COND_V(!vshader.is_valid(), Ref<Resource>());
  2666. Ref<Shader> shader;
  2667. shader.instance();
  2668. String code = vshader->get_code();
  2669. shader->set_code(code);
  2670. return shader;
  2671. }