visual_shader_editor_plugin.cpp 254 KB

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