visual_shader_nodes.cpp 249 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199
  1. /**************************************************************************/
  2. /* visual_shader_nodes.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_nodes.h"
  31. #include "scene/resources/image_texture.h"
  32. ////////////// Vector Base
  33. VisualShaderNodeVectorBase::PortType VisualShaderNodeVectorBase::get_input_port_type(int p_port) const {
  34. switch (op_type) {
  35. case OP_TYPE_VECTOR_2D:
  36. return PORT_TYPE_VECTOR_2D;
  37. case OP_TYPE_VECTOR_3D:
  38. return PORT_TYPE_VECTOR_3D;
  39. case OP_TYPE_VECTOR_4D:
  40. return PORT_TYPE_VECTOR_4D;
  41. default:
  42. break;
  43. }
  44. return PORT_TYPE_SCALAR;
  45. }
  46. VisualShaderNodeVectorBase::PortType VisualShaderNodeVectorBase::get_output_port_type(int p_port) const {
  47. switch (op_type) {
  48. case OP_TYPE_VECTOR_2D:
  49. return PORT_TYPE_VECTOR_2D;
  50. case OP_TYPE_VECTOR_3D:
  51. return PORT_TYPE_VECTOR_3D;
  52. case OP_TYPE_VECTOR_4D:
  53. return PORT_TYPE_VECTOR_4D;
  54. default:
  55. break;
  56. }
  57. return PORT_TYPE_SCALAR;
  58. }
  59. void VisualShaderNodeVectorBase::set_op_type(OpType p_op_type) {
  60. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  61. if (op_type == p_op_type) {
  62. return;
  63. }
  64. op_type = p_op_type;
  65. emit_changed();
  66. }
  67. VisualShaderNodeVectorBase::OpType VisualShaderNodeVectorBase::get_op_type() const {
  68. return op_type;
  69. }
  70. void VisualShaderNodeVectorBase::_bind_methods() {
  71. ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeVectorBase::set_op_type);
  72. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeVectorBase::get_op_type);
  73. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Vector2,Vector3,Vector4"), "set_op_type", "get_op_type");
  74. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  75. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  76. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  77. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  78. }
  79. Vector<StringName> VisualShaderNodeVectorBase::get_editable_properties() const {
  80. Vector<StringName> props;
  81. props.push_back("op_type");
  82. return props;
  83. }
  84. VisualShaderNodeVectorBase::VisualShaderNodeVectorBase() {
  85. }
  86. ////////////// Constants Base
  87. VisualShaderNodeConstant::VisualShaderNodeConstant() {
  88. }
  89. ////////////// Scalar(Float)
  90. String VisualShaderNodeFloatConstant::get_caption() const {
  91. return "FloatConstant";
  92. }
  93. int VisualShaderNodeFloatConstant::get_input_port_count() const {
  94. return 0;
  95. }
  96. VisualShaderNodeFloatConstant::PortType VisualShaderNodeFloatConstant::get_input_port_type(int p_port) const {
  97. return PORT_TYPE_SCALAR;
  98. }
  99. String VisualShaderNodeFloatConstant::get_input_port_name(int p_port) const {
  100. return String();
  101. }
  102. int VisualShaderNodeFloatConstant::get_output_port_count() const {
  103. return 1;
  104. }
  105. VisualShaderNodeFloatConstant::PortType VisualShaderNodeFloatConstant::get_output_port_type(int p_port) const {
  106. return PORT_TYPE_SCALAR;
  107. }
  108. String VisualShaderNodeFloatConstant::get_output_port_name(int p_port) const {
  109. return ""; //no output port means the editor will be used as port
  110. }
  111. String VisualShaderNodeFloatConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  112. return " " + p_output_vars[0] + " = " + vformat("%.6f", constant) + ";\n";
  113. }
  114. void VisualShaderNodeFloatConstant::set_constant(float p_constant) {
  115. if (Math::is_equal_approx(constant, p_constant)) {
  116. return;
  117. }
  118. constant = p_constant;
  119. emit_changed();
  120. }
  121. float VisualShaderNodeFloatConstant::get_constant() const {
  122. return constant;
  123. }
  124. Vector<StringName> VisualShaderNodeFloatConstant::get_editable_properties() const {
  125. Vector<StringName> props;
  126. props.push_back("constant");
  127. return props;
  128. }
  129. void VisualShaderNodeFloatConstant::_bind_methods() {
  130. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeFloatConstant::set_constant);
  131. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeFloatConstant::get_constant);
  132. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "constant"), "set_constant", "get_constant");
  133. }
  134. VisualShaderNodeFloatConstant::VisualShaderNodeFloatConstant() {
  135. }
  136. ////////////// Scalar(Int)
  137. String VisualShaderNodeIntConstant::get_caption() const {
  138. return "IntConstant";
  139. }
  140. int VisualShaderNodeIntConstant::get_input_port_count() const {
  141. return 0;
  142. }
  143. VisualShaderNodeIntConstant::PortType VisualShaderNodeIntConstant::get_input_port_type(int p_port) const {
  144. return PORT_TYPE_SCALAR_INT;
  145. }
  146. String VisualShaderNodeIntConstant::get_input_port_name(int p_port) const {
  147. return String();
  148. }
  149. int VisualShaderNodeIntConstant::get_output_port_count() const {
  150. return 1;
  151. }
  152. VisualShaderNodeIntConstant::PortType VisualShaderNodeIntConstant::get_output_port_type(int p_port) const {
  153. return PORT_TYPE_SCALAR_INT;
  154. }
  155. String VisualShaderNodeIntConstant::get_output_port_name(int p_port) const {
  156. return ""; //no output port means the editor will be used as port
  157. }
  158. String VisualShaderNodeIntConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  159. return " " + p_output_vars[0] + " = " + itos(constant) + ";\n";
  160. }
  161. void VisualShaderNodeIntConstant::set_constant(int p_constant) {
  162. if (constant == p_constant) {
  163. return;
  164. }
  165. constant = p_constant;
  166. emit_changed();
  167. }
  168. int VisualShaderNodeIntConstant::get_constant() const {
  169. return constant;
  170. }
  171. Vector<StringName> VisualShaderNodeIntConstant::get_editable_properties() const {
  172. Vector<StringName> props;
  173. props.push_back("constant");
  174. return props;
  175. }
  176. void VisualShaderNodeIntConstant::_bind_methods() {
  177. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeIntConstant::set_constant);
  178. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeIntConstant::get_constant);
  179. ADD_PROPERTY(PropertyInfo(Variant::INT, "constant"), "set_constant", "get_constant");
  180. }
  181. VisualShaderNodeIntConstant::VisualShaderNodeIntConstant() {
  182. }
  183. ////////////// Scalar(UInt)
  184. String VisualShaderNodeUIntConstant::get_caption() const {
  185. return "UIntConstant";
  186. }
  187. int VisualShaderNodeUIntConstant::get_input_port_count() const {
  188. return 0;
  189. }
  190. VisualShaderNodeUIntConstant::PortType VisualShaderNodeUIntConstant::get_input_port_type(int p_port) const {
  191. return PORT_TYPE_SCALAR_UINT;
  192. }
  193. String VisualShaderNodeUIntConstant::get_input_port_name(int p_port) const {
  194. return String();
  195. }
  196. int VisualShaderNodeUIntConstant::get_output_port_count() const {
  197. return 1;
  198. }
  199. VisualShaderNodeUIntConstant::PortType VisualShaderNodeUIntConstant::get_output_port_type(int p_port) const {
  200. return PORT_TYPE_SCALAR_UINT;
  201. }
  202. String VisualShaderNodeUIntConstant::get_output_port_name(int p_port) const {
  203. return ""; // No output port means the editor will be used as port.
  204. }
  205. String VisualShaderNodeUIntConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  206. return " " + p_output_vars[0] + " = " + itos(constant) + "u;\n";
  207. }
  208. void VisualShaderNodeUIntConstant::set_constant(int p_constant) {
  209. if (constant == p_constant) {
  210. return;
  211. }
  212. constant = p_constant;
  213. emit_changed();
  214. }
  215. int VisualShaderNodeUIntConstant::get_constant() const {
  216. return constant;
  217. }
  218. Vector<StringName> VisualShaderNodeUIntConstant::get_editable_properties() const {
  219. Vector<StringName> props;
  220. props.push_back("constant");
  221. return props;
  222. }
  223. void VisualShaderNodeUIntConstant::_bind_methods() {
  224. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeUIntConstant::set_constant);
  225. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeUIntConstant::get_constant);
  226. ADD_PROPERTY(PropertyInfo(Variant::INT, "constant"), "set_constant", "get_constant");
  227. }
  228. VisualShaderNodeUIntConstant::VisualShaderNodeUIntConstant() {
  229. }
  230. ////////////// Boolean
  231. String VisualShaderNodeBooleanConstant::get_caption() const {
  232. return "BooleanConstant";
  233. }
  234. int VisualShaderNodeBooleanConstant::get_input_port_count() const {
  235. return 0;
  236. }
  237. VisualShaderNodeBooleanConstant::PortType VisualShaderNodeBooleanConstant::get_input_port_type(int p_port) const {
  238. return PORT_TYPE_BOOLEAN;
  239. }
  240. String VisualShaderNodeBooleanConstant::get_input_port_name(int p_port) const {
  241. return String();
  242. }
  243. int VisualShaderNodeBooleanConstant::get_output_port_count() const {
  244. return 1;
  245. }
  246. VisualShaderNodeBooleanConstant::PortType VisualShaderNodeBooleanConstant::get_output_port_type(int p_port) const {
  247. return PORT_TYPE_BOOLEAN;
  248. }
  249. String VisualShaderNodeBooleanConstant::get_output_port_name(int p_port) const {
  250. return ""; //no output port means the editor will be used as port
  251. }
  252. String VisualShaderNodeBooleanConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  253. return " " + p_output_vars[0] + " = " + (constant ? "true" : "false") + ";\n";
  254. }
  255. void VisualShaderNodeBooleanConstant::set_constant(bool p_constant) {
  256. if (constant == p_constant) {
  257. return;
  258. }
  259. constant = p_constant;
  260. emit_changed();
  261. }
  262. bool VisualShaderNodeBooleanConstant::get_constant() const {
  263. return constant;
  264. }
  265. Vector<StringName> VisualShaderNodeBooleanConstant::get_editable_properties() const {
  266. Vector<StringName> props;
  267. props.push_back("constant");
  268. return props;
  269. }
  270. void VisualShaderNodeBooleanConstant::_bind_methods() {
  271. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeBooleanConstant::set_constant);
  272. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeBooleanConstant::get_constant);
  273. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "constant"), "set_constant", "get_constant");
  274. }
  275. VisualShaderNodeBooleanConstant::VisualShaderNodeBooleanConstant() {
  276. }
  277. ////////////// Color
  278. String VisualShaderNodeColorConstant::get_caption() const {
  279. return "ColorConstant";
  280. }
  281. int VisualShaderNodeColorConstant::get_input_port_count() const {
  282. return 0;
  283. }
  284. VisualShaderNodeColorConstant::PortType VisualShaderNodeColorConstant::get_input_port_type(int p_port) const {
  285. return PORT_TYPE_VECTOR_4D;
  286. }
  287. String VisualShaderNodeColorConstant::get_input_port_name(int p_port) const {
  288. return String();
  289. }
  290. int VisualShaderNodeColorConstant::get_output_port_count() const {
  291. return 1;
  292. }
  293. VisualShaderNodeColorConstant::PortType VisualShaderNodeColorConstant::get_output_port_type(int p_port) const {
  294. return p_port == 0 ? PORT_TYPE_VECTOR_4D : PORT_TYPE_SCALAR;
  295. }
  296. String VisualShaderNodeColorConstant::get_output_port_name(int p_port) const {
  297. return "";
  298. }
  299. bool VisualShaderNodeColorConstant::is_output_port_expandable(int p_port) const {
  300. if (p_port == 0) {
  301. return true;
  302. }
  303. return false;
  304. }
  305. String VisualShaderNodeColorConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  306. return " " + p_output_vars[0] + " = " + vformat("vec4(%.6f, %.6f, %.6f, %.6f)", constant.r, constant.g, constant.b, constant.a) + ";\n";
  307. }
  308. void VisualShaderNodeColorConstant::set_constant(const Color &p_constant) {
  309. if (constant.is_equal_approx(p_constant)) {
  310. return;
  311. }
  312. constant = p_constant;
  313. emit_changed();
  314. }
  315. Color VisualShaderNodeColorConstant::get_constant() const {
  316. return constant;
  317. }
  318. Vector<StringName> VisualShaderNodeColorConstant::get_editable_properties() const {
  319. Vector<StringName> props;
  320. props.push_back("constant");
  321. return props;
  322. }
  323. void VisualShaderNodeColorConstant::_bind_methods() {
  324. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeColorConstant::set_constant);
  325. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeColorConstant::get_constant);
  326. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "constant"), "set_constant", "get_constant");
  327. }
  328. VisualShaderNodeColorConstant::VisualShaderNodeColorConstant() {
  329. }
  330. ////////////// Vector2
  331. String VisualShaderNodeVec2Constant::get_caption() const {
  332. return "Vector2Constant";
  333. }
  334. int VisualShaderNodeVec2Constant::get_input_port_count() const {
  335. return 0;
  336. }
  337. VisualShaderNodeVec2Constant::PortType VisualShaderNodeVec2Constant::get_input_port_type(int p_port) const {
  338. return PORT_TYPE_VECTOR_2D;
  339. }
  340. String VisualShaderNodeVec2Constant::get_input_port_name(int p_port) const {
  341. return String();
  342. }
  343. int VisualShaderNodeVec2Constant::get_output_port_count() const {
  344. return 1;
  345. }
  346. VisualShaderNodeVec2Constant::PortType VisualShaderNodeVec2Constant::get_output_port_type(int p_port) const {
  347. return PORT_TYPE_VECTOR_2D;
  348. }
  349. String VisualShaderNodeVec2Constant::get_output_port_name(int p_port) const {
  350. return ""; //no output port means the editor will be used as port
  351. }
  352. String VisualShaderNodeVec2Constant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  353. return " " + p_output_vars[0] + " = " + vformat("vec2(%.6f, %.6f)", constant.x, constant.y) + ";\n";
  354. }
  355. void VisualShaderNodeVec2Constant::set_constant(const Vector2 &p_constant) {
  356. if (constant.is_equal_approx(p_constant)) {
  357. return;
  358. }
  359. constant = p_constant;
  360. emit_changed();
  361. }
  362. Vector2 VisualShaderNodeVec2Constant::get_constant() const {
  363. return constant;
  364. }
  365. Vector<StringName> VisualShaderNodeVec2Constant::get_editable_properties() const {
  366. Vector<StringName> props;
  367. props.push_back("constant");
  368. return props;
  369. }
  370. void VisualShaderNodeVec2Constant::_bind_methods() {
  371. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeVec2Constant::set_constant);
  372. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeVec2Constant::get_constant);
  373. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "constant"), "set_constant", "get_constant");
  374. }
  375. VisualShaderNodeVec2Constant::VisualShaderNodeVec2Constant() {
  376. }
  377. ////////////// Vector3
  378. String VisualShaderNodeVec3Constant::get_caption() const {
  379. return "Vector3Constant";
  380. }
  381. int VisualShaderNodeVec3Constant::get_input_port_count() const {
  382. return 0;
  383. }
  384. VisualShaderNodeVec3Constant::PortType VisualShaderNodeVec3Constant::get_input_port_type(int p_port) const {
  385. return PORT_TYPE_VECTOR_3D;
  386. }
  387. String VisualShaderNodeVec3Constant::get_input_port_name(int p_port) const {
  388. return String();
  389. }
  390. int VisualShaderNodeVec3Constant::get_output_port_count() const {
  391. return 1;
  392. }
  393. VisualShaderNodeVec3Constant::PortType VisualShaderNodeVec3Constant::get_output_port_type(int p_port) const {
  394. return PORT_TYPE_VECTOR_3D;
  395. }
  396. String VisualShaderNodeVec3Constant::get_output_port_name(int p_port) const {
  397. return ""; //no output port means the editor will be used as port
  398. }
  399. String VisualShaderNodeVec3Constant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  400. return " " + p_output_vars[0] + " = " + vformat("vec3(%.6f, %.6f, %.6f)", constant.x, constant.y, constant.z) + ";\n";
  401. }
  402. void VisualShaderNodeVec3Constant::set_constant(const Vector3 &p_constant) {
  403. if (constant.is_equal_approx(p_constant)) {
  404. return;
  405. }
  406. constant = p_constant;
  407. emit_changed();
  408. }
  409. Vector3 VisualShaderNodeVec3Constant::get_constant() const {
  410. return constant;
  411. }
  412. Vector<StringName> VisualShaderNodeVec3Constant::get_editable_properties() const {
  413. Vector<StringName> props;
  414. props.push_back("constant");
  415. return props;
  416. }
  417. void VisualShaderNodeVec3Constant::_bind_methods() {
  418. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeVec3Constant::set_constant);
  419. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeVec3Constant::get_constant);
  420. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant"), "set_constant", "get_constant");
  421. }
  422. VisualShaderNodeVec3Constant::VisualShaderNodeVec3Constant() {
  423. }
  424. ////////////// Vector4
  425. String VisualShaderNodeVec4Constant::get_caption() const {
  426. return "Vector4Constant";
  427. }
  428. int VisualShaderNodeVec4Constant::get_input_port_count() const {
  429. return 0;
  430. }
  431. VisualShaderNodeVec4Constant::PortType VisualShaderNodeVec4Constant::get_input_port_type(int p_port) const {
  432. return PORT_TYPE_VECTOR_4D;
  433. }
  434. String VisualShaderNodeVec4Constant::get_input_port_name(int p_port) const {
  435. return String();
  436. }
  437. int VisualShaderNodeVec4Constant::get_output_port_count() const {
  438. return 1;
  439. }
  440. VisualShaderNodeVec4Constant::PortType VisualShaderNodeVec4Constant::get_output_port_type(int p_port) const {
  441. return PORT_TYPE_VECTOR_4D;
  442. }
  443. String VisualShaderNodeVec4Constant::get_output_port_name(int p_port) const {
  444. return ""; // No output port means the editor will be used as port.
  445. }
  446. String VisualShaderNodeVec4Constant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  447. return " " + p_output_vars[0] + " = " + vformat("vec4(%.6f, %.6f, %.6f, %.6f)", constant.x, constant.y, constant.z, constant.w) + ";\n";
  448. }
  449. void VisualShaderNodeVec4Constant::set_constant(const Quaternion &p_constant) {
  450. if (constant.is_equal_approx(p_constant)) {
  451. return;
  452. }
  453. constant = p_constant;
  454. emit_changed();
  455. }
  456. Quaternion VisualShaderNodeVec4Constant::get_constant() const {
  457. return constant;
  458. }
  459. Vector<StringName> VisualShaderNodeVec4Constant::get_editable_properties() const {
  460. Vector<StringName> props;
  461. props.push_back("constant");
  462. return props;
  463. }
  464. void VisualShaderNodeVec4Constant::_bind_methods() {
  465. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeVec4Constant::set_constant);
  466. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeVec4Constant::get_constant);
  467. ADD_PROPERTY(PropertyInfo(Variant::QUATERNION, "constant"), "set_constant", "get_constant");
  468. }
  469. VisualShaderNodeVec4Constant::VisualShaderNodeVec4Constant() {
  470. }
  471. ////////////// Transform3D
  472. String VisualShaderNodeTransformConstant::get_caption() const {
  473. return "TransformConstant";
  474. }
  475. int VisualShaderNodeTransformConstant::get_input_port_count() const {
  476. return 0;
  477. }
  478. VisualShaderNodeTransformConstant::PortType VisualShaderNodeTransformConstant::get_input_port_type(int p_port) const {
  479. return PORT_TYPE_VECTOR_3D;
  480. }
  481. String VisualShaderNodeTransformConstant::get_input_port_name(int p_port) const {
  482. return String();
  483. }
  484. int VisualShaderNodeTransformConstant::get_output_port_count() const {
  485. return 1;
  486. }
  487. VisualShaderNodeTransformConstant::PortType VisualShaderNodeTransformConstant::get_output_port_type(int p_port) const {
  488. return PORT_TYPE_TRANSFORM;
  489. }
  490. String VisualShaderNodeTransformConstant::get_output_port_name(int p_port) const {
  491. return ""; //no output port means the editor will be used as port
  492. }
  493. String VisualShaderNodeTransformConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  494. Transform3D t = constant;
  495. t.basis.transpose();
  496. String code = " " + p_output_vars[0] + " = mat4(";
  497. code += vformat("vec4(%.6f, %.6f, %.6f, 0.0), ", t.basis[0].x, t.basis[0].y, t.basis[0].z);
  498. code += vformat("vec4(%.6f, %.6f, %.6f, 0.0), ", t.basis[1].x, t.basis[1].y, t.basis[1].z);
  499. code += vformat("vec4(%.6f, %.6f, %.6f, 0.0), ", t.basis[2].x, t.basis[2].y, t.basis[2].z);
  500. code += vformat("vec4(%.6f, %.6f, %.6f, 1.0));\n", t.origin.x, t.origin.y, t.origin.z);
  501. return code;
  502. }
  503. void VisualShaderNodeTransformConstant::set_constant(const Transform3D &p_constant) {
  504. if (constant.is_equal_approx(p_constant)) {
  505. return;
  506. }
  507. constant = p_constant;
  508. emit_changed();
  509. }
  510. Transform3D VisualShaderNodeTransformConstant::get_constant() const {
  511. return constant;
  512. }
  513. Vector<StringName> VisualShaderNodeTransformConstant::get_editable_properties() const {
  514. Vector<StringName> props;
  515. props.push_back("constant");
  516. return props;
  517. }
  518. void VisualShaderNodeTransformConstant::_bind_methods() {
  519. ClassDB::bind_method(D_METHOD("set_constant", "constant"), &VisualShaderNodeTransformConstant::set_constant);
  520. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeTransformConstant::get_constant);
  521. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "constant"), "set_constant", "get_constant");
  522. }
  523. VisualShaderNodeTransformConstant::VisualShaderNodeTransformConstant() {
  524. }
  525. ////////////// Texture
  526. String VisualShaderNodeTexture::get_caption() const {
  527. return "Texture2D";
  528. }
  529. int VisualShaderNodeTexture::get_input_port_count() const {
  530. return 3;
  531. }
  532. VisualShaderNodeTexture::PortType VisualShaderNodeTexture::get_input_port_type(int p_port) const {
  533. switch (p_port) {
  534. case 0:
  535. return PORT_TYPE_VECTOR_2D;
  536. case 1:
  537. return PORT_TYPE_SCALAR;
  538. case 2:
  539. return PORT_TYPE_SAMPLER;
  540. default:
  541. return PORT_TYPE_SCALAR;
  542. }
  543. }
  544. String VisualShaderNodeTexture::get_input_port_name(int p_port) const {
  545. switch (p_port) {
  546. case 0:
  547. return "uv";
  548. case 1:
  549. return "lod";
  550. case 2:
  551. return "sampler2D";
  552. default:
  553. return "";
  554. }
  555. }
  556. int VisualShaderNodeTexture::get_output_port_count() const {
  557. return 1;
  558. }
  559. VisualShaderNodeTexture::PortType VisualShaderNodeTexture::get_output_port_type(int p_port) const {
  560. switch (p_port) {
  561. case 0:
  562. return PORT_TYPE_VECTOR_4D;
  563. default:
  564. return PORT_TYPE_SCALAR;
  565. }
  566. }
  567. String VisualShaderNodeTexture::get_output_port_name(int p_port) const {
  568. switch (p_port) {
  569. case 0:
  570. return "color";
  571. default:
  572. return "";
  573. }
  574. }
  575. bool VisualShaderNodeTexture::is_output_port_expandable(int p_port) const {
  576. if (p_port == 0) {
  577. return true;
  578. }
  579. return false;
  580. }
  581. bool VisualShaderNodeTexture::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  582. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  583. if (p_port == 0) {
  584. return true;
  585. }
  586. }
  587. return false;
  588. }
  589. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeTexture::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  590. VisualShader::DefaultTextureParam dtp;
  591. dtp.name = make_unique_id(p_type, p_id, "tex");
  592. dtp.params.push_back(texture);
  593. Vector<VisualShader::DefaultTextureParam> ret;
  594. ret.push_back(dtp);
  595. return ret;
  596. }
  597. String VisualShaderNodeTexture::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  598. String code;
  599. switch (source) {
  600. case SOURCE_TEXTURE: {
  601. code += "uniform sampler2D " + make_unique_id(p_type, p_id, "tex");
  602. switch (texture_type) {
  603. case TYPE_DATA: {
  604. } break;
  605. case TYPE_COLOR: {
  606. code += " : source_color";
  607. } break;
  608. case TYPE_NORMAL_MAP: {
  609. code += " : hint_normal";
  610. } break;
  611. default: {
  612. } break;
  613. }
  614. code += ";\n";
  615. } break;
  616. case SOURCE_SCREEN: {
  617. if ((p_mode == Shader::MODE_SPATIAL || p_mode == Shader::MODE_CANVAS_ITEM) && p_type == VisualShader::TYPE_FRAGMENT) {
  618. code += "uniform sampler2D " + make_unique_id(p_type, p_id, "screen_tex") + " : hint_screen_texture;\n";
  619. }
  620. } break;
  621. case SOURCE_DEPTH:
  622. case SOURCE_3D_NORMAL:
  623. case SOURCE_ROUGHNESS: {
  624. if (p_mode == Shader::MODE_SPATIAL && p_type == VisualShader::TYPE_FRAGMENT) {
  625. String sampler_name = "";
  626. String hint = " : ";
  627. if (source == SOURCE_DEPTH) {
  628. sampler_name = "depth_tex";
  629. hint += "hint_depth_texture;\n";
  630. } else {
  631. sampler_name = source == SOURCE_ROUGHNESS ? "roughness_tex" : "normal_roughness_tex";
  632. hint += "hint_normal_roughness_texture;\n";
  633. }
  634. code += "uniform sampler2D " + make_unique_id(p_type, p_id, sampler_name) + hint;
  635. }
  636. } break;
  637. default: {
  638. } break;
  639. }
  640. return code;
  641. }
  642. String VisualShaderNodeTexture::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  643. String default_uv;
  644. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  645. if (source == SOURCE_SCREEN) {
  646. default_uv = "SCREEN_UV";
  647. } else {
  648. default_uv = "UV";
  649. }
  650. } else {
  651. default_uv = "vec2(0.0)";
  652. }
  653. String code;
  654. String uv = p_input_vars[0].is_empty() ? default_uv : p_input_vars[0];
  655. switch (source) {
  656. case SOURCE_PORT:
  657. case SOURCE_TEXTURE: {
  658. String id;
  659. if (source == SOURCE_PORT) {
  660. id = p_input_vars[2];
  661. if (id.is_empty()) {
  662. break;
  663. }
  664. } else { // SOURCE_TEXTURE
  665. id = make_unique_id(p_type, p_id, "tex");
  666. }
  667. if (p_input_vars[1].is_empty()) {
  668. code += " " + p_output_vars[0] + " = texture(" + id + ", " + uv + ");\n";
  669. } else {
  670. code += " " + p_output_vars[0] + " = textureLod(" + id + ", " + uv + ", " + p_input_vars[1] + ");\n";
  671. }
  672. return code;
  673. } break;
  674. case SOURCE_SCREEN: {
  675. if ((p_mode == Shader::MODE_SPATIAL || p_mode == Shader::MODE_CANVAS_ITEM) && p_type == VisualShader::TYPE_FRAGMENT) {
  676. String id = make_unique_id(p_type, p_id, "screen_tex");
  677. if (p_input_vars[1].is_empty()) {
  678. code += " " + p_output_vars[0] + " = texture(" + id + ", " + uv + ");\n";
  679. } else {
  680. code += " " + p_output_vars[0] + " = textureLod(" + id + ", " + uv + ", " + p_input_vars[1] + ");\n";
  681. }
  682. return code;
  683. }
  684. } break;
  685. case SOURCE_2D_NORMAL:
  686. case SOURCE_2D_TEXTURE: {
  687. if (p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  688. String id = source == SOURCE_2D_TEXTURE ? "TEXTURE" : "NORMAL_TEXTURE";
  689. if (p_input_vars[1].is_empty()) {
  690. code += " " + p_output_vars[0] + " = texture(" + id + ", " + uv + ");\n";
  691. } else {
  692. code += " " + p_output_vars[0] + " = textureLod(" + id + ", " + uv + ", " + p_input_vars[1] + ");\n";
  693. }
  694. return code;
  695. }
  696. } break;
  697. case SOURCE_3D_NORMAL:
  698. case SOURCE_ROUGHNESS:
  699. case SOURCE_DEPTH: {
  700. if (!p_for_preview && p_mode == Shader::MODE_SPATIAL && p_type == VisualShader::TYPE_FRAGMENT) {
  701. String var_name = "";
  702. String sampler_name = "";
  703. switch (source) {
  704. case SOURCE_DEPTH: {
  705. var_name = "_depth";
  706. sampler_name = "depth_tex";
  707. } break;
  708. case SOURCE_ROUGHNESS: {
  709. var_name = "_roughness";
  710. sampler_name = "roughness_tex";
  711. } break;
  712. case SOURCE_3D_NORMAL: {
  713. var_name = "_normal";
  714. sampler_name = "normal_roughness_tex";
  715. } break;
  716. default: {
  717. } break;
  718. }
  719. String id = make_unique_id(p_type, p_id, sampler_name);
  720. String type = source == SOURCE_3D_NORMAL ? "vec3" : "float";
  721. String components = source == SOURCE_3D_NORMAL ? "rgb" : "r";
  722. code += " {\n";
  723. if (p_input_vars[1].is_empty()) {
  724. code += " " + type + " " + var_name + " = texture(" + id + ", " + uv + ")." + components + ";\n";
  725. } else {
  726. code += " " + type + " " + var_name + " = textureLod(" + id + ", " + uv + ", " + p_input_vars[1] + ")." + components + ";\n";
  727. }
  728. if (source == SOURCE_3D_NORMAL) {
  729. code += " " + p_output_vars[0] + " = vec4(" + var_name + ", 1.0);\n";
  730. } else {
  731. code += " " + p_output_vars[0] + " = vec4(" + var_name + ", " + var_name + ", " + var_name + ", 1.0);\n";
  732. }
  733. code += " }\n";
  734. return code;
  735. }
  736. } break;
  737. default: {
  738. } break;
  739. }
  740. code += " " + p_output_vars[0] + " = vec4(0.0);\n";
  741. return code;
  742. }
  743. void VisualShaderNodeTexture::set_source(Source p_source) {
  744. ERR_FAIL_INDEX(int(p_source), int(SOURCE_MAX));
  745. if (source == p_source) {
  746. return;
  747. }
  748. switch (p_source) {
  749. case SOURCE_TEXTURE:
  750. simple_decl = true;
  751. break;
  752. case SOURCE_SCREEN:
  753. simple_decl = false;
  754. break;
  755. case SOURCE_2D_TEXTURE:
  756. simple_decl = false;
  757. break;
  758. case SOURCE_2D_NORMAL:
  759. simple_decl = false;
  760. break;
  761. case SOURCE_DEPTH:
  762. simple_decl = false;
  763. break;
  764. case SOURCE_PORT:
  765. simple_decl = false;
  766. break;
  767. case SOURCE_3D_NORMAL:
  768. simple_decl = false;
  769. break;
  770. case SOURCE_ROUGHNESS:
  771. simple_decl = false;
  772. break;
  773. default:
  774. break;
  775. }
  776. source = p_source;
  777. emit_changed();
  778. }
  779. VisualShaderNodeTexture::Source VisualShaderNodeTexture::get_source() const {
  780. return source;
  781. }
  782. void VisualShaderNodeTexture::set_texture(Ref<Texture2D> p_texture) {
  783. texture = p_texture;
  784. emit_changed();
  785. }
  786. Ref<Texture2D> VisualShaderNodeTexture::get_texture() const {
  787. return texture;
  788. }
  789. void VisualShaderNodeTexture::set_texture_type(TextureType p_texture_type) {
  790. ERR_FAIL_INDEX(int(p_texture_type), int(TYPE_MAX));
  791. if (texture_type == p_texture_type) {
  792. return;
  793. }
  794. texture_type = p_texture_type;
  795. emit_changed();
  796. }
  797. VisualShaderNodeTexture::TextureType VisualShaderNodeTexture::get_texture_type() const {
  798. return texture_type;
  799. }
  800. Vector<StringName> VisualShaderNodeTexture::get_editable_properties() const {
  801. Vector<StringName> props;
  802. props.push_back("source");
  803. if (source == SOURCE_TEXTURE) {
  804. props.push_back("texture");
  805. props.push_back("texture_type");
  806. }
  807. return props;
  808. }
  809. String VisualShaderNodeTexture::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  810. if (is_input_port_connected(2) && source != SOURCE_PORT) {
  811. return RTR("The sampler port is connected but not used. Consider changing the source to 'SamplerPort'.");
  812. }
  813. switch (source) {
  814. case SOURCE_TEXTURE:
  815. case SOURCE_PORT: {
  816. return String(); // All good.
  817. } break;
  818. case SOURCE_SCREEN: {
  819. if ((p_mode == Shader::MODE_SPATIAL || p_mode == Shader::MODE_CANVAS_ITEM) && p_type == VisualShader::TYPE_FRAGMENT) {
  820. return String(); // All good.
  821. }
  822. } break;
  823. case SOURCE_2D_NORMAL:
  824. case SOURCE_2D_TEXTURE: {
  825. if (p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  826. return String(); // All good.
  827. }
  828. } break;
  829. case SOURCE_3D_NORMAL:
  830. case SOURCE_ROUGHNESS:
  831. case SOURCE_DEPTH: {
  832. if (p_mode == Shader::MODE_SPATIAL && p_type == VisualShader::TYPE_FRAGMENT) {
  833. if (get_output_port_for_preview() == 0) { // Not supported in preview(canvas_item) shader.
  834. return RTR("Invalid source for preview.");
  835. }
  836. return String(); // All good.
  837. }
  838. } break;
  839. default: {
  840. } break;
  841. }
  842. return RTR("Invalid source for shader.");
  843. }
  844. void VisualShaderNodeTexture::_bind_methods() {
  845. ClassDB::bind_method(D_METHOD("set_source", "value"), &VisualShaderNodeTexture::set_source);
  846. ClassDB::bind_method(D_METHOD("get_source"), &VisualShaderNodeTexture::get_source);
  847. ClassDB::bind_method(D_METHOD("set_texture", "value"), &VisualShaderNodeTexture::set_texture);
  848. ClassDB::bind_method(D_METHOD("get_texture"), &VisualShaderNodeTexture::get_texture);
  849. ClassDB::bind_method(D_METHOD("set_texture_type", "value"), &VisualShaderNodeTexture::set_texture_type);
  850. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeTexture::get_texture_type);
  851. ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,Screen,Texture2D,NormalMap2D,Depth,SamplerPort,Normal3D,Roughness"), "set_source", "get_source");
  852. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  853. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normal Map"), "set_texture_type", "get_texture_type");
  854. BIND_ENUM_CONSTANT(SOURCE_TEXTURE);
  855. BIND_ENUM_CONSTANT(SOURCE_SCREEN);
  856. BIND_ENUM_CONSTANT(SOURCE_2D_TEXTURE);
  857. BIND_ENUM_CONSTANT(SOURCE_2D_NORMAL);
  858. BIND_ENUM_CONSTANT(SOURCE_DEPTH);
  859. BIND_ENUM_CONSTANT(SOURCE_PORT);
  860. BIND_ENUM_CONSTANT(SOURCE_3D_NORMAL);
  861. BIND_ENUM_CONSTANT(SOURCE_ROUGHNESS);
  862. BIND_ENUM_CONSTANT(SOURCE_MAX);
  863. BIND_ENUM_CONSTANT(TYPE_DATA);
  864. BIND_ENUM_CONSTANT(TYPE_COLOR);
  865. BIND_ENUM_CONSTANT(TYPE_NORMAL_MAP);
  866. BIND_ENUM_CONSTANT(TYPE_MAX);
  867. }
  868. VisualShaderNodeTexture::VisualShaderNodeTexture() {
  869. }
  870. ////////////// CurveTexture
  871. String VisualShaderNodeCurveTexture::get_caption() const {
  872. return "CurveTexture";
  873. }
  874. int VisualShaderNodeCurveTexture::get_input_port_count() const {
  875. return 1;
  876. }
  877. VisualShaderNodeCurveTexture::PortType VisualShaderNodeCurveTexture::get_input_port_type(int p_port) const {
  878. return PORT_TYPE_SCALAR;
  879. }
  880. String VisualShaderNodeCurveTexture::get_input_port_name(int p_port) const {
  881. return String();
  882. }
  883. int VisualShaderNodeCurveTexture::get_output_port_count() const {
  884. return 1;
  885. }
  886. VisualShaderNodeCurveTexture::PortType VisualShaderNodeCurveTexture::get_output_port_type(int p_port) const {
  887. return PORT_TYPE_SCALAR;
  888. }
  889. String VisualShaderNodeCurveTexture::get_output_port_name(int p_port) const {
  890. return String();
  891. }
  892. void VisualShaderNodeCurveTexture::set_texture(Ref<CurveTexture> p_texture) {
  893. texture = p_texture;
  894. emit_changed();
  895. }
  896. Ref<CurveTexture> VisualShaderNodeCurveTexture::get_texture() const {
  897. return texture;
  898. }
  899. Vector<StringName> VisualShaderNodeCurveTexture::get_editable_properties() const {
  900. Vector<StringName> props;
  901. props.push_back("texture");
  902. return props;
  903. }
  904. String VisualShaderNodeCurveTexture::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  905. return "uniform sampler2D " + make_unique_id(p_type, p_id, "curve") + " : repeat_disable;\n";
  906. }
  907. String VisualShaderNodeCurveTexture::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  908. if (p_input_vars[0].is_empty()) {
  909. return " " + p_output_vars[0] + " = 0.0;\n";
  910. }
  911. String id = make_unique_id(p_type, p_id, "curve");
  912. String code;
  913. code += " " + p_output_vars[0] + " = texture(" + id + ", vec2(" + p_input_vars[0] + ")).r;\n";
  914. return code;
  915. }
  916. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeCurveTexture::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  917. VisualShader::DefaultTextureParam dtp;
  918. dtp.name = make_unique_id(p_type, p_id, "curve");
  919. dtp.params.push_back(texture);
  920. Vector<VisualShader::DefaultTextureParam> ret;
  921. ret.push_back(dtp);
  922. return ret;
  923. }
  924. void VisualShaderNodeCurveTexture::_bind_methods() {
  925. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &VisualShaderNodeCurveTexture::set_texture);
  926. ClassDB::bind_method(D_METHOD("get_texture"), &VisualShaderNodeCurveTexture::get_texture);
  927. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "CurveTexture"), "set_texture", "get_texture");
  928. }
  929. bool VisualShaderNodeCurveTexture::is_use_prop_slots() const {
  930. return true;
  931. }
  932. VisualShaderNodeCurveTexture::VisualShaderNodeCurveTexture() {
  933. set_input_port_default_value(0, 0.0);
  934. simple_decl = true;
  935. allow_v_resize = false;
  936. }
  937. ////////////// CurveXYZTexture
  938. String VisualShaderNodeCurveXYZTexture::get_caption() const {
  939. return "CurveXYZTexture";
  940. }
  941. int VisualShaderNodeCurveXYZTexture::get_input_port_count() const {
  942. return 1;
  943. }
  944. VisualShaderNodeCurveXYZTexture::PortType VisualShaderNodeCurveXYZTexture::get_input_port_type(int p_port) const {
  945. return PORT_TYPE_SCALAR;
  946. }
  947. String VisualShaderNodeCurveXYZTexture::get_input_port_name(int p_port) const {
  948. return String();
  949. }
  950. int VisualShaderNodeCurveXYZTexture::get_output_port_count() const {
  951. return 1;
  952. }
  953. VisualShaderNodeCurveXYZTexture::PortType VisualShaderNodeCurveXYZTexture::get_output_port_type(int p_port) const {
  954. return PORT_TYPE_VECTOR_3D;
  955. }
  956. String VisualShaderNodeCurveXYZTexture::get_output_port_name(int p_port) const {
  957. return String();
  958. }
  959. void VisualShaderNodeCurveXYZTexture::set_texture(Ref<CurveXYZTexture> p_texture) {
  960. texture = p_texture;
  961. emit_changed();
  962. }
  963. Ref<CurveXYZTexture> VisualShaderNodeCurveXYZTexture::get_texture() const {
  964. return texture;
  965. }
  966. Vector<StringName> VisualShaderNodeCurveXYZTexture::get_editable_properties() const {
  967. Vector<StringName> props;
  968. props.push_back("texture");
  969. return props;
  970. }
  971. String VisualShaderNodeCurveXYZTexture::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  972. return "uniform sampler2D " + make_unique_id(p_type, p_id, "curve3d") + ";\n";
  973. }
  974. String VisualShaderNodeCurveXYZTexture::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  975. if (p_input_vars[0].is_empty()) {
  976. return " " + p_output_vars[0] + " = vec3(0.0);\n";
  977. }
  978. String id = make_unique_id(p_type, p_id, "curve3d");
  979. String code;
  980. code += " " + p_output_vars[0] + " = texture(" + id + ", vec2(" + p_input_vars[0] + ")).rgb;\n";
  981. return code;
  982. }
  983. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeCurveXYZTexture::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  984. VisualShader::DefaultTextureParam dtp;
  985. dtp.name = make_unique_id(p_type, p_id, "curve3d");
  986. dtp.params.push_back(texture);
  987. Vector<VisualShader::DefaultTextureParam> ret;
  988. ret.push_back(dtp);
  989. return ret;
  990. }
  991. void VisualShaderNodeCurveXYZTexture::_bind_methods() {
  992. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &VisualShaderNodeCurveXYZTexture::set_texture);
  993. ClassDB::bind_method(D_METHOD("get_texture"), &VisualShaderNodeCurveXYZTexture::get_texture);
  994. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "CurveXYZTexture"), "set_texture", "get_texture");
  995. }
  996. bool VisualShaderNodeCurveXYZTexture::is_use_prop_slots() const {
  997. return true;
  998. }
  999. VisualShaderNodeCurveXYZTexture::VisualShaderNodeCurveXYZTexture() {
  1000. set_input_port_default_value(0, 0.0);
  1001. simple_decl = true;
  1002. allow_v_resize = false;
  1003. }
  1004. ////////////// Sample3D
  1005. int VisualShaderNodeSample3D::get_input_port_count() const {
  1006. return 3;
  1007. }
  1008. VisualShaderNodeSample3D::PortType VisualShaderNodeSample3D::get_input_port_type(int p_port) const {
  1009. switch (p_port) {
  1010. case 0:
  1011. return PORT_TYPE_VECTOR_3D;
  1012. case 1:
  1013. return PORT_TYPE_SCALAR;
  1014. case 2:
  1015. return PORT_TYPE_SAMPLER;
  1016. default:
  1017. return PORT_TYPE_SCALAR;
  1018. }
  1019. }
  1020. String VisualShaderNodeSample3D::get_input_port_name(int p_port) const {
  1021. switch (p_port) {
  1022. case 0:
  1023. return "uvw";
  1024. case 1:
  1025. return "lod";
  1026. default:
  1027. return "";
  1028. }
  1029. }
  1030. int VisualShaderNodeSample3D::get_output_port_count() const {
  1031. return 1;
  1032. }
  1033. VisualShaderNodeSample3D::PortType VisualShaderNodeSample3D::get_output_port_type(int p_port) const {
  1034. return p_port == 0 ? PORT_TYPE_VECTOR_4D : PORT_TYPE_SCALAR;
  1035. }
  1036. String VisualShaderNodeSample3D::get_output_port_name(int p_port) const {
  1037. return "color";
  1038. }
  1039. bool VisualShaderNodeSample3D::is_output_port_expandable(int p_port) const {
  1040. if (p_port == 0) {
  1041. return true;
  1042. }
  1043. return false;
  1044. }
  1045. bool VisualShaderNodeSample3D::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  1046. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  1047. if (p_port == 0) {
  1048. return true;
  1049. }
  1050. }
  1051. return false;
  1052. }
  1053. String VisualShaderNodeSample3D::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1054. String code;
  1055. String id;
  1056. if (source == SOURCE_TEXTURE) {
  1057. id = make_unique_id(p_type, p_id, "tex3d");
  1058. } else { // SOURCE_PORT
  1059. id = p_input_vars[2];
  1060. if (id.is_empty()) {
  1061. code += " " + p_output_vars[0] + " = vec4(0.0);\n";
  1062. return code;
  1063. }
  1064. }
  1065. String default_uv;
  1066. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  1067. default_uv = "vec3(UV, 0.0)";
  1068. } else {
  1069. default_uv = "vec3(0.0)";
  1070. }
  1071. String uv = p_input_vars[0].is_empty() ? default_uv : p_input_vars[0];
  1072. if (p_input_vars[1].is_empty()) {
  1073. code += " " + p_output_vars[0] + " = texture(" + id + ", " + uv + ");\n";
  1074. } else {
  1075. code += " " + p_output_vars[0] + " = textureLod(" + id + ", " + uv + ", " + p_input_vars[1] + ");\n";
  1076. }
  1077. return code;
  1078. }
  1079. void VisualShaderNodeSample3D::set_source(Source p_source) {
  1080. ERR_FAIL_INDEX(int(p_source), int(SOURCE_MAX));
  1081. if (source == p_source) {
  1082. return;
  1083. }
  1084. source = p_source;
  1085. emit_changed();
  1086. }
  1087. VisualShaderNodeSample3D::Source VisualShaderNodeSample3D::get_source() const {
  1088. return source;
  1089. }
  1090. void VisualShaderNodeSample3D::_bind_methods() {
  1091. ClassDB::bind_method(D_METHOD("set_source", "value"), &VisualShaderNodeSample3D::set_source);
  1092. ClassDB::bind_method(D_METHOD("get_source"), &VisualShaderNodeSample3D::get_source);
  1093. ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,SamplerPort"), "set_source", "get_source");
  1094. BIND_ENUM_CONSTANT(SOURCE_TEXTURE);
  1095. BIND_ENUM_CONSTANT(SOURCE_PORT);
  1096. BIND_ENUM_CONSTANT(SOURCE_MAX);
  1097. }
  1098. String VisualShaderNodeSample3D::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  1099. if (is_input_port_connected(2) && source != SOURCE_PORT) {
  1100. return RTR("The sampler port is connected but not used. Consider changing the source to 'SamplerPort'.");
  1101. }
  1102. return String();
  1103. }
  1104. VisualShaderNodeSample3D::VisualShaderNodeSample3D() {
  1105. simple_decl = false;
  1106. }
  1107. ////////////// Texture2DArray
  1108. String VisualShaderNodeTexture2DArray::get_caption() const {
  1109. return "Texture2DArray";
  1110. }
  1111. String VisualShaderNodeTexture2DArray::get_input_port_name(int p_port) const {
  1112. if (p_port == 2) {
  1113. return "sampler2DArray";
  1114. }
  1115. return VisualShaderNodeSample3D::get_input_port_name(p_port);
  1116. }
  1117. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeTexture2DArray::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  1118. VisualShader::DefaultTextureParam dtp;
  1119. dtp.name = make_unique_id(p_type, p_id, "tex3d");
  1120. dtp.params.push_back(texture_array);
  1121. Vector<VisualShader::DefaultTextureParam> ret;
  1122. ret.push_back(dtp);
  1123. return ret;
  1124. }
  1125. String VisualShaderNodeTexture2DArray::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1126. if (source == SOURCE_TEXTURE) {
  1127. return "uniform sampler2DArray " + make_unique_id(p_type, p_id, "tex3d") + ";\n";
  1128. }
  1129. return String();
  1130. }
  1131. void VisualShaderNodeTexture2DArray::set_texture_array(Ref<Texture2DArray> p_texture_array) {
  1132. texture_array = p_texture_array;
  1133. emit_changed();
  1134. }
  1135. Ref<Texture2DArray> VisualShaderNodeTexture2DArray::get_texture_array() const {
  1136. return texture_array;
  1137. }
  1138. Vector<StringName> VisualShaderNodeTexture2DArray::get_editable_properties() const {
  1139. Vector<StringName> props;
  1140. props.push_back("source");
  1141. if (source == SOURCE_TEXTURE) {
  1142. props.push_back("texture_array");
  1143. }
  1144. return props;
  1145. }
  1146. void VisualShaderNodeTexture2DArray::_bind_methods() {
  1147. ClassDB::bind_method(D_METHOD("set_texture_array", "value"), &VisualShaderNodeTexture2DArray::set_texture_array);
  1148. ClassDB::bind_method(D_METHOD("get_texture_array"), &VisualShaderNodeTexture2DArray::get_texture_array);
  1149. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture_array", PROPERTY_HINT_RESOURCE_TYPE, "Texture2DArray"), "set_texture_array", "get_texture_array");
  1150. }
  1151. VisualShaderNodeTexture2DArray::VisualShaderNodeTexture2DArray() {
  1152. }
  1153. ////////////// Texture3D
  1154. String VisualShaderNodeTexture3D::get_caption() const {
  1155. return "Texture3D";
  1156. }
  1157. String VisualShaderNodeTexture3D::get_input_port_name(int p_port) const {
  1158. if (p_port == 2) {
  1159. return "sampler3D";
  1160. }
  1161. return VisualShaderNodeSample3D::get_input_port_name(p_port);
  1162. }
  1163. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeTexture3D::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  1164. VisualShader::DefaultTextureParam dtp;
  1165. dtp.name = make_unique_id(p_type, p_id, "tex3d");
  1166. dtp.params.push_back(texture);
  1167. Vector<VisualShader::DefaultTextureParam> ret;
  1168. ret.push_back(dtp);
  1169. return ret;
  1170. }
  1171. String VisualShaderNodeTexture3D::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1172. if (source == SOURCE_TEXTURE) {
  1173. return "uniform sampler3D " + make_unique_id(p_type, p_id, "tex3d") + ";\n";
  1174. }
  1175. return String();
  1176. }
  1177. void VisualShaderNodeTexture3D::set_texture(Ref<Texture3D> p_texture) {
  1178. texture = p_texture;
  1179. emit_changed();
  1180. }
  1181. Ref<Texture3D> VisualShaderNodeTexture3D::get_texture() const {
  1182. return texture;
  1183. }
  1184. Vector<StringName> VisualShaderNodeTexture3D::get_editable_properties() const {
  1185. Vector<StringName> props;
  1186. props.push_back("source");
  1187. if (source == SOURCE_TEXTURE) {
  1188. props.push_back("texture");
  1189. }
  1190. return props;
  1191. }
  1192. void VisualShaderNodeTexture3D::_bind_methods() {
  1193. ClassDB::bind_method(D_METHOD("set_texture", "value"), &VisualShaderNodeTexture3D::set_texture);
  1194. ClassDB::bind_method(D_METHOD("get_texture"), &VisualShaderNodeTexture3D::get_texture);
  1195. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture3D"), "set_texture", "get_texture");
  1196. }
  1197. VisualShaderNodeTexture3D::VisualShaderNodeTexture3D() {
  1198. }
  1199. ////////////// Cubemap
  1200. String VisualShaderNodeCubemap::get_caption() const {
  1201. return "Cubemap";
  1202. }
  1203. int VisualShaderNodeCubemap::get_input_port_count() const {
  1204. return 3;
  1205. }
  1206. VisualShaderNodeCubemap::PortType VisualShaderNodeCubemap::get_input_port_type(int p_port) const {
  1207. switch (p_port) {
  1208. case 0:
  1209. return PORT_TYPE_VECTOR_3D;
  1210. case 1:
  1211. return PORT_TYPE_SCALAR;
  1212. case 2:
  1213. return PORT_TYPE_SAMPLER;
  1214. default:
  1215. return PORT_TYPE_SCALAR;
  1216. }
  1217. }
  1218. String VisualShaderNodeCubemap::get_input_port_name(int p_port) const {
  1219. switch (p_port) {
  1220. case 0:
  1221. return "uv";
  1222. case 1:
  1223. return "lod";
  1224. case 2:
  1225. return "samplerCube";
  1226. default:
  1227. return "";
  1228. }
  1229. }
  1230. int VisualShaderNodeCubemap::get_output_port_count() const {
  1231. return 1;
  1232. }
  1233. VisualShaderNodeCubemap::PortType VisualShaderNodeCubemap::get_output_port_type(int p_port) const {
  1234. return PORT_TYPE_VECTOR_4D;
  1235. }
  1236. String VisualShaderNodeCubemap::get_output_port_name(int p_port) const {
  1237. return "color";
  1238. }
  1239. bool VisualShaderNodeCubemap::is_output_port_expandable(int p_port) const {
  1240. if (p_port == 0) {
  1241. return true;
  1242. }
  1243. return false;
  1244. }
  1245. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeCubemap::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  1246. VisualShader::DefaultTextureParam dtp;
  1247. dtp.name = make_unique_id(p_type, p_id, "cube");
  1248. dtp.params.push_back(cube_map);
  1249. Vector<VisualShader::DefaultTextureParam> ret;
  1250. ret.push_back(dtp);
  1251. return ret;
  1252. }
  1253. String VisualShaderNodeCubemap::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1254. if (source == SOURCE_TEXTURE) {
  1255. String u = "uniform samplerCube " + make_unique_id(p_type, p_id, "cube");
  1256. switch (texture_type) {
  1257. case TYPE_DATA:
  1258. break;
  1259. case TYPE_COLOR:
  1260. u += " : source_color";
  1261. break;
  1262. case TYPE_NORMAL_MAP:
  1263. u += " : hint_normal";
  1264. break;
  1265. default:
  1266. break;
  1267. }
  1268. return u + ";\n";
  1269. }
  1270. return String();
  1271. }
  1272. String VisualShaderNodeCubemap::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1273. String code;
  1274. String id;
  1275. if (source == SOURCE_TEXTURE) {
  1276. id = make_unique_id(p_type, p_id, "cube");
  1277. } else { // SOURCE_PORT
  1278. id = p_input_vars[2];
  1279. if (id.is_empty()) {
  1280. code += " " + p_output_vars[0] + " = vec4(0.0);\n";
  1281. return code;
  1282. }
  1283. }
  1284. String default_uv;
  1285. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  1286. default_uv = "vec3(UV, 0.0)";
  1287. } else {
  1288. default_uv = "vec3(0.0)";
  1289. }
  1290. String uv = p_input_vars[0].is_empty() ? default_uv : p_input_vars[0];
  1291. if (p_input_vars[1].is_empty()) {
  1292. code += " " + p_output_vars[0] + " = texture(" + id + ", " + uv + ");\n";
  1293. } else {
  1294. code += " " + p_output_vars[0] + " = textureLod(" + id + ", " + uv + ", " + p_input_vars[1] + ");\n";
  1295. }
  1296. return code;
  1297. }
  1298. bool VisualShaderNodeCubemap::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  1299. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  1300. if (p_port == 0) {
  1301. return true;
  1302. }
  1303. }
  1304. return false;
  1305. }
  1306. void VisualShaderNodeCubemap::set_source(Source p_source) {
  1307. ERR_FAIL_INDEX(int(p_source), int(SOURCE_MAX));
  1308. if (source == p_source) {
  1309. return;
  1310. }
  1311. source = p_source;
  1312. emit_changed();
  1313. }
  1314. VisualShaderNodeCubemap::Source VisualShaderNodeCubemap::get_source() const {
  1315. return source;
  1316. }
  1317. void VisualShaderNodeCubemap::set_cube_map(Ref<Cubemap> p_cube_map) {
  1318. cube_map = p_cube_map;
  1319. emit_changed();
  1320. }
  1321. Ref<Cubemap> VisualShaderNodeCubemap::get_cube_map() const {
  1322. return cube_map;
  1323. }
  1324. void VisualShaderNodeCubemap::set_texture_type(TextureType p_texture_type) {
  1325. ERR_FAIL_INDEX(int(p_texture_type), int(TYPE_MAX));
  1326. if (texture_type == p_texture_type) {
  1327. return;
  1328. }
  1329. texture_type = p_texture_type;
  1330. emit_changed();
  1331. }
  1332. VisualShaderNodeCubemap::TextureType VisualShaderNodeCubemap::get_texture_type() const {
  1333. return texture_type;
  1334. }
  1335. Vector<StringName> VisualShaderNodeCubemap::get_editable_properties() const {
  1336. Vector<StringName> props;
  1337. props.push_back("source");
  1338. if (source == SOURCE_TEXTURE) {
  1339. props.push_back("cube_map");
  1340. props.push_back("texture_type");
  1341. }
  1342. return props;
  1343. }
  1344. String VisualShaderNodeCubemap::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  1345. if (is_input_port_connected(2) && source != SOURCE_PORT) {
  1346. return RTR("The sampler port is connected but not used. Consider changing the source to 'SamplerPort'.");
  1347. }
  1348. return String();
  1349. }
  1350. void VisualShaderNodeCubemap::_bind_methods() {
  1351. ClassDB::bind_method(D_METHOD("set_source", "value"), &VisualShaderNodeCubemap::set_source);
  1352. ClassDB::bind_method(D_METHOD("get_source"), &VisualShaderNodeCubemap::get_source);
  1353. ClassDB::bind_method(D_METHOD("set_cube_map", "value"), &VisualShaderNodeCubemap::set_cube_map);
  1354. ClassDB::bind_method(D_METHOD("get_cube_map"), &VisualShaderNodeCubemap::get_cube_map);
  1355. ClassDB::bind_method(D_METHOD("set_texture_type", "value"), &VisualShaderNodeCubemap::set_texture_type);
  1356. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeCubemap::get_texture_type);
  1357. ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,SamplerPort"), "set_source", "get_source");
  1358. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "cube_map", PROPERTY_HINT_RESOURCE_TYPE, "Cubemap"), "set_cube_map", "get_cube_map");
  1359. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normal Map"), "set_texture_type", "get_texture_type");
  1360. BIND_ENUM_CONSTANT(SOURCE_TEXTURE);
  1361. BIND_ENUM_CONSTANT(SOURCE_PORT);
  1362. BIND_ENUM_CONSTANT(SOURCE_MAX);
  1363. BIND_ENUM_CONSTANT(TYPE_DATA);
  1364. BIND_ENUM_CONSTANT(TYPE_COLOR);
  1365. BIND_ENUM_CONSTANT(TYPE_NORMAL_MAP);
  1366. BIND_ENUM_CONSTANT(TYPE_MAX);
  1367. }
  1368. VisualShaderNodeCubemap::VisualShaderNodeCubemap() {
  1369. simple_decl = false;
  1370. }
  1371. ////////////// Linear Depth
  1372. String VisualShaderNodeLinearSceneDepth::get_caption() const {
  1373. return "LinearSceneDepth";
  1374. }
  1375. int VisualShaderNodeLinearSceneDepth::get_input_port_count() const {
  1376. return 0;
  1377. }
  1378. VisualShaderNodeLinearSceneDepth::PortType VisualShaderNodeLinearSceneDepth::get_input_port_type(int p_port) const {
  1379. return PORT_TYPE_SCALAR;
  1380. }
  1381. String VisualShaderNodeLinearSceneDepth::get_input_port_name(int p_port) const {
  1382. return "";
  1383. }
  1384. int VisualShaderNodeLinearSceneDepth::get_output_port_count() const {
  1385. return 1;
  1386. }
  1387. VisualShaderNodeLinearSceneDepth::PortType VisualShaderNodeLinearSceneDepth::get_output_port_type(int p_port) const {
  1388. return PORT_TYPE_SCALAR;
  1389. }
  1390. String VisualShaderNodeLinearSceneDepth::get_output_port_name(int p_port) const {
  1391. return "linear depth";
  1392. }
  1393. bool VisualShaderNodeLinearSceneDepth::has_output_port_preview(int p_port) const {
  1394. return false;
  1395. }
  1396. String VisualShaderNodeLinearSceneDepth::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1397. return "uniform sampler2D " + make_unique_id(p_type, p_id, "depth_tex") + " : hint_depth_texture;\n";
  1398. }
  1399. String VisualShaderNodeLinearSceneDepth::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1400. String code;
  1401. code += " {\n";
  1402. code += " float __log_depth = textureLod(" + make_unique_id(p_type, p_id, "depth_tex") + ", SCREEN_UV, 0.0).x;\n";
  1403. if (!RenderingServer::get_singleton()->is_low_end()) {
  1404. code += " vec4 __depth_view = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, __log_depth, 1.0);\n";
  1405. } else {
  1406. code += " vec4 __depth_view = INV_PROJECTION_MATRIX * vec4(vec3(SCREEN_UV, __log_depth) * 2.0 - 1.0, 1.0);\n";
  1407. }
  1408. code += " __depth_view.xyz /= __depth_view.w;\n";
  1409. code += vformat(" %s = -__depth_view.z;\n", p_output_vars[0]);
  1410. code += " }\n";
  1411. return code;
  1412. }
  1413. VisualShaderNodeLinearSceneDepth::VisualShaderNodeLinearSceneDepth() {
  1414. simple_decl = false;
  1415. }
  1416. ////////////// World Position from Depth
  1417. String VisualShaderNodeWorldPositionFromDepth::get_caption() const {
  1418. return "WorldPositionFromDepth";
  1419. }
  1420. int VisualShaderNodeWorldPositionFromDepth::get_input_port_count() const {
  1421. return 1;
  1422. }
  1423. VisualShaderNodeWorldPositionFromDepth::PortType VisualShaderNodeWorldPositionFromDepth::get_input_port_type(int p_port) const {
  1424. return PORT_TYPE_VECTOR_2D;
  1425. }
  1426. String VisualShaderNodeWorldPositionFromDepth::get_input_port_name(int p_port) const {
  1427. return "screen uv";
  1428. }
  1429. bool VisualShaderNodeWorldPositionFromDepth::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  1430. if (p_port == 0) {
  1431. return true;
  1432. }
  1433. return false;
  1434. }
  1435. int VisualShaderNodeWorldPositionFromDepth::get_output_port_count() const {
  1436. return 1;
  1437. }
  1438. VisualShaderNodeWorldPositionFromDepth::PortType VisualShaderNodeWorldPositionFromDepth::get_output_port_type(int p_port) const {
  1439. return PORT_TYPE_VECTOR_3D;
  1440. }
  1441. String VisualShaderNodeWorldPositionFromDepth::get_output_port_name(int p_port) const {
  1442. return "world position";
  1443. }
  1444. bool VisualShaderNodeWorldPositionFromDepth::has_output_port_preview(int p_port) const {
  1445. return false;
  1446. }
  1447. String VisualShaderNodeWorldPositionFromDepth::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1448. return "uniform sampler2D " + make_unique_id(p_type, p_id, "depth_tex") + " : hint_depth_texture, repeat_disable, filter_nearest;\n";
  1449. }
  1450. String VisualShaderNodeWorldPositionFromDepth::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1451. String code;
  1452. String uv = p_input_vars[0].is_empty() ? "SCREEN_UV" : p_input_vars[0];
  1453. code += " {\n";
  1454. code += " float __log_depth = textureLod(" + make_unique_id(p_type, p_id, "depth_tex") + ", " + uv + ", 0.0).x;\n";
  1455. if (!RenderingServer::get_singleton()->is_low_end()) {
  1456. code += " vec4 __depth_view = INV_PROJECTION_MATRIX * vec4(" + uv + " * 2.0 - 1.0, __log_depth, 1.0);\n";
  1457. } else {
  1458. code += " vec4 __depth_view = INV_PROJECTION_MATRIX * vec4(vec3(" + uv + ", __log_depth) * 2.0 - 1.0, 1.0);\n";
  1459. }
  1460. code += " __depth_view.xyz /= __depth_view.w;\n";
  1461. code += vformat(" %s = (INV_VIEW_MATRIX * __depth_view).xyz;\n", p_output_vars[0]);
  1462. code += " }\n";
  1463. return code;
  1464. }
  1465. VisualShaderNodeWorldPositionFromDepth::VisualShaderNodeWorldPositionFromDepth() {
  1466. simple_decl = false;
  1467. }
  1468. ////////////// Unpack Normals in World Space
  1469. String VisualShaderNodeScreenNormalWorldSpace::get_caption() const {
  1470. return "ScreenNormalWorldSpace";
  1471. }
  1472. int VisualShaderNodeScreenNormalWorldSpace::get_input_port_count() const {
  1473. return 1;
  1474. }
  1475. VisualShaderNodeScreenNormalWorldSpace::PortType VisualShaderNodeScreenNormalWorldSpace::get_input_port_type(int p_port) const {
  1476. return PORT_TYPE_VECTOR_2D;
  1477. }
  1478. String VisualShaderNodeScreenNormalWorldSpace::get_input_port_name(int p_port) const {
  1479. return "screen uv";
  1480. }
  1481. bool VisualShaderNodeScreenNormalWorldSpace::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  1482. if (p_port == 0) {
  1483. return true;
  1484. }
  1485. return false;
  1486. }
  1487. int VisualShaderNodeScreenNormalWorldSpace::get_output_port_count() const {
  1488. return 1;
  1489. }
  1490. VisualShaderNodeScreenNormalWorldSpace::PortType VisualShaderNodeScreenNormalWorldSpace::get_output_port_type(int p_port) const {
  1491. return PORT_TYPE_VECTOR_3D;
  1492. }
  1493. String VisualShaderNodeScreenNormalWorldSpace::get_output_port_name(int p_port) const {
  1494. return "screen normal";
  1495. }
  1496. bool VisualShaderNodeScreenNormalWorldSpace::has_output_port_preview(int p_port) const {
  1497. return false;
  1498. }
  1499. String VisualShaderNodeScreenNormalWorldSpace::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  1500. return "uniform sampler2D " + make_unique_id(p_type, p_id, "normal_rough_tex") + " : hint_normal_roughness_texture, repeat_disable, filter_nearest;\n";
  1501. }
  1502. String VisualShaderNodeScreenNormalWorldSpace::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1503. String code;
  1504. String uv = p_input_vars[0].is_empty() ? "SCREEN_UV" : p_input_vars[0];
  1505. code += " {\n";
  1506. code += " vec3 __normals = textureLod(" + make_unique_id(p_type, p_id, "normal_rough_tex") + ", " + uv + ", 0.0).xyz;\n";
  1507. code += " __normals = __normals * 2.0 - 1.0;\n";
  1508. code += vformat(" %s = mat3(INV_VIEW_MATRIX) * __normals;\n", p_output_vars[0]);
  1509. code += " }\n";
  1510. return code;
  1511. }
  1512. VisualShaderNodeScreenNormalWorldSpace::VisualShaderNodeScreenNormalWorldSpace() {
  1513. simple_decl = false;
  1514. }
  1515. ////////////// Float Op
  1516. String VisualShaderNodeFloatOp::get_caption() const {
  1517. return "FloatOp";
  1518. }
  1519. int VisualShaderNodeFloatOp::get_input_port_count() const {
  1520. return 2;
  1521. }
  1522. VisualShaderNodeFloatOp::PortType VisualShaderNodeFloatOp::get_input_port_type(int p_port) const {
  1523. return PORT_TYPE_SCALAR;
  1524. }
  1525. String VisualShaderNodeFloatOp::get_input_port_name(int p_port) const {
  1526. return p_port == 0 ? "a" : "b";
  1527. }
  1528. int VisualShaderNodeFloatOp::get_output_port_count() const {
  1529. return 1;
  1530. }
  1531. VisualShaderNodeFloatOp::PortType VisualShaderNodeFloatOp::get_output_port_type(int p_port) const {
  1532. return PORT_TYPE_SCALAR;
  1533. }
  1534. String VisualShaderNodeFloatOp::get_output_port_name(int p_port) const {
  1535. return "op"; //no output port means the editor will be used as port
  1536. }
  1537. String VisualShaderNodeFloatOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1538. String code = " " + p_output_vars[0] + " = ";
  1539. switch (op) {
  1540. case OP_ADD:
  1541. code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n";
  1542. break;
  1543. case OP_SUB:
  1544. code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n";
  1545. break;
  1546. case OP_MUL:
  1547. code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  1548. break;
  1549. case OP_DIV:
  1550. code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n";
  1551. break;
  1552. case OP_MOD:
  1553. code += "mod(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1554. break;
  1555. case OP_POW:
  1556. code += "pow(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1557. break;
  1558. case OP_MAX:
  1559. code += "max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1560. break;
  1561. case OP_MIN:
  1562. code += "min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1563. break;
  1564. case OP_ATAN2:
  1565. code += "atan(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1566. break;
  1567. case OP_STEP:
  1568. code += "step(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1569. break;
  1570. default:
  1571. break;
  1572. }
  1573. return code;
  1574. }
  1575. void VisualShaderNodeFloatOp::set_operator(Operator p_op) {
  1576. ERR_FAIL_INDEX(int(p_op), int(OP_ENUM_SIZE));
  1577. if (op == p_op) {
  1578. return;
  1579. }
  1580. op = p_op;
  1581. emit_changed();
  1582. }
  1583. VisualShaderNodeFloatOp::Operator VisualShaderNodeFloatOp::get_operator() const {
  1584. return op;
  1585. }
  1586. Vector<StringName> VisualShaderNodeFloatOp::get_editable_properties() const {
  1587. Vector<StringName> props;
  1588. props.push_back("operator");
  1589. return props;
  1590. }
  1591. void VisualShaderNodeFloatOp::_bind_methods() {
  1592. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeFloatOp::set_operator);
  1593. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeFloatOp::get_operator);
  1594. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Subtract,Multiply,Divide,Remainder,Power,Max,Min,ATan2,Step"), "set_operator", "get_operator");
  1595. BIND_ENUM_CONSTANT(OP_ADD);
  1596. BIND_ENUM_CONSTANT(OP_SUB);
  1597. BIND_ENUM_CONSTANT(OP_MUL);
  1598. BIND_ENUM_CONSTANT(OP_DIV);
  1599. BIND_ENUM_CONSTANT(OP_MOD);
  1600. BIND_ENUM_CONSTANT(OP_POW);
  1601. BIND_ENUM_CONSTANT(OP_MAX);
  1602. BIND_ENUM_CONSTANT(OP_MIN);
  1603. BIND_ENUM_CONSTANT(OP_ATAN2);
  1604. BIND_ENUM_CONSTANT(OP_STEP);
  1605. BIND_ENUM_CONSTANT(OP_ENUM_SIZE);
  1606. }
  1607. VisualShaderNodeFloatOp::VisualShaderNodeFloatOp() {
  1608. set_input_port_default_value(0, 0.0);
  1609. set_input_port_default_value(1, 0.0);
  1610. }
  1611. ////////////// Integer Op
  1612. String VisualShaderNodeIntOp::get_caption() const {
  1613. return "IntOp";
  1614. }
  1615. int VisualShaderNodeIntOp::get_input_port_count() const {
  1616. return 2;
  1617. }
  1618. VisualShaderNodeIntOp::PortType VisualShaderNodeIntOp::get_input_port_type(int p_port) const {
  1619. return PORT_TYPE_SCALAR_INT;
  1620. }
  1621. String VisualShaderNodeIntOp::get_input_port_name(int p_port) const {
  1622. return p_port == 0 ? "a" : "b";
  1623. }
  1624. int VisualShaderNodeIntOp::get_output_port_count() const {
  1625. return 1;
  1626. }
  1627. VisualShaderNodeIntOp::PortType VisualShaderNodeIntOp::get_output_port_type(int p_port) const {
  1628. return PORT_TYPE_SCALAR_INT;
  1629. }
  1630. String VisualShaderNodeIntOp::get_output_port_name(int p_port) const {
  1631. return "op"; // No output port means the editor will be used as port.
  1632. }
  1633. String VisualShaderNodeIntOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1634. String code = " " + p_output_vars[0] + " = ";
  1635. switch (op) {
  1636. case OP_ADD:
  1637. code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n";
  1638. break;
  1639. case OP_SUB:
  1640. code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n";
  1641. break;
  1642. case OP_MUL:
  1643. code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  1644. break;
  1645. case OP_DIV:
  1646. code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n";
  1647. break;
  1648. case OP_MOD:
  1649. code += p_input_vars[0] + " % " + p_input_vars[1] + ";\n";
  1650. break;
  1651. case OP_MAX:
  1652. code += "max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1653. break;
  1654. case OP_MIN:
  1655. code += "min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1656. break;
  1657. case OP_BITWISE_AND:
  1658. code += p_input_vars[0] + " & " + p_input_vars[1] + ";\n";
  1659. break;
  1660. case OP_BITWISE_OR:
  1661. code += p_input_vars[0] + " | " + p_input_vars[1] + ";\n";
  1662. break;
  1663. case OP_BITWISE_XOR:
  1664. code += p_input_vars[0] + " ^ " + p_input_vars[1] + ";\n";
  1665. break;
  1666. case OP_BITWISE_LEFT_SHIFT:
  1667. code += p_input_vars[0] + " << " + p_input_vars[1] + ";\n";
  1668. break;
  1669. case OP_BITWISE_RIGHT_SHIFT:
  1670. code += p_input_vars[0] + " >> " + p_input_vars[1] + ";\n";
  1671. break;
  1672. default:
  1673. break;
  1674. }
  1675. return code;
  1676. }
  1677. void VisualShaderNodeIntOp::set_operator(Operator p_op) {
  1678. ERR_FAIL_INDEX(int(p_op), OP_ENUM_SIZE);
  1679. if (op == p_op) {
  1680. return;
  1681. }
  1682. op = p_op;
  1683. emit_changed();
  1684. }
  1685. VisualShaderNodeIntOp::Operator VisualShaderNodeIntOp::get_operator() const {
  1686. return op;
  1687. }
  1688. Vector<StringName> VisualShaderNodeIntOp::get_editable_properties() const {
  1689. Vector<StringName> props;
  1690. props.push_back("operator");
  1691. return props;
  1692. }
  1693. void VisualShaderNodeIntOp::_bind_methods() {
  1694. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeIntOp::set_operator);
  1695. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeIntOp::get_operator);
  1696. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Subtract,Multiply,Divide,Remainder,Max,Min,Bitwise AND,Bitwise OR,Bitwise XOR,Bitwise Left Shift,Bitwise Right Shift"), "set_operator", "get_operator");
  1697. BIND_ENUM_CONSTANT(OP_ADD);
  1698. BIND_ENUM_CONSTANT(OP_SUB);
  1699. BIND_ENUM_CONSTANT(OP_MUL);
  1700. BIND_ENUM_CONSTANT(OP_DIV);
  1701. BIND_ENUM_CONSTANT(OP_MOD);
  1702. BIND_ENUM_CONSTANT(OP_MAX);
  1703. BIND_ENUM_CONSTANT(OP_MIN);
  1704. BIND_ENUM_CONSTANT(OP_BITWISE_AND);
  1705. BIND_ENUM_CONSTANT(OP_BITWISE_OR);
  1706. BIND_ENUM_CONSTANT(OP_BITWISE_XOR);
  1707. BIND_ENUM_CONSTANT(OP_BITWISE_LEFT_SHIFT);
  1708. BIND_ENUM_CONSTANT(OP_BITWISE_RIGHT_SHIFT);
  1709. BIND_ENUM_CONSTANT(OP_ENUM_SIZE);
  1710. }
  1711. VisualShaderNodeIntOp::VisualShaderNodeIntOp() {
  1712. set_input_port_default_value(0, 0);
  1713. set_input_port_default_value(1, 0);
  1714. }
  1715. ////////////// Unsigned Integer Op
  1716. String VisualShaderNodeUIntOp::get_caption() const {
  1717. return "UIntOp";
  1718. }
  1719. int VisualShaderNodeUIntOp::get_input_port_count() const {
  1720. return 2;
  1721. }
  1722. VisualShaderNodeUIntOp::PortType VisualShaderNodeUIntOp::get_input_port_type(int p_port) const {
  1723. return PORT_TYPE_SCALAR_UINT;
  1724. }
  1725. String VisualShaderNodeUIntOp::get_input_port_name(int p_port) const {
  1726. return p_port == 0 ? "a" : "b";
  1727. }
  1728. int VisualShaderNodeUIntOp::get_output_port_count() const {
  1729. return 1;
  1730. }
  1731. VisualShaderNodeUIntOp::PortType VisualShaderNodeUIntOp::get_output_port_type(int p_port) const {
  1732. return PORT_TYPE_SCALAR_UINT;
  1733. }
  1734. String VisualShaderNodeUIntOp::get_output_port_name(int p_port) const {
  1735. return "op"; // No output port means the editor will be used as port.
  1736. }
  1737. String VisualShaderNodeUIntOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1738. String code = " " + p_output_vars[0] + " = ";
  1739. switch (op) {
  1740. case OP_ADD:
  1741. code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n";
  1742. break;
  1743. case OP_SUB:
  1744. code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n";
  1745. break;
  1746. case OP_MUL:
  1747. code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  1748. break;
  1749. case OP_DIV:
  1750. code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n";
  1751. break;
  1752. case OP_MOD:
  1753. code += p_input_vars[0] + " % " + p_input_vars[1] + ";\n";
  1754. break;
  1755. case OP_MAX:
  1756. code += "max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1757. break;
  1758. case OP_MIN:
  1759. code += "min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1760. break;
  1761. case OP_BITWISE_AND:
  1762. code += p_input_vars[0] + " & " + p_input_vars[1] + ";\n";
  1763. break;
  1764. case OP_BITWISE_OR:
  1765. code += p_input_vars[0] + " | " + p_input_vars[1] + ";\n";
  1766. break;
  1767. case OP_BITWISE_XOR:
  1768. code += p_input_vars[0] + " ^ " + p_input_vars[1] + ";\n";
  1769. break;
  1770. case OP_BITWISE_LEFT_SHIFT:
  1771. code += p_input_vars[0] + " << " + p_input_vars[1] + ";\n";
  1772. break;
  1773. case OP_BITWISE_RIGHT_SHIFT:
  1774. code += p_input_vars[0] + " >> " + p_input_vars[1] + ";\n";
  1775. break;
  1776. default:
  1777. break;
  1778. }
  1779. return code;
  1780. }
  1781. void VisualShaderNodeUIntOp::set_operator(Operator p_op) {
  1782. ERR_FAIL_INDEX(int(p_op), OP_ENUM_SIZE);
  1783. if (op == p_op) {
  1784. return;
  1785. }
  1786. op = p_op;
  1787. emit_changed();
  1788. }
  1789. VisualShaderNodeUIntOp::Operator VisualShaderNodeUIntOp::get_operator() const {
  1790. return op;
  1791. }
  1792. Vector<StringName> VisualShaderNodeUIntOp::get_editable_properties() const {
  1793. Vector<StringName> props;
  1794. props.push_back("operator");
  1795. return props;
  1796. }
  1797. void VisualShaderNodeUIntOp::_bind_methods() {
  1798. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeUIntOp::set_operator);
  1799. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeUIntOp::get_operator);
  1800. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Subtract,Multiply,Divide,Remainder,Max,Min,Bitwise AND,Bitwise OR,Bitwise XOR,Bitwise Left Shift,Bitwise Right Shift"), "set_operator", "get_operator");
  1801. BIND_ENUM_CONSTANT(OP_ADD);
  1802. BIND_ENUM_CONSTANT(OP_SUB);
  1803. BIND_ENUM_CONSTANT(OP_MUL);
  1804. BIND_ENUM_CONSTANT(OP_DIV);
  1805. BIND_ENUM_CONSTANT(OP_MOD);
  1806. BIND_ENUM_CONSTANT(OP_MAX);
  1807. BIND_ENUM_CONSTANT(OP_MIN);
  1808. BIND_ENUM_CONSTANT(OP_BITWISE_AND);
  1809. BIND_ENUM_CONSTANT(OP_BITWISE_OR);
  1810. BIND_ENUM_CONSTANT(OP_BITWISE_XOR);
  1811. BIND_ENUM_CONSTANT(OP_BITWISE_LEFT_SHIFT);
  1812. BIND_ENUM_CONSTANT(OP_BITWISE_RIGHT_SHIFT);
  1813. BIND_ENUM_CONSTANT(OP_ENUM_SIZE);
  1814. }
  1815. VisualShaderNodeUIntOp::VisualShaderNodeUIntOp() {
  1816. set_input_port_default_value(0, 0);
  1817. set_input_port_default_value(1, 0);
  1818. }
  1819. ////////////// Vector Op
  1820. String VisualShaderNodeVectorOp::get_caption() const {
  1821. return "VectorOp";
  1822. }
  1823. int VisualShaderNodeVectorOp::get_input_port_count() const {
  1824. return 2;
  1825. }
  1826. String VisualShaderNodeVectorOp::get_input_port_name(int p_port) const {
  1827. return p_port == 0 ? "a" : "b";
  1828. }
  1829. int VisualShaderNodeVectorOp::get_output_port_count() const {
  1830. return 1;
  1831. }
  1832. String VisualShaderNodeVectorOp::get_output_port_name(int p_port) const {
  1833. return "op";
  1834. }
  1835. String VisualShaderNodeVectorOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1836. String code = " " + p_output_vars[0] + " = ";
  1837. switch (op) {
  1838. case OP_ADD:
  1839. code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n";
  1840. break;
  1841. case OP_SUB:
  1842. code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n";
  1843. break;
  1844. case OP_MUL:
  1845. code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  1846. break;
  1847. case OP_DIV:
  1848. code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n";
  1849. break;
  1850. case OP_MOD:
  1851. code += "mod(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1852. break;
  1853. case OP_POW:
  1854. code += "pow(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1855. break;
  1856. case OP_MAX:
  1857. code += "max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1858. break;
  1859. case OP_MIN:
  1860. code += "min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1861. break;
  1862. case OP_CROSS:
  1863. if (op_type == OP_TYPE_VECTOR_2D) { // Not supported.
  1864. code += "vec2(0.0);\n";
  1865. } else if (op_type == OP_TYPE_VECTOR_4D) { // Not supported.
  1866. code += "vec4(0.0);\n";
  1867. } else {
  1868. code += "cross(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1869. }
  1870. break;
  1871. case OP_ATAN2:
  1872. code += "atan(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1873. break;
  1874. case OP_REFLECT:
  1875. code += "reflect(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1876. break;
  1877. case OP_STEP:
  1878. code += "step(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1879. break;
  1880. default:
  1881. break;
  1882. }
  1883. return code;
  1884. }
  1885. void VisualShaderNodeVectorOp::set_op_type(OpType p_op_type) {
  1886. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  1887. if (op_type == p_op_type) {
  1888. return;
  1889. }
  1890. switch (p_op_type) {
  1891. case OP_TYPE_VECTOR_2D: {
  1892. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  1893. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  1894. } break;
  1895. case OP_TYPE_VECTOR_3D: {
  1896. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  1897. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  1898. } break;
  1899. case OP_TYPE_VECTOR_4D: {
  1900. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  1901. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  1902. } break;
  1903. default:
  1904. break;
  1905. }
  1906. op_type = p_op_type;
  1907. emit_changed();
  1908. }
  1909. void VisualShaderNodeVectorOp::set_operator(Operator p_op) {
  1910. ERR_FAIL_INDEX(int(p_op), int(OP_ENUM_SIZE));
  1911. if (op == p_op) {
  1912. return;
  1913. }
  1914. op = p_op;
  1915. emit_changed();
  1916. }
  1917. VisualShaderNodeVectorOp::Operator VisualShaderNodeVectorOp::get_operator() const {
  1918. return op;
  1919. }
  1920. Vector<StringName> VisualShaderNodeVectorOp::get_editable_properties() const {
  1921. Vector<StringName> props = VisualShaderNodeVectorBase::get_editable_properties();
  1922. props.push_back("operator");
  1923. return props;
  1924. }
  1925. String VisualShaderNodeVectorOp::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  1926. bool invalid_type = false;
  1927. if (op_type == OP_TYPE_VECTOR_2D || op_type == OP_TYPE_VECTOR_4D) {
  1928. if (op == OP_CROSS) {
  1929. invalid_type = true;
  1930. }
  1931. }
  1932. if (invalid_type) {
  1933. return RTR("Invalid operator for that type.");
  1934. }
  1935. return String();
  1936. }
  1937. void VisualShaderNodeVectorOp::_bind_methods() {
  1938. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeVectorOp::set_operator);
  1939. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeVectorOp::get_operator);
  1940. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Subtract,Multiply,Divide,Remainder,Power,Max,Min,Cross,ATan2,Reflect,Step"), "set_operator", "get_operator");
  1941. BIND_ENUM_CONSTANT(OP_ADD);
  1942. BIND_ENUM_CONSTANT(OP_SUB);
  1943. BIND_ENUM_CONSTANT(OP_MUL);
  1944. BIND_ENUM_CONSTANT(OP_DIV);
  1945. BIND_ENUM_CONSTANT(OP_MOD);
  1946. BIND_ENUM_CONSTANT(OP_POW);
  1947. BIND_ENUM_CONSTANT(OP_MAX);
  1948. BIND_ENUM_CONSTANT(OP_MIN);
  1949. BIND_ENUM_CONSTANT(OP_CROSS);
  1950. BIND_ENUM_CONSTANT(OP_ATAN2);
  1951. BIND_ENUM_CONSTANT(OP_REFLECT);
  1952. BIND_ENUM_CONSTANT(OP_STEP);
  1953. BIND_ENUM_CONSTANT(OP_ENUM_SIZE);
  1954. }
  1955. VisualShaderNodeVectorOp::VisualShaderNodeVectorOp() {
  1956. switch (op_type) {
  1957. case OP_TYPE_VECTOR_2D: {
  1958. set_input_port_default_value(0, Vector2());
  1959. set_input_port_default_value(1, Vector2());
  1960. } break;
  1961. case OP_TYPE_VECTOR_3D: {
  1962. set_input_port_default_value(0, Vector3());
  1963. set_input_port_default_value(1, Vector3());
  1964. } break;
  1965. case OP_TYPE_VECTOR_4D: {
  1966. set_input_port_default_value(0, Quaternion());
  1967. set_input_port_default_value(1, Quaternion());
  1968. } break;
  1969. default:
  1970. break;
  1971. }
  1972. }
  1973. ////////////// Color Op
  1974. String VisualShaderNodeColorOp::get_caption() const {
  1975. return "ColorOp";
  1976. }
  1977. int VisualShaderNodeColorOp::get_input_port_count() const {
  1978. return 2;
  1979. }
  1980. VisualShaderNodeColorOp::PortType VisualShaderNodeColorOp::get_input_port_type(int p_port) const {
  1981. return PORT_TYPE_VECTOR_3D;
  1982. }
  1983. String VisualShaderNodeColorOp::get_input_port_name(int p_port) const {
  1984. return p_port == 0 ? "a" : "b";
  1985. }
  1986. int VisualShaderNodeColorOp::get_output_port_count() const {
  1987. return 1;
  1988. }
  1989. VisualShaderNodeColorOp::PortType VisualShaderNodeColorOp::get_output_port_type(int p_port) const {
  1990. return PORT_TYPE_VECTOR_3D;
  1991. }
  1992. String VisualShaderNodeColorOp::get_output_port_name(int p_port) const {
  1993. return "op"; //no output port means the editor will be used as port
  1994. }
  1995. String VisualShaderNodeColorOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1996. String code;
  1997. static const char *axisn[3] = { "x", "y", "z" };
  1998. switch (op) {
  1999. case OP_SCREEN: {
  2000. code += " " + p_output_vars[0] + " = vec3(1.0) - (vec3(1.0) - " + p_input_vars[0] + ") * (vec3(1.0) - " + p_input_vars[1] + ");\n";
  2001. } break;
  2002. case OP_DIFFERENCE: {
  2003. code += " " + p_output_vars[0] + " = abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ");\n";
  2004. } break;
  2005. case OP_DARKEN: {
  2006. code += " " + p_output_vars[0] + " = min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  2007. } break;
  2008. case OP_LIGHTEN: {
  2009. code += " " + p_output_vars[0] + " = max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  2010. } break;
  2011. case OP_OVERLAY: {
  2012. for (int i = 0; i < 3; i++) {
  2013. code += " {\n";
  2014. code += " float base = " + p_input_vars[0] + "." + axisn[i] + ";\n";
  2015. code += " float blend = " + p_input_vars[1] + "." + axisn[i] + ";\n";
  2016. code += " if (base < 0.5) {\n";
  2017. code += " " + p_output_vars[0] + "." + axisn[i] + " = 2.0 * base * blend;\n";
  2018. code += " } else {\n";
  2019. code += " " + p_output_vars[0] + "." + axisn[i] + " = 1.0 - 2.0 * (1.0 - blend) * (1.0 - base);\n";
  2020. code += " }\n";
  2021. code += " }\n";
  2022. }
  2023. } break;
  2024. case OP_DODGE: {
  2025. code += " " + p_output_vars[0] + " = (" + p_input_vars[0] + ") / (vec3(1.0) - " + p_input_vars[1] + ");\n";
  2026. } break;
  2027. case OP_BURN: {
  2028. code += " " + p_output_vars[0] + " = vec3(1.0) - (vec3(1.0) - " + p_input_vars[0] + ") / (" + p_input_vars[1] + ");\n";
  2029. } break;
  2030. case OP_SOFT_LIGHT: {
  2031. for (int i = 0; i < 3; i++) {
  2032. code += " {\n";
  2033. code += " float base = " + p_input_vars[0] + "." + axisn[i] + ";\n";
  2034. code += " float blend = " + p_input_vars[1] + "." + axisn[i] + ";\n";
  2035. code += " if (base < 0.5) {\n";
  2036. code += " " + p_output_vars[0] + "." + axisn[i] + " = (base * (blend + 0.5));\n";
  2037. code += " } else {\n";
  2038. code += " " + p_output_vars[0] + "." + axisn[i] + " = (1.0 - (1.0 - base) * (1.0 - (blend - 0.5)));\n";
  2039. code += " }\n";
  2040. code += " }\n";
  2041. }
  2042. } break;
  2043. case OP_HARD_LIGHT: {
  2044. for (int i = 0; i < 3; i++) {
  2045. code += " {\n";
  2046. code += " float base = " + p_input_vars[0] + "." + axisn[i] + ";\n";
  2047. code += " float blend = " + p_input_vars[1] + "." + axisn[i] + ";\n";
  2048. code += " if (base < 0.5) {\n";
  2049. code += " " + p_output_vars[0] + "." + axisn[i] + " = (base * (2.0 * blend));\n";
  2050. code += " } else {\n";
  2051. code += " " + p_output_vars[0] + "." + axisn[i] + " = (1.0 - (1.0 - base) * (1.0 - 2.0 * (blend - 0.5)));\n";
  2052. code += " }\n";
  2053. code += " }\n";
  2054. }
  2055. } break;
  2056. default:
  2057. break;
  2058. }
  2059. return code;
  2060. }
  2061. void VisualShaderNodeColorOp::set_operator(Operator p_op) {
  2062. ERR_FAIL_INDEX(int(p_op), int(OP_MAX));
  2063. if (op == p_op) {
  2064. return;
  2065. }
  2066. switch (p_op) {
  2067. case OP_SCREEN:
  2068. simple_decl = true;
  2069. break;
  2070. case OP_DIFFERENCE:
  2071. simple_decl = true;
  2072. break;
  2073. case OP_DARKEN:
  2074. simple_decl = true;
  2075. break;
  2076. case OP_LIGHTEN:
  2077. simple_decl = true;
  2078. break;
  2079. case OP_OVERLAY:
  2080. simple_decl = false;
  2081. break;
  2082. case OP_DODGE:
  2083. simple_decl = true;
  2084. break;
  2085. case OP_BURN:
  2086. simple_decl = true;
  2087. break;
  2088. case OP_SOFT_LIGHT:
  2089. simple_decl = false;
  2090. break;
  2091. case OP_HARD_LIGHT:
  2092. simple_decl = false;
  2093. break;
  2094. default:
  2095. break;
  2096. }
  2097. op = p_op;
  2098. emit_changed();
  2099. }
  2100. VisualShaderNodeColorOp::Operator VisualShaderNodeColorOp::get_operator() const {
  2101. return op;
  2102. }
  2103. Vector<StringName> VisualShaderNodeColorOp::get_editable_properties() const {
  2104. Vector<StringName> props;
  2105. props.push_back("operator");
  2106. return props;
  2107. }
  2108. void VisualShaderNodeColorOp::_bind_methods() {
  2109. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeColorOp::set_operator);
  2110. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeColorOp::get_operator);
  2111. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Screen,Difference,Darken,Lighten,Overlay,Dodge,Burn,Soft Light,Hard Light"), "set_operator", "get_operator");
  2112. BIND_ENUM_CONSTANT(OP_SCREEN);
  2113. BIND_ENUM_CONSTANT(OP_DIFFERENCE);
  2114. BIND_ENUM_CONSTANT(OP_DARKEN);
  2115. BIND_ENUM_CONSTANT(OP_LIGHTEN);
  2116. BIND_ENUM_CONSTANT(OP_OVERLAY);
  2117. BIND_ENUM_CONSTANT(OP_DODGE);
  2118. BIND_ENUM_CONSTANT(OP_BURN);
  2119. BIND_ENUM_CONSTANT(OP_SOFT_LIGHT);
  2120. BIND_ENUM_CONSTANT(OP_HARD_LIGHT);
  2121. BIND_ENUM_CONSTANT(OP_MAX);
  2122. }
  2123. VisualShaderNodeColorOp::VisualShaderNodeColorOp() {
  2124. set_input_port_default_value(0, Vector3());
  2125. set_input_port_default_value(1, Vector3());
  2126. }
  2127. ////////////// Transform Op
  2128. String VisualShaderNodeTransformOp::get_caption() const {
  2129. return "TransformOp";
  2130. }
  2131. int VisualShaderNodeTransformOp::get_input_port_count() const {
  2132. return 2;
  2133. }
  2134. VisualShaderNodeTransformOp::PortType VisualShaderNodeTransformOp::get_input_port_type(int p_port) const {
  2135. return PORT_TYPE_TRANSFORM;
  2136. }
  2137. String VisualShaderNodeTransformOp::get_input_port_name(int p_port) const {
  2138. return p_port == 0 ? "a" : "b";
  2139. }
  2140. int VisualShaderNodeTransformOp::get_output_port_count() const {
  2141. return 1;
  2142. }
  2143. VisualShaderNodeTransformOp::PortType VisualShaderNodeTransformOp::get_output_port_type(int p_port) const {
  2144. return PORT_TYPE_TRANSFORM;
  2145. }
  2146. String VisualShaderNodeTransformOp::get_output_port_name(int p_port) const {
  2147. return "mult"; //no output port means the editor will be used as port
  2148. }
  2149. String VisualShaderNodeTransformOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2150. switch (op) {
  2151. case OP_AxB:
  2152. return " " + p_output_vars[0] + " = " + p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  2153. case OP_BxA:
  2154. return " " + p_output_vars[0] + " = " + p_input_vars[1] + " * " + p_input_vars[0] + ";\n";
  2155. case OP_AxB_COMP:
  2156. return " " + p_output_vars[0] + " = matrixCompMult(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  2157. case OP_BxA_COMP:
  2158. return " " + p_output_vars[0] + " = matrixCompMult(" + p_input_vars[1] + ", " + p_input_vars[0] + ");\n";
  2159. case OP_ADD:
  2160. return " " + p_output_vars[0] + " = " + p_input_vars[0] + " + " + p_input_vars[1] + ";\n";
  2161. case OP_A_MINUS_B:
  2162. return " " + p_output_vars[0] + " = " + p_input_vars[0] + " - " + p_input_vars[1] + ";\n";
  2163. case OP_B_MINUS_A:
  2164. return " " + p_output_vars[0] + " = " + p_input_vars[1] + " - " + p_input_vars[0] + ";\n";
  2165. case OP_A_DIV_B:
  2166. return " " + p_output_vars[0] + " = " + p_input_vars[0] + " / " + p_input_vars[1] + ";\n";
  2167. case OP_B_DIV_A:
  2168. return " " + p_output_vars[0] + " = " + p_input_vars[1] + " / " + p_input_vars[0] + ";\n";
  2169. default:
  2170. return "";
  2171. }
  2172. }
  2173. void VisualShaderNodeTransformOp::set_operator(Operator p_op) {
  2174. ERR_FAIL_INDEX(int(p_op), int(OP_MAX));
  2175. if (op == p_op) {
  2176. return;
  2177. }
  2178. op = p_op;
  2179. emit_changed();
  2180. }
  2181. VisualShaderNodeTransformOp::Operator VisualShaderNodeTransformOp::get_operator() const {
  2182. return op;
  2183. }
  2184. Vector<StringName> VisualShaderNodeTransformOp::get_editable_properties() const {
  2185. Vector<StringName> props;
  2186. props.push_back("operator");
  2187. return props;
  2188. }
  2189. void VisualShaderNodeTransformOp::_bind_methods() {
  2190. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeTransformOp::set_operator);
  2191. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeTransformOp::get_operator);
  2192. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "A x B,B x A,A x B(per component),B x A(per component),A + B,A - B,B - A,A / B,B / A"), "set_operator", "get_operator");
  2193. BIND_ENUM_CONSTANT(OP_AxB);
  2194. BIND_ENUM_CONSTANT(OP_BxA);
  2195. BIND_ENUM_CONSTANT(OP_AxB_COMP);
  2196. BIND_ENUM_CONSTANT(OP_BxA_COMP);
  2197. BIND_ENUM_CONSTANT(OP_ADD);
  2198. BIND_ENUM_CONSTANT(OP_A_MINUS_B);
  2199. BIND_ENUM_CONSTANT(OP_B_MINUS_A);
  2200. BIND_ENUM_CONSTANT(OP_A_DIV_B);
  2201. BIND_ENUM_CONSTANT(OP_B_DIV_A);
  2202. BIND_ENUM_CONSTANT(OP_MAX);
  2203. }
  2204. VisualShaderNodeTransformOp::VisualShaderNodeTransformOp() {
  2205. set_input_port_default_value(0, Transform3D());
  2206. set_input_port_default_value(1, Transform3D());
  2207. }
  2208. ////////////// TransformVec Mult
  2209. String VisualShaderNodeTransformVecMult::get_caption() const {
  2210. return "TransformVectorMult";
  2211. }
  2212. int VisualShaderNodeTransformVecMult::get_input_port_count() const {
  2213. return 2;
  2214. }
  2215. VisualShaderNodeTransformVecMult::PortType VisualShaderNodeTransformVecMult::get_input_port_type(int p_port) const {
  2216. return p_port == 0 ? PORT_TYPE_TRANSFORM : PORT_TYPE_VECTOR_3D;
  2217. }
  2218. String VisualShaderNodeTransformVecMult::get_input_port_name(int p_port) const {
  2219. return p_port == 0 ? "a" : "b";
  2220. }
  2221. int VisualShaderNodeTransformVecMult::get_output_port_count() const {
  2222. return 1;
  2223. }
  2224. VisualShaderNodeTransformVecMult::PortType VisualShaderNodeTransformVecMult::get_output_port_type(int p_port) const {
  2225. return PORT_TYPE_VECTOR_3D;
  2226. }
  2227. String VisualShaderNodeTransformVecMult::get_output_port_name(int p_port) const {
  2228. return ""; //no output port means the editor will be used as port
  2229. }
  2230. String VisualShaderNodeTransformVecMult::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2231. if (op == OP_AxB) {
  2232. return " " + p_output_vars[0] + " = (" + p_input_vars[0] + " * vec4(" + p_input_vars[1] + ", 1.0)).xyz;\n";
  2233. } else if (op == OP_BxA) {
  2234. return " " + p_output_vars[0] + " = (vec4(" + p_input_vars[1] + ", 1.0) * " + p_input_vars[0] + ").xyz;\n";
  2235. } else if (op == OP_3x3_AxB) {
  2236. return " " + p_output_vars[0] + " = (" + p_input_vars[0] + " * vec4(" + p_input_vars[1] + ", 0.0)).xyz;\n";
  2237. } else {
  2238. return " " + p_output_vars[0] + " = (vec4(" + p_input_vars[1] + ", 0.0) * " + p_input_vars[0] + ").xyz;\n";
  2239. }
  2240. }
  2241. void VisualShaderNodeTransformVecMult::set_operator(Operator p_op) {
  2242. ERR_FAIL_INDEX(int(p_op), int(OP_MAX));
  2243. if (op == p_op) {
  2244. return;
  2245. }
  2246. op = p_op;
  2247. emit_changed();
  2248. }
  2249. VisualShaderNodeTransformVecMult::Operator VisualShaderNodeTransformVecMult::get_operator() const {
  2250. return op;
  2251. }
  2252. Vector<StringName> VisualShaderNodeTransformVecMult::get_editable_properties() const {
  2253. Vector<StringName> props;
  2254. props.push_back("operator");
  2255. return props;
  2256. }
  2257. void VisualShaderNodeTransformVecMult::_bind_methods() {
  2258. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeTransformVecMult::set_operator);
  2259. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeTransformVecMult::get_operator);
  2260. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "A x B,B x A,A x B (3x3),B x A (3x3)"), "set_operator", "get_operator");
  2261. BIND_ENUM_CONSTANT(OP_AxB);
  2262. BIND_ENUM_CONSTANT(OP_BxA);
  2263. BIND_ENUM_CONSTANT(OP_3x3_AxB);
  2264. BIND_ENUM_CONSTANT(OP_3x3_BxA);
  2265. BIND_ENUM_CONSTANT(OP_MAX);
  2266. }
  2267. VisualShaderNodeTransformVecMult::VisualShaderNodeTransformVecMult() {
  2268. set_input_port_default_value(0, Transform3D());
  2269. set_input_port_default_value(1, Vector3());
  2270. }
  2271. ////////////// Float Func
  2272. String VisualShaderNodeFloatFunc::get_caption() const {
  2273. return "FloatFunc";
  2274. }
  2275. int VisualShaderNodeFloatFunc::get_input_port_count() const {
  2276. return 1;
  2277. }
  2278. VisualShaderNodeFloatFunc::PortType VisualShaderNodeFloatFunc::get_input_port_type(int p_port) const {
  2279. return PORT_TYPE_SCALAR;
  2280. }
  2281. String VisualShaderNodeFloatFunc::get_input_port_name(int p_port) const {
  2282. return "";
  2283. }
  2284. int VisualShaderNodeFloatFunc::get_output_port_count() const {
  2285. return 1;
  2286. }
  2287. VisualShaderNodeFloatFunc::PortType VisualShaderNodeFloatFunc::get_output_port_type(int p_port) const {
  2288. return PORT_TYPE_SCALAR;
  2289. }
  2290. String VisualShaderNodeFloatFunc::get_output_port_name(int p_port) const {
  2291. return ""; //no output port means the editor will be used as port
  2292. }
  2293. String VisualShaderNodeFloatFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2294. static const char *functions[FUNC_MAX] = {
  2295. "sin($)",
  2296. "cos($)",
  2297. "tan($)",
  2298. "asin($)",
  2299. "acos($)",
  2300. "atan($)",
  2301. "sinh($)",
  2302. "cosh($)",
  2303. "tanh($)",
  2304. "log($)",
  2305. "exp($)",
  2306. "sqrt($)",
  2307. "abs($)",
  2308. "sign($)",
  2309. "floor($)",
  2310. "round($)",
  2311. "ceil($)",
  2312. "fract($)",
  2313. "min(max($, 0.0), 1.0)",
  2314. "-($)",
  2315. "acosh($)",
  2316. "asinh($)",
  2317. "atanh($)",
  2318. "degrees($)",
  2319. "exp2($)",
  2320. "inversesqrt($)",
  2321. "log2($)",
  2322. "radians($)",
  2323. "1.0 / ($)",
  2324. "roundEven($)",
  2325. "trunc($)",
  2326. "1.0 - $"
  2327. };
  2328. return " " + p_output_vars[0] + " = " + String(functions[func]).replace("$", p_input_vars[0]) + ";\n";
  2329. }
  2330. void VisualShaderNodeFloatFunc::set_function(Function p_func) {
  2331. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  2332. if (func == p_func) {
  2333. return;
  2334. }
  2335. func = p_func;
  2336. emit_changed();
  2337. }
  2338. VisualShaderNodeFloatFunc::Function VisualShaderNodeFloatFunc::get_function() const {
  2339. return func;
  2340. }
  2341. Vector<StringName> VisualShaderNodeFloatFunc::get_editable_properties() const {
  2342. Vector<StringName> props;
  2343. props.push_back("function");
  2344. return props;
  2345. }
  2346. void VisualShaderNodeFloatFunc::_bind_methods() {
  2347. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeFloatFunc::set_function);
  2348. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeFloatFunc::get_function);
  2349. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Sin,Cos,Tan,ASin,ACos,ATan,SinH,CosH,TanH,Log,Exp,Sqrt,Abs,Sign,Floor,Round,Ceil,Fract,Saturate,Negate,ACosH,ASinH,ATanH,Degrees,Exp2,InverseSqrt,Log2,Radians,Reciprocal,RoundEven,Trunc,OneMinus"), "set_function", "get_function");
  2350. BIND_ENUM_CONSTANT(FUNC_SIN);
  2351. BIND_ENUM_CONSTANT(FUNC_COS);
  2352. BIND_ENUM_CONSTANT(FUNC_TAN);
  2353. BIND_ENUM_CONSTANT(FUNC_ASIN);
  2354. BIND_ENUM_CONSTANT(FUNC_ACOS);
  2355. BIND_ENUM_CONSTANT(FUNC_ATAN);
  2356. BIND_ENUM_CONSTANT(FUNC_SINH);
  2357. BIND_ENUM_CONSTANT(FUNC_COSH);
  2358. BIND_ENUM_CONSTANT(FUNC_TANH);
  2359. BIND_ENUM_CONSTANT(FUNC_LOG);
  2360. BIND_ENUM_CONSTANT(FUNC_EXP);
  2361. BIND_ENUM_CONSTANT(FUNC_SQRT);
  2362. BIND_ENUM_CONSTANT(FUNC_ABS);
  2363. BIND_ENUM_CONSTANT(FUNC_SIGN);
  2364. BIND_ENUM_CONSTANT(FUNC_FLOOR);
  2365. BIND_ENUM_CONSTANT(FUNC_ROUND);
  2366. BIND_ENUM_CONSTANT(FUNC_CEIL);
  2367. BIND_ENUM_CONSTANT(FUNC_FRACT);
  2368. BIND_ENUM_CONSTANT(FUNC_SATURATE);
  2369. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  2370. BIND_ENUM_CONSTANT(FUNC_ACOSH);
  2371. BIND_ENUM_CONSTANT(FUNC_ASINH);
  2372. BIND_ENUM_CONSTANT(FUNC_ATANH);
  2373. BIND_ENUM_CONSTANT(FUNC_DEGREES);
  2374. BIND_ENUM_CONSTANT(FUNC_EXP2);
  2375. BIND_ENUM_CONSTANT(FUNC_INVERSE_SQRT);
  2376. BIND_ENUM_CONSTANT(FUNC_LOG2);
  2377. BIND_ENUM_CONSTANT(FUNC_RADIANS);
  2378. BIND_ENUM_CONSTANT(FUNC_RECIPROCAL);
  2379. BIND_ENUM_CONSTANT(FUNC_ROUNDEVEN);
  2380. BIND_ENUM_CONSTANT(FUNC_TRUNC);
  2381. BIND_ENUM_CONSTANT(FUNC_ONEMINUS);
  2382. BIND_ENUM_CONSTANT(FUNC_MAX);
  2383. }
  2384. VisualShaderNodeFloatFunc::VisualShaderNodeFloatFunc() {
  2385. set_input_port_default_value(0, 0.0);
  2386. }
  2387. ////////////// Int Func
  2388. String VisualShaderNodeIntFunc::get_caption() const {
  2389. return "IntFunc";
  2390. }
  2391. int VisualShaderNodeIntFunc::get_input_port_count() const {
  2392. return 1;
  2393. }
  2394. VisualShaderNodeIntFunc::PortType VisualShaderNodeIntFunc::get_input_port_type(int p_port) const {
  2395. return PORT_TYPE_SCALAR_INT;
  2396. }
  2397. String VisualShaderNodeIntFunc::get_input_port_name(int p_port) const {
  2398. return "";
  2399. }
  2400. int VisualShaderNodeIntFunc::get_output_port_count() const {
  2401. return 1;
  2402. }
  2403. VisualShaderNodeIntFunc::PortType VisualShaderNodeIntFunc::get_output_port_type(int p_port) const {
  2404. return PORT_TYPE_SCALAR_INT;
  2405. }
  2406. String VisualShaderNodeIntFunc::get_output_port_name(int p_port) const {
  2407. return ""; //no output port means the editor will be used as port
  2408. }
  2409. String VisualShaderNodeIntFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2410. static const char *functions[FUNC_MAX] = {
  2411. "abs($)",
  2412. "-($)",
  2413. "sign($)",
  2414. "~($)"
  2415. };
  2416. return " " + p_output_vars[0] + " = " + String(functions[func]).replace("$", p_input_vars[0]) + ";\n";
  2417. }
  2418. void VisualShaderNodeIntFunc::set_function(Function p_func) {
  2419. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  2420. if (func == p_func) {
  2421. return;
  2422. }
  2423. func = p_func;
  2424. emit_changed();
  2425. }
  2426. VisualShaderNodeIntFunc::Function VisualShaderNodeIntFunc::get_function() const {
  2427. return func;
  2428. }
  2429. Vector<StringName> VisualShaderNodeIntFunc::get_editable_properties() const {
  2430. Vector<StringName> props;
  2431. props.push_back("function");
  2432. return props;
  2433. }
  2434. void VisualShaderNodeIntFunc::_bind_methods() {
  2435. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeIntFunc::set_function);
  2436. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeIntFunc::get_function);
  2437. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Abs,Negate,Sign,Bitwise NOT"), "set_function", "get_function");
  2438. BIND_ENUM_CONSTANT(FUNC_ABS);
  2439. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  2440. BIND_ENUM_CONSTANT(FUNC_SIGN);
  2441. BIND_ENUM_CONSTANT(FUNC_BITWISE_NOT);
  2442. BIND_ENUM_CONSTANT(FUNC_MAX);
  2443. }
  2444. VisualShaderNodeIntFunc::VisualShaderNodeIntFunc() {
  2445. set_input_port_default_value(0, 0);
  2446. }
  2447. ////////////// Unsigned Int Func
  2448. String VisualShaderNodeUIntFunc::get_caption() const {
  2449. return "UIntFunc";
  2450. }
  2451. int VisualShaderNodeUIntFunc::get_input_port_count() const {
  2452. return 1;
  2453. }
  2454. VisualShaderNodeUIntFunc::PortType VisualShaderNodeUIntFunc::get_input_port_type(int p_port) const {
  2455. return PORT_TYPE_SCALAR_UINT;
  2456. }
  2457. String VisualShaderNodeUIntFunc::get_input_port_name(int p_port) const {
  2458. return "";
  2459. }
  2460. int VisualShaderNodeUIntFunc::get_output_port_count() const {
  2461. return 1;
  2462. }
  2463. VisualShaderNodeUIntFunc::PortType VisualShaderNodeUIntFunc::get_output_port_type(int p_port) const {
  2464. return PORT_TYPE_SCALAR_UINT;
  2465. }
  2466. String VisualShaderNodeUIntFunc::get_output_port_name(int p_port) const {
  2467. return ""; // No output port means the editor will be used as port.
  2468. }
  2469. String VisualShaderNodeUIntFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2470. static const char *functions[FUNC_MAX] = {
  2471. "-($)",
  2472. "~($)"
  2473. };
  2474. return " " + p_output_vars[0] + " = " + String(functions[func]).replace("$", p_input_vars[0]) + ";\n";
  2475. }
  2476. void VisualShaderNodeUIntFunc::set_function(Function p_func) {
  2477. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  2478. if (func == p_func) {
  2479. return;
  2480. }
  2481. func = p_func;
  2482. emit_changed();
  2483. }
  2484. VisualShaderNodeUIntFunc::Function VisualShaderNodeUIntFunc::get_function() const {
  2485. return func;
  2486. }
  2487. Vector<StringName> VisualShaderNodeUIntFunc::get_editable_properties() const {
  2488. Vector<StringName> props;
  2489. props.push_back("function");
  2490. return props;
  2491. }
  2492. void VisualShaderNodeUIntFunc::_bind_methods() {
  2493. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeUIntFunc::set_function);
  2494. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeUIntFunc::get_function);
  2495. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Negate,Bitwise NOT"), "set_function", "get_function");
  2496. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  2497. BIND_ENUM_CONSTANT(FUNC_BITWISE_NOT);
  2498. BIND_ENUM_CONSTANT(FUNC_MAX);
  2499. }
  2500. VisualShaderNodeUIntFunc::VisualShaderNodeUIntFunc() {
  2501. set_input_port_default_value(0, 0);
  2502. }
  2503. ////////////// Vector Func
  2504. String VisualShaderNodeVectorFunc::get_caption() const {
  2505. return "VectorFunc";
  2506. }
  2507. int VisualShaderNodeVectorFunc::get_input_port_count() const {
  2508. return 1;
  2509. }
  2510. String VisualShaderNodeVectorFunc::get_input_port_name(int p_port) const {
  2511. return "";
  2512. }
  2513. int VisualShaderNodeVectorFunc::get_output_port_count() const {
  2514. return 1;
  2515. }
  2516. String VisualShaderNodeVectorFunc::get_output_port_name(int p_port) const {
  2517. return "result";
  2518. }
  2519. String VisualShaderNodeVectorFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2520. static const char *funcs[FUNC_MAX] = {
  2521. "normalize($)",
  2522. "", // FUNC_SATURATE
  2523. "-($)",
  2524. "1.0 / ($)",
  2525. "abs($)",
  2526. "acos($)",
  2527. "acosh($)",
  2528. "asin($)",
  2529. "asinh($)",
  2530. "atan($)",
  2531. "atanh($)",
  2532. "ceil($)",
  2533. "cos($)",
  2534. "cosh($)",
  2535. "degrees($)",
  2536. "exp($)",
  2537. "exp2($)",
  2538. "floor($)",
  2539. "fract($)",
  2540. "inversesqrt($)",
  2541. "log($)",
  2542. "log2($)",
  2543. "radians($)",
  2544. "round($)",
  2545. "roundEven($)",
  2546. "sign($)",
  2547. "sin($)",
  2548. "sinh($)",
  2549. "sqrt($)",
  2550. "tan($)",
  2551. "tanh($)",
  2552. "trunc($)",
  2553. "" // FUNC_ONEMINUS
  2554. };
  2555. if (func == FUNC_SATURATE) {
  2556. String code;
  2557. if (op_type == OP_TYPE_VECTOR_2D) {
  2558. code = "max(min($, vec2(1.0)), vec2(0.0))";
  2559. } else if (op_type == OP_TYPE_VECTOR_3D) {
  2560. code = "max(min($, vec3(1.0)), vec3(0.0))";
  2561. } else {
  2562. code = "max(min($, vec4(1.0)), vec4(0.0))";
  2563. }
  2564. return " " + p_output_vars[0] + " = " + code.replace("$", p_input_vars[0]) + ";\n";
  2565. }
  2566. if (func == FUNC_ONEMINUS) {
  2567. String code;
  2568. if (op_type == OP_TYPE_VECTOR_2D) {
  2569. code = "vec2(1.0) - $";
  2570. } else if (op_type == OP_TYPE_VECTOR_3D) {
  2571. code = "vec3(1.0) - $";
  2572. } else {
  2573. code = "vec4(1.0) - $";
  2574. }
  2575. return " " + p_output_vars[0] + " = " + code.replace("$", p_input_vars[0]) + ";\n";
  2576. }
  2577. return " " + p_output_vars[0] + " = " + String(funcs[func]).replace("$", p_input_vars[0]) + ";\n";
  2578. }
  2579. void VisualShaderNodeVectorFunc::set_op_type(OpType p_op_type) {
  2580. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  2581. if (op_type == p_op_type) {
  2582. return;
  2583. }
  2584. switch (p_op_type) {
  2585. case OP_TYPE_VECTOR_2D: {
  2586. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  2587. } break;
  2588. case OP_TYPE_VECTOR_3D: {
  2589. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  2590. } break;
  2591. case OP_TYPE_VECTOR_4D: {
  2592. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  2593. } break;
  2594. default:
  2595. break;
  2596. }
  2597. op_type = p_op_type;
  2598. emit_changed();
  2599. }
  2600. void VisualShaderNodeVectorFunc::set_function(Function p_func) {
  2601. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  2602. if (func == p_func) {
  2603. return;
  2604. }
  2605. func = p_func;
  2606. emit_changed();
  2607. }
  2608. VisualShaderNodeVectorFunc::Function VisualShaderNodeVectorFunc::get_function() const {
  2609. return func;
  2610. }
  2611. Vector<StringName> VisualShaderNodeVectorFunc::get_editable_properties() const {
  2612. Vector<StringName> props = VisualShaderNodeVectorBase::get_editable_properties();
  2613. props.push_back("function");
  2614. return props;
  2615. }
  2616. void VisualShaderNodeVectorFunc::_bind_methods() {
  2617. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeVectorFunc::set_function);
  2618. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeVectorFunc::get_function);
  2619. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Normalize,Saturate,Negate,Reciprocal,Abs,ACos,ACosH,ASin,ASinH,ATan,ATanH,Ceil,Cos,CosH,Degrees,Exp,Exp2,Floor,Fract,InverseSqrt,Log,Log2,Radians,Round,RoundEven,Sign,Sin,SinH,Sqrt,Tan,TanH,Trunc,OneMinus"), "set_function", "get_function");
  2620. BIND_ENUM_CONSTANT(FUNC_NORMALIZE);
  2621. BIND_ENUM_CONSTANT(FUNC_SATURATE);
  2622. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  2623. BIND_ENUM_CONSTANT(FUNC_RECIPROCAL);
  2624. BIND_ENUM_CONSTANT(FUNC_ABS);
  2625. BIND_ENUM_CONSTANT(FUNC_ACOS);
  2626. BIND_ENUM_CONSTANT(FUNC_ACOSH);
  2627. BIND_ENUM_CONSTANT(FUNC_ASIN);
  2628. BIND_ENUM_CONSTANT(FUNC_ASINH);
  2629. BIND_ENUM_CONSTANT(FUNC_ATAN);
  2630. BIND_ENUM_CONSTANT(FUNC_ATANH);
  2631. BIND_ENUM_CONSTANT(FUNC_CEIL);
  2632. BIND_ENUM_CONSTANT(FUNC_COS);
  2633. BIND_ENUM_CONSTANT(FUNC_COSH);
  2634. BIND_ENUM_CONSTANT(FUNC_DEGREES);
  2635. BIND_ENUM_CONSTANT(FUNC_EXP);
  2636. BIND_ENUM_CONSTANT(FUNC_EXP2);
  2637. BIND_ENUM_CONSTANT(FUNC_FLOOR);
  2638. BIND_ENUM_CONSTANT(FUNC_FRACT);
  2639. BIND_ENUM_CONSTANT(FUNC_INVERSE_SQRT);
  2640. BIND_ENUM_CONSTANT(FUNC_LOG);
  2641. BIND_ENUM_CONSTANT(FUNC_LOG2);
  2642. BIND_ENUM_CONSTANT(FUNC_RADIANS);
  2643. BIND_ENUM_CONSTANT(FUNC_ROUND);
  2644. BIND_ENUM_CONSTANT(FUNC_ROUNDEVEN);
  2645. BIND_ENUM_CONSTANT(FUNC_SIGN);
  2646. BIND_ENUM_CONSTANT(FUNC_SIN);
  2647. BIND_ENUM_CONSTANT(FUNC_SINH);
  2648. BIND_ENUM_CONSTANT(FUNC_SQRT);
  2649. BIND_ENUM_CONSTANT(FUNC_TAN);
  2650. BIND_ENUM_CONSTANT(FUNC_TANH);
  2651. BIND_ENUM_CONSTANT(FUNC_TRUNC);
  2652. BIND_ENUM_CONSTANT(FUNC_ONEMINUS);
  2653. BIND_ENUM_CONSTANT(FUNC_MAX);
  2654. }
  2655. VisualShaderNodeVectorFunc::VisualShaderNodeVectorFunc() {
  2656. switch (op_type) {
  2657. case OP_TYPE_VECTOR_2D: {
  2658. set_input_port_default_value(0, Vector2());
  2659. } break;
  2660. case OP_TYPE_VECTOR_3D: {
  2661. set_input_port_default_value(0, Vector3());
  2662. } break;
  2663. case OP_TYPE_VECTOR_4D: {
  2664. set_input_port_default_value(0, Quaternion());
  2665. } break;
  2666. default:
  2667. break;
  2668. }
  2669. }
  2670. ////////////// ColorFunc
  2671. String VisualShaderNodeColorFunc::get_caption() const {
  2672. return "ColorFunc";
  2673. }
  2674. int VisualShaderNodeColorFunc::get_input_port_count() const {
  2675. return 1;
  2676. }
  2677. VisualShaderNodeColorFunc::PortType VisualShaderNodeColorFunc::get_input_port_type(int p_port) const {
  2678. return PORT_TYPE_VECTOR_3D;
  2679. }
  2680. String VisualShaderNodeColorFunc::get_input_port_name(int p_port) const {
  2681. return "";
  2682. }
  2683. int VisualShaderNodeColorFunc::get_output_port_count() const {
  2684. return 1;
  2685. }
  2686. VisualShaderNodeColorFunc::PortType VisualShaderNodeColorFunc::get_output_port_type(int p_port) const {
  2687. return PORT_TYPE_VECTOR_3D;
  2688. }
  2689. String VisualShaderNodeColorFunc::get_output_port_name(int p_port) const {
  2690. return "";
  2691. }
  2692. String VisualShaderNodeColorFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2693. String code;
  2694. switch (func) {
  2695. case FUNC_GRAYSCALE:
  2696. code += " {\n";
  2697. code += " vec3 c = " + p_input_vars[0] + ";\n";
  2698. code += " float max1 = max(c.r, c.g);\n";
  2699. code += " float max2 = max(max1, c.b);\n";
  2700. code += " " + p_output_vars[0] + " = vec3(max2, max2, max2);\n";
  2701. code += " }\n";
  2702. break;
  2703. case FUNC_HSV2RGB:
  2704. code += " {\n";
  2705. code += " vec3 c = " + p_input_vars[0] + ";\n";
  2706. code += " vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n";
  2707. code += " vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n";
  2708. code += " " + p_output_vars[0] + " = c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n";
  2709. code += " }\n";
  2710. break;
  2711. case FUNC_RGB2HSV:
  2712. code += " {\n";
  2713. code += " vec3 c = " + p_input_vars[0] + ";\n";
  2714. code += " vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n";
  2715. code += " vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n";
  2716. code += " vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n";
  2717. code += " float d = q.x - min(q.w, q.y);\n";
  2718. code += " float e = 1.0e-10;\n";
  2719. code += " " + p_output_vars[0] + " = vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n";
  2720. code += " }\n";
  2721. break;
  2722. case FUNC_SEPIA:
  2723. code += " {\n";
  2724. code += " vec3 c = " + p_input_vars[0] + ";\n";
  2725. code += " float r = (c.r * .393) + (c.g *.769) + (c.b * .189);\n";
  2726. code += " float g = (c.r * .349) + (c.g *.686) + (c.b * .168);\n";
  2727. code += " float b = (c.r * .272) + (c.g *.534) + (c.b * .131);\n";
  2728. code += " " + p_output_vars[0] + " = vec3(r, g, b);\n";
  2729. code += " }\n";
  2730. break;
  2731. default:
  2732. break;
  2733. }
  2734. return code;
  2735. }
  2736. void VisualShaderNodeColorFunc::set_function(Function p_func) {
  2737. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  2738. if (func == p_func) {
  2739. return;
  2740. }
  2741. func = p_func;
  2742. emit_changed();
  2743. }
  2744. VisualShaderNodeColorFunc::Function VisualShaderNodeColorFunc::get_function() const {
  2745. return func;
  2746. }
  2747. Vector<StringName> VisualShaderNodeColorFunc::get_editable_properties() const {
  2748. Vector<StringName> props;
  2749. props.push_back("function");
  2750. return props;
  2751. }
  2752. void VisualShaderNodeColorFunc::_bind_methods() {
  2753. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeColorFunc::set_function);
  2754. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeColorFunc::get_function);
  2755. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Grayscale,HSV2RGB,RGB2HSV,Sepia"), "set_function", "get_function");
  2756. BIND_ENUM_CONSTANT(FUNC_GRAYSCALE);
  2757. BIND_ENUM_CONSTANT(FUNC_HSV2RGB);
  2758. BIND_ENUM_CONSTANT(FUNC_RGB2HSV);
  2759. BIND_ENUM_CONSTANT(FUNC_SEPIA);
  2760. BIND_ENUM_CONSTANT(FUNC_MAX);
  2761. }
  2762. VisualShaderNodeColorFunc::VisualShaderNodeColorFunc() {
  2763. simple_decl = false;
  2764. set_input_port_default_value(0, Vector3());
  2765. }
  2766. ////////////// Transform Func
  2767. String VisualShaderNodeTransformFunc::get_caption() const {
  2768. return "TransformFunc";
  2769. }
  2770. int VisualShaderNodeTransformFunc::get_input_port_count() const {
  2771. return 1;
  2772. }
  2773. VisualShaderNodeTransformFunc::PortType VisualShaderNodeTransformFunc::get_input_port_type(int p_port) const {
  2774. return PORT_TYPE_TRANSFORM;
  2775. }
  2776. String VisualShaderNodeTransformFunc::get_input_port_name(int p_port) const {
  2777. return "";
  2778. }
  2779. int VisualShaderNodeTransformFunc::get_output_port_count() const {
  2780. return 1;
  2781. }
  2782. VisualShaderNodeTransformFunc::PortType VisualShaderNodeTransformFunc::get_output_port_type(int p_port) const {
  2783. return PORT_TYPE_TRANSFORM;
  2784. }
  2785. String VisualShaderNodeTransformFunc::get_output_port_name(int p_port) const {
  2786. return "";
  2787. }
  2788. String VisualShaderNodeTransformFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2789. static const char *functions[FUNC_MAX] = {
  2790. "inverse($)",
  2791. "transpose($)"
  2792. };
  2793. String code;
  2794. code += " " + p_output_vars[0] + " = " + String(functions[func]).replace("$", p_input_vars[0]) + ";\n";
  2795. return code;
  2796. }
  2797. void VisualShaderNodeTransformFunc::set_function(Function p_func) {
  2798. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  2799. if (func == p_func) {
  2800. return;
  2801. }
  2802. func = p_func;
  2803. emit_changed();
  2804. }
  2805. VisualShaderNodeTransformFunc::Function VisualShaderNodeTransformFunc::get_function() const {
  2806. return func;
  2807. }
  2808. Vector<StringName> VisualShaderNodeTransformFunc::get_editable_properties() const {
  2809. Vector<StringName> props;
  2810. props.push_back("function");
  2811. return props;
  2812. }
  2813. void VisualShaderNodeTransformFunc::_bind_methods() {
  2814. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeTransformFunc::set_function);
  2815. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeTransformFunc::get_function);
  2816. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Inverse,Transpose"), "set_function", "get_function");
  2817. BIND_ENUM_CONSTANT(FUNC_INVERSE);
  2818. BIND_ENUM_CONSTANT(FUNC_TRANSPOSE);
  2819. BIND_ENUM_CONSTANT(FUNC_MAX);
  2820. }
  2821. VisualShaderNodeTransformFunc::VisualShaderNodeTransformFunc() {
  2822. set_input_port_default_value(0, Transform3D());
  2823. }
  2824. ////////////// UV Func
  2825. String VisualShaderNodeUVFunc::get_caption() const {
  2826. return "UVFunc";
  2827. }
  2828. int VisualShaderNodeUVFunc::get_input_port_count() const {
  2829. return 3;
  2830. }
  2831. VisualShaderNodeUVFunc::PortType VisualShaderNodeUVFunc::get_input_port_type(int p_port) const {
  2832. switch (p_port) {
  2833. case 0:
  2834. return PORT_TYPE_VECTOR_2D; // uv
  2835. case 1:
  2836. return PORT_TYPE_VECTOR_2D; // scale
  2837. case 2:
  2838. return PORT_TYPE_VECTOR_2D; // offset & pivot
  2839. default:
  2840. break;
  2841. }
  2842. return PORT_TYPE_SCALAR;
  2843. }
  2844. String VisualShaderNodeUVFunc::get_input_port_name(int p_port) const {
  2845. switch (p_port) {
  2846. case 0:
  2847. return "uv";
  2848. case 1:
  2849. return "scale";
  2850. case 2:
  2851. switch (func) {
  2852. case FUNC_PANNING:
  2853. return "offset";
  2854. case FUNC_SCALING:
  2855. return "pivot";
  2856. default:
  2857. break;
  2858. }
  2859. break;
  2860. default:
  2861. break;
  2862. }
  2863. return "";
  2864. }
  2865. bool VisualShaderNodeUVFunc::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  2866. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  2867. if (p_port == 0) {
  2868. return true;
  2869. }
  2870. }
  2871. return false;
  2872. }
  2873. int VisualShaderNodeUVFunc::get_output_port_count() const {
  2874. return 1;
  2875. }
  2876. VisualShaderNodeUVFunc::PortType VisualShaderNodeUVFunc::get_output_port_type(int p_port) const {
  2877. return PORT_TYPE_VECTOR_2D;
  2878. }
  2879. String VisualShaderNodeUVFunc::get_output_port_name(int p_port) const {
  2880. return "uv";
  2881. }
  2882. bool VisualShaderNodeUVFunc::is_show_prop_names() const {
  2883. return true;
  2884. }
  2885. String VisualShaderNodeUVFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2886. String code;
  2887. String uv;
  2888. if (p_input_vars[0].is_empty()) {
  2889. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  2890. uv = "UV";
  2891. } else {
  2892. uv = "vec2(0.0)";
  2893. }
  2894. } else {
  2895. uv = vformat("%s", p_input_vars[0]);
  2896. }
  2897. String scale = vformat("%s", p_input_vars[1]);
  2898. String offset_pivot = vformat("%s", p_input_vars[2]);
  2899. switch (func) {
  2900. case FUNC_PANNING: {
  2901. code += vformat(" %s = %s * %s + %s;\n", p_output_vars[0], offset_pivot, scale, uv);
  2902. } break;
  2903. case FUNC_SCALING: {
  2904. code += vformat(" %s = (%s - %s) * %s + %s;\n", p_output_vars[0], uv, offset_pivot, scale, offset_pivot);
  2905. } break;
  2906. default:
  2907. break;
  2908. }
  2909. return code;
  2910. }
  2911. void VisualShaderNodeUVFunc::set_function(VisualShaderNodeUVFunc::Function p_func) {
  2912. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  2913. if (func == p_func) {
  2914. return;
  2915. }
  2916. if (p_func == FUNC_PANNING) {
  2917. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2)); // offset
  2918. } else { // FUNC_SCALING
  2919. set_input_port_default_value(2, Vector2(0.5, 0.5), get_input_port_default_value(2)); // pivot
  2920. }
  2921. func = p_func;
  2922. emit_changed();
  2923. }
  2924. VisualShaderNodeUVFunc::Function VisualShaderNodeUVFunc::get_function() const {
  2925. return func;
  2926. }
  2927. Vector<StringName> VisualShaderNodeUVFunc::get_editable_properties() const {
  2928. Vector<StringName> props;
  2929. props.push_back("function");
  2930. return props;
  2931. }
  2932. void VisualShaderNodeUVFunc::_bind_methods() {
  2933. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeUVFunc::set_function);
  2934. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeUVFunc::get_function);
  2935. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Panning,Scaling"), "set_function", "get_function");
  2936. BIND_ENUM_CONSTANT(FUNC_PANNING);
  2937. BIND_ENUM_CONSTANT(FUNC_SCALING);
  2938. BIND_ENUM_CONSTANT(FUNC_MAX);
  2939. }
  2940. VisualShaderNodeUVFunc::VisualShaderNodeUVFunc() {
  2941. set_input_port_default_value(1, Vector2(1.0, 1.0)); // scale
  2942. set_input_port_default_value(2, Vector2()); // offset
  2943. }
  2944. ////////////// UV PolarCoord
  2945. String VisualShaderNodeUVPolarCoord::get_caption() const {
  2946. return "UVPolarCoord";
  2947. }
  2948. int VisualShaderNodeUVPolarCoord::get_input_port_count() const {
  2949. return 4;
  2950. }
  2951. VisualShaderNodeUVPolarCoord::PortType VisualShaderNodeUVPolarCoord::get_input_port_type(int p_port) const {
  2952. switch (p_port) {
  2953. case 0:
  2954. return PORT_TYPE_VECTOR_2D; // uv
  2955. case 1:
  2956. return PORT_TYPE_VECTOR_2D; // center
  2957. case 2:
  2958. return PORT_TYPE_SCALAR; // zoom
  2959. case 3:
  2960. return PORT_TYPE_SCALAR; // repeat
  2961. default:
  2962. break;
  2963. }
  2964. return PORT_TYPE_SCALAR;
  2965. }
  2966. String VisualShaderNodeUVPolarCoord::get_input_port_name(int p_port) const {
  2967. switch (p_port) {
  2968. case 0:
  2969. return "uv";
  2970. case 1:
  2971. return "scale";
  2972. case 2:
  2973. return "zoom strength";
  2974. case 3:
  2975. return "repeat";
  2976. default:
  2977. break;
  2978. }
  2979. return "";
  2980. }
  2981. bool VisualShaderNodeUVPolarCoord::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  2982. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  2983. if (p_port == 0) {
  2984. return true;
  2985. }
  2986. }
  2987. return false;
  2988. }
  2989. int VisualShaderNodeUVPolarCoord::get_output_port_count() const {
  2990. return 1;
  2991. }
  2992. VisualShaderNodeUVPolarCoord::PortType VisualShaderNodeUVPolarCoord::get_output_port_type(int p_port) const {
  2993. return PORT_TYPE_VECTOR_2D;
  2994. }
  2995. String VisualShaderNodeUVPolarCoord::get_output_port_name(int p_port) const {
  2996. return "uv";
  2997. }
  2998. String VisualShaderNodeUVPolarCoord::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2999. String code;
  3000. code += " {\n";
  3001. String uv;
  3002. if (p_input_vars[0].is_empty()) {
  3003. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  3004. uv = "UV";
  3005. } else {
  3006. uv = "vec2(0.0)";
  3007. }
  3008. } else {
  3009. uv = vformat("%s", p_input_vars[0]);
  3010. }
  3011. String center = vformat("%s", p_input_vars[1]);
  3012. String zoom = vformat("%s", p_input_vars[2]);
  3013. String repeat = vformat("%s", p_input_vars[3]);
  3014. if (p_mode == Shader::MODE_CANVAS_ITEM) {
  3015. code += vformat(" vec2 __dir = %s - %s;\n", uv, center);
  3016. code += " float __radius = length(__dir) * 2.0;\n";
  3017. code += " float __angle = atan(__dir.y, __dir.x) * 1.0 / (PI * 2.0);\n";
  3018. code += vformat(" %s = mod(vec2(__radius * %s, __angle * %s), 1.0);\n", p_output_vars[0], zoom, repeat);
  3019. } else {
  3020. code += vformat(" vec2 __dir = %s - %s;\n", uv, center);
  3021. code += " float __radius = length(__dir) * 2.0;\n";
  3022. code += " float __angle = atan(__dir.y, __dir.x) * 1.0 / (PI * 2.0);\n";
  3023. code += vformat(" %s = vec2(__radius * %s, __angle * %s);\n", p_output_vars[0], zoom, repeat);
  3024. }
  3025. code += " }\n";
  3026. return code;
  3027. }
  3028. VisualShaderNodeUVPolarCoord::VisualShaderNodeUVPolarCoord() {
  3029. set_input_port_default_value(1, Vector2(0.5, 0.5)); // center
  3030. set_input_port_default_value(2, 1.0); // zoom
  3031. set_input_port_default_value(3, 1.0); // repeat
  3032. simple_decl = false;
  3033. }
  3034. ////////////// Dot Product
  3035. String VisualShaderNodeDotProduct::get_caption() const {
  3036. return "DotProduct";
  3037. }
  3038. int VisualShaderNodeDotProduct::get_input_port_count() const {
  3039. return 2;
  3040. }
  3041. VisualShaderNodeDotProduct::PortType VisualShaderNodeDotProduct::get_input_port_type(int p_port) const {
  3042. return PORT_TYPE_VECTOR_3D;
  3043. }
  3044. String VisualShaderNodeDotProduct::get_input_port_name(int p_port) const {
  3045. return p_port == 0 ? "a" : "b";
  3046. }
  3047. int VisualShaderNodeDotProduct::get_output_port_count() const {
  3048. return 1;
  3049. }
  3050. VisualShaderNodeDotProduct::PortType VisualShaderNodeDotProduct::get_output_port_type(int p_port) const {
  3051. return PORT_TYPE_SCALAR;
  3052. }
  3053. String VisualShaderNodeDotProduct::get_output_port_name(int p_port) const {
  3054. return "dot";
  3055. }
  3056. String VisualShaderNodeDotProduct::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3057. return " " + p_output_vars[0] + " = dot(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  3058. }
  3059. VisualShaderNodeDotProduct::VisualShaderNodeDotProduct() {
  3060. set_input_port_default_value(0, Vector3());
  3061. set_input_port_default_value(1, Vector3());
  3062. }
  3063. ////////////// Vector Len
  3064. String VisualShaderNodeVectorLen::get_caption() const {
  3065. return "VectorLen";
  3066. }
  3067. int VisualShaderNodeVectorLen::get_input_port_count() const {
  3068. return 1;
  3069. }
  3070. String VisualShaderNodeVectorLen::get_input_port_name(int p_port) const {
  3071. return "";
  3072. }
  3073. int VisualShaderNodeVectorLen::get_output_port_count() const {
  3074. return 1;
  3075. }
  3076. VisualShaderNodeVectorLen::PortType VisualShaderNodeVectorLen::get_output_port_type(int p_port) const {
  3077. return PORT_TYPE_SCALAR;
  3078. }
  3079. String VisualShaderNodeVectorLen::get_output_port_name(int p_port) const {
  3080. return "length";
  3081. }
  3082. void VisualShaderNodeVectorLen::set_op_type(OpType p_op_type) {
  3083. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  3084. if (op_type == p_op_type) {
  3085. return;
  3086. }
  3087. switch (p_op_type) {
  3088. case OP_TYPE_VECTOR_2D: {
  3089. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  3090. } break;
  3091. case OP_TYPE_VECTOR_3D: {
  3092. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  3093. } break;
  3094. case OP_TYPE_VECTOR_4D: {
  3095. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  3096. } break;
  3097. default:
  3098. break;
  3099. }
  3100. op_type = p_op_type;
  3101. emit_changed();
  3102. }
  3103. String VisualShaderNodeVectorLen::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3104. return " " + p_output_vars[0] + " = length(" + p_input_vars[0] + ");\n";
  3105. }
  3106. VisualShaderNodeVectorLen::VisualShaderNodeVectorLen() {
  3107. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  3108. }
  3109. ////////////// Determinant
  3110. String VisualShaderNodeDeterminant::get_caption() const {
  3111. return "Determinant";
  3112. }
  3113. int VisualShaderNodeDeterminant::get_input_port_count() const {
  3114. return 1;
  3115. }
  3116. VisualShaderNodeDeterminant::PortType VisualShaderNodeDeterminant::get_input_port_type(int p_port) const {
  3117. return PORT_TYPE_TRANSFORM;
  3118. }
  3119. String VisualShaderNodeDeterminant::get_input_port_name(int p_port) const {
  3120. return "";
  3121. }
  3122. int VisualShaderNodeDeterminant::get_output_port_count() const {
  3123. return 1;
  3124. }
  3125. VisualShaderNodeDeterminant::PortType VisualShaderNodeDeterminant::get_output_port_type(int p_port) const {
  3126. return PORT_TYPE_SCALAR;
  3127. }
  3128. String VisualShaderNodeDeterminant::get_output_port_name(int p_port) const {
  3129. return "";
  3130. }
  3131. String VisualShaderNodeDeterminant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3132. return " " + p_output_vars[0] + " = determinant(" + p_input_vars[0] + ");\n";
  3133. }
  3134. VisualShaderNodeDeterminant::VisualShaderNodeDeterminant() {
  3135. set_input_port_default_value(0, Transform3D());
  3136. }
  3137. ////////////// Derivative Function
  3138. String VisualShaderNodeDerivativeFunc::get_caption() const {
  3139. return "DerivativeFunc";
  3140. }
  3141. int VisualShaderNodeDerivativeFunc::get_input_port_count() const {
  3142. return 1;
  3143. }
  3144. VisualShaderNodeDerivativeFunc::PortType VisualShaderNodeDerivativeFunc::get_input_port_type(int p_port) const {
  3145. switch (op_type) {
  3146. case OP_TYPE_VECTOR_2D:
  3147. return PORT_TYPE_VECTOR_2D;
  3148. case OP_TYPE_VECTOR_3D:
  3149. return PORT_TYPE_VECTOR_3D;
  3150. case OP_TYPE_VECTOR_4D:
  3151. return PORT_TYPE_VECTOR_4D;
  3152. default:
  3153. break;
  3154. }
  3155. return PORT_TYPE_SCALAR;
  3156. }
  3157. String VisualShaderNodeDerivativeFunc::get_input_port_name(int p_port) const {
  3158. return "p";
  3159. }
  3160. int VisualShaderNodeDerivativeFunc::get_output_port_count() const {
  3161. return 1;
  3162. }
  3163. VisualShaderNodeDerivativeFunc::PortType VisualShaderNodeDerivativeFunc::get_output_port_type(int p_port) const {
  3164. switch (op_type) {
  3165. case OP_TYPE_VECTOR_2D:
  3166. return PORT_TYPE_VECTOR_2D;
  3167. case OP_TYPE_VECTOR_3D:
  3168. return PORT_TYPE_VECTOR_3D;
  3169. case OP_TYPE_VECTOR_4D:
  3170. return PORT_TYPE_VECTOR_4D;
  3171. default:
  3172. break;
  3173. }
  3174. return PORT_TYPE_SCALAR;
  3175. }
  3176. String VisualShaderNodeDerivativeFunc::get_output_port_name(int p_port) const {
  3177. return "result";
  3178. }
  3179. String VisualShaderNodeDerivativeFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3180. static const char *functions[FUNC_MAX] = {
  3181. "fwidth$($)",
  3182. "dFdx$($)",
  3183. "dFdy$($)"
  3184. };
  3185. static const char *precisions[PRECISION_MAX] = {
  3186. "",
  3187. "Coarse",
  3188. "Fine"
  3189. };
  3190. String code;
  3191. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  3192. code += " " + p_output_vars[0] + " = " + String(functions[func]).replace_first("$", "").replace_first("$", p_input_vars[0]) + ";\n";
  3193. return code;
  3194. }
  3195. code += " " + p_output_vars[0] + " = " + String(functions[func]).replace_first("$", String(precisions[precision])).replace_first("$", p_input_vars[0]) + ";\n";
  3196. return code;
  3197. }
  3198. String VisualShaderNodeDerivativeFunc::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  3199. if (precision != PRECISION_NONE && OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  3200. String precision_str;
  3201. switch (precision) {
  3202. case PRECISION_COARSE: {
  3203. precision_str = "Coarse";
  3204. } break;
  3205. case PRECISION_FINE: {
  3206. precision_str = "Fine";
  3207. } break;
  3208. default: {
  3209. } break;
  3210. }
  3211. return vformat(RTR("`%s` precision mode is not available for `gl_compatibility` profile.\nReverted to `None` precision."), precision_str);
  3212. }
  3213. return String();
  3214. }
  3215. void VisualShaderNodeDerivativeFunc::set_op_type(OpType p_op_type) {
  3216. ERR_FAIL_INDEX((int)p_op_type, int(OP_TYPE_MAX));
  3217. if (op_type == p_op_type) {
  3218. return;
  3219. }
  3220. switch (p_op_type) {
  3221. case OP_TYPE_SCALAR: {
  3222. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  3223. } break;
  3224. case OP_TYPE_VECTOR_2D: {
  3225. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  3226. } break;
  3227. case OP_TYPE_VECTOR_3D: {
  3228. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  3229. } break;
  3230. case OP_TYPE_VECTOR_4D: {
  3231. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  3232. } break;
  3233. default:
  3234. break;
  3235. }
  3236. op_type = p_op_type;
  3237. emit_changed();
  3238. }
  3239. VisualShaderNodeDerivativeFunc::OpType VisualShaderNodeDerivativeFunc::get_op_type() const {
  3240. return op_type;
  3241. }
  3242. void VisualShaderNodeDerivativeFunc::set_function(Function p_func) {
  3243. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  3244. if (func == p_func) {
  3245. return;
  3246. }
  3247. func = p_func;
  3248. emit_changed();
  3249. }
  3250. VisualShaderNodeDerivativeFunc::Function VisualShaderNodeDerivativeFunc::get_function() const {
  3251. return func;
  3252. }
  3253. void VisualShaderNodeDerivativeFunc::set_precision(Precision p_precision) {
  3254. ERR_FAIL_INDEX(int(p_precision), int(PRECISION_MAX));
  3255. if (precision == p_precision) {
  3256. return;
  3257. }
  3258. precision = p_precision;
  3259. emit_changed();
  3260. }
  3261. VisualShaderNodeDerivativeFunc::Precision VisualShaderNodeDerivativeFunc::get_precision() const {
  3262. return precision;
  3263. }
  3264. Vector<StringName> VisualShaderNodeDerivativeFunc::get_editable_properties() const {
  3265. Vector<StringName> props;
  3266. props.push_back("op_type");
  3267. props.push_back("function");
  3268. props.push_back("precision");
  3269. return props;
  3270. }
  3271. void VisualShaderNodeDerivativeFunc::_bind_methods() {
  3272. ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeDerivativeFunc::set_op_type);
  3273. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeDerivativeFunc::get_op_type);
  3274. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeDerivativeFunc::set_function);
  3275. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeDerivativeFunc::get_function);
  3276. ClassDB::bind_method(D_METHOD("set_precision", "precision"), &VisualShaderNodeDerivativeFunc::set_precision);
  3277. ClassDB::bind_method(D_METHOD("get_precision"), &VisualShaderNodeDerivativeFunc::get_precision);
  3278. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector3,Vector4"), "set_op_type", "get_op_type");
  3279. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Sum,X,Y"), "set_function", "get_function");
  3280. ADD_PROPERTY(PropertyInfo(Variant::INT, "precision", PROPERTY_HINT_ENUM, "None,Coarse,Fine"), "set_precision", "get_precision");
  3281. BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
  3282. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  3283. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  3284. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  3285. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  3286. BIND_ENUM_CONSTANT(FUNC_SUM);
  3287. BIND_ENUM_CONSTANT(FUNC_X);
  3288. BIND_ENUM_CONSTANT(FUNC_Y);
  3289. BIND_ENUM_CONSTANT(FUNC_MAX);
  3290. BIND_ENUM_CONSTANT(PRECISION_NONE);
  3291. BIND_ENUM_CONSTANT(PRECISION_COARSE);
  3292. BIND_ENUM_CONSTANT(PRECISION_FINE);
  3293. BIND_ENUM_CONSTANT(PRECISION_MAX);
  3294. }
  3295. VisualShaderNodeDerivativeFunc::VisualShaderNodeDerivativeFunc() {
  3296. set_input_port_default_value(0, 0.0);
  3297. }
  3298. ////////////// Clamp
  3299. String VisualShaderNodeClamp::get_caption() const {
  3300. return "Clamp";
  3301. }
  3302. int VisualShaderNodeClamp::get_input_port_count() const {
  3303. return 3;
  3304. }
  3305. VisualShaderNodeClamp::PortType VisualShaderNodeClamp::get_input_port_type(int p_port) const {
  3306. switch (op_type) {
  3307. case OP_TYPE_INT:
  3308. return PORT_TYPE_SCALAR_INT;
  3309. case OP_TYPE_UINT:
  3310. return PORT_TYPE_SCALAR_UINT;
  3311. case OP_TYPE_VECTOR_2D:
  3312. return PORT_TYPE_VECTOR_2D;
  3313. case OP_TYPE_VECTOR_3D:
  3314. return PORT_TYPE_VECTOR_3D;
  3315. case OP_TYPE_VECTOR_4D:
  3316. return PORT_TYPE_VECTOR_4D;
  3317. default:
  3318. break;
  3319. }
  3320. return PORT_TYPE_SCALAR;
  3321. }
  3322. String VisualShaderNodeClamp::get_input_port_name(int p_port) const {
  3323. if (p_port == 0) {
  3324. return "";
  3325. } else if (p_port == 1) {
  3326. return "min";
  3327. } else if (p_port == 2) {
  3328. return "max";
  3329. }
  3330. return "";
  3331. }
  3332. int VisualShaderNodeClamp::get_output_port_count() const {
  3333. return 1;
  3334. }
  3335. VisualShaderNodeClamp::PortType VisualShaderNodeClamp::get_output_port_type(int p_port) const {
  3336. switch (op_type) {
  3337. case OP_TYPE_INT:
  3338. return PORT_TYPE_SCALAR_INT;
  3339. case OP_TYPE_UINT:
  3340. return PORT_TYPE_SCALAR_UINT;
  3341. case OP_TYPE_VECTOR_2D:
  3342. return PORT_TYPE_VECTOR_2D;
  3343. case OP_TYPE_VECTOR_3D:
  3344. return PORT_TYPE_VECTOR_3D;
  3345. case OP_TYPE_VECTOR_4D:
  3346. return PORT_TYPE_VECTOR_4D;
  3347. default:
  3348. break;
  3349. }
  3350. return PORT_TYPE_SCALAR;
  3351. }
  3352. String VisualShaderNodeClamp::get_output_port_name(int p_port) const {
  3353. return "";
  3354. }
  3355. String VisualShaderNodeClamp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3356. return " " + p_output_vars[0] + " = clamp(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  3357. }
  3358. void VisualShaderNodeClamp::set_op_type(OpType p_op_type) {
  3359. ERR_FAIL_INDEX((int)p_op_type, int(OP_TYPE_MAX));
  3360. if (op_type == p_op_type) {
  3361. return;
  3362. }
  3363. switch (p_op_type) {
  3364. case OP_TYPE_FLOAT:
  3365. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  3366. set_input_port_default_value(1, 0.0, get_input_port_default_value(1));
  3367. set_input_port_default_value(2, 0.0, get_input_port_default_value(2));
  3368. break;
  3369. case OP_TYPE_UINT:
  3370. case OP_TYPE_INT:
  3371. set_input_port_default_value(0, 0, get_input_port_default_value(0));
  3372. set_input_port_default_value(1, 0, get_input_port_default_value(1));
  3373. set_input_port_default_value(2, 0, get_input_port_default_value(2));
  3374. break;
  3375. case OP_TYPE_VECTOR_2D:
  3376. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  3377. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  3378. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2));
  3379. break;
  3380. case OP_TYPE_VECTOR_3D:
  3381. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  3382. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  3383. set_input_port_default_value(2, Vector3(), get_input_port_default_value(2));
  3384. break;
  3385. case OP_TYPE_VECTOR_4D:
  3386. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  3387. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  3388. set_input_port_default_value(2, Quaternion(), get_input_port_default_value(2));
  3389. break;
  3390. default:
  3391. break;
  3392. }
  3393. op_type = p_op_type;
  3394. emit_changed();
  3395. }
  3396. VisualShaderNodeClamp::OpType VisualShaderNodeClamp::get_op_type() const {
  3397. return op_type;
  3398. }
  3399. Vector<StringName> VisualShaderNodeClamp::get_editable_properties() const {
  3400. Vector<StringName> props;
  3401. props.push_back("op_type");
  3402. return props;
  3403. }
  3404. void VisualShaderNodeClamp::_bind_methods() {
  3405. ClassDB::bind_method(D_METHOD("set_op_type", "op_type"), &VisualShaderNodeClamp::set_op_type);
  3406. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeClamp::get_op_type);
  3407. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Float,Int,UInt,Vector2,Vector3,Vector4"), "set_op_type", "get_op_type");
  3408. BIND_ENUM_CONSTANT(OP_TYPE_FLOAT);
  3409. BIND_ENUM_CONSTANT(OP_TYPE_INT);
  3410. BIND_ENUM_CONSTANT(OP_TYPE_UINT);
  3411. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  3412. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  3413. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  3414. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  3415. }
  3416. VisualShaderNodeClamp::VisualShaderNodeClamp() {
  3417. set_input_port_default_value(0, 0.0);
  3418. set_input_port_default_value(1, 0.0);
  3419. set_input_port_default_value(2, 1.0);
  3420. }
  3421. ////////////// FaceForward
  3422. String VisualShaderNodeFaceForward::get_caption() const {
  3423. return "FaceForward";
  3424. }
  3425. int VisualShaderNodeFaceForward::get_input_port_count() const {
  3426. return 3;
  3427. }
  3428. String VisualShaderNodeFaceForward::get_input_port_name(int p_port) const {
  3429. switch (p_port) {
  3430. case 0:
  3431. return "N";
  3432. case 1:
  3433. return "I";
  3434. case 2:
  3435. return "Nref";
  3436. default:
  3437. return "";
  3438. }
  3439. }
  3440. int VisualShaderNodeFaceForward::get_output_port_count() const {
  3441. return 1;
  3442. }
  3443. String VisualShaderNodeFaceForward::get_output_port_name(int p_port) const {
  3444. return "";
  3445. }
  3446. void VisualShaderNodeFaceForward::set_op_type(OpType p_op_type) {
  3447. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  3448. if (op_type == p_op_type) {
  3449. return;
  3450. }
  3451. switch (p_op_type) {
  3452. case OP_TYPE_VECTOR_2D: {
  3453. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  3454. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  3455. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2));
  3456. } break;
  3457. case OP_TYPE_VECTOR_3D: {
  3458. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  3459. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  3460. set_input_port_default_value(2, Vector3(), get_input_port_default_value(2));
  3461. } break;
  3462. case OP_TYPE_VECTOR_4D: {
  3463. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  3464. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  3465. set_input_port_default_value(2, Quaternion(), get_input_port_default_value(2));
  3466. } break;
  3467. default:
  3468. break;
  3469. }
  3470. op_type = p_op_type;
  3471. emit_changed();
  3472. }
  3473. String VisualShaderNodeFaceForward::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3474. return " " + p_output_vars[0] + " = faceforward(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  3475. }
  3476. VisualShaderNodeFaceForward::VisualShaderNodeFaceForward() {
  3477. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  3478. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  3479. set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0));
  3480. }
  3481. ////////////// Outer Product
  3482. String VisualShaderNodeOuterProduct::get_caption() const {
  3483. return "OuterProduct";
  3484. }
  3485. int VisualShaderNodeOuterProduct::get_input_port_count() const {
  3486. return 2;
  3487. }
  3488. VisualShaderNodeOuterProduct::PortType VisualShaderNodeOuterProduct::get_input_port_type(int p_port) const {
  3489. return PORT_TYPE_VECTOR_3D;
  3490. }
  3491. String VisualShaderNodeOuterProduct::get_input_port_name(int p_port) const {
  3492. switch (p_port) {
  3493. case 0:
  3494. return "c";
  3495. case 1:
  3496. return "r";
  3497. default:
  3498. return "";
  3499. }
  3500. }
  3501. int VisualShaderNodeOuterProduct::get_output_port_count() const {
  3502. return 1;
  3503. }
  3504. VisualShaderNodeOuterProduct::PortType VisualShaderNodeOuterProduct::get_output_port_type(int p_port) const {
  3505. return PORT_TYPE_TRANSFORM;
  3506. }
  3507. String VisualShaderNodeOuterProduct::get_output_port_name(int p_port) const {
  3508. return "";
  3509. }
  3510. String VisualShaderNodeOuterProduct::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3511. return " " + p_output_vars[0] + " = outerProduct(vec4(" + p_input_vars[0] + ", 0.0), vec4(" + p_input_vars[1] + ", 0.0));\n";
  3512. }
  3513. VisualShaderNodeOuterProduct::VisualShaderNodeOuterProduct() {
  3514. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  3515. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  3516. }
  3517. ////////////// Step
  3518. String VisualShaderNodeStep::get_caption() const {
  3519. return "Step";
  3520. }
  3521. int VisualShaderNodeStep::get_input_port_count() const {
  3522. return 2;
  3523. }
  3524. VisualShaderNodeStep::PortType VisualShaderNodeStep::get_input_port_type(int p_port) const {
  3525. switch (op_type) {
  3526. case OP_TYPE_VECTOR_2D:
  3527. return PORT_TYPE_VECTOR_2D;
  3528. case OP_TYPE_VECTOR_2D_SCALAR:
  3529. if (p_port == 1) {
  3530. return PORT_TYPE_VECTOR_2D;
  3531. }
  3532. break;
  3533. case OP_TYPE_VECTOR_3D:
  3534. return PORT_TYPE_VECTOR_3D;
  3535. case OP_TYPE_VECTOR_3D_SCALAR:
  3536. if (p_port == 1) {
  3537. return PORT_TYPE_VECTOR_3D;
  3538. }
  3539. break;
  3540. case OP_TYPE_VECTOR_4D:
  3541. return PORT_TYPE_VECTOR_4D;
  3542. case OP_TYPE_VECTOR_4D_SCALAR:
  3543. if (p_port == 1) {
  3544. return PORT_TYPE_VECTOR_4D;
  3545. }
  3546. break;
  3547. default:
  3548. break;
  3549. }
  3550. return PORT_TYPE_SCALAR;
  3551. }
  3552. String VisualShaderNodeStep::get_input_port_name(int p_port) const {
  3553. switch (p_port) {
  3554. case 0:
  3555. return "edge";
  3556. case 1:
  3557. return "x";
  3558. }
  3559. return String();
  3560. }
  3561. int VisualShaderNodeStep::get_default_input_port(PortType p_type) const {
  3562. return 1;
  3563. }
  3564. int VisualShaderNodeStep::get_output_port_count() const {
  3565. return 1;
  3566. }
  3567. VisualShaderNodeStep::PortType VisualShaderNodeStep::get_output_port_type(int p_port) const {
  3568. switch (op_type) {
  3569. case OP_TYPE_VECTOR_2D:
  3570. return PORT_TYPE_VECTOR_2D;
  3571. case OP_TYPE_VECTOR_2D_SCALAR:
  3572. return PORT_TYPE_VECTOR_2D;
  3573. case OP_TYPE_VECTOR_3D:
  3574. return PORT_TYPE_VECTOR_3D;
  3575. case OP_TYPE_VECTOR_3D_SCALAR:
  3576. return PORT_TYPE_VECTOR_3D;
  3577. case OP_TYPE_VECTOR_4D:
  3578. return PORT_TYPE_VECTOR_4D;
  3579. case OP_TYPE_VECTOR_4D_SCALAR:
  3580. return PORT_TYPE_VECTOR_4D;
  3581. default:
  3582. break;
  3583. }
  3584. return PORT_TYPE_SCALAR;
  3585. }
  3586. String VisualShaderNodeStep::get_output_port_name(int p_port) const {
  3587. return "";
  3588. }
  3589. void VisualShaderNodeStep::set_op_type(OpType p_op_type) {
  3590. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  3591. if (op_type == p_op_type) {
  3592. return;
  3593. }
  3594. switch (p_op_type) {
  3595. case OP_TYPE_SCALAR: {
  3596. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  3597. set_input_port_default_value(1, 0.0, get_input_port_default_value(1));
  3598. } break;
  3599. case OP_TYPE_VECTOR_2D: {
  3600. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  3601. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  3602. } break;
  3603. case OP_TYPE_VECTOR_2D_SCALAR: {
  3604. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  3605. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  3606. } break;
  3607. case OP_TYPE_VECTOR_3D: {
  3608. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  3609. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  3610. } break;
  3611. case OP_TYPE_VECTOR_3D_SCALAR: {
  3612. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  3613. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  3614. } break;
  3615. case OP_TYPE_VECTOR_4D: {
  3616. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  3617. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  3618. } break;
  3619. case OP_TYPE_VECTOR_4D_SCALAR: {
  3620. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  3621. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  3622. } break;
  3623. default:
  3624. break;
  3625. }
  3626. op_type = p_op_type;
  3627. emit_changed();
  3628. }
  3629. VisualShaderNodeStep::OpType VisualShaderNodeStep::get_op_type() const {
  3630. return op_type;
  3631. }
  3632. String VisualShaderNodeStep::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3633. return " " + p_output_vars[0] + " = step(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  3634. }
  3635. Vector<StringName> VisualShaderNodeStep::get_editable_properties() const {
  3636. Vector<StringName> props;
  3637. props.push_back("op_type");
  3638. return props;
  3639. }
  3640. void VisualShaderNodeStep::_bind_methods() {
  3641. ClassDB::bind_method(D_METHOD("set_op_type", "op_type"), &VisualShaderNodeStep::set_op_type);
  3642. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeStep::get_op_type);
  3643. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector2Scalar,Vector3,Vector3Scalar,Vector4,Vector4Scalar"), "set_op_type", "get_op_type");
  3644. BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
  3645. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  3646. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D_SCALAR);
  3647. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  3648. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D_SCALAR);
  3649. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  3650. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D_SCALAR);
  3651. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  3652. }
  3653. VisualShaderNodeStep::VisualShaderNodeStep() {
  3654. set_input_port_default_value(0, 0.0);
  3655. set_input_port_default_value(1, 0.0);
  3656. }
  3657. ////////////// SmoothStep
  3658. String VisualShaderNodeSmoothStep::get_caption() const {
  3659. return "SmoothStep";
  3660. }
  3661. int VisualShaderNodeSmoothStep::get_input_port_count() const {
  3662. return 3;
  3663. }
  3664. VisualShaderNodeSmoothStep::PortType VisualShaderNodeSmoothStep::get_input_port_type(int p_port) const {
  3665. switch (op_type) {
  3666. case OP_TYPE_VECTOR_2D:
  3667. return PORT_TYPE_VECTOR_2D;
  3668. case OP_TYPE_VECTOR_2D_SCALAR:
  3669. if (p_port == 2) {
  3670. return PORT_TYPE_VECTOR_2D; // x
  3671. }
  3672. break;
  3673. case OP_TYPE_VECTOR_3D:
  3674. return PORT_TYPE_VECTOR_3D;
  3675. case OP_TYPE_VECTOR_3D_SCALAR:
  3676. if (p_port == 2) {
  3677. return PORT_TYPE_VECTOR_3D; // x
  3678. }
  3679. break;
  3680. case OP_TYPE_VECTOR_4D:
  3681. return PORT_TYPE_VECTOR_4D;
  3682. case OP_TYPE_VECTOR_4D_SCALAR:
  3683. if (p_port == 2) {
  3684. return PORT_TYPE_VECTOR_4D; // x
  3685. }
  3686. break;
  3687. default:
  3688. break;
  3689. }
  3690. return PORT_TYPE_SCALAR;
  3691. }
  3692. String VisualShaderNodeSmoothStep::get_input_port_name(int p_port) const {
  3693. switch (p_port) {
  3694. case 0:
  3695. return "edge0";
  3696. case 1:
  3697. return "edge1";
  3698. case 2:
  3699. return "x";
  3700. }
  3701. return String();
  3702. }
  3703. int VisualShaderNodeSmoothStep::get_default_input_port(PortType p_type) const {
  3704. return 2;
  3705. }
  3706. int VisualShaderNodeSmoothStep::get_output_port_count() const {
  3707. return 1;
  3708. }
  3709. VisualShaderNodeSmoothStep::PortType VisualShaderNodeSmoothStep::get_output_port_type(int p_port) const {
  3710. switch (op_type) {
  3711. case OP_TYPE_VECTOR_2D:
  3712. return PORT_TYPE_VECTOR_2D;
  3713. case OP_TYPE_VECTOR_2D_SCALAR:
  3714. return PORT_TYPE_VECTOR_2D;
  3715. case OP_TYPE_VECTOR_3D:
  3716. return PORT_TYPE_VECTOR_3D;
  3717. case OP_TYPE_VECTOR_3D_SCALAR:
  3718. return PORT_TYPE_VECTOR_3D;
  3719. case OP_TYPE_VECTOR_4D:
  3720. return PORT_TYPE_VECTOR_4D;
  3721. case OP_TYPE_VECTOR_4D_SCALAR:
  3722. return PORT_TYPE_VECTOR_4D;
  3723. default:
  3724. break;
  3725. }
  3726. return PORT_TYPE_SCALAR;
  3727. }
  3728. String VisualShaderNodeSmoothStep::get_output_port_name(int p_port) const {
  3729. return "";
  3730. }
  3731. void VisualShaderNodeSmoothStep::set_op_type(OpType p_op_type) {
  3732. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  3733. if (op_type == p_op_type) {
  3734. return;
  3735. }
  3736. switch (p_op_type) {
  3737. case OP_TYPE_SCALAR:
  3738. set_input_port_default_value(0, 0.0, get_input_port_default_value(0)); // edge0
  3739. set_input_port_default_value(1, 0.0, get_input_port_default_value(1)); // edge1
  3740. set_input_port_default_value(2, 0.0, get_input_port_default_value(2)); // x
  3741. break;
  3742. case OP_TYPE_VECTOR_2D:
  3743. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0)); // edge0
  3744. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1)); // edge1
  3745. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2)); // x
  3746. break;
  3747. case OP_TYPE_VECTOR_2D_SCALAR:
  3748. set_input_port_default_value(0, 0.0, get_input_port_default_value(0)); // edge0
  3749. set_input_port_default_value(1, 0.0, get_input_port_default_value(1)); // edge1
  3750. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2)); // x
  3751. break;
  3752. case OP_TYPE_VECTOR_3D:
  3753. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0)); // edge0
  3754. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1)); // edge1
  3755. set_input_port_default_value(2, Vector3(), get_input_port_default_value(2)); // x
  3756. break;
  3757. case OP_TYPE_VECTOR_3D_SCALAR:
  3758. set_input_port_default_value(0, 0.0, get_input_port_default_value(0)); // edge0
  3759. set_input_port_default_value(1, 0.0, get_input_port_default_value(1)); // edge1
  3760. set_input_port_default_value(2, Vector3(), get_input_port_default_value(2)); // x
  3761. break;
  3762. case OP_TYPE_VECTOR_4D:
  3763. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0)); // edge0
  3764. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1)); // edge1
  3765. set_input_port_default_value(2, Quaternion(), get_input_port_default_value(2)); // x
  3766. break;
  3767. case OP_TYPE_VECTOR_4D_SCALAR:
  3768. set_input_port_default_value(0, 0.0, get_input_port_default_value(0)); // edge0
  3769. set_input_port_default_value(1, 0.0, get_input_port_default_value(1)); // edge1
  3770. set_input_port_default_value(2, Quaternion(), get_input_port_default_value(2)); // x
  3771. break;
  3772. default:
  3773. break;
  3774. }
  3775. op_type = p_op_type;
  3776. emit_changed();
  3777. }
  3778. VisualShaderNodeSmoothStep::OpType VisualShaderNodeSmoothStep::get_op_type() const {
  3779. return op_type;
  3780. }
  3781. String VisualShaderNodeSmoothStep::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3782. return " " + p_output_vars[0] + " = smoothstep(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  3783. }
  3784. Vector<StringName> VisualShaderNodeSmoothStep::get_editable_properties() const {
  3785. Vector<StringName> props;
  3786. props.push_back("op_type");
  3787. return props;
  3788. }
  3789. void VisualShaderNodeSmoothStep::_bind_methods() {
  3790. ClassDB::bind_method(D_METHOD("set_op_type", "op_type"), &VisualShaderNodeSmoothStep::set_op_type);
  3791. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeSmoothStep::get_op_type);
  3792. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector2Scalar,Vector3,Vector3Scalar,Vector4,Vector4Scalar"), "set_op_type", "get_op_type");
  3793. BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
  3794. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  3795. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D_SCALAR);
  3796. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  3797. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D_SCALAR);
  3798. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  3799. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D_SCALAR);
  3800. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  3801. }
  3802. VisualShaderNodeSmoothStep::VisualShaderNodeSmoothStep() {
  3803. set_input_port_default_value(0, 0.0); // edge0
  3804. set_input_port_default_value(1, 1.0); // edge1
  3805. set_input_port_default_value(2, 0.5); // x
  3806. }
  3807. ////////////// Distance
  3808. String VisualShaderNodeVectorDistance::get_caption() const {
  3809. return "Distance";
  3810. }
  3811. int VisualShaderNodeVectorDistance::get_input_port_count() const {
  3812. return 2;
  3813. }
  3814. String VisualShaderNodeVectorDistance::get_input_port_name(int p_port) const {
  3815. switch (p_port) {
  3816. case 0:
  3817. return "a";
  3818. case 1:
  3819. return "b";
  3820. }
  3821. return String();
  3822. }
  3823. int VisualShaderNodeVectorDistance::get_output_port_count() const {
  3824. return 1;
  3825. }
  3826. VisualShaderNodeVectorDistance::PortType VisualShaderNodeVectorDistance::get_output_port_type(int p_port) const {
  3827. return PORT_TYPE_SCALAR;
  3828. }
  3829. String VisualShaderNodeVectorDistance::get_output_port_name(int p_port) const {
  3830. return "";
  3831. }
  3832. void VisualShaderNodeVectorDistance::set_op_type(OpType p_op_type) {
  3833. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  3834. if (op_type == p_op_type) {
  3835. return;
  3836. }
  3837. switch (p_op_type) {
  3838. case OP_TYPE_VECTOR_2D: {
  3839. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0)); // a
  3840. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1)); // b
  3841. } break;
  3842. case OP_TYPE_VECTOR_3D: {
  3843. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0)); // a
  3844. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1)); // b
  3845. } break;
  3846. case OP_TYPE_VECTOR_4D: {
  3847. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0)); // a
  3848. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1)); // b
  3849. } break;
  3850. default:
  3851. break;
  3852. }
  3853. op_type = p_op_type;
  3854. emit_changed();
  3855. }
  3856. String VisualShaderNodeVectorDistance::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3857. return " " + p_output_vars[0] + " = distance(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  3858. }
  3859. VisualShaderNodeVectorDistance::VisualShaderNodeVectorDistance() {
  3860. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0)); // a
  3861. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0)); // b
  3862. }
  3863. ////////////// Refract Vector
  3864. String VisualShaderNodeVectorRefract::get_caption() const {
  3865. return "Refract";
  3866. }
  3867. int VisualShaderNodeVectorRefract::get_input_port_count() const {
  3868. return 3;
  3869. }
  3870. String VisualShaderNodeVectorRefract::get_input_port_name(int p_port) const {
  3871. switch (p_port) {
  3872. case 0:
  3873. return "I";
  3874. case 1:
  3875. return "N";
  3876. case 2:
  3877. return "eta";
  3878. }
  3879. return String();
  3880. }
  3881. int VisualShaderNodeVectorRefract::get_output_port_count() const {
  3882. return 1;
  3883. }
  3884. String VisualShaderNodeVectorRefract::get_output_port_name(int p_port) const {
  3885. return "";
  3886. }
  3887. String VisualShaderNodeVectorRefract::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3888. return " " + p_output_vars[0] + " = refract(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  3889. }
  3890. void VisualShaderNodeVectorRefract::set_op_type(OpType p_op_type) {
  3891. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  3892. if (op_type == p_op_type) {
  3893. return;
  3894. }
  3895. switch (p_op_type) {
  3896. case OP_TYPE_VECTOR_2D: {
  3897. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  3898. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  3899. } break;
  3900. case OP_TYPE_VECTOR_3D: {
  3901. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  3902. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  3903. } break;
  3904. case OP_TYPE_VECTOR_4D: {
  3905. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  3906. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  3907. } break;
  3908. default:
  3909. break;
  3910. }
  3911. op_type = p_op_type;
  3912. emit_changed();
  3913. }
  3914. VisualShaderNodeVectorRefract::VisualShaderNodeVectorRefract() {
  3915. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  3916. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  3917. set_input_port_default_value(2, 0.0);
  3918. }
  3919. ////////////// Mix
  3920. String VisualShaderNodeMix::get_caption() const {
  3921. return "Mix";
  3922. }
  3923. int VisualShaderNodeMix::get_input_port_count() const {
  3924. return 3;
  3925. }
  3926. VisualShaderNodeMix::PortType VisualShaderNodeMix::get_input_port_type(int p_port) const {
  3927. switch (op_type) {
  3928. case OP_TYPE_VECTOR_2D:
  3929. return PORT_TYPE_VECTOR_2D;
  3930. case OP_TYPE_VECTOR_2D_SCALAR:
  3931. if (p_port == 2) {
  3932. break;
  3933. }
  3934. return PORT_TYPE_VECTOR_2D;
  3935. case OP_TYPE_VECTOR_3D:
  3936. return PORT_TYPE_VECTOR_3D;
  3937. case OP_TYPE_VECTOR_3D_SCALAR:
  3938. if (p_port == 2) {
  3939. break;
  3940. }
  3941. return PORT_TYPE_VECTOR_3D;
  3942. case OP_TYPE_VECTOR_4D:
  3943. return PORT_TYPE_VECTOR_4D;
  3944. case OP_TYPE_VECTOR_4D_SCALAR:
  3945. if (p_port == 2) {
  3946. break;
  3947. }
  3948. return PORT_TYPE_VECTOR_4D;
  3949. default:
  3950. break;
  3951. }
  3952. return PORT_TYPE_SCALAR;
  3953. }
  3954. String VisualShaderNodeMix::get_input_port_name(int p_port) const {
  3955. if (p_port == 0) {
  3956. return "a";
  3957. } else if (p_port == 1) {
  3958. return "b";
  3959. } else {
  3960. return "weight";
  3961. }
  3962. }
  3963. int VisualShaderNodeMix::get_output_port_count() const {
  3964. return 1;
  3965. }
  3966. VisualShaderNodeMix::PortType VisualShaderNodeMix::get_output_port_type(int p_port) const {
  3967. switch (op_type) {
  3968. case OP_TYPE_VECTOR_2D:
  3969. return PORT_TYPE_VECTOR_2D;
  3970. case OP_TYPE_VECTOR_2D_SCALAR:
  3971. return PORT_TYPE_VECTOR_2D;
  3972. case OP_TYPE_VECTOR_3D:
  3973. return PORT_TYPE_VECTOR_3D;
  3974. case OP_TYPE_VECTOR_3D_SCALAR:
  3975. return PORT_TYPE_VECTOR_3D;
  3976. case OP_TYPE_VECTOR_4D:
  3977. return PORT_TYPE_VECTOR_4D;
  3978. case OP_TYPE_VECTOR_4D_SCALAR:
  3979. return PORT_TYPE_VECTOR_4D;
  3980. default:
  3981. break;
  3982. }
  3983. return PORT_TYPE_SCALAR;
  3984. }
  3985. String VisualShaderNodeMix::get_output_port_name(int p_port) const {
  3986. return "mix";
  3987. }
  3988. void VisualShaderNodeMix::set_op_type(OpType p_op_type) {
  3989. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  3990. if (op_type == p_op_type) {
  3991. return;
  3992. }
  3993. switch (p_op_type) {
  3994. case OP_TYPE_SCALAR: {
  3995. set_input_port_default_value(0, 0.0, get_input_port_default_value(0)); // a
  3996. set_input_port_default_value(1, 0.0, get_input_port_default_value(1)); // b
  3997. set_input_port_default_value(2, 0.0, get_input_port_default_value(2)); // weight
  3998. } break;
  3999. case OP_TYPE_VECTOR_2D: {
  4000. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0)); // a
  4001. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1)); // b
  4002. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2)); // weight
  4003. } break;
  4004. case OP_TYPE_VECTOR_2D_SCALAR: {
  4005. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0)); // a
  4006. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1)); // b
  4007. set_input_port_default_value(2, 0.0, get_input_port_default_value(2)); // weight
  4008. } break;
  4009. case OP_TYPE_VECTOR_3D: {
  4010. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0)); // a
  4011. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1)); // b
  4012. set_input_port_default_value(2, Vector3(), get_input_port_default_value(2)); // weight
  4013. } break;
  4014. case OP_TYPE_VECTOR_3D_SCALAR: {
  4015. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0)); // a
  4016. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1)); // b
  4017. set_input_port_default_value(2, 0.0, get_input_port_default_value(2)); // weight
  4018. } break;
  4019. case OP_TYPE_VECTOR_4D: {
  4020. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0)); // a
  4021. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1)); // b
  4022. set_input_port_default_value(2, Quaternion(), get_input_port_default_value(2)); // weight
  4023. } break;
  4024. case OP_TYPE_VECTOR_4D_SCALAR: {
  4025. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0)); // a
  4026. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1)); // b
  4027. set_input_port_default_value(2, 0.0, get_input_port_default_value(2)); // weight
  4028. } break;
  4029. default:
  4030. break;
  4031. }
  4032. op_type = p_op_type;
  4033. emit_changed();
  4034. }
  4035. VisualShaderNodeMix::OpType VisualShaderNodeMix::get_op_type() const {
  4036. return op_type;
  4037. }
  4038. String VisualShaderNodeMix::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4039. return " " + p_output_vars[0] + " = mix(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  4040. }
  4041. Vector<StringName> VisualShaderNodeMix::get_editable_properties() const {
  4042. Vector<StringName> props;
  4043. props.push_back("op_type");
  4044. return props;
  4045. }
  4046. void VisualShaderNodeMix::_bind_methods() {
  4047. ClassDB::bind_method(D_METHOD("set_op_type", "op_type"), &VisualShaderNodeMix::set_op_type);
  4048. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeMix::get_op_type);
  4049. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector2Scalar,Vector3,Vector3Scalar,Vector4,Vector4Scalar"), "set_op_type", "get_op_type");
  4050. BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
  4051. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  4052. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D_SCALAR);
  4053. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  4054. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D_SCALAR);
  4055. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  4056. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D_SCALAR);
  4057. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  4058. }
  4059. VisualShaderNodeMix::VisualShaderNodeMix() {
  4060. set_input_port_default_value(0, 0.0); // a
  4061. set_input_port_default_value(1, 1.0); // b
  4062. set_input_port_default_value(2, 0.5); // weight
  4063. }
  4064. ////////////// Vector Compose
  4065. String VisualShaderNodeVectorCompose::get_caption() const {
  4066. return "VectorCompose";
  4067. }
  4068. int VisualShaderNodeVectorCompose::get_input_port_count() const {
  4069. switch (op_type) {
  4070. case OP_TYPE_VECTOR_2D:
  4071. return 2;
  4072. case OP_TYPE_VECTOR_3D:
  4073. return 3;
  4074. case OP_TYPE_VECTOR_4D:
  4075. return 4;
  4076. default:
  4077. break;
  4078. }
  4079. return 0;
  4080. }
  4081. VisualShaderNodeVectorCompose::PortType VisualShaderNodeVectorCompose::get_input_port_type(int p_port) const {
  4082. return PORT_TYPE_SCALAR;
  4083. }
  4084. String VisualShaderNodeVectorCompose::get_input_port_name(int p_port) const {
  4085. switch (op_type) {
  4086. case OP_TYPE_VECTOR_2D: {
  4087. switch (p_port) {
  4088. case 0:
  4089. return "x";
  4090. case 1:
  4091. return "y";
  4092. }
  4093. } break;
  4094. case OP_TYPE_VECTOR_3D: {
  4095. switch (p_port) {
  4096. case 0:
  4097. return "x";
  4098. case 1:
  4099. return "y";
  4100. case 2:
  4101. return "z";
  4102. }
  4103. } break;
  4104. case OP_TYPE_VECTOR_4D: {
  4105. switch (p_port) {
  4106. case 0:
  4107. return "x";
  4108. case 1:
  4109. return "y";
  4110. case 2:
  4111. return "z";
  4112. case 3:
  4113. return "w";
  4114. }
  4115. } break;
  4116. default:
  4117. break;
  4118. }
  4119. return String();
  4120. }
  4121. int VisualShaderNodeVectorCompose::get_output_port_count() const {
  4122. return 1;
  4123. }
  4124. String VisualShaderNodeVectorCompose::get_output_port_name(int p_port) const {
  4125. return "vec";
  4126. }
  4127. void VisualShaderNodeVectorCompose::set_op_type(OpType p_op_type) {
  4128. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  4129. if (op_type == p_op_type) {
  4130. return;
  4131. }
  4132. switch (p_op_type) {
  4133. case OP_TYPE_VECTOR_2D: {
  4134. float p1 = get_input_port_default_value(0);
  4135. float p2 = get_input_port_default_value(1);
  4136. set_input_port_default_value(0, p1);
  4137. set_input_port_default_value(1, p2);
  4138. } break;
  4139. case OP_TYPE_VECTOR_3D: {
  4140. float p1 = get_input_port_default_value(0);
  4141. float p2 = get_input_port_default_value(1);
  4142. set_input_port_default_value(0, p1);
  4143. set_input_port_default_value(1, p2);
  4144. set_input_port_default_value(2, 0.0);
  4145. } break;
  4146. case OP_TYPE_VECTOR_4D: {
  4147. float p1 = get_input_port_default_value(0);
  4148. float p2 = get_input_port_default_value(1);
  4149. set_input_port_default_value(0, p1);
  4150. set_input_port_default_value(1, p2);
  4151. set_input_port_default_value(2, 0.0);
  4152. set_input_port_default_value(3, 0.0);
  4153. } break;
  4154. default:
  4155. break;
  4156. }
  4157. op_type = p_op_type;
  4158. emit_changed();
  4159. }
  4160. String VisualShaderNodeVectorCompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4161. String code;
  4162. switch (op_type) {
  4163. case OP_TYPE_VECTOR_2D: {
  4164. code += " " + p_output_vars[0] + " = vec2(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  4165. } break;
  4166. case OP_TYPE_VECTOR_3D: {
  4167. code += " " + p_output_vars[0] + " = vec3(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  4168. } break;
  4169. case OP_TYPE_VECTOR_4D: {
  4170. code += " " + p_output_vars[0] + " = vec4(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ", " + p_input_vars[3] + ");\n";
  4171. } break;
  4172. default:
  4173. break;
  4174. }
  4175. return code;
  4176. }
  4177. VisualShaderNodeVectorCompose::VisualShaderNodeVectorCompose() {
  4178. set_input_port_default_value(0, 0.0);
  4179. set_input_port_default_value(1, 0.0);
  4180. set_input_port_default_value(2, 0.0);
  4181. }
  4182. ////////////// Transform Compose
  4183. String VisualShaderNodeTransformCompose::get_caption() const {
  4184. return "TransformCompose";
  4185. }
  4186. int VisualShaderNodeTransformCompose::get_input_port_count() const {
  4187. return 4;
  4188. }
  4189. VisualShaderNodeTransformCompose::PortType VisualShaderNodeTransformCompose::get_input_port_type(int p_port) const {
  4190. return PORT_TYPE_VECTOR_3D;
  4191. }
  4192. String VisualShaderNodeTransformCompose::get_input_port_name(int p_port) const {
  4193. if (p_port == 0) {
  4194. return "x";
  4195. } else if (p_port == 1) {
  4196. return "y";
  4197. } else if (p_port == 2) {
  4198. return "z";
  4199. } else {
  4200. return "origin";
  4201. }
  4202. }
  4203. int VisualShaderNodeTransformCompose::get_output_port_count() const {
  4204. return 1;
  4205. }
  4206. VisualShaderNodeTransformCompose::PortType VisualShaderNodeTransformCompose::get_output_port_type(int p_port) const {
  4207. return PORT_TYPE_TRANSFORM;
  4208. }
  4209. String VisualShaderNodeTransformCompose::get_output_port_name(int p_port) const {
  4210. return "xform";
  4211. }
  4212. String VisualShaderNodeTransformCompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4213. return " " + p_output_vars[0] + " = mat4(vec4(" + p_input_vars[0] + ", 0.0), vec4(" + p_input_vars[1] + ", 0.0), vec4(" + p_input_vars[2] + ", 0.0), vec4(" + p_input_vars[3] + ", 1.0));\n";
  4214. }
  4215. VisualShaderNodeTransformCompose::VisualShaderNodeTransformCompose() {
  4216. set_input_port_default_value(0, Vector3());
  4217. set_input_port_default_value(1, Vector3());
  4218. set_input_port_default_value(2, Vector3());
  4219. set_input_port_default_value(3, Vector3());
  4220. }
  4221. ////////////// Vector Decompose
  4222. String VisualShaderNodeVectorDecompose::get_caption() const {
  4223. return "VectorDecompose";
  4224. }
  4225. int VisualShaderNodeVectorDecompose::get_input_port_count() const {
  4226. return 1;
  4227. }
  4228. String VisualShaderNodeVectorDecompose::get_input_port_name(int p_port) const {
  4229. return "vec";
  4230. }
  4231. int VisualShaderNodeVectorDecompose::get_output_port_count() const {
  4232. switch (op_type) {
  4233. case OP_TYPE_VECTOR_2D:
  4234. return 2;
  4235. case OP_TYPE_VECTOR_3D:
  4236. return 3;
  4237. case OP_TYPE_VECTOR_4D:
  4238. return 4;
  4239. default:
  4240. break;
  4241. }
  4242. return 0;
  4243. }
  4244. VisualShaderNodeVectorDecompose::PortType VisualShaderNodeVectorDecompose::get_output_port_type(int p_port) const {
  4245. return PORT_TYPE_SCALAR;
  4246. }
  4247. String VisualShaderNodeVectorDecompose::get_output_port_name(int p_port) const {
  4248. switch (op_type) {
  4249. case OP_TYPE_VECTOR_2D: {
  4250. switch (p_port) {
  4251. case 0:
  4252. return "x";
  4253. case 1:
  4254. return "y";
  4255. }
  4256. } break;
  4257. case OP_TYPE_VECTOR_3D: {
  4258. switch (p_port) {
  4259. case 0:
  4260. return "x";
  4261. case 1:
  4262. return "y";
  4263. case 2:
  4264. return "z";
  4265. }
  4266. } break;
  4267. case OP_TYPE_VECTOR_4D: {
  4268. switch (p_port) {
  4269. case 0:
  4270. return "x";
  4271. case 1:
  4272. return "y";
  4273. case 2:
  4274. return "z";
  4275. case 3:
  4276. return "w";
  4277. }
  4278. } break;
  4279. default:
  4280. break;
  4281. }
  4282. return String();
  4283. }
  4284. void VisualShaderNodeVectorDecompose::set_op_type(OpType p_op_type) {
  4285. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  4286. if (op_type == p_op_type) {
  4287. return;
  4288. }
  4289. switch (p_op_type) {
  4290. case OP_TYPE_VECTOR_2D: {
  4291. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  4292. } break;
  4293. case OP_TYPE_VECTOR_3D: {
  4294. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  4295. } break;
  4296. case OP_TYPE_VECTOR_4D: {
  4297. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  4298. } break;
  4299. default:
  4300. break;
  4301. }
  4302. op_type = p_op_type;
  4303. emit_changed();
  4304. }
  4305. String VisualShaderNodeVectorDecompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4306. String code;
  4307. switch (op_type) {
  4308. case OP_TYPE_VECTOR_2D: {
  4309. code += " " + p_output_vars[0] + " = " + p_input_vars[0] + ".x;\n";
  4310. code += " " + p_output_vars[1] + " = " + p_input_vars[0] + ".y;\n";
  4311. } break;
  4312. case OP_TYPE_VECTOR_3D: {
  4313. code += " " + p_output_vars[0] + " = " + p_input_vars[0] + ".x;\n";
  4314. code += " " + p_output_vars[1] + " = " + p_input_vars[0] + ".y;\n";
  4315. code += " " + p_output_vars[2] + " = " + p_input_vars[0] + ".z;\n";
  4316. } break;
  4317. case OP_TYPE_VECTOR_4D: {
  4318. code += " " + p_output_vars[0] + " = " + p_input_vars[0] + ".x;\n";
  4319. code += " " + p_output_vars[1] + " = " + p_input_vars[0] + ".y;\n";
  4320. code += " " + p_output_vars[2] + " = " + p_input_vars[0] + ".z;\n";
  4321. code += " " + p_output_vars[3] + " = " + p_input_vars[0] + ".w;\n";
  4322. } break;
  4323. default:
  4324. break;
  4325. }
  4326. return code;
  4327. }
  4328. VisualShaderNodeVectorDecompose::VisualShaderNodeVectorDecompose() {
  4329. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  4330. }
  4331. ////////////// Transform Decompose
  4332. String VisualShaderNodeTransformDecompose::get_caption() const {
  4333. return "TransformDecompose";
  4334. }
  4335. int VisualShaderNodeTransformDecompose::get_input_port_count() const {
  4336. return 1;
  4337. }
  4338. VisualShaderNodeTransformDecompose::PortType VisualShaderNodeTransformDecompose::get_input_port_type(int p_port) const {
  4339. return PORT_TYPE_TRANSFORM;
  4340. }
  4341. String VisualShaderNodeTransformDecompose::get_input_port_name(int p_port) const {
  4342. return "xform";
  4343. }
  4344. int VisualShaderNodeTransformDecompose::get_output_port_count() const {
  4345. return 4;
  4346. }
  4347. VisualShaderNodeTransformDecompose::PortType VisualShaderNodeTransformDecompose::get_output_port_type(int p_port) const {
  4348. return PORT_TYPE_VECTOR_3D;
  4349. }
  4350. String VisualShaderNodeTransformDecompose::get_output_port_name(int p_port) const {
  4351. if (p_port == 0) {
  4352. return "x";
  4353. } else if (p_port == 1) {
  4354. return "y";
  4355. } else if (p_port == 2) {
  4356. return "z";
  4357. } else {
  4358. return "origin";
  4359. }
  4360. }
  4361. String VisualShaderNodeTransformDecompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4362. String code;
  4363. code += " " + p_output_vars[0] + " = " + p_input_vars[0] + "[0].xyz;\n";
  4364. code += " " + p_output_vars[1] + " = " + p_input_vars[0] + "[1].xyz;\n";
  4365. code += " " + p_output_vars[2] + " = " + p_input_vars[0] + "[2].xyz;\n";
  4366. code += " " + p_output_vars[3] + " = " + p_input_vars[0] + "[3].xyz;\n";
  4367. return code;
  4368. }
  4369. VisualShaderNodeTransformDecompose::VisualShaderNodeTransformDecompose() {
  4370. set_input_port_default_value(0, Transform3D());
  4371. }
  4372. ////////////// Float Parameter
  4373. String VisualShaderNodeFloatParameter::get_caption() const {
  4374. return "FloatParameter";
  4375. }
  4376. int VisualShaderNodeFloatParameter::get_input_port_count() const {
  4377. return 0;
  4378. }
  4379. VisualShaderNodeFloatParameter::PortType VisualShaderNodeFloatParameter::get_input_port_type(int p_port) const {
  4380. return PORT_TYPE_SCALAR;
  4381. }
  4382. String VisualShaderNodeFloatParameter::get_input_port_name(int p_port) const {
  4383. return String();
  4384. }
  4385. int VisualShaderNodeFloatParameter::get_output_port_count() const {
  4386. return 1;
  4387. }
  4388. VisualShaderNodeFloatParameter::PortType VisualShaderNodeFloatParameter::get_output_port_type(int p_port) const {
  4389. return PORT_TYPE_SCALAR;
  4390. }
  4391. String VisualShaderNodeFloatParameter::get_output_port_name(int p_port) const {
  4392. return ""; //no output port means the editor will be used as port
  4393. }
  4394. String VisualShaderNodeFloatParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4395. String code = "";
  4396. if (hint == HINT_RANGE) {
  4397. code += _get_qual_str() + "uniform float " + get_parameter_name() + " : hint_range(" + rtos(hint_range_min) + ", " + rtos(hint_range_max) + ")";
  4398. } else if (hint == HINT_RANGE_STEP) {
  4399. code += _get_qual_str() + "uniform float " + get_parameter_name() + " : hint_range(" + rtos(hint_range_min) + ", " + rtos(hint_range_max) + ", " + rtos(hint_range_step) + ")";
  4400. } else {
  4401. code += _get_qual_str() + "uniform float " + get_parameter_name();
  4402. }
  4403. if (default_value_enabled) {
  4404. code += " = " + rtos(default_value);
  4405. }
  4406. code += ";\n";
  4407. return code;
  4408. }
  4409. String VisualShaderNodeFloatParameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4410. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  4411. }
  4412. bool VisualShaderNodeFloatParameter::is_show_prop_names() const {
  4413. return true;
  4414. }
  4415. bool VisualShaderNodeFloatParameter::is_use_prop_slots() const {
  4416. return true;
  4417. }
  4418. void VisualShaderNodeFloatParameter::set_hint(Hint p_hint) {
  4419. ERR_FAIL_INDEX(int(p_hint), int(HINT_MAX));
  4420. if (hint == p_hint) {
  4421. return;
  4422. }
  4423. hint = p_hint;
  4424. emit_changed();
  4425. }
  4426. VisualShaderNodeFloatParameter::Hint VisualShaderNodeFloatParameter::get_hint() const {
  4427. return hint;
  4428. }
  4429. void VisualShaderNodeFloatParameter::set_min(float p_value) {
  4430. if (Math::is_equal_approx(hint_range_min, p_value)) {
  4431. return;
  4432. }
  4433. hint_range_min = p_value;
  4434. emit_changed();
  4435. }
  4436. float VisualShaderNodeFloatParameter::get_min() const {
  4437. return hint_range_min;
  4438. }
  4439. void VisualShaderNodeFloatParameter::set_max(float p_value) {
  4440. if (Math::is_equal_approx(hint_range_max, p_value)) {
  4441. return;
  4442. }
  4443. hint_range_max = p_value;
  4444. emit_changed();
  4445. }
  4446. float VisualShaderNodeFloatParameter::get_max() const {
  4447. return hint_range_max;
  4448. }
  4449. void VisualShaderNodeFloatParameter::set_step(float p_value) {
  4450. if (Math::is_equal_approx(hint_range_step, p_value)) {
  4451. return;
  4452. }
  4453. hint_range_step = p_value;
  4454. emit_changed();
  4455. }
  4456. float VisualShaderNodeFloatParameter::get_step() const {
  4457. return hint_range_step;
  4458. }
  4459. void VisualShaderNodeFloatParameter::set_default_value_enabled(bool p_enabled) {
  4460. if (default_value_enabled == p_enabled) {
  4461. return;
  4462. }
  4463. default_value_enabled = p_enabled;
  4464. emit_changed();
  4465. }
  4466. bool VisualShaderNodeFloatParameter::is_default_value_enabled() const {
  4467. return default_value_enabled;
  4468. }
  4469. void VisualShaderNodeFloatParameter::set_default_value(float p_value) {
  4470. if (Math::is_equal_approx(default_value, p_value)) {
  4471. return;
  4472. }
  4473. default_value = p_value;
  4474. emit_changed();
  4475. }
  4476. float VisualShaderNodeFloatParameter::get_default_value() const {
  4477. return default_value;
  4478. }
  4479. void VisualShaderNodeFloatParameter::_bind_methods() {
  4480. ClassDB::bind_method(D_METHOD("set_hint", "hint"), &VisualShaderNodeFloatParameter::set_hint);
  4481. ClassDB::bind_method(D_METHOD("get_hint"), &VisualShaderNodeFloatParameter::get_hint);
  4482. ClassDB::bind_method(D_METHOD("set_min", "value"), &VisualShaderNodeFloatParameter::set_min);
  4483. ClassDB::bind_method(D_METHOD("get_min"), &VisualShaderNodeFloatParameter::get_min);
  4484. ClassDB::bind_method(D_METHOD("set_max", "value"), &VisualShaderNodeFloatParameter::set_max);
  4485. ClassDB::bind_method(D_METHOD("get_max"), &VisualShaderNodeFloatParameter::get_max);
  4486. ClassDB::bind_method(D_METHOD("set_step", "value"), &VisualShaderNodeFloatParameter::set_step);
  4487. ClassDB::bind_method(D_METHOD("get_step"), &VisualShaderNodeFloatParameter::get_step);
  4488. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeFloatParameter::set_default_value_enabled);
  4489. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeFloatParameter::is_default_value_enabled);
  4490. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeFloatParameter::set_default_value);
  4491. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeFloatParameter::get_default_value);
  4492. ADD_PROPERTY(PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_ENUM, "None,Range,Range+Step"), "set_hint", "get_hint");
  4493. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min"), "set_min", "get_min");
  4494. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max"), "set_max", "get_max");
  4495. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "set_step", "get_step");
  4496. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  4497. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "default_value"), "set_default_value", "get_default_value");
  4498. BIND_ENUM_CONSTANT(HINT_NONE);
  4499. BIND_ENUM_CONSTANT(HINT_RANGE);
  4500. BIND_ENUM_CONSTANT(HINT_RANGE_STEP);
  4501. BIND_ENUM_CONSTANT(HINT_MAX);
  4502. }
  4503. bool VisualShaderNodeFloatParameter::is_qualifier_supported(Qualifier p_qual) const {
  4504. return true; // all qualifiers are supported
  4505. }
  4506. bool VisualShaderNodeFloatParameter::is_convertible_to_constant() const {
  4507. return true; // conversion is allowed
  4508. }
  4509. Vector<StringName> VisualShaderNodeFloatParameter::get_editable_properties() const {
  4510. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  4511. props.push_back("hint");
  4512. if (hint == HINT_RANGE || hint == HINT_RANGE_STEP) {
  4513. props.push_back("min");
  4514. props.push_back("max");
  4515. }
  4516. if (hint == HINT_RANGE_STEP) {
  4517. props.push_back("step");
  4518. }
  4519. props.push_back("default_value_enabled");
  4520. if (default_value_enabled) {
  4521. props.push_back("default_value");
  4522. }
  4523. return props;
  4524. }
  4525. VisualShaderNodeFloatParameter::VisualShaderNodeFloatParameter() {
  4526. }
  4527. ////////////// Integer Parameter
  4528. String VisualShaderNodeIntParameter::get_caption() const {
  4529. return "IntParameter";
  4530. }
  4531. int VisualShaderNodeIntParameter::get_input_port_count() const {
  4532. return 0;
  4533. }
  4534. VisualShaderNodeIntParameter::PortType VisualShaderNodeIntParameter::get_input_port_type(int p_port) const {
  4535. return PORT_TYPE_SCALAR_INT;
  4536. }
  4537. String VisualShaderNodeIntParameter::get_input_port_name(int p_port) const {
  4538. return String();
  4539. }
  4540. int VisualShaderNodeIntParameter::get_output_port_count() const {
  4541. return 1;
  4542. }
  4543. VisualShaderNodeIntParameter::PortType VisualShaderNodeIntParameter::get_output_port_type(int p_port) const {
  4544. return PORT_TYPE_SCALAR_INT;
  4545. }
  4546. String VisualShaderNodeIntParameter::get_output_port_name(int p_port) const {
  4547. return ""; //no output port means the editor will be used as port
  4548. }
  4549. String VisualShaderNodeIntParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4550. String code = "";
  4551. if (hint == HINT_RANGE) {
  4552. code += _get_qual_str() + "uniform int " + get_parameter_name() + " : hint_range(" + itos(hint_range_min) + ", " + itos(hint_range_max) + ")";
  4553. } else if (hint == HINT_RANGE_STEP) {
  4554. code += _get_qual_str() + "uniform int " + get_parameter_name() + " : hint_range(" + itos(hint_range_min) + ", " + itos(hint_range_max) + ", " + itos(hint_range_step) + ")";
  4555. } else {
  4556. code += _get_qual_str() + "uniform int " + get_parameter_name();
  4557. }
  4558. if (default_value_enabled) {
  4559. code += " = " + itos(default_value);
  4560. }
  4561. code += ";\n";
  4562. return code;
  4563. }
  4564. String VisualShaderNodeIntParameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4565. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  4566. }
  4567. bool VisualShaderNodeIntParameter::is_show_prop_names() const {
  4568. return true;
  4569. }
  4570. bool VisualShaderNodeIntParameter::is_use_prop_slots() const {
  4571. return true;
  4572. }
  4573. void VisualShaderNodeIntParameter::set_hint(Hint p_hint) {
  4574. ERR_FAIL_INDEX(int(p_hint), int(HINT_MAX));
  4575. if (hint == p_hint) {
  4576. return;
  4577. }
  4578. hint = p_hint;
  4579. emit_changed();
  4580. }
  4581. VisualShaderNodeIntParameter::Hint VisualShaderNodeIntParameter::get_hint() const {
  4582. return hint;
  4583. }
  4584. void VisualShaderNodeIntParameter::set_min(int p_value) {
  4585. if (hint_range_min == p_value) {
  4586. return;
  4587. }
  4588. hint_range_min = p_value;
  4589. emit_changed();
  4590. }
  4591. int VisualShaderNodeIntParameter::get_min() const {
  4592. return hint_range_min;
  4593. }
  4594. void VisualShaderNodeIntParameter::set_max(int p_value) {
  4595. if (hint_range_max == p_value) {
  4596. return;
  4597. }
  4598. hint_range_max = p_value;
  4599. emit_changed();
  4600. }
  4601. int VisualShaderNodeIntParameter::get_max() const {
  4602. return hint_range_max;
  4603. }
  4604. void VisualShaderNodeIntParameter::set_step(int p_value) {
  4605. if (hint_range_step == p_value) {
  4606. return;
  4607. }
  4608. hint_range_step = p_value;
  4609. emit_changed();
  4610. }
  4611. int VisualShaderNodeIntParameter::get_step() const {
  4612. return hint_range_step;
  4613. }
  4614. void VisualShaderNodeIntParameter::set_default_value_enabled(bool p_default_value_enabled) {
  4615. if (default_value_enabled == p_default_value_enabled) {
  4616. return;
  4617. }
  4618. default_value_enabled = p_default_value_enabled;
  4619. emit_changed();
  4620. }
  4621. bool VisualShaderNodeIntParameter::is_default_value_enabled() const {
  4622. return default_value_enabled;
  4623. }
  4624. void VisualShaderNodeIntParameter::set_default_value(int p_default_value) {
  4625. if (default_value == p_default_value) {
  4626. return;
  4627. }
  4628. default_value = p_default_value;
  4629. emit_changed();
  4630. }
  4631. int VisualShaderNodeIntParameter::get_default_value() const {
  4632. return default_value;
  4633. }
  4634. void VisualShaderNodeIntParameter::_bind_methods() {
  4635. ClassDB::bind_method(D_METHOD("set_hint", "hint"), &VisualShaderNodeIntParameter::set_hint);
  4636. ClassDB::bind_method(D_METHOD("get_hint"), &VisualShaderNodeIntParameter::get_hint);
  4637. ClassDB::bind_method(D_METHOD("set_min", "value"), &VisualShaderNodeIntParameter::set_min);
  4638. ClassDB::bind_method(D_METHOD("get_min"), &VisualShaderNodeIntParameter::get_min);
  4639. ClassDB::bind_method(D_METHOD("set_max", "value"), &VisualShaderNodeIntParameter::set_max);
  4640. ClassDB::bind_method(D_METHOD("get_max"), &VisualShaderNodeIntParameter::get_max);
  4641. ClassDB::bind_method(D_METHOD("set_step", "value"), &VisualShaderNodeIntParameter::set_step);
  4642. ClassDB::bind_method(D_METHOD("get_step"), &VisualShaderNodeIntParameter::get_step);
  4643. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeIntParameter::set_default_value_enabled);
  4644. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeIntParameter::is_default_value_enabled);
  4645. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeIntParameter::set_default_value);
  4646. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeIntParameter::get_default_value);
  4647. ADD_PROPERTY(PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_ENUM, "None,Range,Range + Step"), "set_hint", "get_hint");
  4648. ADD_PROPERTY(PropertyInfo(Variant::INT, "min"), "set_min", "get_min");
  4649. ADD_PROPERTY(PropertyInfo(Variant::INT, "max"), "set_max", "get_max");
  4650. ADD_PROPERTY(PropertyInfo(Variant::INT, "step"), "set_step", "get_step");
  4651. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  4652. ADD_PROPERTY(PropertyInfo(Variant::INT, "default_value"), "set_default_value", "get_default_value");
  4653. BIND_ENUM_CONSTANT(HINT_NONE);
  4654. BIND_ENUM_CONSTANT(HINT_RANGE);
  4655. BIND_ENUM_CONSTANT(HINT_RANGE_STEP);
  4656. BIND_ENUM_CONSTANT(HINT_MAX);
  4657. }
  4658. bool VisualShaderNodeIntParameter::is_qualifier_supported(Qualifier p_qual) const {
  4659. return true; // all qualifiers are supported
  4660. }
  4661. bool VisualShaderNodeIntParameter::is_convertible_to_constant() const {
  4662. return true; // conversion is allowed
  4663. }
  4664. Vector<StringName> VisualShaderNodeIntParameter::get_editable_properties() const {
  4665. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  4666. props.push_back("hint");
  4667. if (hint == HINT_RANGE || hint == HINT_RANGE_STEP) {
  4668. props.push_back("min");
  4669. props.push_back("max");
  4670. }
  4671. if (hint == HINT_RANGE_STEP) {
  4672. props.push_back("step");
  4673. }
  4674. props.push_back("default_value_enabled");
  4675. if (default_value_enabled) {
  4676. props.push_back("default_value");
  4677. }
  4678. return props;
  4679. }
  4680. VisualShaderNodeIntParameter::VisualShaderNodeIntParameter() {
  4681. }
  4682. ////////////// Unsigned Integer Parameter
  4683. String VisualShaderNodeUIntParameter::get_caption() const {
  4684. return "UIntParameter";
  4685. }
  4686. int VisualShaderNodeUIntParameter::get_input_port_count() const {
  4687. return 0;
  4688. }
  4689. VisualShaderNodeUIntParameter::PortType VisualShaderNodeUIntParameter::get_input_port_type(int p_port) const {
  4690. return PORT_TYPE_SCALAR_UINT;
  4691. }
  4692. String VisualShaderNodeUIntParameter::get_input_port_name(int p_port) const {
  4693. return String();
  4694. }
  4695. int VisualShaderNodeUIntParameter::get_output_port_count() const {
  4696. return 1;
  4697. }
  4698. VisualShaderNodeUIntParameter::PortType VisualShaderNodeUIntParameter::get_output_port_type(int p_port) const {
  4699. return PORT_TYPE_SCALAR_UINT;
  4700. }
  4701. String VisualShaderNodeUIntParameter::get_output_port_name(int p_port) const {
  4702. return ""; // No output port means the editor will be used as port.
  4703. }
  4704. String VisualShaderNodeUIntParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4705. String code = _get_qual_str() + "uniform uint " + get_parameter_name();
  4706. if (default_value_enabled) {
  4707. code += " = " + itos(default_value);
  4708. }
  4709. code += ";\n";
  4710. return code;
  4711. }
  4712. String VisualShaderNodeUIntParameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4713. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  4714. }
  4715. bool VisualShaderNodeUIntParameter::is_show_prop_names() const {
  4716. return true;
  4717. }
  4718. bool VisualShaderNodeUIntParameter::is_use_prop_slots() const {
  4719. return true;
  4720. }
  4721. void VisualShaderNodeUIntParameter::set_default_value_enabled(bool p_default_value_enabled) {
  4722. if (default_value_enabled == p_default_value_enabled) {
  4723. return;
  4724. }
  4725. default_value_enabled = p_default_value_enabled;
  4726. emit_changed();
  4727. }
  4728. bool VisualShaderNodeUIntParameter::is_default_value_enabled() const {
  4729. return default_value_enabled;
  4730. }
  4731. void VisualShaderNodeUIntParameter::set_default_value(int p_default_value) {
  4732. if (default_value == p_default_value) {
  4733. return;
  4734. }
  4735. default_value = p_default_value;
  4736. emit_changed();
  4737. }
  4738. int VisualShaderNodeUIntParameter::get_default_value() const {
  4739. return default_value;
  4740. }
  4741. void VisualShaderNodeUIntParameter::_bind_methods() {
  4742. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeUIntParameter::set_default_value_enabled);
  4743. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeUIntParameter::is_default_value_enabled);
  4744. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeUIntParameter::set_default_value);
  4745. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeUIntParameter::get_default_value);
  4746. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  4747. ADD_PROPERTY(PropertyInfo(Variant::INT, "default_value"), "set_default_value", "get_default_value");
  4748. }
  4749. bool VisualShaderNodeUIntParameter::is_qualifier_supported(Qualifier p_qual) const {
  4750. return true; // All qualifiers are supported.
  4751. }
  4752. bool VisualShaderNodeUIntParameter::is_convertible_to_constant() const {
  4753. return true; // Conversion is allowed.
  4754. }
  4755. Vector<StringName> VisualShaderNodeUIntParameter::get_editable_properties() const {
  4756. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  4757. props.push_back("default_value_enabled");
  4758. if (default_value_enabled) {
  4759. props.push_back("default_value");
  4760. }
  4761. return props;
  4762. }
  4763. VisualShaderNodeUIntParameter::VisualShaderNodeUIntParameter() {
  4764. }
  4765. ////////////// Boolean Parameter
  4766. String VisualShaderNodeBooleanParameter::get_caption() const {
  4767. return "BooleanParameter";
  4768. }
  4769. int VisualShaderNodeBooleanParameter::get_input_port_count() const {
  4770. return 0;
  4771. }
  4772. VisualShaderNodeBooleanParameter::PortType VisualShaderNodeBooleanParameter::get_input_port_type(int p_port) const {
  4773. return PORT_TYPE_BOOLEAN;
  4774. }
  4775. String VisualShaderNodeBooleanParameter::get_input_port_name(int p_port) const {
  4776. return String();
  4777. }
  4778. int VisualShaderNodeBooleanParameter::get_output_port_count() const {
  4779. return 1;
  4780. }
  4781. VisualShaderNodeBooleanParameter::PortType VisualShaderNodeBooleanParameter::get_output_port_type(int p_port) const {
  4782. return PORT_TYPE_BOOLEAN;
  4783. }
  4784. String VisualShaderNodeBooleanParameter::get_output_port_name(int p_port) const {
  4785. return ""; //no output port means the editor will be used as port
  4786. }
  4787. void VisualShaderNodeBooleanParameter::set_default_value_enabled(bool p_default_value_enabled) {
  4788. if (default_value_enabled == p_default_value_enabled) {
  4789. return;
  4790. }
  4791. default_value_enabled = p_default_value_enabled;
  4792. emit_changed();
  4793. }
  4794. bool VisualShaderNodeBooleanParameter::is_default_value_enabled() const {
  4795. return default_value_enabled;
  4796. }
  4797. void VisualShaderNodeBooleanParameter::set_default_value(bool p_default_value) {
  4798. if (default_value == p_default_value) {
  4799. return;
  4800. }
  4801. default_value = p_default_value;
  4802. emit_changed();
  4803. }
  4804. bool VisualShaderNodeBooleanParameter::get_default_value() const {
  4805. return default_value;
  4806. }
  4807. String VisualShaderNodeBooleanParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4808. String code = _get_qual_str() + "uniform bool " + get_parameter_name();
  4809. if (default_value_enabled) {
  4810. if (default_value) {
  4811. code += " = true";
  4812. } else {
  4813. code += " = false";
  4814. }
  4815. }
  4816. code += ";\n";
  4817. return code;
  4818. }
  4819. String VisualShaderNodeBooleanParameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4820. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  4821. }
  4822. bool VisualShaderNodeBooleanParameter::is_show_prop_names() const {
  4823. return true;
  4824. }
  4825. bool VisualShaderNodeBooleanParameter::is_use_prop_slots() const {
  4826. return true;
  4827. }
  4828. void VisualShaderNodeBooleanParameter::_bind_methods() {
  4829. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeBooleanParameter::set_default_value_enabled);
  4830. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeBooleanParameter::is_default_value_enabled);
  4831. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeBooleanParameter::set_default_value);
  4832. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeBooleanParameter::get_default_value);
  4833. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  4834. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value"), "set_default_value", "get_default_value");
  4835. }
  4836. bool VisualShaderNodeBooleanParameter::is_qualifier_supported(Qualifier p_qual) const {
  4837. return true; // all qualifiers are supported
  4838. }
  4839. bool VisualShaderNodeBooleanParameter::is_convertible_to_constant() const {
  4840. return true; // conversion is allowed
  4841. }
  4842. Vector<StringName> VisualShaderNodeBooleanParameter::get_editable_properties() const {
  4843. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  4844. props.push_back("default_value_enabled");
  4845. if (default_value_enabled) {
  4846. props.push_back("default_value");
  4847. }
  4848. return props;
  4849. }
  4850. VisualShaderNodeBooleanParameter::VisualShaderNodeBooleanParameter() {
  4851. }
  4852. ////////////// Color Parameter
  4853. String VisualShaderNodeColorParameter::get_caption() const {
  4854. return "ColorParameter";
  4855. }
  4856. int VisualShaderNodeColorParameter::get_input_port_count() const {
  4857. return 0;
  4858. }
  4859. VisualShaderNodeColorParameter::PortType VisualShaderNodeColorParameter::get_input_port_type(int p_port) const {
  4860. return PORT_TYPE_SCALAR;
  4861. }
  4862. String VisualShaderNodeColorParameter::get_input_port_name(int p_port) const {
  4863. return String();
  4864. }
  4865. int VisualShaderNodeColorParameter::get_output_port_count() const {
  4866. return 1;
  4867. }
  4868. VisualShaderNodeColorParameter::PortType VisualShaderNodeColorParameter::get_output_port_type(int p_port) const {
  4869. return p_port == 0 ? PORT_TYPE_VECTOR_4D : PORT_TYPE_SCALAR;
  4870. }
  4871. String VisualShaderNodeColorParameter::get_output_port_name(int p_port) const {
  4872. return "color";
  4873. }
  4874. bool VisualShaderNodeColorParameter::is_output_port_expandable(int p_port) const {
  4875. if (p_port == 0) {
  4876. return true;
  4877. }
  4878. return false;
  4879. }
  4880. void VisualShaderNodeColorParameter::set_default_value_enabled(bool p_enabled) {
  4881. if (default_value_enabled == p_enabled) {
  4882. return;
  4883. }
  4884. default_value_enabled = p_enabled;
  4885. emit_changed();
  4886. }
  4887. bool VisualShaderNodeColorParameter::is_default_value_enabled() const {
  4888. return default_value_enabled;
  4889. }
  4890. void VisualShaderNodeColorParameter::set_default_value(const Color &p_value) {
  4891. if (default_value.is_equal_approx(p_value)) {
  4892. return;
  4893. }
  4894. default_value = p_value;
  4895. emit_changed();
  4896. }
  4897. Color VisualShaderNodeColorParameter::get_default_value() const {
  4898. return default_value;
  4899. }
  4900. String VisualShaderNodeColorParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4901. String code = _get_qual_str() + "uniform vec4 " + get_parameter_name() + " : source_color";
  4902. if (default_value_enabled) {
  4903. code += vformat(" = vec4(%.6f, %.6f, %.6f, %.6f)", default_value.r, default_value.g, default_value.b, default_value.a);
  4904. }
  4905. code += ";\n";
  4906. return code;
  4907. }
  4908. String VisualShaderNodeColorParameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4909. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  4910. }
  4911. bool VisualShaderNodeColorParameter::is_show_prop_names() const {
  4912. return true;
  4913. }
  4914. void VisualShaderNodeColorParameter::_bind_methods() {
  4915. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeColorParameter::set_default_value_enabled);
  4916. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeColorParameter::is_default_value_enabled);
  4917. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeColorParameter::set_default_value);
  4918. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeColorParameter::get_default_value);
  4919. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  4920. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "default_value"), "set_default_value", "get_default_value");
  4921. }
  4922. bool VisualShaderNodeColorParameter::is_qualifier_supported(Qualifier p_qual) const {
  4923. return true; // all qualifiers are supported
  4924. }
  4925. bool VisualShaderNodeColorParameter::is_convertible_to_constant() const {
  4926. return true; // conversion is allowed
  4927. }
  4928. Vector<StringName> VisualShaderNodeColorParameter::get_editable_properties() const {
  4929. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  4930. props.push_back("default_value_enabled");
  4931. if (default_value_enabled) {
  4932. props.push_back("default_value");
  4933. }
  4934. return props;
  4935. }
  4936. VisualShaderNodeColorParameter::VisualShaderNodeColorParameter() {
  4937. }
  4938. ////////////// Vector2 Parameter
  4939. String VisualShaderNodeVec2Parameter::get_caption() const {
  4940. return "Vector2Parameter";
  4941. }
  4942. int VisualShaderNodeVec2Parameter::get_input_port_count() const {
  4943. return 0;
  4944. }
  4945. VisualShaderNodeVec2Parameter::PortType VisualShaderNodeVec2Parameter::get_input_port_type(int p_port) const {
  4946. return PORT_TYPE_VECTOR_2D;
  4947. }
  4948. String VisualShaderNodeVec2Parameter::get_input_port_name(int p_port) const {
  4949. return String();
  4950. }
  4951. int VisualShaderNodeVec2Parameter::get_output_port_count() const {
  4952. return 1;
  4953. }
  4954. VisualShaderNodeVec2Parameter::PortType VisualShaderNodeVec2Parameter::get_output_port_type(int p_port) const {
  4955. return PORT_TYPE_VECTOR_2D;
  4956. }
  4957. String VisualShaderNodeVec2Parameter::get_output_port_name(int p_port) const {
  4958. return String();
  4959. }
  4960. void VisualShaderNodeVec2Parameter::set_default_value_enabled(bool p_enabled) {
  4961. default_value_enabled = p_enabled;
  4962. emit_changed();
  4963. }
  4964. bool VisualShaderNodeVec2Parameter::is_default_value_enabled() const {
  4965. return default_value_enabled;
  4966. }
  4967. void VisualShaderNodeVec2Parameter::set_default_value(const Vector2 &p_value) {
  4968. default_value = p_value;
  4969. emit_changed();
  4970. }
  4971. Vector2 VisualShaderNodeVec2Parameter::get_default_value() const {
  4972. return default_value;
  4973. }
  4974. String VisualShaderNodeVec2Parameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4975. String code = _get_qual_str() + "uniform vec2 " + get_parameter_name();
  4976. if (default_value_enabled) {
  4977. code += vformat(" = vec2(%.6f, %.6f)", default_value.x, default_value.y);
  4978. }
  4979. code += ";\n";
  4980. return code;
  4981. }
  4982. String VisualShaderNodeVec2Parameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4983. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  4984. }
  4985. void VisualShaderNodeVec2Parameter::_bind_methods() {
  4986. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeVec2Parameter::set_default_value_enabled);
  4987. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeVec2Parameter::is_default_value_enabled);
  4988. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeVec2Parameter::set_default_value);
  4989. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeVec2Parameter::get_default_value);
  4990. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  4991. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "default_value"), "set_default_value", "get_default_value");
  4992. }
  4993. bool VisualShaderNodeVec2Parameter::is_show_prop_names() const {
  4994. return true;
  4995. }
  4996. bool VisualShaderNodeVec2Parameter::is_use_prop_slots() const {
  4997. return true;
  4998. }
  4999. bool VisualShaderNodeVec2Parameter::is_qualifier_supported(Qualifier p_qual) const {
  5000. return true; // all qualifiers are supported
  5001. }
  5002. bool VisualShaderNodeVec2Parameter::is_convertible_to_constant() const {
  5003. return true; // conversion is allowed
  5004. }
  5005. Vector<StringName> VisualShaderNodeVec2Parameter::get_editable_properties() const {
  5006. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  5007. props.push_back("default_value_enabled");
  5008. if (default_value_enabled) {
  5009. props.push_back("default_value");
  5010. }
  5011. return props;
  5012. }
  5013. VisualShaderNodeVec2Parameter::VisualShaderNodeVec2Parameter() {
  5014. }
  5015. ////////////// Vector3 Parameter
  5016. String VisualShaderNodeVec3Parameter::get_caption() const {
  5017. return "Vector3Parameter";
  5018. }
  5019. int VisualShaderNodeVec3Parameter::get_input_port_count() const {
  5020. return 0;
  5021. }
  5022. VisualShaderNodeVec3Parameter::PortType VisualShaderNodeVec3Parameter::get_input_port_type(int p_port) const {
  5023. return PORT_TYPE_VECTOR_3D;
  5024. }
  5025. String VisualShaderNodeVec3Parameter::get_input_port_name(int p_port) const {
  5026. return String();
  5027. }
  5028. int VisualShaderNodeVec3Parameter::get_output_port_count() const {
  5029. return 1;
  5030. }
  5031. VisualShaderNodeVec3Parameter::PortType VisualShaderNodeVec3Parameter::get_output_port_type(int p_port) const {
  5032. return PORT_TYPE_VECTOR_3D;
  5033. }
  5034. String VisualShaderNodeVec3Parameter::get_output_port_name(int p_port) const {
  5035. return ""; //no output port means the editor will be used as port
  5036. }
  5037. void VisualShaderNodeVec3Parameter::set_default_value_enabled(bool p_enabled) {
  5038. default_value_enabled = p_enabled;
  5039. emit_changed();
  5040. }
  5041. bool VisualShaderNodeVec3Parameter::is_default_value_enabled() const {
  5042. return default_value_enabled;
  5043. }
  5044. void VisualShaderNodeVec3Parameter::set_default_value(const Vector3 &p_value) {
  5045. default_value = p_value;
  5046. emit_changed();
  5047. }
  5048. Vector3 VisualShaderNodeVec3Parameter::get_default_value() const {
  5049. return default_value;
  5050. }
  5051. String VisualShaderNodeVec3Parameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5052. String code = _get_qual_str() + "uniform vec3 " + get_parameter_name();
  5053. if (default_value_enabled) {
  5054. code += vformat(" = vec3(%.6f, %.6f, %.6f)", default_value.x, default_value.y, default_value.z);
  5055. }
  5056. code += ";\n";
  5057. return code;
  5058. }
  5059. String VisualShaderNodeVec3Parameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5060. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  5061. }
  5062. void VisualShaderNodeVec3Parameter::_bind_methods() {
  5063. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeVec3Parameter::set_default_value_enabled);
  5064. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeVec3Parameter::is_default_value_enabled);
  5065. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeVec3Parameter::set_default_value);
  5066. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeVec3Parameter::get_default_value);
  5067. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  5068. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "default_value"), "set_default_value", "get_default_value");
  5069. }
  5070. bool VisualShaderNodeVec3Parameter::is_show_prop_names() const {
  5071. return true;
  5072. }
  5073. bool VisualShaderNodeVec3Parameter::is_use_prop_slots() const {
  5074. return true;
  5075. }
  5076. bool VisualShaderNodeVec3Parameter::is_qualifier_supported(Qualifier p_qual) const {
  5077. return true; // all qualifiers are supported
  5078. }
  5079. bool VisualShaderNodeVec3Parameter::is_convertible_to_constant() const {
  5080. return true; // conversion is allowed
  5081. }
  5082. Vector<StringName> VisualShaderNodeVec3Parameter::get_editable_properties() const {
  5083. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  5084. props.push_back("default_value_enabled");
  5085. if (default_value_enabled) {
  5086. props.push_back("default_value");
  5087. }
  5088. return props;
  5089. }
  5090. VisualShaderNodeVec3Parameter::VisualShaderNodeVec3Parameter() {
  5091. }
  5092. ////////////// Vector4 Parameter
  5093. String VisualShaderNodeVec4Parameter::get_caption() const {
  5094. return "Vector4Parameter";
  5095. }
  5096. int VisualShaderNodeVec4Parameter::get_input_port_count() const {
  5097. return 0;
  5098. }
  5099. VisualShaderNodeVec4Parameter::PortType VisualShaderNodeVec4Parameter::get_input_port_type(int p_port) const {
  5100. return PORT_TYPE_VECTOR_4D;
  5101. }
  5102. String VisualShaderNodeVec4Parameter::get_input_port_name(int p_port) const {
  5103. return String();
  5104. }
  5105. int VisualShaderNodeVec4Parameter::get_output_port_count() const {
  5106. return 1;
  5107. }
  5108. VisualShaderNodeVec4Parameter::PortType VisualShaderNodeVec4Parameter::get_output_port_type(int p_port) const {
  5109. return PORT_TYPE_VECTOR_4D;
  5110. }
  5111. String VisualShaderNodeVec4Parameter::get_output_port_name(int p_port) const {
  5112. return ""; // No output port means the editor will be used as port.
  5113. }
  5114. void VisualShaderNodeVec4Parameter::set_default_value_enabled(bool p_enabled) {
  5115. default_value_enabled = p_enabled;
  5116. emit_changed();
  5117. }
  5118. bool VisualShaderNodeVec4Parameter::is_default_value_enabled() const {
  5119. return default_value_enabled;
  5120. }
  5121. void VisualShaderNodeVec4Parameter::set_default_value(const Vector4 &p_value) {
  5122. default_value = p_value;
  5123. emit_changed();
  5124. }
  5125. Vector4 VisualShaderNodeVec4Parameter::get_default_value() const {
  5126. return default_value;
  5127. }
  5128. String VisualShaderNodeVec4Parameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5129. String code = _get_qual_str() + "uniform vec4 " + get_parameter_name();
  5130. if (default_value_enabled) {
  5131. code += vformat(" = vec4(%.6f, %.6f, %.6f, %.6f)", default_value.x, default_value.y, default_value.z, default_value.w);
  5132. }
  5133. code += ";\n";
  5134. return code;
  5135. }
  5136. String VisualShaderNodeVec4Parameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5137. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  5138. }
  5139. void VisualShaderNodeVec4Parameter::_bind_methods() {
  5140. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeVec4Parameter::set_default_value_enabled);
  5141. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeVec4Parameter::is_default_value_enabled);
  5142. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeVec4Parameter::set_default_value);
  5143. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeVec4Parameter::get_default_value);
  5144. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  5145. ADD_PROPERTY(PropertyInfo(Variant::VECTOR4, "default_value"), "set_default_value", "get_default_value");
  5146. }
  5147. bool VisualShaderNodeVec4Parameter::is_show_prop_names() const {
  5148. return true;
  5149. }
  5150. bool VisualShaderNodeVec4Parameter::is_use_prop_slots() const {
  5151. return true;
  5152. }
  5153. bool VisualShaderNodeVec4Parameter::is_qualifier_supported(Qualifier p_qual) const {
  5154. return true; // All qualifiers are supported.
  5155. }
  5156. bool VisualShaderNodeVec4Parameter::is_convertible_to_constant() const {
  5157. return true; // Conversion is allowed.
  5158. }
  5159. Vector<StringName> VisualShaderNodeVec4Parameter::get_editable_properties() const {
  5160. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  5161. props.push_back("default_value_enabled");
  5162. if (default_value_enabled) {
  5163. props.push_back("default_value");
  5164. }
  5165. return props;
  5166. }
  5167. VisualShaderNodeVec4Parameter::VisualShaderNodeVec4Parameter() {
  5168. }
  5169. ////////////// Transform Parameter
  5170. String VisualShaderNodeTransformParameter::get_caption() const {
  5171. return "TransformParameter";
  5172. }
  5173. int VisualShaderNodeTransformParameter::get_input_port_count() const {
  5174. return 0;
  5175. }
  5176. VisualShaderNodeTransformParameter::PortType VisualShaderNodeTransformParameter::get_input_port_type(int p_port) const {
  5177. return PORT_TYPE_VECTOR_3D;
  5178. }
  5179. String VisualShaderNodeTransformParameter::get_input_port_name(int p_port) const {
  5180. return String();
  5181. }
  5182. int VisualShaderNodeTransformParameter::get_output_port_count() const {
  5183. return 1;
  5184. }
  5185. VisualShaderNodeTransformParameter::PortType VisualShaderNodeTransformParameter::get_output_port_type(int p_port) const {
  5186. return PORT_TYPE_TRANSFORM;
  5187. }
  5188. String VisualShaderNodeTransformParameter::get_output_port_name(int p_port) const {
  5189. return ""; //no output port means the editor will be used as port
  5190. }
  5191. void VisualShaderNodeTransformParameter::set_default_value_enabled(bool p_enabled) {
  5192. default_value_enabled = p_enabled;
  5193. emit_changed();
  5194. }
  5195. bool VisualShaderNodeTransformParameter::is_default_value_enabled() const {
  5196. return default_value_enabled;
  5197. }
  5198. void VisualShaderNodeTransformParameter::set_default_value(const Transform3D &p_value) {
  5199. default_value = p_value;
  5200. emit_changed();
  5201. }
  5202. Transform3D VisualShaderNodeTransformParameter::get_default_value() const {
  5203. return default_value;
  5204. }
  5205. String VisualShaderNodeTransformParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5206. String code = _get_qual_str() + "uniform mat4 " + get_parameter_name();
  5207. if (default_value_enabled) {
  5208. Vector3 row0 = default_value.basis.rows[0];
  5209. Vector3 row1 = default_value.basis.rows[1];
  5210. Vector3 row2 = default_value.basis.rows[2];
  5211. Vector3 origin = default_value.origin;
  5212. code += " = mat4(" + vformat("vec4(%.6f, %.6f, %.6f, 0.0)", row0.x, row0.y, row0.z) + vformat(", vec4(%.6f, %.6f, %.6f, 0.0)", row1.x, row1.y, row1.z) + vformat(", vec4(%.6f, %.6f, %.6f, 0.0)", row2.x, row2.y, row2.z) + vformat(", vec4(%.6f, %.6f, %.6f, 1.0)", origin.x, origin.y, origin.z) + ")";
  5213. }
  5214. code += ";\n";
  5215. return code;
  5216. }
  5217. String VisualShaderNodeTransformParameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5218. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  5219. }
  5220. void VisualShaderNodeTransformParameter::_bind_methods() {
  5221. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeTransformParameter::set_default_value_enabled);
  5222. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeTransformParameter::is_default_value_enabled);
  5223. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeTransformParameter::set_default_value);
  5224. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeTransformParameter::get_default_value);
  5225. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  5226. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "default_value"), "set_default_value", "get_default_value");
  5227. }
  5228. bool VisualShaderNodeTransformParameter::is_show_prop_names() const {
  5229. return true;
  5230. }
  5231. bool VisualShaderNodeTransformParameter::is_use_prop_slots() const {
  5232. return true;
  5233. }
  5234. bool VisualShaderNodeTransformParameter::is_qualifier_supported(Qualifier p_qual) const {
  5235. if (p_qual == Qualifier::QUAL_INSTANCE) {
  5236. return false;
  5237. }
  5238. return true;
  5239. }
  5240. bool VisualShaderNodeTransformParameter::is_convertible_to_constant() const {
  5241. return true; // conversion is allowed
  5242. }
  5243. Vector<StringName> VisualShaderNodeTransformParameter::get_editable_properties() const {
  5244. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  5245. props.push_back("default_value_enabled");
  5246. if (default_value_enabled) {
  5247. props.push_back("default_value");
  5248. }
  5249. return props;
  5250. }
  5251. VisualShaderNodeTransformParameter::VisualShaderNodeTransformParameter() {
  5252. }
  5253. //////////////
  5254. String get_sampler_hint(VisualShaderNodeTextureParameter::TextureType p_texture_type, VisualShaderNodeTextureParameter::ColorDefault p_color_default, VisualShaderNodeTextureParameter::TextureFilter p_texture_filter, VisualShaderNodeTextureParameter::TextureRepeat p_texture_repeat, VisualShaderNodeTextureParameter::TextureSource p_texture_source) {
  5255. String code;
  5256. bool has_colon = false;
  5257. // type
  5258. {
  5259. String type_code;
  5260. switch (p_texture_type) {
  5261. case VisualShaderNodeTextureParameter::TYPE_DATA:
  5262. if (p_color_default == VisualShaderNodeTextureParameter::COLOR_DEFAULT_BLACK) {
  5263. type_code = "hint_default_black";
  5264. } else if (p_color_default == VisualShaderNodeTextureParameter::COLOR_DEFAULT_TRANSPARENT) {
  5265. type_code = "hint_default_transparent";
  5266. }
  5267. break;
  5268. case VisualShaderNodeTextureParameter::TYPE_COLOR:
  5269. type_code = "source_color";
  5270. if (p_color_default == VisualShaderNodeTextureParameter::COLOR_DEFAULT_BLACK) {
  5271. type_code += ", hint_default_black";
  5272. } else if (p_color_default == VisualShaderNodeTextureParameter::COLOR_DEFAULT_TRANSPARENT) {
  5273. type_code += ", hint_default_transparent";
  5274. }
  5275. break;
  5276. case VisualShaderNodeTextureParameter::TYPE_NORMAL_MAP:
  5277. type_code = "hint_normal";
  5278. break;
  5279. case VisualShaderNodeTextureParameter::TYPE_ANISOTROPY:
  5280. type_code = "hint_anisotropy";
  5281. break;
  5282. default:
  5283. break;
  5284. }
  5285. if (!type_code.is_empty()) {
  5286. code += " : " + type_code;
  5287. has_colon = true;
  5288. }
  5289. }
  5290. // filter
  5291. {
  5292. String filter_code;
  5293. switch (p_texture_filter) {
  5294. case VisualShaderNodeTextureParameter::FILTER_NEAREST:
  5295. filter_code = "filter_nearest";
  5296. break;
  5297. case VisualShaderNodeTextureParameter::FILTER_LINEAR:
  5298. filter_code = "filter_linear";
  5299. break;
  5300. case VisualShaderNodeTextureParameter::FILTER_NEAREST_MIPMAP:
  5301. filter_code = "filter_nearest_mipmap";
  5302. break;
  5303. case VisualShaderNodeTextureParameter::FILTER_LINEAR_MIPMAP:
  5304. filter_code = "filter_linear_mipmap";
  5305. break;
  5306. case VisualShaderNodeTextureParameter::FILTER_NEAREST_MIPMAP_ANISOTROPIC:
  5307. filter_code = "filter_nearest_mipmap_anisotropic";
  5308. break;
  5309. case VisualShaderNodeTextureParameter::FILTER_LINEAR_MIPMAP_ANISOTROPIC:
  5310. filter_code = "filter_linear_mipmap_anisotropic";
  5311. break;
  5312. default:
  5313. break;
  5314. }
  5315. if (!filter_code.is_empty()) {
  5316. if (!has_colon) {
  5317. code += " : ";
  5318. has_colon = true;
  5319. } else {
  5320. code += ", ";
  5321. }
  5322. code += filter_code;
  5323. }
  5324. }
  5325. // repeat
  5326. {
  5327. String repeat_code;
  5328. switch (p_texture_repeat) {
  5329. case VisualShaderNodeTextureParameter::REPEAT_ENABLED:
  5330. repeat_code = "repeat_enable";
  5331. break;
  5332. case VisualShaderNodeTextureParameter::REPEAT_DISABLED:
  5333. repeat_code = "repeat_disable";
  5334. break;
  5335. default:
  5336. break;
  5337. }
  5338. if (!repeat_code.is_empty()) {
  5339. if (!has_colon) {
  5340. code += " : ";
  5341. } else {
  5342. code += ", ";
  5343. }
  5344. code += repeat_code;
  5345. }
  5346. }
  5347. {
  5348. String source_code;
  5349. switch (p_texture_source) {
  5350. case VisualShaderNodeTextureParameter::SOURCE_SCREEN:
  5351. source_code = "hint_screen_texture";
  5352. break;
  5353. case VisualShaderNodeTextureParameter::SOURCE_DEPTH:
  5354. source_code = "hint_depth_texture";
  5355. break;
  5356. case VisualShaderNodeTextureParameter::SOURCE_NORMAL_ROUGHNESS:
  5357. source_code = "hint_normal_roughness_texture";
  5358. break;
  5359. default:
  5360. break;
  5361. }
  5362. if (!source_code.is_empty()) {
  5363. if (!has_colon) {
  5364. code += " : ";
  5365. } else {
  5366. code += ", ";
  5367. }
  5368. code += source_code;
  5369. }
  5370. }
  5371. return code;
  5372. }
  5373. ////////////// Texture Parameter
  5374. int VisualShaderNodeTextureParameter::get_input_port_count() const {
  5375. return 0;
  5376. }
  5377. VisualShaderNodeTextureParameter::PortType VisualShaderNodeTextureParameter::get_input_port_type(int p_port) const {
  5378. return PORT_TYPE_SCALAR;
  5379. }
  5380. String VisualShaderNodeTextureParameter::get_input_port_name(int p_port) const {
  5381. return "";
  5382. }
  5383. int VisualShaderNodeTextureParameter::get_output_port_count() const {
  5384. return 1;
  5385. }
  5386. VisualShaderNodeTextureParameter::PortType VisualShaderNodeTextureParameter::get_output_port_type(int p_port) const {
  5387. switch (p_port) {
  5388. case 0:
  5389. return PORT_TYPE_SAMPLER;
  5390. default:
  5391. return PORT_TYPE_SCALAR;
  5392. }
  5393. }
  5394. String VisualShaderNodeTextureParameter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5395. return "";
  5396. }
  5397. void VisualShaderNodeTextureParameter::set_texture_type(TextureType p_texture_type) {
  5398. ERR_FAIL_INDEX(int(p_texture_type), int(TYPE_MAX));
  5399. if (texture_type == p_texture_type) {
  5400. return;
  5401. }
  5402. texture_type = p_texture_type;
  5403. emit_changed();
  5404. }
  5405. VisualShaderNodeTextureParameter::TextureType VisualShaderNodeTextureParameter::get_texture_type() const {
  5406. return texture_type;
  5407. }
  5408. void VisualShaderNodeTextureParameter::set_color_default(ColorDefault p_color_default) {
  5409. ERR_FAIL_INDEX(int(p_color_default), int(COLOR_DEFAULT_MAX));
  5410. if (color_default == p_color_default) {
  5411. return;
  5412. }
  5413. color_default = p_color_default;
  5414. emit_changed();
  5415. }
  5416. VisualShaderNodeTextureParameter::ColorDefault VisualShaderNodeTextureParameter::get_color_default() const {
  5417. return color_default;
  5418. }
  5419. void VisualShaderNodeTextureParameter::set_texture_filter(TextureFilter p_filter) {
  5420. ERR_FAIL_INDEX(int(p_filter), int(FILTER_MAX));
  5421. if (texture_filter == p_filter) {
  5422. return;
  5423. }
  5424. texture_filter = p_filter;
  5425. emit_changed();
  5426. }
  5427. VisualShaderNodeTextureParameter::TextureFilter VisualShaderNodeTextureParameter::get_texture_filter() const {
  5428. return texture_filter;
  5429. }
  5430. void VisualShaderNodeTextureParameter::set_texture_repeat(TextureRepeat p_repeat) {
  5431. ERR_FAIL_INDEX(int(p_repeat), int(REPEAT_MAX));
  5432. if (texture_repeat == p_repeat) {
  5433. return;
  5434. }
  5435. texture_repeat = p_repeat;
  5436. emit_changed();
  5437. }
  5438. VisualShaderNodeTextureParameter::TextureRepeat VisualShaderNodeTextureParameter::get_texture_repeat() const {
  5439. return texture_repeat;
  5440. }
  5441. void VisualShaderNodeTextureParameter::set_texture_source(TextureSource p_source) {
  5442. ERR_FAIL_INDEX(int(p_source), int(SOURCE_MAX));
  5443. if (texture_source == p_source) {
  5444. return;
  5445. }
  5446. texture_source = p_source;
  5447. emit_changed();
  5448. }
  5449. VisualShaderNodeTextureParameter::TextureSource VisualShaderNodeTextureParameter::get_texture_source() const {
  5450. return texture_source;
  5451. }
  5452. Vector<StringName> VisualShaderNodeTextureParameter::get_editable_properties() const {
  5453. Vector<StringName> props = VisualShaderNodeParameter::get_editable_properties();
  5454. props.push_back("texture_type");
  5455. if (texture_type == TYPE_DATA || texture_type == TYPE_COLOR) {
  5456. props.push_back("color_default");
  5457. }
  5458. props.push_back("texture_filter");
  5459. props.push_back("texture_repeat");
  5460. props.push_back("texture_source");
  5461. return props;
  5462. }
  5463. bool VisualShaderNodeTextureParameter::is_show_prop_names() const {
  5464. return true;
  5465. }
  5466. HashMap<StringName, String> VisualShaderNodeTextureParameter::get_editable_properties_names() const {
  5467. HashMap<StringName, String> names;
  5468. names.insert("texture_type", RTR("Type"));
  5469. names.insert("color_default", RTR("Default Color"));
  5470. names.insert("texture_filter", RTR("Filter"));
  5471. names.insert("texture_repeat", RTR("Repeat"));
  5472. names.insert("texture_source", RTR("Source"));
  5473. return names;
  5474. }
  5475. void VisualShaderNodeTextureParameter::_bind_methods() {
  5476. ClassDB::bind_method(D_METHOD("set_texture_type", "type"), &VisualShaderNodeTextureParameter::set_texture_type);
  5477. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeTextureParameter::get_texture_type);
  5478. ClassDB::bind_method(D_METHOD("set_color_default", "color"), &VisualShaderNodeTextureParameter::set_color_default);
  5479. ClassDB::bind_method(D_METHOD("get_color_default"), &VisualShaderNodeTextureParameter::get_color_default);
  5480. ClassDB::bind_method(D_METHOD("set_texture_filter", "filter"), &VisualShaderNodeTextureParameter::set_texture_filter);
  5481. ClassDB::bind_method(D_METHOD("get_texture_filter"), &VisualShaderNodeTextureParameter::get_texture_filter);
  5482. ClassDB::bind_method(D_METHOD("set_texture_repeat", "repeat"), &VisualShaderNodeTextureParameter::set_texture_repeat);
  5483. ClassDB::bind_method(D_METHOD("get_texture_repeat"), &VisualShaderNodeTextureParameter::get_texture_repeat);
  5484. ClassDB::bind_method(D_METHOD("set_texture_source", "source"), &VisualShaderNodeTextureParameter::set_texture_source);
  5485. ClassDB::bind_method(D_METHOD("get_texture_source"), &VisualShaderNodeTextureParameter::get_texture_source);
  5486. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normal Map,Anisotropic"), "set_texture_type", "get_texture_type");
  5487. ADD_PROPERTY(PropertyInfo(Variant::INT, "color_default", PROPERTY_HINT_ENUM, "White,Black,Transparent"), "set_color_default", "get_color_default");
  5488. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_filter", PROPERTY_HINT_ENUM, "Default,Nearest,Linear,Nearest Mipmap,Linear Mipmap,Nearest Mipmap Anisotropic,Linear Mipmap Anisotropic"), "set_texture_filter", "get_texture_filter");
  5489. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_repeat", PROPERTY_HINT_ENUM, "Default,Enabled,Disabled"), "set_texture_repeat", "get_texture_repeat");
  5490. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_source", PROPERTY_HINT_ENUM, "None,Screen,Depth,NormalRoughness"), "set_texture_source", "get_texture_source");
  5491. BIND_ENUM_CONSTANT(TYPE_DATA);
  5492. BIND_ENUM_CONSTANT(TYPE_COLOR);
  5493. BIND_ENUM_CONSTANT(TYPE_NORMAL_MAP);
  5494. BIND_ENUM_CONSTANT(TYPE_ANISOTROPY);
  5495. BIND_ENUM_CONSTANT(TYPE_MAX);
  5496. BIND_ENUM_CONSTANT(COLOR_DEFAULT_WHITE);
  5497. BIND_ENUM_CONSTANT(COLOR_DEFAULT_BLACK);
  5498. BIND_ENUM_CONSTANT(COLOR_DEFAULT_TRANSPARENT);
  5499. BIND_ENUM_CONSTANT(COLOR_DEFAULT_MAX);
  5500. BIND_ENUM_CONSTANT(FILTER_DEFAULT);
  5501. BIND_ENUM_CONSTANT(FILTER_NEAREST);
  5502. BIND_ENUM_CONSTANT(FILTER_LINEAR);
  5503. BIND_ENUM_CONSTANT(FILTER_NEAREST_MIPMAP);
  5504. BIND_ENUM_CONSTANT(FILTER_LINEAR_MIPMAP);
  5505. BIND_ENUM_CONSTANT(FILTER_NEAREST_MIPMAP_ANISOTROPIC);
  5506. BIND_ENUM_CONSTANT(FILTER_LINEAR_MIPMAP_ANISOTROPIC);
  5507. BIND_ENUM_CONSTANT(FILTER_MAX);
  5508. BIND_ENUM_CONSTANT(REPEAT_DEFAULT);
  5509. BIND_ENUM_CONSTANT(REPEAT_ENABLED);
  5510. BIND_ENUM_CONSTANT(REPEAT_DISABLED);
  5511. BIND_ENUM_CONSTANT(REPEAT_MAX);
  5512. BIND_ENUM_CONSTANT(SOURCE_NONE);
  5513. BIND_ENUM_CONSTANT(SOURCE_SCREEN);
  5514. BIND_ENUM_CONSTANT(SOURCE_DEPTH);
  5515. BIND_ENUM_CONSTANT(SOURCE_NORMAL_ROUGHNESS);
  5516. BIND_ENUM_CONSTANT(SOURCE_MAX);
  5517. }
  5518. bool VisualShaderNodeTextureParameter::is_qualifier_supported(Qualifier p_qual) const {
  5519. switch (p_qual) {
  5520. case Qualifier::QUAL_NONE:
  5521. return true;
  5522. case Qualifier::QUAL_GLOBAL:
  5523. return true;
  5524. case Qualifier::QUAL_INSTANCE:
  5525. return false;
  5526. default:
  5527. break;
  5528. }
  5529. return false;
  5530. }
  5531. bool VisualShaderNodeTextureParameter::is_convertible_to_constant() const {
  5532. return false; // conversion is not allowed
  5533. }
  5534. VisualShaderNodeTextureParameter::VisualShaderNodeTextureParameter() {
  5535. }
  5536. ////////////// Texture2D Parameter
  5537. String VisualShaderNodeTexture2DParameter::get_caption() const {
  5538. return "Texture2DParameter";
  5539. }
  5540. String VisualShaderNodeTexture2DParameter::get_output_port_name(int p_port) const {
  5541. switch (p_port) {
  5542. case 0:
  5543. return "sampler2D";
  5544. default:
  5545. return "";
  5546. }
  5547. }
  5548. String VisualShaderNodeTexture2DParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5549. String code = _get_qual_str() + "uniform sampler2D " + get_parameter_name();
  5550. code += get_sampler_hint(texture_type, color_default, texture_filter, texture_repeat, texture_source);
  5551. code += ";\n";
  5552. return code;
  5553. }
  5554. VisualShaderNodeTexture2DParameter::VisualShaderNodeTexture2DParameter() {
  5555. }
  5556. ////////////// Texture Parameter (Triplanar)
  5557. String VisualShaderNodeTextureParameterTriplanar::get_caption() const {
  5558. return "TextureParameterTriplanar";
  5559. }
  5560. int VisualShaderNodeTextureParameterTriplanar::get_input_port_count() const {
  5561. return 2;
  5562. }
  5563. VisualShaderNodeTextureParameterTriplanar::PortType VisualShaderNodeTextureParameterTriplanar::get_input_port_type(int p_port) const {
  5564. if (p_port == 0 || p_port == 1) {
  5565. return PORT_TYPE_VECTOR_3D;
  5566. }
  5567. return PORT_TYPE_SCALAR;
  5568. }
  5569. String VisualShaderNodeTextureParameterTriplanar::get_input_port_name(int p_port) const {
  5570. if (p_port == 0) {
  5571. return "weights";
  5572. } else if (p_port == 1) {
  5573. return "pos";
  5574. }
  5575. return "";
  5576. }
  5577. int VisualShaderNodeTextureParameterTriplanar::get_output_port_count() const {
  5578. return 2;
  5579. }
  5580. VisualShaderNodeTextureParameterTriplanar::PortType VisualShaderNodeTextureParameterTriplanar::get_output_port_type(int p_port) const {
  5581. switch (p_port) {
  5582. case 0:
  5583. return PORT_TYPE_VECTOR_4D;
  5584. case 1:
  5585. return PORT_TYPE_SAMPLER;
  5586. default:
  5587. return PORT_TYPE_SCALAR;
  5588. }
  5589. }
  5590. String VisualShaderNodeTextureParameterTriplanar::get_output_port_name(int p_port) const {
  5591. switch (p_port) {
  5592. case 0:
  5593. return "color";
  5594. case 1:
  5595. return "sampler2D";
  5596. default:
  5597. return "";
  5598. }
  5599. }
  5600. String VisualShaderNodeTextureParameterTriplanar::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  5601. String code;
  5602. code += "// " + get_caption() + "\n";
  5603. code += " vec4 triplanar_texture(sampler2D p_sampler, vec3 p_weights, vec3 p_triplanar_pos) {\n";
  5604. code += " vec4 samp = vec4(0.0);\n";
  5605. code += " samp += texture(p_sampler, p_triplanar_pos.xy) * p_weights.z;\n";
  5606. code += " samp += texture(p_sampler, p_triplanar_pos.xz) * p_weights.y;\n";
  5607. code += " samp += texture(p_sampler, p_triplanar_pos.zy * vec2(-1.0, 1.0)) * p_weights.x;\n";
  5608. code += " return samp;\n";
  5609. code += " }\n";
  5610. code += "\n";
  5611. code += " uniform vec3 triplanar_scale = vec3(1.0, 1.0, 1.0);\n";
  5612. code += " uniform vec3 triplanar_offset;\n";
  5613. code += " uniform float triplanar_sharpness = 0.5;\n";
  5614. code += "\n";
  5615. code += " varying vec3 triplanar_power_normal;\n";
  5616. code += " varying vec3 triplanar_pos;\n";
  5617. return code;
  5618. }
  5619. String VisualShaderNodeTextureParameterTriplanar::generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5620. String code;
  5621. if (p_type == VisualShader::TYPE_VERTEX) {
  5622. code += "// " + get_caption() + "\n";
  5623. code += " {\n";
  5624. code += " triplanar_power_normal = pow(abs(NORMAL), vec3(triplanar_sharpness));\n";
  5625. code += " triplanar_power_normal /= dot(triplanar_power_normal, vec3(1.0));\n";
  5626. code += " triplanar_pos = VERTEX * triplanar_scale + triplanar_offset;\n";
  5627. code += " triplanar_pos *= vec3(1.0, -1.0, 1.0);\n";
  5628. code += " }\n";
  5629. }
  5630. return code;
  5631. }
  5632. String VisualShaderNodeTextureParameterTriplanar::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5633. String code = _get_qual_str() + "uniform sampler2D " + get_parameter_name();
  5634. code += get_sampler_hint(texture_type, color_default, texture_filter, texture_repeat, texture_source);
  5635. code += ";\n";
  5636. return code;
  5637. }
  5638. String VisualShaderNodeTextureParameterTriplanar::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5639. String id = get_parameter_name();
  5640. String code;
  5641. if (p_input_vars[0].is_empty() && p_input_vars[1].is_empty()) {
  5642. code += " " + p_output_vars[0] + " = triplanar_texture(" + id + ", triplanar_power_normal, triplanar_pos);\n";
  5643. } else if (!p_input_vars[0].is_empty() && p_input_vars[1].is_empty()) {
  5644. code += " " + p_output_vars[0] + " = triplanar_texture(" + id + ", " + p_input_vars[0] + ", triplanar_pos);\n";
  5645. } else if (p_input_vars[0].is_empty() && !p_input_vars[1].is_empty()) {
  5646. code += " " + p_output_vars[0] + " = triplanar_texture(" + id + ", triplanar_power_normal, " + p_input_vars[1] + ");\n";
  5647. } else {
  5648. code += " " + p_output_vars[0] + " = triplanar_texture(" + id + ", " + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  5649. }
  5650. return code;
  5651. }
  5652. bool VisualShaderNodeTextureParameterTriplanar::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  5653. if (p_port == 0) {
  5654. return true;
  5655. } else if (p_port == 1) {
  5656. return true;
  5657. }
  5658. return false;
  5659. }
  5660. VisualShaderNodeTextureParameterTriplanar::VisualShaderNodeTextureParameterTriplanar() {
  5661. }
  5662. ////////////// Texture2DArray Parameter
  5663. String VisualShaderNodeTexture2DArrayParameter::get_caption() const {
  5664. return "Texture2DArrayParameter";
  5665. }
  5666. String VisualShaderNodeTexture2DArrayParameter::get_output_port_name(int p_port) const {
  5667. return "sampler2DArray";
  5668. }
  5669. String VisualShaderNodeTexture2DArrayParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5670. String code = _get_qual_str() + "uniform sampler2DArray " + get_parameter_name();
  5671. code += get_sampler_hint(texture_type, color_default, texture_filter, texture_repeat, texture_source);
  5672. code += ";\n";
  5673. return code;
  5674. }
  5675. VisualShaderNodeTexture2DArrayParameter::VisualShaderNodeTexture2DArrayParameter() {
  5676. }
  5677. ////////////// Texture3D Parameter
  5678. String VisualShaderNodeTexture3DParameter::get_caption() const {
  5679. return "Texture3DParameter";
  5680. }
  5681. String VisualShaderNodeTexture3DParameter::get_output_port_name(int p_port) const {
  5682. return "sampler3D";
  5683. }
  5684. String VisualShaderNodeTexture3DParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5685. String code = _get_qual_str() + "uniform sampler3D " + get_parameter_name();
  5686. code += get_sampler_hint(texture_type, color_default, texture_filter, texture_repeat, texture_source);
  5687. code += ";\n";
  5688. return code;
  5689. }
  5690. VisualShaderNodeTexture3DParameter::VisualShaderNodeTexture3DParameter() {
  5691. }
  5692. ////////////// Cubemap Parameter
  5693. String VisualShaderNodeCubemapParameter::get_caption() const {
  5694. return "CubemapParameter";
  5695. }
  5696. String VisualShaderNodeCubemapParameter::get_output_port_name(int p_port) const {
  5697. return "samplerCube";
  5698. }
  5699. String VisualShaderNodeCubemapParameter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  5700. String code = _get_qual_str() + "uniform samplerCube " + get_parameter_name();
  5701. code += get_sampler_hint(texture_type, color_default, texture_filter, texture_repeat, texture_source);
  5702. code += ";\n";
  5703. return code;
  5704. }
  5705. VisualShaderNodeCubemapParameter::VisualShaderNodeCubemapParameter() {
  5706. }
  5707. ////////////// If
  5708. String VisualShaderNodeIf::get_caption() const {
  5709. return "If";
  5710. }
  5711. int VisualShaderNodeIf::get_input_port_count() const {
  5712. return 6;
  5713. }
  5714. VisualShaderNodeIf::PortType VisualShaderNodeIf::get_input_port_type(int p_port) const {
  5715. if (p_port == 0 || p_port == 1 || p_port == 2) {
  5716. return PORT_TYPE_SCALAR;
  5717. }
  5718. return PORT_TYPE_VECTOR_3D;
  5719. }
  5720. String VisualShaderNodeIf::get_input_port_name(int p_port) const {
  5721. switch (p_port) {
  5722. case 0:
  5723. return "a";
  5724. case 1:
  5725. return "b";
  5726. case 2:
  5727. return "tolerance";
  5728. case 3:
  5729. return "a == b";
  5730. case 4:
  5731. return "a > b";
  5732. case 5:
  5733. return "a < b";
  5734. default:
  5735. return "";
  5736. }
  5737. }
  5738. int VisualShaderNodeIf::get_output_port_count() const {
  5739. return 1;
  5740. }
  5741. VisualShaderNodeIf::PortType VisualShaderNodeIf::get_output_port_type(int p_port) const {
  5742. return PORT_TYPE_VECTOR_3D;
  5743. }
  5744. String VisualShaderNodeIf::get_output_port_name(int p_port) const {
  5745. return "result";
  5746. }
  5747. String VisualShaderNodeIf::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5748. String code;
  5749. code += " if(abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ") < " + p_input_vars[2] + ")\n"; // abs(a - b) < tolerance eg. a == b
  5750. code += " {\n";
  5751. code += " " + p_output_vars[0] + " = " + p_input_vars[3] + ";\n";
  5752. code += " }\n";
  5753. code += " else if(" + p_input_vars[0] + " < " + p_input_vars[1] + ")\n"; // a < b
  5754. code += " {\n";
  5755. code += " " + p_output_vars[0] + " = " + p_input_vars[5] + ";\n";
  5756. code += " }\n";
  5757. code += " else\n"; // a > b (or a >= b if abs(a - b) < tolerance is false)
  5758. code += " {\n";
  5759. code += " " + p_output_vars[0] + " = " + p_input_vars[4] + ";\n";
  5760. code += " }\n";
  5761. return code;
  5762. }
  5763. VisualShaderNodeIf::VisualShaderNodeIf() {
  5764. simple_decl = false;
  5765. set_input_port_default_value(0, 0.0);
  5766. set_input_port_default_value(1, 0.0);
  5767. set_input_port_default_value(2, CMP_EPSILON);
  5768. set_input_port_default_value(3, Vector3(0.0, 0.0, 0.0));
  5769. set_input_port_default_value(4, Vector3(0.0, 0.0, 0.0));
  5770. set_input_port_default_value(5, Vector3(0.0, 0.0, 0.0));
  5771. }
  5772. ////////////// Switch
  5773. String VisualShaderNodeSwitch::get_caption() const {
  5774. return "Switch";
  5775. }
  5776. int VisualShaderNodeSwitch::get_input_port_count() const {
  5777. return 3;
  5778. }
  5779. VisualShaderNodeSwitch::PortType VisualShaderNodeSwitch::get_input_port_type(int p_port) const {
  5780. if (p_port == 0) {
  5781. return PORT_TYPE_BOOLEAN;
  5782. }
  5783. if (p_port == 1 || p_port == 2) {
  5784. switch (op_type) {
  5785. case OP_TYPE_INT:
  5786. return PORT_TYPE_SCALAR_INT;
  5787. case OP_TYPE_UINT:
  5788. return PORT_TYPE_SCALAR_UINT;
  5789. case OP_TYPE_VECTOR_2D:
  5790. return PORT_TYPE_VECTOR_2D;
  5791. case OP_TYPE_VECTOR_3D:
  5792. return PORT_TYPE_VECTOR_3D;
  5793. case OP_TYPE_VECTOR_4D:
  5794. return PORT_TYPE_VECTOR_4D;
  5795. case OP_TYPE_BOOLEAN:
  5796. return PORT_TYPE_BOOLEAN;
  5797. case OP_TYPE_TRANSFORM:
  5798. return PORT_TYPE_TRANSFORM;
  5799. default:
  5800. break;
  5801. }
  5802. }
  5803. return PORT_TYPE_SCALAR;
  5804. }
  5805. String VisualShaderNodeSwitch::get_input_port_name(int p_port) const {
  5806. switch (p_port) {
  5807. case 0:
  5808. return "value";
  5809. case 1:
  5810. return "true";
  5811. case 2:
  5812. return "false";
  5813. default:
  5814. return "";
  5815. }
  5816. }
  5817. int VisualShaderNodeSwitch::get_output_port_count() const {
  5818. return 1;
  5819. }
  5820. VisualShaderNodeSwitch::PortType VisualShaderNodeSwitch::get_output_port_type(int p_port) const {
  5821. switch (op_type) {
  5822. case OP_TYPE_INT:
  5823. return PORT_TYPE_SCALAR_INT;
  5824. case OP_TYPE_UINT:
  5825. return PORT_TYPE_SCALAR_UINT;
  5826. case OP_TYPE_VECTOR_2D:
  5827. return PORT_TYPE_VECTOR_2D;
  5828. case OP_TYPE_VECTOR_3D:
  5829. return PORT_TYPE_VECTOR_3D;
  5830. case OP_TYPE_VECTOR_4D:
  5831. return PORT_TYPE_VECTOR_4D;
  5832. case OP_TYPE_BOOLEAN:
  5833. return PORT_TYPE_BOOLEAN;
  5834. case OP_TYPE_TRANSFORM:
  5835. return PORT_TYPE_TRANSFORM;
  5836. default:
  5837. break;
  5838. }
  5839. return PORT_TYPE_SCALAR;
  5840. }
  5841. String VisualShaderNodeSwitch::get_output_port_name(int p_port) const {
  5842. return "result";
  5843. }
  5844. void VisualShaderNodeSwitch::set_op_type(OpType p_op_type) {
  5845. ERR_FAIL_INDEX(int(p_op_type), int(OP_TYPE_MAX));
  5846. if (op_type == p_op_type) {
  5847. return;
  5848. }
  5849. switch (p_op_type) {
  5850. case OP_TYPE_FLOAT:
  5851. set_input_port_default_value(1, 1.0, get_input_port_default_value(1));
  5852. set_input_port_default_value(2, 0.0, get_input_port_default_value(2));
  5853. break;
  5854. case OP_TYPE_UINT:
  5855. case OP_TYPE_INT:
  5856. set_input_port_default_value(1, 1, get_input_port_default_value(1));
  5857. set_input_port_default_value(2, 0, get_input_port_default_value(2));
  5858. break;
  5859. case OP_TYPE_VECTOR_2D:
  5860. set_input_port_default_value(1, Vector2(1.0, 1.0), get_input_port_default_value(1));
  5861. set_input_port_default_value(2, Vector2(0.0, 0.0), get_input_port_default_value(2));
  5862. break;
  5863. case OP_TYPE_VECTOR_3D:
  5864. set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0), get_input_port_default_value(1));
  5865. set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0), get_input_port_default_value(2));
  5866. break;
  5867. case OP_TYPE_VECTOR_4D:
  5868. set_input_port_default_value(1, Quaternion(1.0, 1.0, 1.0, 1.0), get_input_port_default_value(1));
  5869. set_input_port_default_value(2, Quaternion(0.0, 0.0, 0.0, 0.0), get_input_port_default_value(2));
  5870. break;
  5871. case OP_TYPE_BOOLEAN:
  5872. set_input_port_default_value(1, true);
  5873. set_input_port_default_value(2, false);
  5874. break;
  5875. case OP_TYPE_TRANSFORM:
  5876. set_input_port_default_value(1, Transform3D());
  5877. set_input_port_default_value(2, Transform3D());
  5878. break;
  5879. default:
  5880. break;
  5881. }
  5882. op_type = p_op_type;
  5883. emit_changed();
  5884. }
  5885. VisualShaderNodeSwitch::OpType VisualShaderNodeSwitch::get_op_type() const {
  5886. return op_type;
  5887. }
  5888. Vector<StringName> VisualShaderNodeSwitch::get_editable_properties() const {
  5889. Vector<StringName> props;
  5890. props.push_back("op_type");
  5891. return props;
  5892. }
  5893. void VisualShaderNodeSwitch::_bind_methods() { // static
  5894. ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeSwitch::set_op_type);
  5895. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeSwitch::get_op_type);
  5896. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Float,Int,UInt,Vector2,Vector3,Vector4,Boolean,Transform"), "set_op_type", "get_op_type");
  5897. BIND_ENUM_CONSTANT(OP_TYPE_FLOAT);
  5898. BIND_ENUM_CONSTANT(OP_TYPE_INT);
  5899. BIND_ENUM_CONSTANT(OP_TYPE_UINT);
  5900. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  5901. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  5902. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  5903. BIND_ENUM_CONSTANT(OP_TYPE_BOOLEAN);
  5904. BIND_ENUM_CONSTANT(OP_TYPE_TRANSFORM);
  5905. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  5906. }
  5907. String VisualShaderNodeSwitch::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5908. bool use_mix = false;
  5909. switch (op_type) {
  5910. case OP_TYPE_FLOAT: {
  5911. use_mix = true;
  5912. } break;
  5913. case OP_TYPE_VECTOR_2D: {
  5914. use_mix = true;
  5915. } break;
  5916. case OP_TYPE_VECTOR_3D: {
  5917. use_mix = true;
  5918. } break;
  5919. case OP_TYPE_VECTOR_4D: {
  5920. use_mix = true;
  5921. } break;
  5922. default: {
  5923. } break;
  5924. }
  5925. String code;
  5926. if (use_mix) {
  5927. code += " " + p_output_vars[0] + " = mix(" + p_input_vars[2] + ", " + p_input_vars[1] + ", float(" + p_input_vars[0] + "));\n";
  5928. } else {
  5929. code += " if (" + p_input_vars[0] + ") {\n";
  5930. code += " " + p_output_vars[0] + " = " + p_input_vars[1] + ";\n";
  5931. code += " } else {\n";
  5932. code += " " + p_output_vars[0] + " = " + p_input_vars[2] + ";\n";
  5933. code += " }\n";
  5934. }
  5935. return code;
  5936. }
  5937. VisualShaderNodeSwitch::VisualShaderNodeSwitch() {
  5938. simple_decl = false;
  5939. set_input_port_default_value(0, false);
  5940. set_input_port_default_value(1, 1.0);
  5941. set_input_port_default_value(2, 0.0);
  5942. }
  5943. ////////////// Fresnel
  5944. String VisualShaderNodeFresnel::get_caption() const {
  5945. return "Fresnel";
  5946. }
  5947. int VisualShaderNodeFresnel::get_input_port_count() const {
  5948. return 4;
  5949. }
  5950. VisualShaderNodeFresnel::PortType VisualShaderNodeFresnel::get_input_port_type(int p_port) const {
  5951. switch (p_port) {
  5952. case 0:
  5953. return PORT_TYPE_VECTOR_3D;
  5954. case 1:
  5955. return PORT_TYPE_VECTOR_3D;
  5956. case 2:
  5957. return PORT_TYPE_BOOLEAN;
  5958. case 3:
  5959. return PORT_TYPE_SCALAR;
  5960. default:
  5961. return PORT_TYPE_VECTOR_3D;
  5962. }
  5963. }
  5964. String VisualShaderNodeFresnel::get_input_port_name(int p_port) const {
  5965. switch (p_port) {
  5966. case 0:
  5967. return "normal";
  5968. case 1:
  5969. return "view";
  5970. case 2:
  5971. return "invert";
  5972. case 3:
  5973. return "power";
  5974. default:
  5975. return "";
  5976. }
  5977. }
  5978. int VisualShaderNodeFresnel::get_output_port_count() const {
  5979. return 1;
  5980. }
  5981. VisualShaderNodeFresnel::PortType VisualShaderNodeFresnel::get_output_port_type(int p_port) const {
  5982. return PORT_TYPE_SCALAR;
  5983. }
  5984. String VisualShaderNodeFresnel::get_output_port_name(int p_port) const {
  5985. return "result";
  5986. }
  5987. bool VisualShaderNodeFresnel::is_generate_input_var(int p_port) const {
  5988. if (p_port == 2) {
  5989. return false;
  5990. }
  5991. return true;
  5992. }
  5993. String VisualShaderNodeFresnel::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  5994. String normal;
  5995. String view;
  5996. if (p_input_vars[0].is_empty()) {
  5997. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  5998. normal = "NORMAL";
  5999. } else {
  6000. normal = "vec3(0.0)";
  6001. }
  6002. } else {
  6003. normal = p_input_vars[0];
  6004. }
  6005. if (p_input_vars[1].is_empty()) {
  6006. if (p_mode == Shader::MODE_SPATIAL) {
  6007. view = "VIEW";
  6008. } else {
  6009. view = "vec3(0.0)";
  6010. }
  6011. } else {
  6012. view = p_input_vars[1];
  6013. }
  6014. if (is_input_port_connected(2)) {
  6015. return " " + p_output_vars[0] + " = " + p_input_vars[2] + " ? (pow(clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + ")) : (pow(1.0 - clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + "));\n";
  6016. } else {
  6017. if (get_input_port_default_value(2)) {
  6018. return " " + p_output_vars[0] + " = pow(clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + ");\n";
  6019. } else {
  6020. return " " + p_output_vars[0] + " = pow(1.0 - clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + ");\n";
  6021. }
  6022. }
  6023. }
  6024. bool VisualShaderNodeFresnel::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  6025. if (p_port == 0) {
  6026. if (p_mode == Shader::MODE_CANVAS_ITEM || p_mode == Shader::MODE_SPATIAL) {
  6027. return true;
  6028. }
  6029. } else if (p_port == 1) {
  6030. if (p_mode == Shader::MODE_SPATIAL) {
  6031. return true;
  6032. }
  6033. }
  6034. return false;
  6035. }
  6036. VisualShaderNodeFresnel::VisualShaderNodeFresnel() {
  6037. set_input_port_default_value(2, false);
  6038. set_input_port_default_value(3, 1.0);
  6039. }
  6040. ////////////// Is
  6041. String VisualShaderNodeIs::get_caption() const {
  6042. return "Is";
  6043. }
  6044. int VisualShaderNodeIs::get_input_port_count() const {
  6045. return 1;
  6046. }
  6047. VisualShaderNodeIs::PortType VisualShaderNodeIs::get_input_port_type(int p_port) const {
  6048. return PORT_TYPE_SCALAR;
  6049. }
  6050. String VisualShaderNodeIs::get_input_port_name(int p_port) const {
  6051. return "";
  6052. }
  6053. int VisualShaderNodeIs::get_output_port_count() const {
  6054. return 1;
  6055. }
  6056. VisualShaderNodeIs::PortType VisualShaderNodeIs::get_output_port_type(int p_port) const {
  6057. return PORT_TYPE_BOOLEAN;
  6058. }
  6059. String VisualShaderNodeIs::get_output_port_name(int p_port) const {
  6060. return "";
  6061. }
  6062. String VisualShaderNodeIs::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6063. static const char *functions[FUNC_MAX] = {
  6064. "isinf($)",
  6065. "isnan($)"
  6066. };
  6067. String code;
  6068. code += " " + p_output_vars[0] + " = " + String(functions[func]).replace("$", p_input_vars[0]) + ";\n";
  6069. return code;
  6070. }
  6071. void VisualShaderNodeIs::set_function(Function p_func) {
  6072. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  6073. if (func == p_func) {
  6074. return;
  6075. }
  6076. func = p_func;
  6077. emit_changed();
  6078. }
  6079. VisualShaderNodeIs::Function VisualShaderNodeIs::get_function() const {
  6080. return func;
  6081. }
  6082. Vector<StringName> VisualShaderNodeIs::get_editable_properties() const {
  6083. Vector<StringName> props;
  6084. props.push_back("function");
  6085. return props;
  6086. }
  6087. void VisualShaderNodeIs::_bind_methods() {
  6088. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeIs::set_function);
  6089. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeIs::get_function);
  6090. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Inf,NaN"), "set_function", "get_function");
  6091. BIND_ENUM_CONSTANT(FUNC_IS_INF);
  6092. BIND_ENUM_CONSTANT(FUNC_IS_NAN);
  6093. BIND_ENUM_CONSTANT(FUNC_MAX);
  6094. }
  6095. VisualShaderNodeIs::VisualShaderNodeIs() {
  6096. set_input_port_default_value(0, 0.0);
  6097. }
  6098. ////////////// Compare
  6099. String VisualShaderNodeCompare::get_caption() const {
  6100. return "Compare";
  6101. }
  6102. int VisualShaderNodeCompare::get_input_port_count() const {
  6103. if (comparison_type == CTYPE_SCALAR && (func == FUNC_EQUAL || func == FUNC_NOT_EQUAL)) {
  6104. return 3;
  6105. }
  6106. return 2;
  6107. }
  6108. VisualShaderNodeCompare::PortType VisualShaderNodeCompare::get_input_port_type(int p_port) const {
  6109. switch (comparison_type) {
  6110. case CTYPE_SCALAR:
  6111. return PORT_TYPE_SCALAR;
  6112. case CTYPE_SCALAR_INT:
  6113. return PORT_TYPE_SCALAR_INT;
  6114. case CTYPE_SCALAR_UINT:
  6115. return PORT_TYPE_SCALAR_UINT;
  6116. case CTYPE_VECTOR_2D:
  6117. return PORT_TYPE_VECTOR_2D;
  6118. case CTYPE_VECTOR_3D:
  6119. return PORT_TYPE_VECTOR_3D;
  6120. case CTYPE_VECTOR_4D:
  6121. return PORT_TYPE_VECTOR_4D;
  6122. case CTYPE_BOOLEAN:
  6123. return PORT_TYPE_BOOLEAN;
  6124. case CTYPE_TRANSFORM:
  6125. return PORT_TYPE_TRANSFORM;
  6126. default:
  6127. return PORT_TYPE_SCALAR;
  6128. }
  6129. }
  6130. String VisualShaderNodeCompare::get_input_port_name(int p_port) const {
  6131. if (p_port == 0) {
  6132. return "a";
  6133. } else if (p_port == 1) {
  6134. return "b";
  6135. } else if (p_port == 2) {
  6136. return "tolerance";
  6137. }
  6138. return "";
  6139. }
  6140. int VisualShaderNodeCompare::get_output_port_count() const {
  6141. return 1;
  6142. }
  6143. VisualShaderNodeCompare::PortType VisualShaderNodeCompare::get_output_port_type(int p_port) const {
  6144. return PORT_TYPE_BOOLEAN;
  6145. }
  6146. String VisualShaderNodeCompare::get_output_port_name(int p_port) const {
  6147. if (p_port == 0) {
  6148. return "result";
  6149. }
  6150. return "";
  6151. }
  6152. String VisualShaderNodeCompare::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  6153. if (comparison_type == CTYPE_BOOLEAN || comparison_type == CTYPE_TRANSFORM) {
  6154. if (func > FUNC_NOT_EQUAL) {
  6155. return RTR("Invalid comparison function for that type.");
  6156. }
  6157. }
  6158. return "";
  6159. }
  6160. String VisualShaderNodeCompare::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6161. static const char *operators[FUNC_MAX] = {
  6162. "==",
  6163. "!=",
  6164. ">",
  6165. ">=",
  6166. "<",
  6167. "<=",
  6168. };
  6169. static const char *functions[FUNC_MAX] = {
  6170. "equal($)",
  6171. "notEqual($)",
  6172. "greaterThan($)",
  6173. "greaterThanEqual($)",
  6174. "lessThan($)",
  6175. "lessThanEqual($)",
  6176. };
  6177. static const char *conditions[COND_MAX] = {
  6178. "all($)",
  6179. "any($)",
  6180. };
  6181. String code;
  6182. switch (comparison_type) {
  6183. case CTYPE_SCALAR: {
  6184. if (func == FUNC_EQUAL) {
  6185. code += " " + p_output_vars[0] + " = (abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ") < " + p_input_vars[2] + ");";
  6186. } else if (func == FUNC_NOT_EQUAL) {
  6187. code += " " + p_output_vars[0] + " = !(abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ") < " + p_input_vars[2] + ");";
  6188. } else {
  6189. code += " " + p_output_vars[0] + " = " + (p_input_vars[0] + " $ " + p_input_vars[1]).replace("$", operators[func]) + ";\n";
  6190. }
  6191. } break;
  6192. case CTYPE_SCALAR_UINT:
  6193. case CTYPE_SCALAR_INT: {
  6194. code += " " + p_output_vars[0] + " = " + (p_input_vars[0] + " $ " + p_input_vars[1]).replace("$", operators[func]) + ";\n";
  6195. } break;
  6196. case CTYPE_VECTOR_2D: {
  6197. code += " {\n";
  6198. code += " bvec2 _bv = " + String(functions[func]).replace("$", p_input_vars[0] + ", " + p_input_vars[1]) + ";\n";
  6199. code += " " + p_output_vars[0] + " = " + String(conditions[condition]).replace("$", "_bv") + ";\n";
  6200. code += " }\n";
  6201. } break;
  6202. case CTYPE_VECTOR_3D: {
  6203. code += " {\n";
  6204. code += " bvec3 _bv = " + String(functions[func]).replace("$", p_input_vars[0] + ", " + p_input_vars[1]) + ";\n";
  6205. code += " " + p_output_vars[0] + " = " + String(conditions[condition]).replace("$", "_bv") + ";\n";
  6206. code += " }\n";
  6207. } break;
  6208. case CTYPE_VECTOR_4D: {
  6209. code += " {\n";
  6210. code += " bvec4 _bv = " + String(functions[func]).replace("$", p_input_vars[0] + ", " + p_input_vars[1]) + ";\n";
  6211. code += " " + p_output_vars[0] + " = " + String(conditions[condition]).replace("$", "_bv") + ";\n";
  6212. code += " }\n";
  6213. } break;
  6214. case CTYPE_BOOLEAN: {
  6215. if (func > FUNC_NOT_EQUAL) {
  6216. return " " + p_output_vars[0] + " = false;\n";
  6217. }
  6218. code += " " + p_output_vars[0] + " = " + (p_input_vars[0] + " $ " + p_input_vars[1]).replace("$", operators[func]) + ";\n";
  6219. } break;
  6220. case CTYPE_TRANSFORM: {
  6221. if (func > FUNC_NOT_EQUAL) {
  6222. return " " + p_output_vars[0] + " = false;\n";
  6223. }
  6224. code += " " + p_output_vars[0] + " = " + (p_input_vars[0] + " $ " + p_input_vars[1]).replace("$", operators[func]) + ";\n";
  6225. } break;
  6226. default:
  6227. break;
  6228. }
  6229. return code;
  6230. }
  6231. void VisualShaderNodeCompare::set_comparison_type(ComparisonType p_comparison_type) {
  6232. ERR_FAIL_INDEX(int(p_comparison_type), int(CTYPE_MAX));
  6233. if (comparison_type == p_comparison_type) {
  6234. return;
  6235. }
  6236. switch (p_comparison_type) {
  6237. case CTYPE_SCALAR:
  6238. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  6239. set_input_port_default_value(1, 0.0, get_input_port_default_value(1));
  6240. simple_decl = true;
  6241. break;
  6242. case CTYPE_SCALAR_UINT:
  6243. case CTYPE_SCALAR_INT:
  6244. set_input_port_default_value(0, 0, get_input_port_default_value(0));
  6245. set_input_port_default_value(1, 0, get_input_port_default_value(1));
  6246. simple_decl = true;
  6247. break;
  6248. case CTYPE_VECTOR_2D:
  6249. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  6250. set_input_port_default_value(1, Vector2(), get_input_port_default_value(1));
  6251. simple_decl = false;
  6252. break;
  6253. case CTYPE_VECTOR_3D:
  6254. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  6255. set_input_port_default_value(1, Vector3(), get_input_port_default_value(1));
  6256. simple_decl = false;
  6257. break;
  6258. case CTYPE_VECTOR_4D:
  6259. set_input_port_default_value(0, Quaternion(), get_input_port_default_value(0));
  6260. set_input_port_default_value(1, Quaternion(), get_input_port_default_value(1));
  6261. simple_decl = false;
  6262. break;
  6263. case CTYPE_BOOLEAN:
  6264. set_input_port_default_value(0, false);
  6265. set_input_port_default_value(1, false);
  6266. simple_decl = true;
  6267. break;
  6268. case CTYPE_TRANSFORM:
  6269. set_input_port_default_value(0, Transform3D());
  6270. set_input_port_default_value(1, Transform3D());
  6271. simple_decl = true;
  6272. break;
  6273. default:
  6274. break;
  6275. }
  6276. comparison_type = p_comparison_type;
  6277. emit_changed();
  6278. }
  6279. VisualShaderNodeCompare::ComparisonType VisualShaderNodeCompare::get_comparison_type() const {
  6280. return comparison_type;
  6281. }
  6282. void VisualShaderNodeCompare::set_function(Function p_func) {
  6283. ERR_FAIL_INDEX(int(p_func), int(FUNC_MAX));
  6284. if (func == p_func) {
  6285. return;
  6286. }
  6287. func = p_func;
  6288. emit_changed();
  6289. }
  6290. VisualShaderNodeCompare::Function VisualShaderNodeCompare::get_function() const {
  6291. return func;
  6292. }
  6293. void VisualShaderNodeCompare::set_condition(Condition p_condition) {
  6294. ERR_FAIL_INDEX(int(p_condition), int(COND_MAX));
  6295. if (condition == p_condition) {
  6296. return;
  6297. }
  6298. condition = p_condition;
  6299. emit_changed();
  6300. }
  6301. VisualShaderNodeCompare::Condition VisualShaderNodeCompare::get_condition() const {
  6302. return condition;
  6303. }
  6304. Vector<StringName> VisualShaderNodeCompare::get_editable_properties() const {
  6305. Vector<StringName> props;
  6306. props.push_back("type");
  6307. props.push_back("function");
  6308. if (comparison_type == CTYPE_VECTOR_2D || comparison_type == CTYPE_VECTOR_3D || comparison_type == CTYPE_VECTOR_4D) {
  6309. props.push_back("condition");
  6310. }
  6311. return props;
  6312. }
  6313. void VisualShaderNodeCompare::_bind_methods() {
  6314. ClassDB::bind_method(D_METHOD("set_comparison_type", "type"), &VisualShaderNodeCompare::set_comparison_type);
  6315. ClassDB::bind_method(D_METHOD("get_comparison_type"), &VisualShaderNodeCompare::get_comparison_type);
  6316. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeCompare::set_function);
  6317. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeCompare::get_function);
  6318. ClassDB::bind_method(D_METHOD("set_condition", "condition"), &VisualShaderNodeCompare::set_condition);
  6319. ClassDB::bind_method(D_METHOD("get_condition"), &VisualShaderNodeCompare::get_condition);
  6320. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, "Float,Int,UInt,Vector2,Vector3,Vector4,Boolean,Transform"), "set_comparison_type", "get_comparison_type");
  6321. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "a == b,a != b,a > b,a >= b,a < b,a <= b"), "set_function", "get_function");
  6322. ADD_PROPERTY(PropertyInfo(Variant::INT, "condition", PROPERTY_HINT_ENUM, "All,Any"), "set_condition", "get_condition");
  6323. BIND_ENUM_CONSTANT(CTYPE_SCALAR);
  6324. BIND_ENUM_CONSTANT(CTYPE_SCALAR_INT);
  6325. BIND_ENUM_CONSTANT(CTYPE_SCALAR_UINT);
  6326. BIND_ENUM_CONSTANT(CTYPE_VECTOR_2D);
  6327. BIND_ENUM_CONSTANT(CTYPE_VECTOR_3D);
  6328. BIND_ENUM_CONSTANT(CTYPE_VECTOR_4D);
  6329. BIND_ENUM_CONSTANT(CTYPE_BOOLEAN);
  6330. BIND_ENUM_CONSTANT(CTYPE_TRANSFORM);
  6331. BIND_ENUM_CONSTANT(CTYPE_MAX);
  6332. BIND_ENUM_CONSTANT(FUNC_EQUAL);
  6333. BIND_ENUM_CONSTANT(FUNC_NOT_EQUAL);
  6334. BIND_ENUM_CONSTANT(FUNC_GREATER_THAN);
  6335. BIND_ENUM_CONSTANT(FUNC_GREATER_THAN_EQUAL);
  6336. BIND_ENUM_CONSTANT(FUNC_LESS_THAN);
  6337. BIND_ENUM_CONSTANT(FUNC_LESS_THAN_EQUAL);
  6338. BIND_ENUM_CONSTANT(FUNC_MAX);
  6339. BIND_ENUM_CONSTANT(COND_ALL);
  6340. BIND_ENUM_CONSTANT(COND_ANY);
  6341. BIND_ENUM_CONSTANT(COND_MAX);
  6342. }
  6343. VisualShaderNodeCompare::VisualShaderNodeCompare() {
  6344. set_input_port_default_value(0, 0.0);
  6345. set_input_port_default_value(1, 0.0);
  6346. set_input_port_default_value(2, CMP_EPSILON);
  6347. }
  6348. ////////////// Fma
  6349. String VisualShaderNodeMultiplyAdd::get_caption() const {
  6350. return "MultiplyAdd";
  6351. }
  6352. int VisualShaderNodeMultiplyAdd::get_input_port_count() const {
  6353. return 3;
  6354. }
  6355. VisualShaderNodeMultiplyAdd::PortType VisualShaderNodeMultiplyAdd::get_input_port_type(int p_port) const {
  6356. switch (op_type) {
  6357. case OP_TYPE_VECTOR_2D:
  6358. return PORT_TYPE_VECTOR_2D;
  6359. case OP_TYPE_VECTOR_3D:
  6360. return PORT_TYPE_VECTOR_3D;
  6361. case OP_TYPE_VECTOR_4D:
  6362. return PORT_TYPE_VECTOR_4D;
  6363. default:
  6364. break;
  6365. }
  6366. return PORT_TYPE_SCALAR;
  6367. }
  6368. String VisualShaderNodeMultiplyAdd::get_input_port_name(int p_port) const {
  6369. if (p_port == 0) {
  6370. return "a";
  6371. } else if (p_port == 1) {
  6372. return "b(*)";
  6373. } else if (p_port == 2) {
  6374. return "c(+)";
  6375. }
  6376. return "";
  6377. }
  6378. int VisualShaderNodeMultiplyAdd::get_output_port_count() const {
  6379. return 1;
  6380. }
  6381. VisualShaderNodeMultiplyAdd::PortType VisualShaderNodeMultiplyAdd::get_output_port_type(int p_port) const {
  6382. switch (op_type) {
  6383. case OP_TYPE_VECTOR_2D:
  6384. return PORT_TYPE_VECTOR_2D;
  6385. case OP_TYPE_VECTOR_3D:
  6386. return PORT_TYPE_VECTOR_3D;
  6387. case OP_TYPE_VECTOR_4D:
  6388. return PORT_TYPE_VECTOR_4D;
  6389. default:
  6390. break;
  6391. }
  6392. return PORT_TYPE_SCALAR;
  6393. }
  6394. String VisualShaderNodeMultiplyAdd::get_output_port_name(int p_port) const {
  6395. return "";
  6396. }
  6397. String VisualShaderNodeMultiplyAdd::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6398. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  6399. return " " + p_output_vars[0] + " = (" + p_input_vars[0] + " * " + p_input_vars[1] + ") + " + p_input_vars[2] + ";\n";
  6400. }
  6401. return " " + p_output_vars[0] + " = fma(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  6402. }
  6403. void VisualShaderNodeMultiplyAdd::set_op_type(OpType p_op_type) {
  6404. ERR_FAIL_INDEX((int)p_op_type, int(OP_TYPE_MAX));
  6405. if (op_type == p_op_type) {
  6406. return;
  6407. }
  6408. switch (p_op_type) {
  6409. case OP_TYPE_SCALAR: {
  6410. set_input_port_default_value(0, 0.0, get_input_port_default_value(0));
  6411. set_input_port_default_value(1, 1.0, get_input_port_default_value(1));
  6412. set_input_port_default_value(2, 0.0, get_input_port_default_value(2));
  6413. } break;
  6414. case OP_TYPE_VECTOR_2D: {
  6415. set_input_port_default_value(0, Vector2(), get_input_port_default_value(0));
  6416. set_input_port_default_value(1, Vector2(1.0, 1.0), get_input_port_default_value(1));
  6417. set_input_port_default_value(2, Vector2(), get_input_port_default_value(2));
  6418. } break;
  6419. case OP_TYPE_VECTOR_3D: {
  6420. set_input_port_default_value(0, Vector3(), get_input_port_default_value(0));
  6421. set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0), get_input_port_default_value(1));
  6422. set_input_port_default_value(2, Vector3(), get_input_port_default_value(2));
  6423. } break;
  6424. case OP_TYPE_VECTOR_4D: {
  6425. set_input_port_default_value(0, Vector4(), get_input_port_default_value(0));
  6426. set_input_port_default_value(1, Vector4(1.0, 1.0, 1.0, 1.0), get_input_port_default_value(1));
  6427. set_input_port_default_value(2, Vector4(), get_input_port_default_value(2));
  6428. } break;
  6429. default:
  6430. break;
  6431. }
  6432. op_type = p_op_type;
  6433. emit_changed();
  6434. }
  6435. VisualShaderNodeMultiplyAdd::OpType VisualShaderNodeMultiplyAdd::get_op_type() const {
  6436. return op_type;
  6437. }
  6438. Vector<StringName> VisualShaderNodeMultiplyAdd::get_editable_properties() const {
  6439. Vector<StringName> props;
  6440. props.push_back("op_type");
  6441. return props;
  6442. }
  6443. void VisualShaderNodeMultiplyAdd::_bind_methods() {
  6444. ClassDB::bind_method(D_METHOD("set_op_type", "type"), &VisualShaderNodeMultiplyAdd::set_op_type);
  6445. ClassDB::bind_method(D_METHOD("get_op_type"), &VisualShaderNodeMultiplyAdd::get_op_type);
  6446. ADD_PROPERTY(PropertyInfo(Variant::INT, "op_type", PROPERTY_HINT_ENUM, "Scalar,Vector2,Vector3,Vector4"), "set_op_type", "get_op_type");
  6447. BIND_ENUM_CONSTANT(OP_TYPE_SCALAR);
  6448. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_2D);
  6449. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_3D);
  6450. BIND_ENUM_CONSTANT(OP_TYPE_VECTOR_4D);
  6451. BIND_ENUM_CONSTANT(OP_TYPE_MAX);
  6452. }
  6453. VisualShaderNodeMultiplyAdd::VisualShaderNodeMultiplyAdd() {
  6454. set_input_port_default_value(0, 0.0);
  6455. set_input_port_default_value(1, 1.0);
  6456. set_input_port_default_value(2, 0.0);
  6457. }
  6458. ////////////// Billboard
  6459. String VisualShaderNodeBillboard::get_caption() const {
  6460. return "GetBillboardMatrix";
  6461. }
  6462. int VisualShaderNodeBillboard::get_input_port_count() const {
  6463. return 0;
  6464. }
  6465. VisualShaderNodeBillboard::PortType VisualShaderNodeBillboard::get_input_port_type(int p_port) const {
  6466. return PORT_TYPE_SCALAR;
  6467. }
  6468. String VisualShaderNodeBillboard::get_input_port_name(int p_port) const {
  6469. return "";
  6470. }
  6471. int VisualShaderNodeBillboard::get_output_port_count() const {
  6472. return 1;
  6473. }
  6474. VisualShaderNodeBillboard::PortType VisualShaderNodeBillboard::get_output_port_type(int p_port) const {
  6475. return PORT_TYPE_TRANSFORM;
  6476. }
  6477. String VisualShaderNodeBillboard::get_output_port_name(int p_port) const {
  6478. return "model_view_matrix";
  6479. }
  6480. String VisualShaderNodeBillboard::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6481. String code;
  6482. switch (billboard_type) {
  6483. case BILLBOARD_TYPE_ENABLED:
  6484. code += " {\n";
  6485. code += " mat4 __mvm = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0], INV_VIEW_MATRIX[1], INV_VIEW_MATRIX[2], MODEL_MATRIX[3]);\n";
  6486. if (keep_scale) {
  6487. code += " __mvm = __mvm * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
  6488. }
  6489. code += " " + p_output_vars[0] + " = __mvm;\n";
  6490. code += " }\n";
  6491. break;
  6492. case BILLBOARD_TYPE_FIXED_Y:
  6493. code += " {\n";
  6494. code += " mat4 __mvm = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0], MODEL_MATRIX[1], vec4(normalize(cross(INV_VIEW_MATRIX[0].xyz, MODEL_MATRIX[1].xyz)), 0.0), MODEL_MATRIX[3]);\n";
  6495. if (keep_scale) {
  6496. code += " __mvm = __mvm * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
  6497. } else {
  6498. code += " __mvm = __mvm * mat4(vec4(1.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0 / length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
  6499. }
  6500. code += " " + p_output_vars[0] + " = __mvm;\n";
  6501. code += " }\n";
  6502. break;
  6503. case BILLBOARD_TYPE_PARTICLES:
  6504. code += " {\n";
  6505. code += " mat4 __wm = mat4(normalize(INV_VIEW_MATRIX[0]), normalize(INV_VIEW_MATRIX[1]), normalize(INV_VIEW_MATRIX[2]), MODEL_MATRIX[3]);\n";
  6506. code += " __wm = __wm * mat4(vec4(cos(INSTANCE_CUSTOM.x), -sin(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
  6507. if (keep_scale) {
  6508. code += " __wm = __wm * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n";
  6509. }
  6510. code += " " + p_output_vars[0] + " = VIEW_MATRIX * __wm;\n";
  6511. code += " }\n";
  6512. break;
  6513. default:
  6514. code += " " + p_output_vars[0] + " = mat4(1.0);\n";
  6515. break;
  6516. }
  6517. return code;
  6518. }
  6519. bool VisualShaderNodeBillboard::is_show_prop_names() const {
  6520. return true;
  6521. }
  6522. void VisualShaderNodeBillboard::set_billboard_type(BillboardType p_billboard_type) {
  6523. ERR_FAIL_INDEX(int(p_billboard_type), int(BILLBOARD_TYPE_MAX));
  6524. if (billboard_type == p_billboard_type) {
  6525. return;
  6526. }
  6527. billboard_type = p_billboard_type;
  6528. simple_decl = bool(billboard_type == BILLBOARD_TYPE_DISABLED);
  6529. set_disabled(simple_decl);
  6530. emit_changed();
  6531. }
  6532. VisualShaderNodeBillboard::BillboardType VisualShaderNodeBillboard::get_billboard_type() const {
  6533. return billboard_type;
  6534. }
  6535. void VisualShaderNodeBillboard::set_keep_scale_enabled(bool p_enabled) {
  6536. keep_scale = p_enabled;
  6537. emit_changed();
  6538. }
  6539. bool VisualShaderNodeBillboard::is_keep_scale_enabled() const {
  6540. return keep_scale;
  6541. }
  6542. Vector<StringName> VisualShaderNodeBillboard::get_editable_properties() const {
  6543. Vector<StringName> props;
  6544. props.push_back("billboard_type");
  6545. if (billboard_type == BILLBOARD_TYPE_ENABLED || billboard_type == BILLBOARD_TYPE_FIXED_Y || billboard_type == BILLBOARD_TYPE_PARTICLES) {
  6546. props.push_back("keep_scale");
  6547. }
  6548. return props;
  6549. }
  6550. void VisualShaderNodeBillboard::_bind_methods() {
  6551. ClassDB::bind_method(D_METHOD("set_billboard_type", "billboard_type"), &VisualShaderNodeBillboard::set_billboard_type);
  6552. ClassDB::bind_method(D_METHOD("get_billboard_type"), &VisualShaderNodeBillboard::get_billboard_type);
  6553. ClassDB::bind_method(D_METHOD("set_keep_scale_enabled", "enabled"), &VisualShaderNodeBillboard::set_keep_scale_enabled);
  6554. ClassDB::bind_method(D_METHOD("is_keep_scale_enabled"), &VisualShaderNodeBillboard::is_keep_scale_enabled);
  6555. ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard_type", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard,Particles"), "set_billboard_type", "get_billboard_type");
  6556. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_scale"), "set_keep_scale_enabled", "is_keep_scale_enabled");
  6557. BIND_ENUM_CONSTANT(BILLBOARD_TYPE_DISABLED);
  6558. BIND_ENUM_CONSTANT(BILLBOARD_TYPE_ENABLED);
  6559. BIND_ENUM_CONSTANT(BILLBOARD_TYPE_FIXED_Y);
  6560. BIND_ENUM_CONSTANT(BILLBOARD_TYPE_PARTICLES);
  6561. BIND_ENUM_CONSTANT(BILLBOARD_TYPE_MAX);
  6562. }
  6563. VisualShaderNodeBillboard::VisualShaderNodeBillboard() {
  6564. simple_decl = false;
  6565. }
  6566. ////////////// DistanceFade
  6567. String VisualShaderNodeDistanceFade::get_caption() const {
  6568. return "DistanceFade";
  6569. }
  6570. int VisualShaderNodeDistanceFade::get_input_port_count() const {
  6571. return 2;
  6572. }
  6573. VisualShaderNodeDistanceFade::PortType VisualShaderNodeDistanceFade::get_input_port_type(int p_port) const {
  6574. switch (p_port) {
  6575. case 0:
  6576. return PORT_TYPE_SCALAR;
  6577. case 1:
  6578. return PORT_TYPE_SCALAR;
  6579. }
  6580. return PORT_TYPE_SCALAR;
  6581. }
  6582. String VisualShaderNodeDistanceFade::get_input_port_name(int p_port) const {
  6583. switch (p_port) {
  6584. case 0:
  6585. return "min";
  6586. case 1:
  6587. return "max";
  6588. }
  6589. return "";
  6590. }
  6591. int VisualShaderNodeDistanceFade::get_output_port_count() const {
  6592. return 1;
  6593. }
  6594. VisualShaderNodeDistanceFade::PortType VisualShaderNodeDistanceFade::get_output_port_type(int p_port) const {
  6595. return PORT_TYPE_SCALAR;
  6596. }
  6597. String VisualShaderNodeDistanceFade::get_output_port_name(int p_port) const {
  6598. return "amount";
  6599. }
  6600. bool VisualShaderNodeDistanceFade::has_output_port_preview(int p_port) const {
  6601. return false;
  6602. }
  6603. String VisualShaderNodeDistanceFade::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6604. String code;
  6605. code += vformat(" %s = clamp(smoothstep(%s, %s,-VERTEX.z),0.0,1.0);\n", p_output_vars[0], p_input_vars[0], p_input_vars[1]);
  6606. return code;
  6607. }
  6608. VisualShaderNodeDistanceFade::VisualShaderNodeDistanceFade() {
  6609. set_input_port_default_value(0, 0.0);
  6610. set_input_port_default_value(1, 10.0);
  6611. }
  6612. ////////////// ProximityFade
  6613. String VisualShaderNodeProximityFade::get_caption() const {
  6614. return "ProximityFade";
  6615. }
  6616. int VisualShaderNodeProximityFade::get_input_port_count() const {
  6617. return 1;
  6618. }
  6619. VisualShaderNodeProximityFade::PortType VisualShaderNodeProximityFade::get_input_port_type(int p_port) const {
  6620. return PORT_TYPE_SCALAR;
  6621. }
  6622. String VisualShaderNodeProximityFade::get_input_port_name(int p_port) const {
  6623. return "distance";
  6624. }
  6625. int VisualShaderNodeProximityFade::get_output_port_count() const {
  6626. return 1;
  6627. }
  6628. VisualShaderNodeProximityFade::PortType VisualShaderNodeProximityFade::get_output_port_type(int p_port) const {
  6629. return PORT_TYPE_SCALAR;
  6630. }
  6631. String VisualShaderNodeProximityFade::get_output_port_name(int p_port) const {
  6632. return "fade";
  6633. }
  6634. bool VisualShaderNodeProximityFade::has_output_port_preview(int p_port) const {
  6635. return false;
  6636. }
  6637. String VisualShaderNodeProximityFade::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  6638. return "uniform sampler2D " + make_unique_id(p_type, p_id, "depth_tex") + " : hint_depth_texture;\n";
  6639. }
  6640. String VisualShaderNodeProximityFade::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6641. String code;
  6642. code += " {\n";
  6643. code += " float __depth_tex = texture(" + make_unique_id(p_type, p_id, "depth_tex") + ", SCREEN_UV).r;\n";
  6644. if (!RenderingServer::get_singleton()->is_low_end()) {
  6645. code += " vec4 __depth_world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, __depth_tex, 1.0);\n";
  6646. } else {
  6647. code += " vec4 __depth_world_pos = INV_PROJECTION_MATRIX * vec4(vec3(SCREEN_UV, __depth_tex) * 2.0 - 1.0, 1.0);\n";
  6648. }
  6649. code += " __depth_world_pos.xyz /= __depth_world_pos.w;\n";
  6650. code += vformat(" %s = clamp(1.0 - smoothstep(__depth_world_pos.z + %s, __depth_world_pos.z, VERTEX.z), 0.0, 1.0);\n", p_output_vars[0], p_input_vars[0]);
  6651. code += " }\n";
  6652. return code;
  6653. }
  6654. VisualShaderNodeProximityFade::VisualShaderNodeProximityFade() {
  6655. set_input_port_default_value(0, 1.0);
  6656. simple_decl = false;
  6657. }
  6658. ////////////// Random Range
  6659. String VisualShaderNodeRandomRange::get_caption() const {
  6660. return "RandomRange";
  6661. }
  6662. int VisualShaderNodeRandomRange::get_input_port_count() const {
  6663. return 3;
  6664. }
  6665. VisualShaderNodeRandomRange::PortType VisualShaderNodeRandomRange::get_input_port_type(int p_port) const {
  6666. switch (p_port) {
  6667. case 0:
  6668. return PORT_TYPE_VECTOR_3D;
  6669. case 1:
  6670. return PORT_TYPE_SCALAR;
  6671. case 2:
  6672. return PORT_TYPE_SCALAR;
  6673. default:
  6674. break;
  6675. }
  6676. return PORT_TYPE_SCALAR;
  6677. }
  6678. String VisualShaderNodeRandomRange::get_input_port_name(int p_port) const {
  6679. switch (p_port) {
  6680. case 0:
  6681. return "seed";
  6682. case 1:
  6683. return "min";
  6684. case 2:
  6685. return "max";
  6686. default:
  6687. break;
  6688. }
  6689. return "";
  6690. }
  6691. int VisualShaderNodeRandomRange::get_output_port_count() const {
  6692. return 1;
  6693. }
  6694. VisualShaderNodeRandomRange::PortType VisualShaderNodeRandomRange::get_output_port_type(int p_port) const {
  6695. return PORT_TYPE_SCALAR;
  6696. }
  6697. String VisualShaderNodeRandomRange::get_output_port_name(int p_port) const {
  6698. return "value";
  6699. }
  6700. String VisualShaderNodeRandomRange::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  6701. String code;
  6702. code += "\n\n";
  6703. code += "// 3D Noise with friendly permission by Inigo Quilez\n";
  6704. code += "vec3 hash_noise_range( vec3 p ) {\n";
  6705. code += " p *= mat3(vec3(127.1, 311.7, -53.7), vec3(269.5, 183.3, 77.1), vec3(-301.7, 27.3, 215.3));\n";
  6706. code += " return 2.0 * fract(fract(p)*4375.55) -1.;\n";
  6707. code += "}\n";
  6708. code += "\n";
  6709. return code;
  6710. }
  6711. String VisualShaderNodeRandomRange::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6712. String code;
  6713. code += vformat(" %s = mix(%s, %s, hash_noise_range(%s).x);\n", p_output_vars[0], p_input_vars[1], p_input_vars[2], p_input_vars[0]);
  6714. return code;
  6715. }
  6716. VisualShaderNodeRandomRange::VisualShaderNodeRandomRange() {
  6717. set_input_port_default_value(0, Vector3(1.0, 1.0, 1.0));
  6718. set_input_port_default_value(1, 0.0);
  6719. set_input_port_default_value(2, 1.0);
  6720. }
  6721. ////////////// Remap
  6722. String VisualShaderNodeRemap::get_caption() const {
  6723. return "Remap";
  6724. }
  6725. int VisualShaderNodeRemap::get_input_port_count() const {
  6726. return 5;
  6727. }
  6728. VisualShaderNodeRemap::PortType VisualShaderNodeRemap::get_input_port_type(int p_port) const {
  6729. switch (p_port) {
  6730. case 0:
  6731. return PORT_TYPE_SCALAR;
  6732. case 1:
  6733. return PORT_TYPE_SCALAR;
  6734. case 2:
  6735. return PORT_TYPE_SCALAR;
  6736. case 3:
  6737. return PORT_TYPE_SCALAR;
  6738. case 4:
  6739. return PORT_TYPE_SCALAR;
  6740. default:
  6741. break;
  6742. }
  6743. return PORT_TYPE_SCALAR;
  6744. }
  6745. String VisualShaderNodeRemap::get_input_port_name(int p_port) const {
  6746. switch (p_port) {
  6747. case 0:
  6748. return "value";
  6749. case 1:
  6750. return "input min";
  6751. case 2:
  6752. return "input max";
  6753. case 3:
  6754. return "output min";
  6755. case 4:
  6756. return "output max";
  6757. default:
  6758. break;
  6759. }
  6760. return "";
  6761. }
  6762. int VisualShaderNodeRemap::get_output_port_count() const {
  6763. return 1;
  6764. }
  6765. VisualShaderNodeRemap::PortType VisualShaderNodeRemap::get_output_port_type(int p_port) const {
  6766. return PORT_TYPE_SCALAR;
  6767. }
  6768. String VisualShaderNodeRemap::get_output_port_name(int p_port) const {
  6769. return "value";
  6770. }
  6771. String VisualShaderNodeRemap::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6772. String code;
  6773. code += " {\n";
  6774. code += vformat(" float __input_range = %s - %s;\n", p_input_vars[2], p_input_vars[1]);
  6775. code += vformat(" float __output_range = %s - %s;\n", p_input_vars[4], p_input_vars[3]);
  6776. code += vformat(" %s = %s + __output_range * ((%s - %s) / __input_range);\n", p_output_vars[0], p_input_vars[3], p_input_vars[0], p_input_vars[1]);
  6777. code += " }\n";
  6778. return code;
  6779. }
  6780. VisualShaderNodeRemap::VisualShaderNodeRemap() {
  6781. set_input_port_default_value(1, 0.0);
  6782. set_input_port_default_value(2, 1.0);
  6783. set_input_port_default_value(3, 0.0);
  6784. set_input_port_default_value(4, 1.0);
  6785. simple_decl = false;
  6786. }
  6787. ////////////// RotationByAxis
  6788. String VisualShaderNodeRotationByAxis::get_caption() const {
  6789. return "RotationByAxis";
  6790. }
  6791. int VisualShaderNodeRotationByAxis::get_input_port_count() const {
  6792. return 3;
  6793. }
  6794. VisualShaderNodeRotationByAxis::PortType VisualShaderNodeRotationByAxis::get_input_port_type(int p_port) const {
  6795. switch (p_port) {
  6796. case 0:
  6797. return PORT_TYPE_VECTOR_3D;
  6798. case 1:
  6799. return PORT_TYPE_SCALAR;
  6800. case 2:
  6801. return PORT_TYPE_VECTOR_3D;
  6802. default:
  6803. break;
  6804. }
  6805. return PORT_TYPE_SCALAR;
  6806. }
  6807. String VisualShaderNodeRotationByAxis::get_input_port_name(int p_port) const {
  6808. switch (p_port) {
  6809. case 0:
  6810. return "input";
  6811. case 1:
  6812. return "angle";
  6813. case 2:
  6814. return "axis";
  6815. default:
  6816. break;
  6817. }
  6818. return "";
  6819. }
  6820. int VisualShaderNodeRotationByAxis::get_output_port_count() const {
  6821. return 2;
  6822. }
  6823. VisualShaderNodeRotationByAxis::PortType VisualShaderNodeRotationByAxis::get_output_port_type(int p_port) const {
  6824. switch (p_port) {
  6825. case 0:
  6826. return PORT_TYPE_VECTOR_3D;
  6827. case 1:
  6828. return PORT_TYPE_TRANSFORM;
  6829. default:
  6830. break;
  6831. }
  6832. return PORT_TYPE_SCALAR;
  6833. }
  6834. String VisualShaderNodeRotationByAxis::get_output_port_name(int p_port) const {
  6835. switch (p_port) {
  6836. case 0:
  6837. return "output";
  6838. case 1:
  6839. return "rotationMat";
  6840. default:
  6841. break;
  6842. }
  6843. return "";
  6844. }
  6845. bool VisualShaderNodeRotationByAxis::has_output_port_preview(int p_port) const {
  6846. return false;
  6847. }
  6848. String VisualShaderNodeRotationByAxis::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  6849. String code;
  6850. code += " {\n";
  6851. code += vformat(" float __angle = %s;\n", p_input_vars[1]);
  6852. code += vformat(" vec3 __axis = normalize(%s);\n", p_input_vars[2]);
  6853. code += vformat(" mat3 __rot_matrix = mat3(\n");
  6854. code += vformat(" vec3( cos(__angle)+__axis.x*__axis.x*(1.0 - cos(__angle)), __axis.x*__axis.y*(1.0-cos(__angle))-__axis.z*sin(__angle), __axis.x*__axis.z*(1.0-cos(__angle))+__axis.y*sin(__angle) ),\n");
  6855. code += vformat(" vec3( __axis.y*__axis.x*(1.0-cos(__angle))+__axis.z*sin(__angle), cos(__angle)+__axis.y*__axis.y*(1.0-cos(__angle)), __axis.y*__axis.z*(1.0-cos(__angle))-__axis.x*sin(__angle) ),\n");
  6856. code += vformat(" vec3( __axis.z*__axis.x*(1.0-cos(__angle))-__axis.y*sin(__angle), __axis.z*__axis.y*(1.0-cos(__angle))+__axis.x*sin(__angle), cos(__angle)+__axis.z*__axis.z*(1.0-cos(__angle)) )\n");
  6857. code += vformat(" );\n");
  6858. code += vformat(" %s = %s * __rot_matrix;\n", p_output_vars[0], p_input_vars[0]);
  6859. code += vformat(" %s = mat4(__rot_matrix);\n", p_output_vars[1]);
  6860. code += " }\n";
  6861. return code;
  6862. }
  6863. VisualShaderNodeRotationByAxis::VisualShaderNodeRotationByAxis() {
  6864. set_input_port_default_value(1, 0.0);
  6865. set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0));
  6866. simple_decl = false;
  6867. }