visual_shader_editor_plugin.cpp 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924
  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/input/input.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/math/math_defs.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/project_settings.h"
  36. #include "core/version.h"
  37. #include "editor/editor_log.h"
  38. #include "editor/editor_properties.h"
  39. #include "editor/editor_scale.h"
  40. #include "scene/animation/animation_player.h"
  41. #include "scene/gui/menu_button.h"
  42. #include "scene/gui/panel.h"
  43. #include "scene/main/window.h"
  44. #include "scene/resources/visual_shader_nodes.h"
  45. #include "servers/display_server.h"
  46. #include "servers/rendering/shader_types.h"
  47. Control *VisualShaderNodePlugin::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  48. if (get_script_instance()) {
  49. return get_script_instance()->call("create_editor", p_parent_resource, p_node);
  50. }
  51. return nullptr;
  52. }
  53. void VisualShaderNodePlugin::_bind_methods() {
  54. 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")));
  55. }
  56. ///////////////////
  57. 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) {
  58. Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));
  59. style->set_default_margin(MARGIN_LEFT, p_margin_left * EDSCALE);
  60. style->set_default_margin(MARGIN_RIGHT, p_margin_right * EDSCALE);
  61. style->set_default_margin(MARGIN_BOTTOM, p_margin_bottom * EDSCALE);
  62. style->set_default_margin(MARGIN_TOP, p_margin_top * EDSCALE);
  63. return style;
  64. }
  65. ///////////////////
  66. VisualShaderGraphPlugin::VisualShaderGraphPlugin() {
  67. }
  68. void VisualShaderGraphPlugin::_bind_methods() {
  69. ClassDB::bind_method("add_node", &VisualShaderGraphPlugin::add_node);
  70. ClassDB::bind_method("remove_node", &VisualShaderGraphPlugin::remove_node);
  71. ClassDB::bind_method("connect_nodes", &VisualShaderGraphPlugin::connect_nodes);
  72. ClassDB::bind_method("disconnect_nodes", &VisualShaderGraphPlugin::disconnect_nodes);
  73. ClassDB::bind_method("set_node_position", &VisualShaderGraphPlugin::set_node_position);
  74. ClassDB::bind_method("set_node_size", &VisualShaderGraphPlugin::set_node_size);
  75. ClassDB::bind_method("show_port_preview", &VisualShaderGraphPlugin::show_port_preview);
  76. ClassDB::bind_method("update_node", &VisualShaderGraphPlugin::update_node);
  77. ClassDB::bind_method("update_node_deferred", &VisualShaderGraphPlugin::update_node_deferred);
  78. ClassDB::bind_method("set_input_port_default_value", &VisualShaderGraphPlugin::set_input_port_default_value);
  79. ClassDB::bind_method("set_uniform_name", &VisualShaderGraphPlugin::set_uniform_name);
  80. }
  81. void VisualShaderGraphPlugin::register_shader(VisualShader *p_shader) {
  82. visual_shader = Ref<VisualShader>(p_shader);
  83. }
  84. void VisualShaderGraphPlugin::set_connections(List<VisualShader::Connection> &p_connections) {
  85. connections = p_connections;
  86. }
  87. void VisualShaderGraphPlugin::show_port_preview(VisualShader::Type p_type, int p_node_id, int p_port_id) {
  88. if (visual_shader->get_shader_type() == p_type && links.has(p_node_id)) {
  89. for (Map<int, Port>::Element *E = links[p_node_id].output_ports.front(); E; E = E->next()) {
  90. E->value().preview_button->set_pressed(false);
  91. }
  92. if (links[p_node_id].preview_visible && !is_dirty() && links[p_node_id].preview_box != nullptr) {
  93. links[p_node_id].graph_node->remove_child(links[p_node_id].preview_box);
  94. memdelete(links[p_node_id].preview_box);
  95. links[p_node_id].graph_node->set_size(Vector2(-1, -1));
  96. links[p_node_id].preview_visible = false;
  97. }
  98. if (p_port_id != -1) {
  99. if (is_dirty()) {
  100. links[p_node_id].preview_pos = links[p_node_id].graph_node->get_child_count();
  101. }
  102. VBoxContainer *vbox = memnew(VBoxContainer);
  103. links[p_node_id].graph_node->add_child(vbox);
  104. if (links[p_node_id].preview_pos != -1) {
  105. links[p_node_id].graph_node->move_child(vbox, links[p_node_id].preview_pos);
  106. }
  107. Control *offset = memnew(Control);
  108. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  109. vbox->add_child(offset);
  110. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  111. port_preview->setup(visual_shader, visual_shader->get_shader_type(), p_node_id, p_port_id);
  112. port_preview->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  113. vbox->add_child(port_preview);
  114. links[p_node_id].preview_visible = true;
  115. links[p_node_id].preview_box = vbox;
  116. links[p_node_id].output_ports[p_port_id].preview_button->set_pressed(true);
  117. }
  118. }
  119. }
  120. void VisualShaderGraphPlugin::update_node_deferred(VisualShader::Type p_type, int p_node_id) {
  121. call_deferred("update_node", p_type, p_node_id);
  122. }
  123. void VisualShaderGraphPlugin::update_node(VisualShader::Type p_type, int p_node_id) {
  124. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) {
  125. return;
  126. }
  127. remove_node(p_type, p_node_id);
  128. add_node(p_type, p_node_id);
  129. }
  130. void VisualShaderGraphPlugin::set_input_port_default_value(VisualShader::Type p_type, int p_node_id, int p_port_id, Variant p_value) {
  131. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) {
  132. return;
  133. }
  134. Button *button = links[p_node_id].input_ports[p_port_id].default_input_button;
  135. switch (p_value.get_type()) {
  136. case Variant::COLOR: {
  137. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  138. if (!button->is_connected("draw", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_draw_color_over_button))) {
  139. button->connect("draw", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_draw_color_over_button), varray(button, p_value));
  140. }
  141. } break;
  142. case Variant::BOOL: {
  143. button->set_text(((bool)p_value) ? "true" : "false");
  144. } break;
  145. case Variant::INT:
  146. case Variant::FLOAT: {
  147. button->set_text(String::num(p_value, 4));
  148. } break;
  149. case Variant::VECTOR3: {
  150. Vector3 v = p_value;
  151. button->set_text(String::num(v.x, 3) + "," + String::num(v.y, 3) + "," + String::num(v.z, 3));
  152. } break;
  153. default: {
  154. }
  155. }
  156. }
  157. void VisualShaderGraphPlugin::set_uniform_name(VisualShader::Type p_type, int p_node_id, const String &p_name) {
  158. if (visual_shader->get_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].uniform_name != nullptr) {
  159. links[p_node_id].uniform_name->set_text(p_name);
  160. }
  161. }
  162. void VisualShaderGraphPlugin::register_default_input_button(int p_node_id, int p_port_id, Button *p_button) {
  163. links[p_node_id].input_ports.insert(p_port_id, { p_button });
  164. }
  165. void VisualShaderGraphPlugin::update_uniform_refs() {
  166. for (Map<int, Link>::Element *E = links.front(); E; E = E->next()) {
  167. VisualShaderNodeUniformRef *ref = Object::cast_to<VisualShaderNodeUniformRef>(E->get().visual_node);
  168. if (ref) {
  169. remove_node(E->get().type, E->key());
  170. add_node(E->get().type, E->key());
  171. }
  172. }
  173. }
  174. VisualShader::Type VisualShaderGraphPlugin::get_shader_type() const {
  175. return visual_shader->get_shader_type();
  176. }
  177. void VisualShaderGraphPlugin::set_node_position(VisualShader::Type p_type, int p_id, const Vector2 &p_position) {
  178. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  179. links[p_id].graph_node->set_offset(p_position);
  180. }
  181. }
  182. void VisualShaderGraphPlugin::set_node_size(VisualShader::Type p_type, int p_id, const Vector2 &p_size) {
  183. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  184. links[p_id].graph_node->set_size(p_size);
  185. }
  186. }
  187. bool VisualShaderGraphPlugin::is_preview_visible(int p_id) const {
  188. return links[p_id].preview_visible;
  189. }
  190. void VisualShaderGraphPlugin::clear_links() {
  191. links.clear();
  192. }
  193. bool VisualShaderGraphPlugin::is_dirty() const {
  194. return dirty;
  195. }
  196. void VisualShaderGraphPlugin::make_dirty(bool p_enabled) {
  197. dirty = p_enabled;
  198. }
  199. void VisualShaderGraphPlugin::register_link(VisualShader::Type p_type, int p_id, VisualShaderNode *p_visual_node, GraphNode *p_graph_node) {
  200. links.insert(p_id, { p_type, p_visual_node, p_graph_node, p_visual_node->get_output_port_for_preview() != -1, -1, Map<int, InputPort>(), Map<int, Port>(), nullptr, nullptr });
  201. }
  202. void VisualShaderGraphPlugin::register_output_port(int p_node_id, int p_port, TextureButton *p_button) {
  203. links[p_node_id].output_ports.insert(p_port, { p_button });
  204. }
  205. void VisualShaderGraphPlugin::register_uniform_name(int p_node_id, LineEdit *p_uniform_name) {
  206. links[p_node_id].uniform_name = p_uniform_name;
  207. }
  208. void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id) {
  209. if (p_type != visual_shader->get_shader_type()) {
  210. return;
  211. }
  212. Control *offset;
  213. static Ref<StyleBoxEmpty> label_style = make_empty_stylebox(2, 1, 2, 1);
  214. static const Color type_color[6] = {
  215. Color(0.38, 0.85, 0.96), // scalar (float)
  216. Color(0.49, 0.78, 0.94), // scalar (int)
  217. Color(0.84, 0.49, 0.93), // vector
  218. Color(0.55, 0.65, 0.94), // boolean
  219. Color(0.96, 0.66, 0.43), // transform
  220. Color(1.0, 1.0, 0.0), // sampler
  221. };
  222. Ref<VisualShaderNode> vsnode = visual_shader->get_node(p_type, p_id);
  223. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(vsnode.ptr());
  224. bool is_group = !group_node.is_null();
  225. Size2 size = Size2(0, 0);
  226. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(group_node.ptr());
  227. bool is_expression = !expression_node.is_null();
  228. String expression = "";
  229. GraphNode *node = memnew(GraphNode);
  230. register_link(p_type, p_id, vsnode.ptr(), node);
  231. if (is_group) {
  232. size = group_node->get_size();
  233. node->set_resizable(true);
  234. node->connect("resize_request", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_node_resized), varray((int)p_type, p_id));
  235. }
  236. if (is_expression) {
  237. expression = expression_node->get_expression();
  238. }
  239. node->set_offset(visual_shader->get_node_position(p_type, p_id));
  240. node->set_title(vsnode->get_caption());
  241. node->set_name(itos(p_id));
  242. if (p_id >= 2) {
  243. node->set_show_close_button(true);
  244. node->connect("close_request", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_delete_request), varray(p_id), CONNECT_DEFERRED);
  245. }
  246. node->connect("dragged", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_node_dragged), varray(p_id));
  247. Control *custom_editor = nullptr;
  248. int port_offset = 0;
  249. if (is_group) {
  250. port_offset += 2;
  251. }
  252. Ref<VisualShaderNodeUniform> uniform = vsnode;
  253. if (uniform.is_valid()) {
  254. VisualShaderEditor::get_singleton()->graph->add_child(node);
  255. VisualShaderEditor::get_singleton()->_update_created_node(node);
  256. LineEdit *uniform_name = memnew(LineEdit);
  257. register_uniform_name(p_id, uniform_name);
  258. uniform_name->set_text(uniform->get_uniform_name());
  259. node->add_child(uniform_name);
  260. uniform_name->connect("text_entered", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_uniform_line_edit_changed), varray(p_id));
  261. uniform_name->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_uniform_line_edit_focus_out), varray(uniform_name, p_id));
  262. if (vsnode->get_input_port_count() == 0 && vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
  263. //shortcut
  264. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  265. node->set_slot(0, false, VisualShaderNode::PORT_TYPE_SCALAR, Color(), true, port_right, type_color[port_right]);
  266. if (!vsnode->is_use_prop_slots()) {
  267. return;
  268. }
  269. }
  270. port_offset++;
  271. }
  272. for (int i = 0; i < VisualShaderEditor::get_singleton()->plugins.size(); i++) {
  273. vsnode->set_meta("id", p_id);
  274. vsnode->set_meta("shader_type", (int)p_type);
  275. custom_editor = VisualShaderEditor::get_singleton()->plugins.write[i]->create_editor(visual_shader, vsnode);
  276. vsnode->remove_meta("id");
  277. vsnode->remove_meta("shader_type");
  278. if (custom_editor) {
  279. if (vsnode->is_show_prop_names()) {
  280. custom_editor->call_deferred("_show_prop_names", true);
  281. }
  282. break;
  283. }
  284. }
  285. if (custom_editor && !vsnode->is_use_prop_slots() && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) {
  286. //will be embedded in first port
  287. } else if (custom_editor) {
  288. port_offset++;
  289. node->add_child(custom_editor);
  290. if (vsnode->is_use_prop_slots()) {
  291. return;
  292. }
  293. custom_editor = nullptr;
  294. }
  295. if (is_group) {
  296. offset = memnew(Control);
  297. offset->set_custom_minimum_size(Size2(0, 6 * EDSCALE));
  298. node->add_child(offset);
  299. if (group_node->is_editable()) {
  300. HBoxContainer *hb2 = memnew(HBoxContainer);
  301. Button *add_input_btn = memnew(Button);
  302. add_input_btn->set_text(TTR("Add Input"));
  303. add_input_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_add_input_port), varray(p_id, group_node->get_free_input_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "input" + itos(group_node->get_free_input_port_id())), CONNECT_DEFERRED);
  304. hb2->add_child(add_input_btn);
  305. hb2->add_spacer();
  306. Button *add_output_btn = memnew(Button);
  307. add_output_btn->set_text(TTR("Add Output"));
  308. add_output_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_add_output_port), varray(p_id, group_node->get_free_output_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "output" + itos(group_node->get_free_output_port_id())), CONNECT_DEFERRED);
  309. hb2->add_child(add_output_btn);
  310. node->add_child(hb2);
  311. }
  312. }
  313. for (int i = 0; i < MAX(vsnode->get_input_port_count(), vsnode->get_output_port_count()); i++) {
  314. if (vsnode->is_port_separator(i)) {
  315. node->add_child(memnew(HSeparator));
  316. port_offset++;
  317. }
  318. bool valid_left = i < vsnode->get_input_port_count();
  319. VisualShaderNode::PortType port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  320. bool port_left_used = false;
  321. String name_left;
  322. if (valid_left) {
  323. name_left = vsnode->get_input_port_name(i);
  324. port_left = vsnode->get_input_port_type(i);
  325. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  326. if (E->get().to_node == p_id && E->get().to_port == i) {
  327. port_left_used = true;
  328. }
  329. }
  330. }
  331. bool valid_right = i < vsnode->get_output_port_count();
  332. VisualShaderNode::PortType port_right = VisualShaderNode::PORT_TYPE_SCALAR;
  333. String name_right;
  334. if (valid_right) {
  335. name_right = vsnode->get_output_port_name(i);
  336. port_right = vsnode->get_output_port_type(i);
  337. }
  338. HBoxContainer *hb = memnew(HBoxContainer);
  339. hb->add_theme_constant_override("separation", 7 * EDSCALE);
  340. Variant default_value;
  341. if (valid_left && !port_left_used) {
  342. default_value = vsnode->get_input_port_default_value(i);
  343. }
  344. Button *button = memnew(Button);
  345. hb->add_child(button);
  346. register_default_input_button(p_id, i, button);
  347. button->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_edit_port_default_input), varray(button, p_id, i));
  348. if (default_value.get_type() != Variant::NIL) { // only a label
  349. set_input_port_default_value(p_type, p_id, i, default_value);
  350. } else {
  351. button->hide();
  352. }
  353. if (i == 0 && custom_editor) {
  354. hb->add_child(custom_editor);
  355. custom_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  356. } else {
  357. if (valid_left) {
  358. if (is_group) {
  359. OptionButton *type_box = memnew(OptionButton);
  360. hb->add_child(type_box);
  361. type_box->add_item(TTR("Float"));
  362. type_box->add_item(TTR("Int"));
  363. type_box->add_item(TTR("Vector"));
  364. type_box->add_item(TTR("Boolean"));
  365. type_box->add_item(TTR("Transform"));
  366. type_box->add_item(TTR("Sampler"));
  367. type_box->select(group_node->get_input_port_type(i));
  368. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  369. type_box->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_input_port_type), varray(p_id, i), CONNECT_DEFERRED);
  370. LineEdit *name_box = memnew(LineEdit);
  371. hb->add_child(name_box);
  372. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  373. name_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  374. name_box->set_text(name_left);
  375. name_box->connect("text_entered", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_input_port_name), varray(name_box, p_id, i));
  376. name_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_port_name_focus_out), varray(name_box, p_id, i, false));
  377. Button *remove_btn = memnew(Button);
  378. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  379. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  380. remove_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_remove_input_port), varray(p_id, i), CONNECT_DEFERRED);
  381. hb->add_child(remove_btn);
  382. } else {
  383. Label *label = memnew(Label);
  384. label->set_text(name_left);
  385. label->add_theme_style_override("normal", label_style); //more compact
  386. hb->add_child(label);
  387. if (vsnode->get_input_port_default_hint(i) != "" && !port_left_used) {
  388. Label *hint_label = memnew(Label);
  389. hint_label->set_text("[" + vsnode->get_input_port_default_hint(i) + "]");
  390. hint_label->add_theme_color_override("font_color", VisualShaderEditor::get_singleton()->get_theme_color("font_color_readonly", "TextEdit"));
  391. hint_label->add_theme_style_override("normal", label_style);
  392. hb->add_child(hint_label);
  393. }
  394. }
  395. }
  396. if (!is_group) {
  397. hb->add_spacer();
  398. }
  399. if (valid_right) {
  400. if (is_group) {
  401. Button *remove_btn = memnew(Button);
  402. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  403. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  404. remove_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_remove_output_port), varray(p_id, i), CONNECT_DEFERRED);
  405. hb->add_child(remove_btn);
  406. LineEdit *name_box = memnew(LineEdit);
  407. hb->add_child(name_box);
  408. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  409. name_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  410. name_box->set_text(name_right);
  411. name_box->connect("text_entered", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_output_port_name), varray(name_box, p_id, i));
  412. name_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_port_name_focus_out), varray(name_box, p_id, i, true));
  413. OptionButton *type_box = memnew(OptionButton);
  414. hb->add_child(type_box);
  415. type_box->add_item(TTR("Float"));
  416. type_box->add_item(TTR("Int"));
  417. type_box->add_item(TTR("Vector"));
  418. type_box->add_item(TTR("Boolean"));
  419. type_box->add_item(TTR("Transform"));
  420. type_box->select(group_node->get_output_port_type(i));
  421. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  422. type_box->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_output_port_type), varray(p_id, i), CONNECT_DEFERRED);
  423. } else {
  424. Label *label = memnew(Label);
  425. label->set_text(name_right);
  426. label->add_theme_style_override("normal", label_style); //more compact
  427. hb->add_child(label);
  428. }
  429. }
  430. }
  431. if (valid_right && visual_shader->get_shader_type() == VisualShader::TYPE_FRAGMENT && port_right != VisualShaderNode::PORT_TYPE_TRANSFORM && port_right != VisualShaderNode::PORT_TYPE_SAMPLER) {
  432. TextureButton *preview = memnew(TextureButton);
  433. preview->set_toggle_mode(true);
  434. preview->set_normal_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityHidden", "EditorIcons"));
  435. preview->set_pressed_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityVisible", "EditorIcons"));
  436. preview->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  437. register_output_port(p_id, i, preview);
  438. preview->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_preview_select_port), varray(p_id, i), CONNECT_DEFERRED);
  439. hb->add_child(preview);
  440. }
  441. if (is_group) {
  442. offset = memnew(Control);
  443. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  444. node->add_child(offset);
  445. port_offset++;
  446. }
  447. node->add_child(hb);
  448. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
  449. }
  450. if (vsnode->get_output_port_for_preview() >= 0) {
  451. show_port_preview(p_type, p_id, vsnode->get_output_port_for_preview());
  452. }
  453. offset = memnew(Control);
  454. offset->set_custom_minimum_size(Size2(0, 4 * EDSCALE));
  455. node->add_child(offset);
  456. String error = vsnode->get_warning(visual_shader->get_mode(), p_type);
  457. if (error != String()) {
  458. Label *error_label = memnew(Label);
  459. error_label->add_theme_color_override("font_color", VisualShaderEditor::get_singleton()->get_theme_color("error_color", "Editor"));
  460. error_label->set_text(error);
  461. node->add_child(error_label);
  462. }
  463. if (is_expression) {
  464. CodeEdit *expression_box = memnew(CodeEdit);
  465. Ref<CodeHighlighter> expression_syntax_highlighter;
  466. expression_syntax_highlighter.instance();
  467. expression_node->set_control(expression_box, 0);
  468. node->add_child(expression_box);
  469. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  470. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  471. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  472. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  473. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  474. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  475. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  476. Color members_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  477. expression_box->set_syntax_highlighter(expression_syntax_highlighter);
  478. expression_box->add_theme_color_override("background_color", background_color);
  479. for (List<String>::Element *E = VisualShaderEditor::get_singleton()->keyword_list.front(); E; E = E->next()) {
  480. expression_syntax_highlighter->add_keyword_color(E->get(), keyword_color);
  481. }
  482. expression_box->add_theme_font_override("font", VisualShaderEditor::get_singleton()->get_theme_font("expression", "EditorFonts"));
  483. expression_box->add_theme_color_override("font_color", text_color);
  484. expression_syntax_highlighter->set_number_color(number_color);
  485. expression_syntax_highlighter->set_symbol_color(symbol_color);
  486. expression_syntax_highlighter->set_function_color(function_color);
  487. expression_syntax_highlighter->set_member_variable_color(members_color);
  488. expression_syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
  489. expression_syntax_highlighter->add_color_region("//", "", comment_color, true);
  490. expression_box->set_text(expression);
  491. expression_box->set_context_menu_enabled(false);
  492. expression_box->set_draw_line_numbers(true);
  493. expression_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_expression_focus_out), varray(expression_box, p_id));
  494. }
  495. if (!uniform.is_valid()) {
  496. VisualShaderEditor::get_singleton()->graph->add_child(node);
  497. VisualShaderEditor::get_singleton()->_update_created_node(node);
  498. if (is_group) {
  499. VisualShaderEditor::get_singleton()->call_deferred("_set_node_size", (int)p_type, p_id, size);
  500. }
  501. }
  502. }
  503. void VisualShaderGraphPlugin::remove_node(VisualShader::Type p_type, int p_id) {
  504. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  505. links[p_id].graph_node->get_parent()->remove_child(links[p_id].graph_node);
  506. memdelete(links[p_id].graph_node);
  507. links.erase(p_id);
  508. }
  509. }
  510. void VisualShaderGraphPlugin::connect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  511. if (visual_shader->get_shader_type() == p_type) {
  512. VisualShaderEditor::get_singleton()->graph->connect_node(itos(p_from_node), p_from_port, itos(p_to_node), p_to_port);
  513. if (links[p_to_node].input_ports.has(p_to_port) && links[p_to_node].input_ports[p_to_port].default_input_button != nullptr) {
  514. links[p_to_node].input_ports[p_to_port].default_input_button->hide();
  515. }
  516. }
  517. }
  518. void VisualShaderGraphPlugin::disconnect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  519. if (visual_shader->get_shader_type() == p_type) {
  520. VisualShaderEditor::get_singleton()->graph->disconnect_node(itos(p_from_node), p_from_port, itos(p_to_node), p_to_port);
  521. if (links[p_to_node].input_ports.has(p_to_port) && links[p_to_node].input_ports[p_to_port].default_input_button != nullptr && links[p_to_node].visual_node->get_input_port_default_value(p_to_port).get_type() != Variant::NIL) {
  522. links[p_to_node].input_ports[p_to_port].default_input_button->show();
  523. set_input_port_default_value(p_type, p_to_node, p_to_port, links[p_to_node].visual_node->get_input_port_default_value(p_to_port));
  524. }
  525. }
  526. }
  527. VisualShaderGraphPlugin::~VisualShaderGraphPlugin() {
  528. }
  529. /////////////////
  530. void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
  531. bool changed = false;
  532. if (p_visual_shader) {
  533. if (visual_shader.is_null()) {
  534. changed = true;
  535. } else {
  536. if (visual_shader.ptr() != p_visual_shader) {
  537. changed = true;
  538. }
  539. }
  540. visual_shader = Ref<VisualShader>(p_visual_shader);
  541. graph_plugin->register_shader(visual_shader.ptr());
  542. if (!visual_shader->is_connected("changed", callable_mp(this, &VisualShaderEditor::_update_preview))) {
  543. visual_shader->connect("changed", callable_mp(this, &VisualShaderEditor::_update_preview));
  544. }
  545. #ifndef DISABLE_DEPRECATED
  546. String version = VERSION_BRANCH;
  547. if (visual_shader->get_version() != version) {
  548. visual_shader->update_version(version);
  549. }
  550. #endif
  551. visual_shader->set_graph_offset(graph->get_scroll_ofs() / EDSCALE);
  552. _set_mode(visual_shader->get_mode());
  553. } else {
  554. if (visual_shader.is_valid()) {
  555. if (visual_shader->is_connected("changed", callable_mp(this, &VisualShaderEditor::_update_preview))) {
  556. visual_shader->disconnect("changed", callable_mp(this, &VisualShaderEditor::_update_preview));
  557. }
  558. }
  559. visual_shader.unref();
  560. }
  561. if (visual_shader.is_null()) {
  562. hide();
  563. } else {
  564. if (changed) { // to avoid tree collapse
  565. _clear_buffer();
  566. _update_options_menu();
  567. _update_preview();
  568. _update_graph();
  569. }
  570. }
  571. }
  572. void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  573. if (plugins.find(p_plugin) != -1) {
  574. return;
  575. }
  576. plugins.push_back(p_plugin);
  577. }
  578. void VisualShaderEditor::remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  579. plugins.erase(p_plugin);
  580. }
  581. void VisualShaderEditor::clear_custom_types() {
  582. for (int i = 0; i < add_options.size(); i++) {
  583. if (add_options[i].is_custom) {
  584. add_options.remove(i);
  585. i--;
  586. }
  587. }
  588. }
  589. 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, bool p_highend) {
  590. ERR_FAIL_COND(!p_name.is_valid_identifier());
  591. ERR_FAIL_COND(!p_script.is_valid());
  592. for (int i = 0; i < add_options.size(); i++) {
  593. if (add_options[i].is_custom) {
  594. if (add_options[i].script == p_script) {
  595. return;
  596. }
  597. }
  598. }
  599. AddOption ao;
  600. ao.name = p_name;
  601. ao.script = p_script;
  602. ao.return_type = p_return_icon_type;
  603. ao.description = p_description;
  604. ao.category = p_category;
  605. ao.highend = p_highend;
  606. ao.is_custom = true;
  607. bool begin = false;
  608. String root = p_category.split("/")[0];
  609. for (int i = 0; i < add_options.size(); i++) {
  610. if (add_options[i].is_custom) {
  611. if (add_options[i].category == root) {
  612. if (!begin) {
  613. begin = true;
  614. }
  615. } else {
  616. if (begin) {
  617. add_options.insert(i, ao);
  618. return;
  619. }
  620. }
  621. }
  622. }
  623. add_options.push_back(ao);
  624. }
  625. bool VisualShaderEditor::_is_available(int p_mode) {
  626. int current_mode = edit_type->get_selected();
  627. if (p_mode != -1) {
  628. switch (current_mode) {
  629. case 0: // Vertex or Emit
  630. current_mode = 1;
  631. break;
  632. case 1: // Fragment or Process
  633. current_mode = 2;
  634. break;
  635. case 2: // Light or End
  636. current_mode = 4;
  637. break;
  638. default:
  639. break;
  640. }
  641. }
  642. return (p_mode == -1 || (p_mode & current_mode) != 0);
  643. }
  644. void VisualShaderEditor::update_custom_nodes() {
  645. if (members_dialog->is_visible()) {
  646. return;
  647. }
  648. clear_custom_types();
  649. List<StringName> class_list;
  650. ScriptServer::get_global_class_list(&class_list);
  651. Dictionary added;
  652. for (int i = 0; i < class_list.size(); i++) {
  653. if (ScriptServer::get_global_class_native_base(class_list[i]) == "VisualShaderNodeCustom") {
  654. String script_path = ScriptServer::get_global_class_path(class_list[i]);
  655. Ref<Resource> res = ResourceLoader::load(script_path);
  656. ERR_FAIL_COND(res.is_null());
  657. ERR_FAIL_COND(!res->is_class("Script"));
  658. Ref<Script> script = Ref<Script>(res);
  659. Ref<VisualShaderNodeCustom> ref;
  660. ref.instance();
  661. ref->set_script(script);
  662. String name;
  663. if (ref->has_method("_get_name")) {
  664. name = (String)ref->call("_get_name");
  665. } else {
  666. name = "Unnamed";
  667. }
  668. String description = "";
  669. if (ref->has_method("_get_description")) {
  670. description = (String)ref->call("_get_description");
  671. }
  672. int return_icon_type = -1;
  673. if (ref->has_method("_get_return_icon_type")) {
  674. return_icon_type = (int)ref->call("_get_return_icon_type");
  675. }
  676. String category = "";
  677. if (ref->has_method("_get_category")) {
  678. category = (String)ref->call("_get_category");
  679. }
  680. String subcategory = "";
  681. if (ref->has_method("_get_subcategory")) {
  682. subcategory = (String)ref->call("_get_subcategory");
  683. }
  684. bool highend = false;
  685. if (ref->has_method("_is_highend")) {
  686. highend = (bool)ref->call("_is_highend");
  687. }
  688. Dictionary dict;
  689. dict["name"] = name;
  690. dict["script"] = script;
  691. dict["description"] = description;
  692. dict["return_icon_type"] = return_icon_type;
  693. category = category.rstrip("/");
  694. category = category.lstrip("/");
  695. category = "Addons/" + category;
  696. if (subcategory != "") {
  697. category += "/" + subcategory;
  698. }
  699. dict["category"] = category;
  700. dict["highend"] = highend;
  701. String key;
  702. key = category + "/" + name;
  703. added[key] = dict;
  704. }
  705. }
  706. Array keys = added.keys();
  707. keys.sort();
  708. for (int i = 0; i < keys.size(); i++) {
  709. const Variant &key = keys.get(i);
  710. const Dictionary &value = (Dictionary)added[key];
  711. add_custom_type(value["name"], value["script"], value["description"], value["return_icon_type"], value["category"], value["highend"]);
  712. }
  713. _update_options_menu();
  714. }
  715. String VisualShaderEditor::_get_description(int p_idx) {
  716. return add_options[p_idx].description;
  717. }
  718. void VisualShaderEditor::_update_options_menu() {
  719. node_desc->set_text("");
  720. members_dialog->get_ok()->set_disabled(true);
  721. members->clear();
  722. TreeItem *root = members->create_item();
  723. String filter = node_filter->get_text().strip_edges();
  724. bool use_filter = !filter.empty();
  725. bool is_first_item = true;
  726. Color unsupported_color = get_theme_color("error_color", "Editor");
  727. Color supported_color = get_theme_color("warning_color", "Editor");
  728. static bool low_driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name") == "GLES2";
  729. Map<String, TreeItem *> folders;
  730. int current_func = -1;
  731. if (!visual_shader.is_null()) {
  732. current_func = visual_shader->get_mode();
  733. }
  734. Vector<AddOption> custom_options;
  735. Vector<AddOption> embedded_options;
  736. for (int i = 0; i < add_options.size(); i++) {
  737. if (!use_filter || add_options[i].name.findn(filter) != -1) {
  738. if ((add_options[i].func != current_func && add_options[i].func != -1) || !_is_available(add_options[i].mode)) {
  739. continue;
  740. }
  741. const_cast<AddOption &>(add_options[i]).temp_idx = i; // save valid id
  742. if (add_options[i].is_custom) {
  743. custom_options.push_back(add_options[i]);
  744. } else {
  745. embedded_options.push_back(add_options[i]);
  746. }
  747. }
  748. }
  749. Vector<AddOption> options;
  750. SortArray<AddOption, _OptionComparator> sorter;
  751. sorter.sort(custom_options.ptrw(), custom_options.size());
  752. options.append_array(custom_options);
  753. options.append_array(embedded_options);
  754. for (int i = 0; i < options.size(); i++) {
  755. String path = options[i].category;
  756. Vector<String> subfolders = path.split("/");
  757. TreeItem *category = nullptr;
  758. if (!folders.has(path)) {
  759. category = root;
  760. String path_temp = "";
  761. for (int j = 0; j < subfolders.size(); j++) {
  762. path_temp += subfolders[j];
  763. if (!folders.has(path_temp)) {
  764. category = members->create_item(category);
  765. category->set_selectable(0, false);
  766. category->set_collapsed(!use_filter);
  767. category->set_text(0, subfolders[j]);
  768. folders.insert(path_temp, category);
  769. } else {
  770. category = folders[path_temp];
  771. }
  772. }
  773. } else {
  774. category = folders[path];
  775. }
  776. TreeItem *item = members->create_item(category);
  777. if (options[i].highend && low_driver) {
  778. item->set_custom_color(0, unsupported_color);
  779. } else if (options[i].highend) {
  780. item->set_custom_color(0, supported_color);
  781. }
  782. item->set_text(0, options[i].name);
  783. if (is_first_item && use_filter) {
  784. item->select(0);
  785. node_desc->set_text(options[i].description);
  786. is_first_item = false;
  787. }
  788. switch (options[i].return_type) {
  789. case VisualShaderNode::PORT_TYPE_SCALAR:
  790. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"));
  791. break;
  792. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  793. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"));
  794. break;
  795. case VisualShaderNode::PORT_TYPE_VECTOR:
  796. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"));
  797. break;
  798. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  799. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"));
  800. break;
  801. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  802. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform", "EditorIcons"));
  803. break;
  804. case VisualShaderNode::PORT_TYPE_SAMPLER:
  805. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"));
  806. break;
  807. default:
  808. break;
  809. }
  810. item->set_meta("id", options[i].temp_idx);
  811. }
  812. }
  813. void VisualShaderEditor::_set_mode(int p_which) {
  814. if (p_which == VisualShader::MODE_PARTICLES) {
  815. edit_type_standart->set_visible(false);
  816. edit_type_particles->set_visible(true);
  817. edit_type = edit_type_particles;
  818. particles_mode = true;
  819. } else {
  820. edit_type_particles->set_visible(false);
  821. edit_type_standart->set_visible(true);
  822. edit_type = edit_type_standart;
  823. particles_mode = false;
  824. }
  825. visual_shader->set_shader_type(get_current_shader_type());
  826. }
  827. Size2 VisualShaderEditor::get_minimum_size() const {
  828. return Size2(10, 200);
  829. }
  830. void VisualShaderEditor::_draw_color_over_button(Object *obj, Color p_color) {
  831. Button *button = Object::cast_to<Button>(obj);
  832. if (!button) {
  833. return;
  834. }
  835. Ref<StyleBox> normal = get_theme_stylebox("normal", "Button");
  836. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  837. }
  838. void VisualShaderEditor::_update_created_node(GraphNode *node) {
  839. if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
  840. Ref<StyleBoxFlat> sb = node->get_theme_stylebox("frame", "GraphNode");
  841. Color c = sb->get_border_color();
  842. Color ic;
  843. Color mono_color;
  844. if (((c.r + c.g + c.b) / 3) < 0.7) {
  845. mono_color = Color(1.0, 1.0, 1.0);
  846. ic = Color(0.0, 0.0, 0.0, 0.7);
  847. } else {
  848. mono_color = Color(0.0, 0.0, 0.0);
  849. ic = Color(1.0, 1.0, 1.0, 0.7);
  850. }
  851. mono_color.a = 0.85;
  852. c = mono_color;
  853. node->add_theme_color_override("title_color", c);
  854. c.a = 0.7;
  855. node->add_theme_color_override("close_color", c);
  856. node->add_theme_color_override("resizer_color", ic);
  857. }
  858. }
  859. void VisualShaderEditor::_update_uniforms(bool p_update_refs) {
  860. VisualShaderNodeUniformRef::clear_uniforms();
  861. for (int t = 0; t < VisualShader::TYPE_MAX; t++) {
  862. Vector<int> tnodes = visual_shader->get_node_list((VisualShader::Type)t);
  863. for (int i = 0; i < tnodes.size(); i++) {
  864. Ref<VisualShaderNode> vsnode = visual_shader->get_node((VisualShader::Type)t, tnodes[i]);
  865. Ref<VisualShaderNodeUniform> uniform = vsnode;
  866. if (uniform.is_valid()) {
  867. Ref<VisualShaderNodeFloatUniform> float_uniform = vsnode;
  868. Ref<VisualShaderNodeIntUniform> int_uniform = vsnode;
  869. Ref<VisualShaderNodeVec3Uniform> vec3_uniform = vsnode;
  870. Ref<VisualShaderNodeColorUniform> color_uniform = vsnode;
  871. Ref<VisualShaderNodeBooleanUniform> bool_uniform = vsnode;
  872. Ref<VisualShaderNodeTransformUniform> transform_uniform = vsnode;
  873. VisualShaderNodeUniformRef::UniformType uniform_type;
  874. if (float_uniform.is_valid()) {
  875. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_FLOAT;
  876. } else if (int_uniform.is_valid()) {
  877. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_INT;
  878. } else if (bool_uniform.is_valid()) {
  879. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_BOOLEAN;
  880. } else if (vec3_uniform.is_valid()) {
  881. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_VECTOR;
  882. } else if (transform_uniform.is_valid()) {
  883. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_TRANSFORM;
  884. } else if (color_uniform.is_valid()) {
  885. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_COLOR;
  886. } else {
  887. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_SAMPLER;
  888. }
  889. VisualShaderNodeUniformRef::add_uniform(uniform->get_uniform_name(), uniform_type);
  890. }
  891. }
  892. }
  893. if (p_update_refs) {
  894. graph_plugin->update_uniform_refs();
  895. }
  896. }
  897. void VisualShaderEditor::_update_uniform_refs(Set<String> &p_deleted_names) {
  898. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  899. VisualShader::Type type = VisualShader::Type(i);
  900. Vector<int> nodes = visual_shader->get_node_list(type);
  901. for (int j = 0; j < nodes.size(); j++) {
  902. if (j > 0) {
  903. Ref<VisualShaderNodeUniformRef> ref = visual_shader->get_node(type, nodes[j]);
  904. if (ref.is_valid()) {
  905. if (p_deleted_names.has(ref->get_uniform_name())) {
  906. undo_redo->add_do_method(ref.ptr(), "set_uniform_name", "[None]");
  907. undo_redo->add_undo_method(ref.ptr(), "set_uniform_name", ref->get_uniform_name());
  908. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", VisualShader::Type(i), nodes[j]);
  909. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", VisualShader::Type(i), nodes[j]);
  910. }
  911. }
  912. }
  913. }
  914. }
  915. }
  916. void VisualShaderEditor::_update_graph() {
  917. if (updating) {
  918. return;
  919. }
  920. if (visual_shader.is_null()) {
  921. return;
  922. }
  923. graph->set_scroll_ofs(visual_shader->get_graph_offset() * EDSCALE);
  924. VisualShader::Type type = get_current_shader_type();
  925. graph->clear_connections();
  926. //erase all nodes
  927. for (int i = 0; i < graph->get_child_count(); i++) {
  928. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  929. Node *node = graph->get_child(i);
  930. graph->remove_child(node);
  931. memdelete(node);
  932. i--;
  933. }
  934. }
  935. List<VisualShader::Connection> connections;
  936. visual_shader->get_node_connections(type, &connections);
  937. graph_plugin->set_connections(connections);
  938. Vector<int> nodes = visual_shader->get_node_list(type);
  939. _update_uniforms(false);
  940. graph_plugin->clear_links();
  941. graph_plugin->make_dirty(true);
  942. for (int n_i = 0; n_i < nodes.size(); n_i++) {
  943. graph_plugin->add_node(type, nodes[n_i]);
  944. }
  945. graph_plugin->make_dirty(false);
  946. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  947. int from = E->get().from_node;
  948. int from_idx = E->get().from_port;
  949. int to = E->get().to_node;
  950. int to_idx = E->get().to_port;
  951. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  952. }
  953. }
  954. VisualShader::Type VisualShaderEditor::get_current_shader_type() const {
  955. VisualShader::Type type;
  956. if (particles_mode) {
  957. type = VisualShader::Type(edit_type->get_selected() + 3);
  958. } else {
  959. type = VisualShader::Type(edit_type->get_selected());
  960. }
  961. return type;
  962. }
  963. void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  964. VisualShader::Type type = get_current_shader_type();
  965. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  966. if (node.is_null()) {
  967. return;
  968. }
  969. undo_redo->create_action(TTR("Add input port"));
  970. undo_redo->add_do_method(node.ptr(), "add_input_port", p_port, p_port_type, p_name);
  971. undo_redo->add_undo_method(node.ptr(), "remove_input_port", p_port);
  972. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  973. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  974. undo_redo->commit_action();
  975. }
  976. void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  977. VisualShader::Type type = get_current_shader_type();
  978. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  979. if (node.is_null()) {
  980. return;
  981. }
  982. undo_redo->create_action(TTR("Add output port"));
  983. undo_redo->add_do_method(node.ptr(), "add_output_port", p_port, p_port_type, p_name);
  984. undo_redo->add_undo_method(node.ptr(), "remove_output_port", p_port);
  985. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  986. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  987. undo_redo->commit_action();
  988. }
  989. void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_port) {
  990. VisualShader::Type type = get_current_shader_type();
  991. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  992. if (node.is_null()) {
  993. return;
  994. }
  995. undo_redo->create_action(TTR("Change input port type"));
  996. undo_redo->add_do_method(node.ptr(), "set_input_port_type", p_port, p_type);
  997. undo_redo->add_undo_method(node.ptr(), "set_input_port_type", p_port, node->get_input_port_type(p_port));
  998. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  999. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1000. undo_redo->commit_action();
  1001. }
  1002. void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_port) {
  1003. VisualShader::Type type = get_current_shader_type();
  1004. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1005. if (node.is_null()) {
  1006. return;
  1007. }
  1008. undo_redo->create_action(TTR("Change output port type"));
  1009. undo_redo->add_do_method(node.ptr(), "set_output_port_type", p_port, p_type);
  1010. undo_redo->add_undo_method(node.ptr(), "set_output_port_type", p_port, node->get_output_port_type(p_port));
  1011. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1012. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1013. undo_redo->commit_action();
  1014. }
  1015. void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  1016. VisualShader::Type type = get_current_shader_type();
  1017. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1018. ERR_FAIL_COND(!node.is_valid());
  1019. undo_redo->create_action(TTR("Change input port name"));
  1020. undo_redo->add_do_method(node.ptr(), "set_input_port_name", p_port_id, p_text);
  1021. undo_redo->add_undo_method(node.ptr(), "set_input_port_name", p_port_id, node->get_input_port_name(p_port_id));
  1022. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1023. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1024. undo_redo->commit_action();
  1025. }
  1026. void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  1027. VisualShader::Type type = get_current_shader_type();
  1028. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1029. ERR_FAIL_COND(!node.is_valid());
  1030. undo_redo->create_action(TTR("Change output port name"));
  1031. undo_redo->add_do_method(node.ptr(), "set_output_port_name", p_port_id, p_text);
  1032. undo_redo->add_undo_method(node.ptr(), "set_output_port_name", p_port_id, node->get_output_port_name(p_port_id));
  1033. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1034. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1035. undo_redo->commit_action();
  1036. }
  1037. void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
  1038. VisualShader::Type type = get_current_shader_type();
  1039. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1040. if (node.is_null()) {
  1041. return;
  1042. }
  1043. undo_redo->create_action(TTR("Remove input port"));
  1044. List<VisualShader::Connection> conns;
  1045. visual_shader->get_node_connections(type, &conns);
  1046. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1047. int from_node = E->get().from_node;
  1048. int from_port = E->get().from_port;
  1049. int to_node = E->get().to_node;
  1050. int to_port = E->get().to_port;
  1051. if (to_node == p_node) {
  1052. if (to_port == p_port) {
  1053. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1054. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1055. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1056. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1057. } else if (to_port > p_port) {
  1058. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1059. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1060. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1061. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1062. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port - 1);
  1063. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1064. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1065. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1066. }
  1067. }
  1068. }
  1069. undo_redo->add_do_method(node.ptr(), "remove_input_port", p_port);
  1070. 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));
  1071. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1072. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1073. undo_redo->commit_action();
  1074. }
  1075. void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
  1076. VisualShader::Type type = get_current_shader_type();
  1077. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1078. if (node.is_null()) {
  1079. return;
  1080. }
  1081. undo_redo->create_action(TTR("Remove output port"));
  1082. List<VisualShader::Connection> conns;
  1083. visual_shader->get_node_connections(type, &conns);
  1084. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1085. int from_node = E->get().from_node;
  1086. int from_port = E->get().from_port;
  1087. int to_node = E->get().to_node;
  1088. int to_port = E->get().to_port;
  1089. if (from_node == p_node) {
  1090. if (from_port == p_port) {
  1091. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1092. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1093. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1094. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1095. } else if (from_port > p_port) {
  1096. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1097. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1098. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1099. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1100. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port - 1, to_node, to_port);
  1101. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1102. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1103. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1104. }
  1105. }
  1106. }
  1107. undo_redo->add_do_method(node.ptr(), "remove_output_port", p_port);
  1108. 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));
  1109. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1110. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1111. undo_redo->commit_action();
  1112. }
  1113. void VisualShaderEditor::_expression_focus_out(Object *code_edit, int p_node) {
  1114. VisualShader::Type type = get_current_shader_type();
  1115. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  1116. if (node.is_null()) {
  1117. return;
  1118. }
  1119. CodeEdit *expression_box = Object::cast_to<CodeEdit>(code_edit);
  1120. if (node->get_expression() == expression_box->get_text()) {
  1121. return;
  1122. }
  1123. undo_redo->create_action(TTR("Set expression"));
  1124. undo_redo->add_do_method(node.ptr(), "set_expression", expression_box->get_text());
  1125. undo_redo->add_undo_method(node.ptr(), "set_expression", node->get_expression());
  1126. undo_redo->commit_action();
  1127. }
  1128. void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p_size) {
  1129. VisualShader::Type type = get_current_shader_type();
  1130. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  1131. if (node.is_null()) {
  1132. return;
  1133. }
  1134. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1135. if (group_node.is_null()) {
  1136. return;
  1137. }
  1138. Vector2 size = p_size;
  1139. group_node->set_size(size);
  1140. GraphNode *gn = nullptr;
  1141. if (edit_type->get_selected() == p_type) { // check - otherwise the error will be emitted
  1142. Node *node2 = graph->get_node(itos(p_node));
  1143. gn = Object::cast_to<GraphNode>(node2);
  1144. if (!gn) {
  1145. return;
  1146. }
  1147. gn->set_custom_minimum_size(size);
  1148. gn->set_size(Size2(1, 1));
  1149. }
  1150. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1151. if (!expression_node.is_null()) {
  1152. Control *text_box = expression_node->get_control(0);
  1153. Size2 box_size = size;
  1154. if (gn != nullptr) {
  1155. if (box_size.x < 150 * EDSCALE || box_size.y < 0) {
  1156. box_size.x = gn->get_size().x;
  1157. }
  1158. }
  1159. box_size.x -= text_box->get_margin(MARGIN_LEFT);
  1160. box_size.x -= 28 * EDSCALE;
  1161. box_size.y -= text_box->get_margin(MARGIN_TOP);
  1162. box_size.y -= 28 * EDSCALE;
  1163. text_box->set_custom_minimum_size(Size2(box_size.x, box_size.y));
  1164. text_box->set_size(Size2(1, 1));
  1165. }
  1166. }
  1167. void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, int p_node) {
  1168. VisualShader::Type type = get_current_shader_type();
  1169. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1170. if (node.is_null()) {
  1171. return;
  1172. }
  1173. undo_redo->create_action(TTR("Resize VisualShader node"), UndoRedo::MERGE_ENDS);
  1174. undo_redo->add_do_method(this, "_set_node_size", p_type, p_node, p_new_size);
  1175. undo_redo->add_undo_method(this, "_set_node_size", p_type, p_node, node->get_size());
  1176. undo_redo->commit_action();
  1177. }
  1178. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  1179. VisualShader::Type type = get_current_shader_type();
  1180. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  1181. if (node.is_null()) {
  1182. return;
  1183. }
  1184. int prev_port = node->get_output_port_for_preview();
  1185. if (node->get_output_port_for_preview() == p_port) {
  1186. p_port = -1; //toggle it
  1187. }
  1188. undo_redo->create_action(p_port == -1 ? TTR("Hide Port Preview") : TTR("Show Port Preview"));
  1189. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  1190. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", prev_port);
  1191. undo_redo->add_do_method(graph_plugin.ptr(), "show_port_preview", (int)type, p_node, p_port);
  1192. undo_redo->add_undo_method(graph_plugin.ptr(), "show_port_preview", (int)type, p_node, prev_port);
  1193. undo_redo->commit_action();
  1194. }
  1195. void VisualShaderEditor::_uniform_line_edit_changed(const String &p_text, int p_node_id) {
  1196. VisualShader::Type type = get_current_shader_type();
  1197. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  1198. ERR_FAIL_COND(!node.is_valid());
  1199. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  1200. if (validated_name == node->get_uniform_name()) {
  1201. return;
  1202. }
  1203. undo_redo->create_action(TTR("Set Uniform Name"));
  1204. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  1205. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  1206. undo_redo->add_do_method(graph_plugin.ptr(), "set_uniform_name", type, p_node_id, validated_name);
  1207. undo_redo->add_undo_method(graph_plugin.ptr(), "set_uniform_name", type, p_node_id, node->get_uniform_name());
  1208. undo_redo->add_do_method(this, "_update_uniforms", true);
  1209. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1210. Set<String> changed_names;
  1211. changed_names.insert(node->get_uniform_name());
  1212. _update_uniform_refs(changed_names);
  1213. undo_redo->commit_action();
  1214. }
  1215. void VisualShaderEditor::_uniform_line_edit_focus_out(Object *line_edit, int p_node_id) {
  1216. _uniform_line_edit_changed(Object::cast_to<LineEdit>(line_edit)->get_text(), p_node_id);
  1217. }
  1218. void VisualShaderEditor::_port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output) {
  1219. VisualShader::Type type = get_current_shader_type();
  1220. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1221. ERR_FAIL_COND(!node.is_valid());
  1222. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  1223. if (!p_output) {
  1224. if (node->get_input_port_name(p_port_id) == text) {
  1225. return;
  1226. }
  1227. } else {
  1228. if (node->get_output_port_name(p_port_id) == text) {
  1229. return;
  1230. }
  1231. }
  1232. List<String> input_names;
  1233. List<String> output_names;
  1234. for (int i = 0; i < node->get_input_port_count(); i++) {
  1235. if (!p_output && i == p_port_id) {
  1236. continue;
  1237. }
  1238. input_names.push_back(node->get_input_port_name(i));
  1239. }
  1240. for (int i = 0; i < node->get_output_port_count(); i++) {
  1241. if (p_output && i == p_port_id) {
  1242. continue;
  1243. }
  1244. output_names.push_back(node->get_output_port_name(i));
  1245. }
  1246. String validated_name = visual_shader->validate_port_name(text, input_names, output_names);
  1247. if (validated_name == "") {
  1248. if (!p_output) {
  1249. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_input_port_name(p_port_id));
  1250. } else {
  1251. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_output_port_name(p_port_id));
  1252. }
  1253. return;
  1254. }
  1255. if (!p_output) {
  1256. _change_input_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1257. } else {
  1258. _change_output_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1259. }
  1260. }
  1261. void VisualShaderEditor::_port_edited() {
  1262. VisualShader::Type type = get_current_shader_type();
  1263. Variant value = property_editor->get_variant();
  1264. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  1265. ERR_FAIL_COND(!vsn.is_valid());
  1266. undo_redo->create_action(TTR("Set Input Default Port"));
  1267. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  1268. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1269. undo_redo->add_do_method(graph_plugin.ptr(), "set_input_port_default_value", type, editing_node, editing_port, value);
  1270. undo_redo->add_undo_method(graph_plugin.ptr(), "set_input_port_default_value", type, editing_node, editing_port, vsn->get_input_port_default_value(editing_port));
  1271. undo_redo->commit_action();
  1272. property_editor->hide();
  1273. }
  1274. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  1275. VisualShader::Type type = get_current_shader_type();
  1276. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  1277. Button *button = Object::cast_to<Button>(p_button);
  1278. ERR_FAIL_COND(!button);
  1279. Variant value = vsn->get_input_port_default_value(p_port);
  1280. property_editor->set_position(button->get_screen_position() + Vector2(0, button->get_size().height));
  1281. property_editor->edit(nullptr, "", value.get_type(), value, 0, "");
  1282. property_editor->popup();
  1283. editing_node = p_node;
  1284. editing_port = p_port;
  1285. }
  1286. void VisualShaderEditor::_add_custom_node(const String &p_path) {
  1287. int idx = -1;
  1288. for (int i = custom_node_option_idx; i < add_options.size(); i++) {
  1289. if (add_options[i].script.is_valid()) {
  1290. if (add_options[i].script->get_path() == p_path) {
  1291. idx = i;
  1292. break;
  1293. }
  1294. }
  1295. }
  1296. if (idx != -1) {
  1297. _add_node(idx);
  1298. }
  1299. }
  1300. void VisualShaderEditor::_add_cubemap_node(const String &p_path) {
  1301. VisualShaderNodeCubemap *cubemap = (VisualShaderNodeCubemap *)_add_node(cubemap_node_option_idx, -1);
  1302. cubemap->set_cube_map(ResourceLoader::load(p_path));
  1303. }
  1304. void VisualShaderEditor::_add_texture2d_node(const String &p_path) {
  1305. VisualShaderNodeTexture *texture2d = (VisualShaderNodeTexture *)_add_node(texture2d_node_option_idx, -1);
  1306. texture2d->set_texture(ResourceLoader::load(p_path));
  1307. }
  1308. void VisualShaderEditor::_add_texture2d_array_node(const String &p_path) {
  1309. VisualShaderNodeTexture2DArray *texture2d_array = (VisualShaderNodeTexture2DArray *)_add_node(texture2d_array_node_option_idx, -1);
  1310. texture2d_array->set_texture_array(ResourceLoader::load(p_path));
  1311. }
  1312. void VisualShaderEditor::_add_texture3d_node(const String &p_path) {
  1313. VisualShaderNodeTexture3D *texture3d = (VisualShaderNodeTexture3D *)_add_node(texture3d_node_option_idx, -1);
  1314. texture3d->set_texture(ResourceLoader::load(p_path));
  1315. }
  1316. VisualShaderNode *VisualShaderEditor::_add_node(int p_idx, int p_op_idx) {
  1317. ERR_FAIL_INDEX_V(p_idx, add_options.size(), nullptr);
  1318. Ref<VisualShaderNode> vsnode;
  1319. bool is_custom = add_options[p_idx].is_custom;
  1320. if (!is_custom && add_options[p_idx].type != String()) {
  1321. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(add_options[p_idx].type));
  1322. ERR_FAIL_COND_V(!vsn, nullptr);
  1323. VisualShaderNodeFloatConstant *constant = Object::cast_to<VisualShaderNodeFloatConstant>(vsn);
  1324. if (constant) {
  1325. if ((int)add_options[p_idx].value != -1) {
  1326. constant->set_constant(add_options[p_idx].value);
  1327. }
  1328. }
  1329. if (p_op_idx != -1) {
  1330. VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(vsn);
  1331. if (input) {
  1332. input->set_input_name(add_options[p_idx].sub_func_str);
  1333. }
  1334. VisualShaderNodeIs *is = Object::cast_to<VisualShaderNodeIs>(vsn);
  1335. if (is) {
  1336. is->set_function((VisualShaderNodeIs::Function)p_op_idx);
  1337. }
  1338. VisualShaderNodeCompare *cmp = Object::cast_to<VisualShaderNodeCompare>(vsn);
  1339. if (cmp) {
  1340. cmp->set_function((VisualShaderNodeCompare::Function)p_op_idx);
  1341. }
  1342. VisualShaderNodeColorOp *colorOp = Object::cast_to<VisualShaderNodeColorOp>(vsn);
  1343. if (colorOp) {
  1344. colorOp->set_operator((VisualShaderNodeColorOp::Operator)p_op_idx);
  1345. }
  1346. VisualShaderNodeColorFunc *colorFunc = Object::cast_to<VisualShaderNodeColorFunc>(vsn);
  1347. if (colorFunc) {
  1348. colorFunc->set_function((VisualShaderNodeColorFunc::Function)p_op_idx);
  1349. }
  1350. VisualShaderNodeFloatOp *floatOp = Object::cast_to<VisualShaderNodeFloatOp>(vsn);
  1351. if (floatOp) {
  1352. floatOp->set_operator((VisualShaderNodeFloatOp::Operator)p_op_idx);
  1353. }
  1354. VisualShaderNodeIntOp *intOp = Object::cast_to<VisualShaderNodeIntOp>(vsn);
  1355. if (intOp) {
  1356. intOp->set_operator((VisualShaderNodeIntOp::Operator)p_op_idx);
  1357. }
  1358. VisualShaderNodeFloatFunc *floatFunc = Object::cast_to<VisualShaderNodeFloatFunc>(vsn);
  1359. if (floatFunc) {
  1360. floatFunc->set_function((VisualShaderNodeFloatFunc::Function)p_op_idx);
  1361. }
  1362. VisualShaderNodeIntFunc *intFunc = Object::cast_to<VisualShaderNodeIntFunc>(vsn);
  1363. if (intFunc) {
  1364. intFunc->set_function((VisualShaderNodeIntFunc::Function)p_op_idx);
  1365. }
  1366. VisualShaderNodeVectorOp *vecOp = Object::cast_to<VisualShaderNodeVectorOp>(vsn);
  1367. if (vecOp) {
  1368. vecOp->set_operator((VisualShaderNodeVectorOp::Operator)p_op_idx);
  1369. }
  1370. VisualShaderNodeVectorFunc *vecFunc = Object::cast_to<VisualShaderNodeVectorFunc>(vsn);
  1371. if (vecFunc) {
  1372. vecFunc->set_function((VisualShaderNodeVectorFunc::Function)p_op_idx);
  1373. }
  1374. VisualShaderNodeTransformFunc *matFunc = Object::cast_to<VisualShaderNodeTransformFunc>(vsn);
  1375. if (matFunc) {
  1376. matFunc->set_function((VisualShaderNodeTransformFunc::Function)p_op_idx);
  1377. }
  1378. VisualShaderNodeScalarDerivativeFunc *sderFunc = Object::cast_to<VisualShaderNodeScalarDerivativeFunc>(vsn);
  1379. if (sderFunc) {
  1380. sderFunc->set_function((VisualShaderNodeScalarDerivativeFunc::Function)p_op_idx);
  1381. }
  1382. VisualShaderNodeVectorDerivativeFunc *vderFunc = Object::cast_to<VisualShaderNodeVectorDerivativeFunc>(vsn);
  1383. if (vderFunc) {
  1384. vderFunc->set_function((VisualShaderNodeVectorDerivativeFunc::Function)p_op_idx);
  1385. }
  1386. VisualShaderNodeMultiplyAdd *fmaFunc = Object::cast_to<VisualShaderNodeMultiplyAdd>(vsn);
  1387. if (fmaFunc) {
  1388. fmaFunc->set_op_type((VisualShaderNodeMultiplyAdd::OpType)p_op_idx);
  1389. }
  1390. }
  1391. vsnode = Ref<VisualShaderNode>(vsn);
  1392. } else {
  1393. ERR_FAIL_COND_V(add_options[p_idx].script.is_null(), nullptr);
  1394. String base_type = add_options[p_idx].script->get_instance_base_type();
  1395. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(base_type));
  1396. ERR_FAIL_COND_V(!vsn, nullptr);
  1397. vsnode = Ref<VisualShaderNode>(vsn);
  1398. vsnode->set_script(add_options[p_idx].script);
  1399. }
  1400. Point2 position = graph->get_scroll_ofs();
  1401. if (saved_node_pos_dirty) {
  1402. position += saved_node_pos;
  1403. } else {
  1404. position += graph->get_size() * 0.5;
  1405. position /= EDSCALE;
  1406. }
  1407. saved_node_pos_dirty = false;
  1408. VisualShader::Type type = get_current_shader_type();
  1409. int id_to_use = visual_shader->get_valid_node_id(type);
  1410. undo_redo->create_action(TTR("Add Node to Visual Shader"));
  1411. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  1412. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  1413. undo_redo->add_do_method(graph_plugin.ptr(), "add_node", type, id_to_use);
  1414. undo_redo->add_undo_method(graph_plugin.ptr(), "remove_node", type, id_to_use);
  1415. VisualShaderNodeExpression *expr = Object::cast_to<VisualShaderNodeExpression>(vsnode.ptr());
  1416. if (expr) {
  1417. undo_redo->add_do_method(expr, "set_size", Size2(250 * EDSCALE, 150 * EDSCALE));
  1418. }
  1419. if (to_node != -1 && to_slot != -1) {
  1420. if (vsnode->get_output_port_count() > 0) {
  1421. int _from_node = id_to_use;
  1422. int _from_slot = 0;
  1423. 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))) {
  1424. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1425. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1426. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1427. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1428. }
  1429. }
  1430. } else if (from_node != -1 && from_slot != -1) {
  1431. if (vsnode->get_input_port_count() > 0) {
  1432. int _to_node = id_to_use;
  1433. int _to_slot = 0;
  1434. 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))) {
  1435. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1436. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1437. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1438. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1439. }
  1440. }
  1441. }
  1442. VisualShaderNodeUniform *uniform = Object::cast_to<VisualShaderNodeUniform>(vsnode.ptr());
  1443. if (uniform) {
  1444. undo_redo->add_do_method(this, "_update_uniforms", true);
  1445. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1446. }
  1447. undo_redo->commit_action();
  1448. return vsnode.ptr();
  1449. }
  1450. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  1451. VisualShader::Type type = get_current_shader_type();
  1452. drag_buffer.push_back({ type, p_node, p_from, p_to });
  1453. if (!drag_dirty) {
  1454. call_deferred("_nodes_dragged");
  1455. }
  1456. drag_dirty = true;
  1457. }
  1458. void VisualShaderEditor::_nodes_dragged() {
  1459. drag_dirty = false;
  1460. undo_redo->create_action(TTR("Node(s) Moved"));
  1461. for (List<DragOp>::Element *E = drag_buffer.front(); E; E = E->next()) {
  1462. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  1463. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  1464. undo_redo->add_do_method(graph_plugin.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  1465. undo_redo->add_undo_method(graph_plugin.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  1466. }
  1467. drag_buffer.clear();
  1468. undo_redo->commit_action();
  1469. }
  1470. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1471. VisualShader::Type type = get_current_shader_type();
  1472. int from = p_from.to_int();
  1473. int to = p_to.to_int();
  1474. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  1475. return;
  1476. }
  1477. undo_redo->create_action(TTR("Nodes Connected"));
  1478. List<VisualShader::Connection> conns;
  1479. visual_shader->get_node_connections(type, &conns);
  1480. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1481. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  1482. 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);
  1483. 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);
  1484. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1485. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1486. }
  1487. }
  1488. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1489. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1490. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1491. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1492. undo_redo->commit_action();
  1493. }
  1494. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1495. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  1496. VisualShader::Type type = get_current_shader_type();
  1497. int from = p_from.to_int();
  1498. int to = p_to.to_int();
  1499. undo_redo->create_action(TTR("Nodes Disconnected"));
  1500. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1501. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1502. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1503. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1504. undo_redo->commit_action();
  1505. }
  1506. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  1507. from_node = p_from.to_int();
  1508. from_slot = p_from_slot;
  1509. _show_members_dialog(true);
  1510. }
  1511. void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  1512. to_node = p_to.to_int();
  1513. to_slot = p_to_slot;
  1514. _show_members_dialog(true);
  1515. }
  1516. void VisualShaderEditor::_delete_request(int which) {
  1517. VisualShader::Type type = get_current_shader_type();
  1518. Ref<VisualShaderNode> node = Ref<VisualShaderNode>(visual_shader->get_node(type, which));
  1519. undo_redo->create_action(TTR("Delete Node"));
  1520. List<VisualShader::Connection> conns;
  1521. visual_shader->get_node_connections(type, &conns);
  1522. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1523. if (E->get().from_node == which || E->get().to_node == which) {
  1524. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1525. }
  1526. }
  1527. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, which);
  1528. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, which), which);
  1529. undo_redo->add_undo_method(graph_plugin.ptr(), "add_node", type, which);
  1530. undo_redo->add_do_method(this, "_clear_buffer");
  1531. undo_redo->add_undo_method(this, "_clear_buffer");
  1532. // restore size, inputs and outputs if node is group
  1533. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1534. if (group) {
  1535. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1536. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1537. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1538. }
  1539. // restore expression text if node is expression
  1540. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1541. if (expression) {
  1542. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1543. }
  1544. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1545. if (E->get().from_node == which || E->get().to_node == which) {
  1546. 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);
  1547. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1548. }
  1549. }
  1550. // delete a node from the graph
  1551. undo_redo->add_do_method(graph_plugin.ptr(), "remove_node", type, which);
  1552. VisualShaderNodeUniform *uniform = Object::cast_to<VisualShaderNodeUniform>(node.ptr());
  1553. if (uniform) {
  1554. undo_redo->add_do_method(this, "_update_uniforms", true);
  1555. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1556. Set<String> uniform_names;
  1557. uniform_names.insert(uniform->get_uniform_name());
  1558. _update_uniform_refs(uniform_names);
  1559. }
  1560. undo_redo->commit_action();
  1561. }
  1562. void VisualShaderEditor::_node_selected(Object *p_node) {
  1563. VisualShader::Type type = get_current_shader_type();
  1564. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  1565. ERR_FAIL_COND(!gn);
  1566. int id = String(gn->get_name()).to_int();
  1567. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  1568. ERR_FAIL_COND(!vsnode.is_valid());
  1569. //do not rely on this, makes editor more complex
  1570. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  1571. }
  1572. void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1573. Ref<InputEventMouseButton> mb = p_event;
  1574. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1575. List<int> to_change;
  1576. for (int i = 0; i < graph->get_child_count(); i++) {
  1577. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1578. if (gn) {
  1579. if (gn->is_selected() && gn->is_close_button_visible()) {
  1580. to_change.push_back(gn->get_name().operator String().to_int());
  1581. }
  1582. }
  1583. }
  1584. if (to_change.empty() && copy_nodes_buffer.empty()) {
  1585. _show_members_dialog(true);
  1586. } else {
  1587. popup_menu->set_item_disabled(NodeMenuOptions::COPY, to_change.empty());
  1588. popup_menu->set_item_disabled(NodeMenuOptions::PASTE, copy_nodes_buffer.empty());
  1589. popup_menu->set_item_disabled(NodeMenuOptions::DELETE, to_change.empty());
  1590. popup_menu->set_item_disabled(NodeMenuOptions::DUPLICATE, to_change.empty());
  1591. menu_point = graph->get_local_mouse_position();
  1592. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1593. popup_menu->set_position(gpos);
  1594. popup_menu->popup();
  1595. }
  1596. }
  1597. }
  1598. void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos) {
  1599. if (at_mouse_pos) {
  1600. saved_node_pos_dirty = true;
  1601. saved_node_pos = graph->get_local_mouse_position();
  1602. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1603. members_dialog->popup();
  1604. members_dialog->set_position(gpos);
  1605. } else {
  1606. members_dialog->popup();
  1607. saved_node_pos_dirty = false;
  1608. members_dialog->set_position(graph->get_global_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
  1609. }
  1610. // keep dialog within window bounds
  1611. Size2 window_size = DisplayServer::get_singleton()->window_get_size();
  1612. Rect2 dialog_rect = Rect2(members_dialog->get_position(), members_dialog->get_size());
  1613. if (dialog_rect.position.y + dialog_rect.size.y > window_size.y) {
  1614. int difference = dialog_rect.position.y + dialog_rect.size.y - window_size.y;
  1615. members_dialog->set_position(members_dialog->get_position() - Point2(0, difference));
  1616. }
  1617. if (dialog_rect.position.x + dialog_rect.size.x > window_size.x) {
  1618. int difference = dialog_rect.position.x + dialog_rect.size.x - window_size.x;
  1619. members_dialog->set_position(members_dialog->get_position() - Point2(difference, 0));
  1620. }
  1621. node_filter->call_deferred("grab_focus"); // still not visible
  1622. node_filter->select_all();
  1623. }
  1624. void VisualShaderEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  1625. Ref<InputEventKey> ie = p_ie;
  1626. if (ie.is_valid() && (ie->get_keycode() == KEY_UP ||
  1627. ie->get_keycode() == KEY_DOWN ||
  1628. ie->get_keycode() == KEY_ENTER ||
  1629. ie->get_keycode() == KEY_KP_ENTER)) {
  1630. members->call("_gui_input", ie);
  1631. node_filter->accept_event();
  1632. }
  1633. }
  1634. void VisualShaderEditor::_notification(int p_what) {
  1635. if (p_what == NOTIFICATION_ENTER_TREE) {
  1636. node_filter->set_clear_button_enabled(true);
  1637. // collapse tree by default
  1638. TreeItem *category = members->get_root()->get_children();
  1639. while (category) {
  1640. category->set_collapsed(true);
  1641. TreeItem *sub_category = category->get_children();
  1642. while (sub_category) {
  1643. sub_category->set_collapsed(true);
  1644. sub_category = sub_category->get_next();
  1645. }
  1646. category = category->get_next();
  1647. }
  1648. }
  1649. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1650. Dictionary dd = get_viewport()->gui_get_drag_data();
  1651. if (members->is_visible_in_tree() && dd.has("id")) {
  1652. members->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1653. }
  1654. } else if (p_what == NOTIFICATION_DRAG_END) {
  1655. members->set_drop_mode_flags(0);
  1656. }
  1657. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1658. highend_label->set_modulate(get_theme_color("vulkan_color", "Editor"));
  1659. error_panel->add_theme_style_override("panel", get_theme_stylebox("bg", "Tree"));
  1660. error_label->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
  1661. node_filter->set_right_icon(Control::get_theme_icon("Search", "EditorIcons"));
  1662. preview_shader->set_icon(Control::get_theme_icon("Shader", "EditorIcons"));
  1663. {
  1664. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  1665. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  1666. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  1667. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  1668. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  1669. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  1670. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  1671. Color members_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  1672. preview_text->add_theme_color_override("background_color", background_color);
  1673. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  1674. syntax_highlighter->add_keyword_color(E->get(), keyword_color);
  1675. }
  1676. preview_text->add_theme_font_override("font", get_theme_font("expression", "EditorFonts"));
  1677. preview_text->add_theme_color_override("font_color", text_color);
  1678. syntax_highlighter->set_number_color(number_color);
  1679. syntax_highlighter->set_symbol_color(symbol_color);
  1680. syntax_highlighter->set_function_color(function_color);
  1681. syntax_highlighter->set_member_variable_color(members_color);
  1682. syntax_highlighter->clear_color_regions();
  1683. syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
  1684. syntax_highlighter->add_color_region("//", "", comment_color, true);
  1685. error_text->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts"));
  1686. error_text->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
  1687. }
  1688. tools->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Tools", "EditorIcons"));
  1689. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree()) {
  1690. _update_graph();
  1691. }
  1692. }
  1693. }
  1694. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  1695. if (updating) {
  1696. return;
  1697. }
  1698. updating = true;
  1699. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  1700. updating = false;
  1701. }
  1702. void VisualShaderEditor::_node_changed(int p_id) {
  1703. if (updating) {
  1704. return;
  1705. }
  1706. if (is_visible_in_tree()) {
  1707. _update_graph();
  1708. }
  1709. }
  1710. void VisualShaderEditor::_dup_update_excluded(int p_type, Set<int> &r_excluded) {
  1711. r_excluded.clear();
  1712. VisualShader::Type type = (VisualShader::Type)p_type;
  1713. for (int i = 0; i < graph->get_child_count(); i++) {
  1714. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1715. if (gn) {
  1716. int id = String(gn->get_name()).to_int();
  1717. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1718. Ref<VisualShaderNodeOutput> output = node;
  1719. if (output.is_valid()) {
  1720. r_excluded.insert(id);
  1721. continue;
  1722. }
  1723. r_excluded.insert(id);
  1724. }
  1725. }
  1726. }
  1727. void VisualShaderEditor::_dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded) {
  1728. VisualShader::Type type = (VisualShader::Type)p_type;
  1729. selection_center.x = 0.0f;
  1730. selection_center.y = 0.0f;
  1731. for (int i = 0; i < graph->get_child_count(); i++) {
  1732. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1733. if (gn) {
  1734. int id = String(gn->get_name()).to_int();
  1735. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1736. Ref<VisualShaderNodeOutput> output = node;
  1737. if (output.is_valid()) { // can't duplicate output
  1738. r_excluded.insert(id);
  1739. continue;
  1740. }
  1741. if (node.is_valid() && gn->is_selected()) {
  1742. Vector2 pos = visual_shader->get_node_position(type, id);
  1743. selection_center += pos;
  1744. r_nodes.push_back(id);
  1745. }
  1746. r_excluded.insert(id);
  1747. }
  1748. }
  1749. selection_center /= (float)r_nodes.size();
  1750. }
  1751. 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) {
  1752. VisualShader::Type type = (VisualShader::Type)p_type;
  1753. VisualShader::Type pasted_type = (VisualShader::Type)p_pasted_type;
  1754. int base_id = visual_shader->get_valid_node_id(type);
  1755. int id_from = base_id;
  1756. Map<int, int> connection_remap;
  1757. Set<int> unsupported_set;
  1758. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  1759. connection_remap[E->get()] = id_from;
  1760. Ref<VisualShaderNode> node = visual_shader->get_node(pasted_type, E->get());
  1761. bool unsupported = false;
  1762. for (int i = 0; i < add_options.size(); i++) {
  1763. if (add_options[i].type == node->get_class_name()) {
  1764. if (!_is_available(add_options[i].mode)) {
  1765. unsupported = true;
  1766. }
  1767. break;
  1768. }
  1769. }
  1770. if (unsupported) {
  1771. unsupported_set.insert(E->get());
  1772. continue;
  1773. }
  1774. Ref<VisualShaderNode> dupli = node->duplicate();
  1775. 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);
  1776. undo_redo->add_do_method(graph_plugin.ptr(), "add_node", type, id_from);
  1777. // duplicate size, inputs and outputs if node is group
  1778. Ref<VisualShaderNodeGroupBase> group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1779. if (!group.is_null()) {
  1780. undo_redo->add_do_method(dupli.ptr(), "set_size", group->get_size());
  1781. undo_redo->add_do_method(graph_plugin.ptr(), "set_node_size", type, id_from, group->get_size());
  1782. undo_redo->add_do_method(dupli.ptr(), "set_inputs", group->get_inputs());
  1783. undo_redo->add_do_method(dupli.ptr(), "set_outputs", group->get_outputs());
  1784. }
  1785. // duplicate expression text if node is expression
  1786. Ref<VisualShaderNodeExpression> expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1787. if (!expression.is_null()) {
  1788. undo_redo->add_do_method(dupli.ptr(), "set_expression", expression->get_expression());
  1789. }
  1790. id_from++;
  1791. }
  1792. List<VisualShader::Connection> conns;
  1793. visual_shader->get_node_connections(pasted_type, &conns);
  1794. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1795. if (unsupported_set.has(E->get().from_node) || unsupported_set.has(E->get().to_node)) {
  1796. continue;
  1797. }
  1798. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  1799. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1800. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1801. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1802. }
  1803. }
  1804. id_from = base_id;
  1805. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  1806. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  1807. undo_redo->add_undo_method(graph_plugin.ptr(), "remove_node", type, id_from);
  1808. id_from++;
  1809. }
  1810. undo_redo->commit_action();
  1811. if (p_select) {
  1812. // reselect duplicated nodes by excluding the other ones
  1813. for (int i = 0; i < graph->get_child_count(); i++) {
  1814. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1815. if (gn) {
  1816. int id = String(gn->get_name()).to_int();
  1817. if (!r_excluded.has(id)) {
  1818. gn->set_selected(true);
  1819. } else {
  1820. gn->set_selected(false);
  1821. }
  1822. }
  1823. }
  1824. }
  1825. }
  1826. void VisualShaderEditor::_clear_buffer() {
  1827. copy_nodes_buffer.clear();
  1828. copy_nodes_excluded_buffer.clear();
  1829. }
  1830. void VisualShaderEditor::_duplicate_nodes() {
  1831. int type = edit_type->get_selected();
  1832. List<int> nodes;
  1833. Set<int> excluded;
  1834. _dup_copy_nodes(type, nodes, excluded);
  1835. if (nodes.empty()) {
  1836. return;
  1837. }
  1838. undo_redo->create_action(TTR("Duplicate Nodes"));
  1839. _dup_paste_nodes(type, type, nodes, excluded, Vector2(10, 10) * EDSCALE, true);
  1840. }
  1841. void VisualShaderEditor::_copy_nodes() {
  1842. copy_type = edit_type->get_selected();
  1843. _clear_buffer();
  1844. _dup_copy_nodes(copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer);
  1845. }
  1846. void VisualShaderEditor::_paste_nodes(bool p_use_custom_position, const Vector2 &p_custom_position) {
  1847. if (copy_nodes_buffer.empty()) {
  1848. return;
  1849. }
  1850. int type = edit_type->get_selected();
  1851. undo_redo->create_action(TTR("Paste Nodes"));
  1852. float scale = graph->get_zoom();
  1853. Vector2 mpos;
  1854. if (p_use_custom_position) {
  1855. mpos = p_custom_position;
  1856. } else {
  1857. mpos = graph->get_local_mouse_position();
  1858. }
  1859. _dup_paste_nodes(type, copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + mpos / scale - selection_center), false);
  1860. _dup_update_excluded(type, copy_nodes_excluded_buffer); // to prevent selection of previous copies at new paste
  1861. }
  1862. void VisualShaderEditor::_delete_nodes() {
  1863. VisualShader::Type type = get_current_shader_type();
  1864. List<int> to_erase;
  1865. for (int i = 0; i < graph->get_child_count(); i++) {
  1866. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1867. if (gn) {
  1868. if (gn->is_selected() && gn->is_close_button_visible()) {
  1869. to_erase.push_back(gn->get_name().operator String().to_int());
  1870. }
  1871. }
  1872. }
  1873. if (to_erase.empty()) {
  1874. return;
  1875. }
  1876. undo_redo->create_action(TTR("Delete Nodes"));
  1877. List<VisualShader::Connection> conns;
  1878. visual_shader->get_node_connections(type, &conns);
  1879. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1880. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1881. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1882. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1883. }
  1884. }
  1885. }
  1886. Set<String> uniform_names;
  1887. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1888. Ref<VisualShaderNode> node = visual_shader->get_node(type, F->get());
  1889. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, F->get());
  1890. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, F->get()), F->get());
  1891. undo_redo->add_undo_method(graph_plugin.ptr(), "add_node", type, F->get());
  1892. undo_redo->add_do_method(this, "_clear_buffer");
  1893. undo_redo->add_undo_method(this, "_clear_buffer");
  1894. // restore size, inputs and outputs if node is group
  1895. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1896. if (group) {
  1897. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1898. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1899. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1900. }
  1901. // restore expression text if node is expression
  1902. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1903. if (expression) {
  1904. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1905. }
  1906. VisualShaderNodeUniform *uniform = Object::cast_to<VisualShaderNodeUniform>(node.ptr());
  1907. if (uniform) {
  1908. uniform_names.insert(uniform->get_uniform_name());
  1909. }
  1910. }
  1911. List<VisualShader::Connection> used_conns;
  1912. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1913. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1914. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1915. bool cancel = false;
  1916. for (List<VisualShader::Connection>::Element *R = used_conns.front(); R; R = R->next()) {
  1917. 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) {
  1918. cancel = true; // to avoid ERR_ALREADY_EXISTS warning
  1919. break;
  1920. }
  1921. }
  1922. if (!cancel) {
  1923. 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);
  1924. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1925. used_conns.push_back(E->get());
  1926. }
  1927. }
  1928. }
  1929. }
  1930. // delete nodes from the graph
  1931. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1932. undo_redo->add_do_method(graph_plugin.ptr(), "remove_node", type, F->get());
  1933. }
  1934. // update uniform refs if any uniform has been deleted
  1935. if (uniform_names.size() > 0) {
  1936. undo_redo->add_do_method(this, "_update_uniforms", true);
  1937. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1938. _update_uniform_refs(uniform_names);
  1939. }
  1940. undo_redo->commit_action();
  1941. }
  1942. void VisualShaderEditor::_mode_selected(int p_id) {
  1943. visual_shader->set_shader_type(particles_mode ? VisualShader::Type(p_id + 3) : VisualShader::Type(p_id));
  1944. _update_options_menu();
  1945. _update_graph();
  1946. }
  1947. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> p_input, String p_name) {
  1948. String prev_name = p_input->get_input_name();
  1949. if (p_name == prev_name) {
  1950. return;
  1951. }
  1952. bool type_changed = p_input->get_input_type_by_name(p_name) != p_input->get_input_type_by_name(prev_name);
  1953. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1954. undo_redo->create_action(TTR("Visual Shader Input Type Changed"));
  1955. undo_redo->add_do_method(p_input.ptr(), "set_input_name", p_name);
  1956. undo_redo->add_undo_method(p_input.ptr(), "set_input_name", prev_name);
  1957. // update output port
  1958. for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
  1959. VisualShader::Type type = VisualShader::Type(type_id);
  1960. int id = visual_shader->find_node_id(type, p_input);
  1961. if (id != VisualShader::NODE_ID_INVALID) {
  1962. if (type_changed) {
  1963. List<VisualShader::Connection> conns;
  1964. visual_shader->get_node_connections(type, &conns);
  1965. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1966. if (E->get().from_node == id) {
  1967. if (visual_shader->is_port_types_compatible(p_input->get_input_type_by_name(p_name), visual_shader->get_node(type, E->get().to_node)->get_input_port_type(E->get().to_port))) {
  1968. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1969. 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);
  1970. continue;
  1971. }
  1972. 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);
  1973. 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);
  1974. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1975. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1976. }
  1977. }
  1978. }
  1979. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type_id, id);
  1980. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type_id, id);
  1981. break;
  1982. }
  1983. }
  1984. undo_redo->commit_action();
  1985. }
  1986. void VisualShaderEditor::_uniform_select_item(Ref<VisualShaderNodeUniformRef> p_uniform_ref, String p_name) {
  1987. String prev_name = p_uniform_ref->get_uniform_name();
  1988. if (p_name == prev_name) {
  1989. return;
  1990. }
  1991. bool type_changed = p_uniform_ref->get_uniform_type_by_name(p_name) != p_uniform_ref->get_uniform_type_by_name(prev_name);
  1992. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1993. undo_redo->create_action(TTR("UniformRef Name Changed"));
  1994. undo_redo->add_do_method(p_uniform_ref.ptr(), "set_uniform_name", p_name);
  1995. undo_redo->add_undo_method(p_uniform_ref.ptr(), "set_uniform_name", prev_name);
  1996. // update output port
  1997. for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
  1998. VisualShader::Type type = VisualShader::Type(type_id);
  1999. int id = visual_shader->find_node_id(type, p_uniform_ref);
  2000. if (id != VisualShader::NODE_ID_INVALID) {
  2001. if (type_changed) {
  2002. List<VisualShader::Connection> conns;
  2003. visual_shader->get_node_connections(type, &conns);
  2004. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2005. if (E->get().from_node == id) {
  2006. if (visual_shader->is_port_types_compatible(p_uniform_ref->get_uniform_type_by_name(p_name), visual_shader->get_node(type, E->get().to_node)->get_input_port_type(E->get().to_port))) {
  2007. continue;
  2008. }
  2009. 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);
  2010. 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);
  2011. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2012. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2013. }
  2014. }
  2015. }
  2016. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type_id, id);
  2017. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type_id, id);
  2018. break;
  2019. }
  2020. }
  2021. undo_redo->commit_action();
  2022. }
  2023. void VisualShaderEditor::_member_filter_changed(const String &p_text) {
  2024. _update_options_menu();
  2025. }
  2026. void VisualShaderEditor::_member_selected() {
  2027. TreeItem *item = members->get_selected();
  2028. if (item != nullptr && item->has_meta("id")) {
  2029. members_dialog->get_ok()->set_disabled(false);
  2030. highend_label->set_visible(add_options[item->get_meta("id")].highend);
  2031. node_desc->set_text(_get_description(item->get_meta("id")));
  2032. } else {
  2033. highend_label->set_visible(false);
  2034. members_dialog->get_ok()->set_disabled(true);
  2035. node_desc->set_text("");
  2036. }
  2037. }
  2038. void VisualShaderEditor::_member_unselected() {
  2039. }
  2040. void VisualShaderEditor::_member_create() {
  2041. TreeItem *item = members->get_selected();
  2042. if (item != nullptr && item->has_meta("id")) {
  2043. int idx = members->get_selected()->get_meta("id");
  2044. _add_node(idx, add_options[idx].sub_func);
  2045. members_dialog->hide();
  2046. }
  2047. }
  2048. void VisualShaderEditor::_member_cancel() {
  2049. to_node = -1;
  2050. to_slot = -1;
  2051. from_node = -1;
  2052. from_slot = -1;
  2053. }
  2054. void VisualShaderEditor::_tools_menu_option(int p_idx) {
  2055. TreeItem *category = members->get_root()->get_children();
  2056. switch (p_idx) {
  2057. case EXPAND_ALL:
  2058. while (category) {
  2059. category->set_collapsed(false);
  2060. TreeItem *sub_category = category->get_children();
  2061. while (sub_category) {
  2062. sub_category->set_collapsed(false);
  2063. sub_category = sub_category->get_next();
  2064. }
  2065. category = category->get_next();
  2066. }
  2067. break;
  2068. case COLLAPSE_ALL:
  2069. while (category) {
  2070. category->set_collapsed(true);
  2071. TreeItem *sub_category = category->get_children();
  2072. while (sub_category) {
  2073. sub_category->set_collapsed(true);
  2074. sub_category = sub_category->get_next();
  2075. }
  2076. category = category->get_next();
  2077. }
  2078. break;
  2079. default:
  2080. break;
  2081. }
  2082. }
  2083. void VisualShaderEditor::_node_menu_id_pressed(int p_idx) {
  2084. switch (p_idx) {
  2085. case NodeMenuOptions::ADD:
  2086. _show_members_dialog(true);
  2087. break;
  2088. case NodeMenuOptions::COPY:
  2089. _copy_nodes();
  2090. break;
  2091. case NodeMenuOptions::PASTE:
  2092. _paste_nodes(true, menu_point);
  2093. break;
  2094. case NodeMenuOptions::DELETE:
  2095. _delete_nodes();
  2096. break;
  2097. case NodeMenuOptions::DUPLICATE:
  2098. _duplicate_nodes();
  2099. break;
  2100. }
  2101. }
  2102. Variant VisualShaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2103. if (p_from == members) {
  2104. TreeItem *it = members->get_item_at_position(p_point);
  2105. if (!it) {
  2106. return Variant();
  2107. }
  2108. if (!it->has_meta("id")) {
  2109. return Variant();
  2110. }
  2111. int id = it->get_meta("id");
  2112. AddOption op = add_options[id];
  2113. Dictionary d;
  2114. d["id"] = id;
  2115. if (op.sub_func == -1) {
  2116. d["sub_func"] = op.sub_func_str;
  2117. } else {
  2118. d["sub_func"] = op.sub_func;
  2119. }
  2120. Label *label = memnew(Label);
  2121. label->set_text(it->get_text(0));
  2122. set_drag_preview(label);
  2123. return d;
  2124. }
  2125. return Variant();
  2126. }
  2127. bool VisualShaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2128. if (p_from == graph) {
  2129. Dictionary d = p_data;
  2130. if (d.has("id")) {
  2131. return true;
  2132. }
  2133. if (d.has("files")) {
  2134. return true;
  2135. }
  2136. }
  2137. return false;
  2138. }
  2139. void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2140. if (p_from == graph) {
  2141. Dictionary d = p_data;
  2142. if (d.has("id")) {
  2143. int idx = d["id"];
  2144. saved_node_pos = p_point;
  2145. saved_node_pos_dirty = true;
  2146. _add_node(idx, add_options[idx].sub_func);
  2147. } else if (d.has("files")) {
  2148. if (d["files"].get_type() == Variant::PACKED_STRING_ARRAY) {
  2149. int j = 0;
  2150. PackedStringArray arr = d["files"];
  2151. for (int i = 0; i < arr.size(); i++) {
  2152. String type = ResourceLoader::get_resource_type(arr[i]);
  2153. if (type == "GDScript") {
  2154. Ref<Script> script = ResourceLoader::load(arr[i]);
  2155. if (script->get_instance_base_type() == "VisualShaderNodeCustom") {
  2156. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2157. saved_node_pos_dirty = true;
  2158. _add_custom_node(arr[i]);
  2159. j++;
  2160. }
  2161. } else if (ClassDB::get_parent_class(type) == "Texture2D") {
  2162. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2163. saved_node_pos_dirty = true;
  2164. _add_texture2d_node(arr[i]);
  2165. j++;
  2166. } else if (type == "Texture2DArray") {
  2167. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2168. saved_node_pos_dirty = true;
  2169. _add_texture2d_array_node(arr[i]);
  2170. j++;
  2171. } else if (ClassDB::get_parent_class(type) == "Texture3D") {
  2172. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2173. saved_node_pos_dirty = true;
  2174. _add_texture3d_node(arr[i]);
  2175. j++;
  2176. } else if (type == "Cubemap") {
  2177. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2178. saved_node_pos_dirty = true;
  2179. _add_cubemap_node(arr[i]);
  2180. j++;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. }
  2186. }
  2187. void VisualShaderEditor::_show_preview_text() {
  2188. preview_showed = !preview_showed;
  2189. preview_vbox->set_visible(preview_showed);
  2190. if (preview_showed) {
  2191. if (pending_update_preview) {
  2192. _update_preview();
  2193. pending_update_preview = false;
  2194. }
  2195. }
  2196. }
  2197. static ShaderLanguage::DataType _get_global_variable_type(const StringName &p_variable) {
  2198. RS::GlobalVariableType gvt = RS::get_singleton()->global_variable_get_type(p_variable);
  2199. return RS::global_variable_type_get_shader_datatype(gvt);
  2200. }
  2201. void VisualShaderEditor::_update_preview() {
  2202. if (!preview_showed) {
  2203. pending_update_preview = true;
  2204. return;
  2205. }
  2206. String code = visual_shader->get_code();
  2207. preview_text->set_text(code);
  2208. ShaderLanguage sl;
  2209. Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_types(), _get_global_variable_type);
  2210. for (int i = 0; i < preview_text->get_line_count(); i++) {
  2211. preview_text->set_line_as_marked(i, false);
  2212. }
  2213. if (err != OK) {
  2214. preview_text->set_line_as_marked(sl.get_error_line() - 1, true);
  2215. error_text->set_visible(true);
  2216. String text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  2217. error_text->set_text(text);
  2218. shader_error = true;
  2219. } else {
  2220. error_text->set_visible(false);
  2221. shader_error = false;
  2222. }
  2223. }
  2224. void VisualShaderEditor::_bind_methods() {
  2225. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  2226. ClassDB::bind_method("_update_options_menu", &VisualShaderEditor::_update_options_menu);
  2227. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  2228. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  2229. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  2230. ClassDB::bind_method("_uniform_select_item", &VisualShaderEditor::_uniform_select_item);
  2231. ClassDB::bind_method("_set_node_size", &VisualShaderEditor::_set_node_size);
  2232. ClassDB::bind_method("_clear_buffer", &VisualShaderEditor::_clear_buffer);
  2233. ClassDB::bind_method("_update_uniforms", &VisualShaderEditor::_update_uniforms);
  2234. ClassDB::bind_method("_set_mode", &VisualShaderEditor::_set_mode);
  2235. ClassDB::bind_method("_nodes_dragged", &VisualShaderEditor::_nodes_dragged);
  2236. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &VisualShaderEditor::get_drag_data_fw);
  2237. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &VisualShaderEditor::can_drop_data_fw);
  2238. ClassDB::bind_method(D_METHOD("drop_data_fw"), &VisualShaderEditor::drop_data_fw);
  2239. ClassDB::bind_method("_is_available", &VisualShaderEditor::_is_available);
  2240. }
  2241. VisualShaderEditor *VisualShaderEditor::singleton = nullptr;
  2242. VisualShaderEditor::VisualShaderEditor() {
  2243. singleton = this;
  2244. updating = false;
  2245. saved_node_pos_dirty = false;
  2246. saved_node_pos = Point2(0, 0);
  2247. ShaderLanguage::get_keyword_list(&keyword_list);
  2248. preview_showed = false;
  2249. pending_update_preview = false;
  2250. shader_error = false;
  2251. to_node = -1;
  2252. to_slot = -1;
  2253. from_node = -1;
  2254. from_slot = -1;
  2255. main_box = memnew(HSplitContainer);
  2256. main_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2257. main_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2258. add_child(main_box);
  2259. graph = memnew(GraphEdit);
  2260. graph->get_zoom_hbox()->set_h_size_flags(SIZE_EXPAND_FILL);
  2261. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  2262. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  2263. main_box->add_child(graph);
  2264. graph->set_drag_forwarding(this);
  2265. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  2266. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2267. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_BOOLEAN);
  2268. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  2269. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  2270. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SAMPLER);
  2271. //graph->add_valid_left_disconnect_type(0);
  2272. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  2273. graph->connect("connection_request", callable_mp(this, &VisualShaderEditor::_connection_request), varray(), CONNECT_DEFERRED);
  2274. graph->connect("disconnection_request", callable_mp(this, &VisualShaderEditor::_disconnection_request), varray(), CONNECT_DEFERRED);
  2275. graph->connect("node_selected", callable_mp(this, &VisualShaderEditor::_node_selected));
  2276. graph->connect("scroll_offset_changed", callable_mp(this, &VisualShaderEditor::_scroll_changed));
  2277. graph->connect("duplicate_nodes_request", callable_mp(this, &VisualShaderEditor::_duplicate_nodes));
  2278. graph->connect("copy_nodes_request", callable_mp(this, &VisualShaderEditor::_copy_nodes));
  2279. graph->connect("paste_nodes_request", callable_mp(this, &VisualShaderEditor::_paste_nodes));
  2280. graph->connect("delete_nodes_request", callable_mp(this, &VisualShaderEditor::_delete_nodes));
  2281. graph->connect("gui_input", callable_mp(this, &VisualShaderEditor::_graph_gui_input));
  2282. graph->connect("connection_to_empty", callable_mp(this, &VisualShaderEditor::_connection_to_empty));
  2283. graph->connect("connection_from_empty", callable_mp(this, &VisualShaderEditor::_connection_from_empty));
  2284. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  2285. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2286. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  2287. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2288. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2289. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_SCALAR);
  2290. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_VECTOR);
  2291. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2292. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  2293. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2294. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  2295. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2296. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR);
  2297. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2298. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_VECTOR);
  2299. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2300. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  2301. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SAMPLER, VisualShaderNode::PORT_TYPE_SAMPLER);
  2302. VSeparator *vs = memnew(VSeparator);
  2303. graph->get_zoom_hbox()->add_child(vs);
  2304. graph->get_zoom_hbox()->move_child(vs, 0);
  2305. edit_type_standart = memnew(OptionButton);
  2306. edit_type_standart->add_item(TTR("Vertex"));
  2307. edit_type_standart->add_item(TTR("Fragment"));
  2308. edit_type_standart->add_item(TTR("Light"));
  2309. edit_type_standart->select(1);
  2310. edit_type_standart->connect("item_selected", callable_mp(this, &VisualShaderEditor::_mode_selected));
  2311. edit_type_particles = memnew(OptionButton);
  2312. edit_type_particles->add_item(TTR("Emit"));
  2313. edit_type_particles->add_item(TTR("Process"));
  2314. edit_type_particles->add_item(TTR("End"));
  2315. edit_type_particles->select(0);
  2316. edit_type_particles->connect("item_selected", callable_mp(this, &VisualShaderEditor::_mode_selected));
  2317. edit_type = edit_type_standart;
  2318. graph->get_zoom_hbox()->add_child(edit_type_particles);
  2319. graph->get_zoom_hbox()->move_child(edit_type_particles, 0);
  2320. graph->get_zoom_hbox()->add_child(edit_type_standart);
  2321. graph->get_zoom_hbox()->move_child(edit_type_standart, 0);
  2322. add_node = memnew(Button);
  2323. add_node->set_flat(true);
  2324. graph->get_zoom_hbox()->add_child(add_node);
  2325. add_node->set_text(TTR("Add Node..."));
  2326. graph->get_zoom_hbox()->move_child(add_node, 0);
  2327. add_node->connect("pressed", callable_mp(this, &VisualShaderEditor::_show_members_dialog), varray(false));
  2328. preview_shader = memnew(Button);
  2329. preview_shader->set_flat(true);
  2330. preview_shader->set_toggle_mode(true);
  2331. preview_shader->set_tooltip(TTR("Show resulted shader code."));
  2332. graph->get_zoom_hbox()->add_child(preview_shader);
  2333. preview_shader->connect("pressed", callable_mp(this, &VisualShaderEditor::_show_preview_text));
  2334. ///////////////////////////////////////
  2335. // PREVIEW PANEL
  2336. ///////////////////////////////////////
  2337. preview_vbox = memnew(VBoxContainer);
  2338. preview_vbox->set_visible(preview_showed);
  2339. main_box->add_child(preview_vbox);
  2340. preview_text = memnew(CodeEdit);
  2341. syntax_highlighter.instance();
  2342. preview_vbox->add_child(preview_text);
  2343. preview_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2344. preview_text->set_v_size_flags(SIZE_EXPAND_FILL);
  2345. preview_text->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
  2346. preview_text->set_syntax_highlighter(syntax_highlighter);
  2347. preview_text->set_draw_line_numbers(true);
  2348. preview_text->set_readonly(true);
  2349. error_text = memnew(Label);
  2350. preview_vbox->add_child(error_text);
  2351. error_text->set_visible(false);
  2352. ///////////////////////////////////////
  2353. // POPUP MENU
  2354. ///////////////////////////////////////
  2355. popup_menu = memnew(PopupMenu);
  2356. add_child(popup_menu);
  2357. popup_menu->add_item("Add Node", NodeMenuOptions::ADD);
  2358. popup_menu->add_separator();
  2359. popup_menu->add_item("Copy", NodeMenuOptions::COPY);
  2360. popup_menu->add_item("Paste", NodeMenuOptions::PASTE);
  2361. popup_menu->add_item("Delete", NodeMenuOptions::DELETE);
  2362. popup_menu->add_item("Duplicate", NodeMenuOptions::DUPLICATE);
  2363. popup_menu->connect("id_pressed", callable_mp(this, &VisualShaderEditor::_node_menu_id_pressed));
  2364. ///////////////////////////////////////
  2365. // SHADER NODES TREE
  2366. ///////////////////////////////////////
  2367. VBoxContainer *members_vb = memnew(VBoxContainer);
  2368. members_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2369. HBoxContainer *filter_hb = memnew(HBoxContainer);
  2370. members_vb->add_child(filter_hb);
  2371. node_filter = memnew(LineEdit);
  2372. filter_hb->add_child(node_filter);
  2373. node_filter->connect("text_changed", callable_mp(this, &VisualShaderEditor::_member_filter_changed));
  2374. node_filter->connect("gui_input", callable_mp(this, &VisualShaderEditor::_sbox_input));
  2375. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2376. node_filter->set_placeholder(TTR("Search"));
  2377. tools = memnew(MenuButton);
  2378. filter_hb->add_child(tools);
  2379. tools->set_tooltip(TTR("Options"));
  2380. tools->get_popup()->connect("id_pressed", callable_mp(this, &VisualShaderEditor::_tools_menu_option));
  2381. tools->get_popup()->add_item(TTR("Expand All"), EXPAND_ALL);
  2382. tools->get_popup()->add_item(TTR("Collapse All"), COLLAPSE_ALL);
  2383. members = memnew(Tree);
  2384. members_vb->add_child(members);
  2385. members->set_drag_forwarding(this);
  2386. members->set_h_size_flags(SIZE_EXPAND_FILL);
  2387. members->set_v_size_flags(SIZE_EXPAND_FILL);
  2388. members->set_hide_root(true);
  2389. members->set_allow_reselect(true);
  2390. members->set_hide_folding(false);
  2391. members->set_custom_minimum_size(Size2(180 * EDSCALE, 200 * EDSCALE));
  2392. members->connect("item_activated", callable_mp(this, &VisualShaderEditor::_member_create));
  2393. members->connect("item_selected", callable_mp(this, &VisualShaderEditor::_member_selected));
  2394. members->connect("nothing_selected", callable_mp(this, &VisualShaderEditor::_member_unselected));
  2395. HBoxContainer *desc_hbox = memnew(HBoxContainer);
  2396. members_vb->add_child(desc_hbox);
  2397. Label *desc_label = memnew(Label);
  2398. desc_hbox->add_child(desc_label);
  2399. desc_label->set_text(TTR("Description:"));
  2400. desc_hbox->add_spacer();
  2401. highend_label = memnew(Label);
  2402. desc_hbox->add_child(highend_label);
  2403. highend_label->set_visible(false);
  2404. highend_label->set_text("Vulkan");
  2405. highend_label->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  2406. highend_label->set_tooltip(TTR("High-end node"));
  2407. node_desc = memnew(RichTextLabel);
  2408. members_vb->add_child(node_desc);
  2409. node_desc->set_h_size_flags(SIZE_EXPAND_FILL);
  2410. node_desc->set_v_size_flags(SIZE_FILL);
  2411. node_desc->set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  2412. members_dialog = memnew(ConfirmationDialog);
  2413. members_dialog->set_title(TTR("Create Shader Node"));
  2414. members_dialog->set_exclusive(false);
  2415. members_dialog->add_child(members_vb);
  2416. members_dialog->get_ok()->set_text(TTR("Create"));
  2417. members_dialog->get_ok()->connect("pressed", callable_mp(this, &VisualShaderEditor::_member_create));
  2418. members_dialog->get_ok()->set_disabled(true);
  2419. members_dialog->connect("cancelled", callable_mp(this, &VisualShaderEditor::_member_cancel));
  2420. add_child(members_dialog);
  2421. alert = memnew(AcceptDialog);
  2422. alert->get_label()->set_autowrap(true);
  2423. alert->get_label()->set_align(Label::ALIGN_CENTER);
  2424. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  2425. alert->get_label()->set_custom_minimum_size(Size2(400, 60) * EDSCALE);
  2426. add_child(alert);
  2427. ///////////////////////////////////////
  2428. // SHADER NODES TREE OPTIONS
  2429. ///////////////////////////////////////
  2430. // COLOR
  2431. add_options.push_back(AddOption("ColorFunc", "Color", "Common", "VisualShaderNodeColorFunc", TTR("Color function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2432. add_options.push_back(AddOption("ColorOp", "Color", "Common", "VisualShaderNodeColorOp", TTR("Color operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2433. add_options.push_back(AddOption("Grayscale", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Grayscale function."), VisualShaderNodeColorFunc::FUNC_GRAYSCALE, VisualShaderNode::PORT_TYPE_VECTOR));
  2434. add_options.push_back(AddOption("HSV2RGB", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts HSV vector to RGB equivalent."), VisualShaderNodeVectorFunc::FUNC_HSV2RGB, VisualShaderNode::PORT_TYPE_VECTOR));
  2435. add_options.push_back(AddOption("RGB2HSV", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts RGB vector to HSV equivalent."), VisualShaderNodeVectorFunc::FUNC_RGB2HSV, VisualShaderNode::PORT_TYPE_VECTOR));
  2436. add_options.push_back(AddOption("Sepia", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Sepia function."), VisualShaderNodeColorFunc::FUNC_SEPIA, VisualShaderNode::PORT_TYPE_VECTOR));
  2437. add_options.push_back(AddOption("Burn", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Burn operator."), VisualShaderNodeColorOp::OP_BURN, VisualShaderNode::PORT_TYPE_VECTOR));
  2438. add_options.push_back(AddOption("Darken", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Darken operator."), VisualShaderNodeColorOp::OP_DARKEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2439. add_options.push_back(AddOption("Difference", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Difference operator."), VisualShaderNodeColorOp::OP_DIFFERENCE, VisualShaderNode::PORT_TYPE_VECTOR));
  2440. add_options.push_back(AddOption("Dodge", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Dodge operator."), VisualShaderNodeColorOp::OP_DODGE, VisualShaderNode::PORT_TYPE_VECTOR));
  2441. add_options.push_back(AddOption("HardLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("HardLight operator."), VisualShaderNodeColorOp::OP_HARD_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2442. add_options.push_back(AddOption("Lighten", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Lighten operator."), VisualShaderNodeColorOp::OP_LIGHTEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2443. add_options.push_back(AddOption("Overlay", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Overlay operator."), VisualShaderNodeColorOp::OP_OVERLAY, VisualShaderNode::PORT_TYPE_VECTOR));
  2444. add_options.push_back(AddOption("Screen", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Screen operator."), VisualShaderNodeColorOp::OP_SCREEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2445. add_options.push_back(AddOption("SoftLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("SoftLight operator."), VisualShaderNodeColorOp::OP_SOFT_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2446. add_options.push_back(AddOption("ColorConstant", "Color", "Variables", "VisualShaderNodeColorConstant", TTR("Color constant."), -1, -1));
  2447. add_options.push_back(AddOption("ColorUniform", "Color", "Variables", "VisualShaderNodeColorUniform", TTR("Color uniform."), -1, -1));
  2448. // CONDITIONAL
  2449. const String &compare_func_desc = TTR("Returns the boolean result of the %s comparison between two parameters.");
  2450. add_options.push_back(AddOption("Equal", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Equal (==)")), VisualShaderNodeCompare::FUNC_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2451. add_options.push_back(AddOption("GreaterThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than (>)")), VisualShaderNodeCompare::FUNC_GREATER_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2452. 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));
  2453. 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));
  2454. 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));
  2455. 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));
  2456. add_options.push_back(AddOption("LessThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than (<)")), VisualShaderNodeCompare::FUNC_LESS_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2457. 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));
  2458. add_options.push_back(AddOption("NotEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Not Equal (!=)")), VisualShaderNodeCompare::FUNC_NOT_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2459. 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));
  2460. 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));
  2461. 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));
  2462. 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));
  2463. add_options.push_back(AddOption("BooleanConstant", "Conditional", "Variables", "VisualShaderNodeBooleanConstant", TTR("Boolean constant."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2464. add_options.push_back(AddOption("BooleanUniform", "Conditional", "Variables", "VisualShaderNodeBooleanUniform", TTR("Boolean uniform."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2465. // INPUT
  2466. // SPATIAL-FOR-ALL
  2467. const String input_param_shader_modes = TTR("'%s' input parameter for all shader modes.");
  2468. add_options.push_back(AddOption("Camera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2469. 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));
  2470. 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));
  2471. add_options.push_back(AddOption("Normal", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  2472. 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));
  2473. add_options.push_back(AddOption("Projection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2474. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_SPATIAL));
  2475. 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));
  2476. add_options.push_back(AddOption("World", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2477. // CANVASITEM-FOR-ALL
  2478. 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));
  2479. 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));
  2480. 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));
  2481. 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));
  2482. 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));
  2483. /////////////////
  2484. add_options.push_back(AddOption("Input", "Input", "Common", "VisualShaderNodeInput", TTR("Input parameter.")));
  2485. // SPATIAL INPUTS
  2486. const String input_param_for_vertex_and_fragment_shader_modes = TTR("'%s' input parameter for vertex and fragment shader modes.");
  2487. const String input_param_for_fragment_and_light_shader_modes = TTR("'%s' input parameter for fragment and light shader modes.");
  2488. const String input_param_for_fragment_shader_mode = TTR("'%s' input parameter for fragment shader mode.");
  2489. const String input_param_for_light_shader_mode = TTR("'%s' input parameter for light shader mode.");
  2490. const String input_param_for_vertex_shader_mode = TTR("'%s' input parameter for vertex shader mode.");
  2491. const String input_param_for_emit_shader_mode = TTR("'%s' input parameter for emit shader mode.");
  2492. const String input_param_for_process_shader_mode = TTR("'%s' input parameter for process shader mode.");
  2493. const String input_param_for_end_shader_mode = TTR("'%s' input parameter for end shader mode.");
  2494. const String input_param_for_emit_and_process_shader_mode = TTR("'%s' input parameter for emit and process shader mode.");
  2495. const String input_param_for_vertex_and_fragment_shader_mode = TTR("'%s' input parameter for vertex and fragment shader mode.");
  2496. add_options.push_back(AddOption("Alpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2497. add_options.push_back(AddOption("Binormal", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2498. add_options.push_back(AddOption("Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2499. add_options.push_back(AddOption("DepthTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "depth_texture"), "depth_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2500. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2501. add_options.push_back(AddOption("FrontFacing", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "front_facing"), "front_facing", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2502. add_options.push_back(AddOption("PointCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2503. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2504. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2505. add_options.push_back(AddOption("Side", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "side"), "side", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2506. add_options.push_back(AddOption("Tangent", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2507. add_options.push_back(AddOption("UV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2508. add_options.push_back(AddOption("UV2", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2509. add_options.push_back(AddOption("Vertex", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2510. add_options.push_back(AddOption("View", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2511. add_options.push_back(AddOption("Albedo", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "albedo"), "albedo", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2512. add_options.push_back(AddOption("Attenuation", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "attenuation"), "attenuation", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2513. add_options.push_back(AddOption("Diffuse", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "diffuse"), "diffuse", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2514. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2515. add_options.push_back(AddOption("Light", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light"), "light", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2516. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2517. add_options.push_back(AddOption("Roughness", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "roughness"), "roughness", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2518. add_options.push_back(AddOption("Specular", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "specular"), "specular", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2519. add_options.push_back(AddOption("Transmission", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "transmission"), "transmission", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2520. add_options.push_back(AddOption("View", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2521. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2522. add_options.push_back(AddOption("Binormal", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2523. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2524. add_options.push_back(AddOption("ModelView", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "modelview"), "modelview", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2525. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2526. add_options.push_back(AddOption("Tangent", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_mode, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2527. add_options.push_back(AddOption("UV", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2528. add_options.push_back(AddOption("UV2", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2529. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2530. // CANVASITEM INPUTS
  2531. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2532. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2533. add_options.push_back(AddOption("NormalTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "normal_texture"), "normal_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2534. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2535. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2536. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2537. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2538. add_options.push_back(AddOption("Texture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2539. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2540. add_options.push_back(AddOption("LightAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_alpha"), "light_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2541. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2542. add_options.push_back(AddOption("LightHeight", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_height"), "light_height", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2543. add_options.push_back(AddOption("LightUV", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_uv"), "light_uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2544. add_options.push_back(AddOption("LightVector", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_vec"), "light_vec", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2545. add_options.push_back(AddOption("Normal", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2546. 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, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2547. 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, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2548. add_options.push_back(AddOption("ShadowAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_alpha"), "shadow_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2549. add_options.push_back(AddOption("ShadowColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_color"), "shadow_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2550. add_options.push_back(AddOption("ShadowVec", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_vec"), "shadow_vec", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2551. add_options.push_back(AddOption("Texture", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2552. add_options.push_back(AddOption("Extra", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "extra"), "extra", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2553. 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, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2554. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2555. add_options.push_back(AddOption("Projection", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "projection"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2556. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2557. add_options.push_back(AddOption("World", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2558. // PARTICLES INPUTS
  2559. add_options.push_back(AddOption("Active", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2560. add_options.push_back(AddOption("Alpha", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2561. add_options.push_back(AddOption("Color", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2562. add_options.push_back(AddOption("Custom", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2563. add_options.push_back(AddOption("CustomAlpha", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2564. add_options.push_back(AddOption("Delta", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2565. add_options.push_back(AddOption("EmissionTransform", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2566. add_options.push_back(AddOption("Index", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR_INT, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2567. add_options.push_back(AddOption("LifeTime", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2568. add_options.push_back(AddOption("Restart", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2569. add_options.push_back(AddOption("Time", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2570. add_options.push_back(AddOption("Transform", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2571. add_options.push_back(AddOption("Velocity", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2572. add_options.push_back(AddOption("Active", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2573. add_options.push_back(AddOption("Alpha", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2574. add_options.push_back(AddOption("Color", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2575. add_options.push_back(AddOption("Custom", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2576. add_options.push_back(AddOption("CustomAlpha", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2577. add_options.push_back(AddOption("Delta", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2578. add_options.push_back(AddOption("EmissionTransform", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2579. add_options.push_back(AddOption("Index", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR_INT, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2580. add_options.push_back(AddOption("LifeTime", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2581. add_options.push_back(AddOption("Restart", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2582. add_options.push_back(AddOption("Time", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2583. add_options.push_back(AddOption("Transform", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2584. add_options.push_back(AddOption("Velocity", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2585. add_options.push_back(AddOption("Active", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2586. add_options.push_back(AddOption("Alpha", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2587. add_options.push_back(AddOption("Color", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2588. add_options.push_back(AddOption("Custom", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2589. add_options.push_back(AddOption("CustomAlpha", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2590. add_options.push_back(AddOption("Delta", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2591. add_options.push_back(AddOption("EmissionTransform", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2592. add_options.push_back(AddOption("Index", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR_INT, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2593. add_options.push_back(AddOption("LifeTime", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2594. add_options.push_back(AddOption("Restart", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2595. add_options.push_back(AddOption("Time", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2596. add_options.push_back(AddOption("Transform", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2597. add_options.push_back(AddOption("Velocity", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2598. // SKY INPUTS
  2599. add_options.push_back(AddOption("AtCubeMapPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "at_cubemap_pass"), "at_cubemap_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2600. add_options.push_back(AddOption("AtHalfResPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "at_half_res_pass"), "at_half_res_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2601. add_options.push_back(AddOption("AtQuarterResPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "at_quarter_res_pass"), "at_quarter_res_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2602. add_options.push_back(AddOption("EyeDir", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "eyedir"), "eyedir", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2603. add_options.push_back(AddOption("HalfResColor", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "half_res_color"), "half_res_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2604. add_options.push_back(AddOption("HalfResAlpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "half_res_alpha"), "half_res_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2605. add_options.push_back(AddOption("Light0Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_color"), "light0_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2606. add_options.push_back(AddOption("Light0Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_direction"), "light0_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2607. add_options.push_back(AddOption("Light0Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_enabled"), "light0_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2608. add_options.push_back(AddOption("Light0Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_energy"), "light0_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2609. add_options.push_back(AddOption("Light1Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_color"), "light1_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2610. add_options.push_back(AddOption("Light1Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_direction"), "light1_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2611. add_options.push_back(AddOption("Light1Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_enabled"), "light1_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2612. add_options.push_back(AddOption("Light1Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_energy"), "light1_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2613. add_options.push_back(AddOption("Light2Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_color"), "light2_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2614. add_options.push_back(AddOption("Light2Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_direction"), "light2_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2615. add_options.push_back(AddOption("Light2Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_enabled"), "light2_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2616. add_options.push_back(AddOption("Light2Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_energy"), "light2_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2617. add_options.push_back(AddOption("Light3Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_color"), "light3_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2618. add_options.push_back(AddOption("Light3Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_direction"), "light3_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2619. add_options.push_back(AddOption("Light3Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_enabled"), "light3_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2620. add_options.push_back(AddOption("Light3Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_energy"), "light3_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2621. add_options.push_back(AddOption("Position", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "position"), "position", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2622. add_options.push_back(AddOption("QuarterResColor", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "quarter_res_color"), "quarter_res_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2623. add_options.push_back(AddOption("QuarterResAlpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "quarter_res_alpha"), "quarter_res_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2624. add_options.push_back(AddOption("Radiance", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "radiance"), "radiance", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2625. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2626. add_options.push_back(AddOption("SkyCoords", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "sky_coords"), "sky_coords", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2627. add_options.push_back(AddOption("Time", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2628. // SCALAR
  2629. add_options.push_back(AddOption("FloatFunc", "Scalar", "Common", "VisualShaderNodeFloatFunc", TTR("Float function."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2630. add_options.push_back(AddOption("IntFunc", "Scalar", "Common", "VisualShaderNodeIntFunc", TTR("Integer function."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2631. add_options.push_back(AddOption("FloatOp", "Scalar", "Common", "VisualShaderNodeFloatOp", TTR("Float operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2632. add_options.push_back(AddOption("IntOp", "Scalar", "Common", "VisualShaderNodeIntOp", TTR("Integer operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2633. //CONSTANTS
  2634. add_options.push_back(AddOption("E", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("E constant (2.718282). Represents the base of the natural logarithm."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_E));
  2635. add_options.push_back(AddOption("Epsilon", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("Epsilon constant (0.00001). Smallest possible scalar number."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, CMP_EPSILON));
  2636. add_options.push_back(AddOption("Phi", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("Phi constant (1.618034). Golden ratio."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, 1.618034f));
  2637. add_options.push_back(AddOption("Pi/4", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("Pi/4 constant (0.785398) or 45 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 4));
  2638. add_options.push_back(AddOption("Pi/2", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("Pi/2 constant (1.570796) or 90 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 2));
  2639. add_options.push_back(AddOption("Pi", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("Pi constant (3.141593) or 180 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI));
  2640. add_options.push_back(AddOption("Tau", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("Tau constant (6.283185) or 360 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_TAU));
  2641. add_options.push_back(AddOption("Sqrt2", "Scalar", "Constants", "VisualShaderNodeFloatConstant", TTR("Sqrt2 constant (1.414214). Square root of 2."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_SQRT2));
  2642. // FUNCTIONS
  2643. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeFloatFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR));
  2644. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeIntFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2645. add_options.push_back(AddOption("ACos", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_SCALAR));
  2646. add_options.push_back(AddOption("ACosH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse hyperbolic cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ACOSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2647. add_options.push_back(AddOption("ASin", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2648. add_options.push_back(AddOption("ASinH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse hyperbolic sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ASINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2649. add_options.push_back(AddOption("ATan", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2650. add_options.push_back(AddOption("ATan2", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeFloatOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_SCALAR));
  2651. add_options.push_back(AddOption("ATanH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse hyperbolic tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_ATANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2652. add_options.push_back(AddOption("Ceil", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest integer that is greater than or equal to the parameter."), VisualShaderNodeFloatFunc::FUNC_CEIL, VisualShaderNode::PORT_TYPE_SCALAR));
  2653. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeScalarClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2654. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Constrains a value to lie between two further values."), VisualShaderNodeIntFunc::FUNC_CLAMP, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2655. add_options.push_back(AddOption("Cos", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_SCALAR));
  2656. add_options.push_back(AddOption("CosH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2657. add_options.push_back(AddOption("Degrees", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeFloatFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_SCALAR));
  2658. add_options.push_back(AddOption("Exp", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-e Exponential."), VisualShaderNodeFloatFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_SCALAR));
  2659. add_options.push_back(AddOption("Exp2", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-2 Exponential."), VisualShaderNodeFloatFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_SCALAR));
  2660. add_options.push_back(AddOption("Floor", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest integer less than or equal to the parameter."), VisualShaderNodeFloatFunc::FUNC_FLOOR, VisualShaderNode::PORT_TYPE_SCALAR));
  2661. add_options.push_back(AddOption("Fract", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeFloatFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_SCALAR));
  2662. add_options.push_back(AddOption("InverseSqrt", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse of the square root of the parameter."), VisualShaderNodeFloatFunc::FUNC_INVERSE_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2663. add_options.push_back(AddOption("Log", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Natural logarithm."), VisualShaderNodeFloatFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_SCALAR));
  2664. add_options.push_back(AddOption("Log2", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-2 logarithm."), VisualShaderNodeFloatFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_SCALAR));
  2665. add_options.push_back(AddOption("Max", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the greater of two values."), VisualShaderNodeFloatOp::OP_MAX, VisualShaderNode::PORT_TYPE_SCALAR));
  2666. add_options.push_back(AddOption("Min", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the lesser of two values."), VisualShaderNodeFloatOp::OP_MIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2667. add_options.push_back(AddOption("Mix", "Scalar", "Functions", "VisualShaderNodeScalarInterp", TTR("Linear interpolation between two scalars."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2668. add_options.push_back(AddOption("MultiplyAdd", "Scalar", "Functions", "VisualShaderNodeMultiplyAdd", TTR("Performs a fused multiply-add operation (a * b + c) on scalars."), VisualShaderNodeMultiplyAdd::OP_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR));
  2669. add_options.push_back(AddOption("Negate", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeFloatFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2670. add_options.push_back(AddOption("Negate", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeIntFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2671. add_options.push_back(AddOption("OneMinus", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("1.0 - scalar"), VisualShaderNodeFloatFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_SCALAR));
  2672. add_options.push_back(AddOption("Pow", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the value of the first parameter raised to the power of the second."), VisualShaderNodeFloatOp::OP_POW, VisualShaderNode::PORT_TYPE_SCALAR));
  2673. add_options.push_back(AddOption("Radians", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeFloatFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_SCALAR));
  2674. add_options.push_back(AddOption("Reciprocal", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("1.0 / scalar"), VisualShaderNodeFloatFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_SCALAR));
  2675. add_options.push_back(AddOption("Round", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeFloatFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_SCALAR));
  2676. add_options.push_back(AddOption("RoundEven", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest even integer to the parameter."), VisualShaderNodeFloatFunc::FUNC_ROUNDEVEN, VisualShaderNode::PORT_TYPE_SCALAR));
  2677. add_options.push_back(AddOption("Saturate", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Clamps the value between 0.0 and 1.0."), VisualShaderNodeFloatFunc::FUNC_SATURATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2678. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeFloatFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR));
  2679. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeIntFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2680. add_options.push_back(AddOption("Sin", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2681. add_options.push_back(AddOption("SinH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2682. add_options.push_back(AddOption("Sqrt", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeFloatFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2683. 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));
  2684. add_options.push_back(AddOption("Step", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Step function( scalar(edge), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeFloatOp::OP_STEP, VisualShaderNode::PORT_TYPE_SCALAR));
  2685. add_options.push_back(AddOption("Tan", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2686. add_options.push_back(AddOption("TanH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2687. add_options.push_back(AddOption("Trunc", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeFloatFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_SCALAR));
  2688. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Sums two floating-point scalars."), VisualShaderNodeFloatOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR));
  2689. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Sums two integer scalars."), VisualShaderNodeIntOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2690. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Divides two floating-point scalars."), VisualShaderNodeFloatOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR));
  2691. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Divides two integer scalars."), VisualShaderNodeIntOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2692. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Multiplies two floating-point scalars."), VisualShaderNodeFloatOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR));
  2693. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Multiplies two integer scalars."), VisualShaderNodeIntOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2694. add_options.push_back(AddOption("Remainder", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Returns the remainder of the two floating-point scalars."), VisualShaderNodeFloatOp::OP_MOD, VisualShaderNode::PORT_TYPE_SCALAR));
  2695. add_options.push_back(AddOption("Remainder", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Returns the remainder of the two integer scalars."), VisualShaderNodeIntOp::OP_MOD, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2696. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Subtracts two floating-point scalars."), VisualShaderNodeFloatOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR));
  2697. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Subtracts two integer scalars."), VisualShaderNodeIntOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2698. add_options.push_back(AddOption("FloatConstant", "Scalar", "Variables", "VisualShaderNodeFloatConstant", TTR("Scalar floating-point constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2699. add_options.push_back(AddOption("IntConstant", "Scalar", "Variables", "VisualShaderNodeIntConstant", TTR("Scalar integer constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2700. add_options.push_back(AddOption("FloatUniform", "Scalar", "Variables", "VisualShaderNodeFloatUniform", TTR("Scalar floating-point uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2701. add_options.push_back(AddOption("IntUniform", "Scalar", "Variables", "VisualShaderNodeIntUniform", TTR("Scalar integer uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2702. // TEXTURES
  2703. cubemap_node_option_idx = add_options.size();
  2704. add_options.push_back(AddOption("CubeMap", "Textures", "Functions", "VisualShaderNodeCubemap", TTR("Perform the cubic texture lookup."), -1, -1));
  2705. texture2d_node_option_idx = add_options.size();
  2706. add_options.push_back(AddOption("Texture2D", "Textures", "Functions", "VisualShaderNodeTexture", TTR("Perform the 2D texture lookup."), -1, -1));
  2707. texture2d_array_node_option_idx = add_options.size();
  2708. add_options.push_back(AddOption("Texture2DArray", "Textures", "Functions", "VisualShaderNodeTexture2DArray", TTR("Perform the 2D-array texture lookup."), -1, -1, -1, -1, -1));
  2709. texture3d_node_option_idx = add_options.size();
  2710. add_options.push_back(AddOption("Texture3D", "Textures", "Functions", "VisualShaderNodeTexture3D", TTR("Perform the 3D texture lookup."), -1, -1));
  2711. add_options.push_back(AddOption("CubeMapUniform", "Textures", "Variables", "VisualShaderNodeCubemapUniform", TTR("Cubic texture uniform lookup."), -1, -1));
  2712. add_options.push_back(AddOption("TextureUniform", "Textures", "Variables", "VisualShaderNodeTextureUniform", TTR("2D texture uniform lookup."), -1, -1));
  2713. add_options.push_back(AddOption("TextureUniformTriplanar", "Textures", "Variables", "VisualShaderNodeTextureUniformTriplanar", TTR("2D texture uniform lookup with triplanar."), -1, -1, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2714. add_options.push_back(AddOption("Texture2DArrayUniform", "Textures", "Variables", "VisualShaderNodeTexture2DArrayUniform", TTR("2D array of textures uniform lookup."), -1, -1, -1, -1, -1));
  2715. add_options.push_back(AddOption("Texture3DUniform", "Textures", "Variables", "VisualShaderNodeTexture3DUniform", TTR("3D texture uniform lookup."), -1, -1, -1, -1, -1));
  2716. // TRANSFORM
  2717. add_options.push_back(AddOption("TransformFunc", "Transform", "Common", "VisualShaderNodeTransformFunc", TTR("Transform function."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2718. 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));
  2719. add_options.push_back(AddOption("TransformCompose", "Transform", "Composition", "VisualShaderNodeTransformCompose", TTR("Composes transform from four vectors."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2720. add_options.push_back(AddOption("TransformDecompose", "Transform", "Composition", "VisualShaderNodeTransformDecompose", TTR("Decomposes transform to four vectors.")));
  2721. add_options.push_back(AddOption("Determinant", "Transform", "Functions", "VisualShaderNodeDeterminant", TTR("Calculates the determinant of a transform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2722. add_options.push_back(AddOption("Inverse", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the inverse of a transform."), VisualShaderNodeTransformFunc::FUNC_INVERSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2723. add_options.push_back(AddOption("Transpose", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the transpose of a transform."), VisualShaderNodeTransformFunc::FUNC_TRANSPOSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2724. add_options.push_back(AddOption("TransformMult", "Transform", "Operators", "VisualShaderNodeTransformMult", TTR("Multiplies transform by transform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2725. add_options.push_back(AddOption("TransformVectorMult", "Transform", "Operators", "VisualShaderNodeTransformVecMult", TTR("Multiplies vector by transform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2726. add_options.push_back(AddOption("TransformConstant", "Transform", "Variables", "VisualShaderNodeTransformConstant", TTR("Transform constant."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2727. add_options.push_back(AddOption("TransformUniform", "Transform", "Variables", "VisualShaderNodeTransformUniform", TTR("Transform uniform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2728. // VECTOR
  2729. add_options.push_back(AddOption("VectorFunc", "Vector", "Common", "VisualShaderNodeVectorFunc", TTR("Vector function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2730. add_options.push_back(AddOption("VectorOp", "Vector", "Common", "VisualShaderNodeVectorOp", TTR("Vector operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2731. add_options.push_back(AddOption("VectorCompose", "Vector", "Composition", "VisualShaderNodeVectorCompose", TTR("Composes vector from three scalars."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2732. add_options.push_back(AddOption("VectorDecompose", "Vector", "Composition", "VisualShaderNodeVectorDecompose", TTR("Decomposes vector to three scalars.")));
  2733. add_options.push_back(AddOption("Abs", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeVectorFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_VECTOR));
  2734. add_options.push_back(AddOption("ACos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_VECTOR));
  2735. 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));
  2736. add_options.push_back(AddOption("ASin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2737. 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));
  2738. add_options.push_back(AddOption("ATan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2739. add_options.push_back(AddOption("ATan2", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeVectorOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_VECTOR));
  2740. 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));
  2741. 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));
  2742. add_options.push_back(AddOption("Clamp", "Vector", "Functions", "VisualShaderNodeVectorClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2743. add_options.push_back(AddOption("Cos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_VECTOR));
  2744. add_options.push_back(AddOption("CosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_VECTOR));
  2745. add_options.push_back(AddOption("Cross", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Calculates the cross product of two vectors."), VisualShaderNodeVectorOp::OP_CROSS, VisualShaderNode::PORT_TYPE_VECTOR));
  2746. add_options.push_back(AddOption("Degrees", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeVectorFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_VECTOR));
  2747. add_options.push_back(AddOption("Distance", "Vector", "Functions", "VisualShaderNodeVectorDistance", TTR("Returns the distance between two points."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2748. add_options.push_back(AddOption("Dot", "Vector", "Functions", "VisualShaderNodeDotProduct", TTR("Calculates the dot product of two vectors."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2749. add_options.push_back(AddOption("Exp", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-e Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_VECTOR));
  2750. add_options.push_back(AddOption("Exp2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_VECTOR));
  2751. 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));
  2752. 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));
  2753. add_options.push_back(AddOption("Fract", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeVectorFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_VECTOR));
  2754. 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));
  2755. add_options.push_back(AddOption("Length", "Vector", "Functions", "VisualShaderNodeVectorLen", TTR("Calculates the length of a vector."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2756. add_options.push_back(AddOption("Log", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Natural logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_VECTOR));
  2757. add_options.push_back(AddOption("Log2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_VECTOR));
  2758. add_options.push_back(AddOption("Max", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the greater of two values."), VisualShaderNodeVectorOp::OP_MAX, VisualShaderNode::PORT_TYPE_VECTOR));
  2759. add_options.push_back(AddOption("Min", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the lesser of two values."), VisualShaderNodeVectorOp::OP_MIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2760. add_options.push_back(AddOption("Mix", "Vector", "Functions", "VisualShaderNodeVectorInterp", TTR("Linear interpolation between two vectors."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2761. add_options.push_back(AddOption("MixS", "Vector", "Functions", "VisualShaderNodeVectorScalarMix", TTR("Linear interpolation between two vectors using scalar."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2762. add_options.push_back(AddOption("MultiplyAdd", "Vector", "Functions", "VisualShaderNodeMultiplyAdd", TTR("Performs a fused multiply-add operation (a * b + c) on vectors."), VisualShaderNodeMultiplyAdd::OP_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR));
  2763. add_options.push_back(AddOption("Negate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeVectorFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_VECTOR));
  2764. add_options.push_back(AddOption("Normalize", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Calculates the normalize product of vector."), VisualShaderNodeVectorFunc::FUNC_NORMALIZE, VisualShaderNode::PORT_TYPE_VECTOR));
  2765. add_options.push_back(AddOption("OneMinus", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 - vector"), VisualShaderNodeVectorFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_VECTOR));
  2766. 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));
  2767. add_options.push_back(AddOption("Radians", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeVectorFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_VECTOR));
  2768. add_options.push_back(AddOption("Reciprocal", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 / vector"), VisualShaderNodeVectorFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_VECTOR));
  2769. 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));
  2770. 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));
  2771. add_options.push_back(AddOption("Round", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_VECTOR));
  2772. 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));
  2773. 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));
  2774. add_options.push_back(AddOption("Sign", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_VECTOR));
  2775. add_options.push_back(AddOption("Sin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2776. add_options.push_back(AddOption("SinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_VECTOR));
  2777. add_options.push_back(AddOption("Sqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  2778. 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));
  2779. 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));
  2780. 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));
  2781. 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));
  2782. add_options.push_back(AddOption("Tan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2783. add_options.push_back(AddOption("TanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_VECTOR));
  2784. add_options.push_back(AddOption("Trunc", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeVectorFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_VECTOR));
  2785. add_options.push_back(AddOption("Add", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Adds vector to vector."), VisualShaderNodeVectorOp::OP_ADD, VisualShaderNode::PORT_TYPE_VECTOR));
  2786. add_options.push_back(AddOption("Divide", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Divides vector by vector."), VisualShaderNodeVectorOp::OP_DIV, VisualShaderNode::PORT_TYPE_VECTOR));
  2787. add_options.push_back(AddOption("Multiply", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Multiplies vector by vector."), VisualShaderNodeVectorOp::OP_MUL, VisualShaderNode::PORT_TYPE_VECTOR));
  2788. add_options.push_back(AddOption("Remainder", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Returns the remainder of the two vectors."), VisualShaderNodeVectorOp::OP_MOD, VisualShaderNode::PORT_TYPE_VECTOR));
  2789. add_options.push_back(AddOption("Subtract", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Subtracts vector from vector."), VisualShaderNodeVectorOp::OP_SUB, VisualShaderNode::PORT_TYPE_VECTOR));
  2790. add_options.push_back(AddOption("VectorConstant", "Vector", "Variables", "VisualShaderNodeVec3Constant", TTR("Vector constant."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2791. add_options.push_back(AddOption("VectorUniform", "Vector", "Variables", "VisualShaderNodeVec3Uniform", TTR("Vector uniform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2792. // SPECIAL
  2793. 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.")));
  2794. 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));
  2795. 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.")));
  2796. add_options.push_back(AddOption("UniformRef", "Special", "", "VisualShaderNodeUniformRef", TTR("A reference to an existing uniform.")));
  2797. add_options.push_back(AddOption("ScalarDerivativeFunc", "Special", "Common", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) Scalar derivative function."), -1, VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2798. add_options.push_back(AddOption("VectorDerivativeFunc", "Special", "Common", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) Vector derivative function."), -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2799. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2800. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2801. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2802. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2803. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2804. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2805. custom_node_option_idx = add_options.size();
  2806. /////////////////////////////////////////////////////////////////////
  2807. _update_options_menu();
  2808. error_panel = memnew(PanelContainer);
  2809. add_child(error_panel);
  2810. error_label = memnew(Label);
  2811. error_panel->add_child(error_label);
  2812. error_label->set_text("eh");
  2813. error_panel->hide();
  2814. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2815. Ref<VisualShaderNodePluginDefault> default_plugin;
  2816. default_plugin.instance();
  2817. add_plugin(default_plugin);
  2818. graph_plugin.instance();
  2819. property_editor = memnew(CustomPropertyEditor);
  2820. add_child(property_editor);
  2821. property_editor->connect("variant_changed", callable_mp(this, &VisualShaderEditor::_port_edited));
  2822. }
  2823. /////////////////
  2824. void VisualShaderEditorPlugin::edit(Object *p_object) {
  2825. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  2826. }
  2827. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  2828. return p_object->is_class("VisualShader");
  2829. }
  2830. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  2831. if (p_visible) {
  2832. //editor->hide_animation_player_editors();
  2833. //editor->animation_panel_make_visible(true);
  2834. button->show();
  2835. editor->make_bottom_panel_item_visible(visual_shader_editor);
  2836. visual_shader_editor->update_custom_nodes();
  2837. visual_shader_editor->set_process_input(true);
  2838. //visual_shader_editor->set_process(true);
  2839. } else {
  2840. if (visual_shader_editor->is_visible_in_tree()) {
  2841. editor->hide_bottom_panel();
  2842. }
  2843. button->hide();
  2844. visual_shader_editor->set_process_input(false);
  2845. //visual_shader_editor->set_process(false);
  2846. }
  2847. }
  2848. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  2849. editor = p_node;
  2850. visual_shader_editor = memnew(VisualShaderEditor);
  2851. visual_shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  2852. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  2853. button->hide();
  2854. }
  2855. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  2856. }
  2857. ////////////////
  2858. class VisualShaderNodePluginInputEditor : public OptionButton {
  2859. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton);
  2860. Ref<VisualShaderNodeInput> input;
  2861. public:
  2862. void _notification(int p_what) {
  2863. if (p_what == NOTIFICATION_READY) {
  2864. connect("item_selected", callable_mp(this, &VisualShaderNodePluginInputEditor::_item_selected));
  2865. }
  2866. }
  2867. void _item_selected(int p_item) {
  2868. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  2869. }
  2870. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  2871. input = p_input;
  2872. Ref<Texture2D> type_icon[6] = {
  2873. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"),
  2874. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"),
  2875. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"),
  2876. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"),
  2877. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform", "EditorIcons"),
  2878. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"),
  2879. };
  2880. add_item("[None]");
  2881. int to_select = -1;
  2882. for (int i = 0; i < input->get_input_index_count(); i++) {
  2883. if (input->get_input_name() == input->get_input_index_name(i)) {
  2884. to_select = i + 1;
  2885. }
  2886. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  2887. }
  2888. if (to_select >= 0) {
  2889. select(to_select);
  2890. }
  2891. }
  2892. };
  2893. ////////////////
  2894. class VisualShaderNodePluginUniformRefEditor : public OptionButton {
  2895. GDCLASS(VisualShaderNodePluginUniformRefEditor, OptionButton);
  2896. Ref<VisualShaderNodeUniformRef> uniform_ref;
  2897. public:
  2898. void _notification(int p_what) {
  2899. if (p_what == NOTIFICATION_READY) {
  2900. connect("item_selected", callable_mp(this, &VisualShaderNodePluginUniformRefEditor::_item_selected));
  2901. }
  2902. }
  2903. void _item_selected(int p_item) {
  2904. VisualShaderEditor::get_singleton()->call_deferred("_uniform_select_item", uniform_ref, get_item_text(p_item));
  2905. }
  2906. void setup(const Ref<VisualShaderNodeUniformRef> &p_uniform_ref) {
  2907. uniform_ref = p_uniform_ref;
  2908. Ref<Texture2D> type_icon[7] = {
  2909. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"),
  2910. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"),
  2911. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"),
  2912. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"),
  2913. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform", "EditorIcons"),
  2914. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Color", "EditorIcons"),
  2915. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"),
  2916. };
  2917. add_item("[None]");
  2918. int to_select = -1;
  2919. for (int i = 0; i < p_uniform_ref->get_uniforms_count(); i++) {
  2920. if (p_uniform_ref->get_uniform_name() == p_uniform_ref->get_uniform_name_by_index(i)) {
  2921. to_select = i + 1;
  2922. }
  2923. add_icon_item(type_icon[p_uniform_ref->get_uniform_type_by_index(i)], p_uniform_ref->get_uniform_name_by_index(i));
  2924. }
  2925. if (to_select >= 0) {
  2926. select(to_select);
  2927. }
  2928. }
  2929. };
  2930. ////////////////
  2931. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  2932. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer);
  2933. Ref<Resource> parent_resource;
  2934. int node_id;
  2935. VisualShader::Type shader_type;
  2936. public:
  2937. void _property_changed(const String &p_property, const Variant &p_value, const String &p_field = "", bool p_changing = false) {
  2938. if (p_changing) {
  2939. return;
  2940. }
  2941. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2942. updating = true;
  2943. undo_redo->create_action(TTR("Edit Visual Property") + ": " + p_property, UndoRedo::MERGE_ENDS);
  2944. undo_redo->add_do_property(node.ptr(), p_property, p_value);
  2945. undo_redo->add_undo_property(node.ptr(), p_property, node->get(p_property));
  2946. if (p_value.get_type() == Variant::OBJECT) {
  2947. RES prev_res = node->get(p_property);
  2948. RES curr_res = p_value;
  2949. if (curr_res.is_null()) {
  2950. undo_redo->add_do_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2951. } else {
  2952. undo_redo->add_do_method(this, "_open_inspector", (RES)curr_res.ptr());
  2953. }
  2954. if (!prev_res.is_null()) {
  2955. undo_redo->add_undo_method(this, "_open_inspector", (RES)prev_res.ptr());
  2956. } else {
  2957. undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2958. }
  2959. }
  2960. if (p_property != "constant") {
  2961. undo_redo->add_do_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id);
  2962. undo_redo->add_undo_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id);
  2963. }
  2964. undo_redo->commit_action();
  2965. updating = false;
  2966. }
  2967. void _node_changed() {
  2968. if (updating) {
  2969. return;
  2970. }
  2971. for (int i = 0; i < properties.size(); i++) {
  2972. properties[i]->update_property();
  2973. }
  2974. }
  2975. void _resource_selected(const String &p_path, RES p_resource) {
  2976. _open_inspector(p_resource);
  2977. }
  2978. void _open_inspector(RES p_resource) {
  2979. EditorNode::get_singleton()->get_inspector()->edit(p_resource.ptr());
  2980. }
  2981. bool updating;
  2982. Ref<VisualShaderNode> node;
  2983. Vector<EditorProperty *> properties;
  2984. Vector<Label *> prop_names;
  2985. void _show_prop_names(bool p_show) {
  2986. for (int i = 0; i < prop_names.size(); i++) {
  2987. prop_names[i]->set_visible(p_show);
  2988. }
  2989. }
  2990. void setup(Ref<Resource> p_parent_resource, Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  2991. parent_resource = p_parent_resource;
  2992. updating = false;
  2993. node = p_node;
  2994. properties = p_properties;
  2995. node_id = (int)p_node->get_meta("id");
  2996. shader_type = VisualShader::Type((int)p_node->get_meta("shader_type"));
  2997. for (int i = 0; i < p_properties.size(); i++) {
  2998. HBoxContainer *hbox = memnew(HBoxContainer);
  2999. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3000. add_child(hbox);
  3001. Label *prop_name = memnew(Label);
  3002. String prop_name_str = p_names[i];
  3003. prop_name_str = prop_name_str.capitalize() + ":";
  3004. prop_name->set_text(prop_name_str);
  3005. prop_name->set_visible(false);
  3006. hbox->add_child(prop_name);
  3007. prop_names.push_back(prop_name);
  3008. p_properties[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  3009. hbox->add_child(p_properties[i]);
  3010. bool res_prop = Object::cast_to<EditorPropertyResource>(p_properties[i]);
  3011. if (res_prop) {
  3012. p_properties[i]->connect("resource_selected", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_resource_selected));
  3013. }
  3014. properties[i]->connect("property_changed", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_property_changed));
  3015. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  3016. properties[i]->update_property();
  3017. properties[i]->set_name_split_ratio(0);
  3018. }
  3019. node->connect("changed", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_node_changed));
  3020. }
  3021. static void _bind_methods() {
  3022. ClassDB::bind_method("_open_inspector", &VisualShaderNodePluginDefaultEditor::_open_inspector); // Used by UndoRedo.
  3023. ClassDB::bind_method("_show_prop_names", &VisualShaderNodePluginDefaultEditor::_show_prop_names); // Used with call_deferred.
  3024. }
  3025. };
  3026. Control *VisualShaderNodePluginDefault::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  3027. if (p_node->is_class("VisualShaderNodeUniformRef")) {
  3028. //create input
  3029. VisualShaderNodePluginUniformRefEditor *uniform_editor = memnew(VisualShaderNodePluginUniformRefEditor);
  3030. uniform_editor->setup(p_node);
  3031. return uniform_editor;
  3032. }
  3033. if (p_node->is_class("VisualShaderNodeInput")) {
  3034. //create input
  3035. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  3036. input_editor->setup(p_node);
  3037. return input_editor;
  3038. }
  3039. Vector<StringName> properties = p_node->get_editable_properties();
  3040. if (properties.size() == 0) {
  3041. return nullptr;
  3042. }
  3043. List<PropertyInfo> props;
  3044. p_node->get_property_list(&props);
  3045. Vector<PropertyInfo> pinfo;
  3046. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  3047. for (int i = 0; i < properties.size(); i++) {
  3048. if (E->get().name == String(properties[i])) {
  3049. pinfo.push_back(E->get());
  3050. }
  3051. }
  3052. }
  3053. if (pinfo.size() == 0) {
  3054. return nullptr;
  3055. }
  3056. properties.clear();
  3057. Ref<VisualShaderNode> node = p_node;
  3058. Vector<EditorProperty *> editors;
  3059. for (int i = 0; i < pinfo.size(); i++) {
  3060. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  3061. if (!prop) {
  3062. return nullptr;
  3063. }
  3064. if (Object::cast_to<EditorPropertyResource>(prop)) {
  3065. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  3066. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3067. } else if (Object::cast_to<EditorPropertyTransform>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  3068. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  3069. } else if (Object::cast_to<EditorPropertyFloat>(prop)) {
  3070. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3071. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  3072. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3073. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  3074. }
  3075. editors.push_back(prop);
  3076. properties.push_back(pinfo[i].name);
  3077. }
  3078. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  3079. editor->setup(p_parent_resource, editors, properties, p_node);
  3080. return editor;
  3081. }
  3082. void EditorPropertyShaderMode::_option_selected(int p_which) {
  3083. //will not use this, instead will do all the logic setting manually
  3084. //emit_signal("property_changed", get_edited_property(), p_which);
  3085. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  3086. if (visual_shader->get_mode() == p_which) {
  3087. return;
  3088. }
  3089. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3090. undo_redo->create_action(TTR("Visual Shader Mode Changed"));
  3091. //do is easy
  3092. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  3093. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  3094. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_set_mode", p_which);
  3095. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_set_mode", visual_shader->get_mode());
  3096. //now undo is hell
  3097. //1. restore connections to output
  3098. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  3099. VisualShader::Type type = VisualShader::Type(i);
  3100. List<VisualShader::Connection> conns;
  3101. visual_shader->get_node_connections(type, &conns);
  3102. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  3103. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  3104. 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);
  3105. }
  3106. }
  3107. }
  3108. //2. restore input indices
  3109. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  3110. VisualShader::Type type = VisualShader::Type(i);
  3111. Vector<int> nodes = visual_shader->get_node_list(type);
  3112. for (int j = 0; j < nodes.size(); j++) {
  3113. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[j]);
  3114. if (!input.is_valid()) {
  3115. continue;
  3116. }
  3117. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  3118. }
  3119. }
  3120. //3. restore enums and flags
  3121. List<PropertyInfo> props;
  3122. visual_shader->get_property_list(&props);
  3123. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  3124. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  3125. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  3126. }
  3127. }
  3128. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  3129. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  3130. //update graph
  3131. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  3132. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  3133. undo_redo->commit_action();
  3134. }
  3135. void EditorPropertyShaderMode::update_property() {
  3136. int which = get_edited_object()->get(get_edited_property());
  3137. options->select(which);
  3138. }
  3139. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  3140. for (int i = 0; i < p_options.size(); i++) {
  3141. options->add_item(p_options[i], i);
  3142. }
  3143. }
  3144. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  3145. options->set_clip_text(p_enable);
  3146. }
  3147. void EditorPropertyShaderMode::_bind_methods() {
  3148. }
  3149. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  3150. options = memnew(OptionButton);
  3151. options->set_clip_text(true);
  3152. add_child(options);
  3153. add_focusable(options);
  3154. options->connect("item_selected", callable_mp(this, &EditorPropertyShaderMode::_option_selected));
  3155. }
  3156. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  3157. return true; //can handle everything
  3158. }
  3159. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  3160. //do none
  3161. }
  3162. 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, bool p_wide) {
  3163. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  3164. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  3165. Vector<String> options = p_hint_text.split(",");
  3166. editor->setup(options);
  3167. add_property_editor(p_path, editor);
  3168. return true;
  3169. }
  3170. return false; //can be overridden, although it will most likely be last anyway
  3171. }
  3172. void EditorInspectorShaderModePlugin::parse_end() {
  3173. //do none
  3174. }
  3175. //////////////////////////////////
  3176. void VisualShaderNodePortPreview::_shader_changed() {
  3177. if (shader.is_null()) {
  3178. return;
  3179. }
  3180. Vector<VisualShader::DefaultTextureParam> default_textures;
  3181. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  3182. Ref<Shader> preview_shader;
  3183. preview_shader.instance();
  3184. preview_shader->set_code(shader_code);
  3185. for (int i = 0; i < default_textures.size(); i++) {
  3186. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  3187. }
  3188. Ref<ShaderMaterial> material;
  3189. material.instance();
  3190. material->set_shader(preview_shader);
  3191. //find if a material is also being edited and copy parameters to this one
  3192. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  3193. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  3194. if (!object) {
  3195. continue;
  3196. }
  3197. ShaderMaterial *src_mat = Object::cast_to<ShaderMaterial>(object);
  3198. if (src_mat && src_mat->get_shader().is_valid()) {
  3199. List<PropertyInfo> params;
  3200. src_mat->get_shader()->get_param_list(&params);
  3201. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  3202. material->set(E->get().name, src_mat->get(E->get().name));
  3203. }
  3204. }
  3205. }
  3206. set_material(material);
  3207. }
  3208. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  3209. shader = p_shader;
  3210. shader->connect("changed", callable_mp(this, &VisualShaderNodePortPreview::_shader_changed));
  3211. type = p_type;
  3212. port = p_port;
  3213. node = p_node;
  3214. update();
  3215. _shader_changed();
  3216. }
  3217. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  3218. return Size2(100, 100) * EDSCALE;
  3219. }
  3220. void VisualShaderNodePortPreview::_notification(int p_what) {
  3221. if (p_what == NOTIFICATION_DRAW) {
  3222. Vector<Vector2> points;
  3223. Vector<Vector2> uvs;
  3224. Vector<Color> colors;
  3225. points.push_back(Vector2());
  3226. uvs.push_back(Vector2(0, 0));
  3227. colors.push_back(Color(1, 1, 1, 1));
  3228. points.push_back(Vector2(get_size().width, 0));
  3229. uvs.push_back(Vector2(1, 0));
  3230. colors.push_back(Color(1, 1, 1, 1));
  3231. points.push_back(get_size());
  3232. uvs.push_back(Vector2(1, 1));
  3233. colors.push_back(Color(1, 1, 1, 1));
  3234. points.push_back(Vector2(0, get_size().height));
  3235. uvs.push_back(Vector2(0, 1));
  3236. colors.push_back(Color(1, 1, 1, 1));
  3237. draw_primitive(points, colors, uvs);
  3238. }
  3239. }
  3240. void VisualShaderNodePortPreview::_bind_methods() {
  3241. }
  3242. VisualShaderNodePortPreview::VisualShaderNodePortPreview() {
  3243. }
  3244. //////////////////////////////////
  3245. String VisualShaderConversionPlugin::converts_to() const {
  3246. return "Shader";
  3247. }
  3248. bool VisualShaderConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  3249. Ref<VisualShader> vshader = p_resource;
  3250. return vshader.is_valid();
  3251. }
  3252. Ref<Resource> VisualShaderConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  3253. Ref<VisualShader> vshader = p_resource;
  3254. ERR_FAIL_COND_V(!vshader.is_valid(), Ref<Resource>());
  3255. Ref<Shader> shader;
  3256. shader.instance();
  3257. String code = vshader->get_code();
  3258. shader->set_code(code);
  3259. return shader;
  3260. }