visual_shader_nodes.cpp 242 KB

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