visual_shader_editor_plugin.cpp 242 KB

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