visual_shader.cpp 197 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405
  1. /**************************************************************************/
  2. /* visual_shader.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.h"
  31. #include "core/templates/rb_map.h"
  32. #include "core/templates/vmap.h"
  33. #include "core/variant/variant_utility.h"
  34. #include "servers/rendering/shader_types.h"
  35. #include "visual_shader_nodes.h"
  36. #include "visual_shader_particle_nodes.h"
  37. #include "visual_shader_sdf_nodes.h"
  38. String make_unique_id(VisualShader::Type p_type, int p_id, const String &p_name) {
  39. static const char *typepf[VisualShader::TYPE_MAX] = { "vtx", "frg", "lgt", "start", "process", "collide", "start_custom", "process_custom", "sky", "fog" };
  40. return p_name + "_" + String(typepf[p_type]) + "_" + itos(p_id);
  41. }
  42. bool VisualShaderNode::is_simple_decl() const {
  43. return simple_decl;
  44. }
  45. int VisualShaderNode::get_default_input_port(PortType p_type) const {
  46. return 0;
  47. }
  48. void VisualShaderNode::set_output_port_for_preview(int p_index) {
  49. port_preview = p_index;
  50. }
  51. int VisualShaderNode::get_output_port_for_preview() const {
  52. return port_preview;
  53. }
  54. void VisualShaderNode::set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value) {
  55. Variant value = p_value;
  56. if (p_prev_value.get_type() != Variant::NIL) {
  57. switch (p_value.get_type()) {
  58. case Variant::FLOAT: {
  59. switch (p_prev_value.get_type()) {
  60. case Variant::INT: {
  61. value = (float)p_prev_value;
  62. } break;
  63. case Variant::FLOAT: {
  64. value = p_prev_value;
  65. } break;
  66. case Variant::VECTOR2: {
  67. Vector2 pv = p_prev_value;
  68. value = pv.x;
  69. } break;
  70. case Variant::VECTOR3: {
  71. Vector3 pv = p_prev_value;
  72. value = pv.x;
  73. } break;
  74. case Variant::QUATERNION: {
  75. Quaternion pv = p_prev_value;
  76. value = pv.x;
  77. } break;
  78. default:
  79. break;
  80. }
  81. } break;
  82. case Variant::INT: {
  83. switch (p_prev_value.get_type()) {
  84. case Variant::INT: {
  85. value = p_prev_value;
  86. } break;
  87. case Variant::FLOAT: {
  88. value = (int)p_prev_value;
  89. } break;
  90. case Variant::VECTOR2: {
  91. Vector2 pv = p_prev_value;
  92. value = (int)pv.x;
  93. } break;
  94. case Variant::VECTOR3: {
  95. Vector3 pv = p_prev_value;
  96. value = (int)pv.x;
  97. } break;
  98. case Variant::QUATERNION: {
  99. Quaternion pv = p_prev_value;
  100. value = (int)pv.x;
  101. } break;
  102. default:
  103. break;
  104. }
  105. } break;
  106. case Variant::VECTOR2: {
  107. switch (p_prev_value.get_type()) {
  108. case Variant::INT: {
  109. float pv = (float)(int)p_prev_value;
  110. value = Vector2(pv, pv);
  111. } break;
  112. case Variant::FLOAT: {
  113. float pv = p_prev_value;
  114. value = Vector2(pv, pv);
  115. } break;
  116. case Variant::VECTOR2: {
  117. value = p_prev_value;
  118. } break;
  119. case Variant::VECTOR3: {
  120. Vector3 pv = p_prev_value;
  121. value = Vector2(pv.x, pv.y);
  122. } break;
  123. case Variant::QUATERNION: {
  124. Quaternion pv = p_prev_value;
  125. value = Vector2(pv.x, pv.y);
  126. } break;
  127. default:
  128. break;
  129. }
  130. } break;
  131. case Variant::VECTOR3: {
  132. switch (p_prev_value.get_type()) {
  133. case Variant::INT: {
  134. float pv = (float)(int)p_prev_value;
  135. value = Vector3(pv, pv, pv);
  136. } break;
  137. case Variant::FLOAT: {
  138. float pv = p_prev_value;
  139. value = Vector3(pv, pv, pv);
  140. } break;
  141. case Variant::VECTOR2: {
  142. Vector2 pv = p_prev_value;
  143. value = Vector3(pv.x, pv.y, pv.y);
  144. } break;
  145. case Variant::VECTOR3: {
  146. value = p_prev_value;
  147. } break;
  148. case Variant::QUATERNION: {
  149. Quaternion pv = p_prev_value;
  150. value = Vector3(pv.x, pv.y, pv.z);
  151. } break;
  152. default:
  153. break;
  154. }
  155. } break;
  156. case Variant::QUATERNION: {
  157. switch (p_prev_value.get_type()) {
  158. case Variant::INT: {
  159. float pv = (float)(int)p_prev_value;
  160. value = Quaternion(pv, pv, pv, pv);
  161. } break;
  162. case Variant::FLOAT: {
  163. float pv = p_prev_value;
  164. value = Quaternion(pv, pv, pv, pv);
  165. } break;
  166. case Variant::VECTOR2: {
  167. Vector2 pv = p_prev_value;
  168. value = Quaternion(pv.x, pv.y, pv.y, pv.y);
  169. } break;
  170. case Variant::VECTOR3: {
  171. Vector3 pv = p_prev_value;
  172. value = Quaternion(pv.x, pv.y, pv.z, pv.z);
  173. } break;
  174. case Variant::QUATERNION: {
  175. value = p_prev_value;
  176. } break;
  177. default:
  178. break;
  179. }
  180. } break;
  181. default:
  182. break;
  183. }
  184. }
  185. default_input_values[p_port] = value;
  186. emit_changed();
  187. }
  188. Variant VisualShaderNode::get_input_port_default_value(int p_port) const {
  189. if (default_input_values.has(p_port)) {
  190. return default_input_values[p_port];
  191. }
  192. return Variant();
  193. }
  194. void VisualShaderNode::remove_input_port_default_value(int p_port) {
  195. if (default_input_values.has(p_port)) {
  196. default_input_values.erase(p_port);
  197. emit_changed();
  198. }
  199. }
  200. void VisualShaderNode::clear_default_input_values() {
  201. if (!default_input_values.is_empty()) {
  202. default_input_values.clear();
  203. emit_changed();
  204. }
  205. }
  206. bool VisualShaderNode::is_port_separator(int p_index) const {
  207. return false;
  208. }
  209. bool VisualShaderNode::is_output_port_connected(int p_port) const {
  210. if (connected_output_ports.has(p_port)) {
  211. return connected_output_ports[p_port] > 0;
  212. }
  213. return false;
  214. }
  215. void VisualShaderNode::set_output_port_connected(int p_port, bool p_connected) {
  216. if (p_connected) {
  217. connected_output_ports[p_port]++;
  218. } else {
  219. connected_output_ports[p_port]--;
  220. }
  221. }
  222. bool VisualShaderNode::is_input_port_connected(int p_port) const {
  223. if (connected_input_ports.has(p_port)) {
  224. return connected_input_ports[p_port];
  225. }
  226. return false;
  227. }
  228. void VisualShaderNode::set_input_port_connected(int p_port, bool p_connected) {
  229. connected_input_ports[p_port] = p_connected;
  230. }
  231. bool VisualShaderNode::is_any_port_connected() const {
  232. for (const KeyValue<int, bool> &E : connected_input_ports) {
  233. if (E.value) {
  234. return true;
  235. }
  236. }
  237. for (const KeyValue<int, int> &E : connected_output_ports) {
  238. if (E.value > 0) {
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. bool VisualShaderNode::is_generate_input_var(int p_port) const {
  245. return true;
  246. }
  247. bool VisualShaderNode::is_output_port_expandable(int p_port) const {
  248. VisualShaderNode::PortType port = get_output_port_type(p_port);
  249. if (get_output_port_count() == 1 && (port == PORT_TYPE_VECTOR_2D || port == PORT_TYPE_VECTOR_3D || port == PORT_TYPE_VECTOR_4D)) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. bool VisualShaderNode::has_output_port_preview(int p_port) const {
  255. return true;
  256. }
  257. void VisualShaderNode::_set_output_ports_expanded(const Array &p_values) {
  258. for (int i = 0; i < p_values.size(); i++) {
  259. expanded_output_ports[p_values[i]] = true;
  260. }
  261. emit_changed();
  262. }
  263. Array VisualShaderNode::_get_output_ports_expanded() const {
  264. Array arr;
  265. for (int i = 0; i < get_output_port_count(); i++) {
  266. if (_is_output_port_expanded(i)) {
  267. arr.push_back(i);
  268. }
  269. }
  270. return arr;
  271. }
  272. void VisualShaderNode::_set_output_port_expanded(int p_port, bool p_expanded) {
  273. expanded_output_ports[p_port] = p_expanded;
  274. emit_changed();
  275. }
  276. bool VisualShaderNode::_is_output_port_expanded(int p_port) const {
  277. if (expanded_output_ports.has(p_port)) {
  278. return expanded_output_ports[p_port];
  279. }
  280. return false;
  281. }
  282. int VisualShaderNode::get_expanded_output_port_count() const {
  283. int count = get_output_port_count();
  284. int count2 = count;
  285. for (int i = 0; i < count; i++) {
  286. if (is_output_port_expandable(i) && _is_output_port_expanded(i)) {
  287. switch (get_output_port_type(i)) {
  288. case PORT_TYPE_VECTOR_2D: {
  289. count2 += 2;
  290. } break;
  291. case PORT_TYPE_VECTOR_3D: {
  292. count2 += 3;
  293. } break;
  294. case PORT_TYPE_VECTOR_4D: {
  295. count2 += 4;
  296. } break;
  297. default:
  298. break;
  299. }
  300. }
  301. }
  302. return count2;
  303. }
  304. bool VisualShaderNode::is_code_generated() const {
  305. return true;
  306. }
  307. bool VisualShaderNode::is_show_prop_names() const {
  308. return false;
  309. }
  310. bool VisualShaderNode::is_use_prop_slots() const {
  311. return false;
  312. }
  313. bool VisualShaderNode::is_disabled() const {
  314. return disabled;
  315. }
  316. void VisualShaderNode::set_disabled(bool p_disabled) {
  317. disabled = p_disabled;
  318. }
  319. bool VisualShaderNode::is_deletable() const {
  320. return closable;
  321. }
  322. void VisualShaderNode::set_deletable(bool p_closable) {
  323. closable = p_closable;
  324. }
  325. void VisualShaderNode::set_frame(int p_node) {
  326. linked_parent_graph_frame = p_node;
  327. }
  328. int VisualShaderNode::get_frame() const {
  329. return linked_parent_graph_frame;
  330. }
  331. Vector<VisualShader::DefaultTextureParam> VisualShaderNode::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  332. return Vector<VisualShader::DefaultTextureParam>();
  333. }
  334. String VisualShaderNode::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  335. return String();
  336. }
  337. String VisualShaderNode::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  338. return String();
  339. }
  340. String VisualShaderNode::generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  341. return String();
  342. }
  343. Vector<StringName> VisualShaderNode::get_editable_properties() const {
  344. return Vector<StringName>();
  345. }
  346. HashMap<StringName, String> VisualShaderNode::get_editable_properties_names() const {
  347. return HashMap<StringName, String>();
  348. }
  349. Array VisualShaderNode::get_default_input_values() const {
  350. Array ret;
  351. for (const KeyValue<int, Variant> &E : default_input_values) {
  352. ret.push_back(E.key);
  353. ret.push_back(E.value);
  354. }
  355. return ret;
  356. }
  357. void VisualShaderNode::set_default_input_values(const Array &p_values) {
  358. if (p_values.size() % 2 == 0) {
  359. for (int i = 0; i < p_values.size(); i += 2) {
  360. default_input_values[p_values[i + 0]] = p_values[i + 1];
  361. }
  362. }
  363. emit_changed();
  364. }
  365. String VisualShaderNode::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  366. return String();
  367. }
  368. VisualShaderNode::Category VisualShaderNode::get_category() const {
  369. return CATEGORY_NONE;
  370. }
  371. bool VisualShaderNode::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  372. return false;
  373. }
  374. void VisualShaderNode::_bind_methods() {
  375. ClassDB::bind_method(D_METHOD("get_default_input_port", "type"), &VisualShaderNode::get_default_input_port);
  376. ClassDB::bind_method(D_METHOD("set_output_port_for_preview", "port"), &VisualShaderNode::set_output_port_for_preview);
  377. ClassDB::bind_method(D_METHOD("get_output_port_for_preview"), &VisualShaderNode::get_output_port_for_preview);
  378. ClassDB::bind_method(D_METHOD("_set_output_port_expanded", "port"), &VisualShaderNode::_set_output_port_expanded);
  379. ClassDB::bind_method(D_METHOD("_is_output_port_expanded"), &VisualShaderNode::_is_output_port_expanded);
  380. ClassDB::bind_method(D_METHOD("_set_output_ports_expanded", "values"), &VisualShaderNode::_set_output_ports_expanded);
  381. ClassDB::bind_method(D_METHOD("_get_output_ports_expanded"), &VisualShaderNode::_get_output_ports_expanded);
  382. ClassDB::bind_method(D_METHOD("set_input_port_default_value", "port", "value", "prev_value"), &VisualShaderNode::set_input_port_default_value, DEFVAL(Variant()));
  383. ClassDB::bind_method(D_METHOD("get_input_port_default_value", "port"), &VisualShaderNode::get_input_port_default_value);
  384. ClassDB::bind_method(D_METHOD("remove_input_port_default_value", "port"), &VisualShaderNode::remove_input_port_default_value);
  385. ClassDB::bind_method(D_METHOD("clear_default_input_values"), &VisualShaderNode::clear_default_input_values);
  386. ClassDB::bind_method(D_METHOD("set_default_input_values", "values"), &VisualShaderNode::set_default_input_values);
  387. ClassDB::bind_method(D_METHOD("get_default_input_values"), &VisualShaderNode::get_default_input_values);
  388. ClassDB::bind_method(D_METHOD("set_frame", "frame"), &VisualShaderNode::set_frame);
  389. ClassDB::bind_method(D_METHOD("get_frame"), &VisualShaderNode::get_frame);
  390. ADD_PROPERTY(PropertyInfo(Variant::INT, "output_port_for_preview"), "set_output_port_for_preview", "get_output_port_for_preview");
  391. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "default_input_values", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_default_input_values", "get_default_input_values");
  392. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "expanded_output_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_output_ports_expanded", "_get_output_ports_expanded");
  393. ADD_PROPERTY(PropertyInfo(Variant::INT, "linked_parent_graph_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_frame", "get_frame");
  394. BIND_ENUM_CONSTANT(PORT_TYPE_SCALAR);
  395. BIND_ENUM_CONSTANT(PORT_TYPE_SCALAR_INT);
  396. BIND_ENUM_CONSTANT(PORT_TYPE_SCALAR_UINT);
  397. BIND_ENUM_CONSTANT(PORT_TYPE_VECTOR_2D);
  398. BIND_ENUM_CONSTANT(PORT_TYPE_VECTOR_3D);
  399. BIND_ENUM_CONSTANT(PORT_TYPE_VECTOR_4D);
  400. BIND_ENUM_CONSTANT(PORT_TYPE_BOOLEAN);
  401. BIND_ENUM_CONSTANT(PORT_TYPE_TRANSFORM);
  402. BIND_ENUM_CONSTANT(PORT_TYPE_SAMPLER);
  403. BIND_ENUM_CONSTANT(PORT_TYPE_MAX);
  404. }
  405. VisualShaderNode::VisualShaderNode() {
  406. }
  407. /////////////////////////////////////////////////////////
  408. void VisualShaderNodeCustom::update_property_default_values() {
  409. int prop_count;
  410. if (GDVIRTUAL_CALL(_get_property_count, prop_count)) {
  411. for (int i = 0; i < prop_count; i++) {
  412. int selected = 0;
  413. if (GDVIRTUAL_CALL(_get_property_default_index, i, selected)) {
  414. dp_selected_cache[i] = selected;
  415. }
  416. }
  417. }
  418. }
  419. void VisualShaderNodeCustom::update_input_port_default_values() {
  420. int input_port_count;
  421. if (GDVIRTUAL_CALL(_get_input_port_count, input_port_count)) {
  422. for (int i = 0; i < input_port_count; i++) {
  423. Variant value;
  424. if (GDVIRTUAL_CALL(_get_input_port_default_value, i, value)) {
  425. default_input_values[i] = value;
  426. }
  427. }
  428. }
  429. }
  430. void VisualShaderNodeCustom::update_ports() {
  431. {
  432. dp_props.clear();
  433. int prop_count;
  434. if (GDVIRTUAL_CALL(_get_property_count, prop_count)) {
  435. for (int i = 0; i < prop_count; i++) {
  436. DropDownListProperty prop;
  437. if (!GDVIRTUAL_CALL(_get_property_name, i, prop.name)) {
  438. prop.name = "prop";
  439. }
  440. if (!GDVIRTUAL_CALL(_get_property_options, i, prop.options)) {
  441. prop.options.push_back("Default");
  442. }
  443. dp_props.push_back(prop);
  444. }
  445. }
  446. }
  447. {
  448. Vector<String> vprops = properties.split(";", false);
  449. for (int i = 0; i < vprops.size(); i++) {
  450. Vector<String> arr = vprops[i].split(",", false);
  451. ERR_FAIL_COND(arr.size() != 2);
  452. ERR_FAIL_COND(!arr[0].is_valid_int());
  453. ERR_FAIL_COND(!arr[1].is_valid_int());
  454. int index = arr[0].to_int();
  455. int selected = arr[1].to_int();
  456. dp_selected_cache[index] = selected;
  457. }
  458. }
  459. {
  460. input_ports.clear();
  461. int input_port_count;
  462. if (GDVIRTUAL_CALL(_get_input_port_count, input_port_count)) {
  463. for (int i = 0; i < input_port_count; i++) {
  464. Port port;
  465. if (!GDVIRTUAL_CALL(_get_input_port_name, i, port.name)) {
  466. port.name = "in" + itos(i);
  467. }
  468. PortType port_type;
  469. if (GDVIRTUAL_CALL(_get_input_port_type, i, port_type)) {
  470. port.type = (int)port_type;
  471. } else {
  472. port.type = (int)PortType::PORT_TYPE_SCALAR;
  473. }
  474. input_ports.push_back(port);
  475. }
  476. }
  477. }
  478. {
  479. output_ports.clear();
  480. int output_port_count;
  481. if (GDVIRTUAL_CALL(_get_output_port_count, output_port_count)) {
  482. for (int i = 0; i < output_port_count; i++) {
  483. Port port;
  484. if (!GDVIRTUAL_CALL(_get_output_port_name, i, port.name)) {
  485. port.name = "out" + itos(i);
  486. }
  487. PortType port_type;
  488. if (GDVIRTUAL_CALL(_get_output_port_type, i, port_type)) {
  489. port.type = (int)port_type;
  490. } else {
  491. port.type = (int)PortType::PORT_TYPE_SCALAR;
  492. }
  493. output_ports.push_back(port);
  494. }
  495. }
  496. }
  497. }
  498. void VisualShaderNodeCustom::update_properties() {
  499. properties = "";
  500. for (const KeyValue<int, int> &p : dp_selected_cache) {
  501. if (p.value != 0) {
  502. properties += itos(p.key) + "," + itos(p.value) + ";";
  503. }
  504. }
  505. }
  506. String VisualShaderNodeCustom::get_caption() const {
  507. String ret = "Unnamed";
  508. GDVIRTUAL_CALL(_get_name, ret);
  509. return ret;
  510. }
  511. int VisualShaderNodeCustom::get_input_port_count() const {
  512. return input_ports.size();
  513. }
  514. VisualShaderNodeCustom::PortType VisualShaderNodeCustom::get_input_port_type(int p_port) const {
  515. ERR_FAIL_INDEX_V(p_port, input_ports.size(), PORT_TYPE_SCALAR);
  516. return (PortType)input_ports.get(p_port).type;
  517. }
  518. String VisualShaderNodeCustom::get_input_port_name(int p_port) const {
  519. ERR_FAIL_INDEX_V(p_port, input_ports.size(), "");
  520. return input_ports.get(p_port).name;
  521. }
  522. int VisualShaderNodeCustom::get_default_input_port(PortType p_type) const {
  523. int ret = 0;
  524. GDVIRTUAL_CALL(_get_default_input_port, p_type, ret);
  525. return ret;
  526. }
  527. int VisualShaderNodeCustom::get_output_port_count() const {
  528. return output_ports.size();
  529. }
  530. VisualShaderNodeCustom::PortType VisualShaderNodeCustom::get_output_port_type(int p_port) const {
  531. ERR_FAIL_INDEX_V(p_port, output_ports.size(), PORT_TYPE_SCALAR);
  532. return (PortType)output_ports.get(p_port).type;
  533. }
  534. String VisualShaderNodeCustom::get_output_port_name(int p_port) const {
  535. ERR_FAIL_INDEX_V(p_port, output_ports.size(), "");
  536. return output_ports.get(p_port).name;
  537. }
  538. String VisualShaderNodeCustom::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 {
  539. ERR_FAIL_COND_V(!GDVIRTUAL_IS_OVERRIDDEN(_get_code), "");
  540. TypedArray<String> input_vars;
  541. for (int i = 0; i < get_input_port_count(); i++) {
  542. input_vars.push_back(p_input_vars[i]);
  543. }
  544. TypedArray<String> output_vars;
  545. for (int i = 0; i < get_output_port_count(); i++) {
  546. output_vars.push_back(p_output_vars[i]);
  547. }
  548. String _code;
  549. GDVIRTUAL_CALL(_get_code, input_vars, output_vars, p_mode, p_type, _code);
  550. if (_is_valid_code(_code)) {
  551. String code = " {\n";
  552. bool nend = _code.ends_with("\n");
  553. _code = _code.insert(0, " ");
  554. _code = _code.replace("\n", "\n ");
  555. code += _code;
  556. if (!nend) {
  557. code += "\n }";
  558. } else {
  559. code.remove_at(code.size() - 1);
  560. code += "}";
  561. }
  562. code += "\n";
  563. return code;
  564. }
  565. return String();
  566. }
  567. String VisualShaderNodeCustom::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  568. String _code;
  569. if (GDVIRTUAL_CALL(_get_global_code, p_mode, _code)) {
  570. if (_is_valid_code(_code)) {
  571. String code = "// " + get_caption() + "\n";
  572. code += _code;
  573. code += "\n";
  574. return code;
  575. }
  576. }
  577. return String();
  578. }
  579. String VisualShaderNodeCustom::generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  580. String _code;
  581. if (GDVIRTUAL_CALL(_get_func_code, p_mode, p_type, _code)) {
  582. if (_is_valid_code(_code)) {
  583. bool nend = _code.ends_with("\n");
  584. String code = "// " + get_caption() + "\n";
  585. code += " {\n";
  586. _code = _code.insert(0, " ");
  587. _code = _code.replace("\n", "\n ");
  588. code += _code;
  589. if (!nend) {
  590. code += "\n }";
  591. } else {
  592. code.remove_at(code.size() - 1);
  593. code += "}";
  594. }
  595. code += "\n";
  596. return code;
  597. }
  598. }
  599. return String();
  600. }
  601. bool VisualShaderNodeCustom::is_available(Shader::Mode p_mode, VisualShader::Type p_type) const {
  602. bool ret = true;
  603. GDVIRTUAL_CALL(_is_available, p_mode, p_type, ret);
  604. return ret;
  605. }
  606. void VisualShaderNodeCustom::set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value) {
  607. if (!is_initialized) {
  608. VisualShaderNode::set_input_port_default_value(p_port, p_value, p_prev_value);
  609. }
  610. }
  611. void VisualShaderNodeCustom::set_default_input_values(const Array &p_values) {
  612. if (!is_initialized) {
  613. VisualShaderNode::set_default_input_values(p_values);
  614. }
  615. }
  616. void VisualShaderNodeCustom::remove_input_port_default_value(int p_port) {
  617. if (!is_initialized) {
  618. VisualShaderNode::remove_input_port_default_value(p_port);
  619. }
  620. }
  621. void VisualShaderNodeCustom::clear_default_input_values() {
  622. if (!is_initialized) {
  623. VisualShaderNode::clear_default_input_values();
  624. }
  625. }
  626. void VisualShaderNodeCustom::_set_input_port_default_value(int p_port, const Variant &p_value) {
  627. VisualShaderNode::set_input_port_default_value(p_port, p_value);
  628. }
  629. bool VisualShaderNodeCustom::_is_valid_code(const String &p_code) const {
  630. if (p_code.is_empty() || p_code == "null") {
  631. return false;
  632. }
  633. return true;
  634. }
  635. bool VisualShaderNodeCustom::_is_initialized() {
  636. return is_initialized;
  637. }
  638. void VisualShaderNodeCustom::_set_initialized(bool p_enabled) {
  639. is_initialized = p_enabled;
  640. }
  641. void VisualShaderNodeCustom::_set_properties(const String &p_properties) {
  642. properties = p_properties;
  643. }
  644. String VisualShaderNodeCustom::_get_properties() const {
  645. return properties;
  646. }
  647. String VisualShaderNodeCustom::_get_name() const {
  648. String ret;
  649. GDVIRTUAL_CALL(_get_name, ret);
  650. return ret;
  651. }
  652. String VisualShaderNodeCustom::_get_description() const {
  653. String ret;
  654. GDVIRTUAL_CALL(_get_description, ret);
  655. return ret;
  656. }
  657. String VisualShaderNodeCustom::_get_category() const {
  658. String ret;
  659. GDVIRTUAL_CALL(_get_category, ret);
  660. return ret;
  661. }
  662. VisualShaderNodeCustom::PortType VisualShaderNodeCustom::_get_return_icon_type() const {
  663. PortType ret = PORT_TYPE_SCALAR;
  664. GDVIRTUAL_CALL(_get_return_icon_type, ret);
  665. return ret;
  666. }
  667. bool VisualShaderNodeCustom::_is_highend() const {
  668. bool ret = false;
  669. GDVIRTUAL_CALL(_is_highend, ret);
  670. return ret;
  671. }
  672. void VisualShaderNodeCustom::_set_option_index(int p_option, int p_value) {
  673. dp_selected_cache[p_option] = p_value;
  674. update_properties();
  675. update_ports();
  676. update_input_port_default_values();
  677. emit_changed();
  678. }
  679. int VisualShaderNodeCustom::get_option_index(int p_option) const {
  680. if (!dp_selected_cache.has(p_option)) {
  681. return 0;
  682. }
  683. return dp_selected_cache[p_option];
  684. }
  685. void VisualShaderNodeCustom::_bind_methods() {
  686. GDVIRTUAL_BIND(_get_name);
  687. GDVIRTUAL_BIND(_get_description);
  688. GDVIRTUAL_BIND(_get_category);
  689. GDVIRTUAL_BIND(_get_return_icon_type);
  690. GDVIRTUAL_BIND(_get_input_port_count);
  691. GDVIRTUAL_BIND(_get_input_port_type, "port");
  692. GDVIRTUAL_BIND(_get_input_port_name, "port");
  693. GDVIRTUAL_BIND(_get_input_port_default_value, "port");
  694. GDVIRTUAL_BIND(_get_default_input_port, "type");
  695. GDVIRTUAL_BIND(_get_output_port_count);
  696. GDVIRTUAL_BIND(_get_output_port_type, "port");
  697. GDVIRTUAL_BIND(_get_output_port_name, "port");
  698. GDVIRTUAL_BIND(_get_property_count);
  699. GDVIRTUAL_BIND(_get_property_name, "index");
  700. GDVIRTUAL_BIND(_get_property_default_index, "index");
  701. GDVIRTUAL_BIND(_get_property_options, "index");
  702. GDVIRTUAL_BIND(_get_code, "input_vars", "output_vars", "mode", "type");
  703. GDVIRTUAL_BIND(_get_func_code, "mode", "type");
  704. GDVIRTUAL_BIND(_get_global_code, "mode");
  705. GDVIRTUAL_BIND(_is_highend);
  706. GDVIRTUAL_BIND(_is_available, "mode", "type");
  707. ClassDB::bind_method(D_METHOD("_set_initialized", "enabled"), &VisualShaderNodeCustom::_set_initialized);
  708. ClassDB::bind_method(D_METHOD("_is_initialized"), &VisualShaderNodeCustom::_is_initialized);
  709. ClassDB::bind_method(D_METHOD("_set_input_port_default_value", "port", "value"), &VisualShaderNodeCustom::_set_input_port_default_value);
  710. ClassDB::bind_method(D_METHOD("_set_option_index", "option", "value"), &VisualShaderNodeCustom::_set_option_index);
  711. ClassDB::bind_method(D_METHOD("_set_properties", "properties"), &VisualShaderNodeCustom::_set_properties);
  712. ClassDB::bind_method(D_METHOD("_get_properties"), &VisualShaderNodeCustom::_get_properties);
  713. ClassDB::bind_method(D_METHOD("get_option_index", "option"), &VisualShaderNodeCustom::get_option_index);
  714. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_initialized", "_is_initialized");
  715. ADD_PROPERTY(PropertyInfo(Variant::STRING, "properties", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_properties", "_get_properties");
  716. }
  717. VisualShaderNodeCustom::VisualShaderNodeCustom() {
  718. simple_decl = false;
  719. }
  720. /////////////////////////////////////////////////////////
  721. void VisualShader::set_shader_type(Type p_type) {
  722. current_type = p_type;
  723. }
  724. VisualShader::Type VisualShader::get_shader_type() const {
  725. return current_type;
  726. }
  727. void VisualShader::add_varying(const String &p_name, VaryingMode p_mode, VaryingType p_type) {
  728. ERR_FAIL_COND(!p_name.is_valid_ascii_identifier());
  729. ERR_FAIL_INDEX((int)p_mode, (int)VARYING_MODE_MAX);
  730. ERR_FAIL_INDEX((int)p_type, (int)VARYING_TYPE_MAX);
  731. ERR_FAIL_COND(varyings.has(p_name));
  732. Varying var = Varying(p_name, p_mode, p_type);
  733. varyings[p_name] = var;
  734. varyings_list.push_back(var);
  735. _queue_update();
  736. }
  737. void VisualShader::remove_varying(const String &p_name) {
  738. ERR_FAIL_COND(!varyings.has(p_name));
  739. varyings.erase(p_name);
  740. for (List<Varying>::Element *E = varyings_list.front(); E; E = E->next()) {
  741. if (E->get().name == p_name) {
  742. varyings_list.erase(E);
  743. break;
  744. }
  745. }
  746. _queue_update();
  747. }
  748. bool VisualShader::has_varying(const String &p_name) const {
  749. return varyings.has(p_name);
  750. }
  751. int VisualShader::get_varyings_count() const {
  752. return varyings_list.size();
  753. }
  754. const VisualShader::Varying *VisualShader::get_varying_by_index(int p_idx) const {
  755. ERR_FAIL_INDEX_V(p_idx, varyings_list.size(), nullptr);
  756. return &varyings_list.get(p_idx);
  757. }
  758. void VisualShader::set_varying_mode(const String &p_name, VaryingMode p_mode) {
  759. ERR_FAIL_INDEX((int)p_mode, (int)VARYING_MODE_MAX);
  760. ERR_FAIL_COND(!varyings.has(p_name));
  761. if (varyings[p_name].mode == p_mode) {
  762. return;
  763. }
  764. varyings[p_name].mode = p_mode;
  765. _queue_update();
  766. }
  767. VisualShader::VaryingMode VisualShader::get_varying_mode(const String &p_name) {
  768. ERR_FAIL_COND_V(!varyings.has(p_name), VARYING_MODE_MAX);
  769. return varyings[p_name].mode;
  770. }
  771. void VisualShader::set_varying_type(const String &p_name, VaryingType p_type) {
  772. ERR_FAIL_INDEX((int)p_type, (int)VARYING_TYPE_MAX);
  773. ERR_FAIL_COND(!varyings.has(p_name));
  774. if (varyings[p_name].type == p_type) {
  775. return;
  776. }
  777. varyings[p_name].type = p_type;
  778. _queue_update();
  779. }
  780. VisualShader::VaryingType VisualShader::get_varying_type(const String &p_name) {
  781. ERR_FAIL_COND_V(!varyings.has(p_name), VARYING_TYPE_MAX);
  782. return varyings[p_name].type;
  783. }
  784. void VisualShader::_set_preview_shader_parameter(const String &p_name, const Variant &p_value) {
  785. #ifdef TOOLS_ENABLED
  786. if (Engine::get_singleton()->is_editor_hint()) {
  787. if (p_value.get_type() == Variant::NIL) {
  788. if (!preview_params.erase(p_name)) {
  789. return;
  790. }
  791. } else {
  792. Variant *var = preview_params.getptr(p_name);
  793. if (var != nullptr && *var == p_value) {
  794. return;
  795. }
  796. preview_params.insert(p_name, p_value);
  797. }
  798. emit_changed();
  799. }
  800. #endif // TOOLS_ENABLED
  801. }
  802. Variant VisualShader::_get_preview_shader_parameter(const String &p_name) const {
  803. #ifdef TOOLS_ENABLED
  804. if (Engine::get_singleton()->is_editor_hint()) {
  805. ERR_FAIL_COND_V(!preview_params.has(p_name), Variant());
  806. return preview_params.get(p_name);
  807. }
  808. #endif // TOOLS_ENABLED
  809. return Variant();
  810. }
  811. bool VisualShader::_has_preview_shader_parameter(const String &p_name) const {
  812. #ifdef TOOLS_ENABLED
  813. if (Engine::get_singleton()->is_editor_hint()) {
  814. return preview_params.has(p_name);
  815. }
  816. #endif // TOOLS_ENABLED
  817. return false;
  818. }
  819. void VisualShader::add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id) {
  820. ERR_FAIL_COND(p_node.is_null());
  821. ERR_FAIL_COND(p_id < 2);
  822. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  823. Graph *g = &graph[p_type];
  824. ERR_FAIL_COND(g->nodes.has(p_id));
  825. Node n;
  826. n.node = p_node;
  827. n.position = p_position;
  828. Ref<VisualShaderNodeParameter> parameter = n.node;
  829. if (parameter.is_valid()) {
  830. String valid_name = validate_parameter_name(parameter->get_parameter_name(), parameter);
  831. parameter->set_parameter_name(valid_name);
  832. }
  833. Ref<VisualShaderNodeInput> input = n.node;
  834. if (input.is_valid()) {
  835. input->shader_mode = shader_mode;
  836. input->shader_type = p_type;
  837. }
  838. n.node->connect_changed(callable_mp(this, &VisualShader::_queue_update));
  839. Ref<VisualShaderNodeCustom> custom = n.node;
  840. if (custom.is_valid()) {
  841. custom->update_ports();
  842. }
  843. g->nodes[p_id] = n;
  844. _queue_update();
  845. }
  846. void VisualShader::set_node_position(Type p_type, int p_id, const Vector2 &p_position) {
  847. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  848. Graph *g = &graph[p_type];
  849. ERR_FAIL_COND(!g->nodes.has(p_id));
  850. g->nodes[p_id].position = p_position;
  851. }
  852. Vector2 VisualShader::get_node_position(Type p_type, int p_id) const {
  853. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector2());
  854. const Graph *g = &graph[p_type];
  855. ERR_FAIL_COND_V(!g->nodes.has(p_id), Vector2());
  856. return g->nodes[p_id].position;
  857. }
  858. Ref<VisualShaderNode> VisualShader::get_node(Type p_type, int p_id) const {
  859. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Ref<VisualShaderNode>());
  860. const Graph *g = &graph[p_type];
  861. if (!g->nodes.has(p_id)) {
  862. return Ref<VisualShaderNode>();
  863. }
  864. ERR_FAIL_COND_V(!g->nodes.has(p_id), Ref<VisualShaderNode>());
  865. return g->nodes[p_id].node;
  866. }
  867. Vector<int> VisualShader::get_node_list(Type p_type) const {
  868. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector<int>());
  869. const Graph *g = &graph[p_type];
  870. Vector<int> ret;
  871. for (const KeyValue<int, Node> &E : g->nodes) {
  872. ret.push_back(E.key);
  873. }
  874. return ret;
  875. }
  876. int VisualShader::get_valid_node_id(Type p_type) const {
  877. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, NODE_ID_INVALID);
  878. const Graph *g = &graph[p_type];
  879. return g->nodes.size() ? MAX(2, g->nodes.back()->key() + 1) : 2;
  880. }
  881. int VisualShader::find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const {
  882. for (const KeyValue<int, Node> &E : graph[p_type].nodes) {
  883. if (E.value.node == p_node) {
  884. return E.key;
  885. }
  886. }
  887. return NODE_ID_INVALID;
  888. }
  889. void VisualShader::remove_node(Type p_type, int p_id) {
  890. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  891. ERR_FAIL_COND(p_id < 2);
  892. Graph *g = &graph[p_type];
  893. ERR_FAIL_COND(!g->nodes.has(p_id));
  894. g->nodes[p_id].node->disconnect_changed(callable_mp(this, &VisualShader::_queue_update));
  895. g->nodes.erase(p_id);
  896. for (List<Connection>::Element *E = g->connections.front(); E;) {
  897. List<Connection>::Element *N = E->next();
  898. const VisualShader::Connection &connection = E->get();
  899. if (connection.from_node == p_id || connection.to_node == p_id) {
  900. if (connection.from_node == p_id) {
  901. g->nodes[connection.to_node].prev_connected_nodes.erase(p_id);
  902. g->nodes[connection.to_node].node->set_input_port_connected(connection.to_port, false);
  903. } else if (connection.to_node == p_id) {
  904. g->nodes[connection.from_node].next_connected_nodes.erase(p_id);
  905. g->nodes[connection.from_node].node->set_output_port_connected(connection.from_port, false);
  906. }
  907. g->connections.erase(E);
  908. }
  909. E = N;
  910. }
  911. _queue_update();
  912. }
  913. void VisualShader::replace_node(Type p_type, int p_id, const StringName &p_new_class) {
  914. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  915. ERR_FAIL_COND(p_id < 2);
  916. Graph *g = &graph[p_type];
  917. ERR_FAIL_COND(!g->nodes.has(p_id));
  918. if (g->nodes[p_id].node->get_class_name() == p_new_class) {
  919. return;
  920. }
  921. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instantiate(p_new_class));
  922. VisualShaderNode *prev_vsn = g->nodes[p_id].node.ptr();
  923. // Update connection data.
  924. for (int i = 0; i < vsn->get_output_port_count(); i++) {
  925. if (i < prev_vsn->get_output_port_count()) {
  926. if (prev_vsn->is_output_port_connected(i)) {
  927. vsn->set_output_port_connected(i, true);
  928. }
  929. if (prev_vsn->is_output_port_expandable(i) && prev_vsn->_is_output_port_expanded(i) && vsn->is_output_port_expandable(i)) {
  930. vsn->_set_output_port_expanded(i, true);
  931. int component_count = 0;
  932. switch (prev_vsn->get_output_port_type(i)) {
  933. case VisualShaderNode::PORT_TYPE_VECTOR_2D:
  934. component_count = 2;
  935. break;
  936. case VisualShaderNode::PORT_TYPE_VECTOR_3D:
  937. component_count = 3;
  938. break;
  939. case VisualShaderNode::PORT_TYPE_VECTOR_4D:
  940. component_count = 4;
  941. break;
  942. default:
  943. break;
  944. }
  945. for (int j = 0; j < component_count; j++) {
  946. int sub_port = i + 1 + j;
  947. if (prev_vsn->is_output_port_connected(sub_port)) {
  948. vsn->set_output_port_connected(sub_port, true);
  949. }
  950. }
  951. i += component_count;
  952. }
  953. } else {
  954. break;
  955. }
  956. }
  957. vsn->connect_changed(callable_mp(this, &VisualShader::_queue_update));
  958. g->nodes[p_id].node = Ref<VisualShaderNode>(vsn);
  959. _queue_update();
  960. }
  961. bool VisualShader::is_node_connection(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const {
  962. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  963. const Graph *g = &graph[p_type];
  964. for (const Connection &E : g->connections) {
  965. if (E.from_node == p_from_node && E.from_port == p_from_port && E.to_node == p_to_node && E.to_port == p_to_port) {
  966. return true;
  967. }
  968. }
  969. return false;
  970. }
  971. bool VisualShader::is_nodes_connected_relatively(const Graph *p_graph, int p_node, int p_target) const {
  972. bool result = false;
  973. const VisualShader::Node &node = p_graph->nodes[p_node];
  974. for (const int &E : node.prev_connected_nodes) {
  975. if (E == p_target) {
  976. return true;
  977. }
  978. result = is_nodes_connected_relatively(p_graph, E, p_target);
  979. if (result) {
  980. break;
  981. }
  982. }
  983. return result;
  984. }
  985. bool VisualShader::_check_reroute_subgraph(Type p_type, int p_target_port_type, int p_reroute_node, List<int> *r_visited_reroute_nodes) const {
  986. const Graph *g = &graph[p_type];
  987. // BFS to check whether connecting to the given subgraph (rooted at p_reroute_node) is valid.
  988. List<int> queue;
  989. queue.push_back(p_reroute_node);
  990. if (r_visited_reroute_nodes != nullptr) {
  991. r_visited_reroute_nodes->push_back(p_reroute_node);
  992. }
  993. while (!queue.is_empty()) {
  994. int current_node_id = queue.front()->get();
  995. VisualShader::Node current_node = g->nodes[current_node_id];
  996. queue.pop_front();
  997. for (const int &next_node_id : current_node.next_connected_nodes) {
  998. Ref<VisualShaderNodeReroute> next_vsnode = g->nodes[next_node_id].node;
  999. if (next_vsnode.is_valid()) {
  1000. queue.push_back(next_node_id);
  1001. if (r_visited_reroute_nodes != nullptr) {
  1002. r_visited_reroute_nodes->push_back(next_node_id);
  1003. }
  1004. continue;
  1005. }
  1006. // Check whether all ports connected with the reroute node are compatible.
  1007. for (const Connection &c : g->connections) {
  1008. VisualShaderNode::PortType to_port_type = g->nodes[next_node_id].node->get_input_port_type(c.to_port);
  1009. if (c.from_node == current_node_id &&
  1010. c.to_node == next_node_id &&
  1011. !is_port_types_compatible(p_target_port_type, to_port_type)) {
  1012. return false;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. return true;
  1018. }
  1019. bool VisualShader::can_connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const {
  1020. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  1021. const Graph *g = &graph[p_type];
  1022. if (!g->nodes.has(p_from_node)) {
  1023. return false;
  1024. }
  1025. if (p_from_node == p_to_node) {
  1026. return false;
  1027. }
  1028. if (p_from_port < 0 || p_from_port >= g->nodes[p_from_node].node->get_expanded_output_port_count()) {
  1029. return false;
  1030. }
  1031. if (!g->nodes.has(p_to_node)) {
  1032. return false;
  1033. }
  1034. if (p_to_port < 0 || p_to_port >= g->nodes[p_to_node].node->get_input_port_count()) {
  1035. return false;
  1036. }
  1037. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  1038. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  1039. Ref<VisualShaderNodeReroute> to_node_reroute = g->nodes[p_to_node].node;
  1040. if (to_node_reroute.is_valid()) {
  1041. if (!_check_reroute_subgraph(p_type, from_port_type, p_to_node)) {
  1042. return false;
  1043. }
  1044. } else if (!is_port_types_compatible(from_port_type, to_port_type)) {
  1045. return false;
  1046. }
  1047. for (const Connection &E : g->connections) {
  1048. if (E.from_node == p_from_node && E.from_port == p_from_port && E.to_node == p_to_node && E.to_port == p_to_port) {
  1049. return false;
  1050. }
  1051. }
  1052. if (is_nodes_connected_relatively(g, p_from_node, p_to_node)) {
  1053. return false;
  1054. }
  1055. return true;
  1056. }
  1057. bool VisualShader::is_port_types_compatible(int p_a, int p_b) const {
  1058. return MAX(0, p_a - (int)VisualShaderNode::PORT_TYPE_BOOLEAN) == (MAX(0, p_b - (int)VisualShaderNode::PORT_TYPE_BOOLEAN));
  1059. }
  1060. void VisualShader::attach_node_to_frame(Type p_type, int p_node, int p_frame) {
  1061. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  1062. ERR_FAIL_COND(p_frame < 0);
  1063. Graph *g = &graph[p_type];
  1064. ERR_FAIL_COND(!g->nodes.has(p_node));
  1065. g->nodes[p_node].node->set_frame(p_frame);
  1066. Ref<VisualShaderNodeFrame> vsnode_frame = g->nodes[p_frame].node;
  1067. if (vsnode_frame.is_valid()) {
  1068. vsnode_frame->add_attached_node(p_node);
  1069. }
  1070. }
  1071. void VisualShader::detach_node_from_frame(Type p_type, int p_node) {
  1072. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  1073. Graph *g = &graph[p_type];
  1074. ERR_FAIL_COND(!g->nodes.has(p_node));
  1075. int parent_frame_id = g->nodes[p_node].node->get_frame();
  1076. Ref<VisualShaderNodeFrame> vsnode_frame = g->nodes[parent_frame_id].node;
  1077. if (vsnode_frame.is_valid()) {
  1078. vsnode_frame->remove_attached_node(p_node);
  1079. }
  1080. g->nodes[p_node].node->set_frame(-1);
  1081. }
  1082. String VisualShader::get_reroute_parameter_name(Type p_type, int p_reroute_node) const {
  1083. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, "");
  1084. const Graph *g = &graph[p_type];
  1085. ERR_FAIL_COND_V(!g->nodes.has(p_reroute_node), "");
  1086. const VisualShader::Node *node = &g->nodes[p_reroute_node];
  1087. while (node->prev_connected_nodes.size() > 0) {
  1088. int connected_node_id = node->prev_connected_nodes[0];
  1089. node = &g->nodes[connected_node_id];
  1090. Ref<VisualShaderNodeParameter> parameter_node = node->node;
  1091. if (parameter_node.is_valid() && parameter_node->get_output_port_type(0) == VisualShaderNode::PORT_TYPE_SAMPLER) {
  1092. return parameter_node->get_parameter_name();
  1093. }
  1094. Ref<VisualShaderNodeInput> input_node = node->node;
  1095. if (input_node.is_valid() && input_node->get_output_port_type(0) == VisualShaderNode::PORT_TYPE_SAMPLER) {
  1096. return input_node->get_input_real_name();
  1097. }
  1098. }
  1099. return "";
  1100. }
  1101. void VisualShader::connect_nodes_forced(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  1102. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  1103. Graph *g = &graph[p_type];
  1104. ERR_FAIL_COND(!g->nodes.has(p_from_node));
  1105. ERR_FAIL_INDEX(p_from_port, g->nodes[p_from_node].node->get_expanded_output_port_count());
  1106. ERR_FAIL_COND(!g->nodes.has(p_to_node));
  1107. ERR_FAIL_INDEX(p_to_port, g->nodes[p_to_node].node->get_input_port_count());
  1108. for (const Connection &E : g->connections) {
  1109. if (E.from_node == p_from_node && E.from_port == p_from_port && E.to_node == p_to_node && E.to_port == p_to_port) {
  1110. return;
  1111. }
  1112. }
  1113. Connection c;
  1114. c.from_node = p_from_node;
  1115. c.from_port = p_from_port;
  1116. c.to_node = p_to_node;
  1117. c.to_port = p_to_port;
  1118. g->connections.push_back(c);
  1119. g->nodes[p_from_node].next_connected_nodes.push_back(p_to_node);
  1120. g->nodes[p_to_node].prev_connected_nodes.push_back(p_from_node);
  1121. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, true);
  1122. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, true);
  1123. _queue_update();
  1124. }
  1125. Error VisualShader::connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  1126. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, ERR_CANT_CONNECT);
  1127. Graph *g = &graph[p_type];
  1128. ERR_FAIL_COND_V(!g->nodes.has(p_from_node), ERR_INVALID_PARAMETER);
  1129. ERR_FAIL_INDEX_V(p_from_port, g->nodes[p_from_node].node->get_expanded_output_port_count(), ERR_INVALID_PARAMETER);
  1130. ERR_FAIL_COND_V(!g->nodes.has(p_to_node), ERR_INVALID_PARAMETER);
  1131. ERR_FAIL_INDEX_V(p_to_port, g->nodes[p_to_node].node->get_input_port_count(), ERR_INVALID_PARAMETER);
  1132. Ref<VisualShaderNodeReroute> from_node_reroute = g->nodes[p_from_node].node;
  1133. Ref<VisualShaderNodeReroute> to_node_reroute = g->nodes[p_to_node].node;
  1134. // Allow connection with incompatible port types only if the reroute node isn't connected to anything.
  1135. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  1136. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  1137. bool port_types_are_compatible = is_port_types_compatible(from_port_type, to_port_type);
  1138. if (to_node_reroute.is_valid()) {
  1139. List<int> visited_reroute_nodes;
  1140. port_types_are_compatible = _check_reroute_subgraph(p_type, from_port_type, p_to_node, &visited_reroute_nodes);
  1141. if (port_types_are_compatible) {
  1142. // Set the port type of all reroute nodes.
  1143. for (const int &E : visited_reroute_nodes) {
  1144. Ref<VisualShaderNodeReroute> reroute_node = g->nodes[E].node;
  1145. reroute_node->_set_port_type(from_port_type);
  1146. }
  1147. }
  1148. } else if (from_node_reroute.is_valid() && !from_node_reroute->is_input_port_connected(0)) {
  1149. from_node_reroute->_set_port_type(to_port_type);
  1150. port_types_are_compatible = true;
  1151. }
  1152. ERR_FAIL_COND_V_MSG(!port_types_are_compatible, ERR_INVALID_PARAMETER, "Incompatible port types.");
  1153. for (const Connection &E : g->connections) {
  1154. if (E.from_node == p_from_node && E.from_port == p_from_port && E.to_node == p_to_node && E.to_port == p_to_port) {
  1155. ERR_FAIL_V(ERR_ALREADY_EXISTS);
  1156. }
  1157. }
  1158. Connection c;
  1159. c.from_node = p_from_node;
  1160. c.from_port = p_from_port;
  1161. c.to_node = p_to_node;
  1162. c.to_port = p_to_port;
  1163. g->connections.push_back(c);
  1164. g->nodes[p_from_node].next_connected_nodes.push_back(p_to_node);
  1165. g->nodes[p_to_node].prev_connected_nodes.push_back(p_from_node);
  1166. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, true);
  1167. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, true);
  1168. _queue_update();
  1169. return OK;
  1170. }
  1171. void VisualShader::disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  1172. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  1173. Graph *g = &graph[p_type];
  1174. for (List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  1175. if (E->get().from_node == p_from_node && E->get().from_port == p_from_port && E->get().to_node == p_to_node && E->get().to_port == p_to_port) {
  1176. g->connections.erase(E);
  1177. g->nodes[p_from_node].next_connected_nodes.erase(p_to_node);
  1178. g->nodes[p_to_node].prev_connected_nodes.erase(p_from_node);
  1179. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, false);
  1180. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, false);
  1181. _queue_update();
  1182. return;
  1183. }
  1184. }
  1185. }
  1186. TypedArray<Dictionary> VisualShader::_get_node_connections(Type p_type) const {
  1187. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Array());
  1188. const Graph *g = &graph[p_type];
  1189. TypedArray<Dictionary> ret;
  1190. for (const Connection &E : g->connections) {
  1191. Dictionary d;
  1192. d["from_node"] = E.from_node;
  1193. d["from_port"] = E.from_port;
  1194. d["to_node"] = E.to_node;
  1195. d["to_port"] = E.to_port;
  1196. ret.push_back(d);
  1197. }
  1198. return ret;
  1199. }
  1200. void VisualShader::get_node_connections(Type p_type, List<Connection> *r_connections) const {
  1201. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  1202. const Graph *g = &graph[p_type];
  1203. for (const Connection &E : g->connections) {
  1204. r_connections->push_back(E);
  1205. }
  1206. }
  1207. void VisualShader::set_mode(Mode p_mode) {
  1208. ERR_FAIL_INDEX_MSG(p_mode, Mode::MODE_MAX, vformat("Invalid shader mode: %d.", p_mode));
  1209. if (shader_mode == p_mode) {
  1210. return;
  1211. }
  1212. //erase input/output connections
  1213. modes.clear();
  1214. flags.clear();
  1215. shader_mode = p_mode;
  1216. for (int i = 0; i < TYPE_MAX; i++) {
  1217. for (KeyValue<int, Node> &E : graph[i].nodes) {
  1218. Ref<VisualShaderNodeInput> input = E.value.node;
  1219. if (input.is_valid()) {
  1220. input->shader_mode = shader_mode;
  1221. //input->input_index = 0;
  1222. }
  1223. }
  1224. Ref<VisualShaderNodeOutput> output = graph[i].nodes[NODE_ID_OUTPUT].node;
  1225. output->shader_mode = shader_mode;
  1226. // clear connections since they are no longer valid
  1227. for (List<Connection>::Element *E = graph[i].connections.front(); E;) {
  1228. bool keep = true;
  1229. List<Connection>::Element *N = E->next();
  1230. int from = E->get().from_node;
  1231. int to = E->get().to_node;
  1232. if (!graph[i].nodes.has(from)) {
  1233. keep = false;
  1234. } else {
  1235. Ref<VisualShaderNode> from_node = graph[i].nodes[from].node;
  1236. if (from_node->is_class("VisualShaderNodeOutput") || from_node->is_class("VisualShaderNodeInput")) {
  1237. keep = false;
  1238. }
  1239. }
  1240. if (!graph[i].nodes.has(to)) {
  1241. keep = false;
  1242. } else {
  1243. Ref<VisualShaderNode> to_node = graph[i].nodes[to].node;
  1244. if (to_node->is_class("VisualShaderNodeOutput") || to_node->is_class("VisualShaderNodeInput")) {
  1245. keep = false;
  1246. }
  1247. }
  1248. if (!keep) {
  1249. graph[i].connections.erase(E);
  1250. }
  1251. E = N;
  1252. }
  1253. }
  1254. _queue_update();
  1255. notify_property_list_changed();
  1256. }
  1257. void VisualShader::set_graph_offset(const Vector2 &p_offset) {
  1258. graph_offset = p_offset;
  1259. }
  1260. Vector2 VisualShader::get_graph_offset() const {
  1261. return graph_offset;
  1262. }
  1263. Shader::Mode VisualShader::get_mode() const {
  1264. return shader_mode;
  1265. }
  1266. bool VisualShader::is_text_shader() const {
  1267. return false;
  1268. }
  1269. String VisualShader::generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &default_tex_params) const {
  1270. Ref<VisualShaderNode> node = get_node(p_type, p_node);
  1271. ERR_FAIL_COND_V(!node.is_valid(), String());
  1272. ERR_FAIL_COND_V(p_port < 0 || p_port >= node->get_expanded_output_port_count(), String());
  1273. ERR_FAIL_COND_V(node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_TRANSFORM, String());
  1274. StringBuilder global_code;
  1275. StringBuilder global_code_per_node;
  1276. HashMap<Type, StringBuilder> global_code_per_func;
  1277. StringBuilder shader_code;
  1278. HashSet<StringName> classes;
  1279. global_code += String() + "shader_type canvas_item;\n";
  1280. String global_expressions;
  1281. for (int i = 0, index = 0; i < TYPE_MAX; i++) {
  1282. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  1283. Ref<VisualShaderNodeGlobalExpression> global_expression = E.value.node;
  1284. if (global_expression.is_valid()) {
  1285. String expr = "";
  1286. expr += "// " + global_expression->get_caption() + ":" + itos(index++) + "\n";
  1287. expr += global_expression->generate_global(get_mode(), Type(i), -1);
  1288. expr = expr.replace("\n", "\n ");
  1289. expr += "\n";
  1290. global_expressions += expr;
  1291. }
  1292. }
  1293. }
  1294. global_code += "\n";
  1295. global_code += global_expressions;
  1296. //make it faster to go around through shader
  1297. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  1298. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  1299. for (const List<Connection>::Element *E = graph[p_type].connections.front(); E; E = E->next()) {
  1300. ConnectionKey from_key;
  1301. from_key.node = E->get().from_node;
  1302. from_key.port = E->get().from_port;
  1303. output_connections.insert(from_key, E);
  1304. ConnectionKey to_key;
  1305. to_key.node = E->get().to_node;
  1306. to_key.port = E->get().to_port;
  1307. input_connections.insert(to_key, E);
  1308. }
  1309. shader_code += "\nvoid fragment() {\n";
  1310. HashSet<int> processed;
  1311. Error err = _write_node(p_type, &global_code, &global_code_per_node, &global_code_per_func, shader_code, default_tex_params, input_connections, output_connections, p_node, processed, true, classes);
  1312. ERR_FAIL_COND_V(err != OK, String());
  1313. switch (node->get_output_port_type(p_port)) {
  1314. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1315. shader_code += " COLOR.rgb = vec3(n_out" + itos(p_node) + "p" + itos(p_port) + ");\n";
  1316. } break;
  1317. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1318. shader_code += " COLOR.rgb = vec3(float(n_out" + itos(p_node) + "p" + itos(p_port) + "));\n";
  1319. } break;
  1320. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1321. shader_code += " COLOR.rgb = vec3(float(n_out" + itos(p_node) + "p" + itos(p_port) + "));\n";
  1322. } break;
  1323. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1324. shader_code += " COLOR.rgb = vec3(n_out" + itos(p_node) + "p" + itos(p_port) + " ? 1.0 : 0.0);\n";
  1325. } break;
  1326. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1327. shader_code += " COLOR.rgb = vec3(n_out" + itos(p_node) + "p" + itos(p_port) + ", 0.0);\n";
  1328. } break;
  1329. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1330. shader_code += " COLOR.rgb = n_out" + itos(p_node) + "p" + itos(p_port) + ";\n";
  1331. } break;
  1332. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1333. shader_code += " COLOR = n_out" + itos(p_node) + "p" + itos(p_port) + ";\n";
  1334. } break;
  1335. default: {
  1336. shader_code += " COLOR.rgb = vec3(0.0);\n";
  1337. } break;
  1338. }
  1339. shader_code += "}\n";
  1340. //set code secretly
  1341. global_code += "\n\n";
  1342. String final_code = global_code;
  1343. final_code += global_code_per_node;
  1344. final_code += shader_code;
  1345. return final_code;
  1346. }
  1347. String VisualShader::validate_port_name(const String &p_port_name, VisualShaderNode *p_node, int p_port_id, bool p_output) const {
  1348. String port_name = p_port_name;
  1349. if (port_name.is_empty()) {
  1350. return String();
  1351. }
  1352. while (port_name.length() && !is_ascii_alphabet_char(port_name[0])) {
  1353. port_name = port_name.substr(1, port_name.length() - 1);
  1354. }
  1355. if (!port_name.is_empty()) {
  1356. String valid_name;
  1357. for (int i = 0; i < port_name.length(); i++) {
  1358. if (is_ascii_identifier_char(port_name[i])) {
  1359. valid_name += String::chr(port_name[i]);
  1360. } else if (port_name[i] == ' ') {
  1361. valid_name += "_";
  1362. }
  1363. }
  1364. port_name = valid_name;
  1365. } else {
  1366. return String();
  1367. }
  1368. List<String> input_names;
  1369. List<String> output_names;
  1370. for (int i = 0; i < p_node->get_input_port_count(); i++) {
  1371. if (!p_output && i == p_port_id) {
  1372. continue;
  1373. }
  1374. if (port_name == p_node->get_input_port_name(i)) {
  1375. return String();
  1376. }
  1377. }
  1378. for (int i = 0; i < p_node->get_output_port_count(); i++) {
  1379. if (p_output && i == p_port_id) {
  1380. continue;
  1381. }
  1382. if (port_name == p_node->get_output_port_name(i)) {
  1383. return String();
  1384. }
  1385. }
  1386. return port_name;
  1387. }
  1388. String VisualShader::validate_parameter_name(const String &p_name, const Ref<VisualShaderNodeParameter> &p_parameter) const {
  1389. String param_name = p_name; //validate name first
  1390. while (param_name.length() && !is_ascii_alphabet_char(param_name[0])) {
  1391. param_name = param_name.substr(1, param_name.length() - 1);
  1392. }
  1393. if (!param_name.is_empty()) {
  1394. String valid_name;
  1395. for (int i = 0; i < param_name.length(); i++) {
  1396. if (is_ascii_identifier_char(param_name[i])) {
  1397. valid_name += String::chr(param_name[i]);
  1398. } else if (param_name[i] == ' ') {
  1399. valid_name += "_";
  1400. }
  1401. }
  1402. param_name = valid_name;
  1403. }
  1404. if (param_name.is_empty()) {
  1405. param_name = p_parameter->get_caption();
  1406. }
  1407. int attempt = 1;
  1408. while (true) {
  1409. bool exists = false;
  1410. for (int i = 0; i < TYPE_MAX; i++) {
  1411. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  1412. Ref<VisualShaderNodeParameter> node = E.value.node;
  1413. if (node == p_parameter) { //do not test on self
  1414. continue;
  1415. }
  1416. if (node.is_valid() && node->get_parameter_name() == param_name) {
  1417. exists = true;
  1418. break;
  1419. }
  1420. }
  1421. if (exists) {
  1422. break;
  1423. }
  1424. }
  1425. if (exists) {
  1426. //remove numbers, put new and try again
  1427. attempt++;
  1428. while (param_name.length() && is_digit(param_name[param_name.length() - 1])) {
  1429. param_name = param_name.substr(0, param_name.length() - 1);
  1430. }
  1431. ERR_FAIL_COND_V(param_name.is_empty(), String());
  1432. param_name += itos(attempt);
  1433. } else {
  1434. break;
  1435. }
  1436. }
  1437. return param_name;
  1438. }
  1439. static const char *type_string[VisualShader::TYPE_MAX] = {
  1440. "vertex",
  1441. "fragment",
  1442. "light",
  1443. "start",
  1444. "process",
  1445. "collide",
  1446. "start_custom",
  1447. "process_custom",
  1448. "sky",
  1449. "fog",
  1450. };
  1451. bool VisualShader::_set(const StringName &p_name, const Variant &p_value) {
  1452. String prop_name = p_name;
  1453. if (prop_name == "mode") {
  1454. set_mode(Shader::Mode(int(p_value)));
  1455. return true;
  1456. } else if (prop_name.begins_with("flags/")) {
  1457. StringName flag = prop_name.get_slicec('/', 1);
  1458. bool enable = p_value;
  1459. if (enable) {
  1460. flags.insert(flag);
  1461. } else {
  1462. flags.erase(flag);
  1463. }
  1464. _queue_update();
  1465. return true;
  1466. } else if (prop_name.begins_with("modes/")) {
  1467. String mode_name = prop_name.get_slicec('/', 1);
  1468. int value = p_value;
  1469. if (value == 0) {
  1470. modes.erase(mode_name); //means it's default anyway, so don't store it
  1471. } else {
  1472. modes[mode_name] = value;
  1473. }
  1474. _queue_update();
  1475. return true;
  1476. } else if (prop_name.begins_with("varyings/")) {
  1477. String var_name = prop_name.get_slicec('/', 1);
  1478. Varying value = Varying();
  1479. value.name = var_name;
  1480. if (value.from_string(p_value) && !varyings.has(var_name)) {
  1481. varyings[var_name] = value;
  1482. varyings_list.push_back(value);
  1483. }
  1484. _queue_update();
  1485. return true;
  1486. }
  1487. #ifdef TOOLS_ENABLED
  1488. else if (prop_name.begins_with("preview_params/") && Engine::get_singleton()->is_editor_hint()) {
  1489. String param_name = prop_name.get_slicec('/', 1);
  1490. Variant value = VariantUtilityFunctions::str_to_var(p_value);
  1491. preview_params[param_name] = value;
  1492. return true;
  1493. }
  1494. #endif
  1495. else if (prop_name.begins_with("nodes/")) {
  1496. String typestr = prop_name.get_slicec('/', 1);
  1497. Type type = TYPE_VERTEX;
  1498. for (int i = 0; i < TYPE_MAX; i++) {
  1499. if (typestr == type_string[i]) {
  1500. type = Type(i);
  1501. break;
  1502. }
  1503. }
  1504. String index = prop_name.get_slicec('/', 2);
  1505. if (index == "connections") {
  1506. Vector<int> conns = p_value;
  1507. if (conns.size() % 4 == 0) {
  1508. for (int i = 0; i < conns.size(); i += 4) {
  1509. connect_nodes_forced(type, conns[i + 0], conns[i + 1], conns[i + 2], conns[i + 3]);
  1510. }
  1511. }
  1512. return true;
  1513. }
  1514. int id = index.to_int();
  1515. String what = prop_name.get_slicec('/', 3);
  1516. if (what == "node") {
  1517. add_node(type, p_value, Vector2(), id);
  1518. return true;
  1519. } else if (what == "position") {
  1520. set_node_position(type, id, p_value);
  1521. return true;
  1522. } else if (what == "size") {
  1523. ((VisualShaderNodeResizableBase *)get_node(type, id).ptr())->set_size(p_value);
  1524. return true;
  1525. } else if (what == "input_ports") {
  1526. ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->set_inputs(p_value);
  1527. return true;
  1528. } else if (what == "output_ports") {
  1529. ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->set_outputs(p_value);
  1530. return true;
  1531. } else if (what == "expression") {
  1532. ((VisualShaderNodeExpression *)get_node(type, id).ptr())->set_expression(p_value);
  1533. return true;
  1534. }
  1535. }
  1536. return false;
  1537. }
  1538. bool VisualShader::_get(const StringName &p_name, Variant &r_ret) const {
  1539. String prop_name = p_name;
  1540. if (prop_name == "mode") {
  1541. r_ret = get_mode();
  1542. return true;
  1543. } else if (prop_name.begins_with("flags/")) {
  1544. StringName flag = prop_name.get_slicec('/', 1);
  1545. r_ret = flags.has(flag);
  1546. return true;
  1547. } else if (prop_name.begins_with("modes/")) {
  1548. String mode_name = prop_name.get_slicec('/', 1);
  1549. if (modes.has(mode_name)) {
  1550. r_ret = modes[mode_name];
  1551. } else {
  1552. r_ret = 0;
  1553. }
  1554. return true;
  1555. } else if (prop_name.begins_with("varyings/")) {
  1556. String var_name = prop_name.get_slicec('/', 1);
  1557. if (varyings.has(var_name)) {
  1558. r_ret = varyings[var_name].to_string();
  1559. } else {
  1560. r_ret = String();
  1561. }
  1562. return true;
  1563. }
  1564. #ifdef TOOLS_ENABLED
  1565. else if (prop_name.begins_with("preview_params/") && Engine::get_singleton()->is_editor_hint()) {
  1566. String param_name = prop_name.get_slicec('/', 1);
  1567. if (preview_params.has(param_name)) {
  1568. r_ret = VariantUtilityFunctions::var_to_str(preview_params[param_name]);
  1569. } else {
  1570. r_ret = String();
  1571. }
  1572. return true;
  1573. }
  1574. #endif // TOOLS_ENABLED
  1575. else if (prop_name.begins_with("nodes/")) {
  1576. String typestr = prop_name.get_slicec('/', 1);
  1577. Type type = TYPE_VERTEX;
  1578. for (int i = 0; i < TYPE_MAX; i++) {
  1579. if (typestr == type_string[i]) {
  1580. type = Type(i);
  1581. break;
  1582. }
  1583. }
  1584. String index = prop_name.get_slicec('/', 2);
  1585. if (index == "connections") {
  1586. Vector<int> conns;
  1587. for (const Connection &E : graph[type].connections) {
  1588. conns.push_back(E.from_node);
  1589. conns.push_back(E.from_port);
  1590. conns.push_back(E.to_node);
  1591. conns.push_back(E.to_port);
  1592. }
  1593. r_ret = conns;
  1594. return true;
  1595. }
  1596. int id = index.to_int();
  1597. String what = prop_name.get_slicec('/', 3);
  1598. if (what == "node") {
  1599. r_ret = get_node(type, id);
  1600. return true;
  1601. } else if (what == "position") {
  1602. r_ret = get_node_position(type, id);
  1603. return true;
  1604. } else if (what == "size") {
  1605. r_ret = ((VisualShaderNodeResizableBase *)get_node(type, id).ptr())->get_size();
  1606. return true;
  1607. } else if (what == "input_ports") {
  1608. r_ret = ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->get_inputs();
  1609. return true;
  1610. } else if (what == "output_ports") {
  1611. r_ret = ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->get_outputs();
  1612. return true;
  1613. } else if (what == "expression") {
  1614. r_ret = ((VisualShaderNodeExpression *)get_node(type, id).ptr())->get_expression();
  1615. return true;
  1616. }
  1617. }
  1618. return false;
  1619. }
  1620. void VisualShader::reset_state() {
  1621. // TODO: Everything needs to be cleared here.
  1622. emit_changed();
  1623. }
  1624. void VisualShader::_get_property_list(List<PropertyInfo> *p_list) const {
  1625. //mode
  1626. p_list->push_back(PropertyInfo(Variant::INT, PNAME("mode"), PROPERTY_HINT_ENUM, "Spatial,CanvasItem,Particles,Sky,Fog"));
  1627. //render modes
  1628. HashMap<String, String> blend_mode_enums;
  1629. HashSet<String> toggles;
  1630. const Vector<ShaderLanguage::ModeInfo> &rmodes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader_mode));
  1631. for (int i = 0; i < rmodes.size(); i++) {
  1632. const ShaderLanguage::ModeInfo &info = rmodes[i];
  1633. if (!info.options.is_empty()) {
  1634. const String begin = String(info.name);
  1635. for (int j = 0; j < info.options.size(); j++) {
  1636. const String option = String(info.options[j]).capitalize();
  1637. if (!blend_mode_enums.has(begin)) {
  1638. blend_mode_enums[begin] = option;
  1639. } else {
  1640. blend_mode_enums[begin] += "," + option;
  1641. }
  1642. }
  1643. } else {
  1644. toggles.insert(String(info.name));
  1645. }
  1646. }
  1647. for (const KeyValue<String, String> &E : blend_mode_enums) {
  1648. p_list->push_back(PropertyInfo(Variant::INT, vformat("%s/%s", PNAME("modes"), E.key), PROPERTY_HINT_ENUM, E.value));
  1649. }
  1650. for (const String &E : toggles) {
  1651. p_list->push_back(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("flags"), E)));
  1652. }
  1653. for (const KeyValue<String, Varying> &E : varyings) {
  1654. p_list->push_back(PropertyInfo(Variant::STRING, vformat("%s/%s", "varyings", E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1655. }
  1656. #ifdef TOOLS_ENABLED
  1657. if (Engine::get_singleton()->is_editor_hint()) {
  1658. for (const KeyValue<String, Variant> &E : preview_params) {
  1659. p_list->push_back(PropertyInfo(Variant::STRING, vformat("%s/%s", "preview_params", E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1660. }
  1661. }
  1662. #endif // TOOLS_ENABLED
  1663. for (int i = 0; i < TYPE_MAX; i++) {
  1664. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  1665. String prop_name = "nodes/";
  1666. prop_name += type_string[i];
  1667. prop_name += "/" + itos(E.key);
  1668. if (E.key != NODE_ID_OUTPUT) {
  1669. p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_ALWAYS_DUPLICATE));
  1670. }
  1671. p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1672. if (Object::cast_to<VisualShaderNodeGroupBase>(E.value.node.ptr()) != nullptr) {
  1673. p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1674. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/input_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1675. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/output_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1676. }
  1677. if (Object::cast_to<VisualShaderNodeExpression>(E.value.node.ptr()) != nullptr) {
  1678. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/expression", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1679. }
  1680. }
  1681. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "nodes/" + String(type_string[i]) + "/connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1682. }
  1683. }
  1684. Error VisualShader::_write_node(Type type, StringBuilder *p_global_code, StringBuilder *p_global_code_per_node, HashMap<Type, StringBuilder> *p_global_code_per_func, StringBuilder &r_code, Vector<VisualShader::DefaultTextureParam> &r_def_tex_params, const VMap<ConnectionKey, const List<Connection>::Element *> &p_input_connections, const VMap<ConnectionKey, const List<Connection>::Element *> &p_output_connections, int p_node, HashSet<int> &r_processed, bool p_for_preview, HashSet<StringName> &r_classes) const {
  1685. const Ref<VisualShaderNode> vsnode = graph[type].nodes[p_node].node;
  1686. if (vsnode->is_disabled()) {
  1687. r_code += "// " + vsnode->get_caption() + ":" + itos(p_node) + "\n";
  1688. r_code += " // Node is disabled and code is not generated.\n";
  1689. return OK;
  1690. }
  1691. //check inputs recursively first
  1692. int input_count = vsnode->get_input_port_count();
  1693. for (int i = 0; i < input_count; i++) {
  1694. ConnectionKey ck;
  1695. ck.node = p_node;
  1696. ck.port = i;
  1697. if (p_input_connections.has(ck)) {
  1698. int from_node = p_input_connections[ck]->get().from_node;
  1699. if (r_processed.has(from_node)) {
  1700. continue;
  1701. }
  1702. Error err = _write_node(type, p_global_code, p_global_code_per_node, p_global_code_per_func, r_code, r_def_tex_params, p_input_connections, p_output_connections, from_node, r_processed, p_for_preview, r_classes);
  1703. if (err) {
  1704. return err;
  1705. }
  1706. }
  1707. }
  1708. // then this node
  1709. Vector<VisualShader::DefaultTextureParam> params = vsnode->get_default_texture_parameters(type, p_node);
  1710. for (int i = 0; i < params.size(); i++) {
  1711. r_def_tex_params.push_back(params[i]);
  1712. }
  1713. Ref<VisualShaderNodeInput> input = vsnode;
  1714. bool skip_global = input.is_valid() && p_for_preview;
  1715. if (!skip_global) {
  1716. Ref<VisualShaderNodeParameter> parameter = vsnode;
  1717. if (!parameter.is_valid() || !parameter->is_global_code_generated()) {
  1718. if (p_global_code) {
  1719. *p_global_code += vsnode->generate_global(get_mode(), type, p_node);
  1720. }
  1721. }
  1722. String class_name = vsnode->get_class_name();
  1723. if (class_name == "VisualShaderNodeCustom") {
  1724. class_name = vsnode->get_script_instance()->get_script()->get_path();
  1725. }
  1726. if (!r_classes.has(class_name)) {
  1727. if (p_global_code_per_node) {
  1728. *p_global_code_per_node += vsnode->generate_global_per_node(get_mode(), p_node);
  1729. }
  1730. for (int i = 0; i < TYPE_MAX; i++) {
  1731. if (p_global_code_per_func) {
  1732. (*p_global_code_per_func)[Type(i)] += vsnode->generate_global_per_func(get_mode(), Type(i), p_node);
  1733. }
  1734. }
  1735. r_classes.insert(class_name);
  1736. }
  1737. }
  1738. if (!vsnode->is_code_generated()) { // just generate globals and ignore locals
  1739. r_processed.insert(p_node);
  1740. return OK;
  1741. }
  1742. String node_name = "// " + vsnode->get_caption() + ":" + itos(p_node) + "\n";
  1743. String node_code;
  1744. Vector<String> input_vars;
  1745. input_vars.resize(vsnode->get_input_port_count());
  1746. String *inputs = input_vars.ptrw();
  1747. for (int i = 0; i < input_count; i++) {
  1748. ConnectionKey ck;
  1749. ck.node = p_node;
  1750. ck.port = i;
  1751. if (p_input_connections.has(ck)) {
  1752. //connected to something, use that output
  1753. int from_node = p_input_connections[ck]->get().from_node;
  1754. if (graph[type].nodes[from_node].node->is_disabled()) {
  1755. continue;
  1756. }
  1757. int from_port = p_input_connections[ck]->get().from_port;
  1758. VisualShaderNode::PortType in_type = vsnode->get_input_port_type(i);
  1759. VisualShaderNode::PortType out_type = graph[type].nodes[from_node].node->get_output_port_type(from_port);
  1760. String src_var = "n_out" + itos(from_node) + "p" + itos(from_port);
  1761. if (in_type == VisualShaderNode::PORT_TYPE_SAMPLER && out_type == VisualShaderNode::PORT_TYPE_SAMPLER) {
  1762. VisualShaderNode *ptr = const_cast<VisualShaderNode *>(graph[type].nodes[from_node].node.ptr());
  1763. // FIXME: This needs to be refactored at some point.
  1764. if (ptr->has_method("get_input_real_name")) {
  1765. inputs[i] = ptr->call("get_input_real_name");
  1766. } else if (ptr->has_method("get_parameter_name")) {
  1767. inputs[i] = ptr->call("get_parameter_name");
  1768. } else {
  1769. Ref<VisualShaderNodeReroute> reroute = graph[type].nodes[from_node].node;
  1770. if (reroute.is_valid()) {
  1771. inputs[i] = get_reroute_parameter_name(type, from_node);
  1772. } else {
  1773. inputs[i] = "";
  1774. }
  1775. }
  1776. } else if (in_type == out_type) {
  1777. inputs[i] = src_var;
  1778. } else {
  1779. switch (in_type) {
  1780. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1781. switch (out_type) {
  1782. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1783. inputs[i] = "float(" + src_var + ")";
  1784. } break;
  1785. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1786. inputs[i] = "float(" + src_var + ")";
  1787. } break;
  1788. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1789. inputs[i] = "(" + src_var + " ? 1.0 : 0.0)";
  1790. } break;
  1791. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1792. inputs[i] = src_var + ".x";
  1793. } break;
  1794. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1795. inputs[i] = src_var + ".x";
  1796. } break;
  1797. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1798. inputs[i] = src_var + ".x";
  1799. } break;
  1800. default:
  1801. break;
  1802. }
  1803. } break;
  1804. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1805. switch (out_type) {
  1806. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1807. inputs[i] = "int(" + src_var + ")";
  1808. } break;
  1809. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1810. inputs[i] = "int(" + src_var + ")";
  1811. } break;
  1812. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1813. inputs[i] = "(" + src_var + " ? 1 : 0)";
  1814. } break;
  1815. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1816. inputs[i] = "int(" + src_var + ".x)";
  1817. } break;
  1818. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1819. inputs[i] = "int(" + src_var + ".x)";
  1820. } break;
  1821. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1822. inputs[i] = "int(" + src_var + ".x)";
  1823. } break;
  1824. default:
  1825. break;
  1826. }
  1827. } break;
  1828. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1829. switch (out_type) {
  1830. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1831. inputs[i] = "uint(" + src_var + ")";
  1832. } break;
  1833. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1834. inputs[i] = "uint(" + src_var + ")";
  1835. } break;
  1836. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1837. inputs[i] = "(" + src_var + " ? 1u : 0u)";
  1838. } break;
  1839. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1840. inputs[i] = "uint(" + src_var + ".x)";
  1841. } break;
  1842. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1843. inputs[i] = "uint(" + src_var + ".x)";
  1844. } break;
  1845. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1846. inputs[i] = "uint(" + src_var + ".x)";
  1847. } break;
  1848. default:
  1849. break;
  1850. }
  1851. } break;
  1852. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1853. switch (out_type) {
  1854. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1855. inputs[i] = src_var + " > 0.0 ? true : false";
  1856. } break;
  1857. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1858. inputs[i] = src_var + " > 0 ? true : false";
  1859. } break;
  1860. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1861. inputs[i] = src_var + " > 0u ? true : false";
  1862. } break;
  1863. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1864. inputs[i] = "all(bvec2(" + src_var + "))";
  1865. } break;
  1866. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1867. inputs[i] = "all(bvec3(" + src_var + "))";
  1868. } break;
  1869. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1870. inputs[i] = "all(bvec4(" + src_var + "))";
  1871. } break;
  1872. default:
  1873. break;
  1874. }
  1875. } break;
  1876. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1877. switch (out_type) {
  1878. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1879. inputs[i] = "vec2(" + src_var + ")";
  1880. } break;
  1881. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1882. inputs[i] = "vec2(float(" + src_var + "))";
  1883. } break;
  1884. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1885. inputs[i] = "vec2(float(" + src_var + "))";
  1886. } break;
  1887. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1888. inputs[i] = "vec2(" + src_var + " ? 1.0 : 0.0)";
  1889. } break;
  1890. case VisualShaderNode::PORT_TYPE_VECTOR_3D:
  1891. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1892. inputs[i] = "vec2(" + src_var + ".xy)";
  1893. } break;
  1894. default:
  1895. break;
  1896. }
  1897. } break;
  1898. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1899. switch (out_type) {
  1900. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1901. inputs[i] = "vec3(" + src_var + ")";
  1902. } break;
  1903. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1904. inputs[i] = "vec3(float(" + src_var + "))";
  1905. } break;
  1906. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1907. inputs[i] = "vec3(float(" + src_var + "))";
  1908. } break;
  1909. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1910. inputs[i] = "vec3(" + src_var + " ? 1.0 : 0.0)";
  1911. } break;
  1912. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1913. inputs[i] = "vec3(" + src_var + ", 0.0)";
  1914. } break;
  1915. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1916. inputs[i] = "vec3(" + src_var + ".xyz)";
  1917. } break;
  1918. default:
  1919. break;
  1920. }
  1921. } break;
  1922. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1923. switch (out_type) {
  1924. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1925. inputs[i] = "vec4(" + src_var + ")";
  1926. } break;
  1927. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1928. inputs[i] = "vec4(float(" + src_var + "))";
  1929. } break;
  1930. case VisualShaderNode::PORT_TYPE_SCALAR_UINT: {
  1931. inputs[i] = "vec4(float(" + src_var + "))";
  1932. } break;
  1933. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1934. inputs[i] = "vec4(" + src_var + " ? 1.0 : 0.0)";
  1935. } break;
  1936. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1937. inputs[i] = "vec4(" + src_var + ", 0.0, 0.0)";
  1938. } break;
  1939. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1940. inputs[i] = "vec4(" + src_var + ", 0.0)";
  1941. } break;
  1942. default:
  1943. break;
  1944. }
  1945. } break;
  1946. default:
  1947. break;
  1948. }
  1949. }
  1950. } else {
  1951. if (!vsnode->is_generate_input_var(i)) {
  1952. continue;
  1953. }
  1954. Variant defval = vsnode->get_input_port_default_value(i);
  1955. if (defval.get_type() == Variant::FLOAT) {
  1956. float val = defval;
  1957. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1958. node_code += " float " + inputs[i] + " = " + vformat("%.5f", val) + ";\n";
  1959. } else if (defval.get_type() == Variant::INT) {
  1960. int val = defval;
  1961. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1962. if (vsnode->get_input_port_type(i) == VisualShaderNode::PORT_TYPE_SCALAR_UINT) {
  1963. node_code += " uint " + inputs[i] + " = " + itos(val) + "u;\n";
  1964. } else {
  1965. node_code += " int " + inputs[i] + " = " + itos(val) + ";\n";
  1966. }
  1967. } else if (defval.get_type() == Variant::BOOL) {
  1968. bool val = defval;
  1969. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1970. node_code += " bool " + inputs[i] + " = " + (val ? "true" : "false") + ";\n";
  1971. } else if (defval.get_type() == Variant::VECTOR2) {
  1972. Vector2 val = defval;
  1973. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1974. node_code += " vec2 " + inputs[i] + " = " + vformat("vec2(%.5f, %.5f);\n", val.x, val.y);
  1975. } else if (defval.get_type() == Variant::VECTOR3) {
  1976. Vector3 val = defval;
  1977. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1978. node_code += " vec3 " + inputs[i] + " = " + vformat("vec3(%.5f, %.5f, %.5f);\n", val.x, val.y, val.z);
  1979. } else if (defval.get_type() == Variant::VECTOR4) {
  1980. Vector4 val = defval;
  1981. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1982. node_code += " vec4 " + inputs[i] + " = " + vformat("vec4(%.5f, %.5f, %.5f, %.5f);\n", val.x, val.y, val.z, val.w);
  1983. } else if (defval.get_type() == Variant::QUATERNION) {
  1984. Quaternion val = defval;
  1985. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1986. node_code += " vec4 " + inputs[i] + " = " + vformat("vec4(%.5f, %.5f, %.5f, %.5f);\n", val.x, val.y, val.z, val.w);
  1987. } else if (defval.get_type() == Variant::TRANSFORM3D) {
  1988. Transform3D val = defval;
  1989. val.basis.transpose();
  1990. inputs[i] = "n_in" + itos(p_node) + "p" + itos(i);
  1991. Array values;
  1992. for (int j = 0; j < 3; j++) {
  1993. values.push_back(val.basis[j].x);
  1994. values.push_back(val.basis[j].y);
  1995. values.push_back(val.basis[j].z);
  1996. }
  1997. values.push_back(val.origin.x);
  1998. values.push_back(val.origin.y);
  1999. values.push_back(val.origin.z);
  2000. bool err = false;
  2001. node_code += " mat4 " + inputs[i] + " = " + String("mat4(vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 1.0));\n").sprintf(values, &err);
  2002. } else {
  2003. //will go empty, node is expected to know what it is doing at this point and handle it
  2004. }
  2005. }
  2006. }
  2007. int output_count = vsnode->get_output_port_count();
  2008. int initial_output_count = output_count;
  2009. HashMap<int, bool> expanded_output_ports;
  2010. for (int i = 0; i < initial_output_count; i++) {
  2011. bool expanded = false;
  2012. if (vsnode->is_output_port_expandable(i) && vsnode->_is_output_port_expanded(i)) {
  2013. expanded = true;
  2014. switch (vsnode->get_output_port_type(i)) {
  2015. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  2016. output_count += 2;
  2017. } break;
  2018. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  2019. output_count += 3;
  2020. } break;
  2021. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  2022. output_count += 4;
  2023. } break;
  2024. default:
  2025. break;
  2026. }
  2027. }
  2028. expanded_output_ports.insert(i, expanded);
  2029. }
  2030. Vector<String> output_vars;
  2031. output_vars.resize(output_count);
  2032. String *outputs = output_vars.ptrw();
  2033. if (vsnode->is_simple_decl()) { // less code to generate for some simple_decl nodes
  2034. for (int i = 0, j = 0; i < initial_output_count; i++, j++) {
  2035. String var_name = "n_out" + itos(p_node) + "p" + itos(j);
  2036. switch (vsnode->get_output_port_type(i)) {
  2037. case VisualShaderNode::PORT_TYPE_SCALAR:
  2038. outputs[i] = "float " + var_name;
  2039. break;
  2040. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  2041. outputs[i] = "int " + var_name;
  2042. break;
  2043. case VisualShaderNode::PORT_TYPE_SCALAR_UINT:
  2044. outputs[i] = "uint " + var_name;
  2045. break;
  2046. case VisualShaderNode::PORT_TYPE_VECTOR_2D:
  2047. outputs[i] = "vec2 " + var_name;
  2048. break;
  2049. case VisualShaderNode::PORT_TYPE_VECTOR_3D:
  2050. outputs[i] = "vec3 " + var_name;
  2051. break;
  2052. case VisualShaderNode::PORT_TYPE_VECTOR_4D:
  2053. outputs[i] = "vec4 " + var_name;
  2054. break;
  2055. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  2056. outputs[i] = "bool " + var_name;
  2057. break;
  2058. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  2059. outputs[i] = "mat4 " + var_name;
  2060. break;
  2061. default:
  2062. break;
  2063. }
  2064. if (expanded_output_ports[i]) {
  2065. switch (vsnode->get_output_port_type(i)) {
  2066. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  2067. j += 2;
  2068. } break;
  2069. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  2070. j += 3;
  2071. } break;
  2072. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  2073. j += 4;
  2074. } break;
  2075. default:
  2076. break;
  2077. }
  2078. }
  2079. }
  2080. } else {
  2081. for (int i = 0, j = 0; i < initial_output_count; i++, j++) {
  2082. outputs[i] = "n_out" + itos(p_node) + "p" + itos(j);
  2083. switch (vsnode->get_output_port_type(i)) {
  2084. case VisualShaderNode::PORT_TYPE_SCALAR:
  2085. r_code += " float " + outputs[i] + ";\n";
  2086. break;
  2087. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  2088. r_code += " int " + outputs[i] + ";\n";
  2089. break;
  2090. case VisualShaderNode::PORT_TYPE_SCALAR_UINT:
  2091. r_code += " uint " + outputs[i] + ";\n";
  2092. break;
  2093. case VisualShaderNode::PORT_TYPE_VECTOR_2D:
  2094. r_code += " vec2 " + outputs[i] + ";\n";
  2095. break;
  2096. case VisualShaderNode::PORT_TYPE_VECTOR_3D:
  2097. r_code += " vec3 " + outputs[i] + ";\n";
  2098. break;
  2099. case VisualShaderNode::PORT_TYPE_VECTOR_4D:
  2100. r_code += " vec4 " + outputs[i] + ";\n";
  2101. break;
  2102. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  2103. r_code += " bool " + outputs[i] + ";\n";
  2104. break;
  2105. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  2106. r_code += " mat4 " + outputs[i] + ";\n";
  2107. break;
  2108. default:
  2109. break;
  2110. }
  2111. if (expanded_output_ports[i]) {
  2112. switch (vsnode->get_output_port_type(i)) {
  2113. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  2114. j += 2;
  2115. } break;
  2116. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  2117. j += 3;
  2118. } break;
  2119. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  2120. j += 4;
  2121. } break;
  2122. default:
  2123. break;
  2124. }
  2125. }
  2126. }
  2127. }
  2128. node_code += vsnode->generate_code(get_mode(), type, p_node, inputs, outputs, p_for_preview);
  2129. if (!node_code.is_empty()) {
  2130. r_code += node_name;
  2131. r_code += node_code;
  2132. }
  2133. for (int i = 0; i < output_count; i++) {
  2134. if (expanded_output_ports[i]) {
  2135. switch (vsnode->get_output_port_type(i)) {
  2136. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  2137. if (vsnode->is_output_port_connected(i + 1) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 1))) { // red-component
  2138. String r = "n_out" + itos(p_node) + "p" + itos(i + 1);
  2139. r_code += " float " + r + " = n_out" + itos(p_node) + "p" + itos(i) + ".r;\n";
  2140. outputs[i + 1] = r;
  2141. }
  2142. if (vsnode->is_output_port_connected(i + 2) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 2))) { // green-component
  2143. String g = "n_out" + itos(p_node) + "p" + itos(i + 2);
  2144. r_code += " float " + g + " = n_out" + itos(p_node) + "p" + itos(i) + ".g;\n";
  2145. outputs[i + 2] = g;
  2146. }
  2147. i += 2;
  2148. } break;
  2149. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  2150. if (vsnode->is_output_port_connected(i + 1) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 1))) { // red-component
  2151. String r = "n_out" + itos(p_node) + "p" + itos(i + 1);
  2152. r_code += " float " + r + " = n_out" + itos(p_node) + "p" + itos(i) + ".r;\n";
  2153. outputs[i + 1] = r;
  2154. }
  2155. if (vsnode->is_output_port_connected(i + 2) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 2))) { // green-component
  2156. String g = "n_out" + itos(p_node) + "p" + itos(i + 2);
  2157. r_code += " float " + g + " = n_out" + itos(p_node) + "p" + itos(i) + ".g;\n";
  2158. outputs[i + 2] = g;
  2159. }
  2160. if (vsnode->is_output_port_connected(i + 3) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 3))) { // blue-component
  2161. String b = "n_out" + itos(p_node) + "p" + itos(i + 3);
  2162. r_code += " float " + b + " = n_out" + itos(p_node) + "p" + itos(i) + ".b;\n";
  2163. outputs[i + 3] = b;
  2164. }
  2165. i += 3;
  2166. } break;
  2167. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  2168. if (vsnode->is_output_port_connected(i + 1) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 1))) { // red-component
  2169. String r = "n_out" + itos(p_node) + "p" + itos(i + 1);
  2170. r_code += " float " + r + " = n_out" + itos(p_node) + "p" + itos(i) + ".r;\n";
  2171. outputs[i + 1] = r;
  2172. }
  2173. if (vsnode->is_output_port_connected(i + 2) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 2))) { // green-component
  2174. String g = "n_out" + itos(p_node) + "p" + itos(i + 2);
  2175. r_code += " float " + g + " = n_out" + itos(p_node) + "p" + itos(i) + ".g;\n";
  2176. outputs[i + 2] = g;
  2177. }
  2178. if (vsnode->is_output_port_connected(i + 3) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 3))) { // blue-component
  2179. String b = "n_out" + itos(p_node) + "p" + itos(i + 3);
  2180. r_code += " float " + b + " = n_out" + itos(p_node) + "p" + itos(i) + ".b;\n";
  2181. outputs[i + 3] = b;
  2182. }
  2183. if (vsnode->is_output_port_connected(i + 4) || (p_for_preview && vsnode->get_output_port_for_preview() == (i + 4))) { // alpha-component
  2184. String a = "n_out" + itos(p_node) + "p" + itos(i + 4);
  2185. r_code += " float " + a + " = n_out" + itos(p_node) + "p" + itos(i) + ".a;\n";
  2186. outputs[i + 4] = a;
  2187. }
  2188. i += 4;
  2189. } break;
  2190. default:
  2191. break;
  2192. }
  2193. }
  2194. }
  2195. if (!node_code.is_empty()) {
  2196. r_code += "\n\n";
  2197. }
  2198. r_processed.insert(p_node);
  2199. return OK;
  2200. }
  2201. bool VisualShader::has_func_name(RenderingServer::ShaderMode p_mode, const String &p_func_name) const {
  2202. if (!ShaderTypes::get_singleton()->get_functions(p_mode).has(p_func_name)) {
  2203. if (p_mode == RenderingServer::ShaderMode::SHADER_PARTICLES) {
  2204. if (p_func_name == "start_custom" || p_func_name == "process_custom" || p_func_name == "collide") {
  2205. return true;
  2206. }
  2207. }
  2208. return false;
  2209. }
  2210. return true;
  2211. }
  2212. void VisualShader::_update_shader() const {
  2213. if (!dirty.is_set()) {
  2214. return;
  2215. }
  2216. dirty.clear();
  2217. StringBuilder global_code;
  2218. StringBuilder global_code_per_node;
  2219. HashMap<Type, StringBuilder> global_code_per_func;
  2220. StringBuilder shader_code;
  2221. Vector<VisualShader::DefaultTextureParam> default_tex_params;
  2222. HashSet<StringName> classes;
  2223. HashMap<int, int> insertion_pos;
  2224. static const char *shader_mode_str[Shader::MODE_MAX] = { "spatial", "canvas_item", "particles", "sky", "fog" };
  2225. global_code += String() + "shader_type " + shader_mode_str[shader_mode] + ";\n";
  2226. String render_mode;
  2227. {
  2228. const Vector<ShaderLanguage::ModeInfo> &rmodes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader_mode));
  2229. Vector<String> flag_names;
  2230. // Add enum modes first.
  2231. for (int i = 0; i < rmodes.size(); i++) {
  2232. const ShaderLanguage::ModeInfo &info = rmodes[i];
  2233. const String temp = String(info.name);
  2234. if (!info.options.is_empty()) {
  2235. if (!render_mode.is_empty()) {
  2236. render_mode += ", ";
  2237. }
  2238. // Always write out a render_mode for the enumerated modes as having no render mode is not always
  2239. // the same as the default. i.e. for depth_draw_opaque, the render mode has to be declared for it
  2240. // to work properly, no render mode is an invalid option.
  2241. if (modes.has(temp) && modes[temp] < info.options.size()) {
  2242. render_mode += temp + "_" + info.options[modes[temp]];
  2243. } else {
  2244. // Use the default.
  2245. render_mode += temp + "_" + info.options[0];
  2246. }
  2247. } else if (flags.has(temp)) {
  2248. flag_names.push_back(temp);
  2249. }
  2250. }
  2251. // Add flags afterward.
  2252. for (int i = 0; i < flag_names.size(); i++) {
  2253. if (!render_mode.is_empty()) {
  2254. render_mode += ", ";
  2255. }
  2256. render_mode += flag_names[i];
  2257. }
  2258. }
  2259. if (!render_mode.is_empty()) {
  2260. global_code += "render_mode " + render_mode + ";\n\n";
  2261. }
  2262. static const char *func_name[TYPE_MAX] = { "vertex", "fragment", "light", "start", "process", "collide", "start_custom", "process_custom", "sky", "fog" };
  2263. String global_expressions;
  2264. HashSet<String> used_parameter_names;
  2265. List<VisualShaderNodeParameter *> parameters;
  2266. HashMap<int, List<int>> emitters;
  2267. HashMap<int, List<int>> varying_setters;
  2268. for (int i = 0, index = 0; i < TYPE_MAX; i++) {
  2269. if (!has_func_name(RenderingServer::ShaderMode(shader_mode), func_name[i])) {
  2270. continue;
  2271. }
  2272. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  2273. Ref<VisualShaderNodeGlobalExpression> global_expression = E.value.node;
  2274. if (global_expression.is_valid()) {
  2275. String expr = "";
  2276. expr += "// " + global_expression->get_caption() + ":" + itos(index++) + "\n";
  2277. expr += global_expression->generate_global(get_mode(), Type(i), -1);
  2278. expr = expr.replace("\n", "\n ");
  2279. expr += "\n";
  2280. global_expressions += expr;
  2281. }
  2282. Ref<VisualShaderNodeParameterRef> parameter_ref = E.value.node;
  2283. if (parameter_ref.is_valid()) {
  2284. used_parameter_names.insert(parameter_ref->get_parameter_name());
  2285. }
  2286. Ref<VisualShaderNodeParameter> parameter = E.value.node;
  2287. if (parameter.is_valid()) {
  2288. parameters.push_back(parameter.ptr());
  2289. }
  2290. Ref<VisualShaderNodeVaryingSetter> varying_setter = E.value.node;
  2291. if (varying_setter.is_valid() && varying_setter->is_input_port_connected(0)) {
  2292. if (!varying_setters.has(i)) {
  2293. varying_setters.insert(i, List<int>());
  2294. }
  2295. varying_setters[i].push_back(E.key);
  2296. }
  2297. Ref<VisualShaderNodeParticleEmit> emit_particle = E.value.node;
  2298. if (emit_particle.is_valid()) {
  2299. if (!emitters.has(i)) {
  2300. emitters.insert(i, List<int>());
  2301. }
  2302. emitters[i].push_back(E.key);
  2303. }
  2304. }
  2305. }
  2306. int idx = 0;
  2307. for (List<VisualShaderNodeParameter *>::Iterator itr = parameters.begin(); itr != parameters.end(); ++itr, ++idx) {
  2308. VisualShaderNodeParameter *parameter = *itr;
  2309. if (used_parameter_names.has(parameter->get_parameter_name())) {
  2310. global_code += parameter->generate_global(get_mode(), Type(idx), -1);
  2311. const_cast<VisualShaderNodeParameter *>(parameter)->set_global_code_generated(true);
  2312. } else {
  2313. const_cast<VisualShaderNodeParameter *>(parameter)->set_global_code_generated(false);
  2314. }
  2315. }
  2316. if (!varyings.is_empty()) {
  2317. global_code += "\n// Varyings\n";
  2318. for (const KeyValue<String, Varying> &E : varyings) {
  2319. global_code += "varying ";
  2320. switch (E.value.type) {
  2321. case VaryingType::VARYING_TYPE_FLOAT:
  2322. global_code += "float ";
  2323. break;
  2324. case VaryingType::VARYING_TYPE_INT:
  2325. if (E.value.mode == VaryingMode::VARYING_MODE_VERTEX_TO_FRAG_LIGHT) {
  2326. global_code += "flat ";
  2327. }
  2328. global_code += "int ";
  2329. break;
  2330. case VaryingType::VARYING_TYPE_UINT:
  2331. if (E.value.mode == VaryingMode::VARYING_MODE_VERTEX_TO_FRAG_LIGHT) {
  2332. global_code += "flat ";
  2333. }
  2334. global_code += "uint ";
  2335. break;
  2336. case VaryingType::VARYING_TYPE_VECTOR_2D:
  2337. global_code += "vec2 ";
  2338. break;
  2339. case VaryingType::VARYING_TYPE_VECTOR_3D:
  2340. global_code += "vec3 ";
  2341. break;
  2342. case VaryingType::VARYING_TYPE_VECTOR_4D:
  2343. global_code += "vec4 ";
  2344. break;
  2345. case VaryingType::VARYING_TYPE_BOOLEAN:
  2346. global_code += "bool ";
  2347. break;
  2348. case VaryingType::VARYING_TYPE_TRANSFORM:
  2349. global_code += "mat4 ";
  2350. break;
  2351. default:
  2352. break;
  2353. }
  2354. global_code += vformat("var_%s;\n", E.key);
  2355. }
  2356. global_code += "\n";
  2357. }
  2358. HashMap<int, String> code_map;
  2359. HashSet<int> empty_funcs;
  2360. for (int i = 0; i < TYPE_MAX; i++) {
  2361. if (!has_func_name(RenderingServer::ShaderMode(shader_mode), func_name[i])) {
  2362. continue;
  2363. }
  2364. //make it faster to go around through shader
  2365. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  2366. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  2367. StringBuilder func_code;
  2368. HashSet<int> processed;
  2369. bool is_empty_func = false;
  2370. if (shader_mode != Shader::MODE_PARTICLES && shader_mode != Shader::MODE_SKY && shader_mode != Shader::MODE_FOG) {
  2371. is_empty_func = true;
  2372. }
  2373. String varying_code;
  2374. if (shader_mode == Shader::MODE_SPATIAL || shader_mode == Shader::MODE_CANVAS_ITEM) {
  2375. for (const KeyValue<String, Varying> &E : varyings) {
  2376. if ((E.value.mode == VARYING_MODE_VERTEX_TO_FRAG_LIGHT && i == TYPE_VERTEX) || (E.value.mode == VARYING_MODE_FRAG_TO_LIGHT && i == TYPE_FRAGMENT)) {
  2377. bool found = false;
  2378. for (int key : varying_setters[i]) {
  2379. Ref<VisualShaderNodeVaryingSetter> setter = Object::cast_to<VisualShaderNodeVaryingSetter>(const_cast<VisualShaderNode *>(graph[i].nodes[key].node.ptr()));
  2380. if (setter.is_valid() && E.value.name == setter->get_varying_name()) {
  2381. found = true;
  2382. break;
  2383. }
  2384. }
  2385. if (!found) {
  2386. String code2;
  2387. switch (E.value.type) {
  2388. case VaryingType::VARYING_TYPE_FLOAT:
  2389. code2 += "0.0";
  2390. break;
  2391. case VaryingType::VARYING_TYPE_INT:
  2392. code2 += "0";
  2393. break;
  2394. case VaryingType::VARYING_TYPE_UINT:
  2395. code2 += "0u";
  2396. break;
  2397. case VaryingType::VARYING_TYPE_VECTOR_2D:
  2398. code2 += "vec2(0.0)";
  2399. break;
  2400. case VaryingType::VARYING_TYPE_VECTOR_3D:
  2401. code2 += "vec3(0.0)";
  2402. break;
  2403. case VaryingType::VARYING_TYPE_VECTOR_4D:
  2404. code2 += "vec4(0.0)";
  2405. break;
  2406. case VaryingType::VARYING_TYPE_BOOLEAN:
  2407. code2 += "false";
  2408. break;
  2409. case VaryingType::VARYING_TYPE_TRANSFORM:
  2410. code2 += "mat4(1.0)";
  2411. break;
  2412. default:
  2413. break;
  2414. }
  2415. varying_code += vformat(" var_%s = %s;\n", E.key, code2);
  2416. }
  2417. is_empty_func = false;
  2418. }
  2419. }
  2420. }
  2421. for (const List<Connection>::Element *E = graph[i].connections.front(); E; E = E->next()) {
  2422. ConnectionKey from_key;
  2423. from_key.node = E->get().from_node;
  2424. from_key.port = E->get().from_port;
  2425. output_connections.insert(from_key, E);
  2426. ConnectionKey to_key;
  2427. to_key.node = E->get().to_node;
  2428. to_key.port = E->get().to_port;
  2429. input_connections.insert(to_key, E);
  2430. if (is_empty_func && to_key.node == NODE_ID_OUTPUT) {
  2431. is_empty_func = false;
  2432. }
  2433. }
  2434. if (is_empty_func) {
  2435. empty_funcs.insert(i);
  2436. continue;
  2437. }
  2438. if (shader_mode != Shader::MODE_PARTICLES) {
  2439. func_code += "\nvoid " + String(func_name[i]) + "() {\n";
  2440. }
  2441. insertion_pos.insert(i, shader_code.get_string_length() + func_code.get_string_length());
  2442. Error err = _write_node(Type(i), &global_code, &global_code_per_node, &global_code_per_func, func_code, default_tex_params, input_connections, output_connections, NODE_ID_OUTPUT, processed, false, classes);
  2443. ERR_FAIL_COND(err != OK);
  2444. if (varying_setters.has(i)) {
  2445. for (int &E : varying_setters[i]) {
  2446. err = _write_node(Type(i), &global_code, nullptr, nullptr, func_code, default_tex_params, input_connections, output_connections, E, processed, false, classes);
  2447. ERR_FAIL_COND(err != OK);
  2448. }
  2449. }
  2450. if (emitters.has(i)) {
  2451. for (int &E : emitters[i]) {
  2452. err = _write_node(Type(i), &global_code, &global_code_per_node, &global_code_per_func, func_code, default_tex_params, input_connections, output_connections, E, processed, false, classes);
  2453. ERR_FAIL_COND(err != OK);
  2454. }
  2455. }
  2456. if (shader_mode == Shader::MODE_PARTICLES) {
  2457. code_map.insert(i, func_code);
  2458. } else {
  2459. func_code += varying_code;
  2460. func_code += "}\n";
  2461. shader_code += func_code;
  2462. }
  2463. }
  2464. String global_compute_code;
  2465. if (shader_mode == Shader::MODE_PARTICLES) {
  2466. bool has_start_custom = !code_map[TYPE_START_CUSTOM].is_empty();
  2467. bool has_process = !code_map[TYPE_PROCESS].is_empty();
  2468. bool has_process_custom = !code_map[TYPE_PROCESS_CUSTOM].is_empty();
  2469. bool has_collide = !code_map[TYPE_COLLIDE].is_empty();
  2470. shader_code += "void start() {\n";
  2471. shader_code += " uint __seed = __hash(NUMBER + uint(1) + RANDOM_SEED);\n";
  2472. shader_code += "\n";
  2473. shader_code += " {\n";
  2474. shader_code += code_map[TYPE_START].replace("\n ", "\n ");
  2475. shader_code += " }\n";
  2476. if (has_start_custom) {
  2477. shader_code += " \n";
  2478. shader_code += " {\n";
  2479. shader_code += code_map[TYPE_START_CUSTOM].replace("\n ", "\n ");
  2480. shader_code += " }\n";
  2481. }
  2482. shader_code += "}\n\n";
  2483. if (has_process || has_process_custom || has_collide) {
  2484. shader_code += "void process() {\n";
  2485. shader_code += " uint __seed = __hash(NUMBER + uint(1) + RANDOM_SEED);\n";
  2486. shader_code += "\n";
  2487. if (has_process || has_collide) {
  2488. shader_code += " {\n";
  2489. }
  2490. String tab = " ";
  2491. if (has_collide) {
  2492. shader_code += " if (COLLIDED) {\n\n";
  2493. shader_code += code_map[TYPE_COLLIDE].replace("\n ", "\n ");
  2494. if (has_process) {
  2495. shader_code += " } else {\n\n";
  2496. tab += " ";
  2497. }
  2498. }
  2499. if (has_process) {
  2500. shader_code += code_map[TYPE_PROCESS].replace("\n ", "\n " + tab);
  2501. }
  2502. if (has_collide) {
  2503. shader_code += " }\n";
  2504. }
  2505. if (has_process || has_collide) {
  2506. shader_code += " }\n";
  2507. }
  2508. if (has_process_custom) {
  2509. if (has_process || has_collide) {
  2510. shader_code += " \n";
  2511. }
  2512. shader_code += " {\n";
  2513. shader_code += code_map[TYPE_PROCESS_CUSTOM].replace("\n ", "\n ");
  2514. shader_code += " }\n";
  2515. }
  2516. shader_code += "}\n\n";
  2517. }
  2518. global_compute_code += "float __rand_from_seed(inout uint seed) {\n";
  2519. global_compute_code += " int k;\n";
  2520. global_compute_code += " int s = int(seed);\n";
  2521. global_compute_code += " if (s == 0)\n";
  2522. global_compute_code += " s = 305420679;\n";
  2523. global_compute_code += " k = s / 127773;\n";
  2524. global_compute_code += " s = 16807 * (s - k * 127773) - 2836 * k;\n";
  2525. global_compute_code += " if (s < 0)\n";
  2526. global_compute_code += " s += 2147483647;\n";
  2527. global_compute_code += " seed = uint(s);\n";
  2528. global_compute_code += " return float(seed % uint(65536)) / 65535.0;\n";
  2529. global_compute_code += "}\n\n";
  2530. global_compute_code += "float __rand_from_seed_m1_p1(inout uint seed) {\n";
  2531. global_compute_code += " return __rand_from_seed(seed) * 2.0 - 1.0;\n";
  2532. global_compute_code += "}\n\n";
  2533. global_compute_code += "float __randf_range(inout uint seed, float from, float to) {\n";
  2534. global_compute_code += " return __rand_from_seed(seed) * (to - from) + from;\n";
  2535. global_compute_code += "}\n\n";
  2536. global_compute_code += "uint __hash(uint x) {\n";
  2537. global_compute_code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  2538. global_compute_code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  2539. global_compute_code += " x = (x >> uint(16)) ^ x;\n";
  2540. global_compute_code += " return x;\n";
  2541. global_compute_code += "}\n\n";
  2542. global_compute_code += "mat3 __build_rotation_mat3(vec3 axis, float angle) {\n";
  2543. global_compute_code += " axis = normalize(axis);\n";
  2544. global_compute_code += " float s = sin(angle);\n";
  2545. global_compute_code += " float c = cos(angle);\n";
  2546. global_compute_code += " float oc = 1.0 - c;\n";
  2547. global_compute_code += " return mat3(vec3(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s), vec3(oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s), vec3(oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c));\n";
  2548. global_compute_code += "}\n\n";
  2549. global_compute_code += "mat4 __build_rotation_mat4(vec3 axis, float angle) {\n";
  2550. global_compute_code += " axis = normalize(axis);\n";
  2551. global_compute_code += " float s = sin(angle);\n";
  2552. global_compute_code += " float c = cos(angle);\n";
  2553. global_compute_code += " float oc = 1.0 - c;\n";
  2554. global_compute_code += " return mat4(vec4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0), vec4(oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0), vec4(oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0), vec4(0, 0, 0, 1));\n";
  2555. global_compute_code += "}\n\n";
  2556. global_compute_code += "vec2 __get_random_unit_vec2(inout uint seed) {\n";
  2557. global_compute_code += " return normalize(vec2(__rand_from_seed_m1_p1(seed), __rand_from_seed_m1_p1(seed)));\n";
  2558. global_compute_code += "}\n\n";
  2559. global_compute_code += "vec3 __get_random_unit_vec3(inout uint seed) {\n";
  2560. global_compute_code += " return normalize(vec3(__rand_from_seed_m1_p1(seed), __rand_from_seed_m1_p1(seed), __rand_from_seed_m1_p1(seed)));\n";
  2561. global_compute_code += "}\n\n";
  2562. }
  2563. //set code secretly
  2564. global_code += "\n\n";
  2565. String final_code = global_code;
  2566. final_code += global_compute_code;
  2567. final_code += global_code_per_node;
  2568. final_code += global_expressions;
  2569. String tcode = shader_code;
  2570. for (int i = 0; i < TYPE_MAX; i++) {
  2571. if (!has_func_name(RenderingServer::ShaderMode(shader_mode), func_name[i])) {
  2572. continue;
  2573. }
  2574. String func_code = global_code_per_func[Type(i)].as_string();
  2575. if (empty_funcs.has(Type(i)) && !func_code.is_empty()) {
  2576. func_code = vformat("%s%s%s", String("\nvoid " + String(func_name[i]) + "() {\n"), func_code, "}\n");
  2577. }
  2578. tcode = tcode.insert(insertion_pos[i], func_code);
  2579. }
  2580. final_code += tcode;
  2581. const_cast<VisualShader *>(this)->set_code(final_code);
  2582. for (int i = 0; i < default_tex_params.size(); i++) {
  2583. int j = 0;
  2584. for (List<Ref<Texture>>::ConstIterator itr = default_tex_params[i].params.begin(); itr != default_tex_params[i].params.end(); ++itr, ++j) {
  2585. const_cast<VisualShader *>(this)->set_default_texture_parameter(default_tex_params[i].name, *itr, j);
  2586. }
  2587. }
  2588. if (previous_code != final_code) {
  2589. const_cast<VisualShader *>(this)->emit_signal(CoreStringName(changed));
  2590. }
  2591. previous_code = final_code;
  2592. }
  2593. void VisualShader::_queue_update() {
  2594. if (dirty.is_set()) {
  2595. return;
  2596. }
  2597. dirty.set();
  2598. call_deferred(SNAME("_update_shader"));
  2599. }
  2600. void VisualShader::rebuild() {
  2601. dirty.set();
  2602. _update_shader();
  2603. }
  2604. void VisualShader::_bind_methods() {
  2605. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShader::set_mode);
  2606. ClassDB::bind_method(D_METHOD("add_node", "type", "node", "position", "id"), &VisualShader::add_node);
  2607. ClassDB::bind_method(D_METHOD("get_node", "type", "id"), &VisualShader::get_node);
  2608. ClassDB::bind_method(D_METHOD("set_node_position", "type", "id", "position"), &VisualShader::set_node_position);
  2609. ClassDB::bind_method(D_METHOD("get_node_position", "type", "id"), &VisualShader::get_node_position);
  2610. ClassDB::bind_method(D_METHOD("get_node_list", "type"), &VisualShader::get_node_list);
  2611. ClassDB::bind_method(D_METHOD("get_valid_node_id", "type"), &VisualShader::get_valid_node_id);
  2612. ClassDB::bind_method(D_METHOD("remove_node", "type", "id"), &VisualShader::remove_node);
  2613. ClassDB::bind_method(D_METHOD("replace_node", "type", "id", "new_class"), &VisualShader::replace_node);
  2614. ClassDB::bind_method(D_METHOD("is_node_connection", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::is_node_connection);
  2615. ClassDB::bind_method(D_METHOD("can_connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::can_connect_nodes);
  2616. ClassDB::bind_method(D_METHOD("connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::connect_nodes);
  2617. ClassDB::bind_method(D_METHOD("disconnect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::disconnect_nodes);
  2618. ClassDB::bind_method(D_METHOD("connect_nodes_forced", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::connect_nodes_forced);
  2619. ClassDB::bind_method(D_METHOD("get_node_connections", "type"), &VisualShader::_get_node_connections);
  2620. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &VisualShader::set_graph_offset);
  2621. ClassDB::bind_method(D_METHOD("get_graph_offset"), &VisualShader::get_graph_offset);
  2622. ClassDB::bind_method(D_METHOD("attach_node_to_frame", "type", "id", "frame"), &VisualShader::attach_node_to_frame);
  2623. ClassDB::bind_method(D_METHOD("detach_node_from_frame", "type", "id"), &VisualShader::detach_node_from_frame);
  2624. ClassDB::bind_method(D_METHOD("add_varying", "name", "mode", "type"), &VisualShader::add_varying);
  2625. ClassDB::bind_method(D_METHOD("remove_varying", "name"), &VisualShader::remove_varying);
  2626. ClassDB::bind_method(D_METHOD("has_varying", "name"), &VisualShader::has_varying);
  2627. ClassDB::bind_method(D_METHOD("_set_preview_shader_parameter", "name", "value"), &VisualShader::_set_preview_shader_parameter);
  2628. ClassDB::bind_method(D_METHOD("_get_preview_shader_parameter", "name"), &VisualShader::_get_preview_shader_parameter);
  2629. ClassDB::bind_method(D_METHOD("_has_preview_shader_parameter", "name"), &VisualShader::_has_preview_shader_parameter);
  2630. ClassDB::bind_method(D_METHOD("_update_shader"), &VisualShader::_update_shader);
  2631. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");
  2632. ADD_PROPERTY_DEFAULT("code", ""); // Inherited from Shader, prevents showing default code as override in docs.
  2633. BIND_ENUM_CONSTANT(TYPE_VERTEX);
  2634. BIND_ENUM_CONSTANT(TYPE_FRAGMENT);
  2635. BIND_ENUM_CONSTANT(TYPE_LIGHT);
  2636. BIND_ENUM_CONSTANT(TYPE_START);
  2637. BIND_ENUM_CONSTANT(TYPE_PROCESS);
  2638. BIND_ENUM_CONSTANT(TYPE_COLLIDE);
  2639. BIND_ENUM_CONSTANT(TYPE_START_CUSTOM);
  2640. BIND_ENUM_CONSTANT(TYPE_PROCESS_CUSTOM);
  2641. BIND_ENUM_CONSTANT(TYPE_SKY);
  2642. BIND_ENUM_CONSTANT(TYPE_FOG);
  2643. BIND_ENUM_CONSTANT(TYPE_MAX);
  2644. BIND_ENUM_CONSTANT(VARYING_MODE_VERTEX_TO_FRAG_LIGHT);
  2645. BIND_ENUM_CONSTANT(VARYING_MODE_FRAG_TO_LIGHT);
  2646. BIND_ENUM_CONSTANT(VARYING_MODE_MAX);
  2647. BIND_ENUM_CONSTANT(VARYING_TYPE_FLOAT);
  2648. BIND_ENUM_CONSTANT(VARYING_TYPE_INT);
  2649. BIND_ENUM_CONSTANT(VARYING_TYPE_UINT);
  2650. BIND_ENUM_CONSTANT(VARYING_TYPE_VECTOR_2D);
  2651. BIND_ENUM_CONSTANT(VARYING_TYPE_VECTOR_3D);
  2652. BIND_ENUM_CONSTANT(VARYING_TYPE_VECTOR_4D);
  2653. BIND_ENUM_CONSTANT(VARYING_TYPE_BOOLEAN);
  2654. BIND_ENUM_CONSTANT(VARYING_TYPE_TRANSFORM);
  2655. BIND_ENUM_CONSTANT(VARYING_TYPE_MAX);
  2656. BIND_CONSTANT(NODE_ID_INVALID);
  2657. BIND_CONSTANT(NODE_ID_OUTPUT);
  2658. }
  2659. VisualShader::VisualShader() {
  2660. dirty.set();
  2661. for (int i = 0; i < TYPE_MAX; i++) {
  2662. if (i > (int)TYPE_LIGHT && i < (int)TYPE_SKY) {
  2663. Ref<VisualShaderNodeParticleOutput> output;
  2664. output.instantiate();
  2665. output->shader_type = Type(i);
  2666. output->shader_mode = shader_mode;
  2667. graph[i].nodes[NODE_ID_OUTPUT].node = output;
  2668. } else {
  2669. Ref<VisualShaderNodeOutput> output;
  2670. output.instantiate();
  2671. output->shader_type = Type(i);
  2672. output->shader_mode = shader_mode;
  2673. graph[i].nodes[NODE_ID_OUTPUT].node = output;
  2674. }
  2675. graph[i].nodes[NODE_ID_OUTPUT].position = Vector2(400, 150);
  2676. }
  2677. }
  2678. ///////////////////////////////////////////////////////////
  2679. const VisualShaderNodeInput::Port VisualShaderNodeInput::ports[] = {
  2680. // Spatial
  2681. // Node3D, Vertex
  2682. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "BINORMAL" },
  2683. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "camera_direction_world", "CAMERA_DIRECTION_WORLD" },
  2684. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "camera_position_world", "CAMERA_POSITION_WORLD" },
  2685. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "camera_visible_layers", "CAMERA_VISIBLE_LAYERS" },
  2686. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "clip_space_far", "CLIP_SPACE_FAR" },
  2687. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2688. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom0", "CUSTOM0" },
  2689. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom1", "CUSTOM1" },
  2690. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom2", "CUSTOM2" },
  2691. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom3", "CUSTOM3" },
  2692. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "exposure", "EXPOSURE" },
  2693. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "eye_offset", "EYE_OFFSET" },
  2694. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "instance_custom", "INSTANCE_CUSTOM" },
  2695. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "instance_id", "INSTANCE_ID" },
  2696. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection_matrix", "INV_PROJECTION_MATRIX" },
  2697. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_view_matrix", "INV_VIEW_MATRIX" },
  2698. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2699. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "modelview_matrix", "MODELVIEW_MATRIX" },
  2700. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "node_position_view", "NODE_POSITION_VIEW" },
  2701. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "node_position_world", "NODE_POSITION_WORLD" },
  2702. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2703. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  2704. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  2705. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection_matrix", "PROJECTION_MATRIX" },
  2706. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  2707. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "TANGENT" },
  2708. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2709. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2710. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV2" },
  2711. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "vertex", "VERTEX" },
  2712. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "vertex_id", "VERTEX_ID" },
  2713. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_index", "VIEW_INDEX" },
  2714. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "view_matrix", "VIEW_MATRIX" },
  2715. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_mono_left", "VIEW_MONO_LEFT" },
  2716. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_right", "VIEW_RIGHT" },
  2717. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "VIEWPORT_SIZE" },
  2718. // Node3D, Fragment
  2719. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "BINORMAL" },
  2720. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "camera_direction_world", "CAMERA_DIRECTION_WORLD" },
  2721. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "camera_position_world", "CAMERA_POSITION_WORLD" },
  2722. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "camera_visible_layers", "CAMERA_VISIBLE_LAYERS" },
  2723. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "clip_space_far", "CLIP_SPACE_FAR" },
  2724. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2725. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "exposure", "EXPOSURE" },
  2726. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "eye_offset", "EYE_OFFSET" },
  2727. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2728. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "front_facing", "FRONT_FACING" },
  2729. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection_matrix", "INV_PROJECTION_MATRIX" },
  2730. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_view_matrix", "INV_VIEW_MATRIX" },
  2731. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2732. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "node_position_view", "NODE_POSITION_VIEW" },
  2733. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "node_position_world", "NODE_POSITION_WORLD" },
  2734. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2735. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  2736. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "point_coord", "POINT_COORD" },
  2737. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection_matrix", "PROJECTION_MATRIX" },
  2738. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2739. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "TANGENT" },
  2740. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2741. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2742. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV2" },
  2743. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "vertex", "VERTEX" },
  2744. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "view", "VIEW" },
  2745. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_index", "VIEW_INDEX" },
  2746. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "view_matrix", "VIEW_MATRIX" },
  2747. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_mono_left", "VIEW_MONO_LEFT" },
  2748. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_right", "VIEW_RIGHT" },
  2749. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "VIEWPORT_SIZE" },
  2750. // Node3D, Light
  2751. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "albedo", "ALBEDO" },
  2752. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "attenuation", "ATTENUATION" },
  2753. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "backlight", "BACKLIGHT" },
  2754. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "clip_space_far", "CLIP_SPACE_FAR" },
  2755. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "diffuse", "DIFFUSE_LIGHT" },
  2756. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "exposure", "EXPOSURE" },
  2757. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2758. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection_matrix", "INV_PROJECTION_MATRIX" },
  2759. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_view_matrix", "INV_VIEW_MATRIX" },
  2760. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light", "LIGHT" },
  2761. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light_color", "LIGHT_COLOR" },
  2762. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_BOOLEAN, "light_is_directional", "LIGHT_IS_DIRECTIONAL" },
  2763. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "metallic", "METALLIC" },
  2764. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2765. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2766. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  2767. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection_matrix", "PROJECTION_MATRIX" },
  2768. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  2769. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2770. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "specular", "SPECULAR_LIGHT" },
  2771. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2772. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2773. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV2" },
  2774. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "view", "VIEW" },
  2775. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "view_matrix", "VIEW_MATRIX" },
  2776. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "VIEWPORT_SIZE" },
  2777. // Canvas Item
  2778. // Canvas Item, Vertex
  2779. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_light_pass", "AT_LIGHT_PASS" },
  2780. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "canvas_matrix", "CANVAS_MATRIX" },
  2781. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2782. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom0", "CUSTOM0" },
  2783. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom1", "CUSTOM1" },
  2784. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "instance_custom", "INSTANCE_CUSTOM" },
  2785. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "instance_id", "INSTANCE_ID" },
  2786. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2787. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  2788. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "screen_matrix", "SCREEN_MATRIX" },
  2789. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "texture_pixel_size", "TEXTURE_PIXEL_SIZE" },
  2790. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2791. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2792. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "vertex", "VERTEX" },
  2793. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "vertex_id", "VERTEX_ID" },
  2794. // Canvas Item, Fragment
  2795. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_light_pass", "AT_LIGHT_PASS" },
  2796. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2797. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2798. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "normal_texture", "NORMAL_TEXTURE" },
  2799. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "point_coord", "POINT_COORD" },
  2800. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_pixel_size", "SCREEN_PIXEL_SIZE" },
  2801. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2802. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "specular_shininess", "SPECULAR_SHININESS" },
  2803. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "specular_shininess_texture", "SPECULAR_SHININESS_TEXTURE" },
  2804. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "texture", "TEXTURE" },
  2805. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "texture_pixel_size", "TEXTURE_PIXEL_SIZE" },
  2806. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2807. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2808. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "vertex", "VERTEX" },
  2809. // Canvas Item, Light
  2810. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2811. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2812. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "light", "LIGHT" },
  2813. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "light_color", "LIGHT_COLOR" },
  2814. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light_direction", "LIGHT_DIRECTION" },
  2815. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "light_energy", "LIGHT_ENERGY" },
  2816. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_BOOLEAN, "light_is_directional", "LIGHT_IS_DIRECTIONAL" },
  2817. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light_position", "LIGHT_POSITION" },
  2818. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light_vertex", "LIGHT_VERTEX" },
  2819. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2820. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "point_coord", "POINT_COORD" },
  2821. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2822. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "shadow", "SHADOW_MODULATE" },
  2823. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "specular_shininess", "SPECULAR_SHININESS" },
  2824. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SAMPLER, "texture", "TEXTURE" },
  2825. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "texture_pixel_size", "TEXTURE_PIXEL_SIZE" },
  2826. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2827. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2828. // Particles, Start
  2829. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2830. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2831. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2832. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2833. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2834. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2835. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "index", "INDEX" },
  2836. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2837. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "number", "NUMBER" },
  2838. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "random_seed", "RANDOM_SEED" },
  2839. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2840. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2841. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2842. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2843. // Particles, Start (Custom)
  2844. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2845. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2846. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2847. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2848. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2849. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2850. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "index", "INDEX" },
  2851. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2852. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "number", "NUMBER" },
  2853. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "random_seed", "RANDOM_SEED" },
  2854. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2855. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2856. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2857. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2858. // Particles, Process
  2859. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2860. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2861. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2862. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2863. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2864. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2865. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "index", "INDEX" },
  2866. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2867. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "number", "NUMBER" },
  2868. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "random_seed", "RANDOM_SEED" },
  2869. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2870. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2871. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2872. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2873. // Particles, Process (Custom)
  2874. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2875. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2876. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2877. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2878. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2879. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2880. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "index", "INDEX" },
  2881. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2882. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "number", "NUMBER" },
  2883. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "random_seed", "RANDOM_SEED" },
  2884. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2885. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2886. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2887. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2888. // Particles, Collide
  2889. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2890. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2891. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "collision_depth", "COLLISION_DEPTH" },
  2892. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_3D, "collision_normal", "COLLISION_NORMAL" },
  2893. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2894. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2895. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2896. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2897. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "index", "INDEX" },
  2898. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2899. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "number", "NUMBER" },
  2900. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR_UINT, "random_seed", "RANDOM_SEED" },
  2901. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2902. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2903. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2904. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2905. // Sky, Sky
  2906. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_cubemap_pass", "AT_CUBEMAP_PASS" },
  2907. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_half_res_pass", "AT_HALF_RES_PASS" },
  2908. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_quarter_res_pass", "AT_QUARTER_RES_PASS" },
  2909. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "eyedir", "EYEDIR" },
  2910. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2911. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_4D, "half_res_color", "HALF_RES_COLOR" },
  2912. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light0_color", "LIGHT0_COLOR" },
  2913. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light0_direction", "LIGHT0_DIRECTION" },
  2914. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light0_enabled", "LIGHT0_ENABLED" },
  2915. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light0_energy", "LIGHT0_ENERGY" },
  2916. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light1_color", "LIGHT1_COLOR" },
  2917. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light1_direction", "LIGHT1_DIRECTION" },
  2918. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light1_enabled", "LIGHT1_ENABLED" },
  2919. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light1_energy", "LIGHT1_ENERGY" },
  2920. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light2_color", "LIGHT2_COLOR" },
  2921. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light2_direction", "LIGHT2_DIRECTION" },
  2922. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light2_enabled", "LIGHT2_ENABLED" },
  2923. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light2_energy", "LIGHT2_ENERGY" },
  2924. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light3_color", "LIGHT3_COLOR" },
  2925. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light3_direction", "LIGHT3_DIRECTION" },
  2926. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light3_enabled", "LIGHT3_ENABLED" },
  2927. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light3_energy", "LIGHT3_ENERGY" },
  2928. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "position", "POSITION" },
  2929. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_4D, "quarter_res_color", "QUARTER_RES_COLOR" },
  2930. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SAMPLER, "radiance", "RADIANCE" },
  2931. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2932. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_2D, "sky_coords", "SKY_COORDS" },
  2933. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2934. // Fog, Fog
  2935. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "object_position", "OBJECT_POSITION" },
  2936. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "sdf", "SDF" },
  2937. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "size", "SIZE" },
  2938. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2939. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "uvw", "UVW" },
  2940. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "world_position", "WORLD_POSITION" },
  2941. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  2942. };
  2943. const VisualShaderNodeInput::Port VisualShaderNodeInput::preview_ports[] = {
  2944. // Spatial, Vertex
  2945. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "vec3(1.0, 0.0, 0.0)" },
  2946. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2947. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2948. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "vec3(0.0, 1.0, 0.0)" },
  2949. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2950. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2951. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV" },
  2952. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "vec2(1.0)" },
  2953. // Spatial, Fragment
  2954. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "vec3(1.0, 0.0, 0.0)" },
  2955. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2956. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2957. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2958. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2959. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "vec3(0.0, 1.0, 0.0)" },
  2960. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2961. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2962. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV" },
  2963. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "vec2(1.0)" },
  2964. // Spatial, Light
  2965. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2966. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2967. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2968. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2969. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV" },
  2970. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "vec2(1.0)" },
  2971. // Canvas Item, Vertex
  2972. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2973. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2974. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2975. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "vertex", "VERTEX" },
  2976. // Canvas Item, Fragment
  2977. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2978. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2979. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "UV" },
  2980. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2981. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2982. // Canvas Item, Light
  2983. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2984. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2985. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2986. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "UV" },
  2987. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2988. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2989. // Particles
  2990. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2991. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2992. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2993. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2994. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2995. // Sky
  2996. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "UV" },
  2997. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2998. // Fog
  2999. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  3000. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  3001. };
  3002. int VisualShaderNodeInput::get_input_port_count() const {
  3003. return 0;
  3004. }
  3005. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_port_type(int p_port) const {
  3006. return PORT_TYPE_SCALAR;
  3007. }
  3008. String VisualShaderNodeInput::get_input_port_name(int p_port) const {
  3009. return "";
  3010. }
  3011. int VisualShaderNodeInput::get_output_port_count() const {
  3012. return 1;
  3013. }
  3014. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_output_port_type(int p_port) const {
  3015. return p_port == 0 ? get_input_type_by_name(input_name) : PORT_TYPE_SCALAR;
  3016. }
  3017. String VisualShaderNodeInput::get_output_port_name(int p_port) const {
  3018. return "";
  3019. }
  3020. String VisualShaderNodeInput::get_caption() const {
  3021. return "Input";
  3022. }
  3023. bool VisualShaderNodeInput::is_output_port_expandable(int p_port) const {
  3024. if (p_port == 0) {
  3025. switch (get_input_type_by_name(input_name)) {
  3026. case PORT_TYPE_VECTOR_2D:
  3027. return true;
  3028. case PORT_TYPE_VECTOR_3D:
  3029. return true;
  3030. case PORT_TYPE_VECTOR_4D:
  3031. return true;
  3032. default:
  3033. return false;
  3034. }
  3035. }
  3036. return false;
  3037. }
  3038. String VisualShaderNodeInput::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 {
  3039. if (get_output_port_type(0) == PORT_TYPE_SAMPLER) {
  3040. return "";
  3041. }
  3042. if (p_for_preview) {
  3043. int idx = 0;
  3044. String code;
  3045. while (preview_ports[idx].mode != Shader::MODE_MAX) {
  3046. if (preview_ports[idx].mode == shader_mode && preview_ports[idx].shader_type == shader_type && preview_ports[idx].name == input_name) {
  3047. code = " " + p_output_vars[0] + " = " + preview_ports[idx].string + ";\n";
  3048. break;
  3049. }
  3050. idx++;
  3051. }
  3052. if (code.is_empty()) {
  3053. switch (get_output_port_type(0)) {
  3054. case PORT_TYPE_SCALAR: {
  3055. code = " " + p_output_vars[0] + " = 0.0;\n";
  3056. } break;
  3057. case PORT_TYPE_SCALAR_INT: {
  3058. code = " " + p_output_vars[0] + " = 0;\n";
  3059. } break;
  3060. case PORT_TYPE_VECTOR_2D: {
  3061. code = " " + p_output_vars[0] + " = vec2(0.0);\n";
  3062. } break;
  3063. case PORT_TYPE_VECTOR_3D: {
  3064. code = " " + p_output_vars[0] + " = vec3(0.0);\n";
  3065. } break;
  3066. case PORT_TYPE_VECTOR_4D: {
  3067. code = " " + p_output_vars[0] + " = vec4(0.0);\n";
  3068. } break;
  3069. case PORT_TYPE_BOOLEAN: {
  3070. code = " " + p_output_vars[0] + " = false;\n";
  3071. } break;
  3072. default:
  3073. break;
  3074. }
  3075. }
  3076. return code;
  3077. } else {
  3078. int idx = 0;
  3079. String code;
  3080. while (ports[idx].mode != Shader::MODE_MAX) {
  3081. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == input_name) {
  3082. code = " " + p_output_vars[0] + " = " + ports[idx].string + ";\n";
  3083. break;
  3084. }
  3085. idx++;
  3086. }
  3087. if (code.is_empty()) {
  3088. code = " " + p_output_vars[0] + " = 0.0;\n"; //default (none found) is scalar
  3089. }
  3090. return code;
  3091. }
  3092. }
  3093. void VisualShaderNodeInput::set_input_name(String p_name) {
  3094. PortType prev_type = get_input_type_by_name(input_name);
  3095. input_name = p_name;
  3096. emit_changed();
  3097. if (get_input_type_by_name(input_name) != prev_type) {
  3098. emit_signal(SNAME("input_type_changed"));
  3099. }
  3100. }
  3101. String VisualShaderNodeInput::get_input_name() const {
  3102. return input_name;
  3103. }
  3104. String VisualShaderNodeInput::get_input_real_name() const {
  3105. int idx = 0;
  3106. while (ports[idx].mode != Shader::MODE_MAX) {
  3107. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == input_name) {
  3108. return String(ports[idx].string);
  3109. }
  3110. idx++;
  3111. }
  3112. return "";
  3113. }
  3114. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_type_by_name(String p_name) const {
  3115. int idx = 0;
  3116. while (ports[idx].mode != Shader::MODE_MAX) {
  3117. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == p_name) {
  3118. return ports[idx].type;
  3119. }
  3120. idx++;
  3121. }
  3122. return PORT_TYPE_SCALAR;
  3123. }
  3124. int VisualShaderNodeInput::get_input_index_count() const {
  3125. int idx = 0;
  3126. int count = 0;
  3127. while (ports[idx].mode != Shader::MODE_MAX) {
  3128. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3129. count++;
  3130. }
  3131. idx++;
  3132. }
  3133. return count;
  3134. }
  3135. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_index_type(int p_index) const {
  3136. int idx = 0;
  3137. int count = 0;
  3138. while (ports[idx].mode != Shader::MODE_MAX) {
  3139. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3140. if (count == p_index) {
  3141. return ports[idx].type;
  3142. }
  3143. count++;
  3144. }
  3145. idx++;
  3146. }
  3147. return PORT_TYPE_SCALAR;
  3148. }
  3149. String VisualShaderNodeInput::get_input_index_name(int p_index) const {
  3150. int idx = 0;
  3151. int count = 0;
  3152. while (ports[idx].mode != Shader::MODE_MAX) {
  3153. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3154. if (count == p_index) {
  3155. return ports[idx].name;
  3156. }
  3157. count++;
  3158. }
  3159. idx++;
  3160. }
  3161. return "";
  3162. }
  3163. void VisualShaderNodeInput::_validate_property(PropertyInfo &p_property) const {
  3164. if (p_property.name == "input_name") {
  3165. String port_list;
  3166. int idx = 0;
  3167. while (ports[idx].mode != Shader::MODE_MAX) {
  3168. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3169. if (!port_list.is_empty()) {
  3170. port_list += ",";
  3171. }
  3172. port_list += ports[idx].name;
  3173. }
  3174. idx++;
  3175. }
  3176. if (port_list.is_empty()) {
  3177. port_list = RTR("None");
  3178. }
  3179. p_property.hint_string = port_list;
  3180. }
  3181. }
  3182. Vector<StringName> VisualShaderNodeInput::get_editable_properties() const {
  3183. Vector<StringName> props;
  3184. props.push_back("input_name");
  3185. return props;
  3186. }
  3187. void VisualShaderNodeInput::set_shader_type(VisualShader::Type p_shader_type) {
  3188. shader_type = p_shader_type;
  3189. }
  3190. void VisualShaderNodeInput::set_shader_mode(Shader::Mode p_shader_mode) {
  3191. shader_mode = p_shader_mode;
  3192. }
  3193. void VisualShaderNodeInput::_bind_methods() {
  3194. ClassDB::bind_method(D_METHOD("set_input_name", "name"), &VisualShaderNodeInput::set_input_name);
  3195. ClassDB::bind_method(D_METHOD("get_input_name"), &VisualShaderNodeInput::get_input_name);
  3196. ClassDB::bind_method(D_METHOD("get_input_real_name"), &VisualShaderNodeInput::get_input_real_name);
  3197. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "input_name", PROPERTY_HINT_ENUM, ""), "set_input_name", "get_input_name");
  3198. ADD_SIGNAL(MethodInfo("input_type_changed"));
  3199. }
  3200. VisualShaderNodeInput::VisualShaderNodeInput() {
  3201. }
  3202. ////////////// ParameterRef
  3203. RBMap<RID, List<VisualShaderNodeParameterRef::Parameter>> parameters;
  3204. void VisualShaderNodeParameterRef::add_parameter(RID p_shader_rid, const String &p_name, ParameterType p_type) {
  3205. parameters[p_shader_rid].push_back({ p_name, p_type });
  3206. }
  3207. void VisualShaderNodeParameterRef::clear_parameters(RID p_shader_rid) {
  3208. parameters[p_shader_rid].clear();
  3209. }
  3210. bool VisualShaderNodeParameterRef::has_parameter(RID p_shader_rid, const String &p_name) {
  3211. for (const VisualShaderNodeParameterRef::Parameter &E : parameters[p_shader_rid]) {
  3212. if (E.name == p_name) {
  3213. return true;
  3214. }
  3215. }
  3216. return false;
  3217. }
  3218. String VisualShaderNodeParameterRef::get_caption() const {
  3219. return "ParameterRef";
  3220. }
  3221. int VisualShaderNodeParameterRef::get_input_port_count() const {
  3222. return 0;
  3223. }
  3224. VisualShaderNodeParameterRef::PortType VisualShaderNodeParameterRef::get_input_port_type(int p_port) const {
  3225. return PortType::PORT_TYPE_SCALAR;
  3226. }
  3227. String VisualShaderNodeParameterRef::get_input_port_name(int p_port) const {
  3228. return "";
  3229. }
  3230. int VisualShaderNodeParameterRef::get_output_port_count() const {
  3231. switch (param_type) {
  3232. case PARAMETER_TYPE_FLOAT:
  3233. return 1;
  3234. case PARAMETER_TYPE_INT:
  3235. return 1;
  3236. case PARAMETER_TYPE_UINT:
  3237. return 1;
  3238. case PARAMETER_TYPE_BOOLEAN:
  3239. return 1;
  3240. case PARAMETER_TYPE_VECTOR2:
  3241. return 1;
  3242. case PARAMETER_TYPE_VECTOR3:
  3243. return 1;
  3244. case PARAMETER_TYPE_VECTOR4:
  3245. return 1;
  3246. case PARAMETER_TYPE_TRANSFORM:
  3247. return 1;
  3248. case PARAMETER_TYPE_COLOR:
  3249. return 2;
  3250. case UNIFORM_TYPE_SAMPLER:
  3251. return 1;
  3252. default:
  3253. break;
  3254. }
  3255. return 1;
  3256. }
  3257. VisualShaderNodeParameterRef::PortType VisualShaderNodeParameterRef::get_output_port_type(int p_port) const {
  3258. switch (param_type) {
  3259. case PARAMETER_TYPE_FLOAT:
  3260. return PortType::PORT_TYPE_SCALAR;
  3261. case PARAMETER_TYPE_INT:
  3262. return PortType::PORT_TYPE_SCALAR_INT;
  3263. case PARAMETER_TYPE_UINT:
  3264. return PortType::PORT_TYPE_SCALAR_UINT;
  3265. case PARAMETER_TYPE_BOOLEAN:
  3266. return PortType::PORT_TYPE_BOOLEAN;
  3267. case PARAMETER_TYPE_VECTOR2:
  3268. return PortType::PORT_TYPE_VECTOR_2D;
  3269. case PARAMETER_TYPE_VECTOR3:
  3270. return PortType::PORT_TYPE_VECTOR_3D;
  3271. case PARAMETER_TYPE_VECTOR4:
  3272. return PortType::PORT_TYPE_VECTOR_4D;
  3273. case PARAMETER_TYPE_TRANSFORM:
  3274. return PortType::PORT_TYPE_TRANSFORM;
  3275. case PARAMETER_TYPE_COLOR:
  3276. if (p_port == 0) {
  3277. return PortType::PORT_TYPE_VECTOR_3D;
  3278. } else if (p_port == 1) {
  3279. return PORT_TYPE_SCALAR;
  3280. }
  3281. break;
  3282. case UNIFORM_TYPE_SAMPLER:
  3283. return PortType::PORT_TYPE_SAMPLER;
  3284. default:
  3285. break;
  3286. }
  3287. return PORT_TYPE_SCALAR;
  3288. }
  3289. String VisualShaderNodeParameterRef::get_output_port_name(int p_port) const {
  3290. switch (param_type) {
  3291. case PARAMETER_TYPE_FLOAT:
  3292. return "";
  3293. case PARAMETER_TYPE_INT:
  3294. return "";
  3295. case PARAMETER_TYPE_UINT:
  3296. return "";
  3297. case PARAMETER_TYPE_BOOLEAN:
  3298. return "";
  3299. case PARAMETER_TYPE_VECTOR2:
  3300. return "";
  3301. case PARAMETER_TYPE_VECTOR3:
  3302. return "";
  3303. case PARAMETER_TYPE_VECTOR4:
  3304. return "";
  3305. case PARAMETER_TYPE_TRANSFORM:
  3306. return "";
  3307. case PARAMETER_TYPE_COLOR:
  3308. if (p_port == 0) {
  3309. return "rgb";
  3310. } else if (p_port == 1) {
  3311. return "alpha";
  3312. }
  3313. break;
  3314. case UNIFORM_TYPE_SAMPLER:
  3315. return "";
  3316. break;
  3317. default:
  3318. break;
  3319. }
  3320. return "";
  3321. }
  3322. bool VisualShaderNodeParameterRef::is_shader_valid() const {
  3323. return shader_rid.is_valid();
  3324. }
  3325. void VisualShaderNodeParameterRef::set_shader_rid(const RID &p_shader_rid) {
  3326. shader_rid = p_shader_rid;
  3327. }
  3328. void VisualShaderNodeParameterRef::set_parameter_name(const String &p_name) {
  3329. parameter_name = p_name;
  3330. if (shader_rid.is_valid()) {
  3331. update_parameter_type();
  3332. }
  3333. emit_changed();
  3334. }
  3335. void VisualShaderNodeParameterRef::update_parameter_type() {
  3336. if (parameter_name != "[None]") {
  3337. param_type = get_parameter_type_by_name(parameter_name);
  3338. } else {
  3339. param_type = PARAMETER_TYPE_FLOAT;
  3340. }
  3341. }
  3342. String VisualShaderNodeParameterRef::get_parameter_name() const {
  3343. return parameter_name;
  3344. }
  3345. int VisualShaderNodeParameterRef::get_parameters_count() const {
  3346. ERR_FAIL_COND_V(!shader_rid.is_valid(), 0);
  3347. return parameters[shader_rid].size();
  3348. }
  3349. String VisualShaderNodeParameterRef::get_parameter_name_by_index(int p_idx) const {
  3350. ERR_FAIL_COND_V(!shader_rid.is_valid(), String());
  3351. if (p_idx >= 0 && p_idx < parameters[shader_rid].size()) {
  3352. return parameters[shader_rid].get(p_idx).name;
  3353. }
  3354. return "";
  3355. }
  3356. VisualShaderNodeParameterRef::ParameterType VisualShaderNodeParameterRef::get_parameter_type_by_name(const String &p_name) const {
  3357. ERR_FAIL_COND_V(!shader_rid.is_valid(), PARAMETER_TYPE_FLOAT);
  3358. for (const VisualShaderNodeParameterRef::Parameter &parameter : parameters[shader_rid]) {
  3359. if (parameter.name == p_name) {
  3360. return parameter.type;
  3361. }
  3362. }
  3363. return PARAMETER_TYPE_FLOAT;
  3364. }
  3365. VisualShaderNodeParameterRef::ParameterType VisualShaderNodeParameterRef::get_parameter_type_by_index(int p_idx) const {
  3366. ERR_FAIL_COND_V(!shader_rid.is_valid(), PARAMETER_TYPE_FLOAT);
  3367. if (p_idx >= 0 && p_idx < parameters[shader_rid].size()) {
  3368. return parameters[shader_rid].get(p_idx).type;
  3369. }
  3370. return PARAMETER_TYPE_FLOAT;
  3371. }
  3372. VisualShaderNodeParameterRef::PortType VisualShaderNodeParameterRef::get_port_type_by_index(int p_idx) const {
  3373. ERR_FAIL_COND_V(!shader_rid.is_valid(), PORT_TYPE_SCALAR);
  3374. if (p_idx >= 0 && p_idx < parameters[shader_rid].size()) {
  3375. switch (parameters[shader_rid].get(p_idx).type) {
  3376. case PARAMETER_TYPE_FLOAT:
  3377. return PORT_TYPE_SCALAR;
  3378. case PARAMETER_TYPE_INT:
  3379. return PORT_TYPE_SCALAR_INT;
  3380. case PARAMETER_TYPE_UINT:
  3381. return PORT_TYPE_SCALAR_UINT;
  3382. case UNIFORM_TYPE_SAMPLER:
  3383. return PORT_TYPE_SAMPLER;
  3384. case PARAMETER_TYPE_VECTOR2:
  3385. return PORT_TYPE_VECTOR_2D;
  3386. case PARAMETER_TYPE_VECTOR3:
  3387. return PORT_TYPE_VECTOR_3D;
  3388. case PARAMETER_TYPE_VECTOR4:
  3389. return PORT_TYPE_VECTOR_4D;
  3390. case PARAMETER_TYPE_TRANSFORM:
  3391. return PORT_TYPE_TRANSFORM;
  3392. case PARAMETER_TYPE_COLOR:
  3393. return PORT_TYPE_VECTOR_3D;
  3394. default:
  3395. break;
  3396. }
  3397. }
  3398. return PORT_TYPE_SCALAR;
  3399. }
  3400. String VisualShaderNodeParameterRef::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 {
  3401. switch (param_type) {
  3402. case PARAMETER_TYPE_FLOAT:
  3403. if (parameter_name == "[None]") {
  3404. return " " + p_output_vars[0] + " = 0.0;\n";
  3405. }
  3406. break;
  3407. case PARAMETER_TYPE_COLOR: {
  3408. String code = " " + p_output_vars[0] + " = " + get_parameter_name() + ".rgb;\n";
  3409. code += " " + p_output_vars[1] + " = " + get_parameter_name() + ".a;\n";
  3410. return code;
  3411. } break;
  3412. case UNIFORM_TYPE_SAMPLER:
  3413. return String();
  3414. default:
  3415. break;
  3416. }
  3417. return " " + p_output_vars[0] + " = " + get_parameter_name() + ";\n";
  3418. }
  3419. void VisualShaderNodeParameterRef::_set_parameter_type(int p_type) {
  3420. param_type = (ParameterType)p_type;
  3421. }
  3422. int VisualShaderNodeParameterRef::_get_parameter_type() const {
  3423. return (int)param_type;
  3424. }
  3425. void VisualShaderNodeParameterRef::_bind_methods() {
  3426. ClassDB::bind_method(D_METHOD("set_parameter_name", "name"), &VisualShaderNodeParameterRef::set_parameter_name);
  3427. ClassDB::bind_method(D_METHOD("get_parameter_name"), &VisualShaderNodeParameterRef::get_parameter_name);
  3428. ClassDB::bind_method(D_METHOD("_set_parameter_type", "type"), &VisualShaderNodeParameterRef::_set_parameter_type);
  3429. ClassDB::bind_method(D_METHOD("_get_parameter_type"), &VisualShaderNodeParameterRef::_get_parameter_type);
  3430. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "parameter_name", PROPERTY_HINT_ENUM, ""), "set_parameter_name", "get_parameter_name");
  3431. ADD_PROPERTY(PropertyInfo(Variant::INT, "param_type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_parameter_type", "_get_parameter_type");
  3432. }
  3433. Vector<StringName> VisualShaderNodeParameterRef::get_editable_properties() const {
  3434. Vector<StringName> props;
  3435. props.push_back("parameter_name");
  3436. props.push_back("param_type");
  3437. return props;
  3438. }
  3439. VisualShaderNodeParameterRef::VisualShaderNodeParameterRef() {
  3440. }
  3441. ////////////////////////////////////////////
  3442. const VisualShaderNodeOutput::Port VisualShaderNodeOutput::ports[] = {
  3443. ////////////////////////////////////////////////////////////////////////
  3444. // Spatial.
  3445. ////////////////////////////////////////////////////////////////////////
  3446. // Spatial, Vertex.
  3447. ////////////////////////////////////////////////////////////////////////
  3448. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Vertex", "VERTEX" },
  3449. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal", "NORMAL" },
  3450. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Tangent", "TANGENT" },
  3451. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Binormal", "BINORMAL" },
  3452. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "UV", "UV" },
  3453. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "UV2", "UV2" },
  3454. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Color", "COLOR.rgb" },
  3455. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "COLOR.a" },
  3456. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Roughness", "ROUGHNESS" },
  3457. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Point Size", "POINT_SIZE" },
  3458. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "Model View Matrix", "MODELVIEW_MATRIX" },
  3459. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "Projection Matrix", "PROJECTION_MATRIX" },
  3460. ////////////////////////////////////////////////////////////////////////
  3461. // Spatial, Fragment.
  3462. ////////////////////////////////////////////////////////////////////////
  3463. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Albedo", "ALBEDO" },
  3464. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "ALPHA" },
  3465. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Metallic", "METALLIC" },
  3466. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Roughness", "ROUGHNESS" },
  3467. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Specular", "SPECULAR" },
  3468. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Emission", "EMISSION" },
  3469. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "AO", "AO" },
  3470. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "AO Light Affect", "AO_LIGHT_AFFECT" },
  3471. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal", "NORMAL" },
  3472. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal Map", "NORMAL_MAP" },
  3473. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Normal Map Depth", "NORMAL_MAP_DEPTH" },
  3474. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Rim", "RIM" },
  3475. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Rim Tint", "RIM_TINT" },
  3476. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Clearcoat", "CLEARCOAT" },
  3477. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Clearcoat Roughness", "CLEARCOAT_ROUGHNESS" },
  3478. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Anisotropy", "ANISOTROPY" },
  3479. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Anisotropy Flow", "ANISOTROPY_FLOW" },
  3480. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Subsurf Scatter", "SSS_STRENGTH" },
  3481. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Backlight", "BACKLIGHT" },
  3482. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha Scissor Threshold", "ALPHA_SCISSOR_THRESHOLD" },
  3483. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha Hash Scale", "ALPHA_HASH_SCALE" },
  3484. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha AA Edge", "ALPHA_ANTIALIASING_EDGE" },
  3485. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Alpha UV", "ALPHA_TEXTURE_COORDINATE" },
  3486. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Depth", "DEPTH" },
  3487. ////////////////////////////////////////////////////////////////////////
  3488. // Spatial, Light.
  3489. ////////////////////////////////////////////////////////////////////////
  3490. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Diffuse", "DIFFUSE_LIGHT" },
  3491. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Specular", "SPECULAR_LIGHT" },
  3492. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "ALPHA" },
  3493. ////////////////////////////////////////////////////////////////////////
  3494. // Canvas Item.
  3495. ////////////////////////////////////////////////////////////////////////
  3496. // Canvas Item, Vertex.
  3497. ////////////////////////////////////////////////////////////////////////
  3498. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Vertex", "VERTEX" },
  3499. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "UV", "UV" },
  3500. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Color", "COLOR.rgb" },
  3501. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "COLOR.a" },
  3502. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Point Size", "POINT_SIZE" },
  3503. ////////////////////////////////////////////////////////////////////////
  3504. // Canvas Item, Fragment.
  3505. ////////////////////////////////////////////////////////////////////////
  3506. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Color", "COLOR.rgb" },
  3507. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "COLOR.a" },
  3508. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal", "NORMAL" },
  3509. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal Map", "NORMAL_MAP" },
  3510. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Normal Map Depth", "NORMAL_MAP_DEPTH" },
  3511. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Light Vertex", "LIGHT_VERTEX" },
  3512. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Shadow Vertex", "SHADOW_VERTEX" },
  3513. ////////////////////////////////////////////////////////////////////////
  3514. // Canvas Item, Light.
  3515. ////////////////////////////////////////////////////////////////////////
  3516. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Light", "LIGHT.rgb" },
  3517. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "Light Alpha", "LIGHT.a" },
  3518. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Shadow Modulate", "SHADOW_MODULATE.rgb" },
  3519. ////////////////////////////////////////////////////////////////////////
  3520. // Sky, Sky.
  3521. ////////////////////////////////////////////////////////////////////////
  3522. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Color", "COLOR" },
  3523. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "ALPHA" },
  3524. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Fog", "FOG.rgb" },
  3525. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "Fog Alpha", "FOG.a" },
  3526. ////////////////////////////////////////////////////////////////////////
  3527. // Fog, Fog.
  3528. ////////////////////////////////////////////////////////////////////////
  3529. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "Density", "DENSITY" },
  3530. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Albedo", "ALBEDO" },
  3531. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Emission", "EMISSION" },
  3532. ////////////////////////////////////////////////////////////////////////
  3533. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  3534. };
  3535. int VisualShaderNodeOutput::get_input_port_count() const {
  3536. int idx = 0;
  3537. int count = 0;
  3538. while (ports[idx].mode != Shader::MODE_MAX) {
  3539. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3540. count++;
  3541. }
  3542. idx++;
  3543. }
  3544. return count;
  3545. }
  3546. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_input_port_type(int p_port) const {
  3547. int idx = 0;
  3548. int count = 0;
  3549. while (ports[idx].mode != Shader::MODE_MAX) {
  3550. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3551. if (count == p_port) {
  3552. return ports[idx].type;
  3553. }
  3554. count++;
  3555. }
  3556. idx++;
  3557. }
  3558. return PORT_TYPE_SCALAR;
  3559. }
  3560. String VisualShaderNodeOutput::get_input_port_name(int p_port) const {
  3561. int idx = 0;
  3562. int count = 0;
  3563. while (ports[idx].mode != Shader::MODE_MAX) {
  3564. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3565. if (count == p_port) {
  3566. return String(ports[idx].name);
  3567. }
  3568. count++;
  3569. }
  3570. idx++;
  3571. }
  3572. return String();
  3573. }
  3574. Variant VisualShaderNodeOutput::get_input_port_default_value(int p_port) const {
  3575. return Variant();
  3576. }
  3577. int VisualShaderNodeOutput::get_output_port_count() const {
  3578. return 0;
  3579. }
  3580. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_output_port_type(int p_port) const {
  3581. return PORT_TYPE_SCALAR;
  3582. }
  3583. String VisualShaderNodeOutput::get_output_port_name(int p_port) const {
  3584. return String();
  3585. }
  3586. bool VisualShaderNodeOutput::is_port_separator(int p_index) const {
  3587. if (shader_mode == Shader::MODE_SPATIAL && shader_type == VisualShader::TYPE_VERTEX) {
  3588. String port_name = get_input_port_name(p_index);
  3589. return bool(port_name == "Model View Matrix");
  3590. }
  3591. if (shader_mode == Shader::MODE_SPATIAL && shader_type == VisualShader::TYPE_FRAGMENT) {
  3592. String port_name = get_input_port_name(p_index);
  3593. return bool(port_name == "AO" || port_name == "Normal" || port_name == "Rim" || port_name == "Clearcoat" || port_name == "Anisotropy" || port_name == "Subsurf Scatter" || port_name == "Alpha Scissor Threshold" || port_name == "Depth");
  3594. }
  3595. return false;
  3596. }
  3597. String VisualShaderNodeOutput::get_caption() const {
  3598. return "Output";
  3599. }
  3600. String VisualShaderNodeOutput::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 {
  3601. int idx = 0;
  3602. int count = 0;
  3603. String shader_code;
  3604. while (ports[idx].mode != Shader::MODE_MAX) {
  3605. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3606. if (!p_input_vars[count].is_empty()) {
  3607. String s = ports[idx].string;
  3608. if (s.contains(":")) {
  3609. shader_code += " " + s.get_slicec(':', 0) + " = " + p_input_vars[count] + "." + s.get_slicec(':', 1) + ";\n";
  3610. } else {
  3611. shader_code += " " + s + " = " + p_input_vars[count] + ";\n";
  3612. }
  3613. }
  3614. count++;
  3615. }
  3616. idx++;
  3617. }
  3618. return shader_code;
  3619. }
  3620. VisualShaderNodeOutput::VisualShaderNodeOutput() {
  3621. }
  3622. ///////////////////////////
  3623. void VisualShaderNodeParameter::set_parameter_name(const String &p_name) {
  3624. parameter_name = p_name;
  3625. emit_signal(SNAME("name_changed"));
  3626. emit_changed();
  3627. }
  3628. String VisualShaderNodeParameter::get_parameter_name() const {
  3629. return parameter_name;
  3630. }
  3631. void VisualShaderNodeParameter::set_qualifier(VisualShaderNodeParameter::Qualifier p_qual) {
  3632. ERR_FAIL_INDEX(int(p_qual), int(QUAL_MAX));
  3633. if (qualifier == p_qual) {
  3634. return;
  3635. }
  3636. qualifier = p_qual;
  3637. emit_changed();
  3638. }
  3639. VisualShaderNodeParameter::Qualifier VisualShaderNodeParameter::get_qualifier() const {
  3640. return qualifier;
  3641. }
  3642. void VisualShaderNodeParameter::set_global_code_generated(bool p_enabled) {
  3643. global_code_generated = p_enabled;
  3644. }
  3645. bool VisualShaderNodeParameter::is_global_code_generated() const {
  3646. return global_code_generated;
  3647. }
  3648. #ifndef DISABLE_DEPRECATED
  3649. // Kept for compatibility from 3.x to 4.0.
  3650. bool VisualShaderNodeParameter::_set(const StringName &p_name, const Variant &p_value) {
  3651. if (p_name == "uniform_name") {
  3652. set_parameter_name(p_value);
  3653. return true;
  3654. }
  3655. return false;
  3656. }
  3657. #endif
  3658. void VisualShaderNodeParameter::_bind_methods() {
  3659. ClassDB::bind_method(D_METHOD("set_parameter_name", "name"), &VisualShaderNodeParameter::set_parameter_name);
  3660. ClassDB::bind_method(D_METHOD("get_parameter_name"), &VisualShaderNodeParameter::get_parameter_name);
  3661. ClassDB::bind_method(D_METHOD("set_qualifier", "qualifier"), &VisualShaderNodeParameter::set_qualifier);
  3662. ClassDB::bind_method(D_METHOD("get_qualifier"), &VisualShaderNodeParameter::get_qualifier);
  3663. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "parameter_name"), "set_parameter_name", "get_parameter_name");
  3664. ADD_PROPERTY(PropertyInfo(Variant::INT, "qualifier", PROPERTY_HINT_ENUM, "None,Global,Instance"), "set_qualifier", "get_qualifier");
  3665. BIND_ENUM_CONSTANT(QUAL_NONE);
  3666. BIND_ENUM_CONSTANT(QUAL_GLOBAL);
  3667. BIND_ENUM_CONSTANT(QUAL_INSTANCE);
  3668. BIND_ENUM_CONSTANT(QUAL_MAX);
  3669. }
  3670. String VisualShaderNodeParameter::_get_qual_str() const {
  3671. if (is_qualifier_supported(qualifier)) {
  3672. switch (qualifier) {
  3673. case QUAL_NONE:
  3674. break;
  3675. case QUAL_GLOBAL:
  3676. return "global ";
  3677. case QUAL_INSTANCE:
  3678. return "instance ";
  3679. default:
  3680. break;
  3681. }
  3682. }
  3683. return String();
  3684. }
  3685. String VisualShaderNodeParameter::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  3686. List<String> keyword_list;
  3687. ShaderLanguage::get_keyword_list(&keyword_list);
  3688. if (keyword_list.find(parameter_name)) {
  3689. return RTR("Shader keywords cannot be used as parameter names.\nChoose another name.");
  3690. }
  3691. if (!is_qualifier_supported(qualifier)) {
  3692. String qualifier_str;
  3693. switch (qualifier) {
  3694. case QUAL_NONE:
  3695. break;
  3696. case QUAL_GLOBAL:
  3697. qualifier_str = "global";
  3698. break;
  3699. case QUAL_INSTANCE:
  3700. qualifier_str = "instance";
  3701. break;
  3702. default:
  3703. break;
  3704. }
  3705. return vformat(RTR("This parameter type does not support the '%s' qualifier."), qualifier_str);
  3706. } else if (qualifier == Qualifier::QUAL_GLOBAL) {
  3707. RS::GlobalShaderParameterType gvt = RS::get_singleton()->global_shader_parameter_get_type(parameter_name);
  3708. if (gvt == RS::GLOBAL_VAR_TYPE_MAX) {
  3709. return vformat(RTR("Global parameter '%s' does not exist.\nCreate it in the Project Settings."), parameter_name);
  3710. }
  3711. bool incompatible_type = false;
  3712. switch (gvt) {
  3713. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  3714. if (!Object::cast_to<VisualShaderNodeFloatParameter>(this)) {
  3715. incompatible_type = true;
  3716. }
  3717. } break;
  3718. case RS::GLOBAL_VAR_TYPE_INT: {
  3719. if (!Object::cast_to<VisualShaderNodeIntParameter>(this)) {
  3720. incompatible_type = true;
  3721. }
  3722. } break;
  3723. case RS::GLOBAL_VAR_TYPE_BOOL: {
  3724. if (!Object::cast_to<VisualShaderNodeBooleanParameter>(this)) {
  3725. incompatible_type = true;
  3726. }
  3727. } break;
  3728. case RS::GLOBAL_VAR_TYPE_COLOR: {
  3729. if (!Object::cast_to<VisualShaderNodeColorParameter>(this)) {
  3730. incompatible_type = true;
  3731. }
  3732. } break;
  3733. case RS::GLOBAL_VAR_TYPE_VEC3: {
  3734. if (!Object::cast_to<VisualShaderNodeVec3Parameter>(this)) {
  3735. incompatible_type = true;
  3736. }
  3737. } break;
  3738. case RS::GLOBAL_VAR_TYPE_VEC4: {
  3739. if (!Object::cast_to<VisualShaderNodeVec4Parameter>(this)) {
  3740. incompatible_type = true;
  3741. }
  3742. } break;
  3743. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  3744. if (!Object::cast_to<VisualShaderNodeTransformParameter>(this)) {
  3745. incompatible_type = true;
  3746. }
  3747. } break;
  3748. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  3749. if (!Object::cast_to<VisualShaderNodeTextureParameter>(this)) {
  3750. incompatible_type = true;
  3751. }
  3752. } break;
  3753. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  3754. if (!Object::cast_to<VisualShaderNodeTexture3DParameter>(this)) {
  3755. incompatible_type = true;
  3756. }
  3757. } break;
  3758. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  3759. if (!Object::cast_to<VisualShaderNodeTexture2DArrayParameter>(this)) {
  3760. incompatible_type = true;
  3761. }
  3762. } break;
  3763. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  3764. if (!Object::cast_to<VisualShaderNodeCubemapParameter>(this)) {
  3765. incompatible_type = true;
  3766. }
  3767. } break;
  3768. default:
  3769. break;
  3770. }
  3771. if (incompatible_type) {
  3772. return vformat(RTR("Global parameter '%s' has an incompatible type for this kind of node.\nChange it in the Project Settings."), parameter_name);
  3773. }
  3774. }
  3775. return String();
  3776. }
  3777. Vector<StringName> VisualShaderNodeParameter::get_editable_properties() const {
  3778. Vector<StringName> props;
  3779. props.push_back("qualifier");
  3780. return props;
  3781. }
  3782. VisualShaderNodeParameter::VisualShaderNodeParameter() {
  3783. }
  3784. ////////////// ResizeableBase
  3785. void VisualShaderNodeResizableBase::set_size(const Size2 &p_size) {
  3786. size = p_size;
  3787. }
  3788. Size2 VisualShaderNodeResizableBase::get_size() const {
  3789. return size;
  3790. }
  3791. void VisualShaderNodeResizableBase::set_allow_v_resize(bool p_enabled) {
  3792. allow_v_resize = p_enabled;
  3793. }
  3794. bool VisualShaderNodeResizableBase::is_allow_v_resize() const {
  3795. return allow_v_resize;
  3796. }
  3797. void VisualShaderNodeResizableBase::_bind_methods() {
  3798. ClassDB::bind_method(D_METHOD("set_size", "size"), &VisualShaderNodeResizableBase::set_size);
  3799. ClassDB::bind_method(D_METHOD("get_size"), &VisualShaderNodeResizableBase::get_size);
  3800. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  3801. }
  3802. VisualShaderNodeResizableBase::VisualShaderNodeResizableBase() {
  3803. set_allow_v_resize(true);
  3804. }
  3805. ////////////// Frame
  3806. String VisualShaderNodeFrame::get_caption() const {
  3807. return title;
  3808. }
  3809. int VisualShaderNodeFrame::get_input_port_count() const {
  3810. return 0;
  3811. }
  3812. VisualShaderNodeFrame::PortType VisualShaderNodeFrame::get_input_port_type(int p_port) const {
  3813. return PortType::PORT_TYPE_SCALAR;
  3814. }
  3815. String VisualShaderNodeFrame::get_input_port_name(int p_port) const {
  3816. return String();
  3817. }
  3818. int VisualShaderNodeFrame::get_output_port_count() const {
  3819. return 0;
  3820. }
  3821. VisualShaderNodeFrame::PortType VisualShaderNodeFrame::get_output_port_type(int p_port) const {
  3822. return PortType::PORT_TYPE_SCALAR;
  3823. }
  3824. String VisualShaderNodeFrame::get_output_port_name(int p_port) const {
  3825. return String();
  3826. }
  3827. void VisualShaderNodeFrame::set_title(const String &p_title) {
  3828. title = p_title;
  3829. }
  3830. String VisualShaderNodeFrame::get_title() const {
  3831. return title;
  3832. }
  3833. void VisualShaderNodeFrame::set_tint_color_enabled(bool p_enabled) {
  3834. tint_color_enabled = p_enabled;
  3835. }
  3836. bool VisualShaderNodeFrame::is_tint_color_enabled() const {
  3837. return tint_color_enabled;
  3838. }
  3839. void VisualShaderNodeFrame::set_tint_color(const Color &p_color) {
  3840. tint_color = p_color;
  3841. }
  3842. Color VisualShaderNodeFrame::get_tint_color() const {
  3843. return tint_color;
  3844. }
  3845. void VisualShaderNodeFrame::set_autoshrink_enabled(bool p_enable) {
  3846. autoshrink = p_enable;
  3847. }
  3848. bool VisualShaderNodeFrame::is_autoshrink_enabled() const {
  3849. return autoshrink;
  3850. }
  3851. String VisualShaderNodeFrame::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 {
  3852. return String();
  3853. }
  3854. void VisualShaderNodeFrame::add_attached_node(int p_node) {
  3855. attached_nodes.insert(p_node);
  3856. }
  3857. void VisualShaderNodeFrame::remove_attached_node(int p_node) {
  3858. attached_nodes.erase(p_node);
  3859. }
  3860. void VisualShaderNodeFrame::set_attached_nodes(const PackedInt32Array &p_attached_nodes) {
  3861. attached_nodes.clear();
  3862. for (const int &node_id : p_attached_nodes) {
  3863. attached_nodes.insert(node_id);
  3864. }
  3865. }
  3866. PackedInt32Array VisualShaderNodeFrame::get_attached_nodes() const {
  3867. PackedInt32Array attached_nodes_arr;
  3868. for (const int &node_id : attached_nodes) {
  3869. attached_nodes_arr.push_back(node_id);
  3870. }
  3871. return attached_nodes_arr;
  3872. }
  3873. void VisualShaderNodeFrame::_bind_methods() {
  3874. ClassDB::bind_method(D_METHOD("set_title", "title"), &VisualShaderNodeFrame::set_title);
  3875. ClassDB::bind_method(D_METHOD("get_title"), &VisualShaderNodeFrame::get_title);
  3876. ClassDB::bind_method(D_METHOD("set_tint_color_enabled", "enable"), &VisualShaderNodeFrame::set_tint_color_enabled);
  3877. ClassDB::bind_method(D_METHOD("is_tint_color_enabled"), &VisualShaderNodeFrame::is_tint_color_enabled);
  3878. ClassDB::bind_method(D_METHOD("set_tint_color", "color"), &VisualShaderNodeFrame::set_tint_color);
  3879. ClassDB::bind_method(D_METHOD("get_tint_color"), &VisualShaderNodeFrame::get_tint_color);
  3880. ClassDB::bind_method(D_METHOD("set_autoshrink_enabled", "enable"), &VisualShaderNodeFrame::set_autoshrink_enabled);
  3881. ClassDB::bind_method(D_METHOD("is_autoshrink_enabled"), &VisualShaderNodeFrame::is_autoshrink_enabled);
  3882. ClassDB::bind_method(D_METHOD("add_attached_node", "node"), &VisualShaderNodeFrame::add_attached_node);
  3883. ClassDB::bind_method(D_METHOD("remove_attached_node", "node"), &VisualShaderNodeFrame::remove_attached_node);
  3884. ClassDB::bind_method(D_METHOD("set_attached_nodes", "attached_nodes"), &VisualShaderNodeFrame::set_attached_nodes);
  3885. ClassDB::bind_method(D_METHOD("get_attached_nodes"), &VisualShaderNodeFrame::get_attached_nodes);
  3886. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  3887. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tint_color_enabled"), "set_tint_color_enabled", "is_tint_color_enabled");
  3888. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "tint_color"), "set_tint_color", "get_tint_color");
  3889. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autoshrink"), "set_autoshrink_enabled", "is_autoshrink_enabled");
  3890. ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "attached_nodes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_attached_nodes", "get_attached_nodes");
  3891. }
  3892. VisualShaderNodeFrame::VisualShaderNodeFrame() {
  3893. }
  3894. ////////////// Comment (Deprecated)
  3895. #ifndef DISABLE_DEPRECATED
  3896. void VisualShaderNodeComment::_bind_methods() {
  3897. ClassDB::bind_method(D_METHOD("set_description", "description"), &VisualShaderNodeComment::set_description);
  3898. ClassDB::bind_method(D_METHOD("get_description"), &VisualShaderNodeComment::get_description);
  3899. ADD_PROPERTY(PropertyInfo(Variant::STRING, "description"), "set_description", "get_description");
  3900. }
  3901. void VisualShaderNodeComment::set_description(const String &p_description) {
  3902. description = p_description;
  3903. }
  3904. String VisualShaderNodeComment::get_description() const {
  3905. return description;
  3906. }
  3907. #endif
  3908. ////////////// GroupBase
  3909. void VisualShaderNodeGroupBase::set_inputs(const String &p_inputs) {
  3910. if (inputs == p_inputs) {
  3911. return;
  3912. }
  3913. clear_input_ports();
  3914. inputs = p_inputs;
  3915. Vector<String> input_strings = inputs.split(";", false);
  3916. int input_port_count = input_strings.size();
  3917. for (int i = 0; i < input_port_count; i++) {
  3918. Vector<String> arr = input_strings[i].split(",");
  3919. ERR_FAIL_COND(arr.size() != 3);
  3920. int port_idx = arr[0].to_int();
  3921. int port_type = arr[1].to_int();
  3922. String port_name = arr[2];
  3923. Port port;
  3924. port.type = (PortType)port_type;
  3925. port.name = port_name;
  3926. input_ports[port_idx] = port;
  3927. }
  3928. }
  3929. String VisualShaderNodeGroupBase::get_inputs() const {
  3930. return inputs;
  3931. }
  3932. void VisualShaderNodeGroupBase::set_outputs(const String &p_outputs) {
  3933. if (outputs == p_outputs) {
  3934. return;
  3935. }
  3936. clear_output_ports();
  3937. outputs = p_outputs;
  3938. Vector<String> output_strings = outputs.split(";", false);
  3939. int output_port_count = output_strings.size();
  3940. for (int i = 0; i < output_port_count; i++) {
  3941. Vector<String> arr = output_strings[i].split(",");
  3942. ERR_FAIL_COND(arr.size() != 3);
  3943. int port_idx = arr[0].to_int();
  3944. int port_type = arr[1].to_int();
  3945. String port_name = arr[2];
  3946. Port port;
  3947. port.type = (PortType)port_type;
  3948. port.name = port_name;
  3949. output_ports[port_idx] = port;
  3950. }
  3951. }
  3952. String VisualShaderNodeGroupBase::get_outputs() const {
  3953. return outputs;
  3954. }
  3955. bool VisualShaderNodeGroupBase::is_valid_port_name(const String &p_name) const {
  3956. if (!p_name.is_valid_ascii_identifier()) {
  3957. return false;
  3958. }
  3959. for (int i = 0; i < get_input_port_count(); i++) {
  3960. if (get_input_port_name(i) == p_name) {
  3961. return false;
  3962. }
  3963. }
  3964. for (int i = 0; i < get_output_port_count(); i++) {
  3965. if (get_output_port_name(i) == p_name) {
  3966. return false;
  3967. }
  3968. }
  3969. return true;
  3970. }
  3971. void VisualShaderNodeGroupBase::add_input_port(int p_id, int p_type, const String &p_name) {
  3972. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  3973. ERR_FAIL_COND(!is_valid_port_name(p_name));
  3974. String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
  3975. Vector<String> inputs_strings = inputs.split(";", false);
  3976. int index = 0;
  3977. if (p_id < inputs_strings.size()) {
  3978. for (int i = 0; i < inputs_strings.size(); i++) {
  3979. if (i == p_id) {
  3980. inputs = inputs.insert(index, str);
  3981. break;
  3982. }
  3983. index += inputs_strings[i].size();
  3984. }
  3985. } else {
  3986. inputs += str;
  3987. }
  3988. inputs_strings = inputs.split(";", false);
  3989. index = 0;
  3990. for (int i = 0; i < inputs_strings.size(); i++) {
  3991. int count = 0;
  3992. for (int j = 0; j < inputs_strings[i].size(); j++) {
  3993. if (inputs_strings[i][j] == ',') {
  3994. break;
  3995. }
  3996. count++;
  3997. }
  3998. inputs = inputs.left(index) + inputs.substr(index + count);
  3999. inputs = inputs.insert(index, itos(i));
  4000. index += inputs_strings[i].size();
  4001. }
  4002. _apply_port_changes();
  4003. emit_changed();
  4004. }
  4005. void VisualShaderNodeGroupBase::remove_input_port(int p_id) {
  4006. ERR_FAIL_COND(!has_input_port(p_id));
  4007. Vector<String> inputs_strings = inputs.split(";", false);
  4008. int count = 0;
  4009. int index = 0;
  4010. for (int i = 0; i < inputs_strings.size(); i++) {
  4011. Vector<String> arr = inputs_strings[i].split(",");
  4012. if (arr[0].to_int() == p_id) {
  4013. count = inputs_strings[i].size();
  4014. break;
  4015. }
  4016. index += inputs_strings[i].size();
  4017. }
  4018. inputs = inputs.left(index) + inputs.substr(index + count);
  4019. inputs_strings = inputs.split(";", false);
  4020. inputs = inputs.substr(0, index);
  4021. for (int i = p_id; i < inputs_strings.size(); i++) {
  4022. inputs += inputs_strings[i].replace_first(inputs_strings[i].split(",")[0], itos(i)) + ";";
  4023. }
  4024. _apply_port_changes();
  4025. emit_changed();
  4026. }
  4027. int VisualShaderNodeGroupBase::get_input_port_count() const {
  4028. return input_ports.size();
  4029. }
  4030. bool VisualShaderNodeGroupBase::has_input_port(int p_id) const {
  4031. return input_ports.has(p_id);
  4032. }
  4033. void VisualShaderNodeGroupBase::add_output_port(int p_id, int p_type, const String &p_name) {
  4034. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  4035. ERR_FAIL_COND(!is_valid_port_name(p_name));
  4036. String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
  4037. Vector<String> outputs_strings = outputs.split(";", false);
  4038. int index = 0;
  4039. if (p_id < outputs_strings.size()) {
  4040. for (int i = 0; i < outputs_strings.size(); i++) {
  4041. if (i == p_id) {
  4042. outputs = outputs.insert(index, str);
  4043. break;
  4044. }
  4045. index += outputs_strings[i].size();
  4046. }
  4047. } else {
  4048. outputs += str;
  4049. }
  4050. outputs_strings = outputs.split(";", false);
  4051. index = 0;
  4052. for (int i = 0; i < outputs_strings.size(); i++) {
  4053. int count = 0;
  4054. for (int j = 0; j < outputs_strings[i].size(); j++) {
  4055. if (outputs_strings[i][j] == ',') {
  4056. break;
  4057. }
  4058. count++;
  4059. }
  4060. outputs = outputs.left(index) + outputs.substr(index + count);
  4061. outputs = outputs.insert(index, itos(i));
  4062. index += outputs_strings[i].size();
  4063. }
  4064. _apply_port_changes();
  4065. emit_changed();
  4066. }
  4067. void VisualShaderNodeGroupBase::remove_output_port(int p_id) {
  4068. ERR_FAIL_COND(!has_output_port(p_id));
  4069. Vector<String> outputs_strings = outputs.split(";", false);
  4070. int count = 0;
  4071. int index = 0;
  4072. for (int i = 0; i < outputs_strings.size(); i++) {
  4073. Vector<String> arr = outputs_strings[i].split(",");
  4074. if (arr[0].to_int() == p_id) {
  4075. count = outputs_strings[i].size();
  4076. break;
  4077. }
  4078. index += outputs_strings[i].size();
  4079. }
  4080. outputs = outputs.left(index) + outputs.substr(index + count);
  4081. outputs_strings = outputs.split(";", false);
  4082. outputs = outputs.substr(0, index);
  4083. for (int i = p_id; i < outputs_strings.size(); i++) {
  4084. outputs += outputs_strings[i].replace_first(outputs_strings[i].split(",")[0], itos(i)) + ";";
  4085. }
  4086. _apply_port_changes();
  4087. emit_changed();
  4088. }
  4089. int VisualShaderNodeGroupBase::get_output_port_count() const {
  4090. return output_ports.size();
  4091. }
  4092. bool VisualShaderNodeGroupBase::has_output_port(int p_id) const {
  4093. return output_ports.has(p_id);
  4094. }
  4095. void VisualShaderNodeGroupBase::clear_input_ports() {
  4096. input_ports.clear();
  4097. }
  4098. void VisualShaderNodeGroupBase::clear_output_ports() {
  4099. output_ports.clear();
  4100. }
  4101. void VisualShaderNodeGroupBase::set_input_port_type(int p_id, int p_type) {
  4102. ERR_FAIL_COND(!has_input_port(p_id));
  4103. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  4104. if (input_ports[p_id].type == p_type) {
  4105. return;
  4106. }
  4107. Vector<String> inputs_strings = inputs.split(";", false);
  4108. int count = 0;
  4109. int index = 0;
  4110. for (int i = 0; i < inputs_strings.size(); i++) {
  4111. Vector<String> arr = inputs_strings[i].split(",");
  4112. ERR_FAIL_COND(arr.size() != 3);
  4113. if (arr[0].to_int() == p_id) {
  4114. index += arr[0].size();
  4115. count = arr[1].size() - 1;
  4116. break;
  4117. }
  4118. index += inputs_strings[i].size();
  4119. }
  4120. inputs = inputs.left(index) + inputs.substr(index + count);
  4121. inputs = inputs.insert(index, itos(p_type));
  4122. _apply_port_changes();
  4123. emit_changed();
  4124. }
  4125. VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_input_port_type(int p_id) const {
  4126. ERR_FAIL_COND_V(!input_ports.has(p_id), (PortType)0);
  4127. return input_ports[p_id].type;
  4128. }
  4129. void VisualShaderNodeGroupBase::set_input_port_name(int p_id, const String &p_name) {
  4130. ERR_FAIL_COND(!has_input_port(p_id));
  4131. ERR_FAIL_COND(!is_valid_port_name(p_name));
  4132. if (input_ports[p_id].name == p_name) {
  4133. return;
  4134. }
  4135. Vector<String> inputs_strings = inputs.split(";", false);
  4136. int count = 0;
  4137. int index = 0;
  4138. for (int i = 0; i < inputs_strings.size(); i++) {
  4139. Vector<String> arr = inputs_strings[i].split(",");
  4140. ERR_FAIL_COND(arr.size() != 3);
  4141. if (arr[0].to_int() == p_id) {
  4142. index += arr[0].size() + arr[1].size();
  4143. count = arr[2].size() - 1;
  4144. break;
  4145. }
  4146. index += inputs_strings[i].size();
  4147. }
  4148. inputs = inputs.left(index) + inputs.substr(index + count);
  4149. inputs = inputs.insert(index, p_name);
  4150. _apply_port_changes();
  4151. emit_changed();
  4152. }
  4153. String VisualShaderNodeGroupBase::get_input_port_name(int p_id) const {
  4154. ERR_FAIL_COND_V(!input_ports.has(p_id), "");
  4155. return input_ports[p_id].name;
  4156. }
  4157. void VisualShaderNodeGroupBase::set_output_port_type(int p_id, int p_type) {
  4158. ERR_FAIL_COND(!has_output_port(p_id));
  4159. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  4160. if (output_ports[p_id].type == p_type) {
  4161. return;
  4162. }
  4163. Vector<String> output_strings = outputs.split(";", false);
  4164. int count = 0;
  4165. int index = 0;
  4166. for (int i = 0; i < output_strings.size(); i++) {
  4167. Vector<String> arr = output_strings[i].split(",");
  4168. ERR_FAIL_COND(arr.size() != 3);
  4169. if (arr[0].to_int() == p_id) {
  4170. index += arr[0].size();
  4171. count = arr[1].size() - 1;
  4172. break;
  4173. }
  4174. index += output_strings[i].size();
  4175. }
  4176. outputs = outputs.left(index) + outputs.substr(index + count);
  4177. outputs = outputs.insert(index, itos(p_type));
  4178. _apply_port_changes();
  4179. emit_changed();
  4180. }
  4181. VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_output_port_type(int p_id) const {
  4182. ERR_FAIL_COND_V(!output_ports.has(p_id), (PortType)0);
  4183. return output_ports[p_id].type;
  4184. }
  4185. void VisualShaderNodeGroupBase::set_output_port_name(int p_id, const String &p_name) {
  4186. ERR_FAIL_COND(!has_output_port(p_id));
  4187. ERR_FAIL_COND(!is_valid_port_name(p_name));
  4188. if (output_ports[p_id].name == p_name) {
  4189. return;
  4190. }
  4191. Vector<String> output_strings = outputs.split(";", false);
  4192. int count = 0;
  4193. int index = 0;
  4194. for (int i = 0; i < output_strings.size(); i++) {
  4195. Vector<String> arr = output_strings[i].split(",");
  4196. ERR_FAIL_COND(arr.size() != 3);
  4197. if (arr[0].to_int() == p_id) {
  4198. index += arr[0].size() + arr[1].size();
  4199. count = arr[2].size() - 1;
  4200. break;
  4201. }
  4202. index += output_strings[i].size();
  4203. }
  4204. outputs = outputs.left(index) + outputs.substr(index + count);
  4205. outputs = outputs.insert(index, p_name);
  4206. _apply_port_changes();
  4207. emit_changed();
  4208. }
  4209. String VisualShaderNodeGroupBase::get_output_port_name(int p_id) const {
  4210. ERR_FAIL_COND_V(!output_ports.has(p_id), "");
  4211. return output_ports[p_id].name;
  4212. }
  4213. int VisualShaderNodeGroupBase::get_free_input_port_id() const {
  4214. return input_ports.size();
  4215. }
  4216. int VisualShaderNodeGroupBase::get_free_output_port_id() const {
  4217. return output_ports.size();
  4218. }
  4219. void VisualShaderNodeGroupBase::set_ctrl_pressed(Control *p_control, int p_index) {
  4220. controls[p_index] = p_control;
  4221. }
  4222. Control *VisualShaderNodeGroupBase::is_ctrl_pressed(int p_index) {
  4223. ERR_FAIL_COND_V(!controls.has(p_index), nullptr);
  4224. return controls[p_index];
  4225. }
  4226. void VisualShaderNodeGroupBase::_apply_port_changes() {
  4227. Vector<String> inputs_strings = inputs.split(";", false);
  4228. Vector<String> outputs_strings = outputs.split(";", false);
  4229. clear_input_ports();
  4230. clear_output_ports();
  4231. for (int i = 0; i < inputs_strings.size(); i++) {
  4232. Vector<String> arr = inputs_strings[i].split(",");
  4233. ERR_FAIL_COND(arr.size() != 3);
  4234. Port port;
  4235. port.type = (PortType)arr[1].to_int();
  4236. port.name = arr[2];
  4237. input_ports[i] = port;
  4238. }
  4239. for (int i = 0; i < outputs_strings.size(); i++) {
  4240. Vector<String> arr = outputs_strings[i].split(",");
  4241. ERR_FAIL_COND(arr.size() != 3);
  4242. Port port;
  4243. port.type = (PortType)arr[1].to_int();
  4244. port.name = arr[2];
  4245. output_ports[i] = port;
  4246. }
  4247. }
  4248. void VisualShaderNodeGroupBase::set_editable(bool p_enabled) {
  4249. editable = p_enabled;
  4250. }
  4251. bool VisualShaderNodeGroupBase::is_editable() const {
  4252. return editable;
  4253. }
  4254. void VisualShaderNodeGroupBase::_bind_methods() {
  4255. ClassDB::bind_method(D_METHOD("set_inputs", "inputs"), &VisualShaderNodeGroupBase::set_inputs);
  4256. ClassDB::bind_method(D_METHOD("get_inputs"), &VisualShaderNodeGroupBase::get_inputs);
  4257. ClassDB::bind_method(D_METHOD("set_outputs", "outputs"), &VisualShaderNodeGroupBase::set_outputs);
  4258. ClassDB::bind_method(D_METHOD("get_outputs"), &VisualShaderNodeGroupBase::get_outputs);
  4259. ClassDB::bind_method(D_METHOD("is_valid_port_name", "name"), &VisualShaderNodeGroupBase::is_valid_port_name);
  4260. ClassDB::bind_method(D_METHOD("add_input_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_input_port);
  4261. ClassDB::bind_method(D_METHOD("remove_input_port", "id"), &VisualShaderNodeGroupBase::remove_input_port);
  4262. ClassDB::bind_method(D_METHOD("get_input_port_count"), &VisualShaderNodeGroupBase::get_input_port_count);
  4263. ClassDB::bind_method(D_METHOD("has_input_port", "id"), &VisualShaderNodeGroupBase::has_input_port);
  4264. ClassDB::bind_method(D_METHOD("clear_input_ports"), &VisualShaderNodeGroupBase::clear_input_ports);
  4265. ClassDB::bind_method(D_METHOD("add_output_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_output_port);
  4266. ClassDB::bind_method(D_METHOD("remove_output_port", "id"), &VisualShaderNodeGroupBase::remove_output_port);
  4267. ClassDB::bind_method(D_METHOD("get_output_port_count"), &VisualShaderNodeGroupBase::get_output_port_count);
  4268. ClassDB::bind_method(D_METHOD("has_output_port", "id"), &VisualShaderNodeGroupBase::has_output_port);
  4269. ClassDB::bind_method(D_METHOD("clear_output_ports"), &VisualShaderNodeGroupBase::clear_output_ports);
  4270. ClassDB::bind_method(D_METHOD("set_input_port_name", "id", "name"), &VisualShaderNodeGroupBase::set_input_port_name);
  4271. ClassDB::bind_method(D_METHOD("set_input_port_type", "id", "type"), &VisualShaderNodeGroupBase::set_input_port_type);
  4272. ClassDB::bind_method(D_METHOD("set_output_port_name", "id", "name"), &VisualShaderNodeGroupBase::set_output_port_name);
  4273. ClassDB::bind_method(D_METHOD("set_output_port_type", "id", "type"), &VisualShaderNodeGroupBase::set_output_port_type);
  4274. ClassDB::bind_method(D_METHOD("get_free_input_port_id"), &VisualShaderNodeGroupBase::get_free_input_port_id);
  4275. ClassDB::bind_method(D_METHOD("get_free_output_port_id"), &VisualShaderNodeGroupBase::get_free_output_port_id);
  4276. }
  4277. String VisualShaderNodeGroupBase::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 {
  4278. return "";
  4279. }
  4280. VisualShaderNodeGroupBase::VisualShaderNodeGroupBase() {
  4281. simple_decl = false;
  4282. }
  4283. ////////////// Expression
  4284. String VisualShaderNodeExpression::get_caption() const {
  4285. return "Expression";
  4286. }
  4287. void VisualShaderNodeExpression::set_expression(const String &p_expression) {
  4288. expression = p_expression;
  4289. emit_changed();
  4290. }
  4291. String VisualShaderNodeExpression::get_expression() const {
  4292. return expression;
  4293. }
  4294. bool VisualShaderNodeExpression::_is_valid_identifier_char(char32_t p_c) const {
  4295. return p_c == '_' || (p_c >= 'A' && p_c <= 'Z') || (p_c >= 'a' && p_c <= 'z') || (p_c >= '0' && p_c <= '9');
  4296. }
  4297. String VisualShaderNodeExpression::_replace_port_names(const Vector<Pair<String, String>> &p_pairs, const String &p_expression) const {
  4298. String _expression = p_expression;
  4299. for (const Pair<String, String> &pair : p_pairs) {
  4300. String from = pair.first;
  4301. String to = pair.second;
  4302. int search_idx = 0;
  4303. int len = from.length();
  4304. while (true) {
  4305. int index = _expression.find(from, search_idx);
  4306. if (index == -1) {
  4307. break;
  4308. }
  4309. int left_index = index - 1;
  4310. int right_index = index + len;
  4311. bool left_correct = left_index <= 0 || !_is_valid_identifier_char(_expression[left_index]);
  4312. bool right_correct = right_index >= _expression.length() || !_is_valid_identifier_char(_expression[right_index]);
  4313. if (left_correct && right_correct) {
  4314. _expression = _expression.erase(index, len);
  4315. _expression = _expression.insert(index, to);
  4316. search_idx = index + to.length();
  4317. } else {
  4318. search_idx = index + len;
  4319. }
  4320. }
  4321. }
  4322. return _expression;
  4323. }
  4324. String VisualShaderNodeExpression::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 {
  4325. String _expression = expression;
  4326. _expression = _expression.insert(0, "\n");
  4327. _expression = _expression.replace("\n", "\n ");
  4328. Vector<Pair<String, String>> input_port_names;
  4329. for (int i = 0; i < get_input_port_count(); i++) {
  4330. input_port_names.push_back(Pair<String, String>(get_input_port_name(i), p_input_vars[i]));
  4331. }
  4332. _expression = _replace_port_names(input_port_names, _expression);
  4333. Vector<Pair<String, String>> output_port_names;
  4334. for (int i = 0; i < get_output_port_count(); i++) {
  4335. output_port_names.push_back(Pair<String, String>(get_output_port_name(i), p_output_vars[i]));
  4336. }
  4337. _expression = _replace_port_names(output_port_names, _expression);
  4338. String output_initializer;
  4339. for (int i = 0; i < get_output_port_count(); i++) {
  4340. int port_type = get_output_port_type(i);
  4341. String tk = "";
  4342. switch (port_type) {
  4343. case PORT_TYPE_SCALAR:
  4344. tk = "0.0";
  4345. break;
  4346. case PORT_TYPE_SCALAR_INT:
  4347. tk = "0";
  4348. break;
  4349. case PORT_TYPE_VECTOR_2D:
  4350. tk = "vec2(0.0, 0.0)";
  4351. break;
  4352. case PORT_TYPE_VECTOR_3D:
  4353. tk = "vec3(0.0, 0.0, 0.0)";
  4354. break;
  4355. case PORT_TYPE_VECTOR_4D:
  4356. tk = "vec4(0.0, 0.0, 0.0, 0.0)";
  4357. break;
  4358. case PORT_TYPE_BOOLEAN:
  4359. tk = "false";
  4360. break;
  4361. case PORT_TYPE_TRANSFORM:
  4362. tk = "mat4(1.0)";
  4363. break;
  4364. default:
  4365. continue;
  4366. }
  4367. output_initializer += " " + p_output_vars[i] + " = " + tk + ";\n";
  4368. }
  4369. String code;
  4370. code += output_initializer;
  4371. code += " {";
  4372. code += _expression;
  4373. code += "\n }\n";
  4374. return code;
  4375. }
  4376. bool VisualShaderNodeExpression::is_output_port_expandable(int p_port) const {
  4377. return false;
  4378. }
  4379. void VisualShaderNodeExpression::_bind_methods() {
  4380. ClassDB::bind_method(D_METHOD("set_expression", "expression"), &VisualShaderNodeExpression::set_expression);
  4381. ClassDB::bind_method(D_METHOD("get_expression"), &VisualShaderNodeExpression::get_expression);
  4382. ADD_PROPERTY(PropertyInfo(Variant::STRING, "expression"), "set_expression", "get_expression");
  4383. }
  4384. VisualShaderNodeExpression::VisualShaderNodeExpression() {
  4385. set_editable(true);
  4386. }
  4387. ////////////// Global Expression
  4388. String VisualShaderNodeGlobalExpression::get_caption() const {
  4389. return "GlobalExpression";
  4390. }
  4391. String VisualShaderNodeGlobalExpression::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4392. return expression;
  4393. }
  4394. VisualShaderNodeGlobalExpression::VisualShaderNodeGlobalExpression() {
  4395. set_editable(false);
  4396. }
  4397. ////////////// Varying
  4398. RBMap<RID, List<VisualShaderNodeVarying::Varying>> varyings;
  4399. void VisualShaderNodeVarying::add_varying(RID p_shader_rid, const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type) { // static
  4400. varyings[p_shader_rid].push_back({ p_name, p_mode, p_type });
  4401. }
  4402. void VisualShaderNodeVarying::clear_varyings(RID p_shader_rid) { // static
  4403. varyings[p_shader_rid].clear();
  4404. }
  4405. bool VisualShaderNodeVarying::has_varying(RID p_shader_rid, const String &p_name) { // static
  4406. for (const VisualShaderNodeVarying::Varying &E : varyings[p_shader_rid]) {
  4407. if (E.name == p_name) {
  4408. return true;
  4409. }
  4410. }
  4411. return false;
  4412. }
  4413. void VisualShaderNodeVarying::set_shader_rid(const RID &p_shader_rid) {
  4414. shader_rid = p_shader_rid;
  4415. }
  4416. int VisualShaderNodeVarying::get_varyings_count() const {
  4417. return varyings[shader_rid].size();
  4418. }
  4419. String VisualShaderNodeVarying::get_varying_name_by_index(int p_idx) const {
  4420. if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) {
  4421. return varyings[shader_rid].get(p_idx).name;
  4422. }
  4423. return "";
  4424. }
  4425. VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type_by_name(const String &p_name) const {
  4426. for (const VisualShaderNodeVarying::Varying &varying : varyings[shader_rid]) {
  4427. if (varying.name == p_name) {
  4428. return varying.type;
  4429. }
  4430. }
  4431. return VisualShader::VARYING_TYPE_FLOAT;
  4432. }
  4433. VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type_by_index(int p_idx) const {
  4434. if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) {
  4435. return varyings[shader_rid].get(p_idx).type;
  4436. }
  4437. return VisualShader::VARYING_TYPE_FLOAT;
  4438. }
  4439. VisualShader::VaryingMode VisualShaderNodeVarying::get_varying_mode_by_name(const String &p_name) const {
  4440. for (const VisualShaderNodeVarying::Varying &varying : varyings[shader_rid]) {
  4441. if (varying.name == p_name) {
  4442. return varying.mode;
  4443. }
  4444. }
  4445. return VisualShader::VARYING_MODE_VERTEX_TO_FRAG_LIGHT;
  4446. }
  4447. VisualShader::VaryingMode VisualShaderNodeVarying::get_varying_mode_by_index(int p_idx) const {
  4448. if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) {
  4449. return varyings[shader_rid].get(p_idx).mode;
  4450. }
  4451. return VisualShader::VARYING_MODE_VERTEX_TO_FRAG_LIGHT;
  4452. }
  4453. VisualShaderNodeVarying::PortType VisualShaderNodeVarying::get_port_type_by_index(int p_idx) const {
  4454. if (p_idx >= 0 && p_idx < varyings[shader_rid].size()) {
  4455. return get_port_type(varyings[shader_rid].get(p_idx).type, 0);
  4456. }
  4457. return PORT_TYPE_SCALAR;
  4458. }
  4459. //////////////
  4460. void VisualShaderNodeVarying::_bind_methods() {
  4461. ClassDB::bind_method(D_METHOD("set_varying_name", "name"), &VisualShaderNodeVarying::set_varying_name);
  4462. ClassDB::bind_method(D_METHOD("get_varying_name"), &VisualShaderNodeVarying::get_varying_name);
  4463. ClassDB::bind_method(D_METHOD("set_varying_type", "type"), &VisualShaderNodeVarying::set_varying_type);
  4464. ClassDB::bind_method(D_METHOD("get_varying_type"), &VisualShaderNodeVarying::get_varying_type);
  4465. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "varying_name"), "set_varying_name", "get_varying_name");
  4466. ADD_PROPERTY(PropertyInfo(Variant::INT, "varying_type", PROPERTY_HINT_ENUM, "Float,Int,Vector2,Vector3,Vector4,Boolean,Transform"), "set_varying_type", "get_varying_type");
  4467. }
  4468. String VisualShaderNodeVarying::get_type_str() const {
  4469. switch (varying_type) {
  4470. case VisualShader::VARYING_TYPE_FLOAT:
  4471. return "float";
  4472. case VisualShader::VARYING_TYPE_INT:
  4473. return "int";
  4474. case VisualShader::VARYING_TYPE_UINT:
  4475. return "uint";
  4476. case VisualShader::VARYING_TYPE_VECTOR_2D:
  4477. return "vec2";
  4478. case VisualShader::VARYING_TYPE_VECTOR_3D:
  4479. return "vec3";
  4480. case VisualShader::VARYING_TYPE_VECTOR_4D:
  4481. return "vec4";
  4482. case VisualShader::VARYING_TYPE_BOOLEAN:
  4483. return "bool";
  4484. case VisualShader::VARYING_TYPE_TRANSFORM:
  4485. return "mat4";
  4486. default:
  4487. break;
  4488. }
  4489. return "";
  4490. }
  4491. VisualShaderNodeVarying::PortType VisualShaderNodeVarying::get_port_type(VisualShader::VaryingType p_type, int p_port) const {
  4492. switch (p_type) {
  4493. case VisualShader::VARYING_TYPE_INT:
  4494. return PORT_TYPE_SCALAR_INT;
  4495. case VisualShader::VARYING_TYPE_UINT:
  4496. return PORT_TYPE_SCALAR_UINT;
  4497. case VisualShader::VARYING_TYPE_VECTOR_2D:
  4498. return p_port == 0 ? PORT_TYPE_VECTOR_2D : PORT_TYPE_SCALAR;
  4499. case VisualShader::VARYING_TYPE_VECTOR_3D:
  4500. return p_port == 0 ? PORT_TYPE_VECTOR_3D : PORT_TYPE_SCALAR;
  4501. case VisualShader::VARYING_TYPE_VECTOR_4D:
  4502. return p_port == 0 ? PORT_TYPE_VECTOR_4D : PORT_TYPE_SCALAR;
  4503. case VisualShader::VARYING_TYPE_BOOLEAN:
  4504. return PORT_TYPE_BOOLEAN;
  4505. case VisualShader::VARYING_TYPE_TRANSFORM:
  4506. return PORT_TYPE_TRANSFORM;
  4507. default:
  4508. break;
  4509. }
  4510. return PORT_TYPE_SCALAR;
  4511. }
  4512. void VisualShaderNodeVarying::set_varying_name(String p_varying_name) {
  4513. if (varying_name == p_varying_name) {
  4514. return;
  4515. }
  4516. varying_name = p_varying_name;
  4517. emit_changed();
  4518. }
  4519. String VisualShaderNodeVarying::get_varying_name() const {
  4520. return varying_name;
  4521. }
  4522. void VisualShaderNodeVarying::set_varying_type(VisualShader::VaryingType p_varying_type) {
  4523. ERR_FAIL_INDEX(p_varying_type, VisualShader::VARYING_TYPE_MAX);
  4524. if (varying_type == p_varying_type) {
  4525. return;
  4526. }
  4527. varying_type = p_varying_type;
  4528. emit_changed();
  4529. }
  4530. VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type() const {
  4531. return varying_type;
  4532. }
  4533. VisualShaderNodeVarying::VisualShaderNodeVarying() {
  4534. }
  4535. ////////////// Varying Setter
  4536. String VisualShaderNodeVaryingSetter::get_caption() const {
  4537. return "VaryingSetter";
  4538. }
  4539. int VisualShaderNodeVaryingSetter::get_input_port_count() const {
  4540. return 1;
  4541. }
  4542. VisualShaderNodeVaryingSetter::PortType VisualShaderNodeVaryingSetter::get_input_port_type(int p_port) const {
  4543. return get_port_type(varying_type, p_port);
  4544. }
  4545. String VisualShaderNodeVaryingSetter::get_input_port_name(int p_port) const {
  4546. return "";
  4547. }
  4548. int VisualShaderNodeVaryingSetter::get_output_port_count() const {
  4549. return 0;
  4550. }
  4551. VisualShaderNodeVaryingSetter::PortType VisualShaderNodeVaryingSetter::get_output_port_type(int p_port) const {
  4552. return PORT_TYPE_SCALAR;
  4553. }
  4554. String VisualShaderNodeVaryingSetter::get_output_port_name(int p_port) const {
  4555. return "";
  4556. }
  4557. String VisualShaderNodeVaryingSetter::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 {
  4558. String code;
  4559. if (varying_name == "[None]") {
  4560. return code;
  4561. }
  4562. code += vformat(" var_%s = %s;\n", varying_name, p_input_vars[0]);
  4563. return code;
  4564. }
  4565. VisualShaderNodeVaryingSetter::VisualShaderNodeVaryingSetter() {
  4566. }
  4567. ////////////// Varying Getter
  4568. String VisualShaderNodeVaryingGetter::get_caption() const {
  4569. return "VaryingGetter";
  4570. }
  4571. int VisualShaderNodeVaryingGetter::get_input_port_count() const {
  4572. return 0;
  4573. }
  4574. VisualShaderNodeVaryingGetter::PortType VisualShaderNodeVaryingGetter::get_input_port_type(int p_port) const {
  4575. return PORT_TYPE_SCALAR;
  4576. }
  4577. String VisualShaderNodeVaryingGetter::get_input_port_name(int p_port) const {
  4578. return "";
  4579. }
  4580. int VisualShaderNodeVaryingGetter::get_output_port_count() const {
  4581. return 1;
  4582. }
  4583. VisualShaderNodeVaryingGetter::PortType VisualShaderNodeVaryingGetter::get_output_port_type(int p_port) const {
  4584. return get_port_type(varying_type, p_port);
  4585. }
  4586. String VisualShaderNodeVaryingGetter::get_output_port_name(int p_port) const {
  4587. return "";
  4588. }
  4589. bool VisualShaderNodeVaryingGetter::has_output_port_preview(int p_port) const {
  4590. return false;
  4591. }
  4592. String VisualShaderNodeVaryingGetter::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 {
  4593. String from = varying_name;
  4594. String from2;
  4595. if (varying_name == "[None]" || p_for_preview) {
  4596. switch (varying_type) {
  4597. case VisualShader::VARYING_TYPE_FLOAT:
  4598. from = "0.0";
  4599. break;
  4600. case VisualShader::VARYING_TYPE_INT:
  4601. from = "0";
  4602. break;
  4603. case VisualShader::VARYING_TYPE_UINT:
  4604. from = "0u";
  4605. break;
  4606. case VisualShader::VARYING_TYPE_VECTOR_2D:
  4607. from = "vec2(0.0)";
  4608. break;
  4609. case VisualShader::VARYING_TYPE_VECTOR_3D:
  4610. from = "vec3(0.0)";
  4611. break;
  4612. case VisualShader::VARYING_TYPE_VECTOR_4D:
  4613. from = "vec4(0.0)";
  4614. break;
  4615. case VisualShader::VARYING_TYPE_BOOLEAN:
  4616. from = "false";
  4617. break;
  4618. case VisualShader::VARYING_TYPE_TRANSFORM:
  4619. from = "mat4(1.0)";
  4620. break;
  4621. default:
  4622. break;
  4623. }
  4624. return vformat(" %s = %s;\n", p_output_vars[0], from);
  4625. }
  4626. return vformat(" %s = var_%s;\n", p_output_vars[0], from);
  4627. }
  4628. VisualShaderNodeVaryingGetter::VisualShaderNodeVaryingGetter() {
  4629. varying_name = "[None]";
  4630. }