visual_shader_editor_plugin.cpp 262 KB

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