visual_shader_nodes.cpp 263 KB

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